x86_64: init local apic on boot, or legacy pic/pit as fallback

This commit is contained in:
2023-03-19 20:36:36 +00:00
parent 7e26050bde
commit 8e9127cd6a
13 changed files with 274 additions and 72 deletions

View File

@@ -3,5 +3,8 @@
kern_status_t acpi_init(void) kern_status_t acpi_init(void)
{ {
apic_init();
smp_init();
return KERN_OK; return KERN_OK;
} }

109
arch/x86_64/acpi/apic.c Normal file
View File

@@ -0,0 +1,109 @@
#include <socks/printk.h>
#include <socks/vm.h>
#include <socks/cpu.h>
#include <arch/acpi.h>
#include <arch/msr.h>
#include <arch/ports.h>
#include <arch/pit.h>
#define PIC1_DATA 0x21
#define PIC2_DATA 0xA1
#define IA32_APIC_BASE_MSR 0x1B
#define IA32_APIC_BASE_MSR_BSP 0x100
#define IA32_APIC_BASE_MSR_ENABLE 0x800
static uint32_t *lapic_base;
extern int check_apic(void);
static uintptr_t apic_get_base(void)
{
return rdmsr(IA32_APIC_BASE_MSR);
}
static void apic_set_base(uintptr_t addr)
{
wrmsr(IA32_APIC_BASE_MSR, addr);
}
static void disable_8259(void)
{
/* mask all interrupts on PICs 1 and 2 */
outportb(PIC1_DATA, 0xFF);
outportb(PIC2_DATA, 0xFF);
}
static uint32_t local_apic_read(uint32_t reg)
{
return READ_ONCE(lapic_base[reg >> 4]);
}
static void local_apic_write(uint32_t reg, uint32_t val)
{
}
static void *find_lapic(struct acpi_madt *madt)
{
phys_addr_t local_apic = madt->m_lapic_ptr;
unsigned char *p = (unsigned char *)madt + sizeof *madt;
unsigned char *madt_end = (unsigned char *)madt + madt->m_base.s_length;
while (p < madt_end) {
struct acpi_madt_record *rec = (struct acpi_madt_record *)p;
struct lapic_override_record *lapic;
switch (rec->r_type) {
case ACPI_MADT_LAPIC_OVERRIDE:
lapic = (struct lapic_override_record *)(rec + 1);
return vm_phys_to_virt(lapic->l_lapic_ptr);
default:
break;
}
p += rec->r_length;
}
return vm_phys_to_virt(local_apic);
}
kern_status_t local_apic_enable(void)
{
struct acpi_madt *madt = (struct acpi_madt *)acpi_find_sdt(ACPI_SIG_MADT);
if (!madt) {
return KERN_UNSUPPORTED;
}
apic_set_base(apic_get_base());
lapic_base = find_lapic(madt);
local_apic_write(0xF0, local_apic_read(0xF0) | 0x100);
printk("acpi: enabled local APIC on core %u", this_cpu());
return KERN_OK;
}
static void configure_legacy_pic(void)
{
printk("acpi: APIC unavailable, using 8259 PIC");
pit_init(1000);
}
kern_status_t apic_init(void)
{
if (check_apic() == 0) {
configure_legacy_pic();
return KERN_UNSUPPORTED;
}
if (local_apic_enable() != KERN_OK) {
configure_legacy_pic();
return KERN_UNSUPPORTED;
}
disable_8259();
return KERN_OK;
}

View File

@@ -0,0 +1,18 @@
.global check_apic
.type check_apic, @function
check_apic:
push %rbp
mov %rsp, %rbp
push %rbx
mov $1, %rax
cpuid
andl $0x200, %edx
shr $9, %edx
mov %rdx, %rax
pop %rbx
pop %rbp
ret

View File

@@ -24,7 +24,6 @@ static int init_rsdt(void)
struct acpi_rsdp_10 *x = search_for_rsdp(); struct acpi_rsdp_10 *x = search_for_rsdp();
if (!x) { if (!x) {
printk("acpi: not available");
return -1; return -1;
} }
@@ -35,7 +34,6 @@ static int init_rsdt(void)
} }
if (chksum & 0xFF) { if (chksum & 0xFF) {
printk("acpi: not available");
return -1; return -1;
} }
@@ -47,7 +45,6 @@ static int init_rsdt(void)
} }
if (chksum & 0xFF) { if (chksum & 0xFF) {
printk("acpi: not available");
return -1; return -1;
} }
} }

View File

