sys_parisc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * PARISC specific syscalls
  4. *
  5. * Copyright (C) 1999-2003 Matthew Wilcox <willy at parisc-linux.org>
  6. * Copyright (C) 2000-2003 Paul Bame <bame at parisc-linux.org>
  7. * Copyright (C) 2001 Thomas Bogendoerfer <tsbogend at parisc-linux.org>
  8. * Copyright (C) 1999-2020 Helge Deller <[email protected]>
  9. */
  10. #include <linux/uaccess.h>
  11. #include <asm/elf.h>
  12. #include <linux/file.h>
  13. #include <linux/fs.h>
  14. #include <linux/linkage.h>
  15. #include <linux/mm.h>
  16. #include <linux/mman.h>
  17. #include <linux/sched/signal.h>
  18. #include <linux/sched/mm.h>
  19. #include <linux/shm.h>
  20. #include <linux/syscalls.h>
  21. #include <linux/utsname.h>
  22. #include <linux/personality.h>
  23. #include <linux/random.h>
  24. #include <linux/compat.h>
  25. #include <linux/elf-randomize.h>
  26. /*
  27. * Construct an artificial page offset for the mapping based on the physical
  28. * address of the kernel file mapping variable.
  29. */
  30. #define GET_FILP_PGOFF(filp) \
  31. (filp ? (((unsigned long) filp->f_mapping) >> 8) \
  32. & ((SHM_COLOUR-1) >> PAGE_SHIFT) : 0UL)
  33. static unsigned long shared_align_offset(unsigned long filp_pgoff,
  34. unsigned long pgoff)
  35. {
  36. return (filp_pgoff + pgoff) << PAGE_SHIFT;
  37. }
  38. static inline unsigned long COLOR_ALIGN(unsigned long addr,
  39. unsigned long filp_pgoff, unsigned long pgoff)
  40. {
  41. unsigned long base = (addr+SHM_COLOUR-1) & ~(SHM_COLOUR-1);
  42. unsigned long off = (SHM_COLOUR-1) &
  43. shared_align_offset(filp_pgoff, pgoff);
  44. return base + off;
  45. }
  46. #define STACK_SIZE_DEFAULT (USER_WIDE_MODE \
  47. ? (1 << 30) /* 1 GB */ \
  48. : (CONFIG_STACK_MAX_DEFAULT_SIZE_MB*1024*1024))
  49. unsigned long calc_max_stack_size(unsigned long stack_max)
  50. {
  51. #ifdef CONFIG_COMPAT
  52. if (!USER_WIDE_MODE && (stack_max == COMPAT_RLIM_INFINITY))
  53. stack_max = STACK_SIZE_DEFAULT;
  54. else
  55. #endif
  56. if (stack_max == RLIM_INFINITY)
  57. stack_max = STACK_SIZE_DEFAULT;
  58. return stack_max;
  59. }
  60. /*
  61. * Top of mmap area (just below the process stack).
  62. */
  63. /*
  64. * When called from arch_get_unmapped_area(), rlim_stack will be NULL,
  65. * indicating that "current" should be used instead of a passed-in
  66. * value from the exec bprm as done with arch_pick_mmap_layout().
  67. */
  68. static unsigned long mmap_upper_limit(struct rlimit *rlim_stack)
  69. {
  70. unsigned long stack_base;
  71. /* Limit stack size - see setup_arg_pages() in fs/exec.c */
  72. stack_base = rlim_stack ? rlim_stack->rlim_max
  73. : rlimit_max(RLIMIT_STACK);
  74. stack_base = calc_max_stack_size(stack_base);
  75. /* Add space for stack randomization. */
  76. if (current->flags & PF_RANDOMIZE)
  77. stack_base += (STACK_RND_MASK << PAGE_SHIFT);
  78. return PAGE_ALIGN(STACK_TOP - stack_base);
  79. }
  80. enum mmap_allocation_direction {UP, DOWN};
  81. static unsigned long arch_get_unmapped_area_common(struct file *filp,
  82. unsigned long addr, unsigned long len, unsigned long pgoff,
  83. unsigned long flags, enum mmap_allocation_direction dir)
  84. {
  85. struct mm_struct *mm = current->mm;
  86. struct vm_area_struct *vma, *prev;
  87. unsigned long filp_pgoff;
  88. int do_color_align;
  89. struct vm_unmapped_area_info info;
  90. if (unlikely(len > TASK_SIZE))
  91. return -ENOMEM;
  92. do_color_align = 0;
  93. if (filp || (flags & MAP_SHARED))
  94. do_color_align = 1;
  95. filp_pgoff = GET_FILP_PGOFF(filp);
  96. if (flags & MAP_FIXED) {
  97. /* Even MAP_FIXED mappings must reside within TASK_SIZE */
  98. if (TASK_SIZE - len < addr)
  99. return -EINVAL;
  100. if ((flags & MAP_SHARED) && filp &&
  101. (addr - shared_align_offset(filp_pgoff, pgoff))
  102. & (SHM_COLOUR - 1))
  103. return -EINVAL;
  104. return addr;
  105. }
  106. if (addr) {
  107. if (do_color_align)
  108. addr = COLOR_ALIGN(addr, filp_pgoff, pgoff);
  109. else
  110. addr = PAGE_ALIGN(addr);
  111. vma = find_vma_prev(mm, addr, &prev);
  112. if (TASK_SIZE - len >= addr &&
  113. (!vma || addr + len <= vm_start_gap(vma)) &&
  114. (!prev || addr >= vm_end_gap(prev)))
  115. return addr;
  116. }
  117. info.length = len;
  118. info.align_mask = do_color_align ? (PAGE_MASK & (SHM_COLOUR - 1)) : 0;
  119. info.align_offset = shared_align_offset(filp_pgoff, pgoff);
  120. if (dir == DOWN) {
  121. info.flags = VM_UNMAPPED_AREA_TOPDOWN;
  122. info.low_limit = PAGE_SIZE;
  123. info.high_limit = mm->mmap_base;
  124. addr = vm_unmapped_area(&info);
  125. if (!(addr & ~PAGE_MASK))
  126. return addr;
  127. VM_BUG_ON(addr != -ENOMEM);
  128. /*
  129. * A failed mmap() very likely causes application failure,
  130. * so fall back to the bottom-up function here. This scenario
  131. * can happen with large stack limits and large mmap()
  132. * allocations.
  133. */
  134. }
  135. info.flags = 0;
  136. info.low_limit = mm->mmap_legacy_base;
  137. info.high_limit = mmap_upper_limit(NULL);
  138. return vm_unmapped_area(&info);
  139. }
  140. unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr,
  141. unsigned long len, unsigned long pgoff, unsigned long flags)
  142. {
  143. return arch_get_unmapped_area_common(filp,
  144. addr, len, pgoff, flags, UP);
  145. }
  146. unsigned long arch_get_unmapped_area_topdown(struct file *filp,
  147. unsigned long addr, unsigned long len, unsigned long pgoff,
  148. unsigned long flags)
  149. {
  150. return arch_get_unmapped_area_common(filp,
  151. addr, len, pgoff, flags, DOWN);
  152. }
  153. static int mmap_is_legacy(void)
  154. {
  155. if (current->personality & ADDR_COMPAT_LAYOUT)
  156. return 1;
  157. /* parisc stack always grows up - so a unlimited stack should
  158. * not be an indicator to use the legacy memory layout.
  159. * if (rlimit(RLIMIT_STACK) == RLIM_INFINITY)
  160. * return 1;
  161. */
  162. return sysctl_legacy_va_layout;
  163. }
  164. static unsigned long mmap_rnd(void)
  165. {
  166. unsigned long rnd = 0;
  167. if (current->flags & PF_RANDOMIZE)
  168. rnd = get_random_u32() & MMAP_RND_MASK;
  169. return rnd << PAGE_SHIFT;
  170. }
  171. unsigned long arch_mmap_rnd(void)
  172. {
  173. return (get_random_u32() & MMAP_RND_MASK) << PAGE_SHIFT;
  174. }
  175. static unsigned long mmap_legacy_base(void)
  176. {
  177. return TASK_UNMAPPED_BASE + mmap_rnd();
  178. }
  179. /*
  180. * This function, called very early during the creation of a new
  181. * process VM image, sets up which VM layout function to use:
  182. */
  183. void arch_pick_mmap_layout(struct mm_struct *mm, struct rlimit *rlim_stack)
  184. {
  185. mm->mmap_legacy_base = mmap_legacy_base();
  186. mm->mmap_base = mmap_upper_limit(rlim_stack);
  187. if (mmap_is_legacy()) {
  188. mm->mmap_base = mm->mmap_legacy_base;
  189. mm->get_unmapped_area = arch_get_unmapped_area;
  190. } else {
  191. mm->get_unmapped_area = arch_get_unmapped_area_topdown;
  192. }
  193. }
  194. asmlinkage unsigned long sys_mmap2(unsigned long addr, unsigned long len,
  195. unsigned long prot, unsigned long flags, unsigned long fd,
  196. unsigned long pgoff)
  197. {
  198. /* Make sure the shift for mmap2 is constant (12), no matter what PAGE_SIZE
  199. we have. */
  200. return ksys_mmap_pgoff(addr, len, prot, flags, fd,
  201. pgoff >> (PAGE_SHIFT - 12));
  202. }
  203. asmlinkage unsigned long sys_mmap(unsigned long addr, unsigned long len,
  204. unsigned long prot, unsigned long flags, unsigned long fd,
  205. unsigned long offset)
  206. {
  207. if (!(offset & ~PAGE_MASK)) {
  208. return ksys_mmap_pgoff(addr, len, prot, flags, fd,
  209. offset >> PAGE_SHIFT);
  210. } else {
  211. return -EINVAL;
  212. }
  213. }
  214. /* Fucking broken ABI */
  215. #ifdef CONFIG_64BIT
  216. asmlinkage long parisc_truncate64(const char __user * path,
  217. unsigned int high, unsigned int low)
  218. {
  219. return ksys_truncate(path, (long)high << 32 | low);
  220. }
  221. asmlinkage long parisc_ftruncate64(unsigned int fd,
  222. unsigned int high, unsigned int low)
  223. {
  224. return ksys_ftruncate(fd, (long)high << 32 | low);
  225. }
  226. /* stubs for the benefit of the syscall_table since truncate64 and truncate
  227. * are identical on LP64 */
  228. asmlinkage long sys_truncate64(const char __user * path, unsigned long length)
  229. {
  230. return ksys_truncate(path, length);
  231. }
  232. asmlinkage long sys_ftruncate64(unsigned int fd, unsigned long length)
  233. {
  234. return ksys_ftruncate(fd, length);
  235. }
  236. asmlinkage long sys_fcntl64(unsigned int fd, unsigned int cmd, unsigned long arg)
  237. {
  238. return sys_fcntl(fd, cmd, arg);
  239. }
  240. #else
  241. asmlinkage long parisc_truncate64(const char __user * path,
  242. unsigned int high, unsigned int low)
  243. {
  244. return ksys_truncate(path, (loff_t)high << 32 | low);
  245. }
  246. asmlinkage long parisc_ftruncate64(unsigned int fd,
  247. unsigned int high, unsigned int low)
  248. {
  249. return sys_ftruncate64(fd, (loff_t)high << 32 | low);
  250. }
  251. #endif
  252. asmlinkage ssize_t parisc_pread64(unsigned int fd, char __user *buf, size_t count,
  253. unsigned int high, unsigned int low)
  254. {
  255. return ksys_pread64(fd, buf, count, (loff_t)high << 32 | low);
  256. }
  257. asmlinkage ssize_t parisc_pwrite64(unsigned int fd, const char __user *buf,
  258. size_t count, unsigned int high, unsigned int low)
  259. {
  260. return ksys_pwrite64(fd, buf, count, (loff_t)high << 32 | low);
  261. }
  262. asmlinkage ssize_t parisc_readahead(int fd, unsigned int high, unsigned int low,
  263. size_t count)
  264. {
  265. return ksys_readahead(fd, (loff_t)high << 32 | low, count);
  266. }
  267. asmlinkage long parisc_fadvise64_64(int fd,
  268. unsigned int high_off, unsigned int low_off,
  269. unsigned int high_len, unsigned int low_len, int advice)
  270. {
  271. return ksys_fadvise64_64(fd, (loff_t)high_off << 32 | low_off,
  272. (loff_t)high_len << 32 | low_len, advice);
  273. }
  274. asmlinkage long parisc_sync_file_range(int fd,
  275. u32 hi_off, u32 lo_off, u32 hi_nbytes, u32 lo_nbytes,
  276. unsigned int flags)
  277. {
  278. return ksys_sync_file_range(fd, (loff_t)hi_off << 32 | lo_off,
  279. (loff_t)hi_nbytes << 32 | lo_nbytes, flags);
  280. }
  281. asmlinkage long parisc_fallocate(int fd, int mode, u32 offhi, u32 offlo,
  282. u32 lenhi, u32 lenlo)
  283. {
  284. return ksys_fallocate(fd, mode, ((u64)offhi << 32) | offlo,
  285. ((u64)lenhi << 32) | lenlo);
  286. }
  287. asmlinkage long parisc_personality(unsigned long personality)
  288. {
  289. long err;
  290. if (personality(current->personality) == PER_LINUX32
  291. && personality(personality) == PER_LINUX)
  292. personality = (personality & ~PER_MASK) | PER_LINUX32;
  293. err = sys_personality(personality);
  294. if (personality(err) == PER_LINUX32)
  295. err = (err & ~PER_MASK) | PER_LINUX;
  296. return err;
  297. }
  298. /*
  299. * Up to kernel v5.9 we defined O_NONBLOCK as 000200004,
  300. * since then O_NONBLOCK is defined as 000200000.
  301. *
  302. * The following wrapper functions mask out the old
  303. * O_NDELAY bit from calls which use O_NONBLOCK.
  304. *
  305. * XXX: Remove those in year 2022 (or later)?
  306. */
  307. #define O_NONBLOCK_OLD 000200004
  308. #define O_NONBLOCK_MASK_OUT (O_NONBLOCK_OLD & ~O_NONBLOCK)
  309. static int FIX_O_NONBLOCK(int flags)
  310. {
  311. if ((flags & O_NONBLOCK_MASK_OUT) &&
  312. !test_thread_flag(TIF_NONBLOCK_WARNING)) {
  313. set_thread_flag(TIF_NONBLOCK_WARNING);
  314. pr_warn("%s(%d) uses a deprecated O_NONBLOCK value."
  315. " Please recompile with newer glibc.\n",
  316. current->comm, current->pid);
  317. }
  318. return flags & ~O_NONBLOCK_MASK_OUT;
  319. }
  320. asmlinkage long parisc_timerfd_create(int clockid, int flags)
  321. {
  322. flags = FIX_O_NONBLOCK(flags);
  323. return sys_timerfd_create(clockid, flags);
  324. }
  325. asmlinkage long parisc_signalfd4(int ufd, sigset_t __user *user_mask,
  326. size_t sizemask, int flags)
  327. {
  328. flags = FIX_O_NONBLOCK(flags);
  329. return sys_signalfd4(ufd, user_mask, sizemask, flags);
  330. }
  331. #ifdef CONFIG_COMPAT
  332. asmlinkage long parisc_compat_signalfd4(int ufd,
  333. compat_sigset_t __user *user_mask,
  334. compat_size_t sizemask, int flags)
  335. {
  336. flags = FIX_O_NONBLOCK(flags);
  337. return compat_sys_signalfd4(ufd, user_mask, sizemask, flags);
  338. }
  339. #endif
  340. asmlinkage long parisc_eventfd2(unsigned int count, int flags)
  341. {
  342. flags = FIX_O_NONBLOCK(flags);
  343. return sys_eventfd2(count, flags);
  344. }
  345. asmlinkage long parisc_userfaultfd(int flags)
  346. {
  347. flags = FIX_O_NONBLOCK(flags);
  348. return sys_userfaultfd(flags);
  349. }
  350. asmlinkage long parisc_pipe2(int __user *fildes, int flags)
  351. {
  352. flags = FIX_O_NONBLOCK(flags);
  353. return sys_pipe2(fildes, flags);
  354. }
  355. asmlinkage long parisc_inotify_init1(int flags)
  356. {
  357. flags = FIX_O_NONBLOCK(flags);
  358. return sys_inotify_init1(flags);
  359. }
  360. /*
  361. * madvise() wrapper
  362. *
  363. * Up to kernel v6.1 parisc has different values than all other
  364. * platforms for the MADV_xxx flags listed below.
  365. * To keep binary compatibility with existing userspace programs
  366. * translate the former values to the new values.
  367. *
  368. * XXX: Remove this wrapper in year 2025 (or later)
  369. */
  370. asmlinkage notrace long parisc_madvise(unsigned long start, size_t len_in, int behavior)
  371. {
  372. switch (behavior) {
  373. case 65: behavior = MADV_MERGEABLE; break;
  374. case 66: behavior = MADV_UNMERGEABLE; break;
  375. case 67: behavior = MADV_HUGEPAGE; break;
  376. case 68: behavior = MADV_NOHUGEPAGE; break;
  377. case 69: behavior = MADV_DONTDUMP; break;
  378. case 70: behavior = MADV_DODUMP; break;
  379. case 71: behavior = MADV_WIPEONFORK; break;
  380. case 72: behavior = MADV_KEEPONFORK; break;
  381. case 73: behavior = MADV_COLLAPSE; break;
  382. }
  383. return sys_madvise(start, len_in, behavior);
  384. }