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> 837868fe1SAndy Lutomirski #include <asm/mmu_context.h> 9fa1e03eaSRoland McGrath 1037cd9cf3SHarvey Harrison unsigned long convert_ip_to_linear(struct task_struct *child, struct pt_regs *regs) 11fa1e03eaSRoland McGrath { 12fa1e03eaSRoland McGrath unsigned long addr, seg; 13fa1e03eaSRoland McGrath 1465ea5b03SH. Peter Anvin addr = regs->ip; 15fa1e03eaSRoland McGrath seg = regs->cs & 0xffff; 1665ea5b03SH. Peter Anvin if (v8086_mode(regs)) { 177122ec81SRoland McGrath addr = (addr & 0xffff) + (seg << 4); 187122ec81SRoland McGrath return addr; 197122ec81SRoland McGrath } 20fa1e03eaSRoland McGrath 21*a5b9e5a2SAndy Lutomirski #ifdef CONFIG_MODIFY_LDT_SYSCALL 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) { 29254e0a6bSAkinobu Mita struct desc_struct *desc; 30fa1e03eaSRoland McGrath unsigned long base; 31fa1e03eaSRoland McGrath 32fa1e03eaSRoland McGrath seg &= ~7UL; 33fa1e03eaSRoland McGrath 34fa1e03eaSRoland McGrath mutex_lock(&child->mm->context.lock); 3537868fe1SAndy Lutomirski if (unlikely(!child->mm->context.ldt || 3637868fe1SAndy Lutomirski (seg >> 3) >= child->mm->context.ldt->size)) 37fa1e03eaSRoland McGrath addr = -1L; /* bogus selector, access would fault */ 38fa1e03eaSRoland McGrath else { 3937868fe1SAndy Lutomirski desc = &child->mm->context.ldt->entries[seg]; 40254e0a6bSAkinobu Mita base = get_desc_base(desc); 41fa1e03eaSRoland McGrath 42fa1e03eaSRoland McGrath /* 16-bit code segment? */ 43254e0a6bSAkinobu Mita if (!desc->d) 44fa1e03eaSRoland McGrath addr &= 0xffff; 45fa1e03eaSRoland McGrath addr += base; 46fa1e03eaSRoland McGrath } 47fa1e03eaSRoland McGrath mutex_unlock(&child->mm->context.lock); 48fa1e03eaSRoland McGrath } 49*a5b9e5a2SAndy Lutomirski #endif 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]; 5837cd9cf3SHarvey Harrison unsigned long addr = convert_ip_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: 81318f5a2aSAndy Lutomirski if (!user_64bit_mode(regs)) 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); 1116718d0d6SRoland McGrath unsigned long oflags; 112fa1e03eaSRoland McGrath 113fa1e03eaSRoland McGrath /* 114380fdd75SRoland McGrath * If we stepped into a sysenter/syscall insn, it trapped in 115380fdd75SRoland McGrath * kernel mode; do_debug() cleared TF and set TIF_SINGLESTEP. 116380fdd75SRoland McGrath * If user-mode had set TF itself, then it's still clear from 117380fdd75SRoland McGrath * do_debug() and we need to set it again to restore the user 118380fdd75SRoland McGrath * state so we don't wrongly set TIF_FORCED_TF below. 119380fdd75SRoland McGrath * If enable_single_step() was used last and that is what 120380fdd75SRoland McGrath * set TIF_SINGLESTEP, then both TF and TIF_FORCED_TF are 121380fdd75SRoland McGrath * already set and our bookkeeping is fine. 122380fdd75SRoland McGrath */ 123380fdd75SRoland McGrath if (unlikely(test_tsk_thread_flag(child, TIF_SINGLESTEP))) 124380fdd75SRoland McGrath regs->flags |= X86_EFLAGS_TF; 125380fdd75SRoland McGrath 126380fdd75SRoland McGrath /* 127fa1e03eaSRoland McGrath * Always set TIF_SINGLESTEP - this guarantees that 128fa1e03eaSRoland McGrath * we single-step system calls etc.. This will also 129fa1e03eaSRoland McGrath * cause us to set TF when returning to user mode. 130fa1e03eaSRoland McGrath */ 131fa1e03eaSRoland McGrath set_tsk_thread_flag(child, TIF_SINGLESTEP); 132fa1e03eaSRoland McGrath 1336718d0d6SRoland McGrath oflags = regs->flags; 134fa1e03eaSRoland McGrath 135fa1e03eaSRoland McGrath /* Set TF on the kernel stack.. */ 13665ea5b03SH. Peter Anvin regs->flags |= X86_EFLAGS_TF; 137fa1e03eaSRoland McGrath 138fa1e03eaSRoland McGrath /* 139fa1e03eaSRoland McGrath * ..but if TF is changed by the instruction we will trace, 140fa1e03eaSRoland McGrath * don't mark it as being "us" that set it, so that we 141fa1e03eaSRoland McGrath * won't clear it by hand later. 1426718d0d6SRoland McGrath * 1436718d0d6SRoland McGrath * Note that if we don't actually execute the popf because 1446718d0d6SRoland McGrath * of a signal arriving right now or suchlike, we will lose 1456718d0d6SRoland McGrath * track of the fact that it really was "us" that set it. 146fa1e03eaSRoland McGrath */ 1476718d0d6SRoland McGrath if (is_setting_trap_flag(child, regs)) { 1486718d0d6SRoland McGrath clear_tsk_thread_flag(child, TIF_FORCED_TF); 14910faa81eSRoland McGrath return 0; 1506718d0d6SRoland McGrath } 1516718d0d6SRoland McGrath 1526718d0d6SRoland McGrath /* 1536718d0d6SRoland McGrath * If TF was already set, check whether it was us who set it. 1546718d0d6SRoland McGrath * If not, we should never attempt a block step. 1556718d0d6SRoland McGrath */ 1566718d0d6SRoland McGrath if (oflags & X86_EFLAGS_TF) 1576718d0d6SRoland McGrath return test_tsk_thread_flag(child, TIF_FORCED_TF); 158fa1e03eaSRoland McGrath 159e1f28773SRoland McGrath set_tsk_thread_flag(child, TIF_FORCED_TF); 16010faa81eSRoland McGrath 16110faa81eSRoland McGrath return 1; 16210faa81eSRoland McGrath } 16310faa81eSRoland McGrath 1649bd1190aSOleg Nesterov void set_task_blockstep(struct task_struct *task, bool on) 165848e8f5fSOleg Nesterov { 166848e8f5fSOleg Nesterov unsigned long debugctl; 167848e8f5fSOleg Nesterov 16895cf00faSOleg Nesterov /* 16995cf00faSOleg Nesterov * Ensure irq/preemption can't change debugctl in between. 17095cf00faSOleg Nesterov * Note also that both TIF_BLOCKSTEP and debugctl should 17195cf00faSOleg Nesterov * be changed atomically wrt preemption. 1729899d11fSOleg Nesterov * 1739899d11fSOleg Nesterov * NOTE: this means that set/clear TIF_BLOCKSTEP is only safe if 1749899d11fSOleg Nesterov * task is current or it can't be running, otherwise we can race 1759899d11fSOleg Nesterov * with __switch_to_xtra(). We rely on ptrace_freeze_traced() but 1769899d11fSOleg Nesterov * PTRACE_KILL is not safe. 17795cf00faSOleg Nesterov */ 17895cf00faSOleg Nesterov local_irq_disable(); 179848e8f5fSOleg Nesterov debugctl = get_debugctlmsr(); 180848e8f5fSOleg Nesterov if (on) { 181848e8f5fSOleg Nesterov debugctl |= DEBUGCTLMSR_BTF; 182848e8f5fSOleg Nesterov set_tsk_thread_flag(task, TIF_BLOCKSTEP); 183848e8f5fSOleg Nesterov } else { 184848e8f5fSOleg Nesterov debugctl &= ~DEBUGCTLMSR_BTF; 185848e8f5fSOleg Nesterov clear_tsk_thread_flag(task, TIF_BLOCKSTEP); 186848e8f5fSOleg Nesterov } 18795cf00faSOleg Nesterov if (task == current) 188848e8f5fSOleg Nesterov update_debugctlmsr(debugctl); 18995cf00faSOleg Nesterov local_irq_enable(); 190848e8f5fSOleg Nesterov } 191848e8f5fSOleg Nesterov 19210faa81eSRoland McGrath /* 19310faa81eSRoland McGrath * Enable single or block step. 19410faa81eSRoland McGrath */ 19510faa81eSRoland McGrath static void enable_step(struct task_struct *child, bool block) 19610faa81eSRoland McGrath { 19710faa81eSRoland McGrath /* 19810faa81eSRoland McGrath * Make sure block stepping (BTF) is not enabled unless it should be. 19910faa81eSRoland McGrath * Note that we don't try to worry about any is_setting_trap_flag() 20010faa81eSRoland McGrath * instructions after the first when using block stepping. 20110faa81eSRoland McGrath * So no one should try to use debugger block stepping in a program 20210faa81eSRoland McGrath * that uses user-mode single stepping itself. 20310faa81eSRoland McGrath */ 204848e8f5fSOleg Nesterov if (enable_single_step(child) && block) 205848e8f5fSOleg Nesterov set_task_blockstep(child, true); 206848e8f5fSOleg Nesterov else if (test_tsk_thread_flag(child, TIF_BLOCKSTEP)) 207848e8f5fSOleg Nesterov set_task_blockstep(child, false); 20810faa81eSRoland McGrath } 20910faa81eSRoland McGrath 21010faa81eSRoland McGrath void user_enable_single_step(struct task_struct *child) 21110faa81eSRoland McGrath { 21210faa81eSRoland McGrath enable_step(child, 0); 21310faa81eSRoland McGrath } 21410faa81eSRoland McGrath 21510faa81eSRoland McGrath void user_enable_block_step(struct task_struct *child) 21610faa81eSRoland McGrath { 21710faa81eSRoland McGrath enable_step(child, 1); 218fa1e03eaSRoland McGrath } 219fa1e03eaSRoland McGrath 220fa1e03eaSRoland McGrath void user_disable_single_step(struct task_struct *child) 221fa1e03eaSRoland McGrath { 22210faa81eSRoland McGrath /* 22310faa81eSRoland McGrath * Make sure block stepping (BTF) is disabled. 22410faa81eSRoland McGrath */ 225848e8f5fSOleg Nesterov if (test_tsk_thread_flag(child, TIF_BLOCKSTEP)) 226848e8f5fSOleg Nesterov set_task_blockstep(child, false); 22710faa81eSRoland McGrath 228fa1e03eaSRoland McGrath /* Always clear TIF_SINGLESTEP... */ 229fa1e03eaSRoland McGrath clear_tsk_thread_flag(child, TIF_SINGLESTEP); 230fa1e03eaSRoland McGrath 231fa1e03eaSRoland McGrath /* But touch TF only if it was set by us.. */ 232e1f28773SRoland McGrath if (test_and_clear_tsk_thread_flag(child, TIF_FORCED_TF)) 23365ea5b03SH. Peter Anvin task_pt_regs(child)->flags &= ~X86_EFLAGS_TF; 234fa1e03eaSRoland McGrath } 235