xref: /titanic_41/usr/src/cmd/mdb/intel/kmdb/kvm_isadep.c (revision d1a96f96f5596d01fbc8f94d718b4d737bfdca8e)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
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 /*
30  * isa-dependent portions of the kmdb target
31  */
32 
33 #include <kmdb/kvm.h>
34 #include <kmdb/kvm_cpu.h>
35 #include <kmdb/kmdb_kdi.h>
36 #include <kmdb/kmdb_asmutil.h>
37 #include <mdb/mdb_debug.h>
38 #include <mdb/mdb_err.h>
39 #include <mdb/mdb_list.h>
40 #include <mdb/mdb_target_impl.h>
41 #include <mdb/mdb_isautil.h>
42 #include <mdb/mdb_kreg_impl.h>
43 #include <mdb/mdb.h>
44 
45 #include <sys/types.h>
46 #include <sys/frame.h>
47 #include <sys/trap.h>
48 #include <sys/bitmap.h>
49 
50 /* Higher than the highest trap number for which we have a defined specifier */
51 #define	KMT_MAXTRAPNO	0x20
52 
53 #define	IOPORTLIMIT	0xffff	/* XXX find a new home for this */
54 
55 const char *
56 kmt_def_dismode(void)
57 {
58 #ifdef	__amd64
59 	return ("amd64");
60 #else
61 	return ("ia32");
62 #endif
63 }
64 
65 int
66 kmt_step_out_validate(mdb_tgt_t *t, uintptr_t pc)
67 {
68 	kmt_data_t *kmt = t->t_data;
69 	int i;
70 
71 	for (i = 0; i < sizeof (kmt->kmt_intrsyms) / sizeof (GElf_Sym); i++) {
72 		GElf_Sym *sym = (GElf_Sym *)&kmt->kmt_intrsyms + i;
73 
74 		if (pc >= sym->st_value && pc < sym->st_value + sym->st_size)
75 			return (0);
76 	}
77 
78 	return (1);
79 }
80 
81 /*
82  * Determine the return address for the current frame.
83  */
84 int
85 kmt_step_out(mdb_tgt_t *t, uintptr_t *p)
86 {
87 	mdb_instr_t instr;
88 	kreg_t pc, sp, fp;
89 
90 	(void) kmdb_dpi_get_register("pc", &pc);
91 	(void) kmdb_dpi_get_register("sp", &sp);
92 	(void) kmdb_dpi_get_register("fp", &fp);
93 
94 	if (mdb_tgt_vread(t, &instr, sizeof (mdb_instr_t), pc) !=
95 	    sizeof (mdb_instr_t))
96 		return (-1); /* errno is set for us */
97 
98 	if (!kmt_step_out_validate(t, pc))
99 		return (set_errno(EMDB_TGTNOTSUP));
100 
101 	return (mdb_isa_step_out(t, p, pc, fp, sp, instr));
102 }
103 
104 int
105 kmt_step_branch(mdb_tgt_t *t)
106 {
107 	kmt_data_t *kmt = t->t_data;
108 
109 	return (kmt_cpu_step_branch(t, kmt->kmt_cpu));
110 }
111 
112 /*
113  * Return the address of the next instruction following a call, or return -1
114  * and set errno to EAGAIN if the target should just single-step.
115  */
116 int
117 kmt_next(mdb_tgt_t *t, uintptr_t *p)
118 {
119 	kreg_t pc;
120 	mdb_instr_t instr;
121 
122 	(void) kmdb_dpi_get_register("pc", &pc);
123 
124 	if (mdb_tgt_vread(t, &instr, sizeof (mdb_instr_t), pc) !=
125 	    sizeof (mdb_instr_t))
126 		return (-1); /* errno is set for us */
127 
128 	return (mdb_isa_next(t, p, pc, instr));
129 }
130 
131 /*
132  * Return a flag indicating if the specified %eip is likely to have an
133  * interrupt frame on the stack.  We do this by comparing the address to the
134  * range of addresses spanned by several well-known routines, and looking
135  * to see if the next and previous %ebp values are "far" apart.  Sigh.
136  */
137 int
138 mdb_kvm_intrframe(mdb_tgt_t *t, uintptr_t pc, uintptr_t fp,
139     uintptr_t prevfp)
140 {
141 	kmt_data_t *kmt = t->t_data;
142 	const size_t dist = 0x800 * sizeof (uintptr_t);
143 
144 	return ((pc >= kmt->kmt_cmnint.st_value &&
145 	    (pc < kmt->kmt_cmnint.st_value + kmt->kmt_cmnint.st_size)) ||
146 	    (pc >= kmt->kmt_cmntrap.st_value &&
147 	    (pc < kmt->kmt_cmntrap.st_value + kmt->kmt_cmntrap.st_size)) ||
148 	    (fp >= prevfp + dist) || (fp <= prevfp - dist));
149 }
150 
151 /*ARGSUSED*/
152 static int
153 kmt_stack_common(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv,
154     int cpuid, mdb_tgt_stack_f *func)
155 {
156 	const mdb_tgt_gregset_t *grp = NULL;
157 	mdb_tgt_gregset_t gregs;
158 	void *arg = (void *)(uintptr_t)mdb.m_nargs;
159 
160 	if (flags & DCMD_ADDRSPEC) {
161 		bzero(&gregs, sizeof (gregs));
162 		gregs.kregs[KREG_FP] = addr;
163 		grp = &gregs;
164 	} else
165 		grp = kmdb_dpi_get_gregs(cpuid);
166 
167 	if (grp == NULL) {
168 		warn("failed to retrieve registers for cpu %d", cpuid);
169 		return (DCMD_ERR);
170 	}
171 
172 	if (argc != 0) {
173 		if (argv->a_type == MDB_TYPE_CHAR || argc > 1)
174 			return (DCMD_USAGE);
175 
176 		if (argv->a_type == MDB_TYPE_STRING)
177 			arg = (void *)(uintptr_t)mdb_strtoull(argv->a_un.a_str);
178 		else
179 			arg = (void *)(uintptr_t)argv->a_un.a_val;
180 	}
181 
182 	(void) mdb_isa_kvm_stack_iter(mdb.m_target, grp, func, arg);
183 
184 	return (DCMD_OK);
185 }
186 
187 int
188 kmt_cpustack(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv,
189     int cpuid, int verbose)
190 {
191 	return (kmt_stack_common(addr, flags, argc, argv, cpuid,
192 	    (verbose ? mdb_isa_kvm_framev : mdb_isa_kvm_frame)));
193 }
194 
195 int
196 kmt_stack(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
197 {
198 	return (kmt_stack_common(addr, flags, argc, argv, DPI_MASTER_CPUID,
199 	    mdb_isa_kvm_frame));
200 }
201 
202 int
203 kmt_stackv(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
204 {
205 	return (kmt_stack_common(addr, flags, argc, argv, DPI_MASTER_CPUID,
206 	    mdb_isa_kvm_framev));
207 }
208 
209 int
210 kmt_stackr(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
211 {
212 	return (kmt_stack_common(addr, flags, argc, argv, DPI_MASTER_CPUID,
213 	    mdb_isa_kvm_framev));
214 }
215 
216 /*ARGSUSED*/
217 void
218 kmt_printregs(const mdb_tgt_gregset_t *gregs)
219 {
220 	mdb_isa_printregs(gregs);
221 }
222 
223 #define	IOCHECK_NOWARN	0
224 #define	IOCHECK_WARN	1
225 
226 static int
227 kmt_io_check(uint64_t nbytes, uintptr_t addr, int dowarn)
228 {
229 	if (addr > IOPORTLIMIT) {
230 		if (dowarn)
231 			warn("port address must be 0-%#x\n", IOPORTLIMIT);
232 		return (set_errno(EINVAL));
233 	}
234 
235 	if (nbytes != 1 && nbytes != 2 && nbytes != 4) {
236 		if (dowarn)
237 			warn("port access must be 1, 2, or 4 bytes\n");
238 		return (set_errno(EINVAL));
239 	}
240 
241 	if ((addr & (nbytes - 1)) != 0) {
242 		if (dowarn) {
243 			warn("address for %llu-byte access must be %llu-byte "
244 			    "aligned\n", (u_longlong_t)nbytes,
245 			    (u_longlong_t)nbytes);
246 		}
247 		return (set_errno(EINVAL));
248 	}
249 
250 	return (0);
251 }
252 
253 /*ARGSUSED1*/
254 int
255 kmt_in_dcmd(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
256 {
257 	uint64_t len = 0;
258 	uint32_t buf;
259 
260 	if (mdb_getopts(argc, argv,
261 	    'L', MDB_OPT_UINT64, &len,
262 	    NULL) != argc)
263 		return (DCMD_USAGE);
264 
265 	if (len == 0)
266 		len = mdb.m_dcount;
267 
268 	if (kmt_io_check(len, addr, IOCHECK_WARN) < 0)
269 		return (DCMD_ERR);
270 
271 	if (mdb_tgt_ioread(mdb.m_target, &buf, len, addr) < 0) {
272 		warn("failed to read from port 0x%llx", (u_longlong_t)addr);
273 		return (DCMD_ERR);
274 	}
275 
276 	mdb_printf("%x\n", buf);
277 
278 	return (DCMD_OK);
279 }
280 
281 /*ARGSUSED1*/
282 int
283 kmt_out_dcmd(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
284 {
285 	uint64_t len = 0;
286 	uint64_t val;
287 
288 	if (mdb_getopts(argc, argv,
289 	    'L', MDB_OPT_UINT64, &len,
290 	    NULL) != argc - 1)
291 		return (DCMD_USAGE);
292 
293 	if (len == 0)
294 		len = mdb.m_dcount;
295 
296 	argv += argc - 1;
297 	if (argv->a_type == MDB_TYPE_STRING)
298 		val = mdb_strtoull(argv->a_un.a_str);
299 	else
300 		val = argv->a_un.a_val;
301 
302 	if (kmt_io_check(len, addr, IOCHECK_WARN) < 0)
303 		return (DCMD_ERR);
304 
305 	if (val > (1ULL << (len * NBBY)) - 1) {
306 		warn("value is out of range for port size\n");
307 		return (DCMD_ERR);
308 	}
309 
310 	if (mdb_tgt_iowrite(mdb.m_target, &val, len, addr) < 0) {
311 		warn("failed to write to port %llx", (u_longlong_t)addr);
312 		return (DCMD_ERR);
313 	}
314 
315 	return (DCMD_OK);
316 }
317 
318 static int
319 kmt_rwmsr(uint32_t addr, uint64_t *valp, void (*rw)(uint32_t, uint64_t *))
320 {
321 	jmp_buf pcb, *oldpcb = NULL;
322 
323 	if (setjmp(pcb) != 0) {
324 		kmdb_dpi_restore_fault_hdlr(oldpcb);
325 		return (-1); /* errno is set for us */
326 	}
327 
328 	oldpcb = kmdb_dpi_set_fault_hdlr(&pcb);
329 	rw(addr, valp);
330 	kmdb_dpi_restore_fault_hdlr(oldpcb);
331 
332 	return (0);
333 }
334 
335 /*ARGSUSED*/
336 int
337 kmt_rdmsr(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
338 {
339 	uint64_t val;
340 
341 	if (!(flags & DCMD_ADDRSPEC))
342 		return (DCMD_USAGE);
343 
344 	if (kmt_rwmsr(addr, &val, rdmsr) < 0) {
345 		warn("rdmsr failed");
346 		return (DCMD_ERR);
347 	}
348 
349 	mdb_printf("%llx\n", (u_longlong_t)val);
350 
351 	return (DCMD_OK);
352 }
353 
354 /*ARGSUSED*/
355 int
356 kmt_wrmsr(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
357 {
358 	uint64_t val;
359 
360 	if (!(flags & DCMD_ADDRSPEC) || argc != 1)
361 		return (DCMD_USAGE);
362 
363 	if (argv->a_type == MDB_TYPE_STRING)
364 		val = mdb_strtoull(argv->a_un.a_str);
365 	else
366 		val = argv->a_un.a_val;
367 
368 	if (kmt_rwmsr(addr, &val, wrmsr)) {
369 		warn("wrmsr failed");
370 		return (DCMD_ERR);
371 	}
372 
373 	return (DCMD_OK);
374 }
375 
376 int
377 kmt_msr_validate(const kmdb_msr_t *msr)
378 {
379 	uint64_t val;
380 
381 	for (/* */; msr->msr_num != 0; msr++) {
382 		if (kmt_rwmsr(msr->msr_num, &val, rdmsr) < 0)
383 			return (0);
384 	}
385 
386 	return (1);
387 }
388 
389 /*ARGSUSED*/
390 ssize_t
391 kmt_write(mdb_tgt_t *t, const void *buf, size_t nbytes, uintptr_t addr)
392 {
393 	if (!(t->t_flags & MDB_TGT_F_ALLOWIO) &&
394 	    (nbytes = kmdb_kdi_range_is_nontoxic(addr, nbytes, 1)) == 0)
395 		return (set_errno(EMDB_NOMAP));
396 
397 	return (kmt_rw(t, (void *)buf, nbytes, addr, kmt_writer));
398 }
399 
400 /*ARGSUSED*/
401 static ssize_t
402 kmt_iorw(mdb_tgt_t *t, void *buf, size_t nbytes, uint64_t addr,
403     void (*iorw)(void *, size_t, uintptr_t))
404 {
405 	jmp_buf pcb, *oldpcb = NULL;
406 
407 	if (kmt_io_check(nbytes, addr, IOCHECK_NOWARN) < 0)
408 		return (-1); /* errno is set for us */
409 
410 	if (setjmp(pcb) != 0) {
411 		kmdb_dpi_restore_fault_hdlr(oldpcb);
412 		return (-1); /* errno is set for us */
413 	}
414 
415 	oldpcb = kmdb_dpi_set_fault_hdlr(&pcb);
416 	iorw(buf, nbytes, addr);
417 	kmdb_dpi_restore_fault_hdlr(oldpcb);
418 
419 	return (nbytes);
420 }
421 
422 /*ARGSUSED*/
423 ssize_t
424 kmt_ioread(mdb_tgt_t *t, void *buf, size_t nbytes, uintptr_t addr)
425 {
426 	return (kmt_iorw(t, buf, nbytes, addr, kmt_in));
427 }
428 
429 /*ARGSUSED*/
430 ssize_t
431 kmt_iowrite(mdb_tgt_t *t, const void *buf, size_t nbytes, uintptr_t addr)
432 {
433 	return (kmt_iorw(t, (void *)buf, nbytes, addr, kmt_out));
434 }
435 
436 const char *
437 kmt_trapname(int trapnum)
438 {
439 	static char trapname[11];
440 
441 	switch (trapnum) {
442 	case T_ZERODIV:
443 		return ("division by zero (#de) trap");
444 	case T_SGLSTP:
445 		return ("single-step (#db) trap");
446 	case T_NMIFLT:
447 		return ("NMI");
448 	case T_BPTFLT:
449 		return ("breakpoint (#bp) trap");
450 	case T_ILLINST:
451 		return ("illegal instruction (#ud) trap");
452 	case T_SEGFLT:
453 		return ("segment not present (#np) trap");
454 	case T_STKFLT:
455 		return ("stack (#ss) trap");
456 	case T_GPFLT:
457 		return ("general protection (#gp) trap");
458 	case T_PGFLT:
459 		return ("page fault (#pf) trap");
460 	case T_ALIGNMENT:
461 		return ("alignment check (#ac) trap");
462 	case T_MCE:
463 		return ("machine check (#mc) trap");
464 	case T_SIMDFPE:
465 		return ("SSE/SSE2 (#xm) trap");
466 	case T_DBGENTR:
467 		return ("debugger entry trap");
468 	default:
469 		(void) mdb_snprintf(trapname, sizeof (trapname), "trap %#x",
470 		    trapnum);
471 		return (trapname);
472 	}
473 }
474 
475 void
476 kmt_init_isadep(mdb_tgt_t *t)
477 {
478 	kmt_data_t *kmt = t->t_data;
479 
480 	kmt->kmt_rds = mdb_isa_kregs;
481 
482 	kmt->kmt_trapmax = KMT_MAXTRAPNO;
483 	kmt->kmt_trapmap = mdb_zalloc(BT_SIZEOFMAP(kmt->kmt_trapmax), UM_SLEEP);
484 
485 	/* Traps for which we want to provide an explicit message */
486 	(void) mdb_tgt_add_fault(t, T_ZERODIV, MDB_TGT_SPEC_INTERNAL,
487 	    no_se_f, NULL);
488 	(void) mdb_tgt_add_fault(t, T_ILLINST, MDB_TGT_SPEC_INTERNAL,
489 	    no_se_f, NULL);
490 	(void) mdb_tgt_add_fault(t, T_SEGFLT, MDB_TGT_SPEC_INTERNAL,
491 	    no_se_f, NULL);
492 	(void) mdb_tgt_add_fault(t, T_STKFLT, MDB_TGT_SPEC_INTERNAL,
493 	    no_se_f, NULL);
494 	(void) mdb_tgt_add_fault(t, T_GPFLT, MDB_TGT_SPEC_INTERNAL,
495 	    no_se_f, NULL);
496 	(void) mdb_tgt_add_fault(t, T_PGFLT, MDB_TGT_SPEC_INTERNAL,
497 	    no_se_f, NULL);
498 	(void) mdb_tgt_add_fault(t, T_ALIGNMENT, MDB_TGT_SPEC_INTERNAL,
499 	    no_se_f, NULL);
500 	(void) mdb_tgt_add_fault(t, T_MCE, MDB_TGT_SPEC_INTERNAL,
501 	    no_se_f, NULL);
502 	(void) mdb_tgt_add_fault(t, T_SIMDFPE, MDB_TGT_SPEC_INTERNAL,
503 	    no_se_f, NULL);
504 
505 	/*
506 	 * Traps which will be handled elsewhere, and which therefore don't
507 	 * need the trap-based message.
508 	 */
509 	BT_SET(kmt->kmt_trapmap, T_SGLSTP);
510 	BT_SET(kmt->kmt_trapmap, T_BPTFLT);
511 	BT_SET(kmt->kmt_trapmap, T_DBGENTR);
512 
513 	/* Catch-all for traps not explicitly listed here */
514 	(void) mdb_tgt_add_fault(t, KMT_TRAP_NOTENUM, MDB_TGT_SPEC_INTERNAL,
515 	    no_se_f, NULL);
516 }
517 
518 void
519 kmt_startup_isadep(mdb_tgt_t *t)
520 {
521 	kmt_data_t *kmt = t->t_data;
522 
523 	/*
524 	 * The stack trace and ::step out code need to detect "interrupt"
525 	 * frames.  The heuristic they use to detect said frames requires the
526 	 * addresses of routines that can generate them.
527 	 */
528 	(void) mdb_tgt_lookup_by_name(t, MDB_TGT_OBJ_EXEC,
529 	    "cmnint", &kmt->kmt_intrsyms._kmt_cmnint, NULL);
530 	(void) mdb_tgt_lookup_by_name(t, MDB_TGT_OBJ_EXEC,
531 	    "cmntrap", &kmt->kmt_intrsyms._kmt_cmntrap, NULL);
532 	(void) mdb_tgt_lookup_by_name(t, MDB_TGT_OBJ_EXEC,
533 	    "sys_sysenter", &kmt->kmt_intrsyms._kmt_sysenter, NULL);
534 #if defined(__amd64)
535 	(void) mdb_tgt_lookup_by_name(t, MDB_TGT_OBJ_EXEC,
536 	    "sys_syscall", &kmt->kmt_intrsyms._kmt_syscall, NULL);
537 #endif
538 }
539