@@ -3,27 +3,6 @@
#include <socks/vm.h> #include <socks/vm.h>
#include <socks/cpu.h> #include <socks/cpu.h>
#define AP_TRAMPOLINE_PADDR 0x8000
#define LAPIC_IPI_STATUS_REG 0x0280
#define LAPIC_IPI_DEST_REG 0x0310
#define LAPIC_IPI_ICR_REG 0x0300
struct acpi_madt_record {
uint8_t r_type;
uint8_t r_length;
} __packed;
struct lapic_record {
uint8_t l_cpu_id;
uint8_t l_apic_id;
uint32_t l_flags;
} __packed;
struct lapic_override_record {
uint16_t l_reserved;
uint64_t l_lapic_ptr;
} __packed;
extern uint8_t acpi_bsp_lapic_id(void); extern uint8_t acpi_bsp_lapic_id(void);
extern char ap_trampoline[]; extern char ap_trampoline[];
@@ -61,31 +40,6 @@ static int __used init_ap(struct acpi_madt_record *rec, void *bsp_lapic, uint8_t
return 0; return 0;
} }
static void *__used find_lapic(struct acpi_madt *madt)
{
phys_addr_t local_apic = madt->m_lapic_ptr;
unsigned char *p = (unsigned char *)madt + sizeof *madt;
unsigned char *madt_end = (unsigned char *)madt + madt->m_base.s_length;
while (p < madt_end) {
struct acpi_madt_record *rec = (struct acpi_madt_record *)p;
struct lapic_override_record *lapic;
switch (rec->r_type) {
case ACPI_MADT_LAPIC_OVERRIDE:
lapic = (struct lapic_override_record *)(rec + 1);
return vm_phys_to_virt(lapic->l_lapic_ptr);
default:
break;
}
p += rec->r_length;
}
return vm_phys_to_virt(local_apic);
}
/* /*
kern_status_t acpi_smp_init(void) kern_status_t acpi_smp_init(void)
{ {
@@ -125,10 +79,20 @@ kern_status_t acpi_smp_init(void)
} }
*/ */
static void no_smp_config(void)
{
cpu_set_available(0);
cpu_set_online(0);
ml_cpu_block *self = ml_this_cpu();
self->c_cpu_id = 0;
}
kern_status_t acpi_scan_cpu_topology(void) kern_status_t acpi_scan_cpu_topology(void)
{ {
struct acpi_madt *madt = (struct acpi_madt *)acpi_find_sdt(ACPI_SIG_MADT); struct acpi_madt *madt = (struct acpi_madt *)acpi_find_sdt(ACPI_SIG_MADT);
if (!madt) { if (!madt) {
no_smp_config();
return KERN_UNSUPPORTED; return KERN_UNSUPPORTED;
} }
@@ -171,3 +135,8 @@ kern_status_t acpi_scan_cpu_topology(void)
printk("acpi: found %u logical cores", nr_processors); printk("acpi: found %u logical cores", nr_processors);
return KERN_OK; return KERN_OK;
} }
kern_status_t smp_init(void)
{
return KERN_OK;
}

View File

@@ -13,6 +13,11 @@
#define ACPI_SIG_FADT 0x50434146 #define ACPI_SIG_FADT 0x50434146
#define ACPI_SIG_MADT 0x43495041 #define ACPI_SIG_MADT 0x43495041
#define AP_TRAMPOLINE_PADDR 0x8000
#define LAPIC_IPI_STATUS_REG 0x0280
#define LAPIC_IPI_DEST_REG 0x0310
#define LAPIC_IPI_ICR_REG 0x0300
struct acpi_rsdp_10 { struct acpi_rsdp_10 {
char r_sig[8]; char r_sig[8];
uint8_t r_chksum; uint8_t r_chksum;
@@ -65,9 +70,29 @@ struct acpi_madt {
uint32_t m_flags; uint32_t m_flags;
} __packed; } __packed;
struct acpi_madt_record {
uint8_t r_type;
uint8_t r_length;
} __packed;
struct lapic_record {
uint8_t l_cpu_id;
uint8_t l_apic_id;
uint32_t l_flags;
} __packed;
struct lapic_override_record {
uint16_t l_reserved;
uint64_t l_lapic_ptr;
} __packed;
extern kern_status_t acpi_init(void); extern kern_status_t acpi_init(void);
extern kern_status_t acpi_scan_cpu_topology(void); extern kern_status_t acpi_scan_cpu_topology(void);
extern kern_status_t apic_init(void);
extern kern_status_t smp_init(void);
extern struct acpi_sdt *acpi_find_sdt(uint32_t sig); extern struct acpi_sdt *acpi_find_sdt(uint32_t sig);
#endif #endif

View File

@@ -2,10 +2,35 @@
#define ARCH_IRQ_H_ #define ARCH_IRQ_H_
#include <socks/compiler.h> #include <socks/compiler.h>
#include <socks/queue.h>
#include <stdint.h> #include <stdint.h>
#define NR_IDT_ENTRIES 48 #define NR_IDT_ENTRIES 48
typedef enum irq_vector {
IRQ0 = 32,
IRQ1,
IRQ2,
IRQ3,
IRQ4,
IRQ5,
IRQ6,
IRQ7,
IRQ8,
IRQ9,
IRQ10,
IRQ11,
IRQ12,
IRQ13,
IRQ14,
IRQ15,
} irq_vector_t;
typedef struct irq_hook {
queue_entry_t irq_entry;
int (*irq_callback)(void);
} irq_hook_t;
struct cpu_context { struct cpu_context {
uint64_t r15, r14, r13, r12, r11, r10, r9, r8; uint64_t r15, r14, r13, r12, r11, r10, r9, r8;
uint64_t rdi, rsi, rbp, unused_rsp, rbx, rdx, rcx, rax; uint64_t rdi, rsi, rbp, unused_rsp, rbx, rdx, rcx, rax;
@@ -40,4 +65,6 @@ typedef void (*int_hook)(struct cpu_context *);
extern int idt_init(struct idt_ptr *idtp); extern int idt_init(struct idt_ptr *idtp);
extern int idt_load(struct idt_ptr *idtp); extern int idt_load(struct idt_ptr *idtp);
extern void hook_irq(irq_vector_t vec, irq_hook_t *hook);
#endif #endif

View File

@@ -0,0 +1,6 @@
#ifndef ARCH_PIT_H_
#define ARCH_PIT_H_
extern void pit_init(unsigned int hz);
#endif

View File

@@ -16,6 +16,9 @@ typedef struct ml_cpu_block {
unsigned int c_cpu_id; unsigned int c_cpu_id;
} ml_cpu_block; } ml_cpu_block;
#define ml_cpu_pause() asm volatile("hlt")
#define ml_cpu_relax() asm volatile("pause")
extern int ml_init_bootcpu(void); extern int ml_init_bootcpu(void);
extern int ml_cpu_block_init(ml_cpu_block *p); extern int ml_cpu_block_init(ml_cpu_block *p);

View File

@@ -70,5 +70,12 @@ int ml_init(uintptr_t arg)
vm_bootstrap(vm_zones, sizeof vm_zones / sizeof vm_zones[0]); vm_bootstrap(vm_zones, sizeof vm_zones / sizeof vm_zones[0]);
object_bootstrap();
sched_init();
acpi_init();
asm volatile("sti");
return 0; return 0;
} }

