yuzu/src/core/hle/kernel/k_shared_memory.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

107 lines
3.9 KiB
C++
Raw Normal View History

chore: make yuzu REUSE compliant [REUSE] is a specification that aims at making file copyright information consistent, so that it can be both human and machine readable. It basically requires that all files have a header containing copyright and licensing information. When this isn't possible, like when dealing with binary assets, generated files or embedded third-party dependencies, it is permitted to insert copyright information in the `.reuse/dep5` file. Oh, and it also requires that all the licenses used in the project are present in the `LICENSES` folder, that's why the diff is so huge. This can be done automatically with `reuse download --all`. The `reuse` tool also contains a handy subcommand that analyzes the project and tells whether or not the project is (still) compliant, `reuse lint`. Following REUSE has a few advantages over the current approach: - Copyright information is easy to access for users / downstream - Files like `dist/license.md` do not need to exist anymore, as `.reuse/dep5` is used instead - `reuse lint` makes it easy to ensure that copyright information of files like binary assets / images is always accurate and up to date To add copyright information of files that didn't have it I looked up who committed what and when, for each file. As yuzu contributors do not have to sign a CLA or similar I couldn't assume that copyright ownership was of the "yuzu Emulator Project", so I used the name and/or email of the commit author instead. [REUSE]: https://reuse.software Follow-up to 01cf05bc75b1e47beb08937439f3ed9339e7b254
2022-05-15 02:06:02 +02:00
// SPDX-FileCopyrightText: 2014 Citra Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include "common/assert.h"
#include "core/core.h"
#include "core/hle/kernel/k_page_table.h"
#include "core/hle/kernel/k_scoped_resource_reservation.h"
#include "core/hle/kernel/k_shared_memory.h"
#include "core/hle/kernel/k_system_resource.h"
#include "core/hle/kernel/kernel.h"
#include "core/hle/kernel/svc_results.h"
namespace Kernel {
2023-03-07 16:49:41 +01:00
KSharedMemory::KSharedMemory(KernelCore& kernel) : KAutoObjectWithSlabHeapAndContainer{kernel} {}
KSharedMemory::~KSharedMemory() = default;
Result KSharedMemory::Initialize(Core::DeviceMemory& device_memory, KProcess* owner_process,
Svc::MemoryPermission owner_permission,
Svc::MemoryPermission user_permission, std::size_t size) {
// Set members.
m_owner_process = owner_process;
m_device_memory = std::addressof(device_memory);
m_owner_permission = owner_permission;
m_user_permission = user_permission;
m_size = Common::AlignUp(size, PageSize);
const size_t num_pages = Common::DivideUp(size, PageSize);
// Get the resource limit.
2023-03-07 16:49:41 +01:00
KResourceLimit* reslimit = m_kernel.GetSystemResourceLimit();
// Reserve memory for ourselves.
2022-11-03 15:22:05 +01:00
KScopedResourceReservation memory_reservation(reslimit, LimitableResource::PhysicalMemoryMax,
size);
R_UNLESS(memory_reservation.Succeeded(), ResultLimitReached);
// Allocate the memory.
//! HACK: Open continuous mapping from sysmodule pool.
auto option = KMemoryManager::EncodeOption(KMemoryManager::Pool::Secure,
KMemoryManager::Direction::FromBack);
2023-03-07 16:49:41 +01:00
m_physical_address = m_kernel.MemoryManager().AllocateAndOpenContinuous(num_pages, 1, option);
R_UNLESS(m_physical_address != 0, ResultOutOfMemory);
//! Insert the result into our page group.
2023-03-07 18:01:07 +01:00
m_page_group.emplace(m_kernel,
std::addressof(m_kernel.GetSystemSystemResource().GetBlockInfoManager()));
m_page_group->AddBlock(m_physical_address, num_pages);
// Commit our reservation.
memory_reservation.Commit();
// Set our resource limit.
m_resource_limit = reslimit;
m_resource_limit->Open();
// Mark initialized.
m_is_initialized = true;
// Clear all pages in the memory.
for (const auto& block : *m_page_group) {
std::memset(m_device_memory->GetPointer<void>(block.GetAddress()), 0, block.GetSize());
}
2023-03-07 02:34:25 +01:00
R_SUCCEED();
}
void KSharedMemory::Finalize() {
// Close and finalize the page group.
m_page_group->Close();
m_page_group->Finalize();
// Release the memory reservation.
m_resource_limit->Release(LimitableResource::PhysicalMemoryMax, m_size);
m_resource_limit->Close();
}
Result KSharedMemory::Map(KProcess& target_process, KProcessAddress address, std::size_t map_size,
Svc::MemoryPermission map_perm) {
// Validate the size.
R_UNLESS(m_size == map_size, ResultInvalidSize);
// Validate the permission.
const Svc::MemoryPermission test_perm =
std::addressof(target_process) == m_owner_process ? m_owner_permission : m_user_permission;
if (test_perm == Svc::MemoryPermission::DontCare) {
ASSERT(map_perm == Svc::MemoryPermission::Read || map_perm == Svc::MemoryPermission::Write);
} else {
R_UNLESS(map_perm == test_perm, ResultInvalidNewMemoryPermission);
}
2023-07-15 03:43:15 +02:00
R_RETURN(target_process.GetPageTable().MapPageGroup(
address, *m_page_group, KMemoryState::Shared, ConvertToKMemoryPermission(map_perm)));
}
Result KSharedMemory::Unmap(KProcess& target_process, KProcessAddress address,
std::size_t unmap_size) {
// Validate the size.
R_UNLESS(m_size == unmap_size, ResultInvalidSize);
R_RETURN(
2023-07-15 03:43:15 +02:00
target_process.GetPageTable().UnmapPageGroup(address, *m_page_group, KMemoryState::Shared));
}
} // namespace Kernel