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