xref: /illumos-gate/usr/src/uts/sun4v/os/mach_trap.c (revision 499fd601)
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 #include <sys/trap.h>
29 #include <sys/machtrap.h>
30 #include <sys/machsystm.h>
31 #include <sys/cpu_module.h>
32 #include <sys/panic.h>
33 #include <sys/uadmin.h>
34 #include <sys/kobj.h>
35 #include <sys/contract/process_impl.h>
36 #include <vm/hat_sfmmu.h>
37 #include <sys/reboot.h>
38 
39 #ifdef  TRAPTRACE
40 #include <sys/traptrace.h>
41 #endif
42 
43 void showregs(unsigned, struct regs *, caddr_t, uint_t);
44 
45 extern int tudebug;
46 
47 void
48 mmu_print_sfsr(uint_t sfsr)
49 {
50 	printf("MMU sfsr=%x:", sfsr);
51 	switch (X_FAULT_TYPE(sfsr)) {
52 	case FT_NONE:
53 		printf(" No error");
54 		break;
55 	case FT_PRIV:
56 		printf(" Privilege violation");
57 		break;
58 	case FT_SPEC_LD:
59 		printf(" Speculative load on E-bit page");
60 		break;
61 	case FT_ATOMIC_NC:
62 		printf(" Atomic to uncacheable page");
63 		break;
64 	case FT_ILL_ALT:
65 		printf(" Illegal lda or sta");
66 		break;
67 	case FT_NFO:
68 		printf(" Normal access to NFO page");
69 		break;
70 	case FT_RANGE:
71 		printf(" Data or instruction address out of range");
72 		break;
73 	default:
74 		printf(" Unknown error");
75 		break;
76 	}
77 
78 	printf(" context 0x%x", X_FAULT_CTX(sfsr));
79 	printf("\n");
80 }
81 
82 
83 /*
84  * Handle an asynchronous hardware error, i.e. an E-$ parity error.
85  * The policy is currently to send a hardware error contract event to
86  * the process's process contract and to kill the process.  Eventually
87  * we may want to instead send a special signal whose default
88  * disposition is to generate the contract event.
89  */
90 void
91 trap_async_hwerr(void)
92 {
93 	k_siginfo_t si;
94 	proc_t *p = ttoproc(curthread);
95 
96 	errorq_drain(ue_queue); /* flush pending async error messages */
97 
98 	contract_process_hwerr(p->p_ct_process, p);
99 
100 	bzero(&si, sizeof (k_siginfo_t));
101 	si.si_signo = SIGKILL;
102 	si.si_code = SI_NOINFO;
103 	trapsig(&si, 1);
104 }
105 
106 /*
107  * Handle bus error and bus timeout for a user process by sending SIGBUS
108  * The type is either ASYNC_BERR or ASYNC_BTO.
109  */
110 void
111 trap_async_berr_bto(int type, struct regs *rp)
112 {
113 	k_siginfo_t si;
114 
115 	errorq_drain(ue_queue); /* flush pending async error messages */
116 	bzero(&si, sizeof (k_siginfo_t));
117 
118 	si.si_signo = SIGBUS;
119 	si.si_code = (type == ASYNC_BERR ? BUS_OBJERR : BUS_ADRERR);
120 	si.si_addr = (caddr_t)rp->r_pc; /* AFAR unavailable - future RFE */
121 	si.si_errno = ENXIO;
122 
123 	trapsig(&si, 1);
124 }
125 
126 /*
127  * Print out debugging info.
128  */
129 /*ARGSUSED*/
130 void
131 showregs(uint_t type, struct regs *rp, caddr_t addr, uint_t mmu_fsr)
132 {
133 	int s;
134 
135 	s = spl7();
136 	type &= ~T_USER;
137 	printf("%s: ", PTOU(curproc)->u_comm);
138 
139 	switch (type) {
140 	case T_SYS_RTT_ALIGN:
141 	case T_ALIGNMENT:
142 		printf("alignment error:\n");
143 		break;
144 	case T_INSTR_EXCEPTION:
145 		printf("text access exception:\n");
146 		break;
147 	case T_DATA_EXCEPTION:
148 		printf("data access exception:\n");
149 		break;
150 	case T_PRIV_INSTR:
151 		printf("privileged instruction fault:\n");
152 		break;
153 	case T_UNIMP_INSTR:
154 		printf("illegal instruction fault:\n");
155 		break;
156 	case T_IDIV0:
157 		printf("integer divide zero trap:\n");
158 		break;
159 	case T_DIV0:
160 		printf("zero divide trap:\n");
161 		break;
162 	case T_INT_OVERFLOW:
163 		printf("integer overflow:\n");
164 		break;
165 	case T_BREAKPOINT:
166 		printf("breakpoint trap:\n");
167 		break;
168 	case T_TAG_OVERFLOW:
169 		printf("tag overflow:\n");
170 		break;
171 	default:
172 		if (type >= T_SOFTWARE_TRAP && type <= T_ESOFTWARE_TRAP)
173 			printf("software trap 0x%x\n", type - T_SOFTWARE_TRAP);
174 		else
175 			printf("trap type = 0x%x\n", type);
176 		break;
177 	}
178 	if (type == T_DATA_EXCEPTION || type == T_INSTR_EXCEPTION) {
179 		mmu_print_sfsr(mmu_fsr);
180 	} else if (addr) {
181 		printf("addr=0x%p\n", (void *)addr);
182 	}
183 
184 	printf("pid=%d, pc=0x%lx, sp=0x%llx, tstate=0x%llx, context=0x%x\n",
185 	    (ttoproc(curthread) && ttoproc(curthread)->p_pidp) ?
186 	    (ttoproc(curthread)->p_pid) : 0, rp->r_pc, rp->r_sp,
187 	    rp->r_tstate, sfmmu_getctx_sec());
188 	if (USERMODE(rp->r_tstate)) {
189 		printf("o0-o7: %llx, %llx, %llx, %llx, %llx, %llx, "
190 		    "%llx, %llx\n", rp->r_o0, rp->r_o1, rp->r_o2, rp->r_o3,
191 		    rp->r_o4, rp->r_o5, rp->r_o6, rp->r_o7);
192 	}
193 	printf("g1-g7: %llx, %llx, %llx, %llx, %llx, %llx, %llx\n",
194 	    rp->r_g1, rp->r_g2, rp->r_g3,
195 	    rp->r_g4, rp->r_g5, rp->r_g6, rp->r_g7);
196 
197 	if (tudebug > 1 && (boothowto & RB_DEBUG)) {
198 		debug_enter((char *)NULL);
199 	}
200 	splx(s);
201 }
202 
203 static void
204 ptl1_showtrap(ptl1_state_t *pstate)
205 {
206 	ptl1_regs_t *rp = &pstate->ptl1_regs;
207 	short i, j, maxtl = rp->ptl1_trap_regs[0].ptl1_tl;
208 	short curgl = rp->ptl1_gregs[0].ptl1_gl;
209 
210 	printf("%%tl %%tpc              %%tnpc             %%tstate"
211 	    "           %%tt\n");
212 
213 	for (i = maxtl - 1; i >= 0; i--) {
214 		ptl1_trapregs_t *ptp = &rp->ptl1_trap_regs[i];
215 		uint64_t tstate = ptp->ptl1_tstate;
216 		uint32_t gl, ccr, asi, cwp, pstate;
217 
218 		cwp = (tstate >> TSTATE_CWP_SHIFT) & TSTATE_CWP_MASK;
219 		pstate = (tstate >> TSTATE_PSTATE_SHIFT) & TSTATE_PSTATE_MASK;
220 		asi = (tstate >> TSTATE_ASI_SHIFT) & TSTATE_ASI_MASK;
221 		ccr = (tstate >> TSTATE_CCR_SHIFT) & TSTATE_CCR_MASK;
222 		gl = (tstate >> TSTATE_GL_SHIFT) & TSTATE_GL_MASK;
223 
224 		printf(" %d  %016" PRIx64 "  %016" PRIx64 "  %010" PRIx64
225 		    "        %03x\n", ptp->ptl1_tl, ptp->ptl1_tpc,
226 		    ptp->ptl1_tnpc, tstate, ptp->ptl1_tt);
227 		printf("    %%gl: %02x  %%ccr: %02x  %%asi: %02x  %%cwp: %x  "
228 		    "%%pstate: %b\n", gl, ccr, asi, cwp, pstate, PSTATE_BITS);
229 	}
230 
231 	/*
232 	 * ptl1_gregs[] array holds global registers for GL 0 through
233 	 * current GL. Note that the current GL global registers are
234 	 * always stored at index 0 in the ptl1_gregs[] array.
235 	 */
236 	for (i = 0; i <= curgl; i++) {
237 		ptl1_gregs_t *pgp = &rp->ptl1_gregs[i];
238 
239 		printf("    %%gl: %02" PRIx64 "\n", pgp->ptl1_gl);
240 		printf("%%g0-3: %016x %016" PRIx64 " %016" PRIx64 " %016"
241 		    PRIx64 "\n", 0, pgp->ptl1_g1, pgp->ptl1_g2, pgp->ptl1_g3);
242 		printf("%%g4-7: %016" PRIx64 " %016" PRIx64 " %016"
243 		    PRIx64 " %016" PRIx64 "\n", pgp->ptl1_g4, pgp->ptl1_g5,
244 		    pgp->ptl1_g6, pgp->ptl1_g7);
245 	}
246 
247 	i = rp->ptl1_cwp;
248 	j = rp->ptl1_canrestore;
249 	for (; j >= 0; i--, j--) {
250 		struct rwindow *wp;
251 		ulong_t off;
252 		char *sym;
253 
254 		if (i < 0)
255 			i += MAXWIN;
256 
257 		wp = &rp->ptl1_rwindow[i];
258 
259 		if ((sym = kobj_getsymname(wp->rw_in[7], &off)) != NULL) {
260 			printf("Register window %d, caller %s+%lx\n",
261 			    i, sym, off);
262 		} else {
263 			printf("Register window %d, caller %lx\n",
264 			    i, wp->rw_in[7]);
265 		}
266 
267 		if (i == rp->ptl1_cwp) {
268 			struct rwindow *nwp;
269 
270 			if (i == MAXWIN - 1)
271 				nwp = &rp->ptl1_rwindow[0];
272 			else
273 				nwp = &rp->ptl1_rwindow[i+1];
274 			printf("%%o0-3: %016lx %016lx %016lx %016lx\n"
275 			    "%%o4-7: %016lx %016lx %016lx %016lx\n",
276 			    nwp->rw_in[0], nwp->rw_in[1], nwp->rw_in[2],
277 			    nwp->rw_in[3], nwp->rw_in[4], nwp->rw_in[5],
278 			    nwp->rw_in[6], nwp->rw_in[7]);
279 		}
280 		printf("%%l0-3: %016lx %016lx %016lx %016lx\n"
281 		    "%%l4-7: %016lx %016lx %016lx %016lx\n",
282 		    wp->rw_local[0], wp->rw_local[1], wp->rw_local[2],
283 		    wp->rw_local[3], wp->rw_local[4], wp->rw_local[5],
284 		    wp->rw_local[6], wp->rw_local[7]);
285 
286 		printf("%%i0-3: %016lx %016lx %016lx %016lx\n"
287 		    "%%i4-7: %016lx %016lx %016lx %016lx\n",
288 		    wp->rw_in[0], wp->rw_in[1], wp->rw_in[2], wp->rw_in[3],
289 		    wp->rw_in[4], wp->rw_in[5], wp->rw_in[6], wp->rw_in[7]);
290 	}
291 }
292 
293 void
294 panic_showtrap(struct panic_trap_info *tip)
295 {
296 	ptl1_state_t *pstate = &CPU->cpu_m.ptl1_state;
297 	/*
298 	 * If ptl1_panic() was called, print out the information
299 	 * saved in the ptl1_state struture.
300 	 */
301 	if (pstate->ptl1_entry_count) {
302 		ptl1_showtrap(pstate);
303 		return;
304 	}
305 
306 	showregs(tip->trap_type, tip->trap_regs, tip->trap_addr,
307 	    tip->trap_mmu_fsr);
308 }
309 
310 static void
311 ptl1_savetrap(panic_data_t *pdp, ptl1_state_t *pstate)
312 {
313 	ptl1_regs_t *rp = &pstate->ptl1_regs;
314 	short i, maxtl = rp->ptl1_trap_regs[0].ptl1_tl;
315 	panic_nv_t *pnv = PANICNVGET(pdp);
316 	char name[PANICNVNAMELEN];
317 
318 	for (i = maxtl - 1; i >= 0; i--) {
319 		ptl1_trapregs_t *ptp = &rp->ptl1_trap_regs[i];
320 
321 		(void) snprintf(name, sizeof (name), "tl[%d]", i);
322 		PANICNVADD(pnv, name, ptp->ptl1_tl);
323 		(void) snprintf(name, sizeof (name), "tt[%d]", i);
324 		PANICNVADD(pnv, name, ptp->ptl1_tt);
325 		(void) snprintf(name, sizeof (name), "tpc[%d]", i);
326 		PANICNVADD(pnv, name, ptp->ptl1_tpc);
327 		(void) snprintf(name, sizeof (name), "tnpc[%d]", i);
328 		PANICNVADD(pnv, name, ptp->ptl1_tnpc);
329 		(void) snprintf(name, sizeof (name), "tstate[%d]", i);
330 		PANICNVADD(pnv, name, ptp->ptl1_tstate);
331 	}
332 
333 	PANICNVSET(pdp, pnv);
334 }
335 
336 void
337 panic_savetrap(panic_data_t *pdp, struct panic_trap_info *tip)
338 {
339 	panic_nv_t *pnv;
340 	ptl1_state_t *pstate = &CPU->cpu_m.ptl1_state;
341 	/*
342 	 * If ptl1_panic() was called, save the trap registers
343 	 * stored in the ptl1_state struture.
344 	 */
345 	if (pstate->ptl1_entry_count) {
346 		ptl1_savetrap(pdp, pstate);
347 		return;
348 	}
349 
350 	panic_saveregs(pdp, tip->trap_regs);
351 	pnv = PANICNVGET(pdp);
352 
353 	PANICNVADD(pnv, "sfsr", tip->trap_mmu_fsr);
354 	PANICNVADD(pnv, "sfar", tip->trap_addr);
355 	PANICNVADD(pnv, "tt", tip->trap_type);
356 
357 	PANICNVSET(pdp, pnv);
358 }
359