1*4bfca438Sphx /* $NetBSD: exception.c,v 1.1 2011/10/30 20:42:09 phx Exp $ */
2*4bfca438Sphx 
3*4bfca438Sphx /*-
4*4bfca438Sphx  * Copyright (c) 2011 Frank Wille.
5*4bfca438Sphx  * All rights reserved.
6*4bfca438Sphx  *
7*4bfca438Sphx  * Written by Frank Wille for The NetBSD Project.
8*4bfca438Sphx  *
9*4bfca438Sphx  * Redistribution and use in source and binary forms, with or without
10*4bfca438Sphx  * modification, are permitted provided that the following conditions
11*4bfca438Sphx  * are met:
12*4bfca438Sphx  * 1. Redistributions of source code must retain the above copyright
13*4bfca438Sphx  *    notice, this list of conditions and the following disclaimer.
14*4bfca438Sphx  * 2. Redistributions in binary form must reproduce the above copyright
15*4bfca438Sphx  *    notice, this list of conditions and the following disclaimer in the
16*4bfca438Sphx  *    documentation and/or other materials provided with the distribution.
17*4bfca438Sphx  *
18*4bfca438Sphx  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19*4bfca438Sphx  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20*4bfca438Sphx  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21*4bfca438Sphx  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22*4bfca438Sphx  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23*4bfca438Sphx  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24*4bfca438Sphx  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25*4bfca438Sphx  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26*4bfca438Sphx  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27*4bfca438Sphx  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28*4bfca438Sphx  * POSSIBILITY OF SUCH DAMAGE.
29*4bfca438Sphx  */
30*4bfca438Sphx 
31*4bfca438Sphx #include <sys/param.h>
32*4bfca438Sphx 
33*4bfca438Sphx #include <lib/libsa/stand.h>
34*4bfca438Sphx #include <lib/libsa/loadfile.h>
35*4bfca438Sphx #include <lib/libkern/libkern.h>
36*4bfca438Sphx 
37*4bfca438Sphx #include "globals.h"
38*4bfca438Sphx 
39*4bfca438Sphx #ifdef DEBUG
40*4bfca438Sphx struct cpu_state {
41*4bfca438Sphx 	uint32_t	gpr[32];
42*4bfca438Sphx 	uint32_t	cr, xer, lr, ctr;
43*4bfca438Sphx 	uint32_t	srr0, srr1, dar, dsisr;
44*4bfca438Sphx 	uint32_t	dmiss, dcmp, hash1, hash2;
45*4bfca438Sphx 	uint32_t	imiss, icmp, rpa;
46*4bfca438Sphx };
47*4bfca438Sphx 
48*4bfca438Sphx void init_vectors(void);	/* called from early startup */
49*4bfca438Sphx void exception_handler(unsigned, struct cpu_state *);
50*4bfca438Sphx 
51*4bfca438Sphx 
52*4bfca438Sphx void
init_vectors(void)53*4bfca438Sphx init_vectors(void)
54*4bfca438Sphx {
55*4bfca438Sphx 	extern uint8_t trap[], trap_end[];
56*4bfca438Sphx 	uint8_t *exc;
57*4bfca438Sphx 	uint32_t *ip;
58*4bfca438Sphx 
59*4bfca438Sphx 	/* install exception handlers */
60*4bfca438Sphx 	for (exc = (uint8_t *)0x100; exc <= (uint8_t *)0x1400; exc += 0x100)
61*4bfca438Sphx 		memcpy(exc, trap, trap_end - trap);
62*4bfca438Sphx 
63*4bfca438Sphx 	/* handle NULL-ptr calls from 0x0 to 0xfc */
64*4bfca438Sphx 	for (ip = 0; ip < (uint32_t *)0x100; ip++)
65*4bfca438Sphx 		*ip = 0x48000002 | (uint32_t)trap;
66*4bfca438Sphx 
67*4bfca438Sphx 	__syncicache(0, 0x1400);
68*4bfca438Sphx }
69*4bfca438Sphx 
70*4bfca438Sphx void
exception_handler(unsigned vector,struct cpu_state * st)71*4bfca438Sphx exception_handler(unsigned vector, struct cpu_state *st)
72*4bfca438Sphx {
73*4bfca438Sphx 	static bool in_exception = false;
74*4bfca438Sphx 	uint32_t *fp;
75*4bfca438Sphx 	int i;
76*4bfca438Sphx 
77*4bfca438Sphx 	if (vector > 0x1400)
78*4bfca438Sphx 		printf("\nCALL TO NULL POINTER FROM %08x\n", st->lr - 4);
79*4bfca438Sphx 	else
80*4bfca438Sphx 		printf("\nEXCEPTION VECTOR %04x\n", vector);
81*4bfca438Sphx 	if (in_exception) {
82*4bfca438Sphx 		printf("caused in exception handler! Restarting...\n");
83*4bfca438Sphx 		_rtt();
84*4bfca438Sphx 	}
85*4bfca438Sphx 	in_exception = true;
86*4bfca438Sphx 
87*4bfca438Sphx 	/* register dump */
88*4bfca438Sphx 	printf("\n SRR0=%08x SRR1=%08x   DAR=%08x DSISR=%08x\n"
89*4bfca438Sphx 	    "   CR=%08x  XER=%08x    LR=%08x   CTR=%08x\n"
90*4bfca438Sphx 	    "DMISS=%08x DCMP=%08x HASH1=%08x HASH2=%08x\n"
91*4bfca438Sphx 	    "IMISS=%08x ICMP=%08x   RPA=%08x\n\n",
92*4bfca438Sphx 	    st->srr0, st->srr1, st->dar, st->dsisr,
93*4bfca438Sphx 	    st->cr, st->xer, st->lr, st->ctr,
94*4bfca438Sphx 	    st->dmiss,st->dcmp,st->hash1,st->hash2,
95*4bfca438Sphx 	    st->imiss,st->icmp,st->rpa);
96*4bfca438Sphx 	for (i = 0; i < 32; i++) {
97*4bfca438Sphx 		if ((i & 7) == 0)
98*4bfca438Sphx 			printf("GPR%02d:", i);
99*4bfca438Sphx 		printf("%c%08x", (i & 7) == 4 ? ':' : ' ', st->gpr[i]);
100*4bfca438Sphx 		if ((i & 7) == 7)
101*4bfca438Sphx 			printf("\n");
102*4bfca438Sphx 	}
103*4bfca438Sphx 
104*4bfca438Sphx 	/* V.4-ABI stack frame back trace */
105*4bfca438Sphx 	printf("\nBacktrace:\n");
106*4bfca438Sphx 	i = 0;
107*4bfca438Sphx 	fp = (uint32_t *)st->gpr[1];
108*4bfca438Sphx 	while ((fp = *(uint32_t **)fp) != NULL && i++ < 10)
109*4bfca438Sphx 		printf("%p: called from %08x\n", fp, *(fp + 1) - 4);
110*4bfca438Sphx 
111*4bfca438Sphx 	printf("\nHit any key to reboot.\n");
112*4bfca438Sphx 	(void)getchar();
113*4bfca438Sphx 	_rtt();
114*4bfca438Sphx }
115*4bfca438Sphx 
116*4bfca438Sphx #endif /* DEBUG */
117