syscall.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 1995, 1996, 1997, 2000, 2001, 05 by Ralf Baechle
  7. * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
  8. * Copyright (C) 2001 MIPS Technologies, Inc.
  9. */
  10. #include <linux/capability.h>
  11. #include <linux/errno.h>
  12. #include <linux/linkage.h>
  13. #include <linux/mm.h>
  14. #include <linux/fs.h>
  15. #include <linux/smp.h>
  16. #include <linux/mman.h>
  17. #include <linux/ptrace.h>
  18. #include <linux/sched.h>
  19. #include <linux/string.h>
  20. #include <linux/syscalls.h>
  21. #include <linux/file.h>
  22. #include <linux/slab.h>
  23. #include <linux/utsname.h>
  24. #include <linux/unistd.h>
  25. #include <linux/sem.h>
  26. #include <linux/msg.h>
  27. #include <linux/shm.h>
  28. #include <linux/compiler.h>
  29. #include <linux/module.h>
  30. #include <linux/ipc.h>
  31. #include <linux/uaccess.h>
  32. #include <asm/asm.h>
  33. #include <asm/branch.h>
  34. #include <asm/cachectl.h>
  35. #include <asm/cacheflush.h>
  36. #include <asm/asm-offsets.h>
  37. #include <asm/signal.h>
  38. #include <asm/sim.h>
  39. #include <asm/shmparam.h>
  40. #include <asm/sysmips.h>
  41. #include <asm/uaccess.h>
  42. /*
  43. * For historic reasons the pipe(2) syscall on MIPS has an unusual calling
  44. * convention. It returns results in registers $v0 / $v1 which means there
  45. * is no need for it to do verify the validity of a userspace pointer
  46. * argument. Historically that used to be expensive in Linux. These days
  47. * the performance advantage is negligible.
  48. */
  49. asmlinkage int sysm_pipe(nabi_no_regargs volatile struct pt_regs regs)
  50. {
  51. int fd[2];
  52. int error, res;
  53. error = do_pipe_flags(fd, 0);
  54. if (error) {
  55. res = error;
  56. goto out;
  57. }
  58. regs.regs[3] = fd[1];
  59. res = fd[0];
  60. out:
  61. return res;
  62. }
  63. unsigned long shm_align_mask = PAGE_SIZE - 1; /* Sane caches */
  64. EXPORT_SYMBOL(shm_align_mask);
  65. #define COLOUR_ALIGN(addr,pgoff) \
  66. ((((addr) + shm_align_mask) & ~shm_align_mask) + \
  67. (((pgoff) << PAGE_SHIFT) & shm_align_mask))
  68. unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr,
  69. unsigned long len, unsigned long pgoff, unsigned long flags)
  70. {
  71. struct vm_area_struct * vmm;
  72. int do_color_align;
  73. unsigned long task_size;
  74. task_size = STACK_TOP;
  75. if (len > task_size)
  76. return -ENOMEM;
  77. if (flags & MAP_FIXED) {
  78. /* Even MAP_FIXED mappings must reside within task_size. */
  79. if (task_size - len < addr)
  80. return -EINVAL;
  81. /*
  82. * We do not accept a shared mapping if it would violate
  83. * cache aliasing constraints.
  84. */
  85. if ((flags & MAP_SHARED) && (addr & shm_align_mask))
  86. return -EINVAL;
  87. return addr;
  88. }
  89. do_color_align = 0;
  90. if (filp || (flags & MAP_SHARED))
  91. do_color_align = 1;
  92. if (addr) {
  93. if (do_color_align)
  94. addr = COLOUR_ALIGN(addr, pgoff);
  95. else
  96. addr = PAGE_ALIGN(addr);
  97. vmm = find_vma(current->mm, addr);
  98. if (task_size - len >= addr &&
  99. (!vmm || addr + len <= vmm->vm_start))
  100. return addr;
  101. }
  102. addr = TASK_UNMAPPED_BASE;
  103. if (do_color_align)
  104. addr = COLOUR_ALIGN(addr, pgoff);
  105. else
  106. addr = PAGE_ALIGN(addr);
  107. for (vmm = find_vma(current->mm, addr); ; vmm = vmm->vm_next) {
  108. /* At this point: (!vmm || addr < vmm->vm_end). */
  109. if (task_size - len < addr)
  110. return -ENOMEM;
  111. if (!vmm || addr + len <= vmm->vm_start)
  112. return addr;
  113. addr = vmm->vm_end;
  114. if (do_color_align)
  115. addr = COLOUR_ALIGN(addr, pgoff);
  116. }
  117. }
  118. SYSCALL_DEFINE6(mips_mmap, unsigned long, addr, unsigned long, len,
  119. unsigned long, prot, unsigned long, flags, unsigned long,
  120. fd, off_t, offset)
  121. {
  122. unsigned long result;
  123. result = -EINVAL;
  124. if (offset & ~PAGE_MASK)
  125. goto out;
  126. result = sys_mmap_pgoff(addr, len, prot, flags, fd, offset >> PAGE_SHIFT);
  127. out:
  128. return result;
  129. }
  130. SYSCALL_DEFINE6(mips_mmap2, unsigned long, addr, unsigned long, len,
  131. unsigned long, prot, unsigned long, flags, unsigned long, fd,
  132. unsigned long, pgoff)
  133. {
  134. if (pgoff & (~PAGE_MASK >> 12))
  135. return -EINVAL;
  136. return sys_mmap_pgoff(addr, len, prot, flags, fd, pgoff >> (PAGE_SHIFT-12));
  137. }
  138. save_static_function(sys_fork);
  139. static int __used noinline
  140. _sys_fork(nabi_no_regargs struct pt_regs regs)
  141. {
  142. return do_fork(SIGCHLD, regs.regs[29], &regs, 0, NULL, NULL);
  143. }
  144. save_static_function(sys_clone);
  145. static int __used noinline
  146. _sys_clone(nabi_no_regargs struct pt_regs regs)
  147. {
  148. unsigned long clone_flags;
  149. unsigned long newsp;
  150. int __user *parent_tidptr, *child_tidptr;
  151. clone_flags = regs.regs[4];
  152. newsp = regs.regs[5];
  153. if (!newsp)
  154. newsp = regs.regs[29];
  155. parent_tidptr = (int __user *) regs.regs[6];
  156. #ifdef CONFIG_32BIT
  157. /* We need to fetch the fifth argument off the stack. */
  158. child_tidptr = NULL;
  159. if (clone_flags & (CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID)) {
  160. int __user *__user *usp = (int __user *__user *) regs.regs[29];
  161. if (regs.regs[2] == __NR_syscall) {
  162. if (get_user (child_tidptr, &usp[5]))
  163. return -EFAULT;
  164. }
  165. else if (get_user (child_tidptr, &usp[4]))
  166. return -EFAULT;
  167. }
  168. #else
  169. child_tidptr = (int __user *) regs.regs[8];
  170. #endif
  171. return do_fork(clone_flags, newsp, &regs, 0,
  172. parent_tidptr, child_tidptr);
  173. }
  174. /*
  175. * sys_execve() executes a new program.
  176. */
  177. asmlinkage int sys_execve(nabi_no_regargs struct pt_regs regs)
  178. {
  179. int error;
  180. char * filename;
  181. filename = getname((char __user *) (long)regs.regs[4]);
  182. error = PTR_ERR(filename);
  183. if (IS_ERR(filename))
  184. goto out;
  185. error = do_execve(filename, (char __user *__user *) (long)regs.regs[5],
  186. (char __user *__user *) (long)regs.regs[6], &regs);
  187. putname(filename);
  188. out:
  189. return error;
  190. }
  191. /*
  192. * Compacrapability ...
  193. */
  194. SYSCALL_DEFINE1(uname, struct old_utsname __user *, name)
  195. {
  196. if (name && !copy_to_user(name, utsname(), sizeof (*name)))
  197. return 0;
  198. return -EFAULT;
  199. }
  200. /*
  201. * Compacrapability ...
  202. */
  203. SYSCALL_DEFINE1(olduname, struct oldold_utsname __user *, name)
  204. {
  205. int error;
  206. if (!name)
  207. return -EFAULT;
  208. if (!access_ok(VERIFY_WRITE, name, sizeof(struct oldold_utsname)))
  209. return -EFAULT;
  210. error = __copy_to_user(&name->sysname, &utsname()->sysname,
  211. __OLD_UTS_LEN);
  212. error -= __put_user(0, name->sysname + __OLD_UTS_LEN);
  213. error -= __copy_to_user(&name->nodename, &utsname()->nodename,
  214. __OLD_UTS_LEN);
  215. error -= __put_user(0, name->nodename + __OLD_UTS_LEN);
  216. error -= __copy_to_user(&name->release, &utsname()->release,
  217. __OLD_UTS_LEN);
  218. error -= __put_user(0, name->release + __OLD_UTS_LEN);
  219. error -= __copy_to_user(&name->version, &utsname()->version,
  220. __OLD_UTS_LEN);
  221. error -= __put_user(0, name->version + __OLD_UTS_LEN);
  222. error -= __copy_to_user(&name->machine, &utsname()->machine,
  223. __OLD_UTS_LEN);
  224. error = __put_user(0, name->machine + __OLD_UTS_LEN);
  225. error = error ? -EFAULT : 0;
  226. return error;
  227. }
  228. SYSCALL_DEFINE1(set_thread_area, unsigned long, addr)
  229. {
  230. struct thread_info *ti = task_thread_info(current);
  231. ti->tp_value = addr;
  232. if (cpu_has_userlocal)
  233. write_c0_userlocal(addr);
  234. return 0;
  235. }
  236. static inline int mips_atomic_set(struct pt_regs *regs,
  237. unsigned long addr, unsigned long new)
  238. {
  239. unsigned long old, tmp;
  240. unsigned int err;
  241. if (unlikely(addr & 3))
  242. return -EINVAL;
  243. if (unlikely(!access_ok(VERIFY_WRITE, addr, 4)))
  244. return -EINVAL;
  245. if (cpu_has_llsc && R10000_LLSC_WAR) {
  246. __asm__ __volatile__ (
  247. " .set mips3 \n"
  248. " li %[err], 0 \n"
  249. "1: ll %[old], (%[addr]) \n"
  250. " move %[tmp], %[new] \n"
  251. "2: sc %[tmp], (%[addr]) \n"
  252. " beqzl %[tmp], 1b \n"
  253. "3: \n"
  254. " .section .fixup,\"ax\" \n"
  255. "4: li %[err], %[efault] \n"
  256. " j 3b \n"
  257. " .previous \n"
  258. " .section __ex_table,\"a\" \n"
  259. " "STR(PTR)" 1b, 4b \n"
  260. " "STR(PTR)" 2b, 4b \n"
  261. " .previous \n"
  262. " .set mips0 \n"
  263. : [old] "=&r" (old),
  264. [err] "=&r" (err),
  265. [tmp] "=&r" (tmp)
  266. : [addr] "r" (addr),
  267. [new] "r" (new),
  268. [efault] "i" (-EFAULT)
  269. : "memory");
  270. } else if (cpu_has_llsc) {
  271. __asm__ __volatile__ (
  272. " .set mips3 \n"
  273. " li %[err], 0 \n"
  274. "1: ll %[old], (%[addr]) \n"
  275. " move %[tmp], %[new] \n"
  276. "2: sc %[tmp], (%[addr]) \n"
  277. " bnez %[tmp], 4f \n"
  278. "3: \n"
  279. " .subsection 2 \n"
  280. "4: b 1b \n"
  281. " .previous \n"
  282. " \n"
  283. " .section .fixup,\"ax\" \n"
  284. "5: li %[err], %[efault] \n"
  285. " j 3b \n"
  286. " .previous \n"
  287. " .section __ex_table,\"a\" \n"
  288. " "STR(PTR)" 1b, 5b \n"
  289. " "STR(PTR)" 2b, 5b \n"
  290. " .previous \n"
  291. " .set mips0 \n"
  292. : [old] "=&r" (old),
  293. [err] "=&r" (err),
  294. [tmp] "=&r" (tmp)
  295. : [addr] "r" (addr),
  296. [new] "r" (new),
  297. [efault] "i" (-EFAULT)
  298. : "memory");
  299. } else {
  300. do {
  301. preempt_disable();
  302. ll_bit = 1;
  303. ll_task = current;
  304. preempt_enable();
  305. err = __get_user(old, (unsigned int *) addr);
  306. err |= __put_user(new, (unsigned int *) addr);
  307. if (err)
  308. break;
  309. rmb();
  310. } while (!ll_bit);
  311. }
  312. if (unlikely(err))
  313. return err;
  314. regs->regs[2] = old;
  315. regs->regs[7] = 0; /* No error */
  316. /*
  317. * Don't let your children do this ...
  318. */
  319. __asm__ __volatile__(
  320. " move $29, %0 \n"
  321. " j syscall_exit \n"
  322. : /* no outputs */
  323. : "r" (regs));
  324. /* unreached. Honestly. */
  325. while (1);
  326. }
  327. save_static_function(sys_sysmips);
  328. static int __used noinline
  329. _sys_sysmips(nabi_no_regargs struct pt_regs regs)
  330. {
  331. long cmd, arg1, arg2, arg3;
  332. cmd = regs.regs[4];
  333. arg1 = regs.regs[5];
  334. arg2 = regs.regs[6];
  335. arg3 = regs.regs[7];
  336. switch (cmd) {
  337. case MIPS_ATOMIC_SET:
  338. return mips_atomic_set(&regs, arg1, arg2);
  339. case MIPS_FIXADE:
  340. if (arg1 & ~3)
  341. return -EINVAL;
  342. if (arg1 & 1)
  343. set_thread_flag(TIF_FIXADE);
  344. else
  345. clear_thread_flag(TIF_FIXADE);
  346. if (arg1 & 2)
  347. set_thread_flag(TIF_LOGADE);
  348. else
  349. clear_thread_flag(TIF_FIXADE);
  350. return 0;
  351. case FLUSH_CACHE:
  352. __flush_cache_all();
  353. return 0;
  354. }
  355. return -EINVAL;
  356. }
  357. /*
  358. * sys_ipc() is the de-multiplexer for the SysV IPC calls..
  359. *
  360. * This is really horribly ugly.
  361. */
  362. SYSCALL_DEFINE6(ipc, unsigned int, call, int, first, int, second,
  363. unsigned long, third, void __user *, ptr, long, fifth)
  364. {
  365. int version, ret;
  366. version = call >> 16; /* hack for backward compatibility */
  367. call &= 0xffff;
  368. switch (call) {
  369. case SEMOP:
  370. return sys_semtimedop(first, (struct sembuf __user *)ptr,
  371. second, NULL);
  372. case SEMTIMEDOP:
  373. return sys_semtimedop(first, (struct sembuf __user *)ptr,
  374. second,
  375. (const struct timespec __user *)fifth);
  376. case SEMGET:
  377. return sys_semget(first, second, third);
  378. case SEMCTL: {
  379. union semun fourth;
  380. if (!ptr)
  381. return -EINVAL;
  382. if (get_user(fourth.__pad, (void __user *__user *) ptr))
  383. return -EFAULT;
  384. return sys_semctl(first, second, third, fourth);
  385. }
  386. case MSGSND:
  387. return sys_msgsnd(first, (struct msgbuf __user *) ptr,
  388. second, third);
  389. case MSGRCV:
  390. switch (version) {
  391. case 0: {
  392. struct ipc_kludge tmp;
  393. if (!ptr)
  394. return -EINVAL;
  395. if (copy_from_user(&tmp,
  396. (struct ipc_kludge __user *) ptr,
  397. sizeof(tmp)))
  398. return -EFAULT;
  399. return sys_msgrcv(first, tmp.msgp, second,
  400. tmp.msgtyp, third);
  401. }
  402. default:
  403. return sys_msgrcv(first,
  404. (struct msgbuf __user *) ptr,
  405. second, fifth, third);
  406. }
  407. case MSGGET:
  408. return sys_msgget((key_t) first, second);
  409. case MSGCTL:
  410. return sys_msgctl(first, second,
  411. (struct msqid_ds __user *) ptr);
  412. case SHMAT:
  413. switch (version) {
  414. default: {
  415. unsigned long raddr;
  416. ret = do_shmat(first, (char __user *) ptr, second,
  417. &raddr);
  418. if (ret)
  419. return ret;
  420. return put_user(raddr, (unsigned long __user *) third);
  421. }
  422. case 1: /* iBCS2 emulator entry point */
  423. if (!segment_eq(get_fs(), get_ds()))
  424. return -EINVAL;
  425. return do_shmat(first, (char __user *) ptr, second,
  426. (unsigned long *) third);
  427. }
  428. case SHMDT:
  429. return sys_shmdt((char __user *)ptr);
  430. case SHMGET:
  431. return sys_shmget(first, second, third);
  432. case SHMCTL:
  433. return sys_shmctl(first, second,
  434. (struct shmid_ds __user *) ptr);
  435. default:
  436. return -ENOSYS;
  437. }
  438. }
  439. /*
  440. * No implemented yet ...
  441. */
  442. SYSCALL_DEFINE3(cachectl, char *, addr, int, nbytes, int, op)
  443. {
  444. return -ENOSYS;
  445. }
  446. /*
  447. * If we ever come here the user sp is bad. Zap the process right away.
  448. * Due to the bad stack signaling wouldn't work.
  449. */
  450. asmlinkage void bad_stack(void)
  451. {
  452. do_exit(SIGSEGV);
  453. }
  454. /*
  455. * Do a system call from kernel instead of calling sys_execve so we
  456. * end up with proper pt_regs.
  457. */
  458. int kernel_execve(const char *filename, char *const argv[], char *const envp[])
  459. {
  460. register unsigned long __a0 asm("$4") = (unsigned long) filename;
  461. register unsigned long __a1 asm("$5") = (unsigned long) argv;
  462. register unsigned long __a2 asm("$6") = (unsigned long) envp;
  463. register unsigned long __a3 asm("$7");
  464. unsigned long __v0;
  465. __asm__ volatile (" \n"
  466. " .set noreorder \n"
  467. " li $2, %5 # __NR_execve \n"
  468. " syscall \n"
  469. " move %0, $2 \n"
  470. " .set reorder \n"
  471. : "=&r" (__v0), "=r" (__a3)
  472. : "r" (__a0), "r" (__a1), "r" (__a2), "i" (__NR_execve)
  473. : "$2", "$8", "$9", "$10", "$11", "$12", "$13", "$14", "$15", "$24",
  474. "memory");
  475. if (__a3 == 0)
  476. return __v0;
  477. return -__v0;
  478. }