:: commit 81f4b86916a69e55fd19e74d63e1faa8fdc172c1

mintsuki <mintsuki@protonmail.com> — 2021-07-05 21:28

parents: 3b0afa71d8

build: Unify do_32() and direct calls under a single common_spinup()

diff --git a/stage23/Makefile b/stage23/Makefile
index c7707fea..e1037cd6 100644
--- a/stage23/Makefile
+++ b/stage23/Makefile
@@ -240,11 +240,15 @@ $(BUILDDIR)/%.32.o: %.32.c
 	rm $@.32
 endif
 
+ifeq ($(TARGET), bios)
 $(BUILDDIR)/%.o: %.asm
 	nasm $< -F dwarf -g -Werror -f elf32 -o $@
+endif
 
+ifeq ($(TARGET), uefi)
 $(BUILDDIR)/%.o: %.asm64
 	nasm $< -F dwarf -g -Werror -f elf64 -o $@
+endif
 
 clean:
 	rm -rf $(BUILDDIR)
diff --git a/stage23/lib/blib.h b/stage23/lib/blib.h
index 31395cf9..ce1815ce 100644
--- a/stage23/lib/blib.h
+++ b/stage23/lib/blib.h
@@ -63,8 +63,6 @@ uint64_t strtoui(const char *s, const char **end, int base);
 
 typedef char symbol[];
 
-#if defined (uefi)
-__attribute__((noreturn)) void do_32(void *fnptr, int args, ...);
-#endif
+__attribute__((noreturn)) void common_spinup(void *fnptr, int args, ...);
 
 #endif
diff --git a/stage23/lib/spinup.asm b/stage23/lib/spinup.asm
new file mode 100644
index 00000000..2d7ab54f
--- /dev/null
+++ b/stage23/lib/spinup.asm
@@ -0,0 +1,30 @@
+section .rodata
+
+invalid_idt:
+    dd 0, 0
+
+section .text
+
+global common_spinup
+bits 32
+common_spinup:
+    cli
+
+    lidt [invalid_idt]
+
+    ; We don't need the return address
+    add esp, 4
+
+    ; Get function address
+    pop edi
+
+    ; We don't need the argument count
+    add esp, 4
+
+    mov eax, 0x00000011
+    mov cr0, eax
+
+    xor eax, eax
+    mov cr4, eax
+
+    call edi
diff --git a/stage23/lib/do_32.asm64 b/stage23/lib/spinup.asm64
similarity index 94%
rename from stage23/lib/do_32.asm64
rename to stage23/lib/spinup.asm64
index 2b63ba8e..73518f7f 100644
--- a/stage23/lib/do_32.asm64
+++ b/stage23/lib/spinup.asm64
@@ -12,15 +12,16 @@ section .text
 
 extern gdt
 
-global do_32
+global common_spinup
 bits 64
-do_32:
+common_spinup:
+    cli
+
     lgdt [rel gdt]
+    lidt [rel invalid_idt]
 
     mov rbp, rsp
 
-    lidt [rel invalid_idt]
-
     cmp esi, 4
     jle .no_stack_args
 
@@ -37,13 +38,6 @@ do_32:
     push32 ecx
     push32 edx
 
-    mov eax, 0x20
-    mov ds, ax
-    mov es, ax
-    mov fs, ax
-    mov gs, ax
-    mov ss, ax
-
     lea rbx, [rel .go_32]
 
     push 0x18
@@ -52,6 +46,13 @@ do_32:
 
 bits 32
 .go_32:
+    mov eax, 0x20
+    mov ds, ax
+    mov es, ax
+    mov fs, ax
+    mov gs, ax
+    mov ss, ax
+
     mov eax, 0x00000011
     mov cr0, eax
 
diff --git a/stage23/protos/linux.c b/stage23/protos/linux.c
index 1961b9f1..7c654606 100644
--- a/stage23/protos/linux.c
+++ b/stage23/protos/linux.c
@@ -576,11 +576,5 @@ void linux_load(char *config, char *cmdline) {
     // Spin up
     ///////////////////////////////////////
 
-#if defined (uefi)
-    do_32(linux_spinup, 2, (void *)kernel_load_addr, boot_params);
-#endif
-
-#if defined (bios)
-    linux_spinup((void *)kernel_load_addr, boot_params);
-#endif
+    common_spinup(linux_spinup, 2, (void *)kernel_load_addr, boot_params);
 }
diff --git a/stage23/protos/multiboot1.c b/stage23/protos/multiboot1.c
index 83e3e204..4420f16f 100644
--- a/stage23/protos/multiboot1.c
+++ b/stage23/protos/multiboot1.c
@@ -16,6 +16,10 @@
 #include <mm/pmm.h>
 #include <drivers/vga_textmode.h>
 
+__attribute__((noreturn)) void multiboot1_spinup_32(
+                 uint32_t entry_point,
+                 uint32_t multiboot1_info);
+
 struct multiboot1_info multiboot1_info = {0};
 
 void multiboot1_load(char *config, char *cmdline) {
@@ -265,24 +269,8 @@ void multiboot1_load(char *config, char *cmdline) {
     multiboot1_info.mmap_addr = ((uint32_t)(size_t)mmap);
     multiboot1_info.flags |= (1 << 0) | (1 << 6);
 
-    multiboot1_spinup(entry_point, (uint32_t)(uintptr_t)&multiboot1_info);
-}
-
-__attribute__((noreturn)) void multiboot1_spinup_32(
-                 uint32_t entry_point,
-                 uint32_t multiboot1_info);
-
-__attribute__((noreturn)) void multiboot1_spinup(
-                 uint32_t entry_point, uint32_t multiboot1_info) {
     pic_flush();
 
-#if defined (uefi)
-    do_32(multiboot1_spinup_32, 2, entry_point, multiboot1_info);
-#endif
-
-#if defined (bios)
-    multiboot1_spinup_32(entry_point, multiboot1_info);
-#endif
-
-    __builtin_unreachable();
+    common_spinup(multiboot1_spinup_32, 2,
+                  entry_point, (uint32_t)(uintptr_t)&multiboot1_info);
 }
diff --git a/stage23/protos/stivale.c b/stage23/protos/stivale.c
index e9746222..73b1e3bc 100644
--- a/stage23/protos/stivale.c
+++ b/stage23/protos/stivale.c
@@ -379,20 +379,9 @@ __attribute__((noreturn)) void stivale_spinup(
     pic_mask_all();
     pic_flush();
 
-#if defined (uefi)
-    do_32(stivale_spinup_32, 9,
+    common_spinup(stivale_spinup_32, 9,
         bits, level5pg, (uint32_t)(uintptr_t)pagemap->top_level,
         (uint32_t)entry_point, (uint32_t)(entry_point >> 32),
         (uint32_t)stivale_struct, (uint32_t)(stivale_struct >> 32),
         (uint32_t)stack, (uint32_t)(stack >> 32));
-#endif
-
-#if defined (bios)
-    stivale_spinup_32(bits, level5pg, (uint32_t)(uintptr_t)pagemap->top_level,
-        (uint32_t)entry_point, (uint32_t)(entry_point >> 32),
-        (uint32_t)stivale_struct, (uint32_t)(stivale_struct >> 32),
-        (uint32_t)stack, (uint32_t)(stack >> 32));
-#endif
-
-    __builtin_unreachable();
 }
tab: 248 wrap: offon