sys_sparc_64.c 15 KB

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