Skip to content

Commit aea5026

Browse files
committed
Save and restore GDT in UEFI loader.
1 parent a4e3176 commit aea5026

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

uefi_loader/src/main.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,14 @@ extern "C" {
1010
#include <Protocol/LoadedImage.h>
1111
#include <Protocol/MpService.h>
1212
}
13+
#include "zpp/scope_guard.h"
14+
#include "zpp/x64/asm.h"
1315
#include <cstddef>
1416
#include <cstdint>
1517
#include <string>
1618

19+
namespace zpp
20+
{
1721
/**
1822
* The boot services.
1923
*/
@@ -112,6 +116,11 @@ static int call_on_cpu(std::size_t cpuid,
112116
{
113117
int result = -1;
114118

119+
// Save this GDT and restore it at function exit.
120+
alignas(0x10) unsigned char gdt_layout[0x10]{};
121+
x64::sgdt(gdt_layout);
122+
scope_guard restore_gdt = [&] { x64::lgdt(gdt_layout); };
123+
115124
// If this is the main CPU, just call the user function.
116125
if (0 == cpuid) {
117126
return function(context);
@@ -383,3 +392,5 @@ extern "C" EFI_STATUS EFIAPI uefi_main(EFI_HANDLE image_handle,
383392
// Return success anyway, no image was found is considered ok.
384393
return EFI_SUCCESS;
385394
}
395+
396+
} // namespace zpp

uefi_loader/uefi_loader.vcxproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,30 +77,30 @@
7777
<NMakeCleanCommandLine>C:\Windows\sysnative\bash -c "make clean"</NMakeCleanCommandLine>
7878
<NMakeReBuildCommandLine>C:\Windows\sysnative\bash -c "make clean &amp;&amp; make"</NMakeReBuildCommandLine>
7979
<NMakePreprocessorDefinitions>WIN32;_DEBUG;$(NMakePreprocessorDefinitions)</NMakePreprocessorDefinitions>
80-
<NMakeIncludeSearchPath>include;../loader/include;C:/Temp/edk2-UDK2018/MdePkg/Include</NMakeIncludeSearchPath>
80+
<NMakeIncludeSearchPath>include;../hypervisor/include;C:/Temp/edk2-UDK2018/MdePkg/Include</NMakeIncludeSearchPath>
8181
</PropertyGroup>
8282
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
8383
<NMakeBuildCommandLine>C:\Windows\sysnative\bash -c make</NMakeBuildCommandLine>
8484
<NMakeCleanCommandLine>C:\Windows\sysnative\bash -c "make clean"</NMakeCleanCommandLine>
8585
<NMakeReBuildCommandLine>C:\Windows\sysnative\bash -c "make clean &amp;&amp; make"</NMakeReBuildCommandLine>
8686
<NMakePreprocessorDefinitions>_DEBUG;$(NMakePreprocessorDefinitions)</NMakePreprocessorDefinitions>
87-
<NMakeIncludeSearchPath>include;../loader/include;C:/Temp/edk2-UDK2018/MdePkg/Include</NMakeIncludeSearchPath>
87+
<NMakeIncludeSearchPath>include;../hypervisor/include;C:/Temp/edk2-UDK2018/MdePkg/Include</NMakeIncludeSearchPath>
8888
</PropertyGroup>
8989
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
9090
<NMakeBuildCommandLine>C:\Windows\sysnative\bash -c "make mode=release"</NMakeBuildCommandLine>
9191
<NMakeOutput>hypervisor.exe</NMakeOutput>
9292
<NMakeCleanCommandLine>C:\Windows\sysnative\bash -c "make clean"</NMakeCleanCommandLine>
9393
<NMakeReBuildCommandLine>C:\Windows\sysnative\bash -c "make clean &amp;&amp; make mode=release"</NMakeReBuildCommandLine>
9494
<NMakePreprocessorDefinitions>WIN32;NDEBUG;$(NMakePreprocessorDefinitions)</NMakePreprocessorDefinitions>
95-
<NMakeIncludeSearchPath>include;../loader/include;C:/Temp/edk2-UDK2018/MdePkg/Include</NMakeIncludeSearchPath>
95+
<NMakeIncludeSearchPath>include;../hypervisor/include;C:/Temp/edk2-UDK2018/MdePkg/Include</NMakeIncludeSearchPath>
9696
</PropertyGroup>
9797
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
9898
<NMakeBuildCommandLine>C:\Windows\sysnative\bash -c "make mode=release"</NMakeBuildCommandLine>
9999
<NMakeOutput>hypervisor.exe</NMakeOutput>
100100
<NMakeCleanCommandLine>C:\Windows\sysnative\bash -c "make clean"</NMakeCleanCommandLine>
101101
<NMakeReBuildCommandLine>C:\Windows\sysnative\bash -c "make clean &amp;&amp; make mode=release"</NMakeReBuildCommandLine>
102102
<NMakePreprocessorDefinitions>NDEBUG;$(NMakePreprocessorDefinitions)</NMakePreprocessorDefinitions>
103-
<NMakeIncludeSearchPath>include;../loader/include;C:/Temp/edk2-UDK2018/MdePkg/Include</NMakeIncludeSearchPath>
103+
<NMakeIncludeSearchPath>include;../hypervisor/include;C:/Temp/edk2-UDK2018/MdePkg/Include</NMakeIncludeSearchPath>
104104
</PropertyGroup>
105105
<ItemDefinitionGroup>
106106
</ItemDefinitionGroup>

0 commit comments

Comments
 (0)