sys_parisc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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 <asm/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/shm.h>
  32. #include <linux/syscalls.h>
  33. #include <linux/utsname.h>
  34. #include <linux/personality.h>
  35. #include <linux/random.h>
  36. /* we construct an artificial offset for the mapping based on the physical
  37. * address of the kernel mapping variable */
  38. #define GET_LAST_MMAP(filp) \
  39. (filp ? ((unsigned long) filp->f_mapping) >> 8 : 0UL)
  40. #define SET_LAST_MMAP(filp, val) \
  41. { /* nothing */ }
  42. static int get_offset(unsigned int last_mmap)
  43. {
  44. return (last_mmap & (SHM_COLOUR-1)) >> PAGE_SHIFT;
  45. }
  46. static unsigned long shared_align_offset(unsigned int last_mmap,
  47. unsigned long pgoff)
  48. {
  49. return (get_offset(last_mmap) + pgoff) << PAGE_SHIFT;
  50. }
  51. static inline unsigned long COLOR_ALIGN(unsigned long addr,
  52. unsigned int last_mmap, unsigned long pgoff)
  53. {
  54. unsigned long base = (addr+SHM_COLOUR-1) & ~(SHM_COLOUR-1);
  55. unsigned long off = (SHM_COLOUR-1) &
  56. (shared_align_offset(last_mmap, pgoff) << PAGE_SHIFT);
  57. return base + off;
  58. }
  59. /*
  60. * Top of mmap area (just below the process stack).
  61. */
  62. static unsigned long mmap_upper_limit(void)
  63. {
  64. unsigned long stack_base;
  65. /* Limit stack size - see setup_arg_pages() in fs/exec.c */
  66. stack_base = rlimit_max(RLIMIT_STACK);
  67. if (stack_base > STACK_SIZE_MAX)
  68. stack_base = STACK_SIZE_MAX;
  69. /* Add space for stack randomization. */
  70. stack_base += (STACK_RND_MASK << PAGE_SHIFT);
  71. return PAGE_ALIGN(STACK_TOP - stack_base);
  72. }
  73. unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr,
  74. unsigned long len, unsigned long pgoff, unsigned long flags)
  75. {
  76. struct mm_struct *mm = current->mm;
  77. struct vm_area_struct *vma, *prev;
  78. unsigned long task_size = TASK_SIZE;
  79. int do_color_align, last_mmap;
  80. struct vm_unmapped_area_info info;
  81. if (len > task_size)
  82. return -ENOMEM;
  83. do_color_align = 0;
  84. if (filp || (flags & MAP_SHARED))
  85. do_color_align = 1;
  86. last_mmap = GET_LAST_MMAP(filp);
  87. if (flags & MAP_FIXED) {
  88. if ((flags & MAP_SHARED) && last_mmap &&
  89. (addr - shared_align_offset(last_mmap, pgoff))
  90. & (SHM_COLOUR - 1))
  91. return -EINVAL;
  92. goto found_addr;
  93. }
  94. if (addr) {
  95. if (do_color_align && last_mmap)
  96. addr = COLOR_ALIGN(addr, last_mmap, pgoff);
  97. else
  98. addr = PAGE_ALIGN(addr);
  99. vma = find_vma_prev(mm, addr, &prev);
  100. if (task_size - len >= addr &&
  101. (!vma || addr + len <= vm_start_gap(vma)) &&
  102. (!prev || addr >= vm_end_gap(prev)))
  103. goto found_addr;
  104. }
  105. info.flags = 0;
  106. info.length = len;
  107. info.low_limit = mm->mmap_legacy_base;
  108. info.high_limit = mmap_upper_limit();
  109. info.align_mask = last_mmap ? (PAGE_MASK & (SHM_COLOUR - 1)) : 0;
  110. info.align_offset = shared_align_offset(last_mmap, pgoff);
  111. addr = vm_unmapped_area(&info);
  112. found_addr:
  113. if (do_color_align && !last_mmap && !(addr & ~PAGE_MASK))
  114. SET_LAST_MMAP(filp, addr - (pgoff << PAGE_SHIFT));
  115. return addr;
  116. }
  117. unsigned long
  118. arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
  119. const unsigned long len, const unsigned long pgoff,
  120. const unsigned long flags)
  121. {
  122. struct vm_area_struct *vma, *prev;
  123. struct mm_struct *mm = current->mm;
  124. unsigned long addr = addr0;
  125. int do_color_align, last_mmap;
  126. struct vm_unmapped_area_info info;
  127. #ifdef CONFIG_64BIT
  128. /* This should only ever run for 32-bit processes. */
  129. BUG_ON(!test_thread_flag(TIF_32BIT));
  130. #endif
  131. /* requested length too big for entire address space */
  132. if (len > TASK_SIZE)
  133. return -ENOMEM;
  134. do_color_align = 0;
  135. if (filp || (flags & MAP_SHARED))
  136. do_color_align = 1;
  137. last_mmap = GET_LAST_MMAP(filp);
  138. if (flags & MAP_FIXED) {
  139. if ((flags & MAP_SHARED) && last_mmap &&
  140. (addr - shared_align_offset(last_mmap, pgoff))
  141. & (SHM_COLOUR - 1))
  142. return -EINVAL;
  143. goto found_addr;
  144. }
  145. /* requesting a specific address */
  146. if (addr) {
  147. if (do_color_align && last_mmap)
  148. addr = COLOR_ALIGN(addr, last_mmap, pgoff);
  149. else
  150. addr = PAGE_ALIGN(addr);
  151. vma = find_vma_prev(mm, addr, &prev);
  152. if (TASK_SIZE - len >= addr &&
  153. (!vma || addr + len <= vm_start_gap(vma)) &&
  154. (!prev || addr >= vm_end_gap(prev)))
  155. goto found_addr;
  156. }
  157. info.flags = VM_UNMAPPED_AREA_TOPDOWN;
  158. info.length = len;
  159. info.low_limit = PAGE_SIZE;
  160. info.high_limit = mm->mmap_base;
  161. info.align_mask = last_mmap ? (PAGE_MASK & (SHM_COLOUR - 1)) : 0;
  162. info.align_offset = shared_align_offset(last_mmap, pgoff);
  163. addr = vm_unmapped_area(&info);
  164. if (!(addr & ~PAGE_MASK))
  165. goto found_addr;
  166. VM_BUG_ON(addr != -ENOMEM);
  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. return arch_get_unmapped_area(filp, addr0, len, pgoff, flags);
  174. found_addr:
  175. if (do_color_align && !last_mmap && !(addr & ~PAGE_MASK))
  176. SET_LAST_MMAP(filp, addr - (pgoff << PAGE_SHIFT));
  177. return addr;
  178. }
  179. static int mmap_is_legacy(void)
  180. {
  181. if (current->personality & ADDR_COMPAT_LAYOUT)
  182. return 1;
  183. /* parisc stack always grows up - so a unlimited stack should
  184. * not be an indicator to use the legacy memory layout.
  185. * if (rlimit(RLIMIT_STACK) == RLIM_INFINITY)
  186. * return 1;
  187. */
  188. return sysctl_legacy_va_layout;
  189. }
  190. static unsigned long mmap_rnd(void)
  191. {
  192. unsigned long rnd = 0;
  193. /*
  194. * 8 bits of randomness in 32bit mmaps, 20 address space bits
  195. * 28 bits of randomness in 64bit mmaps, 40 address space bits
  196. */
  197. if (current->flags & PF_RANDOMIZE) {
  198. if (is_32bit_task())
  199. rnd = get_random_int() % (1<<8);
  200. else
  201. rnd = get_random_int() % (1<<28);
  202. }
  203. return rnd << PAGE_SHIFT;
  204. }
  205. static unsigned long mmap_legacy_base(void)
  206. {
  207. return TASK_UNMAPPED_BASE + mmap_rnd();
  208. }
  209. /*
  210. * This function, called very early during the creation of a new
  211. * process VM image, sets up which VM layout function to use:
  212. */
  213. void arch_pick_mmap_layout(struct mm_struct *mm)
  214. {
  215. mm->mmap_legacy_base = mmap_legacy_base();
  216. mm->mmap_base = mmap_upper_limit();
  217. if (mmap_is_legacy()) {
  218. mm->mmap_base = mm->mmap_legacy_base;
  219. mm->get_unmapped_area = arch_get_unmapped_area;
  220. } else {
  221. mm->get_unmapped_area = arch_get_unmapped_area_topdown;
  222. }
  223. }
  224. asmlinkage unsigned long sys_mmap2(unsigned long addr, unsigned long len,
  225. unsigned long prot, unsigned long flags, unsigned long fd,
  226. unsigned long pgoff)
  227. {
  228. /* Make sure the shift for mmap2 is constant (12), no matter what PAGE_SIZE
  229. we have. */
  230. return sys_mmap_pgoff(addr, len, prot, flags, fd,
  231. pgoff >> (PAGE_SHIFT - 12));
  232. }
  233. asmlinkage unsigned long sys_mmap(unsigned long addr, unsigned long len,
  234. unsigned long prot, unsigned long flags, unsigned long fd,
  235. unsigned long offset)
  236. {
  237. if (!(offset & ~PAGE_MASK)) {
  238. return sys_mmap_pgoff(addr, len, prot, flags, fd,
  239. offset >> PAGE_SHIFT);
  240. } else {
  241. return -EINVAL;
  242. }
  243. }
  244. /* Fucking broken ABI */
  245. #ifdef CONFIG_64BIT
  246. asmlinkage long parisc_truncate64(const char __user * path,
  247. unsigned int high, unsigned int low)
  248. {
  249. return sys_truncate(path, (long)high << 32 | low);
  250. }
  251. asmlinkage long parisc_ftruncate64(unsigned int fd,
  252. unsigned int high, unsigned int low)
  253. {
  254. return sys_ftruncate(fd, (long)high << 32 | low);
  255. }
  256. /* stubs for the benefit of the syscall_table since truncate64 and truncate
  257. * are identical on LP64 */
  258. asmlinkage long sys_truncate64(const char __user * path, unsigned long length)
  259. {
  260. return sys_truncate(path, length);
  261. }
  262. asmlinkage long sys_ftruncate64(unsigned int fd, unsigned long length)
  263. {
  264. return sys_ftruncate(fd, length);
  265. }
  266. asmlinkage long sys_fcntl64(unsigned int fd, unsigned int cmd, unsigned long arg)
  267. {
  268. return sys_fcntl(fd, cmd, arg);
  269. }
  270. #else
  271. asmlinkage long parisc_truncate64(const char __user * path,
  272. unsigned int high, unsigned int low)
  273. {
  274. return sys_truncate64(path, (loff_t)high << 32 | low);
  275. }
  276. asmlinkage long parisc_ftruncate64(unsigned int fd,
  277. unsigned int high, unsigned int low)
  278. {
  279. return sys_ftruncate64(fd, (loff_t)high << 32 | low);
  280. }
  281. #endif
  282. asmlinkage ssize_t parisc_pread64(unsigned int fd, char __user *buf, size_t count,
  283. unsigned int high, unsigned int low)
  284. {
  285. return sys_pread64(fd, buf, count, (loff_t)high << 32 | low);
  286. }
  287. asmlinkage ssize_t parisc_pwrite64(unsigned int fd, const char __user *buf,
  288. size_t count, unsigned int high, unsigned int low)
  289. {
  290. return sys_pwrite64(fd, buf, count, (loff_t)high << 32 | low);
  291. }
  292. asmlinkage ssize_t parisc_readahead(int fd, unsigned int high, unsigned int low,
  293. size_t count)
  294. {
  295. return sys_readahead(fd, (loff_t)high << 32 | low, count);
  296. }
  297. asmlinkage long parisc_fadvise64_64(int fd,
  298. unsigned int high_off, unsigned int low_off,
  299. unsigned int high_len, unsigned int low_len, int advice)
  300. {
  301. return sys_fadvise64_64(fd, (loff_t)high_off << 32 | low_off,
  302. (loff_t)high_len << 32 | low_len, advice);
  303. }
  304. asmlinkage long parisc_sync_file_range(int fd,
  305. u32 hi_off, u32 lo_off, u32 hi_nbytes, u32 lo_nbytes,
  306. unsigned int flags)
  307. {
  308. return sys_sync_file_range(fd, (loff_t)hi_off << 32 | lo_off,
  309. (loff_t)hi_nbytes << 32 | lo_nbytes, flags);
  310. }
  311. asmlinkage long parisc_fallocate(int fd, int mode, u32 offhi, u32 offlo,
  312. u32 lenhi, u32 lenlo)
  313. {
  314. return sys_fallocate(fd, mode, ((u64)offhi << 32) | offlo,
  315. ((u64)lenhi << 32) | lenlo);
  316. }
  317. long parisc_personality(unsigned long personality)
  318. {
  319. long err;
  320. if (personality(current->personality) == PER_LINUX32
  321. && personality(personality) == PER_LINUX)
  322. personality = (personality & ~PER_MASK) | PER_LINUX32;
  323. err = sys_personality(personality);
  324. if (personality(err) == PER_LINUX32)
  325. err = (err & ~PER_MASK) | PER_LINUX;
  326. return err;
  327. }