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 barrier(); 300 /* From now, fpsimd_thread_switch() won't clear TIF_FOREIGN_FPSTATE */ 301 302 set_thread_flag(TIF_FOREIGN_FPSTATE); 303 barrier(); 304 /* From now, fpsimd_thread_switch() won't touch thread.sve_state */ 305 306 sve_alloc(current); 307 err = __copy_from_user(current->thread.sve_state, 308 (char __user const *)user->sve + 309 SVE_SIG_REGS_OFFSET, 310 SVE_SIG_REGS_SIZE(vq)); 311 if (err) 312 return -EFAULT; 313 314 set_thread_flag(TIF_SVE); 315 316 fpsimd_only: 317 /* copy the FP and status/control registers */ 318 /* restore_sigframe() already checked that user->fpsimd != NULL. */ 319 err = __copy_from_user(fpsimd.vregs, user->fpsimd->vregs, 320 sizeof(fpsimd.vregs)); 321 __get_user_error(fpsimd.fpsr, &user->fpsimd->fpsr, err); 322 __get_user_error(fpsimd.fpcr, &user->fpsimd->fpcr, err); 323 324 /* load the hardware registers from the fpsimd_state structure */ 325 if (!err) 326 fpsimd_update_current_state(&fpsimd); 327 328 return err ? -EFAULT : 0; 329 } 330 331 #else /* ! CONFIG_ARM64_SVE */ 332 333 /* Turn any non-optimised out attempts to use these into a link error: */ 334 extern int preserve_sve_context(void __user *ctx); 335 extern int restore_sve_fpsimd_context(struct user_ctxs *user); 336 337 #endif /* ! CONFIG_ARM64_SVE */ 338 339 340 static int parse_user_sigframe(struct user_ctxs *user, 341 struct rt_sigframe __user *sf) 342 { 343 struct sigcontext __user *const sc = &sf->uc.uc_mcontext; 344 struct _aarch64_ctx __user *head; 345 char __user *base = (char __user *)&sc->__reserved; 346 size_t offset = 0; 347 size_t limit = sizeof(sc->__reserved); 348 bool have_extra_context = false; 349 char const __user *const sfp = (char const __user *)sf; 350 351 user->fpsimd = NULL; 352 user->sve = NULL; 353 354 if (!IS_ALIGNED((unsigned long)base, 16)) 355 goto invalid; 356 357 while (1) { 358 int err = 0; 359 u32 magic, size; 360 char const __user *userp; 361 struct extra_context const __user *extra; 362 u64 extra_datap; 363 u32 extra_size; 364 struct _aarch64_ctx const __user *end; 365 u32 end_magic, end_size; 366 367 if (limit - offset < sizeof(*head)) 368 goto invalid; 369 370 if (!IS_ALIGNED(offset, 16)) 371 goto invalid; 372 373 head = (struct _aarch64_ctx __user *)(base + offset); 374 __get_user_error(magic, &head->magic, err); 375 __get_user_error(size, &head->size, err); 376 if (err) 377 return err; 378 379 if (limit - offset < size) 380 goto invalid; 381 382 switch (magic) { 383 case 0: 384 if (size) 385 goto invalid; 386 387 goto done; 388 389 case FPSIMD_MAGIC: 390 if (user->fpsimd) 391 goto invalid; 392 393 if (size < sizeof(*user->fpsimd)) 394 goto invalid; 395 396 user->fpsimd = (struct fpsimd_context __user *)head; 397 break; 398 399 case ESR_MAGIC: 400 /* ignore */ 401 break; 402 403 case SVE_MAGIC: 404 if (!system_supports_sve()) 405 goto invalid; 406 407 if (user->sve) 408 goto invalid; 409 410 if (size < sizeof(*user->sve)) 411 goto invalid; 412 413 user->sve = (struct sve_context __user *)head; 414 break; 415 416 case EXTRA_MAGIC: 417 if (have_extra_context) 418 goto invalid; 419 420 if (size < sizeof(*extra)) 421 goto invalid; 422 423 userp = (char const __user *)head; 424 425 extra = (struct extra_context const __user *)userp; 426 userp += size; 427 428 __get_user_error(extra_datap, &extra->datap, err); 429 __get_user_error(extra_size, &extra->size, err); 430 if (err) 431 return err; 432 433 /* Check for the dummy terminator in __reserved[]: */ 434 435 if (limit - offset - size < TERMINATOR_SIZE) 436 goto invalid; 437 438 end = (struct _aarch64_ctx const __user *)userp; 439 userp += TERMINATOR_SIZE; 440 441 __get_user_error(end_magic, &end->magic, err); 442 __get_user_error(end_size, &end->size, err); 443 if (err) 444 return err; 445 446 if (end_magic || end_size) 447 goto invalid; 448 449 /* Prevent looping/repeated parsing of extra_context */ 450 have_extra_context = true; 451 452 base = (__force void __user *)extra_datap; 453 if (!IS_ALIGNED((unsigned long)base, 16)) 454 goto invalid; 455 456 if (!IS_ALIGNED(extra_size, 16)) 457 goto invalid; 458 459 if (base != userp) 460 goto invalid; 461 462 /* Reject "unreasonably large" frames: */ 463 if (extra_size > sfp + SIGFRAME_MAXSZ - userp) 464 goto invalid; 465 466 /* 467 * Ignore trailing terminator in __reserved[] 468 * and start parsing extra data: 469 */ 470 offset = 0; 471 limit = extra_size; 472 473 if (!access_ok(VERIFY_READ, base, limit)) 474 goto invalid; 475 476 continue; 477 478 default: 479 goto invalid; 480 } 481 482 if (size < sizeof(*head)) 483 goto invalid; 484 485 if (limit - offset < size) 486 goto invalid; 487 488 offset += size; 489 } 490 491 done: 492 return 0; 493 494 invalid: 495 return -EINVAL; 496 } 497 498 static int restore_sigframe(struct pt_regs *regs, 499 struct rt_sigframe __user *sf) 500 { 501 sigset_t set; 502 int i, err; 503 struct user_ctxs user; 504 505 err = __copy_from_user(&set, &sf->uc.uc_sigmask, sizeof(set)); 506 if (err == 0) 507 set_current_blocked(&set); 508 509 for (i = 0; i < 31; i++) 510 __get_user_error(regs->regs[i], &sf->uc.uc_mcontext.regs[i], 511 err); 512 __get_user_error(regs->sp, &sf->uc.uc_mcontext.sp, err); 513 __get_user_error(regs->pc, &sf->uc.uc_mcontext.pc, err); 514 __get_user_error(regs->pstate, &sf->uc.uc_mcontext.pstate, err); 515 516 /* 517 * Avoid sys_rt_sigreturn() restarting. 518 */ 519 forget_syscall(regs); 520 521 err |= !valid_user_regs(®s->user_regs, current); 522 if (err == 0) 523 err = parse_user_sigframe(&user, sf); 524 525 if (err == 0) { 526 if (!user.fpsimd) 527 return -EINVAL; 528 529 if (user.sve) { 530 if (!system_supports_sve()) 531 return -EINVAL; 532 533 err = restore_sve_fpsimd_context(&user); 534 } else { 535 err = restore_fpsimd_context(user.fpsimd); 536 } 537 } 538 539 return err; 540 } 541 542 asmlinkage long sys_rt_sigreturn(struct pt_regs *regs) 543 { 544 struct rt_sigframe __user *frame; 545 546 /* Always make any pending restarted system calls return -EINTR */ 547 current->restart_block.fn = do_no_restart_syscall; 548 549 /* 550 * Since we stacked the signal on a 128-bit boundary, then 'sp' should 551 * be word aligned here. 552 */ 553 if (regs->sp & 15) 554 goto badframe; 555 556 frame = (struct rt_sigframe __user *)regs->sp; 557 558 if (!access_ok(VERIFY_READ, frame, sizeof (*frame))) 559 goto badframe; 560 561 if (restore_sigframe(regs, frame)) 562 goto badframe; 563 564 if (restore_altstack(&frame->uc.uc_stack)) 565 goto badframe; 566 567 return regs->regs[0]; 568 569 badframe: 570 arm64_notify_segfault(regs->sp); 571 return 0; 572 } 573 574 /* 575 * Determine the layout of optional records in the signal frame 576 * 577 * add_all: if true, lays out the biggest possible signal frame for 578 * this task; otherwise, generates a layout for the current state 579 * of the task. 580 */ 581 static int setup_sigframe_layout(struct rt_sigframe_user_layout *user, 582 bool add_all) 583 { 584 int err; 585 586 err = sigframe_alloc(user, &user->fpsimd_offset, 587 sizeof(struct fpsimd_context)); 588 if (err) 589 return err; 590 591 /* fault information, if valid */ 592 if (add_all || current->thread.fault_code) { 593 err = sigframe_alloc(user, &user->esr_offset, 594 sizeof(struct esr_context)); 595 if (err) 596 return err; 597 } 598 599 if (system_supports_sve()) { 600 unsigned int vq = 0; 601 602 if (add_all || test_thread_flag(TIF_SVE)) { 603 int vl = sve_max_vl; 604 605 if (!add_all) 606 vl = current->thread.sve_vl; 607 608 vq = sve_vq_from_vl(vl); 609 } 610 611 err = sigframe_alloc(user, &user->sve_offset, 612 SVE_SIG_CONTEXT_SIZE(vq)); 613 if (err) 614 return err; 615 } 616 617 return sigframe_alloc_end(user); 618 } 619 620 static int setup_sigframe(struct rt_sigframe_user_layout *user, 621 struct pt_regs *regs, sigset_t *set) 622 { 623 int i, err = 0; 624 struct rt_sigframe __user *sf = user->sigframe; 625 626 /* set up the stack frame for unwinding */ 627 __put_user_error(regs->regs[29], &user->next_frame->fp, err); 628 __put_user_error(regs->regs[30], &user->next_frame->lr, err); 629 630 for (i = 0; i < 31; i++) 631 __put_user_error(regs->regs[i], &sf->uc.uc_mcontext.regs[i], 632 err); 633 __put_user_error(regs->sp, &sf->uc.uc_mcontext.sp, err); 634 __put_user_error(regs->pc, &sf->uc.uc_mcontext.pc, err); 635 __put_user_error(regs->pstate, &sf->uc.uc_mcontext.pstate, err); 636 637 __put_user_error(current->thread.fault_address, &sf->uc.uc_mcontext.fault_address, err); 638 639 err |= __copy_to_user(&sf->uc.uc_sigmask, set, sizeof(*set)); 640 641 if (err == 0) { 642 struct fpsimd_context __user *fpsimd_ctx = 643 apply_user_offset(user, user->fpsimd_offset); 644 err |= preserve_fpsimd_context(fpsimd_ctx); 645 } 646 647 /* fault information, if valid */ 648 if (err == 0 && user->esr_offset) { 649 struct esr_context __user *esr_ctx = 650 apply_user_offset(user, user->esr_offset); 651 652 __put_user_error(ESR_MAGIC, &esr_ctx->head.magic, err); 653 __put_user_error(sizeof(*esr_ctx), &esr_ctx->head.size, err); 654 __put_user_error(current->thread.fault_code, &esr_ctx->esr, err); 655 } 656 657 /* Scalable Vector Extension state, if present */ 658 if (system_supports_sve() && err == 0 && user->sve_offset) { 659 struct sve_context __user *sve_ctx = 660 apply_user_offset(user, user->sve_offset); 661 err |= preserve_sve_context(sve_ctx); 662 } 663 664 if (err == 0 && user->extra_offset) { 665 char __user *sfp = (char __user *)user->sigframe; 666 char __user *userp = 667 apply_user_offset(user, user->extra_offset); 668 669 struct extra_context __user *extra; 670 struct _aarch64_ctx __user *end; 671 u64 extra_datap; 672 u32 extra_size; 673 674 extra = (struct extra_context __user *)userp; 675 userp += EXTRA_CONTEXT_SIZE; 676 677 end = (struct _aarch64_ctx __user *)userp; 678 userp += TERMINATOR_SIZE; 679 680 /* 681 * extra_datap is just written to the signal frame. 682 * The value gets cast back to a void __user * 683 * during sigreturn. 684 */ 685 extra_datap = (__force u64)userp; 686 extra_size = sfp + round_up(user->size, 16) - userp; 687 688 __put_user_error(EXTRA_MAGIC, &extra->head.magic, err); 689 __put_user_error(EXTRA_CONTEXT_SIZE, &extra->head.size, err); 690 __put_user_error(extra_datap, &extra->datap, err); 691 __put_user_error(extra_size, &extra->size, err); 692 693 /* Add the terminator */ 694 __put_user_error(0, &end->magic, err); 695 __put_user_error(0, &end->size, err); 696 } 697 698 /* set the "end" magic */ 699 if (err == 0) { 700 struct _aarch64_ctx __user *end = 701 apply_user_offset(user, user->end_offset); 702 703 __put_user_error(0, &end->magic, err); 704 __put_user_error(0, &end->size, err); 705 } 706 707 return err; 708 } 709 710 static int get_sigframe(struct rt_sigframe_user_layout *user, 711 struct ksignal *ksig, struct pt_regs *regs) 712 { 713 unsigned long sp, sp_top; 714 int err; 715 716 init_user_layout(user); 717 err = setup_sigframe_layout(user, false); 718 if (err) 719 return err; 720 721 sp = sp_top = sigsp(regs->sp, ksig); 722 723 sp = round_down(sp - sizeof(struct frame_record), 16); 724 user->next_frame = (struct frame_record __user *)sp; 725 726 sp = round_down(sp, 16) - sigframe_size(user); 727 user->sigframe = (struct rt_sigframe __user *)sp; 728 729 /* 730 * Check that we can actually write to the signal frame. 731 */ 732 if (!access_ok(VERIFY_WRITE, user->sigframe, sp_top - sp)) 733 return -EFAULT; 734 735 return 0; 736 } 737 738 static void setup_return(struct pt_regs *regs, struct k_sigaction *ka, 739 struct rt_sigframe_user_layout *user, int usig) 740 { 741 __sigrestore_t sigtramp; 742 743 regs->regs[0] = usig; 744 regs->sp = (unsigned long)user->sigframe; 745 regs->regs[29] = (unsigned long)&user->next_frame->fp; 746 regs->pc = (unsigned long)ka->sa.sa_handler; 747 748 if (ka->sa.sa_flags & SA_RESTORER) 749 sigtramp = ka->sa.sa_restorer; 750 else 751 sigtramp = VDSO_SYMBOL(current->mm->context.vdso, sigtramp); 752 753 regs->regs[30] = (unsigned long)sigtramp; 754 } 755 756 static int setup_rt_frame(int usig, struct ksignal *ksig, sigset_t *set, 757 struct pt_regs *regs) 758 { 759 struct rt_sigframe_user_layout user; 760 struct rt_sigframe __user *frame; 761 int err = 0; 762 763 fpsimd_signal_preserve_current_state(); 764 765 if (get_sigframe(&user, ksig, regs)) 766 return 1; 767 768 frame = user.sigframe; 769 770 __put_user_error(0, &frame->uc.uc_flags, err); 771 __put_user_error(NULL, &frame->uc.uc_link, err); 772 773 err |= __save_altstack(&frame->uc.uc_stack, regs->sp); 774 err |= setup_sigframe(&user, regs, set); 775 if (err == 0) { 776 setup_return(regs, &ksig->ka, &user, usig); 777 if (ksig->ka.sa.sa_flags & SA_SIGINFO) { 778 err |= copy_siginfo_to_user(&frame->info, &ksig->info); 779 regs->regs[1] = (unsigned long)&frame->info; 780 regs->regs[2] = (unsigned long)&frame->uc; 781 } 782 } 783 784 return err; 785 } 786 787 static void setup_restart_syscall(struct pt_regs *regs) 788 { 789 if (is_compat_task()) 790 compat_setup_restart_syscall(regs); 791 else 792 regs->regs[8] = __NR_restart_syscall; 793 } 794 795 /* 796 * OK, we're invoking a handler 797 */ 798 static void handle_signal(struct ksignal *ksig, struct pt_regs *regs) 799 { 800 struct task_struct *tsk = current; 801 sigset_t *oldset = sigmask_to_save(); 802 int usig = ksig->sig; 803 int ret; 804 805 /* 806 * Set up the stack frame 807 */ 808 if (is_compat_task()) { 809 if (ksig->ka.sa.sa_flags & SA_SIGINFO) 810 ret = compat_setup_rt_frame(usig, ksig, oldset, regs); 811 else 812 ret = compat_setup_frame(usig, ksig, oldset, regs); 813 } else { 814 ret = setup_rt_frame(usig, ksig, oldset, regs); 815 } 816 817 /* 818 * Check that the resulting registers are actually sane. 819 */ 820 ret |= !valid_user_regs(®s->user_regs, current); 821 822 /* 823 * Fast forward the stepping logic so we step into the signal 824 * handler. 825 */ 826 if (!ret) 827 user_fastforward_single_step(tsk); 828 829 signal_setup_done(ret, ksig, 0); 830 } 831 832 /* 833 * Note that 'init' is a special process: it doesn't get signals it doesn't 834 * want to handle. Thus you cannot kill init even with a SIGKILL even by 835 * mistake. 836 * 837 * Note that we go through the signals twice: once to check the signals that 838 * the kernel can handle, and then we build all the user-level signal handling 839 * stack-frames in one go after that. 840 */ 841 static void do_signal(struct pt_regs *regs) 842 { 843 unsigned long continue_addr = 0, restart_addr = 0; 844 int retval = 0; 845 struct ksignal ksig; 846 bool syscall = in_syscall(regs); 847 848 /* 849 * If we were from a system call, check for system call restarting... 850 */ 851 if (syscall) { 852 continue_addr = regs->pc; 853 restart_addr = continue_addr - (compat_thumb_mode(regs) ? 2 : 4); 854 retval = regs->regs[0]; 855 856 /* 857 * Avoid additional syscall restarting via ret_to_user. 858 */ 859 forget_syscall(regs); 860 861 /* 862 * Prepare for system call restart. We do this here so that a 863 * debugger will see the already changed PC. 864 */ 865 switch (retval) { 866 case -ERESTARTNOHAND: 867 case -ERESTARTSYS: 868 case -ERESTARTNOINTR: 869 case -ERESTART_RESTARTBLOCK: 870 regs->regs[0] = regs->orig_x0; 871 regs->pc = restart_addr; 872 break; 873 } 874 } 875 876 /* 877 * Get the signal to deliver. When running under ptrace, at this point 878 * the debugger may change all of our registers. 879 */ 880 if (get_signal(&ksig)) { 881 /* 882 * Depending on the signal settings, we may need to revert the 883 * decision to restart the system call, but skip this if a 884 * debugger has chosen to restart at a different PC. 885 */ 886 if (regs->pc == restart_addr && 887 (retval == -ERESTARTNOHAND || 888 retval == -ERESTART_RESTARTBLOCK || 889 (retval == -ERESTARTSYS && 890 !(ksig.ka.sa.sa_flags & SA_RESTART)))) { 891 regs->regs[0] = -EINTR; 892 regs->pc = continue_addr; 893 } 894 895 handle_signal(&ksig, regs); 896 return; 897 } 898 899 /* 900 * Handle restarting a different system call. As above, if a debugger 901 * has chosen to restart at a different PC, ignore the restart. 902 */ 903 if (syscall && regs->pc == restart_addr) { 904 if (retval == -ERESTART_RESTARTBLOCK) 905 setup_restart_syscall(regs); 906 user_rewind_single_step(current); 907 } 908 909 restore_saved_sigmask(); 910 } 911 912 asmlinkage void do_notify_resume(struct pt_regs *regs, 913 unsigned int thread_flags) 914 { 915 /* 916 * The assembly code enters us with IRQs off, but it hasn't 917 * informed the tracing code of that for efficiency reasons. 918 * Update the trace code with the current status. 919 */ 920 trace_hardirqs_off(); 921 922 do { 923 /* Check valid user FS if needed */ 924 addr_limit_user_check(); 925 926 if (thread_flags & _TIF_NEED_RESCHED) { 927 /* Unmask Debug and SError for the next task */ 928 local_daif_restore(DAIF_PROCCTX_NOIRQ); 929 930 schedule(); 931 } else { 932 local_daif_restore(DAIF_PROCCTX); 933 934 if (thread_flags & _TIF_UPROBE) 935 uprobe_notify_resume(regs); 936 937 if (thread_flags & _TIF_SIGPENDING) 938 do_signal(regs); 939 940 if (thread_flags & _TIF_NOTIFY_RESUME) { 941 clear_thread_flag(TIF_NOTIFY_RESUME); 942 tracehook_notify_resume(regs); 943 } 944 945 if (thread_flags & _TIF_FOREIGN_FPSTATE) 946 fpsimd_restore_current_state(); 947 } 948 949 local_daif_mask(); 950 thread_flags = READ_ONCE(current_thread_info()->flags); 951 } while (thread_flags & _TIF_WORK_MASK); 952 } 953 954 unsigned long __ro_after_init signal_minsigstksz; 955 956 /* 957 * Determine the stack space required for guaranteed signal devliery. 958 * This function is used to populate AT_MINSIGSTKSZ at process startup. 959 * cpufeatures setup is assumed to be complete. 960 */ 961 void __init minsigstksz_setup(void) 962 { 963 struct rt_sigframe_user_layout user; 964 965 init_user_layout(&user); 966 967 /* 968 * If this fails, SIGFRAME_MAXSZ needs to be enlarged. It won't 969 * be big enough, but it's our best guess: 970 */ 971 if (WARN_ON(setup_sigframe_layout(&user, true))) 972 return; 973 974 signal_minsigstksz = sigframe_size(&user) + 975 round_up(sizeof(struct frame_record), 16) + 976 16; /* max alignment padding */ 977 } 978