xref: /illumos-gate/usr/src/cmd/mdb/sparc/mdb/kvm_v9dep.c (revision 0597f6f3)
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 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  *
25  * Copyright 2018 Joyent, Inc.
26  */
27 
28 /*
29  * Libkvm Kernel Target SPARC v9 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.  The SPARC v9
33  * ISA code is actually compiled into *both* the sparcv7 and sparcv9 MDB
34  * binaries because we need to deal with the sparcv9 CPU registers when
35  * debugging a 32-bit crash dump from a kernel running on a sparcv9 CPU.
36  */
37 
38 #ifndef __sparcv9cpu
39 #define	__sparcv9cpu
40 #endif
41 
42 #include <sys/types.h>
43 #include <sys/machtypes.h>
44 #include <sys/regset.h>
45 #include <sys/frame.h>
46 #include <sys/stack.h>
47 #include <sys/sysmacros.h>
48 #include <sys/panic.h>
49 #include <strings.h>
50 
51 #include <mdb/mdb_target_impl.h>
52 #include <mdb/mdb_disasm.h>
53 #include <mdb/mdb_modapi.h>
54 #include <mdb/mdb_conf.h>
55 #include <mdb/mdb_kreg_impl.h>
56 #include <mdb/mdb_v9util.h>
57 #include <mdb/mdb_kvm.h>
58 #include <mdb/mdb_err.h>
59 #include <mdb/mdb_debug.h>
60 #include <mdb/mdb.h>
61 
62 #ifndef STACK_BIAS
63 #define	STACK_BIAS	0
64 #endif
65 
66 static int
kt_getareg(mdb_tgt_t * t,mdb_tgt_tid_t tid,const char * rname,mdb_tgt_reg_t * rp)67 kt_getareg(mdb_tgt_t *t, mdb_tgt_tid_t tid,
68     const char *rname, mdb_tgt_reg_t *rp)
69 {
70 	const mdb_tgt_regdesc_t *rdp;
71 	kt_data_t *kt = t->t_data;
72 
73 	if (tid != kt->k_tid)
74 		return (set_errno(EMDB_NOREGS));
75 
76 	for (rdp = kt->k_rds; rdp->rd_name != NULL; rdp++) {
77 		if (strcmp(rname, rdp->rd_name) == 0) {
78 			*rp = kt->k_regs->kregs[rdp->rd_num];
79 			return (0);
80 		}
81 	}
82 
83 	return (set_errno(EMDB_BADREG));
84 }
85 
86 static int
kt_putareg(mdb_tgt_t * t,mdb_tgt_tid_t tid,const char * rname,mdb_tgt_reg_t r)87 kt_putareg(mdb_tgt_t *t, mdb_tgt_tid_t tid, const char *rname, mdb_tgt_reg_t r)
88 {
89 	const mdb_tgt_regdesc_t *rdp;
90 	kt_data_t *kt = t->t_data;
91 
92 	if (tid != kt->k_tid)
93 		return (set_errno(EMDB_NOREGS));
94 
95 	for (rdp = kt->k_rds; rdp->rd_name != NULL; rdp++) {
96 		if (strcmp(rname, rdp->rd_name) == 0) {
97 			kt->k_regs->kregs[rdp->rd_num] = r;
98 			return (0);
99 		}
100 	}
101 
102 	return (set_errno(EMDB_BADREG));
103 }
104 
105 	/*
106 	 * - If we got a pc, invoke the call back function starting
107 	 *   with gsp.
108 	 * - If we got a saved pc (%i7), invoke the call back function
109 	 *   starting with the first register window.
110 	 * - If we got neither a pc nor a saved pc, invoke the call back
111 	 *   function starting with the second register window.
112 	 */
113 
114 /*ARGSUSED*/
115 static int
kt_regs(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)116 kt_regs(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
117 {
118 	mdb_v9printregs((const mdb_tgt_gregset_t *)addr);
119 	return (DCMD_OK);
120 }
121 
122 static int
kt_stack_common(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv,mdb_tgt_stack_f * func,kreg_t saved_pc)123 kt_stack_common(uintptr_t addr, uint_t flags, int argc,
124     const mdb_arg_t *argv, mdb_tgt_stack_f *func, kreg_t saved_pc)
125 {
126 	kt_data_t *kt = mdb.m_target->t_data;
127 	void *arg = (void *)(uintptr_t)mdb.m_nargs;
128 	mdb_tgt_gregset_t gregs, *grp;
129 
130 	if (flags & DCMD_ADDRSPEC) {
131 		bzero(&gregs, sizeof (gregs));
132 		gregs.kregs[KREG_FP] = addr;
133 		gregs.kregs[KREG_I7] = saved_pc;
134 		grp = &gregs;
135 	} else
136 		grp = kt->k_regs;
137 
138 	if (argc != 0) {
139 		if (argv->a_type == MDB_TYPE_CHAR || argc > 1)
140 			return (DCMD_USAGE);
141 
142 		if (argv->a_type == MDB_TYPE_STRING)
143 			arg = (void *)(uintptr_t)(uint_t)
144 			    mdb_strtoull(argv->a_un.a_str);
145 		else
146 			arg = (void *)(uintptr_t)(uint_t)argv->a_un.a_val;
147 	}
148 
149 	(void) mdb_kvm_v9stack_iter(mdb.m_target, grp, func, arg);
150 	return (DCMD_OK);
151 }
152 
153 static int
kt_stack(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)154 kt_stack(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
155 {
156 	return (kt_stack_common(addr, flags, argc, argv, mdb_kvm_v9frame, 0));
157 }
158 
159 static int
kt_stackv(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)160 kt_stackv(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
161 {
162 	return (kt_stack_common(addr, flags, argc, argv, mdb_kvm_v9framev, 0));
163 }
164 
165 static int
kt_stackr(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)166 kt_stackr(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
167 {
168 	/*
169 	 * Force printing of first register window by setting the
170 	 * saved pc (%i7) to PC_FAKE.
171 	 */
172 	return (kt_stack_common(addr, flags, argc, argv, mdb_kvm_v9framer,
173 	    PC_FAKE));
174 }
175 
176 /*ARGSUSED*/
177 static int
kt_notsup(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)178 kt_notsup(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
179 {
180 	errno = EMDB_TGTNOTSUP;
181 	return (DCMD_ERR);
182 }
183 
184 const mdb_tgt_ops_t kt_sparcv9_ops = {
185 	kt_setflags,				/* t_setflags */
186 	kt_setcontext,				/* t_setcontext */
187 	kt_activate,				/* t_activate */
188 	kt_deactivate,				/* t_deactivate */
189 	(void (*)())(uintptr_t)mdb_tgt_nop,		/* t_periodic */
190 	kt_destroy,				/* t_destroy */
191 	kt_name,				/* t_name */
192 	(const char *(*)())mdb_conf_isa,	/* t_isa */
193 	kt_platform,				/* t_platform */
194 	kt_uname,				/* t_uname */
195 	kt_dmodel,				/* t_dmodel */
196 	kt_aread,				/* t_aread */
197 	kt_awrite,				/* t_awrite */
198 	kt_vread,				/* t_vread */
199 	kt_vwrite,				/* t_vwrite */
200 	kt_pread,				/* t_pread */
201 	kt_pwrite,				/* t_pwrite */
202 	kt_fread,				/* t_fread */
203 	kt_fwrite,				/* t_fwrite */
204 	(ssize_t (*)())mdb_tgt_notsup,		/* t_ioread */
205 	(ssize_t (*)())mdb_tgt_notsup,		/* t_iowrite */
206 	kt_vtop,				/* t_vtop */
207 	kt_lookup_by_name,			/* t_lookup_by_name */
208 	kt_lookup_by_addr,			/* t_lookup_by_addr */
209 	kt_symbol_iter,				/* t_symbol_iter */
210 	kt_mapping_iter,			/* t_mapping_iter */
211 	kt_object_iter,				/* t_object_iter */
212 	kt_addr_to_map,				/* t_addr_to_map */
213 	kt_name_to_map,				/* t_name_to_map */
214 	kt_addr_to_ctf,				/* t_addr_to_ctf */
215 	kt_name_to_ctf,				/* t_name_to_ctf */
216 	kt_status,				/* t_status */
217 	(int (*)())(uintptr_t)mdb_tgt_notsup,	/* t_run */
218 	(int (*)())(uintptr_t)mdb_tgt_notsup,	/* t_step */
219 	(int (*)())(uintptr_t)mdb_tgt_notsup,	/* t_step_out */
220 	(int (*)())(uintptr_t)mdb_tgt_notsup,	/* t_next */
221 	(int (*)())(uintptr_t)mdb_tgt_notsup,	/* t_cont */
222 	(int (*)())(uintptr_t)mdb_tgt_notsup,	/* t_signal */
223 	(int (*)())(uintptr_t)mdb_tgt_null,	/* t_add_vbrkpt */
224 	(int (*)())(uintptr_t)mdb_tgt_null,	/* t_add_sbrkpt */
225 	(int (*)())(uintptr_t)mdb_tgt_null,	/* t_add_pwapt */
226 	(int (*)())(uintptr_t)mdb_tgt_null,	/* t_add_iowapt */
227 	(int (*)())(uintptr_t)mdb_tgt_null,	/* t_add_vwapt */
228 	(int (*)())(uintptr_t)mdb_tgt_null,	/* t_add_sysenter */
229 	(int (*)())(uintptr_t)mdb_tgt_null,	/* t_add_sysexit */
230 	(int (*)())(uintptr_t)mdb_tgt_null,	/* t_add_signal */
231 	(int (*)())(uintptr_t)mdb_tgt_null,	/* t_add_fault */
232 	kt_getareg,				/* t_getareg */
233 	kt_putareg,				/* t_putareg */
234 	mdb_kvm_v9stack_iter,			/* t_stack_iter */
235 	(int (*)())(uintptr_t)mdb_tgt_notsup	/* t_auxv */
236 };
237 
238 void
kt_sparcv9_init(mdb_tgt_t * t)239 kt_sparcv9_init(mdb_tgt_t *t)
240 {
241 	kt_data_t *kt = t->t_data;
242 
243 	struct rwindow rwin;
244 	panic_data_t pd;
245 	label_t label;
246 	kreg_t *kregs;
247 
248 	uint64_t tick;
249 	uint32_t pil;
250 
251 	/*
252 	 * Initialize the machine-dependent parts of the kernel target
253 	 * structure.  Once this is complete and we fill in the ops
254 	 * vector, the target is now fully constructed and we can use
255 	 * the target API itself to perform the rest of our initialization.
256 	 */
257 	kt->k_rds = mdb_sparcv9_kregs;
258 	kt->k_regs = mdb_zalloc(sizeof (mdb_tgt_gregset_t), UM_SLEEP);
259 	kt->k_regsize = sizeof (mdb_tgt_gregset_t);
260 	kt->k_dcmd_regs = kt_regs;
261 	kt->k_dcmd_stack = kt_stack;
262 	kt->k_dcmd_stackv = kt_stackv;
263 	kt->k_dcmd_stackr = kt_stackr;
264 	kt->k_dcmd_cpustack = kt_notsup;
265 	kt->k_dcmd_cpuregs = kt_notsup;
266 
267 	t->t_ops = &kt_sparcv9_ops;
268 	kregs = kt->k_regs->kregs;
269 
270 	(void) mdb_dis_select("v9plus");
271 
272 	/*
273 	 * Don't attempt to load any thread or register information if
274 	 * we're examining the live operating system.
275 	 */
276 	if (strcmp(kt->k_symfile, "/dev/ksyms") == 0)
277 		return;
278 
279 	/*
280 	 * If the panicbuf symbol is present and we can consume a panicbuf
281 	 * header of the appropriate version from this address, then
282 	 * we can initialize our current register set based on its contents:
283 	 */
284 	if (mdb_tgt_readsym(t, MDB_TGT_AS_VIRT, &pd, sizeof (pd),
285 	    MDB_TGT_OBJ_EXEC, "panicbuf") == sizeof (pd) &&
286 	    pd.pd_version == PANICBUFVERS) {
287 
288 		size_t pd_size = MIN(PANICBUFSIZE, pd.pd_msgoff);
289 		panic_data_t *pdp = mdb_zalloc(pd_size, UM_SLEEP);
290 		uint_t i, n;
291 
292 		(void) mdb_tgt_readsym(t, MDB_TGT_AS_VIRT, pdp, pd_size,
293 		    MDB_TGT_OBJ_EXEC, "panicbuf");
294 
295 		n = (pd_size - (sizeof (panic_data_t) -
296 		    sizeof (panic_nv_t))) / sizeof (panic_nv_t);
297 
298 		for (i = 0; i < n; i++) {
299 			const char *name = pdp->pd_nvdata[i].pnv_name;
300 			uint64_t value = pdp->pd_nvdata[i].pnv_value;
301 
302 			if (strcmp(name, "tstate") == 0) {
303 				kregs[KREG_CCR] = KREG_TSTATE_CCR(value);
304 				kregs[KREG_ASI] = KREG_TSTATE_ASI(value);
305 				kregs[KREG_PSTATE] = KREG_TSTATE_PSTATE(value);
306 				kregs[KREG_CWP] = KREG_TSTATE_CWP(value);
307 			} else
308 				(void) kt_putareg(t, kt->k_tid, name, value);
309 		}
310 
311 		mdb_free(pdp, pd_size);
312 	}
313 
314 	/*
315 	 * Prior to the re-structuring of panicbuf, our only register data
316 	 * was the panic_regs label_t, into which a setjmp() was performed.
317 	 */
318 	if (kregs[KREG_PC] == 0 && kregs[KREG_SP] == 0 &&
319 	    mdb_tgt_readsym(t, MDB_TGT_AS_VIRT, &label, sizeof (label),
320 	    MDB_TGT_OBJ_EXEC, "panic_regs") == sizeof (label)) {
321 
322 		kregs[KREG_PC] = label.val[0];
323 		kregs[KREG_SP] = label.val[1];
324 	}
325 
326 	/*
327 	 * If we can read a saved register window from the stack at %sp,
328 	 * we can also fill in the locals and inputs.
329 	 */
330 	if (kregs[KREG_SP] != 0 && mdb_tgt_vread(t, &rwin, sizeof (rwin),
331 	    kregs[KREG_SP] + STACK_BIAS) == sizeof (rwin)) {
332 
333 		kregs[KREG_L0] = rwin.rw_local[0];
334 		kregs[KREG_L1] = rwin.rw_local[1];
335 		kregs[KREG_L2] = rwin.rw_local[2];
336 		kregs[KREG_L3] = rwin.rw_local[3];
337 		kregs[KREG_L4] = rwin.rw_local[4];
338 		kregs[KREG_L5] = rwin.rw_local[5];
339 		kregs[KREG_L6] = rwin.rw_local[6];
340 		kregs[KREG_L7] = rwin.rw_local[7];
341 
342 		kregs[KREG_I0] = rwin.rw_in[0];
343 		kregs[KREG_I1] = rwin.rw_in[1];
344 		kregs[KREG_I2] = rwin.rw_in[2];
345 		kregs[KREG_I3] = rwin.rw_in[3];
346 		kregs[KREG_I4] = rwin.rw_in[4];
347 		kregs[KREG_I5] = rwin.rw_in[5];
348 		kregs[KREG_I6] = rwin.rw_in[6];
349 		kregs[KREG_I7] = rwin.rw_in[7];
350 
351 	} else if (kregs[KREG_SP] != 0) {
352 		warn("failed to read rwindow at %p -- current "
353 		    "frame inputs will be unavailable\n",
354 		    (void *)(uintptr_t)(kregs[KREG_SP] + STACK_BIAS));
355 	}
356 
357 	/*
358 	 * The panic_ipl variable records the IPL of the panic CPU,
359 	 * which on sparcv9 is the %pil register's value.
360 	 */
361 	if (mdb_tgt_readsym(t, MDB_TGT_AS_VIRT, &pil, sizeof (pil),
362 	    MDB_TGT_OBJ_EXEC, "panic_ipl") == sizeof (pil))
363 		kregs[KREG_PIL] = pil;
364 
365 	/*
366 	 * The panic_tick variable records %tick at the approximate
367 	 * time of the panic in a DEBUG kernel.
368 	 */
369 	if (mdb_tgt_readsym(t, MDB_TGT_AS_VIRT, &tick, sizeof (tick),
370 	    MDB_TGT_OBJ_EXEC, "panic_tick") == sizeof (tick))
371 		kregs[KREG_TICK] = tick;
372 }
373