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