root/arch/arm64/kernel/signal.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. sigframe_size
  2. __sigframe_alloc
  3. sigframe_alloc
  4. sigframe_alloc_end
  5. apply_user_offset
  6. preserve_fpsimd_context
  7. restore_fpsimd_context
  8. preserve_sve_context
  9. restore_sve_fpsimd_context
  10. parse_user_sigframe
  11. restore_sigframe
  12. SYSCALL_DEFINE0
  13. setup_sigframe_layout
  14. setup_sigframe
  15. get_sigframe
  16. setup_return
  17. setup_rt_frame
  18. setup_restart_syscall
  19. handle_signal
  20. do_signal
  21. do_notify_resume
  22. minsigstksz_setup

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

/* [<][>][^][v][top][bottom][index][help] */