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