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@…>, 21 months ago

Panic on unexpected use of exception handling

  • Property mode set to 100644
File size: 9.0 KB
RevLine 
[a7fdfe1]1/*
[df4ed85]2 * Copyright (c) 2005 Ondrej Palkovsky
[a7fdfe1]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 */
[b45c443]28
[174156fd]29/** @addtogroup kernel_generic_interrupt
[b45c443]30 * @{
31 */
[cf26ba9]32/**
[b45c443]33 * @file
[da1bafb]34 * @brief Interrupt redirector.
[cf26ba9]35 *
36 * This file provides means of registering interrupt handlers
37 * by kernel functions and calling the handlers when interrupts
38 * occur.
[da1bafb]39 *
[cf26ba9]40 */
41
[63e27ef]42#include <assert.h>
[973be64e]43#include <interrupt.h>
[aace6624]44#include <console/kconsole.h>
45#include <console/console.h>
[0132630]46#include <console/cmd.h>
[a074b4f]47#include <synch/mutex.h>
48#include <time/delay.h>
49#include <macros.h>
[aace6624]50#include <panic.h>
[bab75df6]51#include <stdio.h>
[8f4f444]52#include <stdarg.h>
[aace6624]53#include <symtab.h>
[a2a00e8]54#include <proc/thread.h>
[8eec3c8]55#include <arch/cycle.h>
[e4d96e9]56#include <arch/stack.h>
[8eec3c8]57#include <str.h>
[7a0359b]58#include <trace.h>
[a7fdfe1]59
[dfb16c4]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
[8eec3c8]67exc_table_t exc_table[IVT_ITEMS];
68IRQ_SPINLOCK_INITIALIZE(exctbl_lock);
[aace6624]69
70/** Register exception handler
[da1bafb]71 *
[b3b7e14a]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.
[da1bafb]79 *
[aace6624]80 */
[b3b7e14a]81iroutine_t exc_register(unsigned int n, const char *name, bool hot,
82 iroutine_t handler)
[973be64e]83{
[b3b7e14a]84#if (IVT_ITEMS > 0)
[63e27ef]85 assert(n < IVT_ITEMS);
[a35b458]86
[8eec3c8]87 irq_spinlock_lock(&exctbl_lock, true);
[a35b458]88
[b3b7e14a]89 iroutine_t old = exc_table[n].handler;
90 exc_table[n].handler = handler;
[aace6624]91 exc_table[n].name = name;
[b3b7e14a]92 exc_table[n].hot = hot;
[8eec3c8]93 exc_table[n].cycles = 0;
94 exc_table[n].count = 0;
[a35b458]95
[8eec3c8]96 irq_spinlock_unlock(&exctbl_lock, true);
[a35b458]97
[973be64e]98 return old;
[dfb16c4]99#else
100 panic("No space for any exception handler, cannot register.");
101#endif
[973be64e]102}
[a7fdfe1]103
[aace6624]104/** Dispatch exception according to exception table
105 *
[973be64e]106 * Called directly from the assembler code.
107 * CPU is interrupts_disable()'d.
[da1bafb]108 *
[973be64e]109 */
[8df5f20]110_NO_TRACE void exc_dispatch(unsigned int n, istate_t *istate)
[973be64e]111{
[b3b7e14a]112#if (IVT_ITEMS > 0)
[63e27ef]113 assert(n < IVT_ITEMS);
[a35b458]114
[a2a00e8]115 /* Account user cycles */
[cd98e594]116 if (THREAD) {
[da1bafb]117 irq_spinlock_lock(&THREAD->lock, false);
[b584cd4]118 thread_update_accounting(true);
[da1bafb]119 irq_spinlock_unlock(&THREAD->lock, false);
[cd98e594]120 }
[a35b458]121
[181a746]122 /* Account CPU usage if it woke up from sleep */
123 if (CPU && CPU->idle) {
124 uint64_t now = get_cycle();
[b2ec5cf]125 atomic_time_increment(&CPU->idle_cycles, now - CPU->last_cycle);
[181a746]126 CPU->last_cycle = now;
127 CPU->idle = false;
[d0c82c5]128 }
[a35b458]129
[b584cd4]130 uint64_t begin_cycle = get_cycle();
[a35b458]131
[3ff2b54]132#ifdef CONFIG_UDEBUG
[da1bafb]133 if (THREAD)
134 THREAD->udebug.uspace_state = istate;
[3ff2b54]135#endif
[a35b458]136
[b3b7e14a]137 exc_table[n].handler(n + IVT_FIRST, istate);
[a35b458]138
[3ff2b54]139#ifdef CONFIG_UDEBUG
[da1bafb]140 if (THREAD)
141 THREAD->udebug.uspace_state = NULL;
[3ff2b54]142#endif
[a35b458]143
[0dbc4e7]144 /* This is a safe place to exit exiting thread */
[da1bafb]145 if ((THREAD) && (THREAD->interrupted) && (istate_from_uspace(istate)))
[0dbc4e7]146 thread_exit();
[a35b458]147
[8eec3c8]148 /* Account exception handling */
149 uint64_t end_cycle = get_cycle();
[a35b458]150
[decfbe56]151 irq_spinlock_lock(&exctbl_lock, false);
[8eec3c8]152 exc_table[n].cycles += end_cycle - begin_cycle;
153 exc_table[n].count++;
[decfbe56]154 irq_spinlock_unlock(&exctbl_lock, false);
[a35b458]155
[8eec3c8]156 /* Do not charge THREAD for exception cycles */
[cd98e594]157 if (THREAD) {
[da1bafb]158 irq_spinlock_lock(&THREAD->lock, false);
[8eec3c8]159 THREAD->last_cycle = end_cycle;
[da1bafb]160 irq_spinlock_unlock(&THREAD->lock, false);
[cd98e594]161 }
[dfb16c4]162#else
163 panic("No space for any exception handler, yet we want to handle some exception.");
164#endif
[aace6624]165}
166
[da1bafb]167/** Default 'null' exception handler
168 *
169 */
[8df5f20]170_NO_TRACE static void exc_undef(unsigned int n, istate_t *istate)
[aace6624]171{
[214ec25c]172 fault_if_from_uspace(istate, "Unhandled exception %u.", n);
[4d1be48]173 panic_badtrap(istate, n, "Unhandled exception %u.", n);
[aace6624]174}
175
[8df5f20]176static _NO_TRACE void
[18b6a88]177fault_from_uspace_core(istate_t *istate, const char *fmt, va_list args)
[a074b4f]178{
[908bb96]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));
[a35b458]182
[908bb96]183 istate_decode(istate);
184 stack_trace_istate(istate);
[a35b458]185
[908bb96]186 printf("Kill message: ");
187 vprintf(fmt, args);
188 printf("\n");
[a35b458]189
[8f4f444]190 task_kill_self(true);
191}
192
193/** Terminate thread and task after the exception came from userspace.
194 *
195 */
[8df5f20]196_NO_TRACE void fault_from_uspace(istate_t *istate, const char *fmt, ...)
[8f4f444]197{
[da1bafb]198 va_list args;
[8f4f444]199
[a074b4f]200 va_start(args, fmt);
[8f4f444]201 fault_from_uspace_core(istate, fmt, args);
[a074b4f]202 va_end(args);
[8f4f444]203}
204
205/** Terminate thread and task if exception came from userspace.
206 *
207 */
[8df5f20]208_NO_TRACE void fault_if_from_uspace(istate_t *istate, const char *fmt, ...)
[8f4f444]209{
210 if (!istate_from_uspace(istate))
211 return;
[a35b458]212
[8f4f444]213 va_list args;
214 va_start(args, fmt);
215 fault_from_uspace_core(istate, fmt, args);
216 va_end(args);
[a074b4f]217}
218
[1ad52de]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 */
[63594c0]228istate_t *istate_get(thread_t *thread)
229{
[1ad52de]230 /*
231 * The istate structure should be right at the bottom of the kernel
[e4d96e9]232 * memory stack.
[1ad52de]233 */
[e4d96e9]234 return (istate_t *) &thread->kstack[MEM_STACK_SIZE - sizeof(istate_t)];
[63594c0]235}
236
[76fca31]237#ifdef CONFIG_KCONSOLE
238
[b3b7e14a]239static char flag_buf[MAX_CMDLINE + 1];
240
[da1bafb]241/** Print all exceptions
242 *
243 */
[8df5f20]244_NO_TRACE static int cmd_exc_print(cmd_arg_t *argv)
[aace6624]245{
[b3b7e14a]246 bool excs_all;
[a35b458]247
[b3b7e14a]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 }
[a35b458]256
[6c441cf8]257#if (IVT_ITEMS > 0)
[43b1e86]258 unsigned int i;
[b3b7e14a]259 unsigned int rows;
[a35b458]260
[8eec3c8]261 irq_spinlock_lock(&exctbl_lock, true);
[a35b458]262
[c70d693]263#ifdef __32_BITS__
[b3b7e14a]264 printf("[exc ] [description ] [count ] [cycles ]"
265 " [handler ] [symbol\n");
266 rows = 1;
[c70d693]267#endif
[a35b458]268
[c70d693]269#ifdef __64_BITS__
[b3b7e14a]270 printf("[exc ] [description ] [count ] [cycles ]"
271 " [handler ]\n");
272 printf(" [symbol\n");
273 rows = 2;
[c70d693]274#endif
[a35b458]275
[43b1e86]276 for (i = 0; i < IVT_ITEMS; i++) {
[b3b7e14a]277 if ((!excs_all) && (!exc_table[i].hot))
278 continue;
[a35b458]279
[8eec3c8]280 uint64_t count;
281 char count_suffix;
[a35b458]282
[8eec3c8]283 order_suffix(exc_table[i].count, &count, &count_suffix);
[a35b458]284
[8eec3c8]285 uint64_t cycles;
286 char cycles_suffix;
[a35b458]287
[8eec3c8]288 order_suffix(exc_table[i].cycles, &cycles, &cycles_suffix);
[a35b458]289
[8eec3c8]290 const char *symbol =
[96b02eb9]291 symtab_fmt_name_lookup((sysarg_t) exc_table[i].handler);
[a35b458]292
[c70d693]293#ifdef __32_BITS__
[b3b7e14a]294 printf("%-8u %-20s %9" PRIu64 "%c %9" PRIu64 "%c %10p %s\n",
[8eec3c8]295 i + IVT_FIRST, exc_table[i].name, count, count_suffix,
[b3b7e14a]296 cycles, cycles_suffix, exc_table[i].handler, symbol);
[a35b458]297
[b3b7e14a]298 PAGING(rows, 1, irq_spinlock_unlock(&exctbl_lock, true),
299 irq_spinlock_lock(&exctbl_lock, true));
[c70d693]300#endif
[a35b458]301
[c70d693]302#ifdef __64_BITS__
[b3b7e14a]303 printf("%-8u %-20s %9" PRIu64 "%c %9" PRIu64 "%c %18p\n",
[8eec3c8]304 i + IVT_FIRST, exc_table[i].name, count, count_suffix,
[b3b7e14a]305 cycles, cycles_suffix, exc_table[i].handler);
306 printf(" %s\n", symbol);
[a35b458]307
[b3b7e14a]308 PAGING(rows, 2, irq_spinlock_unlock(&exctbl_lock, true),
309 irq_spinlock_lock(&exctbl_lock, true));
310#endif
[aace6624]311 }
[a35b458]312
[8eec3c8]313 irq_spinlock_unlock(&exctbl_lock, true);
[b3b7e14a]314#else /* (IVT_ITEMS > 0) */
[a35b458]315
[b3b7e14a]316 printf("No exception table%s.\n", excs_all ? " (showing all exceptions)" : "");
[a35b458]317
[b3b7e14a]318#endif /* (IVT_ITEMS > 0) */
[a35b458]319
[aace6624]320 return 1;
321}
322
[b3b7e14a]323static cmd_arg_t exc_argv = {
324 .type = ARG_TYPE_STRING_OPTIONAL,
325 .buffer = flag_buf,
326 .len = sizeof(flag_buf)
327};
328
[aace6624]329static cmd_info_t exc_info = {
[0132630]330 .name = "exc",
[b3b7e14a]331 .description = "Print exception table (use -a for all exceptions).",
[76fca31]332 .func = cmd_exc_print,
[aace6624]333 .help = NULL,
[b3b7e14a]334 .argc = 1,
335 .argv = &exc_argv
[aace6624]336};
337
[da1bafb]338#endif /* CONFIG_KCONSOLE */
[76fca31]339
[da1bafb]340/** Initialize generic exception handling support
341 *
342 */
[aace6624]343void exc_init(void)
344{
[da1bafb]345 (void) exc_undef;
[a35b458]346
[da1bafb]347#if (IVT_ITEMS > 0)
348 unsigned int i;
[a35b458]349
[c70d693]350 for (i = 0; i < IVT_ITEMS; i++)
[b3b7e14a]351 exc_register(i, "undef", false, (iroutine_t) exc_undef);
[da1bafb]352#endif
[a35b458]353
[76fca31]354#ifdef CONFIG_KCONSOLE
[0132630]355 cmd_initialize(&exc_info);
[aace6624]356 if (!cmd_register(&exc_info))
[76fca31]357 printf("Cannot register command %s\n", exc_info.name);
358#endif
[973be64e]359}
[aace6624]360
[cc73a8a1]361/** @}
[b45c443]362 */
Note: See TracBrowser for help on using the repository browser.