sys_parisc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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;
  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(mm, addr);
  100. if (task_size - len >= addr &&
  101. (!vma || addr + len <= vma->vm_start))
  102. goto found_addr;
  103. }
  104. info.flags = 0;
  105. info.length = len;
  106. info.low_limit = mm->mmap_legacy_base;
  107. info.high_limit = mmap_upper_limit();
  108. info.align_mask = last_mmap ? (PAGE_MASK & (SHM_COLOUR - 1)) : 0;
  109. info.align_offset = shared_align_offset(last_mmap, pgoff);
  110. addr = vm_unmapped_area(&info);
  111. found_addr:
  112. if (do_color_align && !last_mmap && !(addr & ~PAGE_MASK))
  113. SET_LAST_MMAP(filp, addr - (pgoff << PAGE_SHIFT));
  114. return addr;
  115. }
  116. unsigned long
  117. arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
  118. const unsigned long len, const unsigned long pgoff,
  119. const unsigned long flags)
  120. {
  121. struct vm_area_struct *vma;
  122. struct mm_struct *mm = current->mm;
  123. unsigned long addr = addr0;
  124. int do_color_align, last_mmap;
  125. struct vm_unmapped_area_info info;
  126. #ifdef CONFIG_64BIT
  127. /* This should only ever run for 32-bit processes. */
  128. BUG_ON(!test_thread_flag(TIF_32BIT));
  129. #endif
  130. /* requested length too big for entire address space */
  131. if (len > TASK_SIZE)
  132. return -ENOMEM;
  133. do_color_align = 0;
  134. if (filp || (flags & MAP_SHARED))
  135. do_color_align = 1;
  136. last_mmap = GET_LAST_MMAP(filp);
  137. if (flags & MAP_FIXED) {
  138. if ((flags & MAP_SHARED) && last_mmap &&
  139. (addr - shared_align_offset(last_mmap, pgoff))
  140. & (SHM_COLOUR - 1))
  141. return -EINVAL;
  142. goto found_addr;
  143. }
  144. /* requesting a specific address */
  145. if (addr) {
  146. if (do_color_align && last_mmap)
  147. addr = COLOR_ALIGN(addr, last_mmap, pgoff);
  148. else
  149. addr = PAGE_ALIGN(addr);
  150. vma = find_vma(mm, addr);
  151. if (TASK_SIZE - len >= addr &&
  152. (!vma || addr + len <= vma->vm_start))
  153. goto found_addr;
  154. }
  155. info.flags = VM_UNMAPPED_AREA_TOPDOWN;
  156. info.length = len;
  157. info.low_limit = PAGE_SIZE;
  158. info.high_limit = mm->mmap_base;
  159. info.align_mask = last_mmap ? (PAGE_MASK & (SHM_COLOUR - 1)) : 0;
  160. info.align_offset = shared_align_offset(last_mmap, pgoff);
  161. addr = vm_unmapped_area(&info);
  162. if (!(addr & ~PAGE_MASK))
  163. goto found_addr;
  164. VM_BUG_ON(addr != -ENOMEM);
  165. /*
  166. * A failed mmap() very likely causes application failure,
  167. * so fall back to the bottom-up function here. This scenario
  168. * can happen with large stack limits and large mmap()
  169. * allocations.
  170. */
  171. return arch_get_unmapped_area(filp, addr0, len, pgoff, flags);
  172. found_addr:
  173. if (do_color_align && !last_mmap && !(addr & ~PAGE_MASK))
  174. SET_LAST_MMAP(filp, addr - (pgoff << PAGE_SHIFT));
  175. return addr;
  176. }
  177. static int mmap_is_legacy(void)
  178. {
  179. if (current->personality & ADDR_COMPAT_LAYOUT)
  180. return 1;
  181. /* parisc stack always grows up - so a unlimited stack should
  182. * not be an indicator to use the legacy memory layout.
  183. * if (rlimit(RLIMIT_STACK) == RLIM_INFINITY)
  184. * return 1;
  185. */
  186. return sysctl_legacy_va_layout;
  187. }
  188. static unsigned long mmap_rnd(void)
  189. {
  190. unsigned long rnd = 0;
  191. /*
  192. * 8 bits of randomness in 32bit mmaps, 20 address space bits
  193. * 28 bits of randomness in 64bit mmaps, 40 address space bits
  194. */
  195. if (current->flags & PF_RANDOMIZE) {
  196. if (is_32bit_task())
  197. rnd = get_random_int() % (1<<8);
  198. else
  199. rnd = get_random_int() % (1<<28);
  200. }
  201. return rnd << PAGE_SHIFT;
  202. }
  203. static unsigned long mmap_legacy_base(void)
  204. {
  205. return TASK_UNMAPPED_BASE + mmap_rnd();
  206. }
  207. /*
  208. * This function, called very early during the creation of a new
  209. * process VM image, sets up which VM layout function to use:
  210. */
  211. void arch_pick_mmap_layout(struct mm_struct *mm)
  212. {
  213. mm->mmap_legacy_base = mmap_legacy_base();
  214. mm->mmap_base = mmap_upper_limit();
  215. if (mmap_is_legacy()) {
  216. mm->mmap_base = mm->mmap_legacy_base;
  217. mm->get_unmapped_area = arch_get_unmapped_area;
  218. } else {
  219. mm->get_unmapped_area = arch_get_unmapped_area_topdown;
  220. }
  221. }
  222. asmlinkage unsigned long sys_mmap2(unsigned long addr, unsigned long len,
  223. unsigned long prot, unsigned long flags, unsigned long fd,
  224. unsigned long pgoff)
  225. {
  226. /* Make sure the shift for mmap2 is constant (12), no matter what PAGE_SIZE
  227. we have. */
  228. return sys_mmap_pgoff(addr, len, prot, flags, fd,
  229. pgoff >> (PAGE_SHIFT - 12));
  230. }
  231. asmlinkage unsigned long sys_mmap(unsigned long addr, unsigned long len,
  232. unsigned long prot, unsigned long flags, unsigned long fd,
  233. unsigned long offset)
  234. {
  235. if (!(offset & ~PAGE_MASK)) {
  236. return sys_mmap_pgoff(addr, len, prot, flags, fd,
  237. offset >> PAGE_SHIFT);
  238. } else {
  239. return -EINVAL;
  240. }
  241. }
  242. /* Fucking broken ABI */
  243. #ifdef CONFIG_64BIT
  244. asmlinkage long parisc_truncate64(const char __user * path,
  245. unsigned int high, unsigned int low)
  246. {
  247. return sys_truncate(path, (long)high << 32 | low);
  248. }
  249. asmlinkage long parisc_ftruncate64(unsigned int fd,
  250. unsigned int high, unsigned int low)
  251. {
  252. return sys_ftruncate(fd, (long)high << 32 | low);
  253. }
  254. /* stubs for the benefit of the syscall_table since truncate64 and truncate
  255. * are identical on LP64 */
  256. asmlinkage long sys_truncate64(const char __user * path, unsigned long length)
  257. {
  258. return sys_truncate(path, length);
  259. }
  260. asmlinkage long sys_ftruncate64(unsigned int fd, unsigned long length)
  261. {
  262. return sys_ftruncate(fd, length);
  263. }
  264. asmlinkage long sys_fcntl64(unsigned int fd, unsigned int cmd, unsigned long arg)
  265. {
  266. return sys_fcntl(fd, cmd, arg);
  267. }
  268. #else
  269. asmlinkage long parisc_truncate64(const char __user * path,
  270. unsigned int high, unsigned int low)
  271. {
  272. return sys_truncate64(path, (loff_t)high << 32 | low);
  273. }
  274. asmlinkage long parisc_ftruncate64(unsigned int fd,
  275. unsigned int high, unsigned int low)
  276. {
  277. return sys_ftruncate64(fd, (loff_t)high << 32 | low);
  278. }
  279. #endif
  280. asmlinkage ssize_t parisc_pread64(unsigned int fd, char __user *buf, size_t count,
  281. unsigned int high, unsigned int low)
  282. {
  283. return sys_pread64(fd, buf, count, (loff_t)high << 32 | low);
  284. }
  285. asmlinkage ssize_t parisc_pwrite64(unsigned int fd, const char __user *buf,
  286. size_t count, unsigned int high, unsigned int low)
  287. {
  288. return sys_pwrite64(fd, buf, count, (loff_t)high << 32 | low);
  289. }
  290. asmlinkage ssize_t parisc_readahead(int fd, unsigned int high, unsigned int low,
  291. size_t count)
  292. {
  293. return sys_readahead(fd, (loff_t)high << 32 | low, count);
  294. }
  295. asmlinkage long parisc_fadvise64_64(int fd,
  296. unsigned int high_off, unsigned int low_off,
  297. unsigned int high_len, unsigned int low_len, int advice)
  298. {
  299. return sys_fadvise64_64(fd, (loff_t)high_off << 32 | low_off,
  300. (loff_t)high_len << 32 | low_len, advice);
  301. }
  302. asmlinkage long parisc_sync_file_range(int fd,
  303. u32 hi_off, u32 lo_off, u32 hi_nbytes, u32 lo_nbytes,
  304. unsigned int flags)
  305. {
  306. return sys_sync_file_range(fd, (loff_t)hi_off << 32 | lo_off,
  307. (loff_t)hi_nbytes << 32 | lo_nbytes, flags);
  308. }
  309. asmlinkage long parisc_fallocate(int fd, int mode, u32 offhi, u32 offlo,
  310. u32 lenhi, u32 lenlo)
  311. {
  312. return sys_fallocate(fd, mode, ((u64)offhi << 32) | offlo,
  313. ((u64)lenhi << 32) | lenlo);
  314. }
  315. asmlinkage unsigned long sys_alloc_hugepages(int key, unsigned long addr, unsigned long len, int prot, int flag)
  316. {
  317. return -ENOMEM;
  318. }
  319. asmlinkage int sys_free_hugepages(unsigned long addr)
  320. {
  321. return -EINVAL;
  322. }
  323. long parisc_personality(unsigned long personality)
  324. {
  325. long err;
  326. if (personality(current->personality) == PER_LINUX32
  327. && personality(personality) == PER_LINUX)
  328. personality = (personality & ~PER_MASK) | PER_LINUX32;
  329. err = sys_personality(personality);
  330. if (personality(err) == PER_LINUX32)
  331. err = (err & ~PER_MASK) | PER_LINUX;
  332. return err;
  333. }