sys_parisc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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 <deller@gmx.de>
  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. 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. .length = len
  91. };
  92. if (unlikely(len > TASK_SIZE))
  93. return -ENOMEM;
  94. do_color_align = 0;
  95. if (filp || (flags & MAP_SHARED))
  96. do_color_align = 1;
  97. filp_pgoff = GET_FILP_PGOFF(filp);
  98. if (flags & MAP_FIXED) {
  99. /* Even MAP_FIXED mappings must reside within TASK_SIZE */
  100. if (TASK_SIZE - len < addr)
  101. return -EINVAL;
  102. if ((flags & MAP_SHARED) && filp &&
  103. (addr - shared_align_offset(filp_pgoff, pgoff))
  104. & (SHM_COLOUR - 1))
  105. return -EINVAL;
  106. return addr;
  107. }
  108. if (addr) {
  109. if (do_color_align)
  110. addr = COLOR_ALIGN(addr, filp_pgoff, pgoff);
  111. else
  112. addr = PAGE_ALIGN(addr);
  113. vma = find_vma_prev(mm, addr, &prev);
  114. if (TASK_SIZE - len >= addr &&
  115. (!vma || addr + len <= vm_start_gap(vma)) &&
  116. (!prev || addr >= vm_end_gap(prev)))
  117. return addr;
  118. }
  119. info.align_mask = do_color_align ? (PAGE_MASK & (SHM_COLOUR - 1)) : 0;
  120. info.align_offset = shared_align_offset(filp_pgoff, pgoff);
  121. if (dir == DOWN) {
  122. info.flags = VM_UNMAPPED_AREA_TOPDOWN;
  123. info.low_limit = PAGE_SIZE;
  124. info.high_limit = mm->mmap_base;
  125. addr = vm_unmapped_area(&info);
  126. if (!(addr & ~PAGE_MASK))
  127. return addr;
  128. VM_BUG_ON(addr != -ENOMEM);
  129. /*
  130. * A failed mmap() very likely causes application failure,
  131. * so fall back to the bottom-up function here. This scenario
  132. * can happen with large stack limits and large mmap()
  133. * allocations.
  134. */
  135. }
  136. info.low_limit = mm->mmap_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. vm_flags_t vm_flags)
  143. {
  144. return arch_get_unmapped_area_common(filp,
  145. addr, len, pgoff, flags, UP);
  146. }
  147. unsigned long arch_get_unmapped_area_topdown(struct file *filp,
  148. unsigned long addr, unsigned long len, unsigned long pgoff,
  149. unsigned long flags, vm_flags_t vm_flags)
  150. {
  151. return arch_get_unmapped_area_common(filp,
  152. addr, len, pgoff, flags, DOWN);
  153. }
  154. asmlinkage unsigned long sys_mmap2(unsigned long addr, unsigned long len,
  155. unsigned long prot, unsigned long flags, unsigned long fd,
  156. unsigned long pgoff)
  157. {
  158. /* Make sure the shift for mmap2 is constant (12), no matter what PAGE_SIZE
  159. we have. */
  160. return ksys_mmap_pgoff(addr, len, prot, flags, fd,
  161. pgoff >> (PAGE_SHIFT - 12));
  162. }
  163. asmlinkage unsigned long sys_mmap(unsigned long addr, unsigned long len,
  164. unsigned long prot, unsigned long flags, unsigned long fd,
  165. unsigned long offset)
  166. {
  167. if (!(offset & ~PAGE_MASK)) {
  168. return ksys_mmap_pgoff(addr, len, prot, flags, fd,
  169. offset >> PAGE_SHIFT);
  170. } else {
  171. return -EINVAL;
  172. }
  173. }
  174. /* Fucking broken ABI */
  175. #ifdef CONFIG_64BIT
  176. asmlinkage long parisc_truncate64(const char __user * path,
  177. unsigned int high, unsigned int low)
  178. {
  179. return ksys_truncate(path, (long)high << 32 | low);
  180. }
  181. asmlinkage long parisc_ftruncate64(unsigned int fd,
  182. unsigned int high, unsigned int low)
  183. {
  184. return ksys_ftruncate(fd, (long)high << 32 | low);
  185. }
  186. /* stubs for the benefit of the syscall_table since truncate64 and truncate
  187. * are identical on LP64 */
  188. asmlinkage long sys_truncate64(const char __user * path, unsigned long length)
  189. {
  190. return ksys_truncate(path, length);
  191. }
  192. asmlinkage long sys_ftruncate64(unsigned int fd, unsigned long length)
  193. {
  194. return ksys_ftruncate(fd, length);
  195. }
  196. asmlinkage long sys_fcntl64(unsigned int fd, unsigned int cmd, unsigned long arg)
  197. {
  198. return sys_fcntl(fd, cmd, arg);
  199. }
  200. #else
  201. asmlinkage long parisc_truncate64(const char __user * path,
  202. unsigned int high, unsigned int low)
  203. {
  204. return ksys_truncate(path, (loff_t)high << 32 | low);
  205. }
  206. asmlinkage long parisc_ftruncate64(unsigned int fd,
  207. unsigned int high, unsigned int low)
  208. {
  209. return sys_ftruncate64(fd, (loff_t)high << 32 | low);
  210. }
  211. #endif
  212. asmlinkage ssize_t parisc_pread64(unsigned int fd, char __user *buf, size_t count,
  213. unsigned int high, unsigned int low)
  214. {
  215. return ksys_pread64(fd, buf, count, (loff_t)high << 32 | low);
  216. }
  217. asmlinkage ssize_t parisc_pwrite64(unsigned int fd, const char __user *buf,
  218. size_t count, unsigned int high, unsigned int low)
  219. {
  220. return ksys_pwrite64(fd, buf, count, (loff_t)high << 32 | low);
  221. }
  222. asmlinkage ssize_t parisc_readahead(int fd, unsigned int high, unsigned int low,
  223. size_t count)
  224. {
  225. return ksys_readahead(fd, (loff_t)high << 32 | low, count);
  226. }
  227. asmlinkage long parisc_fadvise64_64(int fd,
  228. unsigned int high_off, unsigned int low_off,
  229. unsigned int high_len, unsigned int low_len, int advice)
  230. {
  231. return ksys_fadvise64_64(fd, (loff_t)high_off << 32 | low_off,
  232. (loff_t)high_len << 32 | low_len, advice);
  233. }
  234. asmlinkage long parisc_sync_file_range(int fd,
  235. u32 hi_off, u32 lo_off, u32 hi_nbytes, u32 lo_nbytes,
  236. unsigned int flags)
  237. {
  238. return ksys_sync_file_range(fd, (loff_t)hi_off << 32 | lo_off,
  239. (loff_t)hi_nbytes << 32 | lo_nbytes, flags);
  240. }
  241. asmlinkage long parisc_fallocate(int fd, int mode, u32 offhi, u32 offlo,
  242. u32 lenhi, u32 lenlo)
  243. {
  244. return ksys_fallocate(fd, mode, ((u64)offhi << 32) | offlo,
  245. ((u64)lenhi << 32) | lenlo);
  246. }
  247. asmlinkage long parisc_personality(unsigned long personality)
  248. {
  249. long err;
  250. if (personality(current->personality) == PER_LINUX32
  251. && personality(personality) == PER_LINUX)
  252. personality = (personality & ~PER_MASK) | PER_LINUX32;
  253. err = sys_personality(personality);
  254. if (personality(err) == PER_LINUX32)
  255. err = (err & ~PER_MASK) | PER_LINUX;
  256. return err;
  257. }
  258. /*
  259. * Up to kernel v5.9 we defined O_NONBLOCK as 000200004,
  260. * since then O_NONBLOCK is defined as 000200000.
  261. *
  262. * The following wrapper functions mask out the old
  263. * O_NDELAY bit from calls which use O_NONBLOCK.
  264. *
  265. * XXX: Remove those in year 2022 (or later)?
  266. */
  267. #define O_NONBLOCK_OLD 000200004
  268. #define O_NONBLOCK_MASK_OUT (O_NONBLOCK_OLD & ~O_NONBLOCK)
  269. static int FIX_O_NONBLOCK(int flags)
  270. {
  271. if ((flags & O_NONBLOCK_MASK_OUT) &&
  272. !test_thread_flag(TIF_NONBLOCK_WARNING)) {
  273. set_thread_flag(TIF_NONBLOCK_WARNING);
  274. pr_warn("%s(%d) uses a deprecated O_NONBLOCK value."
  275. " Please recompile with newer glibc.\n",
  276. current->comm, current->pid);
  277. }
  278. return flags & ~O_NONBLOCK_MASK_OUT;
  279. }
  280. asmlinkage long parisc_timerfd_create(int clockid, int flags)
  281. {
  282. flags = FIX_O_NONBLOCK(flags);
  283. return sys_timerfd_create(clockid, flags);
  284. }
  285. asmlinkage long parisc_signalfd4(int ufd, sigset_t __user *user_mask,
  286. size_t sizemask, int flags)
  287. {
  288. flags = FIX_O_NONBLOCK(flags);
  289. return sys_signalfd4(ufd, user_mask, sizemask, flags);
  290. }
  291. #ifdef CONFIG_COMPAT
  292. asmlinkage long parisc_compat_signalfd4(int ufd,
  293. compat_sigset_t __user *user_mask,
  294. compat_size_t sizemask, int flags)
  295. {
  296. flags = FIX_O_NONBLOCK(flags);
  297. return compat_sys_signalfd4(ufd, user_mask, sizemask, flags);
  298. }
  299. #endif
  300. asmlinkage long parisc_eventfd2(unsigned int count, int flags)
  301. {
  302. flags = FIX_O_NONBLOCK(flags);
  303. return sys_eventfd2(count, flags);
  304. }
  305. asmlinkage long parisc_userfaultfd(int flags)
  306. {
  307. flags = FIX_O_NONBLOCK(flags);
  308. return sys_userfaultfd(flags);
  309. }
  310. asmlinkage long parisc_pipe2(int __user *fildes, int flags)
  311. {
  312. flags = FIX_O_NONBLOCK(flags);
  313. return sys_pipe2(fildes, flags);
  314. }
  315. asmlinkage long parisc_inotify_init1(int flags)
  316. {
  317. flags = FIX_O_NONBLOCK(flags);
  318. return sys_inotify_init1(flags);
  319. }
  320. /*
  321. * madvise() wrapper
  322. *
  323. * Up to kernel v6.1 parisc has different values than all other
  324. * platforms for the MADV_xxx flags listed below.
  325. * To keep binary compatibility with existing userspace programs
  326. * translate the former values to the new values.
  327. *
  328. * XXX: Remove this wrapper in year 2025 (or later)
  329. */
  330. asmlinkage notrace long parisc_madvise(unsigned long start, size_t len_in, int behavior)
  331. {
  332. switch (behavior) {
  333. case 65: behavior = MADV_MERGEABLE; break;
  334. case 66: behavior = MADV_UNMERGEABLE; break;
  335. case 67: behavior = MADV_HUGEPAGE; break;
  336. case 68: behavior = MADV_NOHUGEPAGE; break;
  337. case 69: behavior = MADV_DONTDUMP; break;
  338. case 70: behavior = MADV_DODUMP; break;
  339. case 71: behavior = MADV_WIPEONFORK; break;
  340. case 72: behavior = MADV_KEEPONFORK; break;
  341. case 73: behavior = MADV_COLLAPSE; break;
  342. }
  343. return sys_madvise(start, len_in, behavior);
  344. }