xref: /titanic_41/usr/src/cmd/mdb/intel/mdb/mdb_amd64util.c (revision bbfe764ef179c2f606b093b6ec6bb149cde0e31b)
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 #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 	{ "ds", KREG_DS, MDB_TGT_R_EXPORT },
68 	{ "es", KREG_ES, MDB_TGT_R_EXPORT },
69 	{ "fs", KREG_FS, MDB_TGT_R_EXPORT },
70 	{ "gs", KREG_GS, MDB_TGT_R_EXPORT },
71 	{ "trapno", KREG_TRAPNO, MDB_TGT_R_EXPORT | MDB_TGT_R_PRIV },
72 	{ "err", KREG_ERR, MDB_TGT_R_EXPORT | MDB_TGT_R_PRIV },
73 	{ "rip", KREG_RIP, MDB_TGT_R_EXPORT },
74 	{ "cs", KREG_CS, MDB_TGT_R_EXPORT },
75 	{ "rflags", KREG_RFLAGS, MDB_TGT_R_EXPORT },
76 	{ "rsp", KREG_RSP, MDB_TGT_R_EXPORT },
77 	{ "ss", KREG_SS, MDB_TGT_R_EXPORT },
78 	{ NULL, 0, 0 }
79 };
80 
81 void
82 mdb_amd64_printregs(const mdb_tgt_gregset_t *gregs)
83 {
84 	const kreg_t *kregs = &gregs->kregs[0];
85 	kreg_t rflags = kregs[KREG_RFLAGS];
86 
87 #define	GETREG2(x) ((uintptr_t)kregs[(x)]), ((uintptr_t)kregs[(x)])
88 
89 	mdb_printf("%%rax = 0x%0?p %15A %%r9  = 0x%0?p %A\n",
90 	    GETREG2(KREG_RAX), GETREG2(KREG_R9));
91 	mdb_printf("%%rbx = 0x%0?p %15A %%r10 = 0x%0?p %A\n",
92 	    GETREG2(KREG_RBX), GETREG2(KREG_R10));
93 	mdb_printf("%%rcx = 0x%0?p %15A %%r11 = 0x%0?p %A\n",
94 	    GETREG2(KREG_RCX), GETREG2(KREG_R11));
95 	mdb_printf("%%rdx = 0x%0?p %15A %%r12 = 0x%0?p %A\n",
96 	    GETREG2(KREG_RDX), GETREG2(KREG_R12));
97 	mdb_printf("%%rsi = 0x%0?p %15A %%r13 = 0x%0?p %A\n",
98 	    GETREG2(KREG_RSI), GETREG2(KREG_R13));
99 	mdb_printf("%%rdi = 0x%0?p %15A %%r14 = 0x%0?p %A\n",
100 	    GETREG2(KREG_RDI), GETREG2(KREG_R14));
101 	mdb_printf("%%r8  = 0x%0?p %15A %%r15 = 0x%0?p %A\n\n",
102 	    GETREG2(KREG_R8), GETREG2(KREG_R15));
103 
104 	mdb_printf("%%rip = 0x%0?p %A\n", GETREG2(KREG_RIP));
105 	mdb_printf("%%rbp = 0x%0?p\n", kregs[KREG_RBP]);
106 	mdb_printf("%%rsp = 0x%0?p\n", kregs[KREG_RSP]);
107 
108 	mdb_printf("%%rflags = 0x%08x\n", rflags);
109 
110 	mdb_printf("  id=%u vip=%u vif=%u ac=%u vm=%u rf=%u nt=%u iopl=0x%x\n",
111 	    (rflags & KREG_EFLAGS_ID_MASK) >> KREG_EFLAGS_ID_SHIFT,
112 	    (rflags & KREG_EFLAGS_VIP_MASK) >> KREG_EFLAGS_VIP_SHIFT,
113 	    (rflags & KREG_EFLAGS_VIF_MASK) >> KREG_EFLAGS_VIF_SHIFT,
114 	    (rflags & KREG_EFLAGS_AC_MASK) >> KREG_EFLAGS_AC_SHIFT,
115 	    (rflags & KREG_EFLAGS_VM_MASK) >> KREG_EFLAGS_VM_SHIFT,
116 	    (rflags & KREG_EFLAGS_RF_MASK) >> KREG_EFLAGS_RF_SHIFT,
117 	    (rflags & KREG_EFLAGS_NT_MASK) >> KREG_EFLAGS_NT_SHIFT,
118 	    (rflags & KREG_EFLAGS_IOPL_MASK) >> KREG_EFLAGS_IOPL_SHIFT);
119 
120 	mdb_printf("  status=<%s,%s,%s,%s,%s,%s,%s,%s,%s>\n\n",
121 	    (rflags & KREG_EFLAGS_OF_MASK) ? "OF" : "of",
122 	    (rflags & KREG_EFLAGS_DF_MASK) ? "DF" : "df",
123 	    (rflags & KREG_EFLAGS_IF_MASK) ? "IF" : "if",
124 	    (rflags & KREG_EFLAGS_TF_MASK) ? "TF" : "tf",
125 	    (rflags & KREG_EFLAGS_SF_MASK) ? "SF" : "sf",
126 	    (rflags & KREG_EFLAGS_ZF_MASK) ? "ZF" : "zf",
127 	    (rflags & KREG_EFLAGS_AF_MASK) ? "AF" : "af",
128 	    (rflags & KREG_EFLAGS_PF_MASK) ? "PF" : "pf",
129 	    (rflags & KREG_EFLAGS_CF_MASK) ? "CF" : "cf");
130 
131 	mdb_printf("%24s%%cs = 0x%04x\t%%ds = 0x%04x\t%%es = 0x%04x\n",
132 	    " ", kregs[KREG_CS], kregs[KREG_DS], kregs[KREG_ES]);
133 
134 	mdb_printf("%%trapno = 0x%x\t\t%%fs = 0x%04x\t%%gs = 0x%04x\n",
135 	    kregs[KREG_TRAPNO], (kregs[KREG_FS] & 0xffff),
136 	    (kregs[KREG_GS] & 0xffff));
137 	mdb_printf("   %%err = 0x%x\n", kregs[KREG_ERR]);
138 }
139 
140 
141 
142 /*
143  * Sun Studio 10 patch compiler and gcc 3.4.3 Sun branch implemented a
144  * "-save_args" option on amd64.  When the option is specified, INTEGER
145  * type function arguments passed via registers will be saved on the stack
146  * immediately after %rbp, and will not be modified through out the life
147  * of the routine.
148  *
149  *				+--------+
150  *		%rbp	-->     |  %rbp  |
151  *				+--------+
152  *		-0x8(%rbp)	|  %rdi  |
153  *				+--------+
154  *		-0x10(%rbp)	|  %rsi  |
155  *				+--------+
156  *		-0x18(%rbp)	|  %rdx  |
157  *				+--------+
158  *		-0x20(%rbp)	|  %rcx  |
159  *				+--------+
160  *		-0x28(%rbp)	|  %r8   |
161  *				+--------+
162  *		-0x30(%rbp)	|  %r9   |
163  *				+--------+
164  *
165  *
166  * For example, for the following function,
167  *
168  * void
169  * foo(int a1, int a2, int a3, int a4, int a5, int a6, int a7)
170  * {
171  * ...
172  * }
173  *
174  * Disassembled code will look something like the following:
175  *
176  *     pushq	%rbp
177  *     movq	%rsp, %rbp
178  *     subq	$imm8, %rsp			**
179  *     movq	%rdi, -0x8(%rbp)
180  *     movq	%rsi, -0x10(%rbp)
181  *     movq	%rdx, -0x18(%rbp)
182  *     movq	%rcx, -0x20(%rbp)
183  *     movq	%r8, -0x28(%rbp)
184  *     movq	%r9, -0x30(%rbp)
185  *     ...
186  * or
187  *     pushq	%rbp
188  *     movq	%rsp, %rbp
189  *     subq	$imm8, %rsp			**
190  *     movq	%r9, -0x30(%rbp)
191  *     movq	%r8, -0x28(%rbp)
192  *     movq	%rcx, -0x20(%rbp)
193  *     movq	%rdx, -0x18(%rbp)
194  *     movq	%rsi, -0x10(%rbp)
195  *     movq	%rdi, -0x8(%rbp)
196  *     ...
197  *
198  * **: The space being reserved is in addition to what the current
199  *     function prolog already reserves.
200  *
201  * If there are odd number of arguments to a function, additional space is
202  * reserved on the stack to maintain 16-byte alignment.  For example,
203  *
204  *     argc == 0: no argument saving.
205  *     argc == 3: save 3, but space for 4 is reserved
206  *     argc == 7: save 6.
207  */
208 
209 /*
210  * The longest instruction sequence in bytes before all 6 arguments are
211  * saved on the stack.  This value depends on compiler implementation,
212  * therefore it should be examined periodically to guarantee accuracy.
213  */
214 #define	SEQ_LEN		80
215 
216 /*
217  * Size of the instruction sequence arrays.  It should correspond to
218  * the maximum number of arguments passed via registers.
219  */
220 #define	INSTR_ARRAY_SIZE	6
221 
222 #define	INSTR4(ins, off)	\
223 	(ins[(off)] + (ins[(off) + 1] << 8) + (ins[(off + 2)] << 16) + \
224 	(ins[(off) + 3] << 24))
225 
226 /*
227  * Sun Studio 10 patch implementation saves %rdi first;
228  * GCC 3.4.3 Sun branch implementation saves them in reverse order.
229  */
230 static const uint32_t save_instr[INSTR_ARRAY_SIZE] = {
231 	0xf87d8948,	/* movq %rdi, -0x8(%rbp) */
232 	0xf0758948,	/* movq %rsi, -0x10(%rbp) */
233 	0xe8558948,	/* movq %rdx, -0x18(%rbp) */
234 	0xe04d8948,	/* movq %rcx, -0x20(%rbp) */
235 	0xd845894c,	/* movq %r8, -0x28(%rbp) */
236 	0xd04d894c	/* movq %r9, -0x30(%rbp) */
237 };
238 
239 static const uint32_t save_fp_instr[] = {
240 	0xe5894855,	/* pushq %rbp; movq %rsp,%rbp, encoding 1 */
241 	0xec8b4855,	/* pushq %rbp; movq %rsp,%rbp, encoding 2 */
242 	0xe58948cc,	/* int $0x3; movq %rsp,%rbp, encoding 1 */
243 	0xec8b48cc,	/* int $0x3; movq %rsp,%rbp, encoding 2 */
244 	NULL
245 };
246 
247 /*
248  * Look for the above instruction sequences as indicators for register
249  * arguments being available on the stack.
250  */
251 static int
252 is_argsaved(mdb_tgt_t *t, uintptr_t fstart, uint64_t size, uint_t argc,
253     int start_index)
254 {
255 	uint8_t		ins[SEQ_LEN];
256 	int		i, j;
257 	uint32_t	n;
258 
259 	size = MIN(size, SEQ_LEN);
260 	argc = MIN((start_index + argc), INSTR_ARRAY_SIZE);
261 
262 	if (mdb_tgt_vread(t, ins, size, fstart) != size)
263 		return (0);
264 
265 	/*
266 	 * Make sure framepointer has been saved.
267 	 */
268 	n = INSTR4(ins, 0);
269 	for (i = 0; save_fp_instr[i] != NULL; i++) {
270 		if (n == save_fp_instr[i])
271 			break;
272 	}
273 
274 	if (save_fp_instr[i] == NULL)
275 		return (0);
276 
277 	/*
278 	 * Compare against Sun Studio implementation
279 	 */
280 	for (i = 8, j = start_index; i < size - 4; i++) {
281 		n = INSTR4(ins, i);
282 
283 		if (n == save_instr[j]) {
284 			i += 3;
285 			if (++j >= argc)
286 				return (1);
287 		}
288 	}
289 
290 	/*
291 	 * Compare against GCC implementation
292 	 */
293 	for (i = 8, j = argc - 1; i < size - 4; i++) {
294 		n = INSTR4(ins, i);
295 
296 		if (n == save_instr[j]) {
297 			i += 3;
298 			if (--j < start_index)
299 				return (1);
300 		}
301 	}
302 
303 	return (0);
304 }
305 
306 int
307 mdb_amd64_kvm_stack_iter(mdb_tgt_t *t, const mdb_tgt_gregset_t *gsp,
308     mdb_tgt_stack_f *func, void *arg)
309 {
310 	mdb_tgt_gregset_t gregs;
311 	kreg_t *kregs = &gregs.kregs[0];
312 	int got_pc = (gsp->kregs[KREG_RIP] != 0);
313 	uint_t argc, reg_argc;
314 	long fr_argv[32];
315 	int start_index; /* index to save_instr where to start comparison */
316 	int i;
317 
318 	struct {
319 		uintptr_t fr_savfp;
320 		uintptr_t fr_savpc;
321 	} fr;
322 
323 	uintptr_t fp = gsp->kregs[KREG_RBP];
324 	uintptr_t pc = gsp->kregs[KREG_RIP];
325 	uintptr_t curpc;
326 
327 	ssize_t size;
328 
329 	GElf_Sym s;
330 	mdb_syminfo_t sip;
331 	mdb_ctf_funcinfo_t mfp;
332 
333 	bcopy(gsp, &gregs, sizeof (gregs));
334 
335 	while (fp != 0) {
336 
337 		curpc = pc;
338 
339 		if (fp & (STACK_ALIGN - 1))
340 			return (set_errno(EMDB_STKALIGN));
341 
342 		if (mdb_tgt_vread(t, &fr, sizeof (fr), fp) != sizeof (fr))
343 			return (-1);	/* errno has been set for us */
344 
345 		if ((mdb_tgt_lookup_by_addr(t, pc, MDB_TGT_SYM_FUZZY,
346 		    NULL, 0, &s, &sip) == 0) &&
347 		    (mdb_ctf_func_info(&s, &sip, &mfp) == 0)) {
348 			int return_type = mdb_ctf_type_kind(mfp.mtf_return);
349 			argc = mfp.mtf_argc;
350 			/*
351 			 * If the function returns a structure or union,
352 			 * %rdi contains the address in which to store the
353 			 * return value rather than for an argument.
354 			 */
355 			if (return_type == CTF_K_STRUCT ||
356 			    return_type == CTF_K_UNION)
357 				start_index = 1;
358 			else
359 				start_index = 0;
360 		} else {
361 			argc = 0;
362 		}
363 
364 		if (argc != 0 && is_argsaved(t, s.st_value, s.st_size,
365 		    argc, start_index)) {
366 
367 			/* Upto to 6 arguments are passed via registers */
368 			reg_argc = MIN(6, mfp.mtf_argc);
369 			size = reg_argc * sizeof (long);
370 
371 			if (mdb_tgt_vread(t, fr_argv, size, (fp - size))
372 			    != size)
373 				return (-1);	/* errno has been set for us */
374 
375 			/*
376 			 * Arrange the arguments in the right order for
377 			 * printing.
378 			 */
379 			for (i = 0; i < (reg_argc >> 1); i++) {
380 				long t = fr_argv[i];
381 
382 				fr_argv[i] = fr_argv[reg_argc - i - 1];
383 				fr_argv[reg_argc - i - 1] = t;
384 			}
385 
386 			if (argc > 6) {
387 				size = (argc - 6) * sizeof (long);
388 				if (mdb_tgt_vread(t, &fr_argv[6], size,
389 				    fp + sizeof (fr)) != size)
390 					return (-1); /* errno has been set */
391 			}
392 		} else
393 			argc = 0;
394 
395 		if (got_pc && func(arg, pc, argc, fr_argv, &gregs) != 0)
396 			break;
397 
398 		kregs[KREG_RSP] = kregs[KREG_RBP];
399 
400 		kregs[KREG_RBP] = fp = fr.fr_savfp;
401 		kregs[KREG_RIP] = pc = fr.fr_savpc;
402 
403 		if (curpc == pc)
404 			break;
405 
406 		got_pc = (pc != 0);
407 	}
408 
409 	return (0);
410 }
411 
412 /*
413  * Determine the return address for the current frame.  Typically this is the
414  * fr_savpc value from the current frame, but we also perform some special
415  * handling to see if we are stopped on one of the first two instructions of
416  * a typical function prologue, in which case %rbp will not be set up yet.
417  */
418 int
419 mdb_amd64_step_out(mdb_tgt_t *t, uintptr_t *p, kreg_t pc, kreg_t fp, kreg_t sp,
420     mdb_instr_t curinstr)
421 {
422 	struct frame fr;
423 	GElf_Sym s;
424 	char buf[1];
425 
426 	enum {
427 		M_PUSHQ_RBP	= 0x55,	/* pushq %rbp */
428 		M_REX_W		= 0x48, /* REX prefix with only W set */
429 		M_MOVL_RBP	= 0x8b	/* movq %rsp, %rbp with prefix */
430 	};
431 
432 	if (mdb_tgt_lookup_by_addr(t, pc, MDB_TGT_SYM_FUZZY,
433 	    buf, 0, &s, NULL) == 0) {
434 		if (pc == s.st_value && curinstr == M_PUSHQ_RBP)
435 			fp = sp - 8;
436 		else if (pc == s.st_value + 1 && curinstr == M_REX_W) {
437 			if (mdb_tgt_vread(t, &curinstr, sizeof (curinstr),
438 			    pc + 1) == sizeof (curinstr) && curinstr ==
439 			    M_MOVL_RBP)
440 				fp = sp;
441 		}
442 	}
443 
444 	if (mdb_tgt_vread(t, &fr, sizeof (fr), fp) == sizeof (fr)) {
445 		*p = fr.fr_savpc;
446 		return (0);
447 	}
448 
449 	return (-1); /* errno is set for us */
450 }
451 
452 /*ARGSUSED*/
453 int
454 mdb_amd64_next(mdb_tgt_t *t, uintptr_t *p, kreg_t pc, mdb_instr_t curinstr)
455 {
456 	mdb_tgt_addr_t npc;
457 	mdb_tgt_addr_t callpc;
458 
459 	enum {
460 		M_CALL_REL = 0xe8, /* call near with relative displacement */
461 		M_CALL_REG = 0xff, /* call near indirect or call far register */
462 
463 		M_REX_LO = 0x40,
464 		M_REX_HI = 0x4f
465 	};
466 
467 	/*
468 	 * If the opcode is a near call with relative displacement, assume the
469 	 * displacement is a rel32 from the next instruction.
470 	 */
471 	if (curinstr == M_CALL_REL) {
472 		*p = pc + sizeof (mdb_instr_t) + sizeof (uint32_t);
473 		return (0);
474 	}
475 
476 	/* Skip the rex prefix, if any */
477 	callpc = pc;
478 	while (curinstr >= M_REX_LO && curinstr <= M_REX_HI) {
479 		if (mdb_tgt_vread(t, &curinstr, sizeof (curinstr), ++callpc) !=
480 		    sizeof (curinstr))
481 			return (-1); /* errno is set for us */
482 	}
483 
484 	if (curinstr != M_CALL_REG) {
485 		/* It's not a call */
486 		return (set_errno(EAGAIN));
487 	}
488 
489 	if ((npc = mdb_dis_nextins(mdb.m_disasm, t, MDB_TGT_AS_VIRT, pc)) == pc)
490 		return (-1); /* errno is set for us */
491 
492 	*p = npc;
493 	return (0);
494 }
495 
496 /*ARGSUSED*/
497 int
498 mdb_amd64_kvm_frame(void *arglim, uintptr_t pc, uint_t argc, const long *argv,
499     const mdb_tgt_gregset_t *gregs)
500 {
501 	argc = MIN(argc, (uintptr_t)arglim);
502 	mdb_printf("%a(", pc);
503 
504 	if (argc != 0) {
505 		mdb_printf("%lr", *argv++);
506 		for (argc--; argc != 0; argc--)
507 			mdb_printf(", %lr", *argv++);
508 	}
509 
510 	mdb_printf(")\n");
511 	return (0);
512 }
513 
514 int
515 mdb_amd64_kvm_framev(void *arglim, uintptr_t pc, uint_t argc, const long *argv,
516     const mdb_tgt_gregset_t *gregs)
517 {
518 	/*
519 	 * Historically adb limited stack trace argument display to a fixed-
520 	 * size number of arguments since no symbolic debugging info existed.
521 	 * On amd64 we can detect the true number of saved arguments so only
522 	 * respect an arglim of zero; otherwise display the entire argv[].
523 	 */
524 	if (arglim == 0)
525 		argc = 0;
526 
527 	mdb_printf("%0?lr %a(", gregs->kregs[KREG_RBP], pc);
528 
529 	if (argc != 0) {
530 		mdb_printf("%lr", *argv++);
531 		for (argc--; argc != 0; argc--)
532 			mdb_printf(", %lr", *argv++);
533 	}
534 
535 	mdb_printf(")\n");
536 	return (0);
537 }
538