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