xref: /titanic_41/usr/src/cmd/mdb/sparc/mdb/kvm_v9dep.c (revision 7b9b3bf3fd4f7bfad91fce91e3e9fba62ac85c77)
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 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
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 (*)()) 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 (*)()) mdb_tgt_notsup,		/* t_run */
218 	(int (*)()) mdb_tgt_notsup,		/* t_step */
219 	(int (*)()) mdb_tgt_notsup,		/* t_step_out */
220 	(int (*)()) mdb_tgt_notsup,		/* t_step_branch */
221 	(int (*)()) mdb_tgt_notsup,		/* t_next */
222 	(int (*)()) mdb_tgt_notsup,		/* t_cont */
223 	(int (*)()) mdb_tgt_notsup,		/* t_signal */
224 	(int (*)()) mdb_tgt_null,		/* t_add_vbrkpt */
225 	(int (*)()) mdb_tgt_null,		/* t_add_sbrkpt */
226 	(int (*)()) mdb_tgt_null,		/* t_add_pwapt */
227 	(int (*)()) mdb_tgt_null,		/* t_add_iowapt */
228 	(int (*)()) mdb_tgt_null,		/* t_add_vwapt */
229 	(int (*)()) mdb_tgt_null,		/* t_add_sysenter */
230 	(int (*)()) mdb_tgt_null,		/* t_add_sysexit */
231 	(int (*)()) mdb_tgt_null,		/* t_add_signal */
232 	(int (*)()) mdb_tgt_null,		/* t_add_fault */
233 	kt_getareg,				/* t_getareg */
234 	kt_putareg,				/* t_putareg */
235 	mdb_kvm_v9stack_iter,			/* t_stack_iter */
236 	(int (*)()) mdb_tgt_notsup		/* t_auxv */
237 };
238 
239 void
kt_sparcv9_init(mdb_tgt_t * t)240 kt_sparcv9_init(mdb_tgt_t *t)
241 {
242 	kt_data_t *kt = t->t_data;
243 
244 	struct rwindow rwin;
245 	panic_data_t pd;
246 	label_t label;
247 	kreg_t *kregs;
248 
249 	uint64_t tick;
250 	uint32_t pil;
251 
252 	/*
253 	 * Initialize the machine-dependent parts of the kernel target
254 	 * structure.  Once this is complete and we fill in the ops
255 	 * vector, the target is now fully constructed and we can use
256 	 * the target API itself to perform the rest of our initialization.
257 	 */
258 	kt->k_rds = mdb_sparcv9_kregs;
259 	kt->k_regs = mdb_zalloc(sizeof (mdb_tgt_gregset_t), UM_SLEEP);
260 	kt->k_regsize = sizeof (mdb_tgt_gregset_t);
261 	kt->k_dcmd_regs = kt_regs;
262 	kt->k_dcmd_stack = kt_stack;
263 	kt->k_dcmd_stackv = kt_stackv;
264 	kt->k_dcmd_stackr = kt_stackr;
265 	kt->k_dcmd_cpustack = kt_notsup;
266 	kt->k_dcmd_cpuregs = kt_notsup;
267 
268 	t->t_ops = &kt_sparcv9_ops;
269 	kregs = kt->k_regs->kregs;
270 
271 	(void) mdb_dis_select("v9plus");
272 
273 	/*
274 	 * Don't attempt to load any thread or register information if
275 	 * we're examining the live operating system.
276 	 */
277 	if (strcmp(kt->k_symfile, "/dev/ksyms") == 0)
278 		return;
279 
280 	/*
281 	 * If the panicbuf symbol is present and we can consume a panicbuf
282 	 * header of the appropriate version from this address, then
283 	 * we can initialize our current register set based on its contents:
284 	 */
285 	if (mdb_tgt_readsym(t, MDB_TGT_AS_VIRT, &pd, sizeof (pd),
286 	    MDB_TGT_OBJ_EXEC, "panicbuf") == sizeof (pd) &&
287 	    pd.pd_version == PANICBUFVERS) {
288 
289 		size_t pd_size = MIN(PANICBUFSIZE, pd.pd_msgoff);
290 		panic_data_t *pdp = mdb_zalloc(pd_size, UM_SLEEP);
291 		uint_t i, n;
292 
293 		(void) mdb_tgt_readsym(t, MDB_TGT_AS_VIRT, pdp, pd_size,
294 		    MDB_TGT_OBJ_EXEC, "panicbuf");
295 
296 		n = (pd_size - (sizeof (panic_data_t) -
297 		    sizeof (panic_nv_t))) / sizeof (panic_nv_t);
298 
299 		for (i = 0; i < n; i++) {
300 			const char *name = pdp->pd_nvdata[i].pnv_name;
301 			uint64_t value = pdp->pd_nvdata[i].pnv_value;
302 
303 			if (strcmp(name, "tstate") == 0) {
304 				kregs[KREG_CCR] = KREG_TSTATE_CCR(value);
305 				kregs[KREG_ASI] = KREG_TSTATE_ASI(value);
306 				kregs[KREG_PSTATE] = KREG_TSTATE_PSTATE(value);
307 				kregs[KREG_CWP] = KREG_TSTATE_CWP(value);
308 			} else
309 				(void) kt_putareg(t, kt->k_tid, name, value);
310 		}
311 
312 		mdb_free(pdp, pd_size);
313 	}
314 
315 	/*
316 	 * Prior to the re-structuring of panicbuf, our only register data
317 	 * was the panic_regs label_t, into which a setjmp() was performed.
318 	 */
319 	if (kregs[KREG_PC] == 0 && kregs[KREG_SP] == 0 &&
320 	    mdb_tgt_readsym(t, MDB_TGT_AS_VIRT, &label, sizeof (label),
321 	    MDB_TGT_OBJ_EXEC, "panic_regs") == sizeof (label)) {
322 
323 		kregs[KREG_PC] = label.val[0];
324 		kregs[KREG_SP] = label.val[1];
325 	}
326 
327 	/*
328 	 * If we can read a saved register window from the stack at %sp,
329 	 * we can also fill in the locals and inputs.
330 	 */
331 	if (kregs[KREG_SP] != 0 && mdb_tgt_vread(t, &rwin, sizeof (rwin),
332 	    kregs[KREG_SP] + STACK_BIAS) == sizeof (rwin)) {
333 
334 		kregs[KREG_L0] = rwin.rw_local[0];
335 		kregs[KREG_L1] = rwin.rw_local[1];
336 		kregs[KREG_L2] = rwin.rw_local[2];
337 		kregs[KREG_L3] = rwin.rw_local[3];
338 		kregs[KREG_L4] = rwin.rw_local[4];
339 		kregs[KREG_L5] = rwin.rw_local[5];
340 		kregs[KREG_L6] = rwin.rw_local[6];
341 		kregs[KREG_L7] = rwin.rw_local[7];
342 
343 		kregs[KREG_I0] = rwin.rw_in[0];
344 		kregs[KREG_I1] = rwin.rw_in[1];
345 		kregs[KREG_I2] = rwin.rw_in[2];
346 		kregs[KREG_I3] = rwin.rw_in[3];
347 		kregs[KREG_I4] = rwin.rw_in[4];
348 		kregs[KREG_I5] = rwin.rw_in[5];
349 		kregs[KREG_I6] = rwin.rw_in[6];
350 		kregs[KREG_I7] = rwin.rw_in[7];
351 
352 	} else if (kregs[KREG_SP] != 0) {
353 		warn("failed to read rwindow at %p -- current "
354 		    "frame inputs will be unavailable\n",
355 		    (void *)(uintptr_t)(kregs[KREG_SP] + STACK_BIAS));
356 	}
357 
358 	/*
359 	 * The panic_ipl variable records the IPL of the panic CPU,
360 	 * which on sparcv9 is the %pil register's value.
361 	 */
362 	if (mdb_tgt_readsym(t, MDB_TGT_AS_VIRT, &pil, sizeof (pil),
363 	    MDB_TGT_OBJ_EXEC, "panic_ipl") == sizeof (pil))
364 		kregs[KREG_PIL] = pil;
365 
366 	/*
367 	 * The panic_tick variable records %tick at the approximate
368 	 * time of the panic in a DEBUG kernel.
369 	 */
370 	if (mdb_tgt_readsym(t, MDB_TGT_AS_VIRT, &tick, sizeof (tick),
371 	    MDB_TGT_OBJ_EXEC, "panic_tick") == sizeof (tick))
372 		kregs[KREG_TICK] = tick;
373 }
374