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