2 * GeekOS interrupt handling data structures and functions
3 * Copyright (c) 2001, David H. Hovemeyer <daveho@cs.umd.edu>
6 * This is free software. You are permitted to use,
7 * redistribute, and modify it as specified in the file "COPYING".
11 * This module describes the C interface which must be implemented
12 * by interrupt handlers, and has the initialization function
13 * for the interrupt system as a whole.
19 #include <geekos/kassert.h>
20 #include <geekos/ktypes.h>
21 #include <geekos/defs.h>
24 // to stop the compilation warnings
25 extern void Print(const char* fmt, ...);
26 extern void Set_Current_Attr(uchar_t attrib);
29 * This struct reflects the contents of the stack when
30 * a C interrupt handler function is called.
31 * It must be kept up to date with the code in "lowlevel.asm".
33 struct Interrupt_State {
35 * The register contents at the time of the exception.
36 * We save these explicitly.
51 * We explicitly push the interrupt number.
52 * This makes it easy for the handler function to determine
53 * which interrupt occurred.
58 * This may be pushed by the processor; if not, we push
59 * a dummy error code, so the stack layout is the same
60 * for every type of interrupt.
64 /* These are always pushed on the stack by the processor. */
71 * An interrupt that occurred in user mode.
72 * If Is_User_Interrupt(state) returns true, then the
73 * Interrupt_State object may be cast to this kind of struct.
75 struct User_Interrupt_State {
76 struct Interrupt_State state;
81 static __inline__ bool Is_User_Interrupt(struct Interrupt_State *state)
83 return (state->cs & 3) == USER_PRIVILEGE;
88 * The signature of an interrupt handler.
90 typedef void (*Interrupt_Handler)(struct Interrupt_State* state);
93 * Perform all low- and high-level initialization of the
96 void Init_Interrupts(void);
99 * Query whether or not interrupts are currently enabled.
101 bool Interrupts_Enabled(void);
106 static __inline__ void __Disable_Interrupts(void)
108 __asm__ __volatile__ ("cli");
110 #define Disable_Interrupts() \
112 KASSERT(Interrupts_Enabled()); \
113 __Disable_Interrupts(); \
117 * Unblock interrupts.
119 static __inline__ void __Enable_Interrupts(void)
121 __asm__ __volatile__ ("sti");
123 #define Enable_Interrupts() \
125 KASSERT(!Interrupts_Enabled()); \
126 __Enable_Interrupts(); \
130 * Dump interrupt state struct to screen
132 void Dump_Interrupt_State(struct Interrupt_State* state);
135 * Start interrupt-atomic region.
136 * @return true if interrupts were enabled at beginning of call,
139 static __inline__ bool Begin_Int_Atomic(void)
141 bool enabled = Interrupts_Enabled();
143 Disable_Interrupts();
148 * End interrupt-atomic region.
149 * @param iflag the value returned from the original Begin_Int_Atomic() call.
151 static __inline__ void End_Int_Atomic(bool iflag)
153 KASSERT(!Interrupts_Enabled());
155 /* Interrupts were originally enabled, so turn them back on */
160 #define EXCEPTION_DE 0 // Divide by zero
161 #define EXCEPTION_DB 1 // reserved
162 #define EXCEPTION_NMI 2 // NMI
163 #define EXCEPTION_BP 3 // Breakpoint
164 #define EXCEPTION_OF 4 // Overflow
165 #define EXCEPTION_BR 5 // Bound range exceeded
166 #define EXCEPTION_UD 6 // Undefined opcode
167 #define EXCEPTION_NM 7 // Math coprocessor gone missing
168 #define EXCEPTION_DF 8 // Double fault
169 #define EXCEPTION_CO 9 // Coprocessor segment overrrun
170 #define EXCEPTION_TS 10 // Invalid TSS
171 #define EXCEPTION_NP 11 // Segment not present
172 #define EXCEPTION_SS 12 // Stack segment fault
173 #define EXCEPTION_GP 13 // General Protection fault
174 #define EXCEPTION_PF 14 // Page Fault
175 #define EXCEPTION_RES 15 // reserved
176 #define EXCEPTION_MF 16 // Math fault
177 #define EXCEPTION_AC 17 // Alignment check
178 #define EXCEPTION_MC 18 // Machine check
179 #define EXCEPTION_XF 19 // SIMD FP exception
185 #endif /* GEEKOS_INT_H */