2020-02-15 08:00:35 +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 {
|
|
|
|
|
2020-07-14 17:49:49 +01: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 17:49:49 +01:00
|
|
|
/* Member functions. */
|
|
|
|
Result KDeviceAddressSpace::Initialize(u64 address, u64 size) {
|
|
|
|
MESOSPHERE_ASSERT_THIS();
|
|
|
|
|
|
|
|
/* Initialize the device page table. */
|
|
|
|
R_TRY(this->table.Initialize(address, size));
|
|
|
|
|
|
|
|
/* Set member variables. */
|
|
|
|
this->space_address = address;
|
|
|
|
this->space_size = size;
|
|
|
|
this->is_initialized = true;
|
|
|
|
|
|
|
|
return ResultSuccess();
|
|
|
|
}
|
|
|
|
|
|
|
|
void KDeviceAddressSpace::Finalize() {
|
|
|
|
MESOSPHERE_ASSERT_THIS();
|
|
|
|
|
|
|
|
/* Finalize the table. */
|
|
|
|
this->table.Finalize();
|
|
|
|
|
|
|
|
/* Finalize base. */
|
|
|
|
KAutoObjectWithSlabHeapAndContainer<KDeviceAddressSpace, KAutoObjectWithList>::Finalize();
|
|
|
|
}
|
|
|
|
|
|
|
|
Result KDeviceAddressSpace::Attach(ams::svc::DeviceName device_name) {
|
|
|
|
/* Lock the address space. */
|
|
|
|
KScopedLightLock lk(this->lock);
|
|
|
|
|
|
|
|
/* Attach. */
|
|
|
|
return this->table.Attach(device_name, this->space_address, this->space_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
Result KDeviceAddressSpace::Detach(ams::svc::DeviceName device_name) {
|
2020-07-15 02:46:25 +01:00
|
|
|
/* Lock the address space. */
|
|
|
|
KScopedLightLock lk(this->lock);
|
|
|
|
|
|
|
|
/* Detach. */
|
|
|
|
return this->table.Detach(device_name);
|
2020-07-14 17:49:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result KDeviceAddressSpace::Map(size_t *out_mapped_size, KProcessPageTable *page_table, KProcessAddress process_address, size_t size, u64 device_address, ams::svc::MemoryPermission device_perm, bool is_aligned, bool refresh_mappings) {
|
2020-07-15 02:46:25 +01:00
|
|
|
/* Check that the address falls within the space. */
|
|
|
|
R_UNLESS((this->space_address <= device_address && device_address + size - 1 <= this->space_address + this->space_size - 1), svc::ResultInvalidCurrentMemory());
|
|
|
|
|
|
|
|
/* Lock the address space. */
|
|
|
|
KScopedLightLock lk(this->lock);
|
|
|
|
|
|
|
|
/* Lock the pages. */
|
|
|
|
KPageGroup pg(page_table->GetBlockInfoManager());
|
|
|
|
R_TRY(page_table->LockForDeviceAddressSpace(std::addressof(pg), process_address, size, ConvertToKMemoryPermission(device_perm), is_aligned));
|
|
|
|
|
2020-07-24 03:26:46 +01:00
|
|
|
/* Close the pages we opened when we're done with them. */
|
|
|
|
ON_SCOPE_EXIT { pg.Close(); };
|
|
|
|
|
2020-07-15 02:46:25 +01: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 02:46:25 +01:00
|
|
|
|
|
|
|
/* Map the pages. */
|
|
|
|
{
|
|
|
|
/* Clear the output size to zero on failure. */
|
2020-12-01 14:53:22 +00:00
|
|
|
auto mapped_size_guard = SCOPE_GUARD { *out_mapped_size = 0; };
|
2020-07-15 02:46:25 +01:00
|
|
|
|
|
|
|
/* Perform the mapping. */
|
|
|
|
R_TRY(this->table.Map(out_mapped_size, pg, device_address, device_perm, refresh_mappings));
|
|
|
|
|
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. */
|
|
|
|
auto map_guard = SCOPE_GUARD { this->table.Unmap(device_address, *out_mapped_size); };
|
|
|
|
|
|
|
|
/* Update the protections in accordance with how much we mapped. */
|
|
|
|
R_TRY(page_table->UnlockForDeviceAddressSpacePartialMap(process_address, size, *out_mapped_size));
|
|
|
|
|
|
|
|
/* We succeeded, so cancel our guards. */
|
2020-07-15 02:46:25 +01:00
|
|
|
map_guard.Cancel();
|
2020-12-01 14:53:22 +00:00
|
|
|
mapped_size_guard.Cancel();
|
2020-07-15 02:46:25 +01:00
|
|
|
}
|
|
|
|
|
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 02:46:25 +01:00
|
|
|
return ResultSuccess();
|
2020-07-14 17:49:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result KDeviceAddressSpace::Unmap(KProcessPageTable *page_table, KProcessAddress process_address, size_t size, u64 device_address) {
|
2020-07-15 02:46:25 +01:00
|
|
|
/* Check that the address falls within the space. */
|
|
|
|
R_UNLESS((this->space_address <= device_address && device_address + size - 1 <= this->space_address + this->space_size - 1), svc::ResultInvalidCurrentMemory());
|
|
|
|
|
|
|
|
/* Lock the address space. */
|
|
|
|
KScopedLightLock lk(this->lock);
|
|
|
|
|
|
|
|
/* Make and open a page group for the unmapped region. */
|
|
|
|
KPageGroup pg(page_table->GetBlockInfoManager());
|
2020-12-01 14:53:22 +00:00
|
|
|
R_TRY(page_table->MakePageGroupForUnmapDeviceAddressSpace(std::addressof(pg), process_address, size));
|
2020-07-15 02:46:25 +01:00
|
|
|
|
|
|
|
/* Ensure the page group is closed on scope exit. */
|
|
|
|
ON_SCOPE_EXIT { pg.Close(); };
|
|
|
|
|
2020-12-01 14:53:22 +00:00
|
|
|
/* If we fail to unmap, we want to do a partial unlock. */
|
|
|
|
{
|
|
|
|
auto unlock_guard = SCOPE_GUARD { page_table->UnlockForDeviceAddressSpacePartialMap(process_address, size, size); };
|
|
|
|
|
|
|
|
/* Unmap. */
|
|
|
|
R_TRY(this->table.Unmap(pg, device_address));
|
|
|
|
|
|
|
|
unlock_guard.Cancel();
|
|
|
|
}
|
2020-07-15 02:46:25 +01: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 02:46:25 +01:00
|
|
|
|
|
|
|
return ResultSuccess();
|
2020-07-14 17:49:49 +01:00
|
|
|
}
|
|
|
|
|
2020-02-15 08:00:35 +00:00
|
|
|
}
|