2020-07-23 09:44:33 +02:00
|
|
|
/*
|
2021-10-04 21:59:10 +02:00
|
|
|
* Copyright (c) Atmosphère-NX
|
2020-07-23 09:44:33 +02: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 {
|
|
|
|
|
|
|
|
Result KTransferMemory::Initialize(KProcessAddress addr, size_t size, ams::svc::MemoryPermission own_perm) {
|
|
|
|
MESOSPHERE_ASSERT_THIS();
|
|
|
|
|
|
|
|
/* Set members. */
|
2020-12-18 02:18:47 +01:00
|
|
|
m_owner = GetCurrentProcessPointer();
|
2020-07-23 09:44:33 +02:00
|
|
|
|
2021-03-22 04:30:40 +01:00
|
|
|
/* Get the owner page table. */
|
2020-12-18 02:18:47 +01:00
|
|
|
auto &page_table = m_owner->GetPageTable();
|
2020-07-23 09:44:33 +02:00
|
|
|
|
2021-03-22 04:30:40 +01:00
|
|
|
/* Construct the page group, guarding to make sure our state is valid on exit. */
|
|
|
|
auto pg_guard = util::ConstructAtGuarded(m_page_group, page_table.GetBlockInfoManager());
|
2020-07-23 09:44:33 +02:00
|
|
|
|
|
|
|
/* Lock the memory. */
|
2020-12-18 02:18:47 +01:00
|
|
|
R_TRY(page_table.LockForTransferMemory(GetPointer(m_page_group), addr, size, ConvertToKMemoryPermission(own_perm)));
|
2020-07-23 09:44:33 +02:00
|
|
|
|
|
|
|
/* Set remaining tracking members. */
|
2020-12-18 02:18:47 +01:00
|
|
|
m_owner->Open();
|
|
|
|
m_owner_perm = own_perm;
|
|
|
|
m_address = addr;
|
|
|
|
m_is_initialized = true;
|
|
|
|
m_is_mapped = false;
|
2020-07-23 09:44:33 +02:00
|
|
|
|
|
|
|
/* We succeeded. */
|
|
|
|
pg_guard.Cancel();
|
|
|
|
return ResultSuccess();
|
|
|
|
}
|
|
|
|
|
|
|
|
void KTransferMemory::Finalize() {
|
|
|
|
MESOSPHERE_ASSERT_THIS();
|
|
|
|
|
2020-07-29 00:09:07 +02:00
|
|
|
/* Unlock. */
|
2020-12-18 02:18:47 +01:00
|
|
|
if (!m_is_mapped) {
|
|
|
|
const size_t size = GetReference(m_page_group).GetNumPages() * PageSize;
|
|
|
|
MESOSPHERE_R_ABORT_UNLESS(m_owner->GetPageTable().UnlockForTransferMemory(m_address, size, GetReference(m_page_group)));
|
2020-07-23 09:44:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Close the page group. */
|
2020-12-18 02:18:47 +01:00
|
|
|
GetReference(m_page_group).Close();
|
|
|
|
GetReference(m_page_group).Finalize();
|
2020-07-23 09:44:33 +02:00
|
|
|
|
|
|
|
/* Perform inherited finalization. */
|
|
|
|
KAutoObjectWithSlabHeapAndContainer<KTransferMemory, KAutoObjectWithList>::Finalize();
|
|
|
|
}
|
|
|
|
|
|
|
|
void KTransferMemory::PostDestroy(uintptr_t arg) {
|
|
|
|
KProcess *owner = reinterpret_cast<KProcess *>(arg);
|
|
|
|
owner->ReleaseResource(ams::svc::LimitableResource_TransferMemoryCountMax, 1);
|
|
|
|
owner->Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
Result KTransferMemory::Map(KProcessAddress address, size_t size, ams::svc::MemoryPermission map_perm) {
|
|
|
|
MESOSPHERE_ASSERT_THIS();
|
|
|
|
|
|
|
|
/* Validate the size. */
|
2020-12-18 02:18:47 +01:00
|
|
|
R_UNLESS(GetReference(m_page_group).GetNumPages() == util::DivideUp(size, PageSize), svc::ResultInvalidSize());
|
2020-07-23 09:44:33 +02:00
|
|
|
|
|
|
|
/* Validate the permission. */
|
2020-12-18 02:18:47 +01:00
|
|
|
R_UNLESS(m_owner_perm == map_perm, svc::ResultInvalidState());
|
2020-07-23 09:44:33 +02:00
|
|
|
|
|
|
|
/* Lock ourselves. */
|
2020-12-18 02:18:47 +01:00
|
|
|
KScopedLightLock lk(m_lock);
|
2020-07-23 09:44:33 +02:00
|
|
|
|
|
|
|
/* Ensure we're not already mapped. */
|
2020-12-18 02:18:47 +01:00
|
|
|
R_UNLESS(!m_is_mapped, svc::ResultInvalidState());
|
2020-07-23 09:44:33 +02:00
|
|
|
|
|
|
|
/* Map the memory. */
|
2020-12-18 02:18:47 +01:00
|
|
|
const KMemoryState state = (m_owner_perm == ams::svc::MemoryPermission_None) ? KMemoryState_Transfered : KMemoryState_SharedTransfered;
|
|
|
|
R_TRY(GetCurrentProcess().GetPageTable().MapPageGroup(address, GetReference(m_page_group), state, KMemoryPermission_UserReadWrite));
|
2020-07-23 09:44:33 +02:00
|
|
|
|
|
|
|
/* Mark ourselves as mapped. */
|
2020-12-18 02:18:47 +01:00
|
|
|
m_is_mapped = true;
|
2020-07-23 09:44:33 +02:00
|
|
|
|
|
|
|
return ResultSuccess();
|
|
|
|
}
|
|
|
|
|
|
|
|
Result KTransferMemory::Unmap(KProcessAddress address, size_t size) {
|
|
|
|
MESOSPHERE_ASSERT_THIS();
|
|
|
|
|
|
|
|
/* Validate the size. */
|
2020-12-18 02:18:47 +01:00
|
|
|
R_UNLESS(GetReference(m_page_group).GetNumPages() == util::DivideUp(size, PageSize), svc::ResultInvalidSize());
|
2020-07-23 09:44:33 +02:00
|
|
|
|
|
|
|
/* Lock ourselves. */
|
2020-12-18 02:18:47 +01:00
|
|
|
KScopedLightLock lk(m_lock);
|
2020-07-23 09:44:33 +02:00
|
|
|
|
|
|
|
/* Unmap the memory. */
|
2020-12-18 02:18:47 +01:00
|
|
|
const KMemoryState state = (m_owner_perm == ams::svc::MemoryPermission_None) ? KMemoryState_Transfered : KMemoryState_SharedTransfered;
|
|
|
|
R_TRY(GetCurrentProcess().GetPageTable().UnmapPageGroup(address, GetReference(m_page_group), state));
|
2020-07-23 09:44:33 +02:00
|
|
|
|
|
|
|
/* Mark ourselves as unmapped. */
|
2020-12-18 02:18:47 +01:00
|
|
|
MESOSPHERE_ASSERT(m_is_mapped);
|
|
|
|
m_is_mapped = false;
|
2020-07-23 09:44:33 +02:00
|
|
|
|
|
|
|
return ResultSuccess();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|