1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Based on arch/arm/kernel/signal.c 4 * 5 * Copyright (C) 1995-2009 Russell King 6 * Copyright (C) 2012 ARM Ltd. 7 */ 8 9 #include <linux/cache.h> 10 #include <linux/compat.h> 11 #include <linux/errno.h> 12 #include <linux/kernel.h> 13 #include <linux/signal.h> 14 #include <linux/personality.h> 15 #include <linux/freezer.h> 16 #include <linux/stddef.h> 17 #include <linux/uaccess.h> 18 #include <linux/sizes.h> 19 #include <linux/string.h> 20 #include <linux/tracehook.h> 21 #include <linux/ratelimit.h> 22 #include <linux/syscalls.h> 23 24 #include <asm/daifflags.h> 25 #include <asm/debug-monitors.h> 26 #include <asm/elf.h> 27 #include <asm/cacheflush.h> 28 #include <asm/ucontext.h> 29 #include <asm/unistd.h> 30 #include <asm/fpsimd.h> 31 #include <asm/ptrace.h> 32 #include <asm/syscall.h> 33 #include <asm/signal32.h> 34 #include <asm/traps.h> 35 #include <asm/vdso.h> 36 37 /* 38 * Do a signal return; undo the signal stack. These are aligned to 128-bit. 39 */ 40 struct rt_sigframe { 41 struct siginfo info; 42 struct ucontext uc; 43 }; 44 45 struct frame_record { 46 u64 fp; 47 u64 lr; 48 }; 49 50 struct rt_sigframe_user_layout { 51 struct rt_sigframe __user *sigframe; 52 struct frame_record __user *next_frame; 53 54 unsigned long size; /* size of allocated sigframe data */ 55 unsigned long limit; /* largest allowed size */ 56 57 unsigned long fpsimd_offset; 58 unsigned long esr_offset; 59 unsigned long sve_offset; 60 unsigned long extra_offset; 61 unsigned long end_offset; 62 }; 63 64 #define BASE_SIGFRAME_SIZE round_up(sizeof(struct rt_sigframe), 16) 65 #define TERMINATOR_SIZE round_up(sizeof(struct _aarch64_ctx), 16) 66 #define EXTRA_CONTEXT_SIZE round_up(sizeof(struct extra_context), 16) 67 68 static void init_user_layout(struct rt_sigframe_user_layout *user) 69 { 70 const size_t reserved_size = 71 sizeof(user->sigframe->uc.uc_mcontext.__reserved); 72 73 memset(user, 0, sizeof(*user)); 74 user->size = offsetof(struct rt_sigframe, uc.uc_mcontext.__reserved); 75 76 user->limit = user->size + reserved_size; 77 78 user->limit -= TERMINATOR_SIZE; 79 user->limit -= EXTRA_CONTEXT_SIZE; 80 /* Reserve space for extension and terminator ^ */ 81 } 82 83 static size_t sigframe_size(struct rt_sigframe_user_layout const *user) 84 { 85 return round_up(max(user->size, sizeof(struct rt_sigframe)), 16); 86 } 87 88 /* 89 * Sanity limit on the approximate maximum size of signal frame we'll 90 * try to generate. Stack alignment padding and the frame record are 91 * not taken into account. This limit is not a guarantee and is 92 * NOT ABI. 93 */ 94 #define SIGFRAME_MAXSZ SZ_64K 95 96 static int __sigframe_alloc(struct rt_sigframe_user_layout *user, 97 unsigned long *offset, size_t size, bool extend) 98 { 99 size_t padded_size = round_up(size, 16); 100 101 if (padded_size > user->limit - user->size && 102 !user->extra_offset && 103 extend) { 104 int ret; 105 106 user->limit += EXTRA_CONTEXT_SIZE; 107 ret = __sigframe_alloc(user, &user->extra_offset, 108 sizeof(struct extra_context), false); 109 if (ret) { 110 user->limit -= EXTRA_CONTEXT_SIZE; 111 return ret; 112 } 113 114 /* Reserve space for the __reserved[] terminator */ 115 user->size += TERMINATOR_SIZE; 116 117 /* 118 * Allow expansion up to SIGFRAME_MAXSZ, ensuring space for 119 * the terminator: 120 */ 121 user->limit = SIGFRAME_MAXSZ - TERMINATOR_SIZE; 122 } 123 124 /* Still not enough space? Bad luck! */ 125 if (padded_size > user->limit - user->size) 126 return -ENOMEM; 127 128 *offset = user->size; 129 user->size += padded_size; 130 131 return 0; 132 } 133 134 /* 135 * Allocate space for an optional record of <size> bytes in the user 136 * signal frame. The offset from the signal frame base address to the 137 * allocated block is assigned to *offset. 138 */ 139 static int sigframe_alloc(struct rt_sigframe_user_layout *user, 140 unsigned long *offset, size_t size) 141 { 142 return __sigframe_alloc(user, offset, size, true); 143 } 144 145 /* Allocate the null terminator record and prevent further allocations */ 146 static int sigframe_alloc_end(struct rt_sigframe_user_layout *user) 147 { 148 int ret; 149 150 /* Un-reserve the space reserved for the terminator: */ 151 user->limit += TERMINATOR_SIZE; 152 153 ret = sigframe_alloc(user, &user->end_offset, 154 sizeof(struct _aarch64_ctx)); 155 if (ret) 156 return ret; 157 158 /* Prevent further allocation: */ 159 user->limit = user->size; 160 return 0; 161 } 162 163 static void __user *apply_user_offset( 164 struct rt_sigframe_user_layout const *user, unsigned long offset) 165 { 166 char __user *base = (char __user *)user->sigframe; 167 168 return base + offset; 169 } 170 171 static int preserve_fpsimd_context(struct fpsimd_context __user *ctx) 172 { 173 struct user_fpsimd_state const *fpsimd = 174 ¤t->thread.uw.fpsimd_state; 175 int err; 176 177 /* copy the FP and status/control registers */ 178 err = __copy_to_user(ctx->vregs, fpsimd->vregs, sizeof(fpsimd->vregs)); 179 __put_user_error(fpsimd->fpsr, &ctx->fpsr, err); 180 __put_user_error(fpsimd->fpcr, &ctx->fpcr, err); 181 182 /* copy the magic/size information */ 183 __put_user_error(FPSIMD_MAGIC, &ctx->head.magic, err); 184 __put_user_error(sizeof(struct fpsimd_context), &ctx->head.size, err); 185 186 return err ? -EFAULT : 0; 187 } 188 189 static int restore_fpsimd_context(struct fpsimd_context __user *ctx) 190 { 191 struct user_fpsimd_state fpsimd; 192 __u32 magic, size; 193 int err = 0; 194 195 /* check the magic/size information */ 196 __get_user_error(magic, &ctx->head.magic, err); 197 __get_user_error(size, &ctx->head.size, err); 198 if (err) 199 return -EFAULT; 200 if (magic != FPSIMD_MAGIC || size != sizeof(struct fpsimd_context)) 201 return -EINVAL; 202 203 /* copy the FP and status/control registers */ 204 err = __copy_from_user(fpsimd.vregs, ctx->vregs, 205 sizeof(fpsimd.vregs)); 206 __get_user_error(fpsimd.fpsr, &ctx->fpsr, err); 207 __get_user_error(fpsimd.fpcr, &ctx->fpcr, err); 208 209 clear_thread_flag(TIF_SVE); 210 211 /* load the hardware registers from the fpsimd_state structure */ 212 if (!err) 213 fpsimd_update_current_state(&fpsimd); 214 215 return err ? -EFAULT : 0; 216 } 217 218 219 struct user_ctxs { 220 struct fpsimd_context __user *fpsimd; 221 struct sve_context __user *sve; 222 }; 223 224 #ifdef CONFIG_ARM64_SVE 225 226 static int preserve_sve_context(struct sve_context __user *ctx) 227 { 228 int err = 0; 229 u16 reserved[ARRAY_SIZE(ctx->__reserved)]; 230 unsigned int vl = current->thread.sve_vl; 231 unsigned int vq = 0; 232 233 if (test_thread_flag(TIF_SVE)) 234 vq = sve_vq_from_vl(vl); 235 236 memset(reserved, 0, sizeof(reserved)); 237 238 __put_user_error(SVE_MAGIC, &ctx->head.magic, err); 239 __put_user_error(round_up(SVE_SIG_CONTEXT_SIZE(vq), 16), 240 &ctx->head.size, err); 241 __put_user_error(vl, &ctx->vl, err); 242 BUILD_BUG_ON(sizeof(ctx->__reserved) != sizeof(reserved)); 243 err |= __copy_to_user(&ctx->__reserved, reserved, sizeof(reserved)); 244 245 if (vq) { 246 /* 247 * This assumes that the SVE state has already been saved to 248 * the task struct by calling the function 249 * fpsimd_signal_preserve_current_state(). 250 */ 251 err |= __copy_to_user((char __user *)ctx + SVE_SIG_REGS_OFFSET, 252 current->thread.sve_state, 253 SVE_SIG_REGS_SIZE(vq)); 254 } 255 256 return err ? -EFAULT : 0; 257 } 258 259 static int restore_sve_fpsimd_context(struct user_ctxs *user) 260 { 261 int err; 262 unsigned int vq; 263 struct user_fpsimd_state fpsimd; 264 struct sve_context sve; 265 266 if (__copy_from_user(&sve, user->sve, sizeof(sve))) 267 return -EFAULT; 268 269 if (sve.vl != current->thread.sve_vl) 270 return -EINVAL; 271 272 if (sve.head.size <= sizeof(*user->sve)) { 273 clear_thread_flag(TIF_SVE); 274 goto fpsimd_only; 275 } 276 277 vq = sve_vq_from_vl(sve.vl); 278 279 if (sve.head.size < SVE_SIG_CONTEXT_SIZE(vq)) 280 return -EINVAL; 281 282 /* 283 * Careful: we are about __copy_from_user() directly into 284 * thread.sve_state with preemption enabled, so protection is 285 * needed to prevent a racing context switch from writing stale 286 * registers back over the new data. 287 */ 288 289 fpsimd_flush_task_state(current); 290 /* From now, fpsimd_thread_switch() won't touch thread.sve_state */ 291 292 sve_alloc(current); 293 if (!current->thread.sve_state) { 294 clear_thread_flag(TIF_SVE); 295 return -ENOMEM; 296 } 297 298 err = __copy_from_user(current->thread.sve_state, 299 (char __user const *)user->sve + 300 SVE_SIG_REGS_OFFSET, 301 SVE_SIG_REGS_SIZE(vq)); 302 if (err) 303 return -EFAULT; 304 305 set_thread_flag(TIF_SVE); 306 307 fpsimd_only: 308 /* copy the FP and status/control registers */ 309 /* restore_sigframe() already checked that user->fpsimd != NULL. */ 310 err = __copy_from_user(fpsimd.vregs, user->fpsimd->vregs, 311 sizeof(fpsimd.vregs)); 312 __get_user_error(fpsimd.fpsr, &user->fpsimd->fpsr, err); 313 __get_user_error(fpsimd.fpcr, &user->fpsimd->fpcr, err); 314 315 /* load the hardware registers from the fpsimd_state structure */ 316 if (!err) 317 fpsimd_update_current_state(&fpsimd); 318 319 return err ? -EFAULT : 0; 320 } 321 322 #else /* ! CONFIG_ARM64_SVE */ 323 324 /* Turn any non-optimised out attempts to use these into a link error: */ 325 extern int preserve_sve_context(void __user *ctx); 326 extern int restore_sve_fpsimd_context(struct user_ctxs *user); 327 328 #endif /* ! CONFIG_ARM64_SVE */ 329 330 331 static int parse_user_sigframe(struct user_ctxs *user, 332 struct rt_sigframe __user *sf) 333 { 334 struct sigcontext __user *const sc = &sf->uc.uc_mcontext; 335 struct _aarch64_ctx __user *head; 336 char __user *base = (char __user *)&sc->__reserved; 337 size_t offset = 0; 338 size_t limit = sizeof(sc->__reserved); 339 bool have_extra_context = false; 340 char const __user *const sfp = (char const __user *)sf; 341 342 user->fpsimd = NULL; 343 user->sve = NULL; 344 345 if (!IS_ALIGNED((unsigned long)base, 16)) 346 goto invalid; 347 348 while (1) { 349 int err = 0; 350 u32 magic, size; 351 char const __user *userp; 352 struct extra_context const __user *extra; 353 u64 extra_datap; 354 u32 extra_size; 355 struct _aarch64_ctx const __user *end; 356 u32 end_magic, end_size; 357 358 if (limit - offset < sizeof(*head)) 359 goto invalid; 360 361 if (!IS_ALIGNED(offset, 16)) 362 goto invalid; 363 364 head = (struct _aarch64_ctx __user *)(base + offset); 365 __get_user_error(magic, &head->magic, err); 366 __get_user_error(size, &head->size, err); 367 if (err) 368 return err; 369 370 if (limit - offset < size) 371 goto invalid; 372 373 switch (magic) { 374 case 0: 375 if (size) 376 goto invalid; 377 378 goto done; 379 380 case FPSIMD_MAGIC: 381 if (!system_supports_fpsimd()) 382 goto invalid; 383 if (user->fpsimd) 384 goto invalid; 385 386 if (size < sizeof(*user->fpsimd)) 387 goto invalid; 388 389 user->fpsimd = (struct fpsimd_context __user *)head; 390 break; 391 392 case ESR_MAGIC: 393 /* ignore */ 394 break; 395 396 case SVE_MAGIC: 397 if (!system_supports_sve()) 398 goto invalid; 399 400 if (user->sve) 401 goto invalid; 402 403 if (size < sizeof(*user->sve)) 404 goto invalid; 405 406 user->sve = (struct sve_context __user *)head; 407 break; 408 409 case EXTRA_MAGIC: 410 if (have_extra_context) 411 goto invalid; 412 413 if (size < sizeof(*extra)) 414 goto invalid; 415 416 userp = (char const __user *)head; 417 418 extra = (struct extra_context const __user *)userp; 419 userp += size; 420 421 __get_user_error(extra_datap, &extra->datap, err); 422 __get_user_error(extra_size, &extra->size, err); 423 if (err) 424 return err; 425 426 /* Check for the dummy terminator in __reserved[]: */ 427 428 if (limit - offset - size < TERMINATOR_SIZE) 429 goto invalid; 430 431 end = (struct _aarch64_ctx const __user *)userp; 432 userp += TERMINATOR_SIZE; 433 434 __get_user_error(end_magic, &end->magic, err); 435 __get_user_error(end_size, &end->size, err); 436 if (err) 437 return err; 438 439 if (end_magic || end_size) 440 goto invalid; 441 442 /* Prevent looping/repeated parsing of extra_context */ 443 have_extra_context = true; 444 445 base = (__force void __user *)extra_datap; 446 if (!IS_ALIGNED((unsigned long)base, 16)) 447 goto invalid; 448 449 if (!IS_ALIGNED(extra_size, 16)) 450 goto invalid; 451 452 if (base != userp) 453 goto invalid; 454 455 /* Reject "unreasonably large" frames: */ 456 if (extra_size > sfp + SIGFRAME_MAXSZ - userp) 457 goto invalid; 458 459 /* 460 * Ignore trailing terminator in __reserved[] 461 * and start parsing extra data: 462 */ 463 offset = 0; 464 limit = extra_size; 465 466 if (!access_ok(base, limit)) 467 goto invalid; 468 469 continue; 470 471 default: 472 goto invalid; 473 } 474 475 if (size < sizeof(*head)) 476 goto invalid; 477 478 if (limit - offset < size) 479 goto invalid; 480 481 offset += size; 482 } 483 484 done: 485 return 0; 486 487 invalid: 488 return -EINVAL; 489 } 490 491 static int restore_sigframe(struct pt_regs *regs, 492 struct rt_sigframe __user *sf) 493 { 494 sigset_t set; 495 int i, err; 496 struct user_ctxs user; 497 498 err = __copy_from_user(&set, &sf->uc.uc_sigmask, sizeof(set)); 499 if (err == 0) 500 set_current_blocked(&set); 501 502 for (i = 0; i < 31; i++) 503 __get_user_error(regs->regs[i], &sf->uc.uc_mcontext.regs[i], 504 err); 505 __get_user_error(regs->sp, &sf->uc.uc_mcontext.sp, err); 506 __get_user_error(regs->pc, &sf->uc.uc_mcontext.pc, err); 507 __get_user_error(regs->pstate, &sf->uc.uc_mcontext.pstate, err); 508 509 /* 510 * Avoid sys_rt_sigreturn() restarting. 511 */ 512 forget_syscall(regs); 513 514 err |= !valid_user_regs(®s->user_regs, current); 515 if (err == 0) 516 err = parse_user_sigframe(&user, sf); 517 518 if (err == 0 && system_supports_fpsimd()) { 519 if (!user.fpsimd) 520 return -EINVAL; 521 522 if (user.sve) { 523 if (!system_supports_sve()) 524 return -EINVAL; 525 526 err = restore_sve_fpsimd_context(&user); 527 } else { 528 err = restore_fpsimd_context(user.fpsimd); 529 } 530 } 531 532 return err; 533 } 534 535 SYSCALL_DEFINE0(rt_sigreturn) 536 { 537 struct pt_regs *regs = current_pt_regs(); 538 struct rt_sigframe __user *frame; 539 540 /* Always make any pending restarted system calls return -EINTR */ 541 current->restart_block.fn = do_no_restart_syscall; 542 543 /* 544 * Since we stacked the signal on a 128-bit boundary, then 'sp' should 545 * be word aligned here. 546 */ 547 if (regs->sp & 15) 548 goto badframe; 549 550 frame = (struct rt_sigframe __user *)regs->sp; 551 552 if (!access_ok(frame, sizeof (*frame))) 553 goto badframe; 554 555 if (restore_sigframe(regs, frame)) 556 goto badframe; 557 558 if (restore_altstack(&frame->uc.uc_stack)) 559 goto badframe; 560 561 return regs->regs[0]; 562 563 badframe: 564 arm64_notify_segfault(regs->sp); 565 return 0; 566 } 567 568 /* 569 * Determine the layout of optional records in the signal frame 570 * 571 * add_all: if true, lays out the biggest possible signal frame for 572 * this task; otherwise, generates a layout for the current state 573 * of the task. 574 */ 575 static int setup_sigframe_layout(struct rt_sigframe_user_layout *user, 576 bool add_all) 577 { 578 int err; 579 580 err = sigframe_alloc(user, &user->fpsimd_offset, 581 sizeof(struct fpsimd_context)); 582 if (err) 583 return err; 584 585 /* fault information, if valid */ 586 if (add_all || current->thread.fault_code) { 587 err = sigframe_alloc(user, &user->esr_offset, 588 sizeof(struct esr_context)); 589 if (err) 590 return err; 591 } 592 593 if (system_supports_sve()) { 594 unsigned int vq = 0; 595 596 if (add_all || test_thread_flag(TIF_SVE)) { 597 int vl = sve_max_vl; 598 599 if (!add_all) 600 vl = current->thread.sve_vl; 601 602 vq = sve_vq_from_vl(vl); 603 } 604 605 err = sigframe_alloc(user, &user->sve_offset, 606 SVE_SIG_CONTEXT_SIZE(vq)); 607 if (err) 608 return err; 609 } 610 611 return sigframe_alloc_end(user); 612 } 613 614 static int setup_sigframe(struct rt_sigframe_user_layout *user, 615 struct pt_regs *regs, sigset_t *set) 616 { 617 int i, err = 0; 618 struct rt_sigframe __user *sf = user->sigframe; 619 620 /* set up the stack frame for unwinding */ 621 __put_user_error(regs->regs[29], &user->next_frame->fp, err); 622 __put_user_error(regs->regs[30], &user->next_frame->lr, err); 623 624 for (i = 0; i < 31; i++) 625 __put_user_error(regs->regs[i], &sf->uc.uc_mcontext.regs[i], 626 err); 627 __put_user_error(regs->sp, &sf->uc.uc_mcontext.sp, err); 628 __put_user_error(regs->pc, &sf->uc.uc_mcontext.pc, err); 629 __put_user_error(regs->pstate, &sf->uc.uc_mcontext.pstate, err); 630 631 __put_user_error(current->thread.fault_address, &sf->uc.uc_mcontext.fault_address, err); 632 633 err |= __copy_to_user(&sf->uc.uc_sigmask, set, sizeof(*set)); 634 635 if (err == 0 && system_supports_fpsimd()) { 636 struct fpsimd_context __user *fpsimd_ctx = 637 apply_user_offset(user, user->fpsimd_offset); 638 err |= preserve_fpsimd_context(fpsimd_ctx); 639 } 640 641 /* fault information, if valid */ 642 if (err == 0 && user->esr_offset) { 643 struct esr_context __user *esr_ctx = 644 apply_user_offset(user, user->esr_offset); 645 646 __put_user_error(ESR_MAGIC, &esr_ctx->head.magic, err); 647 __put_user_error(sizeof(*esr_ctx), &esr_ctx->head.size, err); 648 __put_user_error(current->thread.fault_code, &esr_ctx->esr, err); 649 } 650 651 /* Scalable Vector Extension state, if present */ 652 if (system_supports_sve() && err == 0 && user->sve_offset) { 653 struct sve_context __user *sve_ctx = 654 apply_user_offset(user, user->sve_offset); 655 err |= preserve_sve_context(sve_ctx); 656 } 657 658 if (err == 0 && user->extra_offset) { 659 char __user *sfp = (char __user *)user->sigframe; 660 char __user *userp = 661 apply_user_offset(user, user->extra_offset); 662 663 struct extra_context __user *extra; 664 struct _aarch64_ctx __user *end; 665 u64 extra_datap; 666 u32 extra_size; 667 668 extra = (struct extra_context __user *)userp; 669 userp += EXTRA_CONTEXT_SIZE; 670 671 end = (struct _aarch64_ctx __user *)userp; 672 userp += TERMINATOR_SIZE; 673 674 /* 675 * extra_datap is just written to the signal frame. 676 * The value gets cast back to a void __user * 677 * during sigreturn. 678 */ 679 extra_datap = (__force u64)userp; 680 extra_size = sfp + round_up(user->size, 16) - userp; 681 682 __put_user_error(EXTRA_MAGIC, &extra->head.magic, err); 683 __put_user_error(EXTRA_CONTEXT_SIZE, &extra->head.size, err); 684 __put_user_error(extra_datap, &extra->datap, err); 685 __put_user_error(extra_size, &extra->size, err); 686 687 /* Add the terminator */ 688 __put_user_error(0, &end->magic, err); 689 __put_user_error(0, &end->size, err); 690 } 691 692 /* set the "end" magic */ 693 if (err == 0) { 694 struct _aarch64_ctx __user *end = 695 apply_user_offset(user, user->end_offset); 696 697 __put_user_error(0, &end->magic, err); 698 __put_user_error(0, &end->size, err); 699 } 700 701 return err; 702 } 703 704 static int get_sigframe(struct rt_sigframe_user_layout *user, 705 struct ksignal *ksig, struct pt_regs *regs) 706 { 707 unsigned long sp, sp_top; 708 int err; 709 710 init_user_layout(user); 711 err = setup_sigframe_layout(user, false); 712 if (err) 713 return err; 714 715 sp = sp_top = sigsp(regs->sp, ksig); 716 717 sp = round_down(sp - sizeof(struct frame_record), 16); 718 user->next_frame = (struct frame_record __user *)sp; 719 720 sp = round_down(sp, 16) - sigframe_size(user); 721 user->sigframe = (struct rt_sigframe __user *)sp; 722 723 /* 724 * Check that we can actually write to the signal frame. 725 */ 726 if (!access_ok(user->sigframe, sp_top - sp)) 727 return -EFAULT; 728 729 return 0; 730 } 731 732 static void setup_return(struct pt_regs *regs, struct k_sigaction *ka, 733 struct rt_sigframe_user_layout *user, int usig) 734 { 735 __sigrestore_t sigtramp; 736 737 regs->regs[0] = usig; 738 regs->sp = (unsigned long)user->sigframe; 739 regs->regs[29] = (unsigned long)&user->next_frame->fp; 740 regs->pc = (unsigned long)ka->sa.sa_handler; 741 742 /* 743 * Signal delivery is a (wacky) indirect function call in 744 * userspace, so simulate the same setting of BTYPE as a BLR 745 * <register containing the signal handler entry point>. 746 * Signal delivery to a location in a PROT_BTI guarded page 747 * that is not a function entry point will now trigger a 748 * SIGILL in userspace. 749 * 750 * If the signal handler entry point is not in a PROT_BTI 751 * guarded page, this is harmless. 752 */ 753 if (system_supports_bti()) { 754 regs->pstate &= ~PSR_BTYPE_MASK; 755 regs->pstate |= PSR_BTYPE_C; 756 } 757 758 /* TCO (Tag Check Override) always cleared for signal handlers */ 759 regs->pstate &= ~PSR_TCO_BIT; 760 761 if (ka->sa.sa_flags & SA_RESTORER) 762 sigtramp = ka->sa.sa_restorer; 763 else 764 sigtramp = VDSO_SYMBOL(current->mm->context.vdso, sigtramp); 765 766 regs->regs[30] = (unsigned long)sigtramp; 767 } 768 769 static int setup_rt_frame(int usig, struct ksignal *ksig, sigset_t *set, 770 struct pt_regs *regs) 771 { 772 struct rt_sigframe_user_layout user; 773 struct rt_sigframe __user *frame; 774 int err = 0; 775 776 fpsimd_signal_preserve_current_state(); 777 778 if (get_sigframe(&user, ksig, regs)) 779 return 1; 780 781 frame = user.sigframe; 782 783 __put_user_error(0, &frame->uc.uc_flags, err); 784 __put_user_error(NULL, &frame->uc.uc_link, err); 785 786 err |= __save_altstack(&frame->uc.uc_stack, regs->sp); 787 err |= setup_sigframe(&user, regs, set); 788 if (err == 0) { 789 setup_return(regs, &ksig->ka, &user, usig); 790 if (ksig->ka.sa.sa_flags & SA_SIGINFO) { 791 err |= copy_siginfo_to_user(&frame->info, &ksig->info); 792 regs->regs[1] = (unsigned long)&frame->info; 793 regs->regs[2] = (unsigned long)&frame->uc; 794 } 795 } 796 797 return err; 798 } 799 800 static void setup_restart_syscall(struct pt_regs *regs) 801 { 802 if (is_compat_task()) 803 compat_setup_restart_syscall(regs); 804 else 805 regs->regs[8] = __NR_restart_syscall; 806 } 807 808 /* 809 * OK, we're invoking a handler 810 */ 811 static void handle_signal(struct ksignal *ksig, struct pt_regs *regs) 812 { 813 sigset_t *oldset = sigmask_to_save(); 814 int usig = ksig->sig; 815 int ret; 816 817 rseq_signal_deliver(ksig, regs); 818 819 /* 820 * Set up the stack frame 821 */ 822 if (is_compat_task()) { 823 if (ksig->ka.sa.sa_flags & SA_SIGINFO) 824 ret = compat_setup_rt_frame(usig, ksig, oldset, regs); 825 else 826 ret = compat_setup_frame(usig, ksig, oldset, regs); 827 } else { 828 ret = setup_rt_frame(usig, ksig, oldset, regs); 829 } 830 831 /* 832 * Check that the resulting registers are actually sane. 833 */ 834 ret |= !valid_user_regs(®s->user_regs, current); 835 836 /* Step into the signal handler if we are stepping */ 837 signal_setup_done(ret, ksig, test_thread_flag(TIF_SINGLESTEP)); 838 } 839 840 /* 841 * Note that 'init' is a special process: it doesn't get signals it doesn't 842 * want to handle. Thus you cannot kill init even with a SIGKILL even by 843 * mistake. 844 * 845 * Note that we go through the signals twice: once to check the signals that 846 * the kernel can handle, and then we build all the user-level signal handling 847 * stack-frames in one go after that. 848 */ 849 static void do_signal(struct pt_regs *regs) 850 { 851 unsigned long continue_addr = 0, restart_addr = 0; 852 int retval = 0; 853 struct ksignal ksig; 854 bool syscall = in_syscall(regs); 855 856 /* 857 * If we were from a system call, check for system call restarting... 858 */ 859 if (syscall) { 860 continue_addr = regs->pc; 861 restart_addr = continue_addr - (compat_thumb_mode(regs) ? 2 : 4); 862 retval = regs->regs[0]; 863 864 /* 865 * Avoid additional syscall restarting via ret_to_user. 866 */ 867 forget_syscall(regs); 868 869 /* 870 * Prepare for system call restart. We do this here so that a 871 * debugger will see the already changed PC. 872 */ 873 switch (retval) { 874 case -ERESTARTNOHAND: 875 case -ERESTARTSYS: 876 case -ERESTARTNOINTR: 877 case -ERESTART_RESTARTBLOCK: 878 regs->regs[0] = regs->orig_x0; 879 regs->pc = restart_addr; 880 break; 881 } 882 } 883 884 /* 885 * Get the signal to deliver. When running under ptrace, at this point 886 * the debugger may change all of our registers. 887 */ 888 if (get_signal(&ksig)) { 889 /* 890 * Depending on the signal settings, we may need to revert the 891 * decision to restart the system call, but skip this if a 892 * debugger has chosen to restart at a different PC. 893 */ 894 if (regs->pc == restart_addr && 895 (retval == -ERESTARTNOHAND || 896 retval == -ERESTART_RESTARTBLOCK || 897 (retval == -ERESTARTSYS && 898 !(ksig.ka.sa.sa_flags & SA_RESTART)))) { 899 syscall_set_return_value(current, regs, -EINTR, 0); 900 regs->pc = continue_addr; 901 } 902 903 handle_signal(&ksig, regs); 904 return; 905 } 906 907 /* 908 * Handle restarting a different system call. As above, if a debugger 909 * has chosen to restart at a different PC, ignore the restart. 910 */ 911 if (syscall && regs->pc == restart_addr) { 912 if (retval == -ERESTART_RESTARTBLOCK) 913 setup_restart_syscall(regs); 914 user_rewind_single_step(current); 915 } 916 917 restore_saved_sigmask(); 918 } 919 920 void do_notify_resume(struct pt_regs *regs, unsigned long thread_flags) 921 { 922 do { 923 if (thread_flags & _TIF_NEED_RESCHED) { 924 /* Unmask Debug and SError for the next task */ 925 local_daif_restore(DAIF_PROCCTX_NOIRQ); 926 927 schedule(); 928 } else { 929 local_daif_restore(DAIF_PROCCTX); 930 931 if (thread_flags & _TIF_UPROBE) 932 uprobe_notify_resume(regs); 933 934 if (thread_flags & _TIF_MTE_ASYNC_FAULT) { 935 clear_thread_flag(TIF_MTE_ASYNC_FAULT); 936 send_sig_fault(SIGSEGV, SEGV_MTEAERR, 937 (void __user *)NULL, current); 938 } 939 940 if (thread_flags & (_TIF_SIGPENDING | _TIF_NOTIFY_SIGNAL)) 941 do_signal(regs); 942 943 if (thread_flags & _TIF_NOTIFY_RESUME) { 944 tracehook_notify_resume(regs); 945 rseq_handle_notify_resume(NULL, regs); 946 } 947 948 if (thread_flags & _TIF_FOREIGN_FPSTATE) 949 fpsimd_restore_current_state(); 950 } 951 952 local_daif_mask(); 953 thread_flags = READ_ONCE(current_thread_info()->flags); 954 } while (thread_flags & _TIF_WORK_MASK); 955 } 956 957 unsigned long __ro_after_init signal_minsigstksz; 958 959 /* 960 * Determine the stack space required for guaranteed signal devliery. 961 * This function is used to populate AT_MINSIGSTKSZ at process startup. 962 * cpufeatures setup is assumed to be complete. 963 */ 964 void __init minsigstksz_setup(void) 965 { 966 struct rt_sigframe_user_layout user; 967 968 init_user_layout(&user); 969 970 /* 971 * If this fails, SIGFRAME_MAXSZ needs to be enlarged. It won't 972 * be big enough, but it's our best guess: 973 */ 974 if (WARN_ON(setup_sigframe_layout(&user, true))) 975 return; 976 977 signal_minsigstksz = sigframe_size(&user) + 978 round_up(sizeof(struct frame_record), 16) + 979 16; /* max alignment padding */ 980 } 981 982 /* 983 * Compile-time assertions for siginfo_t offsets. Check NSIG* as well, as 984 * changes likely come with new fields that should be added below. 985 */ 986 static_assert(NSIGILL == 11); 987 static_assert(NSIGFPE == 15); 988 static_assert(NSIGSEGV == 9); 989 static_assert(NSIGBUS == 5); 990 static_assert(NSIGTRAP == 6); 991 static_assert(NSIGCHLD == 6); 992 static_assert(NSIGSYS == 2); 993 static_assert(sizeof(siginfo_t) == 128); 994 static_assert(__alignof__(siginfo_t) == 8); 995 static_assert(offsetof(siginfo_t, si_signo) == 0x00); 996 static_assert(offsetof(siginfo_t, si_errno) == 0x04); 997 static_assert(offsetof(siginfo_t, si_code) == 0x08); 998 static_assert(offsetof(siginfo_t, si_pid) == 0x10); 999 static_assert(offsetof(siginfo_t, si_uid) == 0x14); 1000 static_assert(offsetof(siginfo_t, si_tid) == 0x10); 1001 static_assert(offsetof(siginfo_t, si_overrun) == 0x14); 1002 static_assert(offsetof(siginfo_t, si_status) == 0x18); 1003 static_assert(offsetof(siginfo_t, si_utime) == 0x20); 1004 static_assert(offsetof(siginfo_t, si_stime) == 0x28); 1005 static_assert(offsetof(siginfo_t, si_value) == 0x18); 1006 static_assert(offsetof(siginfo_t, si_int) == 0x18); 1007 static_assert(offsetof(siginfo_t, si_ptr) == 0x18); 1008 static_assert(offsetof(siginfo_t, si_addr) == 0x10); 1009 static_assert(offsetof(siginfo_t, si_addr_lsb) == 0x18); 1010 static_assert(offsetof(siginfo_t, si_lower) == 0x20); 1011 static_assert(offsetof(siginfo_t, si_upper) == 0x28); 1012 static_assert(offsetof(siginfo_t, si_pkey) == 0x20); 1013 static_assert(offsetof(siginfo_t, si_perf_data) == 0x18); 1014 static_assert(offsetof(siginfo_t, si_perf_type) == 0x20); 1015 static_assert(offsetof(siginfo_t, si_band) == 0x10); 1016 static_assert(offsetof(siginfo_t, si_fd) == 0x18); 1017 static_assert(offsetof(siginfo_t, si_call_addr) == 0x10); 1018 static_assert(offsetof(siginfo_t, si_syscall) == 0x18); 1019 static_assert(offsetof(siginfo_t, si_arch) == 0x1c); 1020