PatchworkOS  19e446b
A non-POSIX operating system.
Loading...
Searching...
No Matches
lapic.c
Go to the documentation of this file.
2
3#include <kernel/cpu/cpu.h>
4#include <kernel/cpu/ipi.h>
5#include <kernel/cpu/irq.h>
6#include <kernel/log/log.h>
7#include <kernel/mem/vmm.h>
10
11#include <assert.h>
12#include <sys/defs.h>
13
15
36
38{
39 return READ_32(lapicBase + reg);
40}
41
43{
44 WRITE_32(lapicBase + reg, value);
45}
46
54
55static void lapic_eoi(void)
56{
58}
59
61 .name = "Local APIC IPI",
62 .interrupt = lapic_interrupt,
63 .eoi = lapic_eoi,
64};
65
71
72void lapic_send_sipi(lapic_id_t id, void* entryPoint)
73{
74 assert((uintptr_t)entryPoint % PAGE_SIZE == 0);
75
78}
79
81{
83 if (madt == NULL)
84 {
85 LOG_ERR("no MADT table found\n");
86 return ERR;
87 }
88
89 if (madt->header.length < sizeof(madt_t))
90 {
91 LOG_ERR("madt table too small\n");
92 return ERR;
93 }
94
96 {
97 LOG_ERR("madt has invalid lapic address\n");
98 return ERR;
99 }
100
104 {
105 LOG_ERR("failed to map local apic\n");
106 return ERR;
107 }
108
109 LOG_INFO("local apic mapped base=%p phys=%p\n", lapicBase, (uintptr_t)madt->localInterruptControllerAddress);
110
112 {
114 LOG_ERR("failed to register lapic ipi chip\n");
115 return ERR;
116 }
117
118 return 0;
119}
#define assert(expression)
Definition assert.h:29
sdt_header_t * acpi_tables_lookup(const char *signature, uint64_t minSize, uint64_t n)
Lookup the n'th table matching the signature.
Definition tables.c:259
#define MADT_SIGNATURE
MADT table signature.
Definition tables.h:202
@ VECTOR_SPURIOUS
Made available for any component to use as a sink for spurious interrupts.
Definition interrupt.h:181
uint64_t ipi_chip_register(ipi_chip_t *chip)
Register an IPI chip.
Definition ipi.c:65
uint8_t irq_virt_t
Virtual IRQ numbers.
Definition irq.h:57
#define CPU_PTR(id, ptr)
Macro to get a pointer to a percpu variable on a specific CPU.
Definition percpu.h:102
#define PERCPU_DEFINE_CTOR(type, name)
Macro to define a percpu variable with a constructor.
Definition percpu.h:130
#define LAPIC_REG_ID_OFFSET
The offset at which the lapic id is stored in the LAPIC_REG_ID register.
Definition lapic.h:83
void lapic_send_init(lapic_id_t id)
Send an INIT IPI to the specified local APIC.
Definition lapic.c:66
void lapic_send_sipi(lapic_id_t id, void *entryPoint)
Send a Startup IPI (SIPI) to the specified local APIC.
Definition lapic.c:72
uint32_t lapic_read(uint32_t reg)
Read from a local apic register.
Definition lapic.c:37
uint64_t lapic_global_init(void)
Global initialization for the local APICs.
Definition lapic.c:80
lapic_t PERCPU _pcpu_lapic
The per-CPU local APIC structure.
uint8_t lapic_id_t
Local APIC ID type.
Definition lapic.h:34
void lapic_write(uint32_t reg, uint32_t value)
Write to a local apic register.
Definition lapic.c:42
@ LAPIC_LVT_MASKED
Definition lapic.h:92
@ LAPIC_SPURIOUS_ENABLE
Definition lapic.h:91
@ LAPIC_MSR_BSP
Definition lapic.h:43
@ LAPIC_MSR_ENABLE
Definition lapic.h:42
@ LAPIC_REG_TASK_PRIORITY
Definition lapic.h:54
@ LAPIC_REG_LVT_PERFCTR
Definition lapic.h:71
@ LAPIC_REG_LVT_THERMAL
Definition lapic.h:70
@ LAPIC_REG_LVT_LINT0
Definition lapic.h:72
@ LAPIC_REG_ID
Definition lapic.h:52
@ LAPIC_REG_LVT_ERROR
Definition lapic.h:74
@ LAPIC_REG_ICR1
Definition lapic.h:68
@ LAPIC_REG_EOI
Definition lapic.h:57
@ LAPIC_REG_LVT_LINT1
Definition lapic.h:73
@ LAPIC_REG_SPURIOUS
Definition lapic.h:61
@ LAPIC_REG_ICR0
Definition lapic.h:67
@ LAPIC_REG_LVT_TIMER
Definition lapic.h:69
@ LAPIC_ICR_STARTUP
Definition lapic.h:106
@ LAPIC_ICR_FIXED
Definition lapic.h:101
@ LAPIC_ICR_INIT
Definition lapic.h:105
#define LOG_ERR(format,...)
Definition log.h:93
#define LOG_INFO(format,...)
Definition log.h:91
#define PML_LOWER_TO_HIGHER(addr)
Converts an address from the lower half to the higher half.
@ PML_PRESENT
@ PML_WRITE
@ PML_GLOBAL
void * vmm_map(space_t *space, void *virtAddr, phys_addr_t physAddr, size_t length, pml_flags_t flags, space_callback_func_t func, void *data)
Maps physical memory to virtual memory in a given address space.
Definition vmm.c:226
void * vmm_unmap(space_t *space, void *virtAddr, size_t length)
Unmaps virtual memory from a given address space.
Definition vmm.c:336
#define NULL
Pointer error value.
Definition NULL.h:25
#define ERR
Integer error value.
Definition ERR.h:17
#define PAGE_SIZE
The size of a memory page in bytes.
Definition PAGE_SIZE.h:8
static void lapic_eoi(void)
Definition lapic.c:55
static uintptr_t lapicBase
Definition lapic.c:14
static void lapic_interrupt(cpu_t *cpu, irq_virt_t virt)
Definition lapic.c:47
static ipi_chip_t lapicIpiChip
Definition lapic.c:60
static netfs_family_t local
Definition local.c:479
static void msr_write(uint32_t msr, uint64_t value)
Definition regs.h:73
static uint64_t msr_read(uint32_t msr)
Definition regs.h:65
#define MSR_LAPIC
Definition regs.h:13
__UINT32_TYPE__ uint32_t
Definition stdint.h:15
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
__UINTPTR_TYPE__ uintptr_t
Definition stdint.h:43
CPU structure.
Definition cpu.h:84
cpu_id_t id
Definition cpu.h:86
Inter-Processor Interrupt (IPI) chip structure.
Definition ipi.h:36
const char * name
Definition ipi.h:37
Local APIC Structure.
Definition lapic.h:125
lapic_id_t lapicId
Definition lapic.h:127
Multiple APIC Description Table.
Definition tables.h:180
sdt_header_t header
Definition tables.h:181
uint32_t localInterruptControllerAddress
Definition tables.h:182
uint32_t length
Definition acpi.h:92
#define WRITE_32(address, value)
Definition utils.h:16
#define READ_32(address)
Definition utils.h:15