sys_i386.c 6.1 KB

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