sys_sparc_64.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. /* linux/arch/sparc64/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/fs.h>
  11. #include <linux/file.h>
  12. #include <linux/mm.h>
  13. #include <linux/sem.h>
  14. #include <linux/msg.h>
  15. #include <linux/shm.h>
  16. #include <linux/stat.h>
  17. #include <linux/mman.h>
  18. #include <linux/utsname.h>
  19. #include <linux/smp.h>
  20. #include <linux/slab.h>
  21. #include <linux/syscalls.h>
  22. #include <linux/ipc.h>
  23. #include <linux/personality.h>
  24. #include <linux/random.h>
  25. #include <linux/export.h>
  26. #include <linux/context_tracking.h>
  27. #include <asm/uaccess.h>
  28. #include <asm/utrap.h>
  29. #include <asm/unistd.h>
  30. #include "entry.h"
  31. #include "systbls.h"
  32. /* #define DEBUG_UNIMP_SYSCALL */
  33. asmlinkage unsigned long sys_getpagesize(void)
  34. {
  35. return PAGE_SIZE;
  36. }
  37. /* Does addr --> addr+len fall within 4GB of the VA-space hole or
  38. * overflow past the end of the 64-bit address space?
  39. */
  40. static inline int invalid_64bit_range(unsigned long addr, unsigned long len)
  41. {
  42. unsigned long va_exclude_start, va_exclude_end;
  43. va_exclude_start = VA_EXCLUDE_START;
  44. va_exclude_end = VA_EXCLUDE_END;
  45. if (unlikely(len >= va_exclude_start))
  46. return 1;
  47. if (unlikely((addr + len) < addr))
  48. return 1;
  49. if (unlikely((addr >= va_exclude_start && addr < va_exclude_end) ||
  50. ((addr + len) >= va_exclude_start &&
  51. (addr + len) < va_exclude_end)))
  52. return 1;
  53. return 0;
  54. }
  55. /* These functions differ from the default implementations in
  56. * mm/mmap.c in two ways:
  57. *
  58. * 1) For file backed MAP_SHARED mmap()'s we D-cache color align,
  59. * for fixed such mappings we just validate what the user gave us.
  60. * 2) For 64-bit tasks we avoid mapping anything within 4GB of
  61. * the spitfire/niagara VA-hole.
  62. */
  63. static inline unsigned long COLOR_ALIGN(unsigned long addr,
  64. unsigned long pgoff)
  65. {
  66. unsigned long base = (addr+SHMLBA-1)&~(SHMLBA-1);
  67. unsigned long off = (pgoff<<PAGE_SHIFT) & (SHMLBA-1);
  68. return base + off;
  69. }
  70. unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr, unsigned long len, unsigned long pgoff, unsigned long flags)
  71. {
  72. struct mm_struct *mm = current->mm;
  73. struct vm_area_struct * vma;
  74. unsigned long task_size = TASK_SIZE;
  75. int do_color_align;
  76. struct vm_unmapped_area_info info;
  77. if (flags & MAP_FIXED) {
  78. /* We do not accept a shared mapping if it would violate
  79. * cache aliasing constraints.
  80. */
  81. if ((flags & MAP_SHARED) &&
  82. ((addr - (pgoff << PAGE_SHIFT)) & (SHMLBA - 1)))
  83. return -EINVAL;
  84. return addr;
  85. }
  86. if (test_thread_flag(TIF_32BIT))
  87. task_size = STACK_TOP32;
  88. if (unlikely(len > task_size || len >= VA_EXCLUDE_START))
  89. return -ENOMEM;
  90. do_color_align = 0;
  91. if (filp || (flags & MAP_SHARED))
  92. do_color_align = 1;
  93. if (addr) {
  94. if (do_color_align)
  95. addr = COLOR_ALIGN(addr, pgoff);
  96. else
  97. addr = PAGE_ALIGN(addr);
  98. vma = find_vma(mm, addr);
  99. if (task_size - len >= addr &&
  100. (!vma || addr + len <= vma->vm_start))
  101. return addr;
  102. }
  103. info.flags = 0;
  104. info.length = len;
  105. info.low_limit = TASK_UNMAPPED_BASE;
  106. info.high_limit = min(task_size, VA_EXCLUDE_START);
  107. info.align_mask = do_color_align ? (PAGE_MASK & (SHMLBA - 1)) : 0;
  108. info.align_offset = pgoff << PAGE_SHIFT;
  109. addr = vm_unmapped_area(&info);
  110. if ((addr & ~PAGE_MASK) && task_size > VA_EXCLUDE_END) {
  111. VM_BUG_ON(addr != -ENOMEM);
  112. info.low_limit = VA_EXCLUDE_END;
  113. info.high_limit = task_size;
  114. addr = vm_unmapped_area(&info);
  115. }
  116. return addr;
  117. }
  118. unsigned long
  119. arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
  120. const unsigned long len, const unsigned long pgoff,
  121. const unsigned long flags)
  122. {
  123. struct vm_area_struct *vma;
  124. struct mm_struct *mm = current->mm;
  125. unsigned long task_size = STACK_TOP32;
  126. unsigned long addr = addr0;
  127. int do_color_align;
  128. struct vm_unmapped_area_info info;
  129. /* This should only ever run for 32-bit processes. */
  130. BUG_ON(!test_thread_flag(TIF_32BIT));
  131. if (flags & MAP_FIXED) {
  132. /* We do not accept a shared mapping if it would violate
  133. * cache aliasing constraints.
  134. */
  135. if ((flags & MAP_SHARED) &&
  136. ((addr - (pgoff << PAGE_SHIFT)) & (SHMLBA - 1)))
  137. return -EINVAL;
  138. return addr;
  139. }
  140. if (unlikely(len > task_size))
  141. return -ENOMEM;
  142. do_color_align = 0;
  143. if (filp || (flags & MAP_SHARED))
  144. do_color_align = 1;
  145. /* requesting a specific address */
  146. if (addr) {
  147. if (do_color_align)
  148. addr = COLOR_ALIGN(addr, pgoff);
  149. else
  150. addr = PAGE_ALIGN(addr);
  151. vma = find_vma(mm, addr);
  152. if (task_size - len >= addr &&
  153. (!vma || addr + len <= vma->vm_start))
  154. return addr;
  155. }
  156. info.flags = VM_UNMAPPED_AREA_TOPDOWN;
  157. info.length = len;
  158. info.low_limit = PAGE_SIZE;
  159. info.high_limit = mm->mmap_base;
  160. info.align_mask = do_color_align ? (PAGE_MASK & (SHMLBA - 1)) : 0;
  161. info.align_offset = pgoff << PAGE_SHIFT;
  162. addr = vm_unmapped_area(&info);
  163. /*
  164. * A failed mmap() very likely causes application failure,
  165. * so fall back to the bottom-up function here. This scenario
  166. * can happen with large stack limits and large mmap()
  167. * allocations.
  168. */
  169. if (addr & ~PAGE_MASK) {
  170. VM_BUG_ON(addr != -ENOMEM);
  171. info.flags = 0;
  172. info.low_limit = TASK_UNMAPPED_BASE;
  173. info.high_limit = STACK_TOP32;
  174. addr = vm_unmapped_area(&info);
  175. }
  176. return addr;
  177. }
  178. /* Try to align mapping such that we align it as much as possible. */
  179. unsigned long get_fb_unmapped_area(struct file *filp, unsigned long orig_addr, unsigned long len, unsigned long pgoff, unsigned long flags)
  180. {
  181. unsigned long align_goal, addr = -ENOMEM;
  182. unsigned long (*get_area)(struct file *, unsigned long,
  183. unsigned long, unsigned long, unsigned long);
  184. get_area = current->mm->get_unmapped_area;
  185. if (flags & MAP_FIXED) {
  186. /* Ok, don't mess with it. */
  187. return get_area(NULL, orig_addr, len, pgoff, flags);
  188. }
  189. flags &= ~MAP_SHARED;
  190. align_goal = PAGE_SIZE;
  191. if (len >= (4UL * 1024 * 1024))
  192. align_goal = (4UL * 1024 * 1024);
  193. else if (len >= (512UL * 1024))
  194. align_goal = (512UL * 1024);
  195. else if (len >= (64UL * 1024))
  196. align_goal = (64UL * 1024);
  197. do {
  198. addr = get_area(NULL, orig_addr, len + (align_goal - PAGE_SIZE), pgoff, flags);
  199. if (!(addr & ~PAGE_MASK)) {
  200. addr = (addr + (align_goal - 1UL)) & ~(align_goal - 1UL);
  201. break;
  202. }
  203. if (align_goal == (4UL * 1024 * 1024))
  204. align_goal = (512UL * 1024);
  205. else if (align_goal == (512UL * 1024))
  206. align_goal = (64UL * 1024);
  207. else
  208. align_goal = PAGE_SIZE;
  209. } while ((addr & ~PAGE_MASK) && align_goal > PAGE_SIZE);
  210. /* Mapping is smaller than 64K or larger areas could not
  211. * be obtained.
  212. */
  213. if (addr & ~PAGE_MASK)
  214. addr = get_area(NULL, orig_addr, len, pgoff, flags);
  215. return addr;
  216. }
  217. EXPORT_SYMBOL(get_fb_unmapped_area);
  218. /* Essentially the same as PowerPC. */
  219. static unsigned long mmap_rnd(void)
  220. {
  221. unsigned long rnd = 0UL;
  222. if (current->flags & PF_RANDOMIZE) {
  223. unsigned long val = get_random_int();
  224. if (test_thread_flag(TIF_32BIT))
  225. rnd = (val % (1UL << (23UL-PAGE_SHIFT)));
  226. else
  227. rnd = (val % (1UL << (30UL-PAGE_SHIFT)));
  228. }
  229. return rnd << PAGE_SHIFT;
  230. }
  231. void arch_pick_mmap_layout(struct mm_struct *mm)
  232. {
  233. unsigned long random_factor = mmap_rnd();
  234. unsigned long gap;
  235. /*
  236. * Fall back to the standard layout if the personality
  237. * bit is set, or if the expected stack growth is unlimited:
  238. */
  239. gap = rlimit(RLIMIT_STACK);
  240. if (!test_thread_flag(TIF_32BIT) ||
  241. (current->personality & ADDR_COMPAT_LAYOUT) ||
  242. gap == RLIM_INFINITY ||
  243. sysctl_legacy_va_layout) {
  244. mm->mmap_base = TASK_UNMAPPED_BASE + random_factor;
  245. mm->get_unmapped_area = arch_get_unmapped_area;
  246. } else {
  247. /* We know it's 32-bit */
  248. unsigned long task_size = STACK_TOP32;
  249. if (gap < 128 * 1024 * 1024)
  250. gap = 128 * 1024 * 1024;
  251. if (gap > (task_size / 6 * 5))
  252. gap = (task_size / 6 * 5);
  253. mm->mmap_base = PAGE_ALIGN(task_size - gap - random_factor);
  254. mm->get_unmapped_area = arch_get_unmapped_area_topdown;
  255. }
  256. }
  257. /*
  258. * sys_pipe() is the normal C calling standard for creating
  259. * a pipe. It's not the way unix traditionally does this, though.
  260. */
  261. SYSCALL_DEFINE1(sparc_pipe_real, struct pt_regs *, regs)
  262. {
  263. int fd[2];
  264. int error;
  265. error = do_pipe_flags(fd, 0);
  266. if (error)
  267. goto out;
  268. regs->u_regs[UREG_I1] = fd[1];
  269. error = fd[0];
  270. out:
  271. return error;
  272. }
  273. /*
  274. * sys_ipc() is the de-multiplexer for the SysV IPC calls..
  275. *
  276. * This is really horribly ugly.
  277. */
  278. SYSCALL_DEFINE6(sparc_ipc, unsigned int, call, int, first, unsigned long, second,
  279. unsigned long, third, void __user *, ptr, long, fifth)
  280. {
  281. long err;
  282. /* No need for backward compatibility. We can start fresh... */
  283. if (call <= SEMCTL) {
  284. switch (call) {
  285. case SEMOP:
  286. err = sys_semtimedop(first, ptr,
  287. (unsigned)second, NULL);
  288. goto out;
  289. case SEMTIMEDOP:
  290. err = sys_semtimedop(first, ptr, (unsigned)second,
  291. (const struct timespec __user *)
  292. (unsigned long) fifth);
  293. goto out;
  294. case SEMGET:
  295. err = sys_semget(first, (int)second, (int)third);
  296. goto out;
  297. case SEMCTL: {
  298. err = sys_semctl(first, second,
  299. (int)third | IPC_64,
  300. (unsigned long) ptr);
  301. goto out;
  302. }
  303. default:
  304. err = -ENOSYS;
  305. goto out;
  306. }
  307. }
  308. if (call <= MSGCTL) {
  309. switch (call) {
  310. case MSGSND:
  311. err = sys_msgsnd(first, ptr, (size_t)second,
  312. (int)third);
  313. goto out;
  314. case MSGRCV:
  315. err = sys_msgrcv(first, ptr, (size_t)second, fifth,
  316. (int)third);
  317. goto out;
  318. case MSGGET:
  319. err = sys_msgget((key_t)first, (int)second);
  320. goto out;
  321. case MSGCTL:
  322. err = sys_msgctl(first, (int)second | IPC_64, ptr);
  323. goto out;
  324. default:
  325. err = -ENOSYS;
  326. goto out;
  327. }
  328. }
  329. if (call <= SHMCTL) {
  330. switch (call) {
  331. case SHMAT: {
  332. ulong raddr;
  333. err = do_shmat(first, ptr, (int)second, &raddr, SHMLBA);
  334. if (!err) {
  335. if (put_user(raddr,
  336. (ulong __user *) third))
  337. err = -EFAULT;
  338. }
  339. goto out;
  340. }
  341. case SHMDT:
  342. err = sys_shmdt(ptr);
  343. goto out;
  344. case SHMGET:
  345. err = sys_shmget(first, (size_t)second, (int)third);
  346. goto out;
  347. case SHMCTL:
  348. err = sys_shmctl(first, (int)second | IPC_64, ptr);
  349. goto out;
  350. default:
  351. err = -ENOSYS;
  352. goto out;
  353. }
  354. } else {
  355. err = -ENOSYS;
  356. }
  357. out:
  358. return err;
  359. }
  360. SYSCALL_DEFINE1(sparc64_personality, unsigned long, personality)
  361. {
  362. int ret;
  363. if (personality(current->personality) == PER_LINUX32 &&
  364. personality(personality) == PER_LINUX)
  365. personality |= PER_LINUX32;
  366. ret = sys_personality(personality);
  367. if (personality(ret) == PER_LINUX32)
  368. ret &= ~PER_LINUX32;
  369. return ret;
  370. }
  371. int sparc_mmap_check(unsigned long addr, unsigned long len)
  372. {
  373. if (test_thread_flag(TIF_32BIT)) {
  374. if (len >= STACK_TOP32)
  375. return -EINVAL;
  376. if (addr > STACK_TOP32 - len)
  377. return -EINVAL;
  378. } else {
  379. if (len >= VA_EXCLUDE_START)
  380. return -EINVAL;
  381. if (invalid_64bit_range(addr, len))
  382. return -EINVAL;
  383. }
  384. return 0;
  385. }
  386. /* Linux version of mmap */
  387. SYSCALL_DEFINE6(mmap, unsigned long, addr, unsigned long, len,
  388. unsigned long, prot, unsigned long, flags, unsigned long, fd,
  389. unsigned long, off)
  390. {
  391. unsigned long retval = -EINVAL;
  392. if ((off + PAGE_ALIGN(len)) < off)
  393. goto out;
  394. if (off & ~PAGE_MASK)
  395. goto out;
  396. retval = sys_mmap_pgoff(addr, len, prot, flags, fd, off >> PAGE_SHIFT);
  397. out:
  398. return retval;
  399. }
  400. SYSCALL_DEFINE2(64_munmap, unsigned long, addr, size_t, len)
  401. {
  402. if (invalid_64bit_range(addr, len))
  403. return -EINVAL;
  404. return vm_munmap(addr, len);
  405. }
  406. SYSCALL_DEFINE5(64_mremap, unsigned long, addr, unsigned long, old_len,
  407. unsigned long, new_len, unsigned long, flags,
  408. unsigned long, new_addr)
  409. {
  410. if (test_thread_flag(TIF_32BIT))
  411. return -EINVAL;
  412. return sys_mremap(addr, old_len, new_len, flags, new_addr);
  413. }
  414. /* we come to here via sys_nis_syscall so it can setup the regs argument */
  415. asmlinkage unsigned long c_sys_nis_syscall(struct pt_regs *regs)
  416. {
  417. static int count;
  418. /* Don't make the system unusable, if someone goes stuck */
  419. if (count++ > 5)
  420. return -ENOSYS;
  421. printk ("Unimplemented SPARC system call %ld\n",regs->u_regs[1]);
  422. #ifdef DEBUG_UNIMP_SYSCALL
  423. show_regs (regs);
  424. #endif
  425. return -ENOSYS;
  426. }
  427. /* #define DEBUG_SPARC_BREAKPOINT */
  428. asmlinkage void sparc_breakpoint(struct pt_regs *regs)
  429. {
  430. enum ctx_state prev_state = exception_enter();
  431. siginfo_t info;
  432. if (test_thread_flag(TIF_32BIT)) {
  433. regs->tpc &= 0xffffffff;
  434. regs->tnpc &= 0xffffffff;
  435. }
  436. #ifdef DEBUG_SPARC_BREAKPOINT
  437. printk ("TRAP: Entering kernel PC=%lx, nPC=%lx\n", regs->tpc, regs->tnpc);
  438. #endif
  439. info.si_signo = SIGTRAP;
  440. info.si_errno = 0;
  441. info.si_code = TRAP_BRKPT;
  442. info.si_addr = (void __user *)regs->tpc;
  443. info.si_trapno = 0;
  444. force_sig_info(SIGTRAP, &info, current);
  445. #ifdef DEBUG_SPARC_BREAKPOINT
  446. printk ("TRAP: Returning to space: PC=%lx nPC=%lx\n", regs->tpc, regs->tnpc);
  447. #endif
  448. exception_exit(prev_state);
  449. }
  450. extern void check_pending(int signum);
  451. SYSCALL_DEFINE2(getdomainname, char __user *, name, int, len)
  452. {
  453. int nlen, err;
  454. if (len < 0)
  455. return -EINVAL;
  456. down_read(&uts_sem);
  457. nlen = strlen(utsname()->domainname) + 1;
  458. err = -EINVAL;
  459. if (nlen > len)
  460. goto out;
  461. err = -EFAULT;
  462. if (!copy_to_user(name, utsname()->domainname, nlen))
  463. err = 0;
  464. out:
  465. up_read(&uts_sem);
  466. return err;
  467. }
  468. SYSCALL_DEFINE5(utrap_install, utrap_entry_t, type,
  469. utrap_handler_t, new_p, utrap_handler_t, new_d,
  470. utrap_handler_t __user *, old_p,
  471. utrap_handler_t __user *, old_d)
  472. {
  473. if (type < UT_INSTRUCTION_EXCEPTION || type > UT_TRAP_INSTRUCTION_31)
  474. return -EINVAL;
  475. if (new_p == (utrap_handler_t)(long)UTH_NOCHANGE) {
  476. if (old_p) {
  477. if (!current_thread_info()->utraps) {
  478. if (put_user(NULL, old_p))
  479. return -EFAULT;
  480. } else {
  481. if (put_user((utrap_handler_t)(current_thread_info()->utraps[type]), old_p))
  482. return -EFAULT;
  483. }
  484. }
  485. if (old_d) {
  486. if (put_user(NULL, old_d))
  487. return -EFAULT;
  488. }
  489. return 0;
  490. }
  491. if (!current_thread_info()->utraps) {
  492. current_thread_info()->utraps =
  493. kzalloc((UT_TRAP_INSTRUCTION_31+1)*sizeof(long), GFP_KERNEL);
  494. if (!current_thread_info()->utraps)
  495. return -ENOMEM;
  496. current_thread_info()->utraps[0] = 1;
  497. } else {
  498. if ((utrap_handler_t)current_thread_info()->utraps[type] != new_p &&
  499. current_thread_info()->utraps[0] > 1) {
  500. unsigned long *p = current_thread_info()->utraps;
  501. current_thread_info()->utraps =
  502. kmalloc((UT_TRAP_INSTRUCTION_31+1)*sizeof(long),
  503. GFP_KERNEL);
  504. if (!current_thread_info()->utraps) {
  505. current_thread_info()->utraps = p;
  506. return -ENOMEM;
  507. }
  508. p[0]--;
  509. current_thread_info()->utraps[0] = 1;
  510. memcpy(current_thread_info()->utraps+1, p+1,
  511. UT_TRAP_INSTRUCTION_31*sizeof(long));
  512. }
  513. }
  514. if (old_p) {
  515. if (put_user((utrap_handler_t)(current_thread_info()->utraps[type]), old_p))
  516. return -EFAULT;
  517. }
  518. if (old_d) {
  519. if (put_user(NULL, old_d))
  520. return -EFAULT;
  521. }
  522. current_thread_info()->utraps[type] = (long)new_p;
  523. return 0;
  524. }
  525. asmlinkage long sparc_memory_ordering(unsigned long model,
  526. struct pt_regs *regs)
  527. {
  528. if (model >= 3)
  529. return -EINVAL;
  530. regs->tstate = (regs->tstate & ~TSTATE_MM) | (model << 14);
  531. return 0;
  532. }
  533. SYSCALL_DEFINE5(rt_sigaction, int, sig, const struct sigaction __user *, act,
  534. struct sigaction __user *, oact, void __user *, restorer,
  535. size_t, sigsetsize)
  536. {
  537. struct k_sigaction new_ka, old_ka;
  538. int ret;
  539. /* XXX: Don't preclude handling different sized sigset_t's. */
  540. if (sigsetsize != sizeof(sigset_t))
  541. return -EINVAL;
  542. if (act) {
  543. new_ka.ka_restorer = restorer;
  544. if (copy_from_user(&new_ka.sa, act, sizeof(*act)))
  545. return -EFAULT;
  546. }
  547. ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
  548. if (!ret && oact) {
  549. if (copy_to_user(oact, &old_ka.sa, sizeof(*oact)))
  550. return -EFAULT;
  551. }
  552. return ret;
  553. }
  554. asmlinkage long sys_kern_features(void)
  555. {
  556. return KERN_FEATURE_MIXED_MODE_STACK;
  557. }