xref: /titanic_51/usr/src/cmd/mdb/intel/mdb/mdb_ia32util.c (revision 2983dda76a6d296fdb560c88114fe41caad1b84f)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/types.h>
29 #include <sys/reg.h>
30 #include <sys/privregs.h>
31 #include <sys/stack.h>
32 #include <sys/frame.h>
33 
34 #include <mdb/mdb_ia32util.h>
35 #include <mdb/mdb_target_impl.h>
36 #include <mdb/mdb_kreg_impl.h>
37 #include <mdb/mdb_debug.h>
38 #include <mdb/mdb_modapi.h>
39 #include <mdb/mdb_err.h>
40 #include <mdb/mdb.h>
41 
42 /*
43  * We also define an array of register names and their corresponding
44  * array indices.  This is used by the getareg and putareg entry points,
45  * and also by our register variable discipline.
46  */
47 const mdb_tgt_regdesc_t mdb_ia32_kregs[] = {
48 	{ "savfp", KREG_SAVFP, MDB_TGT_R_EXPORT },
49 	{ "savpc", KREG_SAVPC, MDB_TGT_R_EXPORT },
50 	{ "eax", KREG_EAX, MDB_TGT_R_EXPORT },
51 	{ "ebx", KREG_EBX, MDB_TGT_R_EXPORT },
52 	{ "ecx", KREG_ECX, MDB_TGT_R_EXPORT },
53 	{ "edx", KREG_EDX, MDB_TGT_R_EXPORT },
54 	{ "esi", KREG_ESI, MDB_TGT_R_EXPORT },
55 	{ "edi", KREG_EDI, MDB_TGT_R_EXPORT },
56 	{ "ebp", KREG_EBP, MDB_TGT_R_EXPORT },
57 	{ "esp", KREG_ESP, MDB_TGT_R_EXPORT },
58 	{ "cs", KREG_CS, MDB_TGT_R_EXPORT },
59 	{ "ds", KREG_DS, MDB_TGT_R_EXPORT },
60 	{ "ss", KREG_SS, MDB_TGT_R_EXPORT },
61 	{ "es", KREG_ES, MDB_TGT_R_EXPORT },
62 	{ "fs", KREG_FS, MDB_TGT_R_EXPORT },
63 	{ "gs", KREG_GS, MDB_TGT_R_EXPORT },
64 	{ "eflags", KREG_EFLAGS, MDB_TGT_R_EXPORT },
65 	{ "eip", KREG_EIP, MDB_TGT_R_EXPORT },
66 	{ "uesp", KREG_UESP, MDB_TGT_R_EXPORT | MDB_TGT_R_PRIV },
67 	{ "trapno", KREG_TRAPNO, MDB_TGT_R_EXPORT | MDB_TGT_R_PRIV },
68 	{ "err", KREG_ERR, MDB_TGT_R_EXPORT | MDB_TGT_R_PRIV },
69 	{ NULL, 0, 0 }
70 };
71 
72 void
73 mdb_ia32_printregs(const mdb_tgt_gregset_t *gregs)
74 {
75 	const kreg_t *kregs = &gregs->kregs[0];
76 	kreg_t eflags = kregs[KREG_EFLAGS];
77 
78 	mdb_printf("%%cs = 0x%04x\t\t%%eax = 0x%0?p %A\n",
79 	    kregs[KREG_CS], kregs[KREG_EAX], kregs[KREG_EAX]);
80 
81 	mdb_printf("%%ds = 0x%04x\t\t%%ebx = 0x%0?p %A\n",
82 	    kregs[KREG_DS], kregs[KREG_EBX], kregs[KREG_EBX]);
83 
84 	mdb_printf("%%ss = 0x%04x\t\t%%ecx = 0x%0?p %A\n",
85 	    kregs[KREG_SS], kregs[KREG_ECX], kregs[KREG_ECX]);
86 
87 	mdb_printf("%%es = 0x%04x\t\t%%edx = 0x%0?p %A\n",
88 	    kregs[KREG_ES], kregs[KREG_EDX], kregs[KREG_EDX]);
89 
90 	mdb_printf("%%fs = 0x%04x\t\t%%esi = 0x%0?p %A\n",
91 	    kregs[KREG_FS], kregs[KREG_ESI], kregs[KREG_ESI]);
92 
93 	mdb_printf("%%gs = 0x%04x\t\t%%edi = 0x%0?p %A\n\n",
94 	    kregs[KREG_GS], kregs[KREG_EDI], kregs[KREG_EDI]);
95 
96 	mdb_printf("%%eip = 0x%0?p %A\n", kregs[KREG_EIP], kregs[KREG_EIP]);
97 	mdb_printf("%%ebp = 0x%0?p\n", kregs[KREG_EBP]);
98 	mdb_printf("%%esp = 0x%0?p\n\n", kregs[KREG_ESP]);
99 	mdb_printf("%%eflags = 0x%08x\n", eflags);
100 
101 	mdb_printf("  id=%u vip=%u vif=%u ac=%u vm=%u rf=%u nt=%u iopl=0x%x\n",
102 	    (eflags & KREG_EFLAGS_ID_MASK) >> KREG_EFLAGS_ID_SHIFT,
103 	    (eflags & KREG_EFLAGS_VIP_MASK) >> KREG_EFLAGS_VIP_SHIFT,
104 	    (eflags & KREG_EFLAGS_VIF_MASK) >> KREG_EFLAGS_VIF_SHIFT,
105 	    (eflags & KREG_EFLAGS_AC_MASK) >> KREG_EFLAGS_AC_SHIFT,
106 	    (eflags & KREG_EFLAGS_VM_MASK) >> KREG_EFLAGS_VM_SHIFT,
107 	    (eflags & KREG_EFLAGS_RF_MASK) >> KREG_EFLAGS_RF_SHIFT,
108 	    (eflags & KREG_EFLAGS_NT_MASK) >> KREG_EFLAGS_NT_SHIFT,
109 	    (eflags & KREG_EFLAGS_IOPL_MASK) >> KREG_EFLAGS_IOPL_SHIFT);
110 
111 	mdb_printf("  status=<%s,%s,%s,%s,%s,%s,%s,%s,%s>\n\n",
112 	    (eflags & KREG_EFLAGS_OF_MASK) ? "OF" : "of",
113 	    (eflags & KREG_EFLAGS_DF_MASK) ? "DF" : "df",
114 	    (eflags & KREG_EFLAGS_IF_MASK) ? "IF" : "if",
115 	    (eflags & KREG_EFLAGS_TF_MASK) ? "TF" : "tf",
116 	    (eflags & KREG_EFLAGS_SF_MASK) ? "SF" : "sf",
117 	    (eflags & KREG_EFLAGS_ZF_MASK) ? "ZF" : "zf",
118 	    (eflags & KREG_EFLAGS_AF_MASK) ? "AF" : "af",
119 	    (eflags & KREG_EFLAGS_PF_MASK) ? "PF" : "pf",
120 	    (eflags & KREG_EFLAGS_CF_MASK) ? "CF" : "cf");
121 
122 #ifndef _KMDB
123 	mdb_printf("  %%uesp = 0x%0?x\n", kregs[KREG_UESP]);
124 #endif
125 	mdb_printf("%%trapno = 0x%x\n", kregs[KREG_TRAPNO]);
126 	mdb_printf("   %%err = 0x%x\n", kregs[KREG_ERR]);
127 }
128 
129 /*
130  * Given a return address (%eip), determine the likely number of arguments
131  * that were pushed on the stack prior to its execution.  We do this by
132  * expecting that a typical call sequence consists of pushing arguments on
133  * the stack, executing a call instruction, and then performing an add
134  * on %esp to restore it to the value prior to pushing the arguments for
135  * the call.  We attempt to detect such an add, and divide the addend
136  * by the size of a word to determine the number of pushed arguments.
137  */
138 static uint_t
139 kvm_argcount(mdb_tgt_t *t, uintptr_t eip, ssize_t size)
140 {
141 	uint8_t ins[6];
142 	ulong_t n;
143 
144 	enum {
145 		M_MODRM_ESP = 0xc4,	/* Mod/RM byte indicates %esp */
146 		M_ADD_IMM32 = 0x81,	/* ADD imm32 to r/m32 */
147 		M_ADD_IMM8  = 0x83	/* ADD imm8 to r/m32 */
148 	};
149 
150 	if (mdb_tgt_vread(t, ins, sizeof (ins), eip) != sizeof (ins))
151 		return (0);
152 
153 	if (ins[1] != M_MODRM_ESP)
154 		return (0);
155 
156 	switch (ins[0]) {
157 	case M_ADD_IMM32:
158 		n = ins[2] + (ins[3] << 8) + (ins[4] << 16) + (ins[5] << 24);
159 		break;
160 
161 	case M_ADD_IMM8:
162 		n = ins[2];
163 		break;
164 
165 	default:
166 		n = 0;
167 	}
168 
169 	return (MIN((ssize_t)n, size) / sizeof (long));
170 }
171 
172 int
173 mdb_ia32_kvm_stack_iter(mdb_tgt_t *t, const mdb_tgt_gregset_t *gsp,
174     mdb_tgt_stack_f *func, void *arg)
175 {
176 	mdb_tgt_gregset_t gregs;
177 	kreg_t *kregs = &gregs.kregs[0];
178 	int got_pc = (gsp->kregs[KREG_EIP] != 0);
179 
180 	struct {
181 		uintptr_t fr_savfp;
182 		uintptr_t fr_savpc;
183 		long fr_argv[32];
184 	} fr;
185 
186 	uintptr_t fp = gsp->kregs[KREG_EBP];
187 	uintptr_t pc = gsp->kregs[KREG_EIP];
188 	uintptr_t lastfp;
189 
190 	ssize_t size;
191 	uint_t argc;
192 	int detect_exception_frames = 0;
193 #ifndef	_KMDB
194 	int xp;
195 
196 	if ((mdb_readsym(&xp, sizeof (xp), "xpv_panicking") != -1) && (xp > 0))
197 		detect_exception_frames = 1;
198 #endif
199 
200 	bcopy(gsp, &gregs, sizeof (gregs));
201 
202 	while (fp != 0) {
203 
204 		if (fp & (STACK_ALIGN - 1))
205 			return (set_errno(EMDB_STKALIGN));
206 
207 		if ((size = mdb_tgt_vread(t, &fr, sizeof (fr), fp)) >=
208 		    (ssize_t)(2 * sizeof (uintptr_t))) {
209 			size -= (ssize_t)(2 * sizeof (uintptr_t));
210 			argc = kvm_argcount(t, fr.fr_savpc, size);
211 		} else {
212 			bzero(&fr, sizeof (fr));
213 			argc = 0;
214 		}
215 
216 		if (got_pc && func(arg, pc, argc, fr.fr_argv, &gregs) != 0)
217 			break;
218 
219 		kregs[KREG_ESP] = kregs[KREG_EBP];
220 
221 		lastfp = fp;
222 		fp = fr.fr_savfp;
223 		/*
224 		 * The Xen hypervisor marks a stack frame as belonging to
225 		 * an exception by inverting the bits of the pointer to
226 		 * that frame.  We attempt to identify these frames by
227 		 * inverting the pointer and seeing if it is within 0xfff
228 		 * bytes of the last frame.
229 		 */
230 		if (detect_exception_frames)
231 			if ((fp != 0) && (fp < lastfp) &&
232 			    ((lastfp ^ ~fp) < 0xfff))
233 				fp = ~fp;
234 
235 		kregs[KREG_EBP] = fp;
236 		kregs[KREG_EIP] = pc = fr.fr_savpc;
237 
238 		got_pc = (pc != 0);
239 	}
240 
241 	return (0);
242 }
243 
244 /*
245  * Determine the return address for the current frame.  Typically this is the
246  * fr_savpc value from the current frame, but we also perform some special
247  * handling to see if we are stopped on one of the first two instructions of a
248  * typical function prologue, in which case %ebp will not be set up yet.
249  */
250 int
251 mdb_ia32_step_out(mdb_tgt_t *t, uintptr_t *p, kreg_t pc, kreg_t fp, kreg_t sp,
252     mdb_instr_t curinstr)
253 {
254 	struct frame fr;
255 	GElf_Sym s;
256 	char buf[1];
257 
258 	enum {
259 		M_PUSHL_EBP	= 0x55, /* pushl %ebp */
260 		M_MOVL_EBP	= 0x8b  /* movl %esp, %ebp */
261 	};
262 
263 	if (mdb_tgt_lookup_by_addr(t, pc, MDB_TGT_SYM_FUZZY,
264 	    buf, 0, &s, NULL) == 0) {
265 		if (pc == s.st_value && curinstr == M_PUSHL_EBP)
266 			fp = sp - 4;
267 		else if (pc == s.st_value + 1 && curinstr == M_MOVL_EBP)
268 			fp = sp;
269 	}
270 
271 	if (mdb_tgt_vread(t, &fr, sizeof (fr), fp) == sizeof (fr)) {
272 		*p = fr.fr_savpc;
273 		return (0);
274 	}
275 
276 	return (-1); /* errno is set for us */
277 }
278 
279 /*
280  * Return the address of the next instruction following a call, or return -1
281  * and set errno to EAGAIN if the target should just single-step.  We perform
282  * a bit of disassembly on the current instruction in order to determine if it
283  * is a call and how many bytes should be skipped, depending on the exact form
284  * of the call instruction that is being used.
285  */
286 int
287 mdb_ia32_next(mdb_tgt_t *t, uintptr_t *p, kreg_t pc, mdb_instr_t curinstr)
288 {
289 	uint8_t m;
290 
291 	enum {
292 		M_CALL_REL = 0xe8, /* call near with relative displacement */
293 		M_CALL_REG = 0xff, /* call near indirect or call far register */
294 
295 		M_MODRM_MD = 0xc0, /* mask for Mod/RM byte Mod field */
296 		M_MODRM_OP = 0x38, /* mask for Mod/RM byte opcode field */
297 		M_MODRM_RM = 0x07, /* mask for Mod/RM byte R/M field */
298 
299 		M_MD_IND   = 0x00, /* Mod code for [REG] */
300 		M_MD_DSP8  = 0x40, /* Mod code for disp8[REG] */
301 		M_MD_DSP32 = 0x80, /* Mod code for disp32[REG] */
302 		M_MD_REG   = 0xc0, /* Mod code for REG */
303 
304 		M_OP_IND   = 0x10, /* Opcode for call near indirect */
305 		M_RM_DSP32 = 0x05  /* R/M code for disp32 */
306 	};
307 
308 	/*
309 	 * If the opcode is a near call with relative displacement, assume the
310 	 * displacement is a rel32 from the next instruction.
311 	 */
312 	if (curinstr == M_CALL_REL) {
313 		*p = pc + sizeof (mdb_instr_t) + sizeof (uint32_t);
314 		return (0);
315 	}
316 
317 	/*
318 	 * If the opcode is a call near indirect or call far register opcode,
319 	 * read the subsequent Mod/RM byte to perform additional decoding.
320 	 */
321 	if (curinstr == M_CALL_REG) {
322 		if (mdb_tgt_vread(t, &m, sizeof (m), pc + 1) != sizeof (m))
323 			return (-1); /* errno is set for us */
324 
325 		/*
326 		 * If the Mod/RM opcode extension indicates a near indirect
327 		 * call, then skip the appropriate number of additional
328 		 * bytes depending on the addressing form that is used.
329 		 */
330 		if ((m & M_MODRM_OP) == M_OP_IND) {
331 			switch (m & M_MODRM_MD) {
332 			case M_MD_DSP8:
333 				*p = pc + 3; /* skip pr_instr, m, disp8 */
334 				break;
335 			case M_MD_DSP32:
336 				*p = pc + 6; /* skip pr_instr, m, disp32 */
337 				break;
338 			case M_MD_IND:
339 				if ((m & M_MODRM_RM) == M_RM_DSP32) {
340 					*p = pc + 6;
341 					break; /* skip pr_instr, m, disp32 */
342 				}
343 				/* FALLTHRU */
344 			case M_MD_REG:
345 				*p = pc + 2; /* skip pr_instr, m */
346 				break;
347 			}
348 			return (0);
349 		}
350 	}
351 
352 	return (set_errno(EAGAIN));
353 }
354 
355 /*ARGSUSED*/
356 int
357 mdb_ia32_kvm_frame(void *arglim, uintptr_t pc, uint_t argc, const long *argv,
358     const mdb_tgt_gregset_t *gregs)
359 {
360 	argc = MIN(argc, (uint_t)arglim);
361 	mdb_printf("%a(", pc);
362 
363 	if (argc != 0) {
364 		mdb_printf("%lr", *argv++);
365 		for (argc--; argc != 0; argc--)
366 			mdb_printf(", %lr", *argv++);
367 	}
368 
369 	mdb_printf(")\n");
370 	return (0);
371 }
372 
373 int
374 mdb_ia32_kvm_framev(void *arglim, uintptr_t pc, uint_t argc, const long *argv,
375     const mdb_tgt_gregset_t *gregs)
376 {
377 	argc = MIN(argc, (uint_t)arglim);
378 	mdb_printf("%0?lr %a(", gregs->kregs[KREG_EBP], pc);
379 
380 	if (argc != 0) {
381 		mdb_printf("%lr", *argv++);
382 		for (argc--; argc != 0; argc--)
383 			mdb_printf(", %lr", *argv++);
384 	}
385 
386 	mdb_printf(")\n");
387 	return (0);
388 }
389