sys_sparc_32.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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 && 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 && 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) {
  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_flags(fd, 0);
  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)
  205. {
  206. if (ARCH_SUN4C &&
  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. unsigned long ret = -EINVAL;
  269. if (unlikely(sparc_mmap_check(addr, old_len)))
  270. goto out;
  271. if (unlikely(sparc_mmap_check(new_addr, new_len)))
  272. goto out;
  273. down_write(&current->mm->mmap_sem);
  274. ret = do_mremap(addr, old_len, new_len, flags, new_addr);
  275. up_write(&current->mm->mmap_sem);
  276. out:
  277. return ret;
  278. }
  279. /* we come to here via sys_nis_syscall so it can setup the regs argument */
  280. asmlinkage unsigned long
  281. c_sys_nis_syscall (struct pt_regs *regs)
  282. {
  283. static int count = 0;
  284. if (count++ > 5)
  285. return -ENOSYS;
  286. printk ("%s[%d]: Unimplemented SPARC system call %d\n",
  287. current->comm, task_pid_nr(current), (int)regs->u_regs[1]);
  288. #ifdef DEBUG_UNIMP_SYSCALL
  289. show_regs (regs);
  290. #endif
  291. return -ENOSYS;
  292. }
  293. /* #define DEBUG_SPARC_BREAKPOINT */
  294. asmlinkage void
  295. sparc_breakpoint (struct pt_regs *regs)
  296. {
  297. siginfo_t info;
  298. lock_kernel();
  299. #ifdef DEBUG_SPARC_BREAKPOINT
  300. printk ("TRAP: Entering kernel PC=%x, nPC=%x\n", regs->pc, regs->npc);
  301. #endif
  302. info.si_signo = SIGTRAP;
  303. info.si_errno = 0;
  304. info.si_code = TRAP_BRKPT;
  305. info.si_addr = (void __user *)regs->pc;
  306. info.si_trapno = 0;
  307. force_sig_info(SIGTRAP, &info, current);
  308. #ifdef DEBUG_SPARC_BREAKPOINT
  309. printk ("TRAP: Returning to space: PC=%x nPC=%x\n", regs->pc, regs->npc);
  310. #endif
  311. unlock_kernel();
  312. }
  313. asmlinkage int
  314. sparc_sigaction (int sig, const struct old_sigaction __user *act,
  315. struct old_sigaction __user *oact)
  316. {
  317. struct k_sigaction new_ka, old_ka;
  318. int ret;
  319. WARN_ON_ONCE(sig >= 0);
  320. sig = -sig;
  321. if (act) {
  322. unsigned long mask;
  323. if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
  324. __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
  325. __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
  326. return -EFAULT;
  327. __get_user(new_ka.sa.sa_flags, &act->sa_flags);
  328. __get_user(mask, &act->sa_mask);
  329. siginitset(&new_ka.sa.sa_mask, mask);
  330. new_ka.ka_restorer = NULL;
  331. }
  332. ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
  333. if (!ret && oact) {
  334. /* In the clone() case we could copy half consistent
  335. * state to the user, however this could sleep and
  336. * deadlock us if we held the signal lock on SMP. So for
  337. * now I take the easy way out and do no locking.
  338. */
  339. if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
  340. __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
  341. __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
  342. return -EFAULT;
  343. __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
  344. __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
  345. }
  346. return ret;
  347. }
  348. asmlinkage long
  349. sys_rt_sigaction(int sig,
  350. const struct sigaction __user *act,
  351. struct sigaction __user *oact,
  352. void __user *restorer,
  353. size_t sigsetsize)
  354. {
  355. struct k_sigaction new_ka, old_ka;
  356. int ret;
  357. /* XXX: Don't preclude handling different sized sigset_t's. */
  358. if (sigsetsize != sizeof(sigset_t))
  359. return -EINVAL;
  360. if (act) {
  361. new_ka.ka_restorer = restorer;
  362. if (copy_from_user(&new_ka.sa, act, sizeof(*act)))
  363. return -EFAULT;
  364. }
  365. ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
  366. if (!ret && oact) {
  367. if (copy_to_user(oact, &old_ka.sa, sizeof(*oact)))
  368. return -EFAULT;
  369. }
  370. return ret;
  371. }
  372. asmlinkage int sys_getdomainname(char __user *name, int len)
  373. {
  374. int nlen, err;
  375. if (len < 0)
  376. return -EINVAL;
  377. down_read(&uts_sem);
  378. nlen = strlen(utsname()->domainname) + 1;
  379. err = -EINVAL;
  380. if (nlen > len)
  381. goto out;
  382. err = -EFAULT;
  383. if (!copy_to_user(name, utsname()->domainname, nlen))
  384. err = 0;
  385. out:
  386. up_read(&uts_sem);
  387. return err;
  388. }
  389. /*
  390. * Do a system call from kernel instead of calling sys_execve so we
  391. * end up with proper pt_regs.
  392. */
  393. int kernel_execve(const char *filename, char *const argv[], char *const envp[])
  394. {
  395. long __res;
  396. register long __g1 __asm__ ("g1") = __NR_execve;
  397. register long __o0 __asm__ ("o0") = (long)(filename);
  398. register long __o1 __asm__ ("o1") = (long)(argv);
  399. register long __o2 __asm__ ("o2") = (long)(envp);
  400. asm volatile ("t 0x10\n\t"
  401. "bcc 1f\n\t"
  402. "mov %%o0, %0\n\t"
  403. "sub %%g0, %%o0, %0\n\t"
  404. "1:\n\t"
  405. : "=r" (__res), "=&r" (__o0)
  406. : "1" (__o0), "r" (__o1), "r" (__o2), "r" (__g1)
  407. : "cc");
  408. return __res;
  409. }