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

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

Add a variant of fault_if_from_uspace() that doesn't check whether
the istate is from uspace.

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