syscall.c 13 KB

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