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