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 #define STACK_SIZE_DEFAULT (USER_WIDE_MODE \ 54 ? (1 << 30) /* 1 GB */ \ 55 : (CONFIG_STACK_MAX_DEFAULT_SIZE_MB*1024*1024)) 56 57 unsigned long calc_max_stack_size(unsigned long stack_max) 58 { 59 #ifdef CONFIG_COMPAT 60 if (!USER_WIDE_MODE && (stack_max == COMPAT_RLIM_INFINITY)) 61 stack_max = STACK_SIZE_DEFAULT; 62 else 63 #endif 64 if (stack_max == RLIM_INFINITY) 65 stack_max = STACK_SIZE_DEFAULT; 66 67 return stack_max; 68 } 69 70 71 /* 72 * Top of mmap area (just below the process stack). 73 */ 74 75 /* 76 * When called from arch_get_unmapped_area(), rlim_stack will be NULL, 77 * indicating that "current" should be used instead of a passed-in 78 * value from the exec bprm as done with arch_pick_mmap_layout(). 79 */ 80 static unsigned long mmap_upper_limit(struct rlimit *rlim_stack) 81 { 82 unsigned long stack_base; 83 84 /* Limit stack size - see setup_arg_pages() in fs/exec.c */ 85 stack_base = rlim_stack ? rlim_stack->rlim_max 86 : rlimit_max(RLIMIT_STACK); 87 88 stack_base = calc_max_stack_size(stack_base); 89 90 /* Add space for stack randomization. */ 91 if (current->flags & PF_RANDOMIZE) 92 stack_base += (STACK_RND_MASK << PAGE_SHIFT); 93 94 return PAGE_ALIGN(STACK_TOP - stack_base); 95 } 96 97 enum mmap_allocation_direction {UP, DOWN}; 98 99 static unsigned long arch_get_unmapped_area_common(struct file *filp, 100 unsigned long addr, unsigned long len, unsigned long pgoff, 101 unsigned long flags, enum mmap_allocation_direction dir) 102 { 103 struct mm_struct *mm = current->mm; 104 struct vm_area_struct *vma, *prev; 105 unsigned long filp_pgoff; 106 int do_color_align; 107 struct vm_unmapped_area_info info; 108 109 if (unlikely(len > TASK_SIZE)) 110 return -ENOMEM; 111 112 do_color_align = 0; 113 if (filp || (flags & MAP_SHARED)) 114 do_color_align = 1; 115 filp_pgoff = GET_FILP_PGOFF(filp); 116 117 if (flags & MAP_FIXED) { 118 /* Even MAP_FIXED mappings must reside within TASK_SIZE */ 119 if (TASK_SIZE - len < addr) 120 return -EINVAL; 121 122 if ((flags & MAP_SHARED) && filp && 123 (addr - shared_align_offset(filp_pgoff, pgoff)) 124 & (SHM_COLOUR - 1)) 125 return -EINVAL; 126 return addr; 127 } 128 129 if (addr) { 130 if (do_color_align) 131 addr = COLOR_ALIGN(addr, filp_pgoff, pgoff); 132 else 133 addr = PAGE_ALIGN(addr); 134 135 vma = find_vma_prev(mm, addr, &prev); 136 if (TASK_SIZE - len >= addr && 137 (!vma || addr + len <= vm_start_gap(vma)) && 138 (!prev || addr >= vm_end_gap(prev))) 139 return addr; 140 } 141 142 info.length = len; 143 info.align_mask = do_color_align ? (PAGE_MASK & (SHM_COLOUR - 1)) : 0; 144 info.align_offset = shared_align_offset(filp_pgoff, pgoff); 145 146 if (dir == DOWN) { 147 info.flags = VM_UNMAPPED_AREA_TOPDOWN; 148 info.low_limit = PAGE_SIZE; 149 info.high_limit = mm->mmap_base; 150 addr = vm_unmapped_area(&info); 151 if (!(addr & ~PAGE_MASK)) 152 return addr; 153 VM_BUG_ON(addr != -ENOMEM); 154 155 /* 156 * A failed mmap() very likely causes application failure, 157 * so fall back to the bottom-up function here. This scenario 158 * can happen with large stack limits and large mmap() 159 * allocations. 160 */ 161 } 162 163 info.flags = 0; 164 info.low_limit = mm->mmap_legacy_base; 165 info.high_limit = mmap_upper_limit(NULL); 166 return vm_unmapped_area(&info); 167 } 168 169 unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr, 170 unsigned long len, unsigned long pgoff, unsigned long flags) 171 { 172 return arch_get_unmapped_area_common(filp, 173 addr, len, pgoff, flags, UP); 174 } 175 176 unsigned long arch_get_unmapped_area_topdown(struct file *filp, 177 unsigned long addr, unsigned long len, unsigned long pgoff, 178 unsigned long flags) 179 { 180 return arch_get_unmapped_area_common(filp, 181 addr, len, pgoff, flags, DOWN); 182 } 183 184 static int mmap_is_legacy(void) 185 { 186 if (current->personality & ADDR_COMPAT_LAYOUT) 187 return 1; 188 189 /* parisc stack always grows up - so a unlimited stack should 190 * not be an indicator to use the legacy memory layout. 191 * if (rlimit(RLIMIT_STACK) == RLIM_INFINITY) 192 * return 1; 193 */ 194 195 return sysctl_legacy_va_layout; 196 } 197 198 static unsigned long mmap_rnd(void) 199 { 200 unsigned long rnd = 0; 201 202 if (current->flags & PF_RANDOMIZE) 203 rnd = get_random_u32() & MMAP_RND_MASK; 204 205 return rnd << PAGE_SHIFT; 206 } 207 208 unsigned long arch_mmap_rnd(void) 209 { 210 return (get_random_u32() & MMAP_RND_MASK) << PAGE_SHIFT; 211 } 212 213 static unsigned long mmap_legacy_base(void) 214 { 215 return TASK_UNMAPPED_BASE + mmap_rnd(); 216 } 217 218 /* 219 * This function, called very early during the creation of a new 220 * process VM image, sets up which VM layout function to use: 221 */ 222 void arch_pick_mmap_layout(struct mm_struct *mm, struct rlimit *rlim_stack) 223 { 224 mm->mmap_legacy_base = mmap_legacy_base(); 225 mm->mmap_base = mmap_upper_limit(rlim_stack); 226 227 if (mmap_is_legacy()) { 228 mm->mmap_base = mm->mmap_legacy_base; 229 mm->get_unmapped_area = arch_get_unmapped_area; 230 } else { 231 mm->get_unmapped_area = arch_get_unmapped_area_topdown; 232 } 233 } 234 235 236 asmlinkage unsigned long sys_mmap2(unsigned long addr, unsigned long len, 237 unsigned long prot, unsigned long flags, unsigned long fd, 238 unsigned long pgoff) 239 { 240 /* Make sure the shift for mmap2 is constant (12), no matter what PAGE_SIZE 241 we have. */ 242 return ksys_mmap_pgoff(addr, len, prot, flags, fd, 243 pgoff >> (PAGE_SHIFT - 12)); 244 } 245 246 asmlinkage unsigned long sys_mmap(unsigned long addr, unsigned long len, 247 unsigned long prot, unsigned long flags, unsigned long fd, 248 unsigned long offset) 249 { 250 if (!(offset & ~PAGE_MASK)) { 251 return ksys_mmap_pgoff(addr, len, prot, flags, fd, 252 offset >> PAGE_SHIFT); 253 } else { 254 return -EINVAL; 255 } 256 } 257 258 /* Fucking broken ABI */ 259 260 #ifdef CONFIG_64BIT 261 asmlinkage long parisc_truncate64(const char __user * path, 262 unsigned int high, unsigned int low) 263 { 264 return ksys_truncate(path, (long)high << 32 | low); 265 } 266 267 asmlinkage long parisc_ftruncate64(unsigned int fd, 268 unsigned int high, unsigned int low) 269 { 270 return ksys_ftruncate(fd, (long)high << 32 | low); 271 } 272 273 /* stubs for the benefit of the syscall_table since truncate64 and truncate 274 * are identical on LP64 */ 275 asmlinkage long sys_truncate64(const char __user * path, unsigned long length) 276 { 277 return ksys_truncate(path, length); 278 } 279 asmlinkage long sys_ftruncate64(unsigned int fd, unsigned long length) 280 { 281 return ksys_ftruncate(fd, length); 282 } 283 asmlinkage long sys_fcntl64(unsigned int fd, unsigned int cmd, unsigned long arg) 284 { 285 return sys_fcntl(fd, cmd, arg); 286 } 287 #else 288 289 asmlinkage long parisc_truncate64(const char __user * path, 290 unsigned int high, unsigned int low) 291 { 292 return ksys_truncate(path, (loff_t)high << 32 | low); 293 } 294 295 asmlinkage long parisc_ftruncate64(unsigned int fd, 296 unsigned int high, unsigned int low) 297 { 298 return sys_ftruncate64(fd, (loff_t)high << 32 | low); 299 } 300 #endif 301 302 asmlinkage ssize_t parisc_pread64(unsigned int fd, char __user *buf, size_t count, 303 unsigned int high, unsigned int low) 304 { 305 return ksys_pread64(fd, buf, count, (loff_t)high << 32 | low); 306 } 307 308 asmlinkage ssize_t parisc_pwrite64(unsigned int fd, const char __user *buf, 309 size_t count, unsigned int high, unsigned int low) 310 { 311 return ksys_pwrite64(fd, buf, count, (loff_t)high << 32 | low); 312 } 313 314 asmlinkage ssize_t parisc_readahead(int fd, unsigned int high, unsigned int low, 315 size_t count) 316 { 317 return ksys_readahead(fd, (loff_t)high << 32 | low, count); 318 } 319 320 asmlinkage long parisc_fadvise64_64(int fd, 321 unsigned int high_off, unsigned int low_off, 322 unsigned int high_len, unsigned int low_len, int advice) 323 { 324 return ksys_fadvise64_64(fd, (loff_t)high_off << 32 | low_off, 325 (loff_t)high_len << 32 | low_len, advice); 326 } 327 328 asmlinkage long parisc_sync_file_range(int fd, 329 u32 hi_off, u32 lo_off, u32 hi_nbytes, u32 lo_nbytes, 330 unsigned int flags) 331 { 332 return ksys_sync_file_range(fd, (loff_t)hi_off << 32 | lo_off, 333 (loff_t)hi_nbytes << 32 | lo_nbytes, flags); 334 } 335 336 asmlinkage long parisc_fallocate(int fd, int mode, u32 offhi, u32 offlo, 337 u32 lenhi, u32 lenlo) 338 { 339 return ksys_fallocate(fd, mode, ((u64)offhi << 32) | offlo, 340 ((u64)lenhi << 32) | lenlo); 341 } 342 343 asmlinkage long parisc_personality(unsigned long personality) 344 { 345 long err; 346 347 if (personality(current->personality) == PER_LINUX32 348 && personality(personality) == PER_LINUX) 349 personality = (personality & ~PER_MASK) | PER_LINUX32; 350 351 err = sys_personality(personality); 352 if (personality(err) == PER_LINUX32) 353 err = (err & ~PER_MASK) | PER_LINUX; 354 355 return err; 356 } 357 358 /* 359 * Up to kernel v5.9 we defined O_NONBLOCK as 000200004, 360 * since then O_NONBLOCK is defined as 000200000. 361 * 362 * The following wrapper functions mask out the old 363 * O_NDELAY bit from calls which use O_NONBLOCK. 364 * 365 * XXX: Remove those in year 2022 (or later)? 366 */ 367 368 #define O_NONBLOCK_OLD 000200004 369 #define O_NONBLOCK_MASK_OUT (O_NONBLOCK_OLD & ~O_NONBLOCK) 370 371 static int FIX_O_NONBLOCK(int flags) 372 { 373 if ((flags & O_NONBLOCK_MASK_OUT) && 374 !test_thread_flag(TIF_NONBLOCK_WARNING)) { 375 set_thread_flag(TIF_NONBLOCK_WARNING); 376 pr_warn("%s(%d) uses a deprecated O_NONBLOCK value." 377 " Please recompile with newer glibc.\n", 378 current->comm, current->pid); 379 } 380 return flags & ~O_NONBLOCK_MASK_OUT; 381 } 382 383 asmlinkage long parisc_timerfd_create(int clockid, int flags) 384 { 385 flags = FIX_O_NONBLOCK(flags); 386 return sys_timerfd_create(clockid, flags); 387 } 388 389 asmlinkage long parisc_signalfd4(int ufd, sigset_t __user *user_mask, 390 size_t sizemask, int flags) 391 { 392 flags = FIX_O_NONBLOCK(flags); 393 return sys_signalfd4(ufd, user_mask, sizemask, flags); 394 } 395 396 #ifdef CONFIG_COMPAT 397 asmlinkage long parisc_compat_signalfd4(int ufd, 398 compat_sigset_t __user *user_mask, 399 compat_size_t sizemask, int flags) 400 { 401 flags = FIX_O_NONBLOCK(flags); 402 return compat_sys_signalfd4(ufd, user_mask, sizemask, flags); 403 } 404 #endif 405 406 asmlinkage long parisc_eventfd2(unsigned int count, int flags) 407 { 408 flags = FIX_O_NONBLOCK(flags); 409 return sys_eventfd2(count, flags); 410 } 411 412 asmlinkage long parisc_userfaultfd(int flags) 413 { 414 flags = FIX_O_NONBLOCK(flags); 415 return sys_userfaultfd(flags); 416 } 417 418 asmlinkage long parisc_pipe2(int __user *fildes, int flags) 419 { 420 flags = FIX_O_NONBLOCK(flags); 421 return sys_pipe2(fildes, flags); 422 } 423 424 asmlinkage long parisc_inotify_init1(int flags) 425 { 426 flags = FIX_O_NONBLOCK(flags); 427 return sys_inotify_init1(flags); 428 } 429 430 /* 431 * madvise() wrapper 432 * 433 * Up to kernel v6.1 parisc has different values than all other 434 * platforms for the MADV_xxx flags listed below. 435 * To keep binary compatibility with existing userspace programs 436 * translate the former values to the new values. 437 * 438 * XXX: Remove this wrapper in year 2025 (or later) 439 */ 440 441 asmlinkage notrace long parisc_madvise(unsigned long start, size_t len_in, int behavior) 442 { 443 switch (behavior) { 444 case 65: behavior = MADV_MERGEABLE; break; 445 case 66: behavior = MADV_UNMERGEABLE; break; 446 case 67: behavior = MADV_HUGEPAGE; break; 447 case 68: behavior = MADV_NOHUGEPAGE; break; 448 case 69: behavior = MADV_DONTDUMP; break; 449 case 70: behavior = MADV_DODUMP; break; 450 case 71: behavior = MADV_WIPEONFORK; break; 451 case 72: behavior = MADV_KEEPONFORK; break; 452 case 73: behavior = MADV_COLLAPSE; break; 453 } 454 455 return sys_madvise(start, len_in, behavior); 456 } 457