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/freezer.h> 15 #include <linux/stddef.h> 16 #include <linux/uaccess.h> 17 #include <linux/sizes.h> 18 #include <linux/string.h> 19 #include <linux/resume_user_mode.h> 20 #include <linux/ratelimit.h> 21 #include <linux/syscalls.h> 22 23 #include <asm/daifflags.h> 24 #include <asm/debug-monitors.h> 25 #include <asm/elf.h> 26 #include <asm/cacheflush.h> 27 #include <asm/ucontext.h> 28 #include <asm/unistd.h> 29 #include <asm/fpsimd.h> 30 #include <asm/ptrace.h> 31 #include <asm/syscall.h> 32 #include <asm/signal32.h> 33 #include <asm/traps.h> 34 #include <asm/vdso.h> 35 36 /* 37 * Do a signal return; undo the signal stack. These are aligned to 128-bit. 38 */ 39 struct rt_sigframe { 40 struct siginfo info; 41 struct ucontext uc; 42 }; 43 44 struct frame_record { 45 u64 fp; 46 u64 lr; 47 }; 48 49 struct rt_sigframe_user_layout { 50 struct rt_sigframe __user *sigframe; 51 struct frame_record __user *next_frame; 52 53 unsigned long size; /* size of allocated sigframe data */ 54 unsigned long limit; /* largest allowed size */ 55 56 unsigned long fpsimd_offset; 57 unsigned long esr_offset; 58 unsigned long sve_offset; 59 unsigned long za_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 struct za_context __user *za; 223 }; 224 225 #ifdef CONFIG_ARM64_SVE 226 227 static int preserve_sve_context(struct sve_context __user *ctx) 228 { 229 int err = 0; 230 u16 reserved[ARRAY_SIZE(ctx->__reserved)]; 231 u16 flags = 0; 232 unsigned int vl = task_get_sve_vl(current); 233 unsigned int vq = 0; 234 235 if (thread_sm_enabled(¤t->thread)) { 236 vl = task_get_sme_vl(current); 237 vq = sve_vq_from_vl(vl); 238 flags |= SVE_SIG_FLAG_SM; 239 } else if (test_thread_flag(TIF_SVE)) { 240 vq = sve_vq_from_vl(vl); 241 } 242 243 memset(reserved, 0, sizeof(reserved)); 244 245 __put_user_error(SVE_MAGIC, &ctx->head.magic, err); 246 __put_user_error(round_up(SVE_SIG_CONTEXT_SIZE(vq), 16), 247 &ctx->head.size, err); 248 __put_user_error(vl, &ctx->vl, err); 249 __put_user_error(flags, &ctx->flags, err); 250 BUILD_BUG_ON(sizeof(ctx->__reserved) != sizeof(reserved)); 251 err |= __copy_to_user(&ctx->__reserved, reserved, sizeof(reserved)); 252 253 if (vq) { 254 /* 255 * This assumes that the SVE state has already been saved to 256 * the task struct by calling the function 257 * fpsimd_signal_preserve_current_state(). 258 */ 259 err |= __copy_to_user((char __user *)ctx + SVE_SIG_REGS_OFFSET, 260 current->thread.sve_state, 261 SVE_SIG_REGS_SIZE(vq)); 262 } 263 264 return err ? -EFAULT : 0; 265 } 266 267 static int restore_sve_fpsimd_context(struct user_ctxs *user) 268 { 269 int err; 270 unsigned int vl, vq; 271 struct user_fpsimd_state fpsimd; 272 struct sve_context sve; 273 274 if (__copy_from_user(&sve, user->sve, sizeof(sve))) 275 return -EFAULT; 276 277 if (sve.flags & SVE_SIG_FLAG_SM) { 278 if (!system_supports_sme()) 279 return -EINVAL; 280 281 vl = task_get_sme_vl(current); 282 } else { 283 if (!system_supports_sve()) 284 return -EINVAL; 285 286 vl = task_get_sve_vl(current); 287 } 288 289 if (sve.vl != vl) 290 return -EINVAL; 291 292 if (sve.head.size <= sizeof(*user->sve)) { 293 clear_thread_flag(TIF_SVE); 294 current->thread.svcr &= ~SVCR_SM_MASK; 295 goto fpsimd_only; 296 } 297 298 vq = sve_vq_from_vl(sve.vl); 299 300 if (sve.head.size < SVE_SIG_CONTEXT_SIZE(vq)) 301 return -EINVAL; 302 303 /* 304 * Careful: we are about __copy_from_user() directly into 305 * thread.sve_state with preemption enabled, so protection is 306 * needed to prevent a racing context switch from writing stale 307 * registers back over the new data. 308 */ 309 310 fpsimd_flush_task_state(current); 311 /* From now, fpsimd_thread_switch() won't touch thread.sve_state */ 312 313 sve_alloc(current); 314 if (!current->thread.sve_state) { 315 clear_thread_flag(TIF_SVE); 316 return -ENOMEM; 317 } 318 319 err = __copy_from_user(current->thread.sve_state, 320 (char __user const *)user->sve + 321 SVE_SIG_REGS_OFFSET, 322 SVE_SIG_REGS_SIZE(vq)); 323 if (err) 324 return -EFAULT; 325 326 if (sve.flags & SVE_SIG_FLAG_SM) 327 current->thread.svcr |= SVCR_SM_MASK; 328 else 329 set_thread_flag(TIF_SVE); 330 331 fpsimd_only: 332 /* copy the FP and status/control registers */ 333 /* restore_sigframe() already checked that user->fpsimd != NULL. */ 334 err = __copy_from_user(fpsimd.vregs, user->fpsimd->vregs, 335 sizeof(fpsimd.vregs)); 336 __get_user_error(fpsimd.fpsr, &user->fpsimd->fpsr, err); 337 __get_user_error(fpsimd.fpcr, &user->fpsimd->fpcr, err); 338 339 /* load the hardware registers from the fpsimd_state structure */ 340 if (!err) 341 fpsimd_update_current_state(&fpsimd); 342 343 return err ? -EFAULT : 0; 344 } 345 346 #else /* ! CONFIG_ARM64_SVE */ 347 348 static int restore_sve_fpsimd_context(struct user_ctxs *user) 349 { 350 WARN_ON_ONCE(1); 351 return -EINVAL; 352 } 353 354 /* Turn any non-optimised out attempts to use this into a link error: */ 355 extern int preserve_sve_context(void __user *ctx); 356 357 #endif /* ! CONFIG_ARM64_SVE */ 358 359 #ifdef CONFIG_ARM64_SME 360 361 static int preserve_za_context(struct za_context __user *ctx) 362 { 363 int err = 0; 364 u16 reserved[ARRAY_SIZE(ctx->__reserved)]; 365 unsigned int vl = task_get_sme_vl(current); 366 unsigned int vq; 367 368 if (thread_za_enabled(¤t->thread)) 369 vq = sve_vq_from_vl(vl); 370 else 371 vq = 0; 372 373 memset(reserved, 0, sizeof(reserved)); 374 375 __put_user_error(ZA_MAGIC, &ctx->head.magic, err); 376 __put_user_error(round_up(ZA_SIG_CONTEXT_SIZE(vq), 16), 377 &ctx->head.size, err); 378 __put_user_error(vl, &ctx->vl, err); 379 BUILD_BUG_ON(sizeof(ctx->__reserved) != sizeof(reserved)); 380 err |= __copy_to_user(&ctx->__reserved, reserved, sizeof(reserved)); 381 382 if (vq) { 383 /* 384 * This assumes that the ZA state has already been saved to 385 * the task struct by calling the function 386 * fpsimd_signal_preserve_current_state(). 387 */ 388 err |= __copy_to_user((char __user *)ctx + ZA_SIG_REGS_OFFSET, 389 current->thread.za_state, 390 ZA_SIG_REGS_SIZE(vq)); 391 } 392 393 return err ? -EFAULT : 0; 394 } 395 396 static int restore_za_context(struct user_ctxs *user) 397 { 398 int err; 399 unsigned int vq; 400 struct za_context za; 401 402 if (__copy_from_user(&za, user->za, sizeof(za))) 403 return -EFAULT; 404 405 if (za.vl != task_get_sme_vl(current)) 406 return -EINVAL; 407 408 if (za.head.size <= sizeof(*user->za)) { 409 current->thread.svcr &= ~SVCR_ZA_MASK; 410 return 0; 411 } 412 413 vq = sve_vq_from_vl(za.vl); 414 415 if (za.head.size < ZA_SIG_CONTEXT_SIZE(vq)) 416 return -EINVAL; 417 418 /* 419 * Careful: we are about __copy_from_user() directly into 420 * thread.za_state with preemption enabled, so protection is 421 * needed to prevent a racing context switch from writing stale 422 * registers back over the new data. 423 */ 424 425 fpsimd_flush_task_state(current); 426 /* From now, fpsimd_thread_switch() won't touch thread.sve_state */ 427 428 sme_alloc(current); 429 if (!current->thread.za_state) { 430 current->thread.svcr &= ~SVCR_ZA_MASK; 431 clear_thread_flag(TIF_SME); 432 return -ENOMEM; 433 } 434 435 err = __copy_from_user(current->thread.za_state, 436 (char __user const *)user->za + 437 ZA_SIG_REGS_OFFSET, 438 ZA_SIG_REGS_SIZE(vq)); 439 if (err) 440 return -EFAULT; 441 442 set_thread_flag(TIF_SME); 443 current->thread.svcr |= SVCR_ZA_MASK; 444 445 return 0; 446 } 447 #else /* ! CONFIG_ARM64_SME */ 448 449 /* Turn any non-optimised out attempts to use these into a link error: */ 450 extern int preserve_za_context(void __user *ctx); 451 extern int restore_za_context(struct user_ctxs *user); 452 453 #endif /* ! CONFIG_ARM64_SME */ 454 455 static int parse_user_sigframe(struct user_ctxs *user, 456 struct rt_sigframe __user *sf) 457 { 458 struct sigcontext __user *const sc = &sf->uc.uc_mcontext; 459 struct _aarch64_ctx __user *head; 460 char __user *base = (char __user *)&sc->__reserved; 461 size_t offset = 0; 462 size_t limit = sizeof(sc->__reserved); 463 bool have_extra_context = false; 464 char const __user *const sfp = (char const __user *)sf; 465 466 user->fpsimd = NULL; 467 user->sve = NULL; 468 user->za = NULL; 469 470 if (!IS_ALIGNED((unsigned long)base, 16)) 471 goto invalid; 472 473 while (1) { 474 int err = 0; 475 u32 magic, size; 476 char const __user *userp; 477 struct extra_context const __user *extra; 478 u64 extra_datap; 479 u32 extra_size; 480 struct _aarch64_ctx const __user *end; 481 u32 end_magic, end_size; 482 483 if (limit - offset < sizeof(*head)) 484 goto invalid; 485 486 if (!IS_ALIGNED(offset, 16)) 487 goto invalid; 488 489 head = (struct _aarch64_ctx __user *)(base + offset); 490 __get_user_error(magic, &head->magic, err); 491 __get_user_error(size, &head->size, err); 492 if (err) 493 return err; 494 495 if (limit - offset < size) 496 goto invalid; 497 498 switch (magic) { 499 case 0: 500 if (size) 501 goto invalid; 502 503 goto done; 504 505 case FPSIMD_MAGIC: 506 if (!system_supports_fpsimd()) 507 goto invalid; 508 if (user->fpsimd) 509 goto invalid; 510 511 if (size < sizeof(*user->fpsimd)) 512 goto invalid; 513 514 user->fpsimd = (struct fpsimd_context __user *)head; 515 break; 516 517 case ESR_MAGIC: 518 /* ignore */ 519 break; 520 521 case SVE_MAGIC: 522 if (!system_supports_sve() && !system_supports_sme()) 523 goto invalid; 524 525 if (user->sve) 526 goto invalid; 527 528 if (size < sizeof(*user->sve)) 529 goto invalid; 530 531 user->sve = (struct sve_context __user *)head; 532 break; 533 534 case ZA_MAGIC: 535 if (!system_supports_sme()) 536 goto invalid; 537 538 if (user->za) 539 goto invalid; 540 541 if (size < sizeof(*user->za)) 542 goto invalid; 543 544 user->za = (struct za_context __user *)head; 545 break; 546 547 case EXTRA_MAGIC: 548 if (have_extra_context) 549 goto invalid; 550 551 if (size < sizeof(*extra)) 552 goto invalid; 553 554 userp = (char const __user *)head; 555 556 extra = (struct extra_context const __user *)userp; 557 userp += size; 558 559 __get_user_error(extra_datap, &extra->datap, err); 560 __get_user_error(extra_size, &extra->size, err); 561 if (err) 562 return err; 563 564 /* Check for the dummy terminator in __reserved[]: */ 565 566 if (limit - offset - size < TERMINATOR_SIZE) 567 goto invalid; 568 569 end = (struct _aarch64_ctx const __user *)userp; 570 userp += TERMINATOR_SIZE; 571 572 __get_user_error(end_magic, &end->magic, err); 573 __get_user_error(end_size, &end->size, err); 574 if (err) 575 return err; 576 577 if (end_magic || end_size) 578 goto invalid; 579 580 /* Prevent looping/repeated parsing of extra_context */ 581 have_extra_context = true; 582 583 base = (__force void __user *)extra_datap; 584 if (!IS_ALIGNED((unsigned long)base, 16)) 585 goto invalid; 586 587 if (!IS_ALIGNED(extra_size, 16)) 588 goto invalid; 589 590 if (base != userp) 591 goto invalid; 592 593 /* Reject "unreasonably large" frames: */ 594 if (extra_size > sfp + SIGFRAME_MAXSZ - userp) 595 goto invalid; 596 597 /* 598 * Ignore trailing terminator in __reserved[] 599 * and start parsing extra data: 600 */ 601 offset = 0; 602 limit = extra_size; 603 604 if (!access_ok(base, limit)) 605 goto invalid; 606 607 continue; 608 609 default: 610 goto invalid; 611 } 612 613 if (size < sizeof(*head)) 614 goto invalid; 615 616 if (limit - offset < size) 617 goto invalid; 618 619 offset += size; 620 } 621 622 done: 623 return 0; 624 625 invalid: 626 return -EINVAL; 627 } 628 629 static int restore_sigframe(struct pt_regs *regs, 630 struct rt_sigframe __user *sf) 631 { 632 sigset_t set; 633 int i, err; 634 struct user_ctxs user; 635 636 err = __copy_from_user(&set, &sf->uc.uc_sigmask, sizeof(set)); 637 if (err == 0) 638 set_current_blocked(&set); 639 640 for (i = 0; i < 31; i++) 641 __get_user_error(regs->regs[i], &sf->uc.uc_mcontext.regs[i], 642 err); 643 __get_user_error(regs->sp, &sf->uc.uc_mcontext.sp, err); 644 __get_user_error(regs->pc, &sf->uc.uc_mcontext.pc, err); 645 __get_user_error(regs->pstate, &sf->uc.uc_mcontext.pstate, err); 646 647 /* 648 * Avoid sys_rt_sigreturn() restarting. 649 */ 650 forget_syscall(regs); 651 652 err |= !valid_user_regs(®s->user_regs, current); 653 if (err == 0) 654 err = parse_user_sigframe(&user, sf); 655 656 if (err == 0 && system_supports_fpsimd()) { 657 if (!user.fpsimd) 658 return -EINVAL; 659 660 if (user.sve) 661 err = restore_sve_fpsimd_context(&user); 662 else 663 err = restore_fpsimd_context(user.fpsimd); 664 } 665 666 if (err == 0 && system_supports_sme() && user.za) 667 err = restore_za_context(&user); 668 669 return err; 670 } 671 672 SYSCALL_DEFINE0(rt_sigreturn) 673 { 674 struct pt_regs *regs = current_pt_regs(); 675 struct rt_sigframe __user *frame; 676 677 /* Always make any pending restarted system calls return -EINTR */ 678 current->restart_block.fn = do_no_restart_syscall; 679 680 /* 681 * Since we stacked the signal on a 128-bit boundary, then 'sp' should 682 * be word aligned here. 683 */ 684 if (regs->sp & 15) 685 goto badframe; 686 687 frame = (struct rt_sigframe __user *)regs->sp; 688 689 if (!access_ok(frame, sizeof (*frame))) 690 goto badframe; 691 692 if (restore_sigframe(regs, frame)) 693 goto badframe; 694 695 if (restore_altstack(&frame->uc.uc_stack)) 696 goto badframe; 697 698 return regs->regs[0]; 699 700 badframe: 701 arm64_notify_segfault(regs->sp); 702 return 0; 703 } 704 705 /* 706 * Determine the layout of optional records in the signal frame 707 * 708 * add_all: if true, lays out the biggest possible signal frame for 709 * this task; otherwise, generates a layout for the current state 710 * of the task. 711 */ 712 static int setup_sigframe_layout(struct rt_sigframe_user_layout *user, 713 bool add_all) 714 { 715 int err; 716 717 if (system_supports_fpsimd()) { 718 err = sigframe_alloc(user, &user->fpsimd_offset, 719 sizeof(struct fpsimd_context)); 720 if (err) 721 return err; 722 } 723 724 /* fault information, if valid */ 725 if (add_all || current->thread.fault_code) { 726 err = sigframe_alloc(user, &user->esr_offset, 727 sizeof(struct esr_context)); 728 if (err) 729 return err; 730 } 731 732 if (system_supports_sve()) { 733 unsigned int vq = 0; 734 735 if (add_all || test_thread_flag(TIF_SVE) || 736 thread_sm_enabled(¤t->thread)) { 737 int vl = max(sve_max_vl(), sme_max_vl()); 738 739 if (!add_all) 740 vl = thread_get_cur_vl(¤t->thread); 741 742 vq = sve_vq_from_vl(vl); 743 } 744 745 err = sigframe_alloc(user, &user->sve_offset, 746 SVE_SIG_CONTEXT_SIZE(vq)); 747 if (err) 748 return err; 749 } 750 751 if (system_supports_sme()) { 752 unsigned int vl; 753 unsigned int vq = 0; 754 755 if (add_all) 756 vl = sme_max_vl(); 757 else 758 vl = task_get_sme_vl(current); 759 760 if (thread_za_enabled(¤t->thread)) 761 vq = sve_vq_from_vl(vl); 762 763 err = sigframe_alloc(user, &user->za_offset, 764 ZA_SIG_CONTEXT_SIZE(vq)); 765 if (err) 766 return err; 767 } 768 769 return sigframe_alloc_end(user); 770 } 771 772 static int setup_sigframe(struct rt_sigframe_user_layout *user, 773 struct pt_regs *regs, sigset_t *set) 774 { 775 int i, err = 0; 776 struct rt_sigframe __user *sf = user->sigframe; 777 778 /* set up the stack frame for unwinding */ 779 __put_user_error(regs->regs[29], &user->next_frame->fp, err); 780 __put_user_error(regs->regs[30], &user->next_frame->lr, err); 781 782 for (i = 0; i < 31; i++) 783 __put_user_error(regs->regs[i], &sf->uc.uc_mcontext.regs[i], 784 err); 785 __put_user_error(regs->sp, &sf->uc.uc_mcontext.sp, err); 786 __put_user_error(regs->pc, &sf->uc.uc_mcontext.pc, err); 787 __put_user_error(regs->pstate, &sf->uc.uc_mcontext.pstate, err); 788 789 __put_user_error(current->thread.fault_address, &sf->uc.uc_mcontext.fault_address, err); 790 791 err |= __copy_to_user(&sf->uc.uc_sigmask, set, sizeof(*set)); 792 793 if (err == 0 && system_supports_fpsimd()) { 794 struct fpsimd_context __user *fpsimd_ctx = 795 apply_user_offset(user, user->fpsimd_offset); 796 err |= preserve_fpsimd_context(fpsimd_ctx); 797 } 798 799 /* fault information, if valid */ 800 if (err == 0 && user->esr_offset) { 801 struct esr_context __user *esr_ctx = 802 apply_user_offset(user, user->esr_offset); 803 804 __put_user_error(ESR_MAGIC, &esr_ctx->head.magic, err); 805 __put_user_error(sizeof(*esr_ctx), &esr_ctx->head.size, err); 806 __put_user_error(current->thread.fault_code, &esr_ctx->esr, err); 807 } 808 809 /* Scalable Vector Extension state (including streaming), if present */ 810 if ((system_supports_sve() || system_supports_sme()) && 811 err == 0 && user->sve_offset) { 812 struct sve_context __user *sve_ctx = 813 apply_user_offset(user, user->sve_offset); 814 err |= preserve_sve_context(sve_ctx); 815 } 816 817 /* ZA state if present */ 818 if (system_supports_sme() && err == 0 && user->za_offset) { 819 struct za_context __user *za_ctx = 820 apply_user_offset(user, user->za_offset); 821 err |= preserve_za_context(za_ctx); 822 } 823 824 if (err == 0 && user->extra_offset) { 825 char __user *sfp = (char __user *)user->sigframe; 826 char __user *userp = 827 apply_user_offset(user, user->extra_offset); 828 829 struct extra_context __user *extra; 830 struct _aarch64_ctx __user *end; 831 u64 extra_datap; 832 u32 extra_size; 833 834 extra = (struct extra_context __user *)userp; 835 userp += EXTRA_CONTEXT_SIZE; 836 837 end = (struct _aarch64_ctx __user *)userp; 838 userp += TERMINATOR_SIZE; 839 840 /* 841 * extra_datap is just written to the signal frame. 842 * The value gets cast back to a void __user * 843 * during sigreturn. 844 */ 845 extra_datap = (__force u64)userp; 846 extra_size = sfp + round_up(user->size, 16) - userp; 847 848 __put_user_error(EXTRA_MAGIC, &extra->head.magic, err); 849 __put_user_error(EXTRA_CONTEXT_SIZE, &extra->head.size, err); 850 __put_user_error(extra_datap, &extra->datap, err); 851 __put_user_error(extra_size, &extra->size, err); 852 853 /* Add the terminator */ 854 __put_user_error(0, &end->magic, err); 855 __put_user_error(0, &end->size, err); 856 } 857 858 /* set the "end" magic */ 859 if (err == 0) { 860 struct _aarch64_ctx __user *end = 861 apply_user_offset(user, user->end_offset); 862 863 __put_user_error(0, &end->magic, err); 864 __put_user_error(0, &end->size, err); 865 } 866 867 return err; 868 } 869 870 static int get_sigframe(struct rt_sigframe_user_layout *user, 871 struct ksignal *ksig, struct pt_regs *regs) 872 { 873 unsigned long sp, sp_top; 874 int err; 875 876 init_user_layout(user); 877 err = setup_sigframe_layout(user, false); 878 if (err) 879 return err; 880 881 sp = sp_top = sigsp(regs->sp, ksig); 882 883 sp = round_down(sp - sizeof(struct frame_record), 16); 884 user->next_frame = (struct frame_record __user *)sp; 885 886 sp = round_down(sp, 16) - sigframe_size(user); 887 user->sigframe = (struct rt_sigframe __user *)sp; 888 889 /* 890 * Check that we can actually write to the signal frame. 891 */ 892 if (!access_ok(user->sigframe, sp_top - sp)) 893 return -EFAULT; 894 895 return 0; 896 } 897 898 static void setup_return(struct pt_regs *regs, struct k_sigaction *ka, 899 struct rt_sigframe_user_layout *user, int usig) 900 { 901 __sigrestore_t sigtramp; 902 903 regs->regs[0] = usig; 904 regs->sp = (unsigned long)user->sigframe; 905 regs->regs[29] = (unsigned long)&user->next_frame->fp; 906 regs->pc = (unsigned long)ka->sa.sa_handler; 907 908 /* 909 * Signal delivery is a (wacky) indirect function call in 910 * userspace, so simulate the same setting of BTYPE as a BLR 911 * <register containing the signal handler entry point>. 912 * Signal delivery to a location in a PROT_BTI guarded page 913 * that is not a function entry point will now trigger a 914 * SIGILL in userspace. 915 * 916 * If the signal handler entry point is not in a PROT_BTI 917 * guarded page, this is harmless. 918 */ 919 if (system_supports_bti()) { 920 regs->pstate &= ~PSR_BTYPE_MASK; 921 regs->pstate |= PSR_BTYPE_C; 922 } 923 924 /* TCO (Tag Check Override) always cleared for signal handlers */ 925 regs->pstate &= ~PSR_TCO_BIT; 926 927 /* Signal handlers are invoked with ZA and streaming mode disabled */ 928 if (system_supports_sme()) { 929 current->thread.svcr &= ~(SVCR_ZA_MASK | 930 SVCR_SM_MASK); 931 sme_smstop(); 932 } 933 934 if (ka->sa.sa_flags & SA_RESTORER) 935 sigtramp = ka->sa.sa_restorer; 936 else 937 sigtramp = VDSO_SYMBOL(current->mm->context.vdso, sigtramp); 938 939 regs->regs[30] = (unsigned long)sigtramp; 940 } 941 942 static int setup_rt_frame(int usig, struct ksignal *ksig, sigset_t *set, 943 struct pt_regs *regs) 944 { 945 struct rt_sigframe_user_layout user; 946 struct rt_sigframe __user *frame; 947 int err = 0; 948 949 fpsimd_signal_preserve_current_state(); 950 951 if (get_sigframe(&user, ksig, regs)) 952 return 1; 953 954 frame = user.sigframe; 955 956 __put_user_error(0, &frame->uc.uc_flags, err); 957 __put_user_error(NULL, &frame->uc.uc_link, err); 958 959 err |= __save_altstack(&frame->uc.uc_stack, regs->sp); 960 err |= setup_sigframe(&user, regs, set); 961 if (err == 0) { 962 setup_return(regs, &ksig->ka, &user, usig); 963 if (ksig->ka.sa.sa_flags & SA_SIGINFO) { 964 err |= copy_siginfo_to_user(&frame->info, &ksig->info); 965 regs->regs[1] = (unsigned long)&frame->info; 966 regs->regs[2] = (unsigned long)&frame->uc; 967 } 968 } 969 970 return err; 971 } 972 973 static void setup_restart_syscall(struct pt_regs *regs) 974 { 975 if (is_compat_task()) 976 compat_setup_restart_syscall(regs); 977 else 978 regs->regs[8] = __NR_restart_syscall; 979 } 980 981 /* 982 * OK, we're invoking a handler 983 */ 984 static void handle_signal(struct ksignal *ksig, struct pt_regs *regs) 985 { 986 sigset_t *oldset = sigmask_to_save(); 987 int usig = ksig->sig; 988 int ret; 989 990 rseq_signal_deliver(ksig, regs); 991 992 /* 993 * Set up the stack frame 994 */ 995 if (is_compat_task()) { 996 if (ksig->ka.sa.sa_flags & SA_SIGINFO) 997 ret = compat_setup_rt_frame(usig, ksig, oldset, regs); 998 else 999 ret = compat_setup_frame(usig, ksig, oldset, regs); 1000 } else { 1001 ret = setup_rt_frame(usig, ksig, oldset, regs); 1002 } 1003 1004 /* 1005 * Check that the resulting registers are actually sane. 1006 */ 1007 ret |= !valid_user_regs(®s->user_regs, current); 1008 1009 /* Step into the signal handler if we are stepping */ 1010 signal_setup_done(ret, ksig, test_thread_flag(TIF_SINGLESTEP)); 1011 } 1012 1013 /* 1014 * Note that 'init' is a special process: it doesn't get signals it doesn't 1015 * want to handle. Thus you cannot kill init even with a SIGKILL even by 1016 * mistake. 1017 * 1018 * Note that we go through the signals twice: once to check the signals that 1019 * the kernel can handle, and then we build all the user-level signal handling 1020 * stack-frames in one go after that. 1021 */ 1022 static void do_signal(struct pt_regs *regs) 1023 { 1024 unsigned long continue_addr = 0, restart_addr = 0; 1025 int retval = 0; 1026 struct ksignal ksig; 1027 bool syscall = in_syscall(regs); 1028 1029 /* 1030 * If we were from a system call, check for system call restarting... 1031 */ 1032 if (syscall) { 1033 continue_addr = regs->pc; 1034 restart_addr = continue_addr - (compat_thumb_mode(regs) ? 2 : 4); 1035 retval = regs->regs[0]; 1036 1037 /* 1038 * Avoid additional syscall restarting via ret_to_user. 1039 */ 1040 forget_syscall(regs); 1041 1042 /* 1043 * Prepare for system call restart. We do this here so that a 1044 * debugger will see the already changed PC. 1045 */ 1046 switch (retval) { 1047 case -ERESTARTNOHAND: 1048 case -ERESTARTSYS: 1049 case -ERESTARTNOINTR: 1050 case -ERESTART_RESTARTBLOCK: 1051 regs->regs[0] = regs->orig_x0; 1052 regs->pc = restart_addr; 1053 break; 1054 } 1055 } 1056 1057 /* 1058 * Get the signal to deliver. When running under ptrace, at this point 1059 * the debugger may change all of our registers. 1060 */ 1061 if (get_signal(&ksig)) { 1062 /* 1063 * Depending on the signal settings, we may need to revert the 1064 * decision to restart the system call, but skip this if a 1065 * debugger has chosen to restart at a different PC. 1066 */ 1067 if (regs->pc == restart_addr && 1068 (retval == -ERESTARTNOHAND || 1069 retval == -ERESTART_RESTARTBLOCK || 1070 (retval == -ERESTARTSYS && 1071 !(ksig.ka.sa.sa_flags & SA_RESTART)))) { 1072 syscall_set_return_value(current, regs, -EINTR, 0); 1073 regs->pc = continue_addr; 1074 } 1075 1076 handle_signal(&ksig, regs); 1077 return; 1078 } 1079 1080 /* 1081 * Handle restarting a different system call. As above, if a debugger 1082 * has chosen to restart at a different PC, ignore the restart. 1083 */ 1084 if (syscall && regs->pc == restart_addr) { 1085 if (retval == -ERESTART_RESTARTBLOCK) 1086 setup_restart_syscall(regs); 1087 user_rewind_single_step(current); 1088 } 1089 1090 restore_saved_sigmask(); 1091 } 1092 1093 void do_notify_resume(struct pt_regs *regs, unsigned long thread_flags) 1094 { 1095 do { 1096 if (thread_flags & _TIF_NEED_RESCHED) { 1097 /* Unmask Debug and SError for the next task */ 1098 local_daif_restore(DAIF_PROCCTX_NOIRQ); 1099 1100 schedule(); 1101 } else { 1102 local_daif_restore(DAIF_PROCCTX); 1103 1104 if (thread_flags & _TIF_UPROBE) 1105 uprobe_notify_resume(regs); 1106 1107 if (thread_flags & _TIF_MTE_ASYNC_FAULT) { 1108 clear_thread_flag(TIF_MTE_ASYNC_FAULT); 1109 send_sig_fault(SIGSEGV, SEGV_MTEAERR, 1110 (void __user *)NULL, current); 1111 } 1112 1113 if (thread_flags & (_TIF_SIGPENDING | _TIF_NOTIFY_SIGNAL)) 1114 do_signal(regs); 1115 1116 if (thread_flags & _TIF_NOTIFY_RESUME) 1117 resume_user_mode_work(regs); 1118 1119 if (thread_flags & _TIF_FOREIGN_FPSTATE) 1120 fpsimd_restore_current_state(); 1121 } 1122 1123 local_daif_mask(); 1124 thread_flags = read_thread_flags(); 1125 } while (thread_flags & _TIF_WORK_MASK); 1126 } 1127 1128 unsigned long __ro_after_init signal_minsigstksz; 1129 1130 /* 1131 * Determine the stack space required for guaranteed signal devliery. 1132 * This function is used to populate AT_MINSIGSTKSZ at process startup. 1133 * cpufeatures setup is assumed to be complete. 1134 */ 1135 void __init minsigstksz_setup(void) 1136 { 1137 struct rt_sigframe_user_layout user; 1138 1139 init_user_layout(&user); 1140 1141 /* 1142 * If this fails, SIGFRAME_MAXSZ needs to be enlarged. It won't 1143 * be big enough, but it's our best guess: 1144 */ 1145 if (WARN_ON(setup_sigframe_layout(&user, true))) 1146 return; 1147 1148 signal_minsigstksz = sigframe_size(&user) + 1149 round_up(sizeof(struct frame_record), 16) + 1150 16; /* max alignment padding */ 1151 } 1152 1153 /* 1154 * Compile-time assertions for siginfo_t offsets. Check NSIG* as well, as 1155 * changes likely come with new fields that should be added below. 1156 */ 1157 static_assert(NSIGILL == 11); 1158 static_assert(NSIGFPE == 15); 1159 static_assert(NSIGSEGV == 9); 1160 static_assert(NSIGBUS == 5); 1161 static_assert(NSIGTRAP == 6); 1162 static_assert(NSIGCHLD == 6); 1163 static_assert(NSIGSYS == 2); 1164 static_assert(sizeof(siginfo_t) == 128); 1165 static_assert(__alignof__(siginfo_t) == 8); 1166 static_assert(offsetof(siginfo_t, si_signo) == 0x00); 1167 static_assert(offsetof(siginfo_t, si_errno) == 0x04); 1168 static_assert(offsetof(siginfo_t, si_code) == 0x08); 1169 static_assert(offsetof(siginfo_t, si_pid) == 0x10); 1170 static_assert(offsetof(siginfo_t, si_uid) == 0x14); 1171 static_assert(offsetof(siginfo_t, si_tid) == 0x10); 1172 static_assert(offsetof(siginfo_t, si_overrun) == 0x14); 1173 static_assert(offsetof(siginfo_t, si_status) == 0x18); 1174 static_assert(offsetof(siginfo_t, si_utime) == 0x20); 1175 static_assert(offsetof(siginfo_t, si_stime) == 0x28); 1176 static_assert(offsetof(siginfo_t, si_value) == 0x18); 1177 static_assert(offsetof(siginfo_t, si_int) == 0x18); 1178 static_assert(offsetof(siginfo_t, si_ptr) == 0x18); 1179 static_assert(offsetof(siginfo_t, si_addr) == 0x10); 1180 static_assert(offsetof(siginfo_t, si_addr_lsb) == 0x18); 1181 static_assert(offsetof(siginfo_t, si_lower) == 0x20); 1182 static_assert(offsetof(siginfo_t, si_upper) == 0x28); 1183 static_assert(offsetof(siginfo_t, si_pkey) == 0x20); 1184 static_assert(offsetof(siginfo_t, si_perf_data) == 0x18); 1185 static_assert(offsetof(siginfo_t, si_perf_type) == 0x20); 1186 static_assert(offsetof(siginfo_t, si_perf_flags) == 0x24); 1187 static_assert(offsetof(siginfo_t, si_band) == 0x10); 1188 static_assert(offsetof(siginfo_t, si_fd) == 0x18); 1189 static_assert(offsetof(siginfo_t, si_call_addr) == 0x10); 1190 static_assert(offsetof(siginfo_t, si_syscall) == 0x18); 1191 static_assert(offsetof(siginfo_t, si_arch) == 0x1c); 1192