source: mainline/kernel/generic/src/interrupt/interrupt.c@ 1dbc43f

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 1dbc43f was 7ad17de, checked in by Jakub Jermar <jakub@…>, 13 years ago

Instead of printing the standard kill message, only inform the user when a task
is killed because of its failure to late-reserve memory. This happens, for
example, when there is not enough memory to grow thread stack.

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