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> 7fa1e03eaSRoland McGrath 837cd9cf3SHarvey Harrison unsigned long convert_ip_to_linear(struct task_struct *child, struct pt_regs *regs) 9fa1e03eaSRoland McGrath { 10fa1e03eaSRoland McGrath unsigned long addr, seg; 11fa1e03eaSRoland McGrath 1265ea5b03SH. Peter Anvin addr = regs->ip; 13fa1e03eaSRoland McGrath seg = regs->cs & 0xffff; 1465ea5b03SH. Peter Anvin if (v8086_mode(regs)) { 157122ec81SRoland McGrath addr = (addr & 0xffff) + (seg << 4); 167122ec81SRoland McGrath return addr; 177122ec81SRoland McGrath } 18fa1e03eaSRoland McGrath 19fa1e03eaSRoland McGrath /* 20fa1e03eaSRoland McGrath * We'll assume that the code segments in the GDT 21fa1e03eaSRoland McGrath * are all zero-based. That is largely true: the 22fa1e03eaSRoland McGrath * TLS segments are used for data, and the PNPBIOS 23fa1e03eaSRoland McGrath * and APM bios ones we just ignore here. 24fa1e03eaSRoland McGrath */ 253f80c1adSRoland McGrath if ((seg & SEGMENT_TI_MASK) == SEGMENT_LDT) { 26fa1e03eaSRoland McGrath u32 *desc; 27fa1e03eaSRoland McGrath unsigned long base; 28fa1e03eaSRoland McGrath 29fa1e03eaSRoland McGrath seg &= ~7UL; 30fa1e03eaSRoland McGrath 31fa1e03eaSRoland McGrath mutex_lock(&child->mm->context.lock); 32fa1e03eaSRoland McGrath if (unlikely((seg >> 3) >= child->mm->context.size)) 33fa1e03eaSRoland McGrath addr = -1L; /* bogus selector, access would fault */ 34fa1e03eaSRoland McGrath else { 35fa1e03eaSRoland McGrath desc = child->mm->context.ldt + seg; 36fa1e03eaSRoland McGrath base = ((desc[0] >> 16) | 37fa1e03eaSRoland McGrath ((desc[1] & 0xff) << 16) | 38fa1e03eaSRoland McGrath (desc[1] & 0xff000000)); 39fa1e03eaSRoland McGrath 40fa1e03eaSRoland McGrath /* 16-bit code segment? */ 41fa1e03eaSRoland McGrath if (!((desc[1] >> 22) & 1)) 42fa1e03eaSRoland McGrath addr &= 0xffff; 43fa1e03eaSRoland McGrath addr += base; 44fa1e03eaSRoland McGrath } 45fa1e03eaSRoland McGrath mutex_unlock(&child->mm->context.lock); 46fa1e03eaSRoland McGrath } 47fa1e03eaSRoland McGrath 48fa1e03eaSRoland McGrath return addr; 49fa1e03eaSRoland McGrath } 50fa1e03eaSRoland McGrath 51fa1e03eaSRoland McGrath static int is_setting_trap_flag(struct task_struct *child, struct pt_regs *regs) 52fa1e03eaSRoland McGrath { 53fa1e03eaSRoland McGrath int i, copied; 54fa1e03eaSRoland McGrath unsigned char opcode[15]; 5537cd9cf3SHarvey Harrison unsigned long addr = convert_ip_to_linear(child, regs); 56fa1e03eaSRoland McGrath 57fa1e03eaSRoland McGrath copied = access_process_vm(child, addr, opcode, sizeof(opcode), 0); 58fa1e03eaSRoland McGrath for (i = 0; i < copied; i++) { 59fa1e03eaSRoland McGrath switch (opcode[i]) { 60fa1e03eaSRoland McGrath /* popf and iret */ 61fa1e03eaSRoland McGrath case 0x9d: case 0xcf: 62fa1e03eaSRoland McGrath return 1; 63fa1e03eaSRoland McGrath 64fa1e03eaSRoland McGrath /* CHECKME: 64 65 */ 65fa1e03eaSRoland McGrath 66fa1e03eaSRoland McGrath /* opcode and address size prefixes */ 67fa1e03eaSRoland McGrath case 0x66: case 0x67: 68fa1e03eaSRoland McGrath continue; 69fa1e03eaSRoland McGrath /* irrelevant prefixes (segment overrides and repeats) */ 70fa1e03eaSRoland McGrath case 0x26: case 0x2e: 71fa1e03eaSRoland McGrath case 0x36: case 0x3e: 72fa1e03eaSRoland McGrath case 0x64: case 0x65: 735f76cb1fSRoland McGrath case 0xf0: case 0xf2: case 0xf3: 74fa1e03eaSRoland McGrath continue; 75fa1e03eaSRoland McGrath 767122ec81SRoland McGrath #ifdef CONFIG_X86_64 77fa1e03eaSRoland McGrath case 0x40 ... 0x4f: 78fa1e03eaSRoland McGrath if (regs->cs != __USER_CS) 79fa1e03eaSRoland McGrath /* 32-bit mode: register increment */ 80fa1e03eaSRoland McGrath return 0; 81fa1e03eaSRoland McGrath /* 64-bit mode: REX prefix */ 82fa1e03eaSRoland McGrath continue; 837122ec81SRoland McGrath #endif 84fa1e03eaSRoland McGrath 85fa1e03eaSRoland McGrath /* CHECKME: f2, f3 */ 86fa1e03eaSRoland McGrath 87fa1e03eaSRoland McGrath /* 88fa1e03eaSRoland McGrath * pushf: NOTE! We should probably not let 89fa1e03eaSRoland McGrath * the user see the TF bit being set. But 90fa1e03eaSRoland McGrath * it's more pain than it's worth to avoid 91fa1e03eaSRoland McGrath * it, and a debugger could emulate this 92fa1e03eaSRoland McGrath * all in user space if it _really_ cares. 93fa1e03eaSRoland McGrath */ 94fa1e03eaSRoland McGrath case 0x9c: 95fa1e03eaSRoland McGrath default: 96fa1e03eaSRoland McGrath return 0; 97fa1e03eaSRoland McGrath } 98fa1e03eaSRoland McGrath } 99fa1e03eaSRoland McGrath return 0; 100fa1e03eaSRoland McGrath } 101fa1e03eaSRoland McGrath 10210faa81eSRoland McGrath /* 10310faa81eSRoland McGrath * Enable single-stepping. Return nonzero if user mode is not using TF itself. 10410faa81eSRoland McGrath */ 10510faa81eSRoland McGrath static int enable_single_step(struct task_struct *child) 106fa1e03eaSRoland McGrath { 107fa1e03eaSRoland McGrath struct pt_regs *regs = task_pt_regs(child); 108*6718d0d6SRoland McGrath unsigned long oflags; 109fa1e03eaSRoland McGrath 110fa1e03eaSRoland McGrath /* 111fa1e03eaSRoland McGrath * Always set TIF_SINGLESTEP - this guarantees that 112fa1e03eaSRoland McGrath * we single-step system calls etc.. This will also 113fa1e03eaSRoland McGrath * cause us to set TF when returning to user mode. 114fa1e03eaSRoland McGrath */ 115fa1e03eaSRoland McGrath set_tsk_thread_flag(child, TIF_SINGLESTEP); 116fa1e03eaSRoland McGrath 117*6718d0d6SRoland McGrath oflags = regs->flags; 118fa1e03eaSRoland McGrath 119fa1e03eaSRoland McGrath /* Set TF on the kernel stack.. */ 12065ea5b03SH. Peter Anvin regs->flags |= X86_EFLAGS_TF; 121fa1e03eaSRoland McGrath 122fa1e03eaSRoland McGrath /* 123fa1e03eaSRoland McGrath * ..but if TF is changed by the instruction we will trace, 124fa1e03eaSRoland McGrath * don't mark it as being "us" that set it, so that we 125fa1e03eaSRoland McGrath * won't clear it by hand later. 126*6718d0d6SRoland McGrath * 127*6718d0d6SRoland McGrath * Note that if we don't actually execute the popf because 128*6718d0d6SRoland McGrath * of a signal arriving right now or suchlike, we will lose 129*6718d0d6SRoland McGrath * track of the fact that it really was "us" that set it. 130fa1e03eaSRoland McGrath */ 131*6718d0d6SRoland McGrath if (is_setting_trap_flag(child, regs)) { 132*6718d0d6SRoland McGrath clear_tsk_thread_flag(child, TIF_FORCED_TF); 13310faa81eSRoland McGrath return 0; 134*6718d0d6SRoland McGrath } 135*6718d0d6SRoland McGrath 136*6718d0d6SRoland McGrath /* 137*6718d0d6SRoland McGrath * If TF was already set, check whether it was us who set it. 138*6718d0d6SRoland McGrath * If not, we should never attempt a block step. 139*6718d0d6SRoland McGrath */ 140*6718d0d6SRoland McGrath if (oflags & X86_EFLAGS_TF) 141*6718d0d6SRoland McGrath return test_tsk_thread_flag(child, TIF_FORCED_TF); 142fa1e03eaSRoland McGrath 143e1f28773SRoland McGrath set_tsk_thread_flag(child, TIF_FORCED_TF); 14410faa81eSRoland McGrath 14510faa81eSRoland McGrath return 1; 14610faa81eSRoland McGrath } 14710faa81eSRoland McGrath 14810faa81eSRoland McGrath /* 14910faa81eSRoland McGrath * Install this value in MSR_IA32_DEBUGCTLMSR whenever child is running. 15010faa81eSRoland McGrath */ 15110faa81eSRoland McGrath static void write_debugctlmsr(struct task_struct *child, unsigned long val) 15210faa81eSRoland McGrath { 1534ba51fd7SRoland McGrath if (child->thread.debugctlmsr == val) 1544ba51fd7SRoland McGrath return; 1554ba51fd7SRoland McGrath 15610faa81eSRoland McGrath child->thread.debugctlmsr = val; 15710faa81eSRoland McGrath 15810faa81eSRoland McGrath if (child != current) 15910faa81eSRoland McGrath return; 16010faa81eSRoland McGrath 1615b0e5084SJan Beulich update_debugctlmsr(val); 16210faa81eSRoland McGrath } 16310faa81eSRoland McGrath 16410faa81eSRoland McGrath /* 16510faa81eSRoland McGrath * Enable single or block step. 16610faa81eSRoland McGrath */ 16710faa81eSRoland McGrath static void enable_step(struct task_struct *child, bool block) 16810faa81eSRoland McGrath { 16910faa81eSRoland McGrath /* 17010faa81eSRoland McGrath * Make sure block stepping (BTF) is not enabled unless it should be. 17110faa81eSRoland McGrath * Note that we don't try to worry about any is_setting_trap_flag() 17210faa81eSRoland McGrath * instructions after the first when using block stepping. 17310faa81eSRoland McGrath * So noone should try to use debugger block stepping in a program 17410faa81eSRoland McGrath * that uses user-mode single stepping itself. 17510faa81eSRoland McGrath */ 17610faa81eSRoland McGrath if (enable_single_step(child) && block) { 17710faa81eSRoland McGrath set_tsk_thread_flag(child, TIF_DEBUGCTLMSR); 178eee3af4aSMarkus Metzger write_debugctlmsr(child, 179eee3af4aSMarkus Metzger child->thread.debugctlmsr | DEBUGCTLMSR_BTF); 180eee3af4aSMarkus Metzger } else { 181eee3af4aSMarkus Metzger write_debugctlmsr(child, 182d032b31aSJan Beulich child->thread.debugctlmsr & ~DEBUGCTLMSR_BTF); 183eee3af4aSMarkus Metzger 184eee3af4aSMarkus Metzger if (!child->thread.debugctlmsr) 185eee3af4aSMarkus Metzger clear_tsk_thread_flag(child, TIF_DEBUGCTLMSR); 18610faa81eSRoland McGrath } 18710faa81eSRoland McGrath } 18810faa81eSRoland McGrath 18910faa81eSRoland McGrath void user_enable_single_step(struct task_struct *child) 19010faa81eSRoland McGrath { 19110faa81eSRoland McGrath enable_step(child, 0); 19210faa81eSRoland McGrath } 19310faa81eSRoland McGrath 19410faa81eSRoland McGrath void user_enable_block_step(struct task_struct *child) 19510faa81eSRoland McGrath { 19610faa81eSRoland McGrath enable_step(child, 1); 197fa1e03eaSRoland McGrath } 198fa1e03eaSRoland McGrath 199fa1e03eaSRoland McGrath void user_disable_single_step(struct task_struct *child) 200fa1e03eaSRoland McGrath { 20110faa81eSRoland McGrath /* 20210faa81eSRoland McGrath * Make sure block stepping (BTF) is disabled. 20310faa81eSRoland McGrath */ 204eee3af4aSMarkus Metzger write_debugctlmsr(child, 205d032b31aSJan Beulich child->thread.debugctlmsr & ~DEBUGCTLMSR_BTF); 206eee3af4aSMarkus Metzger 207eee3af4aSMarkus Metzger if (!child->thread.debugctlmsr) 208eee3af4aSMarkus Metzger clear_tsk_thread_flag(child, TIF_DEBUGCTLMSR); 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