sys_sparc_64.c 16 KB

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