(c) versus (C)
[helenos.git] / kernel / generic / src / interrupt / interrupt.c
blob4df11163332fc887b0dfa84839dfbb6582b5ff1c
1 /*
2 * Copyright (c) 2005 Ondrej Palkovsky
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
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.
29 /** @addtogroup genericinterrupt
30 * @{
32 /**
33 * @file
34 * @brief Interrupt redirector.
36 * This file provides means of registering interrupt handlers
37 * by kernel functions and calling the handlers when interrupts
38 * occur.
41 #include <interrupt.h>
42 #include <debug.h>
43 #include <console/kconsole.h>
44 #include <console/console.h>
45 #include <console/chardev.h>
46 #include <console/cmd.h>
47 #include <panic.h>
48 #include <print.h>
49 #include <symtab.h>
51 static struct {
52 const char *name;
53 iroutine f;
54 } exc_table[IVT_ITEMS];
56 SPINLOCK_INITIALIZE(exctbl_lock);
58 /** Register exception handler
60 * @param n Exception number
61 * @param name Description
62 * @param f Exception handler
64 iroutine exc_register(int n, const char *name, iroutine f)
66 ASSERT(n < IVT_ITEMS);
68 iroutine old;
70 spinlock_lock(&exctbl_lock);
72 old = exc_table[n].f;
73 exc_table[n].f = f;
74 exc_table[n].name = name;
76 spinlock_unlock(&exctbl_lock);
78 return old;
81 /** Dispatch exception according to exception table
83 * Called directly from the assembler code.
84 * CPU is interrupts_disable()'d.
86 void exc_dispatch(int n, istate_t *istate)
88 ASSERT(n < IVT_ITEMS);
90 exc_table[n].f(n + IVT_FIRST, istate);
91 /* This is a safe place to exit exiting thread */
92 if (THREAD && THREAD->interrupted && istate_from_uspace(istate))
93 thread_exit();
96 /** Default 'null' exception handler */
97 static void exc_undef(int n, istate_t *istate)
99 fault_if_from_uspace(istate, "Unhandled exception %d.", n);
100 panic("Unhandled exception %d.", n);
103 /** kconsole cmd - print all exceptions */
104 static int exc_print_cmd(cmd_arg_t *argv)
106 int i;
107 char *symbol;
109 spinlock_lock(&exctbl_lock);
110 printf("Exc Description Handler\n");
111 for (i=0; i < IVT_ITEMS; i++) {
112 symbol = get_symtab_entry((unative_t)exc_table[i].f);
113 if (!symbol)
114 symbol = "not found";
115 printf("%d %s %.*p(%s)\n", i + IVT_FIRST, exc_table[i].name,
116 sizeof(uintptr_t) * 2, exc_table[i].f,symbol);
117 if (!((i+1) % 20)) {
118 printf("Press any key to continue.");
119 spinlock_unlock(&exctbl_lock);
120 getc(stdin);
121 spinlock_lock(&exctbl_lock);
122 printf("\n");
125 spinlock_unlock(&exctbl_lock);
127 return 1;
130 static cmd_info_t exc_info = {
131 .name = "exc",
132 .description = "Print exception table.",
133 .func = exc_print_cmd,
134 .help = NULL,
135 .argc = 0,
136 .argv = NULL
139 /** Initialize generic exception handling support */
140 void exc_init(void)
142 int i;
144 for (i=0;i < IVT_ITEMS; i++)
145 exc_register(i, "undef", (iroutine) exc_undef);
147 cmd_initialize(&exc_info);
148 if (!cmd_register(&exc_info))
149 panic("could not register command %s\n", exc_info.name);
152 /** @}