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