2022-10-12 06:44:22 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 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 <mesosphere.hpp>
|
|
|
|
|
|
|
|
namespace ams::kern::svc {
|
|
|
|
|
|
|
|
/* ============================= Common ============================= */
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2023-11-01 17:24:13 +00:00
|
|
|
Result MapInsecurePhysicalMemory(uintptr_t address, size_t size) {
|
2022-10-12 06:44:22 +00:00
|
|
|
/* Validate the address/size. */
|
|
|
|
R_UNLESS(util::IsAligned(size, PageSize), svc::ResultInvalidSize());
|
|
|
|
R_UNLESS(size > 0, svc::ResultInvalidSize());
|
|
|
|
R_UNLESS(util::IsAligned(address, PageSize), svc::ResultInvalidAddress());
|
|
|
|
R_UNLESS((address < address + size), svc::ResultInvalidCurrentMemory());
|
|
|
|
|
|
|
|
/* Verify that the mapping is in range. */
|
|
|
|
auto &pt = GetCurrentProcess().GetPageTable();
|
|
|
|
R_UNLESS(GetCurrentProcess().GetPageTable().CanContain(address, size, KMemoryState_Insecure), svc::ResultInvalidMemoryRegion());
|
|
|
|
|
|
|
|
/* Map the insecure memory. */
|
2023-11-01 17:24:13 +00:00
|
|
|
R_RETURN(pt.MapInsecurePhysicalMemory(address, size));
|
2022-10-12 06:44:22 +00:00
|
|
|
}
|
|
|
|
|
2023-11-01 17:24:13 +00:00
|
|
|
Result UnmapInsecurePhysicalMemory(uintptr_t address, size_t size) {
|
2022-10-12 06:44:22 +00:00
|
|
|
/* Validate the address/size. */
|
|
|
|
R_UNLESS(util::IsAligned(size, PageSize), svc::ResultInvalidSize());
|
|
|
|
R_UNLESS(size > 0, svc::ResultInvalidSize());
|
|
|
|
R_UNLESS(util::IsAligned(address, PageSize), svc::ResultInvalidAddress());
|
|
|
|
R_UNLESS((address < address + size), svc::ResultInvalidCurrentMemory());
|
|
|
|
|
|
|
|
/* Verify that the mapping is in range. */
|
|
|
|
auto &pt = GetCurrentProcess().GetPageTable();
|
|
|
|
R_UNLESS(GetCurrentProcess().GetPageTable().CanContain(address, size, KMemoryState_Insecure), svc::ResultInvalidMemoryRegion());
|
|
|
|
|
|
|
|
/* Map the insecure memory. */
|
2023-11-01 17:24:13 +00:00
|
|
|
R_RETURN(pt.UnmapInsecurePhysicalMemory(address, size));
|
2022-10-12 06:44:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ============================= 64 ABI ============================= */
|
|
|
|
|
2023-11-01 17:24:13 +00:00
|
|
|
Result MapInsecurePhysicalMemory64(ams::svc::Address address, ams::svc::Size size) {
|
|
|
|
R_RETURN(MapInsecurePhysicalMemory(address, size));
|
2022-10-12 06:44:22 +00:00
|
|
|
}
|
|
|
|
|
2023-11-01 17:24:13 +00:00
|
|
|
Result UnmapInsecurePhysicalMemory64(ams::svc::Address address, ams::svc::Size size) {
|
|
|
|
R_RETURN(UnmapInsecurePhysicalMemory(address, size));
|
2022-10-12 06:44:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ============================= 64From32 ABI ============================= */
|
|
|
|
|
2023-11-01 17:24:13 +00:00
|
|
|
Result MapInsecurePhysicalMemory64From32(ams::svc::Address address, ams::svc::Size size) {
|
|
|
|
R_RETURN(MapInsecurePhysicalMemory(address, size));
|
2022-10-12 06:44:22 +00:00
|
|
|
}
|
|
|
|
|
2023-11-01 17:24:13 +00:00
|
|
|
Result UnmapInsecurePhysicalMemory64From32(ams::svc::Address address, ams::svc::Size size) {
|
|
|
|
R_RETURN(UnmapInsecurePhysicalMemory(address, size));
|
2022-10-12 06:44:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|