Merge pull request #2961 from Subv/load_titles

Loaders: Don't automatically set the current process every time we load an application.
This commit is contained in:
bunnei 2017-09-29 14:58:42 -04:00 committed by GitHub
commit b07af7dda8
17 changed files with 157 additions and 70 deletions

View File

@ -13,6 +13,7 @@
#include "core/core_timing.h"
#include "core/gdbstub/gdbstub.h"
#include "core/hle/kernel/kernel.h"
#include "core/hle/kernel/process.h"
#include "core/hle/kernel/thread.h"
#include "core/hle/service/service.h"
#include "core/hw/hw.h"
@ -100,7 +101,7 @@ System::ResultStatus System::Load(EmuWindow* emu_window, const std::string& file
return init_result;
}
const Loader::ResultStatus load_result{app_loader->Load()};
const Loader::ResultStatus load_result{app_loader->Load(Kernel::g_current_process)};
if (Loader::ResultStatus::Success != load_result) {
LOG_CRITICAL(Core, "Failed to load ROM (Error %i)!", load_result);
System::Shutdown();
@ -114,6 +115,7 @@ System::ResultStatus System::Load(EmuWindow* emu_window, const std::string& file
return ResultStatus::ErrorLoader;
}
}
Memory::SetCurrentPageTable(&Kernel::g_current_process->vm_manager.page_table);
status = ResultStatus::Success;
return status;
}
@ -196,4 +198,4 @@ void System::Shutdown() {
LOG_DEBUG(Core, "Shutdown OK");
}
} // namespace
} // namespace Core

View File

