1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Based on arch/arm/kernel/ptrace.c 4 * 5 * By Ross Biro 1/23/92 6 * edited by Linus Torvalds 7 * ARM modifications Copyright (C) 2000 Russell King 8 * Copyright (C) 2012 ARM Ltd. 9 */ 10 11 #include <linux/audit.h> 12 #include <linux/compat.h> 13 #include <linux/kernel.h> 14 #include <linux/sched/signal.h> 15 #include <linux/sched/task_stack.h> 16 #include <linux/mm.h> 17 #include <linux/nospec.h> 18 #include <linux/smp.h> 19 #include <linux/ptrace.h> 20 #include <linux/user.h> 21 #include <linux/seccomp.h> 22 #include <linux/security.h> 23 #include <linux/init.h> 24 #include <linux/signal.h> 25 #include <linux/string.h> 26 #include <linux/uaccess.h> 27 #include <linux/perf_event.h> 28 #include <linux/hw_breakpoint.h> 29 #include <linux/regset.h> 30 #include <linux/elf.h> 31 #include <linux/rseq.h> 32 33 #include <asm/compat.h> 34 #include <asm/cpufeature.h> 35 #include <asm/debug-monitors.h> 36 #include <asm/fpsimd.h> 37 #include <asm/gcs.h> 38 #include <asm/mte.h> 39 #include <asm/pointer_auth.h> 40 #include <asm/stacktrace.h> 41 #include <asm/syscall.h> 42 #include <asm/traps.h> 43 #include <asm/system_misc.h> 44 45 #define CREATE_TRACE_POINTS 46 #include <trace/events/syscalls.h> 47 48 struct pt_regs_offset { 49 const char *name; 50 int offset; 51 }; 52 53 #define REG_OFFSET_NAME(r) {.name = #r, .offset = offsetof(struct pt_regs, r)} 54 #define REG_OFFSET_END {.name = NULL, .offset = 0} 55 #define GPR_OFFSET_NAME(r) \ 56 {.name = "x" #r, .offset = offsetof(struct pt_regs, regs[r])} 57 58 static const struct pt_regs_offset regoffset_table[] = { 59 GPR_OFFSET_NAME(0), 60 GPR_OFFSET_NAME(1), 61 GPR_OFFSET_NAME(2), 62 GPR_OFFSET_NAME(3), 63 GPR_OFFSET_NAME(4), 64 GPR_OFFSET_NAME(5), 65 GPR_OFFSET_NAME(6), 66 GPR_OFFSET_NAME(7), 67 GPR_OFFSET_NAME(8), 68 GPR_OFFSET_NAME(9), 69 GPR_OFFSET_NAME(10), 70 GPR_OFFSET_NAME(11), 71 GPR_OFFSET_NAME(12), 72 GPR_OFFSET_NAME(13), 73 GPR_OFFSET_NAME(14), 74 GPR_OFFSET_NAME(15), 75 GPR_OFFSET_NAME(16), 76 GPR_OFFSET_NAME(17), 77 GPR_OFFSET_NAME(18), 78 GPR_OFFSET_NAME(19), 79 GPR_OFFSET_NAME(20), 80 GPR_OFFSET_NAME(21), 81 GPR_OFFSET_NAME(22), 82 GPR_OFFSET_NAME(23), 83 GPR_OFFSET_NAME(24), 84 GPR_OFFSET_NAME(25), 85 GPR_OFFSET_NAME(26), 86 GPR_OFFSET_NAME(27), 87 GPR_OFFSET_NAME(28), 88 GPR_OFFSET_NAME(29), 89 GPR_OFFSET_NAME(30), 90 {.name = "lr", .offset = offsetof(struct pt_regs, regs[30])}, 91 REG_OFFSET_NAME(sp), 92 REG_OFFSET_NAME(pc), 93 REG_OFFSET_NAME(pstate), 94 REG_OFFSET_END, 95 }; 96 97 /** 98 * regs_query_register_offset() - query register offset from its name 99 * @name: the name of a register 100 * 101 * regs_query_register_offset() returns the offset of a register in struct 102 * pt_regs from its name. If the name is invalid, this returns -EINVAL; 103 */ 104 int regs_query_register_offset(const char *name) 105 { 106 const struct pt_regs_offset *roff; 107 108 for (roff = regoffset_table; roff->name != NULL; roff++) 109 if (!strcmp(roff->name, name)) 110 return roff->offset; 111 return -EINVAL; 112 } 113 114 /** 115 * regs_within_kernel_stack() - check the address in the stack 116 * @regs: pt_regs which contains kernel stack pointer. 117 * @addr: address which is checked. 118 * 119 * regs_within_kernel_stack() checks @addr is within the kernel stack page(s). 120 * If @addr is within the kernel stack, it returns true. If not, returns false. 121 */ 122 static bool regs_within_kernel_stack(struct pt_regs *regs, unsigned long addr) 123 { 124 return ((addr & ~(THREAD_SIZE - 1)) == 125 (kernel_stack_pointer(regs) & ~(THREAD_SIZE - 1))) || 126 on_irq_stack(addr, sizeof(unsigned long)); 127 } 128 129 /** 130 * regs_get_kernel_stack_nth() - get Nth entry of the stack 131 * @regs: pt_regs which contains kernel stack pointer. 132 * @n: stack entry number. 133 * 134 * regs_get_kernel_stack_nth() returns @n th entry of the kernel stack which 135 * is specified by @regs. If the @n th entry is NOT in the kernel stack, 136 * this returns 0. 137 */ 138 unsigned long regs_get_kernel_stack_nth(struct pt_regs *regs, unsigned int n) 139 { 140 unsigned long *addr = (unsigned long *)kernel_stack_pointer(regs); 141 142 addr += n; 143 if (regs_within_kernel_stack(regs, (unsigned long)addr)) 144 return READ_ONCE_NOCHECK(*addr); 145 else 146 return 0; 147 } 148 149 /* 150 * TODO: does not yet catch signals sent when the child dies. 151 * in exit.c or in signal.c. 152 */ 153 154 /* 155 * Called by kernel/ptrace.c when detaching.. 156 */ 157 void ptrace_disable(struct task_struct *child) 158 { 159 /* 160 * This would be better off in core code, but PTRACE_DETACH has 161 * grown its fair share of arch-specific worts and changing it 162 * is likely to cause regressions on obscure architectures. 163 */ 164 user_disable_single_step(child); 165 } 166 167 #ifdef CONFIG_HAVE_HW_BREAKPOINT 168 /* 169 * Handle hitting a HW-breakpoint. 170 */ 171 static void ptrace_hbptriggered(struct perf_event *bp, 172 struct perf_sample_data *data, 173 struct pt_regs *regs) 174 { 175 struct arch_hw_breakpoint *bkpt = counter_arch_bp(bp); 176 const char *desc = "Hardware breakpoint trap (ptrace)"; 177 178 if (is_compat_task()) { 179 int si_errno = 0; 180 int i; 181 182 for (i = 0; i < ARM_MAX_BRP; ++i) { 183 if (current->thread.debug.hbp_break[i] == bp) { 184 si_errno = (i << 1) + 1; 185 break; 186 } 187 } 188 189 for (i = 0; i < ARM_MAX_WRP; ++i) { 190 if (current->thread.debug.hbp_watch[i] == bp) { 191 si_errno = -((i << 1) + 1); 192 break; 193 } 194 } 195 arm64_force_sig_ptrace_errno_trap(si_errno, bkpt->trigger, 196 desc); 197 return; 198 } 199 200 arm64_force_sig_fault(SIGTRAP, TRAP_HWBKPT, bkpt->trigger, desc); 201 } 202 203 /* 204 * Unregister breakpoints from this task and reset the pointers in 205 * the thread_struct. 206 */ 207 void flush_ptrace_hw_breakpoint(struct task_struct *tsk) 208 { 209 int i; 210 struct thread_struct *t = &tsk->thread; 211 212 for (i = 0; i < ARM_MAX_BRP; i++) { 213 if (t->debug.hbp_break[i]) { 214 unregister_hw_breakpoint(t->debug.hbp_break[i]); 215 t->debug.hbp_break[i] = NULL; 216 } 217 } 218 219 for (i = 0; i < ARM_MAX_WRP; i++) { 220 if (t->debug.hbp_watch[i]) { 221 unregister_hw_breakpoint(t->debug.hbp_watch[i]); 222 t->debug.hbp_watch[i] = NULL; 223 } 224 } 225 } 226 227 void ptrace_hw_copy_thread(struct task_struct *tsk) 228 { 229 memset(&tsk->thread.debug, 0, sizeof(struct debug_info)); 230 } 231 232 static struct perf_event *ptrace_hbp_get_event(unsigned int note_type, 233 struct task_struct *tsk, 234 unsigned long idx) 235 { 236 struct perf_event *bp = ERR_PTR(-EINVAL); 237 238 switch (note_type) { 239 case NT_ARM_HW_BREAK: 240 if (idx >= ARM_MAX_BRP) 241 goto out; 242 idx = array_index_nospec(idx, ARM_MAX_BRP); 243 bp = tsk->thread.debug.hbp_break[idx]; 244 break; 245 case NT_ARM_HW_WATCH: 246 if (idx >= ARM_MAX_WRP) 247 goto out; 248 idx = array_index_nospec(idx, ARM_MAX_WRP); 249 bp = tsk->thread.debug.hbp_watch[idx]; 250 break; 251 } 252 253 out: 254 return bp; 255 } 256 257 static int ptrace_hbp_set_event(unsigned int note_type, 258 struct task_struct *tsk, 259 unsigned long idx, 260 struct perf_event *bp) 261 { 262 int err = -EINVAL; 263 264 switch (note_type) { 265 case NT_ARM_HW_BREAK: 266 if (idx >= ARM_MAX_BRP) 267 goto out; 268 idx = array_index_nospec(idx, ARM_MAX_BRP); 269 tsk->thread.debug.hbp_break[idx] = bp; 270 err = 0; 271 break; 272 case NT_ARM_HW_WATCH: 273 if (idx >= ARM_MAX_WRP) 274 goto out; 275 idx = array_index_nospec(idx, ARM_MAX_WRP); 276 tsk->thread.debug.hbp_watch[idx] = bp; 277 err = 0; 278 break; 279 } 280 281 out: 282 return err; 283 } 284 285 static struct perf_event *ptrace_hbp_create(unsigned int note_type, 286 struct task_struct *tsk, 287 unsigned long idx) 288 { 289 struct perf_event *bp; 290 struct perf_event_attr attr; 291 int err, type; 292 293 switch (note_type) { 294 case NT_ARM_HW_BREAK: 295 type = HW_BREAKPOINT_X; 296 break; 297 case NT_ARM_HW_WATCH: 298 type = HW_BREAKPOINT_RW; 299 break; 300 default: 301 return ERR_PTR(-EINVAL); 302 } 303 304 ptrace_breakpoint_init(&attr); 305 306 /* 307 * Initialise fields to sane defaults 308 * (i.e. values that will pass validation). 309 */ 310 attr.bp_addr = 0; 311 attr.bp_len = HW_BREAKPOINT_LEN_4; 312 attr.bp_type = type; 313 attr.disabled = 1; 314 315 bp = register_user_hw_breakpoint(&attr, ptrace_hbptriggered, NULL, tsk); 316 if (IS_ERR(bp)) 317 return bp; 318 319 err = ptrace_hbp_set_event(note_type, tsk, idx, bp); 320 if (err) 321 return ERR_PTR(err); 322 323 return bp; 324 } 325 326 static int ptrace_hbp_fill_attr_ctrl(unsigned int note_type, 327 struct arch_hw_breakpoint_ctrl ctrl, 328 struct perf_event_attr *attr) 329 { 330 int err, len, type, offset, disabled = !ctrl.enabled; 331 332 attr->disabled = disabled; 333 if (disabled) 334 return 0; 335 336 err = arch_bp_generic_fields(ctrl, &len, &type, &offset); 337 if (err) 338 return err; 339 340 switch (note_type) { 341 case NT_ARM_HW_BREAK: 342 if ((type & HW_BREAKPOINT_X) != type) 343 return -EINVAL; 344 break; 345 case NT_ARM_HW_WATCH: 346 if ((type & HW_BREAKPOINT_RW) != type) 347 return -EINVAL; 348 break; 349 default: 350 return -EINVAL; 351 } 352 353 attr->bp_len = len; 354 attr->bp_type = type; 355 attr->bp_addr += offset; 356 357 return 0; 358 } 359 360 static int ptrace_hbp_get_resource_info(unsigned int note_type, u32 *info) 361 { 362 u8 num; 363 u32 reg = 0; 364 365 switch (note_type) { 366 case NT_ARM_HW_BREAK: 367 num = hw_breakpoint_slots(TYPE_INST); 368 break; 369 case NT_ARM_HW_WATCH: 370 num = hw_breakpoint_slots(TYPE_DATA); 371 break; 372 default: 373 return -EINVAL; 374 } 375 376 reg |= debug_monitors_arch(); 377 reg <<= 8; 378 reg |= num; 379 380 *info = reg; 381 return 0; 382 } 383 384 static int ptrace_hbp_get_ctrl(unsigned int note_type, 385 struct task_struct *tsk, 386 unsigned long idx, 387 u32 *ctrl) 388 { 389 struct perf_event *bp = ptrace_hbp_get_event(note_type, tsk, idx); 390 391 if (IS_ERR(bp)) 392 return PTR_ERR(bp); 393 394 *ctrl = bp ? encode_ctrl_reg(counter_arch_bp(bp)->ctrl) : 0; 395 return 0; 396 } 397 398 static int ptrace_hbp_get_addr(unsigned int note_type, 399 struct task_struct *tsk, 400 unsigned long idx, 401 u64 *addr) 402 { 403 struct perf_event *bp = ptrace_hbp_get_event(note_type, tsk, idx); 404 405 if (IS_ERR(bp)) 406 return PTR_ERR(bp); 407 408 *addr = bp ? counter_arch_bp(bp)->address : 0; 409 return 0; 410 } 411 412 static struct perf_event *ptrace_hbp_get_initialised_bp(unsigned int note_type, 413 struct task_struct *tsk, 414 unsigned long idx) 415 { 416 struct perf_event *bp = ptrace_hbp_get_event(note_type, tsk, idx); 417 418 if (!bp) 419 bp = ptrace_hbp_create(note_type, tsk, idx); 420 421 return bp; 422 } 423 424 static int ptrace_hbp_set_ctrl(unsigned int note_type, 425 struct task_struct *tsk, 426 unsigned long idx, 427 u32 uctrl) 428 { 429 int err; 430 struct perf_event *bp; 431 struct perf_event_attr attr; 432 struct arch_hw_breakpoint_ctrl ctrl; 433 434 bp = ptrace_hbp_get_initialised_bp(note_type, tsk, idx); 435 if (IS_ERR(bp)) { 436 err = PTR_ERR(bp); 437 return err; 438 } 439 440 attr = bp->attr; 441 decode_ctrl_reg(uctrl, &ctrl); 442 err = ptrace_hbp_fill_attr_ctrl(note_type, ctrl, &attr); 443 if (err) 444 return err; 445 446 return modify_user_hw_breakpoint(bp, &attr); 447 } 448 449 static int ptrace_hbp_set_addr(unsigned int note_type, 450 struct task_struct *tsk, 451 unsigned long idx, 452 u64 addr) 453 { 454 int err; 455 struct perf_event *bp; 456 struct perf_event_attr attr; 457 458 bp = ptrace_hbp_get_initialised_bp(note_type, tsk, idx); 459 if (IS_ERR(bp)) { 460 err = PTR_ERR(bp); 461 return err; 462 } 463 464 attr = bp->attr; 465 attr.bp_addr = addr; 466 err = modify_user_hw_breakpoint(bp, &attr); 467 return err; 468 } 469 470 #define PTRACE_HBP_ADDR_SZ sizeof(u64) 471 #define PTRACE_HBP_CTRL_SZ sizeof(u32) 472 #define PTRACE_HBP_PAD_SZ sizeof(u32) 473 474 static int hw_break_get(struct task_struct *target, 475 const struct user_regset *regset, 476 struct membuf to) 477 { 478 unsigned int note_type = regset->core_note_type; 479 int ret, idx = 0; 480 u32 info, ctrl; 481 u64 addr; 482 483 /* Resource info */ 484 ret = ptrace_hbp_get_resource_info(note_type, &info); 485 if (ret) 486 return ret; 487 488 membuf_write(&to, &info, sizeof(info)); 489 membuf_zero(&to, sizeof(u32)); 490 /* (address, ctrl) registers */ 491 while (to.left) { 492 ret = ptrace_hbp_get_addr(note_type, target, idx, &addr); 493 if (ret) 494 return ret; 495 ret = ptrace_hbp_get_ctrl(note_type, target, idx, &ctrl); 496 if (ret) 497 return ret; 498 membuf_store(&to, addr); 499 membuf_store(&to, ctrl); 500 membuf_zero(&to, sizeof(u32)); 501 idx++; 502 } 503 return 0; 504 } 505 506 static int hw_break_set(struct task_struct *target, 507 const struct user_regset *regset, 508 unsigned int pos, unsigned int count, 509 const void *kbuf, const void __user *ubuf) 510 { 511 unsigned int note_type = regset->core_note_type; 512 int ret, idx = 0, offset, limit; 513 u32 ctrl; 514 u64 addr; 515 516 /* Resource info and pad */ 517 offset = offsetof(struct user_hwdebug_state, dbg_regs); 518 user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf, 0, offset); 519 520 /* (address, ctrl) registers */ 521 limit = regset->n * regset->size; 522 while (count && offset < limit) { 523 if (count < PTRACE_HBP_ADDR_SZ) 524 return -EINVAL; 525 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &addr, 526 offset, offset + PTRACE_HBP_ADDR_SZ); 527 if (ret) 528 return ret; 529 ret = ptrace_hbp_set_addr(note_type, target, idx, addr); 530 if (ret) 531 return ret; 532 offset += PTRACE_HBP_ADDR_SZ; 533 534 if (!count) 535 break; 536 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &ctrl, 537 offset, offset + PTRACE_HBP_CTRL_SZ); 538 if (ret) 539 return ret; 540 ret = ptrace_hbp_set_ctrl(note_type, target, idx, ctrl); 541 if (ret) 542 return ret; 543 offset += PTRACE_HBP_CTRL_SZ; 544 545 user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf, 546 offset, offset + PTRACE_HBP_PAD_SZ); 547 offset += PTRACE_HBP_PAD_SZ; 548 idx++; 549 } 550 551 return 0; 552 } 553 #endif /* CONFIG_HAVE_HW_BREAKPOINT */ 554 555 static int gpr_get(struct task_struct *target, 556 const struct user_regset *regset, 557 struct membuf to) 558 { 559 struct user_pt_regs *uregs = &task_pt_regs(target)->user_regs; 560 return membuf_write(&to, uregs, sizeof(*uregs)); 561 } 562 563 static void update_syscall_orig_x0_after_ptrace(struct task_struct *target) 564 { 565 struct pt_regs *regs = task_pt_regs(target); 566 struct kernel_siginfo *info = target->last_siginfo; 567 568 /* 569 * Skip the update for NO_SYSCALL (set either by the user or the 570 * tracer), as regs[0] holds the return value (see the comment in 571 * el0_svc_common()) and can be unwound using syscall_rollback(). 572 */ 573 if (regs->syscallno == NO_SYSCALL) 574 return; 575 576 /* 577 * For exit-stops, info is always non-NULL, but that's not true for other 578 * stops such as non-seized group-stops. In those cases, just return early 579 * as there's no need to update orig_x0 anyway. 580 */ 581 if (!info) 582 return; 583 584 /* 585 * For compat tasks, orig_r0 is provided directly through GPR index 586 * 17. 587 */ 588 if (is_compat_thread(task_thread_info(target))) 589 return; 590 591 /* 592 * Don't update orig_x0 for a syscall-exit-stop, as x0 now contains the 593 * return value of the system call. 594 */ 595 if ((info->si_code & ~0x80) == SIGTRAP && 596 target->ptrace_message == PTRACE_EVENTMSG_SYSCALL_EXIT) { 597 return; 598 } 599 600 regs->orig_x0 = regs->regs[0]; 601 } 602 603 static int gpr_set(struct task_struct *target, const struct user_regset *regset, 604 unsigned int pos, unsigned int count, 605 const void *kbuf, const void __user *ubuf) 606 { 607 int ret; 608 struct user_pt_regs newregs = task_pt_regs(target)->user_regs; 609 610 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &newregs, 0, -1); 611 if (ret) 612 return ret; 613 614 if (!valid_user_regs(&newregs, target)) 615 return -EINVAL; 616 617 task_pt_regs(target)->user_regs = newregs; 618 619 /* 620 * Keep orig_x0 authoritative so that seccomp (via 621 * syscall_get_arguments()), audit and the restart path all see the same 622 * first argument the syscall is dispatched with, even if it has been 623 * updated by a tracer. 624 */ 625 update_syscall_orig_x0_after_ptrace(target); 626 return 0; 627 } 628 629 static int fpr_active(struct task_struct *target, const struct user_regset *regset) 630 { 631 if (!system_supports_fpsimd()) 632 return -ENODEV; 633 return regset->n; 634 } 635 636 /* 637 * TODO: update fp accessors for lazy context switching (sync/flush hwstate) 638 */ 639 static int __fpr_get(struct task_struct *target, 640 const struct user_regset *regset, 641 struct membuf to) 642 { 643 struct user_fpsimd_state *uregs; 644 645 fpsimd_sync_from_effective_state(target); 646 647 uregs = &target->thread.uw.fpsimd_state; 648 649 return membuf_write(&to, uregs, sizeof(*uregs)); 650 } 651 652 static int fpr_get(struct task_struct *target, const struct user_regset *regset, 653 struct membuf to) 654 { 655 if (!system_supports_fpsimd()) 656 return -EINVAL; 657 658 if (target == current) 659 fpsimd_preserve_current_state(); 660 661 return __fpr_get(target, regset, to); 662 } 663 664 static int __fpr_set(struct task_struct *target, 665 const struct user_regset *regset, 666 unsigned int pos, unsigned int count, 667 const void *kbuf, const void __user *ubuf, 668 unsigned int start_pos) 669 { 670 int ret; 671 struct user_fpsimd_state newstate; 672 673 /* 674 * Ensure target->thread.uw.fpsimd_state is up to date, so that a 675 * short copyin can't resurrect stale data. 676 */ 677 fpsimd_sync_from_effective_state(target); 678 679 newstate = target->thread.uw.fpsimd_state; 680 681 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &newstate, 682 start_pos, start_pos + sizeof(newstate)); 683 if (ret) 684 return ret; 685 686 target->thread.uw.fpsimd_state = newstate; 687 688 return ret; 689 } 690 691 static int fpr_set(struct task_struct *target, const struct user_regset *regset, 692 unsigned int pos, unsigned int count, 693 const void *kbuf, const void __user *ubuf) 694 { 695 int ret; 696 697 if (!system_supports_fpsimd()) 698 return -EINVAL; 699 700 ret = __fpr_set(target, regset, pos, count, kbuf, ubuf, 0); 701 if (ret) 702 return ret; 703 704 fpsimd_sync_to_effective_state_zeropad(target); 705 fpsimd_flush_task_state(target); 706 707 return ret; 708 } 709 710 static int tls_get(struct task_struct *target, const struct user_regset *regset, 711 struct membuf to) 712 { 713 int ret; 714 715 if (target == current) 716 tls_preserve_current_state(); 717 718 ret = membuf_store(&to, target->thread.uw.tp_value); 719 if (system_supports_tpidr2()) 720 ret = membuf_store(&to, target->thread.tpidr2_el0); 721 else 722 ret = membuf_zero(&to, sizeof(u64)); 723 724 return ret; 725 } 726 727 static int tls_set(struct task_struct *target, const struct user_regset *regset, 728 unsigned int pos, unsigned int count, 729 const void *kbuf, const void __user *ubuf) 730 { 731 int ret; 732 unsigned long tls[2]; 733 734 tls[0] = target->thread.uw.tp_value; 735 if (system_supports_tpidr2()) 736 tls[1] = target->thread.tpidr2_el0; 737 738 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, tls, 0, count); 739 if (ret) 740 return ret; 741 742 target->thread.uw.tp_value = tls[0]; 743 if (system_supports_tpidr2()) 744 target->thread.tpidr2_el0 = tls[1]; 745 746 return ret; 747 } 748 749 static int fpmr_get(struct task_struct *target, const struct user_regset *regset, 750 struct membuf to) 751 { 752 if (!system_supports_fpmr()) 753 return -EINVAL; 754 755 if (target == current) 756 fpsimd_preserve_current_state(); 757 758 return membuf_store(&to, target->thread.uw.fpmr); 759 } 760 761 static int fpmr_set(struct task_struct *target, const struct user_regset *regset, 762 unsigned int pos, unsigned int count, 763 const void *kbuf, const void __user *ubuf) 764 { 765 int ret; 766 unsigned long fpmr; 767 768 if (!system_supports_fpmr()) 769 return -EINVAL; 770 771 fpmr = target->thread.uw.fpmr; 772 773 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &fpmr, 0, count); 774 if (ret) 775 return ret; 776 777 target->thread.uw.fpmr = fpmr; 778 779 fpsimd_flush_task_state(target); 780 781 return 0; 782 } 783 784 static int system_call_get(struct task_struct *target, 785 const struct user_regset *regset, 786 struct membuf to) 787 { 788 return membuf_store(&to, task_pt_regs(target)->syscallno); 789 } 790 791 static int system_call_set(struct task_struct *target, 792 const struct user_regset *regset, 793 unsigned int pos, unsigned int count, 794 const void *kbuf, const void __user *ubuf) 795 { 796 int syscallno = task_pt_regs(target)->syscallno; 797 int ret; 798 799 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &syscallno, 0, -1); 800 if (ret) 801 return ret; 802 803 task_pt_regs(target)->syscallno = syscallno; 804 805 /* 806 * Re-sync orig_x0 in case the syscall number has been changed 807 * from NO_SYSCALL. 808 */ 809 update_syscall_orig_x0_after_ptrace(target); 810 return ret; 811 } 812 813 #ifdef CONFIG_ARM64_SVE 814 815 static void sve_init_header_from_task(struct user_sve_header *header, 816 struct task_struct *target, 817 enum vec_type type) 818 { 819 unsigned int vq; 820 bool active; 821 enum vec_type task_type; 822 823 memset(header, 0, sizeof(*header)); 824 825 /* Check if the requested registers are active for the task */ 826 if (thread_sm_enabled(&target->thread)) 827 task_type = ARM64_VEC_SME; 828 else 829 task_type = ARM64_VEC_SVE; 830 active = (task_type == type); 831 832 if (active && target->thread.fp_type == FP_STATE_SVE) 833 header->flags = SVE_PT_REGS_SVE; 834 else 835 header->flags = SVE_PT_REGS_FPSIMD; 836 837 switch (type) { 838 case ARM64_VEC_SVE: 839 if (test_tsk_thread_flag(target, TIF_SVE_VL_INHERIT)) 840 header->flags |= SVE_PT_VL_INHERIT; 841 break; 842 case ARM64_VEC_SME: 843 if (test_tsk_thread_flag(target, TIF_SME_VL_INHERIT)) 844 header->flags |= SVE_PT_VL_INHERIT; 845 break; 846 default: 847 WARN_ON_ONCE(1); 848 return; 849 } 850 851 header->vl = task_get_vl(target, type); 852 vq = sve_vq_from_vl(header->vl); 853 854 header->max_vl = vec_max_vl(type); 855 if (active) 856 header->size = SVE_PT_SIZE(vq, header->flags); 857 else 858 header->size = sizeof(*header); 859 header->max_size = SVE_PT_SIZE(sve_vq_from_vl(header->max_vl), 860 SVE_PT_REGS_SVE); 861 } 862 863 static unsigned int sve_size_from_header(struct user_sve_header const *header) 864 { 865 return ALIGN(header->size, SVE_VQ_BYTES); 866 } 867 868 static int sve_get_common(struct task_struct *target, 869 const struct user_regset *regset, 870 struct membuf to, 871 enum vec_type type) 872 { 873 struct user_sve_header header; 874 unsigned int vq; 875 unsigned long start, end; 876 877 if (target == current) 878 fpsimd_preserve_current_state(); 879 880 /* Header */ 881 sve_init_header_from_task(&header, target, type); 882 vq = sve_vq_from_vl(header.vl); 883 884 membuf_write(&to, &header, sizeof(header)); 885 886 BUILD_BUG_ON(SVE_PT_FPSIMD_OFFSET != sizeof(header)); 887 BUILD_BUG_ON(SVE_PT_SVE_OFFSET != sizeof(header)); 888 889 /* 890 * When the requested vector type is not active, do not present data 891 * from the other mode to userspace. 892 */ 893 if (header.size == sizeof(header)) 894 return to.left; 895 896 switch ((header.flags & SVE_PT_REGS_MASK)) { 897 case SVE_PT_REGS_FPSIMD: 898 return __fpr_get(target, regset, to); 899 900 case SVE_PT_REGS_SVE: 901 start = SVE_PT_SVE_OFFSET; 902 end = SVE_PT_SVE_FFR_OFFSET(vq) + SVE_PT_SVE_FFR_SIZE(vq); 903 membuf_write(&to, target->thread.sve_state, end - start); 904 905 start = end; 906 end = SVE_PT_SVE_FPSR_OFFSET(vq); 907 membuf_zero(&to, end - start); 908 909 /* 910 * Copy fpsr, and fpcr which must follow contiguously in 911 * struct fpsimd_state: 912 */ 913 start = end; 914 end = SVE_PT_SVE_FPCR_OFFSET(vq) + SVE_PT_SVE_FPCR_SIZE; 915 membuf_write(&to, &target->thread.uw.fpsimd_state.fpsr, 916 end - start); 917 918 start = end; 919 end = sve_size_from_header(&header); 920 return membuf_zero(&to, end - start); 921 922 default: 923 BUILD_BUG(); 924 } 925 } 926 927 static int sve_get(struct task_struct *target, 928 const struct user_regset *regset, 929 struct membuf to) 930 { 931 if (!system_supports_sve()) 932 return -EINVAL; 933 934 return sve_get_common(target, regset, to, ARM64_VEC_SVE); 935 } 936 937 static int sve_set_common(struct task_struct *target, 938 const struct user_regset *regset, 939 unsigned int pos, unsigned int count, 940 const void *kbuf, const void __user *ubuf, 941 enum vec_type type) 942 { 943 int ret; 944 struct user_sve_header header; 945 unsigned int vq; 946 unsigned long start, end; 947 bool fpsimd; 948 949 fpsimd_flush_task_state(target); 950 951 /* Header */ 952 if (count < sizeof(header)) 953 return -EINVAL; 954 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &header, 955 0, sizeof(header)); 956 if (ret) 957 return ret; 958 959 /* 960 * Streaming SVE data is always stored and presented in SVE format. 961 * Require the user to provide SVE formatted data for consistency, and 962 * to avoid the risk that we configure the task into an invalid state. 963 */ 964 fpsimd = (header.flags & SVE_PT_REGS_MASK) == SVE_PT_REGS_FPSIMD; 965 if (fpsimd && type == ARM64_VEC_SME) 966 return -EINVAL; 967 968 /* 969 * On systems without SVE we accept FPSIMD format writes with 970 * a VL of 0 to allow exiting streaming mode, otherwise a VL 971 * is required. 972 */ 973 if (header.vl) { 974 /* 975 * If the system does not support SVE we can't 976 * configure a SVE VL. 977 */ 978 if (!system_supports_sve() && type == ARM64_VEC_SVE) 979 return -EINVAL; 980 981 /* 982 * Apart from SVE_PT_REGS_MASK, all SVE_PT_* flags are 983 * consumed by vec_set_vector_length(), which will 984 * also validate them for us: 985 */ 986 ret = vec_set_vector_length(target, type, header.vl, 987 ((unsigned long)header.flags & ~SVE_PT_REGS_MASK) << 16); 988 if (ret) 989 return ret; 990 } else { 991 /* If the system supports SVE we require a VL. */ 992 if (system_supports_sve()) 993 return -EINVAL; 994 995 /* 996 * Only FPSIMD formatted data with no flags set is 997 * supported. 998 */ 999 if (header.flags != SVE_PT_REGS_FPSIMD) 1000 return -EINVAL; 1001 } 1002 1003 /* Allocate SME storage if necessary, preserving any existing ZA/ZT state */ 1004 if (type == ARM64_VEC_SME) { 1005 sme_alloc(target, false); 1006 if (!target->thread.sme_state) 1007 return -ENOMEM; 1008 } 1009 1010 /* Allocate SVE storage if necessary, zeroing any existing SVE state */ 1011 if (!fpsimd) { 1012 sve_alloc(target, true); 1013 if (!target->thread.sve_state) 1014 return -ENOMEM; 1015 } 1016 1017 /* 1018 * Actual VL set may be different from what the user asked 1019 * for, or we may have configured the _ONEXEC VL not the 1020 * current VL: 1021 */ 1022 vq = sve_vq_from_vl(task_get_vl(target, type)); 1023 1024 /* Enter/exit streaming mode */ 1025 switch (type) { 1026 case ARM64_VEC_SVE: 1027 target->thread.svcr &= ~SVCR_SM_MASK; 1028 set_tsk_thread_flag(target, TIF_SVE); 1029 break; 1030 case ARM64_VEC_SME: 1031 target->thread.svcr |= SVCR_SM_MASK; 1032 set_tsk_thread_flag(target, TIF_SME); 1033 break; 1034 default: 1035 WARN_ON_ONCE(1); 1036 return -EINVAL; 1037 } 1038 1039 /* Always zero V regs, FPSR, and FPCR */ 1040 memset(&target->thread.uw.fpsimd_state, 0, 1041 sizeof(target->thread.uw.fpsimd_state)); 1042 1043 /* Registers: FPSIMD-only case */ 1044 1045 BUILD_BUG_ON(SVE_PT_FPSIMD_OFFSET != sizeof(header)); 1046 if (fpsimd) { 1047 clear_tsk_thread_flag(target, TIF_SVE); 1048 target->thread.fp_type = FP_STATE_FPSIMD; 1049 ret = __fpr_set(target, regset, pos, count, kbuf, ubuf, 1050 SVE_PT_FPSIMD_OFFSET); 1051 return ret; 1052 } 1053 1054 /* Otherwise: no registers or full SVE case. */ 1055 1056 target->thread.fp_type = FP_STATE_SVE; 1057 1058 /* 1059 * If setting a different VL from the requested VL and there is 1060 * register data, the data layout will be wrong: don't even 1061 * try to set the registers in this case. 1062 */ 1063 if (count && vq != sve_vq_from_vl(header.vl)) 1064 return -EIO; 1065 1066 BUILD_BUG_ON(SVE_PT_SVE_OFFSET != sizeof(header)); 1067 start = SVE_PT_SVE_OFFSET; 1068 end = SVE_PT_SVE_FFR_OFFSET(vq) + SVE_PT_SVE_FFR_SIZE(vq); 1069 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, 1070 target->thread.sve_state, 1071 start, end); 1072 if (ret) 1073 return ret; 1074 1075 start = end; 1076 end = SVE_PT_SVE_FPSR_OFFSET(vq); 1077 user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf, start, end); 1078 1079 /* 1080 * Copy fpsr, and fpcr which must follow contiguously in 1081 * struct fpsimd_state: 1082 */ 1083 start = end; 1084 end = SVE_PT_SVE_FPCR_OFFSET(vq) + SVE_PT_SVE_FPCR_SIZE; 1085 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, 1086 &target->thread.uw.fpsimd_state.fpsr, 1087 start, end); 1088 1089 return ret; 1090 } 1091 1092 static int sve_set(struct task_struct *target, 1093 const struct user_regset *regset, 1094 unsigned int pos, unsigned int count, 1095 const void *kbuf, const void __user *ubuf) 1096 { 1097 if (!system_supports_sve() && !system_supports_sme()) 1098 return -EINVAL; 1099 1100 return sve_set_common(target, regset, pos, count, kbuf, ubuf, 1101 ARM64_VEC_SVE); 1102 } 1103 1104 #endif /* CONFIG_ARM64_SVE */ 1105 1106 #ifdef CONFIG_ARM64_SME 1107 1108 static int ssve_get(struct task_struct *target, 1109 const struct user_regset *regset, 1110 struct membuf to) 1111 { 1112 if (!system_supports_sme()) 1113 return -EINVAL; 1114 1115 return sve_get_common(target, regset, to, ARM64_VEC_SME); 1116 } 1117 1118 static int ssve_set(struct task_struct *target, 1119 const struct user_regset *regset, 1120 unsigned int pos, unsigned int count, 1121 const void *kbuf, const void __user *ubuf) 1122 { 1123 if (!system_supports_sme()) 1124 return -EINVAL; 1125 1126 return sve_set_common(target, regset, pos, count, kbuf, ubuf, 1127 ARM64_VEC_SME); 1128 } 1129 1130 static int za_get(struct task_struct *target, 1131 const struct user_regset *regset, 1132 struct membuf to) 1133 { 1134 struct user_za_header header; 1135 unsigned int vq; 1136 unsigned long start, end; 1137 1138 if (!system_supports_sme()) 1139 return -EINVAL; 1140 1141 /* Header */ 1142 memset(&header, 0, sizeof(header)); 1143 1144 if (test_tsk_thread_flag(target, TIF_SME_VL_INHERIT)) 1145 header.flags |= ZA_PT_VL_INHERIT; 1146 1147 header.vl = task_get_sme_vl(target); 1148 vq = sve_vq_from_vl(header.vl); 1149 header.max_vl = sme_max_vl(); 1150 header.max_size = ZA_PT_SIZE(vq); 1151 1152 /* If ZA is not active there is only the header */ 1153 if (thread_za_enabled(&target->thread)) 1154 header.size = ZA_PT_SIZE(vq); 1155 else 1156 header.size = ZA_PT_ZA_OFFSET; 1157 1158 membuf_write(&to, &header, sizeof(header)); 1159 1160 BUILD_BUG_ON(ZA_PT_ZA_OFFSET != sizeof(header)); 1161 end = ZA_PT_ZA_OFFSET; 1162 1163 if (target == current) 1164 fpsimd_preserve_current_state(); 1165 1166 /* Any register data to include? */ 1167 if (thread_za_enabled(&target->thread)) { 1168 start = end; 1169 end = ZA_PT_SIZE(vq); 1170 membuf_write(&to, target->thread.sme_state, end - start); 1171 } 1172 1173 /* Zero any trailing padding */ 1174 start = end; 1175 end = ALIGN(header.size, SVE_VQ_BYTES); 1176 return membuf_zero(&to, end - start); 1177 } 1178 1179 static int za_set(struct task_struct *target, 1180 const struct user_regset *regset, 1181 unsigned int pos, unsigned int count, 1182 const void *kbuf, const void __user *ubuf) 1183 { 1184 int ret; 1185 struct user_za_header header; 1186 unsigned int vq; 1187 unsigned long start, end; 1188 1189 if (!system_supports_sme()) 1190 return -EINVAL; 1191 1192 /* Header */ 1193 if (count < sizeof(header)) 1194 return -EINVAL; 1195 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &header, 1196 0, sizeof(header)); 1197 if (ret) 1198 goto out; 1199 1200 /* 1201 * All current ZA_PT_* flags are consumed by 1202 * vec_set_vector_length(), which will also validate them for 1203 * us: 1204 */ 1205 ret = vec_set_vector_length(target, ARM64_VEC_SME, header.vl, 1206 ((unsigned long)header.flags) << 16); 1207 if (ret) 1208 goto out; 1209 1210 /* 1211 * Actual VL set may be different from what the user asked 1212 * for, or we may have configured the _ONEXEC rather than 1213 * current VL: 1214 */ 1215 vq = sve_vq_from_vl(task_get_sme_vl(target)); 1216 1217 /* Ensure there is some SVE storage for streaming mode */ 1218 if (!target->thread.sve_state) { 1219 sve_alloc(target, false); 1220 if (!target->thread.sve_state) { 1221 ret = -ENOMEM; 1222 goto out; 1223 } 1224 } 1225 1226 /* 1227 * Only flush the storage if PSTATE.ZA was not already set, 1228 * otherwise preserve any existing data. 1229 */ 1230 sme_alloc(target, !thread_za_enabled(&target->thread)); 1231 if (!target->thread.sme_state) 1232 return -ENOMEM; 1233 1234 /* If there is no data then disable ZA */ 1235 if (!count) { 1236 target->thread.svcr &= ~SVCR_ZA_MASK; 1237 goto out; 1238 } 1239 1240 /* 1241 * If setting a different VL from the requested VL and there is 1242 * register data, the data layout will be wrong: don't even 1243 * try to set the registers in this case. 1244 */ 1245 if (vq != sve_vq_from_vl(header.vl)) { 1246 ret = -EIO; 1247 goto out; 1248 } 1249 1250 BUILD_BUG_ON(ZA_PT_ZA_OFFSET != sizeof(header)); 1251 start = ZA_PT_ZA_OFFSET; 1252 end = ZA_PT_SIZE(vq); 1253 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, 1254 target->thread.sme_state, 1255 start, end); 1256 if (ret) 1257 goto out; 1258 1259 /* Mark ZA as active and let userspace use it */ 1260 set_tsk_thread_flag(target, TIF_SME); 1261 target->thread.svcr |= SVCR_ZA_MASK; 1262 1263 out: 1264 fpsimd_flush_task_state(target); 1265 return ret; 1266 } 1267 1268 static int zt_get(struct task_struct *target, 1269 const struct user_regset *regset, 1270 struct membuf to) 1271 { 1272 if (!system_supports_sme2()) 1273 return -EINVAL; 1274 1275 /* 1276 * If PSTATE.ZA is not set then ZT will be zeroed when it is 1277 * enabled so report the current register value as zero. 1278 */ 1279 if (thread_za_enabled(&target->thread)) 1280 membuf_write(&to, thread_zt_state(&target->thread), 1281 ZT_SIG_REG_BYTES); 1282 else 1283 membuf_zero(&to, ZT_SIG_REG_BYTES); 1284 1285 return 0; 1286 } 1287 1288 static int zt_set(struct task_struct *target, 1289 const struct user_regset *regset, 1290 unsigned int pos, unsigned int count, 1291 const void *kbuf, const void __user *ubuf) 1292 { 1293 int ret; 1294 1295 if (!system_supports_sme2()) 1296 return -EINVAL; 1297 1298 /* Ensure SVE storage in case this is first use of SME */ 1299 sve_alloc(target, false); 1300 if (!target->thread.sve_state) 1301 return -ENOMEM; 1302 1303 if (!thread_za_enabled(&target->thread)) { 1304 sme_alloc(target, true); 1305 if (!target->thread.sme_state) 1306 return -ENOMEM; 1307 } 1308 1309 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, 1310 thread_zt_state(&target->thread), 1311 0, ZT_SIG_REG_BYTES); 1312 if (ret == 0) { 1313 target->thread.svcr |= SVCR_ZA_MASK; 1314 set_tsk_thread_flag(target, TIF_SME); 1315 } 1316 1317 fpsimd_flush_task_state(target); 1318 1319 return ret; 1320 } 1321 1322 #endif /* CONFIG_ARM64_SME */ 1323 1324 #ifdef CONFIG_ARM64_PTR_AUTH 1325 static int pac_mask_get(struct task_struct *target, 1326 const struct user_regset *regset, 1327 struct membuf to) 1328 { 1329 /* 1330 * The PAC bits can differ across data and instruction pointers 1331 * depending on TCR_EL1.TBID*, which we may make use of in future, so 1332 * we expose separate masks. 1333 */ 1334 unsigned long mask = ptrauth_user_pac_mask(); 1335 struct user_pac_mask uregs = { 1336 .data_mask = mask, 1337 .insn_mask = mask, 1338 }; 1339 1340 if (!system_supports_address_auth()) 1341 return -EINVAL; 1342 1343 return membuf_write(&to, &uregs, sizeof(uregs)); 1344 } 1345 1346 static int pac_enabled_keys_get(struct task_struct *target, 1347 const struct user_regset *regset, 1348 struct membuf to) 1349 { 1350 long enabled_keys = ptrauth_get_enabled_keys(target); 1351 1352 if (IS_ERR_VALUE(enabled_keys)) 1353 return enabled_keys; 1354 1355 return membuf_write(&to, &enabled_keys, sizeof(enabled_keys)); 1356 } 1357 1358 static int pac_enabled_keys_set(struct task_struct *target, 1359 const struct user_regset *regset, 1360 unsigned int pos, unsigned int count, 1361 const void *kbuf, const void __user *ubuf) 1362 { 1363 int ret; 1364 long enabled_keys = ptrauth_get_enabled_keys(target); 1365 1366 if (IS_ERR_VALUE(enabled_keys)) 1367 return enabled_keys; 1368 1369 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &enabled_keys, 0, 1370 sizeof(long)); 1371 if (ret) 1372 return ret; 1373 1374 return ptrauth_set_enabled_keys(target, PR_PAC_ENABLED_KEYS_MASK, 1375 enabled_keys); 1376 } 1377 1378 #ifdef CONFIG_CHECKPOINT_RESTORE 1379 static __uint128_t pac_key_to_user(const struct ptrauth_key *key) 1380 { 1381 return (__uint128_t)key->hi << 64 | key->lo; 1382 } 1383 1384 static struct ptrauth_key pac_key_from_user(__uint128_t ukey) 1385 { 1386 struct ptrauth_key key = { 1387 .lo = (unsigned long)ukey, 1388 .hi = (unsigned long)(ukey >> 64), 1389 }; 1390 1391 return key; 1392 } 1393 1394 static void pac_address_keys_to_user(struct user_pac_address_keys *ukeys, 1395 const struct ptrauth_keys_user *keys) 1396 { 1397 ukeys->apiakey = pac_key_to_user(&keys->apia); 1398 ukeys->apibkey = pac_key_to_user(&keys->apib); 1399 ukeys->apdakey = pac_key_to_user(&keys->apda); 1400 ukeys->apdbkey = pac_key_to_user(&keys->apdb); 1401 } 1402 1403 static void pac_address_keys_from_user(struct ptrauth_keys_user *keys, 1404 const struct user_pac_address_keys *ukeys) 1405 { 1406 keys->apia = pac_key_from_user(ukeys->apiakey); 1407 keys->apib = pac_key_from_user(ukeys->apibkey); 1408 keys->apda = pac_key_from_user(ukeys->apdakey); 1409 keys->apdb = pac_key_from_user(ukeys->apdbkey); 1410 } 1411 1412 static int pac_address_keys_get(struct task_struct *target, 1413 const struct user_regset *regset, 1414 struct membuf to) 1415 { 1416 struct ptrauth_keys_user *keys = &target->thread.keys_user; 1417 struct user_pac_address_keys user_keys; 1418 1419 if (!system_supports_address_auth()) 1420 return -EINVAL; 1421 1422 pac_address_keys_to_user(&user_keys, keys); 1423 1424 return membuf_write(&to, &user_keys, sizeof(user_keys)); 1425 } 1426 1427 static int pac_address_keys_set(struct task_struct *target, 1428 const struct user_regset *regset, 1429 unsigned int pos, unsigned int count, 1430 const void *kbuf, const void __user *ubuf) 1431 { 1432 struct ptrauth_keys_user *keys = &target->thread.keys_user; 1433 struct user_pac_address_keys user_keys; 1434 int ret; 1435 1436 if (!system_supports_address_auth()) 1437 return -EINVAL; 1438 1439 pac_address_keys_to_user(&user_keys, keys); 1440 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, 1441 &user_keys, 0, -1); 1442 if (ret) 1443 return ret; 1444 pac_address_keys_from_user(keys, &user_keys); 1445 1446 return 0; 1447 } 1448 1449 static void pac_generic_keys_to_user(struct user_pac_generic_keys *ukeys, 1450 const struct ptrauth_keys_user *keys) 1451 { 1452 ukeys->apgakey = pac_key_to_user(&keys->apga); 1453 } 1454 1455 static void pac_generic_keys_from_user(struct ptrauth_keys_user *keys, 1456 const struct user_pac_generic_keys *ukeys) 1457 { 1458 keys->apga = pac_key_from_user(ukeys->apgakey); 1459 } 1460 1461 static int pac_generic_keys_get(struct task_struct *target, 1462 const struct user_regset *regset, 1463 struct membuf to) 1464 { 1465 struct ptrauth_keys_user *keys = &target->thread.keys_user; 1466 struct user_pac_generic_keys user_keys; 1467 1468 if (!system_supports_generic_auth()) 1469 return -EINVAL; 1470 1471 pac_generic_keys_to_user(&user_keys, keys); 1472 1473 return membuf_write(&to, &user_keys, sizeof(user_keys)); 1474 } 1475 1476 static int pac_generic_keys_set(struct task_struct *target, 1477 const struct user_regset *regset, 1478 unsigned int pos, unsigned int count, 1479 const void *kbuf, const void __user *ubuf) 1480 { 1481 struct ptrauth_keys_user *keys = &target->thread.keys_user; 1482 struct user_pac_generic_keys user_keys; 1483 int ret; 1484 1485 if (!system_supports_generic_auth()) 1486 return -EINVAL; 1487 1488 pac_generic_keys_to_user(&user_keys, keys); 1489 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, 1490 &user_keys, 0, -1); 1491 if (ret) 1492 return ret; 1493 pac_generic_keys_from_user(keys, &user_keys); 1494 1495 return 0; 1496 } 1497 #endif /* CONFIG_CHECKPOINT_RESTORE */ 1498 #endif /* CONFIG_ARM64_PTR_AUTH */ 1499 1500 #ifdef CONFIG_ARM64_TAGGED_ADDR_ABI 1501 static int tagged_addr_ctrl_get(struct task_struct *target, 1502 const struct user_regset *regset, 1503 struct membuf to) 1504 { 1505 long ctrl = get_tagged_addr_ctrl(target); 1506 1507 if (WARN_ON_ONCE(IS_ERR_VALUE(ctrl))) 1508 return ctrl; 1509 1510 return membuf_write(&to, &ctrl, sizeof(ctrl)); 1511 } 1512 1513 static int tagged_addr_ctrl_set(struct task_struct *target, const struct 1514 user_regset *regset, unsigned int pos, 1515 unsigned int count, const void *kbuf, const 1516 void __user *ubuf) 1517 { 1518 int ret; 1519 long ctrl; 1520 1521 ctrl = get_tagged_addr_ctrl(target); 1522 if (WARN_ON_ONCE(IS_ERR_VALUE(ctrl))) 1523 return ctrl; 1524 1525 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &ctrl, 0, -1); 1526 if (ret) 1527 return ret; 1528 1529 return set_tagged_addr_ctrl(target, ctrl); 1530 } 1531 #endif 1532 1533 #ifdef CONFIG_ARM64_POE 1534 static int poe_get(struct task_struct *target, 1535 const struct user_regset *regset, 1536 struct membuf to) 1537 { 1538 if (!system_supports_poe()) 1539 return -EINVAL; 1540 1541 if (target == current) 1542 current->thread.por_el0 = read_sysreg_s(SYS_POR_EL0); 1543 1544 return membuf_write(&to, &target->thread.por_el0, 1545 sizeof(target->thread.por_el0)); 1546 } 1547 1548 static int poe_set(struct task_struct *target, const struct 1549 user_regset *regset, unsigned int pos, 1550 unsigned int count, const void *kbuf, const 1551 void __user *ubuf) 1552 { 1553 int ret; 1554 long ctrl; 1555 1556 if (!system_supports_poe()) 1557 return -EINVAL; 1558 1559 ctrl = target->thread.por_el0; 1560 1561 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &ctrl, 0, -1); 1562 if (ret) 1563 return ret; 1564 1565 target->thread.por_el0 = ctrl; 1566 1567 return 0; 1568 } 1569 #endif 1570 1571 #ifdef CONFIG_ARM64_GCS 1572 static void task_gcs_to_user(struct user_gcs *user_gcs, 1573 const struct task_struct *target) 1574 { 1575 user_gcs->features_enabled = target->thread.gcs_el0_mode; 1576 user_gcs->features_locked = target->thread.gcs_el0_locked; 1577 user_gcs->gcspr_el0 = target->thread.gcspr_el0; 1578 } 1579 1580 static void task_gcs_from_user(struct task_struct *target, 1581 const struct user_gcs *user_gcs) 1582 { 1583 target->thread.gcs_el0_mode = user_gcs->features_enabled; 1584 target->thread.gcs_el0_locked = user_gcs->features_locked; 1585 target->thread.gcspr_el0 = user_gcs->gcspr_el0; 1586 } 1587 1588 static int gcs_get(struct task_struct *target, 1589 const struct user_regset *regset, 1590 struct membuf to) 1591 { 1592 struct user_gcs user_gcs; 1593 1594 if (!system_supports_gcs()) 1595 return -EINVAL; 1596 1597 if (target == current) 1598 gcs_preserve_current_state(); 1599 1600 task_gcs_to_user(&user_gcs, target); 1601 1602 return membuf_write(&to, &user_gcs, sizeof(user_gcs)); 1603 } 1604 1605 static int gcs_set(struct task_struct *target, const struct 1606 user_regset *regset, unsigned int pos, 1607 unsigned int count, const void *kbuf, const 1608 void __user *ubuf) 1609 { 1610 int ret; 1611 struct user_gcs user_gcs; 1612 1613 if (!system_supports_gcs()) 1614 return -EINVAL; 1615 1616 task_gcs_to_user(&user_gcs, target); 1617 1618 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &user_gcs, 0, -1); 1619 if (ret) 1620 return ret; 1621 1622 if (user_gcs.features_enabled & ~PR_SHADOW_STACK_SUPPORTED_STATUS_MASK) 1623 return -EINVAL; 1624 1625 task_gcs_from_user(target, &user_gcs); 1626 1627 return 0; 1628 } 1629 #endif 1630 1631 enum aarch64_regset { 1632 REGSET_GPR, 1633 REGSET_FPR, 1634 REGSET_TLS, 1635 #ifdef CONFIG_HAVE_HW_BREAKPOINT 1636 REGSET_HW_BREAK, 1637 REGSET_HW_WATCH, 1638 #endif 1639 REGSET_FPMR, 1640 REGSET_SYSTEM_CALL, 1641 #ifdef CONFIG_ARM64_SVE 1642 REGSET_SVE, 1643 #endif 1644 #ifdef CONFIG_ARM64_SME 1645 REGSET_SSVE, 1646 REGSET_ZA, 1647 REGSET_ZT, 1648 #endif 1649 #ifdef CONFIG_ARM64_PTR_AUTH 1650 REGSET_PAC_MASK, 1651 REGSET_PAC_ENABLED_KEYS, 1652 #ifdef CONFIG_CHECKPOINT_RESTORE 1653 REGSET_PACA_KEYS, 1654 REGSET_PACG_KEYS, 1655 #endif 1656 #endif 1657 #ifdef CONFIG_ARM64_TAGGED_ADDR_ABI 1658 REGSET_TAGGED_ADDR_CTRL, 1659 #endif 1660 #ifdef CONFIG_ARM64_POE 1661 REGSET_POE, 1662 #endif 1663 #ifdef CONFIG_ARM64_GCS 1664 REGSET_GCS, 1665 #endif 1666 }; 1667 1668 static const struct user_regset aarch64_regsets[] = { 1669 [REGSET_GPR] = { 1670 USER_REGSET_NOTE_TYPE(PRSTATUS), 1671 .n = sizeof(struct user_pt_regs) / sizeof(u64), 1672 .size = sizeof(u64), 1673 .align = sizeof(u64), 1674 .regset_get = gpr_get, 1675 .set = gpr_set 1676 }, 1677 [REGSET_FPR] = { 1678 USER_REGSET_NOTE_TYPE(PRFPREG), 1679 .n = sizeof(struct user_fpsimd_state) / sizeof(u32), 1680 /* 1681 * We pretend we have 32-bit registers because the fpsr and 1682 * fpcr are 32-bits wide. 1683 */ 1684 .size = sizeof(u32), 1685 .align = sizeof(u32), 1686 .active = fpr_active, 1687 .regset_get = fpr_get, 1688 .set = fpr_set 1689 }, 1690 [REGSET_TLS] = { 1691 USER_REGSET_NOTE_TYPE(ARM_TLS), 1692 .n = 2, 1693 .size = sizeof(void *), 1694 .align = sizeof(void *), 1695 .regset_get = tls_get, 1696 .set = tls_set, 1697 }, 1698 #ifdef CONFIG_HAVE_HW_BREAKPOINT 1699 [REGSET_HW_BREAK] = { 1700 USER_REGSET_NOTE_TYPE(ARM_HW_BREAK), 1701 .n = sizeof(struct user_hwdebug_state) / sizeof(u32), 1702 .size = sizeof(u32), 1703 .align = sizeof(u32), 1704 .regset_get = hw_break_get, 1705 .set = hw_break_set, 1706 }, 1707 [REGSET_HW_WATCH] = { 1708 USER_REGSET_NOTE_TYPE(ARM_HW_WATCH), 1709 .n = sizeof(struct user_hwdebug_state) / sizeof(u32), 1710 .size = sizeof(u32), 1711 .align = sizeof(u32), 1712 .regset_get = hw_break_get, 1713 .set = hw_break_set, 1714 }, 1715 #endif 1716 [REGSET_SYSTEM_CALL] = { 1717 USER_REGSET_NOTE_TYPE(ARM_SYSTEM_CALL), 1718 .n = 1, 1719 .size = sizeof(int), 1720 .align = sizeof(int), 1721 .regset_get = system_call_get, 1722 .set = system_call_set, 1723 }, 1724 [REGSET_FPMR] = { 1725 USER_REGSET_NOTE_TYPE(ARM_FPMR), 1726 .n = 1, 1727 .size = sizeof(u64), 1728 .align = sizeof(u64), 1729 .regset_get = fpmr_get, 1730 .set = fpmr_set, 1731 }, 1732 #ifdef CONFIG_ARM64_SVE 1733 [REGSET_SVE] = { /* Scalable Vector Extension */ 1734 USER_REGSET_NOTE_TYPE(ARM_SVE), 1735 .n = DIV_ROUND_UP(SVE_PT_SIZE(ARCH_SVE_VQ_MAX, 1736 SVE_PT_REGS_SVE), 1737 SVE_VQ_BYTES), 1738 .size = SVE_VQ_BYTES, 1739 .align = SVE_VQ_BYTES, 1740 .regset_get = sve_get, 1741 .set = sve_set, 1742 }, 1743 #endif 1744 #ifdef CONFIG_ARM64_SME 1745 [REGSET_SSVE] = { /* Streaming mode SVE */ 1746 USER_REGSET_NOTE_TYPE(ARM_SSVE), 1747 .n = DIV_ROUND_UP(SVE_PT_SIZE(SME_VQ_MAX, SVE_PT_REGS_SVE), 1748 SVE_VQ_BYTES), 1749 .size = SVE_VQ_BYTES, 1750 .align = SVE_VQ_BYTES, 1751 .regset_get = ssve_get, 1752 .set = ssve_set, 1753 }, 1754 [REGSET_ZA] = { /* SME ZA */ 1755 USER_REGSET_NOTE_TYPE(ARM_ZA), 1756 /* 1757 * ZA is a single register but it's variably sized and 1758 * the ptrace core requires that the size of any data 1759 * be an exact multiple of the configured register 1760 * size so report as though we had SVE_VQ_BYTES 1761 * registers. These values aren't exposed to 1762 * userspace. 1763 */ 1764 .n = DIV_ROUND_UP(ZA_PT_SIZE(SME_VQ_MAX), SVE_VQ_BYTES), 1765 .size = SVE_VQ_BYTES, 1766 .align = SVE_VQ_BYTES, 1767 .regset_get = za_get, 1768 .set = za_set, 1769 }, 1770 [REGSET_ZT] = { /* SME ZT */ 1771 USER_REGSET_NOTE_TYPE(ARM_ZT), 1772 .n = 1, 1773 .size = ZT_SIG_REG_BYTES, 1774 .align = sizeof(u64), 1775 .regset_get = zt_get, 1776 .set = zt_set, 1777 }, 1778 #endif 1779 #ifdef CONFIG_ARM64_PTR_AUTH 1780 [REGSET_PAC_MASK] = { 1781 USER_REGSET_NOTE_TYPE(ARM_PAC_MASK), 1782 .n = sizeof(struct user_pac_mask) / sizeof(u64), 1783 .size = sizeof(u64), 1784 .align = sizeof(u64), 1785 .regset_get = pac_mask_get, 1786 /* this cannot be set dynamically */ 1787 }, 1788 [REGSET_PAC_ENABLED_KEYS] = { 1789 USER_REGSET_NOTE_TYPE(ARM_PAC_ENABLED_KEYS), 1790 .n = 1, 1791 .size = sizeof(long), 1792 .align = sizeof(long), 1793 .regset_get = pac_enabled_keys_get, 1794 .set = pac_enabled_keys_set, 1795 }, 1796 #ifdef CONFIG_CHECKPOINT_RESTORE 1797 [REGSET_PACA_KEYS] = { 1798 USER_REGSET_NOTE_TYPE(ARM_PACA_KEYS), 1799 .n = sizeof(struct user_pac_address_keys) / sizeof(__uint128_t), 1800 .size = sizeof(__uint128_t), 1801 .align = sizeof(__uint128_t), 1802 .regset_get = pac_address_keys_get, 1803 .set = pac_address_keys_set, 1804 }, 1805 [REGSET_PACG_KEYS] = { 1806 USER_REGSET_NOTE_TYPE(ARM_PACG_KEYS), 1807 .n = sizeof(struct user_pac_generic_keys) / sizeof(__uint128_t), 1808 .size = sizeof(__uint128_t), 1809 .align = sizeof(__uint128_t), 1810 .regset_get = pac_generic_keys_get, 1811 .set = pac_generic_keys_set, 1812 }, 1813 #endif 1814 #endif 1815 #ifdef CONFIG_ARM64_TAGGED_ADDR_ABI 1816 [REGSET_TAGGED_ADDR_CTRL] = { 1817 USER_REGSET_NOTE_TYPE(ARM_TAGGED_ADDR_CTRL), 1818 .n = 1, 1819 .size = sizeof(long), 1820 .align = sizeof(long), 1821 .regset_get = tagged_addr_ctrl_get, 1822 .set = tagged_addr_ctrl_set, 1823 }, 1824 #endif 1825 #ifdef CONFIG_ARM64_POE 1826 [REGSET_POE] = { 1827 USER_REGSET_NOTE_TYPE(ARM_POE), 1828 .n = 1, 1829 .size = sizeof(long), 1830 .align = sizeof(long), 1831 .regset_get = poe_get, 1832 .set = poe_set, 1833 }, 1834 #endif 1835 #ifdef CONFIG_ARM64_GCS 1836 [REGSET_GCS] = { 1837 USER_REGSET_NOTE_TYPE(ARM_GCS), 1838 .n = sizeof(struct user_gcs) / sizeof(u64), 1839 .size = sizeof(u64), 1840 .align = sizeof(u64), 1841 .regset_get = gcs_get, 1842 .set = gcs_set, 1843 }, 1844 #endif 1845 }; 1846 1847 static const struct user_regset_view user_aarch64_view = { 1848 .name = "aarch64", .e_machine = EM_AARCH64, 1849 .regsets = aarch64_regsets, .n = ARRAY_SIZE(aarch64_regsets) 1850 }; 1851 1852 enum compat_regset { 1853 REGSET_COMPAT_GPR, 1854 REGSET_COMPAT_VFP, 1855 }; 1856 1857 static inline compat_ulong_t compat_get_user_reg(struct task_struct *task, int idx) 1858 { 1859 struct pt_regs *regs = task_pt_regs(task); 1860 1861 switch (idx) { 1862 case 15: 1863 return regs->pc; 1864 case 16: 1865 return pstate_to_compat_psr(regs->pstate); 1866 case 17: 1867 return regs->orig_x0; 1868 default: 1869 return regs->regs[idx]; 1870 } 1871 } 1872 1873 static int compat_gpr_get(struct task_struct *target, 1874 const struct user_regset *regset, 1875 struct membuf to) 1876 { 1877 int i = 0; 1878 1879 while (to.left) 1880 membuf_store(&to, compat_get_user_reg(target, i++)); 1881 return 0; 1882 } 1883 1884 static int compat_gpr_set(struct task_struct *target, 1885 const struct user_regset *regset, 1886 unsigned int pos, unsigned int count, 1887 const void *kbuf, const void __user *ubuf) 1888 { 1889 struct pt_regs newregs; 1890 int ret = 0; 1891 unsigned int i, start, num_regs; 1892 1893 /* Calculate the number of AArch32 registers contained in count */ 1894 num_regs = count / regset->size; 1895 1896 /* Convert pos into an register number */ 1897 start = pos / regset->size; 1898 1899 if (start + num_regs > regset->n) 1900 return -EIO; 1901 1902 newregs = *task_pt_regs(target); 1903 1904 for (i = 0; i < num_regs; ++i) { 1905 unsigned int idx = start + i; 1906 compat_ulong_t reg; 1907 1908 if (kbuf) { 1909 memcpy(®, kbuf, sizeof(reg)); 1910 kbuf += sizeof(reg); 1911 } else { 1912 ret = copy_from_user(®, ubuf, sizeof(reg)); 1913 if (ret) { 1914 ret = -EFAULT; 1915 break; 1916 } 1917 1918 ubuf += sizeof(reg); 1919 } 1920 1921 switch (idx) { 1922 case 15: 1923 newregs.pc = reg; 1924 break; 1925 case 16: 1926 reg = compat_psr_to_pstate(reg); 1927 newregs.pstate = reg; 1928 break; 1929 case 17: 1930 newregs.orig_x0 = reg; 1931 break; 1932 default: 1933 newregs.regs[idx] = reg; 1934 } 1935 1936 } 1937 1938 if (valid_user_regs(&newregs.user_regs, target)) 1939 *task_pt_regs(target) = newregs; 1940 else 1941 ret = -EINVAL; 1942 1943 return ret; 1944 } 1945 1946 static int compat_vfp_get(struct task_struct *target, 1947 const struct user_regset *regset, 1948 struct membuf to) 1949 { 1950 struct user_fpsimd_state *uregs; 1951 compat_ulong_t fpscr; 1952 1953 if (!system_supports_fpsimd()) 1954 return -EINVAL; 1955 1956 uregs = &target->thread.uw.fpsimd_state; 1957 1958 if (target == current) 1959 fpsimd_preserve_current_state(); 1960 1961 /* 1962 * The VFP registers are packed into the fpsimd_state, so they all sit 1963 * nicely together for us. We just need to create the fpscr separately. 1964 */ 1965 membuf_write(&to, uregs, VFP_STATE_SIZE - sizeof(compat_ulong_t)); 1966 fpscr = (uregs->fpsr & VFP_FPSCR_STAT_MASK) | 1967 (uregs->fpcr & VFP_FPSCR_CTRL_MASK); 1968 return membuf_store(&to, fpscr); 1969 } 1970 1971 static int compat_vfp_set(struct task_struct *target, 1972 const struct user_regset *regset, 1973 unsigned int pos, unsigned int count, 1974 const void *kbuf, const void __user *ubuf) 1975 { 1976 struct user_fpsimd_state *uregs; 1977 compat_ulong_t fpscr; 1978 int ret, vregs_end_pos; 1979 1980 if (!system_supports_fpsimd()) 1981 return -EINVAL; 1982 1983 uregs = &target->thread.uw.fpsimd_state; 1984 1985 vregs_end_pos = VFP_STATE_SIZE - sizeof(compat_ulong_t); 1986 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, uregs, 0, 1987 vregs_end_pos); 1988 1989 if (count && !ret) { 1990 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &fpscr, 1991 vregs_end_pos, VFP_STATE_SIZE); 1992 if (!ret) { 1993 uregs->fpsr = fpscr & VFP_FPSCR_STAT_MASK; 1994 uregs->fpcr = fpscr & VFP_FPSCR_CTRL_MASK; 1995 } 1996 } 1997 1998 fpsimd_flush_task_state(target); 1999 return ret; 2000 } 2001 2002 static int compat_tls_get(struct task_struct *target, 2003 const struct user_regset *regset, 2004 struct membuf to) 2005 { 2006 return membuf_store(&to, (compat_ulong_t)target->thread.uw.tp_value); 2007 } 2008 2009 static int compat_tls_set(struct task_struct *target, 2010 const struct user_regset *regset, unsigned int pos, 2011 unsigned int count, const void *kbuf, 2012 const void __user *ubuf) 2013 { 2014 int ret; 2015 compat_ulong_t tls = target->thread.uw.tp_value; 2016 2017 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &tls, 0, -1); 2018 if (ret) 2019 return ret; 2020 2021 target->thread.uw.tp_value = tls; 2022 return ret; 2023 } 2024 2025 static const struct user_regset aarch32_regsets[] = { 2026 [REGSET_COMPAT_GPR] = { 2027 USER_REGSET_NOTE_TYPE(PRSTATUS), 2028 .n = COMPAT_ELF_NGREG, 2029 .size = sizeof(compat_elf_greg_t), 2030 .align = sizeof(compat_elf_greg_t), 2031 .regset_get = compat_gpr_get, 2032 .set = compat_gpr_set 2033 }, 2034 [REGSET_COMPAT_VFP] = { 2035 USER_REGSET_NOTE_TYPE(ARM_VFP), 2036 .n = VFP_STATE_SIZE / sizeof(compat_ulong_t), 2037 .size = sizeof(compat_ulong_t), 2038 .align = sizeof(compat_ulong_t), 2039 .active = fpr_active, 2040 .regset_get = compat_vfp_get, 2041 .set = compat_vfp_set 2042 }, 2043 }; 2044 2045 static const struct user_regset_view user_aarch32_view = { 2046 .name = "aarch32", .e_machine = EM_ARM, 2047 .regsets = aarch32_regsets, .n = ARRAY_SIZE(aarch32_regsets) 2048 }; 2049 2050 static const struct user_regset aarch32_ptrace_regsets[] = { 2051 [REGSET_GPR] = { 2052 USER_REGSET_NOTE_TYPE(PRSTATUS), 2053 .n = COMPAT_ELF_NGREG, 2054 .size = sizeof(compat_elf_greg_t), 2055 .align = sizeof(compat_elf_greg_t), 2056 .regset_get = compat_gpr_get, 2057 .set = compat_gpr_set 2058 }, 2059 [REGSET_FPR] = { 2060 USER_REGSET_NOTE_TYPE(ARM_VFP), 2061 .n = VFP_STATE_SIZE / sizeof(compat_ulong_t), 2062 .size = sizeof(compat_ulong_t), 2063 .align = sizeof(compat_ulong_t), 2064 .regset_get = compat_vfp_get, 2065 .set = compat_vfp_set 2066 }, 2067 [REGSET_TLS] = { 2068 USER_REGSET_NOTE_TYPE(ARM_TLS), 2069 .n = 1, 2070 .size = sizeof(compat_ulong_t), 2071 .align = sizeof(compat_ulong_t), 2072 .regset_get = compat_tls_get, 2073 .set = compat_tls_set, 2074 }, 2075 #ifdef CONFIG_HAVE_HW_BREAKPOINT 2076 [REGSET_HW_BREAK] = { 2077 USER_REGSET_NOTE_TYPE(ARM_HW_BREAK), 2078 .n = sizeof(struct user_hwdebug_state) / sizeof(u32), 2079 .size = sizeof(u32), 2080 .align = sizeof(u32), 2081 .regset_get = hw_break_get, 2082 .set = hw_break_set, 2083 }, 2084 [REGSET_HW_WATCH] = { 2085 USER_REGSET_NOTE_TYPE(ARM_HW_WATCH), 2086 .n = sizeof(struct user_hwdebug_state) / sizeof(u32), 2087 .size = sizeof(u32), 2088 .align = sizeof(u32), 2089 .regset_get = hw_break_get, 2090 .set = hw_break_set, 2091 }, 2092 #endif 2093 [REGSET_SYSTEM_CALL] = { 2094 USER_REGSET_NOTE_TYPE(ARM_SYSTEM_CALL), 2095 .n = 1, 2096 .size = sizeof(int), 2097 .align = sizeof(int), 2098 .regset_get = system_call_get, 2099 .set = system_call_set, 2100 }, 2101 }; 2102 2103 static const struct user_regset_view user_aarch32_ptrace_view = { 2104 .name = "aarch32", .e_machine = EM_ARM, 2105 .regsets = aarch32_ptrace_regsets, .n = ARRAY_SIZE(aarch32_ptrace_regsets) 2106 }; 2107 2108 #ifdef CONFIG_COMPAT 2109 static int compat_ptrace_read_user(struct task_struct *tsk, compat_ulong_t off, 2110 compat_ulong_t __user *ret) 2111 { 2112 compat_ulong_t tmp; 2113 2114 if (off & 3) 2115 return -EIO; 2116 2117 if (off == COMPAT_PT_TEXT_ADDR) 2118 tmp = tsk->mm->start_code; 2119 else if (off == COMPAT_PT_DATA_ADDR) 2120 tmp = tsk->mm->start_data; 2121 else if (off == COMPAT_PT_TEXT_END_ADDR) 2122 tmp = tsk->mm->end_code; 2123 else if (off < sizeof(compat_elf_gregset_t)) 2124 tmp = compat_get_user_reg(tsk, off >> 2); 2125 else if (off >= COMPAT_USER_SZ) 2126 return -EIO; 2127 else 2128 tmp = 0; 2129 2130 return put_user(tmp, ret); 2131 } 2132 2133 static int compat_ptrace_write_user(struct task_struct *tsk, compat_ulong_t off, 2134 compat_ulong_t val) 2135 { 2136 struct pt_regs newregs = *task_pt_regs(tsk); 2137 unsigned int idx = off / 4; 2138 2139 if (off & 3 || off >= COMPAT_USER_SZ) 2140 return -EIO; 2141 2142 if (off >= sizeof(compat_elf_gregset_t)) 2143 return 0; 2144 2145 switch (idx) { 2146 case 15: 2147 newregs.pc = val; 2148 break; 2149 case 16: 2150 newregs.pstate = compat_psr_to_pstate(val); 2151 break; 2152 case 17: 2153 newregs.orig_x0 = val; 2154 break; 2155 default: 2156 newregs.regs[idx] = val; 2157 } 2158 2159 if (!valid_user_regs(&newregs.user_regs, tsk)) 2160 return -EINVAL; 2161 2162 *task_pt_regs(tsk) = newregs; 2163 return 0; 2164 } 2165 2166 #ifdef CONFIG_HAVE_HW_BREAKPOINT 2167 2168 /* 2169 * Convert a virtual register number into an index for a thread_info 2170 * breakpoint array. Breakpoints are identified using positive numbers 2171 * whilst watchpoints are negative. The registers are laid out as pairs 2172 * of (address, control), each pair mapping to a unique hw_breakpoint struct. 2173 * Register 0 is reserved for describing resource information. 2174 */ 2175 static int compat_ptrace_hbp_num_to_idx(compat_long_t num) 2176 { 2177 return (abs(num) - 1) >> 1; 2178 } 2179 2180 static int compat_ptrace_hbp_get_resource_info(u32 *kdata) 2181 { 2182 u8 num_brps, num_wrps, debug_arch, wp_len; 2183 u32 reg = 0; 2184 2185 num_brps = hw_breakpoint_slots(TYPE_INST); 2186 num_wrps = hw_breakpoint_slots(TYPE_DATA); 2187 2188 debug_arch = debug_monitors_arch(); 2189 wp_len = 8; 2190 reg |= debug_arch; 2191 reg <<= 8; 2192 reg |= wp_len; 2193 reg <<= 8; 2194 reg |= num_wrps; 2195 reg <<= 8; 2196 reg |= num_brps; 2197 2198 *kdata = reg; 2199 return 0; 2200 } 2201 2202 static int compat_ptrace_hbp_get(unsigned int note_type, 2203 struct task_struct *tsk, 2204 compat_long_t num, 2205 u32 *kdata) 2206 { 2207 u64 addr = 0; 2208 u32 ctrl = 0; 2209 2210 int err, idx = compat_ptrace_hbp_num_to_idx(num); 2211 2212 if (num & 1) { 2213 err = ptrace_hbp_get_addr(note_type, tsk, idx, &addr); 2214 *kdata = (u32)addr; 2215 } else { 2216 err = ptrace_hbp_get_ctrl(note_type, tsk, idx, &ctrl); 2217 *kdata = ctrl; 2218 } 2219 2220 return err; 2221 } 2222 2223 static int compat_ptrace_hbp_set(unsigned int note_type, 2224 struct task_struct *tsk, 2225 compat_long_t num, 2226 u32 *kdata) 2227 { 2228 u64 addr; 2229 u32 ctrl; 2230 2231 int err, idx = compat_ptrace_hbp_num_to_idx(num); 2232 2233 if (num & 1) { 2234 addr = *kdata; 2235 err = ptrace_hbp_set_addr(note_type, tsk, idx, addr); 2236 } else { 2237 ctrl = *kdata; 2238 err = ptrace_hbp_set_ctrl(note_type, tsk, idx, ctrl); 2239 } 2240 2241 return err; 2242 } 2243 2244 static int compat_ptrace_gethbpregs(struct task_struct *tsk, compat_long_t num, 2245 compat_ulong_t __user *data) 2246 { 2247 int ret; 2248 u32 kdata; 2249 2250 /* Watchpoint */ 2251 if (num < 0) { 2252 ret = compat_ptrace_hbp_get(NT_ARM_HW_WATCH, tsk, num, &kdata); 2253 /* Resource info */ 2254 } else if (num == 0) { 2255 ret = compat_ptrace_hbp_get_resource_info(&kdata); 2256 /* Breakpoint */ 2257 } else { 2258 ret = compat_ptrace_hbp_get(NT_ARM_HW_BREAK, tsk, num, &kdata); 2259 } 2260 2261 if (!ret) 2262 ret = put_user(kdata, data); 2263 2264 return ret; 2265 } 2266 2267 static int compat_ptrace_sethbpregs(struct task_struct *tsk, compat_long_t num, 2268 compat_ulong_t __user *data) 2269 { 2270 int ret; 2271 u32 kdata = 0; 2272 2273 if (num == 0) 2274 return 0; 2275 2276 ret = get_user(kdata, data); 2277 if (ret) 2278 return ret; 2279 2280 if (num < 0) 2281 ret = compat_ptrace_hbp_set(NT_ARM_HW_WATCH, tsk, num, &kdata); 2282 else 2283 ret = compat_ptrace_hbp_set(NT_ARM_HW_BREAK, tsk, num, &kdata); 2284 2285 return ret; 2286 } 2287 #endif /* CONFIG_HAVE_HW_BREAKPOINT */ 2288 2289 long compat_arch_ptrace(struct task_struct *child, compat_long_t request, 2290 compat_ulong_t caddr, compat_ulong_t cdata) 2291 { 2292 unsigned long addr = caddr; 2293 unsigned long data = cdata; 2294 void __user *datap = compat_ptr(data); 2295 int ret; 2296 2297 switch (request) { 2298 case PTRACE_PEEKUSR: 2299 ret = compat_ptrace_read_user(child, addr, datap); 2300 break; 2301 2302 case PTRACE_POKEUSR: 2303 ret = compat_ptrace_write_user(child, addr, data); 2304 break; 2305 2306 case COMPAT_PTRACE_GETREGS: 2307 ret = copy_regset_to_user(child, 2308 &user_aarch32_view, 2309 REGSET_COMPAT_GPR, 2310 0, sizeof(compat_elf_gregset_t), 2311 datap); 2312 break; 2313 2314 case COMPAT_PTRACE_SETREGS: 2315 ret = copy_regset_from_user(child, 2316 &user_aarch32_view, 2317 REGSET_COMPAT_GPR, 2318 0, sizeof(compat_elf_gregset_t), 2319 datap); 2320 break; 2321 2322 case COMPAT_PTRACE_GET_THREAD_AREA: 2323 ret = put_user((compat_ulong_t)child->thread.uw.tp_value, 2324 (compat_ulong_t __user *)datap); 2325 break; 2326 2327 case COMPAT_PTRACE_SET_SYSCALL: 2328 task_pt_regs(child)->syscallno = data; 2329 ret = 0; 2330 break; 2331 2332 case COMPAT_PTRACE_GETVFPREGS: 2333 ret = copy_regset_to_user(child, 2334 &user_aarch32_view, 2335 REGSET_COMPAT_VFP, 2336 0, VFP_STATE_SIZE, 2337 datap); 2338 break; 2339 2340 case COMPAT_PTRACE_SETVFPREGS: 2341 ret = copy_regset_from_user(child, 2342 &user_aarch32_view, 2343 REGSET_COMPAT_VFP, 2344 0, VFP_STATE_SIZE, 2345 datap); 2346 break; 2347 2348 #ifdef CONFIG_HAVE_HW_BREAKPOINT 2349 case COMPAT_PTRACE_GETHBPREGS: 2350 ret = compat_ptrace_gethbpregs(child, addr, datap); 2351 break; 2352 2353 case COMPAT_PTRACE_SETHBPREGS: 2354 ret = compat_ptrace_sethbpregs(child, addr, datap); 2355 break; 2356 #endif 2357 2358 default: 2359 ret = compat_ptrace_request(child, request, addr, 2360 data); 2361 break; 2362 } 2363 2364 return ret; 2365 } 2366 #endif /* CONFIG_COMPAT */ 2367 2368 const struct user_regset_view *task_user_regset_view(struct task_struct *task) 2369 { 2370 /* 2371 * Core dumping of 32-bit tasks or compat ptrace requests must use the 2372 * user_aarch32_view compatible with arm32. Native ptrace requests on 2373 * 32-bit children use an extended user_aarch32_ptrace_view to allow 2374 * access to the TLS register. 2375 */ 2376 if (is_compat_task()) 2377 return &user_aarch32_view; 2378 else if (is_compat_thread(task_thread_info(task))) 2379 return &user_aarch32_ptrace_view; 2380 2381 return &user_aarch64_view; 2382 } 2383 2384 long arch_ptrace(struct task_struct *child, long request, 2385 unsigned long addr, unsigned long data) 2386 { 2387 switch (request) { 2388 case PTRACE_PEEKMTETAGS: 2389 case PTRACE_POKEMTETAGS: 2390 return mte_ptrace_copy_tags(child, request, addr, data); 2391 } 2392 2393 return ptrace_request(child, request, addr, data); 2394 } 2395 2396 enum ptrace_syscall_dir { 2397 PTRACE_SYSCALL_ENTER = 0, 2398 PTRACE_SYSCALL_EXIT, 2399 }; 2400 2401 static __always_inline unsigned long ptrace_save_reg(struct pt_regs *regs, 2402 enum ptrace_syscall_dir dir, 2403 int *regno) 2404 { 2405 unsigned long saved_reg; 2406 2407 /* 2408 * We have some ABI weirdness here in the way that we handle syscall 2409 * exit stops because we indicate whether or not the stop has been 2410 * signalled from syscall entry or syscall exit by clobbering a general 2411 * purpose register (ip/r12 for AArch32, x7 for AArch64) in the tracee 2412 * and restoring its old value after the stop. This means that: 2413 * 2414 * - Any writes by the tracer to this register during the stop are 2415 * ignored/discarded. 2416 * 2417 * - The actual value of the register is not available during the stop, 2418 * so the tracer cannot save it and restore it later. 2419 * 2420 * - Syscall stops behave differently to seccomp and pseudo-step traps 2421 * (the latter do not nobble any registers). 2422 */ 2423 *regno = (is_compat_task() ? 12 : 7); 2424 saved_reg = regs->regs[*regno]; 2425 regs->regs[*regno] = dir; 2426 2427 return saved_reg; 2428 } 2429 2430 static int report_syscall_entry(struct pt_regs *regs) 2431 { 2432 unsigned long saved_reg; 2433 int regno, ret; 2434 2435 saved_reg = ptrace_save_reg(regs, PTRACE_SYSCALL_ENTER, ®no); 2436 ret = !ptrace_report_syscall_permit_entry(regs); 2437 if (ret) 2438 forget_syscall(regs); 2439 regs->regs[regno] = saved_reg; 2440 2441 return ret; 2442 } 2443 2444 static void report_syscall_exit(struct pt_regs *regs) 2445 { 2446 unsigned long saved_reg; 2447 int regno; 2448 2449 saved_reg = ptrace_save_reg(regs, PTRACE_SYSCALL_EXIT, ®no); 2450 if (!test_thread_flag(TIF_SINGLESTEP)) { 2451 ptrace_report_syscall_exit(regs, 0); 2452 regs->regs[regno] = saved_reg; 2453 } else { 2454 regs->regs[regno] = saved_reg; 2455 2456 /* 2457 * Signal a pseudo-step exception since we are stepping but 2458 * tracer modifications to the registers may have rewound the 2459 * state machine. 2460 */ 2461 ptrace_report_syscall_exit(regs, 1); 2462 } 2463 } 2464 2465 int syscall_trace_enter(struct pt_regs *regs) 2466 { 2467 unsigned long flags = read_thread_flags(); 2468 int ret; 2469 2470 if (flags & (_TIF_SYSCALL_EMU | _TIF_SYSCALL_TRACE)) { 2471 ret = report_syscall_entry(regs); 2472 if (ret || (flags & _TIF_SYSCALL_EMU)) 2473 return NO_SYSCALL; 2474 } 2475 2476 /* Do the secure computing after ptrace; failures should be fast. */ 2477 if (!seccomp_permit_syscall()) 2478 return NO_SYSCALL; 2479 2480 if (test_thread_flag(TIF_SYSCALL_TRACEPOINT)) 2481 trace_sys_enter(regs, regs->syscallno); 2482 2483 audit_syscall_entry(regs->syscallno, regs->orig_x0, regs->regs[1], 2484 regs->regs[2], regs->regs[3]); 2485 2486 return regs->syscallno; 2487 } 2488 2489 void syscall_trace_exit(struct pt_regs *regs) 2490 { 2491 unsigned long flags = read_thread_flags(); 2492 2493 audit_syscall_exit(regs); 2494 2495 if (flags & _TIF_SYSCALL_TRACEPOINT) 2496 trace_sys_exit(regs, syscall_get_return_value(current, regs)); 2497 2498 if (flags & (_TIF_SYSCALL_TRACE | _TIF_SINGLESTEP)) 2499 report_syscall_exit(regs); 2500 2501 rseq_syscall(regs); 2502 } 2503 2504 /* 2505 * SPSR_ELx bits which are always architecturally RES0 per ARM DDI 0487D.a. 2506 * We permit userspace to set SSBS (AArch64 bit 12, AArch32 bit 23) which is 2507 * not described in ARM DDI 0487D.a. 2508 * We treat PAN and UAO as RES0 bits, as they are meaningless at EL0, and may 2509 * be allocated an EL0 meaning in future. 2510 * Userspace cannot use these until they have an architectural meaning. 2511 * Note that this follows the SPSR_ELx format, not the AArch32 PSR format. 2512 * We also reserve IL for the kernel; SS is handled dynamically. 2513 */ 2514 #define SPSR_EL1_AARCH64_RES0_BITS \ 2515 (GENMASK_ULL(63, 32) | GENMASK_ULL(27, 26) | GENMASK_ULL(23, 22) | \ 2516 GENMASK_ULL(20, 13) | GENMASK_ULL(5, 5)) 2517 #define SPSR_EL1_AARCH32_RES0_BITS \ 2518 (GENMASK_ULL(63, 32) | GENMASK_ULL(22, 22) | GENMASK_ULL(20, 20)) 2519 2520 static int valid_compat_regs(struct user_pt_regs *regs) 2521 { 2522 regs->pstate &= ~SPSR_EL1_AARCH32_RES0_BITS; 2523 2524 if (!system_supports_mixed_endian_el0()) 2525 regs->pstate &= ~PSR_AA32_E_BIT; 2526 2527 if (user_mode(regs) && (regs->pstate & PSR_MODE32_BIT) && 2528 (regs->pstate & PSR_AA32_A_BIT) == 0 && 2529 (regs->pstate & PSR_AA32_I_BIT) == 0 && 2530 (regs->pstate & PSR_AA32_F_BIT) == 0) { 2531 return 1; 2532 } 2533 2534 /* 2535 * Force PSR to a valid 32-bit EL0t, preserving the same bits as 2536 * arch/arm. 2537 */ 2538 regs->pstate &= PSR_AA32_N_BIT | PSR_AA32_Z_BIT | 2539 PSR_AA32_C_BIT | PSR_AA32_V_BIT | 2540 PSR_AA32_Q_BIT | PSR_AA32_IT_MASK | 2541 PSR_AA32_GE_MASK | PSR_AA32_E_BIT | 2542 PSR_AA32_T_BIT; 2543 regs->pstate |= PSR_MODE32_BIT; 2544 2545 return 0; 2546 } 2547 2548 static int valid_native_regs(struct user_pt_regs *regs) 2549 { 2550 regs->pstate &= ~SPSR_EL1_AARCH64_RES0_BITS; 2551 2552 if (user_mode(regs) && !(regs->pstate & PSR_MODE32_BIT) && 2553 (regs->pstate & PSR_D_BIT) == 0 && 2554 (regs->pstate & PSR_A_BIT) == 0 && 2555 (regs->pstate & PSR_I_BIT) == 0 && 2556 (regs->pstate & PSR_F_BIT) == 0) { 2557 return 1; 2558 } 2559 2560 /* Force PSR to a valid 64-bit EL0t */ 2561 regs->pstate &= PSR_N_BIT | PSR_Z_BIT | PSR_C_BIT | PSR_V_BIT; 2562 2563 return 0; 2564 } 2565 2566 /* 2567 * Are the current registers suitable for user mode? (used to maintain 2568 * security in signal handlers) 2569 */ 2570 int valid_user_regs(struct user_pt_regs *regs, struct task_struct *task) 2571 { 2572 /* https://lore.kernel.org/lkml/20191118131525.GA4180@willie-the-truck */ 2573 user_regs_reset_single_step(regs, task); 2574 2575 if (is_compat_thread(task_thread_info(task))) 2576 return valid_compat_regs(regs); 2577 else 2578 return valid_native_regs(regs); 2579 } 2580