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> 7254e0a6bSAkinobu 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) { 27254e0a6bSAkinobu 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; 37254e0a6bSAkinobu Mita base = get_desc_base(desc); 38fa1e03eaSRoland McGrath 39fa1e03eaSRoland McGrath /* 16-bit code segment? */ 40254e0a6bSAkinobu 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: 77*318f5a2aSAndy Lutomirski if (!user_64bit_mode(regs)) 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 * Enable single or block step. 16210faa81eSRoland McGrath */ 16310faa81eSRoland McGrath static void enable_step(struct task_struct *child, bool block) 16410faa81eSRoland McGrath { 16510faa81eSRoland McGrath /* 16610faa81eSRoland McGrath * Make sure block stepping (BTF) is not enabled unless it should be. 16710faa81eSRoland McGrath * Note that we don't try to worry about any is_setting_trap_flag() 16810faa81eSRoland McGrath * instructions after the first when using block stepping. 16910faa81eSRoland McGrath * So no one should try to use debugger block stepping in a program 17010faa81eSRoland McGrath * that uses user-mode single stepping itself. 17110faa81eSRoland McGrath */ 172ea8e61b7SPeter Zijlstra if (enable_single_step(child) && block) { 173ea8e61b7SPeter Zijlstra unsigned long debugctl = get_debugctlmsr(); 174ea8e61b7SPeter Zijlstra 175ea8e61b7SPeter Zijlstra debugctl |= DEBUGCTLMSR_BTF; 176ea8e61b7SPeter Zijlstra update_debugctlmsr(debugctl); 177ea8e61b7SPeter Zijlstra set_tsk_thread_flag(child, TIF_BLOCKSTEP); 178ea8e61b7SPeter Zijlstra } else if (test_tsk_thread_flag(child, TIF_BLOCKSTEP)) { 179ea8e61b7SPeter Zijlstra unsigned long debugctl = get_debugctlmsr(); 180ea8e61b7SPeter Zijlstra 181ea8e61b7SPeter Zijlstra debugctl &= ~DEBUGCTLMSR_BTF; 182ea8e61b7SPeter Zijlstra update_debugctlmsr(debugctl); 183ea8e61b7SPeter Zijlstra clear_tsk_thread_flag(child, TIF_BLOCKSTEP); 184ea8e61b7SPeter Zijlstra } 18510faa81eSRoland McGrath } 18610faa81eSRoland McGrath 18710faa81eSRoland McGrath void user_enable_single_step(struct task_struct *child) 18810faa81eSRoland McGrath { 18910faa81eSRoland McGrath enable_step(child, 0); 19010faa81eSRoland McGrath } 19110faa81eSRoland McGrath 19210faa81eSRoland McGrath void user_enable_block_step(struct task_struct *child) 19310faa81eSRoland McGrath { 19410faa81eSRoland McGrath enable_step(child, 1); 195fa1e03eaSRoland McGrath } 196fa1e03eaSRoland McGrath 197fa1e03eaSRoland McGrath void user_disable_single_step(struct task_struct *child) 198fa1e03eaSRoland McGrath { 19910faa81eSRoland McGrath /* 20010faa81eSRoland McGrath * Make sure block stepping (BTF) is disabled. 20110faa81eSRoland McGrath */ 202ea8e61b7SPeter Zijlstra if (test_tsk_thread_flag(child, TIF_BLOCKSTEP)) { 203ea8e61b7SPeter Zijlstra unsigned long debugctl = get_debugctlmsr(); 204ea8e61b7SPeter Zijlstra 205ea8e61b7SPeter Zijlstra debugctl &= ~DEBUGCTLMSR_BTF; 206ea8e61b7SPeter Zijlstra update_debugctlmsr(debugctl); 207ea8e61b7SPeter Zijlstra clear_tsk_thread_flag(child, TIF_BLOCKSTEP); 208ea8e61b7SPeter Zijlstra } 20910faa81eSRoland McGrath 210fa1e03eaSRoland McGrath /* Always clear TIF_SINGLESTEP... */ 211fa1e03eaSRoland McGrath clear_tsk_thread_flag(child, TIF_SINGLESTEP); 212fa1e03eaSRoland McGrath 213fa1e03eaSRoland McGrath /* But touch TF only if it was set by us.. */ 214e1f28773SRoland McGrath if (test_and_clear_tsk_thread_flag(child, TIF_FORCED_TF)) 21565ea5b03SH. Peter Anvin task_pt_regs(child)->flags &= ~X86_EFLAGS_TF; 216fa1e03eaSRoland McGrath } 217