xref: /openbmc/linux/arch/x86/kernel/step.c (revision 254e0a6bff87ab8b22293c4bd1443507df698407)
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>
7*254e0a6bSAkinobu Mita #include <asm/desc.h>
8fa1e03eaSRoland McGrath 
937cd9cf3SHarvey Harrison unsigned long convert_ip_to_linear(struct task_struct *child, struct pt_regs *regs)
10fa1e03eaSRoland McGrath {
11fa1e03eaSRoland McGrath 	unsigned long addr, seg;
12fa1e03eaSRoland McGrath 
1365ea5b03SH. Peter Anvin 	addr = regs->ip;
14fa1e03eaSRoland McGrath 	seg = regs->cs & 0xffff;
1565ea5b03SH. Peter Anvin 	if (v8086_mode(regs)) {
167122ec81SRoland McGrath 		addr = (addr & 0xffff) + (seg << 4);
177122ec81SRoland McGrath 		return addr;
187122ec81SRoland McGrath 	}
19fa1e03eaSRoland McGrath 
20fa1e03eaSRoland McGrath 	/*
21fa1e03eaSRoland McGrath 	 * We'll assume that the code segments in the GDT
22fa1e03eaSRoland McGrath 	 * are all zero-based. That is largely true: the
23fa1e03eaSRoland McGrath 	 * TLS segments are used for data, and the PNPBIOS
24fa1e03eaSRoland McGrath 	 * and APM bios ones we just ignore here.
25fa1e03eaSRoland McGrath 	 */
263f80c1adSRoland McGrath 	if ((seg & SEGMENT_TI_MASK) == SEGMENT_LDT) {
27*254e0a6bSAkinobu Mita 		struct desc_struct *desc;
28fa1e03eaSRoland McGrath 		unsigned long base;
29fa1e03eaSRoland McGrath 
30fa1e03eaSRoland McGrath 		seg &= ~7UL;
31fa1e03eaSRoland McGrath 
32fa1e03eaSRoland McGrath 		mutex_lock(&child->mm->context.lock);
33fa1e03eaSRoland McGrath 		if (unlikely((seg >> 3) >= child->mm->context.size))
34fa1e03eaSRoland McGrath 			addr = -1L; /* bogus selector, access would fault */
35fa1e03eaSRoland McGrath 		else {
36fa1e03eaSRoland McGrath 			desc = child->mm->context.ldt + seg;
37*254e0a6bSAkinobu Mita 			base = get_desc_base(desc);
38fa1e03eaSRoland McGrath 
39fa1e03eaSRoland McGrath 			/* 16-bit code segment? */
40*254e0a6bSAkinobu Mita 			if (!desc->d)
41fa1e03eaSRoland McGrath 				addr &= 0xffff;
42fa1e03eaSRoland McGrath 			addr += base;
43fa1e03eaSRoland McGrath 		}
44fa1e03eaSRoland McGrath 		mutex_unlock(&child->mm->context.lock);
45fa1e03eaSRoland McGrath 	}
46fa1e03eaSRoland McGrath 
47fa1e03eaSRoland McGrath 	return addr;
48fa1e03eaSRoland McGrath }
49fa1e03eaSRoland McGrath 
50fa1e03eaSRoland McGrath static int is_setting_trap_flag(struct task_struct *child, struct pt_regs *regs)
51fa1e03eaSRoland McGrath {
52fa1e03eaSRoland McGrath 	int i, copied;
53fa1e03eaSRoland McGrath 	unsigned char opcode[15];
5437cd9cf3SHarvey Harrison 	unsigned long addr = convert_ip_to_linear(child, regs);
55fa1e03eaSRoland McGrath 
56fa1e03eaSRoland McGrath 	copied = access_process_vm(child, addr, opcode, sizeof(opcode), 0);
57fa1e03eaSRoland McGrath 	for (i = 0; i < copied; i++) {
58fa1e03eaSRoland McGrath 		switch (opcode[i]) {
59fa1e03eaSRoland McGrath 		/* popf and iret */
60fa1e03eaSRoland McGrath 		case 0x9d: case 0xcf:
61fa1e03eaSRoland McGrath 			return 1;
62fa1e03eaSRoland McGrath 
63fa1e03eaSRoland McGrath 			/* CHECKME: 64 65 */
64fa1e03eaSRoland McGrath 
65fa1e03eaSRoland McGrath 		/* opcode and address size prefixes */
66fa1e03eaSRoland McGrath 		case 0x66: case 0x67:
67fa1e03eaSRoland McGrath 			continue;
68fa1e03eaSRoland McGrath 		/* irrelevant prefixes (segment overrides and repeats) */
69fa1e03eaSRoland McGrath 		case 0x26: case 0x2e:
70fa1e03eaSRoland McGrath 		case 0x36: case 0x3e:
71fa1e03eaSRoland McGrath 		case 0x64: case 0x65:
725f76cb1fSRoland McGrath 		case 0xf0: case 0xf2: case 0xf3:
73fa1e03eaSRoland McGrath 			continue;
74fa1e03eaSRoland McGrath 
757122ec81SRoland McGrath #ifdef CONFIG_X86_64
76fa1e03eaSRoland McGrath 		case 0x40 ... 0x4f:
77fa1e03eaSRoland McGrath 			if (regs->cs != __USER_CS)
78fa1e03eaSRoland McGrath 				/* 32-bit mode: register increment */
79fa1e03eaSRoland McGrath 				return 0;
80fa1e03eaSRoland McGrath 			/* 64-bit mode: REX prefix */
81fa1e03eaSRoland McGrath 			continue;
827122ec81SRoland McGrath #endif
83fa1e03eaSRoland McGrath 
84fa1e03eaSRoland McGrath 			/* CHECKME: f2, f3 */
85fa1e03eaSRoland McGrath 
86fa1e03eaSRoland McGrath 		/*
87fa1e03eaSRoland McGrath 		 * pushf: NOTE! We should probably not let
88fa1e03eaSRoland McGrath 		 * the user see the TF bit being set. But
89fa1e03eaSRoland McGrath 		 * it's more pain than it's worth to avoid
90fa1e03eaSRoland McGrath 		 * it, and a debugger could emulate this
91fa1e03eaSRoland McGrath 		 * all in user space if it _really_ cares.
92fa1e03eaSRoland McGrath 		 */
93fa1e03eaSRoland McGrath 		case 0x9c:
94fa1e03eaSRoland McGrath 		default:
95fa1e03eaSRoland McGrath 			return 0;
96fa1e03eaSRoland McGrath 		}
97fa1e03eaSRoland McGrath 	}
98fa1e03eaSRoland McGrath 	return 0;
99fa1e03eaSRoland McGrath }
100fa1e03eaSRoland McGrath 
10110faa81eSRoland McGrath /*
10210faa81eSRoland McGrath  * Enable single-stepping.  Return nonzero if user mode is not using TF itself.
10310faa81eSRoland McGrath  */
10410faa81eSRoland McGrath static int enable_single_step(struct task_struct *child)
105fa1e03eaSRoland McGrath {
106fa1e03eaSRoland McGrath 	struct pt_regs *regs = task_pt_regs(child);
1076718d0d6SRoland McGrath 	unsigned long oflags;
108fa1e03eaSRoland McGrath 
109fa1e03eaSRoland McGrath 	/*
110380fdd75SRoland McGrath 	 * If we stepped into a sysenter/syscall insn, it trapped in
111380fdd75SRoland McGrath 	 * kernel mode; do_debug() cleared TF and set TIF_SINGLESTEP.
112380fdd75SRoland McGrath 	 * If user-mode had set TF itself, then it's still clear from
113380fdd75SRoland McGrath 	 * do_debug() and we need to set it again to restore the user
114380fdd75SRoland McGrath 	 * state so we don't wrongly set TIF_FORCED_TF below.
115380fdd75SRoland McGrath 	 * If enable_single_step() was used last and that is what
116380fdd75SRoland McGrath 	 * set TIF_SINGLESTEP, then both TF and TIF_FORCED_TF are
117380fdd75SRoland McGrath 	 * already set and our bookkeeping is fine.
118380fdd75SRoland McGrath 	 */
119380fdd75SRoland McGrath 	if (unlikely(test_tsk_thread_flag(child, TIF_SINGLESTEP)))
120380fdd75SRoland McGrath 		regs->flags |= X86_EFLAGS_TF;
121380fdd75SRoland McGrath 
122380fdd75SRoland McGrath 	/*
123fa1e03eaSRoland McGrath 	 * Always set TIF_SINGLESTEP - this guarantees that
124fa1e03eaSRoland McGrath 	 * we single-step system calls etc..  This will also
125fa1e03eaSRoland McGrath 	 * cause us to set TF when returning to user mode.
126fa1e03eaSRoland McGrath 	 */
127fa1e03eaSRoland McGrath 	set_tsk_thread_flag(child, TIF_SINGLESTEP);
128fa1e03eaSRoland McGrath 
1296718d0d6SRoland McGrath 	oflags = regs->flags;
130fa1e03eaSRoland McGrath 
131fa1e03eaSRoland McGrath 	/* Set TF on the kernel stack.. */
13265ea5b03SH. Peter Anvin 	regs->flags |= X86_EFLAGS_TF;
133fa1e03eaSRoland McGrath 
134fa1e03eaSRoland McGrath 	/*
135fa1e03eaSRoland McGrath 	 * ..but if TF is changed by the instruction we will trace,
136fa1e03eaSRoland McGrath 	 * don't mark it as being "us" that set it, so that we
137fa1e03eaSRoland McGrath 	 * won't clear it by hand later.
1386718d0d6SRoland McGrath 	 *
1396718d0d6SRoland McGrath 	 * Note that if we don't actually execute the popf because
1406718d0d6SRoland McGrath 	 * of a signal arriving right now or suchlike, we will lose
1416718d0d6SRoland McGrath 	 * track of the fact that it really was "us" that set it.
142fa1e03eaSRoland McGrath 	 */
1436718d0d6SRoland McGrath 	if (is_setting_trap_flag(child, regs)) {
1446718d0d6SRoland McGrath 		clear_tsk_thread_flag(child, TIF_FORCED_TF);
14510faa81eSRoland McGrath 		return 0;
1466718d0d6SRoland McGrath 	}
1476718d0d6SRoland McGrath 
1486718d0d6SRoland McGrath 	/*
1496718d0d6SRoland McGrath 	 * If TF was already set, check whether it was us who set it.
1506718d0d6SRoland McGrath 	 * If not, we should never attempt a block step.
1516718d0d6SRoland McGrath 	 */
1526718d0d6SRoland McGrath 	if (oflags & X86_EFLAGS_TF)
1536718d0d6SRoland McGrath 		return test_tsk_thread_flag(child, TIF_FORCED_TF);
154fa1e03eaSRoland McGrath 
155e1f28773SRoland McGrath 	set_tsk_thread_flag(child, TIF_FORCED_TF);
15610faa81eSRoland McGrath 
15710faa81eSRoland McGrath 	return 1;
15810faa81eSRoland McGrath }
15910faa81eSRoland McGrath 
16010faa81eSRoland McGrath /*
16110faa81eSRoland McGrath  * Install this value in MSR_IA32_DEBUGCTLMSR whenever child is running.
16210faa81eSRoland McGrath  */
16310faa81eSRoland McGrath static void write_debugctlmsr(struct task_struct *child, unsigned long val)
16410faa81eSRoland McGrath {
1654ba51fd7SRoland McGrath 	if (child->thread.debugctlmsr == val)
1664ba51fd7SRoland McGrath 		return;
1674ba51fd7SRoland McGrath 
16810faa81eSRoland McGrath 	child->thread.debugctlmsr = val;
16910faa81eSRoland McGrath 
17010faa81eSRoland McGrath 	if (child != current)
17110faa81eSRoland McGrath 		return;
17210faa81eSRoland McGrath 
1735b0e5084SJan Beulich 	update_debugctlmsr(val);
17410faa81eSRoland McGrath }
17510faa81eSRoland McGrath 
17610faa81eSRoland McGrath /*
17710faa81eSRoland McGrath  * Enable single or block step.
17810faa81eSRoland McGrath  */
17910faa81eSRoland McGrath static void enable_step(struct task_struct *child, bool block)
18010faa81eSRoland McGrath {
18110faa81eSRoland McGrath 	/*
18210faa81eSRoland McGrath 	 * Make sure block stepping (BTF) is not enabled unless it should be.
18310faa81eSRoland McGrath 	 * Note that we don't try to worry about any is_setting_trap_flag()
18410faa81eSRoland McGrath 	 * instructions after the first when using block stepping.
18510faa81eSRoland McGrath 	 * So noone should try to use debugger block stepping in a program
18610faa81eSRoland McGrath 	 * that uses user-mode single stepping itself.
18710faa81eSRoland McGrath 	 */
18810faa81eSRoland McGrath 	if (enable_single_step(child) && block) {
18910faa81eSRoland McGrath 		set_tsk_thread_flag(child, TIF_DEBUGCTLMSR);
190eee3af4aSMarkus Metzger 		write_debugctlmsr(child,
191eee3af4aSMarkus Metzger 				  child->thread.debugctlmsr | DEBUGCTLMSR_BTF);
192eee3af4aSMarkus Metzger 	} else {
193eee3af4aSMarkus Metzger 		write_debugctlmsr(child,
194d032b31aSJan Beulich 				  child->thread.debugctlmsr & ~DEBUGCTLMSR_BTF);
195eee3af4aSMarkus Metzger 
196eee3af4aSMarkus Metzger 		if (!child->thread.debugctlmsr)
197eee3af4aSMarkus Metzger 			clear_tsk_thread_flag(child, TIF_DEBUGCTLMSR);
19810faa81eSRoland McGrath 	}
19910faa81eSRoland McGrath }
20010faa81eSRoland McGrath 
20110faa81eSRoland McGrath void user_enable_single_step(struct task_struct *child)
20210faa81eSRoland McGrath {
20310faa81eSRoland McGrath 	enable_step(child, 0);
20410faa81eSRoland McGrath }
20510faa81eSRoland McGrath 
20610faa81eSRoland McGrath void user_enable_block_step(struct task_struct *child)
20710faa81eSRoland McGrath {
20810faa81eSRoland McGrath 	enable_step(child, 1);
209fa1e03eaSRoland McGrath }
210fa1e03eaSRoland McGrath 
211fa1e03eaSRoland McGrath void user_disable_single_step(struct task_struct *child)
212fa1e03eaSRoland McGrath {
21310faa81eSRoland McGrath 	/*
21410faa81eSRoland McGrath 	 * Make sure block stepping (BTF) is disabled.
21510faa81eSRoland McGrath 	 */
216eee3af4aSMarkus Metzger 	write_debugctlmsr(child,
217d032b31aSJan Beulich 			  child->thread.debugctlmsr & ~DEBUGCTLMSR_BTF);
218eee3af4aSMarkus Metzger 
219eee3af4aSMarkus Metzger 	if (!child->thread.debugctlmsr)
220eee3af4aSMarkus Metzger 		clear_tsk_thread_flag(child, TIF_DEBUGCTLMSR);
22110faa81eSRoland McGrath 
222fa1e03eaSRoland McGrath 	/* Always clear TIF_SINGLESTEP... */
223fa1e03eaSRoland McGrath 	clear_tsk_thread_flag(child, TIF_SINGLESTEP);
224fa1e03eaSRoland McGrath 
225fa1e03eaSRoland McGrath 	/* But touch TF only if it was set by us.. */
226e1f28773SRoland McGrath 	if (test_and_clear_tsk_thread_flag(child, TIF_FORCED_TF))
22765ea5b03SH. Peter Anvin 		task_pt_regs(child)->flags &= ~X86_EFLAGS_TF;
228fa1e03eaSRoland McGrath }
229