sys_sparc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. /* linux/arch/sparc/kernel/sys_sparc.c
  2. *
  3. * This file contains various random system calls that
  4. * have a non-standard calling sequence on the Linux/sparc
  5. * platform.
  6. */
  7. #include <linux/errno.h>
  8. #include <linux/types.h>
  9. #include <linux/sched.h>
  10. #include <linux/mm.h>
  11. #include <linux/fs.h>
  12. #include <linux/file.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/utsname.h>
  20. #include <linux/smp.h>
  21. #include <linux/smp_lock.h>
  22. #include <linux/ipc.h>
  23. #include <asm/uaccess.h>
  24. #include <asm/unistd.h>
  25. /* #define DEBUG_UNIMP_SYSCALL */
  26. /* XXX Make this per-binary type, this way we can detect the type of
  27. * XXX a binary. Every Sparc executable calls this very early on.
  28. */
  29. asmlinkage unsigned long sys_getpagesize(void)
  30. {
  31. return PAGE_SIZE; /* Possibly older binaries want 8192 on sun4's? */
  32. }
  33. #define COLOUR_ALIGN(addr) (((addr)+SHMLBA-1)&~(SHMLBA-1))
  34. unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr, unsigned long len, unsigned long pgoff, unsigned long flags)
  35. {
  36. struct vm_area_struct * vmm;
  37. if (flags & MAP_FIXED) {
  38. /* We do not accept a shared mapping if it would violate
  39. * cache aliasing constraints.
  40. */
  41. if ((flags & MAP_SHARED) && (addr & (SHMLBA - 1)))
  42. return -EINVAL;
  43. return addr;
  44. }
  45. /* See asm-sparc/uaccess.h */
  46. if (len > TASK_SIZE - PAGE_SIZE)
  47. return -ENOMEM;
  48. if (ARCH_SUN4C_SUN4 && len > 0x20000000)
  49. return -ENOMEM;
  50. if (!addr)
  51. addr = TASK_UNMAPPED_BASE;
  52. if (flags & MAP_SHARED)
  53. addr = COLOUR_ALIGN(addr);
  54. else
  55. addr = PAGE_ALIGN(addr);
  56. for (vmm = find_vma(current->mm, addr); ; vmm = vmm->vm_next) {
  57. /* At this point: (!vmm || addr < vmm->vm_end). */
  58. if (ARCH_SUN4C_SUN4 && addr < 0xe0000000 && 0x20000000 - len < addr) {
  59. addr = PAGE_OFFSET;
  60. vmm = find_vma(current->mm, PAGE_OFFSET);
  61. }
  62. if (TASK_SIZE - PAGE_SIZE - len < addr)
  63. return -ENOMEM;
  64. if (!vmm || addr + len <= vmm->vm_start)
  65. return addr;
  66. addr = vmm->vm_end;
  67. if (flags & MAP_SHARED)
  68. addr = COLOUR_ALIGN(addr);
  69. }
  70. }
  71. asmlinkage unsigned long sparc_brk(unsigned long brk)
  72. {
  73. if(ARCH_SUN4C_SUN4) {
  74. if ((brk & 0xe0000000) != (current->mm->brk & 0xe0000000))
  75. return current->mm->brk;
  76. }
  77. return sys_brk(brk);
  78. }
  79. /*
  80. * sys_pipe() is the normal C calling standard for creating
  81. * a pipe. It's not the way unix traditionally does this, though.
  82. */
  83. asmlinkage int sparc_pipe(struct pt_regs *regs)
  84. {
  85. int fd[2];
  86. int error;
  87. error = do_pipe(fd);
  88. if (error)
  89. goto out;
  90. regs->u_regs[UREG_I1] = fd[1];
  91. error = fd[0];
  92. out:
  93. return error;
  94. }
  95. /*
  96. * sys_ipc() is the de-multiplexer for the SysV IPC calls..
  97. *
  98. * This is really horribly ugly.
  99. */
  100. asmlinkage int sys_ipc (uint call, int first, int second, int third, void __user *ptr, long fifth)
  101. {
  102. int version, err;
  103. version = call >> 16; /* hack for backward compatibility */
  104. call &= 0xffff;
  105. if (call <= SEMCTL)
  106. switch (call) {
  107. case SEMOP:
  108. err = sys_semtimedop (first, (struct sembuf __user *)ptr, second, NULL);
  109. goto out;
  110. case SEMTIMEDOP:
  111. err = sys_semtimedop (first, (struct sembuf __user *)ptr, second, (const struct timespec __user *) fifth);
  112. goto out;
  113. case SEMGET:
  114. err = sys_semget (first, second, third);
  115. goto out;
  116. case SEMCTL: {
  117. union semun fourth;
  118. err = -EINVAL;
  119. if (!ptr)
  120. goto out;
  121. err = -EFAULT;
  122. if (get_user(fourth.__pad,
  123. (void __user * __user *)ptr))
  124. goto out;
  125. err = sys_semctl (first, second, third, fourth);
  126. goto out;
  127. }
  128. default:
  129. err = -ENOSYS;
  130. goto out;
  131. }
  132. if (call <= MSGCTL)
  133. switch (call) {
  134. case MSGSND:
  135. err = sys_msgsnd (first, (struct msgbuf __user *) ptr,
  136. second, third);
  137. goto out;
  138. case MSGRCV:
  139. switch (version) {
  140. case 0: {
  141. struct ipc_kludge tmp;
  142. err = -EINVAL;
  143. if (!ptr)
  144. goto out;
  145. err = -EFAULT;
  146. if (copy_from_user(&tmp, (struct ipc_kludge __user *) ptr, sizeof (tmp)))
  147. goto out;
  148. err = sys_msgrcv (first, tmp.msgp, second, tmp.msgtyp, third);
  149. goto out;
  150. }
  151. case 1: default:
  152. err = sys_msgrcv (first,
  153. (struct msgbuf __user *) ptr,
  154. second, fifth, third);
  155. goto out;
  156. }
  157. case MSGGET:
  158. err = sys_msgget ((key_t) first, second);
  159. goto out;
  160. case MSGCTL:
  161. err = sys_msgctl (first, second, (struct msqid_ds __user *) ptr);
  162. goto out;
  163. default:
  164. err = -ENOSYS;
  165. goto out;
  166. }
  167. if (call <= SHMCTL)
  168. switch (call) {
  169. case SHMAT:
  170. switch (version) {
  171. case 0: default: {
  172. ulong raddr;
  173. err = do_shmat (first, (char __user *) ptr, second, &raddr);
  174. if (err)
  175. goto out;
  176. err = -EFAULT;
  177. if (put_user (raddr, (ulong __user *) third))
  178. goto out;
  179. err = 0;
  180. goto out;
  181. }
  182. case 1: /* iBCS2 emulator entry point */
  183. err = -EINVAL;
  184. goto out;
  185. }
  186. case SHMDT:
  187. err = sys_shmdt ((char __user *)ptr);
  188. goto out;
  189. case SHMGET:
  190. err = sys_shmget (first, second, third);
  191. goto out;
  192. case SHMCTL:
  193. err = sys_shmctl (first, second, (struct shmid_ds __user *) ptr);
  194. goto out;
  195. default:
  196. err = -ENOSYS;
  197. goto out;
  198. }
  199. else
  200. err = -ENOSYS;
  201. out:
  202. return err;
  203. }
  204. int sparc_mmap_check(unsigned long addr, unsigned long len, unsigned long flags)
  205. {
  206. if (ARCH_SUN4C_SUN4 &&
  207. (len > 0x20000000 ||
  208. (addr < 0xe0000000 && addr + len > 0x20000000)))
  209. return -EINVAL;
  210. /* See asm-sparc/uaccess.h */
  211. if (len > TASK_SIZE - PAGE_SIZE || addr + len > TASK_SIZE - PAGE_SIZE)
  212. return -EINVAL;
  213. return 0;
  214. }
  215. /* Linux version of mmap */
  216. static unsigned long do_mmap2(unsigned long addr, unsigned long len,
  217. unsigned long prot, unsigned long flags, unsigned long fd,
  218. unsigned long pgoff)
  219. {
  220. struct file * file = NULL;
  221. unsigned long retval = -EBADF;
  222. if (!(flags & MAP_ANONYMOUS)) {
  223. file = fget(fd);
  224. if (!file)
  225. goto out;
  226. }
  227. len = PAGE_ALIGN(len);
  228. flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
  229. down_write(&current->mm->mmap_sem);
  230. retval = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
  231. up_write(&current->mm->mmap_sem);
  232. if (file)
  233. fput(file);
  234. out:
  235. return retval;
  236. }
  237. asmlinkage unsigned long sys_mmap2(unsigned long addr, unsigned long len,
  238. unsigned long prot, unsigned long flags, unsigned long fd,
  239. unsigned long pgoff)
  240. {
  241. /* Make sure the shift for mmap2 is constant (12), no matter what PAGE_SIZE
  242. we have. */
  243. return do_mmap2(addr, len, prot, flags, fd, pgoff >> (PAGE_SHIFT - 12));
  244. }
  245. asmlinkage unsigned long sys_mmap(unsigned long addr, unsigned long len,
  246. unsigned long prot, unsigned long flags, unsigned long fd,
  247. unsigned long off)
  248. {
  249. return do_mmap2(addr, len, prot, flags, fd, off >> PAGE_SHIFT);
  250. }
  251. long sparc_remap_file_pages(unsigned long start, unsigned long size,
  252. unsigned long prot, unsigned long pgoff,
  253. unsigned long flags)
  254. {
  255. /* This works on an existing mmap so we don't need to validate
  256. * the range as that was done at the original mmap call.
  257. */
  258. return sys_remap_file_pages(start, size, prot,
  259. (pgoff >> (PAGE_SHIFT - 12)), flags);
  260. }
  261. extern unsigned long do_mremap(unsigned long addr,
  262. unsigned long old_len, unsigned long new_len,
  263. unsigned long flags, unsigned long new_addr);
  264. asmlinkage unsigned long sparc_mremap(unsigned long addr,
  265. unsigned long old_len, unsigned long new_len,
  266. unsigned long flags, unsigned long new_addr)
  267. {
  268. struct vm_area_struct *vma;
  269. unsigned long ret = -EINVAL;
  270. if (ARCH_SUN4C_SUN4) {
  271. if (old_len > 0x20000000 || new_len > 0x20000000)
  272. goto out;
  273. if (addr < 0xe0000000 && addr + old_len > 0x20000000)
  274. goto out;
  275. }
  276. if (old_len > TASK_SIZE - PAGE_SIZE ||
  277. new_len > TASK_SIZE - PAGE_SIZE)
  278. goto out;
  279. down_write(&current->mm->mmap_sem);
  280. if (flags & MREMAP_FIXED) {
  281. if (ARCH_SUN4C_SUN4 &&
  282. new_addr < 0xe0000000 &&
  283. new_addr + new_len > 0x20000000)
  284. goto out_sem;
  285. if (new_addr + new_len > TASK_SIZE - PAGE_SIZE)
  286. goto out_sem;
  287. } else if ((ARCH_SUN4C_SUN4 && addr < 0xe0000000 &&
  288. addr + new_len > 0x20000000) ||
  289. addr + new_len > TASK_SIZE - PAGE_SIZE) {
  290. unsigned long map_flags = 0;
  291. struct file *file = NULL;
  292. ret = -ENOMEM;
  293. if (!(flags & MREMAP_MAYMOVE))
  294. goto out_sem;
  295. vma = find_vma(current->mm, addr);
  296. if (vma) {
  297. if (vma->vm_flags & VM_SHARED)
  298. map_flags |= MAP_SHARED;
  299. file = vma->vm_file;
  300. }
  301. new_addr = get_unmapped_area(file, addr, new_len,
  302. vma ? vma->vm_pgoff : 0,
  303. map_flags);
  304. ret = new_addr;
  305. if (new_addr & ~PAGE_MASK)
  306. goto out_sem;
  307. flags |= MREMAP_FIXED;
  308. }
  309. ret = do_mremap(addr, old_len, new_len, flags, new_addr);
  310. out_sem:
  311. up_write(&current->mm->mmap_sem);
  312. out:
  313. return ret;
  314. }
  315. /* we come to here via sys_nis_syscall so it can setup the regs argument */
  316. asmlinkage unsigned long
  317. c_sys_nis_syscall (struct pt_regs *regs)
  318. {
  319. static int count = 0;
  320. if (count++ > 5)
  321. return -ENOSYS;
  322. printk ("%s[%d]: Unimplemented SPARC system call %d\n",
  323. current->comm, task_pid_nr(current), (int)regs->u_regs[1]);
  324. #ifdef DEBUG_UNIMP_SYSCALL
  325. show_regs (regs);
  326. #endif
  327. return -ENOSYS;
  328. }
  329. /* #define DEBUG_SPARC_BREAKPOINT */
  330. asmlinkage void
  331. sparc_breakpoint (struct pt_regs *regs)
  332. {
  333. siginfo_t info;
  334. lock_kernel();
  335. #ifdef DEBUG_SPARC_BREAKPOINT
  336. printk ("TRAP: Entering kernel PC=%x, nPC=%x\n", regs->pc, regs->npc);
  337. #endif
  338. info.si_signo = SIGTRAP;
  339. info.si_errno = 0;
  340. info.si_code = TRAP_BRKPT;
  341. info.si_addr = (void __user *)regs->pc;
  342. info.si_trapno = 0;
  343. force_sig_info(SIGTRAP, &info, current);
  344. #ifdef DEBUG_SPARC_BREAKPOINT
  345. printk ("TRAP: Returning to space: PC=%x nPC=%x\n", regs->pc, regs->npc);
  346. #endif
  347. unlock_kernel();
  348. }
  349. asmlinkage int
  350. sparc_sigaction (int sig, const struct old_sigaction __user *act,
  351. struct old_sigaction __user *oact)
  352. {
  353. struct k_sigaction new_ka, old_ka;
  354. int ret;
  355. WARN_ON_ONCE(sig >= 0);
  356. sig = -sig;
  357. if (act) {
  358. unsigned long mask;
  359. if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
  360. __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
  361. __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
  362. return -EFAULT;
  363. __get_user(new_ka.sa.sa_flags, &act->sa_flags);
  364. __get_user(mask, &act->sa_mask);
  365. siginitset(&new_ka.sa.sa_mask, mask);
  366. new_ka.ka_restorer = NULL;
  367. }
  368. ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
  369. if (!ret && oact) {
  370. /* In the clone() case we could copy half consistent
  371. * state to the user, however this could sleep and
  372. * deadlock us if we held the signal lock on SMP. So for
  373. * now I take the easy way out and do no locking.
  374. */
  375. if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
  376. __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
  377. __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
  378. return -EFAULT;
  379. __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
  380. __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
  381. }
  382. return ret;
  383. }
  384. asmlinkage long
  385. sys_rt_sigaction(int sig,
  386. const struct sigaction __user *act,
  387. struct sigaction __user *oact,
  388. void __user *restorer,
  389. size_t sigsetsize)
  390. {
  391. struct k_sigaction new_ka, old_ka;
  392. int ret;
  393. /* XXX: Don't preclude handling different sized sigset_t's. */
  394. if (sigsetsize != sizeof(sigset_t))
  395. return -EINVAL;
  396. if (act) {
  397. new_ka.ka_restorer = restorer;
  398. if (copy_from_user(&new_ka.sa, act, sizeof(*act)))
  399. return -EFAULT;
  400. }
  401. ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
  402. if (!ret && oact) {
  403. if (copy_to_user(oact, &old_ka.sa, sizeof(*oact)))
  404. return -EFAULT;
  405. }
  406. return ret;
  407. }
  408. asmlinkage int sys_getdomainname(char __user *name, int len)
  409. {
  410. int nlen, err;
  411. if (len < 0)
  412. return -EINVAL;
  413. down_read(&uts_sem);
  414. nlen = strlen(utsname()->domainname) + 1;
  415. err = -EINVAL;
  416. if (nlen > len)
  417. goto out;
  418. err = -EFAULT;
  419. if (!copy_to_user(name, utsname()->domainname, nlen))
  420. err = 0;
  421. out:
  422. up_read(&uts_sem);
  423. return err;
  424. }
  425. /*
  426. * Do a system call from kernel instead of calling sys_execve so we
  427. * end up with proper pt_regs.
  428. */
  429. int kernel_execve(const char *filename, char *const argv[], char *const envp[])
  430. {
  431. long __res;
  432. register long __g1 __asm__ ("g1") = __NR_execve;
  433. register long __o0 __asm__ ("o0") = (long)(filename);
  434. register long __o1 __asm__ ("o1") = (long)(argv);
  435. register long __o2 __asm__ ("o2") = (long)(envp);
  436. asm volatile ("t 0x10\n\t"
  437. "bcc 1f\n\t"
  438. "mov %%o0, %0\n\t"
  439. "sub %%g0, %%o0, %0\n\t"
  440. "1:\n\t"
  441. : "=r" (__res), "=&r" (__o0)
  442. : "1" (__o0), "r" (__o1), "r" (__o2), "r" (__g1)
  443. : "cc");
  444. return __res;
  445. }