From d6d40e9ecf2aff5b1e265d26f7651e8633eddabd Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Wed, 19 Nov 2025 19:00:55 -0500 Subject: kern/amd64: mp: Load per-core GDT/IDT Signed-off-by: Ian Moffett --- sys/arch/amd64/cpu/mp.c | 15 +++++++++++++++ sys/inc/arch/amd64/gdt.h | 17 +++++++++++++++++ sys/inc/mu/cpu.h | 7 ++++++- 3 files changed, 38 insertions(+), 1 deletion(-) (limited to 'sys') diff --git a/sys/arch/amd64/cpu/mp.c b/sys/arch/amd64/cpu/mp.c index 1fdc9f7..6911a49 100644 --- a/sys/arch/amd64/cpu/mp.c +++ b/sys/arch/amd64/cpu/mp.c @@ -105,6 +105,9 @@ struct mtrr_save { uintptr_t physmask[256]; } mtrr_save; +extern struct gdtr GDTR; +extern void idt_load(void); + static struct cpu_info *cpu_list[MAX_CPUS]; static struct ap_bootspace bs; static volatile size_t ap_sync = 0; @@ -293,6 +296,18 @@ cpu_lm_entry(void) atomic_inc_int(&aps_up); cpu_list[aps_up] = ci; + /* Initialize the GDT */ + memcpy(ci->ap_gdt, (void *)GDTR.offset, GDTR.limit); + ci->ap_gdtr.offset = (uintptr_t)&ci->ap_gdt[0]; + ci->ap_gdtr.limit = GDTR.limit; + __asmv( + "lgdt %0" + : + : "m" (ci->ap_gdtr) + : "memory" + ); + + idt_load(); cpu_idle(); __builtin_unreachable(); } diff --git a/sys/inc/arch/amd64/gdt.h b/sys/inc/arch/amd64/gdt.h index dacfbda..291ab3c 100644 --- a/sys/inc/arch/amd64/gdt.h +++ b/sys/inc/arch/amd64/gdt.h @@ -30,9 +30,26 @@ #ifndef _MACHINE_GDT_H_ #define _MACHINE_GDT_H_ +#include +#include + #define GDT_KERNCODE 0x08 #define GDT_KERNDATA 0x10 #define GDT_USERCODE 0x18 #define GDT_USERDATA 0x20 +struct __packed gdt_entry { + uint16_t limit; + uint16_t base_low; + uint8_t base_mid; + uint8_t access; + uint8_t granularity; + uint8_t base_hi; +}; + +struct __packed gdtr { + uint16_t limit; + uintptr_t offset; +}; + #endif /* !_MACHINE_GDT_H_ */ diff --git a/sys/inc/mu/cpu.h b/sys/inc/mu/cpu.h index 288ba9e..524e42a 100644 --- a/sys/inc/mu/cpu.h +++ b/sys/inc/mu/cpu.h @@ -33,7 +33,8 @@ #include #include #include -#include +#include /* shared */ +#include /* shared */ /* * Processor descriptor @@ -41,12 +42,16 @@ * @id: Logical ID of the processor * @mcb: Machine core block * @curproc: Current process + * @ap_gdt: GDT for APs [unused for BSP] + * @ap_gdtr: GDTR for APs [unused for BSP] * @pqueue: Process queue */ struct cpu_info { uint8_t id; struct mcb mcb; struct process *curproc; + struct gdt_entry ap_gdt[256]; + struct gdtr ap_gdtr; TAILQ_HEAD(, process) pqueue; }; -- cgit v1.2.3