sys_i386.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. * linux/arch/i386/kernel/sys_i386.c
  3. *
  4. * This file contains various random system calls that
  5. * have a non-standard calling sequence on the Linux/i386
  6. * platform.
  7. */
  8. #include <linux/errno.h>
  9. #include <linux/sched.h>
  10. #include <linux/mm.h>
  11. #include <linux/fs.h>
  12. #include <linux/smp.h>
  13. #include <linux/sem.h>
  14. #include <linux/msg.h>
  15. #include <linux/shm.h>
  16. #include <linux/stat.h>
  17. #include <linux/syscalls.h>
  18. #include <linux/mman.h>
  19. #include <linux/file.h>
  20. #include <linux/utsname.h>
  21. #include <asm/uaccess.h>
  22. #include <asm/unistd.h>
  23. #include <asm/ipc.h>
  24. /*
  25. * sys_pipe() is the normal C calling standard for creating
  26. * a pipe. It's not the way Unix traditionally does this, though.
  27. */
  28. asmlinkage int sys_pipe(unsigned long __user * fildes)
  29. {
  30. int fd[2];
  31. int error;
  32. error = do_pipe(fd);
  33. if (!error) {
  34. if (copy_to_user(fildes, fd, 2*sizeof(int)))
  35. error = -EFAULT;
  36. }
  37. return error;
  38. }
  39. asmlinkage long sys_mmap2(unsigned long addr, unsigned long len,
  40. unsigned long prot, unsigned long flags,
  41. unsigned long fd, unsigned long pgoff)
  42. {
  43. int error = -EBADF;
  44. struct file *file = NULL;
  45. struct mm_struct *mm = current->mm;
  46. flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
  47. if (!(flags & MAP_ANONYMOUS)) {
  48. file = fget(fd);
  49. if (!file)
  50. goto out;
  51. }
  52. down_write(&mm->mmap_sem);
  53. error = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
  54. up_write(&mm->mmap_sem);
  55. if (file)
  56. fput(file);
  57. out:
  58. return error;
  59. }
  60. /*
  61. * Perform the select(nd, in, out, ex, tv) and mmap() system
  62. * calls. Linux/i386 didn't use to be able to handle more than
  63. * 4 system call parameters, so these system calls used a memory
  64. * block for parameter passing..
  65. */
  66. struct mmap_arg_struct {
  67. unsigned long addr;
  68. unsigned long len;
  69. unsigned long prot;
  70. unsigned long flags;
  71. unsigned long fd;
  72. unsigned long offset;
  73. };
  74. asmlinkage int old_mmap(struct mmap_arg_struct __user *arg)
  75. {
  76. struct mmap_arg_struct a;
  77. int err = -EFAULT;
  78. if (copy_from_user(&a, arg, sizeof(a)))
  79. goto out;
  80. err = -EINVAL;
  81. if (a.offset & ~PAGE_MASK)
  82. goto out;
  83. err = sys_mmap2(a.addr, a.len, a.prot, a.flags,
  84. a.fd, a.offset >> PAGE_SHIFT);
  85. out:
  86. return err;
  87. }
  88. struct sel_arg_struct {
  89. unsigned long n;
  90. fd_set __user *inp, *outp, *exp;
  91. struct timeval __user *tvp;
  92. };
  93. asmlinkage int old_select(struct sel_arg_struct __user *arg)
  94. {
  95. struct sel_arg_struct a;
  96. if (copy_from_user(&a, arg, sizeof(a)))
  97. return -EFAULT;
  98. /* sys_select() does the appropriate kernel locking */
  99. return sys_select(a.n, a.inp, a.outp, a.exp, a.tvp);
  100. }
  101. /*
  102. * sys_ipc() is the de-multiplexer for the SysV IPC calls..
  103. *
  104. * This is really horribly ugly.
  105. */
  106. asmlinkage int sys_ipc (uint call, int first, int second,
  107. int third, void __user *ptr, long fifth)
  108. {
  109. int version, ret;
  110. version = call >> 16; /* hack for backward compatibility */
  111. call &= 0xffff;
  112. switch (call) {
  113. case SEMOP:
  114. return sys_semtimedop (first, (struct sembuf __user *)ptr, second, NULL);
  115. case SEMTIMEDOP:
  116. return sys_semtimedop(first, (struct sembuf __user *)ptr, second,
  117. (const struct timespec __user *)fifth);
  118. case SEMGET:
  119. return sys_semget (first, second, third);
  120. case SEMCTL: {
  121. union semun fourth;
  122. if (!ptr)
  123. return -EINVAL;
  124. if (get_user(fourth.__pad, (void __user * __user *) ptr))
  125. return -EFAULT;
  126. return sys_semctl (first, second, third, fourth);
  127. }
  128. case MSGSND:
  129. return sys_msgsnd (first, (struct msgbuf __user *) ptr,
  130. second, third);
  131. case MSGRCV:
  132. switch (version) {
  133. case 0: {
  134. struct ipc_kludge tmp;
  135. if (!ptr)
  136. return -EINVAL;
  137. if (copy_from_user(&tmp,
  138. (struct ipc_kludge __user *) ptr,
  139. sizeof (tmp)))
  140. return -EFAULT;
  141. return sys_msgrcv (first, tmp.msgp, second,
  142. tmp.msgtyp, third);
  143. }
  144. default:
  145. return sys_msgrcv (first,
  146. (struct msgbuf __user *) ptr,
  147. second, fifth, third);
  148. }
  149. case MSGGET:
  150. return sys_msgget ((key_t) first, second);
  151. case MSGCTL:
  152. return sys_msgctl (first, second, (struct msqid_ds __user *) ptr);
  153. case SHMAT:
  154. switch (version) {
  155. default: {
  156. ulong raddr;
  157. ret = do_shmat (first, (char __user *) ptr, second, &raddr);
  158. if (ret)
  159. return ret;
  160. return put_user (raddr, (ulong __user *) third);
  161. }
  162. case 1: /* iBCS2 emulator entry point */
  163. if (!segment_eq(get_fs(), get_ds()))
  164. return -EINVAL;
  165. /* The "(ulong *) third" is valid _only_ because of the kernel segment thing */
  166. return do_shmat (first, (char __user *) ptr, second, (ulong *) third);
  167. }
  168. case SHMDT:
  169. return sys_shmdt ((char __user *)ptr);
  170. case SHMGET:
  171. return sys_shmget (first, second, third);
  172. case SHMCTL:
  173. return sys_shmctl (first, second,
  174. (struct shmid_ds __user *) ptr);
  175. default:
  176. return -ENOSYS;
  177. }
  178. }
  179. /*
  180. * Old cruft
  181. */
  182. asmlinkage int sys_uname(struct old_utsname __user * name)
  183. {
  184. int err;
  185. if (!name)
  186. return -EFAULT;
  187. down_read(&uts_sem);
  188. err = copy_to_user(name, utsname(), sizeof (*name));
  189. up_read(&uts_sem);
  190. return err?-EFAULT:0;
  191. }
  192. asmlinkage int sys_olduname(struct oldold_utsname __user * name)
  193. {
  194. int error;
  195. if (!name)
  196. return -EFAULT;
  197. if (!access_ok(VERIFY_WRITE,name,sizeof(struct oldold_utsname)))
  198. return -EFAULT;
  199. down_read(&uts_sem);
  200. error = __copy_to_user(&name->sysname, &utsname()->sysname,
  201. __OLD_UTS_LEN);
  202. error |= __put_user(0, name->sysname + __OLD_UTS_LEN);
  203. error |= __copy_to_user(&name->nodename, &utsname()->nodename,
  204. __OLD_UTS_LEN);
  205. error |= __put_user(0, name->nodename + __OLD_UTS_LEN);
  206. error |= __copy_to_user(&name->release, &utsname()->release,
  207. __OLD_UTS_LEN);
  208. error |= __put_user(0, name->release + __OLD_UTS_LEN);
  209. error |= __copy_to_user(&name->version, &utsname()->version,
  210. __OLD_UTS_LEN);
  211. error |= __put_user(0, name->version + __OLD_UTS_LEN);
  212. error |= __copy_to_user(&name->machine, &utsname()->machine,
  213. __OLD_UTS_LEN);
  214. error |= __put_user(0, name->machine + __OLD_UTS_LEN);
  215. up_read(&uts_sem);
  216. error = error ? -EFAULT : 0;
  217. return error;
  218. }
  219. /*
  220. * Do a system call from kernel instead of calling sys_execve so we
  221. * end up with proper pt_regs.
  222. */
  223. int kernel_execve(const char *filename, char *const argv[], char *const envp[])
  224. {
  225. long __res;
  226. asm volatile ("push %%ebx ; movl %2,%%ebx ; int $0x80 ; pop %%ebx"
  227. : "=a" (__res)
  228. : "0" (__NR_execve),"ri" (filename),"c" (argv), "d" (envp) : "memory");
  229. return __res;
  230. }