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