[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 |
|
---|
[174156fd] | 29 | /** @addtogroup kernel_generic_interrupt
|
---|
[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 |
|
---|
[63e27ef] | 42 | #include <assert.h>
|
---|
[973be64e] | 43 | #include <interrupt.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>
|
---|
[bab75df6] | 51 | #include <stdio.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 |
|
---|
[dfb16c4] | 60 | /*
|
---|
| 61 | * If IVT_ITEMS is zero (e.g. for special/abs32le) we hide completely any
|
---|
| 62 | * access to the exception table array and panic if the function is called
|
---|
| 63 | * at all. It also silences (correct) compiler warnings about possible
|
---|
| 64 | * out-of-bound array access.
|
---|
| 65 | */
|
---|
| 66 |
|
---|
[8eec3c8] | 67 | exc_table_t exc_table[IVT_ITEMS];
|
---|
| 68 | IRQ_SPINLOCK_INITIALIZE(exctbl_lock);
|
---|
[aace6624] | 69 |
|
---|
| 70 | /** Register exception handler
|
---|
[da1bafb] | 71 | *
|
---|
[b3b7e14a] | 72 | * @param n Exception number.
|
---|
| 73 | * @param name Description.
|
---|
| 74 | * @param hot Whether the exception is actually handled
|
---|
| 75 | * in any meaningful way.
|
---|
| 76 | * @param handler New exception handler.
|
---|
| 77 | *
|
---|
| 78 | * @return Previously registered exception handler.
|
---|
[da1bafb] | 79 | *
|
---|
[aace6624] | 80 | */
|
---|
[b3b7e14a] | 81 | iroutine_t exc_register(unsigned int n, const char *name, bool hot,
|
---|
| 82 | iroutine_t handler)
|
---|
[973be64e] | 83 | {
|
---|
[b3b7e14a] | 84 | #if (IVT_ITEMS > 0)
|
---|
[63e27ef] | 85 | assert(n < IVT_ITEMS);
|
---|
[a35b458] | 86 |
|
---|
[8eec3c8] | 87 | irq_spinlock_lock(&exctbl_lock, true);
|
---|
[a35b458] | 88 |
|
---|
[b3b7e14a] | 89 | iroutine_t old = exc_table[n].handler;
|
---|
| 90 | exc_table[n].handler = handler;
|
---|
[aace6624] | 91 | exc_table[n].name = name;
|
---|
[b3b7e14a] | 92 | exc_table[n].hot = hot;
|
---|
[8eec3c8] | 93 | exc_table[n].cycles = 0;
|
---|
| 94 | exc_table[n].count = 0;
|
---|
[a35b458] | 95 |
|
---|
[8eec3c8] | 96 | irq_spinlock_unlock(&exctbl_lock, true);
|
---|
[a35b458] | 97 |
|
---|
[973be64e] | 98 | return old;
|
---|
[dfb16c4] | 99 | #else
|
---|
| 100 | panic("No space for any exception handler, cannot register.");
|
---|
| 101 | #endif
|
---|
[973be64e] | 102 | }
|
---|
[a7fdfe1] | 103 |
|
---|
[aace6624] | 104 | /** Dispatch exception according to exception table
|
---|
| 105 | *
|
---|
[973be64e] | 106 | * Called directly from the assembler code.
|
---|
| 107 | * CPU is interrupts_disable()'d.
|
---|
[da1bafb] | 108 | *
|
---|
[973be64e] | 109 | */
|
---|
[8df5f20] | 110 | _NO_TRACE void exc_dispatch(unsigned int n, istate_t *istate)
|
---|
[973be64e] | 111 | {
|
---|
[b3b7e14a] | 112 | #if (IVT_ITEMS > 0)
|
---|
[63e27ef] | 113 | assert(n < IVT_ITEMS);
|
---|
[a35b458] | 114 |
|
---|
[a2a00e8] | 115 | /* Account user cycles */
|
---|
[11909ce3] | 116 | if (THREAD)
|
---|
[b584cd4] | 117 | thread_update_accounting(true);
|
---|
[a35b458] | 118 |
|
---|
[181a746] | 119 | /* Account CPU usage if it woke up from sleep */
|
---|
[4760793] | 120 | if (CPU && CPU_LOCAL->idle) {
|
---|
[181a746] | 121 | uint64_t now = get_cycle();
|
---|
[4760793] | 122 | atomic_time_increment(&CPU->idle_cycles, now - CPU_LOCAL->last_cycle);
|
---|
| 123 | CPU_LOCAL->last_cycle = now;
|
---|
| 124 | CPU_LOCAL->idle = false;
|
---|
[d0c82c5] | 125 | }
|
---|
[a35b458] | 126 |
|
---|
[b584cd4] | 127 | uint64_t begin_cycle = get_cycle();
|
---|
[a35b458] | 128 |
|
---|
[3ff2b54] | 129 | #ifdef CONFIG_UDEBUG
|
---|
[da1bafb] | 130 | if (THREAD)
|
---|
| 131 | THREAD->udebug.uspace_state = istate;
|
---|
[3ff2b54] | 132 | #endif
|
---|
[a35b458] | 133 |
|
---|
[b3b7e14a] | 134 | exc_table[n].handler(n + IVT_FIRST, istate);
|
---|
[a35b458] | 135 |
|
---|
[3ff2b54] | 136 | #ifdef CONFIG_UDEBUG
|
---|
[da1bafb] | 137 | if (THREAD)
|
---|
| 138 | THREAD->udebug.uspace_state = NULL;
|
---|
[3ff2b54] | 139 | #endif
|
---|
[a35b458] | 140 |
|
---|
[0dbc4e7] | 141 | /* This is a safe place to exit exiting thread */
|
---|
[da1bafb] | 142 | if ((THREAD) && (THREAD->interrupted) && (istate_from_uspace(istate)))
|
---|
[0dbc4e7] | 143 | thread_exit();
|
---|
[a35b458] | 144 |
|
---|
[8eec3c8] | 145 | /* Account exception handling */
|
---|
| 146 | uint64_t end_cycle = get_cycle();
|
---|
[a35b458] | 147 |
|
---|
[decfbe56] | 148 | irq_spinlock_lock(&exctbl_lock, false);
|
---|
[8eec3c8] | 149 | exc_table[n].cycles += end_cycle - begin_cycle;
|
---|
| 150 | exc_table[n].count++;
|
---|
[decfbe56] | 151 | irq_spinlock_unlock(&exctbl_lock, false);
|
---|
[a35b458] | 152 |
|
---|
[8eec3c8] | 153 | /* Do not charge THREAD for exception cycles */
|
---|
[11909ce3] | 154 | if (THREAD)
|
---|
[8eec3c8] | 155 | THREAD->last_cycle = end_cycle;
|
---|
[dfb16c4] | 156 | #else
|
---|
| 157 | panic("No space for any exception handler, yet we want to handle some exception.");
|
---|
| 158 | #endif
|
---|
[aace6624] | 159 | }
|
---|
| 160 |
|
---|
[da1bafb] | 161 | /** Default 'null' exception handler
|
---|
| 162 | *
|
---|
| 163 | */
|
---|
[8df5f20] | 164 | _NO_TRACE static void exc_undef(unsigned int n, istate_t *istate)
|
---|
[aace6624] | 165 | {
|
---|
[214ec25c] | 166 | fault_if_from_uspace(istate, "Unhandled exception %u.", n);
|
---|
[4d1be48] | 167 | panic_badtrap(istate, n, "Unhandled exception %u.", n);
|
---|
[aace6624] | 168 | }
|
---|
| 169 |
|
---|
[8df5f20] | 170 | static _NO_TRACE void
|
---|
[18b6a88] | 171 | fault_from_uspace_core(istate_t *istate, const char *fmt, va_list args)
|
---|
[a074b4f] | 172 | {
|
---|
[908bb96] | 173 | printf("Task %s (%" PRIu64 ") killed due to an exception at "
|
---|
| 174 | "program counter %p.\n", TASK->name, TASK->taskid,
|
---|
| 175 | (void *) istate_get_pc(istate));
|
---|
[a35b458] | 176 |
|
---|
[908bb96] | 177 | istate_decode(istate);
|
---|
| 178 | stack_trace_istate(istate);
|
---|
[a35b458] | 179 |
|
---|
[908bb96] | 180 | printf("Kill message: ");
|
---|
| 181 | vprintf(fmt, args);
|
---|
| 182 | printf("\n");
|
---|
[a35b458] | 183 |
|
---|
[8f4f444] | 184 | task_kill_self(true);
|
---|
| 185 | }
|
---|
| 186 |
|
---|
| 187 | /** Terminate thread and task after the exception came from userspace.
|
---|
| 188 | *
|
---|
| 189 | */
|
---|
[8df5f20] | 190 | _NO_TRACE void fault_from_uspace(istate_t *istate, const char *fmt, ...)
|
---|
[8f4f444] | 191 | {
|
---|
[da1bafb] | 192 | va_list args;
|
---|
[8f4f444] | 193 |
|
---|
[a074b4f] | 194 | va_start(args, fmt);
|
---|
[8f4f444] | 195 | fault_from_uspace_core(istate, fmt, args);
|
---|
[a074b4f] | 196 | va_end(args);
|
---|
[8f4f444] | 197 | }
|
---|
| 198 |
|
---|
| 199 | /** Terminate thread and task if exception came from userspace.
|
---|
| 200 | *
|
---|
| 201 | */
|
---|
[8df5f20] | 202 | _NO_TRACE void fault_if_from_uspace(istate_t *istate, const char *fmt, ...)
|
---|
[8f4f444] | 203 | {
|
---|
| 204 | if (!istate_from_uspace(istate))
|
---|
| 205 | return;
|
---|
[a35b458] | 206 |
|
---|
[8f4f444] | 207 | va_list args;
|
---|
| 208 | va_start(args, fmt);
|
---|
| 209 | fault_from_uspace_core(istate, fmt, args);
|
---|
| 210 | va_end(args);
|
---|
[a074b4f] | 211 | }
|
---|
| 212 |
|
---|
[1ad52de] | 213 | /** Get istate structure of a thread.
|
---|
| 214 | *
|
---|
| 215 | * Get pointer to the istate structure at the bottom of the kernel stack.
|
---|
| 216 | *
|
---|
| 217 | * This function can be called in interrupt or user context. In interrupt
|
---|
| 218 | * context the istate structure is created by the low-level exception
|
---|
| 219 | * handler. In user context the istate structure is created by the
|
---|
| 220 | * low-level syscall handler.
|
---|
| 221 | */
|
---|
[63594c0] | 222 | istate_t *istate_get(thread_t *thread)
|
---|
| 223 | {
|
---|
[1ad52de] | 224 | /*
|
---|
| 225 | * The istate structure should be right at the bottom of the kernel
|
---|
[e4d96e9] | 226 | * memory stack.
|
---|
[1ad52de] | 227 | */
|
---|
[e4d96e9] | 228 | return (istate_t *) &thread->kstack[MEM_STACK_SIZE - sizeof(istate_t)];
|
---|
[63594c0] | 229 | }
|
---|
| 230 |
|
---|
[76fca31] | 231 | #ifdef CONFIG_KCONSOLE
|
---|
| 232 |
|
---|
[b3b7e14a] | 233 | static char flag_buf[MAX_CMDLINE + 1];
|
---|
| 234 |
|
---|
[da1bafb] | 235 | /** Print all exceptions
|
---|
| 236 | *
|
---|
| 237 | */
|
---|
[8df5f20] | 238 | _NO_TRACE static int cmd_exc_print(cmd_arg_t *argv)
|
---|
[aace6624] | 239 | {
|
---|
[b3b7e14a] | 240 | bool excs_all;
|
---|
[a35b458] | 241 |
|
---|
[b3b7e14a] | 242 | if (str_cmp(flag_buf, "-a") == 0)
|
---|
| 243 | excs_all = true;
|
---|
| 244 | else if (str_cmp(flag_buf, "") == 0)
|
---|
| 245 | excs_all = false;
|
---|
| 246 | else {
|
---|
| 247 | printf("Unknown argument \"%s\".\n", flag_buf);
|
---|
| 248 | return 1;
|
---|
| 249 | }
|
---|
[a35b458] | 250 |
|
---|
[6c441cf8] | 251 | #if (IVT_ITEMS > 0)
|
---|
[43b1e86] | 252 | unsigned int i;
|
---|
[b3b7e14a] | 253 | unsigned int rows;
|
---|
[a35b458] | 254 |
|
---|
[8eec3c8] | 255 | irq_spinlock_lock(&exctbl_lock, true);
|
---|
[a35b458] | 256 |
|
---|
[c70d693] | 257 | #ifdef __32_BITS__
|
---|
[b3b7e14a] | 258 | printf("[exc ] [description ] [count ] [cycles ]"
|
---|
| 259 | " [handler ] [symbol\n");
|
---|
| 260 | rows = 1;
|
---|
[c70d693] | 261 | #endif
|
---|
[a35b458] | 262 |
|
---|
[c70d693] | 263 | #ifdef __64_BITS__
|
---|
[b3b7e14a] | 264 | printf("[exc ] [description ] [count ] [cycles ]"
|
---|
| 265 | " [handler ]\n");
|
---|
| 266 | printf(" [symbol\n");
|
---|
| 267 | rows = 2;
|
---|
[c70d693] | 268 | #endif
|
---|
[a35b458] | 269 |
|
---|
[43b1e86] | 270 | for (i = 0; i < IVT_ITEMS; i++) {
|
---|
[b3b7e14a] | 271 | if ((!excs_all) && (!exc_table[i].hot))
|
---|
| 272 | continue;
|
---|
[a35b458] | 273 |
|
---|
[8eec3c8] | 274 | uint64_t count;
|
---|
| 275 | char count_suffix;
|
---|
[a35b458] | 276 |
|
---|
[8eec3c8] | 277 | order_suffix(exc_table[i].count, &count, &count_suffix);
|
---|
[a35b458] | 278 |
|
---|
[8eec3c8] | 279 | uint64_t cycles;
|
---|
| 280 | char cycles_suffix;
|
---|
[a35b458] | 281 |
|
---|
[8eec3c8] | 282 | order_suffix(exc_table[i].cycles, &cycles, &cycles_suffix);
|
---|
[a35b458] | 283 |
|
---|
[8eec3c8] | 284 | const char *symbol =
|
---|
[96b02eb9] | 285 | symtab_fmt_name_lookup((sysarg_t) exc_table[i].handler);
|
---|
[a35b458] | 286 |
|
---|
[c70d693] | 287 | #ifdef __32_BITS__
|
---|
[b3b7e14a] | 288 | printf("%-8u %-20s %9" PRIu64 "%c %9" PRIu64 "%c %10p %s\n",
|
---|
[8eec3c8] | 289 | i + IVT_FIRST, exc_table[i].name, count, count_suffix,
|
---|
[b3b7e14a] | 290 | cycles, cycles_suffix, exc_table[i].handler, symbol);
|
---|
[a35b458] | 291 |
|
---|
[b3b7e14a] | 292 | PAGING(rows, 1, irq_spinlock_unlock(&exctbl_lock, true),
|
---|
| 293 | irq_spinlock_lock(&exctbl_lock, true));
|
---|
[c70d693] | 294 | #endif
|
---|
[a35b458] | 295 |
|
---|
[c70d693] | 296 | #ifdef __64_BITS__
|
---|
[b3b7e14a] | 297 | printf("%-8u %-20s %9" PRIu64 "%c %9" PRIu64 "%c %18p\n",
|
---|
[8eec3c8] | 298 | i + IVT_FIRST, exc_table[i].name, count, count_suffix,
|
---|
[b3b7e14a] | 299 | cycles, cycles_suffix, exc_table[i].handler);
|
---|
| 300 | printf(" %s\n", symbol);
|
---|
[a35b458] | 301 |
|
---|
[b3b7e14a] | 302 | PAGING(rows, 2, irq_spinlock_unlock(&exctbl_lock, true),
|
---|
| 303 | irq_spinlock_lock(&exctbl_lock, true));
|
---|
| 304 | #endif
|
---|
[aace6624] | 305 | }
|
---|
[a35b458] | 306 |
|
---|
[8eec3c8] | 307 | irq_spinlock_unlock(&exctbl_lock, true);
|
---|
[b3b7e14a] | 308 | #else /* (IVT_ITEMS > 0) */
|
---|
[a35b458] | 309 |
|
---|
[b3b7e14a] | 310 | printf("No exception table%s.\n", excs_all ? " (showing all exceptions)" : "");
|
---|
[a35b458] | 311 |
|
---|
[b3b7e14a] | 312 | #endif /* (IVT_ITEMS > 0) */
|
---|
[a35b458] | 313 |
|
---|
[aace6624] | 314 | return 1;
|
---|
| 315 | }
|
---|
| 316 |
|
---|
[b3b7e14a] | 317 | static cmd_arg_t exc_argv = {
|
---|
| 318 | .type = ARG_TYPE_STRING_OPTIONAL,
|
---|
| 319 | .buffer = flag_buf,
|
---|
| 320 | .len = sizeof(flag_buf)
|
---|
| 321 | };
|
---|
| 322 |
|
---|
[aace6624] | 323 | static cmd_info_t exc_info = {
|
---|
[0132630] | 324 | .name = "exc",
|
---|
[b3b7e14a] | 325 | .description = "Print exception table (use -a for all exceptions).",
|
---|
[76fca31] | 326 | .func = cmd_exc_print,
|
---|
[aace6624] | 327 | .help = NULL,
|
---|
[b3b7e14a] | 328 | .argc = 1,
|
---|
| 329 | .argv = &exc_argv
|
---|
[aace6624] | 330 | };
|
---|
| 331 |
|
---|
[da1bafb] | 332 | #endif /* CONFIG_KCONSOLE */
|
---|
[76fca31] | 333 |
|
---|
[da1bafb] | 334 | /** Initialize generic exception handling support
|
---|
| 335 | *
|
---|
| 336 | */
|
---|
[aace6624] | 337 | void exc_init(void)
|
---|
| 338 | {
|
---|
[da1bafb] | 339 | (void) exc_undef;
|
---|
[a35b458] | 340 |
|
---|
[da1bafb] | 341 | #if (IVT_ITEMS > 0)
|
---|
| 342 | unsigned int i;
|
---|
[a35b458] | 343 |
|
---|
[c70d693] | 344 | for (i = 0; i < IVT_ITEMS; i++)
|
---|
[b3b7e14a] | 345 | exc_register(i, "undef", false, (iroutine_t) exc_undef);
|
---|
[da1bafb] | 346 | #endif
|
---|
[a35b458] | 347 |
|
---|
[76fca31] | 348 | #ifdef CONFIG_KCONSOLE
|
---|
[0132630] | 349 | cmd_initialize(&exc_info);
|
---|
[aace6624] | 350 | if (!cmd_register(&exc_info))
|
---|
[76fca31] | 351 | printf("Cannot register command %s\n", exc_info.name);
|
---|
| 352 | #endif
|
---|
[973be64e] | 353 | }
|
---|
[aace6624] | 354 |
|
---|
[cc73a8a1] | 355 | /** @}
|
---|
[b45c443] | 356 | */
|
---|