2020-02-08 19:53:27 +00:00
|
|
|
/*
|
|
|
|
* 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 <mesosphere.hpp>
|
|
|
|
|
|
|
|
namespace ams::kern::svc {
|
|
|
|
|
|
|
|
/* ============================= Common ============================= */
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2020-07-15 10:07:00 +00:00
|
|
|
Result SetHeapSize(uintptr_t *out_address, size_t size) {
|
|
|
|
/* Validate size. */
|
|
|
|
R_UNLESS(util::IsAligned(size, ams::svc::HeapSizeAlignment), svc::ResultInvalidSize());
|
|
|
|
R_UNLESS(size < ams::kern::MainMemorySize, svc::ResultInvalidSize());
|
|
|
|
|
|
|
|
/* Set the heap size. */
|
|
|
|
KProcessAddress address;
|
|
|
|
R_TRY(GetCurrentProcess().GetPageTable().SetHeapSize(std::addressof(address), size));
|
|
|
|
|
|
|
|
/* Set the output. */
|
|
|
|
*out_address = GetInteger(address);
|
|
|
|
return ResultSuccess();
|
|
|
|
}
|
|
|
|
|
2020-04-26 09:49:59 +00:00
|
|
|
Result SetUnsafeLimit(size_t limit) {
|
|
|
|
/* Ensure the size is aligned. */
|
|
|
|
R_UNLESS(util::IsAligned(limit, PageSize), svc::ResultInvalidSize());
|
2020-02-08 19:53:27 +00:00
|
|
|
|
2020-04-26 09:49:59 +00:00
|
|
|
/* Ensure that the size is not bigger than we can accommodate. */
|
|
|
|
R_UNLESS(limit <= Kernel::GetMemoryManager().GetSize(KMemoryManager::Pool_Unsafe), svc::ResultOutOfRange());
|
|
|
|
|
|
|
|
/* Set the size. */
|
|
|
|
return Kernel::GetUnsafeMemory().SetLimitSize(limit);
|
|
|
|
}
|
2020-02-08 19:53:27 +00:00
|
|
|
|
2020-07-24 15:07:34 +00:00
|
|
|
Result MapPhysicalMemory(uintptr_t address, size_t size) {
|
|
|
|
/* Validate address / size. */
|
|
|
|
R_UNLESS(util::IsAligned(address, PageSize), svc::ResultInvalidAddress());
|
|
|
|
R_UNLESS(util::IsAligned(size, PageSize), svc::ResultInvalidSize());
|
|
|
|
R_UNLESS(size > 0, svc::ResultInvalidSize());
|
|
|
|
R_UNLESS((address < address + size), svc::ResultInvalidMemoryRegion());
|
|
|
|
|
|
|
|
/* Verify that the process has system resource. */
|
|
|
|
auto &process = GetCurrentProcess();
|
|
|
|
R_UNLESS(process.GetTotalSystemResourceSize() > 0, svc::ResultInvalidState());
|
|
|
|
|
|
|
|
/* Verify that the region is in range. */
|
|
|
|
auto &page_table = process.GetPageTable();
|
|
|
|
R_UNLESS(page_table.IsInAliasRegion(address, size), svc::ResultInvalidMemoryRegion());
|
|
|
|
|
|
|
|
/* Map the memory. */
|
|
|
|
R_TRY(page_table.MapPhysicalMemory(address, size));
|
|
|
|
|
|
|
|
return ResultSuccess();
|
|
|
|
}
|
|
|
|
|
|
|
|
Result UnmapPhysicalMemory(uintptr_t address, size_t size) {
|
|
|
|
/* Validate address / size. */
|
|
|
|
R_UNLESS(util::IsAligned(address, PageSize), svc::ResultInvalidAddress());
|
|
|
|
R_UNLESS(util::IsAligned(size, PageSize), svc::ResultInvalidSize());
|
|
|
|
R_UNLESS(size > 0, svc::ResultInvalidSize());
|
|
|
|
R_UNLESS((address < address + size), svc::ResultInvalidMemoryRegion());
|
|
|
|
|
|
|
|
/* Verify that the process has system resource. */
|
|
|
|
auto &process = GetCurrentProcess();
|
|
|
|
R_UNLESS(process.GetTotalSystemResourceSize() > 0, svc::ResultInvalidState());
|
|
|
|
|
|
|
|
/* Verify that the region is in range. */
|
|
|
|
auto &page_table = process.GetPageTable();
|
|
|
|
R_UNLESS(page_table.IsInAliasRegion(address, size), svc::ResultInvalidMemoryRegion());
|
|
|
|
|
|
|
|
/* Unmap the memory. */
|
|
|
|
R_TRY(page_table.UnmapPhysicalMemory(address, size));
|
|
|
|
|
|
|
|
return ResultSuccess();
|
|
|
|
}
|
|
|
|
|
2020-07-25 05:04:04 +00:00
|
|
|
Result MapPhysicalMemoryUnsafe(uintptr_t address, size_t size) {
|
|
|
|
/* Validate address / size. */
|
|
|
|
R_UNLESS(util::IsAligned(address, PageSize), svc::ResultInvalidAddress());
|
|
|
|
R_UNLESS(util::IsAligned(size, PageSize), svc::ResultInvalidSize());
|
|
|
|
R_UNLESS(size > 0, svc::ResultInvalidSize());
|
|
|
|
R_UNLESS((address < address + size), svc::ResultInvalidCurrentMemory());
|
|
|
|
|
|
|
|
/* Verify that the region is in range. */
|
|
|
|
auto &process = GetCurrentProcess();
|
|
|
|
auto &page_table = process.GetPageTable();
|
|
|
|
R_UNLESS(page_table.IsInUnsafeAliasRegion(address, size), svc::ResultInvalidMemoryRegion());
|
|
|
|
|
|
|
|
/* Verify that the process isn't already using the unsafe pool. */
|
|
|
|
R_UNLESS(process.GetMemoryPool() != KMemoryManager::Pool_Unsafe, svc::ResultInvalidMemoryPool());
|
|
|
|
|
|
|
|
/* Map the memory. */
|
|
|
|
R_TRY(page_table.MapPhysicalMemoryUnsafe(address, size));
|
|
|
|
|
|
|
|
return ResultSuccess();
|
|
|
|
}
|
|
|
|
|
|
|
|
Result UnmapPhysicalMemoryUnsafe(uintptr_t address, size_t size) {
|
|
|
|
/* Validate address / size. */
|
|
|
|
R_UNLESS(util::IsAligned(address, PageSize), svc::ResultInvalidAddress());
|
|
|
|
R_UNLESS(util::IsAligned(size, PageSize), svc::ResultInvalidSize());
|
|
|
|
R_UNLESS(size > 0, svc::ResultInvalidSize());
|
|
|
|
R_UNLESS((address < address + size), svc::ResultInvalidCurrentMemory());
|
|
|
|
|
|
|
|
/* Verify that the region is in range. */
|
|
|
|
auto &process = GetCurrentProcess();
|
|
|
|
auto &page_table = process.GetPageTable();
|
|
|
|
R_UNLESS(page_table.IsInUnsafeAliasRegion(address, size), svc::ResultInvalidMemoryRegion());
|
|
|
|
|
|
|
|
/* Unmap the memory. */
|
|
|
|
R_TRY(page_table.UnmapPhysicalMemoryUnsafe(address, size));
|
|
|
|
|
|
|
|
return ResultSuccess();
|
|
|
|
}
|
|
|
|
|
2020-02-08 19:53:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ============================= 64 ABI ============================= */
|
|
|
|
|
|
|
|
Result SetHeapSize64(ams::svc::Address *out_address, ams::svc::Size size) {
|
2020-07-15 10:07:00 +00:00
|
|
|
static_assert(sizeof(*out_address) == sizeof(uintptr_t));
|
|
|
|
return SetHeapSize(reinterpret_cast<uintptr_t *>(out_address), size);
|
2020-02-08 19:53:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result MapPhysicalMemory64(ams::svc::Address address, ams::svc::Size size) {
|
2020-07-24 15:07:34 +00:00
|
|
|
return MapPhysicalMemory(address, size);
|
2020-02-08 19:53:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result UnmapPhysicalMemory64(ams::svc::Address address, ams::svc::Size size) {
|
2020-07-24 15:07:34 +00:00
|
|
|
return UnmapPhysicalMemory(address, size);
|
2020-02-08 19:53:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result MapPhysicalMemoryUnsafe64(ams::svc::Address address, ams::svc::Size size) {
|
2020-07-25 05:04:04 +00:00
|
|
|
return MapPhysicalMemoryUnsafe(address, size);
|
2020-02-08 19:53:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result UnmapPhysicalMemoryUnsafe64(ams::svc::Address address, ams::svc::Size size) {
|
2020-07-25 05:04:04 +00:00
|
|
|
return UnmapPhysicalMemoryUnsafe(address, size);
|
2020-02-08 19:53:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result SetUnsafeLimit64(ams::svc::Size limit) {
|
2020-04-26 09:49:59 +00:00
|
|
|
return SetUnsafeLimit(limit);
|
2020-02-08 19:53:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ============================= 64From32 ABI ============================= */
|
|
|
|
|
|
|
|
Result SetHeapSize64From32(ams::svc::Address *out_address, ams::svc::Size size) {
|
2020-07-15 10:07:00 +00:00
|
|
|
static_assert(sizeof(*out_address) == sizeof(uintptr_t));
|
|
|
|
return SetHeapSize(reinterpret_cast<uintptr_t *>(out_address), size);
|
2020-02-08 19:53:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result MapPhysicalMemory64From32(ams::svc::Address address, ams::svc::Size size) {
|
2020-07-24 15:07:34 +00:00
|
|
|
return MapPhysicalMemory(address, size);
|
2020-02-08 19:53:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result UnmapPhysicalMemory64From32(ams::svc::Address address, ams::svc::Size size) {
|
2020-07-24 15:07:34 +00:00
|
|
|
return UnmapPhysicalMemory(address, size);
|
2020-02-08 19:53:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result MapPhysicalMemoryUnsafe64From32(ams::svc::Address address, ams::svc::Size size) {
|
2020-07-25 05:04:04 +00:00
|
|
|
return MapPhysicalMemoryUnsafe(address, size);
|
2020-02-08 19:53:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result UnmapPhysicalMemoryUnsafe64From32(ams::svc::Address address, ams::svc::Size size) {
|
2020-07-25 05:04:04 +00:00
|
|
|
return UnmapPhysicalMemoryUnsafe(address, size);
|
2020-02-08 19:53:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result SetUnsafeLimit64From32(ams::svc::Size limit) {
|
2020-04-26 09:49:59 +00:00
|
|
|
return SetUnsafeLimit(limit);
|
2020-02-08 19:53:27 +00:00
|
|
|
}
|
|
|
|
|
2020-02-08 19:56:13 +00:00
|
|
|
}
|