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