1 // SPDX-License-Identifier: GPL-2.0-only 2 /* By Ross Biro 1/23/92 */ 3 /* 4 * Pentium III FXSR, SSE support 5 * Gareth Hughes <gareth@valinux.com>, May 2000 6 */ 7 8 #include <linux/kernel.h> 9 #include <linux/sched.h> 10 #include <linux/sched/task_stack.h> 11 #include <linux/mm.h> 12 #include <linux/smp.h> 13 #include <linux/errno.h> 14 #include <linux/slab.h> 15 #include <linux/ptrace.h> 16 #include <linux/tracehook.h> 17 #include <linux/user.h> 18 #include <linux/elf.h> 19 #include <linux/security.h> 20 #include <linux/audit.h> 21 #include <linux/seccomp.h> 22 #include <linux/signal.h> 23 #include <linux/perf_event.h> 24 #include <linux/hw_breakpoint.h> 25 #include <linux/rcupdate.h> 26 #include <linux/export.h> 27 #include <linux/context_tracking.h> 28 #include <linux/nospec.h> 29 30 #include <linux/uaccess.h> 31 #include <asm/processor.h> 32 #include <asm/fpu/internal.h> 33 #include <asm/fpu/signal.h> 34 #include <asm/fpu/regset.h> 35 #include <asm/debugreg.h> 36 #include <asm/ldt.h> 37 #include <asm/desc.h> 38 #include <asm/prctl.h> 39 #include <asm/proto.h> 40 #include <asm/hw_breakpoint.h> 41 #include <asm/traps.h> 42 #include <asm/syscall.h> 43 #include <asm/fsgsbase.h> 44 #include <asm/io_bitmap.h> 45 46 #include "tls.h" 47 48 enum x86_regset { 49 REGSET_GENERAL, 50 REGSET_FP, 51 REGSET_XFP, 52 REGSET_IOPERM64 = REGSET_XFP, 53 REGSET_XSTATE, 54 REGSET_TLS, 55 REGSET_IOPERM32, 56 }; 57 58 struct pt_regs_offset { 59 const char *name; 60 int offset; 61 }; 62 63 #define REG_OFFSET_NAME(r) {.name = #r, .offset = offsetof(struct pt_regs, r)} 64 #define REG_OFFSET_END {.name = NULL, .offset = 0} 65 66 static const struct pt_regs_offset regoffset_table[] = { 67 #ifdef CONFIG_X86_64 68 REG_OFFSET_NAME(r15), 69 REG_OFFSET_NAME(r14), 70 REG_OFFSET_NAME(r13), 71 REG_OFFSET_NAME(r12), 72 REG_OFFSET_NAME(r11), 73 REG_OFFSET_NAME(r10), 74 REG_OFFSET_NAME(r9), 75 REG_OFFSET_NAME(r8), 76 #endif 77 REG_OFFSET_NAME(bx), 78 REG_OFFSET_NAME(cx), 79 REG_OFFSET_NAME(dx), 80 REG_OFFSET_NAME(si), 81 REG_OFFSET_NAME(di), 82 REG_OFFSET_NAME(bp), 83 REG_OFFSET_NAME(ax), 84 #ifdef CONFIG_X86_32 85 REG_OFFSET_NAME(ds), 86 REG_OFFSET_NAME(es), 87 REG_OFFSET_NAME(fs), 88 REG_OFFSET_NAME(gs), 89 #endif 90 REG_OFFSET_NAME(orig_ax), 91 REG_OFFSET_NAME(ip), 92 REG_OFFSET_NAME(cs), 93 REG_OFFSET_NAME(flags), 94 REG_OFFSET_NAME(sp), 95 REG_OFFSET_NAME(ss), 96 REG_OFFSET_END, 97 }; 98 99 /** 100 * regs_query_register_offset() - query register offset from its name 101 * @name: the name of a register 102 * 103 * regs_query_register_offset() returns the offset of a register in struct 104 * pt_regs from its name. If the name is invalid, this returns -EINVAL; 105 */ 106 int regs_query_register_offset(const char *name) 107 { 108 const struct pt_regs_offset *roff; 109 for (roff = regoffset_table; roff->name != NULL; roff++) 110 if (!strcmp(roff->name, name)) 111 return roff->offset; 112 return -EINVAL; 113 } 114 115 /** 116 * regs_query_register_name() - query register name from its offset 117 * @offset: the offset of a register in struct pt_regs. 118 * 119 * regs_query_register_name() returns the name of a register from its 120 * offset in struct pt_regs. If the @offset is invalid, this returns NULL; 121 */ 122 const char *regs_query_register_name(unsigned int offset) 123 { 124 const struct pt_regs_offset *roff; 125 for (roff = regoffset_table; roff->name != NULL; roff++) 126 if (roff->offset == offset) 127 return roff->name; 128 return NULL; 129 } 130 131 /* 132 * does not yet catch signals sent when the child dies. 133 * in exit.c or in signal.c. 134 */ 135 136 /* 137 * Determines which flags the user has access to [1 = access, 0 = no access]. 138 */ 139 #define FLAG_MASK_32 ((unsigned long) \ 140 (X86_EFLAGS_CF | X86_EFLAGS_PF | \ 141 X86_EFLAGS_AF | X86_EFLAGS_ZF | \ 142 X86_EFLAGS_SF | X86_EFLAGS_TF | \ 143 X86_EFLAGS_DF | X86_EFLAGS_OF | \ 144 X86_EFLAGS_RF | X86_EFLAGS_AC)) 145 146 /* 147 * Determines whether a value may be installed in a segment register. 148 */ 149 static inline bool invalid_selector(u16 value) 150 { 151 return unlikely(value != 0 && (value & SEGMENT_RPL_MASK) != USER_RPL); 152 } 153 154 #ifdef CONFIG_X86_32 155 156 #define FLAG_MASK FLAG_MASK_32 157 158 static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long regno) 159 { 160 BUILD_BUG_ON(offsetof(struct pt_regs, bx) != 0); 161 return ®s->bx + (regno >> 2); 162 } 163 164 static u16 get_segment_reg(struct task_struct *task, unsigned long offset) 165 { 166 /* 167 * Returning the value truncates it to 16 bits. 168 */ 169 unsigned int retval; 170 if (offset != offsetof(struct user_regs_struct, gs)) 171 retval = *pt_regs_access(task_pt_regs(task), offset); 172 else { 173 if (task == current) 174 retval = get_user_gs(task_pt_regs(task)); 175 else 176 retval = task_user_gs(task); 177 } 178 return retval; 179 } 180 181 static int set_segment_reg(struct task_struct *task, 182 unsigned long offset, u16 value) 183 { 184 if (WARN_ON_ONCE(task == current)) 185 return -EIO; 186 187 /* 188 * The value argument was already truncated to 16 bits. 189 */ 190 if (invalid_selector(value)) 191 return -EIO; 192 193 /* 194 * For %cs and %ss we cannot permit a null selector. 195 * We can permit a bogus selector as long as it has USER_RPL. 196 * Null selectors are fine for other segment registers, but 197 * we will never get back to user mode with invalid %cs or %ss 198 * and will take the trap in iret instead. Much code relies 199 * on user_mode() to distinguish a user trap frame (which can 200 * safely use invalid selectors) from a kernel trap frame. 201 */ 202 switch (offset) { 203 case offsetof(struct user_regs_struct, cs): 204 case offsetof(struct user_regs_struct, ss): 205 if (unlikely(value == 0)) 206 return -EIO; 207 /* Else, fall through */ 208 209 default: 210 *pt_regs_access(task_pt_regs(task), offset) = value; 211 break; 212 213 case offsetof(struct user_regs_struct, gs): 214 task_user_gs(task) = value; 215 } 216 217 return 0; 218 } 219 220 #else /* CONFIG_X86_64 */ 221 222 #define FLAG_MASK (FLAG_MASK_32 | X86_EFLAGS_NT) 223 224 static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long offset) 225 { 226 BUILD_BUG_ON(offsetof(struct pt_regs, r15) != 0); 227 return ®s->r15 + (offset / sizeof(regs->r15)); 228 } 229 230 static u16 get_segment_reg(struct task_struct *task, unsigned long offset) 231 { 232 /* 233 * Returning the value truncates it to 16 bits. 234 */ 235 unsigned int seg; 236 237 switch (offset) { 238 case offsetof(struct user_regs_struct, fs): 239 if (task == current) { 240 /* Older gas can't assemble movq %?s,%r?? */ 241 asm("movl %%fs,%0" : "=r" (seg)); 242 return seg; 243 } 244 return task->thread.fsindex; 245 case offsetof(struct user_regs_struct, gs): 246 if (task == current) { 247 asm("movl %%gs,%0" : "=r" (seg)); 248 return seg; 249 } 250 return task->thread.gsindex; 251 case offsetof(struct user_regs_struct, ds): 252 if (task == current) { 253 asm("movl %%ds,%0" : "=r" (seg)); 254 return seg; 255 } 256 return task->thread.ds; 257 case offsetof(struct user_regs_struct, es): 258 if (task == current) { 259 asm("movl %%es,%0" : "=r" (seg)); 260 return seg; 261 } 262 return task->thread.es; 263 264 case offsetof(struct user_regs_struct, cs): 265 case offsetof(struct user_regs_struct, ss): 266 break; 267 } 268 return *pt_regs_access(task_pt_regs(task), offset); 269 } 270 271 static int set_segment_reg(struct task_struct *task, 272 unsigned long offset, u16 value) 273 { 274 if (WARN_ON_ONCE(task == current)) 275 return -EIO; 276 277 /* 278 * The value argument was already truncated to 16 bits. 279 */ 280 if (invalid_selector(value)) 281 return -EIO; 282 283 /* 284 * Writes to FS and GS will change the stored selector. Whether 285 * this changes the segment base as well depends on whether 286 * FSGSBASE is enabled. 287 */ 288 289 switch (offset) { 290 case offsetof(struct user_regs_struct,fs): 291 task->thread.fsindex = value; 292 break; 293 case offsetof(struct user_regs_struct,gs): 294 task->thread.gsindex = value; 295 break; 296 case offsetof(struct user_regs_struct,ds): 297 task->thread.ds = value; 298 break; 299 case offsetof(struct user_regs_struct,es): 300 task->thread.es = value; 301 break; 302 303 /* 304 * Can't actually change these in 64-bit mode. 305 */ 306 case offsetof(struct user_regs_struct,cs): 307 if (unlikely(value == 0)) 308 return -EIO; 309 task_pt_regs(task)->cs = value; 310 break; 311 case offsetof(struct user_regs_struct,ss): 312 if (unlikely(value == 0)) 313 return -EIO; 314 task_pt_regs(task)->ss = value; 315 break; 316 } 317 318 return 0; 319 } 320 321 #endif /* CONFIG_X86_32 */ 322 323 static unsigned long get_flags(struct task_struct *task) 324 { 325 unsigned long retval = task_pt_regs(task)->flags; 326 327 /* 328 * If the debugger set TF, hide it from the readout. 329 */ 330 if (test_tsk_thread_flag(task, TIF_FORCED_TF)) 331 retval &= ~X86_EFLAGS_TF; 332 333 return retval; 334 } 335 336 static int set_flags(struct task_struct *task, unsigned long value) 337 { 338 struct pt_regs *regs = task_pt_regs(task); 339 340 /* 341 * If the user value contains TF, mark that 342 * it was not "us" (the debugger) that set it. 343 * If not, make sure it stays set if we had. 344 */ 345 if (value & X86_EFLAGS_TF) 346 clear_tsk_thread_flag(task, TIF_FORCED_TF); 347 else if (test_tsk_thread_flag(task, TIF_FORCED_TF)) 348 value |= X86_EFLAGS_TF; 349 350 regs->flags = (regs->flags & ~FLAG_MASK) | (value & FLAG_MASK); 351 352 return 0; 353 } 354 355 static int putreg(struct task_struct *child, 356 unsigned long offset, unsigned long value) 357 { 358 switch (offset) { 359 case offsetof(struct user_regs_struct, cs): 360 case offsetof(struct user_regs_struct, ds): 361 case offsetof(struct user_regs_struct, es): 362 case offsetof(struct user_regs_struct, fs): 363 case offsetof(struct user_regs_struct, gs): 364 case offsetof(struct user_regs_struct, ss): 365 return set_segment_reg(child, offset, value); 366 367 case offsetof(struct user_regs_struct, flags): 368 return set_flags(child, value); 369 370 #ifdef CONFIG_X86_64 371 case offsetof(struct user_regs_struct,fs_base): 372 if (value >= TASK_SIZE_MAX) 373 return -EIO; 374 x86_fsbase_write_task(child, value); 375 return 0; 376 case offsetof(struct user_regs_struct,gs_base): 377 if (value >= TASK_SIZE_MAX) 378 return -EIO; 379 x86_gsbase_write_task(child, value); 380 return 0; 381 #endif 382 } 383 384 *pt_regs_access(task_pt_regs(child), offset) = value; 385 return 0; 386 } 387 388 static unsigned long getreg(struct task_struct *task, unsigned long offset) 389 { 390 switch (offset) { 391 case offsetof(struct user_regs_struct, cs): 392 case offsetof(struct user_regs_struct, ds): 393 case offsetof(struct user_regs_struct, es): 394 case offsetof(struct user_regs_struct, fs): 395 case offsetof(struct user_regs_struct, gs): 396 case offsetof(struct user_regs_struct, ss): 397 return get_segment_reg(task, offset); 398 399 case offsetof(struct user_regs_struct, flags): 400 return get_flags(task); 401 402 #ifdef CONFIG_X86_64 403 case offsetof(struct user_regs_struct, fs_base): 404 return x86_fsbase_read_task(task); 405 case offsetof(struct user_regs_struct, gs_base): 406 return x86_gsbase_read_task(task); 407 #endif 408 } 409 410 return *pt_regs_access(task_pt_regs(task), offset); 411 } 412 413 static int genregs_get(struct task_struct *target, 414 const struct user_regset *regset, 415 unsigned int pos, unsigned int count, 416 void *kbuf, void __user *ubuf) 417 { 418 if (kbuf) { 419 unsigned long *k = kbuf; 420 while (count >= sizeof(*k)) { 421 *k++ = getreg(target, pos); 422 count -= sizeof(*k); 423 pos += sizeof(*k); 424 } 425 } else { 426 unsigned long __user *u = ubuf; 427 while (count >= sizeof(*u)) { 428 if (__put_user(getreg(target, pos), u++)) 429 return -EFAULT; 430 count -= sizeof(*u); 431 pos += sizeof(*u); 432 } 433 } 434 435 return 0; 436 } 437 438 static int genregs_set(struct task_struct *target, 439 const struct user_regset *regset, 440 unsigned int pos, unsigned int count, 441 const void *kbuf, const void __user *ubuf) 442 { 443 int ret = 0; 444 if (kbuf) { 445 const unsigned long *k = kbuf; 446 while (count >= sizeof(*k) && !ret) { 447 ret = putreg(target, pos, *k++); 448 count -= sizeof(*k); 449 pos += sizeof(*k); 450 } 451 } else { 452 const unsigned long __user *u = ubuf; 453 while (count >= sizeof(*u) && !ret) { 454 unsigned long word; 455 ret = __get_user(word, u++); 456 if (ret) 457 break; 458 ret = putreg(target, pos, word); 459 count -= sizeof(*u); 460 pos += sizeof(*u); 461 } 462 } 463 return ret; 464 } 465 466 static void ptrace_triggered(struct perf_event *bp, 467 struct perf_sample_data *data, 468 struct pt_regs *regs) 469 { 470 int i; 471 struct thread_struct *thread = &(current->thread); 472 473 /* 474 * Store in the virtual DR6 register the fact that the breakpoint 475 * was hit so the thread's debugger will see it. 476 */ 477 for (i = 0; i < HBP_NUM; i++) { 478 if (thread->ptrace_bps[i] == bp) 479 break; 480 } 481 482 thread->debugreg6 |= (DR_TRAP0 << i); 483 } 484 485 /* 486 * Walk through every ptrace breakpoints for this thread and 487 * build the dr7 value on top of their attributes. 488 * 489 */ 490 static unsigned long ptrace_get_dr7(struct perf_event *bp[]) 491 { 492 int i; 493 int dr7 = 0; 494 struct arch_hw_breakpoint *info; 495 496 for (i = 0; i < HBP_NUM; i++) { 497 if (bp[i] && !bp[i]->attr.disabled) { 498 info = counter_arch_bp(bp[i]); 499 dr7 |= encode_dr7(i, info->len, info->type); 500 } 501 } 502 503 return dr7; 504 } 505 506 static int ptrace_fill_bp_fields(struct perf_event_attr *attr, 507 int len, int type, bool disabled) 508 { 509 int err, bp_len, bp_type; 510 511 err = arch_bp_generic_fields(len, type, &bp_len, &bp_type); 512 if (!err) { 513 attr->bp_len = bp_len; 514 attr->bp_type = bp_type; 515 attr->disabled = disabled; 516 } 517 518 return err; 519 } 520 521 static struct perf_event * 522 ptrace_register_breakpoint(struct task_struct *tsk, int len, int type, 523 unsigned long addr, bool disabled) 524 { 525 struct perf_event_attr attr; 526 int err; 527 528 ptrace_breakpoint_init(&attr); 529 attr.bp_addr = addr; 530 531 err = ptrace_fill_bp_fields(&attr, len, type, disabled); 532 if (err) 533 return ERR_PTR(err); 534 535 return register_user_hw_breakpoint(&attr, ptrace_triggered, 536 NULL, tsk); 537 } 538 539 static int ptrace_modify_breakpoint(struct perf_event *bp, int len, int type, 540 int disabled) 541 { 542 struct perf_event_attr attr = bp->attr; 543 int err; 544 545 err = ptrace_fill_bp_fields(&attr, len, type, disabled); 546 if (err) 547 return err; 548 549 return modify_user_hw_breakpoint(bp, &attr); 550 } 551 552 /* 553 * Handle ptrace writes to debug register 7. 554 */ 555 static int ptrace_write_dr7(struct task_struct *tsk, unsigned long data) 556 { 557 struct thread_struct *thread = &tsk->thread; 558 unsigned long old_dr7; 559 bool second_pass = false; 560 int i, rc, ret = 0; 561 562 data &= ~DR_CONTROL_RESERVED; 563 old_dr7 = ptrace_get_dr7(thread->ptrace_bps); 564 565 restore: 566 rc = 0; 567 for (i = 0; i < HBP_NUM; i++) { 568 unsigned len, type; 569 bool disabled = !decode_dr7(data, i, &len, &type); 570 struct perf_event *bp = thread->ptrace_bps[i]; 571 572 if (!bp) { 573 if (disabled) 574 continue; 575 576 bp = ptrace_register_breakpoint(tsk, 577 len, type, 0, disabled); 578 if (IS_ERR(bp)) { 579 rc = PTR_ERR(bp); 580 break; 581 } 582 583 thread->ptrace_bps[i] = bp; 584 continue; 585 } 586 587 rc = ptrace_modify_breakpoint(bp, len, type, disabled); 588 if (rc) 589 break; 590 } 591 592 /* Restore if the first pass failed, second_pass shouldn't fail. */ 593 if (rc && !WARN_ON(second_pass)) { 594 ret = rc; 595 data = old_dr7; 596 second_pass = true; 597 goto restore; 598 } 599 600 return ret; 601 } 602 603 /* 604 * Handle PTRACE_PEEKUSR calls for the debug register area. 605 */ 606 static unsigned long ptrace_get_debugreg(struct task_struct *tsk, int n) 607 { 608 struct thread_struct *thread = &tsk->thread; 609 unsigned long val = 0; 610 611 if (n < HBP_NUM) { 612 int index = array_index_nospec(n, HBP_NUM); 613 struct perf_event *bp = thread->ptrace_bps[index]; 614 615 if (bp) 616 val = bp->hw.info.address; 617 } else if (n == 6) { 618 val = thread->debugreg6; 619 } else if (n == 7) { 620 val = thread->ptrace_dr7; 621 } 622 return val; 623 } 624 625 static int ptrace_set_breakpoint_addr(struct task_struct *tsk, int nr, 626 unsigned long addr) 627 { 628 struct thread_struct *t = &tsk->thread; 629 struct perf_event *bp = t->ptrace_bps[nr]; 630 int err = 0; 631 632 if (!bp) { 633 /* 634 * Put stub len and type to create an inactive but correct bp. 635 * 636 * CHECKME: the previous code returned -EIO if the addr wasn't 637 * a valid task virtual addr. The new one will return -EINVAL in 638 * this case. 639 * -EINVAL may be what we want for in-kernel breakpoints users, 640 * but -EIO looks better for ptrace, since we refuse a register 641 * writing for the user. And anyway this is the previous 642 * behaviour. 643 */ 644 bp = ptrace_register_breakpoint(tsk, 645 X86_BREAKPOINT_LEN_1, X86_BREAKPOINT_WRITE, 646 addr, true); 647 if (IS_ERR(bp)) 648 err = PTR_ERR(bp); 649 else 650 t->ptrace_bps[nr] = bp; 651 } else { 652 struct perf_event_attr attr = bp->attr; 653 654 attr.bp_addr = addr; 655 err = modify_user_hw_breakpoint(bp, &attr); 656 } 657 658 return err; 659 } 660 661 /* 662 * Handle PTRACE_POKEUSR calls for the debug register area. 663 */ 664 static int ptrace_set_debugreg(struct task_struct *tsk, int n, 665 unsigned long val) 666 { 667 struct thread_struct *thread = &tsk->thread; 668 /* There are no DR4 or DR5 registers */ 669 int rc = -EIO; 670 671 if (n < HBP_NUM) { 672 rc = ptrace_set_breakpoint_addr(tsk, n, val); 673 } else if (n == 6) { 674 thread->debugreg6 = val; 675 rc = 0; 676 } else if (n == 7) { 677 rc = ptrace_write_dr7(tsk, val); 678 if (!rc) 679 thread->ptrace_dr7 = val; 680 } 681 return rc; 682 } 683 684 /* 685 * These access the current or another (stopped) task's io permission 686 * bitmap for debugging or core dump. 687 */ 688 static int ioperm_active(struct task_struct *target, 689 const struct user_regset *regset) 690 { 691 struct io_bitmap *iobm = target->thread.io_bitmap; 692 693 return iobm ? DIV_ROUND_UP(iobm->max, regset->size) : 0; 694 } 695 696 static int ioperm_get(struct task_struct *target, 697 const struct user_regset *regset, 698 unsigned int pos, unsigned int count, 699 void *kbuf, void __user *ubuf) 700 { 701 struct io_bitmap *iobm = target->thread.io_bitmap; 702 703 if (!iobm) 704 return -ENXIO; 705 706 return user_regset_copyout(&pos, &count, &kbuf, &ubuf, 707 iobm->bitmap, 0, IO_BITMAP_BYTES); 708 } 709 710 /* 711 * Called by kernel/ptrace.c when detaching.. 712 * 713 * Make sure the single step bit is not set. 714 */ 715 void ptrace_disable(struct task_struct *child) 716 { 717 user_disable_single_step(child); 718 } 719 720 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION 721 static const struct user_regset_view user_x86_32_view; /* Initialized below. */ 722 #endif 723 724 long arch_ptrace(struct task_struct *child, long request, 725 unsigned long addr, unsigned long data) 726 { 727 int ret; 728 unsigned long __user *datap = (unsigned long __user *)data; 729 730 switch (request) { 731 /* read the word at location addr in the USER area. */ 732 case PTRACE_PEEKUSR: { 733 unsigned long tmp; 734 735 ret = -EIO; 736 if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user)) 737 break; 738 739 tmp = 0; /* Default return condition */ 740 if (addr < sizeof(struct user_regs_struct)) 741 tmp = getreg(child, addr); 742 else if (addr >= offsetof(struct user, u_debugreg[0]) && 743 addr <= offsetof(struct user, u_debugreg[7])) { 744 addr -= offsetof(struct user, u_debugreg[0]); 745 tmp = ptrace_get_debugreg(child, addr / sizeof(data)); 746 } 747 ret = put_user(tmp, datap); 748 break; 749 } 750 751 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */ 752 ret = -EIO; 753 if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user)) 754 break; 755 756 if (addr < sizeof(struct user_regs_struct)) 757 ret = putreg(child, addr, data); 758 else if (addr >= offsetof(struct user, u_debugreg[0]) && 759 addr <= offsetof(struct user, u_debugreg[7])) { 760 addr -= offsetof(struct user, u_debugreg[0]); 761 ret = ptrace_set_debugreg(child, 762 addr / sizeof(data), data); 763 } 764 break; 765 766 case PTRACE_GETREGS: /* Get all gp regs from the child. */ 767 return copy_regset_to_user(child, 768 task_user_regset_view(current), 769 REGSET_GENERAL, 770 0, sizeof(struct user_regs_struct), 771 datap); 772 773 case PTRACE_SETREGS: /* Set all gp regs in the child. */ 774 return copy_regset_from_user(child, 775 task_user_regset_view(current), 776 REGSET_GENERAL, 777 0, sizeof(struct user_regs_struct), 778 datap); 779 780 case PTRACE_GETFPREGS: /* Get the child FPU state. */ 781 return copy_regset_to_user(child, 782 task_user_regset_view(current), 783 REGSET_FP, 784 0, sizeof(struct user_i387_struct), 785 datap); 786 787 case PTRACE_SETFPREGS: /* Set the child FPU state. */ 788 return copy_regset_from_user(child, 789 task_user_regset_view(current), 790 REGSET_FP, 791 0, sizeof(struct user_i387_struct), 792 datap); 793 794 #ifdef CONFIG_X86_32 795 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */ 796 return copy_regset_to_user(child, &user_x86_32_view, 797 REGSET_XFP, 798 0, sizeof(struct user_fxsr_struct), 799 datap) ? -EIO : 0; 800 801 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */ 802 return copy_regset_from_user(child, &user_x86_32_view, 803 REGSET_XFP, 804 0, sizeof(struct user_fxsr_struct), 805 datap) ? -EIO : 0; 806 #endif 807 808 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION 809 case PTRACE_GET_THREAD_AREA: 810 if ((int) addr < 0) 811 return -EIO; 812 ret = do_get_thread_area(child, addr, 813 (struct user_desc __user *)data); 814 break; 815 816 case PTRACE_SET_THREAD_AREA: 817 if ((int) addr < 0) 818 return -EIO; 819 ret = do_set_thread_area(child, addr, 820 (struct user_desc __user *)data, 0); 821 break; 822 #endif 823 824 #ifdef CONFIG_X86_64 825 /* normal 64bit interface to access TLS data. 826 Works just like arch_prctl, except that the arguments 827 are reversed. */ 828 case PTRACE_ARCH_PRCTL: 829 ret = do_arch_prctl_64(child, data, addr); 830 break; 831 #endif 832 833 default: 834 ret = ptrace_request(child, request, addr, data); 835 break; 836 } 837 838 return ret; 839 } 840 841 #ifdef CONFIG_IA32_EMULATION 842 843 #include <linux/compat.h> 844 #include <linux/syscalls.h> 845 #include <asm/ia32.h> 846 #include <asm/user32.h> 847 848 #define R32(l,q) \ 849 case offsetof(struct user32, regs.l): \ 850 regs->q = value; break 851 852 #define SEG32(rs) \ 853 case offsetof(struct user32, regs.rs): \ 854 return set_segment_reg(child, \ 855 offsetof(struct user_regs_struct, rs), \ 856 value); \ 857 break 858 859 static int putreg32(struct task_struct *child, unsigned regno, u32 value) 860 { 861 struct pt_regs *regs = task_pt_regs(child); 862 int ret; 863 864 switch (regno) { 865 866 SEG32(cs); 867 SEG32(ds); 868 SEG32(es); 869 870 /* 871 * A 32-bit ptracer on a 64-bit kernel expects that writing 872 * FS or GS will also update the base. This is needed for 873 * operations like PTRACE_SETREGS to fully restore a saved 874 * CPU state. 875 */ 876 877 case offsetof(struct user32, regs.fs): 878 ret = set_segment_reg(child, 879 offsetof(struct user_regs_struct, fs), 880 value); 881 if (ret == 0) 882 child->thread.fsbase = 883 x86_fsgsbase_read_task(child, value); 884 return ret; 885 886 case offsetof(struct user32, regs.gs): 887 ret = set_segment_reg(child, 888 offsetof(struct user_regs_struct, gs), 889 value); 890 if (ret == 0) 891 child->thread.gsbase = 892 x86_fsgsbase_read_task(child, value); 893 return ret; 894 895 SEG32(ss); 896 897 R32(ebx, bx); 898 R32(ecx, cx); 899 R32(edx, dx); 900 R32(edi, di); 901 R32(esi, si); 902 R32(ebp, bp); 903 R32(eax, ax); 904 R32(eip, ip); 905 R32(esp, sp); 906 907 case offsetof(struct user32, regs.orig_eax): 908 /* 909 * Warning: bizarre corner case fixup here. A 32-bit 910 * debugger setting orig_eax to -1 wants to disable 911 * syscall restart. Make sure that the syscall 912 * restart code sign-extends orig_ax. Also make sure 913 * we interpret the -ERESTART* codes correctly if 914 * loaded into regs->ax in case the task is not 915 * actually still sitting at the exit from a 32-bit 916 * syscall with TS_COMPAT still set. 917 */ 918 regs->orig_ax = value; 919 if (syscall_get_nr(child, regs) >= 0) 920 child->thread_info.status |= TS_I386_REGS_POKED; 921 break; 922 923 case offsetof(struct user32, regs.eflags): 924 return set_flags(child, value); 925 926 case offsetof(struct user32, u_debugreg[0]) ... 927 offsetof(struct user32, u_debugreg[7]): 928 regno -= offsetof(struct user32, u_debugreg[0]); 929 return ptrace_set_debugreg(child, regno / 4, value); 930 931 default: 932 if (regno > sizeof(struct user32) || (regno & 3)) 933 return -EIO; 934 935 /* 936 * Other dummy fields in the virtual user structure 937 * are ignored 938 */ 939 break; 940 } 941 return 0; 942 } 943 944 #undef R32 945 #undef SEG32 946 947 #define R32(l,q) \ 948 case offsetof(struct user32, regs.l): \ 949 *val = regs->q; break 950 951 #define SEG32(rs) \ 952 case offsetof(struct user32, regs.rs): \ 953 *val = get_segment_reg(child, \ 954 offsetof(struct user_regs_struct, rs)); \ 955 break 956 957 static int getreg32(struct task_struct *child, unsigned regno, u32 *val) 958 { 959 struct pt_regs *regs = task_pt_regs(child); 960 961 switch (regno) { 962 963 SEG32(ds); 964 SEG32(es); 965 SEG32(fs); 966 SEG32(gs); 967 968 R32(cs, cs); 969 R32(ss, ss); 970 R32(ebx, bx); 971 R32(ecx, cx); 972 R32(edx, dx); 973 R32(edi, di); 974 R32(esi, si); 975 R32(ebp, bp); 976 R32(eax, ax); 977 R32(orig_eax, orig_ax); 978 R32(eip, ip); 979 R32(esp, sp); 980 981 case offsetof(struct user32, regs.eflags): 982 *val = get_flags(child); 983 break; 984 985 case offsetof(struct user32, u_debugreg[0]) ... 986 offsetof(struct user32, u_debugreg[7]): 987 regno -= offsetof(struct user32, u_debugreg[0]); 988 *val = ptrace_get_debugreg(child, regno / 4); 989 break; 990 991 default: 992 if (regno > sizeof(struct user32) || (regno & 3)) 993 return -EIO; 994 995 /* 996 * Other dummy fields in the virtual user structure 997 * are ignored 998 */ 999 *val = 0; 1000 break; 1001 } 1002 return 0; 1003 } 1004 1005 #undef R32 1006 #undef SEG32 1007 1008 static int genregs32_get(struct task_struct *target, 1009 const struct user_regset *regset, 1010 unsigned int pos, unsigned int count, 1011 void *kbuf, void __user *ubuf) 1012 { 1013 if (kbuf) { 1014 compat_ulong_t *k = kbuf; 1015 while (count >= sizeof(*k)) { 1016 getreg32(target, pos, k++); 1017 count -= sizeof(*k); 1018 pos += sizeof(*k); 1019 } 1020 } else { 1021 compat_ulong_t __user *u = ubuf; 1022 while (count >= sizeof(*u)) { 1023 compat_ulong_t word; 1024 getreg32(target, pos, &word); 1025 if (__put_user(word, u++)) 1026 return -EFAULT; 1027 count -= sizeof(*u); 1028 pos += sizeof(*u); 1029 } 1030 } 1031 1032 return 0; 1033 } 1034 1035 static int genregs32_set(struct task_struct *target, 1036 const struct user_regset *regset, 1037 unsigned int pos, unsigned int count, 1038 const void *kbuf, const void __user *ubuf) 1039 { 1040 int ret = 0; 1041 if (kbuf) { 1042 const compat_ulong_t *k = kbuf; 1043 while (count >= sizeof(*k) && !ret) { 1044 ret = putreg32(target, pos, *k++); 1045 count -= sizeof(*k); 1046 pos += sizeof(*k); 1047 } 1048 } else { 1049 const compat_ulong_t __user *u = ubuf; 1050 while (count >= sizeof(*u) && !ret) { 1051 compat_ulong_t word; 1052 ret = __get_user(word, u++); 1053 if (ret) 1054 break; 1055 ret = putreg32(target, pos, word); 1056 count -= sizeof(*u); 1057 pos += sizeof(*u); 1058 } 1059 } 1060 return ret; 1061 } 1062 1063 static long ia32_arch_ptrace(struct task_struct *child, compat_long_t request, 1064 compat_ulong_t caddr, compat_ulong_t cdata) 1065 { 1066 unsigned long addr = caddr; 1067 unsigned long data = cdata; 1068 void __user *datap = compat_ptr(data); 1069 int ret; 1070 __u32 val; 1071 1072 switch (request) { 1073 case PTRACE_PEEKUSR: 1074 ret = getreg32(child, addr, &val); 1075 if (ret == 0) 1076 ret = put_user(val, (__u32 __user *)datap); 1077 break; 1078 1079 case PTRACE_POKEUSR: 1080 ret = putreg32(child, addr, data); 1081 break; 1082 1083 case PTRACE_GETREGS: /* Get all gp regs from the child. */ 1084 return copy_regset_to_user(child, &user_x86_32_view, 1085 REGSET_GENERAL, 1086 0, sizeof(struct user_regs_struct32), 1087 datap); 1088 1089 case PTRACE_SETREGS: /* Set all gp regs in the child. */ 1090 return copy_regset_from_user(child, &user_x86_32_view, 1091 REGSET_GENERAL, 0, 1092 sizeof(struct user_regs_struct32), 1093 datap); 1094 1095 case PTRACE_GETFPREGS: /* Get the child FPU state. */ 1096 return copy_regset_to_user(child, &user_x86_32_view, 1097 REGSET_FP, 0, 1098 sizeof(struct user_i387_ia32_struct), 1099 datap); 1100 1101 case PTRACE_SETFPREGS: /* Set the child FPU state. */ 1102 return copy_regset_from_user( 1103 child, &user_x86_32_view, REGSET_FP, 1104 0, sizeof(struct user_i387_ia32_struct), datap); 1105 1106 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */ 1107 return copy_regset_to_user(child, &user_x86_32_view, 1108 REGSET_XFP, 0, 1109 sizeof(struct user32_fxsr_struct), 1110 datap); 1111 1112 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */ 1113 return copy_regset_from_user(child, &user_x86_32_view, 1114 REGSET_XFP, 0, 1115 sizeof(struct user32_fxsr_struct), 1116 datap); 1117 1118 case PTRACE_GET_THREAD_AREA: 1119 case PTRACE_SET_THREAD_AREA: 1120 return arch_ptrace(child, request, addr, data); 1121 1122 default: 1123 return compat_ptrace_request(child, request, addr, data); 1124 } 1125 1126 return ret; 1127 } 1128 #endif /* CONFIG_IA32_EMULATION */ 1129 1130 #ifdef CONFIG_X86_X32_ABI 1131 static long x32_arch_ptrace(struct task_struct *child, 1132 compat_long_t request, compat_ulong_t caddr, 1133 compat_ulong_t cdata) 1134 { 1135 unsigned long addr = caddr; 1136 unsigned long data = cdata; 1137 void __user *datap = compat_ptr(data); 1138 int ret; 1139 1140 switch (request) { 1141 /* Read 32bits at location addr in the USER area. Only allow 1142 to return the lower 32bits of segment and debug registers. */ 1143 case PTRACE_PEEKUSR: { 1144 u32 tmp; 1145 1146 ret = -EIO; 1147 if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user) || 1148 addr < offsetof(struct user_regs_struct, cs)) 1149 break; 1150 1151 tmp = 0; /* Default return condition */ 1152 if (addr < sizeof(struct user_regs_struct)) 1153 tmp = getreg(child, addr); 1154 else if (addr >= offsetof(struct user, u_debugreg[0]) && 1155 addr <= offsetof(struct user, u_debugreg[7])) { 1156 addr -= offsetof(struct user, u_debugreg[0]); 1157 tmp = ptrace_get_debugreg(child, addr / sizeof(data)); 1158 } 1159 ret = put_user(tmp, (__u32 __user *)datap); 1160 break; 1161 } 1162 1163 /* Write the word at location addr in the USER area. Only allow 1164 to update segment and debug registers with the upper 32bits 1165 zero-extended. */ 1166 case PTRACE_POKEUSR: 1167 ret = -EIO; 1168 if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user) || 1169 addr < offsetof(struct user_regs_struct, cs)) 1170 break; 1171 1172 if (addr < sizeof(struct user_regs_struct)) 1173 ret = putreg(child, addr, data); 1174 else if (addr >= offsetof(struct user, u_debugreg[0]) && 1175 addr <= offsetof(struct user, u_debugreg[7])) { 1176 addr -= offsetof(struct user, u_debugreg[0]); 1177 ret = ptrace_set_debugreg(child, 1178 addr / sizeof(data), data); 1179 } 1180 break; 1181 1182 case PTRACE_GETREGS: /* Get all gp regs from the child. */ 1183 return copy_regset_to_user(child, 1184 task_user_regset_view(current), 1185 REGSET_GENERAL, 1186 0, sizeof(struct user_regs_struct), 1187 datap); 1188 1189 case PTRACE_SETREGS: /* Set all gp regs in the child. */ 1190 return copy_regset_from_user(child, 1191 task_user_regset_view(current), 1192 REGSET_GENERAL, 1193 0, sizeof(struct user_regs_struct), 1194 datap); 1195 1196 case PTRACE_GETFPREGS: /* Get the child FPU state. */ 1197 return copy_regset_to_user(child, 1198 task_user_regset_view(current), 1199 REGSET_FP, 1200 0, sizeof(struct user_i387_struct), 1201 datap); 1202 1203 case PTRACE_SETFPREGS: /* Set the child FPU state. */ 1204 return copy_regset_from_user(child, 1205 task_user_regset_view(current), 1206 REGSET_FP, 1207 0, sizeof(struct user_i387_struct), 1208 datap); 1209 1210 default: 1211 return compat_ptrace_request(child, request, addr, data); 1212 } 1213 1214 return ret; 1215 } 1216 #endif 1217 1218 #ifdef CONFIG_COMPAT 1219 long compat_arch_ptrace(struct task_struct *child, compat_long_t request, 1220 compat_ulong_t caddr, compat_ulong_t cdata) 1221 { 1222 #ifdef CONFIG_X86_X32_ABI 1223 if (!in_ia32_syscall()) 1224 return x32_arch_ptrace(child, request, caddr, cdata); 1225 #endif 1226 #ifdef CONFIG_IA32_EMULATION 1227 return ia32_arch_ptrace(child, request, caddr, cdata); 1228 #else 1229 return 0; 1230 #endif 1231 } 1232 #endif /* CONFIG_COMPAT */ 1233 1234 #ifdef CONFIG_X86_64 1235 1236 static struct user_regset x86_64_regsets[] __ro_after_init = { 1237 [REGSET_GENERAL] = { 1238 .core_note_type = NT_PRSTATUS, 1239 .n = sizeof(struct user_regs_struct) / sizeof(long), 1240 .size = sizeof(long), .align = sizeof(long), 1241 .get = genregs_get, .set = genregs_set 1242 }, 1243 [REGSET_FP] = { 1244 .core_note_type = NT_PRFPREG, 1245 .n = sizeof(struct user_i387_struct) / sizeof(long), 1246 .size = sizeof(long), .align = sizeof(long), 1247 .active = regset_xregset_fpregs_active, .get = xfpregs_get, .set = xfpregs_set 1248 }, 1249 [REGSET_XSTATE] = { 1250 .core_note_type = NT_X86_XSTATE, 1251 .size = sizeof(u64), .align = sizeof(u64), 1252 .active = xstateregs_active, .get = xstateregs_get, 1253 .set = xstateregs_set 1254 }, 1255 [REGSET_IOPERM64] = { 1256 .core_note_type = NT_386_IOPERM, 1257 .n = IO_BITMAP_LONGS, 1258 .size = sizeof(long), .align = sizeof(long), 1259 .active = ioperm_active, .get = ioperm_get 1260 }, 1261 }; 1262 1263 static const struct user_regset_view user_x86_64_view = { 1264 .name = "x86_64", .e_machine = EM_X86_64, 1265 .regsets = x86_64_regsets, .n = ARRAY_SIZE(x86_64_regsets) 1266 }; 1267 1268 #else /* CONFIG_X86_32 */ 1269 1270 #define user_regs_struct32 user_regs_struct 1271 #define genregs32_get genregs_get 1272 #define genregs32_set genregs_set 1273 1274 #endif /* CONFIG_X86_64 */ 1275 1276 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION 1277 static struct user_regset x86_32_regsets[] __ro_after_init = { 1278 [REGSET_GENERAL] = { 1279 .core_note_type = NT_PRSTATUS, 1280 .n = sizeof(struct user_regs_struct32) / sizeof(u32), 1281 .size = sizeof(u32), .align = sizeof(u32), 1282 .get = genregs32_get, .set = genregs32_set 1283 }, 1284 [REGSET_FP] = { 1285 .core_note_type = NT_PRFPREG, 1286 .n = sizeof(struct user_i387_ia32_struct) / sizeof(u32), 1287 .size = sizeof(u32), .align = sizeof(u32), 1288 .active = regset_fpregs_active, .get = fpregs_get, .set = fpregs_set 1289 }, 1290 [REGSET_XFP] = { 1291 .core_note_type = NT_PRXFPREG, 1292 .n = sizeof(struct user32_fxsr_struct) / sizeof(u32), 1293 .size = sizeof(u32), .align = sizeof(u32), 1294 .active = regset_xregset_fpregs_active, .get = xfpregs_get, .set = xfpregs_set 1295 }, 1296 [REGSET_XSTATE] = { 1297 .core_note_type = NT_X86_XSTATE, 1298 .size = sizeof(u64), .align = sizeof(u64), 1299 .active = xstateregs_active, .get = xstateregs_get, 1300 .set = xstateregs_set 1301 }, 1302 [REGSET_TLS] = { 1303 .core_note_type = NT_386_TLS, 1304 .n = GDT_ENTRY_TLS_ENTRIES, .bias = GDT_ENTRY_TLS_MIN, 1305 .size = sizeof(struct user_desc), 1306 .align = sizeof(struct user_desc), 1307 .active = regset_tls_active, 1308 .get = regset_tls_get, .set = regset_tls_set 1309 }, 1310 [REGSET_IOPERM32] = { 1311 .core_note_type = NT_386_IOPERM, 1312 .n = IO_BITMAP_BYTES / sizeof(u32), 1313 .size = sizeof(u32), .align = sizeof(u32), 1314 .active = ioperm_active, .get = ioperm_get 1315 }, 1316 }; 1317 1318 static const struct user_regset_view user_x86_32_view = { 1319 .name = "i386", .e_machine = EM_386, 1320 .regsets = x86_32_regsets, .n = ARRAY_SIZE(x86_32_regsets) 1321 }; 1322 #endif 1323 1324 /* 1325 * This represents bytes 464..511 in the memory layout exported through 1326 * the REGSET_XSTATE interface. 1327 */ 1328 u64 xstate_fx_sw_bytes[USER_XSTATE_FX_SW_WORDS]; 1329 1330 void __init update_regset_xstate_info(unsigned int size, u64 xstate_mask) 1331 { 1332 #ifdef CONFIG_X86_64 1333 x86_64_regsets[REGSET_XSTATE].n = size / sizeof(u64); 1334 #endif 1335 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION 1336 x86_32_regsets[REGSET_XSTATE].n = size / sizeof(u64); 1337 #endif 1338 xstate_fx_sw_bytes[USER_XSTATE_XCR0_WORD] = xstate_mask; 1339 } 1340 1341 const struct user_regset_view *task_user_regset_view(struct task_struct *task) 1342 { 1343 #ifdef CONFIG_IA32_EMULATION 1344 if (!user_64bit_mode(task_pt_regs(task))) 1345 #endif 1346 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION 1347 return &user_x86_32_view; 1348 #endif 1349 #ifdef CONFIG_X86_64 1350 return &user_x86_64_view; 1351 #endif 1352 } 1353 1354 void send_sigtrap(struct pt_regs *regs, int error_code, int si_code) 1355 { 1356 struct task_struct *tsk = current; 1357 1358 tsk->thread.trap_nr = X86_TRAP_DB; 1359 tsk->thread.error_code = error_code; 1360 1361 /* Send us the fake SIGTRAP */ 1362 force_sig_fault(SIGTRAP, si_code, 1363 user_mode(regs) ? (void __user *)regs->ip : NULL); 1364 } 1365 1366 void user_single_step_report(struct pt_regs *regs) 1367 { 1368 send_sigtrap(regs, 0, TRAP_BRKPT); 1369 } 1370