xref: /titanic_41/usr/src/cmd/mdb/intel/mdb/mdb_amd64util.c (revision 355b4669e025ff377602b6fc7caaf30dbc218371)
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 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/types.h>
30 #include <sys/reg.h>
31 #include <sys/privregs.h>
32 #include <sys/stack.h>
33 #include <sys/frame.h>
34 
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_amd64util.h>
40 #include <mdb/mdb_ctf.h>
41 #include <mdb/mdb_err.h>
42 #include <mdb/mdb.h>
43 
44 /*
45  * This array is used by the getareg and putareg entry points, and also by our
46  * register variable discipline.
47  */
48 
49 const mdb_tgt_regdesc_t mdb_amd64_kregs[] = {
50 	{ "savfp", KREG_SAVFP, MDB_TGT_R_EXPORT },
51 	{ "savpc", KREG_SAVPC, MDB_TGT_R_EXPORT },
52 	{ "rdi", KREG_RDI, MDB_TGT_R_EXPORT },
53 	{ "rsi", KREG_RSI, MDB_TGT_R_EXPORT },
54 	{ "rdx", KREG_RDX, MDB_TGT_R_EXPORT },
55 	{ "rcx", KREG_RCX, MDB_TGT_R_EXPORT },
56 	{ "r8", KREG_R8, MDB_TGT_R_EXPORT },
57 	{ "r9", KREG_R9, MDB_TGT_R_EXPORT },
58 	{ "rax", KREG_RAX, MDB_TGT_R_EXPORT },
59 	{ "rbx", KREG_RBX, MDB_TGT_R_EXPORT },
60 	{ "rbp", KREG_RBP, MDB_TGT_R_EXPORT },
61 	{ "r10", KREG_R10, MDB_TGT_R_EXPORT },
62 	{ "r11", KREG_R11, MDB_TGT_R_EXPORT },
63 	{ "r12", KREG_R12, MDB_TGT_R_EXPORT },
64 	{ "r13", KREG_R13, MDB_TGT_R_EXPORT },
65 	{ "r14", KREG_R14, MDB_TGT_R_EXPORT },
66 	{ "r15", KREG_R15, MDB_TGT_R_EXPORT },
67 	{ "fsbase", KREG_FSBASE, MDB_TGT_R_EXPORT | MDB_TGT_R_PRIV },
68 	{ "gsbase", KREG_GSBASE, MDB_TGT_R_EXPORT | MDB_TGT_R_PRIV },
69 	{ "kgsbase", KREG_KGSBASE, MDB_TGT_R_EXPORT | MDB_TGT_R_PRIV },
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\tfsbase = 0x%0?p\n",
138 	    kregs[KREG_TRAPNO], (kregs[KREG_FS] & 0xffff), kregs[KREG_FSBASE]);
139 	mdb_printf("   %%err = 0x%x\t\t%%gs = 0x%04x\tgsbase = 0x%0?p\n",
140 	    kregs[KREG_ERR], (kregs[KREG_GS] & 0xffff), kregs[KREG_GSBASE]);
141 }
142 
143 
144 
145 /*
146  * Sun Studio 10 patch compiler and gcc 3.4.3 Sun branch implemented a
147  * "-save_args" option on amd64.  When the option is specified, INTEGER
148  * type function arguments passed via registers will be saved on the stack
149  * immediately after %rbp, and will not be modified through out the life
150  * of the routine.
151  *
152  *				+--------+
153  *		%rbp	-->     |  %rbp  |
154  *				+--------+
155  *		-0x8(%rbp)	|  %rdi  |
156  *				+--------+
157  *		-0x10(%rbp)	|  %rsi  |
158  *				+--------+
159  *		-0x18(%rbp)	|  %rdx  |
160  *				+--------+
161  *		-0x20(%rbp)	|  %rcx  |
162  *				+--------+
163  *		-0x28(%rbp)	|  %r8   |
164  *				+--------+
165  *		-0x30(%rbp)	|  %r9   |
166  *				+--------+
167  *
168  *
169  * For example, for the following function,
170  *
171  * void
172  * foo(int a1, int a2, int a3, int a4, int a5, int a6, int a7)
173  * {
174  * ...
175  * }
176  *
177  * Disassembled code will look something like the following:
178  *
179  *     pushq	%rbp
180  *     movq	%rsp, %rbp
181  *     subq	$imm8, %rsp			**
182  *     movq	%rdi, -0x8(%rbp)
183  *     movq	%rsi, -0x10(%rbp)
184  *     movq	%rdx, -0x18(%rbp)
185  *     movq	%rcx, -0x20(%rbp)
186  *     movq	%r8, -0x28(%rbp)
187  *     movq	%r9, -0x30(%rbp)
188  *     ...
189  * or
190  *     pushq	%rbp
191  *     movq	%rsp, %rbp
192  *     subq	$imm8, %rsp			**
193  *     movq	%r9, -0x30(%rbp)
194  *     movq	%r8, -0x28(%rbp)
195  *     movq	%rcx, -0x20(%rbp)
196  *     movq	%rdx, -0x18(%rbp)
197  *     movq	%rsi, -0x10(%rbp)
198  *     movq	%rdi, -0x8(%rbp)
199  *     ...
200  *
201  * **: The space being reserved is in addition to what the current
202  *     function prolog already reserves.
203  *
204  * If there are odd number of arguments to a function, additional space is
205  * reserved on the stack to maintain 16-byte alignment.  For example,
206  *
207  *     argc == 0: no argument saving.
208  *     argc == 3: save 3, but space for 4 is reserved
209  *     argc == 7: save 6.
210  */
211 
212 /*
213  * The longest instruction sequence in bytes before all 6 arguments are
214  * saved on the stack.  This value depends on compiler implementation,
215  * therefore it should be examined periodically to guarantee accuracy.
216  */
217 #define	SEQ_LEN		80
218 
219 /*
220  * Size of the instruction sequence arrays.  It should correspond to
221  * the maximum number of arguments passed via registers.
222  */
223 #define	INSTR_ARRAY_SIZE	6
224 
225 #define	INSTR4(ins, off)	\
226 	(ins[(off)] + (ins[(off) + 1] << 8) + (ins[(off + 2)] << 16) + \
227 	(ins[(off) + 3] << 24))
228 
229 /*
230  * Sun Studio 10 patch implementation saves %rdi first;
231  * GCC 3.4.3 Sun branch implementation saves them in reverse order.
232  */
233 static const uint32_t save_instr[INSTR_ARRAY_SIZE] = {
234 	0xf87d8948,	/* movq %rdi, -0x8(%rbp) */
235 	0xf0758948,	/* movq %rsi, -0x10(%rbp) */
236 	0xe8558948,	/* movq %rdx, -0x18(%rbp) */
237 	0xe04d8948,	/* movq %rcx, -0x20(%rbp) */
238 	0xd845894c,	/* movq %r8, -0x28(%rbp) */
239 	0xd04d894c	/* movq %r9, -0x30(%rbp) */
240 };
241 
242 static const uint32_t save_fp_instr[] = {
243 	0xe5894855,	/* pushq %rbp; movq %rsp,%rbp, encoding 1 */
244 	0xec8b4855,	/* pushq %rbp; movq %rsp,%rbp, encoding 2 */
245 	0xe58948cc,	/* int $0x3; movq %rsp,%rbp, encoding 1 */
246 	0xec8b48cc,	/* int $0x3; movq %rsp,%rbp, encoding 2 */
247 	NULL
248 };
249 
250 /*
251  * Look for the above instruction sequences as indicators for register
252  * arguments being available on the stack.
253  */
254 static int
255 is_argsaved(mdb_tgt_t *t, uintptr_t fstart, uint64_t size, uint_t argc,
256     int start_index)
257 {
258 	uint8_t		ins[SEQ_LEN];
259 	int		i, j;
260 	uint32_t	n;
261 
262 	size = MIN(size, SEQ_LEN);
263 	argc = MIN((start_index + argc), INSTR_ARRAY_SIZE);
264 
265 	if (mdb_tgt_vread(t, ins, size, fstart) != size)
266 		return (0);
267 
268 	/*
269 	 * Make sure framepointer has been saved.
270 	 */
271 	n = INSTR4(ins, 0);
272 	for (i = 0; save_fp_instr[i] != NULL; i++) {
273 		if (n == save_fp_instr[i])
274 			break;
275 	}
276 
277 	if (save_fp_instr[i] == NULL)
278 		return (0);
279 
280 	/*
281 	 * Compare against Sun Studio implementation
282 	 */
283 	for (i = 8, j = start_index; i < size - 4; i++) {
284 		n = INSTR4(ins, i);
285 
286 		if (n == save_instr[j]) {
287 			i += 3;
288 			if (++j >= argc)
289 				return (1);
290 		}
291 	}
292 
293 	/*
294 	 * Compare against GCC implementation
295 	 */
296 	for (i = 8, j = argc - 1; i < size - 4; i++) {
297 		n = INSTR4(ins, i);
298 
299 		if (n == save_instr[j]) {
300 			i += 3;
301 			if (--j < start_index)
302 				return (1);
303 		}
304 	}
305 
306 	return (0);
307 }
308 
309 int
310 mdb_amd64_kvm_stack_iter(mdb_tgt_t *t, const mdb_tgt_gregset_t *gsp,
311     mdb_tgt_stack_f *func, void *arg)
312 {
313 	mdb_tgt_gregset_t gregs;
314 	kreg_t *kregs = &gregs.kregs[0];
315 	int got_pc = (gsp->kregs[KREG_RIP] != 0);
316 	uint_t argc, reg_argc;
317 	long fr_argv[32];
318 	int start_index; /* index to save_instr where to start comparison */
319 	int i;
320 
321 	struct {
322 		uintptr_t fr_savfp;
323 		uintptr_t fr_savpc;
324 	} fr;
325 
326 	uintptr_t fp = gsp->kregs[KREG_RBP];
327 	uintptr_t pc = gsp->kregs[KREG_RIP];
328 	uintptr_t curpc;
329 
330 	ssize_t size;
331 
332 	GElf_Sym s;
333 	mdb_syminfo_t sip;
334 	mdb_ctf_funcinfo_t mfp;
335 
336 	bcopy(gsp, &gregs, sizeof (gregs));
337 
338 	while (fp != 0) {
339 
340 		curpc = pc;
341 
342 		if (fp & (STACK_ALIGN - 1))
343 			return (set_errno(EMDB_STKALIGN));
344 
345 		if (mdb_tgt_vread(t, &fr, sizeof (fr), fp) != sizeof (fr))
346 			return (-1);	/* errno has been set for us */
347 
348 		if ((mdb_tgt_lookup_by_addr(t, pc, MDB_TGT_SYM_FUZZY,
349 		    NULL, 0, &s, &sip) == 0) &&
350 		    (mdb_ctf_func_info(&s, &sip, &mfp) == 0)) {
351 			int return_type = mdb_ctf_type_kind(mfp.mtf_return);
352 			argc = mfp.mtf_argc;
353 			/*
354 			 * If the function returns a structure or union,
355 			 * %rdi contains the address in which to store the
356 			 * return value rather than for an argument.
357 			 */
358 			if (return_type == CTF_K_STRUCT ||
359 			    return_type == CTF_K_UNION)
360 				start_index = 1;
361 			else
362 				start_index = 0;
363 		} else {
364 			argc = 0;
365 		}
366 
367 		if (argc != 0 && is_argsaved(t, s.st_value, s.st_size,
368 		    argc, start_index)) {
369 
370 			/* Upto to 6 arguments are passed via registers */
371 			reg_argc = MIN(6, mfp.mtf_argc);
372 			size = reg_argc * sizeof (long);
373 
374 			if (mdb_tgt_vread(t, fr_argv, size, (fp - size))
375 			    != size)
376 				return (-1);	/* errno has been set for us */
377 
378 			/*
379 			 * Arrange the arguments in the right order for
380 			 * printing.
381 			 */
382 			for (i = 0; i < (reg_argc >> 1); i++) {
383 				long t = fr_argv[i];
384 
385 				fr_argv[i] = fr_argv[reg_argc - i - 1];
386 				fr_argv[reg_argc - i - 1] = t;
387 			}
388 
389 			if (argc > 6) {
390 				size = (argc - 6) * sizeof (long);
391 				if (mdb_tgt_vread(t, &fr_argv[6], size,
392 				    fp + sizeof (fr)) != size)
393 					return (-1); /* errno has been set */
394 			}
395 		} else
396 			argc = 0;
397 
398 		if (got_pc && func(arg, pc, argc, fr_argv, &gregs) != 0)
399 			break;
400 
401 		kregs[KREG_RSP] = kregs[KREG_RBP];
402 
403 		kregs[KREG_RBP] = fp = fr.fr_savfp;
404 		kregs[KREG_RIP] = pc = fr.fr_savpc;
405 
406 		if (curpc == pc)
407 			break;
408 
409 		got_pc = (pc != 0);
410 	}
411 
412 	return (0);
413 }
414 
415 /*
416  * Determine the return address for the current frame.  Typically this is the
417  * fr_savpc value from the current frame, but we also perform some special
418  * handling to see if we are stopped on one of the first two instructions of
419  * a typical function prologue, in which case %rbp will not be set up yet.
420  */
421 int
422 mdb_amd64_step_out(mdb_tgt_t *t, uintptr_t *p, kreg_t pc, kreg_t fp, kreg_t sp,
423     mdb_instr_t curinstr)
424 {
425 	struct frame fr;
426 	GElf_Sym s;
427 	char buf[1];
428 
429 	enum {
430 		M_PUSHQ_RBP	= 0x55,	/* pushq %rbp */
431 		M_REX_W		= 0x48, /* REX prefix with only W set */
432 		M_MOVL_RBP	= 0x8b	/* movq %rsp, %rbp with prefix */
433 	};
434 
435 	if (mdb_tgt_lookup_by_addr(t, pc, MDB_TGT_SYM_FUZZY,
436 	    buf, 0, &s, NULL) == 0) {
437 		if (pc == s.st_value && curinstr == M_PUSHQ_RBP)
438 			fp = sp - 8;
439 		else if (pc == s.st_value + 1 && curinstr == M_REX_W) {
440 			if (mdb_tgt_vread(t, &curinstr, sizeof (curinstr),
441 			    pc + 1) == sizeof (curinstr) && curinstr ==
442 			    M_MOVL_RBP)
443 				fp = sp;
444 		}
445 	}
446 
447 	if (mdb_tgt_vread(t, &fr, sizeof (fr), fp) == sizeof (fr)) {
448 		*p = fr.fr_savpc;
449 		return (0);
450 	}
451 
452 	return (-1); /* errno is set for us */
453 }
454 
455 /*ARGSUSED*/
456 int
457 mdb_amd64_next(mdb_tgt_t *t, uintptr_t *p, kreg_t pc, mdb_instr_t curinstr)
458 {
459 	mdb_tgt_addr_t npc;
460 	mdb_tgt_addr_t callpc;
461 
462 	enum {
463 		M_CALL_REL = 0xe8, /* call near with relative displacement */
464 		M_CALL_REG = 0xff, /* call near indirect or call far register */
465 
466 		M_REX_LO = 0x40,
467 		M_REX_HI = 0x4f
468 	};
469 
470 	/*
471 	 * If the opcode is a near call with relative displacement, assume the
472 	 * displacement is a rel32 from the next instruction.
473 	 */
474 	if (curinstr == M_CALL_REL) {
475 		*p = pc + sizeof (mdb_instr_t) + sizeof (uint32_t);
476 		return (0);
477 	}
478 
479 	/* Skip the rex prefix, if any */
480 	callpc = pc;
481 	while (curinstr >= M_REX_LO && curinstr <= M_REX_HI) {
482 		if (mdb_tgt_vread(t, &curinstr, sizeof (curinstr), ++callpc) !=
483 		    sizeof (curinstr))
484 			return (-1); /* errno is set for us */
485 	}
486 
487 	if (curinstr != M_CALL_REG) {
488 		/* It's not a call */
489 		return (set_errno(EAGAIN));
490 	}
491 
492 	if ((npc = mdb_dis_nextins(mdb.m_disasm, t, MDB_TGT_AS_VIRT, pc)) == pc)
493 		return (-1); /* errno is set for us */
494 
495 	*p = npc;
496 	return (0);
497 }
498 
499 /*ARGSUSED*/
500 int
501 mdb_amd64_kvm_frame(void *arglim, uintptr_t pc, uint_t argc, const long *argv,
502     const mdb_tgt_gregset_t *gregs)
503 {
504 	argc = MIN(argc, (uintptr_t)arglim);
505 	mdb_printf("%a(", pc);
506 
507 	if (argc != 0) {
508 		mdb_printf("%lr", *argv++);
509 		for (argc--; argc != 0; argc--)
510 			mdb_printf(", %lr", *argv++);
511 	}
512 
513 	mdb_printf(")\n");
514 	return (0);
515 }
516 
517 int
518 mdb_amd64_kvm_framev(void *arglim, uintptr_t pc, uint_t argc, const long *argv,
519     const mdb_tgt_gregset_t *gregs)
520 {
521 	/*
522 	 * Historically adb limited stack trace argument display to a fixed-
523 	 * size number of arguments since no symbolic debugging info existed.
524 	 * On amd64 we can detect the true number of saved arguments so only
525 	 * respect an arglim of zero; otherwise display the entire argv[].
526 	 */
527 	if (arglim == 0)
528 		argc = 0;
529 
530 	mdb_printf("%0?lr %a(", gregs->kregs[KREG_RBP], pc);
531 
532 	if (argc != 0) {
533 		mdb_printf("%lr", *argv++);
534 		for (argc--; argc != 0; argc--)
535 			mdb_printf(", %lr", *argv++);
536 	}
537 
538 	mdb_printf(")\n");
539 	return (0);
540 }
541