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 21a5b9e5a2SAndy 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 32136d9d83SJuergen Gross seg >>= 3; 33fa1e03eaSRoland McGrath 34fa1e03eaSRoland McGrath mutex_lock(&child->mm->context.lock); 3537868fe1SAndy Lutomirski if (unlikely(!child->mm->context.ldt || 36136d9d83SJuergen Gross seg >= 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 } 49a5b9e5a2SAndy 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 60*f307ab6dSLorenzo Stoakes copied = access_process_vm(child, addr, opcode, sizeof(opcode), 61*f307ab6dSLorenzo Stoakes FOLL_FORCE); 62fa1e03eaSRoland McGrath for (i = 0; i < copied; i++) { 63fa1e03eaSRoland McGrath switch (opcode[i]) { 64fa1e03eaSRoland McGrath /* popf and iret */ 65fa1e03eaSRoland McGrath case 0x9d: case 0xcf: 66fa1e03eaSRoland McGrath return 1; 67fa1e03eaSRoland McGrath 68fa1e03eaSRoland McGrath /* CHECKME: 64 65 */ 69fa1e03eaSRoland McGrath 70fa1e03eaSRoland McGrath /* opcode and address size prefixes */ 71fa1e03eaSRoland McGrath case 0x66: case 0x67: 72fa1e03eaSRoland McGrath continue; 73fa1e03eaSRoland McGrath /* irrelevant prefixes (segment overrides and repeats) */ 74fa1e03eaSRoland McGrath case 0x26: case 0x2e: 75fa1e03eaSRoland McGrath case 0x36: case 0x3e: 76fa1e03eaSRoland McGrath case 0x64: case 0x65: 775f76cb1fSRoland McGrath case 0xf0: case 0xf2: case 0xf3: 78fa1e03eaSRoland McGrath continue; 79fa1e03eaSRoland McGrath 807122ec81SRoland McGrath #ifdef CONFIG_X86_64 81fa1e03eaSRoland McGrath case 0x40 ... 0x4f: 82318f5a2aSAndy Lutomirski if (!user_64bit_mode(regs)) 83fa1e03eaSRoland McGrath /* 32-bit mode: register increment */ 84fa1e03eaSRoland McGrath return 0; 85fa1e03eaSRoland McGrath /* 64-bit mode: REX prefix */ 86fa1e03eaSRoland McGrath continue; 877122ec81SRoland McGrath #endif 88fa1e03eaSRoland McGrath 89fa1e03eaSRoland McGrath /* CHECKME: f2, f3 */ 90fa1e03eaSRoland McGrath 91fa1e03eaSRoland McGrath /* 92fa1e03eaSRoland McGrath * pushf: NOTE! We should probably not let 93fa1e03eaSRoland McGrath * the user see the TF bit being set. But 94fa1e03eaSRoland McGrath * it's more pain than it's worth to avoid 95fa1e03eaSRoland McGrath * it, and a debugger could emulate this 96fa1e03eaSRoland McGrath * all in user space if it _really_ cares. 97fa1e03eaSRoland McGrath */ 98fa1e03eaSRoland McGrath case 0x9c: 99fa1e03eaSRoland McGrath default: 100fa1e03eaSRoland McGrath return 0; 101fa1e03eaSRoland McGrath } 102fa1e03eaSRoland McGrath } 103fa1e03eaSRoland McGrath return 0; 104fa1e03eaSRoland McGrath } 105fa1e03eaSRoland McGrath 10610faa81eSRoland McGrath /* 10710faa81eSRoland McGrath * Enable single-stepping. Return nonzero if user mode is not using TF itself. 10810faa81eSRoland McGrath */ 10910faa81eSRoland McGrath static int enable_single_step(struct task_struct *child) 110fa1e03eaSRoland McGrath { 111fa1e03eaSRoland McGrath struct pt_regs *regs = task_pt_regs(child); 1126718d0d6SRoland McGrath unsigned long oflags; 113fa1e03eaSRoland McGrath 114fa1e03eaSRoland McGrath /* 115380fdd75SRoland McGrath * If we stepped into a sysenter/syscall insn, it trapped in 116380fdd75SRoland McGrath * kernel mode; do_debug() cleared TF and set TIF_SINGLESTEP. 117380fdd75SRoland McGrath * If user-mode had set TF itself, then it's still clear from 118380fdd75SRoland McGrath * do_debug() and we need to set it again to restore the user 119380fdd75SRoland McGrath * state so we don't wrongly set TIF_FORCED_TF below. 120380fdd75SRoland McGrath * If enable_single_step() was used last and that is what 121380fdd75SRoland McGrath * set TIF_SINGLESTEP, then both TF and TIF_FORCED_TF are 122380fdd75SRoland McGrath * already set and our bookkeeping is fine. 123380fdd75SRoland McGrath */ 124380fdd75SRoland McGrath if (unlikely(test_tsk_thread_flag(child, TIF_SINGLESTEP))) 125380fdd75SRoland McGrath regs->flags |= X86_EFLAGS_TF; 126380fdd75SRoland McGrath 127380fdd75SRoland McGrath /* 128fa1e03eaSRoland McGrath * Always set TIF_SINGLESTEP - this guarantees that 129fa1e03eaSRoland McGrath * we single-step system calls etc.. This will also 130fa1e03eaSRoland McGrath * cause us to set TF when returning to user mode. 131fa1e03eaSRoland McGrath */ 132fa1e03eaSRoland McGrath set_tsk_thread_flag(child, TIF_SINGLESTEP); 133fa1e03eaSRoland McGrath 1346718d0d6SRoland McGrath oflags = regs->flags; 135fa1e03eaSRoland McGrath 136fa1e03eaSRoland McGrath /* Set TF on the kernel stack.. */ 13765ea5b03SH. Peter Anvin regs->flags |= X86_EFLAGS_TF; 138fa1e03eaSRoland McGrath 139fa1e03eaSRoland McGrath /* 140fa1e03eaSRoland McGrath * ..but if TF is changed by the instruction we will trace, 141fa1e03eaSRoland McGrath * don't mark it as being "us" that set it, so that we 142fa1e03eaSRoland McGrath * won't clear it by hand later. 1436718d0d6SRoland McGrath * 1446718d0d6SRoland McGrath * Note that if we don't actually execute the popf because 1456718d0d6SRoland McGrath * of a signal arriving right now or suchlike, we will lose 1466718d0d6SRoland McGrath * track of the fact that it really was "us" that set it. 147fa1e03eaSRoland McGrath */ 1486718d0d6SRoland McGrath if (is_setting_trap_flag(child, regs)) { 1496718d0d6SRoland McGrath clear_tsk_thread_flag(child, TIF_FORCED_TF); 15010faa81eSRoland McGrath return 0; 1516718d0d6SRoland McGrath } 1526718d0d6SRoland McGrath 1536718d0d6SRoland McGrath /* 1546718d0d6SRoland McGrath * If TF was already set, check whether it was us who set it. 1556718d0d6SRoland McGrath * If not, we should never attempt a block step. 1566718d0d6SRoland McGrath */ 1576718d0d6SRoland McGrath if (oflags & X86_EFLAGS_TF) 1586718d0d6SRoland McGrath return test_tsk_thread_flag(child, TIF_FORCED_TF); 159fa1e03eaSRoland McGrath 160e1f28773SRoland McGrath set_tsk_thread_flag(child, TIF_FORCED_TF); 16110faa81eSRoland McGrath 16210faa81eSRoland McGrath return 1; 16310faa81eSRoland McGrath } 16410faa81eSRoland McGrath 1659bd1190aSOleg Nesterov void set_task_blockstep(struct task_struct *task, bool on) 166848e8f5fSOleg Nesterov { 167848e8f5fSOleg Nesterov unsigned long debugctl; 168848e8f5fSOleg Nesterov 16995cf00faSOleg Nesterov /* 17095cf00faSOleg Nesterov * Ensure irq/preemption can't change debugctl in between. 17195cf00faSOleg Nesterov * Note also that both TIF_BLOCKSTEP and debugctl should 17295cf00faSOleg Nesterov * be changed atomically wrt preemption. 1739899d11fSOleg Nesterov * 1749899d11fSOleg Nesterov * NOTE: this means that set/clear TIF_BLOCKSTEP is only safe if 1759899d11fSOleg Nesterov * task is current or it can't be running, otherwise we can race 1769899d11fSOleg Nesterov * with __switch_to_xtra(). We rely on ptrace_freeze_traced() but 1779899d11fSOleg Nesterov * PTRACE_KILL is not safe. 17895cf00faSOleg Nesterov */ 17995cf00faSOleg Nesterov local_irq_disable(); 180848e8f5fSOleg Nesterov debugctl = get_debugctlmsr(); 181848e8f5fSOleg Nesterov if (on) { 182848e8f5fSOleg Nesterov debugctl |= DEBUGCTLMSR_BTF; 183848e8f5fSOleg Nesterov set_tsk_thread_flag(task, TIF_BLOCKSTEP); 184848e8f5fSOleg Nesterov } else { 185848e8f5fSOleg Nesterov debugctl &= ~DEBUGCTLMSR_BTF; 186848e8f5fSOleg Nesterov clear_tsk_thread_flag(task, TIF_BLOCKSTEP); 187848e8f5fSOleg Nesterov } 18895cf00faSOleg Nesterov if (task == current) 189848e8f5fSOleg Nesterov update_debugctlmsr(debugctl); 19095cf00faSOleg Nesterov local_irq_enable(); 191848e8f5fSOleg Nesterov } 192848e8f5fSOleg Nesterov 19310faa81eSRoland McGrath /* 19410faa81eSRoland McGrath * Enable single or block step. 19510faa81eSRoland McGrath */ 19610faa81eSRoland McGrath static void enable_step(struct task_struct *child, bool block) 19710faa81eSRoland McGrath { 19810faa81eSRoland McGrath /* 19910faa81eSRoland McGrath * Make sure block stepping (BTF) is not enabled unless it should be. 20010faa81eSRoland McGrath * Note that we don't try to worry about any is_setting_trap_flag() 20110faa81eSRoland McGrath * instructions after the first when using block stepping. 20210faa81eSRoland McGrath * So no one should try to use debugger block stepping in a program 20310faa81eSRoland McGrath * that uses user-mode single stepping itself. 20410faa81eSRoland McGrath */ 205848e8f5fSOleg Nesterov if (enable_single_step(child) && block) 206848e8f5fSOleg Nesterov set_task_blockstep(child, true); 207848e8f5fSOleg Nesterov else if (test_tsk_thread_flag(child, TIF_BLOCKSTEP)) 208848e8f5fSOleg Nesterov set_task_blockstep(child, false); 20910faa81eSRoland McGrath } 21010faa81eSRoland McGrath 21110faa81eSRoland McGrath void user_enable_single_step(struct task_struct *child) 21210faa81eSRoland McGrath { 21310faa81eSRoland McGrath enable_step(child, 0); 21410faa81eSRoland McGrath } 21510faa81eSRoland McGrath 21610faa81eSRoland McGrath void user_enable_block_step(struct task_struct *child) 21710faa81eSRoland McGrath { 21810faa81eSRoland McGrath enable_step(child, 1); 219fa1e03eaSRoland McGrath } 220fa1e03eaSRoland McGrath 221fa1e03eaSRoland McGrath void user_disable_single_step(struct task_struct *child) 222fa1e03eaSRoland McGrath { 22310faa81eSRoland McGrath /* 22410faa81eSRoland McGrath * Make sure block stepping (BTF) is disabled. 22510faa81eSRoland McGrath */ 226848e8f5fSOleg Nesterov if (test_tsk_thread_flag(child, TIF_BLOCKSTEP)) 227848e8f5fSOleg Nesterov set_task_blockstep(child, false); 22810faa81eSRoland McGrath 229fa1e03eaSRoland McGrath /* Always clear TIF_SINGLESTEP... */ 230fa1e03eaSRoland McGrath clear_tsk_thread_flag(child, TIF_SINGLESTEP); 231fa1e03eaSRoland McGrath 232fa1e03eaSRoland McGrath /* But touch TF only if it was set by us.. */ 233e1f28773SRoland McGrath if (test_and_clear_tsk_thread_flag(child, TIF_FORCED_TF)) 23465ea5b03SH. Peter Anvin task_pt_regs(child)->flags &= ~X86_EFLAGS_TF; 235fa1e03eaSRoland McGrath } 236