[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 | *
|
---|
[b3b7e14a] | 63 | * @param n Exception number.
|
---|
| 64 | * @param name Description.
|
---|
| 65 | * @param hot Whether the exception is actually handled
|
---|
| 66 | * in any meaningful way.
|
---|
| 67 | * @param handler New exception handler.
|
---|
| 68 | *
|
---|
| 69 | * @return Previously registered exception handler.
|
---|
[da1bafb] | 70 | *
|
---|
[aace6624] | 71 | */
|
---|
[b3b7e14a] | 72 | iroutine_t exc_register(unsigned int n, const char *name, bool hot,
|
---|
| 73 | iroutine_t handler)
|
---|
[973be64e] | 74 | {
|
---|
[b3b7e14a] | 75 | #if (IVT_ITEMS > 0)
|
---|
[973be64e] | 76 | ASSERT(n < IVT_ITEMS);
|
---|
[b3b7e14a] | 77 | #endif
|
---|
[973be64e] | 78 |
|
---|
[8eec3c8] | 79 | irq_spinlock_lock(&exctbl_lock, true);
|
---|
[94a981a] | 80 |
|
---|
[b3b7e14a] | 81 | iroutine_t old = exc_table[n].handler;
|
---|
| 82 | exc_table[n].handler = handler;
|
---|
[aace6624] | 83 | exc_table[n].name = name;
|
---|
[b3b7e14a] | 84 | exc_table[n].hot = hot;
|
---|
[8eec3c8] | 85 | exc_table[n].cycles = 0;
|
---|
| 86 | exc_table[n].count = 0;
|
---|
[94a981a] | 87 |
|
---|
[8eec3c8] | 88 | irq_spinlock_unlock(&exctbl_lock, true);
|
---|
[94a981a] | 89 |
|
---|
[973be64e] | 90 | return old;
|
---|
| 91 | }
|
---|
[a7fdfe1] | 92 |
|
---|
[aace6624] | 93 | /** Dispatch exception according to exception table
|
---|
| 94 | *
|
---|
[973be64e] | 95 | * Called directly from the assembler code.
|
---|
| 96 | * CPU is interrupts_disable()'d.
|
---|
[da1bafb] | 97 | *
|
---|
[973be64e] | 98 | */
|
---|
[b3b7e14a] | 99 | void exc_dispatch(unsigned int n, istate_t *istate)
|
---|
[973be64e] | 100 | {
|
---|
[b3b7e14a] | 101 | #if (IVT_ITEMS > 0)
|
---|
[973be64e] | 102 | ASSERT(n < IVT_ITEMS);
|
---|
[b3b7e14a] | 103 | #endif
|
---|
[da1bafb] | 104 |
|
---|
[8eec3c8] | 105 | uint64_t begin_cycle = get_cycle();
|
---|
| 106 |
|
---|
[a2a00e8] | 107 | /* Account user cycles */
|
---|
[cd98e594] | 108 | if (THREAD) {
|
---|
[da1bafb] | 109 | irq_spinlock_lock(&THREAD->lock, false);
|
---|
[8eec3c8] | 110 | THREAD->ucycles += begin_cycle - THREAD->last_cycle;
|
---|
[da1bafb] | 111 | irq_spinlock_unlock(&THREAD->lock, false);
|
---|
[cd98e594] | 112 | }
|
---|
[da1bafb] | 113 |
|
---|
[3ff2b54] | 114 | #ifdef CONFIG_UDEBUG
|
---|
[da1bafb] | 115 | if (THREAD)
|
---|
| 116 | THREAD->udebug.uspace_state = istate;
|
---|
[3ff2b54] | 117 | #endif
|
---|
[973be64e] | 118 |
|
---|
[b3b7e14a] | 119 | exc_table[n].handler(n + IVT_FIRST, istate);
|
---|
[da1bafb] | 120 |
|
---|
[3ff2b54] | 121 | #ifdef CONFIG_UDEBUG
|
---|
[da1bafb] | 122 | if (THREAD)
|
---|
| 123 | THREAD->udebug.uspace_state = NULL;
|
---|
[3ff2b54] | 124 | #endif
|
---|
[da1bafb] | 125 |
|
---|
[0dbc4e7] | 126 | /* This is a safe place to exit exiting thread */
|
---|
[da1bafb] | 127 | if ((THREAD) && (THREAD->interrupted) && (istate_from_uspace(istate)))
|
---|
[0dbc4e7] | 128 | thread_exit();
|
---|
[da1bafb] | 129 |
|
---|
[8eec3c8] | 130 | /* Account exception handling */
|
---|
| 131 | uint64_t end_cycle = get_cycle();
|
---|
| 132 | exc_table[n].cycles += end_cycle - begin_cycle;
|
---|
| 133 | exc_table[n].count++;
|
---|
| 134 |
|
---|
| 135 | /* Do not charge THREAD for exception cycles */
|
---|
[cd98e594] | 136 | if (THREAD) {
|
---|
[da1bafb] | 137 | irq_spinlock_lock(&THREAD->lock, false);
|
---|
[8eec3c8] | 138 | THREAD->last_cycle = end_cycle;
|
---|
[da1bafb] | 139 | irq_spinlock_unlock(&THREAD->lock, false);
|
---|
[cd98e594] | 140 | }
|
---|
[aace6624] | 141 | }
|
---|
| 142 |
|
---|
[da1bafb] | 143 | /** Default 'null' exception handler
|
---|
| 144 | *
|
---|
| 145 | */
|
---|
[214ec25c] | 146 | static void exc_undef(unsigned int n, istate_t *istate)
|
---|
[aace6624] | 147 | {
|
---|
[214ec25c] | 148 | fault_if_from_uspace(istate, "Unhandled exception %u.", n);
|
---|
| 149 | panic("Unhandled exception %u.", n);
|
---|
[aace6624] | 150 | }
|
---|
| 151 |
|
---|
[da1bafb] | 152 | /** Terminate thread and task if exception came from userspace.
|
---|
| 153 | *
|
---|
| 154 | */
|
---|
[a000878c] | 155 | void fault_if_from_uspace(istate_t *istate, const char *fmt, ...)
|
---|
[a074b4f] | 156 | {
|
---|
| 157 | if (!istate_from_uspace(istate))
|
---|
| 158 | return;
|
---|
[da1bafb] | 159 |
|
---|
[a074b4f] | 160 | printf("Task %s (%" PRIu64 ") killed due to an exception at "
|
---|
[da1bafb] | 161 | "program counter %p.\n", TASK->name, TASK->taskid,
|
---|
[a074b4f] | 162 | istate_get_pc(istate));
|
---|
[da1bafb] | 163 |
|
---|
[a074b4f] | 164 | stack_trace_istate(istate);
|
---|
[da1bafb] | 165 |
|
---|
[a074b4f] | 166 | printf("Kill message: ");
|
---|
[da1bafb] | 167 |
|
---|
| 168 | va_list args;
|
---|
[a074b4f] | 169 | va_start(args, fmt);
|
---|
| 170 | vprintf(fmt, args);
|
---|
| 171 | va_end(args);
|
---|
| 172 | printf("\n");
|
---|
[da1bafb] | 173 |
|
---|
[0d21b53] | 174 | /*
|
---|
| 175 | * Userspace can subscribe for FAULT events to take action
|
---|
| 176 | * whenever a thread faults. (E.g. take a dump, run a debugger).
|
---|
| 177 | * The notification is always available, but unless Udebug is enabled,
|
---|
| 178 | * that's all you get.
|
---|
| 179 | */
|
---|
[a074b4f] | 180 | if (event_is_subscribed(EVENT_FAULT)) {
|
---|
[0d21b53] | 181 | /* Notify the subscriber that a fault occurred. */
|
---|
[a074b4f] | 182 | event_notify_3(EVENT_FAULT, LOWER32(TASK->taskid),
|
---|
| 183 | UPPER32(TASK->taskid), (unative_t) THREAD);
|
---|
[da1bafb] | 184 |
|
---|
[a074b4f] | 185 | #ifdef CONFIG_UDEBUG
|
---|
[0d21b53] | 186 | /* Wait for a debugging session. */
|
---|
| 187 | udebug_thread_fault();
|
---|
[a074b4f] | 188 | #endif
|
---|
[0d21b53] | 189 | }
|
---|
[da1bafb] | 190 |
|
---|
| 191 | task_kill(TASK->taskid);
|
---|
[a074b4f] | 192 | thread_exit();
|
---|
| 193 | }
|
---|
| 194 |
|
---|
[76fca31] | 195 | #ifdef CONFIG_KCONSOLE
|
---|
| 196 |
|
---|
[b3b7e14a] | 197 | static char flag_buf[MAX_CMDLINE + 1];
|
---|
| 198 |
|
---|
[da1bafb] | 199 | /** Print all exceptions
|
---|
| 200 | *
|
---|
| 201 | */
|
---|
[76fca31] | 202 | static int cmd_exc_print(cmd_arg_t *argv)
|
---|
[aace6624] | 203 | {
|
---|
[b3b7e14a] | 204 | bool excs_all;
|
---|
| 205 |
|
---|
| 206 | if (str_cmp(flag_buf, "-a") == 0)
|
---|
| 207 | excs_all = true;
|
---|
| 208 | else if (str_cmp(flag_buf, "") == 0)
|
---|
| 209 | excs_all = false;
|
---|
| 210 | else {
|
---|
| 211 | printf("Unknown argument \"%s\".\n", flag_buf);
|
---|
| 212 | return 1;
|
---|
| 213 | }
|
---|
| 214 |
|
---|
[6c441cf8] | 215 | #if (IVT_ITEMS > 0)
|
---|
[43b1e86] | 216 | unsigned int i;
|
---|
[b3b7e14a] | 217 | unsigned int rows;
|
---|
[da1bafb] | 218 |
|
---|
[8eec3c8] | 219 | irq_spinlock_lock(&exctbl_lock, true);
|
---|
[da1bafb] | 220 |
|
---|
[c70d693] | 221 | #ifdef __32_BITS__
|
---|
[b3b7e14a] | 222 | printf("[exc ] [description ] [count ] [cycles ]"
|
---|
| 223 | " [handler ] [symbol\n");
|
---|
| 224 | rows = 1;
|
---|
[c70d693] | 225 | #endif
|
---|
[da1bafb] | 226 |
|
---|
[c70d693] | 227 | #ifdef __64_BITS__
|
---|
[b3b7e14a] | 228 | printf("[exc ] [description ] [count ] [cycles ]"
|
---|
| 229 | " [handler ]\n");
|
---|
| 230 | printf(" [symbol\n");
|
---|
| 231 | rows = 2;
|
---|
[c70d693] | 232 | #endif
|
---|
[43b1e86] | 233 |
|
---|
| 234 | for (i = 0; i < IVT_ITEMS; i++) {
|
---|
[b3b7e14a] | 235 | if ((!excs_all) && (!exc_table[i].hot))
|
---|
| 236 | continue;
|
---|
| 237 |
|
---|
[8eec3c8] | 238 | uint64_t count;
|
---|
| 239 | char count_suffix;
|
---|
| 240 |
|
---|
| 241 | order_suffix(exc_table[i].count, &count, &count_suffix);
|
---|
| 242 |
|
---|
| 243 | uint64_t cycles;
|
---|
| 244 | char cycles_suffix;
|
---|
| 245 |
|
---|
| 246 | order_suffix(exc_table[i].cycles, &cycles, &cycles_suffix);
|
---|
| 247 |
|
---|
| 248 | const char *symbol =
|
---|
[b3b7e14a] | 249 | symtab_fmt_name_lookup((unative_t) exc_table[i].handler);
|
---|
[da1bafb] | 250 |
|
---|
[c70d693] | 251 | #ifdef __32_BITS__
|
---|
[b3b7e14a] | 252 | printf("%-8u %-20s %9" PRIu64 "%c %9" PRIu64 "%c %10p %s\n",
|
---|
[8eec3c8] | 253 | i + IVT_FIRST, exc_table[i].name, count, count_suffix,
|
---|
[b3b7e14a] | 254 | cycles, cycles_suffix, exc_table[i].handler, symbol);
|
---|
| 255 |
|
---|
| 256 | PAGING(rows, 1, irq_spinlock_unlock(&exctbl_lock, true),
|
---|
| 257 | irq_spinlock_lock(&exctbl_lock, true));
|
---|
[c70d693] | 258 | #endif
|
---|
[da1bafb] | 259 |
|
---|
[c70d693] | 260 | #ifdef __64_BITS__
|
---|
[b3b7e14a] | 261 | printf("%-8u %-20s %9" PRIu64 "%c %9" PRIu64 "%c %18p\n",
|
---|
[8eec3c8] | 262 | i + IVT_FIRST, exc_table[i].name, count, count_suffix,
|
---|
[b3b7e14a] | 263 | cycles, cycles_suffix, exc_table[i].handler);
|
---|
| 264 | printf(" %s\n", symbol);
|
---|
[43b1e86] | 265 |
|
---|
[b3b7e14a] | 266 | PAGING(rows, 2, irq_spinlock_unlock(&exctbl_lock, true),
|
---|
| 267 | irq_spinlock_lock(&exctbl_lock, true));
|
---|
| 268 | #endif
|
---|
[aace6624] | 269 | }
|
---|
[43b1e86] | 270 |
|
---|
[8eec3c8] | 271 | irq_spinlock_unlock(&exctbl_lock, true);
|
---|
[b3b7e14a] | 272 | #else /* (IVT_ITEMS > 0) */
|
---|
| 273 |
|
---|
| 274 | printf("No exception table%s.\n", excs_all ? " (showing all exceptions)" : "");
|
---|
| 275 |
|
---|
| 276 | #endif /* (IVT_ITEMS > 0) */
|
---|
[aace6624] | 277 |
|
---|
| 278 | return 1;
|
---|
| 279 | }
|
---|
| 280 |
|
---|
[b3b7e14a] | 281 | static cmd_arg_t exc_argv = {
|
---|
| 282 | .type = ARG_TYPE_STRING_OPTIONAL,
|
---|
| 283 | .buffer = flag_buf,
|
---|
| 284 | .len = sizeof(flag_buf)
|
---|
| 285 | };
|
---|
| 286 |
|
---|
[aace6624] | 287 | static cmd_info_t exc_info = {
|
---|
[0132630] | 288 | .name = "exc",
|
---|
[b3b7e14a] | 289 | .description = "Print exception table (use -a for all exceptions).",
|
---|
[76fca31] | 290 | .func = cmd_exc_print,
|
---|
[aace6624] | 291 | .help = NULL,
|
---|
[b3b7e14a] | 292 | .argc = 1,
|
---|
| 293 | .argv = &exc_argv
|
---|
[aace6624] | 294 | };
|
---|
| 295 |
|
---|
[da1bafb] | 296 | #endif /* CONFIG_KCONSOLE */
|
---|
[76fca31] | 297 |
|
---|
[da1bafb] | 298 | /** Initialize generic exception handling support
|
---|
| 299 | *
|
---|
| 300 | */
|
---|
[aace6624] | 301 | void exc_init(void)
|
---|
| 302 | {
|
---|
[da1bafb] | 303 | (void) exc_undef;
|
---|
| 304 |
|
---|
| 305 | #if (IVT_ITEMS > 0)
|
---|
| 306 | unsigned int i;
|
---|
| 307 |
|
---|
[c70d693] | 308 | for (i = 0; i < IVT_ITEMS; i++)
|
---|
[b3b7e14a] | 309 | exc_register(i, "undef", false, (iroutine_t) exc_undef);
|
---|
[da1bafb] | 310 | #endif
|
---|
| 311 |
|
---|
[76fca31] | 312 | #ifdef CONFIG_KCONSOLE
|
---|
[0132630] | 313 | cmd_initialize(&exc_info);
|
---|
[aace6624] | 314 | if (!cmd_register(&exc_info))
|
---|
[76fca31] | 315 | printf("Cannot register command %s\n", exc_info.name);
|
---|
| 316 | #endif
|
---|
[973be64e] | 317 | }
|
---|
[aace6624] | 318 |
|
---|
[cc73a8a1] | 319 | /** @}
|
---|
[b45c443] | 320 | */
|
---|