]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - arch/x86/kernel/step.c
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs
[mirror_ubuntu-bionic-kernel.git] / arch / x86 / kernel / step.c
CommitLineData
fa1e03ea
RM
1/*
2 * x86 single-step support code, common to 32-bit and 64-bit.
3 */
4#include <linux/sched.h>
68db0cf1 5#include <linux/sched/task_stack.h>
fa1e03ea
RM
6#include <linux/mm.h>
7#include <linux/ptrace.h>
254e0a6b 8#include <asm/desc.h>
37868fe1 9#include <asm/mmu_context.h>
fa1e03ea 10
37cd9cf3 11unsigned long convert_ip_to_linear(struct task_struct *child, struct pt_regs *regs)
fa1e03ea
RM
12{
13 unsigned long addr, seg;
14
65ea5b03 15 addr = regs->ip;
fa1e03ea 16 seg = regs->cs & 0xffff;
65ea5b03 17 if (v8086_mode(regs)) {
7122ec81
RM
18 addr = (addr & 0xffff) + (seg << 4);
19 return addr;
20 }
fa1e03ea 21
a5b9e5a2 22#ifdef CONFIG_MODIFY_LDT_SYSCALL
fa1e03ea
RM
23 /*
24 * We'll assume that the code segments in the GDT
25 * are all zero-based. That is largely true: the
26 * TLS segments are used for data, and the PNPBIOS
27 * and APM bios ones we just ignore here.
28 */
3f80c1ad 29 if ((seg & SEGMENT_TI_MASK) == SEGMENT_LDT) {
254e0a6b 30 struct desc_struct *desc;
fa1e03ea
RM
31 unsigned long base;
32
136d9d83 33 seg >>= 3;
fa1e03ea
RM
34
35 mutex_lock(&child->mm->context.lock);
37868fe1 36 if (unlikely(!child->mm->context.ldt ||
136d9d83 37 seg >= child->mm->context.ldt->size))
fa1e03ea
RM
38 addr = -1L; /* bogus selector, access would fault */
39 else {
37868fe1 40 desc = &child->mm->context.ldt->entries[seg];
254e0a6b 41 base = get_desc_base(desc);
fa1e03ea
RM
42
43 /* 16-bit code segment? */
254e0a6b 44 if (!desc->d)
fa1e03ea
RM
45 addr &= 0xffff;
46 addr += base;
47 }
48 mutex_unlock(&child->mm->context.lock);
49 }
a5b9e5a2 50#endif
fa1e03ea
RM
51
52 return addr;
53}
54
55static int is_setting_trap_flag(struct task_struct *child, struct pt_regs *regs)
56{
57 int i, copied;
58 unsigned char opcode[15];
37cd9cf3 59 unsigned long addr = convert_ip_to_linear(child, regs);
fa1e03ea 60
f307ab6d
LS
61 copied = access_process_vm(child, addr, opcode, sizeof(opcode),
62 FOLL_FORCE);
fa1e03ea
RM
63 for (i = 0; i < copied; i++) {
64 switch (opcode[i]) {
65 /* popf and iret */
66 case 0x9d: case 0xcf:
67 return 1;
68
69 /* CHECKME: 64 65 */
70
71 /* opcode and address size prefixes */
72 case 0x66: case 0x67:
73 continue;
74 /* irrelevant prefixes (segment overrides and repeats) */
75 case 0x26: case 0x2e:
76 case 0x36: case 0x3e:
77 case 0x64: case 0x65:
5f76cb1f 78 case 0xf0: case 0xf2: case 0xf3:
fa1e03ea
RM
79 continue;
80
7122ec81 81#ifdef CONFIG_X86_64
fa1e03ea 82 case 0x40 ... 0x4f:
318f5a2a 83 if (!user_64bit_mode(regs))
fa1e03ea
RM
84 /* 32-bit mode: register increment */
85 return 0;
86 /* 64-bit mode: REX prefix */
87 continue;
7122ec81 88#endif
fa1e03ea
RM
89
90 /* CHECKME: f2, f3 */
91
92 /*
93 * pushf: NOTE! We should probably not let
94 * the user see the TF bit being set. But
95 * it's more pain than it's worth to avoid
96 * it, and a debugger could emulate this
97 * all in user space if it _really_ cares.
98 */
99 case 0x9c:
100 default:
101 return 0;
102 }
103 }
104 return 0;
105}
106
10faa81e
RM
107/*
108 * Enable single-stepping. Return nonzero if user mode is not using TF itself.
109 */
110static int enable_single_step(struct task_struct *child)
fa1e03ea
RM
111{
112 struct pt_regs *regs = task_pt_regs(child);
6718d0d6 113 unsigned long oflags;
fa1e03ea 114
380fdd75
RM
115 /*
116 * If we stepped into a sysenter/syscall insn, it trapped in
117 * kernel mode; do_debug() cleared TF and set TIF_SINGLESTEP.
118 * If user-mode had set TF itself, then it's still clear from
119 * do_debug() and we need to set it again to restore the user
120 * state so we don't wrongly set TIF_FORCED_TF below.
121 * If enable_single_step() was used last and that is what
122 * set TIF_SINGLESTEP, then both TF and TIF_FORCED_TF are
123 * already set and our bookkeeping is fine.
124 */
125 if (unlikely(test_tsk_thread_flag(child, TIF_SINGLESTEP)))
126 regs->flags |= X86_EFLAGS_TF;
127
fa1e03ea
RM
128 /*
129 * Always set TIF_SINGLESTEP - this guarantees that
130 * we single-step system calls etc.. This will also
131 * cause us to set TF when returning to user mode.
132 */
133 set_tsk_thread_flag(child, TIF_SINGLESTEP);
134
6718d0d6 135 oflags = regs->flags;
fa1e03ea
RM
136
137 /* Set TF on the kernel stack.. */
65ea5b03 138 regs->flags |= X86_EFLAGS_TF;
fa1e03ea
RM
139
140 /*
141 * ..but if TF is changed by the instruction we will trace,
142 * don't mark it as being "us" that set it, so that we
143 * won't clear it by hand later.
6718d0d6
RM
144 *
145 * Note that if we don't actually execute the popf because
146 * of a signal arriving right now or suchlike, we will lose
147 * track of the fact that it really was "us" that set it.
fa1e03ea 148 */
6718d0d6
RM
149 if (is_setting_trap_flag(child, regs)) {
150 clear_tsk_thread_flag(child, TIF_FORCED_TF);
10faa81e 151 return 0;
6718d0d6
RM
152 }
153
154 /*
155 * If TF was already set, check whether it was us who set it.
156 * If not, we should never attempt a block step.
157 */
158 if (oflags & X86_EFLAGS_TF)
159 return test_tsk_thread_flag(child, TIF_FORCED_TF);
fa1e03ea 160
e1f28773 161 set_tsk_thread_flag(child, TIF_FORCED_TF);
10faa81e
RM
162
163 return 1;
164}
165
9bd1190a 166void set_task_blockstep(struct task_struct *task, bool on)
848e8f5f
ON
167{
168 unsigned long debugctl;
169
95cf00fa
ON
170 /*
171 * Ensure irq/preemption can't change debugctl in between.
172 * Note also that both TIF_BLOCKSTEP and debugctl should
173 * be changed atomically wrt preemption.
9899d11f
ON
174 *
175 * NOTE: this means that set/clear TIF_BLOCKSTEP is only safe if
176 * task is current or it can't be running, otherwise we can race
177 * with __switch_to_xtra(). We rely on ptrace_freeze_traced() but
178 * PTRACE_KILL is not safe.
95cf00fa
ON
179 */
180 local_irq_disable();
848e8f5f
ON
181 debugctl = get_debugctlmsr();
182 if (on) {
183 debugctl |= DEBUGCTLMSR_BTF;
184 set_tsk_thread_flag(task, TIF_BLOCKSTEP);
185 } else {
186 debugctl &= ~DEBUGCTLMSR_BTF;
187 clear_tsk_thread_flag(task, TIF_BLOCKSTEP);
188 }
95cf00fa
ON
189 if (task == current)
190 update_debugctlmsr(debugctl);
191 local_irq_enable();
848e8f5f
ON
192}
193
10faa81e
RM
194/*
195 * Enable single or block step.
196 */
197static void enable_step(struct task_struct *child, bool block)
198{
199 /*
200 * Make sure block stepping (BTF) is not enabled unless it should be.
201 * Note that we don't try to worry about any is_setting_trap_flag()
202 * instructions after the first when using block stepping.
0d2eb44f 203 * So no one should try to use debugger block stepping in a program
10faa81e
RM
204 * that uses user-mode single stepping itself.
205 */
848e8f5f
ON
206 if (enable_single_step(child) && block)
207 set_task_blockstep(child, true);
208 else if (test_tsk_thread_flag(child, TIF_BLOCKSTEP))
209 set_task_blockstep(child, false);
10faa81e
RM
210}
211
212void user_enable_single_step(struct task_struct *child)
213{
214 enable_step(child, 0);
215}
216
217void user_enable_block_step(struct task_struct *child)
218{
219 enable_step(child, 1);
fa1e03ea
RM
220}
221
222void user_disable_single_step(struct task_struct *child)
223{
10faa81e
RM
224 /*
225 * Make sure block stepping (BTF) is disabled.
226 */
848e8f5f
ON
227 if (test_tsk_thread_flag(child, TIF_BLOCKSTEP))
228 set_task_blockstep(child, false);
10faa81e 229
fa1e03ea
RM
230 /* Always clear TIF_SINGLESTEP... */
231 clear_tsk_thread_flag(child, TIF_SINGLESTEP);
232
233 /* But touch TF only if it was set by us.. */
e1f28773 234 if (test_and_clear_tsk_thread_flag(child, TIF_FORCED_TF))
65ea5b03 235 task_pt_regs(child)->flags &= ~X86_EFLAGS_TF;
fa1e03ea 236}