kernel: process: Updates for new VMM.

This commit is contained in:
bunnei 2020-04-08 22:19:12 -04:00
parent 77382ac2b2
commit 108564df57
2 changed files with 156 additions and 84 deletions

View file

@ -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) { void SetupMainThread(Process& owner_process, KernelCore& kernel, u32 priority, VAddr stack_top) {
const auto& vm_manager = owner_process.VMManager(); const VAddr entry_point = owner_process.PageTable().GetCodeRegionStart();
const VAddr entry_point = vm_manager.GetCodeRegionBaseAddress();
const VAddr stack_top = vm_manager.GetTLSIORegionEndAddress();
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 + return image_size + main_thread_stack_size + page_table->GetTotalHeapSize();
GetSystemResourceUsage();
} }
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;
vm_manager.Reset(metadata.GetAddressSpaceType()); // Initialize proces address space
if (const ResultCode result{
const auto& caps = metadata.GetKernelCapabilities(); page_table->InitializeForProcess(metadata.GetAddressSpaceType(), false, 0x8000000,
const auto capability_init_result = code_size, Memory::MemoryManager::Pool::Application)};
capabilities.InitializeForUserProcess(caps.data(), caps.size(), vm_manager); result.IsError()) {
if (capability_init_result.IsError()) { return result;
return capability_init_result;
} }
// 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;
}
// 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,30 +338,35 @@ 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()) { Memory::Page* const tls_page_ptr{kernel.GetUserSlabHeapPages().Allocate()};
const auto region_address = ASSERT(tls_page_ptr);
vm_manager.FindFreeRegion(vm_manager.GetTLSIORegionBaseAddress(),
vm_manager.GetTLSIORegionEndAddress(), Memory::PAGE_SIZE);
ASSERT(region_address.Succeeded());
const auto map_result = vm_manager.MapMemoryBlock( const VAddr start{page_table->GetKernelMapRegionStart()};
*region_address, std::make_shared<PhysicalMemory>(Memory::PAGE_SIZE), 0, const VAddr size{page_table->GetKernelMapRegionEnd() - start};
Memory::PAGE_SIZE, MemoryState::ThreadLocal); const PAddr tls_map_addr{system.DeviceMemory().GetPhysicalAddr(tls_page_ptr)};
ASSERT(map_result.Succeeded()); 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(); std::memset(tls_page_ptr, 0, Memory::PageSize);
tls_pages.emplace_back(tls_page_addr);
const auto reserve_result{tls_pages.back().ReserveSlot()};
ASSERT(reserve_result.has_value()); ASSERT(reserve_result.has_value());
return *reserve_result; return *reserve_result;
} }
return *tls_page_iter->ReserveSlot();
}
void Process::FreeTLSRegion(VAddr tls_address) { void Process::FreeTLSRegion(VAddr tls_address) {
const VAddr aligned_address = Common::AlignDown(tls_address, Core::Memory::PAGE_SIZE); const VAddr aligned_address = Common::AlignDown(tls_address, Core::Memory::PAGE_SIZE);
auto iter = auto iter =
@ -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) { void Process::LoadModule(CodeSet code_set, VAddr base_addr) {
code_memory_size += module_.memory.size(); const auto ReprotectSegment = [&](const CodeSet::Segment& segment,
Memory::MemoryPermission permission) {
const auto memory = std::make_shared<PhysicalMemory>(std::move(module_.memory)); page_table->SetCodeMemoryPermission(segment.addr + base_addr, segment.size, permission);
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);
}; };
// Map CodeSet segments system.Memory().WriteBlock(*this, base_addr, code_set.memory.data(), code_set.memory.size());
MapSegment(module_.CodeSegment(), VMAPermission::ReadExecute, MemoryState::Code);
MapSegment(module_.RODataSegment(), VMAPermission::Read, MemoryState::CodeData); ReprotectSegment(code_set.CodeSegment(), Memory::MemoryPermission::ReadAndExecute);
MapSegment(module_.DataSegment(), VMAPermission::ReadWrite, MemoryState::CodeData); 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) {
// The kernel always ensures that the given stack size is page aligned. ASSERT(stack_size);
main_thread_stack_size = Common::AlignUp(stack_size, Memory::PAGE_SIZE);
// Allocate and map the main thread stack // The kernel always ensures that the given stack size is page aligned.
const VAddr mapping_address = vm_manager.GetTLSIORegionEndAddress() - main_thread_stack_size; main_thread_stack_size = Common::AlignUp(stack_size, Memory::PageSize);
vm_manager
.MapMemoryBlock(mapping_address, std::make_shared<PhysicalMemory>(main_thread_stack_size), const VAddr start{page_table->GetStackRegionStart()};
0, main_thread_stack_size, MemoryState::Stack) const std::size_t size{page_table->GetStackRegionEnd() - start};
.Unwrap();
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

View file

@ -16,7 +16,6 @@
#include "core/hle/kernel/mutex.h" #include "core/hle/kernel/mutex.h"
#include "core/hle/kernel/process_capability.h" #include "core/hle/kernel/process_capability.h"
#include "core/hle/kernel/synchronization_object.h" #include "core/hle/kernel/synchronization_object.h"
#include "core/hle/kernel/vm_manager.h"
#include "core/hle/result.h" #include "core/hle/result.h"
namespace Core { namespace Core {
@ -36,6 +35,10 @@ class TLSPage;
struct CodeSet; struct CodeSet;
namespace Memory {
class PageTable;
}
enum class MemoryRegion : u16 { enum class MemoryRegion : u16 {
APPLICATION = 1, APPLICATION = 1,
SYSTEM = 2, SYSTEM = 2,
@ -100,14 +103,14 @@ public:
return HANDLE_TYPE; return HANDLE_TYPE;
} }
/// Gets a reference to the process' memory manager. /// Gets a reference to the process' page table.
Kernel::VMManager& VMManager() { Memory::PageTable& PageTable() {
return vm_manager; return *page_table;
} }
/// Gets a const reference to the process' memory manager. /// Gets const a reference to the process' page table.
const Kernel::VMManager& VMManager() const { const Memory::PageTable& PageTable() const {
return vm_manager; return *page_table;
} }
/// Gets a reference to the process' handle table. /// Gets a reference to the process' handle table.
@ -273,7 +276,7 @@ public:
* @returns RESULT_SUCCESS if all relevant metadata was able to be * @returns RESULT_SUCCESS if all relevant metadata was able to be
* loaded and parsed. Otherwise, an error code is returned. * loaded and parsed. Otherwise, an error code is returned.
*/ */
ResultCode LoadFromMetadata(const FileSys::ProgramMetadata& metadata); ResultCode LoadFromMetadata(const FileSys::ProgramMetadata& metadata, std::size_t code_size);
/** /**
* Starts the main application thread for this process. * Starts the main application thread for this process.
@ -289,7 +292,7 @@ public:
*/ */
void PrepareForTermination(); void PrepareForTermination();
void LoadModule(CodeSet module_, VAddr base_addr); void LoadModule(CodeSet code_set, VAddr base_addr);
/////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////
// Thread-local storage management // Thread-local storage management
@ -313,16 +316,10 @@ private:
void ChangeStatus(ProcessStatus new_status); void ChangeStatus(ProcessStatus new_status);
/// Allocates the main thread stack for the process, given the stack size in bytes. /// Allocates the main thread stack for the process, given the stack size in bytes.
void AllocateMainThreadStack(u64 stack_size); ResultCode AllocateMainThreadStack(std::size_t stack_size);
/// Memory manager for this process. /// Memory manager for this process
Kernel::VMManager vm_manager; std::unique_ptr<Memory::PageTable> page_table;
/// Size of the main thread's stack in bytes.
u64 main_thread_stack_size = 0;
/// Size of the loaded code memory in bytes.
u64 code_memory_size = 0;
/// Current status of the process /// Current status of the process
ProcessStatus status{}; ProcessStatus status{};
@ -390,6 +387,18 @@ private:
/// Name of this process /// Name of this process
std::string name; std::string name;
/// Address of the top of the main thread's stack
VAddr main_thread_stack_top{};
/// Size of the main thread's stack
std::size_t main_thread_stack_size{};
/// Memory usage capacity for the process
std::size_t memory_usage_capacity{};
/// Process total image size
std::size_t image_size{};
}; };
} // namespace Kernel } // namespace Kernel