sys_sparc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. /* $Id: sys_sparc.c,v 1.70 2001/04/14 01:12:02 davem Exp $
  2. * linux/arch/sparc/kernel/sys_sparc.c
  3. *
  4. * This file contains various random system calls that
  5. * have a non-standard calling sequence on the Linux/sparc
  6. * platform.
  7. */
  8. #include <linux/errno.h>
  9. #include <linux/types.h>
  10. #include <linux/sched.h>
  11. #include <linux/mm.h>
  12. #include <linux/fs.h>
  13. #include <linux/file.h>
  14. #include <linux/sem.h>
  15. #include <linux/msg.h>
  16. #include <linux/shm.h>
  17. #include <linux/stat.h>
  18. #include <linux/syscalls.h>
  19. #include <linux/mman.h>
  20. #include <linux/utsname.h>
  21. #include <linux/smp.h>
  22. #include <linux/smp_lock.h>
  23. #include <asm/uaccess.h>
  24. #include <asm/ipc.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. ((flags & MAP_FIXED) &&
  209. addr < 0xe0000000 && addr + len > 0x20000000)))
  210. return -EINVAL;
  211. /* See asm-sparc/uaccess.h */
  212. if (len > TASK_SIZE - PAGE_SIZE || addr + len > TASK_SIZE - PAGE_SIZE)
  213. return -EINVAL;
  214. return 0;
  215. }
  216. /* Linux version of mmap */
  217. static unsigned long do_mmap2(unsigned long addr, unsigned long len,
  218. unsigned long prot, unsigned long flags, unsigned long fd,
  219. unsigned long pgoff)
  220. {
  221. struct file * file = NULL;
  222. unsigned long retval = -EBADF;
  223. if (!(flags & MAP_ANONYMOUS)) {
  224. file = fget(fd);
  225. if (!file)
  226. goto out;
  227. }
  228. len = PAGE_ALIGN(len);
  229. flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
  230. down_write(&current->mm->mmap_sem);
  231. retval = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
  232. up_write(&current->mm->mmap_sem);
  233. if (file)
  234. fput(file);
  235. out:
  236. return retval;
  237. }
  238. asmlinkage unsigned long sys_mmap2(unsigned long addr, unsigned long len,
  239. unsigned long prot, unsigned long flags, unsigned long fd,
  240. unsigned long pgoff)
  241. {
  242. /* Make sure the shift for mmap2 is constant (12), no matter what PAGE_SIZE
  243. we have. */
  244. return do_mmap2(addr, len, prot, flags, fd, pgoff >> (PAGE_SHIFT - 12));
  245. }
  246. asmlinkage unsigned long sys_mmap(unsigned long addr, unsigned long len,
  247. unsigned long prot, unsigned long flags, unsigned long fd,
  248. unsigned long off)
  249. {
  250. return do_mmap2(addr, len, prot, flags, fd, off >> PAGE_SHIFT);
  251. }
  252. long sparc_remap_file_pages(unsigned long start, unsigned long size,
  253. unsigned long prot, unsigned long pgoff,
  254. unsigned long flags)
  255. {
  256. /* This works on an existing mmap so we don't need to validate
  257. * the range as that was done at the original mmap call.
  258. */
  259. return sys_remap_file_pages(start, size, prot,
  260. (pgoff >> (PAGE_SHIFT - 12)), flags);
  261. }
  262. extern unsigned long do_mremap(unsigned long addr,
  263. unsigned long old_len, unsigned long new_len,
  264. unsigned long flags, unsigned long new_addr);
  265. asmlinkage unsigned long sparc_mremap(unsigned long addr,
  266. unsigned long old_len, unsigned long new_len,
  267. unsigned long flags, unsigned long new_addr)
  268. {
  269. struct vm_area_struct *vma;
  270. unsigned long ret = -EINVAL;
  271. if (ARCH_SUN4C_SUN4) {
  272. if (old_len > 0x20000000 || new_len > 0x20000000)
  273. goto out;
  274. if (addr < 0xe0000000 && addr + old_len > 0x20000000)
  275. goto out;
  276. }
  277. if (old_len > TASK_SIZE - PAGE_SIZE ||
  278. new_len > TASK_SIZE - PAGE_SIZE)
  279. goto out;
  280. down_write(&current->mm->mmap_sem);
  281. if (flags & MREMAP_FIXED) {
  282. if (ARCH_SUN4C_SUN4 &&
  283. new_addr < 0xe0000000 &&
  284. new_addr + new_len > 0x20000000)
  285. goto out_sem;
  286. if (new_addr + new_len > TASK_SIZE - PAGE_SIZE)
  287. goto out_sem;
  288. } else if ((ARCH_SUN4C_SUN4 && addr < 0xe0000000 &&
  289. addr + new_len > 0x20000000) ||
  290. addr + new_len > TASK_SIZE - PAGE_SIZE) {
  291. unsigned long map_flags = 0;
  292. struct file *file = NULL;
  293. ret = -ENOMEM;
  294. if (!(flags & MREMAP_MAYMOVE))
  295. goto out_sem;
  296. vma = find_vma(current->mm, addr);
  297. if (vma) {
  298. if (vma->vm_flags & VM_SHARED)
  299. map_flags |= MAP_SHARED;
  300. file = vma->vm_file;
  301. }
  302. new_addr = get_unmapped_area(file, addr, new_len,
  303. vma ? vma->vm_pgoff : 0,
  304. map_flags);
  305. ret = new_addr;
  306. if (new_addr & ~PAGE_MASK)
  307. goto out_sem;
  308. flags |= MREMAP_FIXED;
  309. }
  310. ret = do_mremap(addr, old_len, new_len, flags, new_addr);
  311. out_sem:
  312. up_write(&current->mm->mmap_sem);
  313. out:
  314. return ret;
  315. }
  316. /* we come to here via sys_nis_syscall so it can setup the regs argument */
  317. asmlinkage unsigned long
  318. c_sys_nis_syscall (struct pt_regs *regs)
  319. {
  320. static int count = 0;
  321. if (count++ > 5)
  322. return -ENOSYS;
  323. printk ("%s[%d]: Unimplemented SPARC system call %d\n",
  324. current->comm, current->pid, (int)regs->u_regs[1]);
  325. #ifdef DEBUG_UNIMP_SYSCALL
  326. show_regs (regs);
  327. #endif
  328. return -ENOSYS;
  329. }
  330. /* #define DEBUG_SPARC_BREAKPOINT */
  331. asmlinkage void
  332. sparc_breakpoint (struct pt_regs *regs)
  333. {
  334. siginfo_t info;
  335. lock_kernel();
  336. #ifdef DEBUG_SPARC_BREAKPOINT
  337. printk ("TRAP: Entering kernel PC=%x, nPC=%x\n", regs->pc, regs->npc);
  338. #endif
  339. info.si_signo = SIGTRAP;
  340. info.si_errno = 0;
  341. info.si_code = TRAP_BRKPT;
  342. info.si_addr = (void __user *)regs->pc;
  343. info.si_trapno = 0;
  344. force_sig_info(SIGTRAP, &info, current);
  345. #ifdef DEBUG_SPARC_BREAKPOINT
  346. printk ("TRAP: Returning to space: PC=%x nPC=%x\n", regs->pc, regs->npc);
  347. #endif
  348. unlock_kernel();
  349. }
  350. asmlinkage int
  351. sparc_sigaction (int sig, const struct old_sigaction __user *act,
  352. struct old_sigaction __user *oact)
  353. {
  354. struct k_sigaction new_ka, old_ka;
  355. int ret;
  356. if (sig < 0) {
  357. current->thread.new_signal = 1;
  358. sig = -sig;
  359. }
  360. if (act) {
  361. unsigned long mask;
  362. if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
  363. __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
  364. __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
  365. return -EFAULT;
  366. __get_user(new_ka.sa.sa_flags, &act->sa_flags);
  367. __get_user(mask, &act->sa_mask);
  368. siginitset(&new_ka.sa.sa_mask, mask);
  369. new_ka.ka_restorer = NULL;
  370. }
  371. ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
  372. if (!ret && oact) {
  373. /* In the clone() case we could copy half consistent
  374. * state to the user, however this could sleep and
  375. * deadlock us if we held the signal lock on SMP. So for
  376. * now I take the easy way out and do no locking.
  377. */
  378. if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
  379. __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
  380. __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
  381. return -EFAULT;
  382. __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
  383. __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
  384. }
  385. return ret;
  386. }
  387. asmlinkage long
  388. sys_rt_sigaction(int sig,
  389. const struct sigaction __user *act,
  390. struct sigaction __user *oact,
  391. void __user *restorer,
  392. size_t sigsetsize)
  393. {
  394. struct k_sigaction new_ka, old_ka;
  395. int ret;
  396. /* XXX: Don't preclude handling different sized sigset_t's. */
  397. if (sigsetsize != sizeof(sigset_t))
  398. return -EINVAL;
  399. /* All tasks which use RT signals (effectively) use
  400. * new style signals.
  401. */
  402. current->thread.new_signal = 1;
  403. if (act) {
  404. new_ka.ka_restorer = restorer;
  405. if (copy_from_user(&new_ka.sa, act, sizeof(*act)))
  406. return -EFAULT;
  407. }
  408. ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
  409. if (!ret && oact) {
  410. if (copy_to_user(oact, &old_ka.sa, sizeof(*oact)))
  411. return -EFAULT;
  412. }
  413. return ret;
  414. }
  415. asmlinkage int sys_getdomainname(char __user *name, int len)
  416. {
  417. int nlen, err;
  418. if (len < 0)
  419. return -EINVAL;
  420. down_read(&uts_sem);
  421. nlen = strlen(system_utsname.domainname) + 1;
  422. err = -EINVAL;
  423. if (nlen > len)
  424. goto out;
  425. err = -EFAULT;
  426. if (!copy_to_user(name, system_utsname.domainname, nlen))
  427. err = 0;
  428. out:
  429. up_read(&uts_sem);
  430. return err;
  431. }