exo2: implement the atmosphere extension mappers

This commit is contained in:
Michael Scire 2020-05-20 00:34:01 -07:00 committed by SciresM
parent 1e0124fb67
commit 36754e2c38
12 changed files with 370 additions and 102 deletions

View file

@ -16,7 +16,9 @@
#include <exosphere.hpp>
#include "secmon_cache.hpp"
#include "secmon_setup.hpp"
#include "secmon_spinlock.hpp"
#include "secmon_map.hpp"
#include "smc/secmon_smc_info.hpp"
namespace ams::secmon {
@ -26,10 +28,16 @@ namespace ams::secmon {
constexpr inline const size_t BootCodeSize = MemoryRegionVirtualTzramBootCode.GetSize();
constinit uintptr_t g_smc_user_page_physical_address = 0;
constinit uintptr_t g_ams_iram_page_physical_address = 0;
constinit uintptr_t g_ams_user_page_physical_address = 0;
constinit SpinLockType g_ams_iram_page_spin_lock = {};
constinit SpinLockType g_ams_user_page_spin_lock = {};
using namespace ams::mmu;
constexpr inline PageTableMappingAttribute MappingAttributesEl3NonSecureRwData = AddMappingAttributeIndex(PageTableMappingAttributes_El3NonSecureRwData, MemoryAttributeIndexNormal);
constexpr inline PageTableMappingAttribute MappingAttributesEl3NonSecureDevice = AddMappingAttributeIndex(PageTableMappingAttributes_El3NonSecureRwData, MemoryAttributeIndexDevice);
constexpr void UnmapBootCodeImpl(u64 *l1, u64 *l2, u64 *l3, uintptr_t boot_code, size_t boot_code_size) {
/* Unmap the L3 entries corresponding to the boot code. */
@ -57,6 +65,26 @@ namespace ams::secmon {
InvalidateL3Entries(l3, MemoryRegionVirtualSmcUserPage.GetAddress(), MemoryRegionVirtualSmcUserPage.GetSize());
}
constexpr void MapAtmosphereIramPageImpl(u64 *l3, uintptr_t address) {
/* Set the L3 entry. */
SetL3BlockEntry(l3, MemoryRegionVirtualAtmosphereIramPage.GetAddress(), address, MemoryRegionVirtualAtmosphereIramPage.GetSize(), MappingAttributesEl3NonSecureDevice);
}
constexpr void UnmapAtmosphereIramPageImpl(u64 *l3) {
/* Unmap the L3 entry. */
InvalidateL3Entries(l3, MemoryRegionVirtualAtmosphereIramPage.GetAddress(), MemoryRegionVirtualAtmosphereIramPage.GetSize());
}
constexpr void MapAtmosphereUserPageImpl(u64 *l3, uintptr_t address) {
/* Set the L3 entry. */
SetL3BlockEntry(l3, MemoryRegionVirtualAtmosphereUserPage.GetAddress(), address, MemoryRegionVirtualAtmosphereUserPage.GetSize(), MappingAttributesEl3NonSecureRwData);
}
constexpr void UnmapAtmosphereUserPageImpl(u64 *l3) {
/* Unmap the L3 entry. */
InvalidateL3Entries(l3, MemoryRegionVirtualAtmosphereUserPage.GetAddress(), MemoryRegionVirtualAtmosphereUserPage.GetSize());
}
void ClearLow(uintptr_t address, size_t size) {
/* Clear the low part. */
util::ClearMemory(reinterpret_cast<void *>(address), size / 2);
@ -67,6 +95,10 @@ namespace ams::secmon {
util::ClearMemory(reinterpret_cast<void *>(address + size / 2), size / 2);
}
bool IsPhysicalMemoryAddress(uintptr_t address) {
return (address - MemoryRegionDram.GetAddress()) < GetPhysicalMemorySize();
}
}
void ClearBootCodeHigh() {
@ -88,6 +120,15 @@ namespace ams::secmon {
secmon::EnsureMappingConsistency();
}
size_t GetPhysicalMemorySize() {
switch (smc::GetPhysicalMemorySize()) {
case pkg1::MemorySize_4GB: return 4_GB;
case pkg1::MemorySize_6GB: return 6_GB;
case pkg1::MemorySize_8GB: return 8_GB;
AMS_UNREACHABLE_DEFAULT_CASE();
}
}
void UnmapTzram() {
/* Get the tables. */
u64 * const l1 = MemoryRegionVirtualTzramL1PageTable.GetPointer<u64>();
@ -102,9 +143,10 @@ namespace ams::secmon {
uintptr_t MapSmcUserPage(uintptr_t address) {
if (g_smc_user_page_physical_address == 0) {
if (!(MemoryRegionDram.GetAddress() <= address && address <= MemoryRegionDramHigh.GetEndAddress() - MemoryRegionVirtualSmcUserPage.GetSize())) {
if (!IsPhysicalMemoryAddress(address)) {
return 0;
}
if (!util::IsAligned(address, 4_KB)) {
return 0;
}
@ -139,4 +181,106 @@ namespace ams::secmon {
g_smc_user_page_physical_address = 0;
}
uintptr_t MapAtmosphereIramPage(uintptr_t address) {
/* Acquire the ams iram spinlock. */
AcquireSpinLock(g_ams_iram_page_spin_lock);
auto lock_guard = SCOPE_GUARD { ReleaseSpinLock(g_ams_iram_page_spin_lock); };
/* Validate that the page is an IRAM page. */
if (!MemoryRegionPhysicalIram.Contains(address, 1)) {
return 0;
}
/* Validate that the page is aligned. */
if (!util::IsAligned(address, 4_KB)) {
return 0;
}
/* Map the page. */
g_ams_iram_page_physical_address = address;
u64 * const l2_l3 = MemoryRegionVirtualTzramL2L3PageTable.GetPointer<u64>();
MapAtmosphereIramPageImpl(l2_l3, address);
/* Ensure the mappings are consistent. */
secmon::EnsureMappingConsistency(MemoryRegionVirtualAtmosphereIramPage.GetAddress());
/* Hold the lock. */
lock_guard.Cancel();
return true;
}
void UnmapAtmosphereIramPage() {
/* Can't unmap if nothing's unmapped. */
if (g_ams_iram_page_physical_address == 0) {
return;
}
/* Unmap the page. */
u64 * const l2_l3 = MemoryRegionVirtualTzramL2L3PageTable.GetPointer<u64>();
UnmapAtmosphereIramPageImpl(l2_l3);
/* Ensure the mappings are consistent. */
secmon::EnsureMappingConsistency(MemoryRegionVirtualAtmosphereIramPage.GetAddress());
/* Release the page. */
g_ams_iram_page_physical_address = 0;
ReleaseSpinLock(g_ams_iram_page_spin_lock);
}
uintptr_t MapAtmosphereUserPage(uintptr_t address) {
/* Acquire the ams user spinlock. */
AcquireSpinLock(g_ams_user_page_spin_lock);
auto lock_guard = SCOPE_GUARD { ReleaseSpinLock(g_ams_user_page_spin_lock); };
/* Validate that the page is a dram page. */
if (!IsPhysicalMemoryAddress(address)) {
return 0;
}
/* Validate that the page is aligned. */
if (!util::IsAligned(address, 4_KB)) {
return 0;
}
/* Map the page. */
g_ams_user_page_physical_address = address;
u64 * const l2_l3 = MemoryRegionVirtualTzramL2L3PageTable.GetPointer<u64>();
MapAtmosphereUserPageImpl(l2_l3, address);
/* Ensure the mappings are consistent. */
secmon::EnsureMappingConsistency(MemoryRegionVirtualAtmosphereUserPage.GetAddress());
/* Hold the lock. */
lock_guard.Cancel();
return true;
}
void UnmapAtmosphereUserPage() {
/* Can't unmap if nothing's unmapped. */
if (g_ams_user_page_physical_address == 0) {
return;
}
/* Unmap the page. */
u64 * const l2_l3 = MemoryRegionVirtualTzramL2L3PageTable.GetPointer<u64>();
UnmapAtmosphereUserPageImpl(l2_l3);
/* Ensure the mappings are consistent. */
secmon::EnsureMappingConsistency(MemoryRegionVirtualAtmosphereUserPage.GetAddress());
/* Release the page. */
g_ams_user_page_physical_address = 0;
ReleaseSpinLock(g_ams_user_page_spin_lock);
}
}