syscall.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  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) &&
  86. ((addr - (pgoff << PAGE_SHIFT)) & shm_align_mask))
  87. return -EINVAL;
  88. return addr;
  89. }
  90. do_color_align = 0;
  91. if (filp || (flags & MAP_SHARED))
  92. do_color_align = 1;
  93. if (addr) {
  94. if (do_color_align)
  95. addr = COLOUR_ALIGN(addr, pgoff);
  96. else
  97. addr = PAGE_ALIGN(addr);
  98. vmm = find_vma(current->mm, addr);
  99. if (task_size - len >= addr &&
  100. (!vmm || addr + len <= vmm->vm_start))
  101. return addr;
  102. }
  103. addr = TASK_UNMAPPED_BASE;
  104. if (do_color_align)
  105. addr = COLOUR_ALIGN(addr, pgoff);
  106. else
  107. addr = PAGE_ALIGN(addr);
  108. for (vmm = find_vma(current->mm, addr); ; vmm = vmm->vm_next) {
  109. /* At this point: (!vmm || addr < vmm->vm_end). */
  110. if (task_size - len < addr)
  111. return -ENOMEM;
  112. if (!vmm || addr + len <= vmm->vm_start)
  113. return addr;
  114. addr = vmm->vm_end;
  115. if (do_color_align)
  116. addr = COLOUR_ALIGN(addr, pgoff);
  117. }
  118. }
  119. SYSCALL_DEFINE6(mips_mmap, unsigned long, addr, unsigned long, len,
  120. unsigned long, prot, unsigned long, flags, unsigned long,
  121. fd, off_t, offset)
  122. {
  123. unsigned long result;
  124. result = -EINVAL;
  125. if (offset & ~PAGE_MASK)
  126. goto out;
  127. result = sys_mmap_pgoff(addr, len, prot, flags, fd, offset >> PAGE_SHIFT);
  128. out:
  129. return result;
  130. }
  131. SYSCALL_DEFINE6(mips_mmap2, unsigned long, addr, unsigned long, len,
  132. unsigned long, prot, unsigned long, flags, unsigned long, fd,
  133. unsigned long, pgoff)
  134. {
  135. if (pgoff & (~PAGE_MASK >> 12))
  136. return -EINVAL;
  137. return sys_mmap_pgoff(addr, len, prot, flags, fd, pgoff >> (PAGE_SHIFT-12));
  138. }
  139. save_static_function(sys_fork);
  140. static int __used noinline
  141. _sys_fork(nabi_no_regargs struct pt_regs regs)
  142. {
  143. return do_fork(SIGCHLD, regs.regs[29], &regs, 0, NULL, NULL);
  144. }
  145. save_static_function(sys_clone);
  146. static int __used noinline
  147. _sys_clone(nabi_no_regargs struct pt_regs regs)
  148. {
  149. unsigned long clone_flags;
  150. unsigned long newsp;
  151. int __user *parent_tidptr, *child_tidptr;
  152. clone_flags = regs.regs[4];
  153. newsp = regs.regs[5];
  154. if (!newsp)
  155. newsp = regs.regs[29];
  156. parent_tidptr = (int __user *) regs.regs[6];
  157. #ifdef CONFIG_32BIT
  158. /* We need to fetch the fifth argument off the stack. */
  159. child_tidptr = NULL;
  160. if (clone_flags & (CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID)) {
  161. int __user *__user *usp = (int __user *__user *) regs.regs[29];
  162. if (regs.regs[2] == __NR_syscall) {
  163. if (get_user (child_tidptr, &usp[5]))
  164. return -EFAULT;
  165. }
  166. else if (get_user (child_tidptr, &usp[4]))
  167. return -EFAULT;
  168. }
  169. #else
  170. child_tidptr = (int __user *) regs.regs[8];
  171. #endif
  172. return do_fork(clone_flags, newsp, &regs, 0,
  173. parent_tidptr, child_tidptr);
  174. }
  175. /*
  176. * sys_execve() executes a new program.
  177. */
  178. asmlinkage int sys_execve(nabi_no_regargs struct pt_regs regs)
  179. {
  180. int error;
  181. char * filename;
  182. filename = getname((char __user *) (long)regs.regs[4]);
  183. error = PTR_ERR(filename);
  184. if (IS_ERR(filename))
  185. goto out;
  186. error = do_execve(filename, (char __user *__user *) (long)regs.regs[5],
  187. (char __user *__user *) (long)regs.regs[6], &regs);
  188. putname(filename);
  189. out:
  190. return error;
  191. }
  192. /*
  193. * Compacrapability ...
  194. */
  195. SYSCALL_DEFINE1(uname, struct old_utsname __user *, name)
  196. {
  197. if (name && !copy_to_user(name, utsname(), sizeof (*name)))
  198. return 0;
  199. return -EFAULT;
  200. }
  201. /*
  202. * Compacrapability ...
  203. */
  204. SYSCALL_DEFINE1(olduname, struct oldold_utsname __user *, name)
  205. {
  206. int error;
  207. if (!name)
  208. return -EFAULT;
  209. if (!access_ok(VERIFY_WRITE, name, sizeof(struct oldold_utsname)))
  210. return -EFAULT;
  211. error = __copy_to_user(&name->sysname, &utsname()->sysname,
  212. __OLD_UTS_LEN);
  213. error -= __put_user(0, name->sysname + __OLD_UTS_LEN);
  214. error -= __copy_to_user(&name->nodename, &utsname()->nodename,
  215. __OLD_UTS_LEN);
  216. error -= __put_user(0, name->nodename + __OLD_UTS_LEN);
  217. error -= __copy_to_user(&name->release, &utsname()->release,
  218. __OLD_UTS_LEN);
  219. error -= __put_user(0, name->release + __OLD_UTS_LEN);
  220. error -= __copy_to_user(&name->version, &utsname()->version,
  221. __OLD_UTS_LEN);
  222. error -= __put_user(0, name->version + __OLD_UTS_LEN);
  223. error -= __copy_to_user(&name->machine, &utsname()->machine,
  224. __OLD_UTS_LEN);
  225. error = __put_user(0, name->machine + __OLD_UTS_LEN);
  226. error = error ? -EFAULT : 0;
  227. return error;
  228. }
  229. SYSCALL_DEFINE1(set_thread_area, unsigned long, addr)
  230. {
  231. struct thread_info *ti = task_thread_info(current);
  232. ti->tp_value = addr;
  233. if (cpu_has_userlocal)
  234. write_c0_userlocal(addr);
  235. return 0;
  236. }
  237. static inline int mips_atomic_set(struct pt_regs *regs,
  238. unsigned long addr, unsigned long new)
  239. {
  240. unsigned long old, tmp;
  241. unsigned int err;
  242. if (unlikely(addr & 3))
  243. return -EINVAL;
  244. if (unlikely(!access_ok(VERIFY_WRITE, addr, 4)))
  245. return -EINVAL;
  246. if (cpu_has_llsc && R10000_LLSC_WAR) {
  247. __asm__ __volatile__ (
  248. " .set mips3 \n"
  249. " li %[err], 0 \n"
  250. "1: ll %[old], (%[addr]) \n"
  251. " move %[tmp], %[new] \n"
  252. "2: sc %[tmp], (%[addr]) \n"
  253. " beqzl %[tmp], 1b \n"
  254. "3: \n"
  255. " .section .fixup,\"ax\" \n"
  256. "4: li %[err], %[efault] \n"
  257. " j 3b \n"
  258. " .previous \n"
  259. " .section __ex_table,\"a\" \n"
  260. " "STR(PTR)" 1b, 4b \n"
  261. " "STR(PTR)" 2b, 4b \n"
  262. " .previous \n"
  263. " .set mips0 \n"
  264. : [old] "=&r" (old),
  265. [err] "=&r" (err),
  266. [tmp] "=&r" (tmp)
  267. : [addr] "r" (addr),
  268. [new] "r" (new),
  269. [efault] "i" (-EFAULT)
  270. : "memory");
  271. } else if (cpu_has_llsc) {
  272. __asm__ __volatile__ (
  273. " .set mips3 \n"
  274. " li %[err], 0 \n"
  275. "1: ll %[old], (%[addr]) \n"
  276. " move %[tmp], %[new] \n"
  277. "2: sc %[tmp], (%[addr]) \n"
  278. " bnez %[tmp], 4f \n"
  279. "3: \n"
  280. " .subsection 2 \n"
  281. "4: b 1b \n"
  282. " .previous \n"
  283. " \n"
  284. " .section .fixup,\"ax\" \n"
  285. "5: li %[err], %[efault] \n"
  286. " j 3b \n"
  287. " .previous \n"
  288. " .section __ex_table,\"a\" \n"
  289. " "STR(PTR)" 1b, 5b \n"
  290. " "STR(PTR)" 2b, 5b \n"
  291. " .previous \n"
  292. " .set mips0 \n"
  293. : [old] "=&r" (old),
  294. [err] "=&r" (err),
  295. [tmp] "=&r" (tmp)
  296. : [addr] "r" (addr),
  297. [new] "r" (new),
  298. [efault] "i" (-EFAULT)
  299. : "memory");
  300. } else {
  301. do {
  302. preempt_disable();
  303. ll_bit = 1;
  304. ll_task = current;
  305. preempt_enable();
  306. err = __get_user(old, (unsigned int *) addr);
  307. err |= __put_user(new, (unsigned int *) addr);
  308. if (err)
  309. break;
  310. rmb();
  311. } while (!ll_bit);
  312. }
  313. if (unlikely(err))
  314. return err;
  315. regs->regs[2] = old;
  316. regs->regs[7] = 0; /* No error */
  317. /*
  318. * Don't let your children do this ...
  319. */
  320. __asm__ __volatile__(
  321. " move $29, %0 \n"
  322. " j syscall_exit \n"
  323. : /* no outputs */
  324. : "r" (regs));
  325. /* unreached. Honestly. */
  326. while (1);
  327. }
  328. save_static_function(sys_sysmips);
  329. static int __used noinline
  330. _sys_sysmips(nabi_no_regargs struct pt_regs regs)
  331. {
  332. long cmd, arg1, arg2, arg3;
  333. cmd = regs.regs[4];
  334. arg1 = regs.regs[5];
  335. arg2 = regs.regs[6];
  336. arg3 = regs.regs[7];
  337. switch (cmd) {
  338. case MIPS_ATOMIC_SET:
  339. return mips_atomic_set(&regs, arg1, arg2);
  340. case MIPS_FIXADE:
  341. if (arg1 & ~3)
  342. return -EINVAL;
  343. if (arg1 & 1)
  344. set_thread_flag(TIF_FIXADE);
  345. else
  346. clear_thread_flag(TIF_FIXADE);
  347. if (arg1 & 2)
  348. set_thread_flag(TIF_LOGADE);
  349. else
  350. clear_thread_flag(TIF_FIXADE);
  351. return 0;
  352. case FLUSH_CACHE:
  353. __flush_cache_all();
  354. return 0;
  355. }
  356. return -EINVAL;
  357. }
  358. /*
  359. * sys_ipc() is the de-multiplexer for the SysV IPC calls..
  360. *
  361. * This is really horribly ugly.
  362. */
  363. SYSCALL_DEFINE6(ipc, unsigned int, call, int, first, int, second,
  364. unsigned long, third, void __user *, ptr, long, fifth)
  365. {
  366. int version, ret;
  367. version = call >> 16; /* hack for backward compatibility */
  368. call &= 0xffff;
  369. switch (call) {
  370. case SEMOP:
  371. return sys_semtimedop(first, (struct sembuf __user *)ptr,
  372. second, NULL);
  373. case SEMTIMEDOP:
  374. return sys_semtimedop(first, (struct sembuf __user *)ptr,
  375. second,
  376. (const struct timespec __user *)fifth);
  377. case SEMGET:
  378. return sys_semget(first, second, third);
  379. case SEMCTL: {
  380. union semun fourth;
  381. if (!ptr)
  382. return -EINVAL;
  383. if (get_user(fourth.__pad, (void __user *__user *) ptr))
  384. return -EFAULT;
  385. return sys_semctl(first, second, third, fourth);
  386. }
  387. case MSGSND:
  388. return sys_msgsnd(first, (struct msgbuf __user *) ptr,
  389. second, third);
  390. case MSGRCV:
  391. switch (version) {
  392. case 0: {
  393. struct ipc_kludge tmp;
  394. if (!ptr)
  395. return -EINVAL;
  396. if (copy_from_user(&tmp,
  397. (struct ipc_kludge __user *) ptr,
  398. sizeof(tmp)))
  399. return -EFAULT;
  400. return sys_msgrcv(first, tmp.msgp, second,
  401. tmp.msgtyp, third);
  402. }
  403. default:
  404. return sys_msgrcv(first,
  405. (struct msgbuf __user *) ptr,
  406. second, fifth, third);
  407. }
  408. case MSGGET:
  409. return sys_msgget((key_t) first, second);
  410. case MSGCTL:
  411. return sys_msgctl(first, second,
  412. (struct msqid_ds __user *) ptr);
  413. case SHMAT:
  414. switch (version) {
  415. default: {
  416. unsigned long raddr;
  417. ret = do_shmat(first, (char __user *) ptr, second,
  418. &raddr);
  419. if (ret)
  420. return ret;
  421. return put_user(raddr, (unsigned long __user *) third);
  422. }
  423. case 1: /* iBCS2 emulator entry point */
  424. if (!segment_eq(get_fs(), get_ds()))
  425. return -EINVAL;
  426. return do_shmat(first, (char __user *) ptr, second,
  427. (unsigned long *) third);
  428. }
  429. case SHMDT:
  430. return sys_shmdt((char __user *)ptr);
  431. case SHMGET:
  432. return sys_shmget(first, second, third);
  433. case SHMCTL:
  434. return sys_shmctl(first, second,
  435. (struct shmid_ds __user *) ptr);
  436. default:
  437. return -ENOSYS;
  438. }
  439. }
  440. /*
  441. * No implemented yet ...
  442. */
  443. SYSCALL_DEFINE3(cachectl, char *, addr, int, nbytes, int, op)
  444. {
  445. return -ENOSYS;
  446. }
  447. /*
  448. * If we ever come here the user sp is bad. Zap the process right away.
  449. * Due to the bad stack signaling wouldn't work.
  450. */
  451. asmlinkage void bad_stack(void)
  452. {
  453. do_exit(SIGSEGV);
  454. }
  455. /*
  456. * Do a system call from kernel instead of calling sys_execve so we
  457. * end up with proper pt_regs.
  458. */
  459. int kernel_execve(const char *filename, char *const argv[], char *const envp[])
  460. {
  461. register unsigned long __a0 asm("$4") = (unsigned long) filename;
  462. register unsigned long __a1 asm("$5") = (unsigned long) argv;
  463. register unsigned long __a2 asm("$6") = (unsigned long) envp;
  464. register unsigned long __a3 asm("$7");
  465. unsigned long __v0;
  466. __asm__ volatile (" \n"
  467. " .set noreorder \n"
  468. " li $2, %5 # __NR_execve \n"
  469. " syscall \n"
  470. " move %0, $2 \n"
  471. " .set reorder \n"
  472. : "=&r" (__v0), "=r" (__a3)
  473. : "r" (__a0), "r" (__a1), "r" (__a2), "i" (__NR_execve)
  474. : "$2", "$8", "$9", "$10", "$11", "$12", "$13", "$14", "$15", "$24",
  475. "memory");
  476. if (__a3 == 0)
  477. return __v0;
  478. return -__v0;
  479. }