:: commit 110d2dc7d4e2fd7e0f405fa64500e77430a40414

mintsuki <mintsuki@protonmail.com> — 2021-05-19 23:16

parents: 40e65eddb1

misc: Misc EFI-related adjustments

diff --git a/stage23/entry.s3.c b/stage23/entry.s3.c
index 5597f859..fb42c235 100644
--- a/stage23/entry.s3.c
+++ b/stage23/entry.s3.c
@@ -23,19 +23,16 @@
 void stage3_common(void);
 
 #if defined (uefi)
-EFI_STATUS EFIAPI efi_main(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable) {
+__attribute__((naked))
+EFI_STATUS efi_main(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable) {
     // Invalid return address of 0 to end stacktraces here
-    asm volatile (
+    asm (
         "push 0\n\t"
         "push 0\n\t"
         "xor eax, eax\n\t"
         "jmp uefi_entry\n\t"
-        :
-        : "D" (ImageHandle), "S" (SystemTable)
-        : "memory"
     );
-
-    __builtin_unreachable();
+    (void)ImageHandle; (void)SystemTable;
 }
 
 __attribute__((noreturn))
@@ -66,7 +63,7 @@ void uefi_entry(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable) {
                                    &loaded_image);
 
         if (status) {
-            panic("HandleProtocol failure (%x)\n", status);
+            panic("HandleProtocol failure (%x)", status);
         }
 
         boot_volume = disk_volume_from_efi_handle(loaded_image->DeviceHandle);
diff --git a/stage23/lib/do_32.asm64 b/stage23/lib/do_32.asm64
index 66d0f82c..2b63ba8e 100644
--- a/stage23/lib/do_32.asm64
+++ b/stage23/lib/do_32.asm64
@@ -52,17 +52,15 @@ do_32:
 
 bits 32
 .go_32:
-    mov eax, cr0
-    btr eax, 31
+    mov eax, 0x00000011
     mov cr0, eax
 
     mov ecx, 0xc0000080
-    rdmsr
-    btr eax, 8
+    xor eax, eax
+    xor edx, edx
     wrmsr
 
-    mov eax, cr4
-    btr eax, 5
+    xor eax, eax
     mov cr4, eax
 
     call edi
diff --git a/stage23/lib/panic.s2.c b/stage23/lib/panic.s2.c
index db751182..7acec053 100644
--- a/stage23/lib/panic.s2.c
+++ b/stage23/lib/panic.s2.c
@@ -10,8 +10,6 @@
 #include <mm/pmm.h>
 
 __attribute__((noreturn)) void panic(const char *fmt, ...) {
-    asm volatile ("cli" ::: "memory");
-
     va_list args;
 
     va_start(args, fmt);
diff --git a/stage23/protos/chainload.c b/stage23/protos/chainload.c
index 1943e9d6..773bc2c9 100644
--- a/stage23/protos/chainload.c
+++ b/stage23/protos/chainload.c
@@ -165,7 +165,7 @@ void chainload(char *config) {
                                efi_image_handle, &loaded_img_prot_guid,
                                &loader_loaded_image);
     if (status) {
-        panic("HandleProtocol failure (%x)\n", status);
+        panic("chainload: HandleProtocol failure (%x)", status);
     }
 
     EFI_LOADED_IMAGE_PROTOCOL *new_handle_loaded_image = NULL;
@@ -173,7 +173,7 @@ void chainload(char *config) {
                                new_handle, &loaded_img_prot_guid,
                                &new_handle_loaded_image);
     if (status) {
-        panic("HandleProtocol failure (%x)\n", status);
+        panic("chainload: HandleProtocol failure (%x)", status);
     }
 
     new_handle_loaded_image->DeviceHandle = loader_loaded_image->DeviceHandle;
diff --git a/stage23/protos/linux.32.c b/stage23/protos/linux.32.c
index 3de2d7e6..6e7d36a7 100644
--- a/stage23/protos/linux.32.c
+++ b/stage23/protos/linux.32.c
@@ -33,6 +33,15 @@ __attribute__((noreturn)) void linux_spinup(void *entry, void *boot_params) {
 #endif
     };
 
+    // Load invalid IDT
+    uint64_t invalid_idt[2] = {0, 0};
+    asm volatile (
+        "lidt %0"
+        :
+        : "m" (invalid_idt)
+        : "memory"
+    );
+
     asm volatile (
         "lgdt %0\n\t"
 
diff --git a/stage23/protos/linux.c b/stage23/protos/linux.c
index 2425ea44..eb266ad9 100644
--- a/stage23/protos/linux.c
+++ b/stage23/protos/linux.c
@@ -350,17 +350,17 @@ void linux_load(char *config, char *cmdline) {
 
     char *kernel_path = config_get_value(config, 0, "KERNEL_PATH");
     if (kernel_path == NULL)
-        panic("KERNEL_PATH not specified");
+        panic("linux: KERNEL_PATH not specified");
 
     if (!uri_open(kernel, kernel_path))
-        panic("Could not open kernel resource");
+        panic("linux: Could not open kernel resource");
 
     uint32_t signature;
     fread(kernel, &signature, 0x202, sizeof(uint32_t));
 
     // validate signature
     if (signature != 0x53726448) {
-        panic("Invalid Linux kernel signature");
+        panic("linux: Invalid Linux kernel signature");
     }
 
     size_t setup_code_size = 0;
@@ -389,7 +389,7 @@ void linux_load(char *config, char *cmdline) {
            setup_header->version >> 8, setup_header->version & 0xff);
 
     if (setup_header->version < 0x203) {
-        panic("Linux protocols < 2.03 are not supported");
+        panic("linux: Protocols < 2.03 are not supported");
     }
 
     setup_header->cmd_line_ptr = (uint32_t)(uintptr_t)cmdline;
@@ -408,7 +408,7 @@ void linux_load(char *config, char *cmdline) {
     setup_header->type_of_loader = 0xff;
 
     if (!(setup_header->loadflags & (1 << 0))) {
-        panic("Linux kernels that load at 0x10000 are not supported");
+        panic("linux: Kernels that load at 0x10000 are not supported");
     }
 
     setup_header->loadflags &= ~(1 << 5);     // print early messages
@@ -443,7 +443,7 @@ void linux_load(char *config, char *cmdline) {
 
         struct file_handle module;
         if (!uri_open(&module, module_path))
-            panic("Could not open `%s`", module_path);
+            panic("linux: Could not open `%s`", module_path);
 
         size_of_all_modules += module.size;
     }
@@ -466,7 +466,7 @@ void linux_load(char *config, char *cmdline) {
 
         struct file_handle module;
         if (!uri_open(&module, module_path))
-            panic("Could not open `%s`", module_path);
+            panic("linux: Could not open `%s`", module_path);
 
         print("linux: Loading module `%s`...\n", module_path);
 
diff --git a/stage23/protos/stivale.c b/stage23/protos/stivale.c
index 2f4dc51b..25b99265 100644
--- a/stage23/protos/stivale.c
+++ b/stage23/protos/stivale.c
@@ -41,12 +41,12 @@ void stivale_load(char *config, char *cmdline) {
 
     char *kernel_path = config_get_value(config, 0, "KERNEL_PATH");
     if (kernel_path == NULL)
-        panic("KERNEL_PATH not specified");
+        panic("stivale: KERNEL_PATH not specified");
 
     print("stivale: Loading kernel `%s`...\n", kernel_path);
 
     if (!uri_open(kernel_file, kernel_path))
-        panic("Could not open kernel resource");
+        panic("stivale: Could not open kernel resource");
 
     struct stivale_header stivale_hdr;
 
@@ -153,7 +153,7 @@ void stivale_load(char *config, char *cmdline) {
 
         struct file_handle f;
         if (!uri_open(&f, module_path))
-            panic("Requested module with path \"%s\" not found!", module_path);
+            panic("stivale: Requested module with path \"%s\" not found!", module_path);
 
         m->begin = REPORTED_ADDR((uint64_t)(size_t)freadall(&f, STIVALE_MMAP_KERNEL_AND_MODULES));
         m->end   = m->begin + f.size;
diff --git a/stage23/protos/stivale2.c b/stage23/protos/stivale2.c
index 62b524a5..3ffda050 100644
--- a/stage23/protos/stivale2.c
+++ b/stage23/protos/stivale2.c
@@ -63,12 +63,12 @@ void stivale2_load(char *config, char *cmdline, bool pxe, void *efi_system_table
 
     char *kernel_path = config_get_value(config, 0, "KERNEL_PATH");
     if (kernel_path == NULL)
-        panic("KERNEL_PATH not specified");
+        panic("stivale2: KERNEL_PATH not specified");
 
     print("stivale2: Loading kernel `%s`...\n", kernel_path);
 
     if (!uri_open(kernel_file, kernel_path))
-        panic("Could not open kernel resource");
+        panic("stivale2: Could not open kernel resource");
 
     struct stivale2_header stivale2_hdr;
 
@@ -228,7 +228,7 @@ void stivale2_load(char *config, char *cmdline, bool pxe, void *efi_system_table
 
         struct file_handle f;
         if (!uri_open(&f, module_path))
-            panic("Requested module with path \"%s\" not found!", module_path);
+            panic("stivale2: Requested module with path \"%s\" not found!", module_path);
 
         m->begin = REPORTED_ADDR((uint64_t)(size_t)freadall(&f, STIVALE2_MMAP_KERNEL_AND_MODULES));
         m->end   = m->begin + f.size;
diff --git a/stage23/sys/smp_trampoline.real b/stage23/sys/smp_trampoline.real
index 482dd684..81a6fc8c 100644
--- a/stage23/sys/smp_trampoline.real
+++ b/stage23/sys/smp_trampoline.real
@@ -14,8 +14,7 @@ smp_trampoline:
     lea eax, [ebx + .mode32]
     mov [cs:.farjmp_off], eax
 
-    mov eax, cr0
-    bts eax, 0
+    mov eax, 0x00000011
     mov cr0, eax
     o32 jmp far [cs:.farjmp]
 
@@ -32,10 +31,8 @@ smp_trampoline:
     mov gs, ax
     mov ss, ax
 
-    mov eax, cr0
-    btr eax, 29
-    btr eax, 30
-    mov cr0, eax
+    xor eax, eax
+    mov cr4, eax
 
     test dword [ebx + passed_info.target_mode], (1 << 2)
     jz .nox2apic
@@ -68,8 +65,8 @@ smp_trampoline:
     mov cr3, eax
 
     mov ecx, 0xc0000080
-    rdmsr
-    bts eax, 8
+    mov eax, 0x100
+    xor edx, edx
     wrmsr
 
     mov eax, cr0
tab: 248 wrap: offon