From d2030b602dfb82286da056cfee867abc6ac001eb Mon Sep 17 00:00:00 2001 From: Christopher Pelloux Date: Sun, 17 Oct 2021 22:54:52 -0400 Subject: [PATCH] Loader: Build the arch target defined by CMake This patch fixes an issue that could arise if the following conditions are met: - set HYPERVISOR_TARGET_ARCH to Intel in CMake - build the loader for Linux on an AMD machine - On AMD, `make driver_quick`, then `make start` -> computer hangs This occurs because the loader is built using the architecture of the build machine while the hypervisor is built using the target arch set during cmake configuration which can be different. The cpu checks are done inside the Linux driver which will succeed and load the hypervisor which is using Intel specific instructions causing the machine to hang when it tries to start on AMD. We now use the cmake HYPERVISOR_TARGET_ARCH variable in Makefile which will keep the hypervisor and the Linux driver in sync. --- .gitignore | 1 + cmake/target/loader_build.cmake | 6 ++++++ loader/linux/{Makefile => Makefile.in} | 2 +- loader/src/x64/amd/check_cpu_configuration.c | 6 +++--- loader/src/x64/intel/check_cpu_configuration.c | 6 +++--- 5 files changed, 14 insertions(+), 7 deletions(-) rename loader/linux/{Makefile => Makefile.in} (99%) diff --git a/.gitignore b/.gitignore index 789fe084..bc8218e7 100644 --- a/.gitignore +++ b/.gitignore @@ -78,6 +78,7 @@ loader/**/*.o loader/**/*.o.cmd loader/**/*.cmd loader/**/*.dwo +loader/linux/Makefile # Windows Loader loader/windows/x64/ diff --git a/cmake/target/loader_build.cmake b/cmake/target/loader_build.cmake index fbe1540b..6069d655 100644 --- a/cmake/target/loader_build.cmake +++ b/cmake/target/loader_build.cmake @@ -20,6 +20,12 @@ # SOFTWARE. if(HYPERVISOR_BUILD_LOADER AND NOT HYPERVISOR_TARGET_ARCH STREQUAL "aarch64") + configure_file( + ${hypervisor_SOURCE_DIR}/loader/linux/Makefile.in + ${hypervisor_SOURCE_DIR}/loader/linux/Makefile + @ONLY + ) + if(CMAKE_SYSTEM_NAME STREQUAL "Linux") add_custom_target(loader_build COMMAND ${CMAKE_COMMAND} -E chdir ${hypervisor_SOURCE_DIR}/loader/linux make CMAKE_BINARY_DIR='${CMAKE_BINARY_DIR}' diff --git a/loader/linux/Makefile b/loader/linux/Makefile.in similarity index 99% rename from loader/linux/Makefile rename to loader/linux/Makefile.in index 2e93c951..19b7b91b 100644 --- a/loader/linux/Makefile +++ b/loader/linux/Makefile.in @@ -21,7 +21,7 @@ # SOFTWARE. TARGET_MODULE := bareflank_loader -VENDOR_ID := $(shell lscpu | grep 'Vendor ID') +VENDOR_ID := @HYPERVISOR_TARGET_ARCH@ ifneq ($(KERNELRELEASE),) obj-m := $(TARGET_MODULE).o diff --git a/loader/src/x64/amd/check_cpu_configuration.c b/loader/src/x64/amd/check_cpu_configuration.c index ce1ab848..7e7a765d 100644 --- a/loader/src/x64/amd/check_cpu_configuration.c +++ b/loader/src/x64/amd/check_cpu_configuration.c @@ -84,17 +84,17 @@ check_for_amd(void) NOEXCEPT intrinsic_cpuid(&eax, &ebx, &ecx, &edx); if (CPUID_VENDOR_EBX != ebx) { - bferror_x32("cpu is vendor is not AuthenticAMD", ebx); + bferror_x32("cpu vendor is not AuthenticAMD", ebx); return LOADER_FAILURE; } if (CPUID_VENDOR_ECX != ecx) { - bferror_x32("cpu is vendor is not AuthenticAMD", ecx); + bferror_x32("cpu vendor is not AuthenticAMD", ecx); return LOADER_FAILURE; } if (CPUID_VENDOR_EDX != edx) { - bferror_x32("cpu is vendor is not AuthenticAMD", edx); + bferror_x32("cpu vendor is not AuthenticAMD", edx); return LOADER_FAILURE; } diff --git a/loader/src/x64/intel/check_cpu_configuration.c b/loader/src/x64/intel/check_cpu_configuration.c index 1039868b..75914f15 100644 --- a/loader/src/x64/intel/check_cpu_configuration.c +++ b/loader/src/x64/intel/check_cpu_configuration.c @@ -106,17 +106,17 @@ check_for_intel(void) NOEXCEPT intrinsic_cpuid(&eax, &ebx, &ecx, &edx); if (CPUID_VENDOR_EBX != ebx) { - bferror_x32("cpu is vendor is not GenuineIntel", ebx); + bferror_x32("cpu vendor is not GenuineIntel", ebx); return LOADER_FAILURE; } if (CPUID_VENDOR_ECX != ecx) { - bferror_x32("cpu is vendor is not GenuineIntel", ecx); + bferror_x32("cpu vendor is not GenuineIntel", ecx); return LOADER_FAILURE; } if (CPUID_VENDOR_EDX != edx) { - bferror_x32("cpu is vendor is not GenuineIntel", edx); + bferror_x32("cpu vendor is not GenuineIntel", edx); return LOADER_FAILURE; }