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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since bb0d3d24 was a074b4f, checked in by Jiri Svoboda <jiri@…>, 15 years ago

Implement fault notifications and task monitoring service.

  • Property mode set to 100644
File size: 5.9 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
[cf26ba9]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
[973be64e]41#include <interrupt.h>
42#include <debug.h>
[aace6624]43#include <console/kconsole.h>
44#include <console/console.h>
[0132630]45#include <console/cmd.h>
[a074b4f]46#include <ipc/event.h>
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>
[a7fdfe1]53
[973be64e]54static struct {
[aace6624]55 const char *name;
[973be64e]56 iroutine f;
[aace6624]57} exc_table[IVT_ITEMS];
[a7fdfe1]58
[dc747e3]59SPINLOCK_INITIALIZE(exctbl_lock);
[aace6624]60
61/** Register exception handler
62 *
63 * @param n Exception number
64 * @param name Description
65 * @param f Exception handler
66 */
67iroutine exc_register(int n, const char *name, iroutine f)
[973be64e]68{
69 ASSERT(n < IVT_ITEMS);
70
71 iroutine old;
72
[aace6624]73 spinlock_lock(&exctbl_lock);
[94a981a]74
[aace6624]75 old = exc_table[n].f;
76 exc_table[n].f = f;
77 exc_table[n].name = name;
[94a981a]78
79 spinlock_unlock(&exctbl_lock);
80
[973be64e]81 return old;
82}
[a7fdfe1]83
[aace6624]84/** Dispatch exception according to exception table
85 *
[973be64e]86 * Called directly from the assembler code.
87 * CPU is interrupts_disable()'d.
88 */
[25d7709]89void exc_dispatch(int n, istate_t *istate)
[973be64e]90{
91 ASSERT(n < IVT_ITEMS);
[3ff2b54]92
93#ifdef CONFIG_UDEBUG
94 if (THREAD) THREAD->udebug.uspace_state = istate;
95#endif
[973be64e]96
[25d7709]97 exc_table[n].f(n + IVT_FIRST, istate);
[3ff2b54]98
99#ifdef CONFIG_UDEBUG
100 if (THREAD) THREAD->udebug.uspace_state = NULL;
101#endif
102
[0dbc4e7]103 /* This is a safe place to exit exiting thread */
104 if (THREAD && THREAD->interrupted && istate_from_uspace(istate))
105 thread_exit();
[aace6624]106}
107
108/** Default 'null' exception handler */
[25d7709]109static void exc_undef(int n, istate_t *istate)
[aace6624]110{
[f651e80]111 fault_if_from_uspace(istate, "Unhandled exception %d.", n);
112 panic("Unhandled exception %d.", n);
[aace6624]113}
114
[a074b4f]115/** Terminate thread and task if exception came from userspace. */
116void 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 if (event_is_subscribed(EVENT_FAULT)) {
137 event_notify_3(EVENT_FAULT, LOWER32(TASK->taskid),
138 UPPER32(TASK->taskid), (unative_t) THREAD);
139 }
140
141#ifdef CONFIG_UDEBUG
142 /* Wait until a debugger attends to us. */
143 mutex_lock(&THREAD->udebug.lock);
144 while (!THREAD->udebug.active)
145 condvar_wait(&THREAD->udebug.active_cv, &THREAD->udebug.lock);
146 mutex_unlock(&THREAD->udebug.lock);
147
148 udebug_stoppable_begin();
149 udebug_stoppable_end();
150
151 /* Make sure the debugging session is over before proceeding. */
152 mutex_lock(&THREAD->udebug.lock);
153 while (THREAD->udebug.active)
154 condvar_wait(&THREAD->udebug.active_cv, &THREAD->udebug.lock);
155 mutex_unlock(&THREAD->udebug.lock);
156#endif
157
158 task_kill(task->taskid);
159 thread_exit();
160}
161
[76fca31]162#ifdef CONFIG_KCONSOLE
163
[39494010]164/** kconsole cmd - print all exceptions */
[76fca31]165static int cmd_exc_print(cmd_arg_t *argv)
[aace6624]166{
[6c441cf8]167#if (IVT_ITEMS > 0)
[43b1e86]168 unsigned int i;
[aace6624]169 char *symbol;
170
171 spinlock_lock(&exctbl_lock);
[c70d693]172
173#ifdef __32_BITS__
174 printf("Exc Description Handler Symbol\n");
175 printf("--- -------------------- ---------- --------\n");
176#endif
177
178#ifdef __64_BITS__
179 printf("Exc Description Handler Symbol\n");
180 printf("--- -------------------- ------------------ --------\n");
181#endif
[43b1e86]182
183 for (i = 0; i < IVT_ITEMS; i++) {
[e16e0d59]184 symbol = symtab_fmt_name_lookup((unative_t) exc_table[i].f);
[c70d693]185
186#ifdef __32_BITS__
187 printf("%-3u %-20s %10p %s\n", i + IVT_FIRST, exc_table[i].name,
188 exc_table[i].f, symbol);
189#endif
190
191#ifdef __64_BITS__
192 printf("%-3u %-20s %18p %s\n", i + IVT_FIRST, exc_table[i].name,
193 exc_table[i].f, symbol);
194#endif
[43b1e86]195
196 if (((i + 1) % 20) == 0) {
197 printf(" -- Press any key to continue -- ");
[402fc8bf]198 spinlock_unlock(&exctbl_lock);
[44b7783]199 indev_pop_character(stdin);
[402fc8bf]200 spinlock_lock(&exctbl_lock);
[aace6624]201 printf("\n");
202 }
203 }
[43b1e86]204
[aace6624]205 spinlock_unlock(&exctbl_lock);
[6c441cf8]206#endif
[aace6624]207
208 return 1;
209}
210
[76fca31]211
[aace6624]212static cmd_info_t exc_info = {
[0132630]213 .name = "exc",
[adb2ebf8]214 .description = "Print exception table.",
[76fca31]215 .func = cmd_exc_print,
[aace6624]216 .help = NULL,
217 .argc = 0,
218 .argv = NULL
219};
220
[76fca31]221#endif
222
[aace6624]223/** Initialize generic exception handling support */
224void exc_init(void)
225{
226 int i;
227
[c70d693]228 for (i = 0; i < IVT_ITEMS; i++)
[25d7709]229 exc_register(i, "undef", (iroutine) exc_undef);
[aace6624]230
[76fca31]231#ifdef CONFIG_KCONSOLE
[0132630]232 cmd_initialize(&exc_info);
[aace6624]233 if (!cmd_register(&exc_info))
[76fca31]234 printf("Cannot register command %s\n", exc_info.name);
235#endif
[973be64e]236}
[aace6624]237
[cc73a8a1]238/** @}
[b45c443]239 */
Note: See TracBrowser for help on using the repository browser.