|
@@ -10,15 +10,18 @@
|
|
|
#include "common/assert.h"
|
|
#include "common/assert.h"
|
|
|
#include "common/logging/log.h"
|
|
#include "common/logging/log.h"
|
|
|
#include "core/core.h"
|
|
#include "core/core.h"
|
|
|
|
|
+#include "core/device_memory.h"
|
|
|
#include "core/file_sys/program_metadata.h"
|
|
#include "core/file_sys/program_metadata.h"
|
|
|
#include "core/hle/kernel/code_set.h"
|
|
#include "core/hle/kernel/code_set.h"
|
|
|
#include "core/hle/kernel/errors.h"
|
|
#include "core/hle/kernel/errors.h"
|
|
|
#include "core/hle/kernel/kernel.h"
|
|
#include "core/hle/kernel/kernel.h"
|
|
|
|
|
+#include "core/hle/kernel/memory/memory_block_manager.h"
|
|
|
|
|
+#include "core/hle/kernel/memory/page_table.h"
|
|
|
|
|
+#include "core/hle/kernel/memory/slab_heap.h"
|
|
|
#include "core/hle/kernel/process.h"
|
|
#include "core/hle/kernel/process.h"
|
|
|
#include "core/hle/kernel/resource_limit.h"
|
|
#include "core/hle/kernel/resource_limit.h"
|
|
|
#include "core/hle/kernel/scheduler.h"
|
|
#include "core/hle/kernel/scheduler.h"
|
|
|
#include "core/hle/kernel/thread.h"
|
|
#include "core/hle/kernel/thread.h"
|
|
|
-#include "core/hle/kernel/vm_manager.h"
|
|
|
|
|
#include "core/memory.h"
|
|
#include "core/memory.h"
|
|
|
#include "core/settings.h"
|
|
#include "core/settings.h"
|
|
|
|
|
|
|
@@ -31,10 +34,8 @@ namespace {
|
|
|
* @param kernel The kernel instance to create the main thread under.
|
|
* @param kernel The kernel instance to create the main thread under.
|
|
|
* @param priority The priority to give the main thread
|
|
* @param priority The priority to give the main thread
|
|
|
*/
|
|
*/
|
|
|
-void SetupMainThread(Process& owner_process, KernelCore& kernel, u32 priority) {
|
|
|
|
|
- const auto& vm_manager = owner_process.VMManager();
|
|
|
|
|
- const VAddr entry_point = vm_manager.GetCodeRegionBaseAddress();
|
|
|
|
|
- const VAddr stack_top = vm_manager.GetTLSIORegionEndAddress();
|
|
|
|
|
|
|
+void SetupMainThread(Process& owner_process, KernelCore& kernel, u32 priority, VAddr stack_top) {
|
|
|
|
|
+ const VAddr entry_point = owner_process.PageTable().GetCodeRegionStart();
|
|
|
auto thread_res = Thread::Create(kernel, "main", entry_point, priority, 0,
|
|
auto thread_res = Thread::Create(kernel, "main", entry_point, priority, 0,
|
|
|
owner_process.GetIdealCore(), stack_top, owner_process);
|
|
owner_process.GetIdealCore(), stack_top, owner_process);
|
|
|
|
|
|
|
@@ -109,7 +110,7 @@ std::shared_ptr<Process> Process::Create(Core::System& system, std::string name,
|
|
|
|
|
|
|
|
std::shared_ptr<Process> process = std::make_shared<Process>(system);
|
|
std::shared_ptr<Process> process = std::make_shared<Process>(system);
|
|
|
process->name = std::move(name);
|
|
process->name = std::move(name);
|
|
|
- process->resource_limit = kernel.GetSystemResourceLimit();
|
|
|
|
|
|
|
+ process->resource_limit = ResourceLimit::Create(kernel);
|
|
|
process->status = ProcessStatus::Created;
|
|
process->status = ProcessStatus::Created;
|
|
|
process->program_id = 0;
|
|
process->program_id = 0;
|
|
|
process->process_id = type == ProcessType::KernelInternal ? kernel.CreateNewKernelProcessID()
|
|
process->process_id = type == ProcessType::KernelInternal ? kernel.CreateNewKernelProcessID()
|
|
@@ -130,7 +131,14 @@ std::shared_ptr<ResourceLimit> Process::GetResourceLimit() const {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
u64 Process::GetTotalPhysicalMemoryAvailable() const {
|
|
u64 Process::GetTotalPhysicalMemoryAvailable() const {
|
|
|
- return vm_manager.GetTotalPhysicalMemoryAvailable();
|
|
|
|
|
|
|
+ const u64 capacity{resource_limit->GetCurrentResourceValue(ResourceType::PhysicalMemory) +
|
|
|
|
|
+ page_table->GetTotalHeapSize() + image_size + main_thread_stack_size};
|
|
|
|
|
+
|
|
|
|
|
+ if (capacity < memory_usage_capacity) {
|
|
|
|
|
+ return capacity;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return memory_usage_capacity;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
u64 Process::GetTotalPhysicalMemoryAvailableWithoutSystemResource() const {
|
|
u64 Process::GetTotalPhysicalMemoryAvailableWithoutSystemResource() const {
|
|
@@ -138,8 +146,7 @@ u64 Process::GetTotalPhysicalMemoryAvailableWithoutSystemResource() const {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
u64 Process::GetTotalPhysicalMemoryUsed() const {
|
|
u64 Process::GetTotalPhysicalMemoryUsed() const {
|
|
|
- return vm_manager.GetCurrentHeapSize() + main_thread_stack_size + code_memory_size +
|
|
|
|
|
- GetSystemResourceUsage();
|
|
|
|
|
|
|
+ return image_size + main_thread_stack_size + page_table->GetTotalHeapSize();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
u64 Process::GetTotalPhysicalMemoryUsedWithoutSystemResource() const {
|
|
u64 Process::GetTotalPhysicalMemoryUsedWithoutSystemResource() const {
|
|
@@ -212,33 +219,82 @@ ResultCode Process::ClearSignalState() {
|
|
|
return RESULT_SUCCESS;
|
|
return RESULT_SUCCESS;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-ResultCode Process::LoadFromMetadata(const FileSys::ProgramMetadata& metadata) {
|
|
|
|
|
|
|
+ResultCode Process::LoadFromMetadata(const FileSys::ProgramMetadata& metadata,
|
|
|
|
|
+ std::size_t code_size) {
|
|
|
program_id = metadata.GetTitleID();
|
|
program_id = metadata.GetTitleID();
|
|
|
ideal_core = metadata.GetMainThreadCore();
|
|
ideal_core = metadata.GetMainThreadCore();
|
|
|
is_64bit_process = metadata.Is64BitProgram();
|
|
is_64bit_process = metadata.Is64BitProgram();
|
|
|
system_resource_size = metadata.GetSystemResourceSize();
|
|
system_resource_size = metadata.GetSystemResourceSize();
|
|
|
|
|
+ image_size = code_size;
|
|
|
|
|
+
|
|
|
|
|
+ // Initialize proces address space
|
|
|
|
|
+ if (const ResultCode result{
|
|
|
|
|
+ page_table->InitializeForProcess(metadata.GetAddressSpaceType(), false, 0x8000000,
|
|
|
|
|
+ code_size, Memory::MemoryManager::Pool::Application)};
|
|
|
|
|
+ result.IsError()) {
|
|
|
|
|
+ return result;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- vm_manager.Reset(metadata.GetAddressSpaceType());
|
|
|
|
|
|
|
+ // Map process code region
|
|
|
|
|
+ if (const ResultCode result{page_table->MapProcessCode(
|
|
|
|
|
+ page_table->GetCodeRegionStart(), code_size / Memory::PageSize,
|
|
|
|
|
+ Memory::MemoryState::Code, Memory::MemoryPermission::None)};
|
|
|
|
|
+ result.IsError()) {
|
|
|
|
|
+ return result;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- const auto& caps = metadata.GetKernelCapabilities();
|
|
|
|
|
- const auto capability_init_result =
|
|
|
|
|
- capabilities.InitializeForUserProcess(caps.data(), caps.size(), vm_manager);
|
|
|
|
|
- if (capability_init_result.IsError()) {
|
|
|
|
|
- return capability_init_result;
|
|
|
|
|
|
|
+ // Initialize process capabilities
|
|
|
|
|
+ const auto& caps{metadata.GetKernelCapabilities()};
|
|
|
|
|
+ if (const ResultCode result{
|
|
|
|
|
+ capabilities.InitializeForUserProcess(caps.data(), caps.size(), *page_table)};
|
|
|
|
|
+ result.IsError()) {
|
|
|
|
|
+ return result;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ // Set memory usage capacity
|
|
|
|
|
+ switch (metadata.GetAddressSpaceType()) {
|
|
|
|
|
+ case FileSys::ProgramAddressSpaceType::Is32Bit:
|
|
|
|
|
+ case FileSys::ProgramAddressSpaceType::Is36Bit:
|
|
|
|
|
+ case FileSys::ProgramAddressSpaceType::Is39Bit:
|
|
|
|
|
+ memory_usage_capacity = page_table->GetHeapRegionEnd() - page_table->GetHeapRegionStart();
|
|
|
|
|
+ break;
|
|
|
|
|
+
|
|
|
|
|
+ case FileSys::ProgramAddressSpaceType::Is32BitNoMap:
|
|
|
|
|
+ memory_usage_capacity = page_table->GetHeapRegionEnd() - page_table->GetHeapRegionStart() +
|
|
|
|
|
+ page_table->GetAliasRegionEnd() - page_table->GetAliasRegionStart();
|
|
|
|
|
+ break;
|
|
|
|
|
+
|
|
|
|
|
+ default:
|
|
|
|
|
+ UNREACHABLE();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // Set initial resource limits
|
|
|
|
|
+ resource_limit->SetLimitValue(
|
|
|
|
|
+ ResourceType::PhysicalMemory,
|
|
|
|
|
+ kernel.MemoryManager().GetSize(Memory::MemoryManager::Pool::Application));
|
|
|
|
|
+ resource_limit->SetLimitValue(ResourceType::Threads, 608);
|
|
|
|
|
+ resource_limit->SetLimitValue(ResourceType::Events, 700);
|
|
|
|
|
+ resource_limit->SetLimitValue(ResourceType::TransferMemory, 128);
|
|
|
|
|
+ resource_limit->SetLimitValue(ResourceType::Sessions, 894);
|
|
|
|
|
+ ASSERT(resource_limit->Reserve(ResourceType::PhysicalMemory, code_size));
|
|
|
|
|
+
|
|
|
|
|
+ // Create TLS region
|
|
|
|
|
+ tls_region_address = CreateTLSRegion();
|
|
|
|
|
+
|
|
|
return handle_table.SetSize(capabilities.GetHandleTableSize());
|
|
return handle_table.SetSize(capabilities.GetHandleTableSize());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void Process::Run(s32 main_thread_priority, u64 stack_size) {
|
|
void Process::Run(s32 main_thread_priority, u64 stack_size) {
|
|
|
AllocateMainThreadStack(stack_size);
|
|
AllocateMainThreadStack(stack_size);
|
|
|
- tls_region_address = CreateTLSRegion();
|
|
|
|
|
|
|
|
|
|
- vm_manager.LogLayout();
|
|
|
|
|
|
|
+ const std::size_t heap_capacity{memory_usage_capacity - main_thread_stack_size - image_size};
|
|
|
|
|
+ ASSERT(!page_table->SetHeapCapacity(heap_capacity).IsError());
|
|
|
|
|
|
|
|
ChangeStatus(ProcessStatus::Running);
|
|
ChangeStatus(ProcessStatus::Running);
|
|
|
|
|
|
|
|
- SetupMainThread(*this, kernel, main_thread_priority);
|
|
|
|
|
|
|
+ SetupMainThread(*this, kernel, main_thread_priority, main_thread_stack_top);
|
|
|
|
|
+ resource_limit->Reserve(ResourceType::Threads, 1);
|
|
|
|
|
+ resource_limit->Reserve(ResourceType::PhysicalMemory, main_thread_stack_size);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void Process::PrepareForTermination() {
|
|
void Process::PrepareForTermination() {
|
|
@@ -282,28 +338,33 @@ static auto FindTLSPageWithAvailableSlots(std::vector<TLSPage>& tls_pages) {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
VAddr Process::CreateTLSRegion() {
|
|
VAddr Process::CreateTLSRegion() {
|
|
|
- auto tls_page_iter = FindTLSPageWithAvailableSlots(tls_pages);
|
|
|
|
|
|
|
+ if (auto tls_page_iter{FindTLSPageWithAvailableSlots(tls_pages)};
|
|
|
|
|
+ tls_page_iter != tls_pages.cend()) {
|
|
|
|
|
+ return *tls_page_iter->ReserveSlot();
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- if (tls_page_iter == tls_pages.cend()) {
|
|
|
|
|
- const auto region_address =
|
|
|
|
|
- vm_manager.FindFreeRegion(vm_manager.GetTLSIORegionBaseAddress(),
|
|
|
|
|
- vm_manager.GetTLSIORegionEndAddress(), Memory::PAGE_SIZE);
|
|
|
|
|
- ASSERT(region_address.Succeeded());
|
|
|
|
|
|
|
+ Memory::Page* const tls_page_ptr{kernel.GetUserSlabHeapPages().Allocate()};
|
|
|
|
|
+ ASSERT(tls_page_ptr);
|
|
|
|
|
|
|
|
- const auto map_result = vm_manager.MapMemoryBlock(
|
|
|
|
|
- *region_address, std::make_shared<PhysicalMemory>(Memory::PAGE_SIZE), 0,
|
|
|
|
|
- Memory::PAGE_SIZE, MemoryState::ThreadLocal);
|
|
|
|
|
- ASSERT(map_result.Succeeded());
|
|
|
|
|
|
|
+ const VAddr start{page_table->GetKernelMapRegionStart()};
|
|
|
|
|
+ const VAddr size{page_table->GetKernelMapRegionEnd() - start};
|
|
|
|
|
+ const PAddr tls_map_addr{system.DeviceMemory().GetPhysicalAddr(tls_page_ptr)};
|
|
|
|
|
+ const VAddr tls_page_addr{
|
|
|
|
|
+ page_table
|
|
|
|
|
+ ->AllocateAndMapMemory(1, Memory::PageSize, true, start, size / Memory::PageSize,
|
|
|
|
|
+ Memory::MemoryState::ThreadLocal,
|
|
|
|
|
+ Memory::MemoryPermission::ReadAndWrite, tls_map_addr)
|
|
|
|
|
+ .ValueOr(0)};
|
|
|
|
|
|
|
|
- tls_pages.emplace_back(*region_address);
|
|
|
|
|
|
|
+ ASSERT(tls_page_addr);
|
|
|
|
|
|
|
|
- const auto reserve_result = tls_pages.back().ReserveSlot();
|
|
|
|
|
- ASSERT(reserve_result.has_value());
|
|
|
|
|
|
|
+ std::memset(tls_page_ptr, 0, Memory::PageSize);
|
|
|
|
|
+ tls_pages.emplace_back(tls_page_addr);
|
|
|
|
|
|
|
|
- return *reserve_result;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ const auto reserve_result{tls_pages.back().ReserveSlot()};
|
|
|
|
|
+ ASSERT(reserve_result.has_value());
|
|
|
|
|
|
|
|
- return *tls_page_iter->ReserveSlot();
|
|
|
|
|
|
|
+ return *reserve_result;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void Process::FreeTLSRegion(VAddr tls_address) {
|
|
void Process::FreeTLSRegion(VAddr tls_address) {
|
|
@@ -320,28 +381,22 @@ void Process::FreeTLSRegion(VAddr tls_address) {
|
|
|
iter->ReleaseSlot(tls_address);
|
|
iter->ReleaseSlot(tls_address);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void Process::LoadModule(CodeSet module_, VAddr base_addr) {
|
|
|
|
|
- code_memory_size += module_.memory.size();
|
|
|
|
|
-
|
|
|
|
|
- const auto memory = std::make_shared<PhysicalMemory>(std::move(module_.memory));
|
|
|
|
|
-
|
|
|
|
|
- const auto MapSegment = [&](const CodeSet::Segment& segment, VMAPermission permissions,
|
|
|
|
|
- MemoryState memory_state) {
|
|
|
|
|
- const auto vma = vm_manager
|
|
|
|
|
- .MapMemoryBlock(segment.addr + base_addr, memory, segment.offset,
|
|
|
|
|
- segment.size, memory_state)
|
|
|
|
|
- .Unwrap();
|
|
|
|
|
- vm_manager.Reprotect(vma, permissions);
|
|
|
|
|
|
|
+void Process::LoadModule(CodeSet code_set, VAddr base_addr) {
|
|
|
|
|
+ const auto ReprotectSegment = [&](const CodeSet::Segment& segment,
|
|
|
|
|
+ Memory::MemoryPermission permission) {
|
|
|
|
|
+ page_table->SetCodeMemoryPermission(segment.addr + base_addr, segment.size, permission);
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
- // Map CodeSet segments
|
|
|
|
|
- MapSegment(module_.CodeSegment(), VMAPermission::ReadExecute, MemoryState::Code);
|
|
|
|
|
- MapSegment(module_.RODataSegment(), VMAPermission::Read, MemoryState::CodeData);
|
|
|
|
|
- MapSegment(module_.DataSegment(), VMAPermission::ReadWrite, MemoryState::CodeData);
|
|
|
|
|
|
|
+ system.Memory().WriteBlock(*this, base_addr, code_set.memory.data(), code_set.memory.size());
|
|
|
|
|
+
|
|
|
|
|
+ ReprotectSegment(code_set.CodeSegment(), Memory::MemoryPermission::ReadAndExecute);
|
|
|
|
|
+ ReprotectSegment(code_set.RODataSegment(), Memory::MemoryPermission::Read);
|
|
|
|
|
+ ReprotectSegment(code_set.DataSegment(), Memory::MemoryPermission::ReadAndWrite);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
Process::Process(Core::System& system)
|
|
Process::Process(Core::System& system)
|
|
|
- : SynchronizationObject{system.Kernel()}, vm_manager{system},
|
|
|
|
|
|
|
+ : SynchronizationObject{system.Kernel()}, page_table{std::make_unique<Memory::PageTable>(
|
|
|
|
|
+ system)},
|
|
|
address_arbiter{system}, mutex{system}, system{system} {}
|
|
address_arbiter{system}, mutex{system}, system{system} {}
|
|
|
|
|
|
|
|
Process::~Process() = default;
|
|
Process::~Process() = default;
|
|
@@ -364,16 +419,24 @@ void Process::ChangeStatus(ProcessStatus new_status) {
|
|
|
Signal();
|
|
Signal();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void Process::AllocateMainThreadStack(u64 stack_size) {
|
|
|
|
|
|
|
+ResultCode Process::AllocateMainThreadStack(std::size_t stack_size) {
|
|
|
|
|
+ ASSERT(stack_size);
|
|
|
|
|
+
|
|
|
// The kernel always ensures that the given stack size is page aligned.
|
|
// The kernel always ensures that the given stack size is page aligned.
|
|
|
- main_thread_stack_size = Common::AlignUp(stack_size, Memory::PAGE_SIZE);
|
|
|
|
|
-
|
|
|
|
|
- // Allocate and map the main thread stack
|
|
|
|
|
- const VAddr mapping_address = vm_manager.GetTLSIORegionEndAddress() - main_thread_stack_size;
|
|
|
|
|
- vm_manager
|
|
|
|
|
- .MapMemoryBlock(mapping_address, std::make_shared<PhysicalMemory>(main_thread_stack_size),
|
|
|
|
|
- 0, main_thread_stack_size, MemoryState::Stack)
|
|
|
|
|
- .Unwrap();
|
|
|
|
|
|
|
+ main_thread_stack_size = Common::AlignUp(stack_size, Memory::PageSize);
|
|
|
|
|
+
|
|
|
|
|
+ const VAddr start{page_table->GetStackRegionStart()};
|
|
|
|
|
+ const std::size_t size{page_table->GetStackRegionEnd() - start};
|
|
|
|
|
+
|
|
|
|
|
+ CASCADE_RESULT(main_thread_stack_top,
|
|
|
|
|
+ page_table->AllocateAndMapMemory(
|
|
|
|
|
+ main_thread_stack_size / Memory::PageSize, Memory::PageSize, false, start,
|
|
|
|
|
+ size / Memory::PageSize, Memory::MemoryState::Stack,
|
|
|
|
|
+ Memory::MemoryPermission::ReadAndWrite));
|
|
|
|
|
+
|
|
|
|
|
+ main_thread_stack_top += main_thread_stack_size;
|
|
|
|
|
+
|
|
|
|
|
+ return RESULT_SUCCESS;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
} // namespace Kernel
|
|
} // namespace Kernel
|