xref: /titanic_44/usr/src/cmd/mdb/intel/mdb/kvm_amd64dep.c (revision b9238976491622ad75a67ab0c12edf99e36212b9)
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  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * Libkvm Kernel Target Intel 64-bit component
30  *
31  * This file provides the ISA-dependent portion of the libkvm kernel target.
32  * For more details on the implementation refer to mdb_kvm.c.
33  */
34 
35 #include <sys/types.h>
36 #include <sys/reg.h>
37 #include <sys/frame.h>
38 #include <sys/stack.h>
39 #include <sys/sysmacros.h>
40 #include <sys/panic.h>
41 #include <sys/privregs.h>
42 #include <strings.h>
43 
44 #include <mdb/mdb_target_impl.h>
45 #include <mdb/mdb_disasm.h>
46 #include <mdb/mdb_modapi.h>
47 #include <mdb/mdb_conf.h>
48 #include <mdb/mdb_kreg_impl.h>
49 #include <mdb/mdb_amd64util.h>
50 #include <mdb/kvm_isadep.h>
51 #include <mdb/mdb_kvm.h>
52 #include <mdb/mdb_err.h>
53 #include <mdb/mdb_debug.h>
54 #include <mdb/mdb.h>
55 
56 /*ARGSUSED*/
57 int
58 kt_regs(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
59 {
60 	mdb_amd64_printregs((const mdb_tgt_gregset_t *)addr);
61 	return (DCMD_OK);
62 }
63 
64 static int
65 kt_stack_common(uintptr_t addr, uint_t flags, int argc,
66     const mdb_arg_t *argv, mdb_tgt_stack_f *func)
67 {
68 	kt_data_t *kt = mdb.m_target->t_data;
69 	void *arg = (void *)(uintptr_t)mdb.m_nargs;
70 	mdb_tgt_gregset_t gregs, *grp;
71 
72 	if (flags & DCMD_ADDRSPEC) {
73 		bzero(&gregs, sizeof (gregs));
74 		gregs.kregs[KREG_RBP] = addr;
75 		grp = &gregs;
76 	} else
77 		grp = kt->k_regs;
78 
79 	if (argc != 0) {
80 		if (argv->a_type == MDB_TYPE_CHAR || argc > 1)
81 			return (DCMD_USAGE);
82 
83 		if (argv->a_type == MDB_TYPE_STRING)
84 			arg = (void *)mdb_strtoull(argv->a_un.a_str);
85 		else
86 			arg = (void *)argv->a_un.a_val;
87 	}
88 
89 	(void) mdb_amd64_kvm_stack_iter(mdb.m_target, grp, func, arg);
90 	return (DCMD_OK);
91 }
92 
93 int
94 kt_stack(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
95 {
96 	return (kt_stack_common(addr, flags, argc, argv, mdb_amd64_kvm_frame));
97 }
98 
99 int
100 kt_stackv(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
101 {
102 	return (kt_stack_common(addr, flags, argc, argv, mdb_amd64_kvm_framev));
103 }
104 
105 const mdb_tgt_ops_t kt_amd64_ops = {
106 	kt_setflags,				/* t_setflags */
107 	kt_setcontext,				/* t_setcontext */
108 	kt_activate,				/* t_activate */
109 	kt_deactivate,				/* t_deactivate */
110 	(void (*)()) mdb_tgt_nop,		/* t_periodic */
111 	kt_destroy,				/* t_destroy */
112 	kt_name,				/* t_name */
113 	(const char *(*)()) mdb_conf_isa,	/* t_isa */
114 	kt_platform,				/* t_platform */
115 	kt_uname,				/* t_uname */
116 	kt_dmodel,				/* t_dmodel */
117 	kt_aread,				/* t_aread */
118 	kt_awrite,				/* t_awrite */
119 	kt_vread,				/* t_vread */
120 	kt_vwrite,				/* t_vwrite */
121 	kt_pread,				/* t_pread */
122 	kt_pwrite,				/* t_pwrite */
123 	kt_fread,				/* t_fread */
124 	kt_fwrite,				/* t_fwrite */
125 	(ssize_t (*)()) mdb_tgt_notsup,		/* t_ioread */
126 	(ssize_t (*)()) mdb_tgt_notsup,		/* t_iowrite */
127 	kt_vtop,				/* t_vtop */
128 	kt_lookup_by_name,			/* t_lookup_by_name */
129 	kt_lookup_by_addr,			/* t_lookup_by_addr */
130 	kt_symbol_iter,				/* t_symbol_iter */
131 	kt_mapping_iter,			/* t_mapping_iter */
132 	kt_object_iter,				/* t_object_iter */
133 	kt_addr_to_map,				/* t_addr_to_map */
134 	kt_name_to_map,				/* t_name_to_map */
135 	kt_addr_to_ctf,				/* t_addr_to_ctf */
136 	kt_name_to_ctf,				/* t_name_to_ctf */
137 	kt_status,				/* t_status */
138 	(int (*)()) mdb_tgt_notsup,		/* t_run */
139 	(int (*)()) mdb_tgt_notsup,		/* t_step */
140 	(int (*)()) mdb_tgt_notsup,		/* t_step_out */
141 	(int (*)()) mdb_tgt_notsup,		/* t_step_branch */
142 	(int (*)()) mdb_tgt_notsup,		/* t_next */
143 	(int (*)()) mdb_tgt_notsup,		/* t_cont */
144 	(int (*)()) mdb_tgt_notsup,		/* t_signal */
145 	(int (*)()) mdb_tgt_null,		/* t_add_vbrkpt */
146 	(int (*)()) mdb_tgt_null,		/* t_add_sbrkpt */
147 	(int (*)()) mdb_tgt_null,		/* t_add_pwapt */
148 	(int (*)()) mdb_tgt_null,		/* t_add_vwapt */
149 	(int (*)()) mdb_tgt_null,		/* t_add_iowapt */
150 	(int (*)()) mdb_tgt_null,		/* t_add_sysenter */
151 	(int (*)()) mdb_tgt_null,		/* t_add_sysexit */
152 	(int (*)()) mdb_tgt_null,		/* t_add_signal */
153 	(int (*)()) mdb_tgt_null,		/* t_add_fault */
154 	kt_getareg,				/* t_getareg */
155 	kt_putareg,				/* t_putareg */
156 	mdb_amd64_kvm_stack_iter,		/* t_stack_iter */
157 };
158 
159 void
160 kt_regs_to_kregs(struct regs *regs, mdb_tgt_gregset_t *gregs)
161 {
162 	gregs->kregs[KREG_SAVFP] = regs->r_savfp;
163 	gregs->kregs[KREG_SAVPC] = regs->r_savpc;
164 	gregs->kregs[KREG_RDI] = regs->r_rdi;
165 	gregs->kregs[KREG_RSI] = regs->r_rsi;
166 	gregs->kregs[KREG_RDX] = regs->r_rdx;
167 	gregs->kregs[KREG_RCX] = regs->r_rcx;
168 	gregs->kregs[KREG_R8] = regs->r_r8;
169 	gregs->kregs[KREG_R9] = regs->r_r9;
170 	gregs->kregs[KREG_RAX] = regs->r_rax;
171 	gregs->kregs[KREG_RBX] = regs->r_rbx;
172 	gregs->kregs[KREG_RBP] = regs->r_rbp;
173 	gregs->kregs[KREG_R10] = regs->r_r10;
174 	gregs->kregs[KREG_R11] = regs->r_r11;
175 	gregs->kregs[KREG_R12] = regs->r_r12;
176 	gregs->kregs[KREG_R13] = regs->r_r13;
177 	gregs->kregs[KREG_R14] = regs->r_r14;
178 	gregs->kregs[KREG_R15] = regs->r_r15;
179 	gregs->kregs[KREG_DS] = regs->r_ds;
180 	gregs->kregs[KREG_ES] = regs->r_es;
181 	gregs->kregs[KREG_FS] = regs->r_fs;
182 	gregs->kregs[KREG_GS] = regs->r_gs;
183 	gregs->kregs[KREG_TRAPNO] = regs->r_trapno;
184 	gregs->kregs[KREG_ERR] = regs->r_err;
185 	gregs->kregs[KREG_RIP] = regs->r_rip;
186 	gregs->kregs[KREG_CS] = regs->r_cs;
187 	gregs->kregs[KREG_RFLAGS] = regs->r_rfl;
188 	gregs->kregs[KREG_RSP] = regs->r_rsp;
189 	gregs->kregs[KREG_SS] = regs->r_ss;
190 }
191 
192 void
193 kt_amd64_init(mdb_tgt_t *t)
194 {
195 	kt_data_t *kt = t->t_data;
196 	panic_data_t pd;
197 	struct regs regs;
198 	uintptr_t addr;
199 
200 	/*
201 	 * Initialize the machine-dependent parts of the kernel target
202 	 * structure.  Once this is complete and we fill in the ops
203 	 * vector, the target is now fully constructed and we can use
204 	 * the target API itself to perform the rest of our initialization.
205 	 */
206 	kt->k_rds = mdb_amd64_kregs;
207 	kt->k_regs = mdb_zalloc(sizeof (mdb_tgt_gregset_t), UM_SLEEP);
208 	kt->k_regsize = sizeof (mdb_tgt_gregset_t);
209 	kt->k_dcmd_regs = kt_regs;
210 	kt->k_dcmd_stack = kt_stack;
211 	kt->k_dcmd_stackv = kt_stackv;
212 	kt->k_dcmd_stackr = kt_stackv;
213 	kt->k_dcmd_cpustack = kt_cpustack;
214 	kt->k_dcmd_cpuregs = kt_cpuregs;
215 
216 	t->t_ops = &kt_amd64_ops;
217 
218 	(void) mdb_dis_select("amd64");
219 
220 	/*
221 	 * Lookup the symbols corresponding to subroutines in locore.s where
222 	 * we expect a saved regs structure to be pushed on the stack.  When
223 	 * performing stack tracebacks we will attempt to detect interrupt
224 	 * frames by comparing the %eip value to these symbols.
225 	 */
226 	(void) mdb_tgt_lookup_by_name(t, MDB_TGT_OBJ_EXEC,
227 	    "cmnint", &kt->k_intr_sym, NULL);
228 
229 	(void) mdb_tgt_lookup_by_name(t, MDB_TGT_OBJ_EXEC,
230 	    "cmntrap", &kt->k_trap_sym, NULL);
231 
232 	/*
233 	 * Don't attempt to load any thread or register information if
234 	 * we're examining the live operating system.
235 	 */
236 	if (kt->k_symfile != NULL && strcmp(kt->k_symfile, "/dev/ksyms") == 0)
237 		return;
238 
239 	/*
240 	 * If the panicbuf symbol is present and we can consume a panicbuf
241 	 * header of the appropriate version from this address, then we can
242 	 * initialize our current register set based on its contents.
243 	 * Prior to the re-structuring of panicbuf, our only register data
244 	 * was the panic_regs label_t, into which a setjmp() was performed,
245 	 * or the panic_reg register pointer, which was only non-zero if
246 	 * the system panicked as a result of a trap calling die().
247 	 */
248 	if (mdb_tgt_readsym(t, MDB_TGT_AS_VIRT, &pd, sizeof (pd),
249 	    MDB_TGT_OBJ_EXEC, "panicbuf") == sizeof (pd) &&
250 	    pd.pd_version == PANICBUFVERS) {
251 
252 		size_t pd_size = MIN(PANICBUFSIZE, pd.pd_msgoff);
253 		panic_data_t *pdp = mdb_zalloc(pd_size, UM_SLEEP);
254 		uint_t i, n;
255 
256 		(void) mdb_tgt_readsym(t, MDB_TGT_AS_VIRT, pdp, pd_size,
257 		    MDB_TGT_OBJ_EXEC, "panicbuf");
258 
259 		n = (pd_size - (sizeof (panic_data_t) -
260 		    sizeof (panic_nv_t))) / sizeof (panic_nv_t);
261 
262 		for (i = 0; i < n; i++) {
263 			(void) kt_putareg(t, kt->k_tid,
264 			    pdp->pd_nvdata[i].pnv_name,
265 			    pdp->pd_nvdata[i].pnv_value);
266 		}
267 
268 		mdb_free(pdp, pd_size);
269 
270 		return;
271 	};
272 
273 	if (mdb_tgt_readsym(t, MDB_TGT_AS_VIRT, &addr, sizeof (addr),
274 	    MDB_TGT_OBJ_EXEC, "panic_reg") == sizeof (addr) && addr != NULL &&
275 	    mdb_tgt_vread(t, &regs, sizeof (regs), addr) == sizeof (regs)) {
276 		kt_regs_to_kregs(&regs, kt->k_regs);
277 		return;
278 	}
279 
280 	/*
281 	 * If we can't read any panic regs, then our final try is for any CPU
282 	 * context that may have been stored (for example, in Xen core dumps).
283 	 */
284 	if (kt_kvmregs(t, 0, kt->k_regs) == 0)
285 		return;
286 
287 	warn("failed to read panicbuf and panic_reg -- "
288 	    "current register set will be unavailable\n");
289 }
290