source: mainline/kernel/generic/src/interrupt/interrupt.c@ dfb16c4

ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since dfb16c4 was dfb16c4, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 20 months ago

Panic on unexpected use of exception handling

  • Property mode set to 100644
File size: 9.0 KB
Line 
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 kernel_generic_interrupt
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
42#include <assert.h>
43#include <interrupt.h>
44#include <console/kconsole.h>
45#include <console/console.h>
46#include <console/cmd.h>
47#include <synch/mutex.h>
48#include <time/delay.h>
49#include <macros.h>
50#include <panic.h>
51#include <stdio.h>
52#include <stdarg.h>
53#include <symtab.h>
54#include <proc/thread.h>
55#include <arch/cycle.h>
56#include <arch/stack.h>
57#include <str.h>
58#include <trace.h>
59
60/*
61 * If IVT_ITEMS is zero (e.g. for special/abs32le) we hide completely any
62 * access to the exception table array and panic if the function is called
63 * at all. It also silences (correct) compiler warnings about possible
64 * out-of-bound array access.
65 */
66
67exc_table_t exc_table[IVT_ITEMS];
68IRQ_SPINLOCK_INITIALIZE(exctbl_lock);
69
70/** Register exception handler
71 *
72 * @param n Exception number.
73 * @param name Description.
74 * @param hot Whether the exception is actually handled
75 * in any meaningful way.
76 * @param handler New exception handler.
77 *
78 * @return Previously registered exception handler.
79 *
80 */
81iroutine_t exc_register(unsigned int n, const char *name, bool hot,
82 iroutine_t handler)
83{
84#if (IVT_ITEMS > 0)
85 assert(n < IVT_ITEMS);
86
87 irq_spinlock_lock(&exctbl_lock, true);
88
89 iroutine_t old = exc_table[n].handler;
90 exc_table[n].handler = handler;
91 exc_table[n].name = name;
92 exc_table[n].hot = hot;
93 exc_table[n].cycles = 0;
94 exc_table[n].count = 0;
95
96 irq_spinlock_unlock(&exctbl_lock, true);
97
98 return old;
99#else
100 panic("No space for any exception handler, cannot register.");
101#endif
102}
103
104/** Dispatch exception according to exception table
105 *
106 * Called directly from the assembler code.
107 * CPU is interrupts_disable()'d.
108 *
109 */
110_NO_TRACE void exc_dispatch(unsigned int n, istate_t *istate)
111{
112#if (IVT_ITEMS > 0)
113 assert(n < IVT_ITEMS);
114
115 /* Account user cycles */
116 if (THREAD) {
117 irq_spinlock_lock(&THREAD->lock, false);
118 thread_update_accounting(true);
119 irq_spinlock_unlock(&THREAD->lock, false);
120 }
121
122 /* Account CPU usage if it woke up from sleep */
123 if (CPU && CPU->idle) {
124 uint64_t now = get_cycle();
125 atomic_time_increment(&CPU->idle_cycles, now - CPU->last_cycle);
126 CPU->last_cycle = now;
127 CPU->idle = false;
128 }
129
130 uint64_t begin_cycle = get_cycle();
131
132#ifdef CONFIG_UDEBUG
133 if (THREAD)
134 THREAD->udebug.uspace_state = istate;
135#endif
136
137 exc_table[n].handler(n + IVT_FIRST, istate);
138
139#ifdef CONFIG_UDEBUG
140 if (THREAD)
141 THREAD->udebug.uspace_state = NULL;
142#endif
143
144 /* This is a safe place to exit exiting thread */
145 if ((THREAD) && (THREAD->interrupted) && (istate_from_uspace(istate)))
146 thread_exit();
147
148 /* Account exception handling */
149 uint64_t end_cycle = get_cycle();
150
151 irq_spinlock_lock(&exctbl_lock, false);
152 exc_table[n].cycles += end_cycle - begin_cycle;
153 exc_table[n].count++;
154 irq_spinlock_unlock(&exctbl_lock, false);
155
156 /* Do not charge THREAD for exception cycles */
157 if (THREAD) {
158 irq_spinlock_lock(&THREAD->lock, false);
159 THREAD->last_cycle = end_cycle;
160 irq_spinlock_unlock(&THREAD->lock, false);
161 }
162#else
163 panic("No space for any exception handler, yet we want to handle some exception.");
164#endif
165}
166
167/** Default 'null' exception handler
168 *
169 */
170_NO_TRACE static void exc_undef(unsigned int n, istate_t *istate)
171{
172 fault_if_from_uspace(istate, "Unhandled exception %u.", n);
173 panic_badtrap(istate, n, "Unhandled exception %u.", n);
174}
175
176static _NO_TRACE void
177fault_from_uspace_core(istate_t *istate, const char *fmt, va_list args)
178{
179 printf("Task %s (%" PRIu64 ") killed due to an exception at "
180 "program counter %p.\n", TASK->name, TASK->taskid,
181 (void *) istate_get_pc(istate));
182
183 istate_decode(istate);
184 stack_trace_istate(istate);
185
186 printf("Kill message: ");
187 vprintf(fmt, args);
188 printf("\n");
189
190 task_kill_self(true);
191}
192
193/** Terminate thread and task after the exception came from userspace.
194 *
195 */
196_NO_TRACE void fault_from_uspace(istate_t *istate, const char *fmt, ...)
197{
198 va_list args;
199
200 va_start(args, fmt);
201 fault_from_uspace_core(istate, fmt, args);
202 va_end(args);
203}
204
205/** Terminate thread and task if exception came from userspace.
206 *
207 */
208_NO_TRACE void fault_if_from_uspace(istate_t *istate, const char *fmt, ...)
209{
210 if (!istate_from_uspace(istate))
211 return;
212
213 va_list args;
214 va_start(args, fmt);
215 fault_from_uspace_core(istate, fmt, args);
216 va_end(args);
217}
218
219/** Get istate structure of a thread.
220 *
221 * Get pointer to the istate structure at the bottom of the kernel stack.
222 *
223 * This function can be called in interrupt or user context. In interrupt
224 * context the istate structure is created by the low-level exception
225 * handler. In user context the istate structure is created by the
226 * low-level syscall handler.
227 */
228istate_t *istate_get(thread_t *thread)
229{
230 /*
231 * The istate structure should be right at the bottom of the kernel
232 * memory stack.
233 */
234 return (istate_t *) &thread->kstack[MEM_STACK_SIZE - sizeof(istate_t)];
235}
236
237#ifdef CONFIG_KCONSOLE
238
239static char flag_buf[MAX_CMDLINE + 1];
240
241/** Print all exceptions
242 *
243 */
244_NO_TRACE static int cmd_exc_print(cmd_arg_t *argv)
245{
246 bool excs_all;
247
248 if (str_cmp(flag_buf, "-a") == 0)
249 excs_all = true;
250 else if (str_cmp(flag_buf, "") == 0)
251 excs_all = false;
252 else {
253 printf("Unknown argument \"%s\".\n", flag_buf);
254 return 1;
255 }
256
257#if (IVT_ITEMS > 0)
258 unsigned int i;
259 unsigned int rows;
260
261 irq_spinlock_lock(&exctbl_lock, true);
262
263#ifdef __32_BITS__
264 printf("[exc ] [description ] [count ] [cycles ]"
265 " [handler ] [symbol\n");
266 rows = 1;
267#endif
268
269#ifdef __64_BITS__
270 printf("[exc ] [description ] [count ] [cycles ]"
271 " [handler ]\n");
272 printf(" [symbol\n");
273 rows = 2;
274#endif
275
276 for (i = 0; i < IVT_ITEMS; i++) {
277 if ((!excs_all) && (!exc_table[i].hot))
278 continue;
279
280 uint64_t count;
281 char count_suffix;
282
283 order_suffix(exc_table[i].count, &count, &count_suffix);
284
285 uint64_t cycles;
286 char cycles_suffix;
287
288 order_suffix(exc_table[i].cycles, &cycles, &cycles_suffix);
289
290 const char *symbol =
291 symtab_fmt_name_lookup((sysarg_t) exc_table[i].handler);
292
293#ifdef __32_BITS__
294 printf("%-8u %-20s %9" PRIu64 "%c %9" PRIu64 "%c %10p %s\n",
295 i + IVT_FIRST, exc_table[i].name, count, count_suffix,
296 cycles, cycles_suffix, exc_table[i].handler, symbol);
297
298 PAGING(rows, 1, irq_spinlock_unlock(&exctbl_lock, true),
299 irq_spinlock_lock(&exctbl_lock, true));
300#endif
301
302#ifdef __64_BITS__
303 printf("%-8u %-20s %9" PRIu64 "%c %9" PRIu64 "%c %18p\n",
304 i + IVT_FIRST, exc_table[i].name, count, count_suffix,
305 cycles, cycles_suffix, exc_table[i].handler);
306 printf(" %s\n", symbol);
307
308 PAGING(rows, 2, irq_spinlock_unlock(&exctbl_lock, true),
309 irq_spinlock_lock(&exctbl_lock, true));
310#endif
311 }
312
313 irq_spinlock_unlock(&exctbl_lock, true);
314#else /* (IVT_ITEMS > 0) */
315
316 printf("No exception table%s.\n", excs_all ? " (showing all exceptions)" : "");
317
318#endif /* (IVT_ITEMS > 0) */
319
320 return 1;
321}
322
323static cmd_arg_t exc_argv = {
324 .type = ARG_TYPE_STRING_OPTIONAL,
325 .buffer = flag_buf,
326 .len = sizeof(flag_buf)
327};
328
329static cmd_info_t exc_info = {
330 .name = "exc",
331 .description = "Print exception table (use -a for all exceptions).",
332 .func = cmd_exc_print,
333 .help = NULL,
334 .argc = 1,
335 .argv = &exc_argv
336};
337
338#endif /* CONFIG_KCONSOLE */
339
340/** Initialize generic exception handling support
341 *
342 */
343void exc_init(void)
344{
345 (void) exc_undef;
346
347#if (IVT_ITEMS > 0)
348 unsigned int i;
349
350 for (i = 0; i < IVT_ITEMS; i++)
351 exc_register(i, "undef", false, (iroutine_t) exc_undef);
352#endif
353
354#ifdef CONFIG_KCONSOLE
355 cmd_initialize(&exc_info);
356 if (!cmd_register(&exc_info))
357 printf("Cannot register command %s\n", exc_info.name);
358#endif
359}
360
361/** @}
362 */
Note: See TracBrowser for help on using the repository browser.