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