kernel: implement cpu IDs and per-cpu variables

This commit is contained in:
2023-03-18 19:35:00 +00:00
parent 56bc47f570
commit 57eaf4e01c
14 changed files with 232 additions and 18 deletions

View File

@@ -3,10 +3,5 @@
kern_status_t acpi_init(void)
{
kern_status_t status = acpi_smp_init();
if (status != KERN_OK) {
return status;
}
return KERN_OK;
}

View File

@@ -1,6 +1,7 @@
#include <arch/acpi.h>
#include <socks/printk.h>
#include <socks/vm.h>
#include <socks/cpu.h>
#define AP_TRAMPOLINE_PADDR 0x8000
#define LAPIC_IPI_STATUS_REG 0x0280
@@ -26,7 +27,7 @@ struct lapic_override_record {
extern uint8_t acpi_bsp_lapic_id(void);
extern char ap_trampoline[];
static int send_ipi(void *lapic, unsigned int target_id, uint32_t payload)
static int __used send_ipi(void *lapic, unsigned int target_id, uint32_t payload)
{
uintptr_t lapic_ptr = (uintptr_t)lapic;
@@ -38,7 +39,7 @@ static int send_ipi(void *lapic, unsigned int target_id, uint32_t payload)
return 0;
}
static int init_ap(struct acpi_madt_record *rec, void *bsp_lapic, uint8_t bsp_id)
static int __used init_ap(struct acpi_madt_record *rec, void *bsp_lapic, uint8_t bsp_id)
{
struct lapic_record *lapic = (struct lapic_record *)(rec + 1);
if (!(lapic->l_flags & 0x1) && !(lapic->l_flags & 0x2)) {
@@ -60,7 +61,7 @@ static int init_ap(struct acpi_madt_record *rec, void *bsp_lapic, uint8_t bsp_id
return 0;
}
static void *find_lapic(struct acpi_madt *madt)
static void *__used find_lapic(struct acpi_madt *madt)
{
phys_addr_t local_apic = madt->m_lapic_ptr;
@@ -85,6 +86,7 @@ static void *find_lapic(struct acpi_madt *madt)
return vm_phys_to_virt(local_apic);
}
/*
kern_status_t acpi_smp_init(void)
{
struct acpi_madt *madt = (struct acpi_madt *)acpi_find_sdt(ACPI_SIG_MADT);
@@ -118,6 +120,54 @@ kern_status_t acpi_smp_init(void)
p += rec->r_length;
}
printk("acpi: initialised %u processors", nr_processors);
printk("acpi: found %u logical cores", nr_processors);
return KERN_OK;
}
*/
kern_status_t acpi_scan_cpu_topology(void)
{
struct acpi_madt *madt = (struct acpi_madt *)acpi_find_sdt(ACPI_SIG_MADT);
if (!madt) {
return KERN_UNSUPPORTED;
}
uint8_t bsp_id = acpi_bsp_lapic_id();
//void *bsp_lapic = find_lapic(madt);
unsigned char *p = (unsigned char *)madt + sizeof *madt;
unsigned char *madt_end = (unsigned char *)madt + madt->m_base.s_length;
unsigned int nr_processors = 0;
while (p < madt_end) {
struct acpi_madt_record *rec = (struct acpi_madt_record *)p;
if (rec->r_type != ACPI_MADT_LAPIC) {
p += rec->r_length;
continue;
}
struct lapic_record *lapic = (struct lapic_record *)(rec + 1);
if (!(lapic->l_flags & 0x1) && !(lapic->l_flags & 0x2)) {
/* processor cannot be used */
continue;
}
cpu_set_available(lapic->l_apic_id);
nr_processors++;
p += rec->r_length;
}
cpu_set_online(bsp_id);
/* store the BSP ID in the current cpu block */
ml_cpu_block *self = ml_this_cpu();
self->c_cpu_id = bsp_id;
printk("acpi: found %u logical cores", nr_processors);
return KERN_OK;
}

View File

@@ -1,7 +1,9 @@
#include <socks/machine/cpu.h>
#include <arch/msr.h>
int ml_cpu_block_init(ml_cpu_block *p)
{
p->c_this = p;
gdt_init(&p->c_gdt, &p->c_gdt_ptr);
idt_init(&p->c_idt_ptr);
return 0;
@@ -11,5 +13,6 @@ int ml_cpu_block_use(ml_cpu_block *p)
{
gdt_load(&p->c_gdt_ptr);
idt_load(&p->c_idt_ptr);
wrmsr(MSR_GS_BASE, (uint64_t)p);
return 0;
}

View File

@@ -6,3 +6,35 @@ ml_halt_cpu:
1: cli
hlt
jmp 1b
.global ml_this_cpu
.type ml_this_cpu, @function
ml_this_cpu:
movq %gs:0, %rax
ret
.global rdmsr
.type rdmsr, @function
rdmsr:
mov %rdi, %rcx
rdmsr
shl $32, %rdx
orq %rax, %rdx
ret
.global wrmsr
.type wrmsr, @function
wrmsr:
mov %rdi, %rcx
mov %rsi, %rax
mov %rsi, %rdx
shr $32, %rdx
wrmsr
ret

View File

@@ -36,7 +36,7 @@ struct acpi_rsdp {
struct acpi_rsdp_20 rsdp_20;
};
} __packed;
struct acpi_sdt {
char s_sig[4];
uint32_t s_length;
@@ -66,7 +66,7 @@ struct acpi_madt {
} __packed;
extern kern_status_t acpi_init(void);
extern kern_status_t acpi_smp_init(void);
extern kern_status_t acpi_scan_cpu_topology(void);
extern struct acpi_sdt *acpi_find_sdt(uint32_t sig);

View File

@@ -0,0 +1,12 @@
#ifndef ARCH_MSR_H_
#define ARCH_MSR_H_
#include <stdint.h>
#define MSR_GS_BASE 0xC0000101
/* defined in cpu_ctrl.S */
extern uint64_t rdmsr(uint32_t id);
extern void wrmsr(uint32_t id, uint64_t val);
#endif

View File

@@ -4,11 +4,16 @@
#include <arch/gdt.h>
#include <arch/irq.h>
#define ml_cpu_block_get_id(p) ((p)->c_cpu_id)
typedef struct ml_cpu_block {
struct ml_cpu_block *c_this;
struct gdt c_gdt;
struct gdt_ptr c_gdt_ptr;
struct idt_ptr c_idt_ptr;
unsigned int c_cpu_id;
} ml_cpu_block;
extern int ml_init_bootcpu(void);
@@ -18,5 +23,6 @@ extern int ml_cpu_block_use(ml_cpu_block *p);
/* defined in cpu_ctrl.S */
extern void ml_halt_cpu(void);
extern ml_cpu_block *ml_this_cpu(void);
#endif

View File

@@ -3,6 +3,8 @@
#include <socks/object.h>
#include <arch/e820.h>
#include <socks/init.h>
#include <socks/percpu.h>
#include <socks/cpu.h>
#include <socks/memblock.h>
#include <socks/vm.h>
#include <socks/printk.h>
@@ -53,7 +55,13 @@ int ml_init(uintptr_t arg)
pmap_bootstrap();
acpi_init();
acpi_scan_cpu_topology();
init_per_cpu_areas();
cpu_data_t *this_cpu = get_this_cpu();
this_cpu->c_flags = CPU_ONLINE;
this_cpu->c_id = this_cpu();
put_cpu(this_cpu);
vm_zone_descriptor_t vm_zones[] = {
{ .zd_id = VM_ZONE_DMA, .zd_node = 0, .zd_name = "dma", .zd_base = 0x00, .zd_limit = 0xffffff },