source: mainline/kernel/generic/src/interrupt/interrupt.c@ 42bbbe2

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 42bbbe2 was cd98e594, checked in by Jakub Jermar <jakub@…>, 15 years ago

Lock THREAD before calling thread_update_accounting() from exc_dispatch().

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