Max OS 0.3
Loading...
Searching...
No Matches
interrupts.h
Go to the documentation of this file.
1
9#ifndef MAX_OS_HARDWARECOMMUNICATION_INTERRUPTS_H
10#define MAX_OS_HARDWARECOMMUNICATION_INTERRUPTS_H
11
12#include <cstdint>
13
14#include <libcommon/port.h>
15#include <libcommon/inputStream.h>
16#include <libcommon/outputStream.h>
17#include <libcommon/map.h>
18
19#include <system/gdt.h>
20#include <system/cpu.h>
22
23
24namespace MaxOS::hardwarecommunication {
25
26 class InterruptManager;
27
33 {
34 KERNEL_ONLY,
35 RING_1,
36 RING_2,
37 USERSPACE
38 };
39
45 protected:
47
48 explicit InterruptHandler(uint8_t interrupt_number, int64_t redirect = -1, uint64_t redirect_index = 0);
49 virtual ~InterruptHandler();
50
51 public:
52 virtual void handle_interrupt();
54
55 };
56
62
63 public:
64 ExceptionHandler(uint8_t interrupt_number);
66
67 virtual string get_message(system::cpu_status_t* status);
69
70 };
71
77 public:
78
81
82 string get_message(system::cpu_status_t* status) final;
83 };
84
90 public:
91
94
95 string get_message(system::cpu_status_t* status) final;
96 };
97
103 public:
104
107
108 string get_message(system::cpu_status_t* status) final;
109 };
110
116 public:
117
120
121 string get_message(system::cpu_status_t* status) final;
122 };
123
129 public:
130
133
135 };
136
137
145 typedef struct PACKED IDTR {
146 uint16_t limit;
147 uint64_t base;
148 } idtr_t;
149
157 typedef struct PACKED InterruptDescriptor {
158
161 uint8_t ist;
162 uint8_t flags;
165 uint32_t reserved;
166
167 } interrupt_descriptor_t;
168
169 constexpr uint16_t HARDWARE_INTERRUPT_OFFSET = 0x20;
170 constexpr uint16_t MAX_INTERRUPT_HANDLERS = 256;
171
177
178 private:
179
180 // Exception handles
181 DivideByZeroHandler m_db0_handler;
182 InvaildOpCodeHandler m_opce_handler;
183 GeneralProtectionHandler m_gpe_handler;
184 PageFaultHandler m_page_fault_handler;
185 CPUStopHandler m_cpu_stop_handler;
186
187 public:
190
191 };
192
201 friend class InterruptHandler;
202
203 private:
205
206 static system::cpu_status_t* page_fault(system::cpu_status_t* status);
207 static system::cpu_status_t* general_protection_fault(system::cpu_status_t* status);
208
209 protected:
210
212
215
216 static void set_interrupt_descriptor_table_entry(uint8_t interrupt, void (* handler)(), InterruptPrivilegeLevel descriptor_privilege_level);
217
218 //Various Interrupts
239
240 // Exceptions
241 static void HandleException0x00();
242 static void HandleException0x01();
243 static void HandleException0x02();
244 static void HandleException0x03();
245 static void HandleException0x04();
246 static void HandleException0x05();
247 static void HandleException0x06();
248 static void HandleException0x07();
250 static void HandleException0x09();
256 static void HandleException0x0F();
257 static void HandleException0x10();
259 static void HandleException0x12();
260 static void HandleException0x13();
261 static void HandleException0x14();
262 static void HandleException0x15();
263 static void HandleException0x16();
264 static void HandleException0x17();
265 static void HandleException0x18();
266 static void HandleException0x19();
267 static void HandleException0x1A();
268 static void HandleException0x1B();
269 static void HandleException0x1C();
270 static void HandleException0x1D();
271 static void HandleException0x1E();
272 static void HandleException0x1F();
273
276
277 public:
280
282
283 void set_interrupt_handler(uint8_t interrupt, InterruptHandler* handler);
284 void remove_interrupt_handler(uint8_t interrupt);
285
288
289 static void load_current();
290 void activate();
291 static void deactivate();
292
298
299 };
300}
301
302
303#endif //MAX_OS_HARDWARECOMMUNICATION_INTERRUPTS_H
Defines classes and structures for handling the Advanced Programmable Interrupt Controller (APIC) in ...
Handles both the Local APIC (boot core) and IO APIC.
Definition apic.h:240
Handles a interrupt that tells a CPU core to stop running.
Definition interrupts.h:128
CPUStopHandler()
Create a new CPUStopHandler.
Definition interrupts.h:132
Handles an attempt to divide by 0.
Definition interrupts.h:76
DivideByZeroHandler()
Create a new DivideByZeroHandler.
Definition interrupts.h:80
string get_message(system::cpu_status_t *status) final
Get an human readable error message explaining this exception.
Base class for handing CPU expection interrupts.
Definition interrupts.h:61
virtual string get_message(system::cpu_status_t *status)
Get an human readable error message explaining this exception.
Holds and instantiates the CPU exception handlers.
Definition interrupts.h:176
Handles a general protection exception.
Definition interrupts.h:102
string get_message(system::cpu_status_t *status) final
Get an human readable error message explaining this exception.
GeneralProtectionHandler()
Create a new GeneralProtectionHandler.
Definition interrupts.h:106
Handles a certain interrupt number.
Definition interrupts.h:44
virtual ~InterruptHandler()
Destroys the interrupt handler and unregisters it from the interrupt manager.
uint8_t m_interrupt_number
The interrupt number this handler handles.
Definition interrupts.h:46
virtual void handle_interrupt()
Handles an interrupt.
Handles all interrupts and passes them to the correct handler.
Definition interrupts.h:200
static InterruptManager * s_active_interrupt_manager
The interrupt manger that is currently handling interrupts and is loaded into the IDTR.
Definition interrupts.h:211
static void HandleInterruptRequest0x06()
Stub (see interrupts.s)
static void HandleException0x15()
Stub (see interrupts.s)
static void HandleInterruptError0x0A()
Stub (see interrupts.s)
static void HandleInterruptRequest0x05()
Stub (see interrupts.s)
static void HandleInterruptRequest0x31()
Stub (see interrupts.s)
static void deactivate()
Deactivates the interrupt manager and stops interrupts.
static void HandleException0x14()
Stub (see interrupts.s)
system::cpu_status_t * handle_interrupt_request(system::cpu_status_t *status)
Handles the interrupt request by passing it to the appropriate handler.
static void HandleInterruptRequest0x0A()
Stub (see interrupts.s)
static void HandleException0x00()
Stub (see interrupts.s)
static void HandleInterruptRequest0x0C()
Stub (see interrupts.s)
static void HandleInterruptRequest0x0F()
Stub (see interrupts.s)
static void HandleInterruptRequest0x07()
Stub (see interrupts.s)
static void HandleException0x07()
Stub (see interrupts.s)
static void HandleInterruptError0x0D()
Stub (see interrupts.s)
static InterruptManager * active_interrupt_manager()
Gets the active interrupt manager.
static void HandleInterruptError0x08()
Stub (see interrupts.s)
static void HandleException0x13()
Stub (see interrupts.s)
static void HandleInterruptError0x0C()
Stub (see interrupts.s)
static void HandleException0x04()
Stub (see interrupts.s)
static void HandleInterruptRequest0x0E()
Stub (see interrupts.s)
static void HandleException0x02()
Stub (see interrupts.s)
static void HandleException0x17()
Stub (see interrupts.s)
static void HandleException0x10()
Stub (see interrupts.s)
static void HandleInterruptRequest0x01()
Stub (see interrupts.s)
static void HandleInterruptRequest0x09()
Stub (see interrupts.s)
void set_apic(AdvancedProgrammableInterruptController *apic)
Sets the APIC.
static void HandleInterruptError0x11()
Stub (see interrupts.s)
static void set_interrupt_descriptor_table_entry(uint8_t interrupt, void(*handler)(), InterruptPrivilegeLevel descriptor_privilege_level)
Sets an entry in the Interrupt Descriptor Table.
static void HandleException0x1E()
Stub (see interrupts.s)
static void HandleInterruptRequest0x60()
Stub (see interrupts.s)
static void HandleInterruptRequest0x0B()
Stub (see interrupts.s)
static void HandleException0x1F()
Stub (see interrupts.s)
void remove_interrupt_handler(uint8_t interrupt)
Removes the interrupt handler for the interrupt.
static void load_current()
Tell the processor to use the current InterruptManager.
static void HandleException0x05()
Stub (see interrupts.s)
static void HandleInterruptError0x0B()
Stub (see interrupts.s)
static void ForceInterruptReturn(system::cpu_status_t *state)
Force the CPU to return from an interrupt (see interrupts.s)
static void HandleException0x19()
Stub (see interrupts.s)
~InterruptManager()
Destroys the Interrupt Manager and deactivates interrupts.
static void HandleException0x1A()
Stub (see interrupts.s)
static void HandleException0x01()
Stub (see interrupts.s)
static void HandleException0x18()
Stub (see interrupts.s)
static void HandleException0x1B()
Stub (see interrupts.s)
InterruptManager()
Constructs the Interrupt Manager and sets up the interrupt descriptor table (does not activate interr...
void activate()
Activates the interrupt manager and starts interrupts (also deactivates the current interrupt manager...
void set_interrupt_handler(uint8_t interrupt, InterruptHandler *handler)
Sets the interrupt handler for the interrupt.
static void HandleInterruptRequest0x80()
Stub (see interrupts.s)
static InterruptDescriptor s_interrupt_descriptor_table[MAX_INTERRUPT_HANDLERS]
The Interrupt Descriptor Table (IDT)
Definition interrupts.h:214
static void HandleException0x12()
Stub (see interrupts.s)
static void HandleInterruptRequest0x00()
Stub (see interrupts.s)
static void HandleInterruptRequest0x08()
Stub (see interrupts.s)
static void HandleException0x09()
Stub (see interrupts.s)
static void HandleException0x16()
Stub (see interrupts.s)
static void HandleInterruptRequest0x0D()
Stub (see interrupts.s)
static void HandleInterruptError0x0E()
Stub (see interrupts.s)
static void HandleInterruptRequest0x61()
Stub (see interrupts.s)
static system::cpu_status_t * HandleInterrupt(system::cpu_status_t *status)
Handles the interrupt request by passing it to the interrupt manager.
static void HandleException0x06()
Stub (see interrupts.s)
static void HandleException0x03()
Stub (see interrupts.s)
static void HandleException0x0F()
Stub (see interrupts.s)
static void HandleInterruptRequest0x04()
Stub (see interrupts.s)
static void HandleInterruptRequest0x03()
Stub (see interrupts.s)
static void HandleException0x1C()
Stub (see interrupts.s)
static void HandleInterruptRequest0x02()
Stub (see interrupts.s)
AdvancedProgrammableInterruptController * active_apic()
Gets the APIC.
static void HandleException0x1D()
Stub (see interrupts.s)
InterruptHandler * m_interrupt_handlers[MAX_INTERRUPT_HANDLERS]
A map of index-to-interrupt of interrupt handlers to fire when an interrupt occurs.
Definition interrupts.h:213
Handles calling an invalid assembly instruction for the configuration running on this architecture.
Definition interrupts.h:89
InvaildOpCodeHandler()
Create a new InvaildOpCodeHandler.
Definition interrupts.h:93
string get_message(system::cpu_status_t *status) final
Get an human readable error message explaining this exception.
Handles a page fault exception.
Definition interrupts.h:115
string get_message(system::cpu_status_t *status) final
Get an human readable error message explaining this exception.
PageFaultHandler()
Create a new PageFaultHandler.
Definition interrupts.h:119
Defines Central Processing Unit (CPU) structures and functions for managing CPU state and features.
struct PACKED MaxOS::system::CPUStatus cpu_status_t
Alias for CPUStatus struct.
Defines a Global Descriptor Table (GDT) for setting up memory segments in protected mode.
constexpr uint16_t MAX_INTERRUPT_HANDLERS
The maximum number of interrupt handlers.
Definition interrupts.h:170
constexpr uint16_t HARDWARE_INTERRUPT_OFFSET
The offset in the IDT where interrupts from hardware start.
Definition interrupts.h:169
InterruptPrivilegeLevel
Represents the CPU ring level that a interrupt is handler is executed in.
Definition interrupts.h:33
A struct that holds the Interrupt Descriptor Table Register (IDTR)
Definition interrupts.h:145
uint16_t limit
The size of the IDT.
Definition interrupts.h:146
uint64_t base
The start address of the IDT.
Definition interrupts.h:147
An entry in the Interrupt Descriptor Table (IDT)
Definition interrupts.h:157
uint16_t segment_selector
The code segment selector in the GDT to use when this interrupt fires.
Definition interrupts.h:160
uint32_t address_high_bits
The high bits of the handler function address.
Definition interrupts.h:164
uint32_t reserved
Reserved, must be zero.
Definition interrupts.h:165
uint8_t ist
The offset into the Interrupt Stack Table (0 if not used) (see TSS)
Definition interrupts.h:161
uint16_t address_mid_bits
The middle bits of the handler function address.
Definition interrupts.h:163
uint16_t address_low_bits
The low bits of the handler function address.
Definition interrupts.h:159
uint8_t flags
Flags: The type of gate this entry represents (0xE for interrupt gate, 0xF for trap gate),...
Definition interrupts.h:162