xref: /onnv-gate/usr/src/cmd/mdb/intel/mdb/mdb_amd64util.c (revision 3446:5903aece022d)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52244Sdmick  * Common Development and Distribution License (the "License").
62244Sdmick  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
212244Sdmick 
220Sstevel@tonic-gate /*
23*3446Smrj  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <sys/types.h>
300Sstevel@tonic-gate #include <sys/reg.h>
310Sstevel@tonic-gate #include <sys/privregs.h>
320Sstevel@tonic-gate #include <sys/stack.h>
330Sstevel@tonic-gate #include <sys/frame.h>
340Sstevel@tonic-gate 
350Sstevel@tonic-gate #include <mdb/mdb_target_impl.h>
360Sstevel@tonic-gate #include <mdb/mdb_kreg_impl.h>
370Sstevel@tonic-gate #include <mdb/mdb_debug.h>
380Sstevel@tonic-gate #include <mdb/mdb_modapi.h>
390Sstevel@tonic-gate #include <mdb/mdb_amd64util.h>
40170Ssherrym #include <mdb/mdb_ctf.h>
410Sstevel@tonic-gate #include <mdb/mdb_err.h>
420Sstevel@tonic-gate #include <mdb/mdb.h>
430Sstevel@tonic-gate 
440Sstevel@tonic-gate /*
450Sstevel@tonic-gate  * This array is used by the getareg and putareg entry points, and also by our
460Sstevel@tonic-gate  * register variable discipline.
470Sstevel@tonic-gate  */
480Sstevel@tonic-gate 
490Sstevel@tonic-gate const mdb_tgt_regdesc_t mdb_amd64_kregs[] = {
500Sstevel@tonic-gate 	{ "savfp", KREG_SAVFP, MDB_TGT_R_EXPORT },
510Sstevel@tonic-gate 	{ "savpc", KREG_SAVPC, MDB_TGT_R_EXPORT },
520Sstevel@tonic-gate 	{ "rdi", KREG_RDI, MDB_TGT_R_EXPORT },
530Sstevel@tonic-gate 	{ "rsi", KREG_RSI, MDB_TGT_R_EXPORT },
540Sstevel@tonic-gate 	{ "rdx", KREG_RDX, MDB_TGT_R_EXPORT },
550Sstevel@tonic-gate 	{ "rcx", KREG_RCX, MDB_TGT_R_EXPORT },
560Sstevel@tonic-gate 	{ "r8", KREG_R8, MDB_TGT_R_EXPORT },
570Sstevel@tonic-gate 	{ "r9", KREG_R9, MDB_TGT_R_EXPORT },
580Sstevel@tonic-gate 	{ "rax", KREG_RAX, MDB_TGT_R_EXPORT },
590Sstevel@tonic-gate 	{ "rbx", KREG_RBX, MDB_TGT_R_EXPORT },
600Sstevel@tonic-gate 	{ "rbp", KREG_RBP, MDB_TGT_R_EXPORT },
610Sstevel@tonic-gate 	{ "r10", KREG_R10, MDB_TGT_R_EXPORT },
620Sstevel@tonic-gate 	{ "r11", KREG_R11, MDB_TGT_R_EXPORT },
630Sstevel@tonic-gate 	{ "r12", KREG_R12, MDB_TGT_R_EXPORT },
640Sstevel@tonic-gate 	{ "r13", KREG_R13, MDB_TGT_R_EXPORT },
650Sstevel@tonic-gate 	{ "r14", KREG_R14, MDB_TGT_R_EXPORT },
660Sstevel@tonic-gate 	{ "r15", KREG_R15, MDB_TGT_R_EXPORT },
670Sstevel@tonic-gate 	{ "ds", KREG_DS, MDB_TGT_R_EXPORT },
680Sstevel@tonic-gate 	{ "es", KREG_ES, MDB_TGT_R_EXPORT },
690Sstevel@tonic-gate 	{ "fs", KREG_FS, MDB_TGT_R_EXPORT },
700Sstevel@tonic-gate 	{ "gs", KREG_GS, MDB_TGT_R_EXPORT },
710Sstevel@tonic-gate 	{ "trapno", KREG_TRAPNO, MDB_TGT_R_EXPORT | MDB_TGT_R_PRIV },
720Sstevel@tonic-gate 	{ "err", KREG_ERR, MDB_TGT_R_EXPORT | MDB_TGT_R_PRIV },
730Sstevel@tonic-gate 	{ "rip", KREG_RIP, MDB_TGT_R_EXPORT },
740Sstevel@tonic-gate 	{ "cs", KREG_CS, MDB_TGT_R_EXPORT },
750Sstevel@tonic-gate 	{ "rflags", KREG_RFLAGS, MDB_TGT_R_EXPORT },
760Sstevel@tonic-gate 	{ "rsp", KREG_RSP, MDB_TGT_R_EXPORT },
770Sstevel@tonic-gate 	{ "ss", KREG_SS, MDB_TGT_R_EXPORT },
780Sstevel@tonic-gate 	{ NULL, 0, 0 }
790Sstevel@tonic-gate };
800Sstevel@tonic-gate 
810Sstevel@tonic-gate void
820Sstevel@tonic-gate mdb_amd64_printregs(const mdb_tgt_gregset_t *gregs)
830Sstevel@tonic-gate {
840Sstevel@tonic-gate 	const kreg_t *kregs = &gregs->kregs[0];
850Sstevel@tonic-gate 	kreg_t rflags = kregs[KREG_RFLAGS];
860Sstevel@tonic-gate 
870Sstevel@tonic-gate #define	GETREG2(x) ((uintptr_t)kregs[(x)]), ((uintptr_t)kregs[(x)])
880Sstevel@tonic-gate 
890Sstevel@tonic-gate 	mdb_printf("%%rax = 0x%0?p %15A %%r9  = 0x%0?p %A\n",
900Sstevel@tonic-gate 	    GETREG2(KREG_RAX), GETREG2(KREG_R9));
910Sstevel@tonic-gate 	mdb_printf("%%rbx = 0x%0?p %15A %%r10 = 0x%0?p %A\n",
920Sstevel@tonic-gate 	    GETREG2(KREG_RBX), GETREG2(KREG_R10));
930Sstevel@tonic-gate 	mdb_printf("%%rcx = 0x%0?p %15A %%r11 = 0x%0?p %A\n",
940Sstevel@tonic-gate 	    GETREG2(KREG_RCX), GETREG2(KREG_R11));
950Sstevel@tonic-gate 	mdb_printf("%%rdx = 0x%0?p %15A %%r12 = 0x%0?p %A\n",
960Sstevel@tonic-gate 	    GETREG2(KREG_RDX), GETREG2(KREG_R12));
970Sstevel@tonic-gate 	mdb_printf("%%rsi = 0x%0?p %15A %%r13 = 0x%0?p %A\n",
980Sstevel@tonic-gate 	    GETREG2(KREG_RSI), GETREG2(KREG_R13));
990Sstevel@tonic-gate 	mdb_printf("%%rdi = 0x%0?p %15A %%r14 = 0x%0?p %A\n",
1000Sstevel@tonic-gate 	    GETREG2(KREG_RDI), GETREG2(KREG_R14));
1010Sstevel@tonic-gate 	mdb_printf("%%r8  = 0x%0?p %15A %%r15 = 0x%0?p %A\n\n",
1020Sstevel@tonic-gate 	    GETREG2(KREG_R8), GETREG2(KREG_R15));
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate 	mdb_printf("%%rip = 0x%0?p %A\n", GETREG2(KREG_RIP));
1050Sstevel@tonic-gate 	mdb_printf("%%rbp = 0x%0?p\n", kregs[KREG_RBP]);
1060Sstevel@tonic-gate 	mdb_printf("%%rsp = 0x%0?p\n", kregs[KREG_RSP]);
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate 	mdb_printf("%%rflags = 0x%08x\n", rflags);
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate 	mdb_printf("  id=%u vip=%u vif=%u ac=%u vm=%u rf=%u nt=%u iopl=0x%x\n",
1110Sstevel@tonic-gate 	    (rflags & KREG_EFLAGS_ID_MASK) >> KREG_EFLAGS_ID_SHIFT,
1120Sstevel@tonic-gate 	    (rflags & KREG_EFLAGS_VIP_MASK) >> KREG_EFLAGS_VIP_SHIFT,
1130Sstevel@tonic-gate 	    (rflags & KREG_EFLAGS_VIF_MASK) >> KREG_EFLAGS_VIF_SHIFT,
1140Sstevel@tonic-gate 	    (rflags & KREG_EFLAGS_AC_MASK) >> KREG_EFLAGS_AC_SHIFT,
1150Sstevel@tonic-gate 	    (rflags & KREG_EFLAGS_VM_MASK) >> KREG_EFLAGS_VM_SHIFT,
1160Sstevel@tonic-gate 	    (rflags & KREG_EFLAGS_RF_MASK) >> KREG_EFLAGS_RF_SHIFT,
1170Sstevel@tonic-gate 	    (rflags & KREG_EFLAGS_NT_MASK) >> KREG_EFLAGS_NT_SHIFT,
1180Sstevel@tonic-gate 	    (rflags & KREG_EFLAGS_IOPL_MASK) >> KREG_EFLAGS_IOPL_SHIFT);
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate 	mdb_printf("  status=<%s,%s,%s,%s,%s,%s,%s,%s,%s>\n\n",
1210Sstevel@tonic-gate 	    (rflags & KREG_EFLAGS_OF_MASK) ? "OF" : "of",
1220Sstevel@tonic-gate 	    (rflags & KREG_EFLAGS_DF_MASK) ? "DF" : "df",
1230Sstevel@tonic-gate 	    (rflags & KREG_EFLAGS_IF_MASK) ? "IF" : "if",
1240Sstevel@tonic-gate 	    (rflags & KREG_EFLAGS_TF_MASK) ? "TF" : "tf",
1250Sstevel@tonic-gate 	    (rflags & KREG_EFLAGS_SF_MASK) ? "SF" : "sf",
1260Sstevel@tonic-gate 	    (rflags & KREG_EFLAGS_ZF_MASK) ? "ZF" : "zf",
1270Sstevel@tonic-gate 	    (rflags & KREG_EFLAGS_AF_MASK) ? "AF" : "af",
1280Sstevel@tonic-gate 	    (rflags & KREG_EFLAGS_PF_MASK) ? "PF" : "pf",
1290Sstevel@tonic-gate 	    (rflags & KREG_EFLAGS_CF_MASK) ? "CF" : "cf");
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate 	mdb_printf("%24s%%cs = 0x%04x\t%%ds = 0x%04x\t%%es = 0x%04x\n",
1320Sstevel@tonic-gate 	    " ", kregs[KREG_CS], kregs[KREG_DS], kregs[KREG_ES]);
1330Sstevel@tonic-gate 
134*3446Smrj 	mdb_printf("%%trapno = 0x%x\t\t%%fs = 0x%04x\t%%gs = 0x%04x\n",
135*3446Smrj 	    kregs[KREG_TRAPNO], (kregs[KREG_FS] & 0xffff),
136*3446Smrj 	    (kregs[KREG_GS] & 0xffff));
137*3446Smrj 	mdb_printf("   %%err = 0x%x\n", kregs[KREG_ERR]);
1380Sstevel@tonic-gate }
1390Sstevel@tonic-gate 
140170Ssherrym 
141170Ssherrym 
142170Ssherrym /*
143170Ssherrym  * Sun Studio 10 patch compiler and gcc 3.4.3 Sun branch implemented a
144170Ssherrym  * "-save_args" option on amd64.  When the option is specified, INTEGER
145170Ssherrym  * type function arguments passed via registers will be saved on the stack
146170Ssherrym  * immediately after %rbp, and will not be modified through out the life
147170Ssherrym  * of the routine.
148170Ssherrym  *
149170Ssherrym  *				+--------+
150170Ssherrym  *		%rbp	-->     |  %rbp  |
151170Ssherrym  *				+--------+
152170Ssherrym  *		-0x8(%rbp)	|  %rdi  |
153170Ssherrym  *				+--------+
154170Ssherrym  *		-0x10(%rbp)	|  %rsi  |
155170Ssherrym  *				+--------+
156170Ssherrym  *		-0x18(%rbp)	|  %rdx  |
157170Ssherrym  *				+--------+
158170Ssherrym  *		-0x20(%rbp)	|  %rcx  |
159170Ssherrym  *				+--------+
160170Ssherrym  *		-0x28(%rbp)	|  %r8   |
161170Ssherrym  *				+--------+
162170Ssherrym  *		-0x30(%rbp)	|  %r9   |
163170Ssherrym  *				+--------+
164170Ssherrym  *
165170Ssherrym  *
166170Ssherrym  * For example, for the following function,
167170Ssherrym  *
168170Ssherrym  * void
169170Ssherrym  * foo(int a1, int a2, int a3, int a4, int a5, int a6, int a7)
170170Ssherrym  * {
171170Ssherrym  * ...
172170Ssherrym  * }
173170Ssherrym  *
174170Ssherrym  * Disassembled code will look something like the following:
175170Ssherrym  *
176170Ssherrym  *     pushq	%rbp
177170Ssherrym  *     movq	%rsp, %rbp
178170Ssherrym  *     subq	$imm8, %rsp			**
179170Ssherrym  *     movq	%rdi, -0x8(%rbp)
180170Ssherrym  *     movq	%rsi, -0x10(%rbp)
181170Ssherrym  *     movq	%rdx, -0x18(%rbp)
182170Ssherrym  *     movq	%rcx, -0x20(%rbp)
183170Ssherrym  *     movq	%r8, -0x28(%rbp)
184170Ssherrym  *     movq	%r9, -0x30(%rbp)
185170Ssherrym  *     ...
186170Ssherrym  * or
187170Ssherrym  *     pushq	%rbp
188170Ssherrym  *     movq	%rsp, %rbp
189170Ssherrym  *     subq	$imm8, %rsp			**
190170Ssherrym  *     movq	%r9, -0x30(%rbp)
191170Ssherrym  *     movq	%r8, -0x28(%rbp)
192170Ssherrym  *     movq	%rcx, -0x20(%rbp)
193170Ssherrym  *     movq	%rdx, -0x18(%rbp)
194170Ssherrym  *     movq	%rsi, -0x10(%rbp)
195170Ssherrym  *     movq	%rdi, -0x8(%rbp)
196170Ssherrym  *     ...
197170Ssherrym  *
198170Ssherrym  * **: The space being reserved is in addition to what the current
199170Ssherrym  *     function prolog already reserves.
200170Ssherrym  *
201170Ssherrym  * If there are odd number of arguments to a function, additional space is
202170Ssherrym  * reserved on the stack to maintain 16-byte alignment.  For example,
203170Ssherrym  *
204170Ssherrym  *     argc == 0: no argument saving.
205170Ssherrym  *     argc == 3: save 3, but space for 4 is reserved
206170Ssherrym  *     argc == 7: save 6.
207170Ssherrym  */
208170Ssherrym 
209170Ssherrym /*
210170Ssherrym  * The longest instruction sequence in bytes before all 6 arguments are
211170Ssherrym  * saved on the stack.  This value depends on compiler implementation,
212170Ssherrym  * therefore it should be examined periodically to guarantee accuracy.
213170Ssherrym  */
214170Ssherrym #define	SEQ_LEN		80
215170Ssherrym 
216170Ssherrym /*
217170Ssherrym  * Size of the instruction sequence arrays.  It should correspond to
218170Ssherrym  * the maximum number of arguments passed via registers.
219170Ssherrym  */
220170Ssherrym #define	INSTR_ARRAY_SIZE	6
221170Ssherrym 
222170Ssherrym #define	INSTR4(ins, off)	\
223170Ssherrym 	(ins[(off)] + (ins[(off) + 1] << 8) + (ins[(off + 2)] << 16) + \
224170Ssherrym 	(ins[(off) + 3] << 24))
225170Ssherrym 
226170Ssherrym /*
227170Ssherrym  * Sun Studio 10 patch implementation saves %rdi first;
228170Ssherrym  * GCC 3.4.3 Sun branch implementation saves them in reverse order.
229170Ssherrym  */
230170Ssherrym static const uint32_t save_instr[INSTR_ARRAY_SIZE] = {
231170Ssherrym 	0xf87d8948,	/* movq %rdi, -0x8(%rbp) */
232170Ssherrym 	0xf0758948,	/* movq %rsi, -0x10(%rbp) */
233170Ssherrym 	0xe8558948,	/* movq %rdx, -0x18(%rbp) */
234170Ssherrym 	0xe04d8948,	/* movq %rcx, -0x20(%rbp) */
235170Ssherrym 	0xd845894c,	/* movq %r8, -0x28(%rbp) */
236170Ssherrym 	0xd04d894c	/* movq %r9, -0x30(%rbp) */
237170Ssherrym };
238170Ssherrym 
239275Ssherrym static const uint32_t save_fp_instr[] = {
240170Ssherrym 	0xe5894855,	/* pushq %rbp; movq %rsp,%rbp, encoding 1 */
241275Ssherrym 	0xec8b4855,	/* pushq %rbp; movq %rsp,%rbp, encoding 2 */
242275Ssherrym 	0xe58948cc,	/* int $0x3; movq %rsp,%rbp, encoding 1 */
243275Ssherrym 	0xec8b48cc,	/* int $0x3; movq %rsp,%rbp, encoding 2 */
244275Ssherrym 	NULL
245170Ssherrym };
246170Ssherrym 
247170Ssherrym /*
248170Ssherrym  * Look for the above instruction sequences as indicators for register
249170Ssherrym  * arguments being available on the stack.
250170Ssherrym  */
251170Ssherrym static int
252170Ssherrym is_argsaved(mdb_tgt_t *t, uintptr_t fstart, uint64_t size, uint_t argc,
253170Ssherrym     int start_index)
254170Ssherrym {
255170Ssherrym 	uint8_t		ins[SEQ_LEN];
256170Ssherrym 	int		i, j;
257170Ssherrym 	uint32_t	n;
258170Ssherrym 
259170Ssherrym 	size = MIN(size, SEQ_LEN);
260170Ssherrym 	argc = MIN((start_index + argc), INSTR_ARRAY_SIZE);
261170Ssherrym 
262170Ssherrym 	if (mdb_tgt_vread(t, ins, size, fstart) != size)
263170Ssherrym 		return (0);
264170Ssherrym 
265170Ssherrym 	/*
266170Ssherrym 	 * Make sure framepointer has been saved.
267170Ssherrym 	 */
268170Ssherrym 	n = INSTR4(ins, 0);
269275Ssherrym 	for (i = 0; save_fp_instr[i] != NULL; i++) {
270170Ssherrym 		if (n == save_fp_instr[i])
271170Ssherrym 			break;
272170Ssherrym 	}
273170Ssherrym 
274275Ssherrym 	if (save_fp_instr[i] == NULL)
275170Ssherrym 		return (0);
276170Ssherrym 
277170Ssherrym 	/*
278170Ssherrym 	 * Compare against Sun Studio implementation
279170Ssherrym 	 */
280170Ssherrym 	for (i = 8, j = start_index; i < size - 4; i++) {
281170Ssherrym 		n = INSTR4(ins, i);
282170Ssherrym 
283170Ssherrym 		if (n == save_instr[j]) {
284170Ssherrym 			i += 3;
285170Ssherrym 			if (++j >= argc)
286170Ssherrym 				return (1);
287170Ssherrym 		}
288170Ssherrym 	}
289170Ssherrym 
290170Ssherrym 	/*
291170Ssherrym 	 * Compare against GCC implementation
292170Ssherrym 	 */
293170Ssherrym 	for (i = 8, j = argc - 1; i < size - 4; i++) {
294170Ssherrym 		n = INSTR4(ins, i);
295170Ssherrym 
296170Ssherrym 		if (n == save_instr[j]) {
297170Ssherrym 			i += 3;
298170Ssherrym 			if (--j < start_index)
299170Ssherrym 				return (1);
300170Ssherrym 		}
301170Ssherrym 	}
302170Ssherrym 
303170Ssherrym 	return (0);
304170Ssherrym }
305170Ssherrym 
3060Sstevel@tonic-gate int
3070Sstevel@tonic-gate mdb_amd64_kvm_stack_iter(mdb_tgt_t *t, const mdb_tgt_gregset_t *gsp,
3080Sstevel@tonic-gate     mdb_tgt_stack_f *func, void *arg)
3090Sstevel@tonic-gate {
3100Sstevel@tonic-gate 	mdb_tgt_gregset_t gregs;
3110Sstevel@tonic-gate 	kreg_t *kregs = &gregs.kregs[0];
3120Sstevel@tonic-gate 	int got_pc = (gsp->kregs[KREG_RIP] != 0);
313170Ssherrym 	uint_t argc, reg_argc;
314170Ssherrym 	long fr_argv[32];
315170Ssherrym 	int start_index; /* index to save_instr where to start comparison */
316170Ssherrym 	int i;
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate 	struct {
3190Sstevel@tonic-gate 		uintptr_t fr_savfp;
3200Sstevel@tonic-gate 		uintptr_t fr_savpc;
3210Sstevel@tonic-gate 	} fr;
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate 	uintptr_t fp = gsp->kregs[KREG_RBP];
3240Sstevel@tonic-gate 	uintptr_t pc = gsp->kregs[KREG_RIP];
325170Ssherrym 	uintptr_t curpc;
326170Ssherrym 
327170Ssherrym 	ssize_t size;
328170Ssherrym 
329170Ssherrym 	GElf_Sym s;
330170Ssherrym 	mdb_syminfo_t sip;
331170Ssherrym 	mdb_ctf_funcinfo_t mfp;
3320Sstevel@tonic-gate 
3330Sstevel@tonic-gate 	bcopy(gsp, &gregs, sizeof (gregs));
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate 	while (fp != 0) {
3360Sstevel@tonic-gate 
337170Ssherrym 		curpc = pc;
338170Ssherrym 
3390Sstevel@tonic-gate 		if (fp & (STACK_ALIGN - 1))
3400Sstevel@tonic-gate 			return (set_errno(EMDB_STKALIGN));
3410Sstevel@tonic-gate 
342170Ssherrym 		if (mdb_tgt_vread(t, &fr, sizeof (fr), fp) != sizeof (fr))
343170Ssherrym 			return (-1);	/* errno has been set for us */
344170Ssherrym 
345170Ssherrym 		if ((mdb_tgt_lookup_by_addr(t, pc, MDB_TGT_SYM_FUZZY,
346170Ssherrym 		    NULL, 0, &s, &sip) == 0) &&
347170Ssherrym 		    (mdb_ctf_func_info(&s, &sip, &mfp) == 0)) {
348170Ssherrym 			int return_type = mdb_ctf_type_kind(mfp.mtf_return);
349170Ssherrym 			argc = mfp.mtf_argc;
350170Ssherrym 			/*
351170Ssherrym 			 * If the function returns a structure or union,
352170Ssherrym 			 * %rdi contains the address in which to store the
353170Ssherrym 			 * return value rather than for an argument.
354170Ssherrym 			 */
355170Ssherrym 			if (return_type == CTF_K_STRUCT ||
356170Ssherrym 			    return_type == CTF_K_UNION)
357170Ssherrym 				start_index = 1;
358170Ssherrym 			else
359170Ssherrym 				start_index = 0;
360170Ssherrym 		} else {
361170Ssherrym 			argc = 0;
362170Ssherrym 		}
363170Ssherrym 
364170Ssherrym 		if (argc != 0 && is_argsaved(t, s.st_value, s.st_size,
365170Ssherrym 		    argc, start_index)) {
3660Sstevel@tonic-gate 
367170Ssherrym 			/* Upto to 6 arguments are passed via registers */
368170Ssherrym 			reg_argc = MIN(6, mfp.mtf_argc);
369170Ssherrym 			size = reg_argc * sizeof (long);
370170Ssherrym 
371170Ssherrym 			if (mdb_tgt_vread(t, fr_argv, size, (fp - size))
372170Ssherrym 			    != size)
373170Ssherrym 				return (-1);	/* errno has been set for us */
374170Ssherrym 
375170Ssherrym 			/*
376170Ssherrym 			 * Arrange the arguments in the right order for
377170Ssherrym 			 * printing.
378170Ssherrym 			 */
379170Ssherrym 			for (i = 0; i < (reg_argc >> 1); i++) {
380170Ssherrym 				long t = fr_argv[i];
381170Ssherrym 
382170Ssherrym 				fr_argv[i] = fr_argv[reg_argc - i - 1];
383170Ssherrym 				fr_argv[reg_argc - i - 1] = t;
384170Ssherrym 			}
385170Ssherrym 
386170Ssherrym 			if (argc > 6) {
387170Ssherrym 				size = (argc - 6) * sizeof (long);
388170Ssherrym 				if (mdb_tgt_vread(t, &fr_argv[6], size,
389170Ssherrym 				    fp + sizeof (fr)) != size)
390170Ssherrym 					return (-1); /* errno has been set */
391170Ssherrym 			}
392170Ssherrym 		} else
393170Ssherrym 			argc = 0;
394170Ssherrym 
395170Ssherrym 		if (got_pc && func(arg, pc, argc, fr_argv, &gregs) != 0)
3960Sstevel@tonic-gate 			break;
3970Sstevel@tonic-gate 
3980Sstevel@tonic-gate 		kregs[KREG_RSP] = kregs[KREG_RBP];
3990Sstevel@tonic-gate 
4000Sstevel@tonic-gate 		kregs[KREG_RBP] = fp = fr.fr_savfp;
4010Sstevel@tonic-gate 		kregs[KREG_RIP] = pc = fr.fr_savpc;
4020Sstevel@tonic-gate 
403170Ssherrym 		if (curpc == pc)
404170Ssherrym 			break;
405170Ssherrym 
4060Sstevel@tonic-gate 		got_pc = (pc != 0);
4070Sstevel@tonic-gate 	}
4080Sstevel@tonic-gate 
4090Sstevel@tonic-gate 	return (0);
4100Sstevel@tonic-gate }
4110Sstevel@tonic-gate 
4120Sstevel@tonic-gate /*
4130Sstevel@tonic-gate  * Determine the return address for the current frame.  Typically this is the
4140Sstevel@tonic-gate  * fr_savpc value from the current frame, but we also perform some special
4150Sstevel@tonic-gate  * handling to see if we are stopped on one of the first two instructions of
4160Sstevel@tonic-gate  * a typical function prologue, in which case %rbp will not be set up yet.
4170Sstevel@tonic-gate  */
4180Sstevel@tonic-gate int
4190Sstevel@tonic-gate mdb_amd64_step_out(mdb_tgt_t *t, uintptr_t *p, kreg_t pc, kreg_t fp, kreg_t sp,
4200Sstevel@tonic-gate     mdb_instr_t curinstr)
4210Sstevel@tonic-gate {
4220Sstevel@tonic-gate 	struct frame fr;
4230Sstevel@tonic-gate 	GElf_Sym s;
4240Sstevel@tonic-gate 	char buf[1];
4250Sstevel@tonic-gate 
4260Sstevel@tonic-gate 	enum {
4270Sstevel@tonic-gate 		M_PUSHQ_RBP	= 0x55,	/* pushq %rbp */
4280Sstevel@tonic-gate 		M_REX_W		= 0x48, /* REX prefix with only W set */
4290Sstevel@tonic-gate 		M_MOVL_RBP	= 0x8b	/* movq %rsp, %rbp with prefix */
4300Sstevel@tonic-gate 	};
4310Sstevel@tonic-gate 
4320Sstevel@tonic-gate 	if (mdb_tgt_lookup_by_addr(t, pc, MDB_TGT_SYM_FUZZY,
4330Sstevel@tonic-gate 	    buf, 0, &s, NULL) == 0) {
4340Sstevel@tonic-gate 		if (pc == s.st_value && curinstr == M_PUSHQ_RBP)
4350Sstevel@tonic-gate 			fp = sp - 8;
4360Sstevel@tonic-gate 		else if (pc == s.st_value + 1 && curinstr == M_REX_W) {
4370Sstevel@tonic-gate 			if (mdb_tgt_vread(t, &curinstr, sizeof (curinstr),
4380Sstevel@tonic-gate 			    pc + 1) == sizeof (curinstr) && curinstr ==
4390Sstevel@tonic-gate 			    M_MOVL_RBP)
4400Sstevel@tonic-gate 				fp = sp;
4410Sstevel@tonic-gate 		}
4420Sstevel@tonic-gate 	}
4430Sstevel@tonic-gate 
4440Sstevel@tonic-gate 	if (mdb_tgt_vread(t, &fr, sizeof (fr), fp) == sizeof (fr)) {
4450Sstevel@tonic-gate 		*p = fr.fr_savpc;
4460Sstevel@tonic-gate 		return (0);
4470Sstevel@tonic-gate 	}
4480Sstevel@tonic-gate 
4490Sstevel@tonic-gate 	return (-1); /* errno is set for us */
4500Sstevel@tonic-gate }
4510Sstevel@tonic-gate 
4520Sstevel@tonic-gate /*ARGSUSED*/
4530Sstevel@tonic-gate int
4540Sstevel@tonic-gate mdb_amd64_next(mdb_tgt_t *t, uintptr_t *p, kreg_t pc, mdb_instr_t curinstr)
4550Sstevel@tonic-gate {
4560Sstevel@tonic-gate 	mdb_tgt_addr_t npc;
4572244Sdmick 	mdb_tgt_addr_t callpc;
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate 	enum {
4600Sstevel@tonic-gate 		M_CALL_REL = 0xe8, /* call near with relative displacement */
4610Sstevel@tonic-gate 		M_CALL_REG = 0xff, /* call near indirect or call far register */
4620Sstevel@tonic-gate 
4630Sstevel@tonic-gate 		M_REX_LO = 0x40,
4640Sstevel@tonic-gate 		M_REX_HI = 0x4f
4650Sstevel@tonic-gate 	};
4660Sstevel@tonic-gate 
4670Sstevel@tonic-gate 	/*
4680Sstevel@tonic-gate 	 * If the opcode is a near call with relative displacement, assume the
4690Sstevel@tonic-gate 	 * displacement is a rel32 from the next instruction.
4700Sstevel@tonic-gate 	 */
4710Sstevel@tonic-gate 	if (curinstr == M_CALL_REL) {
4720Sstevel@tonic-gate 		*p = pc + sizeof (mdb_instr_t) + sizeof (uint32_t);
4730Sstevel@tonic-gate 		return (0);
4740Sstevel@tonic-gate 	}
4750Sstevel@tonic-gate 
4760Sstevel@tonic-gate 	/* Skip the rex prefix, if any */
4772244Sdmick 	callpc = pc;
4782244Sdmick 	while (curinstr >= M_REX_LO && curinstr <= M_REX_HI) {
4792244Sdmick 		if (mdb_tgt_vread(t, &curinstr, sizeof (curinstr), ++callpc) !=
4802244Sdmick 		    sizeof (curinstr))
4812244Sdmick 			return (-1); /* errno is set for us */
4822244Sdmick 	}
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate 	if (curinstr != M_CALL_REG) {
4850Sstevel@tonic-gate 		/* It's not a call */
4860Sstevel@tonic-gate 		return (set_errno(EAGAIN));
4870Sstevel@tonic-gate 	}
4880Sstevel@tonic-gate 
4890Sstevel@tonic-gate 	if ((npc = mdb_dis_nextins(mdb.m_disasm, t, MDB_TGT_AS_VIRT, pc)) == pc)
4900Sstevel@tonic-gate 		return (-1); /* errno is set for us */
4910Sstevel@tonic-gate 
4920Sstevel@tonic-gate 	*p = npc;
4930Sstevel@tonic-gate 	return (0);
4940Sstevel@tonic-gate }
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate /*ARGSUSED*/
4970Sstevel@tonic-gate int
4980Sstevel@tonic-gate mdb_amd64_kvm_frame(void *arglim, uintptr_t pc, uint_t argc, const long *argv,
4990Sstevel@tonic-gate     const mdb_tgt_gregset_t *gregs)
5000Sstevel@tonic-gate {
5010Sstevel@tonic-gate 	argc = MIN(argc, (uintptr_t)arglim);
5020Sstevel@tonic-gate 	mdb_printf("%a(", pc);
5030Sstevel@tonic-gate 
5040Sstevel@tonic-gate 	if (argc != 0) {
5050Sstevel@tonic-gate 		mdb_printf("%lr", *argv++);
5060Sstevel@tonic-gate 		for (argc--; argc != 0; argc--)
5070Sstevel@tonic-gate 			mdb_printf(", %lr", *argv++);
5080Sstevel@tonic-gate 	}
5090Sstevel@tonic-gate 
5100Sstevel@tonic-gate 	mdb_printf(")\n");
5110Sstevel@tonic-gate 	return (0);
5120Sstevel@tonic-gate }
5130Sstevel@tonic-gate 
5140Sstevel@tonic-gate int
5150Sstevel@tonic-gate mdb_amd64_kvm_framev(void *arglim, uintptr_t pc, uint_t argc, const long *argv,
5160Sstevel@tonic-gate     const mdb_tgt_gregset_t *gregs)
5170Sstevel@tonic-gate {
518170Ssherrym 	/*
519170Ssherrym 	 * Historically adb limited stack trace argument display to a fixed-
520170Ssherrym 	 * size number of arguments since no symbolic debugging info existed.
521170Ssherrym 	 * On amd64 we can detect the true number of saved arguments so only
522170Ssherrym 	 * respect an arglim of zero; otherwise display the entire argv[].
523170Ssherrym 	 */
524170Ssherrym 	if (arglim == 0)
525170Ssherrym 		argc = 0;
526170Ssherrym 
5270Sstevel@tonic-gate 	mdb_printf("%0?lr %a(", gregs->kregs[KREG_RBP], pc);
5280Sstevel@tonic-gate 
5290Sstevel@tonic-gate 	if (argc != 0) {
5300Sstevel@tonic-gate 		mdb_printf("%lr", *argv++);
5310Sstevel@tonic-gate 		for (argc--; argc != 0; argc--)
5320Sstevel@tonic-gate 			mdb_printf(", %lr", *argv++);
5330Sstevel@tonic-gate 	}
5340Sstevel@tonic-gate 
5350Sstevel@tonic-gate 	mdb_printf(")\n");
5360Sstevel@tonic-gate 	return (0);
5370Sstevel@tonic-gate }
538