The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
apic.c File Reference
#include "apic.h"
#include "arch/arch.h"
#include "idt.h"
#include "io.h"
#include "isr.h"
#include "pic.h"
#include "irq.h"
#include "kernel/acpi/acpi.h"
#include "kernel/memory/tlb.h"
#include "kernel/process/process.h"
#include "kernel/scheduler/scheduler.h"
#include "kernel/klog/klog.h"
#include "kernel/memory/mmio.h"
#include "kernel/sync/spinlock.h"

Macros

#define LAPIC_ID   0x020
 the local APIC and the I/O APIC (Intel SDM volume 3, chapter 11; the 82093AA I/O APIC datasheet).
#define LAPIC_TPR   0x080
#define LAPIC_EOI   0x0B0
#define LAPIC_SVR   0x0F0
#define LAPIC_ICR_LOW   0x300
#define LAPIC_ICR_HIGH   0x310
#define LAPIC_LVT_TIMER   0x320
#define LAPIC_LVT_LINT0   0x350
#define LAPIC_LVT_LINT1   0x360
#define LAPIC_LVT_ERROR   0x370
#define LAPIC_TIMER_INIT   0x380
#define LAPIC_TIMER_COUNT   0x390
#define LAPIC_TIMER_DIV   0x3E0
#define LAPIC_SVR_ENABLE   0x100u
#define LVT_MASKED   0x10000u
#define LVT_TIMER_PERIODIC   0x20000u
#define LVT_DELIVERY_EXTINT   0x700u
#define LVT_DELIVERY_NMI   0x400u
#define TIMER_DIVIDE_16   0x3u
#define ICR_INIT   0x500u
#define ICR_STARTUP   0x600u
#define ICR_NMI   0x400u
#define ICR_PENDING   0x1000u
#define ICR_ASSERT   0x4000u
#define ICR_LEVEL   0x8000u
#define ICR_SEND_SPINS   1000000u
#define SPURIOUS_VECTOR   0xFF
#define MSR_APIC_BASE   0x1Bu
#define APIC_BASE_ENABLE   (1u << 11)
#define APIC_DEFAULT_ADDRESS   0xFEE00000u
#define IOAPIC_REGSEL   0x00
#define IOAPIC_WIN   0x10
#define IOAPIC_VERSION   0x01
#define IOAPIC_REDTBL   0x10
#define REDIR_ACTIVE_LOW   0x2000u
#define REDIR_LEVEL   0x8000u
#define REDIR_MASKED   0x10000u
#define PIT_CLOCK_HZ   1193182u
#define CALIBRATE_MS   10u

Functions

bool apic_init (uint32_t timer_hz)
 change from the 8259 PICs to the APICs.
bool apic_active (void)
 true after a successful apic_init.
void apic_eoi (void)
 end of interrupt to the local APIC.
void apic_set_irq_masked (uint32_t irq, bool masked)
 mask or unmask the I/O APIC entry of an ISA IRQ (0 to 15).
uint8_t apic_id (void)
 the id of the local APIC of the boot core, for the destination of MSI messages and the I/O APIC entries.
void apic_set_irq_level (uint32_t irq)
 make the I/O APIC entry of an ISA IRQ level-triggered (a PCI interrupt line routed to it).
bool apic_set_irq_dest (uint32_t irq, uint8_t lapic_id)
 send the I/O APIC entry of an ISA IRQ to a local APIC (physical destination mode).
bool apic_init_ap (void)
 set up the local APIC of a core other than the boot core: turn it on, mask LINT0, LINT1 and the error interrupt, and start its timer on LOCAL_TIMER_VECTOR at the rate the boot core measured (if the boot core uses its timer).
bool apic_send_init (uint8_t target)
 send INIT to a core: it stops and waits for a startup IPI.
bool apic_send_startup (uint8_t target, uint32_t page_address)
 send a startup IPI (SIPI) to a core that waits after INIT.
void apic_send_ipi (uint8_t target, uint8_t vector)
 send an interrupt to another core (an IPI) on a vector, with fixed delivery.
void apic_send_nmi (uint8_t target)
 send a non-maskable interrupt (NMI, vector 2) to another core.

Macro Definition Documentation

◆ LAPIC_ID

#define LAPIC_ID   0x020

the local APIC and the I/O APIC (Intel SDM volume 3, chapter 11; the 82093AA I/O APIC datasheet).

the local APIC registers are 32 bits wide, 16 bytes apart, at the address from the MADT. the I/O APIC has two registers: IOREGSEL selects a register, IOWIN reads or writes it. its redirection table has one 64-bit entry per global system interrupt (GSI).

the boot core routes the I/O APIC interrupts to itself; irq_spread() moves them to the other cores after they start. each other core sets up its own local APIC when it starts (apic_init_ap), with a timer on LOCAL_TIMER_VECTOR at the rate the boot core measured.

◆ LAPIC_TPR

#define LAPIC_TPR   0x080

◆ LAPIC_EOI

#define LAPIC_EOI   0x0B0

◆ LAPIC_SVR

#define LAPIC_SVR   0x0F0

◆ LAPIC_ICR_LOW

#define LAPIC_ICR_LOW   0x300

◆ LAPIC_ICR_HIGH

#define LAPIC_ICR_HIGH   0x310

◆ LAPIC_LVT_TIMER

#define LAPIC_LVT_TIMER   0x320

◆ LAPIC_LVT_LINT0

#define LAPIC_LVT_LINT0   0x350

◆ LAPIC_LVT_LINT1

#define LAPIC_LVT_LINT1   0x360

◆ LAPIC_LVT_ERROR

