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: 77318f5a2aSAndy 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 160*9bd1190aSOleg Nesterov void set_task_blockstep(struct task_struct *task, bool on) 161848e8f5fSOleg Nesterov { 162848e8f5fSOleg Nesterov unsigned long debugctl; 163848e8f5fSOleg Nesterov 16495cf00faSOleg Nesterov /* 16595cf00faSOleg Nesterov * Ensure irq/preemption can't change debugctl in between. 16695cf00faSOleg Nesterov * Note also that both TIF_BLOCKSTEP and debugctl should 16795cf00faSOleg Nesterov * be changed atomically wrt preemption. 16895cf00faSOleg Nesterov * FIXME: this means that set/clear TIF_BLOCKSTEP is simply 16995cf00faSOleg Nesterov * wrong if task != current, SIGKILL can wakeup the stopped 17095cf00faSOleg Nesterov * tracee and set/clear can play with the running task, this 17195cf00faSOleg Nesterov * can confuse the next __switch_to_xtra(). 17295cf00faSOleg Nesterov */ 17395cf00faSOleg Nesterov local_irq_disable(); 174848e8f5fSOleg Nesterov debugctl = get_debugctlmsr(); 175848e8f5fSOleg Nesterov if (on) { 176848e8f5fSOleg Nesterov debugctl |= DEBUGCTLMSR_BTF; 177848e8f5fSOleg Nesterov set_tsk_thread_flag(task, TIF_BLOCKSTEP); 178848e8f5fSOleg Nesterov } else { 179848e8f5fSOleg Nesterov debugctl &= ~DEBUGCTLMSR_BTF; 180848e8f5fSOleg Nesterov clear_tsk_thread_flag(task, TIF_BLOCKSTEP); 181848e8f5fSOleg Nesterov } 18295cf00faSOleg Nesterov if (task == current) 183848e8f5fSOleg Nesterov update_debugctlmsr(debugctl); 18495cf00faSOleg Nesterov local_irq_enable(); 185848e8f5fSOleg Nesterov } 186848e8f5fSOleg Nesterov 18710faa81eSRoland McGrath /* 18810faa81eSRoland McGrath * Enable single or block step. 18910faa81eSRoland McGrath */ 19010faa81eSRoland McGrath static void enable_step(struct task_struct *child, bool block) 19110faa81eSRoland McGrath { 19210faa81eSRoland McGrath /* 19310faa81eSRoland McGrath * Make sure block stepping (BTF) is not enabled unless it should be. 19410faa81eSRoland McGrath * Note that we don't try to worry about any is_setting_trap_flag() 19510faa81eSRoland McGrath * instructions after the first when using block stepping. 19610faa81eSRoland McGrath * So no one should try to use debugger block stepping in a program 19710faa81eSRoland McGrath * that uses user-mode single stepping itself. 19810faa81eSRoland McGrath */ 199848e8f5fSOleg Nesterov if (enable_single_step(child) && block) 200848e8f5fSOleg Nesterov set_task_blockstep(child, true); 201848e8f5fSOleg Nesterov else if (test_tsk_thread_flag(child, TIF_BLOCKSTEP)) 202848e8f5fSOleg Nesterov set_task_blockstep(child, false); 20310faa81eSRoland McGrath } 20410faa81eSRoland McGrath 20510faa81eSRoland McGrath void user_enable_single_step(struct task_struct *child) 20610faa81eSRoland McGrath { 20710faa81eSRoland McGrath enable_step(child, 0); 20810faa81eSRoland McGrath } 20910faa81eSRoland McGrath 21010faa81eSRoland McGrath void user_enable_block_step(struct task_struct *child) 21110faa81eSRoland McGrath { 21210faa81eSRoland McGrath enable_step(child, 1); 213fa1e03eaSRoland McGrath } 214fa1e03eaSRoland McGrath 215fa1e03eaSRoland McGrath void user_disable_single_step(struct task_struct *child) 216fa1e03eaSRoland McGrath { 21710faa81eSRoland McGrath /* 21810faa81eSRoland McGrath * Make sure block stepping (BTF) is disabled. 21910faa81eSRoland McGrath */ 220848e8f5fSOleg Nesterov if (test_tsk_thread_flag(child, TIF_BLOCKSTEP)) 221848e8f5fSOleg Nesterov set_task_blockstep(child, false); 22210faa81eSRoland McGrath 223fa1e03eaSRoland McGrath /* Always clear TIF_SINGLESTEP... */ 224fa1e03eaSRoland McGrath clear_tsk_thread_flag(child, TIF_SINGLESTEP); 225fa1e03eaSRoland McGrath 226fa1e03eaSRoland McGrath /* But touch TF only if it was set by us.. */ 227e1f28773SRoland McGrath if (test_and_clear_tsk_thread_flag(child, TIF_FORCED_TF)) 22865ea5b03SH. Peter Anvin task_pt_regs(child)->flags &= ~X86_EFLAGS_TF; 229fa1e03eaSRoland McGrath } 230