sys_parisc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*
  2. * PARISC specific syscalls
  3. *
  4. * Copyright (C) 1999-2003 Matthew Wilcox <willy at parisc-linux.org>
  5. * Copyright (C) 2000-2003 Paul Bame <bame at parisc-linux.org>
  6. * Copyright (C) 2001 Thomas Bogendoerfer <tsbogend at parisc-linux.org>
  7. * Copyright (C) 1999-2014 Helge Deller <deller@gmx.de>
  8. *
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. */
  24. #include <linux/uaccess.h>
  25. #include <asm/elf.h>
  26. #include <linux/file.h>
  27. #include <linux/fs.h>
  28. #include <linux/linkage.h>
  29. #include <linux/mm.h>
  30. #include <linux/mman.h>
  31. #include <linux/sched/signal.h>
  32. #include <linux/sched/mm.h>
  33. #include <linux/shm.h>
  34. #include <linux/syscalls.h>
  35. #include <linux/utsname.h>
  36. #include <linux/personality.h>
  37. #include <linux/random.h>
  38. /* we construct an artificial offset for the mapping based on the physical
  39. * address of the kernel mapping variable */
  40. #define GET_LAST_MMAP(filp) \
  41. (filp ? ((unsigned long) filp->f_mapping) >> 8 : 0UL)
  42. #define SET_LAST_MMAP(filp, val) \
  43. { /* nothing */ }
  44. static int get_offset(unsigned int last_mmap)
  45. {
  46. return (last_mmap & (SHM_COLOUR-1)) >> PAGE_SHIFT;
  47. }
  48. static unsigned long shared_align_offset(unsigned int last_mmap,
  49. unsigned long pgoff)
  50. {
  51. return (get_offset(last_mmap) + pgoff) << PAGE_SHIFT;
  52. }
  53. static inline unsigned long COLOR_ALIGN(unsigned long addr,
  54. unsigned int last_mmap, unsigned long pgoff)
  55. {
  56. unsigned long base = (addr+SHM_COLOUR-1) & ~(SHM_COLOUR-1);
  57. unsigned long off = (SHM_COLOUR-1) &
  58. (shared_align_offset(last_mmap, pgoff) << PAGE_SHIFT);
  59. return base + off;
  60. }
  61. /*
  62. * Top of mmap area (just below the process stack).
  63. */
  64. /*
  65. * When called from arch_get_unmapped_area(), rlim_stack will be NULL,
  66. * indicating that "current" should be used instead of a passed-in
  67. * value from the exec bprm as done with arch_pick_mmap_layout().
  68. */
  69. static unsigned long mmap_upper_limit(struct rlimit *rlim_stack)
  70. {
  71. unsigned long stack_base;
  72. /* Limit stack size - see setup_arg_pages() in fs/exec.c */
  73. stack_base = rlim_stack ? rlim_stack->rlim_max
  74. : rlimit_max(RLIMIT_STACK);
  75. if (stack_base > STACK_SIZE_MAX)
  76. stack_base = STACK_SIZE_MAX;
  77. /* Add space for stack randomization. */
  78. stack_base += (STACK_RND_MASK << PAGE_SHIFT);
  79. return PAGE_ALIGN(STACK_TOP - stack_base);
  80. }
  81. unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr,
  82. unsigned long len, unsigned long pgoff, unsigned long flags)
  83. {
  84. struct mm_struct *mm = current->mm;
  85. struct vm_area_struct *vma, *prev;
  86. unsigned long task_size = TASK_SIZE;
  87. int do_color_align, last_mmap;
  88. struct vm_unmapped_area_info info;
  89. if (len > task_size)
  90. return -ENOMEM;
  91. do_color_align = 0;
  92. if (filp || (flags & MAP_SHARED))
  93. do_color_align = 1;
  94. last_mmap = GET_LAST_MMAP(filp);
  95. if (flags & MAP_FIXED) {
  96. if ((flags & MAP_SHARED) && last_mmap &&
  97. (addr - shared_align_offset(last_mmap, pgoff))
  98. & (SHM_COLOUR - 1))
  99. return -EINVAL;
  100. goto found_addr;
  101. }
  102. if (addr) {
  103. if (do_color_align && last_mmap)
  104. addr = COLOR_ALIGN(addr, last_mmap, pgoff);
  105. else
  106. addr = PAGE_ALIGN(addr);
  107. vma = find_vma_prev(mm, addr, &prev);
  108. if (task_size - len >= addr &&
  109. (!vma || addr + len <= vm_start_gap(vma)) &&
  110. (!prev || addr >= vm_end_gap(prev)))
  111. goto found_addr;
  112. }
  113. info.flags = 0;
  114. info.length = len;
  115. info.low_limit = mm->mmap_legacy_base;
  116. info.high_limit = mmap_upper_limit(NULL);
  117. info.align_mask = last_mmap ? (PAGE_MASK & (SHM_COLOUR - 1)) : 0;
  118. info.align_offset = shared_align_offset(last_mmap, pgoff);
  119. addr = vm_unmapped_area(&info);
  120. found_addr:
  121. if (do_color_align && !last_mmap && !(addr & ~PAGE_MASK))
  122. SET_LAST_MMAP(filp, addr - (pgoff << PAGE_SHIFT));
  123. return addr;
  124. }
  125. unsigned long
  126. arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
  127. const unsigned long len, const unsigned long pgoff,
  128. const unsigned long flags)
  129. {
  130. struct vm_area_struct *vma, *prev;
  131. struct mm_struct *mm = current->mm;
  132. unsigned long addr = addr0;
  133. int do_color_align, last_mmap;
  134. struct vm_unmapped_area_info info;
  135. #ifdef CONFIG_64BIT
  136. /* This should only ever run for 32-bit processes. */
  137. BUG_ON(!test_thread_flag(TIF_32BIT));
  138. #endif
  139. /* requested length too big for entire address space */
  140. if (len > TASK_SIZE)
  141. return -ENOMEM;
  142. do_color_align = 0;
  143. if (filp || (flags & MAP_SHARED))
  144. do_color_align = 1;
  145. last_mmap = GET_LAST_MMAP(filp);
  146. if (flags & MAP_FIXED) {
  147. if ((flags & MAP_SHARED) && last_mmap &&
  148. (addr - shared_align_offset(last_mmap, pgoff))
  149. & (SHM_COLOUR - 1))
  150. return -EINVAL;
  151. goto found_addr;
  152. }
  153. /* requesting a specific address */
  154. if (addr) {
  155. if (do_color_align && last_mmap)
  156. addr = COLOR_ALIGN(addr, last_mmap, pgoff);
  157. else
  158. addr = PAGE_ALIGN(addr);
  159. vma = find_vma_prev(mm, addr, &prev);
  160. if (TASK_SIZE - len >= addr &&
  161. (!vma || addr + len <= vm_start_gap(vma)) &&
  162. (!prev || addr >= vm_end_gap(prev)))
  163. goto found_addr;
  164. }
  165. info.flags = VM_UNMAPPED_AREA_TOPDOWN;
  166. info.length = len;
  167. info.low_limit = PAGE_SIZE;
  168. info.high_limit = mm->mmap_base;
  169. info.align_mask = last_mmap ? (PAGE_MASK & (SHM_COLOUR - 1)) : 0;
  170. info.align_offset = shared_align_offset(last_mmap, pgoff);
  171. addr = vm_unmapped_area(&info);
  172. if (!(addr & ~PAGE_MASK))
  173. goto found_addr;
  174. VM_BUG_ON(addr != -ENOMEM);
  175. /*
  176. * A failed mmap() very likely causes application failure,
  177. * so fall back to the bottom-up function here. This scenario
  178. * can happen with large stack limits and large mmap()
  179. * allocations.
  180. */
  181. return arch_get_unmapped_area(filp, addr0, len, pgoff, flags);
  182. found_addr:
  183. if (do_color_align && !last_mmap && !(addr & ~PAGE_MASK))
  184. SET_LAST_MMAP(filp, addr - (pgoff << PAGE_SHIFT));
  185. return addr;
  186. }
  187. static int mmap_is_legacy(void)
  188. {
  189. if (current->personality & ADDR_COMPAT_LAYOUT)
  190. return 1;
  191. /* parisc stack always grows up - so a unlimited stack should
  192. * not be an indicator to use the legacy memory layout.
  193. * if (rlimit(RLIMIT_STACK) == RLIM_INFINITY)
  194. * return 1;
  195. */
  196. return sysctl_legacy_va_layout;
  197. }
  198. static unsigned long mmap_rnd(void)
  199. {
  200. unsigned long rnd = 0;
  201. if (current->flags & PF_RANDOMIZE)
  202. rnd = get_random_int() & MMAP_RND_MASK;
  203. return rnd << PAGE_SHIFT;
  204. }
  205. unsigned long arch_mmap_rnd(void)
  206. {
  207. return (get_random_int() & MMAP_RND_MASK) << PAGE_SHIFT;
  208. }
  209. static unsigned long mmap_legacy_base(void)
  210. {
  211. return TASK_UNMAPPED_BASE + mmap_rnd();
  212. }
  213. /*
  214. * This function, called very early during the creation of a new
  215. * process VM image, sets up which VM layout function to use:
  216. */
  217. void arch_pick_mmap_layout(struct mm_struct *mm, struct rlimit *rlim_stack)
  218. {
  219. mm->mmap_legacy_base = mmap_legacy_base();
  220. mm->mmap_base = mmap_upper_limit(rlim_stack);
  221. if (mmap_is_legacy()) {
  222. mm->mmap_base = mm->mmap_legacy_base;
  223. mm->get_unmapped_area = arch_get_unmapped_area;
  224. } else {
  225. mm->get_unmapped_area = arch_get_unmapped_area_topdown;
  226. }
  227. }
  228. asmlinkage unsigned long sys_mmap2(unsigned long addr, unsigned long len,
  229. unsigned long prot, unsigned long flags, unsigned long fd,
  230. unsigned long pgoff)
  231. {
  232. /* Make sure the shift for mmap2 is constant (12), no matter what PAGE_SIZE
  233. we have. */
  234. return ksys_mmap_pgoff(addr, len, prot, flags, fd,
  235. pgoff >> (PAGE_SHIFT - 12));
  236. }
  237. asmlinkage unsigned long sys_mmap(unsigned long addr, unsigned long len,
  238. unsigned long prot, unsigned long flags, unsigned long fd,
  239. unsigned long offset)
  240. {
  241. if (!(offset & ~PAGE_MASK)) {
  242. return ksys_mmap_pgoff(addr, len, prot, flags, fd,
  243. offset >> PAGE_SHIFT);
  244. } else {
  245. return -EINVAL;
  246. }
  247. }
  248. /* Fucking broken ABI */
  249. #ifdef CONFIG_64BIT
  250. asmlinkage long parisc_truncate64(const char __user * path,
  251. unsigned int high, unsigned int low)
  252. {
  253. return ksys_truncate(path, (long)high << 32 | low);
  254. }
  255. asmlinkage long parisc_ftruncate64(unsigned int fd,
  256. unsigned int high, unsigned int low)
  257. {
  258. return ksys_ftruncate(fd, (long)high << 32 | low);
  259. }
  260. /* stubs for the benefit of the syscall_table since truncate64 and truncate
  261. * are identical on LP64 */
  262. asmlinkage long sys_truncate64(const char __user * path, unsigned long length)
  263. {
  264. return ksys_truncate(path, length);
  265. }
  266. asmlinkage long sys_ftruncate64(unsigned int fd, unsigned long length)
  267. {
  268. return ksys_ftruncate(fd, length);
  269. }
  270. asmlinkage long sys_fcntl64(unsigned int fd, unsigned int cmd, unsigned long arg)
  271. {
  272. return sys_fcntl(fd, cmd, arg);
  273. }
  274. #else
  275. asmlinkage long parisc_truncate64(const char __user * path,
  276. unsigned int high, unsigned int low)
  277. {
  278. return ksys_truncate(path, (loff_t)high << 32 | low);
  279. }
  280. asmlinkage long parisc_ftruncate64(unsigned int fd,
  281. unsigned int high, unsigned int low)
  282. {
  283. return sys_ftruncate64(fd, (loff_t)high << 32 | low);
  284. }
  285. #endif
  286. asmlinkage ssize_t parisc_pread64(unsigned int fd, char __user *buf, size_t count,
  287. unsigned int high, unsigned int low)
  288. {
  289. return ksys_pread64(fd, buf, count, (loff_t)high << 32 | low);
  290. }
  291. asmlinkage ssize_t parisc_pwrite64(unsigned int fd, const char __user *buf,
  292. size_t count, unsigned int high, unsigned int low)
  293. {
  294. return ksys_pwrite64(fd, buf, count, (loff_t)high << 32 | low);
  295. }
  296. asmlinkage ssize_t parisc_readahead(int fd, unsigned int high, unsigned int low,
  297. size_t count)
  298. {
  299. return ksys_readahead(fd, (loff_t)high << 32 | low, count);
  300. }
  301. asmlinkage long parisc_fadvise64_64(int fd,
  302. unsigned int high_off, unsigned int low_off,
  303. unsigned int high_len, unsigned int low_len, int advice)
  304. {
  305. return ksys_fadvise64_64(fd, (loff_t)high_off << 32 | low_off,
  306. (loff_t)high_len << 32 | low_len, advice);
  307. }
  308. asmlinkage long parisc_sync_file_range(int fd,
  309. u32 hi_off, u32 lo_off, u32 hi_nbytes, u32 lo_nbytes,
  310. unsigned int flags)
  311. {
  312. return ksys_sync_file_range(fd, (loff_t)hi_off << 32 | lo_off,
  313. (loff_t)hi_nbytes << 32 | lo_nbytes, flags);
  314. }
  315. asmlinkage long parisc_fallocate(int fd, int mode, u32 offhi, u32 offlo,
  316. u32 lenhi, u32 lenlo)
  317. {
  318. return ksys_fallocate(fd, mode, ((u64)offhi << 32) | offlo,
  319. ((u64)lenhi << 32) | lenlo);
  320. }
  321. long parisc_personality(unsigned long personality)
  322. {
  323. long err;
  324. if (personality(current->personality) == PER_LINUX32
  325. && personality(personality) == PER_LINUX)
  326. personality = (personality & ~PER_MASK) | PER_LINUX32;
  327. err = sys_personality(personality);
  328. if (personality(err) == PER_LINUX32)
  329. err = (err & ~PER_MASK) | PER_LINUX;
  330. return err;
  331. }