Max OS 0.3
Loading...
Searching...
No Matches
apic.h
Go to the documentation of this file.
1
9#ifndef MAXOS_HARDWARECOMMUNICATION_APIC_H
10#define MAXOS_HARDWARECOMMUNICATION_APIC_H
11
12#include <common/macros.h>
15#include <memory/physical.h>
16
17
18namespace MaxOS::hardwarecommunication {
19
24 class LocalAPIC {
25
26 private:
27 uint64_t m_apic_base { };
28 uint64_t m_apic_base_high { };
29 uint32_t m_id { };
30 bool m_x2apic { };
31
32 public:
33 LocalAPIC();
34 ~LocalAPIC();
35
36 [[nodiscard]] uint32_t read(uint32_t reg) const;
37 void write(uint32_t reg, uint32_t value) const;
38
39 [[nodiscard]] uint32_t id() const;
40 void send_eoi() const;
41
42 void send_init(uint8_t apic_id, bool assert) const;
43 void send_startup(uint8_t apic_id, uint8_t vector) const;
44
45 };
46
55 typedef struct PACKED MADT {
58 uint32_t flags;
59 } madt_t;
60
68 typedef struct PACKED MADTEntry {
69 uint8_t type;
70 uint8_t length;
71 } madt_entry_t;
72
77 enum class MADT_TYPE {
78 PROCESSOR_APIC,
79 IO_APIC,
80 INTERRUPT_SOURCE_OVERRIDE,
81 NMI_SOURCE,
82 APIC_NMIS,
83 APIC_ADDRESS_OVERRIDE,
84 PROCESSOR_X2APIC,
85 };
86
96 typedef struct PACKED MADT_IO_APIC {
97 uint8_t io_apic_id;
98 uint8_t reserved;
99 uint32_t io_apic_address;
101 } madt_io_apic_t;
102
110 typedef struct PACKED MADT_PROCESSOR_APIC {
111 uint8_t processor_id;
112 uint8_t apic_id;
113 uint32_t flags;
114 } madt_processor_apic_t;
115
123 typedef union RedirectionEntry {
124 struct PACKED {
125 uint64_t vector : 8;
126 uint64_t delivery_mode : 3;
127 uint64_t destination_mode : 1;
128 uint64_t delivery_status : 1;
129 uint64_t pin_polarity : 1;
130 uint64_t remote_irr : 1;
131 uint64_t trigger_mode : 1;
132 uint64_t mask : 1;
133 uint64_t reserved : 39;
134 uint64_t destination : 8;
135 };
136
137 uint64_t raw;
138 } redirection_entry_t;
139
147 FIXED = 0,
148 LOWEST_PRIORITY = 1,
149 SMI = 2,
150 NMI = 4,
151 INIT = 5,
152 EXTINT = 7,
153 };
154
162 typedef struct InterruptRedirect {
163
164 uint8_t type;
165 uint8_t index;
166 uint8_t interrupt;
167 uint8_t destination;
168 uint32_t flags;
169 bool mask;
170
171 } interrupt_redirect_t;
172
180 typedef struct PACKED Override {
181
182 uint8_t bus;
183 uint8_t source;
185 uint16_t flags;
186
187 } override_t;
188
193 class IOAPIC {
194 private:
196 MADT* m_madt = { nullptr };
197 uint64_t m_address = 0;
198 uint64_t m_address_high = 0;
199 uint32_t m_version = 0;
200 uint8_t m_max_redirect_entry = 0;
201
202 uint8_t m_override_array_size = 0;
203 Override m_override_array[0x10] = { };
204
205 void read_redirect(uint8_t index, RedirectionEntry* entry) const;
206 void write_redirect(uint8_t index, RedirectionEntry* entry) const;
207
208 public:
210 ~IOAPIC();
211
212 [[nodiscard]] uint32_t read(uint32_t reg) const;
213 void write(uint32_t reg, uint32_t value) const;
214
215 void set_redirect(interrupt_redirect_t* redirect);
216 void set_redirect_mask(uint8_t index, bool mask);
217
218 MADTEntry* get_madt_item(MADT_TYPE type, uint8_t index);
219
220 };
221
229
230 private:
231 LocalAPIC* m_local_apic;
232 IOAPIC* m_io_apic;
233
234 Port8BitSlow m_pic_master_command_port;
235 Port8BitSlow m_pic_master_data_port;
236 Port8BitSlow m_pic_slave_command_port;
237 Port8BitSlow m_pic_slave_data_port;
238
239 void disable_pic();
240
241 public:
244
245 [[nodiscard]] LocalAPIC* local_apic() const;
246 [[nodiscard]] IOAPIC* io_apic() const;
247
248 };
249
250}
251
252
253#endif // MAXOS_HARDWARECOMMUNICATION_APIC_H
Defines an Advanced Configuration And Power Interface (ACPI class for handling ACPI table parsing and...
MADT_TYPE
The types of MADT entries.
Definition apic.h:77
DeliveryMode
How the interrupt should be delivered (can it override others etc). Set fixed/lowest priority for nor...
Definition apic.h:146
Handles ACPI table parsing and retrieval.
Definition acpi.h:99
Handles both the Local APIC (boot core) and IO APIC.
Definition apic.h:228
~AdvancedProgrammableInterruptController()
Destroy the Advanced Programmable Interrupt Controller object and frees the Local APIC and IO APIC.
Definition apic.cpp:442
Handles the IO APIC in the system (one per system)
Definition apic.h:193
void write(uint32_t reg, uint32_t value) const
write a value to an IO Apic register
Definition apic.cpp:312
void set_redirect(interrupt_redirect_t *redirect)
Redirect a system interrupt to a different IRQ.
Definition apic.cpp:365
MADTEntry * get_madt_item(MADT_TYPE type, uint8_t index)
Get an item in the MADT.
Definition apic.cpp:261
void set_redirect_mask(uint8_t index, bool mask)
Enables/Disables an interrupt redirect by masking it.
Definition apic.cpp:399
uint32_t read(uint32_t reg) const
read a value from a IO Apic register
Definition apic.cpp:297
Handles the local APIC for the current core.
Definition apic.h:24
void send_eoi() const
Acknowledge that the interrupt has been handled, allowing the PIC to process the next interrupt.
Definition apic.cpp:120
void write(uint32_t reg, uint32_t value) const
write a value to the apic register using the MSR or memory I/O depending on the local apic version
Definition apic.cpp:91
uint32_t id() const
Get the id of the local apic.
Definition apic.cpp:108
LocalAPIC()
Construct a new Local APIC object and map the APIC base address to the higher half if needed as well ...
Definition apic.cpp:21
void send_startup(uint8_t apic_id, uint8_t vector) const
send the start up IPI to another apic
Definition apic.cpp:170
uint32_t read(uint32_t reg) const
read a value from the apic register using the MSR or memory I/O depending on the local apic version
Definition apic.cpp:76
void send_init(uint8_t apic_id, bool assert) const
send the init IPI to another apic
Definition apic.cpp:131
Handles 8 bit ports (slow)
Definition port.h:45
Defines a PhysicalMemoryManager class for managing physical memory allocation and deallocation of pag...
Defines classes for handling hardware communication ports (8, 16, and 32 bit)
Common header for all ACPI System Description Tables (SDTs)
Definition acpi.h:59
Defines how an interrupt input is redirected to a specific processor via the I/O APIC.
Definition apic.h:162
uint8_t index
Where in the IO APIC redirection table this redirect should be placed.
Definition apic.h:165
uint8_t interrupt
The interrupt number to trigger when this redirect is used.
Definition apic.h:166
uint32_t flags
Configuration flags for the interrupt (see MADT override flags)
Definition apic.h:168
bool mask
Should the interrupt be disabled initially.
Definition apic.h:169
uint8_t type
The type of redirect (should be 0 for normal interrupts)
Definition apic.h:164
uint8_t destination
The ID of the core the interrupt should be sent to.
Definition apic.h:167
An item in the MADT table.
Definition apic.h:68
uint8_t type
The type of the MADT entry (see MADT_TYPE)
Definition apic.h:69
uint8_t length
How many bytes this entry takes up (including the type and length fields)
Definition apic.h:70
An entry in the MADT table describing an IO APIC.
Definition apic.h:96
uint8_t io_apic_id
The ID of the IO APIC.
Definition apic.h:97
uint32_t io_apic_address
The physical address of the IO APIC.
Definition apic.h:99
uint32_t global_system_interrupt_base
The first GSI number that this IO APIC handles.
Definition apic.h:100
An entry in the MADT table describing a processor local APIC.
Definition apic.h:110
uint8_t processor_id
The ID for the core.
Definition apic.h:111
uint8_t apic_id
The ID for the APIC that the core uses.
Definition apic.h:112
uint32_t flags
Flags indicating the status of the core (bit 0 = core enabled, bit 1 = can be enabled)
Definition apic.h:113
Multiple APIC Description Table (MADT) (ACPI 2 or higher). Provides information about the APICs in th...
Definition apic.h:55
uint32_t local_apic_address
The physical address of the local APIC.
Definition apic.h:57
ACPISDTHeader header
Common ACPI SDT header.
Definition apic.h:56
uint32_t flags
Flags indicating the capabilities of the system (1 = Legacy PICs are present)
Definition apic.h:58
Specifies how a specific interrupt source is mapped to a global system interrupt.
Definition apic.h:180
uint8_t source
The interrupt source number on the specified bus.
Definition apic.h:183
uint32_t global_system_interrupt
The global system interrupt that the source is mapped to.
Definition apic.h:184
uint8_t bus
Identifies the hardware bus the interrupt comes from (0 = ISA, 1 = PCI)
Definition apic.h:182
uint16_t flags
Flags describing the polarity and trigger mode of the interrupt source (see MADT override flags)
Definition apic.h:185
Packed because the bitfields should be one uint64_t together.
Definition apic.h:124
uint64_t destination_mode
How to interpret the destination field (0 = means its the core ID, 1 = means a bitmask of cores)
Definition apic.h:127
uint64_t mask
Whether the interrupt is disabled or not.
Definition apic.h:132
uint64_t delivery_mode
How the interrupt will be delvied to the core(s) (see DeliveryMode)
Definition apic.h:126
uint64_t destination
The core(s) the interrupt should be sent to (interpreted based on destination_mode)
Definition apic.h:134
uint64_t trigger_mode
How the electrical signal is interpreted (0 = edge triggered, 1 = level triggered) (must match the MA...
Definition apic.h:131
uint64_t delivery_status
Indicates whether, when setting this entry, the interrupt is still being sent.
Definition apic.h:128
uint64_t vector
The interrupt that will be triggered when this redirection entry is used.
Definition apic.h:125
uint64_t reserved
Reserved bits (should be zero)
Definition apic.h:133
uint64_t pin_polarity
Defines the electrical signal that indicates an active interrupt line (0 = high active,...
Definition apic.h:129
An IO APIC redirection entry. Describes how an interrupt is routed.
Definition apic.h:123
uint64_t raw
The raw 64-bit value of the redirection entry.
Definition apic.h:137