sys_sparc_64.c 15 KB

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