xref: /openbmc/linux/arch/x86/kernel/step.c (revision 380fdd7585a4c2f41b48925eba85c0654b7b858b)
1fa1e03eaSRoland McGrath /*
2fa1e03eaSRoland McGrath  * x86 single-step support code, common to 32-bit and 64-bit.
3fa1e03eaSRoland McGrath  */
4fa1e03eaSRoland McGrath #include <linux/sched.h>
5fa1e03eaSRoland McGrath #include <linux/mm.h>
6fa1e03eaSRoland McGrath #include <linux/ptrace.h>
7fa1e03eaSRoland McGrath 
837cd9cf3SHarvey Harrison unsigned long convert_ip_to_linear(struct task_struct *child, struct pt_regs *regs)
9fa1e03eaSRoland McGrath {
10fa1e03eaSRoland McGrath 	unsigned long addr, seg;
11fa1e03eaSRoland McGrath 
1265ea5b03SH. Peter Anvin 	addr = regs->ip;
13fa1e03eaSRoland McGrath 	seg = regs->cs & 0xffff;
1465ea5b03SH. Peter Anvin 	if (v8086_mode(regs)) {
157122ec81SRoland McGrath 		addr = (addr & 0xffff) + (seg << 4);
167122ec81SRoland McGrath 		return addr;
177122ec81SRoland McGrath 	}
18fa1e03eaSRoland McGrath 
19fa1e03eaSRoland McGrath 	/*
20fa1e03eaSRoland McGrath 	 * We'll assume that the code segments in the GDT
21fa1e03eaSRoland McGrath 	 * are all zero-based. That is largely true: the
22fa1e03eaSRoland McGrath 	 * TLS segments are used for data, and the PNPBIOS
23fa1e03eaSRoland McGrath 	 * and APM bios ones we just ignore here.
24fa1e03eaSRoland McGrath 	 */
253f80c1adSRoland McGrath 	if ((seg & SEGMENT_TI_MASK) == SEGMENT_LDT) {
26fa1e03eaSRoland McGrath 		u32 *desc;
27fa1e03eaSRoland McGrath 		unsigned long base;
28fa1e03eaSRoland McGrath 
29fa1e03eaSRoland McGrath 		seg &= ~7UL;
30fa1e03eaSRoland McGrath 
31fa1e03eaSRoland McGrath 		mutex_lock(&child->mm->context.lock);
32fa1e03eaSRoland McGrath 		if (unlikely((seg >> 3) >= child->mm->context.size))
33fa1e03eaSRoland McGrath 			addr = -1L; /* bogus selector, access would fault */
34fa1e03eaSRoland McGrath 		else {
35fa1e03eaSRoland McGrath 			desc = child->mm->context.ldt + seg;
36fa1e03eaSRoland McGrath 			base = ((desc[0] >> 16) |
37fa1e03eaSRoland McGrath 				((desc[1] & 0xff) << 16) |
38fa1e03eaSRoland McGrath 				(desc[1] & 0xff000000));
39fa1e03eaSRoland McGrath 
40fa1e03eaSRoland McGrath 			/* 16-bit code segment? */
41fa1e03eaSRoland McGrath 			if (!((desc[1] >> 22) & 1))
42fa1e03eaSRoland McGrath 				addr &= 0xffff;
43fa1e03eaSRoland McGrath 			addr += base;
44fa1e03eaSRoland McGrath 		}
45fa1e03eaSRoland McGrath 		mutex_unlock(&child->mm->context.lock);
46fa1e03eaSRoland McGrath 	}
47fa1e03eaSRoland McGrath 
48fa1e03eaSRoland McGrath 	return addr;
49fa1e03eaSRoland McGrath }
50fa1e03eaSRoland McGrath 
51fa1e03eaSRoland McGrath static int is_setting_trap_flag(struct task_struct *child, struct pt_regs *regs)
52fa1e03eaSRoland McGrath {
53fa1e03eaSRoland McGrath 	int i, copied;
54fa1e03eaSRoland McGrath 	unsigned char opcode[15];
5537cd9cf3SHarvey Harrison 	unsigned long addr = convert_ip_to_linear(child, regs);
56fa1e03eaSRoland McGrath 
57fa1e03eaSRoland McGrath 	copied = access_process_vm(child, addr, opcode, sizeof(opcode), 0);
58fa1e03eaSRoland McGrath 	for (i = 0; i < copied; i++) {
59fa1e03eaSRoland McGrath 		switch (opcode[i]) {
60fa1e03eaSRoland McGrath 		/* popf and iret */
61fa1e03eaSRoland McGrath 		case 0x9d: case 0xcf:
62fa1e03eaSRoland McGrath 			return 1;
63fa1e03eaSRoland McGrath 
64fa1e03eaSRoland McGrath 			/* CHECKME: 64 65 */
65fa1e03eaSRoland McGrath 
66fa1e03eaSRoland McGrath 		/* opcode and address size prefixes */
67fa1e03eaSRoland McGrath 		case 0x66: case 0x67:
68fa1e03eaSRoland McGrath 			continue;
69fa1e03eaSRoland McGrath 		/* irrelevant prefixes (segment overrides and repeats) */
70fa1e03eaSRoland McGrath 		case 0x26: case 0x2e:
71fa1e03eaSRoland McGrath 		case 0x36: case 0x3e:
72fa1e03eaSRoland McGrath 		case 0x64: case 0x65:
735f76cb1fSRoland McGrath 		case 0xf0: case 0xf2: case 0xf3:
74fa1e03eaSRoland McGrath 			continue;
75fa1e03eaSRoland McGrath 
767122ec81SRoland McGrath #ifdef CONFIG_X86_64
77fa1e03eaSRoland McGrath 		case 0x40 ... 0x4f:
78fa1e03eaSRoland McGrath 			if (regs->cs != __USER_CS)
79fa1e03eaSRoland McGrath 				/* 32-bit mode: register increment */
80fa1e03eaSRoland McGrath 				return 0;
81fa1e03eaSRoland McGrath 			/* 64-bit mode: REX prefix */
82fa1e03eaSRoland McGrath 			continue;
837122ec81SRoland McGrath #endif
84fa1e03eaSRoland McGrath 
85fa1e03eaSRoland McGrath 			/* CHECKME: f2, f3 */
86fa1e03eaSRoland McGrath 
87fa1e03eaSRoland McGrath 		/*
88fa1e03eaSRoland McGrath 		 * pushf: NOTE! We should probably not let
89fa1e03eaSRoland McGrath 		 * the user see the TF bit being set. But
90fa1e03eaSRoland McGrath 		 * it's more pain than it's worth to avoid
91fa1e03eaSRoland McGrath 		 * it, and a debugger could emulate this
92fa1e03eaSRoland McGrath 		 * all in user space if it _really_ cares.
93fa1e03eaSRoland McGrath 		 */
94fa1e03eaSRoland McGrath 		case 0x9c:
95fa1e03eaSRoland McGrath 		default:
96fa1e03eaSRoland McGrath 			return 0;
97fa1e03eaSRoland McGrath 		}
98fa1e03eaSRoland McGrath 	}
99fa1e03eaSRoland McGrath 	return 0;
100fa1e03eaSRoland McGrath }
101fa1e03eaSRoland McGrath 
10210faa81eSRoland McGrath /*
10310faa81eSRoland McGrath  * Enable single-stepping.  Return nonzero if user mode is not using TF itself.
10410faa81eSRoland McGrath  */
10510faa81eSRoland McGrath static int enable_single_step(struct task_struct *child)
106fa1e03eaSRoland McGrath {
107fa1e03eaSRoland McGrath 	struct pt_regs *regs = task_pt_regs(child);
1086718d0d6SRoland McGrath 	unsigned long oflags;
109fa1e03eaSRoland McGrath 
110fa1e03eaSRoland McGrath 	/*
111*380fdd75SRoland McGrath 	 * If we stepped into a sysenter/syscall insn, it trapped in
112*380fdd75SRoland McGrath 	 * kernel mode; do_debug() cleared TF and set TIF_SINGLESTEP.
113*380fdd75SRoland McGrath 	 * If user-mode had set TF itself, then it's still clear from
114*380fdd75SRoland McGrath 	 * do_debug() and we need to set it again to restore the user
115*380fdd75SRoland McGrath 	 * state so we don't wrongly set TIF_FORCED_TF below.
116*380fdd75SRoland McGrath 	 * If enable_single_step() was used last and that is what
117*380fdd75SRoland McGrath 	 * set TIF_SINGLESTEP, then both TF and TIF_FORCED_TF are
118*380fdd75SRoland McGrath 	 * already set and our bookkeeping is fine.
119*380fdd75SRoland McGrath 	 */
120*380fdd75SRoland McGrath 	if (unlikely(test_tsk_thread_flag(child, TIF_SINGLESTEP)))
121*380fdd75SRoland McGrath 		regs->flags |= X86_EFLAGS_TF;
122*380fdd75SRoland McGrath 
123*380fdd75SRoland McGrath 	/*
124fa1e03eaSRoland McGrath 	 * Always set TIF_SINGLESTEP - this guarantees that
125fa1e03eaSRoland McGrath 	 * we single-step system calls etc..  This will also
126fa1e03eaSRoland McGrath 	 * cause us to set TF when returning to user mode.
127fa1e03eaSRoland McGrath 	 */
128fa1e03eaSRoland McGrath 	set_tsk_thread_flag(child, TIF_SINGLESTEP);
129fa1e03eaSRoland McGrath 
1306718d0d6SRoland McGrath 	oflags = regs->flags;
131fa1e03eaSRoland McGrath 
132fa1e03eaSRoland McGrath 	/* Set TF on the kernel stack.. */
13365ea5b03SH. Peter Anvin 	regs->flags |= X86_EFLAGS_TF;
134fa1e03eaSRoland McGrath 
135fa1e03eaSRoland McGrath 	/*
136fa1e03eaSRoland McGrath 	 * ..but if TF is changed by the instruction we will trace,
137fa1e03eaSRoland McGrath 	 * don't mark it as being "us" that set it, so that we
138fa1e03eaSRoland McGrath 	 * won't clear it by hand later.
1396718d0d6SRoland McGrath 	 *
1406718d0d6SRoland McGrath 	 * Note that if we don't actually execute the popf because
1416718d0d6SRoland McGrath 	 * of a signal arriving right now or suchlike, we will lose
1426718d0d6SRoland McGrath 	 * track of the fact that it really was "us" that set it.
143fa1e03eaSRoland McGrath 	 */
1446718d0d6SRoland McGrath 	if (is_setting_trap_flag(child, regs)) {
1456718d0d6SRoland McGrath 		clear_tsk_thread_flag(child, TIF_FORCED_TF);
14610faa81eSRoland McGrath 		return 0;
1476718d0d6SRoland McGrath 	}
1486718d0d6SRoland McGrath 
1496718d0d6SRoland McGrath 	/*
1506718d0d6SRoland McGrath 	 * If TF was already set, check whether it was us who set it.
1516718d0d6SRoland McGrath 	 * If not, we should never attempt a block step.
1526718d0d6SRoland McGrath 	 */
1536718d0d6SRoland McGrath 	if (oflags & X86_EFLAGS_TF)
1546718d0d6SRoland McGrath 		return test_tsk_thread_flag(child, TIF_FORCED_TF);
155fa1e03eaSRoland McGrath 
156e1f28773SRoland McGrath 	set_tsk_thread_flag(child, TIF_FORCED_TF);
15710faa81eSRoland McGrath 
15810faa81eSRoland McGrath 	return 1;
15910faa81eSRoland McGrath }
16010faa81eSRoland McGrath 
16110faa81eSRoland McGrath /*
16210faa81eSRoland McGrath  * Install this value in MSR_IA32_DEBUGCTLMSR whenever child is running.
16310faa81eSRoland McGrath  */
16410faa81eSRoland McGrath static void write_debugctlmsr(struct task_struct *child, unsigned long val)
16510faa81eSRoland McGrath {
1664ba51fd7SRoland McGrath 	if (child->thread.debugctlmsr == val)
1674ba51fd7SRoland McGrath 		return;
1684ba51fd7SRoland McGrath 
16910faa81eSRoland McGrath 	child->thread.debugctlmsr = val;
17010faa81eSRoland McGrath 
17110faa81eSRoland McGrath 	if (child != current)
17210faa81eSRoland McGrath 		return;
17310faa81eSRoland McGrath 
1745b0e5084SJan Beulich 	update_debugctlmsr(val);
17510faa81eSRoland McGrath }
17610faa81eSRoland McGrath 
17710faa81eSRoland McGrath /*
17810faa81eSRoland McGrath  * Enable single or block step.
17910faa81eSRoland McGrath  */
18010faa81eSRoland McGrath static void enable_step(struct task_struct *child, bool block)
18110faa81eSRoland McGrath {
18210faa81eSRoland McGrath 	/*
18310faa81eSRoland McGrath 	 * Make sure block stepping (BTF) is not enabled unless it should be.
18410faa81eSRoland McGrath 	 * Note that we don't try to worry about any is_setting_trap_flag()
18510faa81eSRoland McGrath 	 * instructions after the first when using block stepping.
18610faa81eSRoland McGrath 	 * So noone should try to use debugger block stepping in a program
18710faa81eSRoland McGrath 	 * that uses user-mode single stepping itself.
18810faa81eSRoland McGrath 	 */
18910faa81eSRoland McGrath 	if (enable_single_step(child) && block) {
19010faa81eSRoland McGrath 		set_tsk_thread_flag(child, TIF_DEBUGCTLMSR);
191eee3af4aSMarkus Metzger 		write_debugctlmsr(child,
192eee3af4aSMarkus Metzger 				  child->thread.debugctlmsr | DEBUGCTLMSR_BTF);
193eee3af4aSMarkus Metzger 	} else {
194eee3af4aSMarkus Metzger 		write_debugctlmsr(child,
195d032b31aSJan Beulich 				  child->thread.debugctlmsr & ~DEBUGCTLMSR_BTF);
196eee3af4aSMarkus Metzger 
197eee3af4aSMarkus Metzger 		if (!child->thread.debugctlmsr)
198eee3af4aSMarkus Metzger 			clear_tsk_thread_flag(child, TIF_DEBUGCTLMSR);
19910faa81eSRoland McGrath 	}
20010faa81eSRoland McGrath }
20110faa81eSRoland McGrath 
20210faa81eSRoland McGrath void user_enable_single_step(struct task_struct *child)
20310faa81eSRoland McGrath {
20410faa81eSRoland McGrath 	enable_step(child, 0);
20510faa81eSRoland McGrath }
20610faa81eSRoland McGrath 
20710faa81eSRoland McGrath void user_enable_block_step(struct task_struct *child)
20810faa81eSRoland McGrath {
20910faa81eSRoland McGrath 	enable_step(child, 1);
210fa1e03eaSRoland McGrath }
211fa1e03eaSRoland McGrath 
212fa1e03eaSRoland McGrath void user_disable_single_step(struct task_struct *child)
213fa1e03eaSRoland McGrath {
21410faa81eSRoland McGrath 	/*
21510faa81eSRoland McGrath 	 * Make sure block stepping (BTF) is disabled.
21610faa81eSRoland McGrath 	 */
217eee3af4aSMarkus Metzger 	write_debugctlmsr(child,
218d032b31aSJan Beulich 			  child->thread.debugctlmsr & ~DEBUGCTLMSR_BTF);
219eee3af4aSMarkus Metzger 
220eee3af4aSMarkus Metzger 	if (!child->thread.debugctlmsr)
221eee3af4aSMarkus Metzger 		clear_tsk_thread_flag(child, TIF_DEBUGCTLMSR);
22210faa81eSRoland McGrath 
223fa1e03eaSRoland McGrath 	/* Always clear TIF_SINGLESTEP... */
224fa1e03eaSRoland McGrath 	clear_tsk_thread_flag(child, TIF_SINGLESTEP);
225fa1e03eaSRoland McGrath 
226fa1e03eaSRoland McGrath 	/* But touch TF only if it was set by us.. */
227e1f28773SRoland McGrath 	if (test_and_clear_tsk_thread_flag(child, TIF_FORCED_TF))
22865ea5b03SH. Peter Anvin 		task_pt_regs(child)->flags &= ~X86_EFLAGS_TF;
229fa1e03eaSRoland McGrath }
230