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 <libcommon/macros.h>
13#include <libcommon/port.h>
14
16#include <memory/physical.h>
17
18
19namespace MaxOS::hardwarecommunication {
20
26 SEND_ALL = (1 << 0),
27 EXCLUDE_SELF = (1 << 1),
28 };
29
34 class LocalAPIC {
35
36 private:
37 uint64_t m_apic_base = 0;
38 uint64_t m_apic_base_high = 0;
39 uint32_t m_id = 0;
40 bool m_x2apic = false;
41
42 public:
43 LocalAPIC();
44 ~LocalAPIC();
45
46 [[nodiscard]] uint32_t read(uint32_t reg) const;
47 void write(uint32_t reg, uint32_t value) const;
48
49 [[nodiscard]] uint32_t id() const;
50 void send_eoi() const;
51 void send_icr(uint32_t icr_low, uint32_t icr_high, bool use_shorthand) const;
52
53 void send_init(uint8_t apic_id, bool assert) const;
54 void send_startup(uint8_t apic_id, uint8_t vector) const;
55 void send_interrupt(uint8_t apic_id, uint8_t vector, size_t flags) const;
56
57 };
58
67 typedef struct PACKED MADT {
70 uint32_t flags;
71 } madt_t;
72
80 typedef struct PACKED MADTEntry {
81 uint8_t type;
82 uint8_t length;
83 } madt_entry_t;
84
89 enum class MADT_TYPE {
90 PROCESSOR_APIC,
91 IO_APIC,
92 INTERRUPT_SOURCE_OVERRIDE,
93 NMI_SOURCE,
94 APIC_NMIS,
95 APIC_ADDRESS_OVERRIDE,
96 PROCESSOR_X2APIC,
97 };
98
108 typedef struct PACKED MADT_IO_APIC {
109 uint8_t io_apic_id;
110 uint8_t reserved;
113 } madt_io_apic_t;
114
122 typedef struct PACKED MADT_PROCESSOR_APIC {
123 uint8_t processor_id;
124 uint8_t apic_id;
125 uint32_t flags;
126 } madt_processor_apic_t;
127
135 typedef union RedirectionEntry {
136 struct PACKED {
137 uint64_t vector : 8;
138 uint64_t delivery_mode : 3;
139 uint64_t destination_mode : 1;
140 uint64_t delivery_status : 1;
141 uint64_t pin_polarity : 1;
142 uint64_t remote_irr : 1;
143 uint64_t trigger_mode : 1;
144 uint64_t mask : 1;
145 uint64_t reserved : 39;
146 uint64_t destination : 8;
147 };
148
149 uint64_t raw;
150 } redirection_entry_t;
151
159 FIXED = 0,
160 LOWEST_PRIORITY = 1,
161 SMI = 2,
162 NMI = 4,
163 INIT = 5,
164 EXTINT = 7,
165 };
166
174 typedef struct InterruptRedirect {
175
176 uint8_t type;
177 uint8_t index;
178 uint8_t interrupt;
179 uint8_t destination;
180 uint32_t flags;
181 bool mask;
182
183 } interrupt_redirect_t;
184
192 typedef struct PACKED Override {
193
194 uint8_t bus;
195 uint8_t source;
197 uint16_t flags;
198
199 } override_t;
200
205 class IOAPIC {
206 private:
208 MADT* m_madt = { nullptr };
209 uint64_t m_address = 0;
210 uint64_t m_address_high = 0;
211 uint32_t m_version = 0;
212 uint8_t m_max_redirect_entry = 0;
213
214 uint8_t m_override_array_size = 0;
215 Override m_override_array[0x10] = { };
216
217 void read_redirect(uint8_t index, RedirectionEntry* entry) const;
218 void write_redirect(uint8_t index, RedirectionEntry* entry) const;
219
220 public:
222 ~IOAPIC();
223
224 [[nodiscard]] uint32_t read(uint32_t reg) const;
225 void write(uint32_t reg, uint32_t value) const;
226
227 void set_redirect(interrupt_redirect_t* redirect);
228 void set_redirect_mask(uint8_t index, bool mask);
229
230 MADTEntry* get_madt_item(MADT_TYPE type, uint8_t index);
231
232 };
233
241
242 private:
243 LocalAPIC* m_local_apic;
244 IOAPIC* m_io_apic;
245
246 common::Port8BitSlow m_pic_master_command_port;
247 common::Port8BitSlow m_pic_master_data_port;
248 common::Port8BitSlow m_pic_slave_command_port;
249 common::Port8BitSlow m_pic_slave_data_port;
250
251 void disable_pic();
252
253 public:
256
257 [[nodiscard]] LocalAPIC* local_apic() const;
258 [[nodiscard]] IOAPIC* io_apic() const;
259
260 };
261
262}
263
264
265#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:89
DeliveryMode
How the interrupt should be delivered (can it override others etc). Set fixed/lowest priority for nor...
Definition apic.h:158
LAPICSendInterruptFlags
Represents bitfield entries in the LAPIC flags.
Definition apic.h:25
Handles ACPI table parsing and retrieval.
Definition acpi.h:102
Handles both the Local APIC (boot core) and IO APIC.
Definition apic.h:240
~AdvancedProgrammableInterruptController()
Destroy the Advanced Programmable Interrupt Controller object and frees the Local APIC and IO APIC.
Definition apic.cpp:489
Handles the IO APIC in the system (one per system)
Definition apic.h:205
void write(uint32_t reg, uint32_t value) const
write a value to an IO Apic register
Definition apic.cpp:359
void set_redirect(interrupt_redirect_t *redirect)
Redirect a system interrupt to a different IRQ.
Definition apic.cpp:412
MADTEntry * get_madt_item(MADT_TYPE type, uint8_t index)
Get an item in the MADT.
Definition apic.cpp:308
void set_redirect_mask(uint8_t index, bool mask)
Enables/Disables an interrupt redirect by masking it.
Definition apic.cpp:446
uint32_t read(uint32_t reg) const
read a value from a IO Apic register
Definition apic.cpp:344
Handles the local APIC for the current core.
Definition apic.h:34
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
void send_icr(uint32_t icr_low, uint32_t icr_high, bool use_shorthand) const
Send an Interrupt Command Register (ICR) to the local apic.
Definition apic.cpp:132
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:190
void send_interrupt(uint8_t apic_id, uint8_t vector, size_t flags) const
Send an interrupt to a specific core.
Definition apic.cpp:213
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:165
Defines a PhysicalMemoryManager class for managing physical memory allocation and deallocation of pag...
Common header for all ACPI System Description Tables (SDTs)
Definition acpi.h:62
Defines how an interrupt input is redirected to a specific processor via the I/O APIC.
Definition apic.h:174
uint8_t index
Where in the IO APIC redirection table this redirect should be placed.
Definition apic.h:177
uint8_t interrupt
The interrupt number to trigger when this redirect is used.
Definition apic.h:178
uint32_t flags
Configuration flags for the interrupt (see MADT override flags)
Definition apic.h:180
bool mask
Should the interrupt be disabled initially.
Definition apic.h:181
uint8_t type
The type of redirect (should be 0 for normal interrupts)
Definition apic.h:176
uint8_t destination
The ID of the core the interrupt should be sent to.
Definition apic.h:179
An item in the MADT table.
Definition apic.h:80
uint8_t type
The type of the MADT entry (see MADT_TYPE)
Definition apic.h:81
uint8_t length
How many bytes this entry takes up (including the type and length fields)
Definition apic.h:82
An entry in the MADT table describing an IO APIC.
Definition apic.h:108
uint8_t io_apic_id
The ID of the IO APIC.
Definition apic.h:109
uint32_t io_apic_address
The physical address of the IO APIC.
Definition apic.h:111
uint32_t global_system_interrupt_base
The first GSI number that this IO APIC handles.
Definition apic.h:112
An entry in the MADT table describing a processor local APIC.
Definition apic.h:122
uint8_t processor_id
The ID for the core.
Definition apic.h:123
uint8_t apic_id
The ID for the APIC that the core uses.
Definition apic.h:124
uint32_t flags
Flags indicating the status of the core (bit 0 = core enabled, bit 1 = can be enabled)
Definition apic.h:125
Multiple APIC Description Table (MADT) (ACPI 2 or higher). Provides information about the APICs in th...
Definition apic.h:67
uint32_t local_apic_address
The physical address of the local APIC.
Definition apic.h:69
ACPISDTHeader header
Common ACPI SDT header.
Definition apic.h:68
uint32_t flags
Flags indicating the capabilities of the system (1 = Legacy PICs are present)
Definition apic.h:70
Specifies how a specific interrupt source is mapped to a global system interrupt.
Definition apic.h:192
uint8_t source
The interrupt source number on the specified bus.
Definition apic.h:195
uint32_t global_system_interrupt
The global system interrupt that the source is mapped to.
Definition apic.h:196
uint8_t bus
Identifies the hardware bus the interrupt comes from (0 = ISA, 1 = PCI)
Definition apic.h:194
uint16_t flags
Flags describing the polarity and trigger mode of the interrupt source (see MADT override flags)
Definition apic.h:197
Packed because the bitfields should be one uint64_t together.
Definition apic.h:136
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:139
uint64_t mask
Whether the interrupt is disabled or not.
Definition apic.h:144
uint64_t delivery_mode
How the interrupt will be delvied to the core(s) (see DeliveryMode)
Definition apic.h:138
uint64_t destination
The core(s) the interrupt should be sent to (interpreted based on destination_mode)
Definition apic.h:146
uint64_t trigger_mode
How the electrical signal is interpreted (0 = edge triggered, 1 = level triggered) (must match the MA...
Definition apic.h:143
uint64_t delivery_status
Indicates whether, when setting this entry, the interrupt is still being sent.
Definition apic.h:140
uint64_t vector
The interrupt that will be triggered when this redirection entry is used.
Definition apic.h:137
uint64_t reserved
Reserved bits (should be zero)
Definition apic.h:145
uint64_t pin_polarity
Defines the electrical signal that indicates an active interrupt line (0 = high active,...
Definition apic.h:141
An IO APIC redirection entry. Describes how an interrupt is routed.
Definition apic.h:135
uint64_t raw
The raw 64-bit value of the redirection entry.
Definition apic.h:149