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