diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/core/hle/kernel/process.cpp | 6 | ||||
| -rw-r--r-- | src/core/hle/kernel/thread.cpp | 2 | ||||
| -rw-r--r-- | src/core/loader/loader.cpp | 4 | ||||
| -rw-r--r-- | src/core/loader/nro.cpp | 17 | ||||
| -rw-r--r-- | src/core/loader/nro.h | 7 | ||||
| -rw-r--r-- | src/core/loader/nso.cpp | 50 | ||||
| -rw-r--r-- | src/core/loader/nso.h | 8 | ||||
| -rw-r--r-- | src/core/memory.h | 4 |
8 files changed, 40 insertions, 58 deletions
diff --git a/src/core/hle/kernel/process.cpp b/src/core/hle/kernel/process.cpp index 2a80c2492..84ebdbc58 100644 --- a/src/core/hle/kernel/process.cpp +++ b/src/core/hle/kernel/process.cpp | |||
| @@ -147,9 +147,9 @@ void Process::LoadModule(SharedPtr<CodeSet> module_, VAddr base_addr) { | |||
| 147 | }; | 147 | }; |
| 148 | 148 | ||
| 149 | // Map CodeSet segments | 149 | // Map CodeSet segments |
| 150 | MapSegment(module_->code, VMAPermission::ReadExecute, MemoryState::Code); | 150 | MapSegment(module_->code, VMAPermission::ReadWrite, MemoryState::Private); |
| 151 | MapSegment(module_->rodata, VMAPermission::Read, MemoryState::Code); | 151 | MapSegment(module_->rodata, VMAPermission::Read, MemoryState::Static); |
| 152 | MapSegment(module_->data, VMAPermission::ReadWrite, MemoryState::Private); | 152 | MapSegment(module_->data, VMAPermission::ReadWrite, MemoryState::Static); |
| 153 | } | 153 | } |
| 154 | 154 | ||
| 155 | VAddr Process::GetLinearHeapAreaAddress() const { | 155 | VAddr Process::GetLinearHeapAreaAddress() const { |
diff --git a/src/core/hle/kernel/thread.cpp b/src/core/hle/kernel/thread.cpp index 736be50db..c01d08ebb 100644 --- a/src/core/hle/kernel/thread.cpp +++ b/src/core/hle/kernel/thread.cpp | |||
| @@ -429,7 +429,7 @@ ResultVal<SharedPtr<Thread>> Thread::Create(std::string name, VAddr entry_point, | |||
| 429 | // Map the page to the current process' address space. | 429 | // Map the page to the current process' address space. |
| 430 | // TODO(Subv): Find the correct MemoryState for this region. | 430 | // TODO(Subv): Find the correct MemoryState for this region. |
| 431 | vm_manager.MapMemoryBlock(Memory::TLS_AREA_VADDR + available_page * Memory::PAGE_SIZE, | 431 | vm_manager.MapMemoryBlock(Memory::TLS_AREA_VADDR + available_page * Memory::PAGE_SIZE, |
| 432 | linheap_memory, offset, Memory::PAGE_SIZE, MemoryState::Private); | 432 | linheap_memory, offset, Memory::PAGE_SIZE, MemoryState::Static); |
| 433 | } | 433 | } |
| 434 | 434 | ||
| 435 | // Mark the slot as used | 435 | // Mark the slot as used |
diff --git a/src/core/loader/loader.cpp b/src/core/loader/loader.cpp index d96b9f1f0..73318c584 100644 --- a/src/core/loader/loader.cpp +++ b/src/core/loader/loader.cpp | |||
| @@ -121,11 +121,11 @@ static std::unique_ptr<AppLoader> GetFileLoader(FileUtil::IOFile&& file, FileTyp | |||
| 121 | 121 | ||
| 122 | // NX NSO file format. | 122 | // NX NSO file format. |
| 123 | case FileType::NSO: | 123 | case FileType::NSO: |
| 124 | return std::make_unique<AppLoader_NSO>(std::move(file), filename, filepath); | 124 | return std::make_unique<AppLoader_NSO>(std::move(file), filepath); |
| 125 | 125 | ||
| 126 | // NX NRO file format. | 126 | // NX NRO file format. |
| 127 | case FileType::NRO: | 127 | case FileType::NRO: |
| 128 | return std::make_unique<AppLoader_NRO>(std::move(file), filename, filepath); | 128 | return std::make_unique<AppLoader_NRO>(std::move(file), filepath); |
| 129 | 129 | ||
| 130 | default: | 130 | default: |
| 131 | return nullptr; | 131 | return nullptr; |
diff --git a/src/core/loader/nro.cpp b/src/core/loader/nro.cpp index ed638e1fa..753e7e08b 100644 --- a/src/core/loader/nro.cpp +++ b/src/core/loader/nro.cpp | |||
| @@ -75,17 +75,6 @@ static std::vector<u8> ReadSegment(FileUtil::IOFile& file, const NroSegmentHeade | |||
| 75 | return data; | 75 | return data; |
| 76 | } | 76 | } |
| 77 | 77 | ||
| 78 | VAddr AppLoader_NRO::GetEntryPoint(VAddr load_base) const { | ||
| 79 | // Find nnMain function, set entrypoint to that address | ||
| 80 | const auto& search = exports.find("nnMain"); | ||
| 81 | if (search != exports.end()) { | ||
| 82 | return load_base + search->second; | ||
| 83 | } | ||
| 84 | const VAddr entry_point{load_base + sizeof(NroHeader)}; | ||
| 85 | LOG_ERROR(Loader, "Unable to find entrypoint, defaulting to: 0x%llx", entry_point); | ||
| 86 | return entry_point; | ||
| 87 | } | ||
| 88 | |||
| 89 | bool AppLoader_NRO::LoadNro(const std::string& path, VAddr load_base) { | 78 | bool AppLoader_NRO::LoadNro(const std::string& path, VAddr load_base) { |
| 90 | FileUtil::IOFile file(path, "rb"); | 79 | FileUtil::IOFile file(path, "rb"); |
| 91 | if (!file.IsOpen()) { | 80 | if (!file.IsOpen()) { |
| @@ -152,9 +141,9 @@ ResultStatus AppLoader_NRO::Load() { | |||
| 152 | } | 141 | } |
| 153 | 142 | ||
| 154 | // Load and relocate "main" and "sdk" NSO | 143 | // Load and relocate "main" and "sdk" NSO |
| 155 | static constexpr VAddr main_base{0x10000000}; | 144 | static constexpr VAddr base_addr{Memory::PROCESS_IMAGE_VADDR}; |
| 156 | Kernel::g_current_process = Kernel::Process::Create("main"); | 145 | Kernel::g_current_process = Kernel::Process::Create("main"); |
| 157 | if (!LoadNro(filepath, main_base)) { | 146 | if (!LoadNro(filepath, base_addr)) { |
| 158 | return ResultStatus::ErrorInvalidFormat; | 147 | return ResultStatus::ErrorInvalidFormat; |
| 159 | } | 148 | } |
| 160 | 149 | ||
| @@ -162,7 +151,7 @@ ResultStatus AppLoader_NRO::Load() { | |||
| 162 | Kernel::g_current_process->address_mappings = default_address_mappings; | 151 | Kernel::g_current_process->address_mappings = default_address_mappings; |
| 163 | Kernel::g_current_process->resource_limit = | 152 | Kernel::g_current_process->resource_limit = |
| 164 | Kernel::ResourceLimit::GetForCategory(Kernel::ResourceLimitCategory::APPLICATION); | 153 | Kernel::ResourceLimit::GetForCategory(Kernel::ResourceLimitCategory::APPLICATION); |
| 165 | Kernel::g_current_process->Run(GetEntryPoint(main_base), 48, Kernel::DEFAULT_STACK_SIZE); | 154 | Kernel::g_current_process->Run(base_addr, 48, Kernel::DEFAULT_STACK_SIZE); |
| 166 | 155 | ||
| 167 | ResolveImports(); | 156 | ResolveImports(); |
| 168 | 157 | ||
diff --git a/src/core/loader/nro.h b/src/core/loader/nro.h index d145b68d5..c3c7622fd 100644 --- a/src/core/loader/nro.h +++ b/src/core/loader/nro.h | |||
| @@ -17,9 +17,8 @@ namespace Loader { | |||
| 17 | /// Loads an NRO file | 17 | /// Loads an NRO file |
| 18 | class AppLoader_NRO final : public AppLoader, Linker { | 18 | class AppLoader_NRO final : public AppLoader, Linker { |
| 19 | public: | 19 | public: |
| 20 | AppLoader_NRO(FileUtil::IOFile&& file, std::string filename, std::string filepath) | 20 | AppLoader_NRO(FileUtil::IOFile&& file, std::string filepath) |
| 21 | : AppLoader(std::move(file)), filename(std::move(filename)), filepath(std::move(filepath)) { | 21 | : AppLoader(std::move(file)), filepath(std::move(filepath)) {} |
| 22 | } | ||
| 23 | 22 | ||
| 24 | /** | 23 | /** |
| 25 | * Returns the type of the file | 24 | * Returns the type of the file |
| @@ -35,10 +34,8 @@ public: | |||
| 35 | ResultStatus Load() override; | 34 | ResultStatus Load() override; |
| 36 | 35 | ||
| 37 | private: | 36 | private: |
| 38 | VAddr GetEntryPoint(VAddr load_base) const; | ||
| 39 | bool LoadNro(const std::string& path, VAddr load_base); | 37 | bool LoadNro(const std::string& path, VAddr load_base); |
| 40 | 38 | ||
| 41 | std::string filename; | ||
| 42 | std::string filepath; | 39 | std::string filepath; |
| 43 | }; | 40 | }; |
| 44 | 41 | ||
diff --git a/src/core/loader/nso.cpp b/src/core/loader/nso.cpp index 4d885fef7..ac8d12ecc 100644 --- a/src/core/loader/nso.cpp +++ b/src/core/loader/nso.cpp | |||
| @@ -70,31 +70,21 @@ static std::vector<u8> ReadSegment(FileUtil::IOFile& file, const NsoSegmentHeade | |||
| 70 | 70 | ||
| 71 | std::vector<u8> uncompressed_data; | 71 | std::vector<u8> uncompressed_data; |
| 72 | uncompressed_data.resize(header.size); | 72 | uncompressed_data.resize(header.size); |
| 73 | const int bytes_uncompressed = | 73 | const int bytes_uncompressed = LZ4_decompress_safe( |
| 74 | LZ4_decompress_safe_partial(reinterpret_cast<const char*>(compressed_data.data()), | 74 | reinterpret_cast<const char*>(compressed_data.data()), |
| 75 | reinterpret_cast<char*>(uncompressed_data.data()), | 75 | reinterpret_cast<char*>(uncompressed_data.data()), compressed_size, header.size); |
| 76 | compressed_size, header.size, header.size); | ||
| 77 | 76 | ||
| 78 | ASSERT_MSG(bytes_uncompressed == header.size, "%d != %d", bytes_uncompressed, header.size); | 77 | ASSERT_MSG(bytes_uncompressed == header.size && bytes_uncompressed == uncompressed_data.size(), |
| 78 | "%d != %d != %d", bytes_uncompressed, header.size, uncompressed_data.size()); | ||
| 79 | 79 | ||
| 80 | return uncompressed_data; | 80 | return uncompressed_data; |
| 81 | } | 81 | } |
| 82 | 82 | ||
| 83 | VAddr AppLoader_NSO::GetEntryPoint(VAddr load_base) const { | ||
| 84 | // Find nnMain function, set entrypoint to that address | ||
| 85 | const auto& search = exports.find("nnMain"); | ||
| 86 | if (search != exports.end()) { | ||
| 87 | return search->second; | ||
| 88 | } | ||
| 89 | LOG_ERROR(Loader, "Unable to find entrypoint, defaulting to: 0x%llx", load_base); | ||
| 90 | return load_base; | ||
| 91 | } | ||
| 92 | |||
| 93 | static constexpr u32 PageAlignSize(u32 size) { | 83 | static constexpr u32 PageAlignSize(u32 size) { |
| 94 | return (size + Memory::PAGE_MASK) & ~Memory::PAGE_MASK; | 84 | return (size + Memory::PAGE_MASK) & ~Memory::PAGE_MASK; |
| 95 | } | 85 | } |
| 96 | 86 | ||
| 97 | bool AppLoader_NSO::LoadNso(const std::string& path, VAddr load_base) { | 87 | VAddr AppLoader_NSO::LoadNso(const std::string& path, VAddr load_base, bool relocate) { |
| 98 | FileUtil::IOFile file(path, "rb"); | 88 | FileUtil::IOFile file(path, "rb"); |
| 99 | if (!file.IsOpen()) { | 89 | if (!file.IsOpen()) { |
| 100 | return {}; | 90 | return {}; |
| @@ -137,11 +127,12 @@ bool AppLoader_NSO::LoadNso(const std::string& path, VAddr load_base) { | |||
| 137 | bss_size = PageAlignSize(mod_header.bss_end_offset - mod_header.bss_start_offset); | 127 | bss_size = PageAlignSize(mod_header.bss_end_offset - mod_header.bss_start_offset); |
| 138 | codeset->data.size += bss_size; | 128 | codeset->data.size += bss_size; |
| 139 | } | 129 | } |
| 140 | program_image.resize(PageAlignSize(static_cast<u32>(program_image.size()) + bss_size)); | 130 | const u32 image_size{PageAlignSize(static_cast<u32>(program_image.size()) + bss_size)}; |
| 131 | program_image.resize(image_size); | ||
| 141 | 132 | ||
| 142 | // Relocate symbols if there was a proper MOD header - This must happen after the image has been | 133 | // Relocate symbols if there was a proper MOD header - This must happen after the image has been |
| 143 | // loaded into memory | 134 | // loaded into memory |
| 144 | if (has_mod_header) { | 135 | if (has_mod_header && relocate) { |
| 145 | Relocate(program_image, module_offset + mod_header.dynamic_offset, load_base); | 136 | Relocate(program_image, module_offset + mod_header.dynamic_offset, load_base); |
| 146 | } | 137 | } |
| 147 | 138 | ||
| @@ -150,7 +141,7 @@ bool AppLoader_NSO::LoadNso(const std::string& path, VAddr load_base) { | |||
| 150 | codeset->memory = std::make_shared<std::vector<u8>>(std::move(program_image)); | 141 | codeset->memory = std::make_shared<std::vector<u8>>(std::move(program_image)); |
| 151 | Kernel::g_current_process->LoadModule(codeset, load_base); | 142 | Kernel::g_current_process->LoadModule(codeset, load_base); |
| 152 | 143 | ||
| 153 | return true; | 144 | return load_base + image_size; |
| 154 | } | 145 | } |
| 155 | 146 | ||
| 156 | ResultStatus AppLoader_NSO::Load() { | 147 | ResultStatus AppLoader_NSO::Load() { |
| @@ -161,22 +152,29 @@ ResultStatus AppLoader_NSO::Load() { | |||
| 161 | return ResultStatus::Error; | 152 | return ResultStatus::Error; |
| 162 | } | 153 | } |
| 163 | 154 | ||
| 164 | // Load and relocate "main" and "sdk" NSO | 155 | // Load and relocate "rtld" NSO |
| 165 | static constexpr VAddr main_base{0x710000000}; | 156 | static constexpr VAddr base_addr{Memory::PROCESS_IMAGE_VADDR}; |
| 166 | Kernel::g_current_process = Kernel::Process::Create("main"); | 157 | Kernel::g_current_process = Kernel::Process::Create("main"); |
| 167 | if (!LoadNso(filepath, main_base)) { | 158 | VAddr next_base_addr{LoadNso(filepath, base_addr)}; |
| 159 | if (!next_base_addr) { | ||
| 168 | return ResultStatus::ErrorInvalidFormat; | 160 | return ResultStatus::ErrorInvalidFormat; |
| 169 | } | 161 | } |
| 170 | const std::string sdkpath = filepath.substr(0, filepath.find_last_of("/\\")) + "/sdk"; | 162 | |
| 171 | if (!LoadNso(sdkpath, 0x720000000)) { | 163 | // Load and relocate remaining submodules |
| 172 | LOG_WARNING(Loader, "failed to find SDK NSO"); | 164 | for (const auto& module_name : {"main", "sdk", "subsdk0", "subsdk1"}) { |
| 165 | const std::string module_path = | ||
| 166 | filepath.substr(0, filepath.find_last_of("/\\")) + "/" + module_name; | ||
| 167 | next_base_addr = LoadNso(module_path, next_base_addr); | ||
| 168 | if (!next_base_addr) { | ||
| 169 | LOG_WARNING(Loader, "failed to find load module: %s", module_name); | ||
| 170 | } | ||
| 173 | } | 171 | } |
| 174 | 172 | ||
| 175 | Kernel::g_current_process->svc_access_mask.set(); | 173 | Kernel::g_current_process->svc_access_mask.set(); |
| 176 | Kernel::g_current_process->address_mappings = default_address_mappings; | 174 | Kernel::g_current_process->address_mappings = default_address_mappings; |
| 177 | Kernel::g_current_process->resource_limit = | 175 | Kernel::g_current_process->resource_limit = |
| 178 | Kernel::ResourceLimit::GetForCategory(Kernel::ResourceLimitCategory::APPLICATION); | 176 | Kernel::ResourceLimit::GetForCategory(Kernel::ResourceLimitCategory::APPLICATION); |
| 179 | Kernel::g_current_process->Run(GetEntryPoint(main_base), 48, Kernel::DEFAULT_STACK_SIZE); | 177 | Kernel::g_current_process->Run(base_addr, 48, Kernel::DEFAULT_STACK_SIZE); |
| 180 | 178 | ||
| 181 | ResolveImports(); | 179 | ResolveImports(); |
| 182 | 180 | ||
diff --git a/src/core/loader/nso.h b/src/core/loader/nso.h index 431b960b1..c29803d81 100644 --- a/src/core/loader/nso.h +++ b/src/core/loader/nso.h | |||
| @@ -17,8 +17,8 @@ namespace Loader { | |||
| 17 | /// Loads an NSO file | 17 | /// Loads an NSO file |
| 18 | class AppLoader_NSO final : public AppLoader, Linker { | 18 | class AppLoader_NSO final : public AppLoader, Linker { |
| 19 | public: | 19 | public: |
| 20 | AppLoader_NSO(FileUtil::IOFile&& file, std::string filename, std::string filepath) | 20 | AppLoader_NSO(FileUtil::IOFile&& file, std::string filepath) |
| 21 | : AppLoader(std::move(file)), filename(std::move(filename)), filepath(std::move(filepath)) { | 21 | : AppLoader(std::move(file)), filepath(std::move(filepath)) { |
| 22 | } | 22 | } |
| 23 | 23 | ||
| 24 | /** | 24 | /** |
| @@ -35,10 +35,8 @@ public: | |||
| 35 | ResultStatus Load() override; | 35 | ResultStatus Load() override; |
| 36 | 36 | ||
| 37 | private: | 37 | private: |
| 38 | VAddr GetEntryPoint(VAddr load_base) const; | 38 | VAddr LoadNso(const std::string& path, VAddr load_base, bool relocate = false); |
| 39 | bool LoadNso(const std::string& path, VAddr load_base); | ||
| 40 | 39 | ||
| 41 | std::string filename; | ||
| 42 | std::string filepath; | 40 | std::string filepath; |
| 43 | }; | 41 | }; |
| 44 | 42 | ||
diff --git a/src/core/memory.h b/src/core/memory.h index e8d796d24..e14d68654 100644 --- a/src/core/memory.h +++ b/src/core/memory.h | |||
| @@ -65,8 +65,8 @@ enum : PAddr { | |||
| 65 | /// Virtual user-space memory regions | 65 | /// Virtual user-space memory regions |
| 66 | enum : VAddr { | 66 | enum : VAddr { |
| 67 | /// Where the application text, data and bss reside. | 67 | /// Where the application text, data and bss reside. |
| 68 | PROCESS_IMAGE_VADDR = 0x00100000, | 68 | PROCESS_IMAGE_VADDR = 0x08000000, |
| 69 | PROCESS_IMAGE_MAX_SIZE = 0x03F00000, | 69 | PROCESS_IMAGE_MAX_SIZE = 0x08000000, |
| 70 | PROCESS_IMAGE_VADDR_END = PROCESS_IMAGE_VADDR + PROCESS_IMAGE_MAX_SIZE, | 70 | PROCESS_IMAGE_VADDR_END = PROCESS_IMAGE_VADDR + PROCESS_IMAGE_MAX_SIZE, |
| 71 | 71 | ||
| 72 | /// Area where IPC buffers are mapped onto. | 72 | /// Area where IPC buffers are mapped onto. |