sys_sparc.c 12 KB

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