source: mainline/kernel/generic/src/interrupt/interrupt.c@ 4a06045

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 4a06045 was 5cac9cd, checked in by Jakub Jermar <jakub@…>, 14 years ago

Dump registers in fault_if_from_uspace() for better diagnosability.

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