1fa1e03eaSRoland McGrath /* 2fa1e03eaSRoland McGrath * x86 single-step support code, common to 32-bit and 64-bit. 3fa1e03eaSRoland McGrath */ 4fa1e03eaSRoland McGrath #include <linux/sched.h> 568db0cf1SIngo Molnar #include <linux/sched/task_stack.h> 6fa1e03eaSRoland McGrath #include <linux/mm.h> 7fa1e03eaSRoland McGrath #include <linux/ptrace.h> 8254e0a6bSAkinobu Mita #include <asm/desc.h> 937868fe1SAndy Lutomirski #include <asm/mmu_context.h> 10fa1e03eaSRoland McGrath 1137cd9cf3SHarvey Harrison unsigned long convert_ip_to_linear(struct task_struct *child, struct pt_regs *regs) 12fa1e03eaSRoland McGrath { 13fa1e03eaSRoland McGrath unsigned long addr, seg; 14fa1e03eaSRoland McGrath 1565ea5b03SH. Peter Anvin addr = regs->ip; 16fa1e03eaSRoland McGrath seg = regs->cs & 0xffff; 1765ea5b03SH. Peter Anvin if (v8086_mode(regs)) { 187122ec81SRoland McGrath addr = (addr & 0xffff) + (seg << 4); 197122ec81SRoland McGrath return addr; 207122ec81SRoland McGrath } 21fa1e03eaSRoland McGrath 22a5b9e5a2SAndy Lutomirski #ifdef CONFIG_MODIFY_LDT_SYSCALL 23fa1e03eaSRoland McGrath /* 24fa1e03eaSRoland McGrath * We'll assume that the code segments in the GDT 25fa1e03eaSRoland McGrath * are all zero-based. That is largely true: the 26fa1e03eaSRoland McGrath * TLS segments are used for data, and the PNPBIOS 27fa1e03eaSRoland McGrath * and APM bios ones we just ignore here. 28fa1e03eaSRoland McGrath */ 293f80c1adSRoland McGrath if ((seg & SEGMENT_TI_MASK) == SEGMENT_LDT) { 30254e0a6bSAkinobu Mita struct desc_struct *desc; 31fa1e03eaSRoland McGrath unsigned long base; 32fa1e03eaSRoland McGrath 33136d9d83SJuergen Gross seg >>= 3; 34fa1e03eaSRoland McGrath 35fa1e03eaSRoland McGrath mutex_lock(&child->mm->context.lock); 3637868fe1SAndy Lutomirski if (unlikely(!child->mm->context.ldt || 37*bbf79d21SBorislav Petkov seg >= child->mm->context.ldt->nr_entries)) 38fa1e03eaSRoland McGrath addr = -1L; /* bogus selector, access would fault */ 39fa1e03eaSRoland McGrath else { 4037868fe1SAndy Lutomirski desc = &child->mm->context.ldt->entries[seg]; 41254e0a6bSAkinobu Mita base = get_desc_base(desc); 42fa1e03eaSRoland McGrath 43fa1e03eaSRoland McGrath /* 16-bit code segment? */ 44254e0a6bSAkinobu Mita if (!desc->d) 45fa1e03eaSRoland McGrath addr &= 0xffff; 46fa1e03eaSRoland McGrath addr += base; 47fa1e03eaSRoland McGrath } 48fa1e03eaSRoland McGrath mutex_unlock(&child->mm->context.lock); 49fa1e03eaSRoland McGrath } 50a5b9e5a2SAndy Lutomirski #endif 51fa1e03eaSRoland McGrath 52fa1e03eaSRoland McGrath return addr; 53fa1e03eaSRoland McGrath } 54fa1e03eaSRoland McGrath 55fa1e03eaSRoland McGrath static int is_setting_trap_flag(struct task_struct *child, struct pt_regs *regs) 56fa1e03eaSRoland McGrath { 57fa1e03eaSRoland McGrath int i, copied; 58fa1e03eaSRoland McGrath unsigned char opcode[15]; 5937cd9cf3SHarvey Harrison unsigned long addr = convert_ip_to_linear(child, regs); 60fa1e03eaSRoland McGrath 61f307ab6dSLorenzo Stoakes copied = access_process_vm(child, addr, opcode, sizeof(opcode), 62f307ab6dSLorenzo Stoakes FOLL_FORCE); 63fa1e03eaSRoland McGrath for (i = 0; i < copied; i++) { 64fa1e03eaSRoland McGrath switch (opcode[i]) { 65fa1e03eaSRoland McGrath /* popf and iret */ 66fa1e03eaSRoland McGrath case 0x9d: case 0xcf: 67fa1e03eaSRoland McGrath return 1; 68fa1e03eaSRoland McGrath 69fa1e03eaSRoland McGrath /* CHECKME: 64 65 */ 70fa1e03eaSRoland McGrath 71fa1e03eaSRoland McGrath /* opcode and address size prefixes */ 72fa1e03eaSRoland McGrath case 0x66: case 0x67: 73fa1e03eaSRoland McGrath continue; 74fa1e03eaSRoland McGrath /* irrelevant prefixes (segment overrides and repeats) */ 75fa1e03eaSRoland McGrath case 0x26: case 0x2e: 76fa1e03eaSRoland McGrath case 0x36: case 0x3e: 77fa1e03eaSRoland McGrath case 0x64: case 0x65: 785f76cb1fSRoland McGrath case 0xf0: case 0xf2: case 0xf3: 79fa1e03eaSRoland McGrath continue; 80fa1e03eaSRoland McGrath 817122ec81SRoland McGrath #ifdef CONFIG_X86_64 82fa1e03eaSRoland McGrath case 0x40 ... 0x4f: 83318f5a2aSAndy Lutomirski if (!user_64bit_mode(regs)) 84fa1e03eaSRoland McGrath /* 32-bit mode: register increment */ 85fa1e03eaSRoland McGrath return 0; 86fa1e03eaSRoland McGrath /* 64-bit mode: REX prefix */ 87fa1e03eaSRoland McGrath continue; 887122ec81SRoland McGrath #endif 89fa1e03eaSRoland McGrath 90fa1e03eaSRoland McGrath /* CHECKME: f2, f3 */ 91fa1e03eaSRoland McGrath 92fa1e03eaSRoland McGrath /* 93fa1e03eaSRoland McGrath * pushf: NOTE! We should probably not let 94fa1e03eaSRoland McGrath * the user see the TF bit being set. But 95fa1e03eaSRoland McGrath * it's more pain than it's worth to avoid 96fa1e03eaSRoland McGrath * it, and a debugger could emulate this 97fa1e03eaSRoland McGrath * all in user space if it _really_ cares. 98fa1e03eaSRoland McGrath */ 99fa1e03eaSRoland McGrath case 0x9c: 100fa1e03eaSRoland McGrath default: 101fa1e03eaSRoland McGrath return 0; 102fa1e03eaSRoland McGrath } 103fa1e03eaSRoland McGrath } 104fa1e03eaSRoland McGrath return 0; 105fa1e03eaSRoland McGrath } 106fa1e03eaSRoland McGrath 10710faa81eSRoland McGrath /* 10810faa81eSRoland McGrath * Enable single-stepping. Return nonzero if user mode is not using TF itself. 10910faa81eSRoland McGrath */ 11010faa81eSRoland McGrath static int enable_single_step(struct task_struct *child) 111fa1e03eaSRoland McGrath { 112fa1e03eaSRoland McGrath struct pt_regs *regs = task_pt_regs(child); 1136718d0d6SRoland McGrath unsigned long oflags; 114fa1e03eaSRoland McGrath 115fa1e03eaSRoland McGrath /* 116380fdd75SRoland McGrath * If we stepped into a sysenter/syscall insn, it trapped in 117380fdd75SRoland McGrath * kernel mode; do_debug() cleared TF and set TIF_SINGLESTEP. 118380fdd75SRoland McGrath * If user-mode had set TF itself, then it's still clear from 119380fdd75SRoland McGrath * do_debug() and we need to set it again to restore the user 120380fdd75SRoland McGrath * state so we don't wrongly set TIF_FORCED_TF below. 121380fdd75SRoland McGrath * If enable_single_step() was used last and that is what 122380fdd75SRoland McGrath * set TIF_SINGLESTEP, then both TF and TIF_FORCED_TF are 123380fdd75SRoland McGrath * already set and our bookkeeping is fine. 124380fdd75SRoland McGrath */ 125380fdd75SRoland McGrath if (unlikely(test_tsk_thread_flag(child, TIF_SINGLESTEP))) 126380fdd75SRoland McGrath regs->flags |= X86_EFLAGS_TF; 127380fdd75SRoland McGrath 128380fdd75SRoland McGrath /* 129fa1e03eaSRoland McGrath * Always set TIF_SINGLESTEP - this guarantees that 130fa1e03eaSRoland McGrath * we single-step system calls etc.. This will also 131fa1e03eaSRoland McGrath * cause us to set TF when returning to user mode. 132fa1e03eaSRoland McGrath */ 133fa1e03eaSRoland McGrath set_tsk_thread_flag(child, TIF_SINGLESTEP); 134fa1e03eaSRoland McGrath 1356718d0d6SRoland McGrath oflags = regs->flags; 136fa1e03eaSRoland McGrath 137fa1e03eaSRoland McGrath /* Set TF on the kernel stack.. */ 13865ea5b03SH. Peter Anvin regs->flags |= X86_EFLAGS_TF; 139fa1e03eaSRoland McGrath 140fa1e03eaSRoland McGrath /* 141fa1e03eaSRoland McGrath * ..but if TF is changed by the instruction we will trace, 142fa1e03eaSRoland McGrath * don't mark it as being "us" that set it, so that we 143fa1e03eaSRoland McGrath * won't clear it by hand later. 1446718d0d6SRoland McGrath * 1456718d0d6SRoland McGrath * Note that if we don't actually execute the popf because 1466718d0d6SRoland McGrath * of a signal arriving right now or suchlike, we will lose 1476718d0d6SRoland McGrath * track of the fact that it really was "us" that set it. 148fa1e03eaSRoland McGrath */ 1496718d0d6SRoland McGrath if (is_setting_trap_flag(child, regs)) { 1506718d0d6SRoland McGrath clear_tsk_thread_flag(child, TIF_FORCED_TF); 15110faa81eSRoland McGrath return 0; 1526718d0d6SRoland McGrath } 1536718d0d6SRoland McGrath 1546718d0d6SRoland McGrath /* 1556718d0d6SRoland McGrath * If TF was already set, check whether it was us who set it. 1566718d0d6SRoland McGrath * If not, we should never attempt a block step. 1576718d0d6SRoland McGrath */ 1586718d0d6SRoland McGrath if (oflags & X86_EFLAGS_TF) 1596718d0d6SRoland McGrath return test_tsk_thread_flag(child, TIF_FORCED_TF); 160fa1e03eaSRoland McGrath 161e1f28773SRoland McGrath set_tsk_thread_flag(child, TIF_FORCED_TF); 16210faa81eSRoland McGrath 16310faa81eSRoland McGrath return 1; 16410faa81eSRoland McGrath } 16510faa81eSRoland McGrath 1669bd1190aSOleg Nesterov void set_task_blockstep(struct task_struct *task, bool on) 167848e8f5fSOleg Nesterov { 168848e8f5fSOleg Nesterov unsigned long debugctl; 169848e8f5fSOleg Nesterov 17095cf00faSOleg Nesterov /* 17195cf00faSOleg Nesterov * Ensure irq/preemption can't change debugctl in between. 17295cf00faSOleg Nesterov * Note also that both TIF_BLOCKSTEP and debugctl should 17395cf00faSOleg Nesterov * be changed atomically wrt preemption. 1749899d11fSOleg Nesterov * 1759899d11fSOleg Nesterov * NOTE: this means that set/clear TIF_BLOCKSTEP is only safe if 1769899d11fSOleg Nesterov * task is current or it can't be running, otherwise we can race 1779899d11fSOleg Nesterov * with __switch_to_xtra(). We rely on ptrace_freeze_traced() but 1789899d11fSOleg Nesterov * PTRACE_KILL is not safe. 17995cf00faSOleg Nesterov */ 18095cf00faSOleg Nesterov local_irq_disable(); 181848e8f5fSOleg Nesterov debugctl = get_debugctlmsr(); 182848e8f5fSOleg Nesterov if (on) { 183848e8f5fSOleg Nesterov debugctl |= DEBUGCTLMSR_BTF; 184848e8f5fSOleg Nesterov set_tsk_thread_flag(task, TIF_BLOCKSTEP); 185848e8f5fSOleg Nesterov } else { 186848e8f5fSOleg Nesterov debugctl &= ~DEBUGCTLMSR_BTF; 187848e8f5fSOleg Nesterov clear_tsk_thread_flag(task, TIF_BLOCKSTEP); 188848e8f5fSOleg Nesterov } 18995cf00faSOleg Nesterov if (task == current) 190848e8f5fSOleg Nesterov update_debugctlmsr(debugctl); 19195cf00faSOleg Nesterov local_irq_enable(); 192848e8f5fSOleg Nesterov } 193848e8f5fSOleg Nesterov 19410faa81eSRoland McGrath /* 19510faa81eSRoland McGrath * Enable single or block step. 19610faa81eSRoland McGrath */ 19710faa81eSRoland McGrath static void enable_step(struct task_struct *child, bool block) 19810faa81eSRoland McGrath { 19910faa81eSRoland McGrath /* 20010faa81eSRoland McGrath * Make sure block stepping (BTF) is not enabled unless it should be. 20110faa81eSRoland McGrath * Note that we don't try to worry about any is_setting_trap_flag() 20210faa81eSRoland McGrath * instructions after the first when using block stepping. 20310faa81eSRoland McGrath * So no one should try to use debugger block stepping in a program 20410faa81eSRoland McGrath * that uses user-mode single stepping itself. 20510faa81eSRoland McGrath */ 206848e8f5fSOleg Nesterov if (enable_single_step(child) && block) 207848e8f5fSOleg Nesterov set_task_blockstep(child, true); 208848e8f5fSOleg Nesterov else if (test_tsk_thread_flag(child, TIF_BLOCKSTEP)) 209848e8f5fSOleg Nesterov set_task_blockstep(child, false); 21010faa81eSRoland McGrath } 21110faa81eSRoland McGrath 21210faa81eSRoland McGrath void user_enable_single_step(struct task_struct *child) 21310faa81eSRoland McGrath { 21410faa81eSRoland McGrath enable_step(child, 0); 21510faa81eSRoland McGrath } 21610faa81eSRoland McGrath 21710faa81eSRoland McGrath void user_enable_block_step(struct task_struct *child) 21810faa81eSRoland McGrath { 21910faa81eSRoland McGrath enable_step(child, 1); 220fa1e03eaSRoland McGrath } 221fa1e03eaSRoland McGrath 222fa1e03eaSRoland McGrath void user_disable_single_step(struct task_struct *child) 223fa1e03eaSRoland McGrath { 22410faa81eSRoland McGrath /* 22510faa81eSRoland McGrath * Make sure block stepping (BTF) is disabled. 22610faa81eSRoland McGrath */ 227848e8f5fSOleg Nesterov if (test_tsk_thread_flag(child, TIF_BLOCKSTEP)) 228848e8f5fSOleg Nesterov set_task_blockstep(child, false); 22910faa81eSRoland McGrath 230fa1e03eaSRoland McGrath /* Always clear TIF_SINGLESTEP... */ 231fa1e03eaSRoland McGrath clear_tsk_thread_flag(child, TIF_SINGLESTEP); 232fa1e03eaSRoland McGrath 233fa1e03eaSRoland McGrath /* But touch TF only if it was set by us.. */ 234e1f28773SRoland McGrath if (test_and_clear_tsk_thread_flag(child, TIF_FORCED_TF)) 23565ea5b03SH. Peter Anvin task_pt_regs(child)->flags &= ~X86_EFLAGS_TF; 236fa1e03eaSRoland McGrath } 237