From ce0bcbd3b623f5add3e2fb4dea913d660ff13531 Mon Sep 17 00:00:00 2001 From: "Justin C. Miller" Date: Sat, 23 May 2020 12:35:59 -0700 Subject: [PATCH] [boot] Set up CR4 in bootloader Moving the initial CR4 settings from the kernel's `memory_initialize` (where it doesn't really fit anyway) to the bootloader's `hardware.cpp`. --- src/boot/hardware.cpp | 13 +++++++++++++ src/boot/hardware.h | 3 +++ src/boot/main.cpp | 1 + 3 files changed, 17 insertions(+) diff --git a/src/boot/hardware.cpp b/src/boot/hardware.cpp index 73ff470..fb3eca7 100644 --- a/src/boot/hardware.cpp +++ b/src/boot/hardware.cpp @@ -36,6 +36,19 @@ find_acpi_table(uefi::system_table *st) return reinterpret_cast(acpi1_table); } +void +setup_cr4() +{ + uint64_t cr4 = 0; + asm volatile ( "mov %%cr4, %0" : "=r" (cr4) ); + cr4 |= + 0x000080 | // Enable global pages + 0x000200 | // Enable FXSAVE/FXRSTOR + 0x010000 | // Enable FSGSBASE + 0x020000 | // Enable PCIDs + 0; + asm volatile ( "mov %0, %%cr4" :: "r" (cr4) ); +} } // namespace hw } // namespace boot diff --git a/src/boot/hardware.h b/src/boot/hardware.h index d148403..a37c903 100644 --- a/src/boot/hardware.h +++ b/src/boot/hardware.h @@ -13,5 +13,8 @@ namespace hw { /// significant bit set to 1. void * find_acpi_table(uefi::system_table *st); +/// Enable CPU options in CR4 for the kernel starting state. +void setup_cr4(); + } // namespace hw } // namespace boot diff --git a/src/boot/main.cpp b/src/boot/main.cpp index 550b60b..7a96fda 100644 --- a/src/boot/main.cpp +++ b/src/boot/main.cpp @@ -197,6 +197,7 @@ efi_main(uefi::handle image_handle, uefi::system_table *st) L"Failed to exit boot services"); memory::virtualize(args->pml4, map, st->runtime_services); + hw::setup_cr4(); kentry(args); debug_break();