|
|
|
@ -72,6 +72,7 @@ Result KProcess::Initialize(KProcess* process, Core::System& system, std::string |
|
|
|
|
|
|
|
process->name = std::move(process_name); |
|
|
|
process->resource_limit = res_limit; |
|
|
|
process->system_resource_address = 0; |
|
|
|
process->state = State::Created; |
|
|
|
process->program_id = 0; |
|
|
|
process->process_id = type == ProcessType::KernelInternal ? kernel.CreateNewKernelProcessID() |
|
|
|
@ -92,6 +93,7 @@ Result KProcess::Initialize(KProcess* process, Core::System& system, std::string |
|
|
|
process->exception_thread = nullptr; |
|
|
|
process->is_suspended = false; |
|
|
|
process->schedule_count = 0; |
|
|
|
process->is_handle_table_initialized = false; |
|
|
|
|
|
|
|
// Open a reference to the resource limit.
|
|
|
|
process->resource_limit->Open(); |
|
|
|
@ -121,9 +123,9 @@ void KProcess::DecrementRunningThreadCount() { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
u64 KProcess::GetTotalPhysicalMemoryAvailable() const { |
|
|
|
u64 KProcess::GetTotalPhysicalMemoryAvailable() { |
|
|
|
const u64 capacity{resource_limit->GetFreeValue(LimitableResource::PhysicalMemory) + |
|
|
|
page_table->GetNormalMemorySize() + GetSystemResourceSize() + image_size + |
|
|
|
page_table.GetNormalMemorySize() + GetSystemResourceSize() + image_size + |
|
|
|
main_thread_stack_size}; |
|
|
|
if (const auto pool_size = kernel.MemoryManager().GetSize(KMemoryManager::Pool::Application); |
|
|
|
capacity != pool_size) { |
|
|
|
@ -135,16 +137,16 @@ u64 KProcess::GetTotalPhysicalMemoryAvailable() const { |
|
|
|
return memory_usage_capacity; |
|
|
|
} |
|
|
|
|
|
|
|
u64 KProcess::GetTotalPhysicalMemoryAvailableWithoutSystemResource() const { |
|
|
|
u64 KProcess::GetTotalPhysicalMemoryAvailableWithoutSystemResource() { |
|
|
|
return GetTotalPhysicalMemoryAvailable() - GetSystemResourceSize(); |
|
|
|
} |
|
|
|
|
|
|
|
u64 KProcess::GetTotalPhysicalMemoryUsed() const { |
|
|
|
return image_size + main_thread_stack_size + page_table->GetNormalMemorySize() + |
|
|
|
u64 KProcess::GetTotalPhysicalMemoryUsed() { |
|
|
|
return image_size + main_thread_stack_size + page_table.GetNormalMemorySize() + |
|
|
|
GetSystemResourceSize(); |
|
|
|
} |
|
|
|
|
|
|
|
u64 KProcess::GetTotalPhysicalMemoryUsedWithoutSystemResource() const { |
|
|
|
u64 KProcess::GetTotalPhysicalMemoryUsedWithoutSystemResource() { |
|
|
|
return GetTotalPhysicalMemoryUsed() - GetSystemResourceUsage(); |
|
|
|
} |
|
|
|
|
|
|
|
@ -348,6 +350,9 @@ Result KProcess::LoadFromMetadata(const FileSys::ProgramMetadata& metadata, std: |
|
|
|
system_resource_size = metadata.GetSystemResourceSize(); |
|
|
|
image_size = code_size; |
|
|
|
|
|
|
|
// We currently do not support process-specific system resource
|
|
|
|
UNIMPLEMENTED_IF(system_resource_size != 0); |
|
|
|
|
|
|
|
KScopedResourceReservation memory_reservation(resource_limit, LimitableResource::PhysicalMemory, |
|
|
|
code_size + system_resource_size); |
|
|
|
if (!memory_reservation.Succeeded()) { |
|
|
|
@ -356,7 +361,7 @@ Result KProcess::LoadFromMetadata(const FileSys::ProgramMetadata& metadata, std: |
|
|
|
return ResultLimitReached; |
|
|
|
} |
|
|
|
// Initialize proces address space
|
|
|
|
if (const Result result{page_table->InitializeForProcess( |
|
|
|
if (const Result result{page_table.InitializeForProcess( |
|
|
|
metadata.GetAddressSpaceType(), false, 0x8000000, code_size, |
|
|
|
&kernel.GetApplicationMemoryBlockManager(), KMemoryManager::Pool::Application)}; |
|
|
|
result.IsError()) { |
|
|
|
@ -364,9 +369,9 @@ Result KProcess::LoadFromMetadata(const FileSys::ProgramMetadata& metadata, std: |
|
|
|
} |
|
|
|
|
|
|
|
// Map process code region
|
|
|
|
if (const Result result{page_table->MapProcessCode(page_table->GetCodeRegionStart(), |
|
|
|
code_size / PageSize, KMemoryState::Code, |
|
|
|
KMemoryPermission::None)}; |
|
|
|
if (const Result result{page_table.MapProcessCode(page_table.GetCodeRegionStart(), |
|
|
|
code_size / PageSize, KMemoryState::Code, |
|
|
|
KMemoryPermission::None)}; |
|
|
|
result.IsError()) { |
|
|
|
return result; |
|
|
|
} |
|
|
|
@ -374,7 +379,7 @@ Result KProcess::LoadFromMetadata(const FileSys::ProgramMetadata& metadata, std: |
|
|
|
// Initialize process capabilities
|
|
|
|
const auto& caps{metadata.GetKernelCapabilities()}; |
|
|
|
if (const Result result{ |
|
|
|
capabilities.InitializeForUserProcess(caps.data(), caps.size(), *page_table)}; |
|
|
|
capabilities.InitializeForUserProcess(caps.data(), caps.size(), page_table)}; |
|
|
|
result.IsError()) { |
|
|
|
return result; |
|
|
|
} |
|
|
|
@ -384,12 +389,12 @@ Result KProcess::LoadFromMetadata(const FileSys::ProgramMetadata& metadata, std: |
|
|
|
case FileSys::ProgramAddressSpaceType::Is32Bit: |
|
|
|
case FileSys::ProgramAddressSpaceType::Is36Bit: |
|
|
|
case FileSys::ProgramAddressSpaceType::Is39Bit: |
|
|
|
memory_usage_capacity = page_table->GetHeapRegionEnd() - page_table->GetHeapRegionStart(); |
|
|
|
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(); |
|
|
|
memory_usage_capacity = page_table.GetHeapRegionEnd() - page_table.GetHeapRegionStart() + |
|
|
|
page_table.GetAliasRegionEnd() - page_table.GetAliasRegionStart(); |
|
|
|
break; |
|
|
|
|
|
|
|
default: |
|
|
|
@ -397,7 +402,7 @@ Result KProcess::LoadFromMetadata(const FileSys::ProgramMetadata& metadata, std: |
|
|
|
} |
|
|
|
|
|
|
|
// Create TLS region
|
|
|
|
R_TRY(this->CreateThreadLocalRegion(std::addressof(tls_region_address))); |
|
|
|
R_TRY(this->CreateThreadLocalRegion(std::addressof(plr_address))); |
|
|
|
memory_reservation.Commit(); |
|
|
|
|
|
|
|
return handle_table.Initialize(capabilities.GetHandleTableSize()); |
|
|
|
@ -409,7 +414,7 @@ void KProcess::Run(s32 main_thread_priority, u64 stack_size) { |
|
|
|
resource_limit->Reserve(LimitableResource::PhysicalMemory, main_thread_stack_size); |
|
|
|
|
|
|
|
const std::size_t heap_capacity{memory_usage_capacity - (main_thread_stack_size + image_size)}; |
|
|
|
ASSERT(!page_table->SetMaxHeapSize(heap_capacity).IsError()); |
|
|
|
ASSERT(!page_table.SetMaxHeapSize(heap_capacity).IsError()); |
|
|
|
|
|
|
|
ChangeState(State::Running); |
|
|
|
|
|
|
|
@ -437,8 +442,8 @@ void KProcess::PrepareForTermination() { |
|
|
|
|
|
|
|
stop_threads(kernel.System().GlobalSchedulerContext().GetThreadList()); |
|
|
|
|
|
|
|
this->DeleteThreadLocalRegion(tls_region_address); |
|
|
|
tls_region_address = 0; |
|
|
|
this->DeleteThreadLocalRegion(plr_address); |
|
|
|
plr_address = 0; |
|
|
|
|
|
|
|
if (resource_limit) { |
|
|
|
resource_limit->Release(LimitableResource::PhysicalMemory, |
|
|
|
@ -474,7 +479,7 @@ void KProcess::Finalize() { |
|
|
|
} |
|
|
|
|
|
|
|
// Finalize the page table.
|
|
|
|
page_table.reset(); |
|
|
|
page_table.Finalize(); |
|
|
|
|
|
|
|
// Perform inherited finalization.
|
|
|
|
KAutoObjectWithSlabHeapAndContainer<KProcess, KWorkerTask>::Finalize(); |
|
|
|
@ -628,7 +633,7 @@ bool KProcess::RemoveWatchpoint(Core::System& system, VAddr addr, u64 size, |
|
|
|
void KProcess::LoadModule(CodeSet code_set, VAddr base_addr) { |
|
|
|
const auto ReprotectSegment = [&](const CodeSet::Segment& segment, |
|
|
|
Svc::MemoryPermission permission) { |
|
|
|
page_table->SetProcessMemoryPermission(segment.addr + base_addr, segment.size, permission); |
|
|
|
page_table.SetProcessMemoryPermission(segment.addr + base_addr, segment.size, permission); |
|
|
|
}; |
|
|
|
|
|
|
|
kernel.System().Memory().WriteBlock(*this, base_addr, code_set.memory.data(), |
|
|
|
@ -645,8 +650,7 @@ bool KProcess::IsSignaled() const { |
|
|
|
} |
|
|
|
|
|
|
|
KProcess::KProcess(KernelCore& kernel_) |
|
|
|
: KAutoObjectWithSlabHeapAndContainer{kernel_}, page_table{std::make_unique<KPageTable>( |
|
|
|
kernel_.System())}, |
|
|
|
: KAutoObjectWithSlabHeapAndContainer{kernel_}, page_table{kernel_.System()}, |
|
|
|
handle_table{kernel_}, address_arbiter{kernel_.System()}, condition_var{kernel_.System()}, |
|
|
|
state_lock{kernel_}, list_lock{kernel_} {} |
|
|
|
|
|
|
|
@ -668,11 +672,11 @@ Result KProcess::AllocateMainThreadStack(std::size_t stack_size) { |
|
|
|
// The kernel always ensures that the given stack size is page aligned.
|
|
|
|
main_thread_stack_size = Common::AlignUp(stack_size, PageSize); |
|
|
|
|
|
|
|
const VAddr start{page_table->GetStackRegionStart()}; |
|
|
|
const std::size_t size{page_table->GetStackRegionEnd() - start}; |
|
|
|
const VAddr start{page_table.GetStackRegionStart()}; |
|
|
|
const std::size_t size{page_table.GetStackRegionEnd() - start}; |
|
|
|
|
|
|
|
CASCADE_RESULT(main_thread_stack_top, |
|
|
|
page_table->AllocateAndMapMemory( |
|
|
|
page_table.AllocateAndMapMemory( |
|
|
|
main_thread_stack_size / PageSize, PageSize, false, start, size / PageSize, |
|
|
|
KMemoryState::Stack, KMemoryPermission::UserReadWrite)); |
|
|
|
|
|
|
|
@ -681,4 +685,12 @@ Result KProcess::AllocateMainThreadStack(std::size_t stack_size) { |
|
|
|
return ResultSuccess; |
|
|
|
} |
|
|
|
|
|
|
|
void KProcess::FinalizeHandleTable() { |
|
|
|
// Finalize the table.
|
|
|
|
handle_table.Finalize(); |
|
|
|
|
|
|
|
// Note that the table is finalized.
|
|
|
|
is_handle_table_initialized = false; |
|
|
|
} |
|
|
|
|
|
|
|
} // namespace Kernel
|