xref: /illumos-gate/usr/src/cmd/mdb/intel/mdb/mdb_amd64util.c (revision 21072fc3aa88bec97f3089899d4c2916ac8f0695)
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 /*
23  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 /*
27  * Copyright 2013 Nexenta Systems, Inc.  All rights reserved.
28  */
29 
30 #include <sys/types.h>
31 #include <sys/reg.h>
32 #include <sys/privregs.h>
33 #include <sys/stack.h>
34 #include <sys/frame.h>
35 
36 #include <mdb/mdb_target_impl.h>
37 #include <mdb/mdb_kreg_impl.h>
38 #include <mdb/mdb_debug.h>
39 #include <mdb/mdb_modapi.h>
40 #include <mdb/mdb_amd64util.h>
41 #include <mdb/mdb_ctf.h>
42 #include <mdb/mdb_err.h>
43 #include <mdb/mdb.h>
44 
45 #include <saveargs.h>
46 
47 /*
48  * This array is used by the getareg and putareg entry points, and also by our
49  * register variable discipline.
50  */
51 
52 const mdb_tgt_regdesc_t mdb_amd64_kregs[] = {
53 	{ "savfp", KREG_SAVFP, MDB_TGT_R_EXPORT },
54 	{ "savpc", KREG_SAVPC, MDB_TGT_R_EXPORT },
55 	{ "rdi", KREG_RDI, MDB_TGT_R_EXPORT },
56 	{ "rsi", KREG_RSI, MDB_TGT_R_EXPORT },
57 	{ "rdx", KREG_RDX, MDB_TGT_R_EXPORT },
58 	{ "rcx", KREG_RCX, MDB_TGT_R_EXPORT },
59 	{ "r8", KREG_R8, MDB_TGT_R_EXPORT },
60 	{ "r9", KREG_R9, MDB_TGT_R_EXPORT },
61 	{ "rax", KREG_RAX, MDB_TGT_R_EXPORT },
62 	{ "rbx", KREG_RBX, MDB_TGT_R_EXPORT },
63 	{ "rbp", KREG_RBP, MDB_TGT_R_EXPORT },
64 	{ "r10", KREG_R10, MDB_TGT_R_EXPORT },
65 	{ "r11", KREG_R11, MDB_TGT_R_EXPORT },
66 	{ "r12", KREG_R12, MDB_TGT_R_EXPORT },
67 	{ "r13", KREG_R13, MDB_TGT_R_EXPORT },
68 	{ "r14", KREG_R14, MDB_TGT_R_EXPORT },
69 	{ "r15", KREG_R15, MDB_TGT_R_EXPORT },
70 	{ "ds", KREG_DS, MDB_TGT_R_EXPORT },
71 	{ "es", KREG_ES, MDB_TGT_R_EXPORT },
72 	{ "fs", KREG_FS, MDB_TGT_R_EXPORT },
73 	{ "gs", KREG_GS, MDB_TGT_R_EXPORT },
74 	{ "trapno", KREG_TRAPNO, MDB_TGT_R_EXPORT | MDB_TGT_R_PRIV },
75 	{ "err", KREG_ERR, MDB_TGT_R_EXPORT | MDB_TGT_R_PRIV },
76 	{ "rip", KREG_RIP, MDB_TGT_R_EXPORT },
77 	{ "cs", KREG_CS, MDB_TGT_R_EXPORT },
78 	{ "rflags", KREG_RFLAGS, MDB_TGT_R_EXPORT },
79 	{ "rsp", KREG_RSP, MDB_TGT_R_EXPORT },
80 	{ "ss", KREG_SS, MDB_TGT_R_EXPORT },
81 	{ NULL, 0, 0 }
82 };
83 
84 void
85 mdb_amd64_printregs(const mdb_tgt_gregset_t *gregs)
86 {
87 	const kreg_t *kregs = &gregs->kregs[0];
88 	kreg_t rflags = kregs[KREG_RFLAGS];
89 
90 #define	GETREG2(x) ((uintptr_t)kregs[(x)]), ((uintptr_t)kregs[(x)])
91 
92 	mdb_printf("%%rax = 0x%0?p %15A %%r9  = 0x%0?p %A\n",
93 	    GETREG2(KREG_RAX), GETREG2(KREG_R9));
94 	mdb_printf("%%rbx = 0x%0?p %15A %%r10 = 0x%0?p %A\n",
95 	    GETREG2(KREG_RBX), GETREG2(KREG_R10));
96 	mdb_printf("%%rcx = 0x%0?p %15A %%r11 = 0x%0?p %A\n",
97 	    GETREG2(KREG_RCX), GETREG2(KREG_R11));
98 	mdb_printf("%%rdx = 0x%0?p %15A %%r12 = 0x%0?p %A\n",
99 	    GETREG2(KREG_RDX), GETREG2(KREG_R12));
100 	mdb_printf("%%rsi = 0x%0?p %15A %%r13 = 0x%0?p %A\n",
101 	    GETREG2(KREG_RSI), GETREG2(KREG_R13));
102 	mdb_printf("%%rdi = 0x%0?p %15A %%r14 = 0x%0?p %A\n",
103 	    GETREG2(KREG_RDI), GETREG2(KREG_R14));
104 	mdb_printf("%%r8  = 0x%0?p %15A %%r15 = 0x%0?p %A\n\n",
105 	    GETREG2(KREG_R8), GETREG2(KREG_R15));
106 
107 	mdb_printf("%%rip = 0x%0?p %A\n", GETREG2(KREG_RIP));
108 	mdb_printf("%%rbp = 0x%0?p\n", kregs[KREG_RBP]);
109 	mdb_printf("%%rsp = 0x%0?p\n", kregs[KREG_RSP]);
110 
111 	mdb_printf("%%rflags = 0x%08x\n", rflags);
112 
113 	mdb_printf("  id=%u vip=%u vif=%u ac=%u vm=%u rf=%u nt=%u iopl=0x%x\n",
114 	    (rflags & KREG_EFLAGS_ID_MASK) >> KREG_EFLAGS_ID_SHIFT,
115 	    (rflags & KREG_EFLAGS_VIP_MASK) >> KREG_EFLAGS_VIP_SHIFT,
116 	    (rflags & KREG_EFLAGS_VIF_MASK) >> KREG_EFLAGS_VIF_SHIFT,
117 	    (rflags & KREG_EFLAGS_AC_MASK) >> KREG_EFLAGS_AC_SHIFT,
118 	    (rflags & KREG_EFLAGS_VM_MASK) >> KREG_EFLAGS_VM_SHIFT,
119 	    (rflags & KREG_EFLAGS_RF_MASK) >> KREG_EFLAGS_RF_SHIFT,
120 	    (rflags & KREG_EFLAGS_NT_MASK) >> KREG_EFLAGS_NT_SHIFT,
121 	    (rflags & KREG_EFLAGS_IOPL_MASK) >> KREG_EFLAGS_IOPL_SHIFT);
122 
123 	mdb_printf("  status=<%s,%s,%s,%s,%s,%s,%s,%s,%s>\n\n",
124 	    (rflags & KREG_EFLAGS_OF_MASK) ? "OF" : "of",
125 	    (rflags & KREG_EFLAGS_DF_MASK) ? "DF" : "df",
126 	    (rflags & KREG_EFLAGS_IF_MASK) ? "IF" : "if",
127 	    (rflags & KREG_EFLAGS_TF_MASK) ? "TF" : "tf",
128 	    (rflags & KREG_EFLAGS_SF_MASK) ? "SF" : "sf",
129 	    (rflags & KREG_EFLAGS_ZF_MASK) ? "ZF" : "zf",
130 	    (rflags & KREG_EFLAGS_AF_MASK) ? "AF" : "af",
131 	    (rflags & KREG_EFLAGS_PF_MASK) ? "PF" : "pf",
132 	    (rflags & KREG_EFLAGS_CF_MASK) ? "CF" : "cf");
133 
134 	mdb_printf("%24s%%cs = 0x%04x\t%%ds = 0x%04x\t%%es = 0x%04x\n",
135 	    " ", kregs[KREG_CS], kregs[KREG_DS], kregs[KREG_ES]);
136 
137 	mdb_printf("%%trapno = 0x%x\t\t%%fs = 0x%04x\t%%gs = 0x%04x\n",
138 	    kregs[KREG_TRAPNO], (kregs[KREG_FS] & 0xffff),
139 	    (kregs[KREG_GS] & 0xffff));
140 	mdb_printf("   %%err = 0x%x\n", kregs[KREG_ERR]);
141 }
142 
143 /*
144  * We expect all proper Solaris core files to have STACK_ALIGN-aligned stacks.
145  * Hence the name.  However, if the core file resulted from a
146  * hypervisor-initiated panic, the hypervisor's frames may only be 64-bit
147  * aligned instead of 128.
148  */
149 static int
150 fp_is_aligned(uintptr_t fp, int xpv_panic)
151 {
152 	if (!xpv_panic && (fp & (STACK_ALIGN -1)))
153 		return (0);
154 	if ((fp & sizeof (uintptr_t) - 1))
155 		return (0);
156 	return (1);
157 }
158 
159 int
160 mdb_amd64_kvm_stack_iter(mdb_tgt_t *t, const mdb_tgt_gregset_t *gsp,
161     mdb_tgt_stack_f *func, void *arg)
162 {
163 	mdb_tgt_gregset_t gregs;
164 	kreg_t *kregs = &gregs.kregs[0];
165 	int got_pc = (gsp->kregs[KREG_RIP] != 0);
166 	uint_t argc, reg_argc;
167 	long fr_argv[32];
168 	int start_index; /* index to save_instr where to start comparison */
169 	int i;
170 
171 	struct {
172 		uintptr_t fr_savfp;
173 		uintptr_t fr_savpc;
174 	} fr;
175 
176 	uintptr_t fp = gsp->kregs[KREG_RBP];
177 	uintptr_t pc = gsp->kregs[KREG_RIP];
178 	uintptr_t lastfp;
179 
180 	ssize_t size;
181 	ssize_t insnsize;
182 	uint8_t ins[SAVEARGS_INSN_SEQ_LEN];
183 
184 	GElf_Sym s;
185 	mdb_syminfo_t sip;
186 	mdb_ctf_funcinfo_t mfp;
187 	int xpv_panic = 0;
188 #ifndef	_KMDB
189 	int xp;
190 
191 	if ((mdb_readsym(&xp, sizeof (xp), "xpv_panicking") != -1) && (xp > 0))
192 		xpv_panic = 1;
193 #endif
194 
195 	bcopy(gsp, &gregs, sizeof (gregs));
196 
197 	while (fp != 0) {
198 		int args_style = 0;
199 
200 		if (!fp_is_aligned(fp, xpv_panic))
201 			return (set_errno(EMDB_STKALIGN));
202 
203 		if (mdb_tgt_vread(t, &fr, sizeof (fr), fp) != sizeof (fr))
204 			return (-1);	/* errno has been set for us */
205 
206 		if ((mdb_tgt_lookup_by_addr(t, pc, MDB_TGT_SYM_FUZZY,
207 		    NULL, 0, &s, &sip) == 0) &&
208 		    (mdb_ctf_func_info(&s, &sip, &mfp) == 0)) {
209 			int return_type = mdb_ctf_type_kind(mfp.mtf_return);
210 			mdb_ctf_id_t args_types[5];
211 
212 			argc = mfp.mtf_argc;
213 
214 			/*
215 			 * If the function returns a structure or union
216 			 * greater than 16 bytes in size %rdi contains the
217 			 * address in which to store the return value rather
218 			 * than for an argument.
219 			 */
220 			if ((return_type == CTF_K_STRUCT ||
221 			    return_type == CTF_K_UNION) &&
222 			    mdb_ctf_type_size(mfp.mtf_return) > 16)
223 				start_index = 1;
224 			else
225 				start_index = 0;
226 
227 			/*
228 			 * If any of the first 5 arguments are a structure
229 			 * less than 16 bytes in size, it will be passed
230 			 * spread across two argument registers, and we will
231 			 * not cope.
232 			 */
233 			if (mdb_ctf_func_args(&mfp, 5, args_types) == CTF_ERR)
234 				argc = 0;
235 
236 			for (i = 0; i < MIN(5, argc); i++) {
237 				int t = mdb_ctf_type_kind(args_types[i]);
238 
239 				if (((t == CTF_K_STRUCT) ||
240 				    (t == CTF_K_UNION)) &&
241 				    mdb_ctf_type_size(args_types[i]) <= 16) {
242 					argc = 0;
243 					break;
244 				}
245 			}
246 		} else {
247 			argc = 0;
248 		}
249 
250 		/*
251 		 * The number of instructions to search for argument saving is
252 		 * limited such that only instructions prior to %pc are
253 		 * considered such that we never read arguments from a
254 		 * function where the saving code has not in fact yet
255 		 * executed.
256 		 */
257 		insnsize = MIN(MIN(s.st_size, SAVEARGS_INSN_SEQ_LEN),
258 		    pc - s.st_value);
259 
260 		if (mdb_tgt_vread(t, ins, insnsize, s.st_value) != insnsize)
261 			argc = 0;
262 
263 		if ((argc != 0) &&
264 		    ((args_style = saveargs_has_args(ins, insnsize, argc,
265 		    start_index)) != SAVEARGS_NO_ARGS)) {
266 			/* Up to 6 arguments are passed via registers */
267 			reg_argc = MIN((6 - start_index), mfp.mtf_argc);
268 			size = reg_argc * sizeof (long);
269 
270 			/*
271 			 * If Studio pushed a structure return address as an
272 			 * argument, we need to read one more argument than
273 			 * actually exists (the addr) to make everything line
274 			 * up.
275 			 */
276 			if (args_style == SAVEARGS_STRUCT_ARGS)
277 				size += sizeof (long);
278 
279 			if (mdb_tgt_vread(t, fr_argv, size, (fp - size))
280 			    != size)
281 				return (-1);	/* errno has been set for us */
282 
283 			/*
284 			 * Arrange the arguments in the right order for
285 			 * printing.
286 			 */
287 			for (i = 0; i < (reg_argc / 2); i++) {
288 				long t = fr_argv[i];
289 
290 				fr_argv[i] = fr_argv[reg_argc - i - 1];
291 				fr_argv[reg_argc - i - 1] = t;
292 			}
293 
294 			if (argc > reg_argc) {
295 				size = MIN((argc - reg_argc) * sizeof (long),
296 				    sizeof (fr_argv) -
297 				    (reg_argc * sizeof (long)));
298 
299 				if (mdb_tgt_vread(t, &fr_argv[reg_argc], size,
300 				    fp + sizeof (fr)) != size)
301 					return (-1); /* errno has been set */
302 			}
303 		} else {
304 			argc = 0;
305 		}
306 
307 		if (got_pc && func(arg, pc, argc, fr_argv, &gregs) != 0)
308 			break;
309 
310 		kregs[KREG_RSP] = kregs[KREG_RBP];
311 
312 		lastfp = fp;
313 		fp = fr.fr_savfp;
314 		/*
315 		 * The Xen hypervisor marks a stack frame as belonging to
316 		 * an exception by inverting the bits of the pointer to
317 		 * that frame.  We attempt to identify these frames by
318 		 * inverting the pointer and seeing if it is within 0xfff
319 		 * bytes of the last frame.
320 		 */
321 		if (xpv_panic)
322 			if ((fp != 0) && (fp < lastfp) &&
323 			    ((lastfp ^ ~fp) < 0xfff))
324 				fp = ~fp;
325 
326 		kregs[KREG_RBP] = fp;
327 		kregs[KREG_RIP] = pc = fr.fr_savpc;
328 
329 		got_pc = (pc != 0);
330 	}
331 
332 	return (0);
333 }
334 
335 /*
336  * Determine the return address for the current frame.  Typically this is the
337  * fr_savpc value from the current frame, but we also perform some special
338  * handling to see if we are stopped on one of the first two instructions of
339  * a typical function prologue, in which case %rbp will not be set up yet.
340  */
341 int
342 mdb_amd64_step_out(mdb_tgt_t *t, uintptr_t *p, kreg_t pc, kreg_t fp, kreg_t sp,
343     mdb_instr_t curinstr)
344 {
345 	struct frame fr;
346 	GElf_Sym s;
347 	char buf[1];
348 
349 	enum {
350 		M_PUSHQ_RBP	= 0x55,	/* pushq %rbp */
351 		M_REX_W		= 0x48, /* REX prefix with only W set */
352 		M_MOVL_RBP	= 0x8b	/* movq %rsp, %rbp with prefix */
353 	};
354 
355 	if (mdb_tgt_lookup_by_addr(t, pc, MDB_TGT_SYM_FUZZY,
356 	    buf, 0, &s, NULL) == 0) {
357 		if (pc == s.st_value && curinstr == M_PUSHQ_RBP)
358 			fp = sp - 8;
359 		else if (pc == s.st_value + 1 && curinstr == M_REX_W) {
360 			if (mdb_tgt_vread(t, &curinstr, sizeof (curinstr),
361 			    pc + 1) == sizeof (curinstr) && curinstr ==
362 			    M_MOVL_RBP)
363 				fp = sp;
364 		}
365 	}
366 
367 	if (mdb_tgt_vread(t, &fr, sizeof (fr), fp) == sizeof (fr)) {
368 		*p = fr.fr_savpc;
369 		return (0);
370 	}
371 
372 	return (-1); /* errno is set for us */
373 }
374 
375 /*ARGSUSED*/
376 int
377 mdb_amd64_next(mdb_tgt_t *t, uintptr_t *p, kreg_t pc, mdb_instr_t curinstr)
378 {
379 	mdb_tgt_addr_t npc;
380 	mdb_tgt_addr_t callpc;
381 
382 	enum {
383 		M_CALL_REL = 0xe8, /* call near with relative displacement */
384 		M_CALL_REG = 0xff, /* call near indirect or call far register */
385 
386 		M_REX_LO = 0x40,
387 		M_REX_HI = 0x4f
388 	};
389 
390 	/*
391 	 * If the opcode is a near call with relative displacement, assume the
392 	 * displacement is a rel32 from the next instruction.
393 	 */
394 	if (curinstr == M_CALL_REL) {
395 		*p = pc + sizeof (mdb_instr_t) + sizeof (uint32_t);
396 		return (0);
397 	}
398 
399 	/* Skip the rex prefix, if any */
400 	callpc = pc;
401 	while (curinstr >= M_REX_LO && curinstr <= M_REX_HI) {
402 		if (mdb_tgt_vread(t, &curinstr, sizeof (curinstr), ++callpc) !=
403 		    sizeof (curinstr))
404 			return (-1); /* errno is set for us */
405 	}
406 
407 	if (curinstr != M_CALL_REG) {
408 		/* It's not a call */
409 		return (set_errno(EAGAIN));
410 	}
411 
412 	if ((npc = mdb_dis_nextins(mdb.m_disasm, t, MDB_TGT_AS_VIRT, pc)) == pc)
413 		return (-1); /* errno is set for us */
414 
415 	*p = npc;
416 	return (0);
417 }
418 
419 /*ARGSUSED*/
420 int
421 mdb_amd64_kvm_frame(void *arglim, uintptr_t pc, uint_t argc, const long *argv,
422     const mdb_tgt_gregset_t *gregs)
423 {
424 	argc = MIN(argc, (uintptr_t)arglim);
425 	mdb_printf("%a(", pc);
426 
427 	if (argc != 0) {
428 		mdb_printf("%lr", *argv++);
429 		for (argc--; argc != 0; argc--)
430 			mdb_printf(", %lr", *argv++);
431 	}
432 
433 	mdb_printf(")\n");
434 	return (0);
435 }
436 
437 int
438 mdb_amd64_kvm_framev(void *arglim, uintptr_t pc, uint_t argc, const long *argv,
439     const mdb_tgt_gregset_t *gregs)
440 {
441 	/*
442 	 * Historically adb limited stack trace argument display to a fixed-
443 	 * size number of arguments since no symbolic debugging info existed.
444 	 * On amd64 we can detect the true number of saved arguments so only
445 	 * respect an arglim of zero; otherwise display the entire argv[].
446 	 */
447 	if (arglim == 0)
448 		argc = 0;
449 
450 	mdb_printf("%0?lr %a(", gregs->kregs[KREG_RBP], pc);
451 
452 	if (argc != 0) {
453 		mdb_printf("%lr", *argv++);
454 		for (argc--; argc != 0; argc--)
455 			mdb_printf(", %lr", *argv++);
456 	}
457 
458 	mdb_printf(")\n");
459 	return (0);
460 }
461