View File

@@ -63,6 +63,7 @@ extern void syscall_gate();
extern uintptr_t pf_faultptr(void); extern uintptr_t pf_faultptr(void);
static int_hook isr_handlers[NR_IDT_ENTRIES]; static int_hook isr_handlers[NR_IDT_ENTRIES];
static queue_t irq_hooks[32];
static struct idt idt; static struct idt idt;
static int idt_initialised = 0; static int idt_initialised = 0;
@@ -103,9 +104,7 @@ static void set_syscall_gate(uintptr_t rip)
/* sysret adds 0x10 to this to get cs, and 0x8 to get ss /* sysret adds 0x10 to this to get cs, and 0x8 to get ss
* note that the CPU should force the RPL to 3 when loading * note that the CPU should force the RPL to 3 when loading
* the selector by using user_cs | 3. However, this doesn't happen * the selector by using user_cs | 3. However, this doesn't happen
* in certain scenarios (specifically, QEMU + KVM on a Ryzen 5 1600X). * in certain scenarios (specifically, QEMU + KVM on a Ryzen 5 1600X). */
* It's probably a Magenta bug, but just in case it's not,
* we perform the RPL OR ourselves */
uint64_t user_cs = 0x13; uint64_t user_cs = 0x13;
uint64_t kernel_cs = 0x8; uint64_t kernel_cs = 0x8;
@@ -237,6 +236,11 @@ void irq_dispatch(struct cpu_context *regs)
} }
outportb(0x20, 0x20); outportb(0x20, 0x20);
queue_t *hooks = &irq_hooks[regs->int_no - IRQ0];
queue_foreach(irq_hook_t, hook, hooks, irq_entry) {
hook->irq_callback();
}
} }
void syscall_dispatch(struct cpu_context *regs) void syscall_dispatch(struct cpu_context *regs)
@@ -253,3 +257,9 @@ void ml_int_disable()
{ {
asm volatile("cli"); asm volatile("cli");
} }
void hook_irq(irq_vector_t vec, irq_hook_t *hook)
{
queue_t *hook_queue = &irq_hooks[vec - IRQ0];
queue_push_back(hook_queue, &hook->irq_entry);
}

29
arch/x86_64/pit.c Normal file
View File

@@ -0,0 +1,29 @@
#include <socks/printk.h>
#include <arch/ports.h>
#include <arch/irq.h>
static int pit_callback(void)
{
printk("tick");
return 0;
}
static irq_hook_t pit_irq_hook = {
.irq_callback = pit_callback
};
void pit_init(unsigned int hz)
{
unsigned int divisor = 1193180 / hz;
outportb(0x43, 0x36);
uint8_t lo = (uint8_t)(divisor & 0xFF);
uint8_t hi = (uint8_t)((divisor >> 8) & 0xFF);
outportb(0x40, lo);
outportb(0x40, hi);
hook_irq(IRQ0, &pit_irq_hook);
printk("clock: 8253 PIT initialised at %uHz", hz);
}

View File

@@ -20,12 +20,11 @@ void kernel_init(uintptr_t arg)
{ {
ml_init(arg); ml_init(arg);
object_bootstrap();
sched_init();
printk("kernel_init() running on processor %u", this_cpu()); printk("kernel_init() running on processor %u", this_cpu());
run_all_tests(); run_all_tests();
ml_halt_cpu(); while (1) {
ml_cpu_pause();
}
} }