xref: /openbmc/linux/arch/x86/kernel/step.c (revision eee3af4a2c83a97fff107ddc445d9df6fded9ce4)
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 
87122ec81SRoland McGrath #ifdef CONFIG_X86_32
97122ec81SRoland McGrath static
107122ec81SRoland McGrath #endif
11fa1e03eaSRoland McGrath unsigned long convert_rip_to_linear(struct task_struct *child, struct pt_regs *regs)
12fa1e03eaSRoland McGrath {
13fa1e03eaSRoland McGrath 	unsigned long addr, seg;
14fa1e03eaSRoland McGrath 
1565ea5b03SH. Peter Anvin 	addr = regs->ip;
16fa1e03eaSRoland McGrath 	seg = regs->cs & 0xffff;
1765ea5b03SH. Peter Anvin 	if (v8086_mode(regs)) {
187122ec81SRoland McGrath 		addr = (addr & 0xffff) + (seg << 4);
197122ec81SRoland McGrath 		return addr;
207122ec81SRoland McGrath 	}
21fa1e03eaSRoland McGrath 
22fa1e03eaSRoland McGrath 	/*
23fa1e03eaSRoland McGrath 	 * We'll assume that the code segments in the GDT
24fa1e03eaSRoland McGrath 	 * are all zero-based. That is largely true: the
25fa1e03eaSRoland McGrath 	 * TLS segments are used for data, and the PNPBIOS
26fa1e03eaSRoland McGrath 	 * and APM bios ones we just ignore here.
27fa1e03eaSRoland McGrath 	 */
283f80c1adSRoland McGrath 	if ((seg & SEGMENT_TI_MASK) == SEGMENT_LDT) {
29fa1e03eaSRoland McGrath 		u32 *desc;
30fa1e03eaSRoland McGrath 		unsigned long base;
31fa1e03eaSRoland McGrath 
32fa1e03eaSRoland McGrath 		seg &= ~7UL;
33fa1e03eaSRoland McGrath 
34fa1e03eaSRoland McGrath 		mutex_lock(&child->mm->context.lock);
35fa1e03eaSRoland McGrath 		if (unlikely((seg >> 3) >= child->mm->context.size))
36fa1e03eaSRoland McGrath 			addr = -1L; /* bogus selector, access would fault */
37fa1e03eaSRoland McGrath 		else {
38fa1e03eaSRoland McGrath 			desc = child->mm->context.ldt + seg;
39fa1e03eaSRoland McGrath 			base = ((desc[0] >> 16) |
40fa1e03eaSRoland McGrath 				((desc[1] & 0xff) << 16) |
41fa1e03eaSRoland McGrath 				(desc[1] & 0xff000000));
42fa1e03eaSRoland McGrath 
43fa1e03eaSRoland McGrath 			/* 16-bit code segment? */
44fa1e03eaSRoland McGrath 			if (!((desc[1] >> 22) & 1))
45fa1e03eaSRoland McGrath 				addr &= 0xffff;
46fa1e03eaSRoland McGrath 			addr += base;
47fa1e03eaSRoland McGrath 		}
48fa1e03eaSRoland McGrath 		mutex_unlock(&child->mm->context.lock);
49fa1e03eaSRoland McGrath 	}
50fa1e03eaSRoland McGrath 
51fa1e03eaSRoland McGrath 	return addr;
52fa1e03eaSRoland McGrath }
53fa1e03eaSRoland McGrath 
54fa1e03eaSRoland McGrath static int is_setting_trap_flag(struct task_struct *child, struct pt_regs *regs)
55fa1e03eaSRoland McGrath {
56fa1e03eaSRoland McGrath 	int i, copied;
57fa1e03eaSRoland McGrath 	unsigned char opcode[15];
58fa1e03eaSRoland McGrath 	unsigned long addr = convert_rip_to_linear(child, regs);
59fa1e03eaSRoland McGrath 
60fa1e03eaSRoland McGrath 	copied = access_process_vm(child, addr, opcode, sizeof(opcode), 0);
61fa1e03eaSRoland McGrath 	for (i = 0; i < copied; i++) {
62fa1e03eaSRoland McGrath 		switch (opcode[i]) {
63fa1e03eaSRoland McGrath 		/* popf and iret */
64fa1e03eaSRoland McGrath 		case 0x9d: case 0xcf:
65fa1e03eaSRoland McGrath 			return 1;
66fa1e03eaSRoland McGrath 
67fa1e03eaSRoland McGrath 			/* CHECKME: 64 65 */
68fa1e03eaSRoland McGrath 
69fa1e03eaSRoland McGrath 		/* opcode and address size prefixes */
70fa1e03eaSRoland McGrath 		case 0x66: case 0x67:
71fa1e03eaSRoland McGrath 			continue;
72fa1e03eaSRoland McGrath 		/* irrelevant prefixes (segment overrides and repeats) */
73fa1e03eaSRoland McGrath 		case 0x26: case 0x2e:
74fa1e03eaSRoland McGrath 		case 0x36: case 0x3e:
75fa1e03eaSRoland McGrath 		case 0x64: case 0x65:
765f76cb1fSRoland McGrath 		case 0xf0: case 0xf2: case 0xf3:
77fa1e03eaSRoland McGrath 			continue;
78fa1e03eaSRoland McGrath 
797122ec81SRoland McGrath #ifdef CONFIG_X86_64
80fa1e03eaSRoland McGrath 		case 0x40 ... 0x4f:
81fa1e03eaSRoland McGrath 			if (regs->cs != __USER_CS)
82fa1e03eaSRoland McGrath 				/* 32-bit mode: register increment */
83fa1e03eaSRoland McGrath 				return 0;
84fa1e03eaSRoland McGrath 			/* 64-bit mode: REX prefix */
85fa1e03eaSRoland McGrath 			continue;
867122ec81SRoland McGrath #endif
87fa1e03eaSRoland McGrath 
88fa1e03eaSRoland McGrath 			/* CHECKME: f2, f3 */
89fa1e03eaSRoland McGrath 
90fa1e03eaSRoland McGrath 		/*
91fa1e03eaSRoland McGrath 		 * pushf: NOTE! We should probably not let
92fa1e03eaSRoland McGrath 		 * the user see the TF bit being set. But
93fa1e03eaSRoland McGrath 		 * it's more pain than it's worth to avoid
94fa1e03eaSRoland McGrath 		 * it, and a debugger could emulate this
95fa1e03eaSRoland McGrath 		 * all in user space if it _really_ cares.
96fa1e03eaSRoland McGrath 		 */
97fa1e03eaSRoland McGrath 		case 0x9c:
98fa1e03eaSRoland McGrath 		default:
99fa1e03eaSRoland McGrath 			return 0;
100fa1e03eaSRoland McGrath 		}
101fa1e03eaSRoland McGrath 	}
102fa1e03eaSRoland McGrath 	return 0;
103fa1e03eaSRoland McGrath }
104fa1e03eaSRoland McGrath 
10510faa81eSRoland McGrath /*
10610faa81eSRoland McGrath  * Enable single-stepping.  Return nonzero if user mode is not using TF itself.
10710faa81eSRoland McGrath  */
10810faa81eSRoland McGrath static int enable_single_step(struct task_struct *child)
109fa1e03eaSRoland McGrath {
110fa1e03eaSRoland McGrath 	struct pt_regs *regs = task_pt_regs(child);
111fa1e03eaSRoland McGrath 
112fa1e03eaSRoland McGrath 	/*
113fa1e03eaSRoland McGrath 	 * Always set TIF_SINGLESTEP - this guarantees that
114fa1e03eaSRoland McGrath 	 * we single-step system calls etc..  This will also
115fa1e03eaSRoland McGrath 	 * cause us to set TF when returning to user mode.
116fa1e03eaSRoland McGrath 	 */
117fa1e03eaSRoland McGrath 	set_tsk_thread_flag(child, TIF_SINGLESTEP);
118fa1e03eaSRoland McGrath 
119fa1e03eaSRoland McGrath 	/*
120fa1e03eaSRoland McGrath 	 * If TF was already set, don't do anything else
121fa1e03eaSRoland McGrath 	 */
12265ea5b03SH. Peter Anvin 	if (regs->flags & X86_EFLAGS_TF)
12310faa81eSRoland McGrath 		return 0;
124fa1e03eaSRoland McGrath 
125fa1e03eaSRoland McGrath 	/* Set TF on the kernel stack.. */
12665ea5b03SH. Peter Anvin 	regs->flags |= X86_EFLAGS_TF;
127fa1e03eaSRoland McGrath 
128fa1e03eaSRoland McGrath 	/*
129fa1e03eaSRoland McGrath 	 * ..but if TF is changed by the instruction we will trace,
130fa1e03eaSRoland McGrath 	 * don't mark it as being "us" that set it, so that we
131fa1e03eaSRoland McGrath 	 * won't clear it by hand later.
132fa1e03eaSRoland McGrath 	 */
133fa1e03eaSRoland McGrath 	if (is_setting_trap_flag(child, regs))
13410faa81eSRoland McGrath 		return 0;
135fa1e03eaSRoland McGrath 
136e1f28773SRoland McGrath 	set_tsk_thread_flag(child, TIF_FORCED_TF);
13710faa81eSRoland McGrath 
13810faa81eSRoland McGrath 	return 1;
13910faa81eSRoland McGrath }
14010faa81eSRoland McGrath 
14110faa81eSRoland McGrath /*
14210faa81eSRoland McGrath  * Install this value in MSR_IA32_DEBUGCTLMSR whenever child is running.
14310faa81eSRoland McGrath  */
14410faa81eSRoland McGrath static void write_debugctlmsr(struct task_struct *child, unsigned long val)
14510faa81eSRoland McGrath {
14610faa81eSRoland McGrath 	child->thread.debugctlmsr = val;
14710faa81eSRoland McGrath 
14810faa81eSRoland McGrath 	if (child != current)
14910faa81eSRoland McGrath 		return;
15010faa81eSRoland McGrath 
15110faa81eSRoland McGrath #ifdef CONFIG_X86_64
15210faa81eSRoland McGrath 	wrmsrl(MSR_IA32_DEBUGCTLMSR, val);
15310faa81eSRoland McGrath #else
15410faa81eSRoland McGrath 	wrmsr(MSR_IA32_DEBUGCTLMSR, val, 0);
15510faa81eSRoland McGrath #endif
15610faa81eSRoland McGrath }
15710faa81eSRoland McGrath 
15810faa81eSRoland McGrath /*
15910faa81eSRoland McGrath  * Enable single or block step.
16010faa81eSRoland McGrath  */
16110faa81eSRoland McGrath static void enable_step(struct task_struct *child, bool block)
16210faa81eSRoland McGrath {
16310faa81eSRoland McGrath 	/*
16410faa81eSRoland McGrath 	 * Make sure block stepping (BTF) is not enabled unless it should be.
16510faa81eSRoland McGrath 	 * Note that we don't try to worry about any is_setting_trap_flag()
16610faa81eSRoland McGrath 	 * instructions after the first when using block stepping.
16710faa81eSRoland McGrath 	 * So noone should try to use debugger block stepping in a program
16810faa81eSRoland McGrath 	 * that uses user-mode single stepping itself.
16910faa81eSRoland McGrath 	 */
17010faa81eSRoland McGrath 	if (enable_single_step(child) && block) {
17110faa81eSRoland McGrath 		set_tsk_thread_flag(child, TIF_DEBUGCTLMSR);
172*eee3af4aSMarkus Metzger 		write_debugctlmsr(child,
173*eee3af4aSMarkus Metzger 				  child->thread.debugctlmsr | DEBUGCTLMSR_BTF);
174*eee3af4aSMarkus Metzger 	} else {
175*eee3af4aSMarkus Metzger 	    write_debugctlmsr(child,
176*eee3af4aSMarkus Metzger 			      child->thread.debugctlmsr & ~TIF_DEBUGCTLMSR);
177*eee3af4aSMarkus Metzger 
178*eee3af4aSMarkus Metzger 	    if (!child->thread.debugctlmsr)
179*eee3af4aSMarkus Metzger 		    clear_tsk_thread_flag(child, TIF_DEBUGCTLMSR);
18010faa81eSRoland McGrath 	}
18110faa81eSRoland McGrath }
18210faa81eSRoland McGrath 
18310faa81eSRoland McGrath void user_enable_single_step(struct task_struct *child)
18410faa81eSRoland McGrath {
18510faa81eSRoland McGrath 	enable_step(child, 0);
18610faa81eSRoland McGrath }
18710faa81eSRoland McGrath 
18810faa81eSRoland McGrath void user_enable_block_step(struct task_struct *child)
18910faa81eSRoland McGrath {
19010faa81eSRoland McGrath 	enable_step(child, 1);
191fa1e03eaSRoland McGrath }
192fa1e03eaSRoland McGrath 
193fa1e03eaSRoland McGrath void user_disable_single_step(struct task_struct *child)
194fa1e03eaSRoland McGrath {
19510faa81eSRoland McGrath 	/*
19610faa81eSRoland McGrath 	 * Make sure block stepping (BTF) is disabled.
19710faa81eSRoland McGrath 	 */
198*eee3af4aSMarkus Metzger 	write_debugctlmsr(child,
199*eee3af4aSMarkus Metzger 			  child->thread.debugctlmsr & ~TIF_DEBUGCTLMSR);
200*eee3af4aSMarkus Metzger 
201*eee3af4aSMarkus Metzger 	if (!child->thread.debugctlmsr)
202*eee3af4aSMarkus Metzger 		clear_tsk_thread_flag(child, TIF_DEBUGCTLMSR);
20310faa81eSRoland McGrath 
204fa1e03eaSRoland McGrath 	/* Always clear TIF_SINGLESTEP... */
205fa1e03eaSRoland McGrath 	clear_tsk_thread_flag(child, TIF_SINGLESTEP);
206fa1e03eaSRoland McGrath 
207fa1e03eaSRoland McGrath 	/* But touch TF only if it was set by us.. */
208e1f28773SRoland McGrath 	if (test_and_clear_tsk_thread_flag(child, TIF_FORCED_TF))
20965ea5b03SH. Peter Anvin 		task_pt_regs(child)->flags &= ~X86_EFLAGS_TF;
210fa1e03eaSRoland McGrath }
211