[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 <ipc/event.h>
|
---|
| 48 | #include <synch/mutex.h>
|
---|
| 49 | #include <time/delay.h>
|
---|
| 50 | #include <macros.h>
|
---|
[aace6624] | 51 | #include <panic.h>
|
---|
| 52 | #include <print.h>
|
---|
| 53 | #include <symtab.h>
|
---|
[a2a00e8] | 54 | #include <proc/thread.h>
|
---|
[a7fdfe1] | 55 |
|
---|
[973be64e] | 56 | static struct {
|
---|
[aace6624] | 57 | const char *name;
|
---|
[973be64e] | 58 | iroutine f;
|
---|
[aace6624] | 59 | } exc_table[IVT_ITEMS];
|
---|
[a7fdfe1] | 60 |
|
---|
[dc747e3] | 61 | SPINLOCK_INITIALIZE(exctbl_lock);
|
---|
[aace6624] | 62 |
|
---|
| 63 | /** Register exception handler
|
---|
[da1bafb] | 64 | *
|
---|
| 65 | * @param n Exception number
|
---|
| 66 | * @param name Description
|
---|
| 67 | * @param handler Exception handler
|
---|
| 68 | *
|
---|
[aace6624] | 69 | */
|
---|
[da1bafb] | 70 | iroutine exc_register(int n, const char *name, iroutine handler)
|
---|
[973be64e] | 71 | {
|
---|
| 72 | ASSERT(n < IVT_ITEMS);
|
---|
| 73 |
|
---|
[aace6624] | 74 | spinlock_lock(&exctbl_lock);
|
---|
[94a981a] | 75 |
|
---|
[da1bafb] | 76 | iroutine old = exc_table[n].f;
|
---|
| 77 | exc_table[n].f = handler;
|
---|
[aace6624] | 78 | exc_table[n].name = name;
|
---|
[94a981a] | 79 |
|
---|
| 80 | spinlock_unlock(&exctbl_lock);
|
---|
| 81 |
|
---|
[973be64e] | 82 | return old;
|
---|
| 83 | }
|
---|
[a7fdfe1] | 84 |
|
---|
[aace6624] | 85 | /** Dispatch exception according to exception table
|
---|
| 86 | *
|
---|
[973be64e] | 87 | * Called directly from the assembler code.
|
---|
| 88 | * CPU is interrupts_disable()'d.
|
---|
[da1bafb] | 89 | *
|
---|
[973be64e] | 90 | */
|
---|
[25d7709] | 91 | void exc_dispatch(int n, istate_t *istate)
|
---|
[973be64e] | 92 | {
|
---|
| 93 | ASSERT(n < IVT_ITEMS);
|
---|
[da1bafb] | 94 |
|
---|
[a2a00e8] | 95 | /* Account user cycles */
|
---|
[cd98e594] | 96 | if (THREAD) {
|
---|
[da1bafb] | 97 | irq_spinlock_lock(&THREAD->lock, false);
|
---|
[a2a00e8] | 98 | thread_update_accounting(true);
|
---|
[da1bafb] | 99 | irq_spinlock_unlock(&THREAD->lock, false);
|
---|
[cd98e594] | 100 | }
|
---|
[da1bafb] | 101 |
|
---|
[3ff2b54] | 102 | #ifdef CONFIG_UDEBUG
|
---|
[da1bafb] | 103 | if (THREAD)
|
---|
| 104 | THREAD->udebug.uspace_state = istate;
|
---|
[3ff2b54] | 105 | #endif
|
---|
[973be64e] | 106 |
|
---|
[25d7709] | 107 | exc_table[n].f(n + IVT_FIRST, istate);
|
---|
[da1bafb] | 108 |
|
---|
[3ff2b54] | 109 | #ifdef CONFIG_UDEBUG
|
---|
[da1bafb] | 110 | if (THREAD)
|
---|
| 111 | THREAD->udebug.uspace_state = NULL;
|
---|
[3ff2b54] | 112 | #endif
|
---|
[da1bafb] | 113 |
|
---|
[0dbc4e7] | 114 | /* This is a safe place to exit exiting thread */
|
---|
[da1bafb] | 115 | if ((THREAD) && (THREAD->interrupted) && (istate_from_uspace(istate)))
|
---|
[0dbc4e7] | 116 | thread_exit();
|
---|
[da1bafb] | 117 |
|
---|
[cd98e594] | 118 | if (THREAD) {
|
---|
[da1bafb] | 119 | irq_spinlock_lock(&THREAD->lock, false);
|
---|
[07640dfd] | 120 | thread_update_accounting(false);
|
---|
[da1bafb] | 121 | irq_spinlock_unlock(&THREAD->lock, false);
|
---|
[cd98e594] | 122 | }
|
---|
[aace6624] | 123 | }
|
---|
| 124 |
|
---|
[da1bafb] | 125 | /** Default 'null' exception handler
|
---|
| 126 | *
|
---|
| 127 | */
|
---|
[25d7709] | 128 | static void exc_undef(int n, istate_t *istate)
|
---|
[aace6624] | 129 | {
|
---|
[f651e80] | 130 | fault_if_from_uspace(istate, "Unhandled exception %d.", n);
|
---|
| 131 | panic("Unhandled exception %d.", n);
|
---|
[aace6624] | 132 | }
|
---|
| 133 |
|
---|
[da1bafb] | 134 | /** Terminate thread and task if exception came from userspace.
|
---|
| 135 | *
|
---|
| 136 | */
|
---|
[a000878c] | 137 | void fault_if_from_uspace(istate_t *istate, const char *fmt, ...)
|
---|
[a074b4f] | 138 | {
|
---|
| 139 | if (!istate_from_uspace(istate))
|
---|
| 140 | return;
|
---|
[da1bafb] | 141 |
|
---|
[a074b4f] | 142 | printf("Task %s (%" PRIu64 ") killed due to an exception at "
|
---|
[da1bafb] | 143 | "program counter %p.\n", TASK->name, TASK->taskid,
|
---|
[a074b4f] | 144 | istate_get_pc(istate));
|
---|
[da1bafb] | 145 |
|
---|
[a074b4f] | 146 | stack_trace_istate(istate);
|
---|
[da1bafb] | 147 |
|
---|
[a074b4f] | 148 | printf("Kill message: ");
|
---|
[da1bafb] | 149 |
|
---|
| 150 | va_list args;
|
---|
[a074b4f] | 151 | va_start(args, fmt);
|
---|
| 152 | vprintf(fmt, args);
|
---|
| 153 | va_end(args);
|
---|
| 154 | printf("\n");
|
---|
[da1bafb] | 155 |
|
---|
[0d21b53] | 156 | /*
|
---|
| 157 | * Userspace can subscribe for FAULT events to take action
|
---|
| 158 | * whenever a thread faults. (E.g. take a dump, run a debugger).
|
---|
| 159 | * The notification is always available, but unless Udebug is enabled,
|
---|
| 160 | * that's all you get.
|
---|
| 161 | */
|
---|
[a074b4f] | 162 | if (event_is_subscribed(EVENT_FAULT)) {
|
---|
[0d21b53] | 163 | /* Notify the subscriber that a fault occurred. */
|
---|
[a074b4f] | 164 | event_notify_3(EVENT_FAULT, LOWER32(TASK->taskid),
|
---|
| 165 | UPPER32(TASK->taskid), (unative_t) THREAD);
|
---|
[da1bafb] | 166 |
|
---|
[a074b4f] | 167 | #ifdef CONFIG_UDEBUG
|
---|
[0d21b53] | 168 | /* Wait for a debugging session. */
|
---|
| 169 | udebug_thread_fault();
|
---|
[a074b4f] | 170 | #endif
|
---|
[0d21b53] | 171 | }
|
---|
[da1bafb] | 172 |
|
---|
| 173 | task_kill(TASK->taskid);
|
---|
[a074b4f] | 174 | thread_exit();
|
---|
| 175 | }
|
---|
| 176 |
|
---|
[76fca31] | 177 | #ifdef CONFIG_KCONSOLE
|
---|
| 178 |
|
---|
[da1bafb] | 179 | /** Print all exceptions
|
---|
| 180 | *
|
---|
| 181 | */
|
---|
[76fca31] | 182 | static int cmd_exc_print(cmd_arg_t *argv)
|
---|
[aace6624] | 183 | {
|
---|
[6c441cf8] | 184 | #if (IVT_ITEMS > 0)
|
---|
[43b1e86] | 185 | unsigned int i;
|
---|
[da1bafb] | 186 |
|
---|
[aace6624] | 187 | spinlock_lock(&exctbl_lock);
|
---|
[da1bafb] | 188 |
|
---|
[c70d693] | 189 | #ifdef __32_BITS__
|
---|
| 190 | printf("Exc Description Handler Symbol\n");
|
---|
| 191 | printf("--- -------------------- ---------- --------\n");
|
---|
| 192 | #endif
|
---|
[da1bafb] | 193 |
|
---|
[c70d693] | 194 | #ifdef __64_BITS__
|
---|
| 195 | printf("Exc Description Handler Symbol\n");
|
---|
| 196 | printf("--- -------------------- ------------------ --------\n");
|
---|
| 197 | #endif
|
---|
[43b1e86] | 198 |
|
---|
| 199 | for (i = 0; i < IVT_ITEMS; i++) {
|
---|
[a000878c] | 200 | const char *symbol = symtab_fmt_name_lookup((unative_t) exc_table[i].f);
|
---|
[da1bafb] | 201 |
|
---|
[c70d693] | 202 | #ifdef __32_BITS__
|
---|
| 203 | printf("%-3u %-20s %10p %s\n", i + IVT_FIRST, exc_table[i].name,
|
---|
| 204 | exc_table[i].f, symbol);
|
---|
| 205 | #endif
|
---|
[da1bafb] | 206 |
|
---|
[c70d693] | 207 | #ifdef __64_BITS__
|
---|
| 208 | printf("%-3u %-20s %18p %s\n", i + IVT_FIRST, exc_table[i].name,
|
---|
| 209 | exc_table[i].f, symbol);
|
---|
| 210 | #endif
|
---|
[43b1e86] | 211 |
|
---|
| 212 | if (((i + 1) % 20) == 0) {
|
---|
| 213 | printf(" -- Press any key to continue -- ");
|
---|
[402fc8bf] | 214 | spinlock_unlock(&exctbl_lock);
|
---|
[44b7783] | 215 | indev_pop_character(stdin);
|
---|
[402fc8bf] | 216 | spinlock_lock(&exctbl_lock);
|
---|
[aace6624] | 217 | printf("\n");
|
---|
| 218 | }
|
---|
| 219 | }
|
---|
[43b1e86] | 220 |
|
---|
[aace6624] | 221 | spinlock_unlock(&exctbl_lock);
|
---|
[6c441cf8] | 222 | #endif
|
---|
[aace6624] | 223 |
|
---|
| 224 | return 1;
|
---|
| 225 | }
|
---|
| 226 |
|
---|
| 227 | static cmd_info_t exc_info = {
|
---|
[0132630] | 228 | .name = "exc",
|
---|
[adb2ebf8] | 229 | .description = "Print exception table.",
|
---|
[76fca31] | 230 | .func = cmd_exc_print,
|
---|
[aace6624] | 231 | .help = NULL,
|
---|
| 232 | .argc = 0,
|
---|
| 233 | .argv = NULL
|
---|
| 234 | };
|
---|
| 235 |
|
---|
[da1bafb] | 236 | #endif /* CONFIG_KCONSOLE */
|
---|
[76fca31] | 237 |
|
---|
[da1bafb] | 238 | /** Initialize generic exception handling support
|
---|
| 239 | *
|
---|
| 240 | */
|
---|
[aace6624] | 241 | void exc_init(void)
|
---|
| 242 | {
|
---|
[da1bafb] | 243 | (void) exc_undef;
|
---|
| 244 |
|
---|
| 245 | #if (IVT_ITEMS > 0)
|
---|
| 246 | unsigned int i;
|
---|
| 247 |
|
---|
[c70d693] | 248 | for (i = 0; i < IVT_ITEMS; i++)
|
---|
[25d7709] | 249 | exc_register(i, "undef", (iroutine) exc_undef);
|
---|
[da1bafb] | 250 | #endif
|
---|
| 251 |
|
---|
[76fca31] | 252 | #ifdef CONFIG_KCONSOLE
|
---|
[0132630] | 253 | cmd_initialize(&exc_info);
|
---|
[aace6624] | 254 | if (!cmd_register(&exc_info))
|
---|
[76fca31] | 255 | printf("Cannot register command %s\n", exc_info.name);
|
---|
| 256 | #endif
|
---|
[973be64e] | 257 | }
|
---|
[aace6624] | 258 |
|
---|
[cc73a8a1] | 259 | /** @}
|
---|
[b45c443] | 260 | */
|
---|