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