#define LAPIC_LVT_ERROR   0x370

◆ LAPIC_TIMER_INIT

#define LAPIC_TIMER_INIT   0x380

◆ LAPIC_TIMER_COUNT

#define LAPIC_TIMER_COUNT   0x390

◆ LAPIC_TIMER_DIV

#define LAPIC_TIMER_DIV   0x3E0

◆ LAPIC_SVR_ENABLE

#define LAPIC_SVR_ENABLE   0x100u

◆ LVT_MASKED

#define LVT_MASKED   0x10000u

◆ LVT_TIMER_PERIODIC

#define LVT_TIMER_PERIODIC   0x20000u

◆ LVT_DELIVERY_EXTINT

#define LVT_DELIVERY_EXTINT   0x700u

◆ LVT_DELIVERY_NMI

#define LVT_DELIVERY_NMI   0x400u

◆ TIMER_DIVIDE_16

#define TIMER_DIVIDE_16   0x3u

◆ ICR_INIT

#define ICR_INIT   0x500u

◆ ICR_STARTUP

#define ICR_STARTUP   0x600u

◆ ICR_NMI

#define ICR_NMI   0x400u

◆ ICR_PENDING

#define ICR_PENDING   0x1000u

◆ ICR_ASSERT

#define ICR_ASSERT   0x4000u

◆ ICR_LEVEL

#define ICR_LEVEL   0x8000u

◆ ICR_SEND_SPINS

#define ICR_SEND_SPINS   1000000u

◆ SPURIOUS_VECTOR

#define SPURIOUS_VECTOR   0xFF

◆ MSR_APIC_BASE

#define MSR_APIC_BASE   0x1Bu

◆ APIC_BASE_ENABLE

#define APIC_BASE_ENABLE   (1u << 11)

◆ APIC_DEFAULT_ADDRESS

#define APIC_DEFAULT_ADDRESS   0xFEE00000u

◆ IOAPIC_REGSEL

#define IOAPIC_REGSEL   0x00

◆ IOAPIC_WIN

#define IOAPIC_WIN   0x10

◆ IOAPIC_VERSION

#define IOAPIC_VERSION   0x01

◆ IOAPIC_REDTBL

#define IOAPIC_REDTBL   0x10

◆ REDIR_ACTIVE_LOW

#define REDIR_ACTIVE_LOW   0x2000u

◆ REDIR_LEVEL

#define REDIR_LEVEL   0x8000u

◆ REDIR_MASKED

#define REDIR_MASKED   0x10000u

◆ PIT_CLOCK_HZ

#define PIT_CLOCK_HZ   1193182u

◆ CALIBRATE_MS

#define CALIBRATE_MS   10u

Function Documentation

◆ apic_init()

bool apic_init ( uint32_t timer_hz)

change from the 8259 PICs to the APICs.

call with interrupts off, after acpi_init (it needs the MADT) and mmio_init.

Parameters
timer_hzthe tick rate for the local APIC timer.
Returns
true if the APICs are in use.

◆ apic_active()

bool apic_active ( void )

true after a successful apic_init.

◆ apic_eoi()

void apic_eoi ( void )

end of interrupt to the local APIC.

◆ apic_set_irq_masked()

void apic_set_irq_masked ( uint32_t irq,
bool masked )

mask or unmask the I/O APIC entry of an ISA IRQ (0 to 15).

◆ apic_id()

uint8_t apic_id ( void )

the id of the local APIC of the boot core, for the destination of MSI messages and the I/O APIC entries.

◆ apic_set_irq_level()

void apic_set_irq_level ( uint32_t irq)

make the I/O APIC entry of an ISA IRQ level-triggered (a PCI interrupt line routed to it).

the polarity stays as the MADT gives it.

◆ apic_set_irq_dest()

bool apic_set_irq_dest ( uint32_t irq,
uint8_t lapic_id )

send the I/O APIC entry of an ISA IRQ to a local APIC (physical destination mode).

Parameters
irqthe ISA IRQ (0 to 15).
lapic_idthe id of the local APIC of the core.
Returns
false if the IRQ has no I/O APIC entry.

◆ apic_init_ap()

bool apic_init_ap ( void )

set up the local APIC of a core other than the boot core: turn it on, mask LINT0, LINT1 and the error interrupt, and start its timer on LOCAL_TIMER_VECTOR at the rate the boot core measured (if the boot core uses its timer).

call it on that core, after apic_init on the boot core.

Returns
true if the timer of the core runs.

◆ apic_send_init()

bool apic_send_init ( uint8_t target)

send INIT to a core: it stops and waits for a startup IPI.

Parameters
targetthe local APIC id of the core.
Returns
false if the local APIC could not send the IPI.

◆ apic_send_startup()

bool apic_send_startup ( uint8_t target,
uint32_t page_address )

send a startup IPI (SIPI) to a core that waits after INIT.

the core starts in real mode at page_address (CS = page_address / 16, IP = 0).

Parameters
targetthe local APIC id of the core.
page_addressa page-aligned physical address below 1 MiB.
Returns
false if the local APIC could not send the IPI.

◆ apic_send_ipi()

void apic_send_ipi ( uint8_t target,
uint8_t vector )

send an interrupt to another core (an IPI) on a vector, with fixed delivery.

interrupts are off on this core while the IPI goes out.

Parameters
targetthe local APIC id of the core.
vectorthe vector the core receives.

◆ apic_send_nmi()

void apic_send_nmi ( uint8_t target)

send a non-maskable interrupt (NMI, vector 2) to another core.

it comes also when interrupts are off on that core.

Parameters
targetthe local APIC id of the core.