From b917ea283e764c34af6a893591b87685180eca77 Mon Sep 17 00:00:00 2001 From: fincs Date: Mon, 10 Aug 2020 21:42:19 +0200 Subject: [PATCH] kern: make GetTargetFirmware a compile-time constant when not building for Switch --- .../include/mesosphere/kern_common.hpp | 6 ++++++ .../kernel/source/arch/arm64/init/start.s | 4 ++++ .../kernel_ldr/source/arch/arm64/start.s | 5 ++++- .../kernel_ldr/source/kern_init_loader.cpp | 20 +++++++++++++++++-- 4 files changed, 32 insertions(+), 3 deletions(-) diff --git a/libraries/libmesosphere/include/mesosphere/kern_common.hpp b/libraries/libmesosphere/include/mesosphere/kern_common.hpp index be7c94ab8..091d4ba68 100644 --- a/libraries/libmesosphere/include/mesosphere/kern_common.hpp +++ b/libraries/libmesosphere/include/mesosphere/kern_common.hpp @@ -22,6 +22,12 @@ namespace ams::kern { constexpr size_t PageSize = 4_KB; +#ifdef ATMOSPHERE_BOARD_NINTENDO_NX ams::TargetFirmware GetTargetFirmware(); +#else + consteval ALWAYS_INLINE ams::TargetFirmware GetTargetFirmware() { + return ams::TargetFirmware_Current; + } +#endif } diff --git a/mesosphere/kernel/source/arch/arm64/init/start.s b/mesosphere/kernel/source/arch/arm64/init/start.s index f4a6d1bde..0549679b8 100644 --- a/mesosphere/kernel/source/arch/arm64/init/start.s +++ b/mesosphere/kernel/source/arch/arm64/init/start.s @@ -61,12 +61,14 @@ __metadata_kernel_layout: .error "Incorrect Mesosphere Metadata" .endif +#ifdef ATMOSPHERE_BOARD_NINTENDO_NX .global _ZN3ams4kern17GetTargetFirmwareEv .type _ZN3ams4kern17GetTargetFirmwareEv, %function _ZN3ams4kern17GetTargetFirmwareEv: adr x0, __metadata_target_firmware ldr w0, [x0] ret +#endif /* ams::kern::init::StartCore0(uintptr_t, uintptr_t) */ .section .crt0.text._ZN3ams4kern4init10StartCore0Emm, "ax", %progbits @@ -94,6 +96,7 @@ core0_el2: core0_el1: bl _ZN3ams4kern4init19DisableMmuAndCachesEv +#ifdef ATMOSPHERE_BOARD_NINTENDO_NX /* Get the target firmware from exosphere. */ LOAD_IMMEDIATE_32(w0, 0xC3000004) mov w1, #65000 @@ -105,6 +108,7 @@ core0_el1: /* Store the target firmware. */ adr x0, __metadata_target_firmware str w1, [x0] +#endif /* We want to invoke kernel loader. */ adr x0, _start diff --git a/mesosphere/kernel_ldr/source/arch/arm64/start.s b/mesosphere/kernel_ldr/source/arch/arm64/start.s index 29efffa5c..f8610ae72 100644 --- a/mesosphere/kernel_ldr/source/arch/arm64/start.s +++ b/mesosphere/kernel_ldr/source/arch/arm64/start.s @@ -56,6 +56,7 @@ _main: stp x2, x30, [sp, #0x10] stp xzr, xzr, [sp, #0x20] +#ifdef ATMOSPHERE_BOARD_NINTENDO_NX /* Get the target firmware from exosphere. */ LOAD_IMMEDIATE_32(w0, 0xC3000004) mov w1, #65000 @@ -67,6 +68,7 @@ _main: /* Store the target firmware. */ adr x0, __metadata_target_firmware str w1, [x0] +#endif /* Apply relocations and call init array for KernelLdr. */ adr x0, _start @@ -121,13 +123,14 @@ _main: mov sp, x2 br x1 - +#ifdef ATMOSPHERE_BOARD_NINTENDO_NX .global _ZN3ams4kern17GetTargetFirmwareEv .type _ZN3ams4kern17GetTargetFirmwareEv, %function _ZN3ams4kern17GetTargetFirmwareEv: adr x0, __metadata_target_firmware ldr w0, [x0] ret +#endif .balign 8 __external_references: diff --git a/mesosphere/kernel_ldr/source/kern_init_loader.cpp b/mesosphere/kernel_ldr/source/kern_init_loader.cpp index 658b2a39e..fb25f4890 100644 --- a/mesosphere/kernel_ldr/source/kern_init_loader.cpp +++ b/mesosphere/kernel_ldr/source/kern_init_loader.cpp @@ -87,6 +87,22 @@ namespace ams::kern::init::loader { cpu::InvalidateEntireTlb(); } + #ifdef ATMOSPHERE_BOARD_NINTENDO_NX + + ALWAYS_INLINE bool ShouldPerformCpuSpecificSetup() { + /* Perform cpu-specific setup only on < 10.0.0. */ + return kern::GetTargetFirmware() < ams::TargetFirmware_10_0_0; + } + + #else + + consteval ALWAYS_INLINE bool ShouldPerformCpuSpecificSetup() { + /* Always perform cpu-specific setup. */ + return true; + } + + #endif + void SetupInitialIdentityMapping(KInitialPageTable &ttbr1_table, uintptr_t base_address, uintptr_t kernel_size, uintptr_t page_table_region, size_t page_table_region_size, KInitialPageTable::IPageAllocator &allocator) { /* Make a new page table for TTBR0_EL1. */ KInitialPageTable ttbr0_table(allocator.Allocate()); @@ -116,8 +132,8 @@ namespace ams::kern::init::loader { cpu::MemoryAccessIndirectionRegisterAccessor(MairValue).Store(); cpu::TranslationControlRegisterAccessor(TcrValue).Store(); - /* Perform cpu-specific setup on < 10.0.0. */ - if (kern::GetTargetFirmware() < ams::TargetFirmware_10_0_0) { + /* Perform cpu-specific setup if needed. */ + if (ShouldPerformCpuSpecificSetup()) { SavedRegisterState saved_registers; SaveRegistersToTpidrEl1(&saved_registers); ON_SCOPE_EXIT { VerifyAndClearTpidrEl1(&saved_registers); };