Xyris  0.5
isr.cpp
Go to the documentation of this file.
1 /**
2  * @file isr.cpp
3  * @author Keeton Feavel ([email protected])
4  * @brief
5  * @version 0.3
6  * @date 2019-11-15
7  *
8  * @copyright Copyright the Xyris Contributors (c) 2019
9  *
10  */
11 #include <Arch/Arch.hpp>
12 #include <Arch/i686/idt.hpp>
13 #include <Arch/i686/isr.hpp>
15 #include <Panic.hpp>
16 
17 namespace Interrupts {
18 
19 // Interrupt handler function pointers
21 
22 extern "C" {
23 
24 /**
25  * @brief Hardware exception handler. Called by each exception handler stub.
26  *
27  * @param regs CPU registers structure. Includes interrupt number.
28  */
29 void exceptionHandler(struct registers* regs)
30 {
31  panic(regs);
32 }
33 
34 /**
35  * @brief Hardware interrupt handler. Called by each interrupt handler stub.
36  *
37  * @param regs CPU registers structure. Includes interrupt number.
38  */
39 void interruptHandler(struct registers* regs)
40 {
41  // After every interrupt we need to send an EOI to the PICs or it won't send another
42  if (regs->int_num >= 0x28) {
43  // Respond to secondard PIC
44  writeByte(0xA0, 0x20);
45  }
46 
47  // Respond to primary PIC
48  writeByte(0x20, 0x20);
49 
50  if (interruptHandlers[regs->int_num]) {
52  handler(regs);
53  }
54 }
55 
56 } // !extern "C"
57 
63 };
64 
68 };
69 
70 void init()
71 {
72  // Set all of the gate addresses
73  for (int exception = 0; exception < 32; exception++) {
74  IDT::setGate(exception, (uint32_t)exceptionHandlerStubs[exception]);
75  }
76 
77  // Remap the programmable interrupt controller
78  writeByte(0x20, 0x11);
79  writeByte(0xA0, 0x11);
80  writeByte(0x21, 0x20);
81  writeByte(0xA1, 0x28);
82  writeByte(0x21, 0x04);
83  writeByte(0xA1, 0x02);
84  writeByte(0x21, 0x01);
85  writeByte(0xA1, 0x01);
86  writeByte(0x21, 0x0);
87  writeByte(0xA1, 0x0);
88 
89  // Install the interrupt requests
90  for (int interrupt = 0; interrupt < ARCH_INTERRUPT_NUM; interrupt++) {
91  IDT::setGate(32 + interrupt, (uint32_t)interruptHandlerStubs[interrupt]);
92  }
93 
94  // Load the IDT now that we've registered all of our IDT, IRQ, and ISR addresses
95  IDT::init();
96 }
97 
98 void registerHandler(uint8_t interrupt, InterruptHandler_t handler)
99 {
100  interruptHandlers[interrupt] = handler;
101 }
102 
103 } // !namespace Interrupts
Interrupts::init
void init()
Definition: isr.cpp:70
Interrupts::exception2
void exception2()
Interrupts::exception30
void exception30()
Interrupts::interrupt15
void interrupt15()
Interrupts::interrupt1
void interrupt1()
Interrupts::InterruptHandler_t
void(* InterruptHandler_t)(struct registers *)
Definition: isr.hpp:79
Interrupts::exception24
void exception24()
Interrupts::interrupt6
void interrupt6()
Interrupts::interruptHandler
void interruptHandler(struct registers *regs)
CPU interrupt handler. Must be available for each interrupt stub to be able to call.
Definition: isr.cpp:39
Interrupts::interrupt5
void interrupt5()
Interrupts::exception17
void exception17()
Interrupts::exception12
void exception12()
Interrupts::exception18
void exception18()
Interrupts
Definition: Interrupts.h:14
Interrupts::exception31
void exception31()
IDT::init
void init()
Calls the lidt instruction and installs the IDT onto the CPU.
Definition: idt.cpp:41
Interrupts::interrupt3
void interrupt3()
registers
A structure definining values for all x86 registers. Cannot be namespaced due to C linkage and ASM in...
Definition: regs.hpp:19
Interrupts::exception19
void exception19()
Interrupts::interrupt8
void interrupt8()
Interrupts::exception7
void exception7()
Interrupts::registerHandler
void registerHandler(uint8_t interrupt, InterruptHandler_t handler)
Definition: isr.cpp:98
Interrupts::interrupt10
void interrupt10()
Arch.hpp
Architecture control and initialization.
Panic.hpp
Kernel panic management.
Interrupts::exception16
void exception16()
Interrupts::interruptHandlerStubs
void(* interruptHandlerStubs[ARCH_INTERRUPT_NUM])(void)
Definition: isr.cpp:65
Interrupts::exception0
void exception0()
Interrupts::interrupt13
void interrupt13()
ARCH_INTERRUPT_NUM
#define ARCH_INTERRUPT_NUM
Definition: isr.hpp:16
Interrupts::exception5
void exception5()
Interrupts::exception1
void exception1()
Interrupts::interrupt4
void interrupt4()
Interrupts::exception26
void exception26()
Interrupts::exception10
void exception10()
Interrupts::interrupt12
void interrupt12()
Interrupts::exception4
void exception4()
Interrupts::interrupt0
void interrupt0()
Interrupts::exception9
void exception9()
Interrupts::exception14
void exception14()
Interrupts::exception8
void exception8()
Interrupts::exception27
void exception27()
ARCH_EXCEPTION_NUM
#define ARCH_EXCEPTION_NUM
Definition: isr.hpp:15
Interrupts::exception28
void exception28()
Interrupts::exception13
void exception13()
Interrupts::exception21
void exception21()
Interrupts::exception15
void exception15()
Interrupts::interrupt2
void interrupt2()
IDT::setGate
void setGate(int n, uint32_t handler_addr)
Sets the handler function (via address) for a specific IDT.
Definition: idt.cpp:21
Interrupts::exception23
void exception23()
Interrupts::interrupt11
void interrupt11()
writeByte
void writeByte(uint16_t port, uint8_t data)
Writes a byte (8 bits) to the CPU bus at a given port address.
Definition: ports.cpp:19
Interrupts::exception29
void exception29()
idt.hpp
Interrupt Descriptor Table header.
registers::int_num
uint32_t int_num
Definition: regs.hpp:22
Interrupts::exception3
void exception3()
Interrupts::exception11
void exception11()
Interrupts::exception22
void exception22()
Interrupts::exception25
void exception25()
panic
void panic(const char *msg)
Halt the system and print the provided message on the panic screen.
Definition: Panic.cpp:82
Interrupts::exception6
void exception6()
Interrupts::interrupt14
void interrupt14()
Interrupts::exception20
void exception20()
isr.hpp
Interrupt Service Routine header.
Interrupts::exceptionHandlerStubs
void(* exceptionHandlerStubs[ARCH_EXCEPTION_NUM])(void)
Definition: isr.cpp:58
Interrupts::interrupt7
void interrupt7()
Interrupts.h
Interrupt/Exception stub functions and handlers.
Interrupts::interruptHandlers
InterruptHandler_t interruptHandlers[256]
Definition: isr.cpp:20
Interrupts::exceptionHandler
void exceptionHandler(struct registers *regs)
CPU exception handler. Must be available for each exception stub to be able to call.
Definition: isr.cpp:29
Interrupts::interrupt9
void interrupt9()