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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since da1bafb was da1bafb, checked in by Martin Decky <martin@…>, 15 years ago

major code revision

  • replace spinlocks taken with interrupts disabled with irq_spinlocks
  • change spacing (not indendation) to be tab-size independent
  • use unsigned integer types where appropriate (especially bit flags)
  • visual separation
  • remove argument names in function prototypes
  • string changes
  • correct some formating directives
  • replace various cryptic single-character variables (t, a, m, c, b, etc.) with proper identifiers (thread, task, timeout, as, itm, itc, etc.)
  • unify some assembler constructs
  • unused page table levels are now optimized out in compile time
  • replace several ints (with boolean semantics) with bools
  • use specifically sized types instead of generic types where appropriate (size_t, uint32_t, btree_key_t)
  • improve comments
  • split asserts with conjuction into multiple independent asserts
  • Property mode set to 100644
File size: 6.2 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 genericinterrupt
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 <interrupt.h>
43#include <debug.h>
44#include <console/kconsole.h>
45#include <console/console.h>
46#include <console/cmd.h>
47#include <ipc/event.h>
48#include <synch/mutex.h>
49#include <time/delay.h>
50#include <macros.h>
51#include <panic.h>
52#include <print.h>
53#include <symtab.h>
54#include <proc/thread.h>
55
56static struct {
57 const char *name;
58 iroutine f;
59} exc_table[IVT_ITEMS];
60
61SPINLOCK_INITIALIZE(exctbl_lock);
62
63/** Register exception handler
64 *
65 * @param n Exception number
66 * @param name Description
67 * @param handler Exception handler
68 *
69 */
70iroutine exc_register(int n, const char *name, iroutine handler)
71{
72 ASSERT(n < IVT_ITEMS);
73
74 spinlock_lock(&exctbl_lock);
75
76 iroutine old = exc_table[n].f;
77 exc_table[n].f = handler;
78 exc_table[n].name = name;
79
80 spinlock_unlock(&exctbl_lock);
81
82 return old;
83}
84
85/** Dispatch exception according to exception table
86 *
87 * Called directly from the assembler code.
88 * CPU is interrupts_disable()'d.
89 *
90 */
91void exc_dispatch(int n, istate_t *istate)
92{
93 ASSERT(n < IVT_ITEMS);
94
95 /* Account user cycles */
96 if (THREAD) {
97 irq_spinlock_lock(&THREAD->lock, false);
98 thread_update_accounting(true);
99 irq_spinlock_unlock(&THREAD->lock, false);
100 }
101
102#ifdef CONFIG_UDEBUG
103 if (THREAD)
104 THREAD->udebug.uspace_state = istate;
105#endif
106
107 exc_table[n].f(n + IVT_FIRST, istate);
108
109#ifdef CONFIG_UDEBUG
110 if (THREAD)
111 THREAD->udebug.uspace_state = NULL;
112#endif
113
114 /* This is a safe place to exit exiting thread */
115 if ((THREAD) && (THREAD->interrupted) && (istate_from_uspace(istate)))
116 thread_exit();
117
118 if (THREAD) {
119 irq_spinlock_lock(&THREAD->lock, false);
120 thread_update_accounting(false);
121 irq_spinlock_unlock(&THREAD->lock, false);
122 }
123}
124
125/** Default 'null' exception handler
126 *
127 */
128static void exc_undef(int n, istate_t *istate)
129{
130 fault_if_from_uspace(istate, "Unhandled exception %d.", n);
131 panic("Unhandled exception %d.", n);
132}
133
134/** Terminate thread and task if exception came from userspace.
135 *
136 */
137void fault_if_from_uspace(istate_t *istate, const char *fmt, ...)
138{
139 if (!istate_from_uspace(istate))
140 return;
141
142 printf("Task %s (%" PRIu64 ") killed due to an exception at "
143 "program counter %p.\n", TASK->name, TASK->taskid,
144 istate_get_pc(istate));
145
146 stack_trace_istate(istate);
147
148 printf("Kill message: ");
149
150 va_list args;
151 va_start(args, fmt);
152 vprintf(fmt, args);
153 va_end(args);
154 printf("\n");
155
156 /*
157 * Userspace can subscribe for FAULT events to take action
158 * whenever a thread faults. (E.g. take a dump, run a debugger).
159 * The notification is always available, but unless Udebug is enabled,
160 * that's all you get.
161 */
162 if (event_is_subscribed(EVENT_FAULT)) {
163 /* Notify the subscriber that a fault occurred. */
164 event_notify_3(EVENT_FAULT, LOWER32(TASK->taskid),
165 UPPER32(TASK->taskid), (unative_t) THREAD);
166
167#ifdef CONFIG_UDEBUG
168 /* Wait for a debugging session. */
169 udebug_thread_fault();
170#endif
171 }
172
173 task_kill(TASK->taskid);
174 thread_exit();
175}
176
177#ifdef CONFIG_KCONSOLE
178
179/** Print all exceptions
180 *
181 */
182static int cmd_exc_print(cmd_arg_t *argv)
183{
184#if (IVT_ITEMS > 0)
185 unsigned int i;
186
187 spinlock_lock(&exctbl_lock);
188
189#ifdef __32_BITS__
190 printf("Exc Description Handler Symbol\n");
191 printf("--- -------------------- ---------- --------\n");
192#endif
193
194#ifdef __64_BITS__
195 printf("Exc Description Handler Symbol\n");
196 printf("--- -------------------- ------------------ --------\n");
197#endif
198
199 for (i = 0; i < IVT_ITEMS; i++) {
200 const char *symbol = symtab_fmt_name_lookup((unative_t) exc_table[i].f);
201
202#ifdef __32_BITS__
203 printf("%-3u %-20s %10p %s\n", i + IVT_FIRST, exc_table[i].name,
204 exc_table[i].f, symbol);
205#endif
206
207#ifdef __64_BITS__
208 printf("%-3u %-20s %18p %s\n", i + IVT_FIRST, exc_table[i].name,
209 exc_table[i].f, symbol);
210#endif
211
212 if (((i + 1) % 20) == 0) {
213 printf(" -- Press any key to continue -- ");
214 spinlock_unlock(&exctbl_lock);
215 indev_pop_character(stdin);
216 spinlock_lock(&exctbl_lock);
217 printf("\n");
218 }
219 }
220
221 spinlock_unlock(&exctbl_lock);
222#endif
223
224 return 1;
225}
226
227static cmd_info_t exc_info = {
228 .name = "exc",
229 .description = "Print exception table.",
230 .func = cmd_exc_print,
231 .help = NULL,
232 .argc = 0,
233 .argv = NULL
234};
235
236#endif /* CONFIG_KCONSOLE */
237
238/** Initialize generic exception handling support
239 *
240 */
241void exc_init(void)
242{
243 (void) exc_undef;
244
245#if (IVT_ITEMS > 0)
246 unsigned int i;
247
248 for (i = 0; i < IVT_ITEMS; i++)
249 exc_register(i, "undef", (iroutine) exc_undef);
250#endif
251
252#ifdef CONFIG_KCONSOLE
253 cmd_initialize(&exc_info);
254 if (!cmd_register(&exc_info))
255 printf("Cannot register command %s\n", exc_info.name);
256#endif
257}
258
259/** @}
260 */
Note: See TracBrowser for help on using the repository browser.