White space fixes, detab.
[AROS.git] / rom / kernel / addexceptionhandler.c
blob1ebf0ef0f8e1a405cfd6a73d4d7ee2612c84457f
1 #include <aros/kernel.h>
2 #include <proto/exec.h>
4 #include <inttypes.h>
6 #include <kernel_base.h>
7 #include <kernel_cpu.h>
8 #include <kernel_debug.h>
9 #include <kernel_interrupts.h>
10 #include <kernel_objects.h>
12 /* We use own implementation of bug(), so we don't need aros/debug.h */
13 #define D(x)
15 /*****************************************************************************
17 NAME */
18 #include <proto/kernel.h>
20 AROS_LH4(void *, KrnAddExceptionHandler,
22 /* SYNOPSIS */
23 AROS_LHA(uint8_t, num, D0),
24 AROS_LHA(exhandler_t *, handler, A0),
25 AROS_LHA(void *, handlerData, A1),
26 AROS_LHA(void *, handlerData2, A2),
28 /* LOCATION */
29 struct KernelBase *, KernelBase, 14, Kernel)
31 /* FUNCTION
32 Add a raw CPU exception handler to the chain of handlers.
34 INPUTS
35 num - CPU-specific exception number
36 handler - Pointer to a handler function
37 handlerData,
38 handlerData2 - User-defined data which is passed to the
39 handler.
41 Handler function uses a C calling convention and must be
42 declared as follows:
44 int ExceptionHandler(void *ctx, void *handlerData, void *handlerData2)
46 handlerData and handlerData2 will be values passed to the
47 KrnAddExceptionHandler() function. ctx is a CPU context handle.
48 Consider this parameter private and reserved for now.
50 Exception handler should return nonzero value if it processes the
51 exception and wants to continue program execution. Otherwise it should
52 return zero. If all exception handlers in the chain return zero, the
53 exception will be passed on to exec.library trap handler pointed to
54 by tc_TrapCode field of task structure.
56 RESULT
57 An opaque handle that can be used for handler removal or NULL in case
58 of failure (like unsupported exception number).
60 NOTES
61 The specification of this function is preliminary and subject to change.
62 Consider it private for now.
64 EXAMPLE
66 BUGS
68 SEE ALSO
69 KrnRemExceptionHandler()
71 INTERNALS
73 ******************************************************************************/
75 AROS_LIBFUNC_INIT
77 struct IntrNode *handle = NULL;
78 D(bug("[KRN] KrnAddExceptionHandler(%02x, %012p, %012p, %012p):\n", num, handler, handlerData, handlerData2));
80 if (num < EXCEPTIONS_COUNT)
82 /* Go to supervisor mode */
83 (void)goSuper();
85 /* Allocate protected memory, accessible only in supervisor mode */
86 handle = krnAllocIntrNode();
87 D(bug("[KRN] handle=%012p\n", handle));
89 if (handle)
91 handle->in_Handler = handler;
92 handle->in_HandlerData = handlerData;
93 handle->in_HandlerData2 = handlerData2;
94 handle->in_type = it_exception;
95 handle->in_nr = num;
97 Disable();
98 ADDHEAD(&KernelBase->kb_Exceptions[num], &handle->in_Node);
99 Enable();
102 goUser();
105 return handle;
107 AROS_LIBFUNC_EXIT
110 /* Run exception handlers and accumulate return value */
111 int krnRunExceptionHandlers(struct KernelBase *KernelBase, uint8_t exception, void *ctx)
113 struct IntrNode *in, *in2;
114 int ret = 0;
116 /* We can be called really early. Protect against this. */
117 if (!KernelBase)
118 return 0;
120 ForeachNodeSafe(&KernelBase->kb_Exceptions[exception], in, in2)
122 exhandler_t h = in->in_Handler;
124 if (h)
125 ret |= h(ctx, in->in_HandlerData, in->in_HandlerData2);
128 return ret;