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 8fa1e03eaSRoland McGrath unsigned long convert_rip_to_linear(struct task_struct *child, struct pt_regs *regs) 9fa1e03eaSRoland McGrath { 10fa1e03eaSRoland McGrath unsigned long addr, seg; 11fa1e03eaSRoland McGrath 12fa1e03eaSRoland McGrath addr = regs->rip; 13fa1e03eaSRoland McGrath seg = regs->cs & 0xffff; 14fa1e03eaSRoland McGrath 15fa1e03eaSRoland McGrath /* 16fa1e03eaSRoland McGrath * We'll assume that the code segments in the GDT 17fa1e03eaSRoland McGrath * are all zero-based. That is largely true: the 18fa1e03eaSRoland McGrath * TLS segments are used for data, and the PNPBIOS 19fa1e03eaSRoland McGrath * and APM bios ones we just ignore here. 20fa1e03eaSRoland McGrath */ 213f80c1adSRoland McGrath if ((seg & SEGMENT_TI_MASK) == SEGMENT_LDT) { 22fa1e03eaSRoland McGrath u32 *desc; 23fa1e03eaSRoland McGrath unsigned long base; 24fa1e03eaSRoland McGrath 25fa1e03eaSRoland McGrath seg &= ~7UL; 26fa1e03eaSRoland McGrath 27fa1e03eaSRoland McGrath mutex_lock(&child->mm->context.lock); 28fa1e03eaSRoland McGrath if (unlikely((seg >> 3) >= child->mm->context.size)) 29fa1e03eaSRoland McGrath addr = -1L; /* bogus selector, access would fault */ 30fa1e03eaSRoland McGrath else { 31fa1e03eaSRoland McGrath desc = child->mm->context.ldt + seg; 32fa1e03eaSRoland McGrath base = ((desc[0] >> 16) | 33fa1e03eaSRoland McGrath ((desc[1] & 0xff) << 16) | 34fa1e03eaSRoland McGrath (desc[1] & 0xff000000)); 35fa1e03eaSRoland McGrath 36fa1e03eaSRoland McGrath /* 16-bit code segment? */ 37fa1e03eaSRoland McGrath if (!((desc[1] >> 22) & 1)) 38fa1e03eaSRoland McGrath addr &= 0xffff; 39fa1e03eaSRoland McGrath addr += base; 40fa1e03eaSRoland McGrath } 41fa1e03eaSRoland McGrath mutex_unlock(&child->mm->context.lock); 42fa1e03eaSRoland McGrath } 43fa1e03eaSRoland McGrath 44fa1e03eaSRoland McGrath return addr; 45fa1e03eaSRoland McGrath } 46fa1e03eaSRoland McGrath 47fa1e03eaSRoland McGrath static int is_setting_trap_flag(struct task_struct *child, struct pt_regs *regs) 48fa1e03eaSRoland McGrath { 49fa1e03eaSRoland McGrath int i, copied; 50fa1e03eaSRoland McGrath unsigned char opcode[15]; 51fa1e03eaSRoland McGrath unsigned long addr = convert_rip_to_linear(child, regs); 52fa1e03eaSRoland McGrath 53fa1e03eaSRoland McGrath copied = access_process_vm(child, addr, opcode, sizeof(opcode), 0); 54fa1e03eaSRoland McGrath for (i = 0; i < copied; i++) { 55fa1e03eaSRoland McGrath switch (opcode[i]) { 56fa1e03eaSRoland McGrath /* popf and iret */ 57fa1e03eaSRoland McGrath case 0x9d: case 0xcf: 58fa1e03eaSRoland McGrath return 1; 59fa1e03eaSRoland McGrath 60fa1e03eaSRoland McGrath /* CHECKME: 64 65 */ 61fa1e03eaSRoland McGrath 62fa1e03eaSRoland McGrath /* opcode and address size prefixes */ 63fa1e03eaSRoland McGrath case 0x66: case 0x67: 64fa1e03eaSRoland McGrath continue; 65fa1e03eaSRoland McGrath /* irrelevant prefixes (segment overrides and repeats) */ 66fa1e03eaSRoland McGrath case 0x26: case 0x2e: 67fa1e03eaSRoland McGrath case 0x36: case 0x3e: 68fa1e03eaSRoland McGrath case 0x64: case 0x65: 69*5f76cb1fSRoland McGrath case 0xf0: case 0xf2: case 0xf3: 70fa1e03eaSRoland McGrath continue; 71fa1e03eaSRoland McGrath 72fa1e03eaSRoland McGrath case 0x40 ... 0x4f: 73fa1e03eaSRoland McGrath if (regs->cs != __USER_CS) 74fa1e03eaSRoland McGrath /* 32-bit mode: register increment */ 75fa1e03eaSRoland McGrath return 0; 76fa1e03eaSRoland McGrath /* 64-bit mode: REX prefix */ 77fa1e03eaSRoland McGrath continue; 78fa1e03eaSRoland McGrath 79fa1e03eaSRoland McGrath /* CHECKME: f2, f3 */ 80fa1e03eaSRoland McGrath 81fa1e03eaSRoland McGrath /* 82fa1e03eaSRoland McGrath * pushf: NOTE! We should probably not let 83fa1e03eaSRoland McGrath * the user see the TF bit being set. But 84fa1e03eaSRoland McGrath * it's more pain than it's worth to avoid 85fa1e03eaSRoland McGrath * it, and a debugger could emulate this 86fa1e03eaSRoland McGrath * all in user space if it _really_ cares. 87fa1e03eaSRoland McGrath */ 88fa1e03eaSRoland McGrath case 0x9c: 89fa1e03eaSRoland McGrath default: 90fa1e03eaSRoland McGrath return 0; 91fa1e03eaSRoland McGrath } 92fa1e03eaSRoland McGrath } 93fa1e03eaSRoland McGrath return 0; 94fa1e03eaSRoland McGrath } 95fa1e03eaSRoland McGrath 96fa1e03eaSRoland McGrath void user_enable_single_step(struct task_struct *child) 97fa1e03eaSRoland McGrath { 98fa1e03eaSRoland McGrath struct pt_regs *regs = task_pt_regs(child); 99fa1e03eaSRoland McGrath 100fa1e03eaSRoland McGrath /* 101fa1e03eaSRoland McGrath * Always set TIF_SINGLESTEP - this guarantees that 102fa1e03eaSRoland McGrath * we single-step system calls etc.. This will also 103fa1e03eaSRoland McGrath * cause us to set TF when returning to user mode. 104fa1e03eaSRoland McGrath */ 105fa1e03eaSRoland McGrath set_tsk_thread_flag(child, TIF_SINGLESTEP); 106fa1e03eaSRoland McGrath 107fa1e03eaSRoland McGrath /* 108fa1e03eaSRoland McGrath * If TF was already set, don't do anything else 109fa1e03eaSRoland McGrath */ 110fa1e03eaSRoland McGrath if (regs->eflags & X86_EFLAGS_TF) 111fa1e03eaSRoland McGrath return; 112fa1e03eaSRoland McGrath 113fa1e03eaSRoland McGrath /* Set TF on the kernel stack.. */ 114fa1e03eaSRoland McGrath regs->eflags |= X86_EFLAGS_TF; 115fa1e03eaSRoland McGrath 116fa1e03eaSRoland McGrath /* 117fa1e03eaSRoland McGrath * ..but if TF is changed by the instruction we will trace, 118fa1e03eaSRoland McGrath * don't mark it as being "us" that set it, so that we 119fa1e03eaSRoland McGrath * won't clear it by hand later. 120fa1e03eaSRoland McGrath */ 121fa1e03eaSRoland McGrath if (is_setting_trap_flag(child, regs)) 122fa1e03eaSRoland McGrath return; 123fa1e03eaSRoland McGrath 124fa1e03eaSRoland McGrath child->ptrace |= PT_DTRACE; 125fa1e03eaSRoland McGrath } 126fa1e03eaSRoland McGrath 127fa1e03eaSRoland McGrath void user_disable_single_step(struct task_struct *child) 128fa1e03eaSRoland McGrath { 129fa1e03eaSRoland McGrath /* Always clear TIF_SINGLESTEP... */ 130fa1e03eaSRoland McGrath clear_tsk_thread_flag(child, TIF_SINGLESTEP); 131fa1e03eaSRoland McGrath 132fa1e03eaSRoland McGrath /* But touch TF only if it was set by us.. */ 133fa1e03eaSRoland McGrath if (child->ptrace & PT_DTRACE) { 134fa1e03eaSRoland McGrath struct pt_regs *regs = task_pt_regs(child); 135fa1e03eaSRoland McGrath regs->eflags &= ~X86_EFLAGS_TF; 136fa1e03eaSRoland McGrath child->ptrace &= ~PT_DTRACE; 137fa1e03eaSRoland McGrath } 138fa1e03eaSRoland McGrath } 139