|
|
@ -6,6 +6,7 @@
|
|
|
|
#include "core/hle/kernel/k_page_table.h"
|
|
|
|
#include "core/hle/kernel/k_page_table.h"
|
|
|
|
#include "core/hle/kernel/k_scoped_resource_reservation.h"
|
|
|
|
#include "core/hle/kernel/k_scoped_resource_reservation.h"
|
|
|
|
#include "core/hle/kernel/k_shared_memory.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/kernel.h"
|
|
|
|
#include "core/hle/kernel/svc_results.h"
|
|
|
|
#include "core/hle/kernel/svc_results.h"
|
|
|
|
|
|
|
|
|
|
|
@ -18,19 +19,19 @@ KSharedMemory::~KSharedMemory() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Result KSharedMemory::Initialize(Core::DeviceMemory& device_memory_, KProcess* owner_process_,
|
|
|
|
Result KSharedMemory::Initialize(Core::DeviceMemory& device_memory_, KProcess* owner_process_,
|
|
|
|
KPageGroup&& page_list_, Svc::MemoryPermission owner_permission_,
|
|
|
|
Svc::MemoryPermission owner_permission_,
|
|
|
|
Svc::MemoryPermission user_permission_, PAddr physical_address_,
|
|
|
|
Svc::MemoryPermission user_permission_, std::size_t size_,
|
|
|
|
std::size_t size_, std::string name_) {
|
|
|
|
std::string name_) {
|
|
|
|
// Set members.
|
|
|
|
// Set members.
|
|
|
|
owner_process = owner_process_;
|
|
|
|
owner_process = owner_process_;
|
|
|
|
device_memory = &device_memory_;
|
|
|
|
device_memory = &device_memory_;
|
|
|
|
page_list = std::move(page_list_);
|
|
|
|
|
|
|
|
owner_permission = owner_permission_;
|
|
|
|
owner_permission = owner_permission_;
|
|
|
|
user_permission = user_permission_;
|
|
|
|
user_permission = user_permission_;
|
|
|
|
physical_address = physical_address_;
|
|
|
|
size = Common::AlignUp(size_, PageSize);
|
|
|
|
size = size_;
|
|
|
|
|
|
|
|
name = std::move(name_);
|
|
|
|
name = std::move(name_);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const size_t num_pages = Common::DivideUp(size, PageSize);
|
|
|
|
|
|
|
|
|
|
|
|
// Get the resource limit.
|
|
|
|
// Get the resource limit.
|
|
|
|
KResourceLimit* reslimit = kernel.GetSystemResourceLimit();
|
|
|
|
KResourceLimit* reslimit = kernel.GetSystemResourceLimit();
|
|
|
|
|
|
|
|
|
|
|
@ -39,6 +40,17 @@ Result KSharedMemory::Initialize(Core::DeviceMemory& device_memory_, KProcess* o
|
|
|
|
size_);
|
|
|
|
size_);
|
|
|
|
R_UNLESS(memory_reservation.Succeeded(), ResultLimitReached);
|
|
|
|
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);
|
|
|
|
|
|
|
|
physical_address = kernel.MemoryManager().AllocateAndOpenContinuous(num_pages, 1, option);
|
|
|
|
|
|
|
|
R_UNLESS(physical_address != 0, ResultOutOfMemory);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//! Insert the result into our page group.
|
|
|
|
|
|
|
|
page_group.emplace(physical_address, num_pages);
|
|
|
|
|
|
|
|
|
|
|
|
// Commit our reservation.
|
|
|
|
// Commit our reservation.
|
|
|
|
memory_reservation.Commit();
|
|
|
|
memory_reservation.Commit();
|
|
|
|
|
|
|
|
|
|
|
@ -50,12 +62,23 @@ Result KSharedMemory::Initialize(Core::DeviceMemory& device_memory_, KProcess* o
|
|
|
|
is_initialized = true;
|
|
|
|
is_initialized = true;
|
|
|
|
|
|
|
|
|
|
|
|
// Clear all pages in the memory.
|
|
|
|
// Clear all pages in the memory.
|
|
|
|
std::memset(device_memory_.GetPointer<void>(physical_address_), 0, size_);
|
|
|
|
for (const auto& block : page_group->Nodes()) {
|
|
|
|
|
|
|
|
std::memset(device_memory_.GetPointer<void>(block.GetAddress()), 0, block.GetSize());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return ResultSuccess;
|
|
|
|
return ResultSuccess;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void KSharedMemory::Finalize() {
|
|
|
|
void KSharedMemory::Finalize() {
|
|
|
|
|
|
|
|
// Close and finalize the page group.
|
|
|
|
|
|
|
|
// page_group->Close();
|
|
|
|
|
|
|
|
// page_group->Finalize();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//! HACK: Manually close.
|
|
|
|
|
|
|
|
for (const auto& block : page_group->Nodes()) {
|
|
|
|
|
|
|
|
kernel.MemoryManager().Close(block.GetAddress(), block.GetNumPages());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Release the memory reservation.
|
|
|
|
// Release the memory reservation.
|
|
|
|
resource_limit->Release(LimitableResource::PhysicalMemoryMax, size);
|
|
|
|
resource_limit->Release(LimitableResource::PhysicalMemoryMax, size);
|
|
|
|
resource_limit->Close();
|
|
|
|
resource_limit->Close();
|
|
|
@ -65,32 +88,28 @@ void KSharedMemory::Finalize() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Result KSharedMemory::Map(KProcess& target_process, VAddr address, std::size_t map_size,
|
|
|
|
Result KSharedMemory::Map(KProcess& target_process, VAddr address, std::size_t map_size,
|
|
|
|
Svc::MemoryPermission permissions) {
|
|
|
|
Svc::MemoryPermission map_perm) {
|
|
|
|
const u64 page_count{(map_size + PageSize - 1) / PageSize};
|
|
|
|
// Validate the size.
|
|
|
|
|
|
|
|
R_UNLESS(size == map_size, ResultInvalidSize);
|
|
|
|
|
|
|
|
|
|
|
|
if (page_list.GetNumPages() != page_count) {
|
|
|
|
// Validate the permission.
|
|
|
|
UNIMPLEMENTED_MSG("Page count does not match");
|
|
|
|
const Svc::MemoryPermission test_perm =
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const Svc::MemoryPermission expected =
|
|
|
|
|
|
|
|
&target_process == owner_process ? owner_permission : user_permission;
|
|
|
|
&target_process == owner_process ? owner_permission : user_permission;
|
|
|
|
|
|
|
|
if (test_perm == Svc::MemoryPermission::DontCare) {
|
|
|
|
if (permissions != expected) {
|
|
|
|
ASSERT(map_perm == Svc::MemoryPermission::Read || map_perm == Svc::MemoryPermission::Write);
|
|
|
|
UNIMPLEMENTED_MSG("Permission does not match");
|
|
|
|
} else {
|
|
|
|
|
|
|
|
R_UNLESS(map_perm == test_perm, ResultInvalidNewMemoryPermission);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return target_process.PageTable().MapPages(address, page_list, KMemoryState::Shared,
|
|
|
|
return target_process.PageTable().MapPages(address, *page_group, KMemoryState::Shared,
|
|
|
|
ConvertToKMemoryPermission(permissions));
|
|
|
|
ConvertToKMemoryPermission(map_perm));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Result KSharedMemory::Unmap(KProcess& target_process, VAddr address, std::size_t unmap_size) {
|
|
|
|
Result KSharedMemory::Unmap(KProcess& target_process, VAddr address, std::size_t unmap_size) {
|
|
|
|
const u64 page_count{(unmap_size + PageSize - 1) / PageSize};
|
|
|
|
// Validate the size.
|
|
|
|
|
|
|
|
R_UNLESS(size == unmap_size, ResultInvalidSize);
|
|
|
|
|
|
|
|
|
|
|
|
if (page_list.GetNumPages() != page_count) {
|
|
|
|
return target_process.PageTable().UnmapPages(address, *page_group, KMemoryState::Shared);
|
|
|
|
UNIMPLEMENTED_MSG("Page count does not match");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return target_process.PageTable().UnmapPages(address, page_list, KMemoryState::Shared);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace Kernel
|
|
|
|
} // namespace Kernel
|
|
|
|