2020-02-15 08:00:35 +00:00
|
|
|
/*
|
2021-10-04 19:59:10 +00:00
|
|
|
* Copyright (c) Atmosphère-NX
|
2020-02-15 08:00:35 +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 {
|
|
|
|
|
2020-07-14 16:49:49 +00:00
|
|
|
/* Static initializer. */
|
2020-02-15 08:00:35 +00:00
|
|
|
void KDeviceAddressSpace::Initialize() {
|
|
|
|
/* This just forwards to the device page table manager. */
|
|
|
|
KDevicePageTable::Initialize();
|
|
|
|
}
|
|
|
|
|
2020-07-14 16:49:49 +00:00
|
|
|
/* Member functions. */
|
|
|
|
Result KDeviceAddressSpace::Initialize(u64 address, u64 size) {
|
|
|
|
MESOSPHERE_ASSERT_THIS();
|
|
|
|
|
|
|
|
/* Initialize the device page table. */
|
2020-12-18 01:18:47 +00:00
|
|
|
R_TRY(m_table.Initialize(address, size));
|
2020-07-14 16:49:49 +00:00
|
|
|
|
|
|
|
/* Set member variables. */
|
2020-12-18 01:18:47 +00:00
|
|
|
m_space_address = address;
|
|
|
|
m_space_size = size;
|
|
|
|
m_is_initialized = true;
|
2020-07-14 16:49:49 +00:00
|
|
|
|
|
|
|
return ResultSuccess();
|
|
|
|
}
|
|
|
|
|
|
|
|
void KDeviceAddressSpace::Finalize() {
|
|
|
|
MESOSPHERE_ASSERT_THIS();
|
|
|
|
|
|
|
|
/* Finalize the table. */
|
2020-12-18 01:18:47 +00:00
|
|
|
m_table.Finalize();
|
2020-07-14 16:49:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result KDeviceAddressSpace::Attach(ams::svc::DeviceName device_name) {
|
|
|
|
/* Lock the address space. */
|
2020-12-18 01:18:47 +00:00
|
|
|
KScopedLightLock lk(m_lock);
|
2020-07-14 16:49:49 +00:00
|
|
|
|
|
|
|
/* Attach. */
|
2020-12-18 01:18:47 +00:00
|
|
|
return m_table.Attach(device_name, m_space_address, m_space_size);
|
2020-07-14 16:49:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result KDeviceAddressSpace::Detach(ams::svc::DeviceName device_name) {
|
2020-07-15 01:46:25 +00:00
|
|
|
/* Lock the address space. */
|
2020-12-18 01:18:47 +00:00
|
|
|
KScopedLightLock lk(m_lock);
|
2020-07-15 01:46:25 +00:00
|
|
|
|
|
|
|
/* Detach. */
|
2020-12-18 01:18:47 +00:00
|
|
|
return m_table.Detach(device_name);
|
2020-07-14 16:49:49 +00:00
|
|
|
}
|
|
|
|
|
2021-09-18 18:28:39 +00:00
|
|
|
Result KDeviceAddressSpace::Map(KProcessPageTable *page_table, KProcessAddress process_address, size_t size, u64 device_address, ams::svc::MemoryPermission device_perm, bool is_aligned) {
|
2020-07-15 01:46:25 +00:00
|
|
|
/* Check that the address falls within the space. */
|
2020-12-18 01:18:47 +00:00
|
|
|
R_UNLESS((m_space_address <= device_address && device_address + size - 1 <= m_space_address + m_space_size - 1), svc::ResultInvalidCurrentMemory());
|
2020-07-15 01:46:25 +00:00
|
|
|
|
|
|
|
/* Lock the address space. */
|
2020-12-18 01:18:47 +00:00
|
|
|
KScopedLightLock lk(m_lock);
|
2020-07-15 01:46:25 +00:00
|
|
|
|
2021-04-08 00:07:01 +00:00
|
|
|
/* Lock the page table to prevent concurrent device mapping operations. */
|
|
|
|
KScopedLightLock pt_lk = page_table->AcquireDeviceMapLock();
|
2020-07-15 01:46:25 +00:00
|
|
|
|
2021-04-08 00:07:01 +00:00
|
|
|
/* Lock the pages. */
|
|
|
|
R_TRY(page_table->LockForMapDeviceAddressSpace(process_address, size, ConvertToKMemoryPermission(device_perm), is_aligned));
|
2020-07-24 02:26:46 +00:00
|
|
|
|
2020-07-15 01:46:25 +00:00
|
|
|
/* Ensure that if we fail, we don't keep unmapped pages locked. */
|
2020-12-01 14:53:22 +00:00
|
|
|
auto unlock_guard = SCOPE_GUARD { MESOSPHERE_R_ABORT_UNLESS(page_table->UnlockForDeviceAddressSpace(process_address, size)); };
|
2020-07-15 01:46:25 +00:00
|
|
|
|
|
|
|
/* Map the pages. */
|
|
|
|
{
|
|
|
|
/* Perform the mapping. */
|
2021-09-18 18:28:39 +00:00
|
|
|
R_TRY(m_table.Map(page_table, process_address, size, device_address, device_perm, is_aligned));
|
2020-07-15 01:46:25 +00:00
|
|
|
|
2020-12-01 14:53:22 +00:00
|
|
|
/* Ensure that we unmap the pages if we fail to update the protections. */
|
|
|
|
/* NOTE: Nintendo does not check the result of this unmap call. */
|
2021-09-18 18:28:39 +00:00
|
|
|
auto map_guard = SCOPE_GUARD { m_table.Unmap(device_address, size); };
|
2020-12-01 14:53:22 +00:00
|
|
|
|
|
|
|
/* Update the protections in accordance with how much we mapped. */
|
2021-09-18 18:28:39 +00:00
|
|
|
R_TRY(page_table->UnlockForDeviceAddressSpacePartialMap(process_address, size));
|
2020-12-01 14:53:22 +00:00
|
|
|
|
2021-09-18 18:28:39 +00:00
|
|
|
/* We succeeded, so cancel our guard. */
|
2020-07-15 01:46:25 +00:00
|
|
|
map_guard.Cancel();
|
|
|
|
}
|
|
|
|
|
2020-12-01 14:53:22 +00:00
|
|
|
/* We succeeded, so we don't need to unlock our pages. */
|
|
|
|
unlock_guard.Cancel();
|
2020-07-15 01:46:25 +00:00
|
|
|
return ResultSuccess();
|
2020-07-14 16:49:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result KDeviceAddressSpace::Unmap(KProcessPageTable *page_table, KProcessAddress process_address, size_t size, u64 device_address) {
|
2020-07-15 01:46:25 +00:00
|
|
|
/* Check that the address falls within the space. */
|
2020-12-18 01:18:47 +00:00
|
|
|
R_UNLESS((m_space_address <= device_address && device_address + size - 1 <= m_space_address + m_space_size - 1), svc::ResultInvalidCurrentMemory());
|
2020-07-15 01:46:25 +00:00
|
|
|
|
|
|
|
/* Lock the address space. */
|
2020-12-18 01:18:47 +00:00
|
|
|
KScopedLightLock lk(m_lock);
|
2020-07-15 01:46:25 +00:00
|
|
|
|
2021-04-08 00:07:01 +00:00
|
|
|
/* Lock the page table to prevent concurrent device mapping operations. */
|
|
|
|
KScopedLightLock pt_lk = page_table->AcquireDeviceMapLock();
|
2020-07-15 01:46:25 +00:00
|
|
|
|
2021-04-08 00:07:01 +00:00
|
|
|
/* Lock the pages. */
|
|
|
|
R_TRY(page_table->LockForUnmapDeviceAddressSpace(process_address, size));
|
2020-07-15 01:46:25 +00:00
|
|
|
|
2020-12-01 14:53:22 +00:00
|
|
|
/* If we fail to unmap, we want to do a partial unlock. */
|
|
|
|
{
|
2021-09-18 18:28:39 +00:00
|
|
|
auto unlock_guard = SCOPE_GUARD { MESOSPHERE_R_ABORT_UNLESS(page_table->UnlockForDeviceAddressSpacePartialMap(process_address, size)); };
|
2020-12-01 14:53:22 +00:00
|
|
|
|
|
|
|
/* Unmap. */
|
2021-04-08 00:07:01 +00:00
|
|
|
R_TRY(m_table.Unmap(page_table, process_address, size, device_address));
|
2020-12-01 14:53:22 +00:00
|
|
|
|
|
|
|
unlock_guard.Cancel();
|
|
|
|
}
|
2020-07-15 01:46:25 +00:00
|
|
|
|
|
|
|
/* Unlock the pages. */
|
2020-12-01 14:53:22 +00:00
|
|
|
MESOSPHERE_R_ABORT_UNLESS(page_table->UnlockForDeviceAddressSpace(process_address, size));
|
2020-07-15 01:46:25 +00:00
|
|
|
|
|
|
|
return ResultSuccess();
|
2020-07-14 16:49:49 +00:00
|
|
|
}
|
|
|
|
|
2020-02-15 08:00:35 +00:00
|
|
|
}
|