1 | /*
|
---|
2 | * Copyright (c) 2005 Ondrej Palkovsky
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup genericinterrupt
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /**
|
---|
33 | * @file
|
---|
34 | * @brief Interrupt redirector.
|
---|
35 | *
|
---|
36 | * This file provides means of registering interrupt handlers
|
---|
37 | * by kernel functions and calling the handlers when interrupts
|
---|
38 | * occur.
|
---|
39 | */
|
---|
40 |
|
---|
41 | #include <interrupt.h>
|
---|
42 | #include <debug.h>
|
---|
43 | #include <console/kconsole.h>
|
---|
44 | #include <console/console.h>
|
---|
45 | #include <console/cmd.h>
|
---|
46 | #include <ipc/event.h>
|
---|
47 | #include <synch/mutex.h>
|
---|
48 | #include <time/delay.h>
|
---|
49 | #include <macros.h>
|
---|
50 | #include <panic.h>
|
---|
51 | #include <print.h>
|
---|
52 | #include <symtab.h>
|
---|
53 |
|
---|
54 | static struct {
|
---|
55 | const char *name;
|
---|
56 | iroutine f;
|
---|
57 | } exc_table[IVT_ITEMS];
|
---|
58 |
|
---|
59 | SPINLOCK_INITIALIZE(exctbl_lock);
|
---|
60 |
|
---|
61 | /** Register exception handler
|
---|
62 | *
|
---|
63 | * @param n Exception number
|
---|
64 | * @param name Description
|
---|
65 | * @param f Exception handler
|
---|
66 | */
|
---|
67 | iroutine exc_register(int n, const char *name, iroutine f)
|
---|
68 | {
|
---|
69 | ASSERT(n < IVT_ITEMS);
|
---|
70 |
|
---|
71 | iroutine old;
|
---|
72 |
|
---|
73 | spinlock_lock(&exctbl_lock);
|
---|
74 |
|
---|
75 | old = exc_table[n].f;
|
---|
76 | exc_table[n].f = f;
|
---|
77 | exc_table[n].name = name;
|
---|
78 |
|
---|
79 | spinlock_unlock(&exctbl_lock);
|
---|
80 |
|
---|
81 | return old;
|
---|
82 | }
|
---|
83 |
|
---|
84 | /** Dispatch exception according to exception table
|
---|
85 | *
|
---|
86 | * Called directly from the assembler code.
|
---|
87 | * CPU is interrupts_disable()'d.
|
---|
88 | */
|
---|
89 | void exc_dispatch(int n, istate_t *istate)
|
---|
90 | {
|
---|
91 | ASSERT(n < IVT_ITEMS);
|
---|
92 |
|
---|
93 | #ifdef CONFIG_UDEBUG
|
---|
94 | if (THREAD) THREAD->udebug.uspace_state = istate;
|
---|
95 | #endif
|
---|
96 |
|
---|
97 | exc_table[n].f(n + IVT_FIRST, istate);
|
---|
98 |
|
---|
99 | #ifdef CONFIG_UDEBUG
|
---|
100 | if (THREAD) THREAD->udebug.uspace_state = NULL;
|
---|
101 | #endif
|
---|
102 |
|
---|
103 | /* This is a safe place to exit exiting thread */
|
---|
104 | if (THREAD && THREAD->interrupted && istate_from_uspace(istate))
|
---|
105 | thread_exit();
|
---|
106 | }
|
---|
107 |
|
---|
108 | /** Default 'null' exception handler */
|
---|
109 | static void exc_undef(int n, istate_t *istate)
|
---|
110 | {
|
---|
111 | fault_if_from_uspace(istate, "Unhandled exception %d.", n);
|
---|
112 | panic("Unhandled exception %d.", n);
|
---|
113 | }
|
---|
114 |
|
---|
115 | /** Terminate thread and task if exception came from userspace. */
|
---|
116 | void fault_if_from_uspace(istate_t *istate, char *fmt, ...)
|
---|
117 | {
|
---|
118 | task_t *task = TASK;
|
---|
119 | va_list args;
|
---|
120 |
|
---|
121 | if (!istate_from_uspace(istate))
|
---|
122 | return;
|
---|
123 |
|
---|
124 | printf("Task %s (%" PRIu64 ") killed due to an exception at "
|
---|
125 | "program counter %p.\n", task->name, task->taskid,
|
---|
126 | istate_get_pc(istate));
|
---|
127 |
|
---|
128 | stack_trace_istate(istate);
|
---|
129 |
|
---|
130 | printf("Kill message: ");
|
---|
131 | va_start(args, fmt);
|
---|
132 | vprintf(fmt, args);
|
---|
133 | va_end(args);
|
---|
134 | printf("\n");
|
---|
135 |
|
---|
136 | /*
|
---|
137 | * Userspace can subscribe for FAULT events to take action
|
---|
138 | * whenever a thread faults. (E.g. take a dump, run a debugger).
|
---|
139 | * The notification is always available, but unless Udebug is enabled,
|
---|
140 | * that's all you get.
|
---|
141 | */
|
---|
142 | if (event_is_subscribed(EVENT_FAULT)) {
|
---|
143 | /* Notify the subscriber that a fault occurred. */
|
---|
144 | event_notify_3(EVENT_FAULT, LOWER32(TASK->taskid),
|
---|
145 | UPPER32(TASK->taskid), (unative_t) THREAD);
|
---|
146 |
|
---|
147 | #ifdef CONFIG_UDEBUG
|
---|
148 | /* Wait for a debugging session. */
|
---|
149 | udebug_thread_fault();
|
---|
150 | #endif
|
---|
151 | }
|
---|
152 |
|
---|
153 | task_kill(task->taskid);
|
---|
154 | thread_exit();
|
---|
155 | }
|
---|
156 |
|
---|
157 | #ifdef CONFIG_KCONSOLE
|
---|
158 |
|
---|
159 | /** kconsole cmd - print all exceptions */
|
---|
160 | static int cmd_exc_print(cmd_arg_t *argv)
|
---|
161 | {
|
---|
162 | #if (IVT_ITEMS > 0)
|
---|
163 | unsigned int i;
|
---|
164 | char *symbol;
|
---|
165 |
|
---|
166 | spinlock_lock(&exctbl_lock);
|
---|
167 |
|
---|
168 | #ifdef __32_BITS__
|
---|
169 | printf("Exc Description Handler Symbol\n");
|
---|
170 | printf("--- -------------------- ---------- --------\n");
|
---|
171 | #endif
|
---|
172 |
|
---|
173 | #ifdef __64_BITS__
|
---|
174 | printf("Exc Description Handler Symbol\n");
|
---|
175 | printf("--- -------------------- ------------------ --------\n");
|
---|
176 | #endif
|
---|
177 |
|
---|
178 | for (i = 0; i < IVT_ITEMS; i++) {
|
---|
179 | symbol = symtab_fmt_name_lookup((unative_t) exc_table[i].f);
|
---|
180 |
|
---|
181 | #ifdef __32_BITS__
|
---|
182 | printf("%-3u %-20s %10p %s\n", i + IVT_FIRST, exc_table[i].name,
|
---|
183 | exc_table[i].f, symbol);
|
---|
184 | #endif
|
---|
185 |
|
---|
186 | #ifdef __64_BITS__
|
---|
187 | printf("%-3u %-20s %18p %s\n", i + IVT_FIRST, exc_table[i].name,
|
---|
188 | exc_table[i].f, symbol);
|
---|
189 | #endif
|
---|
190 |
|
---|
191 | if (((i + 1) % 20) == 0) {
|
---|
192 | printf(" -- Press any key to continue -- ");
|
---|
193 | spinlock_unlock(&exctbl_lock);
|
---|
194 | indev_pop_character(stdin);
|
---|
195 | spinlock_lock(&exctbl_lock);
|
---|
196 | printf("\n");
|
---|
197 | }
|
---|
198 | }
|
---|
199 |
|
---|
200 | spinlock_unlock(&exctbl_lock);
|
---|
201 | #endif
|
---|
202 |
|
---|
203 | return 1;
|
---|
204 | }
|
---|
205 |
|
---|
206 |
|
---|
207 | static cmd_info_t exc_info = {
|
---|
208 | .name = "exc",
|
---|
209 | .description = "Print exception table.",
|
---|
210 | .func = cmd_exc_print,
|
---|
211 | .help = NULL,
|
---|
212 | .argc = 0,
|
---|
213 | .argv = NULL
|
---|
214 | };
|
---|
215 |
|
---|
216 | #endif
|
---|
217 |
|
---|
218 | /** Initialize generic exception handling support */
|
---|
219 | void exc_init(void)
|
---|
220 | {
|
---|
221 | int i;
|
---|
222 |
|
---|
223 | for (i = 0; i < IVT_ITEMS; i++)
|
---|
224 | exc_register(i, "undef", (iroutine) exc_undef);
|
---|
225 |
|
---|
226 | #ifdef CONFIG_KCONSOLE
|
---|
227 | cmd_initialize(&exc_info);
|
---|
228 | if (!cmd_register(&exc_info))
|
---|
229 | printf("Cannot register command %s\n", exc_info.name);
|
---|
230 | #endif
|
---|
231 | }
|
---|
232 |
|
---|
233 | /** @}
|
---|
234 | */
|
---|