xref: /openbmc/linux/arch/x86/kernel/step.c (revision 5b0e508415a83989fe704b4718a1a214bc333ca7)
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);
108fa1e03eaSRoland McGrath 
109fa1e03eaSRoland McGrath 	/*
110fa1e03eaSRoland McGrath 	 * Always set TIF_SINGLESTEP - this guarantees that
111fa1e03eaSRoland McGrath 	 * we single-step system calls etc..  This will also
112fa1e03eaSRoland McGrath 	 * cause us to set TF when returning to user mode.
113fa1e03eaSRoland McGrath 	 */
114fa1e03eaSRoland McGrath 	set_tsk_thread_flag(child, TIF_SINGLESTEP);
115fa1e03eaSRoland McGrath 
116fa1e03eaSRoland McGrath 	/*
117fa1e03eaSRoland McGrath 	 * If TF was already set, don't do anything else
118fa1e03eaSRoland McGrath 	 */
11965ea5b03SH. Peter Anvin 	if (regs->flags & X86_EFLAGS_TF)
12010faa81eSRoland McGrath 		return 0;
121fa1e03eaSRoland McGrath 
122fa1e03eaSRoland McGrath 	/* Set TF on the kernel stack.. */
12365ea5b03SH. Peter Anvin 	regs->flags |= X86_EFLAGS_TF;
124fa1e03eaSRoland McGrath 
125fa1e03eaSRoland McGrath 	/*
126fa1e03eaSRoland McGrath 	 * ..but if TF is changed by the instruction we will trace,
127fa1e03eaSRoland McGrath 	 * don't mark it as being "us" that set it, so that we
128fa1e03eaSRoland McGrath 	 * won't clear it by hand later.
129fa1e03eaSRoland McGrath 	 */
130fa1e03eaSRoland McGrath 	if (is_setting_trap_flag(child, regs))
13110faa81eSRoland McGrath 		return 0;
132fa1e03eaSRoland McGrath 
133e1f28773SRoland McGrath 	set_tsk_thread_flag(child, TIF_FORCED_TF);
13410faa81eSRoland McGrath 
13510faa81eSRoland McGrath 	return 1;
13610faa81eSRoland McGrath }
13710faa81eSRoland McGrath 
13810faa81eSRoland McGrath /*
13910faa81eSRoland McGrath  * Install this value in MSR_IA32_DEBUGCTLMSR whenever child is running.
14010faa81eSRoland McGrath  */
14110faa81eSRoland McGrath static void write_debugctlmsr(struct task_struct *child, unsigned long val)
14210faa81eSRoland McGrath {
1434ba51fd7SRoland McGrath 	if (child->thread.debugctlmsr == val)
1444ba51fd7SRoland McGrath 		return;
1454ba51fd7SRoland McGrath 
14610faa81eSRoland McGrath 	child->thread.debugctlmsr = val;
14710faa81eSRoland McGrath 
14810faa81eSRoland McGrath 	if (child != current)
14910faa81eSRoland McGrath 		return;
15010faa81eSRoland McGrath 
151*5b0e5084SJan Beulich 	update_debugctlmsr(val);
15210faa81eSRoland McGrath }
15310faa81eSRoland McGrath 
15410faa81eSRoland McGrath /*
15510faa81eSRoland McGrath  * Enable single or block step.
15610faa81eSRoland McGrath  */
15710faa81eSRoland McGrath static void enable_step(struct task_struct *child, bool block)
15810faa81eSRoland McGrath {
15910faa81eSRoland McGrath 	/*
16010faa81eSRoland McGrath 	 * Make sure block stepping (BTF) is not enabled unless it should be.
16110faa81eSRoland McGrath 	 * Note that we don't try to worry about any is_setting_trap_flag()
16210faa81eSRoland McGrath 	 * instructions after the first when using block stepping.
16310faa81eSRoland McGrath 	 * So noone should try to use debugger block stepping in a program
16410faa81eSRoland McGrath 	 * that uses user-mode single stepping itself.
16510faa81eSRoland McGrath 	 */
16610faa81eSRoland McGrath 	if (enable_single_step(child) && block) {
16710faa81eSRoland McGrath 		set_tsk_thread_flag(child, TIF_DEBUGCTLMSR);
168eee3af4aSMarkus Metzger 		write_debugctlmsr(child,
169eee3af4aSMarkus Metzger 				  child->thread.debugctlmsr | DEBUGCTLMSR_BTF);
170eee3af4aSMarkus Metzger 	} else {
171eee3af4aSMarkus Metzger 		write_debugctlmsr(child,
172d032b31aSJan Beulich 				  child->thread.debugctlmsr & ~DEBUGCTLMSR_BTF);
173eee3af4aSMarkus Metzger 
174eee3af4aSMarkus Metzger 		if (!child->thread.debugctlmsr)
175eee3af4aSMarkus Metzger 			clear_tsk_thread_flag(child, TIF_DEBUGCTLMSR);
17610faa81eSRoland McGrath 	}
17710faa81eSRoland McGrath }
17810faa81eSRoland McGrath 
17910faa81eSRoland McGrath void user_enable_single_step(struct task_struct *child)
18010faa81eSRoland McGrath {
18110faa81eSRoland McGrath 	enable_step(child, 0);
18210faa81eSRoland McGrath }
18310faa81eSRoland McGrath 
18410faa81eSRoland McGrath void user_enable_block_step(struct task_struct *child)
18510faa81eSRoland McGrath {
18610faa81eSRoland McGrath 	enable_step(child, 1);
187fa1e03eaSRoland McGrath }
188fa1e03eaSRoland McGrath 
189fa1e03eaSRoland McGrath void user_disable_single_step(struct task_struct *child)
190fa1e03eaSRoland McGrath {
19110faa81eSRoland McGrath 	/*
19210faa81eSRoland McGrath 	 * Make sure block stepping (BTF) is disabled.
19310faa81eSRoland McGrath 	 */
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 
200fa1e03eaSRoland McGrath 	/* Always clear TIF_SINGLESTEP... */
201fa1e03eaSRoland McGrath 	clear_tsk_thread_flag(child, TIF_SINGLESTEP);
202fa1e03eaSRoland McGrath 
203fa1e03eaSRoland McGrath 	/* But touch TF only if it was set by us.. */
204e1f28773SRoland McGrath 	if (test_and_clear_tsk_thread_flag(child, TIF_FORCED_TF))
20565ea5b03SH. Peter Anvin 		task_pt_regs(child)->flags &= ~X86_EFLAGS_TF;
206fa1e03eaSRoland McGrath }
207