2020-02-08 19:53:27 +00:00
|
|
|
/*
|
2021-10-04 19:59:10 +00:00
|
|
|
* Copyright (c) Atmosphère-NX
|
2020-02-08 19:53:27 +00:00
|
|
|
*
|
|
|
|
* 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-23 07:44:33 +00:00
|
|
|
constexpr bool IsValidTransferMemoryPermission(ams::svc::MemoryPermission perm) {
|
|
|
|
switch (perm) {
|
|
|
|
case ams::svc::MemoryPermission_None:
|
|
|
|
case ams::svc::MemoryPermission_Read:
|
|
|
|
case ams::svc::MemoryPermission_ReadWrite:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2020-02-08 19:53:27 +00:00
|
|
|
|
2020-07-23 07:44:33 +00:00
|
|
|
Result MapTransferMemory(ams::svc::Handle trmem_handle, uintptr_t address, size_t size, ams::svc::MemoryPermission map_perm) {
|
|
|
|
/* Validate the 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());
|
|
|
|
|
|
|
|
/* Validate the permission. */
|
|
|
|
R_UNLESS(IsValidTransferMemoryPermission(map_perm), svc::ResultInvalidState());
|
|
|
|
|
|
|
|
/* Get the transfer memory. */
|
|
|
|
KScopedAutoObject trmem = GetCurrentProcess().GetHandleTable().GetObject<KTransferMemory>(trmem_handle);
|
|
|
|
R_UNLESS(trmem.IsNotNull(), svc::ResultInvalidHandle());
|
|
|
|
|
|
|
|
/* Verify that the mapping is in range. */
|
|
|
|
R_UNLESS(GetCurrentProcess().GetPageTable().CanContain(address, size, KMemoryState_Transfered), svc::ResultInvalidMemoryRegion());
|
|
|
|
|
|
|
|
/* Map the transfer memory. */
|
|
|
|
R_TRY(trmem->Map(address, size, map_perm));
|
|
|
|
|
|
|
|
/* We succeeded. */
|
2022-02-14 22:45:32 +00:00
|
|
|
R_SUCCEED();
|
2020-07-23 07:44:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result UnmapTransferMemory(ams::svc::Handle trmem_handle, uintptr_t address, size_t size) {
|
|
|
|
/* Validate the 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());
|
|
|
|
|
|
|
|
/* Get the transfer memory. */
|
|
|
|
KScopedAutoObject trmem = GetCurrentProcess().GetHandleTable().GetObject<KTransferMemory>(trmem_handle);
|
|
|
|
R_UNLESS(trmem.IsNotNull(), svc::ResultInvalidHandle());
|
|
|
|
|
|
|
|
/* Verify that the mapping is in range. */
|
|
|
|
R_UNLESS(GetCurrentProcess().GetPageTable().CanContain(address, size, KMemoryState_Transfered), svc::ResultInvalidMemoryRegion());
|
|
|
|
|
|
|
|
/* Unmap the transfer memory. */
|
|
|
|
R_TRY(trmem->Unmap(address, size));
|
|
|
|
|
2022-02-14 22:45:32 +00:00
|
|
|
R_SUCCEED();
|
2020-07-23 07:44:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result CreateTransferMemory(ams::svc::Handle *out, uintptr_t address, size_t size, ams::svc::MemoryPermission map_perm) {
|
|
|
|
/* Validate the 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());
|
|
|
|
|
|
|
|
/* Validate the permissions. */
|
|
|
|
R_UNLESS(IsValidTransferMemoryPermission(map_perm), svc::ResultInvalidNewMemoryPermission());
|
|
|
|
|
|
|
|
/* Get the current process and handle table. */
|
|
|
|
auto &process = GetCurrentProcess();
|
|
|
|
auto &handle_table = process.GetHandleTable();
|
|
|
|
|
|
|
|
/* Reserve a new transfer memory from the process resource limit. */
|
|
|
|
KScopedResourceReservation trmem_reservation(std::addressof(process), ams::svc::LimitableResource_TransferMemoryCountMax);
|
|
|
|
R_UNLESS(trmem_reservation.Succeeded(), svc::ResultLimitReached());
|
|
|
|
|
|
|
|
/* Create the transfer memory. */
|
|
|
|
KTransferMemory *trmem = KTransferMemory::Create();
|
|
|
|
R_UNLESS(trmem != nullptr, svc::ResultOutOfResource());
|
|
|
|
|
|
|
|
/* Ensure the only reference is in the handle table when we're done. */
|
|
|
|
ON_SCOPE_EXIT { trmem->Close(); };
|
|
|
|
|
|
|
|
/* Ensure that the region is in range. */
|
|
|
|
R_UNLESS(process.GetPageTable().Contains(address, size), svc::ResultInvalidCurrentMemory());
|
|
|
|
|
|
|
|
/* Initialize the transfer memory. */
|
|
|
|
R_TRY(trmem->Initialize(address, size, map_perm));
|
|
|
|
|
|
|
|
/* Commit the reservation. */
|
|
|
|
trmem_reservation.Commit();
|
|
|
|
|
|
|
|
/* Register the transfer memory. */
|
2020-12-01 21:49:30 +00:00
|
|
|
KTransferMemory::Register(trmem);
|
2020-07-23 07:44:33 +00:00
|
|
|
|
|
|
|
/* Add the transfer memory to the handle table. */
|
|
|
|
R_TRY(handle_table.Add(out, trmem));
|
|
|
|
|
2022-02-14 22:45:32 +00:00
|
|
|
R_SUCCEED();
|
2020-07-23 07:44:33 +00:00
|
|
|
}
|
2020-02-08 19:53:27 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ============================= 64 ABI ============================= */
|
|
|
|
|
2020-07-23 07:44:33 +00:00
|
|
|
Result MapTransferMemory64(ams::svc::Handle trmem_handle, ams::svc::Address address, ams::svc::Size size, ams::svc::MemoryPermission owner_perm) {
|
2022-02-14 22:45:32 +00:00
|
|
|
R_RETURN(MapTransferMemory(trmem_handle, address, size, owner_perm));
|
2020-02-08 19:53:27 +00:00
|
|
|
}
|
|
|
|
|
2020-07-23 07:44:33 +00:00
|
|
|
Result UnmapTransferMemory64(ams::svc::Handle trmem_handle, ams::svc::Address address, ams::svc::Size size) {
|
2022-02-14 22:45:32 +00:00
|
|
|
R_RETURN(UnmapTransferMemory(trmem_handle, address, size));
|
2020-02-08 19:53:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result CreateTransferMemory64(ams::svc::Handle *out_handle, ams::svc::Address address, ams::svc::Size size, ams::svc::MemoryPermission map_perm) {
|
2022-02-14 22:45:32 +00:00
|
|
|
R_RETURN(CreateTransferMemory(out_handle, address, size, map_perm));
|
2020-02-08 19:53:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ============================= 64From32 ABI ============================= */
|
|
|
|
|
2020-07-23 07:44:33 +00:00
|
|
|
Result MapTransferMemory64From32(ams::svc::Handle trmem_handle, ams::svc::Address address, ams::svc::Size size, ams::svc::MemoryPermission owner_perm) {
|
2022-02-14 22:45:32 +00:00
|
|
|
R_RETURN(MapTransferMemory(trmem_handle, address, size, owner_perm));
|
2020-02-08 19:53:27 +00:00
|
|
|
}
|
|
|
|
|
2020-07-23 07:44:33 +00:00
|
|
|
Result UnmapTransferMemory64From32(ams::svc::Handle trmem_handle, ams::svc::Address address, ams::svc::Size size) {
|
2022-02-14 22:45:32 +00:00
|
|
|
R_RETURN(UnmapTransferMemory(trmem_handle, address, size));
|
2020-02-08 19:53:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result CreateTransferMemory64From32(ams::svc::Handle *out_handle, ams::svc::Address address, ams::svc::Size size, ams::svc::MemoryPermission map_perm) {
|
2022-02-14 22:45:32 +00:00
|
|
|
R_RETURN(CreateTransferMemory(out_handle, address, size, map_perm));
|
2020-02-08 19:53:27 +00:00
|
|
|
}
|
|
|
|
|
2020-02-08 19:56:13 +00:00
|
|
|
}
|