1 /* 2 * x86 single-step support code, common to 32-bit and 64-bit. 3 */ 4 #include <linux/sched.h> 5 #include <linux/sched/task_stack.h> 6 #include <linux/mm.h> 7 #include <linux/ptrace.h> 8 #include <asm/desc.h> 9 #include <asm/mmu_context.h> 10 11 unsigned long convert_ip_to_linear(struct task_struct *child, struct pt_regs *regs) 12 { 13 unsigned long addr, seg; 14 15 addr = regs->ip; 16 seg = regs->cs; 17 if (v8086_mode(regs)) { 18 addr = (addr & 0xffff) + (seg << 4); 19 return addr; 20 } 21 22 #ifdef CONFIG_MODIFY_LDT_SYSCALL 23 /* 24 * We'll assume that the code segments in the GDT 25 * are all zero-based. That is largely true: the 26 * TLS segments are used for data, and the PNPBIOS 27 * and APM bios ones we just ignore here. 28 */ 29 if ((seg & SEGMENT_TI_MASK) == SEGMENT_LDT) { 30 struct desc_struct *desc; 31 unsigned long base; 32 33 seg >>= 3; 34 35 mutex_lock(&child->mm->context.lock); 36 if (unlikely(!child->mm->context.ldt || 37 seg >= child->mm->context.ldt->nr_entries)) 38 addr = -1L; /* bogus selector, access would fault */ 39 else { 40 desc = &child->mm->context.ldt->entries[seg]; 41 base = get_desc_base(desc); 42 43 /* 16-bit code segment? */ 44 if (!desc->d) 45 addr &= 0xffff; 46 addr += base; 47 } 48 mutex_unlock(&child->mm->context.lock); 49 } 50 #endif 51 52 return addr; 53 } 54 55 static int is_setting_trap_flag(struct task_struct *child, struct pt_regs *regs) 56 { 57 int i, copied; 58 unsigned char opcode[15]; 59 unsigned long addr = convert_ip_to_linear(child, regs); 60 61 copied = access_process_vm(child, addr, opcode, sizeof(opcode), 62 FOLL_FORCE); 63 for (i = 0; i < copied; i++) { 64 switch (opcode[i]) { 65 /* popf and iret */ 66 case 0x9d: case 0xcf: 67 return 1; 68 69 /* CHECKME: 64 65 */ 70 71 /* opcode and address size prefixes */ 72 case 0x66: case 0x67: 73 continue; 74 /* irrelevant prefixes (segment overrides and repeats) */ 75 case 0x26: case 0x2e: 76 case 0x36: case 0x3e: 77 case 0x64: case 0x65: 78 case 0xf0: case 0xf2: case 0xf3: 79 continue; 80 81 #ifdef CONFIG_X86_64 82 case 0x40 ... 0x4f: 83 if (!user_64bit_mode(regs)) 84 /* 32-bit mode: register increment */ 85 return 0; 86 /* 64-bit mode: REX prefix */ 87 continue; 88 #endif 89 90 /* CHECKME: f2, f3 */ 91 92 /* 93 * pushf: NOTE! We should probably not let 94 * the user see the TF bit being set. But 95 * it's more pain than it's worth to avoid 96 * it, and a debugger could emulate this 97 * all in user space if it _really_ cares. 98 */ 99 case 0x9c: 100 default: 101 return 0; 102 } 103 } 104 return 0; 105 } 106 107 /* 108 * Enable single-stepping. Return nonzero if user mode is not using TF itself. 109 */ 110 static int enable_single_step(struct task_struct *child) 111 { 112 struct pt_regs *regs = task_pt_regs(child); 113 unsigned long oflags; 114 115 /* 116 * If we stepped into a sysenter/syscall insn, it trapped in 117 * kernel mode; do_debug() cleared TF and set TIF_SINGLESTEP. 118 * If user-mode had set TF itself, then it's still clear from 119 * do_debug() and we need to set it again to restore the user 120 * state so we don't wrongly set TIF_FORCED_TF below. 121 * If enable_single_step() was used last and that is what 122 * set TIF_SINGLESTEP, then both TF and TIF_FORCED_TF are 123 * already set and our bookkeeping is fine. 124 */ 125 if (unlikely(test_tsk_thread_flag(child, TIF_SINGLESTEP))) 126 regs->flags |= X86_EFLAGS_TF; 127 128 /* 129 * Always set TIF_SINGLESTEP - this guarantees that 130 * we single-step system calls etc.. This will also 131 * cause us to set TF when returning to user mode. 132 */ 133 set_tsk_thread_flag(child, TIF_SINGLESTEP); 134 135 oflags = regs->flags; 136 137 /* Set TF on the kernel stack.. */ 138 regs->flags |= X86_EFLAGS_TF; 139 140 /* 141 * ..but if TF is changed by the instruction we will trace, 142 * don't mark it as being "us" that set it, so that we 143 * won't clear it by hand later. 144 * 145 * Note that if we don't actually execute the popf because 146 * of a signal arriving right now or suchlike, we will lose 147 * track of the fact that it really was "us" that set it. 148 */ 149 if (is_setting_trap_flag(child, regs)) { 150 clear_tsk_thread_flag(child, TIF_FORCED_TF); 151 return 0; 152 } 153 154 /* 155 * If TF was already set, check whether it was us who set it. 156 * If not, we should never attempt a block step. 157 */ 158 if (oflags & X86_EFLAGS_TF) 159 return test_tsk_thread_flag(child, TIF_FORCED_TF); 160 161 set_tsk_thread_flag(child, TIF_FORCED_TF); 162 163 return 1; 164 } 165 166 void set_task_blockstep(struct task_struct *task, bool on) 167 { 168 unsigned long debugctl; 169 170 /* 171 * Ensure irq/preemption can't change debugctl in between. 172 * Note also that both TIF_BLOCKSTEP and debugctl should 173 * be changed atomically wrt preemption. 174 * 175 * NOTE: this means that set/clear TIF_BLOCKSTEP is only safe if 176 * task is current or it can't be running, otherwise we can race 177 * with __switch_to_xtra(). We rely on ptrace_freeze_traced() but 178 * PTRACE_KILL is not safe. 179 */ 180 local_irq_disable(); 181 debugctl = get_debugctlmsr(); 182 if (on) { 183 debugctl |= DEBUGCTLMSR_BTF; 184 set_tsk_thread_flag(task, TIF_BLOCKSTEP); 185 } else { 186 debugctl &= ~DEBUGCTLMSR_BTF; 187 clear_tsk_thread_flag(task, TIF_BLOCKSTEP); 188 } 189 if (task == current) 190 update_debugctlmsr(debugctl); 191 local_irq_enable(); 192 } 193 194 /* 195 * Enable single or block step. 196 */ 197 static void enable_step(struct task_struct *child, bool block) 198 { 199 /* 200 * Make sure block stepping (BTF) is not enabled unless it should be. 201 * Note that we don't try to worry about any is_setting_trap_flag() 202 * instructions after the first when using block stepping. 203 * So no one should try to use debugger block stepping in a program 204 * that uses user-mode single stepping itself. 205 */ 206 if (enable_single_step(child) && block) 207 set_task_blockstep(child, true); 208 else if (test_tsk_thread_flag(child, TIF_BLOCKSTEP)) 209 set_task_blockstep(child, false); 210 } 211 212 void user_enable_single_step(struct task_struct *child) 213 { 214 enable_step(child, 0); 215 } 216 217 void user_enable_block_step(struct task_struct *child) 218 { 219 enable_step(child, 1); 220 } 221 222 void user_disable_single_step(struct task_struct *child) 223 { 224 /* 225 * Make sure block stepping (BTF) is disabled. 226 */ 227 if (test_tsk_thread_flag(child, TIF_BLOCKSTEP)) 228 set_task_blockstep(child, false); 229 230 /* Always clear TIF_SINGLESTEP... */ 231 clear_tsk_thread_flag(child, TIF_SINGLESTEP); 232 233 /* But touch TF only if it was set by us.. */ 234 if (test_and_clear_tsk_thread_flag(child, TIF_FORCED_TF)) 235 task_pt_regs(child)->flags &= ~X86_EFLAGS_TF; 236 } 237