1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Copyright (C) 2009 Sunplus Core Technology Co., Ltd. 4 * Chen Liqin <liqin.chen@sunplusct.com> 5 * Lennox Wu <lennox.wu@sunplusct.com> 6 * Copyright (C) 2012 Regents of the University of California 7 */ 8 9 #include <linux/compat.h> 10 #include <linux/signal.h> 11 #include <linux/uaccess.h> 12 #include <linux/syscalls.h> 13 #include <linux/resume_user_mode.h> 14 #include <linux/linkage.h> 15 16 #include <asm/ucontext.h> 17 #include <asm/vdso.h> 18 #include <asm/signal.h> 19 #include <asm/signal32.h> 20 #include <asm/switch_to.h> 21 #include <asm/csr.h> 22 #include <asm/cacheflush.h> 23 24 extern u32 __user_rt_sigreturn[2]; 25 26 #define DEBUG_SIG 0 27 28 struct rt_sigframe { 29 struct siginfo info; 30 struct ucontext uc; 31 #ifndef CONFIG_MMU 32 u32 sigreturn_code[2]; 33 #endif 34 }; 35 36 #ifdef CONFIG_FPU 37 static long restore_fp_state(struct pt_regs *regs, 38 union __riscv_fp_state __user *sc_fpregs) 39 { 40 long err; 41 struct __riscv_d_ext_state __user *state = &sc_fpregs->d; 42 size_t i; 43 44 err = __copy_from_user(¤t->thread.fstate, state, sizeof(*state)); 45 if (unlikely(err)) 46 return err; 47 48 fstate_restore(current, regs); 49 50 /* We support no other extension state at this time. */ 51 for (i = 0; i < ARRAY_SIZE(sc_fpregs->q.reserved); i++) { 52 u32 value; 53 54 err = __get_user(value, &sc_fpregs->q.reserved[i]); 55 if (unlikely(err)) 56 break; 57 if (value != 0) 58 return -EINVAL; 59 } 60 61 return err; 62 } 63 64 static long save_fp_state(struct pt_regs *regs, 65 union __riscv_fp_state __user *sc_fpregs) 66 { 67 long err; 68 struct __riscv_d_ext_state __user *state = &sc_fpregs->d; 69 size_t i; 70 71 fstate_save(current, regs); 72 err = __copy_to_user(state, ¤t->thread.fstate, sizeof(*state)); 73 if (unlikely(err)) 74 return err; 75 76 /* We support no other extension state at this time. */ 77 for (i = 0; i < ARRAY_SIZE(sc_fpregs->q.reserved); i++) { 78 err = __put_user(0, &sc_fpregs->q.reserved[i]); 79 if (unlikely(err)) 80 break; 81 } 82 83 return err; 84 } 85 #else 86 #define save_fp_state(task, regs) (0) 87 #define restore_fp_state(task, regs) (0) 88 #endif 89 90 static long restore_sigcontext(struct pt_regs *regs, 91 struct sigcontext __user *sc) 92 { 93 long err; 94 /* sc_regs is structured the same as the start of pt_regs */ 95 err = __copy_from_user(regs, &sc->sc_regs, sizeof(sc->sc_regs)); 96 /* Restore the floating-point state. */ 97 if (has_fpu()) 98 err |= restore_fp_state(regs, &sc->sc_fpregs); 99 return err; 100 } 101 102 SYSCALL_DEFINE0(rt_sigreturn) 103 { 104 struct pt_regs *regs = current_pt_regs(); 105 struct rt_sigframe __user *frame; 106 struct task_struct *task; 107 sigset_t set; 108 109 /* Always make any pending restarted system calls return -EINTR */ 110 current->restart_block.fn = do_no_restart_syscall; 111 112 frame = (struct rt_sigframe __user *)regs->sp; 113 114 if (!access_ok(frame, sizeof(*frame))) 115 goto badframe; 116 117 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set))) 118 goto badframe; 119 120 set_current_blocked(&set); 121 122 if (restore_sigcontext(regs, &frame->uc.uc_mcontext)) 123 goto badframe; 124 125 if (restore_altstack(&frame->uc.uc_stack)) 126 goto badframe; 127 128 regs->cause = -1UL; 129 130 return regs->a0; 131 132 badframe: 133 task = current; 134 if (show_unhandled_signals) { 135 pr_info_ratelimited( 136 "%s[%d]: bad frame in %s: frame=%p pc=%p sp=%p\n", 137 task->comm, task_pid_nr(task), __func__, 138 frame, (void *)regs->epc, (void *)regs->sp); 139 } 140 force_sig(SIGSEGV); 141 return 0; 142 } 143 144 static long setup_sigcontext(struct rt_sigframe __user *frame, 145 struct pt_regs *regs) 146 { 147 struct sigcontext __user *sc = &frame->uc.uc_mcontext; 148 long err; 149 /* sc_regs is structured the same as the start of pt_regs */ 150 err = __copy_to_user(&sc->sc_regs, regs, sizeof(sc->sc_regs)); 151 /* Save the floating-point state. */ 152 if (has_fpu()) 153 err |= save_fp_state(regs, &sc->sc_fpregs); 154 return err; 155 } 156 157 static inline void __user *get_sigframe(struct ksignal *ksig, 158 struct pt_regs *regs, size_t framesize) 159 { 160 unsigned long sp; 161 /* Default to using normal stack */ 162 sp = regs->sp; 163 164 /* 165 * If we are on the alternate signal stack and would overflow it, don't. 166 * Return an always-bogus address instead so we will die with SIGSEGV. 167 */ 168 if (on_sig_stack(sp) && !likely(on_sig_stack(sp - framesize))) 169 return (void __user __force *)(-1UL); 170 171 /* This is the X/Open sanctioned signal stack switching. */ 172 sp = sigsp(sp, ksig) - framesize; 173 174 /* Align the stack frame. */ 175 sp &= ~0xfUL; 176 177 return (void __user *)sp; 178 } 179 180 static int setup_rt_frame(struct ksignal *ksig, sigset_t *set, 181 struct pt_regs *regs) 182 { 183 struct rt_sigframe __user *frame; 184 long err = 0; 185 unsigned long __maybe_unused addr; 186 187 frame = get_sigframe(ksig, regs, sizeof(*frame)); 188 if (!access_ok(frame, sizeof(*frame))) 189 return -EFAULT; 190 191 err |= copy_siginfo_to_user(&frame->info, &ksig->info); 192 193 /* Create the ucontext. */ 194 err |= __put_user(0, &frame->uc.uc_flags); 195 err |= __put_user(NULL, &frame->uc.uc_link); 196 err |= __save_altstack(&frame->uc.uc_stack, regs->sp); 197 err |= setup_sigcontext(frame, regs); 198 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set)); 199 if (err) 200 return -EFAULT; 201 202 /* Set up to return from userspace. */ 203 #ifdef CONFIG_MMU 204 regs->ra = (unsigned long)VDSO_SYMBOL( 205 current->mm->context.vdso, rt_sigreturn); 206 #else 207 /* 208 * For the nommu case we don't have a VDSO. Instead we push two 209 * instructions to call the rt_sigreturn syscall onto the user stack. 210 */ 211 if (copy_to_user(&frame->sigreturn_code, __user_rt_sigreturn, 212 sizeof(frame->sigreturn_code))) 213 return -EFAULT; 214 215 addr = (unsigned long)&frame->sigreturn_code; 216 /* Make sure the two instructions are pushed to icache. */ 217 flush_icache_range(addr, addr + sizeof(frame->sigreturn_code)); 218 219 regs->ra = addr; 220 #endif /* CONFIG_MMU */ 221 222 /* 223 * Set up registers for signal handler. 224 * Registers that we don't modify keep the value they had from 225 * user-space at the time we took the signal. 226 * We always pass siginfo and mcontext, regardless of SA_SIGINFO, 227 * since some things rely on this (e.g. glibc's debug/segfault.c). 228 */ 229 regs->epc = (unsigned long)ksig->ka.sa.sa_handler; 230 regs->sp = (unsigned long)frame; 231 regs->a0 = ksig->sig; /* a0: signal number */ 232 regs->a1 = (unsigned long)(&frame->info); /* a1: siginfo pointer */ 233 regs->a2 = (unsigned long)(&frame->uc); /* a2: ucontext pointer */ 234 235 #if DEBUG_SIG 236 pr_info("SIG deliver (%s:%d): sig=%d pc=%p ra=%p sp=%p\n", 237 current->comm, task_pid_nr(current), ksig->sig, 238 (void *)regs->epc, (void *)regs->ra, frame); 239 #endif 240 241 return 0; 242 } 243 244 static void handle_signal(struct ksignal *ksig, struct pt_regs *regs) 245 { 246 sigset_t *oldset = sigmask_to_save(); 247 int ret; 248 249 /* Are we from a system call? */ 250 if (regs->cause == EXC_SYSCALL) { 251 /* Avoid additional syscall restarting via ret_from_exception */ 252 regs->cause = -1UL; 253 /* If so, check system call restarting.. */ 254 switch (regs->a0) { 255 case -ERESTART_RESTARTBLOCK: 256 case -ERESTARTNOHAND: 257 regs->a0 = -EINTR; 258 break; 259 260 case -ERESTARTSYS: 261 if (!(ksig->ka.sa.sa_flags & SA_RESTART)) { 262 regs->a0 = -EINTR; 263 break; 264 } 265 fallthrough; 266 case -ERESTARTNOINTR: 267 regs->a0 = regs->orig_a0; 268 regs->epc -= 0x4; 269 break; 270 } 271 } 272 273 rseq_signal_deliver(ksig, regs); 274 275 /* Set up the stack frame */ 276 if (is_compat_task()) 277 ret = compat_setup_rt_frame(ksig, oldset, regs); 278 else 279 ret = setup_rt_frame(ksig, oldset, regs); 280 281 signal_setup_done(ret, ksig, 0); 282 } 283 284 static void do_signal(struct pt_regs *regs) 285 { 286 struct ksignal ksig; 287 288 if (get_signal(&ksig)) { 289 /* Actually deliver the signal */ 290 handle_signal(&ksig, regs); 291 return; 292 } 293 294 /* Did we come from a system call? */ 295 if (regs->cause == EXC_SYSCALL) { 296 /* Avoid additional syscall restarting via ret_from_exception */ 297 regs->cause = -1UL; 298 299 /* Restart the system call - no handlers present */ 300 switch (regs->a0) { 301 case -ERESTARTNOHAND: 302 case -ERESTARTSYS: 303 case -ERESTARTNOINTR: 304 regs->a0 = regs->orig_a0; 305 regs->epc -= 0x4; 306 break; 307 case -ERESTART_RESTARTBLOCK: 308 regs->a0 = regs->orig_a0; 309 regs->a7 = __NR_restart_syscall; 310 regs->epc -= 0x4; 311 break; 312 } 313 } 314 315 /* 316 * If there is no signal to deliver, we just put the saved 317 * sigmask back. 318 */ 319 restore_saved_sigmask(); 320 } 321 322 /* 323 * Handle any pending work on the resume-to-userspace path, as indicated by 324 * _TIF_WORK_MASK. Entered from assembly with IRQs off. 325 */ 326 asmlinkage __visible void do_work_pending(struct pt_regs *regs, 327 unsigned long thread_info_flags) 328 { 329 do { 330 if (thread_info_flags & _TIF_NEED_RESCHED) { 331 schedule(); 332 } else { 333 local_irq_enable(); 334 if (thread_info_flags & _TIF_UPROBE) 335 uprobe_notify_resume(regs); 336 /* Handle pending signal delivery */ 337 if (thread_info_flags & (_TIF_SIGPENDING | 338 _TIF_NOTIFY_SIGNAL)) 339 do_signal(regs); 340 if (thread_info_flags & _TIF_NOTIFY_RESUME) 341 resume_user_mode_work(regs); 342 } 343 local_irq_disable(); 344 thread_info_flags = read_thread_flags(); 345 } while (thread_info_flags & _TIF_WORK_MASK); 346 } 347