exo2: implement the atmosphere extension mappers
This commit is contained in:
80
exosphere2/program/source/smc/secmon_page_mapper.cpp
Normal file
80
exosphere2/program/source/smc/secmon_page_mapper.cpp
Normal file
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
* Copyright (c) 2018-2020 Atmosphère-NX
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
* under the terms and conditions of the GNU General Public License,
|
||||
* version 2, as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include <exosphere.hpp>
|
||||
#include "../secmon_map.hpp"
|
||||
#include "secmon_page_mapper.hpp"
|
||||
|
||||
namespace ams::secmon::smc {
|
||||
|
||||
namespace impl {
|
||||
|
||||
void *PageMapperImpl::GetPointerTo(uintptr_t phys, size_t size) const {
|
||||
/* Ensure we stay within the page. */
|
||||
if (util::AlignDown(phys, 4_KB) != this->physical_address) {
|
||||
return nullptr;
|
||||
}
|
||||
if (size != 0) {
|
||||
if (util::AlignDown(phys + size - 1, 4_KB) != this->physical_address) {
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
return reinterpret_cast<void *>(phys + (this->virtual_address - this->physical_address));
|
||||
}
|
||||
|
||||
bool PageMapperImpl::CopyToUser(uintptr_t dst_phys, const void *src, size_t size) const {
|
||||
void * const dst = this->GetPointerTo(dst_phys, size);
|
||||
if (dst == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
std::memcpy(dst, src, size);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool PageMapperImpl::CopyFromUser(void *dst, uintptr_t src_phys, size_t size) const {
|
||||
const void * const src = this->GetPointerTo(src_phys, size);
|
||||
if (src == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
std::memcpy(dst, src, size);
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
bool UserPageMapper::Map() {
|
||||
return this->MapImpl<MapSmcUserPage>();
|
||||
}
|
||||
|
||||
bool AtmosphereIramPageMapper::Map() {
|
||||
return this->MapImpl<MapAtmosphereIramPage>();
|
||||
}
|
||||
|
||||
bool AtmosphereUserPageMapper::Map() {
|
||||
return this->MapImpl<MapAtmosphereUserPage>();
|
||||
}
|
||||
|
||||
AtmosphereIramPageMapper::~AtmosphereIramPageMapper() {
|
||||
this->UnmapImpl<UnmapAtmosphereIramPage>();
|
||||
}
|
||||
|
||||
AtmosphereUserPageMapper::~AtmosphereUserPageMapper() {
|
||||
this->UnmapImpl<UnmapAtmosphereUserPage>();
|
||||
}
|
||||
|
||||
}
|
||||
73
exosphere2/program/source/smc/secmon_page_mapper.hpp
Normal file
73
exosphere2/program/source/smc/secmon_page_mapper.hpp
Normal file
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
* Copyright (c) 2018-2020 Atmosphère-NX
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
* under the terms and conditions of the GNU General Public License,
|
||||
* version 2, as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#pragma once
|
||||
#include <exosphere.hpp>
|
||||
#include "secmon_smc_common.hpp"
|
||||
|
||||
namespace ams::secmon::smc {
|
||||
|
||||
namespace impl {
|
||||
|
||||
class PageMapperImpl {
|
||||
private:
|
||||
uintptr_t physical_address;
|
||||
uintptr_t virtual_address;
|
||||
public:
|
||||
constexpr PageMapperImpl(uintptr_t phys) : physical_address(util::AlignDown(phys, 4_KB)), virtual_address() { /* ... */ }
|
||||
|
||||
void *GetPointerTo(uintptr_t phys, size_t size) const;
|
||||
bool CopyToUser(uintptr_t dst_phys, const void *src, size_t size) const;
|
||||
bool CopyFromUser(void *dst, uintptr_t src_phys, size_t size) const;
|
||||
|
||||
template<auto F>
|
||||
bool MapImpl() {
|
||||
this->virtual_address = F(this->physical_address);
|
||||
return this->virtual_address != 0;
|
||||
}
|
||||
|
||||
template<auto F>
|
||||
void UnmapImpl() {
|
||||
F();
|
||||
this->virtual_address = 0;
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
class UserPageMapper : public impl::PageMapperImpl {
|
||||
public:
|
||||
constexpr UserPageMapper(uintptr_t phys) : PageMapperImpl(phys) { /* ... */ }
|
||||
|
||||
bool Map();
|
||||
};
|
||||
|
||||
class AtmosphereIramPageMapper : public impl::PageMapperImpl {
|
||||
public:
|
||||
constexpr AtmosphereIramPageMapper(uintptr_t phys) : PageMapperImpl(phys) { /* ... */ }
|
||||
~AtmosphereIramPageMapper();
|
||||
|
||||
bool Map();
|
||||
};
|
||||
|
||||
class AtmosphereUserPageMapper : public impl::PageMapperImpl {
|
||||
public:
|
||||
constexpr AtmosphereUserPageMapper(uintptr_t phys) : PageMapperImpl(phys) { /* ... */ }
|
||||
~AtmosphereUserPageMapper();
|
||||
|
||||
bool Map();
|
||||
};
|
||||
|
||||
}
|
||||
@@ -20,7 +20,7 @@
|
||||
#include "secmon_smc_aes.hpp"
|
||||
#include "secmon_smc_device_unique_data.hpp"
|
||||
#include "secmon_smc_se_lock.hpp"
|
||||
#include "secmon_user_page_mapper.hpp"
|
||||
#include "secmon_page_mapper.hpp"
|
||||
|
||||
namespace ams::secmon::smc {
|
||||
|
||||
|
||||
@@ -97,12 +97,6 @@ namespace ams::secmon::smc {
|
||||
return pkg1::MemoryMode_Auto;
|
||||
}
|
||||
|
||||
pkg1::MemorySize GetPhysicalMemorySize() {
|
||||
const auto dram_id = fuse::GetDramId();
|
||||
AMS_ABORT_UNLESS(dram_id < fuse::DramId_Count);
|
||||
return DramIdToMemorySize[dram_id];
|
||||
}
|
||||
|
||||
pkg1::MemorySize GetAvailableMemorySize(pkg1::MemorySize size) {
|
||||
return std::min(GetPhysicalMemorySize(), size);
|
||||
}
|
||||
@@ -294,4 +288,11 @@ namespace ams::secmon::smc {
|
||||
return SmcResult::NotImplemented;
|
||||
}
|
||||
|
||||
/* For exosphere's usage. */
|
||||
pkg1::MemorySize GetPhysicalMemorySize() {
|
||||
const auto dram_id = fuse::GetDramId();
|
||||
AMS_ABORT_UNLESS(dram_id < fuse::DramId_Count);
|
||||
return DramIdToMemorySize[dram_id];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -56,4 +56,7 @@ namespace ams::secmon::smc {
|
||||
/* This is an atmosphere extension smc. */
|
||||
SmcResult SmcGetEmummcConfig(SmcArguments &args);
|
||||
|
||||
/* For other parts of exosphere. */
|
||||
pkg1::MemorySize GetPhysicalMemorySize();
|
||||
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
#include <exosphere.hpp>
|
||||
#include "../secmon_error.hpp"
|
||||
#include "secmon_smc_result.hpp"
|
||||
#include "secmon_user_page_mapper.hpp"
|
||||
#include "secmon_page_mapper.hpp"
|
||||
|
||||
namespace ams::secmon::smc {
|
||||
|
||||
|
||||
@@ -1,61 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2018-2020 Atmosphère-NX
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
* under the terms and conditions of the GNU General Public License,
|
||||
* version 2, as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include <exosphere.hpp>
|
||||
#include "../secmon_map.hpp"
|
||||
#include "secmon_user_page_mapper.hpp"
|
||||
|
||||
namespace ams::secmon::smc {
|
||||
|
||||
bool UserPageMapper::Map() {
|
||||
this->virtual_address = MapSmcUserPage(this->physical_address);
|
||||
return this->virtual_address != 0;
|
||||
}
|
||||
|
||||
void *UserPageMapper::GetPointerTo(uintptr_t phys, size_t size) const {
|
||||
/* Ensure we stay within the page. */
|
||||
if (util::AlignDown(phys, 4_KB) != this->physical_address) {
|
||||
return nullptr;
|
||||
}
|
||||
if (size != 0) {
|
||||
if (util::AlignDown(phys + size - 1, 4_KB) != this->physical_address) {
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
return reinterpret_cast<void *>(phys + (this->virtual_address - this->physical_address));
|
||||
}
|
||||
|
||||
bool UserPageMapper::CopyToUser(uintptr_t dst_phys, const void *src, size_t size) const {
|
||||
void * const dst = this->GetPointerTo(dst_phys, size);
|
||||
if (dst == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
std::memcpy(dst, src, size);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool UserPageMapper::CopyFromUser(void *dst, uintptr_t src_phys, size_t size) const {
|
||||
const void * const src = this->GetPointerTo(src_phys, size);
|
||||
if (src == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
std::memcpy(dst, src, size);
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2018-2020 Atmosphère-NX
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
* under the terms and conditions of the GNU General Public License,
|
||||
* version 2, as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#pragma once
|
||||
#include <exosphere.hpp>
|
||||
#include "secmon_smc_common.hpp"
|
||||
|
||||
namespace ams::secmon::smc {
|
||||
|
||||
class UserPageMapper {
|
||||
private:
|
||||
uintptr_t physical_address;
|
||||
uintptr_t virtual_address;
|
||||
public:
|
||||
constexpr UserPageMapper(uintptr_t phys) : physical_address(util::AlignDown(phys, 4_KB)), virtual_address() { /* ... */ }
|
||||
|
||||
bool Map();
|
||||
void *GetPointerTo(uintptr_t phys, size_t size) const;
|
||||
bool CopyToUser(uintptr_t dst_phys, const void *src, size_t size) const;
|
||||
bool CopyFromUser(void *dst, uintptr_t src_phys, size_t size) const;
|
||||
};
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user