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 #include <linux/entry-common.h> 16 17 #include <asm/ucontext.h> 18 #include <asm/vdso.h> 19 #include <asm/signal.h> 20 #include <asm/signal32.h> 21 #include <asm/switch_to.h> 22 #include <asm/vector.h> 23 #include <asm/csr.h> 24 #include <asm/cacheflush.h> 25 26 unsigned long signal_minsigstksz __ro_after_init; 27 28 extern u32 __user_rt_sigreturn[2]; 29 static size_t riscv_v_sc_size __ro_after_init; 30 31 #define DEBUG_SIG 0 32 33 struct rt_sigframe { 34 struct siginfo info; 35 struct ucontext uc; 36 #ifndef CONFIG_MMU 37 u32 sigreturn_code[2]; 38 #endif 39 }; 40 41 #ifdef CONFIG_FPU 42 static long restore_fp_state(struct pt_regs *regs, 43 union __riscv_fp_state __user *sc_fpregs) 44 { 45 long err; 46 struct __riscv_d_ext_state __user *state = &sc_fpregs->d; 47 48 err = __copy_from_user(¤t->thread.fstate, state, sizeof(*state)); 49 if (unlikely(err)) 50 return err; 51 52 fstate_restore(current, regs); 53 return 0; 54 } 55 56 static long save_fp_state(struct pt_regs *regs, 57 union __riscv_fp_state __user *sc_fpregs) 58 { 59 long err; 60 struct __riscv_d_ext_state __user *state = &sc_fpregs->d; 61 62 fstate_save(current, regs); 63 err = __copy_to_user(state, ¤t->thread.fstate, sizeof(*state)); 64 return err; 65 } 66 #else 67 #define save_fp_state(task, regs) (0) 68 #define restore_fp_state(task, regs) (0) 69 #endif 70 71 #ifdef CONFIG_RISCV_ISA_V 72 73 static long save_v_state(struct pt_regs *regs, void __user **sc_vec) 74 { 75 struct __riscv_ctx_hdr __user *hdr; 76 struct __sc_riscv_v_state __user *state; 77 void __user *datap; 78 long err; 79 80 hdr = *sc_vec; 81 /* Place state to the user's signal context space after the hdr */ 82 state = (struct __sc_riscv_v_state __user *)(hdr + 1); 83 /* Point datap right after the end of __sc_riscv_v_state */ 84 datap = state + 1; 85 86 /* datap is designed to be 16 byte aligned for better performance */ 87 WARN_ON(unlikely(!IS_ALIGNED((unsigned long)datap, 16))); 88 89 riscv_v_vstate_save(current, regs); 90 /* Copy everything of vstate but datap. */ 91 err = __copy_to_user(&state->v_state, ¤t->thread.vstate, 92 offsetof(struct __riscv_v_ext_state, datap)); 93 /* Copy the pointer datap itself. */ 94 err |= __put_user(datap, &state->v_state.datap); 95 /* Copy the whole vector content to user space datap. */ 96 err |= __copy_to_user(datap, current->thread.vstate.datap, riscv_v_vsize); 97 /* Copy magic to the user space after saving all vector conetext */ 98 err |= __put_user(RISCV_V_MAGIC, &hdr->magic); 99 err |= __put_user(riscv_v_sc_size, &hdr->size); 100 if (unlikely(err)) 101 return err; 102 103 /* Only progress the sv_vec if everything has done successfully */ 104 *sc_vec += riscv_v_sc_size; 105 return 0; 106 } 107 108 /* 109 * Restore Vector extension context from the user's signal frame. This function 110 * assumes a valid extension header. So magic and size checking must be done by 111 * the caller. 112 */ 113 static long __restore_v_state(struct pt_regs *regs, void __user *sc_vec) 114 { 115 long err; 116 struct __sc_riscv_v_state __user *state = sc_vec; 117 void __user *datap; 118 119 /* Copy everything of __sc_riscv_v_state except datap. */ 120 err = __copy_from_user(¤t->thread.vstate, &state->v_state, 121 offsetof(struct __riscv_v_ext_state, datap)); 122 if (unlikely(err)) 123 return err; 124 125 /* Copy the pointer datap itself. */ 126 err = __get_user(datap, &state->v_state.datap); 127 if (unlikely(err)) 128 return err; 129 /* 130 * Copy the whole vector content from user space datap. Use 131 * copy_from_user to prevent information leak. 132 */ 133 err = copy_from_user(current->thread.vstate.datap, datap, riscv_v_vsize); 134 if (unlikely(err)) 135 return err; 136 137 riscv_v_vstate_restore(current, regs); 138 139 return err; 140 } 141 #else 142 #define save_v_state(task, regs) (0) 143 #define __restore_v_state(task, regs) (0) 144 #endif 145 146 static long restore_sigcontext(struct pt_regs *regs, 147 struct sigcontext __user *sc) 148 { 149 void __user *sc_ext_ptr = &sc->sc_extdesc.hdr; 150 __u32 rsvd; 151 long err; 152 /* sc_regs is structured the same as the start of pt_regs */ 153 err = __copy_from_user(regs, &sc->sc_regs, sizeof(sc->sc_regs)); 154 if (unlikely(err)) 155 return err; 156 157 /* Restore the floating-point state. */ 158 if (has_fpu()) { 159 err = restore_fp_state(regs, &sc->sc_fpregs); 160 if (unlikely(err)) 161 return err; 162 } 163 164 /* Check the reserved word before extensions parsing */ 165 err = __get_user(rsvd, &sc->sc_extdesc.reserved); 166 if (unlikely(err)) 167 return err; 168 if (unlikely(rsvd)) 169 return -EINVAL; 170 171 while (!err) { 172 __u32 magic, size; 173 struct __riscv_ctx_hdr __user *head = sc_ext_ptr; 174 175 err |= __get_user(magic, &head->magic); 176 err |= __get_user(size, &head->size); 177 if (unlikely(err)) 178 return err; 179 180 sc_ext_ptr += sizeof(*head); 181 switch (magic) { 182 case END_MAGIC: 183 if (size != END_HDR_SIZE) 184 return -EINVAL; 185 186 return 0; 187 case RISCV_V_MAGIC: 188 if (!has_vector() || !riscv_v_vstate_query(regs) || 189 size != riscv_v_sc_size) 190 return -EINVAL; 191 192 err = __restore_v_state(regs, sc_ext_ptr); 193 break; 194 default: 195 return -EINVAL; 196 } 197 sc_ext_ptr = (void __user *)head + size; 198 } 199 return err; 200 } 201 202 static size_t get_rt_frame_size(bool cal_all) 203 { 204 struct rt_sigframe __user *frame; 205 size_t frame_size; 206 size_t total_context_size = 0; 207 208 frame_size = sizeof(*frame); 209 210 if (has_vector()) { 211 if (cal_all || riscv_v_vstate_query(task_pt_regs(current))) 212 total_context_size += riscv_v_sc_size; 213 } 214 /* 215 * Preserved a __riscv_ctx_hdr for END signal context header if an 216 * extension uses __riscv_extra_ext_header 217 */ 218 if (total_context_size) 219 total_context_size += sizeof(struct __riscv_ctx_hdr); 220 221 frame_size += total_context_size; 222 223 frame_size = round_up(frame_size, 16); 224 return frame_size; 225 } 226 227 SYSCALL_DEFINE0(rt_sigreturn) 228 { 229 struct pt_regs *regs = current_pt_regs(); 230 struct rt_sigframe __user *frame; 231 struct task_struct *task; 232 sigset_t set; 233 size_t frame_size = get_rt_frame_size(false); 234 235 /* Always make any pending restarted system calls return -EINTR */ 236 current->restart_block.fn = do_no_restart_syscall; 237 238 frame = (struct rt_sigframe __user *)regs->sp; 239 240 if (!access_ok(frame, frame_size)) 241 goto badframe; 242 243 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set))) 244 goto badframe; 245 246 set_current_blocked(&set); 247 248 if (restore_sigcontext(regs, &frame->uc.uc_mcontext)) 249 goto badframe; 250 251 if (restore_altstack(&frame->uc.uc_stack)) 252 goto badframe; 253 254 regs->cause = -1UL; 255 256 return regs->a0; 257 258 badframe: 259 task = current; 260 if (show_unhandled_signals) { 261 pr_info_ratelimited( 262 "%s[%d]: bad frame in %s: frame=%p pc=%p sp=%p\n", 263 task->comm, task_pid_nr(task), __func__, 264 frame, (void *)regs->epc, (void *)regs->sp); 265 } 266 force_sig(SIGSEGV); 267 return 0; 268 } 269 270 static long setup_sigcontext(struct rt_sigframe __user *frame, 271 struct pt_regs *regs) 272 { 273 struct sigcontext __user *sc = &frame->uc.uc_mcontext; 274 struct __riscv_ctx_hdr __user *sc_ext_ptr = &sc->sc_extdesc.hdr; 275 long err; 276 277 /* sc_regs is structured the same as the start of pt_regs */ 278 err = __copy_to_user(&sc->sc_regs, regs, sizeof(sc->sc_regs)); 279 /* Save the floating-point state. */ 280 if (has_fpu()) 281 err |= save_fp_state(regs, &sc->sc_fpregs); 282 /* Save the vector state. */ 283 if (has_vector() && riscv_v_vstate_query(regs)) 284 err |= save_v_state(regs, (void __user **)&sc_ext_ptr); 285 /* Write zero to fp-reserved space and check it on restore_sigcontext */ 286 err |= __put_user(0, &sc->sc_extdesc.reserved); 287 /* And put END __riscv_ctx_hdr at the end. */ 288 err |= __put_user(END_MAGIC, &sc_ext_ptr->magic); 289 err |= __put_user(END_HDR_SIZE, &sc_ext_ptr->size); 290 291 return err; 292 } 293 294 static inline void __user *get_sigframe(struct ksignal *ksig, 295 struct pt_regs *regs, size_t framesize) 296 { 297 unsigned long sp; 298 /* Default to using normal stack */ 299 sp = regs->sp; 300 301 /* 302 * If we are on the alternate signal stack and would overflow it, don't. 303 * Return an always-bogus address instead so we will die with SIGSEGV. 304 */ 305 if (on_sig_stack(sp) && !likely(on_sig_stack(sp - framesize))) 306 return (void __user __force *)(-1UL); 307 308 /* This is the X/Open sanctioned signal stack switching. */ 309 sp = sigsp(sp, ksig) - framesize; 310 311 /* Align the stack frame. */ 312 sp &= ~0xfUL; 313 314 return (void __user *)sp; 315 } 316 317 static int setup_rt_frame(struct ksignal *ksig, sigset_t *set, 318 struct pt_regs *regs) 319 { 320 struct rt_sigframe __user *frame; 321 long err = 0; 322 unsigned long __maybe_unused addr; 323 size_t frame_size = get_rt_frame_size(false); 324 325 frame = get_sigframe(ksig, regs, frame_size); 326 if (!access_ok(frame, frame_size)) 327 return -EFAULT; 328 329 err |= copy_siginfo_to_user(&frame->info, &ksig->info); 330 331 /* Create the ucontext. */ 332 err |= __put_user(0, &frame->uc.uc_flags); 333 err |= __put_user(NULL, &frame->uc.uc_link); 334 err |= __save_altstack(&frame->uc.uc_stack, regs->sp); 335 err |= setup_sigcontext(frame, regs); 336 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set)); 337 if (err) 338 return -EFAULT; 339 340 /* Set up to return from userspace. */ 341 #ifdef CONFIG_MMU 342 regs->ra = (unsigned long)VDSO_SYMBOL( 343 current->mm->context.vdso, rt_sigreturn); 344 #else 345 /* 346 * For the nommu case we don't have a VDSO. Instead we push two 347 * instructions to call the rt_sigreturn syscall onto the user stack. 348 */ 349 if (copy_to_user(&frame->sigreturn_code, __user_rt_sigreturn, 350 sizeof(frame->sigreturn_code))) 351 return -EFAULT; 352 353 addr = (unsigned long)&frame->sigreturn_code; 354 /* Make sure the two instructions are pushed to icache. */ 355 flush_icache_range(addr, addr + sizeof(frame->sigreturn_code)); 356 357 regs->ra = addr; 358 #endif /* CONFIG_MMU */ 359 360 /* 361 * Set up registers for signal handler. 362 * Registers that we don't modify keep the value they had from 363 * user-space at the time we took the signal. 364 * We always pass siginfo and mcontext, regardless of SA_SIGINFO, 365 * since some things rely on this (e.g. glibc's debug/segfault.c). 366 */ 367 regs->epc = (unsigned long)ksig->ka.sa.sa_handler; 368 regs->sp = (unsigned long)frame; 369 regs->a0 = ksig->sig; /* a0: signal number */ 370 regs->a1 = (unsigned long)(&frame->info); /* a1: siginfo pointer */ 371 regs->a2 = (unsigned long)(&frame->uc); /* a2: ucontext pointer */ 372 373 #if DEBUG_SIG 374 pr_info("SIG deliver (%s:%d): sig=%d pc=%p ra=%p sp=%p\n", 375 current->comm, task_pid_nr(current), ksig->sig, 376 (void *)regs->epc, (void *)regs->ra, frame); 377 #endif 378 379 return 0; 380 } 381 382 static void handle_signal(struct ksignal *ksig, struct pt_regs *regs) 383 { 384 sigset_t *oldset = sigmask_to_save(); 385 int ret; 386 387 /* Are we from a system call? */ 388 if (regs->cause == EXC_SYSCALL) { 389 /* Avoid additional syscall restarting via ret_from_exception */ 390 regs->cause = -1UL; 391 /* If so, check system call restarting.. */ 392 switch (regs->a0) { 393 case -ERESTART_RESTARTBLOCK: 394 case -ERESTARTNOHAND: 395 regs->a0 = -EINTR; 396 break; 397 398 case -ERESTARTSYS: 399 if (!(ksig->ka.sa.sa_flags & SA_RESTART)) { 400 regs->a0 = -EINTR; 401 break; 402 } 403 fallthrough; 404 case -ERESTARTNOINTR: 405 regs->a0 = regs->orig_a0; 406 regs->epc -= 0x4; 407 break; 408 } 409 } 410 411 rseq_signal_deliver(ksig, regs); 412 413 /* Set up the stack frame */ 414 if (is_compat_task()) 415 ret = compat_setup_rt_frame(ksig, oldset, regs); 416 else 417 ret = setup_rt_frame(ksig, oldset, regs); 418 419 signal_setup_done(ret, ksig, 0); 420 } 421 422 void arch_do_signal_or_restart(struct pt_regs *regs) 423 { 424 struct ksignal ksig; 425 426 if (get_signal(&ksig)) { 427 /* Actually deliver the signal */ 428 handle_signal(&ksig, regs); 429 return; 430 } 431 432 /* Did we come from a system call? */ 433 if (regs->cause == EXC_SYSCALL) { 434 /* Avoid additional syscall restarting via ret_from_exception */ 435 regs->cause = -1UL; 436 437 /* Restart the system call - no handlers present */ 438 switch (regs->a0) { 439 case -ERESTARTNOHAND: 440 case -ERESTARTSYS: 441 case -ERESTARTNOINTR: 442 regs->a0 = regs->orig_a0; 443 regs->epc -= 0x4; 444 break; 445 case -ERESTART_RESTARTBLOCK: 446 regs->a0 = regs->orig_a0; 447 regs->a7 = __NR_restart_syscall; 448 regs->epc -= 0x4; 449 break; 450 } 451 } 452 453 /* 454 * If there is no signal to deliver, we just put the saved 455 * sigmask back. 456 */ 457 restore_saved_sigmask(); 458 } 459 460 void init_rt_signal_env(void); 461 void __init init_rt_signal_env(void) 462 { 463 riscv_v_sc_size = sizeof(struct __riscv_ctx_hdr) + 464 sizeof(struct __sc_riscv_v_state) + riscv_v_vsize; 465 /* 466 * Determine the stack space required for guaranteed signal delivery. 467 * The signal_minsigstksz will be populated into the AT_MINSIGSTKSZ entry 468 * in the auxiliary array at process startup. 469 */ 470 signal_minsigstksz = get_rt_frame_size(true); 471 } 472 473 #ifdef CONFIG_DYNAMIC_SIGFRAME 474 bool sigaltstack_size_valid(size_t ss_size) 475 { 476 return ss_size > get_rt_frame_size(false); 477 } 478 #endif /* CONFIG_DYNAMIC_SIGFRAME */ 479