source: mainline/kernel/generic/src/interrupt/interrupt.c@ 07640dfd

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 07640dfd was 07640dfd, checked in by Stanislav Kozina <stanislav.kozina@…>, 15 years ago

Small changes on kernel & user accounting.

  • 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>
[a2a00e8]53#include <proc/thread.h>
[a7fdfe1]54
[973be64e]55static struct {
[aace6624]56 const char *name;
[973be64e]57 iroutine f;
[aace6624]58} exc_table[IVT_ITEMS];
[a7fdfe1]59
[dc747e3]60SPINLOCK_INITIALIZE(exctbl_lock);
[aace6624]61
62/** Register exception handler
63 *
64 * @param n Exception number
65 * @param name Description
66 * @param f Exception handler
67 */
68iroutine exc_register(int n, const char *name, iroutine f)
[973be64e]69{
70 ASSERT(n < IVT_ITEMS);
71
72 iroutine old;
73
[aace6624]74 spinlock_lock(&exctbl_lock);
[94a981a]75
[aace6624]76 old = exc_table[n].f;
77 exc_table[n].f = f;
78 exc_table[n].name = name;
[94a981a]79
80 spinlock_unlock(&exctbl_lock);
81
[973be64e]82 return old;
83}
[a7fdfe1]84
[aace6624]85/** Dispatch exception according to exception table
86 *
[973be64e]87 * Called directly from the assembler code.
88 * CPU is interrupts_disable()'d.
89 */
[25d7709]90void exc_dispatch(int n, istate_t *istate)
[973be64e]91{
92 ASSERT(n < IVT_ITEMS);
[3ff2b54]93
[a2a00e8]94 /* Account user cycles */
95 if (THREAD)
96 thread_update_accounting(true);
97
[3ff2b54]98#ifdef CONFIG_UDEBUG
99 if (THREAD) THREAD->udebug.uspace_state = istate;
100#endif
[973be64e]101
[25d7709]102 exc_table[n].f(n + IVT_FIRST, istate);
[3ff2b54]103
104#ifdef CONFIG_UDEBUG
105 if (THREAD) THREAD->udebug.uspace_state = NULL;
106#endif
107
[0dbc4e7]108 /* This is a safe place to exit exiting thread */
109 if (THREAD && THREAD->interrupted && istate_from_uspace(istate))
110 thread_exit();
[07640dfd]111
112 if (THREAD)
113 thread_update_accounting(false);
[aace6624]114}
115
116/** Default 'null' exception handler */
[25d7709]117static void exc_undef(int n, istate_t *istate)
[aace6624]118{
[f651e80]119 fault_if_from_uspace(istate, "Unhandled exception %d.", n);
120 panic("Unhandled exception %d.", n);
[aace6624]121}
122
[a074b4f]123/** Terminate thread and task if exception came from userspace. */
[a000878c]124void fault_if_from_uspace(istate_t *istate, const char *fmt, ...)
[a074b4f]125{
126 task_t *task = TASK;
127 va_list args;
128
129 if (!istate_from_uspace(istate))
130 return;
131
132 printf("Task %s (%" PRIu64 ") killed due to an exception at "
133 "program counter %p.\n", task->name, task->taskid,
134 istate_get_pc(istate));
135
136 stack_trace_istate(istate);
137
138 printf("Kill message: ");
139 va_start(args, fmt);
140 vprintf(fmt, args);
141 va_end(args);
142 printf("\n");
143
[0d21b53]144 /*
145 * Userspace can subscribe for FAULT events to take action
146 * whenever a thread faults. (E.g. take a dump, run a debugger).
147 * The notification is always available, but unless Udebug is enabled,
148 * that's all you get.
149 */
[a074b4f]150 if (event_is_subscribed(EVENT_FAULT)) {
[0d21b53]151 /* Notify the subscriber that a fault occurred. */
[a074b4f]152 event_notify_3(EVENT_FAULT, LOWER32(TASK->taskid),
153 UPPER32(TASK->taskid), (unative_t) THREAD);
154
155#ifdef CONFIG_UDEBUG
[0d21b53]156 /* Wait for a debugging session. */
157 udebug_thread_fault();
[a074b4f]158#endif
[0d21b53]159 }
[a074b4f]160
161 task_kill(task->taskid);
162 thread_exit();
163}
164
[76fca31]165#ifdef CONFIG_KCONSOLE
166
[39494010]167/** kconsole cmd - print all exceptions */
[76fca31]168static int cmd_exc_print(cmd_arg_t *argv)
[aace6624]169{
[6c441cf8]170#if (IVT_ITEMS > 0)
[43b1e86]171 unsigned int i;
[aace6624]172
173 spinlock_lock(&exctbl_lock);
[c70d693]174
175#ifdef __32_BITS__
176 printf("Exc Description Handler Symbol\n");
177 printf("--- -------------------- ---------- --------\n");
178#endif
179
180#ifdef __64_BITS__
181 printf("Exc Description Handler Symbol\n");
182 printf("--- -------------------- ------------------ --------\n");
183#endif
[43b1e86]184
185 for (i = 0; i < IVT_ITEMS; i++) {
[a000878c]186 const char *symbol = symtab_fmt_name_lookup((unative_t) exc_table[i].f);
[c70d693]187
188#ifdef __32_BITS__
189 printf("%-3u %-20s %10p %s\n", i + IVT_FIRST, exc_table[i].name,
190 exc_table[i].f, symbol);
191#endif
192
193#ifdef __64_BITS__
194 printf("%-3u %-20s %18p %s\n", i + IVT_FIRST, exc_table[i].name,
195 exc_table[i].f, symbol);
196#endif
[43b1e86]197
198 if (((i + 1) % 20) == 0) {
199 printf(" -- Press any key to continue -- ");
[402fc8bf]200 spinlock_unlock(&exctbl_lock);
[44b7783]201 indev_pop_character(stdin);
[402fc8bf]202 spinlock_lock(&exctbl_lock);
[aace6624]203 printf("\n");
204 }
205 }
[43b1e86]206
[aace6624]207 spinlock_unlock(&exctbl_lock);
[6c441cf8]208#endif
[aace6624]209
210 return 1;
211}
212
[76fca31]213
[aace6624]214static cmd_info_t exc_info = {
[0132630]215 .name = "exc",
[adb2ebf8]216 .description = "Print exception table.",
[76fca31]217 .func = cmd_exc_print,
[aace6624]218 .help = NULL,
219 .argc = 0,
220 .argv = NULL
221};
222
[76fca31]223#endif
224
[aace6624]225/** Initialize generic exception handling support */
226void exc_init(void)
227{
228 int i;
229
[c70d693]230 for (i = 0; i < IVT_ITEMS; i++)
[25d7709]231 exc_register(i, "undef", (iroutine) exc_undef);
[aace6624]232
[76fca31]233#ifdef CONFIG_KCONSOLE
[0132630]234 cmd_initialize(&exc_info);
[aace6624]235 if (!cmd_register(&exc_info))
[76fca31]236 printf("Cannot register command %s\n", exc_info.name);
237#endif
[973be64e]238}
[aace6624]239
[cc73a8a1]240/** @}
[b45c443]241 */
Note: See TracBrowser for help on using the repository browser.