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