@ -147,7 +147,7 @@ void Process::Run(s32 main_thread_priority, u32 stack_size) {
}
vm_manager.LogLayout(Log::Level::Debug);
Kernel::SetupMainThread(codeset->entrypoint, main_thread_priority);
Kernel::SetupMainThread(codeset->entrypoint, main_thread_priority, this);
}
VAddr Process::GetLinearHeapAreaAddress() const {

View File

@ -361,7 +361,8 @@ static void ResetThreadContext(ARM_Interface::ThreadContext& context, u32 stack_
}
ResultVal<SharedPtr<Thread>> Thread::Create(std::string name, VAddr entry_point, u32 priority,
u32 arg, s32 processor_id, VAddr stack_top) {
u32 arg, s32 processor_id, VAddr stack_top,
SharedPtr<Process> owner_process) {
// Check if priority is in ranged. Lowest priority -> highest priority id.
if (priority > THREADPRIO_LOWEST) {
LOG_ERROR(Kernel_SVC, "Invalid thread priority: %d", priority);
@ -375,7 +376,7 @@ ResultVal<SharedPtr<Thread>> Thread::Create(std::string name, VAddr entry_point,
// TODO(yuriks): Other checks, returning 0xD9001BEA
if (!Memory::IsValidVirtualAddress(entry_point)) {
if (!Memory::IsValidVirtualAddress(*owner_process, entry_point)) {
LOG_ERROR(Kernel_SVC, "(name=%s): invalid entry %08x", name.c_str(), entry_point);
// TODO: Verify error
return ResultCode(ErrorDescription::InvalidAddress, ErrorModule::Kernel,
@ -399,10 +400,10 @@ ResultVal<SharedPtr<Thread>> Thread::Create(std::string name, VAddr entry_point,
thread->wait_address = 0;
thread->name = std::move(name);
thread->callback_handle = wakeup_callback_handle_table.Create(thread).Unwrap();
thread->owner_process = g_current_process;
thread->owner_process = owner_process;
// Find the next available TLS index, and mark it as used
auto& tls_slots = Kernel::g_current_process->tls_slots;
auto& tls_slots = owner_process->tls_slots;
bool needs_allocation = true;
u32 available_page; // Which allocated page has free space
u32 available_slot; // Which slot within the page is free
@ -426,13 +427,13 @@ ResultVal<SharedPtr<Thread>> Thread::Create(std::string name, VAddr entry_point,
// Allocate some memory from the end of the linear heap for this region.
linheap_memory->insert(linheap_memory->end(), Memory::PAGE_SIZE, 0);
memory_region->used += Memory::PAGE_SIZE;
Kernel::g_current_process->linear_heap_used += Memory::PAGE_SIZE;
owner_process->linear_heap_used += Memory::PAGE_SIZE;
tls_slots.emplace_back(0); // The page is completely available at the start
available_page = tls_slots.size() - 1;
available_slot = 0; // Use the first slot in the new page
auto& vm_manager = Kernel::g_current_process->vm_manager;
auto& vm_manager = owner_process->vm_manager;
vm_manager.RefreshMemoryBlockMappings(linheap_memory.get());
// Map the page to the current process' address space.
@ -486,10 +487,10 @@ void Thread::BoostPriority(s32 priority) {
current_priority = priority;
}
SharedPtr<Thread> SetupMainThread(u32 entry_point, s32 priority) {
SharedPtr<Thread> SetupMainThread(u32 entry_point, s32 priority, SharedPtr<Process> owner_process) {
// Initialize new "main" thread
auto thread_res = Thread::Create("main", entry_point, priority, 0, THREADPROCESSORID_0,
Memory::HEAP_VADDR_END);
Memory::HEAP_VADDR_END, owner_process);
SharedPtr<Thread> thread = std::move(thread_res).Unwrap();

View File

@ -56,10 +56,12 @@ public:
* @param arg User data to pass to the thread
* @param processor_id The ID(s) of the processors on which the thread is desired to be run
* @param stack_top The address of the thread's stack top
* @param owner_process The parent process for the thread
* @return A shared pointer to the newly created thread
*/
static ResultVal<SharedPtr<Thread>> Create(std::string name, VAddr entry_point, u32 priority,
u32 arg, s32 processor_id, VAddr stack_top);
u32 arg, s32 processor_id, VAddr stack_top,
SharedPtr<Process> owner_process);
std::string GetName() const override {
return name;
@ -214,9 +216,10 @@ private:
* Sets up the primary application thread
* @param entry_point The address at which the thread should start execution
* @param priority The priority to give the main thread
* @param owner_process The parent process for the main thread
* @return A shared pointer to the main thread
*/
SharedPtr<Thread> SetupMainThread(u32 entry_point, s32 priority);
SharedPtr<Thread> SetupMainThread(u32 entry_point, s32 priority, SharedPtr<Process> owner_process);
/**
* Returns whether there are any threads that are ready to run.
@ -276,4 +279,4 @@ void ThreadingShutdown();
*/
const std::vector<SharedPtr<Thread>>& GetThreadList();
} // namespace
} // namespace Kernel

View File

@ -656,8 +656,9 @@ static ResultCode CreateThread(Kernel::Handle* out_handle, u32 priority, u32 ent
"Newly created thread must run in the SysCore (Core1), unimplemented.");
}
CASCADE_RESULT(SharedPtr<Thread> thread, Kernel::Thread::Create(name, entry_point, priority,
arg, processor_id, stack_top));
CASCADE_RESULT(SharedPtr<Thread> thread,
Kernel::Thread::Create(name, entry_point, priority, arg, processor_id, stack_top,
Kernel::g_current_process));
thread->context.fpscr =
FPSCR_DEFAULT_NAN | FPSCR_FLUSH_TO_ZERO | FPSCR_ROUND_TOZERO; // 0x03C00000

View File

@ -91,8 +91,8 @@ static u32 TranslateAddr(u32 addr, const THREEloadinfo* loadinfo, u32* offsets)
return loadinfo->seg_addrs[2] + addr - offsets[1];
}
using Kernel::SharedPtr;
using Kernel::CodeSet;
using Kernel::SharedPtr;
static THREEDSX_Error Load3DSXFile(FileUtil::IOFile& file, u32 base_addr,
SharedPtr<CodeSet>* out_codeset) {
@ -255,7 +255,7 @@ FileType AppLoader_THREEDSX::IdentifyType(FileUtil::IOFile& file) {
return FileType::Error;
}
ResultStatus AppLoader_THREEDSX::Load() {
ResultStatus AppLoader_THREEDSX::Load(Kernel::SharedPtr<Kernel::Process>& process) {
if (is_loaded)
return ResultStatus::ErrorAlreadyLoaded;
@ -267,16 +267,15 @@ ResultStatus AppLoader_THREEDSX::Load() {
return ResultStatus::Error;
codeset->name = filename;
Kernel::g_current_process = Kernel::Process::Create(std::move(codeset));
Kernel::g_current_process->svc_access_mask.set();
Kernel::g_current_process->address_mappings = default_address_mappings;
Memory::SetCurrentPageTable(&Kernel::g_current_process->vm_manager.page_table);
process = Kernel::Process::Create(std::move(codeset));
process->svc_access_mask.set();
process->address_mappings = default_address_mappings;
// Attach the default resource limit (APPLICATION) to the process
Kernel::g_current_process->resource_limit =
process->resource_limit =
Kernel::ResourceLimit::GetForCategory(Kernel::ResourceLimitCategory::APPLICATION);
Kernel::g_current_process->Run(48, Kernel::DEFAULT_STACK_SIZE);
process->Run(48, Kernel::DEFAULT_STACK_SIZE);
Service::FS::RegisterSelfNCCH(*this);

View File

@ -31,7 +31,7 @@ public:
return IdentifyType(file);
}
ResultStatus Load() override;
ResultStatus Load(Kernel::SharedPtr<Kernel::Process>& process) override;
ResultStatus ReadIcon(std::vector<u8>& buffer) override;

View File

@ -13,8 +13,8 @@
#include "core/loader/elf.h"
#include "core/memory.h"
using Kernel::SharedPtr;
using Kernel::CodeSet;
using Kernel::SharedPtr;
////////////////////////////////////////////////////////////////////////////////////////////////////
// ELF Header Constants
@ -375,7 +375,7 @@ FileType AppLoader_ELF::IdentifyType(FileUtil::IOFile& file) {
return FileType::Error;
}
ResultStatus AppLoader_ELF::Load() {
ResultStatus AppLoader_ELF::Load(Kernel::SharedPtr<Kernel::Process>& process) {
if (is_loaded)
return ResultStatus::ErrorAlreadyLoaded;
@ -394,16 +394,15 @@ ResultStatus AppLoader_ELF::Load() {
SharedPtr<CodeSet> codeset = elf_reader.LoadInto(Memory::PROCESS_IMAGE_VADDR);
codeset->name = filename;
Kernel::g_current_process = Kernel::Process::Create(std::move(codeset));
Kernel::g_current_process->svc_access_mask.set();
Kernel::g_current_process->address_mappings = default_address_mappings;
Memory::SetCurrentPageTable(&Kernel::g_current_process->vm_manager.page_table);
process = Kernel::Process::Create(std::move(codeset));
process->svc_access_mask.set();
process->address_mappings = default_address_mappings;
// Attach the default resource limit (APPLICATION) to the process
Kernel::g_current_process->resource_limit =
process->resource_limit =
Kernel::ResourceLimit::GetForCategory(Kernel::ResourceLimitCategory::APPLICATION);
Kernel::g_current_process->Run(48, Kernel::DEFAULT_STACK_SIZE);
process->Run(48, Kernel::DEFAULT_STACK_SIZE);
is_loaded = true;
return ResultStatus::Success;

View File

@ -30,7 +30,7 @@ public:
return IdentifyType(file);
}
ResultStatus Load() override;
ResultStatus Load(Kernel::SharedPtr<Kernel::Process>& process) override;
private:
std::string filename;

View File

@ -13,10 +13,12 @@
#include <boost/optional.hpp>
#include "common/common_types.h"
#include "common/file_util.h"
#include "core/hle/kernel/kernel.h"
namespace Kernel {
struct AddressMapping;
}
class Process;
} // namespace Kernel
////////////////////////////////////////////////////////////////////////////////////////////////////
// Loader namespace
@ -92,10 +94,11 @@ public:
virtual FileType GetFileType() = 0;
/**
* Load the application
* @return ResultStatus result of function
* Load the application and return the created Process instance
* @param process The newly created process.
* @return The status result of the operation.
*/
virtual ResultStatus Load() = 0;
virtual ResultStatus Load(Kernel::SharedPtr<Kernel::Process>& process) = 0;
/**
* Loads the system mode that this application needs.
@ -206,4 +209,4 @@ extern const std::initializer_list<Kernel::AddressMapping> default_address_mappi
*/
std::unique_ptr<AppLoader> GetLoader(const std::string& filename);
} // namespace
} // namespace Loader

View File

@ -67,9 +67,9 @@ std::pair<boost::optional<u32>, ResultStatus> AppLoader_NCCH::LoadKernelSystemMo
ResultStatus::Success);
}
ResultStatus AppLoader_NCCH::LoadExec() {
using Kernel::SharedPtr;
ResultStatus AppLoader_NCCH::LoadExec(Kernel::SharedPtr<Kernel::Process>& process) {
using Kernel::CodeSet;
using Kernel::SharedPtr;
if (!is_loaded)
return ResultStatus::ErrorNotLoaded;
@ -107,16 +107,15 @@ ResultStatus AppLoader_NCCH::LoadExec() {
codeset->entrypoint = codeset->code.addr;
codeset->memory = std::make_shared<std::vector<u8>>(std::move(code));
Kernel::g_current_process = Kernel::Process::Create(std::move(codeset));
Memory::SetCurrentPageTable(&Kernel::g_current_process->vm_manager.page_table);
process = Kernel::Process::Create(std::move(codeset));
// Attach a resource limit to the process based on the resource limit category
Kernel::g_current_process->resource_limit =
process->resource_limit =
Kernel::ResourceLimit::GetForCategory(static_cast<Kernel::ResourceLimitCategory>(
overlay_ncch->exheader_header.arm11_system_local_caps.resource_limit_category));
// Set the default CPU core for this process
Kernel::g_current_process->ideal_processor =
process->ideal_processor =
overlay_ncch->exheader_header.arm11_system_local_caps.ideal_processor;
// Copy data while converting endianness
@ -124,11 +123,11 @@ ResultStatus AppLoader_NCCH::LoadExec() {
kernel_caps;
std::copy_n(overlay_ncch->exheader_header.arm11_kernel_caps.descriptors, kernel_caps.size(),
begin(kernel_caps));
Kernel::g_current_process->ParseKernelCaps(kernel_caps.data(), kernel_caps.size());
process->ParseKernelCaps(kernel_caps.data(), kernel_caps.size());
s32 priority = overlay_ncch->exheader_header.arm11_system_local_caps.priority;
u32 stack_size = overlay_ncch->exheader_header.codeset_info.stack_size;
Kernel::g_current_process->Run(priority, stack_size);
process->Run(priority, stack_size);
return ResultStatus::Success;
}
return ResultStatus::Error;
@ -151,7 +150,7 @@ void AppLoader_NCCH::ParseRegionLockoutInfo() {
}
}
ResultStatus AppLoader_NCCH::Load() {
ResultStatus AppLoader_NCCH::Load(Kernel::SharedPtr<Kernel::Process>& process) {
u64_le ncch_program_id;
if (is_loaded)
@ -183,7 +182,7 @@ ResultStatus AppLoader_NCCH::Load() {
is_loaded = true; // Set state to loaded
result = LoadExec(); // Load the executable into memory for booting
result = LoadExec(process); // Load the executable into memory for booting
if (ResultStatus::Success != result)
return result;

View File

@ -33,7 +33,7 @@ public:
return IdentifyType(file);
}
ResultStatus Load() override;
ResultStatus Load(Kernel::SharedPtr<Kernel::Process>& process) override;
/**
* Loads the Exheader and returns the system mode for this application.
@ -62,9 +62,10 @@ public:
private:
/**
* Loads .code section into memory for booting
* @param process The newly created process
* @return ResultStatus result of function
*/
ResultStatus LoadExec();
ResultStatus LoadExec(Kernel::SharedPtr<Kernel::Process>& process);
/// Reads the region lockout info in the SMDH and send it to CFG service
void ParseRegionLockoutInfo();

View File

@ -110,8 +110,8 @@ static u8* GetPointerFromVMA(VAddr vaddr) {
/**
* This function should only be called for virtual addreses with attribute `PageType::Special`.
*/
static MMIORegionPointer GetMMIOHandler(VAddr vaddr) {
for (const auto& region : current_page_table->special_regions) {
static MMIORegionPointer GetMMIOHandler(const PageTable& page_table, VAddr vaddr) {
for (const auto& region : page_table.special_regions) {
if (vaddr >= region.base && vaddr < (region.base + region.size)) {
return region.handler;
}
@ -120,6 +120,11 @@ static MMIORegionPointer GetMMIOHandler(VAddr vaddr) {
return nullptr; // Should never happen
}
static MMIORegionPointer GetMMIOHandler(VAddr vaddr) {
const PageTable& page_table = Kernel::g_current_process->vm_manager.page_table;
return GetMMIOHandler(page_table, vaddr);
}
template <typename T>
T ReadMMIO(MMIORegionPointer mmio_handler, VAddr addr);
@ -204,18 +209,20 @@ void Write(const VAddr vaddr, const T data) {
}
}
bool IsValidVirtualAddress(const VAddr vaddr) {
const u8* page_pointer = current_page_table->pointers[vaddr >> PAGE_BITS];
bool IsValidVirtualAddress(const Kernel::Process& process, const VAddr vaddr) {
auto& page_table = process.vm_manager.page_table;
const u8* page_pointer = page_table.pointers[vaddr >> PAGE_BITS];
if (page_pointer)
return true;
if (current_page_table->attributes[vaddr >> PAGE_BITS] == PageType::RasterizerCachedMemory)
if (page_table.attributes[vaddr >> PAGE_BITS] == PageType::RasterizerCachedMemory)
return true;
if (current_page_table->attributes[vaddr >> PAGE_BITS] != PageType::Special)
if (page_table.attributes[vaddr >> PAGE_BITS] != PageType::Special)
return false;
MMIORegionPointer mmio_region = GetMMIOHandler(vaddr);
MMIORegionPointer mmio_region = GetMMIOHandler(page_table, vaddr);
if (mmio_region) {
return mmio_region->IsValidAddress(vaddr);
}
@ -223,6 +230,10 @@ bool IsValidVirtualAddress(const VAddr vaddr) {
return false;
}
bool IsValidVirtualAddress(const VAddr vaddr) {
return IsValidVirtualAddress(*Kernel::g_current_process, vaddr);
}
bool IsValidPhysicalAddress(const PAddr paddr) {
return GetPhysicalPointer(paddr) != nullptr;
}

View File

@ -12,6 +12,10 @@
#include "common/common_types.h"
#include "core/mmio.h"
namespace Kernel {
class Process;
}
namespace Memory {
/**
@ -185,7 +189,10 @@ enum : VAddr {
void SetCurrentPageTable(PageTable* page_table);
PageTable* GetCurrentPageTable();
/// Determines if the given VAddr is valid for the specified process.
bool IsValidVirtualAddress(const Kernel::Process& process, const VAddr vaddr);
bool IsValidVirtualAddress(const VAddr addr);
bool IsValidPhysicalAddress(const PAddr addr);
u8 Read8(VAddr addr);

View File

@ -4,6 +4,7 @@ set(SRCS
core/arm/dyncom/arm_dyncom_vfp_tests.cpp
core/file_sys/path_parser.cpp
core/hle/kernel/hle_ipc.cpp
core/memory/memory.cpp
glad.cpp
tests.cpp
)

View File

@ -3,30 +3,34 @@
// Refer to the license.txt file included.
#include "core/core.h"
#include "core/hle/kernel/process.h"
#include "core/memory.h"
#include "core/memory_setup.h"
#include "tests/core/arm/arm_test_common.h"
namespace ArmTests {
static Memory::PageTable page_table;
static Memory::PageTable* page_table = nullptr;
TestEnvironment::TestEnvironment(bool mutable_memory_)
: mutable_memory(mutable_memory_), test_memory(std::make_shared<TestMemory>(this)) {
page_table.pointers.fill(nullptr);
page_table.attributes.fill(Memory::PageType::Unmapped);
page_table.cached_res_count.fill(0);
Kernel::g_current_process = Kernel::Process::Create(Kernel::CodeSet::Create("", 0));
page_table = &Kernel::g_current_process->vm_manager.page_table;
Memory::MapIoRegion(page_table, 0x00000000, 0x80000000, test_memory);
Memory::MapIoRegion(page_table, 0x80000000, 0x80000000, test_memory);
page_table->pointers.fill(nullptr);
page_table->attributes.fill(Memory::PageType::Unmapped);
page_table->cached_res_count.fill(0);
Memory::SetCurrentPageTable(&page_table);
Memory::MapIoRegion(*page_table, 0x00000000, 0x80000000, test_memory);
Memory::MapIoRegion(*page_table, 0x80000000, 0x80000000, test_memory);
Memory::SetCurrentPageTable(page_table);
}
TestEnvironment::~TestEnvironment() {
Memory::UnmapRegion(page_table, 0x80000000, 0x80000000);
Memory::UnmapRegion(page_table, 0x00000000, 0x80000000);
Memory::UnmapRegion(*page_table, 0x80000000, 0x80000000);
Memory::UnmapRegion(*page_table, 0x00000000, 0x80000000);
}
void TestEnvironment::SetMemory64(VAddr vaddr, u64 value) {

View File

@ -0,0 +1,56 @@
// Copyright 2017 Citra Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#include <catch.hpp>
#include "core/hle/kernel/memory.h"
#include "core/hle/kernel/process.h"
#include "core/memory.h"
TEST_CASE("Memory::IsValidVirtualAddress", "[core][memory]") {
SECTION("these regions should not be mapped on an empty process") {
auto process = Kernel::Process::Create(Kernel::CodeSet::Create("", 0));
CHECK(Memory::IsValidVirtualAddress(*process, Memory::PROCESS_IMAGE_VADDR) == false);
CHECK(Memory::IsValidVirtualAddress(*process, Memory::HEAP_VADDR) == false);
CHECK(Memory::IsValidVirtualAddress(*process, Memory::LINEAR_HEAP_VADDR) == false);
CHECK(Memory::IsValidVirtualAddress(*process, Memory::VRAM_VADDR) == false);
CHECK(Memory::IsValidVirtualAddress(*process, Memory::CONFIG_MEMORY_VADDR) == false);
CHECK(Memory::IsValidVirtualAddress(*process, Memory::SHARED_PAGE_VADDR) == false);
CHECK(Memory::IsValidVirtualAddress(*process, Memory::TLS_AREA_VADDR) == false);
}
SECTION("CONFIG_MEMORY_VADDR and SHARED_PAGE_VADDR should be valid after mapping them") {
auto process = Kernel::Process::Create(Kernel::CodeSet::Create("", 0));
Kernel::MapSharedPages(process->vm_manager);
CHECK(Memory::IsValidVirtualAddress(*process, Memory::CONFIG_MEMORY_VADDR) == true);
CHECK(Memory::IsValidVirtualAddress(*process, Memory::SHARED_PAGE_VADDR) == true);
}
SECTION("special regions should be valid after mapping them") {
auto process = Kernel::Process::Create(Kernel::CodeSet::Create("", 0));
SECTION("VRAM") {
Kernel::HandleSpecialMapping(process->vm_manager,
{Memory::VRAM_VADDR, Memory::VRAM_SIZE, false, false});
CHECK(Memory::IsValidVirtualAddress(*process, Memory::VRAM_VADDR) == true);
}
SECTION("IO (Not yet implemented)") {
Kernel::HandleSpecialMapping(
process->vm_manager, {Memory::IO_AREA_VADDR, Memory::IO_AREA_SIZE, false, false});
CHECK_FALSE(Memory::IsValidVirtualAddress(*process, Memory::IO_AREA_VADDR) == true);
}
SECTION("DSP") {
Kernel::HandleSpecialMapping(
process->vm_manager, {Memory::DSP_RAM_VADDR, Memory::DSP_RAM_SIZE, false, false});
CHECK(Memory::IsValidVirtualAddress(*process, Memory::DSP_RAM_VADDR) == true);
}
}
SECTION("Unmapping a VAddr should make it invalid") {
auto process = Kernel::Process::Create(Kernel::CodeSet::Create("", 0));
Kernel::MapSharedPages(process->vm_manager);
process->vm_manager.UnmapRange(Memory::CONFIG_MEMORY_VADDR, Memory::CONFIG_MEMORY_SIZE);
CHECK(Memory::IsValidVirtualAddress(*process, Memory::CONFIG_MEMORY_VADDR) == false);
}
}