xref: /openbmc/linux/arch/x86/kernel/step.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2fa1e03eaSRoland McGrath /*
3fa1e03eaSRoland McGrath  * x86 single-step support code, common to 32-bit and 64-bit.
4fa1e03eaSRoland McGrath  */
5fa1e03eaSRoland McGrath #include <linux/sched.h>
668db0cf1SIngo Molnar #include <linux/sched/task_stack.h>
7fa1e03eaSRoland McGrath #include <linux/mm.h>
8fa1e03eaSRoland McGrath #include <linux/ptrace.h>
9254e0a6bSAkinobu Mita #include <asm/desc.h>
1037868fe1SAndy Lutomirski #include <asm/mmu_context.h>
11fa1e03eaSRoland McGrath 
convert_ip_to_linear(struct task_struct * child,struct pt_regs * regs)1237cd9cf3SHarvey Harrison unsigned long convert_ip_to_linear(struct task_struct *child, struct pt_regs *regs)
13fa1e03eaSRoland McGrath {
14fa1e03eaSRoland McGrath 	unsigned long addr, seg;
15fa1e03eaSRoland McGrath 
1665ea5b03SH. Peter Anvin 	addr = regs->ip;
1799504819SAndy Lutomirski 	seg = regs->cs;
1865ea5b03SH. Peter Anvin 	if (v8086_mode(regs)) {
197122ec81SRoland McGrath 		addr = (addr & 0xffff) + (seg << 4);
207122ec81SRoland McGrath 		return addr;
217122ec81SRoland McGrath 	}
22fa1e03eaSRoland McGrath 
23a5b9e5a2SAndy Lutomirski #ifdef CONFIG_MODIFY_LDT_SYSCALL
24fa1e03eaSRoland McGrath 	/*
25fa1e03eaSRoland McGrath 	 * We'll assume that the code segments in the GDT
26fa1e03eaSRoland McGrath 	 * are all zero-based. That is largely true: the
27fa1e03eaSRoland McGrath 	 * TLS segments are used for data, and the PNPBIOS
28fa1e03eaSRoland McGrath 	 * and APM bios ones we just ignore here.
29fa1e03eaSRoland McGrath 	 */
303f80c1adSRoland McGrath 	if ((seg & SEGMENT_TI_MASK) == SEGMENT_LDT) {
31254e0a6bSAkinobu Mita 		struct desc_struct *desc;
32fa1e03eaSRoland McGrath 		unsigned long base;
33fa1e03eaSRoland McGrath 
34136d9d83SJuergen Gross 		seg >>= 3;
35fa1e03eaSRoland McGrath 
36fa1e03eaSRoland McGrath 		mutex_lock(&child->mm->context.lock);
3737868fe1SAndy Lutomirski 		if (unlikely(!child->mm->context.ldt ||
38bbf79d21SBorislav Petkov 			     seg >= child->mm->context.ldt->nr_entries))
39fa1e03eaSRoland McGrath 			addr = -1L; /* bogus selector, access would fault */
40fa1e03eaSRoland McGrath 		else {
4137868fe1SAndy Lutomirski 			desc = &child->mm->context.ldt->entries[seg];
42254e0a6bSAkinobu Mita 			base = get_desc_base(desc);
43fa1e03eaSRoland McGrath 
44fa1e03eaSRoland McGrath 			/* 16-bit code segment? */
45254e0a6bSAkinobu Mita 			if (!desc->d)
46fa1e03eaSRoland McGrath 				addr &= 0xffff;
47fa1e03eaSRoland McGrath 			addr += base;
48fa1e03eaSRoland McGrath 		}
49fa1e03eaSRoland McGrath 		mutex_unlock(&child->mm->context.lock);
50fa1e03eaSRoland McGrath 	}
51a5b9e5a2SAndy Lutomirski #endif
52fa1e03eaSRoland McGrath 
53fa1e03eaSRoland McGrath 	return addr;
54fa1e03eaSRoland McGrath }
55fa1e03eaSRoland McGrath 
is_setting_trap_flag(struct task_struct * child,struct pt_regs * regs)56fa1e03eaSRoland McGrath static int is_setting_trap_flag(struct task_struct *child, struct pt_regs *regs)
57fa1e03eaSRoland McGrath {
58fa1e03eaSRoland McGrath 	int i, copied;
59fa1e03eaSRoland McGrath 	unsigned char opcode[15];
6037cd9cf3SHarvey Harrison 	unsigned long addr = convert_ip_to_linear(child, regs);
61fa1e03eaSRoland McGrath 
62f307ab6dSLorenzo Stoakes 	copied = access_process_vm(child, addr, opcode, sizeof(opcode),
63f307ab6dSLorenzo Stoakes 			FOLL_FORCE);
64fa1e03eaSRoland McGrath 	for (i = 0; i < copied; i++) {
65fa1e03eaSRoland McGrath 		switch (opcode[i]) {
66fa1e03eaSRoland McGrath 		/* popf and iret */
67fa1e03eaSRoland McGrath 		case 0x9d: case 0xcf:
68fa1e03eaSRoland McGrath 			return 1;
69fa1e03eaSRoland McGrath 
70fa1e03eaSRoland McGrath 			/* CHECKME: 64 65 */
71fa1e03eaSRoland McGrath 
72fa1e03eaSRoland McGrath 		/* opcode and address size prefixes */
73fa1e03eaSRoland McGrath 		case 0x66: case 0x67:
74fa1e03eaSRoland McGrath 			continue;
75fa1e03eaSRoland McGrath 		/* irrelevant prefixes (segment overrides and repeats) */
76fa1e03eaSRoland McGrath 		case 0x26: case 0x2e:
77fa1e03eaSRoland McGrath 		case 0x36: case 0x3e:
78fa1e03eaSRoland McGrath 		case 0x64: case 0x65:
795f76cb1fSRoland McGrath 		case 0xf0: case 0xf2: case 0xf3:
80fa1e03eaSRoland McGrath 			continue;
81fa1e03eaSRoland McGrath 
827122ec81SRoland McGrath #ifdef CONFIG_X86_64
83fa1e03eaSRoland McGrath 		case 0x40 ... 0x4f:
84318f5a2aSAndy Lutomirski 			if (!user_64bit_mode(regs))
85fa1e03eaSRoland McGrath 				/* 32-bit mode: register increment */
86fa1e03eaSRoland McGrath 				return 0;
87fa1e03eaSRoland McGrath 			/* 64-bit mode: REX prefix */
88fa1e03eaSRoland McGrath 			continue;
897122ec81SRoland McGrath #endif
90fa1e03eaSRoland McGrath 
91fa1e03eaSRoland McGrath 			/* CHECKME: f2, f3 */
92fa1e03eaSRoland McGrath 
93fa1e03eaSRoland McGrath 		/*
94fa1e03eaSRoland McGrath 		 * pushf: NOTE! We should probably not let
95fa1e03eaSRoland McGrath 		 * the user see the TF bit being set. But
96fa1e03eaSRoland McGrath 		 * it's more pain than it's worth to avoid
97fa1e03eaSRoland McGrath 		 * it, and a debugger could emulate this
98fa1e03eaSRoland McGrath 		 * all in user space if it _really_ cares.
99fa1e03eaSRoland McGrath 		 */
100fa1e03eaSRoland McGrath 		case 0x9c:
101fa1e03eaSRoland McGrath 		default:
102fa1e03eaSRoland McGrath 			return 0;
103fa1e03eaSRoland McGrath 		}
104fa1e03eaSRoland McGrath 	}
105fa1e03eaSRoland McGrath 	return 0;
106fa1e03eaSRoland McGrath }
107fa1e03eaSRoland McGrath 
10810faa81eSRoland McGrath /*
10910faa81eSRoland McGrath  * Enable single-stepping.  Return nonzero if user mode is not using TF itself.
11010faa81eSRoland McGrath  */
enable_single_step(struct task_struct * child)11110faa81eSRoland McGrath static int enable_single_step(struct task_struct *child)
112fa1e03eaSRoland McGrath {
113fa1e03eaSRoland McGrath 	struct pt_regs *regs = task_pt_regs(child);
1146718d0d6SRoland McGrath 	unsigned long oflags;
115fa1e03eaSRoland McGrath 
116fa1e03eaSRoland McGrath 	/*
117380fdd75SRoland McGrath 	 * If we stepped into a sysenter/syscall insn, it trapped in
118380fdd75SRoland McGrath 	 * kernel mode; do_debug() cleared TF and set TIF_SINGLESTEP.
119380fdd75SRoland McGrath 	 * If user-mode had set TF itself, then it's still clear from
120380fdd75SRoland McGrath 	 * do_debug() and we need to set it again to restore the user
121380fdd75SRoland McGrath 	 * state so we don't wrongly set TIF_FORCED_TF below.
122380fdd75SRoland McGrath 	 * If enable_single_step() was used last and that is what
123380fdd75SRoland McGrath 	 * set TIF_SINGLESTEP, then both TF and TIF_FORCED_TF are
124380fdd75SRoland McGrath 	 * already set and our bookkeeping is fine.
125380fdd75SRoland McGrath 	 */
126380fdd75SRoland McGrath 	if (unlikely(test_tsk_thread_flag(child, TIF_SINGLESTEP)))
127380fdd75SRoland McGrath 		regs->flags |= X86_EFLAGS_TF;
128380fdd75SRoland McGrath 
129380fdd75SRoland McGrath 	/*
1306342adcaSGabriel Krisman Bertazi 	 * Always set TIF_SINGLESTEP.  This will also
131fa1e03eaSRoland McGrath 	 * cause us to set TF when returning to user mode.
132fa1e03eaSRoland McGrath 	 */
133fa1e03eaSRoland McGrath 	set_tsk_thread_flag(child, TIF_SINGLESTEP);
134fa1e03eaSRoland McGrath 
1356342adcaSGabriel Krisman Bertazi 	/*
1366342adcaSGabriel Krisman Bertazi 	 * Ensure that a trap is triggered once stepping out of a system
1376342adcaSGabriel Krisman Bertazi 	 * call prior to executing any user instruction.
1386342adcaSGabriel Krisman Bertazi 	 */
1396342adcaSGabriel Krisman Bertazi 	set_task_syscall_work(child, SYSCALL_EXIT_TRAP);
1406342adcaSGabriel Krisman Bertazi 
1416718d0d6SRoland McGrath 	oflags = regs->flags;
142fa1e03eaSRoland McGrath 
143fa1e03eaSRoland McGrath 	/* Set TF on the kernel stack.. */
14465ea5b03SH. Peter Anvin 	regs->flags |= X86_EFLAGS_TF;
145fa1e03eaSRoland McGrath 
146fa1e03eaSRoland McGrath 	/*
147fa1e03eaSRoland McGrath 	 * ..but if TF is changed by the instruction we will trace,
148fa1e03eaSRoland McGrath 	 * don't mark it as being "us" that set it, so that we
149fa1e03eaSRoland McGrath 	 * won't clear it by hand later.
1506718d0d6SRoland McGrath 	 *
1516718d0d6SRoland McGrath 	 * Note that if we don't actually execute the popf because
1526718d0d6SRoland McGrath 	 * of a signal arriving right now or suchlike, we will lose
1536718d0d6SRoland McGrath 	 * track of the fact that it really was "us" that set it.
154fa1e03eaSRoland McGrath 	 */
1556718d0d6SRoland McGrath 	if (is_setting_trap_flag(child, regs)) {
1566718d0d6SRoland McGrath 		clear_tsk_thread_flag(child, TIF_FORCED_TF);
15710faa81eSRoland McGrath 		return 0;
1586718d0d6SRoland McGrath 	}
1596718d0d6SRoland McGrath 
1606718d0d6SRoland McGrath 	/*
1616718d0d6SRoland McGrath 	 * If TF was already set, check whether it was us who set it.
1626718d0d6SRoland McGrath 	 * If not, we should never attempt a block step.
1636718d0d6SRoland McGrath 	 */
1646718d0d6SRoland McGrath 	if (oflags & X86_EFLAGS_TF)
1656718d0d6SRoland McGrath 		return test_tsk_thread_flag(child, TIF_FORCED_TF);
166fa1e03eaSRoland McGrath 
167e1f28773SRoland McGrath 	set_tsk_thread_flag(child, TIF_FORCED_TF);
16810faa81eSRoland McGrath 
16910faa81eSRoland McGrath 	return 1;
17010faa81eSRoland McGrath }
17110faa81eSRoland McGrath 
set_task_blockstep(struct task_struct * task,bool on)1729bd1190aSOleg Nesterov void set_task_blockstep(struct task_struct *task, bool on)
173848e8f5fSOleg Nesterov {
174848e8f5fSOleg Nesterov 	unsigned long debugctl;
175848e8f5fSOleg Nesterov 
17695cf00faSOleg Nesterov 	/*
17795cf00faSOleg Nesterov 	 * Ensure irq/preemption can't change debugctl in between.
17895cf00faSOleg Nesterov 	 * Note also that both TIF_BLOCKSTEP and debugctl should
17995cf00faSOleg Nesterov 	 * be changed atomically wrt preemption.
1809899d11fSOleg Nesterov 	 *
1819899d11fSOleg Nesterov 	 * NOTE: this means that set/clear TIF_BLOCKSTEP is only safe if
1829899d11fSOleg Nesterov 	 * task is current or it can't be running, otherwise we can race
183*6a2d90baSEric W. Biederman 	 * with __switch_to_xtra(). We rely on ptrace_freeze_traced().
18495cf00faSOleg Nesterov 	 */
18595cf00faSOleg Nesterov 	local_irq_disable();
186848e8f5fSOleg Nesterov 	debugctl = get_debugctlmsr();
187848e8f5fSOleg Nesterov 	if (on) {
188848e8f5fSOleg Nesterov 		debugctl |= DEBUGCTLMSR_BTF;
189848e8f5fSOleg Nesterov 		set_tsk_thread_flag(task, TIF_BLOCKSTEP);
190848e8f5fSOleg Nesterov 	} else {
191848e8f5fSOleg Nesterov 		debugctl &= ~DEBUGCTLMSR_BTF;
192848e8f5fSOleg Nesterov 		clear_tsk_thread_flag(task, TIF_BLOCKSTEP);
193848e8f5fSOleg Nesterov 	}
19495cf00faSOleg Nesterov 	if (task == current)
195848e8f5fSOleg Nesterov 		update_debugctlmsr(debugctl);
19695cf00faSOleg Nesterov 	local_irq_enable();
197848e8f5fSOleg Nesterov }
198848e8f5fSOleg Nesterov 
19910faa81eSRoland McGrath /*
20010faa81eSRoland McGrath  * Enable single or block step.
20110faa81eSRoland McGrath  */
enable_step(struct task_struct * child,bool block)20210faa81eSRoland McGrath static void enable_step(struct task_struct *child, bool block)
20310faa81eSRoland McGrath {
20410faa81eSRoland McGrath 	/*
20510faa81eSRoland McGrath 	 * Make sure block stepping (BTF) is not enabled unless it should be.
20610faa81eSRoland McGrath 	 * Note that we don't try to worry about any is_setting_trap_flag()
20710faa81eSRoland McGrath 	 * instructions after the first when using block stepping.
20810faa81eSRoland McGrath 	 * So no one should try to use debugger block stepping in a program
20910faa81eSRoland McGrath 	 * that uses user-mode single stepping itself.
21010faa81eSRoland McGrath 	 */
211848e8f5fSOleg Nesterov 	if (enable_single_step(child) && block)
212848e8f5fSOleg Nesterov 		set_task_blockstep(child, true);
213848e8f5fSOleg Nesterov 	else if (test_tsk_thread_flag(child, TIF_BLOCKSTEP))
214848e8f5fSOleg Nesterov 		set_task_blockstep(child, false);
21510faa81eSRoland McGrath }
21610faa81eSRoland McGrath 
user_enable_single_step(struct task_struct * child)21710faa81eSRoland McGrath void user_enable_single_step(struct task_struct *child)
21810faa81eSRoland McGrath {
21910faa81eSRoland McGrath 	enable_step(child, 0);
22010faa81eSRoland McGrath }
22110faa81eSRoland McGrath 
user_enable_block_step(struct task_struct * child)22210faa81eSRoland McGrath void user_enable_block_step(struct task_struct *child)
22310faa81eSRoland McGrath {
22410faa81eSRoland McGrath 	enable_step(child, 1);
225fa1e03eaSRoland McGrath }
226fa1e03eaSRoland McGrath 
user_disable_single_step(struct task_struct * child)227fa1e03eaSRoland McGrath void user_disable_single_step(struct task_struct *child)
228fa1e03eaSRoland McGrath {
22910faa81eSRoland McGrath 	/*
23010faa81eSRoland McGrath 	 * Make sure block stepping (BTF) is disabled.
23110faa81eSRoland McGrath 	 */
232848e8f5fSOleg Nesterov 	if (test_tsk_thread_flag(child, TIF_BLOCKSTEP))
233848e8f5fSOleg Nesterov 		set_task_blockstep(child, false);
23410faa81eSRoland McGrath 
235fa1e03eaSRoland McGrath 	/* Always clear TIF_SINGLESTEP... */
236fa1e03eaSRoland McGrath 	clear_tsk_thread_flag(child, TIF_SINGLESTEP);
2376342adcaSGabriel Krisman Bertazi 	clear_task_syscall_work(child, SYSCALL_EXIT_TRAP);
238fa1e03eaSRoland McGrath 
239fa1e03eaSRoland McGrath 	/* But touch TF only if it was set by us.. */
240e1f28773SRoland McGrath 	if (test_and_clear_tsk_thread_flag(child, TIF_FORCED_TF))
24165ea5b03SH. Peter Anvin 		task_pt_regs(child)->flags &= ~X86_EFLAGS_TF;
242fa1e03eaSRoland McGrath }
243