Root/panic.ccp

1#pypp 0
2// Iris: micro-kernel for a capability-based operating system.
3// panic.ccp: Stop running and try to notify the user of the problem.
4// Copyright 2009 Bas Wijnen <wijnen@debian.org>
5//
6// This program is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10//
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15//
16// You should have received a copy of the GNU General Public License
17// along with this program. If not, see <http://www.gnu.org/licenses/>.
18
19#define ARCH
20#include "kernel.hh"
21
22#ifdef USE_SERIAL
23void kdebug (unsigned ch):
24    while !(UART0_LSR & UARTLSR_TDRQ):
25    UART0_TDR = ch
26#else
27void kdebug (unsigned ch):
28    if !dbg_cap.valid () || dbg_code.l:
29        return
30    kCapability::Context c
31    c.data[0] = ch
32    ++dbg_code.l
33    dbg_cap->invoke (&c)
34    --dbg_code.l
35#endif
36
37void kdebug (char const *str):
38    while *str:
39        kdebug (*str++)
40
41void kdebug_num (unsigned num, unsigned digits):
42    char const *encode = "0123456789abcdef"
43    for unsigned i = 0; i < digits; ++i:
44        kdebug (encode[(num >> (4 * ((digits - 1) - i))) & 0xf])
45    return
46
47#if 1 || defined (NDEBUG)
48static void print_addr (char const *t, unsigned addr, bool last = false):
49    kdebug (t)
50    kdebug_num (addr)
51    #if 0
52    unsigned de = addr >> 21
53    unsigned te = (addr >> 12) & ((1 << 9) - 1)
54    if de < 0x400 && old_current && old_current->address_space->arch.directory && old_current->address_space->arch.directory[de]:
55        kdebug ("; EntryLo = ")
56        kdebug_num (old_current->address_space->arch.directory[de]->entrylo[te])
57    else:
58        kdebug ("; no directory or page table")
59    if old_current && de < 0x400:
60        cp0_set (CP0_ENTRY_HI, (addr & (PAGE_MASK << 1)) | old_current->address_space->arch.asid)
61        __asm__ volatile ("tlbp")
62        unsigned idx, hi
63        cp0_get (CP0_INDEX, idx)
64        kdebug ("; tlb index: ")
65        if idx & (1 << 31):
66            kdebug ("none\n")
67        else:
68            __asm__ volatile ("tlbr")
69            kdebug_num (idx, 2)
70            kdebug ("; EntryLo = ")
71            unsigned lo
72            cp0_get (CP0_ENTRY_LO0, lo)
73            kdebug_num (lo)
74            kdebug (":")
75            cp0_get (CP0_ENTRY_LO1, lo)
76            kdebug_num (lo)
77            kdebug ("\n")
78    else:
79        kdebug ('\n')
80    if addr >= 0x80000000 && addr < 0xc0000000:
81        for int i = -4; i < 4; ++i:
82            kdebug (" ")
83            kdebug_num (((unsigned *)addr)[i])
84        kdebug ("\n")
85    #else
86    kdebug (last ? "\n" : "; ")
87    #endif
88
89static void panic_message (unsigned n, const char *line, char const *name, char const *message):
90    kdebug ("**********************************************************************\n")
91    unsigned vaddr, epc
92    cp0_get (CP0_BAD_V_ADDR, vaddr)
93    cp0_get (CP0_EPC, epc)
94    #if 1
95    unsigned addr
96    print_addr ("BadVAddr: ", vaddr)
97    if old_current:
98        print_addr ("PC: ", old_current->pc)
99    print_addr ("epc: ", epc, true)
100    #endif
101    kdebug ("Panic: caller = ")
102    if old_current:
103        kdebug_num (old_current->id, 2)
104        kdebug (":")
105    kdebug_num ((unsigned)old_current)
106    if old_current:
107        kdebug ('@')
108        kdebug_num (old_current->pc)
109        kdebug ("\nregisters: sp=")
110        kdebug_num (old_current->sp)
111        kdebug (" at=")
112        kdebug_num (old_current->arch.at)
113        kdebug ("\nv=")
114        kdebug_num (old_current->arch.v[0])
115        kdebug (" ")
116        kdebug_num (old_current->arch.v[1])
117        kdebug ("\na=")
118        for unsigned i = 0; i < 4; ++i:
119            kdebug_num (old_current->arch.a[i])
120            kdebug (" ")
121        kdebug ("\nt=")
122        for unsigned i = 0; i < 10; ++i:
123            kdebug_num (old_current->arch.t[i])
124            kdebug (" ")
125        kdebug ("\ns=")
126        for unsigned i = 0; i < 8; ++i:
127            kdebug_num (old_current->arch.s[i])
128            kdebug (" ")
129        kdebug ("\ngp=")
130        kdebug_num (old_current->arch.gp)
131        kdebug (" fp=")
132        kdebug_num (old_current->arch.fp)
133        kdebug (" ra=")
134        kdebug_num (old_current->arch.ra)
135        kdebug ("\nhi=")
136        kdebug_num (old_current->arch.hi)
137        kdebug (" lo=")
138        kdebug_num (old_current->arch.lo)
139        kdebug ("\nk=")
140        kdebug_num (old_current->arch.k[0])
141        kdebug (" ")
142        kdebug_num (old_current->arch.k[1])
143        kdebug ('\n')
144    kdebug ("; ")
145    kdebug (name)
146    kdebug (':')
147    kdebug (line)
148    kdebug (": ")
149    kdebug (message)
150    kdebug ('/')
151    kdebug_num (n)
152    kdebug ("; debug: ")
153    kdebug_num (dbg_code.h)
154    kdebug (':')
155    kdebug_num (dbg_code.l)
156    kdebug ('\n')
157    kdebug ("debug buffer (most recently pushed at end):")
158    unsigned b = dbg_buffer_head
159    for unsigned i = 0; i < 8; ++i:
160        kdebug ('\n')
161        for unsigned j = 0; j < 4; ++j:
162            kdebug (' ')
163            kdebug_num (dbg_buffer[b])
164            ++b
165            if b == 32:
166                b = 0
167    kdebug ('\n')
168    if old_current:
169        kdebug ("Attempt to print *pc:\n")
170        unsigned page = old_current->pc & PAGE_MASK
171        unsigned start = old_current->pc & ~(4 * 4 - 1)
172        for int i = -4; i < 8; ++i:
173            kdebug_num (start + i * 4 * 4)
174            kdebug (" ==>")
175            for unsigned j = 0; j < 4; ++j:
176                kdebug (' ')
177                unsigned addr = start + (i * 4 + j) * 4
178                if (addr & PAGE_MASK) == page:
179                    kdebug_num (*(unsigned *)addr)
180                else:
181                    kdebug ("--------")
182            kdebug ('\n')
183
184void panic_impl (unsigned n, const char *line, char const *name, char const *message):
185    // Stop all threads.
186    while first_scheduled:
187        first_scheduled->unrun ()
188    for kReceiver *r = first_alarm; r; r = r->next_alarm:
189        if r->owner:
190            r->owner->unrun ()
191    for unsigned i = 0; i < 32; ++i:
192        if arch_interrupt_receiver[i] && arch_interrupt_receiver[i]->owner:
193            arch_interrupt_receiver[i]->owner->unrun ()
194    #ifndef NDEBUG
195    panic_message (n, line, name, message)
196    // If a log capability is registered, run its owner.
197    #ifndef USE_SERIAL
198    if dbg_cap.valid () && dbg_cap->target->owner:
199        dbg_cap->target->owner->run ()
200        // Use the (now running) log thread to display the message.
201    // If no log capability is registered, the machine just hangs.
202    #else
203    arch_reboot ()
204    #endif
205    #endif
206#ifndef NDEBUG
207void dbg_send (unsigned num, unsigned bits):
208    kdebug ("Warning: ")
209    kdebug_num (num)
210    kdebug ('/')
211    kdebug_num (bits)
212    kdebug ('\n')
213#endif
214#else
215void delay (unsigned ms):
216    for unsigned t = 0; t < 8000 * ms; ++t:
217        GPIO_GPDIR (0) = GPIO_GPDIR (0)
218
219void set_leds (bool a, bool b):
220    gpio_as_output (GPIO_NUM_PORT, GPIO_NUM)
221    gpio_as_output (GPIO_CAPS_PORT, GPIO_CAPS)
222    if a:
223        GPIO_GPDR (GPIO_NUM_PORT) &= ~(1 << GPIO_NUM)
224    else:
225        GPIO_GPDR (GPIO_NUM_PORT) |= 1 << GPIO_NUM
226    if b:
227        GPIO_GPDR (GPIO_CAPS_PORT) &= ~(1 << GPIO_CAPS)
228    else:
229        GPIO_GPDR (GPIO_CAPS_PORT) |= 1 << GPIO_CAPS
230
231void dbg_send (unsigned n, unsigned bits):
232    for unsigned i = 0; i < bits; ++i:
233        bool v = n & (1 << (bits - 1))
234        set_leds (v, !v)
235        delay (350)
236        set_leds (false, false)
237        delay (150)
238        n <<= 1
239    set_leds (true, true)
240    delay (50)
241    set_leds (false, false)
242    delay (50)
243void panic_impl (unsigned n, const char *line, char const *name, char const *message):
244    unsigned epc
245    cp0_get (CP0_EPC, epc)
246    dbg_send (epc, 32)
247    while true:
248        dbg_send (n, 32)
249#endif
250

Archive Download this file

Branches:
master



interactive