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

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

Improvements of stack and istate_get() definitions.

  • Make sure each architecture has arch/stack.h in kernel.
  • Make each architecture define MEM_STACK_SIZE
  • istate_get() uses MEM_STACK_SIZE to allow for a greater flexibility in computing the address of the bottom of the memory stack.
  • This fixes istate_get() on ia64.
  • 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>
[e4d96e9]56#include <arch/stack.h>
[8eec3c8]57#include <str.h>
[7a0359b]58#include <trace.h>
[a7fdfe1]59
[8eec3c8]60exc_table_t exc_table[IVT_ITEMS];
61IRQ_SPINLOCK_INITIALIZE(exctbl_lock);
[aace6624]62
63/** Register exception handler
[da1bafb]64 *
[b3b7e14a]65 * @param n Exception number.
66 * @param name Description.
67 * @param hot Whether the exception is actually handled
68 * in any meaningful way.
69 * @param handler New exception handler.
70 *
71 * @return Previously registered exception handler.
[da1bafb]72 *
[aace6624]73 */
[b3b7e14a]74iroutine_t exc_register(unsigned int n, const char *name, bool hot,
75 iroutine_t handler)
[973be64e]76{
[b3b7e14a]77#if (IVT_ITEMS > 0)
[973be64e]78 ASSERT(n < IVT_ITEMS);
[b3b7e14a]79#endif
[973be64e]80
[8eec3c8]81 irq_spinlock_lock(&exctbl_lock, true);
[94a981a]82
[b3b7e14a]83 iroutine_t old = exc_table[n].handler;
84 exc_table[n].handler = handler;
[aace6624]85 exc_table[n].name = name;
[b3b7e14a]86 exc_table[n].hot = hot;
[8eec3c8]87 exc_table[n].cycles = 0;
88 exc_table[n].count = 0;
[94a981a]89
[8eec3c8]90 irq_spinlock_unlock(&exctbl_lock, true);
[94a981a]91
[973be64e]92 return old;
93}
[a7fdfe1]94
[aace6624]95/** Dispatch exception according to exception table
96 *
[973be64e]97 * Called directly from the assembler code.
98 * CPU is interrupts_disable()'d.
[da1bafb]99 *
[973be64e]100 */
[7a0359b]101NO_TRACE void exc_dispatch(unsigned int n, istate_t *istate)
[973be64e]102{
[b3b7e14a]103#if (IVT_ITEMS > 0)
[973be64e]104 ASSERT(n < IVT_ITEMS);
[b3b7e14a]105#endif
[da1bafb]106
[a2a00e8]107 /* Account user cycles */
[cd98e594]108 if (THREAD) {
[da1bafb]109 irq_spinlock_lock(&THREAD->lock, false);
[b584cd4]110 thread_update_accounting(true);
[da1bafb]111 irq_spinlock_unlock(&THREAD->lock, false);
[cd98e594]112 }
[da1bafb]113
[d0c82c5]114 /* Account CPU usage if it has waked up from sleep */
[04e3d9f]115 if (CPU) {
116 irq_spinlock_lock(&CPU->lock, false);
117 if (CPU->idle) {
118 uint64_t now = get_cycle();
119 CPU->idle_cycles += now - CPU->last_cycle;
120 CPU->last_cycle = now;
121 CPU->idle = false;
122 }
123 irq_spinlock_unlock(&CPU->lock, false);
[d0c82c5]124 }
125
[b584cd4]126 uint64_t begin_cycle = get_cycle();
127
[3ff2b54]128#ifdef CONFIG_UDEBUG
[da1bafb]129 if (THREAD)
130 THREAD->udebug.uspace_state = istate;
[3ff2b54]131#endif
[973be64e]132
[b3b7e14a]133 exc_table[n].handler(n + IVT_FIRST, istate);
[da1bafb]134
[3ff2b54]135#ifdef CONFIG_UDEBUG
[da1bafb]136 if (THREAD)
137 THREAD->udebug.uspace_state = NULL;
[3ff2b54]138#endif
[da1bafb]139
[0dbc4e7]140 /* This is a safe place to exit exiting thread */
[da1bafb]141 if ((THREAD) && (THREAD->interrupted) && (istate_from_uspace(istate)))
[0dbc4e7]142 thread_exit();
[da1bafb]143
[8eec3c8]144 /* Account exception handling */
145 uint64_t end_cycle = get_cycle();
[b584cd4]146
[decfbe56]147 irq_spinlock_lock(&exctbl_lock, false);
[8eec3c8]148 exc_table[n].cycles += end_cycle - begin_cycle;
149 exc_table[n].count++;
[decfbe56]150 irq_spinlock_unlock(&exctbl_lock, false);
[8eec3c8]151
152 /* Do not charge THREAD for exception cycles */
[cd98e594]153 if (THREAD) {
[da1bafb]154 irq_spinlock_lock(&THREAD->lock, false);
[8eec3c8]155 THREAD->last_cycle = end_cycle;
[da1bafb]156 irq_spinlock_unlock(&THREAD->lock, false);
[cd98e594]157 }
[aace6624]158}
159
[da1bafb]160/** Default 'null' exception handler
161 *
162 */
[7a0359b]163NO_TRACE static void exc_undef(unsigned int n, istate_t *istate)
[aace6624]164{
[214ec25c]165 fault_if_from_uspace(istate, "Unhandled exception %u.", n);
[4d1be48]166 panic_badtrap(istate, n, "Unhandled exception %u.", n);
[aace6624]167}
168
[908bb96]169static NO_TRACE
170void fault_from_uspace_core(istate_t *istate, const char *fmt, va_list args)
[a074b4f]171{
[908bb96]172 printf("Task %s (%" PRIu64 ") killed due to an exception at "
173 "program counter %p.\n", TASK->name, TASK->taskid,
174 (void *) istate_get_pc(istate));
[da1bafb]175
[908bb96]176 istate_decode(istate);
177 stack_trace_istate(istate);
[da1bafb]178
[908bb96]179 printf("Kill message: ");
180 vprintf(fmt, args);
181 printf("\n");
[da1bafb]182
[8f4f444]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{
[da1bafb]191 va_list args;
[8f4f444]192
[a074b4f]193 va_start(args, fmt);
[8f4f444]194 fault_from_uspace_core(istate, fmt, args);
[a074b4f]195 va_end(args);
[8f4f444]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;
[da1bafb]205
[8f4f444]206 va_list args;
207 va_start(args, fmt);
208 fault_from_uspace_core(istate, fmt, args);
209 va_end(args);
[a074b4f]210}
211
[1ad52de]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 */
[63594c0]221istate_t *istate_get(thread_t *thread)
222{
[1ad52de]223 /*
224 * The istate structure should be right at the bottom of the kernel
[e4d96e9]225 * memory stack.
[1ad52de]226 */
[e4d96e9]227 return (istate_t *) &thread->kstack[MEM_STACK_SIZE - sizeof(istate_t)];
[63594c0]228}
229
[76fca31]230#ifdef CONFIG_KCONSOLE
231
[b3b7e14a]232static char flag_buf[MAX_CMDLINE + 1];
233
[da1bafb]234/** Print all exceptions
235 *
236 */
[7a0359b]237NO_TRACE static int cmd_exc_print(cmd_arg_t *argv)
[aace6624]238{
[b3b7e14a]239 bool excs_all;
240
241 if (str_cmp(flag_buf, "-a") == 0)
242 excs_all = true;
243 else if (str_cmp(flag_buf, "") == 0)
244 excs_all = false;
245 else {
246 printf("Unknown argument \"%s\".\n", flag_buf);
247 return 1;
248 }
249
[6c441cf8]250#if (IVT_ITEMS > 0)
[43b1e86]251 unsigned int i;
[b3b7e14a]252 unsigned int rows;
[da1bafb]253
[8eec3c8]254 irq_spinlock_lock(&exctbl_lock, true);
[da1bafb]255
[c70d693]256#ifdef __32_BITS__
[b3b7e14a]257 printf("[exc ] [description ] [count ] [cycles ]"
258 " [handler ] [symbol\n");
259 rows = 1;
[c70d693]260#endif
[da1bafb]261
[c70d693]262#ifdef __64_BITS__
[b3b7e14a]263 printf("[exc ] [description ] [count ] [cycles ]"
264 " [handler ]\n");
265 printf(" [symbol\n");
266 rows = 2;
[c70d693]267#endif
[43b1e86]268
269 for (i = 0; i < IVT_ITEMS; i++) {
[b3b7e14a]270 if ((!excs_all) && (!exc_table[i].hot))
271 continue;
272
[8eec3c8]273 uint64_t count;
274 char count_suffix;
275
276 order_suffix(exc_table[i].count, &count, &count_suffix);
277
278 uint64_t cycles;
279 char cycles_suffix;
280
281 order_suffix(exc_table[i].cycles, &cycles, &cycles_suffix);
282
283 const char *symbol =
[96b02eb9]284 symtab_fmt_name_lookup((sysarg_t) exc_table[i].handler);
[da1bafb]285
[c70d693]286#ifdef __32_BITS__
[b3b7e14a]287 printf("%-8u %-20s %9" PRIu64 "%c %9" PRIu64 "%c %10p %s\n",
[8eec3c8]288 i + IVT_FIRST, exc_table[i].name, count, count_suffix,
[b3b7e14a]289 cycles, cycles_suffix, exc_table[i].handler, symbol);
290
291 PAGING(rows, 1, irq_spinlock_unlock(&exctbl_lock, true),
292 irq_spinlock_lock(&exctbl_lock, true));
[c70d693]293#endif
[da1bafb]294
[c70d693]295#ifdef __64_BITS__
[b3b7e14a]296 printf("%-8u %-20s %9" PRIu64 "%c %9" PRIu64 "%c %18p\n",
[8eec3c8]297 i + IVT_FIRST, exc_table[i].name, count, count_suffix,
[b3b7e14a]298 cycles, cycles_suffix, exc_table[i].handler);
299 printf(" %s\n", symbol);
[43b1e86]300
[b3b7e14a]301 PAGING(rows, 2, irq_spinlock_unlock(&exctbl_lock, true),
302 irq_spinlock_lock(&exctbl_lock, true));
303#endif
[aace6624]304 }
[43b1e86]305
[8eec3c8]306 irq_spinlock_unlock(&exctbl_lock, true);
[b3b7e14a]307#else /* (IVT_ITEMS > 0) */
308
309 printf("No exception table%s.\n", excs_all ? " (showing all exceptions)" : "");
310
311#endif /* (IVT_ITEMS > 0) */
[aace6624]312
313 return 1;
314}
315
[b3b7e14a]316static cmd_arg_t exc_argv = {
317 .type = ARG_TYPE_STRING_OPTIONAL,
318 .buffer = flag_buf,
319 .len = sizeof(flag_buf)
320};
321
[aace6624]322static cmd_info_t exc_info = {
[0132630]323 .name = "exc",
[b3b7e14a]324 .description = "Print exception table (use -a for all exceptions).",
[76fca31]325 .func = cmd_exc_print,
[aace6624]326 .help = NULL,
[b3b7e14a]327 .argc = 1,
328 .argv = &exc_argv
[aace6624]329};
330
[da1bafb]331#endif /* CONFIG_KCONSOLE */
[76fca31]332
[da1bafb]333/** Initialize generic exception handling support
334 *
335 */
[aace6624]336void exc_init(void)
337{
[da1bafb]338 (void) exc_undef;
339
340#if (IVT_ITEMS > 0)
341 unsigned int i;
342
[c70d693]343 for (i = 0; i < IVT_ITEMS; i++)
[b3b7e14a]344 exc_register(i, "undef", false, (iroutine_t) exc_undef);
[da1bafb]345#endif
346
[76fca31]347#ifdef CONFIG_KCONSOLE
[0132630]348 cmd_initialize(&exc_info);
[aace6624]349 if (!cmd_register(&exc_info))
[76fca31]350 printf("Cannot register command %s\n", exc_info.name);
351#endif
[973be64e]352}
[aace6624]353
[cc73a8a1]354/** @}
[b45c443]355 */
Note: See TracBrowser for help on using the repository browser.