yuzu-emu
/
yuzu-mainline
Archived
1
0
Fork 0

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/logging/log.h"
#include "core/core.h"
#include "core/device_memory.h"
#include "core/file_sys/program_metadata.h"
#include "core/hle/kernel/code_set.h"
#include "core/hle/kernel/errors.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/resource_limit.h"
#include "core/hle/kernel/scheduler.h"
#include "core/hle/kernel/thread.h"
#include "core/hle/kernel/vm_manager.h"
#include "core/memory.h"
#include "core/settings.h"
@ -31,10 +34,8 @@ namespace {
* @param kernel The kernel instance to create the main thread under.
* @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,
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);
process->name = std::move(name);
process->resource_limit = kernel.GetSystemResourceLimit();
process->resource_limit = ResourceLimit::Create(kernel);
process->status = ProcessStatus::Created;
process->program_id = 0;
process->process_id = type == ProcessType::KernelInternal ? kernel.CreateNewKernelProcessID()
@ -130,7 +131,14 @@ std::shared_ptr<ResourceLimit> Process::GetResourceLimit() 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 {
@ -138,8 +146,7 @@ u64 Process::GetTotalPhysicalMemoryAvailableWithoutSystemResource() 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 {
@ -212,33 +219,82 @@ ResultCode Process::ClearSignalState() {
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();
ideal_core = metadata.GetMainThreadCore();
is_64bit_process = metadata.Is64BitProgram();
system_resource_size = metadata.GetSystemResourceSize();
image_size = code_size;
vm_manager.Reset(metadata.GetAddressSpaceType());
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 proces address space
if (const ResultCode result{
page_table->InitializeForProcess(metadata.GetAddressSpaceType(), false, 0x8000000,
code_size, Memory::MemoryManager::Pool::Application)};
result.IsError()) {
return 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());
}
void Process::Run(s32 main_thread_priority, u64 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);
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() {
@ -282,28 +338,33 @@ static auto FindTLSPageWithAvailableSlots(std::vector<TLSPage>& tls_pages) {
}
VAddr Process::CreateTLSRegion() {
auto tls_page_iter = FindTLSPageWithAvailableSlots(tls_pages);
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());
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());
tls_pages.emplace_back(*region_address);
const auto reserve_result = tls_pages.back().ReserveSlot();
ASSERT(reserve_result.has_value());
return *reserve_result;
if (auto tls_page_iter{FindTLSPageWithAvailableSlots(tls_pages)};
tls_page_iter != tls_pages.cend()) {
return *tls_page_iter->ReserveSlot();
}
return *tls_page_iter->ReserveSlot();
Memory::Page* const tls_page_ptr{kernel.GetUserSlabHeapPages().Allocate()};
ASSERT(tls_page_ptr);
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)};
ASSERT(tls_page_addr);
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());
return *reserve_result;
}
void Process::FreeTLSRegion(VAddr tls_address) {
@ -320,28 +381,22 @@ void Process::FreeTLSRegion(VAddr 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)
: SynchronizationObject{system.Kernel()}, vm_manager{system},
: SynchronizationObject{system.Kernel()}, page_table{std::make_unique<Memory::PageTable>(
system)},
address_arbiter{system}, mutex{system}, system{system} {}
Process::~Process() = default;
@ -364,16 +419,24 @@ void Process::ChangeStatus(ProcessStatus new_status) {
Signal();
}
void Process::AllocateMainThreadStack(u64 stack_size) {
// The kernel always ensures that the given stack size is page aligned.
main_thread_stack_size = Common::AlignUp(stack_size, Memory::PAGE_SIZE);
ResultCode Process::AllocateMainThreadStack(std::size_t stack_size) {
ASSERT(stack_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();
// The kernel always ensures that the given stack size is page aligned.
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

View File

@ -16,7 +16,6 @@
#include "core/hle/kernel/mutex.h"
#include "core/hle/kernel/process_capability.h"
#include "core/hle/kernel/synchronization_object.h"
#include "core/hle/kernel/vm_manager.h"
#include "core/hle/result.h"
namespace Core {
@ -36,6 +35,10 @@ class TLSPage;
struct CodeSet;
namespace Memory {
class PageTable;
}
enum class MemoryRegion : u16 {
APPLICATION = 1,
SYSTEM = 2,
@ -100,14 +103,14 @@ public:
return HANDLE_TYPE;
}
/// Gets a reference to the process' memory manager.
Kernel::VMManager& VMManager() {
return vm_manager;
/// Gets a reference to the process' page table.
Memory::PageTable& PageTable() {
return *page_table;
}
/// Gets a const reference to the process' memory manager.
const Kernel::VMManager& VMManager() const {
return vm_manager;
/// Gets const a reference to the process' page table.
const Memory::PageTable& PageTable() const {
return *page_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
* 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.
@ -289,7 +292,7 @@ public:
*/
void PrepareForTermination();
void LoadModule(CodeSet module_, VAddr base_addr);
void LoadModule(CodeSet code_set, VAddr base_addr);
///////////////////////////////////////////////////////////////////////////////////////////////
// Thread-local storage management
@ -313,16 +316,10 @@ private:
void ChangeStatus(ProcessStatus new_status);
/// 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.
Kernel::VMManager vm_manager;
/// 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;
/// Memory manager for this process
std::unique_ptr<Memory::PageTable> page_table;
/// Current status of the process
ProcessStatus status{};
@ -390,6 +387,18 @@ private:
/// Name of this process
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