citra-emu
/
citra-canary
Archived
1
0
Fork 0

Merge pull request #912 from yuriks/process-loading

Core: Properly configure address space during binary loading
This commit is contained in:
Yuri Kunde Schlesner 2015-07-11 20:07:49 -07:00
commit 4e900d56f3
14 changed files with 242 additions and 74 deletions

View File

@ -37,7 +37,7 @@ ArchiveFactory_SaveData::ArchiveFactory_SaveData(const std::string& sdmc_directo
} }
ResultVal<std::unique_ptr<ArchiveBackend>> ArchiveFactory_SaveData::Open(const Path& path) { ResultVal<std::unique_ptr<ArchiveBackend>> ArchiveFactory_SaveData::Open(const Path& path) {
std::string concrete_mount_point = GetSaveDataPath(mount_point, Kernel::g_current_process->program_id); std::string concrete_mount_point = GetSaveDataPath(mount_point, Kernel::g_current_process->codeset->program_id);
if (!FileUtil::Exists(concrete_mount_point)) { if (!FileUtil::Exists(concrete_mount_point)) {
// When a SaveData archive is created for the first time, it is not yet formatted // When a SaveData archive is created for the first time, it is not yet formatted
// and the save file/directory structure expected by the game has not yet been initialized. // and the save file/directory structure expected by the game has not yet been initialized.
@ -52,7 +52,7 @@ ResultVal<std::unique_ptr<ArchiveBackend>> ArchiveFactory_SaveData::Open(const P
} }
ResultCode ArchiveFactory_SaveData::Format(const Path& path) { ResultCode ArchiveFactory_SaveData::Format(const Path& path) {
std::string concrete_mount_point = GetSaveDataPath(mount_point, Kernel::g_current_process->program_id); std::string concrete_mount_point = GetSaveDataPath(mount_point, Kernel::g_current_process->codeset->program_id);
FileUtil::DeleteDirRecursively(concrete_mount_point); FileUtil::DeleteDirRecursively(concrete_mount_point);
FileUtil::CreateFullPath(concrete_mount_point); FileUtil::CreateFullPath(concrete_mount_point);
return RESULT_SUCCESS; return RESULT_SUCCESS;

View File

@ -21,7 +21,7 @@ SharedPtr<Event> Event::Create(ResetType reset_type, std::string name) {
SharedPtr<Event> evt(new Event); SharedPtr<Event> evt(new Event);
evt->signaled = false; evt->signaled = false;
evt->reset_type = evt->intitial_reset_type = reset_type; evt->reset_type = reset_type;
evt->name = std::move(name); evt->name = std::move(name);
return evt; return evt;

View File

@ -26,7 +26,6 @@ public:
static const HandleType HANDLE_TYPE = HandleType::Event; static const HandleType HANDLE_TYPE = HandleType::Event;
HandleType GetHandleType() const override { return HANDLE_TYPE; } HandleType GetHandleType() const override { return HANDLE_TYPE; }
ResetType intitial_reset_type; ///< ResetType specified at Event initialization
ResetType reset_type; ///< Current ResetType ResetType reset_type; ///< Current ResetType
bool signaled; ///< Whether the event has already been signaled bool signaled; ///< Whether the event has already been signaled

View File

@ -47,6 +47,7 @@ enum class HandleType : u32 {
Semaphore = 10, Semaphore = 10,
Timer = 11, Timer = 11,
ResourceLimit = 12, ResourceLimit = 12,
CodeSet = 13,
}; };
enum { enum {

View File

@ -5,24 +5,39 @@
#include "common/assert.h" #include "common/assert.h"
#include "common/common_funcs.h" #include "common/common_funcs.h"
#include "common/logging/log.h" #include "common/logging/log.h"
#include "common/make_unique.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/thread.h" #include "core/hle/kernel/thread.h"
#include "core/hle/kernel/vm_manager.h"
#include "core/mem_map.h"
#include "core/memory.h" #include "core/memory.h"
namespace Kernel { namespace Kernel {
SharedPtr<CodeSet> CodeSet::Create(std::string name, u64 program_id) {
SharedPtr<CodeSet> codeset(new CodeSet);
codeset->name = std::move(name);
codeset->program_id = program_id;
return codeset;
}
CodeSet::CodeSet() {}
CodeSet::~CodeSet() {}
u32 Process::next_process_id; u32 Process::next_process_id;
SharedPtr<Process> Process::Create(std::string name, u64 program_id) { SharedPtr<Process> Process::Create(SharedPtr<CodeSet> code_set) {
SharedPtr<Process> process(new Process); SharedPtr<Process> process(new Process);
process->name = std::move(name); process->codeset = std::move(code_set);
process->program_id = program_id;
process->flags.raw = 0; process->flags.raw = 0;
process->flags.memory_region = MemoryRegion::APPLICATION; process->flags.memory_region = MemoryRegion::APPLICATION;
process->address_space = Common::make_unique<VMManager>();
Memory::InitLegacyAddressSpace(*process->address_space);
return process; return process;
} }
@ -87,8 +102,19 @@ void Process::ParseKernelCaps(const u32* kernel_caps, size_t len) {
} }
} }
void Process::Run(VAddr entry_point, s32 main_thread_priority, u32 stack_size) { void Process::Run(s32 main_thread_priority, u32 stack_size) {
Kernel::SetupMainThread(entry_point, main_thread_priority); auto MapSegment = [&](CodeSet::Segment& segment, VMAPermission permissions, MemoryState memory_state) {
auto vma = address_space->MapMemoryBlock(segment.addr, codeset->memory,
segment.offset, segment.size, memory_state).Unwrap();
address_space->Reprotect(vma, permissions);
};
MapSegment(codeset->code, VMAPermission::ReadExecute, MemoryState::Code);
MapSegment(codeset->rodata, VMAPermission::Read, MemoryState::Code);
MapSegment(codeset->data, VMAPermission::ReadWrite, MemoryState::Private);
address_space->LogLayout();
Kernel::SetupMainThread(codeset->entrypoint, main_thread_priority);
} }
Kernel::Process::Process() {} Kernel::Process::Process() {}

View File

@ -47,23 +47,51 @@ union ProcessFlags {
}; };
class ResourceLimit; class ResourceLimit;
class VMManager;
struct CodeSet final : public Object {
static SharedPtr<CodeSet> Create(std::string name, u64 program_id);
std::string GetTypeName() const override { return "CodeSet"; }
std::string GetName() const override { return name; }
static const HandleType HANDLE_TYPE = HandleType::CodeSet;
HandleType GetHandleType() const override { return HANDLE_TYPE; }
/// Name of the process
std::string name;
/// Title ID corresponding to the process
u64 program_id;
std::shared_ptr<std::vector<u8>> memory;
struct Segment {
size_t offset = 0;
VAddr addr = 0;
u32 size = 0;
};
Segment code, rodata, data;
VAddr entrypoint;
private:
CodeSet();
~CodeSet() override;
};
class Process final : public Object { class Process final : public Object {
public: public:
static SharedPtr<Process> Create(std::string name, u64 program_id); static SharedPtr<Process> Create(SharedPtr<CodeSet> code_set);
std::string GetTypeName() const override { return "Process"; } std::string GetTypeName() const override { return "Process"; }
std::string GetName() const override { return name; } std::string GetName() const override { return codeset->name; }
static const HandleType HANDLE_TYPE = HandleType::Process; static const HandleType HANDLE_TYPE = HandleType::Process;
HandleType GetHandleType() const override { return HANDLE_TYPE; } HandleType GetHandleType() const override { return HANDLE_TYPE; }
static u32 next_process_id; static u32 next_process_id;
/// Name of the process SharedPtr<CodeSet> codeset;
std::string name;
/// Title ID corresponding to the process
u64 program_id;
/// Resource limit descriptor for this process /// Resource limit descriptor for this process
SharedPtr<ResourceLimit> resource_limit; SharedPtr<ResourceLimit> resource_limit;
@ -81,6 +109,7 @@ public:
/// Bitmask of the used TLS slots /// Bitmask of the used TLS slots
std::bitset<300> used_tls_slots; std::bitset<300> used_tls_slots;
std::unique_ptr<VMManager> address_space;
/** /**
* Parses a list of kernel capability descriptors (as found in the ExHeader) and applies them * Parses a list of kernel capability descriptors (as found in the ExHeader) and applies them
@ -91,7 +120,7 @@ public:
/** /**
* Applies address space changes and launches the process main thread. * Applies address space changes and launches the process main thread.
*/ */
void Run(VAddr entry_point, s32 main_thread_priority, u32 stack_size); void Run(s32 main_thread_priority, u32 stack_size);
private: private:
Process(); Process();

View File

@ -35,6 +35,10 @@ VMManager::VMManager() {
Reset(); Reset();
} }
VMManager::~VMManager() {
Reset();
}
void VMManager::Reset() { void VMManager::Reset() {
vma_map.clear(); vma_map.clear();
@ -130,6 +134,16 @@ void VMManager::Reprotect(VMAHandle vma_handle, VMAPermission new_perms) {
MergeAdjacent(iter); MergeAdjacent(iter);
} }
void VMManager::LogLayout() const {
for (const auto& p : vma_map) {
const VirtualMemoryArea& vma = p.second;
LOG_DEBUG(Kernel, "%08X - %08X size: %8X %c%c%c", vma.base, vma.base + vma.size, vma.size,
(u8)vma.permissions & (u8)VMAPermission::Read ? 'R' : '-',
(u8)vma.permissions & (u8)VMAPermission::Write ? 'W' : '-',
(u8)vma.permissions & (u8)VMAPermission::Execute ? 'X' : '-');
}
}
VMManager::VMAIter VMManager::StripIterConstness(const VMAHandle & iter) { VMManager::VMAIter VMManager::StripIterConstness(const VMAHandle & iter) {
// This uses a neat C++ trick to convert a const_iterator to a regular iterator, given // This uses a neat C++ trick to convert a const_iterator to a regular iterator, given
// non-const access to its container. // non-const access to its container.

View File

@ -101,7 +101,7 @@ struct VirtualMemoryArea {
* - http://duartes.org/gustavo/blog/post/how-the-kernel-manages-your-memory/ * - http://duartes.org/gustavo/blog/post/how-the-kernel-manages-your-memory/
* - http://duartes.org/gustavo/blog/post/page-cache-the-affair-between-memory-and-files/ * - http://duartes.org/gustavo/blog/post/page-cache-the-affair-between-memory-and-files/
*/ */
class VMManager { class VMManager final {
// TODO(yuriks): Make page tables switchable to support multiple VMManagers // TODO(yuriks): Make page tables switchable to support multiple VMManagers
public: public:
/** /**
@ -121,6 +121,7 @@ public:
using VMAHandle = decltype(vma_map)::const_iterator; using VMAHandle = decltype(vma_map)::const_iterator;
VMManager(); VMManager();
~VMManager();
/// Clears the address space map, re-initializing with a single free area. /// Clears the address space map, re-initializing with a single free area.
void Reset(); void Reset();
@ -168,6 +169,9 @@ public:
/// Changes the permissions of the given VMA. /// Changes the permissions of the given VMA.
void Reprotect(VMAHandle vma, VMAPermission new_perms); void Reprotect(VMAHandle vma, VMAPermission new_perms);
/// Dumps the address space layout to the log, for debugging
void LogLayout() const;
private: private:
using VMAIter = decltype(vma_map)::iterator; using VMAIter = decltype(vma_map)::iterator;

View File

@ -19,7 +19,7 @@
namespace Loader { namespace Loader {
/** /*
* File layout: * File layout:
* - File header * - File header
* - Code, rodata and data relocation table headers * - Code, rodata and data relocation table headers
@ -39,13 +39,16 @@ namespace Loader {
* The entrypoint is always the start of the code segment. * The entrypoint is always the start of the code segment.
* The BSS section must be cleared manually by the application. * The BSS section must be cleared manually by the application.
*/ */
enum THREEDSX_Error { enum THREEDSX_Error {
ERROR_NONE = 0, ERROR_NONE = 0,
ERROR_READ = 1, ERROR_READ = 1,
ERROR_FILE = 2, ERROR_FILE = 2,
ERROR_ALLOC = 3 ERROR_ALLOC = 3
}; };
static const u32 RELOCBUFSIZE = 512; static const u32 RELOCBUFSIZE = 512;
static const unsigned int NUM_SEGMENTS = 3;
// File header // File header
#pragma pack(1) #pragma pack(1)
@ -98,7 +101,10 @@ static u32 TranslateAddr(u32 addr, const THREEloadinfo *loadinfo, u32* offsets)
return loadinfo->seg_addrs[2] + addr - offsets[1]; return loadinfo->seg_addrs[2] + addr - offsets[1];
} }
static THREEDSX_Error Load3DSXFile(FileUtil::IOFile& file, u32 base_addr) using Kernel::SharedPtr;
using Kernel::CodeSet;
static THREEDSX_Error Load3DSXFile(FileUtil::IOFile& file, u32 base_addr, SharedPtr<CodeSet>* out_codeset)
{ {
if (!file.IsOpen()) if (!file.IsOpen())
return ERROR_FILE; return ERROR_FILE;
@ -116,15 +122,13 @@ static THREEDSX_Error Load3DSXFile(FileUtil::IOFile& file, u32 base_addr)
loadinfo.seg_sizes[1] = (hdr.rodata_seg_size + 0xFFF) &~0xFFF; loadinfo.seg_sizes[1] = (hdr.rodata_seg_size + 0xFFF) &~0xFFF;
loadinfo.seg_sizes[2] = (hdr.data_seg_size + 0xFFF) &~0xFFF; loadinfo.seg_sizes[2] = (hdr.data_seg_size + 0xFFF) &~0xFFF;
u32 offsets[2] = { loadinfo.seg_sizes[0], loadinfo.seg_sizes[0] + loadinfo.seg_sizes[1] }; u32 offsets[2] = { loadinfo.seg_sizes[0], loadinfo.seg_sizes[0] + loadinfo.seg_sizes[1] };
u32 data_load_size = (hdr.data_seg_size - hdr.bss_size + 0xFFF) &~0xFFF; u32 n_reloc_tables = hdr.reloc_hdr_size / sizeof(u32);
u32 bss_load_size = loadinfo.seg_sizes[2] - data_load_size; std::vector<u8> program_image(loadinfo.seg_sizes[0] + loadinfo.seg_sizes[1] + loadinfo.seg_sizes[2]);
u32 n_reloc_tables = hdr.reloc_hdr_size / 4;
std::vector<u8> all_mem(loadinfo.seg_sizes[0] + loadinfo.seg_sizes[1] + loadinfo.seg_sizes[2] + 3 * n_reloc_tables);
loadinfo.seg_addrs[0] = base_addr; loadinfo.seg_addrs[0] = base_addr;
loadinfo.seg_addrs[1] = loadinfo.seg_addrs[0] + loadinfo.seg_sizes[0]; loadinfo.seg_addrs[1] = loadinfo.seg_addrs[0] + loadinfo.seg_sizes[0];
loadinfo.seg_addrs[2] = loadinfo.seg_addrs[1] + loadinfo.seg_sizes[1]; loadinfo.seg_addrs[2] = loadinfo.seg_addrs[1] + loadinfo.seg_sizes[1];
loadinfo.seg_ptrs[0] = &all_mem[0]; loadinfo.seg_ptrs[0] = program_image.data();
loadinfo.seg_ptrs[1] = loadinfo.seg_ptrs[0] + loadinfo.seg_sizes[0]; loadinfo.seg_ptrs[1] = loadinfo.seg_ptrs[0] + loadinfo.seg_sizes[0];
loadinfo.seg_ptrs[2] = loadinfo.seg_ptrs[1] + loadinfo.seg_sizes[1]; loadinfo.seg_ptrs[2] = loadinfo.seg_ptrs[1] + loadinfo.seg_sizes[1];
@ -132,10 +136,9 @@ static THREEDSX_Error Load3DSXFile(FileUtil::IOFile& file, u32 base_addr)
file.Seek(hdr.header_size, SEEK_SET); file.Seek(hdr.header_size, SEEK_SET);
// Read the relocation headers // Read the relocation headers
u32* relocs = (u32*)(loadinfo.seg_ptrs[2] + hdr.data_seg_size); std::vector<u32> relocs(n_reloc_tables * NUM_SEGMENTS);
for (unsigned int current_segment = 0; current_segment < NUM_SEGMENTS; ++current_segment) {
for (unsigned current_segment : {0, 1, 2}) { size_t size = n_reloc_tables * sizeof(u32);
size_t size = n_reloc_tables * 4;
if (file.ReadBytes(&relocs[current_segment * n_reloc_tables], size) != size) if (file.ReadBytes(&relocs[current_segment * n_reloc_tables], size) != size)
return ERROR_READ; return ERROR_READ;
} }
@ -152,7 +155,7 @@ static THREEDSX_Error Load3DSXFile(FileUtil::IOFile& file, u32 base_addr)
memset((char*)loadinfo.seg_ptrs[2] + hdr.data_seg_size - hdr.bss_size, 0, hdr.bss_size); memset((char*)loadinfo.seg_ptrs[2] + hdr.data_seg_size - hdr.bss_size, 0, hdr.bss_size);
// Relocate the segments // Relocate the segments
for (unsigned current_segment : {0, 1, 2}) { for (unsigned int current_segment = 0; current_segment < NUM_SEGMENTS; ++current_segment) {
for (unsigned current_segment_reloc_table = 0; current_segment_reloc_table < n_reloc_tables; current_segment_reloc_table++) { for (unsigned current_segment_reloc_table = 0; current_segment_reloc_table < n_reloc_tables; current_segment_reloc_table++) {
u32 n_relocs = relocs[current_segment * n_reloc_tables + current_segment_reloc_table]; u32 n_relocs = relocs[current_segment * n_reloc_tables + current_segment_reloc_table];
if (current_segment_reloc_table >= 2) { if (current_segment_reloc_table >= 2) {
@ -160,7 +163,7 @@ static THREEDSX_Error Load3DSXFile(FileUtil::IOFile& file, u32 base_addr)
file.Seek(n_relocs*sizeof(THREEDSX_Reloc), SEEK_CUR); file.Seek(n_relocs*sizeof(THREEDSX_Reloc), SEEK_CUR);
continue; continue;
} }
static THREEDSX_Reloc reloc_table[RELOCBUFSIZE]; THREEDSX_Reloc reloc_table[RELOCBUFSIZE];
u32* pos = (u32*)loadinfo.seg_ptrs[current_segment]; u32* pos = (u32*)loadinfo.seg_ptrs[current_segment];
const u32* end_pos = pos + (loadinfo.seg_sizes[current_segment] / 4); const u32* end_pos = pos + (loadinfo.seg_sizes[current_segment] / 4);
@ -179,7 +182,7 @@ static THREEDSX_Error Load3DSXFile(FileUtil::IOFile& file, u32 base_addr)
pos += table.skip; pos += table.skip;
s32 num_patches = table.patch; s32 num_patches = table.patch;
while (0 < num_patches && pos < end_pos) { while (0 < num_patches && pos < end_pos) {
u32 in_addr = (char*)pos - (char*)&all_mem[0]; u32 in_addr = (u8*)pos - program_image.data();
u32 addr = TranslateAddr(*pos, &loadinfo, offsets); u32 addr = TranslateAddr(*pos, &loadinfo, offsets);
LOG_TRACE(Loader, "Patching %08X <-- rel(%08X,%d) (%08X)\n", LOG_TRACE(Loader, "Patching %08X <-- rel(%08X,%d) (%08X)\n",
base_addr + in_addr, addr, current_segment_reloc_table, *pos); base_addr + in_addr, addr, current_segment_reloc_table, *pos);
@ -201,14 +204,29 @@ static THREEDSX_Error Load3DSXFile(FileUtil::IOFile& file, u32 base_addr)
} }
} }
// Write the data // Create the CodeSet
memcpy(Memory::GetPointer(base_addr), &all_mem[0], loadinfo.seg_sizes[0] + loadinfo.seg_sizes[1] + loadinfo.seg_sizes[2]); SharedPtr<CodeSet> code_set = CodeSet::Create("", 0);
LOG_DEBUG(Loader, "CODE: %u pages\n", loadinfo.seg_sizes[0] / 0x1000); code_set->code.offset = loadinfo.seg_ptrs[0] - program_image.data();
LOG_DEBUG(Loader, "RODATA: %u pages\n", loadinfo.seg_sizes[1] / 0x1000); code_set->code.addr = loadinfo.seg_addrs[0];
LOG_DEBUG(Loader, "DATA: %u pages\n", data_load_size / 0x1000); code_set->code.size = loadinfo.seg_sizes[0];
LOG_DEBUG(Loader, "BSS: %u pages\n", bss_load_size / 0x1000);
code_set->rodata.offset = loadinfo.seg_ptrs[1] - program_image.data();
code_set->rodata.addr = loadinfo.seg_addrs[1];
code_set->rodata.size = loadinfo.seg_sizes[1];
code_set->data.offset = loadinfo.seg_ptrs[2] - program_image.data();
code_set->data.addr = loadinfo.seg_addrs[2];
code_set->data.size = loadinfo.seg_sizes[2];
code_set->entrypoint = code_set->code.addr;
code_set->memory = std::make_shared<std::vector<u8>>(std::move(program_image));
LOG_DEBUG(Loader, "code size: 0x%X", loadinfo.seg_sizes[0]);
LOG_DEBUG(Loader, "rodata size: 0x%X", loadinfo.seg_sizes[1]);
LOG_DEBUG(Loader, "data size: 0x%X (including 0x%X of bss)", loadinfo.seg_sizes[2], hdr.bss_size);
*out_codeset = code_set;
return ERROR_NONE; return ERROR_NONE;
} }
@ -231,16 +249,19 @@ ResultStatus AppLoader_THREEDSX::Load() {
if (!file->IsOpen()) if (!file->IsOpen())
return ResultStatus::Error; return ResultStatus::Error;
Kernel::g_current_process = Kernel::Process::Create(filename, 0); SharedPtr<CodeSet> codeset;
if (Load3DSXFile(*file, Memory::PROCESS_IMAGE_VADDR, &codeset) != ERROR_NONE)
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->svc_access_mask.set();
Kernel::g_current_process->address_mappings = default_address_mappings; Kernel::g_current_process->address_mappings = default_address_mappings;
// Attach the default resource limit (APPLICATION) to the process // Attach the default resource limit (APPLICATION) to the process
Kernel::g_current_process->resource_limit = Kernel::ResourceLimit::GetForCategory(Kernel::ResourceLimitCategory::APPLICATION); Kernel::g_current_process->resource_limit = Kernel::ResourceLimit::GetForCategory(Kernel::ResourceLimitCategory::APPLICATION);
Load3DSXFile(*file, Memory::PROCESS_IMAGE_VADDR); Kernel::g_current_process->Run(48, Kernel::DEFAULT_STACK_SIZE);
Kernel::g_current_process->Run(Memory::PROCESS_IMAGE_VADDR, 48, Kernel::DEFAULT_STACK_SIZE);
is_loaded = true; is_loaded = true;
return ResultStatus::Success; return ResultStatus::Success;

View File

@ -16,6 +16,9 @@
#include "core/loader/elf.h" #include "core/loader/elf.h"
#include "core/memory.h" #include "core/memory.h"
using Kernel::SharedPtr;
using Kernel::CodeSet;
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
// ELF Header Constants // ELF Header Constants
@ -97,6 +100,12 @@ enum ElfSectionFlags
#define PT_LOPROC 0x70000000 #define PT_LOPROC 0x70000000
#define PT_HIPROC 0x7FFFFFFF #define PT_HIPROC 0x7FFFFFFF
// Segment flags
#define PF_X 0x1
#define PF_W 0x2
#define PF_R 0x4
#define PF_MASKPROC 0xF0000000
typedef unsigned int Elf32_Addr; typedef unsigned int Elf32_Addr;
typedef unsigned short Elf32_Half; typedef unsigned short Elf32_Half;
typedef unsigned int Elf32_Off; typedef unsigned int Elf32_Off;
@ -193,7 +202,7 @@ public:
ElfMachine GetMachine() const { return (ElfMachine)(header->e_machine); } ElfMachine GetMachine() const { return (ElfMachine)(header->e_machine); }
u32 GetEntryPoint() const { return entryPoint; } u32 GetEntryPoint() const { return entryPoint; }
u32 GetFlags() const { return (u32)(header->e_flags); } u32 GetFlags() const { return (u32)(header->e_flags); }
void LoadInto(u32 vaddr); SharedPtr<CodeSet> LoadInto(u32 vaddr);
bool LoadSymbols(); bool LoadSymbols();
int GetNumSegments() const { return (int)(header->e_phnum); } int GetNumSegments() const { return (int)(header->e_phnum); }
@ -249,7 +258,7 @@ const char *ElfReader::GetSectionName(int section) const {
return nullptr; return nullptr;
} }
void ElfReader::LoadInto(u32 vaddr) { SharedPtr<CodeSet> ElfReader::LoadInto(u32 vaddr) {
LOG_DEBUG(Loader, "String section: %i", header->e_shstrndx); LOG_DEBUG(Loader, "String section: %i", header->e_shstrndx);
// Should we relocate? // Should we relocate?
@ -267,19 +276,61 @@ void ElfReader::LoadInto(u32 vaddr) {
u32 segment_addr[32]; u32 segment_addr[32];
u32 base_addr = relocate ? vaddr : 0; u32 base_addr = relocate ? vaddr : 0;
for (unsigned i = 0; i < header->e_phnum; i++) { u32 total_image_size = 0;
Elf32_Phdr* p = segments + i; for (unsigned int i = 0; i < header->e_phnum; ++i) {
LOG_DEBUG(Loader, "Type: %i Vaddr: %08x Filesz: %i Memsz: %i ", p->p_type, p->p_vaddr, Elf32_Phdr* p = &segments[i];
if (p->p_type == PT_LOAD) {
total_image_size += (p->p_memsz + 0xFFF) & ~0xFFF;
}
}
std::vector<u8> program_image(total_image_size);
size_t current_image_position = 0;
SharedPtr<CodeSet> codeset = CodeSet::Create("", 0);
for (unsigned int i = 0; i < header->e_phnum; ++i) {
Elf32_Phdr* p = &segments[i];
LOG_DEBUG(Loader, "Type: %i Vaddr: %08X Filesz: %8X Memsz: %8X ", p->p_type, p->p_vaddr,
p->p_filesz, p->p_memsz); p->p_filesz, p->p_memsz);
if (p->p_type == PT_LOAD) { if (p->p_type == PT_LOAD) {
segment_addr[i] = base_addr + p->p_vaddr; CodeSet::Segment* codeset_segment;
memcpy(Memory::GetPointer(segment_addr[i]), GetSegmentPtr(i), p->p_filesz); u32 permission_flags = p->p_flags & (PF_R | PF_W | PF_X);
LOG_DEBUG(Loader, "Loadable Segment Copied to %08x, size %08x", segment_addr[i], if (permission_flags == (PF_R | PF_X)) {
p->p_memsz); codeset_segment = &codeset->code;
} else if (permission_flags == (PF_R)) {
codeset_segment = &codeset->rodata;
} else if (permission_flags == (PF_R | PF_W)) {
codeset_segment = &codeset->data;
} else {
LOG_ERROR(Loader, "Unexpected ELF PT_LOAD segment id %u with flags %X", i, p->p_flags);
continue;
}
if (codeset_segment->size != 0) {
LOG_ERROR(Loader, "ELF has more than one segment of the same type. Skipping extra segment (id %i)", i);
continue;
}
u32 segment_addr = base_addr + p->p_vaddr;
u32 aligned_size = (p->p_memsz + 0xFFF) & ~0xFFF;
codeset_segment->offset = current_image_position;
codeset_segment->addr = segment_addr;
codeset_segment->size = aligned_size;
memcpy(&program_image[current_image_position], GetSegmentPtr(i), p->p_filesz);
current_image_position += aligned_size;
} }
} }
codeset->entrypoint = base_addr + header->e_entry;
codeset->memory = std::make_shared<std::vector<u8>>(std::move(program_image));
LOG_DEBUG(Loader, "Done loading."); LOG_DEBUG(Loader, "Done loading.");
return codeset;
} }
SectionID ElfReader::GetSectionByName(const char *name, int firstSection) const { SectionID ElfReader::GetSectionByName(const char *name, int firstSection) const {
@ -352,18 +403,18 @@ ResultStatus AppLoader_ELF::Load() {
if (file->ReadBytes(&buffer[0], size) != size) if (file->ReadBytes(&buffer[0], size) != size)
return ResultStatus::Error; return ResultStatus::Error;
Kernel::g_current_process = Kernel::Process::Create(filename, 0); ElfReader elf_reader(&buffer[0]);
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->svc_access_mask.set();
Kernel::g_current_process->address_mappings = default_address_mappings; Kernel::g_current_process->address_mappings = default_address_mappings;
// Attach the default resource limit (APPLICATION) to the process // Attach the default resource limit (APPLICATION) to the process
Kernel::g_current_process->resource_limit = Kernel::ResourceLimit::GetForCategory(Kernel::ResourceLimitCategory::APPLICATION); Kernel::g_current_process->resource_limit = Kernel::ResourceLimit::GetForCategory(Kernel::ResourceLimitCategory::APPLICATION);
ElfReader elf_reader(&buffer[0]); Kernel::g_current_process->Run(48, Kernel::DEFAULT_STACK_SIZE);
elf_reader.LoadInto(Memory::PROCESS_IMAGE_VADDR);
// TODO: Fill application title
Kernel::g_current_process->Run(elf_reader.GetEntryPoint(), 48, Kernel::DEFAULT_STACK_SIZE);
is_loaded = true; is_loaded = true;
return ResultStatus::Success; return ResultStatus::Success;

View File

@ -118,6 +118,9 @@ FileType AppLoader_NCCH::IdentifyType(FileUtil::IOFile& file) {
} }
ResultStatus AppLoader_NCCH::LoadExec() const { ResultStatus AppLoader_NCCH::LoadExec() const {
using Kernel::SharedPtr;
using Kernel::CodeSet;
if (!is_loaded) if (!is_loaded)
return ResultStatus::ErrorNotLoaded; return ResultStatus::ErrorNotLoaded;
@ -126,7 +129,30 @@ ResultStatus AppLoader_NCCH::LoadExec() const {
std::string process_name = Common::StringFromFixedZeroTerminatedBuffer( std::string process_name = Common::StringFromFixedZeroTerminatedBuffer(
(const char*)exheader_header.codeset_info.name, 8); (const char*)exheader_header.codeset_info.name, 8);
u64 program_id = *reinterpret_cast<u64_le const*>(&ncch_header.program_id[0]); u64 program_id = *reinterpret_cast<u64_le const*>(&ncch_header.program_id[0]);
Kernel::g_current_process = Kernel::Process::Create(process_name, program_id);
SharedPtr<CodeSet> codeset = CodeSet::Create(process_name, program_id);
codeset->code.offset = 0;
codeset->code.addr = exheader_header.codeset_info.text.address;
codeset->code.size = exheader_header.codeset_info.text.num_max_pages * Memory::PAGE_SIZE;
codeset->rodata.offset = codeset->code.offset + codeset->code.size;
codeset->rodata.addr = exheader_header.codeset_info.ro.address;
codeset->rodata.size = exheader_header.codeset_info.ro.num_max_pages * Memory::PAGE_SIZE;
// TODO(yuriks): Not sure if the bss size is added to the page-aligned .data size or just
// to the regular size. Playing it safe for now.
u32 bss_page_size = (exheader_header.codeset_info.bss_size + 0xFFF) & ~0xFFF;
code.resize(code.size() + bss_page_size, 0);
codeset->data.offset = codeset->rodata.offset + codeset->rodata.size;
codeset->data.addr = exheader_header.codeset_info.data.address;
codeset->data.size = exheader_header.codeset_info.data.num_max_pages * Memory::PAGE_SIZE + bss_page_size;
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));
// Attach a resource limit to the process based on the resource limit category // Attach a resource limit to the process based on the resource limit category
Kernel::g_current_process->resource_limit = Kernel::ResourceLimit::GetForCategory( Kernel::g_current_process->resource_limit = Kernel::ResourceLimit::GetForCategory(
@ -137,11 +163,9 @@ ResultStatus AppLoader_NCCH::LoadExec() const {
std::copy_n(exheader_header.arm11_kernel_caps.descriptors, kernel_caps.size(), begin(kernel_caps)); std::copy_n(exheader_header.arm11_kernel_caps.descriptors, kernel_caps.size(), begin(kernel_caps));
Kernel::g_current_process->ParseKernelCaps(kernel_caps.data(), kernel_caps.size()); Kernel::g_current_process->ParseKernelCaps(kernel_caps.data(), kernel_caps.size());
Memory::WriteBlock(entry_point, &code[0], code.size());
s32 priority = exheader_header.arm11_system_local_caps.priority; s32 priority = exheader_header.arm11_system_local_caps.priority;
u32 stack_size = exheader_header.codeset_info.stack_size; u32 stack_size = exheader_header.codeset_info.stack_size;
Kernel::g_current_process->Run(entry_point, priority, stack_size); Kernel::g_current_process->Run(priority, stack_size);
return ResultStatus::Success; return ResultStatus::Success;
} }
return ResultStatus::Error; return ResultStatus::Error;

View File

@ -32,7 +32,6 @@ struct MemoryArea {
// We don't declare the IO regions in here since its handled by other means. // We don't declare the IO regions in here since its handled by other means.
static MemoryArea memory_areas[] = { static MemoryArea memory_areas[] = {
{PROCESS_IMAGE_VADDR, PROCESS_IMAGE_MAX_SIZE, "Process Image"}, // ExeFS:/.code is loaded here
{HEAP_VADDR, HEAP_SIZE, "Heap"}, // Application heap (main memory) {HEAP_VADDR, HEAP_SIZE, "Heap"}, // Application heap (main memory)
{SHARED_MEMORY_VADDR, SHARED_MEMORY_SIZE, "Shared Memory"}, // Shared memory {SHARED_MEMORY_VADDR, SHARED_MEMORY_SIZE, "Shared Memory"}, // Shared memory
{LINEAR_HEAP_VADDR, LINEAR_HEAP_SIZE, "Linear Heap"}, // Linear heap (main memory) {LINEAR_HEAP_VADDR, LINEAR_HEAP_SIZE, "Linear Heap"}, // Linear heap (main memory)
@ -132,13 +131,13 @@ VAddr PhysicalToVirtualAddress(const PAddr addr) {
return addr | 0x80000000; return addr | 0x80000000;
} }
// TODO(yuriks): Move this into Process
static Kernel::VMManager address_space;
void Init() { void Init() {
using namespace Kernel;
InitMemoryMap(); InitMemoryMap();
LOG_DEBUG(HW_Memory, "initialized OK");
}
void InitLegacyAddressSpace(Kernel::VMManager& address_space) {
using namespace Kernel;
for (MemoryArea& area : memory_areas) { for (MemoryArea& area : memory_areas) {
auto block = std::make_shared<std::vector<u8>>(area.size); auto block = std::make_shared<std::vector<u8>>(area.size);
@ -152,14 +151,11 @@ void Init() {
auto shared_page_vma = address_space.MapBackingMemory(SHARED_PAGE_VADDR, auto shared_page_vma = address_space.MapBackingMemory(SHARED_PAGE_VADDR,
(u8*)&SharedPage::shared_page, SHARED_PAGE_SIZE, MemoryState::Shared).MoveFrom(); (u8*)&SharedPage::shared_page, SHARED_PAGE_SIZE, MemoryState::Shared).MoveFrom();
address_space.Reprotect(shared_page_vma, VMAPermission::Read); address_space.Reprotect(shared_page_vma, VMAPermission::Read);
LOG_DEBUG(HW_Memory, "initialized OK");
} }
void Shutdown() { void Shutdown() {
heap_map.clear(); heap_map.clear();
heap_linear_map.clear(); heap_linear_map.clear();
address_space.Reset();
LOG_DEBUG(HW_Memory, "shutdown OK"); LOG_DEBUG(HW_Memory, "shutdown OK");
} }

View File

@ -6,9 +6,14 @@
#include "common/common_types.h" #include "common/common_types.h"
namespace Kernel {
class VMManager;
}
namespace Memory { namespace Memory {
void Init(); void Init();
void InitLegacyAddressSpace(Kernel::VMManager& address_space);
void Shutdown(); void Shutdown();
/** /**

View File

@ -59,14 +59,12 @@ static void MapPages(u32 base, u32 size, u8* memory, PageType type) {
while (base != end) { while (base != end) {
ASSERT_MSG(base < PageTable::NUM_ENTRIES, "out of range mapping at %08X", base); ASSERT_MSG(base < PageTable::NUM_ENTRIES, "out of range mapping at %08X", base);
if (current_page_table->attributes[base] != PageType::Unmapped && type != PageType::Unmapped) {
LOG_ERROR(HW_Memory, "overlapping memory ranges at %08X", base * PAGE_SIZE);
}
current_page_table->attributes[base] = type; current_page_table->attributes[base] = type;
current_page_table->pointers[base] = memory; current_page_table->pointers[base] = memory;
base += 1; base += 1;
memory += PAGE_SIZE; if (memory != nullptr)
memory += PAGE_SIZE;
} }
} }