sys_sparc.c 12 KB

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