0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <linux/errno.h>
0010 #include <linux/types.h>
0011 #include <linux/sched/signal.h>
0012 #include <linux/sched/mm.h>
0013 #include <linux/sched/debug.h>
0014 #include <linux/fs.h>
0015 #include <linux/file.h>
0016 #include <linux/mm.h>
0017 #include <linux/sem.h>
0018 #include <linux/msg.h>
0019 #include <linux/shm.h>
0020 #include <linux/stat.h>
0021 #include <linux/mman.h>
0022 #include <linux/utsname.h>
0023 #include <linux/smp.h>
0024 #include <linux/slab.h>
0025 #include <linux/syscalls.h>
0026 #include <linux/ipc.h>
0027 #include <linux/personality.h>
0028 #include <linux/random.h>
0029 #include <linux/export.h>
0030 #include <linux/context_tracking.h>
0031 #include <linux/timex.h>
0032 #include <linux/uaccess.h>
0033
0034 #include <asm/utrap.h>
0035 #include <asm/unistd.h>
0036
0037 #include "entry.h"
0038 #include "kernel.h"
0039 #include "systbls.h"
0040
0041
0042
0043 SYSCALL_DEFINE0(getpagesize)
0044 {
0045 return PAGE_SIZE;
0046 }
0047
0048
0049
0050
0051 static inline int invalid_64bit_range(unsigned long addr, unsigned long len)
0052 {
0053 unsigned long va_exclude_start, va_exclude_end;
0054
0055 va_exclude_start = VA_EXCLUDE_START;
0056 va_exclude_end = VA_EXCLUDE_END;
0057
0058 if (unlikely(len >= va_exclude_start))
0059 return 1;
0060
0061 if (unlikely((addr + len) < addr))
0062 return 1;
0063
0064 if (unlikely((addr >= va_exclude_start && addr < va_exclude_end) ||
0065 ((addr + len) >= va_exclude_start &&
0066 (addr + len) < va_exclude_end)))
0067 return 1;
0068
0069 return 0;
0070 }
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081 static inline unsigned long COLOR_ALIGN(unsigned long addr,
0082 unsigned long pgoff)
0083 {
0084 unsigned long base = (addr+SHMLBA-1)&~(SHMLBA-1);
0085 unsigned long off = (pgoff<<PAGE_SHIFT) & (SHMLBA-1);
0086
0087 return base + off;
0088 }
0089
0090 unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr, unsigned long len, unsigned long pgoff, unsigned long flags)
0091 {
0092 struct mm_struct *mm = current->mm;
0093 struct vm_area_struct * vma;
0094 unsigned long task_size = TASK_SIZE;
0095 int do_color_align;
0096 struct vm_unmapped_area_info info;
0097
0098 if (flags & MAP_FIXED) {
0099
0100
0101
0102 if ((flags & MAP_SHARED) &&
0103 ((addr - (pgoff << PAGE_SHIFT)) & (SHMLBA - 1)))
0104 return -EINVAL;
0105 return addr;
0106 }
0107
0108 if (test_thread_flag(TIF_32BIT))
0109 task_size = STACK_TOP32;
0110 if (unlikely(len > task_size || len >= VA_EXCLUDE_START))
0111 return -ENOMEM;
0112
0113 do_color_align = 0;
0114 if (filp || (flags & MAP_SHARED))
0115 do_color_align = 1;
0116
0117 if (addr) {
0118 if (do_color_align)
0119 addr = COLOR_ALIGN(addr, pgoff);
0120 else
0121 addr = PAGE_ALIGN(addr);
0122
0123 vma = find_vma(mm, addr);
0124 if (task_size - len >= addr &&
0125 (!vma || addr + len <= vm_start_gap(vma)))
0126 return addr;
0127 }
0128
0129 info.flags = 0;
0130 info.length = len;
0131 info.low_limit = TASK_UNMAPPED_BASE;
0132 info.high_limit = min(task_size, VA_EXCLUDE_START);
0133 info.align_mask = do_color_align ? (PAGE_MASK & (SHMLBA - 1)) : 0;
0134 info.align_offset = pgoff << PAGE_SHIFT;
0135 addr = vm_unmapped_area(&info);
0136
0137 if ((addr & ~PAGE_MASK) && task_size > VA_EXCLUDE_END) {
0138 VM_BUG_ON(addr != -ENOMEM);
0139 info.low_limit = VA_EXCLUDE_END;
0140 info.high_limit = task_size;
0141 addr = vm_unmapped_area(&info);
0142 }
0143
0144 return addr;
0145 }
0146
0147 unsigned long
0148 arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
0149 const unsigned long len, const unsigned long pgoff,
0150 const unsigned long flags)
0151 {
0152 struct vm_area_struct *vma;
0153 struct mm_struct *mm = current->mm;
0154 unsigned long task_size = STACK_TOP32;
0155 unsigned long addr = addr0;
0156 int do_color_align;
0157 struct vm_unmapped_area_info info;
0158
0159
0160 BUG_ON(!test_thread_flag(TIF_32BIT));
0161
0162 if (flags & MAP_FIXED) {
0163
0164
0165
0166 if ((flags & MAP_SHARED) &&
0167 ((addr - (pgoff << PAGE_SHIFT)) & (SHMLBA - 1)))
0168 return -EINVAL;
0169 return addr;
0170 }
0171
0172 if (unlikely(len > task_size))
0173 return -ENOMEM;
0174
0175 do_color_align = 0;
0176 if (filp || (flags & MAP_SHARED))
0177 do_color_align = 1;
0178
0179
0180 if (addr) {
0181 if (do_color_align)
0182 addr = COLOR_ALIGN(addr, pgoff);
0183 else
0184 addr = PAGE_ALIGN(addr);
0185
0186 vma = find_vma(mm, addr);
0187 if (task_size - len >= addr &&
0188 (!vma || addr + len <= vm_start_gap(vma)))
0189 return addr;
0190 }
0191
0192 info.flags = VM_UNMAPPED_AREA_TOPDOWN;
0193 info.length = len;
0194 info.low_limit = PAGE_SIZE;
0195 info.high_limit = mm->mmap_base;
0196 info.align_mask = do_color_align ? (PAGE_MASK & (SHMLBA - 1)) : 0;
0197 info.align_offset = pgoff << PAGE_SHIFT;
0198 addr = vm_unmapped_area(&info);
0199
0200
0201
0202
0203
0204
0205
0206 if (addr & ~PAGE_MASK) {
0207 VM_BUG_ON(addr != -ENOMEM);
0208 info.flags = 0;
0209 info.low_limit = TASK_UNMAPPED_BASE;
0210 info.high_limit = STACK_TOP32;
0211 addr = vm_unmapped_area(&info);
0212 }
0213
0214 return addr;
0215 }
0216
0217
0218 unsigned long get_fb_unmapped_area(struct file *filp, unsigned long orig_addr, unsigned long len, unsigned long pgoff, unsigned long flags)
0219 {
0220 unsigned long align_goal, addr = -ENOMEM;
0221 unsigned long (*get_area)(struct file *, unsigned long,
0222 unsigned long, unsigned long, unsigned long);
0223
0224 get_area = current->mm->get_unmapped_area;
0225
0226 if (flags & MAP_FIXED) {
0227
0228 return get_area(NULL, orig_addr, len, pgoff, flags);
0229 }
0230 flags &= ~MAP_SHARED;
0231
0232 align_goal = PAGE_SIZE;
0233 if (len >= (4UL * 1024 * 1024))
0234 align_goal = (4UL * 1024 * 1024);
0235 else if (len >= (512UL * 1024))
0236 align_goal = (512UL * 1024);
0237 else if (len >= (64UL * 1024))
0238 align_goal = (64UL * 1024);
0239
0240 do {
0241 addr = get_area(NULL, orig_addr, len + (align_goal - PAGE_SIZE), pgoff, flags);
0242 if (!(addr & ~PAGE_MASK)) {
0243 addr = (addr + (align_goal - 1UL)) & ~(align_goal - 1UL);
0244 break;
0245 }
0246
0247 if (align_goal == (4UL * 1024 * 1024))
0248 align_goal = (512UL * 1024);
0249 else if (align_goal == (512UL * 1024))
0250 align_goal = (64UL * 1024);
0251 else
0252 align_goal = PAGE_SIZE;
0253 } while ((addr & ~PAGE_MASK) && align_goal > PAGE_SIZE);
0254
0255
0256
0257
0258 if (addr & ~PAGE_MASK)
0259 addr = get_area(NULL, orig_addr, len, pgoff, flags);
0260
0261 return addr;
0262 }
0263 EXPORT_SYMBOL(get_fb_unmapped_area);
0264
0265
0266 static unsigned long mmap_rnd(void)
0267 {
0268 unsigned long rnd = 0UL;
0269
0270 if (current->flags & PF_RANDOMIZE) {
0271 unsigned long val = get_random_long();
0272 if (test_thread_flag(TIF_32BIT))
0273 rnd = (val % (1UL << (23UL-PAGE_SHIFT)));
0274 else
0275 rnd = (val % (1UL << (30UL-PAGE_SHIFT)));
0276 }
0277 return rnd << PAGE_SHIFT;
0278 }
0279
0280 void arch_pick_mmap_layout(struct mm_struct *mm, struct rlimit *rlim_stack)
0281 {
0282 unsigned long random_factor = mmap_rnd();
0283 unsigned long gap;
0284
0285
0286
0287
0288
0289 gap = rlim_stack->rlim_cur;
0290 if (!test_thread_flag(TIF_32BIT) ||
0291 (current->personality & ADDR_COMPAT_LAYOUT) ||
0292 gap == RLIM_INFINITY ||
0293 sysctl_legacy_va_layout) {
0294 mm->mmap_base = TASK_UNMAPPED_BASE + random_factor;
0295 mm->get_unmapped_area = arch_get_unmapped_area;
0296 } else {
0297
0298 unsigned long task_size = STACK_TOP32;
0299
0300 if (gap < 128 * 1024 * 1024)
0301 gap = 128 * 1024 * 1024;
0302 if (gap > (task_size / 6 * 5))
0303 gap = (task_size / 6 * 5);
0304
0305 mm->mmap_base = PAGE_ALIGN(task_size - gap - random_factor);
0306 mm->get_unmapped_area = arch_get_unmapped_area_topdown;
0307 }
0308 }
0309
0310
0311
0312
0313
0314 SYSCALL_DEFINE0(sparc_pipe)
0315 {
0316 int fd[2];
0317 int error;
0318
0319 error = do_pipe_flags(fd, 0);
0320 if (error)
0321 goto out;
0322 current_pt_regs()->u_regs[UREG_I1] = fd[1];
0323 error = fd[0];
0324 out:
0325 return error;
0326 }
0327
0328
0329
0330
0331
0332
0333
0334 SYSCALL_DEFINE6(sparc_ipc, unsigned int, call, int, first, unsigned long, second,
0335 unsigned long, third, void __user *, ptr, long, fifth)
0336 {
0337 long err;
0338
0339 if (!IS_ENABLED(CONFIG_SYSVIPC))
0340 return -ENOSYS;
0341
0342
0343 if (call <= SEMTIMEDOP) {
0344 switch (call) {
0345 case SEMOP:
0346 err = ksys_semtimedop(first, ptr,
0347 (unsigned int)second, NULL);
0348 goto out;
0349 case SEMTIMEDOP:
0350 err = ksys_semtimedop(first, ptr, (unsigned int)second,
0351 (const struct __kernel_timespec __user *)
0352 (unsigned long) fifth);
0353 goto out;
0354 case SEMGET:
0355 err = ksys_semget(first, (int)second, (int)third);
0356 goto out;
0357 case SEMCTL: {
0358 err = ksys_old_semctl(first, second,
0359 (int)third | IPC_64,
0360 (unsigned long) ptr);
0361 goto out;
0362 }
0363 default:
0364 err = -ENOSYS;
0365 goto out;
0366 }
0367 }
0368 if (call <= MSGCTL) {
0369 switch (call) {
0370 case MSGSND:
0371 err = ksys_msgsnd(first, ptr, (size_t)second,
0372 (int)third);
0373 goto out;
0374 case MSGRCV:
0375 err = ksys_msgrcv(first, ptr, (size_t)second, fifth,
0376 (int)third);
0377 goto out;
0378 case MSGGET:
0379 err = ksys_msgget((key_t)first, (int)second);
0380 goto out;
0381 case MSGCTL:
0382 err = ksys_old_msgctl(first, (int)second | IPC_64, ptr);
0383 goto out;
0384 default:
0385 err = -ENOSYS;
0386 goto out;
0387 }
0388 }
0389 if (call <= SHMCTL) {
0390 switch (call) {
0391 case SHMAT: {
0392 ulong raddr;
0393 err = do_shmat(first, ptr, (int)second, &raddr, SHMLBA);
0394 if (!err) {
0395 if (put_user(raddr,
0396 (ulong __user *) third))
0397 err = -EFAULT;
0398 }
0399 goto out;
0400 }
0401 case SHMDT:
0402 err = ksys_shmdt(ptr);
0403 goto out;
0404 case SHMGET:
0405 err = ksys_shmget(first, (size_t)second, (int)third);
0406 goto out;
0407 case SHMCTL:
0408 err = ksys_old_shmctl(first, (int)second | IPC_64, ptr);
0409 goto out;
0410 default:
0411 err = -ENOSYS;
0412 goto out;
0413 }
0414 } else {
0415 err = -ENOSYS;
0416 }
0417 out:
0418 return err;
0419 }
0420
0421 SYSCALL_DEFINE1(sparc64_personality, unsigned long, personality)
0422 {
0423 long ret;
0424
0425 if (personality(current->personality) == PER_LINUX32 &&
0426 personality(personality) == PER_LINUX)
0427 personality |= PER_LINUX32;
0428 ret = sys_personality(personality);
0429 if (personality(ret) == PER_LINUX32)
0430 ret &= ~PER_LINUX32;
0431
0432 return ret;
0433 }
0434
0435 int sparc_mmap_check(unsigned long addr, unsigned long len)
0436 {
0437 if (test_thread_flag(TIF_32BIT)) {
0438 if (len >= STACK_TOP32)
0439 return -EINVAL;
0440
0441 if (addr > STACK_TOP32 - len)
0442 return -EINVAL;
0443 } else {
0444 if (len >= VA_EXCLUDE_START)
0445 return -EINVAL;
0446
0447 if (invalid_64bit_range(addr, len))
0448 return -EINVAL;
0449 }
0450
0451 return 0;
0452 }
0453
0454
0455 SYSCALL_DEFINE6(mmap, unsigned long, addr, unsigned long, len,
0456 unsigned long, prot, unsigned long, flags, unsigned long, fd,
0457 unsigned long, off)
0458 {
0459 unsigned long retval = -EINVAL;
0460
0461 if ((off + PAGE_ALIGN(len)) < off)
0462 goto out;
0463 if (off & ~PAGE_MASK)
0464 goto out;
0465 retval = ksys_mmap_pgoff(addr, len, prot, flags, fd, off >> PAGE_SHIFT);
0466 out:
0467 return retval;
0468 }
0469
0470 SYSCALL_DEFINE2(64_munmap, unsigned long, addr, size_t, len)
0471 {
0472 if (invalid_64bit_range(addr, len))
0473 return -EINVAL;
0474
0475 return vm_munmap(addr, len);
0476 }
0477
0478 SYSCALL_DEFINE5(64_mremap, unsigned long, addr, unsigned long, old_len,
0479 unsigned long, new_len, unsigned long, flags,
0480 unsigned long, new_addr)
0481 {
0482 if (test_thread_flag(TIF_32BIT))
0483 return -EINVAL;
0484 return sys_mremap(addr, old_len, new_len, flags, new_addr);
0485 }
0486
0487 SYSCALL_DEFINE0(nis_syscall)
0488 {
0489 static int count;
0490 struct pt_regs *regs = current_pt_regs();
0491
0492
0493 if (count++ > 5)
0494 return -ENOSYS;
0495
0496 printk ("Unimplemented SPARC system call %ld\n",regs->u_regs[1]);
0497 #ifdef DEBUG_UNIMP_SYSCALL
0498 show_regs (regs);
0499 #endif
0500
0501 return -ENOSYS;
0502 }
0503
0504
0505
0506 asmlinkage void sparc_breakpoint(struct pt_regs *regs)
0507 {
0508 enum ctx_state prev_state = exception_enter();
0509
0510 if (test_thread_flag(TIF_32BIT)) {
0511 regs->tpc &= 0xffffffff;
0512 regs->tnpc &= 0xffffffff;
0513 }
0514 #ifdef DEBUG_SPARC_BREAKPOINT
0515 printk ("TRAP: Entering kernel PC=%lx, nPC=%lx\n", regs->tpc, regs->tnpc);
0516 #endif
0517 force_sig_fault(SIGTRAP, TRAP_BRKPT, (void __user *)regs->tpc);
0518 #ifdef DEBUG_SPARC_BREAKPOINT
0519 printk ("TRAP: Returning to space: PC=%lx nPC=%lx\n", regs->tpc, regs->tnpc);
0520 #endif
0521 exception_exit(prev_state);
0522 }
0523
0524 SYSCALL_DEFINE2(getdomainname, char __user *, name, int, len)
0525 {
0526 int nlen, err;
0527 char tmp[__NEW_UTS_LEN + 1];
0528
0529 if (len < 0)
0530 return -EINVAL;
0531
0532 down_read(&uts_sem);
0533
0534 nlen = strlen(utsname()->domainname) + 1;
0535 err = -EINVAL;
0536 if (nlen > len)
0537 goto out_unlock;
0538 memcpy(tmp, utsname()->domainname, nlen);
0539
0540 up_read(&uts_sem);
0541
0542 if (copy_to_user(name, tmp, nlen))
0543 return -EFAULT;
0544 return 0;
0545
0546 out_unlock:
0547 up_read(&uts_sem);
0548 return err;
0549 }
0550
0551 SYSCALL_DEFINE1(sparc_adjtimex, struct __kernel_timex __user *, txc_p)
0552 {
0553 struct __kernel_timex txc;
0554 struct __kernel_old_timeval *tv = (void *)&txc.time;
0555 int ret;
0556
0557
0558
0559
0560
0561 if (copy_from_user(&txc, txc_p, sizeof(txc)))
0562 return -EFAULT;
0563
0564
0565
0566
0567
0568 txc.time.tv_usec = tv->tv_usec;
0569 ret = do_adjtimex(&txc);
0570 tv->tv_usec = txc.time.tv_usec;
0571
0572 return copy_to_user(txc_p, &txc, sizeof(txc)) ? -EFAULT : ret;
0573 }
0574
0575 SYSCALL_DEFINE2(sparc_clock_adjtime, const clockid_t, which_clock,
0576 struct __kernel_timex __user *, txc_p)
0577 {
0578 struct __kernel_timex txc;
0579 struct __kernel_old_timeval *tv = (void *)&txc.time;
0580 int ret;
0581
0582 if (!IS_ENABLED(CONFIG_POSIX_TIMERS)) {
0583 pr_err_once("process %d (%s) attempted a POSIX timer syscall "
0584 "while CONFIG_POSIX_TIMERS is not set\n",
0585 current->pid, current->comm);
0586
0587 return -ENOSYS;
0588 }
0589
0590
0591
0592
0593
0594 if (copy_from_user(&txc, txc_p, sizeof(txc)))
0595 return -EFAULT;
0596
0597
0598
0599
0600
0601 txc.time.tv_usec = tv->tv_usec;
0602 ret = do_clock_adjtime(which_clock, &txc);
0603 tv->tv_usec = txc.time.tv_usec;
0604
0605 return copy_to_user(txc_p, &txc, sizeof(txc)) ? -EFAULT : ret;
0606 }
0607
0608 SYSCALL_DEFINE5(utrap_install, utrap_entry_t, type,
0609 utrap_handler_t, new_p, utrap_handler_t, new_d,
0610 utrap_handler_t __user *, old_p,
0611 utrap_handler_t __user *, old_d)
0612 {
0613 if (type < UT_INSTRUCTION_EXCEPTION || type > UT_TRAP_INSTRUCTION_31)
0614 return -EINVAL;
0615 if (new_p == (utrap_handler_t)(long)UTH_NOCHANGE) {
0616 if (old_p) {
0617 if (!current_thread_info()->utraps) {
0618 if (put_user(NULL, old_p))
0619 return -EFAULT;
0620 } else {
0621 if (put_user((utrap_handler_t)(current_thread_info()->utraps[type]), old_p))
0622 return -EFAULT;
0623 }
0624 }
0625 if (old_d) {
0626 if (put_user(NULL, old_d))
0627 return -EFAULT;
0628 }
0629 return 0;
0630 }
0631 if (!current_thread_info()->utraps) {
0632 current_thread_info()->utraps =
0633 kcalloc(UT_TRAP_INSTRUCTION_31 + 1, sizeof(long),
0634 GFP_KERNEL);
0635 if (!current_thread_info()->utraps)
0636 return -ENOMEM;
0637 current_thread_info()->utraps[0] = 1;
0638 } else {
0639 if ((utrap_handler_t)current_thread_info()->utraps[type] != new_p &&
0640 current_thread_info()->utraps[0] > 1) {
0641 unsigned long *p = current_thread_info()->utraps;
0642
0643 current_thread_info()->utraps =
0644 kmalloc_array(UT_TRAP_INSTRUCTION_31 + 1,
0645 sizeof(long),
0646 GFP_KERNEL);
0647 if (!current_thread_info()->utraps) {
0648 current_thread_info()->utraps = p;
0649 return -ENOMEM;
0650 }
0651 p[0]--;
0652 current_thread_info()->utraps[0] = 1;
0653 memcpy(current_thread_info()->utraps+1, p+1,
0654 UT_TRAP_INSTRUCTION_31*sizeof(long));
0655 }
0656 }
0657 if (old_p) {
0658 if (put_user((utrap_handler_t)(current_thread_info()->utraps[type]), old_p))
0659 return -EFAULT;
0660 }
0661 if (old_d) {
0662 if (put_user(NULL, old_d))
0663 return -EFAULT;
0664 }
0665 current_thread_info()->utraps[type] = (long)new_p;
0666
0667 return 0;
0668 }
0669
0670 SYSCALL_DEFINE1(memory_ordering, unsigned long, model)
0671 {
0672 struct pt_regs *regs = current_pt_regs();
0673 if (model >= 3)
0674 return -EINVAL;
0675 regs->tstate = (regs->tstate & ~TSTATE_MM) | (model << 14);
0676 return 0;
0677 }
0678
0679 SYSCALL_DEFINE5(rt_sigaction, int, sig, const struct sigaction __user *, act,
0680 struct sigaction __user *, oact, void __user *, restorer,
0681 size_t, sigsetsize)
0682 {
0683 struct k_sigaction new_ka, old_ka;
0684 int ret;
0685
0686
0687 if (sigsetsize != sizeof(sigset_t))
0688 return -EINVAL;
0689
0690 if (act) {
0691 new_ka.ka_restorer = restorer;
0692 if (copy_from_user(&new_ka.sa, act, sizeof(*act)))
0693 return -EFAULT;
0694 }
0695
0696 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
0697
0698 if (!ret && oact) {
0699 if (copy_to_user(oact, &old_ka.sa, sizeof(*oact)))
0700 return -EFAULT;
0701 }
0702
0703 return ret;
0704 }
0705
0706 SYSCALL_DEFINE0(kern_features)
0707 {
0708 return KERN_FEATURE_MIXED_MODE_STACK;
0709 }