From 0265810d9a1034601356a3f6678695984dee8193 Mon Sep 17 00:00:00 2001 From: Chloe M Date: Wed, 15 Jul 2026 19:48:12 -0400 Subject: [PATCH] ptos/amd64: cpu: Add MP trampoline stub Signed-off-by: Chloe M --- build.sh | 3 +- service/ptos/arch/amd64/Makefile | 11 +++- service/ptos/arch/amd64/cpu/mp.c | 51 +++++++++++++++++++ service/ptos/arch/amd64/cpu/mp_trampoline.asm | 14 +++++ service/ptos/arch/amd64/dev/link.ld | 6 +++ 5 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 service/ptos/arch/amd64/cpu/mp_trampoline.asm diff --git a/build.sh b/build.sh index f3d9dd9..e6e1ebe 100755 --- a/build.sh +++ b/build.sh @@ -37,7 +37,8 @@ build_verify() { check_deps \ clang \ rsync \ - xorriso + xorriso \ + nasm # We need build envs !! if [ -z "${PT_BUILD_SOURCED}" ] diff --git a/service/ptos/arch/amd64/Makefile b/service/ptos/arch/amd64/Makefile index 927f0b7..c97b739 100644 --- a/service/ptos/arch/amd64/Makefile +++ b/service/ptos/arch/amd64/Makefile @@ -33,12 +33,21 @@ CFLAGS = \ -I$(PT_PROJECT_ROOT)/service/spkg/head/ .PHONY: all -all: bin +all: trampoline bin .PHONY: bin bin: $(OFILES) $(ASMOFILES) $(PROMPT) "LD" $(KERNEL_PATH) $(SYS_LD) -Tdev/link.ld $(MISC_OFILES) -o $(KERNEL_PATH) + # Copy the MP trampoline + objcopy \ + --update-section .mp_trampoline=cpu/mp_trampoline.bin \ + $(KERNEL_PATH) \ + $(KERNEL_PATH) + +.PHONY: trampoline +trampoline: + nasm -fbin cpu/mp_trampoline.asm -o cpu/mp_trampoline.bin -include $(CDFILES) %.o: %.c diff --git a/service/ptos/arch/amd64/cpu/mp.c b/service/ptos/arch/amd64/cpu/mp.c index eb78e24..129045f 100644 --- a/service/ptos/arch/amd64/cpu/mp.c +++ b/service/ptos/arch/amd64/cpu/mp.c @@ -7,11 +7,18 @@ */ #include +#include #include #include #include #include +#include #include +#include + +/* Bring up region */ +#define BUA_BASE 0x9000 +#define BUA_LENGTH PAGESIZE /* Max processors to bootstrap */ #define CPU_MAX 64 @@ -22,6 +29,13 @@ /* Number of processor cores */ static USHORT CoreCount = 0; +/* + * The multiprocessor trampoline should fit within a single + * page and be no larger than such. + */ +ALIGN(8) SECTION(".mp_trampoline") +static UCHAR MpTrampoline[BUA_LENGTH]; + /* * Bootstrap a single core * @@ -32,10 +46,18 @@ static USHORT CoreCount = 0; static VOID BootstrapCore(ACPI_LOCAL_APIC *LocalApic) { + ULONG SelfId; + if (LocalApic == NULL) { return; } + /* Let's not shoot ourselves in the face */ + SelfId = MdLapicId(); + if (LocalApic->ApicId == SelfId) { + return; + } + ++CoreCount; } @@ -78,5 +100,34 @@ MpBootstrap(VOID) VOID HalMpBootstrap(VOID) { + MMU_VAS Vas; + MM_RANGE TrampolineRange; + PT_STATUS Status; + VOID *BuaDest; + + /* Map the BUA page */ + HalMmuReadVas(&Vas); + TrampolineRange.VirtualBase = BUA_BASE; + TrampolineRange.PhysicalBase = BUA_BASE; + TrampolineRange.Length = BUA_LENGTH; + Status = MmMapRange( + &Vas, + &TrampolineRange, + PAGE_READ | PAGE_WRITE | PAGE_EXEC, + PAGESIZE_4K + ); + + if (PT_ERROR(Status)) { + KeBugCheck( + BUGCHECK_MISC, + "failed to map mp bringup area\n" + ); + } + + /* Copy trampoline to the BUA page */ + BuaDest = (VOID *)BUA_BASE; + RtlMemCpy(BuaDest, MpTrampoline, BUA_LENGTH); + + /* Start up the APs */ MpBootstrap(); } diff --git a/service/ptos/arch/amd64/cpu/mp_trampoline.asm b/service/ptos/arch/amd64/cpu/mp_trampoline.asm new file mode 100644 index 0000000..994cf94 --- /dev/null +++ b/service/ptos/arch/amd64/cpu/mp_trampoline.asm @@ -0,0 +1,14 @@ +;; +;; Copyright (c) 2026, Chloe M. +;; Provided under the BSD-3 clause. +;; + +[org 0x9000] +[global _start] + +_start: + cli + hlt + jmp _start + +times 4096 - ($ - $$) db 0 diff --git a/service/ptos/arch/amd64/dev/link.ld b/service/ptos/arch/amd64/dev/link.ld index 71943dc..d2e8327 100644 --- a/service/ptos/arch/amd64/dev/link.ld +++ b/service/ptos/arch/amd64/dev/link.ld @@ -41,6 +41,12 @@ SECTIONS . += CONSTANT(MAXPAGESIZE); + .mp_trampoline : ALIGN(8) { + KEEP(*(.mp_trampoline)) + } + + . += CONSTANT(MAXPAGESIZE); + .data : { *(.data) } :data