xref: /kernel/linux/linux-6.6/arch/x86/kernel/step.c (revision 62306a36)
162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * x86 single-step support code, common to 32-bit and 64-bit.
462306a36Sopenharmony_ci */
562306a36Sopenharmony_ci#include <linux/sched.h>
662306a36Sopenharmony_ci#include <linux/sched/task_stack.h>
762306a36Sopenharmony_ci#include <linux/mm.h>
862306a36Sopenharmony_ci#include <linux/ptrace.h>
962306a36Sopenharmony_ci#include <asm/desc.h>
1062306a36Sopenharmony_ci#include <asm/mmu_context.h>
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_ciunsigned long convert_ip_to_linear(struct task_struct *child, struct pt_regs *regs)
1362306a36Sopenharmony_ci{
1462306a36Sopenharmony_ci	unsigned long addr, seg;
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_ci	addr = regs->ip;
1762306a36Sopenharmony_ci	seg = regs->cs;
1862306a36Sopenharmony_ci	if (v8086_mode(regs)) {
1962306a36Sopenharmony_ci		addr = (addr & 0xffff) + (seg << 4);
2062306a36Sopenharmony_ci		return addr;
2162306a36Sopenharmony_ci	}
2262306a36Sopenharmony_ci
2362306a36Sopenharmony_ci#ifdef CONFIG_MODIFY_LDT_SYSCALL
2462306a36Sopenharmony_ci	/*
2562306a36Sopenharmony_ci	 * We'll assume that the code segments in the GDT
2662306a36Sopenharmony_ci	 * are all zero-based. That is largely true: the
2762306a36Sopenharmony_ci	 * TLS segments are used for data, and the PNPBIOS
2862306a36Sopenharmony_ci	 * and APM bios ones we just ignore here.
2962306a36Sopenharmony_ci	 */
3062306a36Sopenharmony_ci	if ((seg & SEGMENT_TI_MASK) == SEGMENT_LDT) {
3162306a36Sopenharmony_ci		struct desc_struct *desc;
3262306a36Sopenharmony_ci		unsigned long base;
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_ci		seg >>= 3;
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_ci		mutex_lock(&child->mm->context.lock);
3762306a36Sopenharmony_ci		if (unlikely(!child->mm->context.ldt ||
3862306a36Sopenharmony_ci			     seg >= child->mm->context.ldt->nr_entries))
3962306a36Sopenharmony_ci			addr = -1L; /* bogus selector, access would fault */
4062306a36Sopenharmony_ci		else {
4162306a36Sopenharmony_ci			desc = &child->mm->context.ldt->entries[seg];
4262306a36Sopenharmony_ci			base = get_desc_base(desc);
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_ci			/* 16-bit code segment? */
4562306a36Sopenharmony_ci			if (!desc->d)
4662306a36Sopenharmony_ci				addr &= 0xffff;
4762306a36Sopenharmony_ci			addr += base;
4862306a36Sopenharmony_ci		}
4962306a36Sopenharmony_ci		mutex_unlock(&child->mm->context.lock);
5062306a36Sopenharmony_ci	}
5162306a36Sopenharmony_ci#endif
5262306a36Sopenharmony_ci
5362306a36Sopenharmony_ci	return addr;
5462306a36Sopenharmony_ci}
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_cistatic int is_setting_trap_flag(struct task_struct *child, struct pt_regs *regs)
5762306a36Sopenharmony_ci{
5862306a36Sopenharmony_ci	int i, copied;
5962306a36Sopenharmony_ci	unsigned char opcode[15];
6062306a36Sopenharmony_ci	unsigned long addr = convert_ip_to_linear(child, regs);
6162306a36Sopenharmony_ci
6262306a36Sopenharmony_ci	copied = access_process_vm(child, addr, opcode, sizeof(opcode),
6362306a36Sopenharmony_ci			FOLL_FORCE);
6462306a36Sopenharmony_ci	for (i = 0; i < copied; i++) {
6562306a36Sopenharmony_ci		switch (opcode[i]) {
6662306a36Sopenharmony_ci		/* popf and iret */
6762306a36Sopenharmony_ci		case 0x9d: case 0xcf:
6862306a36Sopenharmony_ci			return 1;
6962306a36Sopenharmony_ci
7062306a36Sopenharmony_ci			/* CHECKME: 64 65 */
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_ci		/* opcode and address size prefixes */
7362306a36Sopenharmony_ci		case 0x66: case 0x67:
7462306a36Sopenharmony_ci			continue;
7562306a36Sopenharmony_ci		/* irrelevant prefixes (segment overrides and repeats) */
7662306a36Sopenharmony_ci		case 0x26: case 0x2e:
7762306a36Sopenharmony_ci		case 0x36: case 0x3e:
7862306a36Sopenharmony_ci		case 0x64: case 0x65:
7962306a36Sopenharmony_ci		case 0xf0: case 0xf2: case 0xf3:
8062306a36Sopenharmony_ci			continue;
8162306a36Sopenharmony_ci
8262306a36Sopenharmony_ci#ifdef CONFIG_X86_64
8362306a36Sopenharmony_ci		case 0x40 ... 0x4f:
8462306a36Sopenharmony_ci			if (!user_64bit_mode(regs))
8562306a36Sopenharmony_ci				/* 32-bit mode: register increment */
8662306a36Sopenharmony_ci				return 0;
8762306a36Sopenharmony_ci			/* 64-bit mode: REX prefix */
8862306a36Sopenharmony_ci			continue;
8962306a36Sopenharmony_ci#endif
9062306a36Sopenharmony_ci
9162306a36Sopenharmony_ci			/* CHECKME: f2, f3 */
9262306a36Sopenharmony_ci
9362306a36Sopenharmony_ci		/*
9462306a36Sopenharmony_ci		 * pushf: NOTE! We should probably not let
9562306a36Sopenharmony_ci		 * the user see the TF bit being set. But
9662306a36Sopenharmony_ci		 * it's more pain than it's worth to avoid
9762306a36Sopenharmony_ci		 * it, and a debugger could emulate this
9862306a36Sopenharmony_ci		 * all in user space if it _really_ cares.
9962306a36Sopenharmony_ci		 */
10062306a36Sopenharmony_ci		case 0x9c:
10162306a36Sopenharmony_ci		default:
10262306a36Sopenharmony_ci			return 0;
10362306a36Sopenharmony_ci		}
10462306a36Sopenharmony_ci	}
10562306a36Sopenharmony_ci	return 0;
10662306a36Sopenharmony_ci}
10762306a36Sopenharmony_ci
10862306a36Sopenharmony_ci/*
10962306a36Sopenharmony_ci * Enable single-stepping.  Return nonzero if user mode is not using TF itself.
11062306a36Sopenharmony_ci */
11162306a36Sopenharmony_cistatic int enable_single_step(struct task_struct *child)
11262306a36Sopenharmony_ci{
11362306a36Sopenharmony_ci	struct pt_regs *regs = task_pt_regs(child);
11462306a36Sopenharmony_ci	unsigned long oflags;
11562306a36Sopenharmony_ci
11662306a36Sopenharmony_ci	/*
11762306a36Sopenharmony_ci	 * If we stepped into a sysenter/syscall insn, it trapped in
11862306a36Sopenharmony_ci	 * kernel mode; do_debug() cleared TF and set TIF_SINGLESTEP.
11962306a36Sopenharmony_ci	 * If user-mode had set TF itself, then it's still clear from
12062306a36Sopenharmony_ci	 * do_debug() and we need to set it again to restore the user
12162306a36Sopenharmony_ci	 * state so we don't wrongly set TIF_FORCED_TF below.
12262306a36Sopenharmony_ci	 * If enable_single_step() was used last and that is what
12362306a36Sopenharmony_ci	 * set TIF_SINGLESTEP, then both TF and TIF_FORCED_TF are
12462306a36Sopenharmony_ci	 * already set and our bookkeeping is fine.
12562306a36Sopenharmony_ci	 */
12662306a36Sopenharmony_ci	if (unlikely(test_tsk_thread_flag(child, TIF_SINGLESTEP)))
12762306a36Sopenharmony_ci		regs->flags |= X86_EFLAGS_TF;
12862306a36Sopenharmony_ci
12962306a36Sopenharmony_ci	/*
13062306a36Sopenharmony_ci	 * Always set TIF_SINGLESTEP.  This will also
13162306a36Sopenharmony_ci	 * cause us to set TF when returning to user mode.
13262306a36Sopenharmony_ci	 */
13362306a36Sopenharmony_ci	set_tsk_thread_flag(child, TIF_SINGLESTEP);
13462306a36Sopenharmony_ci
13562306a36Sopenharmony_ci	/*
13662306a36Sopenharmony_ci	 * Ensure that a trap is triggered once stepping out of a system
13762306a36Sopenharmony_ci	 * call prior to executing any user instruction.
13862306a36Sopenharmony_ci	 */
13962306a36Sopenharmony_ci	set_task_syscall_work(child, SYSCALL_EXIT_TRAP);
14062306a36Sopenharmony_ci
14162306a36Sopenharmony_ci	oflags = regs->flags;
14262306a36Sopenharmony_ci
14362306a36Sopenharmony_ci	/* Set TF on the kernel stack.. */
14462306a36Sopenharmony_ci	regs->flags |= X86_EFLAGS_TF;
14562306a36Sopenharmony_ci
14662306a36Sopenharmony_ci	/*
14762306a36Sopenharmony_ci	 * ..but if TF is changed by the instruction we will trace,
14862306a36Sopenharmony_ci	 * don't mark it as being "us" that set it, so that we
14962306a36Sopenharmony_ci	 * won't clear it by hand later.
15062306a36Sopenharmony_ci	 *
15162306a36Sopenharmony_ci	 * Note that if we don't actually execute the popf because
15262306a36Sopenharmony_ci	 * of a signal arriving right now or suchlike, we will lose
15362306a36Sopenharmony_ci	 * track of the fact that it really was "us" that set it.
15462306a36Sopenharmony_ci	 */
15562306a36Sopenharmony_ci	if (is_setting_trap_flag(child, regs)) {
15662306a36Sopenharmony_ci		clear_tsk_thread_flag(child, TIF_FORCED_TF);
15762306a36Sopenharmony_ci		return 0;
15862306a36Sopenharmony_ci	}
15962306a36Sopenharmony_ci
16062306a36Sopenharmony_ci	/*
16162306a36Sopenharmony_ci	 * If TF was already set, check whether it was us who set it.
16262306a36Sopenharmony_ci	 * If not, we should never attempt a block step.
16362306a36Sopenharmony_ci	 */
16462306a36Sopenharmony_ci	if (oflags & X86_EFLAGS_TF)
16562306a36Sopenharmony_ci		return test_tsk_thread_flag(child, TIF_FORCED_TF);
16662306a36Sopenharmony_ci
16762306a36Sopenharmony_ci	set_tsk_thread_flag(child, TIF_FORCED_TF);
16862306a36Sopenharmony_ci
16962306a36Sopenharmony_ci	return 1;
17062306a36Sopenharmony_ci}
17162306a36Sopenharmony_ci
17262306a36Sopenharmony_civoid set_task_blockstep(struct task_struct *task, bool on)
17362306a36Sopenharmony_ci{
17462306a36Sopenharmony_ci	unsigned long debugctl;
17562306a36Sopenharmony_ci
17662306a36Sopenharmony_ci	/*
17762306a36Sopenharmony_ci	 * Ensure irq/preemption can't change debugctl in between.
17862306a36Sopenharmony_ci	 * Note also that both TIF_BLOCKSTEP and debugctl should
17962306a36Sopenharmony_ci	 * be changed atomically wrt preemption.
18062306a36Sopenharmony_ci	 *
18162306a36Sopenharmony_ci	 * NOTE: this means that set/clear TIF_BLOCKSTEP is only safe if
18262306a36Sopenharmony_ci	 * task is current or it can't be running, otherwise we can race
18362306a36Sopenharmony_ci	 * with __switch_to_xtra(). We rely on ptrace_freeze_traced().
18462306a36Sopenharmony_ci	 */
18562306a36Sopenharmony_ci	local_irq_disable();
18662306a36Sopenharmony_ci	debugctl = get_debugctlmsr();
18762306a36Sopenharmony_ci	if (on) {
18862306a36Sopenharmony_ci		debugctl |= DEBUGCTLMSR_BTF;
18962306a36Sopenharmony_ci		set_tsk_thread_flag(task, TIF_BLOCKSTEP);
19062306a36Sopenharmony_ci	} else {
19162306a36Sopenharmony_ci		debugctl &= ~DEBUGCTLMSR_BTF;
19262306a36Sopenharmony_ci		clear_tsk_thread_flag(task, TIF_BLOCKSTEP);
19362306a36Sopenharmony_ci	}
19462306a36Sopenharmony_ci	if (task == current)
19562306a36Sopenharmony_ci		update_debugctlmsr(debugctl);
19662306a36Sopenharmony_ci	local_irq_enable();
19762306a36Sopenharmony_ci}
19862306a36Sopenharmony_ci
19962306a36Sopenharmony_ci/*
20062306a36Sopenharmony_ci * Enable single or block step.
20162306a36Sopenharmony_ci */
20262306a36Sopenharmony_cistatic void enable_step(struct task_struct *child, bool block)
20362306a36Sopenharmony_ci{
20462306a36Sopenharmony_ci	/*
20562306a36Sopenharmony_ci	 * Make sure block stepping (BTF) is not enabled unless it should be.
20662306a36Sopenharmony_ci	 * Note that we don't try to worry about any is_setting_trap_flag()
20762306a36Sopenharmony_ci	 * instructions after the first when using block stepping.
20862306a36Sopenharmony_ci	 * So no one should try to use debugger block stepping in a program
20962306a36Sopenharmony_ci	 * that uses user-mode single stepping itself.
21062306a36Sopenharmony_ci	 */
21162306a36Sopenharmony_ci	if (enable_single_step(child) && block)
21262306a36Sopenharmony_ci		set_task_blockstep(child, true);
21362306a36Sopenharmony_ci	else if (test_tsk_thread_flag(child, TIF_BLOCKSTEP))
21462306a36Sopenharmony_ci		set_task_blockstep(child, false);
21562306a36Sopenharmony_ci}
21662306a36Sopenharmony_ci
21762306a36Sopenharmony_civoid user_enable_single_step(struct task_struct *child)
21862306a36Sopenharmony_ci{
21962306a36Sopenharmony_ci	enable_step(child, 0);
22062306a36Sopenharmony_ci}
22162306a36Sopenharmony_ci
22262306a36Sopenharmony_civoid user_enable_block_step(struct task_struct *child)
22362306a36Sopenharmony_ci{
22462306a36Sopenharmony_ci	enable_step(child, 1);
22562306a36Sopenharmony_ci}
22662306a36Sopenharmony_ci
22762306a36Sopenharmony_civoid user_disable_single_step(struct task_struct *child)
22862306a36Sopenharmony_ci{
22962306a36Sopenharmony_ci	/*
23062306a36Sopenharmony_ci	 * Make sure block stepping (BTF) is disabled.
23162306a36Sopenharmony_ci	 */
23262306a36Sopenharmony_ci	if (test_tsk_thread_flag(child, TIF_BLOCKSTEP))
23362306a36Sopenharmony_ci		set_task_blockstep(child, false);
23462306a36Sopenharmony_ci
23562306a36Sopenharmony_ci	/* Always clear TIF_SINGLESTEP... */
23662306a36Sopenharmony_ci	clear_tsk_thread_flag(child, TIF_SINGLESTEP);
23762306a36Sopenharmony_ci	clear_task_syscall_work(child, SYSCALL_EXIT_TRAP);
23862306a36Sopenharmony_ci
23962306a36Sopenharmony_ci	/* But touch TF only if it was set by us.. */
24062306a36Sopenharmony_ci	if (test_and_clear_tsk_thread_flag(child, TIF_FORCED_TF))
24162306a36Sopenharmony_ci		task_pt_regs(child)->flags &= ~X86_EFLAGS_TF;
24262306a36Sopenharmony_ci}
243