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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c0c26ac was da68871a, checked in by Adam Hraska <adam.hraska+hos@…>, 13 years ago

Merged changes from mainline.

  • 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
[181a746]113 /* Account CPU usage if it woke up from sleep */
114 if (CPU && CPU->idle) {
[04e3d9f]115 irq_spinlock_lock(&CPU->lock, false);
[181a746]116 uint64_t now = get_cycle();
117 CPU->idle_cycles += now - CPU->last_cycle;
118 CPU->last_cycle = now;
119 CPU->idle = false;
[04e3d9f]120 irq_spinlock_unlock(&CPU->lock, false);
[d0c82c5]121 }
122
[b584cd4]123 uint64_t begin_cycle = get_cycle();
124
[3ff2b54]125#ifdef CONFIG_UDEBUG
[da1bafb]126 if (THREAD)
127 THREAD->udebug.uspace_state = istate;
[3ff2b54]128#endif
[973be64e]129
[b3b7e14a]130 exc_table[n].handler(n + IVT_FIRST, istate);
[da1bafb]131
[3ff2b54]132#ifdef CONFIG_UDEBUG
[da1bafb]133 if (THREAD)
134 THREAD->udebug.uspace_state = NULL;
[3ff2b54]135#endif
[da1bafb]136
[0dbc4e7]137 /* This is a safe place to exit exiting thread */
[da1bafb]138 if ((THREAD) && (THREAD->interrupted) && (istate_from_uspace(istate)))
[0dbc4e7]139 thread_exit();
[da1bafb]140
[8eec3c8]141 /* Account exception handling */
142 uint64_t end_cycle = get_cycle();
[b584cd4]143
[decfbe56]144 irq_spinlock_lock(&exctbl_lock, false);
[8eec3c8]145 exc_table[n].cycles += end_cycle - begin_cycle;
146 exc_table[n].count++;
[decfbe56]147 irq_spinlock_unlock(&exctbl_lock, false);
[8eec3c8]148
149 /* Do not charge THREAD for exception cycles */
[cd98e594]150 if (THREAD) {
[da1bafb]151 irq_spinlock_lock(&THREAD->lock, false);
[8eec3c8]152 THREAD->last_cycle = end_cycle;
[da1bafb]153 irq_spinlock_unlock(&THREAD->lock, false);
[cd98e594]154 }
[aace6624]155}
156
[da1bafb]157/** Default 'null' exception handler
158 *
159 */
[7a0359b]160NO_TRACE static void exc_undef(unsigned int n, istate_t *istate)
[aace6624]161{
[214ec25c]162 fault_if_from_uspace(istate, "Unhandled exception %u.", n);
[4d1be48]163 panic_badtrap(istate, n, "Unhandled exception %u.", n);
[aace6624]164}
165
[8f4f444]166static NO_TRACE void fault_from_uspace_core(istate_t *istate, const char *fmt, va_list args)
[a074b4f]167{
168 printf("Task %s (%" PRIu64 ") killed due to an exception at "
[da1bafb]169 "program counter %p.\n", TASK->name, TASK->taskid,
[7e752b2]170 (void *) istate_get_pc(istate));
[da1bafb]171
[5cac9cd]172 istate_decode(istate);
[a074b4f]173 stack_trace_istate(istate);
[da1bafb]174
[a074b4f]175 printf("Kill message: ");
[8f4f444]176 vprintf(fmt, args);
177 printf("\n");
[da1bafb]178
[8f4f444]179 task_kill_self(true);
180}
181
182/** Terminate thread and task after the exception came from userspace.
183 *
184 */
185NO_TRACE void fault_from_uspace(istate_t *istate, const char *fmt, ...)
186{
[da1bafb]187 va_list args;
[8f4f444]188
[a074b4f]189 va_start(args, fmt);
[8f4f444]190 fault_from_uspace_core(istate, fmt, args);
[a074b4f]191 va_end(args);
[8f4f444]192}
193
194/** Terminate thread and task if exception came from userspace.
195 *
196 */
197NO_TRACE void fault_if_from_uspace(istate_t *istate, const char *fmt, ...)
198{
199 if (!istate_from_uspace(istate))
200 return;
[da1bafb]201
[8f4f444]202 va_list args;
203 va_start(args, fmt);
204 fault_from_uspace_core(istate, fmt, args);
205 va_end(args);
[a074b4f]206}
207
[1ad52de]208/** Get istate structure of a thread.
209 *
210 * Get pointer to the istate structure at the bottom of the kernel stack.
211 *
212 * This function can be called in interrupt or user context. In interrupt
213 * context the istate structure is created by the low-level exception
214 * handler. In user context the istate structure is created by the
215 * low-level syscall handler.
216 */
[63594c0]217istate_t *istate_get(thread_t *thread)
218{
[1ad52de]219 /*
220 * The istate structure should be right at the bottom of the kernel
221 * stack.
222 */
[26aafe8]223 return (istate_t *) ((uint8_t *)
224 thread->kstack + STACK_SIZE - sizeof(istate_t));
[63594c0]225}
226
[76fca31]227#ifdef CONFIG_KCONSOLE
228
[b3b7e14a]229static char flag_buf[MAX_CMDLINE + 1];
230
[da1bafb]231/** Print all exceptions
232 *
233 */
[7a0359b]234NO_TRACE static int cmd_exc_print(cmd_arg_t *argv)
[aace6624]235{
[b3b7e14a]236 bool excs_all;
237
238 if (str_cmp(flag_buf, "-a") == 0)
239 excs_all = true;
240 else if (str_cmp(flag_buf, "") == 0)
241 excs_all = false;
242 else {
243 printf("Unknown argument \"%s\".\n", flag_buf);
244 return 1;
245 }
246
[6c441cf8]247#if (IVT_ITEMS > 0)
[43b1e86]248 unsigned int i;
[b3b7e14a]249 unsigned int rows;
[da1bafb]250
[8eec3c8]251 irq_spinlock_lock(&exctbl_lock, true);
[da1bafb]252
[c70d693]253#ifdef __32_BITS__
[b3b7e14a]254 printf("[exc ] [description ] [count ] [cycles ]"
255 " [handler ] [symbol\n");
256 rows = 1;
[c70d693]257#endif
[da1bafb]258
[c70d693]259#ifdef __64_BITS__
[b3b7e14a]260 printf("[exc ] [description ] [count ] [cycles ]"
261 " [handler ]\n");
262 printf(" [symbol\n");
263 rows = 2;
[c70d693]264#endif
[43b1e86]265
266 for (i = 0; i < IVT_ITEMS; i++) {
[b3b7e14a]267 if ((!excs_all) && (!exc_table[i].hot))
268 continue;
269
[8eec3c8]270 uint64_t count;
271 char count_suffix;
272
273 order_suffix(exc_table[i].count, &count, &count_suffix);
274
275 uint64_t cycles;
276 char cycles_suffix;
277
278 order_suffix(exc_table[i].cycles, &cycles, &cycles_suffix);
279
280 const char *symbol =
[96b02eb9]281 symtab_fmt_name_lookup((sysarg_t) exc_table[i].handler);
[da1bafb]282
[c70d693]283#ifdef __32_BITS__
[b3b7e14a]284 printf("%-8u %-20s %9" PRIu64 "%c %9" PRIu64 "%c %10p %s\n",
[8eec3c8]285 i + IVT_FIRST, exc_table[i].name, count, count_suffix,
[b3b7e14a]286 cycles, cycles_suffix, exc_table[i].handler, symbol);
287
288 PAGING(rows, 1, irq_spinlock_unlock(&exctbl_lock, true),
289 irq_spinlock_lock(&exctbl_lock, true));
[c70d693]290#endif
[da1bafb]291
[c70d693]292#ifdef __64_BITS__
[b3b7e14a]293 printf("%-8u %-20s %9" PRIu64 "%c %9" PRIu64 "%c %18p\n",
[8eec3c8]294 i + IVT_FIRST, exc_table[i].name, count, count_suffix,
[b3b7e14a]295 cycles, cycles_suffix, exc_table[i].handler);
296 printf(" %s\n", symbol);
[43b1e86]297
[b3b7e14a]298 PAGING(rows, 2, irq_spinlock_unlock(&exctbl_lock, true),
299 irq_spinlock_lock(&exctbl_lock, true));
300#endif
[aace6624]301 }
[43b1e86]302
[8eec3c8]303 irq_spinlock_unlock(&exctbl_lock, true);
[b3b7e14a]304#else /* (IVT_ITEMS > 0) */
305
306 printf("No exception table%s.\n", excs_all ? " (showing all exceptions)" : "");
307
308#endif /* (IVT_ITEMS > 0) */
[aace6624]309
310 return 1;
311}
312
[b3b7e14a]313static cmd_arg_t exc_argv = {
314 .type = ARG_TYPE_STRING_OPTIONAL,
315 .buffer = flag_buf,
316 .len = sizeof(flag_buf)
317};
318
[aace6624]319static cmd_info_t exc_info = {
[0132630]320 .name = "exc",
[b3b7e14a]321 .description = "Print exception table (use -a for all exceptions).",
[76fca31]322 .func = cmd_exc_print,
[aace6624]323 .help = NULL,
[b3b7e14a]324 .argc = 1,
325 .argv = &exc_argv
[aace6624]326};
327
[da1bafb]328#endif /* CONFIG_KCONSOLE */
[76fca31]329
[da1bafb]330/** Initialize generic exception handling support
331 *
332 */
[aace6624]333void exc_init(void)
334{
[da1bafb]335 (void) exc_undef;
336
337#if (IVT_ITEMS > 0)
338 unsigned int i;
339
[c70d693]340 for (i = 0; i < IVT_ITEMS; i++)
[b3b7e14a]341 exc_register(i, "undef", false, (iroutine_t) exc_undef);
[da1bafb]342#endif
343
[76fca31]344#ifdef CONFIG_KCONSOLE
[0132630]345 cmd_initialize(&exc_info);
[aace6624]346 if (!cmd_register(&exc_info))
[76fca31]347 printf("Cannot register command %s\n", exc_info.name);
348#endif
[973be64e]349}
[aace6624]350
[cc73a8a1]351/** @}
[b45c443]352 */
Note: See TracBrowser for help on using the repository browser.