0001
0002
0003
0004
0005
0006
0007
0008 #include <linux/kernel.h>
0009 #include <linux/sched.h>
0010 #include <linux/sched/task_stack.h>
0011 #include <linux/mm.h>
0012 #include <linux/smp.h>
0013 #include <linux/errno.h>
0014 #include <linux/slab.h>
0015 #include <linux/ptrace.h>
0016 #include <linux/user.h>
0017 #include <linux/elf.h>
0018 #include <linux/security.h>
0019 #include <linux/audit.h>
0020 #include <linux/seccomp.h>
0021 #include <linux/signal.h>
0022 #include <linux/perf_event.h>
0023 #include <linux/hw_breakpoint.h>
0024 #include <linux/rcupdate.h>
0025 #include <linux/export.h>
0026 #include <linux/context_tracking.h>
0027 #include <linux/nospec.h>
0028
0029 #include <linux/uaccess.h>
0030 #include <asm/processor.h>
0031 #include <asm/fpu/signal.h>
0032 #include <asm/fpu/regset.h>
0033 #include <asm/fpu/xstate.h>
0034 #include <asm/debugreg.h>
0035 #include <asm/ldt.h>
0036 #include <asm/desc.h>
0037 #include <asm/prctl.h>
0038 #include <asm/proto.h>
0039 #include <asm/hw_breakpoint.h>
0040 #include <asm/traps.h>
0041 #include <asm/syscall.h>
0042 #include <asm/fsgsbase.h>
0043 #include <asm/io_bitmap.h>
0044
0045 #include "tls.h"
0046
0047 enum x86_regset {
0048 REGSET_GENERAL,
0049 REGSET_FP,
0050 REGSET_XFP,
0051 REGSET_IOPERM64 = REGSET_XFP,
0052 REGSET_XSTATE,
0053 REGSET_TLS,
0054 REGSET_IOPERM32,
0055 };
0056
0057 struct pt_regs_offset {
0058 const char *name;
0059 int offset;
0060 };
0061
0062 #define REG_OFFSET_NAME(r) {.name = #r, .offset = offsetof(struct pt_regs, r)}
0063 #define REG_OFFSET_END {.name = NULL, .offset = 0}
0064
0065 static const struct pt_regs_offset regoffset_table[] = {
0066 #ifdef CONFIG_X86_64
0067 REG_OFFSET_NAME(r15),
0068 REG_OFFSET_NAME(r14),
0069 REG_OFFSET_NAME(r13),
0070 REG_OFFSET_NAME(r12),
0071 REG_OFFSET_NAME(r11),
0072 REG_OFFSET_NAME(r10),
0073 REG_OFFSET_NAME(r9),
0074 REG_OFFSET_NAME(r8),
0075 #endif
0076 REG_OFFSET_NAME(bx),
0077 REG_OFFSET_NAME(cx),
0078 REG_OFFSET_NAME(dx),
0079 REG_OFFSET_NAME(si),
0080 REG_OFFSET_NAME(di),
0081 REG_OFFSET_NAME(bp),
0082 REG_OFFSET_NAME(ax),
0083 #ifdef CONFIG_X86_32
0084 REG_OFFSET_NAME(ds),
0085 REG_OFFSET_NAME(es),
0086 REG_OFFSET_NAME(fs),
0087 REG_OFFSET_NAME(gs),
0088 #endif
0089 REG_OFFSET_NAME(orig_ax),
0090 REG_OFFSET_NAME(ip),
0091 REG_OFFSET_NAME(cs),
0092 REG_OFFSET_NAME(flags),
0093 REG_OFFSET_NAME(sp),
0094 REG_OFFSET_NAME(ss),
0095 REG_OFFSET_END,
0096 };
0097
0098
0099
0100
0101
0102
0103
0104
0105 int regs_query_register_offset(const char *name)
0106 {
0107 const struct pt_regs_offset *roff;
0108 for (roff = regoffset_table; roff->name != NULL; roff++)
0109 if (!strcmp(roff->name, name))
0110 return roff->offset;
0111 return -EINVAL;
0112 }
0113
0114
0115
0116
0117
0118
0119
0120
0121 const char *regs_query_register_name(unsigned int offset)
0122 {
0123 const struct pt_regs_offset *roff;
0124 for (roff = regoffset_table; roff->name != NULL; roff++)
0125 if (roff->offset == offset)
0126 return roff->name;
0127 return NULL;
0128 }
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138 #define FLAG_MASK_32 ((unsigned long) \
0139 (X86_EFLAGS_CF | X86_EFLAGS_PF | \
0140 X86_EFLAGS_AF | X86_EFLAGS_ZF | \
0141 X86_EFLAGS_SF | X86_EFLAGS_TF | \
0142 X86_EFLAGS_DF | X86_EFLAGS_OF | \
0143 X86_EFLAGS_RF | X86_EFLAGS_AC))
0144
0145
0146
0147
0148 static inline bool invalid_selector(u16 value)
0149 {
0150 return unlikely(value != 0 && (value & SEGMENT_RPL_MASK) != USER_RPL);
0151 }
0152
0153 #ifdef CONFIG_X86_32
0154
0155 #define FLAG_MASK FLAG_MASK_32
0156
0157 static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long regno)
0158 {
0159 BUILD_BUG_ON(offsetof(struct pt_regs, bx) != 0);
0160 return ®s->bx + (regno >> 2);
0161 }
0162
0163 static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
0164 {
0165
0166
0167
0168 unsigned int retval;
0169 if (offset != offsetof(struct user_regs_struct, gs))
0170 retval = *pt_regs_access(task_pt_regs(task), offset);
0171 else {
0172 if (task == current)
0173 savesegment(gs, retval);
0174 else
0175 retval = task->thread.gs;
0176 }
0177 return retval;
0178 }
0179
0180 static int set_segment_reg(struct task_struct *task,
0181 unsigned long offset, u16 value)
0182 {
0183 if (WARN_ON_ONCE(task == current))
0184 return -EIO;
0185
0186
0187
0188
0189 if (invalid_selector(value))
0190 return -EIO;
0191
0192
0193
0194
0195
0196
0197
0198
0199
0200
0201 switch (offset) {
0202 case offsetof(struct user_regs_struct, cs):
0203 case offsetof(struct user_regs_struct, ss):
0204 if (unlikely(value == 0))
0205 return -EIO;
0206 fallthrough;
0207
0208 default:
0209 *pt_regs_access(task_pt_regs(task), offset) = value;
0210 break;
0211
0212 case offsetof(struct user_regs_struct, gs):
0213 task->thread.gs = value;
0214 }
0215
0216 return 0;
0217 }
0218
0219 #else
0220
0221 #define FLAG_MASK (FLAG_MASK_32 | X86_EFLAGS_NT)
0222
0223 static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long offset)
0224 {
0225 BUILD_BUG_ON(offsetof(struct pt_regs, r15) != 0);
0226 return ®s->r15 + (offset / sizeof(regs->r15));
0227 }
0228
0229 static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
0230 {
0231
0232
0233
0234 unsigned int seg;
0235
0236 switch (offset) {
0237 case offsetof(struct user_regs_struct, fs):
0238 if (task == current) {
0239
0240 asm("movl %%fs,%0" : "=r" (seg));
0241 return seg;
0242 }
0243 return task->thread.fsindex;
0244 case offsetof(struct user_regs_struct, gs):
0245 if (task == current) {
0246 asm("movl %%gs,%0" : "=r" (seg));
0247 return seg;
0248 }
0249 return task->thread.gsindex;
0250 case offsetof(struct user_regs_struct, ds):
0251 if (task == current) {
0252 asm("movl %%ds,%0" : "=r" (seg));
0253 return seg;
0254 }
0255 return task->thread.ds;
0256 case offsetof(struct user_regs_struct, es):
0257 if (task == current) {
0258 asm("movl %%es,%0" : "=r" (seg));
0259 return seg;
0260 }
0261 return task->thread.es;
0262
0263 case offsetof(struct user_regs_struct, cs):
0264 case offsetof(struct user_regs_struct, ss):
0265 break;
0266 }
0267 return *pt_regs_access(task_pt_regs(task), offset);
0268 }
0269
0270 static int set_segment_reg(struct task_struct *task,
0271 unsigned long offset, u16 value)
0272 {
0273 if (WARN_ON_ONCE(task == current))
0274 return -EIO;
0275
0276
0277
0278
0279 if (invalid_selector(value))
0280 return -EIO;
0281
0282
0283
0284
0285
0286
0287
0288 switch (offset) {
0289 case offsetof(struct user_regs_struct,fs):
0290 task->thread.fsindex = value;
0291 break;
0292 case offsetof(struct user_regs_struct,gs):
0293 task->thread.gsindex = value;
0294 break;
0295 case offsetof(struct user_regs_struct,ds):
0296 task->thread.ds = value;
0297 break;
0298 case offsetof(struct user_regs_struct,es):
0299 task->thread.es = value;
0300 break;
0301
0302
0303
0304
0305 case offsetof(struct user_regs_struct,cs):
0306 if (unlikely(value == 0))
0307 return -EIO;
0308 task_pt_regs(task)->cs = value;
0309 break;
0310 case offsetof(struct user_regs_struct,ss):
0311 if (unlikely(value == 0))
0312 return -EIO;
0313 task_pt_regs(task)->ss = value;
0314 break;
0315 }
0316
0317 return 0;
0318 }
0319
0320 #endif
0321
0322 static unsigned long get_flags(struct task_struct *task)
0323 {
0324 unsigned long retval = task_pt_regs(task)->flags;
0325
0326
0327
0328
0329 if (test_tsk_thread_flag(task, TIF_FORCED_TF))
0330 retval &= ~X86_EFLAGS_TF;
0331
0332 return retval;
0333 }
0334
0335 static int set_flags(struct task_struct *task, unsigned long value)
0336 {
0337 struct pt_regs *regs = task_pt_regs(task);
0338
0339
0340
0341
0342
0343
0344 if (value & X86_EFLAGS_TF)
0345 clear_tsk_thread_flag(task, TIF_FORCED_TF);
0346 else if (test_tsk_thread_flag(task, TIF_FORCED_TF))
0347 value |= X86_EFLAGS_TF;
0348
0349 regs->flags = (regs->flags & ~FLAG_MASK) | (value & FLAG_MASK);
0350
0351 return 0;
0352 }
0353
0354 static int putreg(struct task_struct *child,
0355 unsigned long offset, unsigned long value)
0356 {
0357 switch (offset) {
0358 case offsetof(struct user_regs_struct, cs):
0359 case offsetof(struct user_regs_struct, ds):
0360 case offsetof(struct user_regs_struct, es):
0361 case offsetof(struct user_regs_struct, fs):
0362 case offsetof(struct user_regs_struct, gs):
0363 case offsetof(struct user_regs_struct, ss):
0364 return set_segment_reg(child, offset, value);
0365
0366 case offsetof(struct user_regs_struct, flags):
0367 return set_flags(child, value);
0368
0369 #ifdef CONFIG_X86_64
0370 case offsetof(struct user_regs_struct,fs_base):
0371 if (value >= TASK_SIZE_MAX)
0372 return -EIO;
0373 x86_fsbase_write_task(child, value);
0374 return 0;
0375 case offsetof(struct user_regs_struct,gs_base):
0376 if (value >= TASK_SIZE_MAX)
0377 return -EIO;
0378 x86_gsbase_write_task(child, value);
0379 return 0;
0380 #endif
0381 }
0382
0383 *pt_regs_access(task_pt_regs(child), offset) = value;
0384 return 0;
0385 }
0386
0387 static unsigned long getreg(struct task_struct *task, unsigned long offset)
0388 {
0389 switch (offset) {
0390 case offsetof(struct user_regs_struct, cs):
0391 case offsetof(struct user_regs_struct, ds):
0392 case offsetof(struct user_regs_struct, es):
0393 case offsetof(struct user_regs_struct, fs):
0394 case offsetof(struct user_regs_struct, gs):
0395 case offsetof(struct user_regs_struct, ss):
0396 return get_segment_reg(task, offset);
0397
0398 case offsetof(struct user_regs_struct, flags):
0399 return get_flags(task);
0400
0401 #ifdef CONFIG_X86_64
0402 case offsetof(struct user_regs_struct, fs_base):
0403 return x86_fsbase_read_task(task);
0404 case offsetof(struct user_regs_struct, gs_base):
0405 return x86_gsbase_read_task(task);
0406 #endif
0407 }
0408
0409 return *pt_regs_access(task_pt_regs(task), offset);
0410 }
0411
0412 static int genregs_get(struct task_struct *target,
0413 const struct user_regset *regset,
0414 struct membuf to)
0415 {
0416 int reg;
0417
0418 for (reg = 0; to.left; reg++)
0419 membuf_store(&to, getreg(target, reg * sizeof(unsigned long)));
0420 return 0;
0421 }
0422
0423 static int genregs_set(struct task_struct *target,
0424 const struct user_regset *regset,
0425 unsigned int pos, unsigned int count,
0426 const void *kbuf, const void __user *ubuf)
0427 {
0428 int ret = 0;
0429 if (kbuf) {
0430 const unsigned long *k = kbuf;
0431 while (count >= sizeof(*k) && !ret) {
0432 ret = putreg(target, pos, *k++);
0433 count -= sizeof(*k);
0434 pos += sizeof(*k);
0435 }
0436 } else {
0437 const unsigned long __user *u = ubuf;
0438 while (count >= sizeof(*u) && !ret) {
0439 unsigned long word;
0440 ret = __get_user(word, u++);
0441 if (ret)
0442 break;
0443 ret = putreg(target, pos, word);
0444 count -= sizeof(*u);
0445 pos += sizeof(*u);
0446 }
0447 }
0448 return ret;
0449 }
0450
0451 static void ptrace_triggered(struct perf_event *bp,
0452 struct perf_sample_data *data,
0453 struct pt_regs *regs)
0454 {
0455 int i;
0456 struct thread_struct *thread = &(current->thread);
0457
0458
0459
0460
0461
0462 for (i = 0; i < HBP_NUM; i++) {
0463 if (thread->ptrace_bps[i] == bp)
0464 break;
0465 }
0466
0467 thread->virtual_dr6 |= (DR_TRAP0 << i);
0468 }
0469
0470
0471
0472
0473
0474
0475 static unsigned long ptrace_get_dr7(struct perf_event *bp[])
0476 {
0477 int i;
0478 int dr7 = 0;
0479 struct arch_hw_breakpoint *info;
0480
0481 for (i = 0; i < HBP_NUM; i++) {
0482 if (bp[i] && !bp[i]->attr.disabled) {
0483 info = counter_arch_bp(bp[i]);
0484 dr7 |= encode_dr7(i, info->len, info->type);
0485 }
0486 }
0487
0488 return dr7;
0489 }
0490
0491 static int ptrace_fill_bp_fields(struct perf_event_attr *attr,
0492 int len, int type, bool disabled)
0493 {
0494 int err, bp_len, bp_type;
0495
0496 err = arch_bp_generic_fields(len, type, &bp_len, &bp_type);
0497 if (!err) {
0498 attr->bp_len = bp_len;
0499 attr->bp_type = bp_type;
0500 attr->disabled = disabled;
0501 }
0502
0503 return err;
0504 }
0505
0506 static struct perf_event *
0507 ptrace_register_breakpoint(struct task_struct *tsk, int len, int type,
0508 unsigned long addr, bool disabled)
0509 {
0510 struct perf_event_attr attr;
0511 int err;
0512
0513 ptrace_breakpoint_init(&attr);
0514 attr.bp_addr = addr;
0515
0516 err = ptrace_fill_bp_fields(&attr, len, type, disabled);
0517 if (err)
0518 return ERR_PTR(err);
0519
0520 return register_user_hw_breakpoint(&attr, ptrace_triggered,
0521 NULL, tsk);
0522 }
0523
0524 static int ptrace_modify_breakpoint(struct perf_event *bp, int len, int type,
0525 int disabled)
0526 {
0527 struct perf_event_attr attr = bp->attr;
0528 int err;
0529
0530 err = ptrace_fill_bp_fields(&attr, len, type, disabled);
0531 if (err)
0532 return err;
0533
0534 return modify_user_hw_breakpoint(bp, &attr);
0535 }
0536
0537
0538
0539
0540 static int ptrace_write_dr7(struct task_struct *tsk, unsigned long data)
0541 {
0542 struct thread_struct *thread = &tsk->thread;
0543 unsigned long old_dr7;
0544 bool second_pass = false;
0545 int i, rc, ret = 0;
0546
0547 data &= ~DR_CONTROL_RESERVED;
0548 old_dr7 = ptrace_get_dr7(thread->ptrace_bps);
0549
0550 restore:
0551 rc = 0;
0552 for (i = 0; i < HBP_NUM; i++) {
0553 unsigned len, type;
0554 bool disabled = !decode_dr7(data, i, &len, &type);
0555 struct perf_event *bp = thread->ptrace_bps[i];
0556
0557 if (!bp) {
0558 if (disabled)
0559 continue;
0560
0561 bp = ptrace_register_breakpoint(tsk,
0562 len, type, 0, disabled);
0563 if (IS_ERR(bp)) {
0564 rc = PTR_ERR(bp);
0565 break;
0566 }
0567
0568 thread->ptrace_bps[i] = bp;
0569 continue;
0570 }
0571
0572 rc = ptrace_modify_breakpoint(bp, len, type, disabled);
0573 if (rc)
0574 break;
0575 }
0576
0577
0578 if (rc && !WARN_ON(second_pass)) {
0579 ret = rc;
0580 data = old_dr7;
0581 second_pass = true;
0582 goto restore;
0583 }
0584
0585 return ret;
0586 }
0587
0588
0589
0590
0591 static unsigned long ptrace_get_debugreg(struct task_struct *tsk, int n)
0592 {
0593 struct thread_struct *thread = &tsk->thread;
0594 unsigned long val = 0;
0595
0596 if (n < HBP_NUM) {
0597 int index = array_index_nospec(n, HBP_NUM);
0598 struct perf_event *bp = thread->ptrace_bps[index];
0599
0600 if (bp)
0601 val = bp->hw.info.address;
0602 } else if (n == 6) {
0603 val = thread->virtual_dr6 ^ DR6_RESERVED;
0604 } else if (n == 7) {
0605 val = thread->ptrace_dr7;
0606 }
0607 return val;
0608 }
0609
0610 static int ptrace_set_breakpoint_addr(struct task_struct *tsk, int nr,
0611 unsigned long addr)
0612 {
0613 struct thread_struct *t = &tsk->thread;
0614 struct perf_event *bp = t->ptrace_bps[nr];
0615 int err = 0;
0616
0617 if (!bp) {
0618
0619
0620
0621
0622
0623
0624
0625
0626
0627
0628
0629 bp = ptrace_register_breakpoint(tsk,
0630 X86_BREAKPOINT_LEN_1, X86_BREAKPOINT_WRITE,
0631 addr, true);
0632 if (IS_ERR(bp))
0633 err = PTR_ERR(bp);
0634 else
0635 t->ptrace_bps[nr] = bp;
0636 } else {
0637 struct perf_event_attr attr = bp->attr;
0638
0639 attr.bp_addr = addr;
0640 err = modify_user_hw_breakpoint(bp, &attr);
0641 }
0642
0643 return err;
0644 }
0645
0646
0647
0648
0649 static int ptrace_set_debugreg(struct task_struct *tsk, int n,
0650 unsigned long val)
0651 {
0652 struct thread_struct *thread = &tsk->thread;
0653
0654 int rc = -EIO;
0655
0656 if (n < HBP_NUM) {
0657 rc = ptrace_set_breakpoint_addr(tsk, n, val);
0658 } else if (n == 6) {
0659 thread->virtual_dr6 = val ^ DR6_RESERVED;
0660 rc = 0;
0661 } else if (n == 7) {
0662 rc = ptrace_write_dr7(tsk, val);
0663 if (!rc)
0664 thread->ptrace_dr7 = val;
0665 }
0666 return rc;
0667 }
0668
0669
0670
0671
0672
0673 static int ioperm_active(struct task_struct *target,
0674 const struct user_regset *regset)
0675 {
0676 struct io_bitmap *iobm = target->thread.io_bitmap;
0677
0678 return iobm ? DIV_ROUND_UP(iobm->max, regset->size) : 0;
0679 }
0680
0681 static int ioperm_get(struct task_struct *target,
0682 const struct user_regset *regset,
0683 struct membuf to)
0684 {
0685 struct io_bitmap *iobm = target->thread.io_bitmap;
0686
0687 if (!iobm)
0688 return -ENXIO;
0689
0690 return membuf_write(&to, iobm->bitmap, IO_BITMAP_BYTES);
0691 }
0692
0693
0694
0695
0696
0697
0698 void ptrace_disable(struct task_struct *child)
0699 {
0700 user_disable_single_step(child);
0701 }
0702
0703 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
0704 static const struct user_regset_view user_x86_32_view;
0705 #endif
0706 #ifdef CONFIG_X86_64
0707 static const struct user_regset_view user_x86_64_view;
0708 #endif
0709
0710 long arch_ptrace(struct task_struct *child, long request,
0711 unsigned long addr, unsigned long data)
0712 {
0713 int ret;
0714 unsigned long __user *datap = (unsigned long __user *)data;
0715
0716 #ifdef CONFIG_X86_64
0717
0718 const struct user_regset_view *regset_view = &user_x86_64_view;
0719 #else
0720
0721 const struct user_regset_view *regset_view = &user_x86_32_view;
0722 #endif
0723
0724 switch (request) {
0725
0726 case PTRACE_PEEKUSR: {
0727 unsigned long tmp;
0728
0729 ret = -EIO;
0730 if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user))
0731 break;
0732
0733 tmp = 0;
0734 if (addr < sizeof(struct user_regs_struct))
0735 tmp = getreg(child, addr);
0736 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
0737 addr <= offsetof(struct user, u_debugreg[7])) {
0738 addr -= offsetof(struct user, u_debugreg[0]);
0739 tmp = ptrace_get_debugreg(child, addr / sizeof(data));
0740 }
0741 ret = put_user(tmp, datap);
0742 break;
0743 }
0744
0745 case PTRACE_POKEUSR:
0746 ret = -EIO;
0747 if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user))
0748 break;
0749
0750 if (addr < sizeof(struct user_regs_struct))
0751 ret = putreg(child, addr, data);
0752 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
0753 addr <= offsetof(struct user, u_debugreg[7])) {
0754 addr -= offsetof(struct user, u_debugreg[0]);
0755 ret = ptrace_set_debugreg(child,
0756 addr / sizeof(data), data);
0757 }
0758 break;
0759
0760 case PTRACE_GETREGS:
0761 return copy_regset_to_user(child,
0762 regset_view,
0763 REGSET_GENERAL,
0764 0, sizeof(struct user_regs_struct),
0765 datap);
0766
0767 case PTRACE_SETREGS:
0768 return copy_regset_from_user(child,
0769 regset_view,
0770 REGSET_GENERAL,
0771 0, sizeof(struct user_regs_struct),
0772 datap);
0773
0774 case PTRACE_GETFPREGS:
0775 return copy_regset_to_user(child,
0776 regset_view,
0777 REGSET_FP,
0778 0, sizeof(struct user_i387_struct),
0779 datap);
0780
0781 case PTRACE_SETFPREGS:
0782 return copy_regset_from_user(child,
0783 regset_view,
0784 REGSET_FP,
0785 0, sizeof(struct user_i387_struct),
0786 datap);
0787
0788 #ifdef CONFIG_X86_32
0789 case PTRACE_GETFPXREGS:
0790 return copy_regset_to_user(child, &user_x86_32_view,
0791 REGSET_XFP,
0792 0, sizeof(struct user_fxsr_struct),
0793 datap) ? -EIO : 0;
0794
0795 case PTRACE_SETFPXREGS:
0796 return copy_regset_from_user(child, &user_x86_32_view,
0797 REGSET_XFP,
0798 0, sizeof(struct user_fxsr_struct),
0799 datap) ? -EIO : 0;
0800 #endif
0801
0802 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
0803 case PTRACE_GET_THREAD_AREA:
0804 if ((int) addr < 0)
0805 return -EIO;
0806 ret = do_get_thread_area(child, addr,
0807 (struct user_desc __user *)data);
0808 break;
0809
0810 case PTRACE_SET_THREAD_AREA:
0811 if ((int) addr < 0)
0812 return -EIO;
0813 ret = do_set_thread_area(child, addr,
0814 (struct user_desc __user *)data, 0);
0815 break;
0816 #endif
0817
0818 #ifdef CONFIG_X86_64
0819
0820
0821
0822 case PTRACE_ARCH_PRCTL:
0823 ret = do_arch_prctl_64(child, data, addr);
0824 break;
0825 #endif
0826
0827 default:
0828 ret = ptrace_request(child, request, addr, data);
0829 break;
0830 }
0831
0832 return ret;
0833 }
0834
0835 #ifdef CONFIG_IA32_EMULATION
0836
0837 #include <linux/compat.h>
0838 #include <linux/syscalls.h>
0839 #include <asm/ia32.h>
0840 #include <asm/user32.h>
0841
0842 #define R32(l,q) \
0843 case offsetof(struct user32, regs.l): \
0844 regs->q = value; break
0845
0846 #define SEG32(rs) \
0847 case offsetof(struct user32, regs.rs): \
0848 return set_segment_reg(child, \
0849 offsetof(struct user_regs_struct, rs), \
0850 value); \
0851 break
0852
0853 static int putreg32(struct task_struct *child, unsigned regno, u32 value)
0854 {
0855 struct pt_regs *regs = task_pt_regs(child);
0856 int ret;
0857
0858 switch (regno) {
0859
0860 SEG32(cs);
0861 SEG32(ds);
0862 SEG32(es);
0863
0864
0865
0866
0867
0868
0869
0870
0871 case offsetof(struct user32, regs.fs):
0872 ret = set_segment_reg(child,
0873 offsetof(struct user_regs_struct, fs),
0874 value);
0875 if (ret == 0)
0876 child->thread.fsbase =
0877 x86_fsgsbase_read_task(child, value);
0878 return ret;
0879
0880 case offsetof(struct user32, regs.gs):
0881 ret = set_segment_reg(child,
0882 offsetof(struct user_regs_struct, gs),
0883 value);
0884 if (ret == 0)
0885 child->thread.gsbase =
0886 x86_fsgsbase_read_task(child, value);
0887 return ret;
0888
0889 SEG32(ss);
0890
0891 R32(ebx, bx);
0892 R32(ecx, cx);
0893 R32(edx, dx);
0894 R32(edi, di);
0895 R32(esi, si);
0896 R32(ebp, bp);
0897 R32(eax, ax);
0898 R32(eip, ip);
0899 R32(esp, sp);
0900
0901 case offsetof(struct user32, regs.orig_eax):
0902
0903
0904
0905
0906
0907
0908
0909
0910
0911
0912 regs->orig_ax = value;
0913 if (syscall_get_nr(child, regs) != -1)
0914 child->thread_info.status |= TS_I386_REGS_POKED;
0915 break;
0916
0917 case offsetof(struct user32, regs.eflags):
0918 return set_flags(child, value);
0919
0920 case offsetof(struct user32, u_debugreg[0]) ...
0921 offsetof(struct user32, u_debugreg[7]):
0922 regno -= offsetof(struct user32, u_debugreg[0]);
0923 return ptrace_set_debugreg(child, regno / 4, value);
0924
0925 default:
0926 if (regno > sizeof(struct user32) || (regno & 3))
0927 return -EIO;
0928
0929
0930
0931
0932
0933 break;
0934 }
0935 return 0;
0936 }
0937
0938 #undef R32
0939 #undef SEG32
0940
0941 #define R32(l,q) \
0942 case offsetof(struct user32, regs.l): \
0943 *val = regs->q; break
0944
0945 #define SEG32(rs) \
0946 case offsetof(struct user32, regs.rs): \
0947 *val = get_segment_reg(child, \
0948 offsetof(struct user_regs_struct, rs)); \
0949 break
0950
0951 static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
0952 {
0953 struct pt_regs *regs = task_pt_regs(child);
0954
0955 switch (regno) {
0956
0957 SEG32(ds);
0958 SEG32(es);
0959 SEG32(fs);
0960 SEG32(gs);
0961
0962 R32(cs, cs);
0963 R32(ss, ss);
0964 R32(ebx, bx);
0965 R32(ecx, cx);
0966 R32(edx, dx);
0967 R32(edi, di);
0968 R32(esi, si);
0969 R32(ebp, bp);
0970 R32(eax, ax);
0971 R32(orig_eax, orig_ax);
0972 R32(eip, ip);
0973 R32(esp, sp);
0974
0975 case offsetof(struct user32, regs.eflags):
0976 *val = get_flags(child);
0977 break;
0978
0979 case offsetof(struct user32, u_debugreg[0]) ...
0980 offsetof(struct user32, u_debugreg[7]):
0981 regno -= offsetof(struct user32, u_debugreg[0]);
0982 *val = ptrace_get_debugreg(child, regno / 4);
0983 break;
0984
0985 default:
0986 if (regno > sizeof(struct user32) || (regno & 3))
0987 return -EIO;
0988
0989
0990
0991
0992
0993 *val = 0;
0994 break;
0995 }
0996 return 0;
0997 }
0998
0999 #undef R32
1000 #undef SEG32
1001
1002 static int genregs32_get(struct task_struct *target,
1003 const struct user_regset *regset,
1004 struct membuf to)
1005 {
1006 int reg;
1007
1008 for (reg = 0; to.left; reg++) {
1009 u32 val;
1010 getreg32(target, reg * 4, &val);
1011 membuf_store(&to, val);
1012 }
1013 return 0;
1014 }
1015
1016 static int genregs32_set(struct task_struct *target,
1017 const struct user_regset *regset,
1018 unsigned int pos, unsigned int count,
1019 const void *kbuf, const void __user *ubuf)
1020 {
1021 int ret = 0;
1022 if (kbuf) {
1023 const compat_ulong_t *k = kbuf;
1024 while (count >= sizeof(*k) && !ret) {
1025 ret = putreg32(target, pos, *k++);
1026 count -= sizeof(*k);
1027 pos += sizeof(*k);
1028 }
1029 } else {
1030 const compat_ulong_t __user *u = ubuf;
1031 while (count >= sizeof(*u) && !ret) {
1032 compat_ulong_t word;
1033 ret = __get_user(word, u++);
1034 if (ret)
1035 break;
1036 ret = putreg32(target, pos, word);
1037 count -= sizeof(*u);
1038 pos += sizeof(*u);
1039 }
1040 }
1041 return ret;
1042 }
1043
1044 static long ia32_arch_ptrace(struct task_struct *child, compat_long_t request,
1045 compat_ulong_t caddr, compat_ulong_t cdata)
1046 {
1047 unsigned long addr = caddr;
1048 unsigned long data = cdata;
1049 void __user *datap = compat_ptr(data);
1050 int ret;
1051 __u32 val;
1052
1053 switch (request) {
1054 case PTRACE_PEEKUSR:
1055 ret = getreg32(child, addr, &val);
1056 if (ret == 0)
1057 ret = put_user(val, (__u32 __user *)datap);
1058 break;
1059
1060 case PTRACE_POKEUSR:
1061 ret = putreg32(child, addr, data);
1062 break;
1063
1064 case PTRACE_GETREGS:
1065 return copy_regset_to_user(child, &user_x86_32_view,
1066 REGSET_GENERAL,
1067 0, sizeof(struct user_regs_struct32),
1068 datap);
1069
1070 case PTRACE_SETREGS:
1071 return copy_regset_from_user(child, &user_x86_32_view,
1072 REGSET_GENERAL, 0,
1073 sizeof(struct user_regs_struct32),
1074 datap);
1075
1076 case PTRACE_GETFPREGS:
1077 return copy_regset_to_user(child, &user_x86_32_view,
1078 REGSET_FP, 0,
1079 sizeof(struct user_i387_ia32_struct),
1080 datap);
1081
1082 case PTRACE_SETFPREGS:
1083 return copy_regset_from_user(
1084 child, &user_x86_32_view, REGSET_FP,
1085 0, sizeof(struct user_i387_ia32_struct), datap);
1086
1087 case PTRACE_GETFPXREGS:
1088 return copy_regset_to_user(child, &user_x86_32_view,
1089 REGSET_XFP, 0,
1090 sizeof(struct user32_fxsr_struct),
1091 datap);
1092
1093 case PTRACE_SETFPXREGS:
1094 return copy_regset_from_user(child, &user_x86_32_view,
1095 REGSET_XFP, 0,
1096 sizeof(struct user32_fxsr_struct),
1097 datap);
1098
1099 case PTRACE_GET_THREAD_AREA:
1100 case PTRACE_SET_THREAD_AREA:
1101 return arch_ptrace(child, request, addr, data);
1102
1103 default:
1104 return compat_ptrace_request(child, request, addr, data);
1105 }
1106
1107 return ret;
1108 }
1109 #endif
1110
1111 #ifdef CONFIG_X86_X32_ABI
1112 static long x32_arch_ptrace(struct task_struct *child,
1113 compat_long_t request, compat_ulong_t caddr,
1114 compat_ulong_t cdata)
1115 {
1116 unsigned long addr = caddr;
1117 unsigned long data = cdata;
1118 void __user *datap = compat_ptr(data);
1119 int ret;
1120
1121 switch (request) {
1122
1123
1124 case PTRACE_PEEKUSR: {
1125 u32 tmp;
1126
1127 ret = -EIO;
1128 if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user) ||
1129 addr < offsetof(struct user_regs_struct, cs))
1130 break;
1131
1132 tmp = 0;
1133 if (addr < sizeof(struct user_regs_struct))
1134 tmp = getreg(child, addr);
1135 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
1136 addr <= offsetof(struct user, u_debugreg[7])) {
1137 addr -= offsetof(struct user, u_debugreg[0]);
1138 tmp = ptrace_get_debugreg(child, addr / sizeof(data));
1139 }
1140 ret = put_user(tmp, (__u32 __user *)datap);
1141 break;
1142 }
1143
1144
1145
1146
1147 case PTRACE_POKEUSR:
1148 ret = -EIO;
1149 if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user) ||
1150 addr < offsetof(struct user_regs_struct, cs))
1151 break;
1152
1153 if (addr < sizeof(struct user_regs_struct))
1154 ret = putreg(child, addr, data);
1155 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
1156 addr <= offsetof(struct user, u_debugreg[7])) {
1157 addr -= offsetof(struct user, u_debugreg[0]);
1158 ret = ptrace_set_debugreg(child,
1159 addr / sizeof(data), data);
1160 }
1161 break;
1162
1163 case PTRACE_GETREGS:
1164 return copy_regset_to_user(child,
1165 &user_x86_64_view,
1166 REGSET_GENERAL,
1167 0, sizeof(struct user_regs_struct),
1168 datap);
1169
1170 case PTRACE_SETREGS:
1171 return copy_regset_from_user(child,
1172 &user_x86_64_view,
1173 REGSET_GENERAL,
1174 0, sizeof(struct user_regs_struct),
1175 datap);
1176
1177 case PTRACE_GETFPREGS:
1178 return copy_regset_to_user(child,
1179 &user_x86_64_view,
1180 REGSET_FP,
1181 0, sizeof(struct user_i387_struct),
1182 datap);
1183
1184 case PTRACE_SETFPREGS:
1185 return copy_regset_from_user(child,
1186 &user_x86_64_view,
1187 REGSET_FP,
1188 0, sizeof(struct user_i387_struct),
1189 datap);
1190
1191 default:
1192 return compat_ptrace_request(child, request, addr, data);
1193 }
1194
1195 return ret;
1196 }
1197 #endif
1198
1199 #ifdef CONFIG_COMPAT
1200 long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
1201 compat_ulong_t caddr, compat_ulong_t cdata)
1202 {
1203 #ifdef CONFIG_X86_X32_ABI
1204 if (!in_ia32_syscall())
1205 return x32_arch_ptrace(child, request, caddr, cdata);
1206 #endif
1207 #ifdef CONFIG_IA32_EMULATION
1208 return ia32_arch_ptrace(child, request, caddr, cdata);
1209 #else
1210 return 0;
1211 #endif
1212 }
1213 #endif
1214
1215 #ifdef CONFIG_X86_64
1216
1217 static struct user_regset x86_64_regsets[] __ro_after_init = {
1218 [REGSET_GENERAL] = {
1219 .core_note_type = NT_PRSTATUS,
1220 .n = sizeof(struct user_regs_struct) / sizeof(long),
1221 .size = sizeof(long), .align = sizeof(long),
1222 .regset_get = genregs_get, .set = genregs_set
1223 },
1224 [REGSET_FP] = {
1225 .core_note_type = NT_PRFPREG,
1226 .n = sizeof(struct fxregs_state) / sizeof(long),
1227 .size = sizeof(long), .align = sizeof(long),
1228 .active = regset_xregset_fpregs_active, .regset_get = xfpregs_get, .set = xfpregs_set
1229 },
1230 [REGSET_XSTATE] = {
1231 .core_note_type = NT_X86_XSTATE,
1232 .size = sizeof(u64), .align = sizeof(u64),
1233 .active = xstateregs_active, .regset_get = xstateregs_get,
1234 .set = xstateregs_set
1235 },
1236 [REGSET_IOPERM64] = {
1237 .core_note_type = NT_386_IOPERM,
1238 .n = IO_BITMAP_LONGS,
1239 .size = sizeof(long), .align = sizeof(long),
1240 .active = ioperm_active, .regset_get = ioperm_get
1241 },
1242 };
1243
1244 static const struct user_regset_view user_x86_64_view = {
1245 .name = "x86_64", .e_machine = EM_X86_64,
1246 .regsets = x86_64_regsets, .n = ARRAY_SIZE(x86_64_regsets)
1247 };
1248
1249 #else
1250
1251 #define user_regs_struct32 user_regs_struct
1252 #define genregs32_get genregs_get
1253 #define genregs32_set genregs_set
1254
1255 #endif
1256
1257 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1258 static struct user_regset x86_32_regsets[] __ro_after_init = {
1259 [REGSET_GENERAL] = {
1260 .core_note_type = NT_PRSTATUS,
1261 .n = sizeof(struct user_regs_struct32) / sizeof(u32),
1262 .size = sizeof(u32), .align = sizeof(u32),
1263 .regset_get = genregs32_get, .set = genregs32_set
1264 },
1265 [REGSET_FP] = {
1266 .core_note_type = NT_PRFPREG,
1267 .n = sizeof(struct user_i387_ia32_struct) / sizeof(u32),
1268 .size = sizeof(u32), .align = sizeof(u32),
1269 .active = regset_fpregs_active, .regset_get = fpregs_get, .set = fpregs_set
1270 },
1271 [REGSET_XFP] = {
1272 .core_note_type = NT_PRXFPREG,
1273 .n = sizeof(struct fxregs_state) / sizeof(u32),
1274 .size = sizeof(u32), .align = sizeof(u32),
1275 .active = regset_xregset_fpregs_active, .regset_get = xfpregs_get, .set = xfpregs_set
1276 },
1277 [REGSET_XSTATE] = {
1278 .core_note_type = NT_X86_XSTATE,
1279 .size = sizeof(u64), .align = sizeof(u64),
1280 .active = xstateregs_active, .regset_get = xstateregs_get,
1281 .set = xstateregs_set
1282 },
1283 [REGSET_TLS] = {
1284 .core_note_type = NT_386_TLS,
1285 .n = GDT_ENTRY_TLS_ENTRIES, .bias = GDT_ENTRY_TLS_MIN,
1286 .size = sizeof(struct user_desc),
1287 .align = sizeof(struct user_desc),
1288 .active = regset_tls_active,
1289 .regset_get = regset_tls_get, .set = regset_tls_set
1290 },
1291 [REGSET_IOPERM32] = {
1292 .core_note_type = NT_386_IOPERM,
1293 .n = IO_BITMAP_BYTES / sizeof(u32),
1294 .size = sizeof(u32), .align = sizeof(u32),
1295 .active = ioperm_active, .regset_get = ioperm_get
1296 },
1297 };
1298
1299 static const struct user_regset_view user_x86_32_view = {
1300 .name = "i386", .e_machine = EM_386,
1301 .regsets = x86_32_regsets, .n = ARRAY_SIZE(x86_32_regsets)
1302 };
1303 #endif
1304
1305
1306
1307
1308
1309 u64 xstate_fx_sw_bytes[USER_XSTATE_FX_SW_WORDS];
1310
1311 void __init update_regset_xstate_info(unsigned int size, u64 xstate_mask)
1312 {
1313 #ifdef CONFIG_X86_64
1314 x86_64_regsets[REGSET_XSTATE].n = size / sizeof(u64);
1315 #endif
1316 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1317 x86_32_regsets[REGSET_XSTATE].n = size / sizeof(u64);
1318 #endif
1319 xstate_fx_sw_bytes[USER_XSTATE_XCR0_WORD] = xstate_mask;
1320 }
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
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
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 }