| 1 | /*
|
|---|
| 2 | * Copyright (c) 2005 Ondrej Palkovsky
|
|---|
| 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 | */
|
|---|
| 28 |
|
|---|
| 29 | /** @addtogroup genericinterrupt
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /**
|
|---|
| 33 | * @file
|
|---|
| 34 | * @brief Interrupt redirector.
|
|---|
| 35 | *
|
|---|
| 36 | * This file provides means of registering interrupt handlers
|
|---|
| 37 | * by kernel functions and calling the handlers when interrupts
|
|---|
| 38 | * occur.
|
|---|
| 39 | *
|
|---|
| 40 | */
|
|---|
| 41 |
|
|---|
| 42 | #include <interrupt.h>
|
|---|
| 43 | #include <debug.h>
|
|---|
| 44 | #include <console/kconsole.h>
|
|---|
| 45 | #include <console/console.h>
|
|---|
| 46 | #include <console/cmd.h>
|
|---|
| 47 | #include <ipc/event.h>
|
|---|
| 48 | #include <synch/mutex.h>
|
|---|
| 49 | #include <time/delay.h>
|
|---|
| 50 | #include <macros.h>
|
|---|
| 51 | #include <panic.h>
|
|---|
| 52 | #include <print.h>
|
|---|
| 53 | #include <symtab.h>
|
|---|
| 54 | #include <proc/thread.h>
|
|---|
| 55 | #include <arch/cycle.h>
|
|---|
| 56 | #include <str.h>
|
|---|
| 57 |
|
|---|
| 58 | exc_table_t exc_table[IVT_ITEMS];
|
|---|
| 59 | IRQ_SPINLOCK_INITIALIZE(exctbl_lock);
|
|---|
| 60 |
|
|---|
| 61 | /** Register exception handler
|
|---|
| 62 | *
|
|---|
| 63 | * @param n Exception number
|
|---|
| 64 | * @param name Description
|
|---|
| 65 | * @param handler Exception handler
|
|---|
| 66 | *
|
|---|
| 67 | */
|
|---|
| 68 | iroutine exc_register(int n, const char *name, iroutine handler)
|
|---|
| 69 | {
|
|---|
| 70 | ASSERT(n < IVT_ITEMS);
|
|---|
| 71 |
|
|---|
| 72 | irq_spinlock_lock(&exctbl_lock, true);
|
|---|
| 73 |
|
|---|
| 74 | iroutine old = exc_table[n].f;
|
|---|
| 75 | exc_table[n].f = handler;
|
|---|
| 76 | exc_table[n].name = name;
|
|---|
| 77 | exc_table[n].cycles = 0;
|
|---|
| 78 | exc_table[n].count = 0;
|
|---|
| 79 |
|
|---|
| 80 | irq_spinlock_unlock(&exctbl_lock, true);
|
|---|
| 81 |
|
|---|
| 82 | return old;
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | /** Dispatch exception according to exception table
|
|---|
| 86 | *
|
|---|
| 87 | * Called directly from the assembler code.
|
|---|
| 88 | * CPU is interrupts_disable()'d.
|
|---|
| 89 | *
|
|---|
| 90 | */
|
|---|
| 91 | void exc_dispatch(int n, istate_t *istate)
|
|---|
| 92 | {
|
|---|
| 93 | ASSERT(n < IVT_ITEMS);
|
|---|
| 94 |
|
|---|
| 95 | uint64_t begin_cycle = get_cycle();
|
|---|
| 96 |
|
|---|
| 97 | /* Account user cycles */
|
|---|
| 98 | if (THREAD) {
|
|---|
| 99 | irq_spinlock_lock(&THREAD->lock, false);
|
|---|
| 100 | THREAD->ucycles += begin_cycle - THREAD->last_cycle;
|
|---|
| 101 | irq_spinlock_unlock(&THREAD->lock, false);
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | #ifdef CONFIG_UDEBUG
|
|---|
| 105 | if (THREAD)
|
|---|
| 106 | THREAD->udebug.uspace_state = istate;
|
|---|
| 107 | #endif
|
|---|
| 108 |
|
|---|
| 109 | exc_table[n].f(n + IVT_FIRST, istate);
|
|---|
| 110 |
|
|---|
| 111 | #ifdef CONFIG_UDEBUG
|
|---|
| 112 | if (THREAD)
|
|---|
| 113 | THREAD->udebug.uspace_state = NULL;
|
|---|
| 114 | #endif
|
|---|
| 115 |
|
|---|
| 116 | /* This is a safe place to exit exiting thread */
|
|---|
| 117 | if ((THREAD) && (THREAD->interrupted) && (istate_from_uspace(istate)))
|
|---|
| 118 | thread_exit();
|
|---|
| 119 |
|
|---|
| 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 */
|
|---|
| 126 | if (THREAD) {
|
|---|
| 127 | irq_spinlock_lock(&THREAD->lock, false);
|
|---|
| 128 | THREAD->last_cycle = end_cycle;
|
|---|
| 129 | irq_spinlock_unlock(&THREAD->lock, false);
|
|---|
| 130 | }
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | /** Default 'null' exception handler
|
|---|
| 134 | *
|
|---|
| 135 | */
|
|---|
| 136 | static void exc_undef(int n, istate_t *istate)
|
|---|
| 137 | {
|
|---|
| 138 | fault_if_from_uspace(istate, "Unhandled exception %d.", n);
|
|---|
| 139 | panic("Unhandled exception %d.", n);
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | /** Terminate thread and task if exception came from userspace.
|
|---|
| 143 | *
|
|---|
| 144 | */
|
|---|
| 145 | void fault_if_from_uspace(istate_t *istate, const char *fmt, ...)
|
|---|
| 146 | {
|
|---|
| 147 | if (!istate_from_uspace(istate))
|
|---|
| 148 | return;
|
|---|
| 149 |
|
|---|
| 150 | printf("Task %s (%" PRIu64 ") killed due to an exception at "
|
|---|
| 151 | "program counter %p.\n", TASK->name, TASK->taskid,
|
|---|
| 152 | istate_get_pc(istate));
|
|---|
| 153 |
|
|---|
| 154 | stack_trace_istate(istate);
|
|---|
| 155 |
|
|---|
| 156 | printf("Kill message: ");
|
|---|
| 157 |
|
|---|
| 158 | va_list args;
|
|---|
| 159 | va_start(args, fmt);
|
|---|
| 160 | vprintf(fmt, args);
|
|---|
| 161 | va_end(args);
|
|---|
| 162 | printf("\n");
|
|---|
| 163 |
|
|---|
| 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 | */
|
|---|
| 170 | if (event_is_subscribed(EVENT_FAULT)) {
|
|---|
| 171 | /* Notify the subscriber that a fault occurred. */
|
|---|
| 172 | event_notify_3(EVENT_FAULT, LOWER32(TASK->taskid),
|
|---|
| 173 | UPPER32(TASK->taskid), (unative_t) THREAD);
|
|---|
| 174 |
|
|---|
| 175 | #ifdef CONFIG_UDEBUG
|
|---|
| 176 | /* Wait for a debugging session. */
|
|---|
| 177 | udebug_thread_fault();
|
|---|
| 178 | #endif
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| 181 | task_kill(TASK->taskid);
|
|---|
| 182 | thread_exit();
|
|---|
| 183 | }
|
|---|
| 184 |
|
|---|
| 185 | #ifdef CONFIG_KCONSOLE
|
|---|
| 186 |
|
|---|
| 187 | /** Print all exceptions
|
|---|
| 188 | *
|
|---|
| 189 | */
|
|---|
| 190 | static int cmd_exc_print(cmd_arg_t *argv)
|
|---|
| 191 | {
|
|---|
| 192 | #if (IVT_ITEMS > 0)
|
|---|
| 193 | unsigned int i;
|
|---|
| 194 |
|
|---|
| 195 | irq_spinlock_lock(&exctbl_lock, true);
|
|---|
| 196 |
|
|---|
| 197 | #ifdef __32_BITS__
|
|---|
| 198 | printf("Exc Description Count Cycles Handler Symbol\n");
|
|---|
| 199 | printf("--- -------------------- ---------- ---------- ---------- --------\n");
|
|---|
| 200 | #endif
|
|---|
| 201 |
|
|---|
| 202 | #ifdef __64_BITS__
|
|---|
| 203 | printf("Exc Description Count Cycles Handler Symbol\n");
|
|---|
| 204 | printf("--- -------------------- ---------- ---------- ------------------ --------\n");
|
|---|
| 205 | #endif
|
|---|
| 206 |
|
|---|
| 207 | for (i = 0; i < IVT_ITEMS; i++) {
|
|---|
| 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);
|
|---|
| 220 |
|
|---|
| 221 | #ifdef __32_BITS__
|
|---|
| 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);
|
|---|
| 225 | #endif
|
|---|
| 226 |
|
|---|
| 227 | #ifdef __64_BITS__
|
|---|
| 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);
|
|---|
| 231 | #endif
|
|---|
| 232 |
|
|---|
| 233 | if (((i + 1) % 20) == 0) {
|
|---|
| 234 | printf(" -- Press any key to continue -- ");
|
|---|
| 235 | irq_spinlock_unlock(&exctbl_lock, true);
|
|---|
| 236 | indev_pop_character(stdin);
|
|---|
| 237 | irq_spinlock_lock(&exctbl_lock, true);
|
|---|
| 238 | printf("\n");
|
|---|
| 239 | }
|
|---|
| 240 | }
|
|---|
| 241 |
|
|---|
| 242 | irq_spinlock_unlock(&exctbl_lock, true);
|
|---|
| 243 | #endif
|
|---|
| 244 |
|
|---|
| 245 | return 1;
|
|---|
| 246 | }
|
|---|
| 247 |
|
|---|
| 248 | static cmd_info_t exc_info = {
|
|---|
| 249 | .name = "exc",
|
|---|
| 250 | .description = "Print exception table.",
|
|---|
| 251 | .func = cmd_exc_print,
|
|---|
| 252 | .help = NULL,
|
|---|
| 253 | .argc = 0,
|
|---|
| 254 | .argv = NULL
|
|---|
| 255 | };
|
|---|
| 256 |
|
|---|
| 257 | #endif /* CONFIG_KCONSOLE */
|
|---|
| 258 |
|
|---|
| 259 | /** Initialize generic exception handling support
|
|---|
| 260 | *
|
|---|
| 261 | */
|
|---|
| 262 | void exc_init(void)
|
|---|
| 263 | {
|
|---|
| 264 | (void) exc_undef;
|
|---|
| 265 |
|
|---|
| 266 | #if (IVT_ITEMS > 0)
|
|---|
| 267 | unsigned int i;
|
|---|
| 268 |
|
|---|
| 269 | for (i = 0; i < IVT_ITEMS; i++)
|
|---|
| 270 | exc_register(i, "undef", (iroutine) exc_undef);
|
|---|
| 271 | #endif
|
|---|
| 272 |
|
|---|
| 273 | #ifdef CONFIG_KCONSOLE
|
|---|
| 274 | cmd_initialize(&exc_info);
|
|---|
| 275 | if (!cmd_register(&exc_info))
|
|---|
| 276 | printf("Cannot register command %s\n", exc_info.name);
|
|---|
| 277 | #endif
|
|---|
| 278 | }
|
|---|
| 279 |
|
|---|
| 280 | /** @}
|
|---|
| 281 | */
|
|---|