source: mainline/kernel/generic/src/interrupt/interrupt.c@ 4a6da62

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 4a6da62 was 181a746, checked in by Adam Hraska <adam.hraska+hos@…>, 13 years ago

rcu: Added preemptible RCU's core API implementation.

  • Property mode set to 100644
File size: 8.3 KB
Line 
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 <synch/mutex.h>
48#include <time/delay.h>
49#include <macros.h>
50#include <panic.h>
51#include <print.h>
52#include <symtab.h>
53#include <proc/thread.h>
54#include <arch/cycle.h>
55#include <str.h>
56#include <trace.h>
57
58exc_table_t exc_table[IVT_ITEMS];
59IRQ_SPINLOCK_INITIALIZE(exctbl_lock);
60
61/** Register exception handler
62 *
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.
70 *
71 */
72iroutine_t exc_register(unsigned int n, const char *name, bool hot,
73 iroutine_t handler)
74{
75#if (IVT_ITEMS > 0)
76 ASSERT(n < IVT_ITEMS);
77#endif
78
79 irq_spinlock_lock(&exctbl_lock, true);
80
81 iroutine_t old = exc_table[n].handler;
82 exc_table[n].handler = handler;
83 exc_table[n].name = name;
84 exc_table[n].hot = hot;
85 exc_table[n].cycles = 0;
86 exc_table[n].count = 0;
87
88 irq_spinlock_unlock(&exctbl_lock, true);
89
90 return old;
91}
92
93/** Dispatch exception according to exception table
94 *
95 * Called directly from the assembler code.
96 * CPU is interrupts_disable()'d.
97 *
98 */
99NO_TRACE void exc_dispatch(unsigned int n, istate_t *istate)
100{
101#if (IVT_ITEMS > 0)
102 ASSERT(n < IVT_ITEMS);
103#endif
104
105 /* Account user cycles */
106 if (THREAD) {
107 irq_spinlock_lock(&THREAD->lock, false);
108 thread_update_accounting(true);
109 irq_spinlock_unlock(&THREAD->lock, false);
110 }
111
112 /* Account CPU usage if it woke up from sleep */
113 if (CPU && CPU->idle) {
114 irq_spinlock_lock(&CPU->lock, false);
115 uint64_t now = get_cycle();
116 CPU->idle_cycles += now - CPU->last_cycle;
117 CPU->last_cycle = now;
118 CPU->idle = false;
119 irq_spinlock_unlock(&CPU->lock, false);
120 }
121
122 uint64_t begin_cycle = get_cycle();
123
124#ifdef CONFIG_UDEBUG
125 if (THREAD)
126 THREAD->udebug.uspace_state = istate;
127#endif
128
129 exc_table[n].handler(n + IVT_FIRST, istate);
130
131#ifdef CONFIG_UDEBUG
132 if (THREAD)
133 THREAD->udebug.uspace_state = NULL;
134#endif
135
136 /* This is a safe place to exit exiting thread */
137 if ((THREAD) && (THREAD->interrupted) && (istate_from_uspace(istate)))
138 thread_exit();
139
140 /* Account exception handling */
141 uint64_t end_cycle = get_cycle();
142
143 irq_spinlock_lock(&exctbl_lock, false);
144 exc_table[n].cycles += end_cycle - begin_cycle;
145 exc_table[n].count++;
146 irq_spinlock_unlock(&exctbl_lock, false);
147
148 /* Do not charge THREAD for exception cycles */
149 if (THREAD) {
150 irq_spinlock_lock(&THREAD->lock, false);
151 THREAD->last_cycle = end_cycle;
152 irq_spinlock_unlock(&THREAD->lock, false);
153 }
154}
155
156/** Default 'null' exception handler
157 *
158 */
159NO_TRACE static void exc_undef(unsigned int n, istate_t *istate)
160{
161 fault_if_from_uspace(istate, "Unhandled exception %u.", n);
162 panic_badtrap(istate, n, "Unhandled exception %u.", n);
163}
164
165/** Terminate thread and task if exception came from userspace.
166 *
167 */
168NO_TRACE void fault_if_from_uspace(istate_t *istate, const char *fmt, ...)
169{
170 if (!istate_from_uspace(istate))
171 return;
172
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));
176
177 istate_decode(istate);
178 stack_trace_istate(istate);
179
180 printf("Kill message: ");
181
182 va_list args;
183 va_start(args, fmt);
184 vprintf(fmt, args);
185 va_end(args);
186 printf("\n");
187
188 task_kill_self(true);
189}
190
191/** Get istate structure of a thread.
192 *
193 * Get pointer to the istate structure at the bottom of the kernel stack.
194 *
195 * This function can be called in interrupt or user context. In interrupt
196 * context the istate structure is created by the low-level exception
197 * handler. In user context the istate structure is created by the
198 * low-level syscall handler.
199 */
200istate_t *istate_get(thread_t *thread)
201{
202 /*
203 * The istate structure should be right at the bottom of the kernel
204 * stack.
205 */
206 return (istate_t *) ((uint8_t *)
207 thread->kstack + STACK_SIZE - sizeof(istate_t));
208}
209
210#ifdef CONFIG_KCONSOLE
211
212static char flag_buf[MAX_CMDLINE + 1];
213
214/** Print all exceptions
215 *
216 */
217NO_TRACE static int cmd_exc_print(cmd_arg_t *argv)
218{
219 bool excs_all;
220
221 if (str_cmp(flag_buf, "-a") == 0)
222 excs_all = true;
223 else if (str_cmp(flag_buf, "") == 0)
224 excs_all = false;
225 else {
226 printf("Unknown argument \"%s\".\n", flag_buf);
227 return 1;
228 }
229
230#if (IVT_ITEMS > 0)
231 unsigned int i;
232 unsigned int rows;
233
234 irq_spinlock_lock(&exctbl_lock, true);
235
236#ifdef __32_BITS__
237 printf("[exc ] [description ] [count ] [cycles ]"
238 " [handler ] [symbol\n");
239 rows = 1;
240#endif
241
242#ifdef __64_BITS__
243 printf("[exc ] [description ] [count ] [cycles ]"
244 " [handler ]\n");
245 printf(" [symbol\n");
246 rows = 2;
247#endif
248
249 for (i = 0; i < IVT_ITEMS; i++) {
250 if ((!excs_all) && (!exc_table[i].hot))
251 continue;
252
253 uint64_t count;
254 char count_suffix;
255
256 order_suffix(exc_table[i].count, &count, &count_suffix);
257
258 uint64_t cycles;
259 char cycles_suffix;
260
261 order_suffix(exc_table[i].cycles, &cycles, &cycles_suffix);
262
263 const char *symbol =
264 symtab_fmt_name_lookup((sysarg_t) exc_table[i].handler);
265
266#ifdef __32_BITS__
267 printf("%-8u %-20s %9" PRIu64 "%c %9" PRIu64 "%c %10p %s\n",
268 i + IVT_FIRST, exc_table[i].name, count, count_suffix,
269 cycles, cycles_suffix, exc_table[i].handler, symbol);
270
271 PAGING(rows, 1, irq_spinlock_unlock(&exctbl_lock, true),
272 irq_spinlock_lock(&exctbl_lock, true));
273#endif
274
275#ifdef __64_BITS__
276 printf("%-8u %-20s %9" PRIu64 "%c %9" PRIu64 "%c %18p\n",
277 i + IVT_FIRST, exc_table[i].name, count, count_suffix,
278 cycles, cycles_suffix, exc_table[i].handler);
279 printf(" %s\n", symbol);
280
281 PAGING(rows, 2, irq_spinlock_unlock(&exctbl_lock, true),
282 irq_spinlock_lock(&exctbl_lock, true));
283#endif
284 }
285
286 irq_spinlock_unlock(&exctbl_lock, true);
287#else /* (IVT_ITEMS > 0) */
288
289 printf("No exception table%s.\n", excs_all ? " (showing all exceptions)" : "");
290
291#endif /* (IVT_ITEMS > 0) */
292
293 return 1;
294}
295
296static cmd_arg_t exc_argv = {
297 .type = ARG_TYPE_STRING_OPTIONAL,
298 .buffer = flag_buf,
299 .len = sizeof(flag_buf)
300};
301
302static cmd_info_t exc_info = {
303 .name = "exc",
304 .description = "Print exception table (use -a for all exceptions).",
305 .func = cmd_exc_print,
306 .help = NULL,
307 .argc = 1,
308 .argv = &exc_argv
309};
310
311#endif /* CONFIG_KCONSOLE */
312
313/** Initialize generic exception handling support
314 *
315 */
316void exc_init(void)
317{
318 (void) exc_undef;
319
320#if (IVT_ITEMS > 0)
321 unsigned int i;
322
323 for (i = 0; i < IVT_ITEMS; i++)
324 exc_register(i, "undef", false, (iroutine_t) exc_undef);
325#endif
326
327#ifdef CONFIG_KCONSOLE
328 cmd_initialize(&exc_info);
329 if (!cmd_register(&exc_info))
330 printf("Cannot register command %s\n", exc_info.name);
331#endif
332}
333
334/** @}
335 */
Note: See TracBrowser for help on using the repository browser.