summaryrefslogtreecommitdiff
path: root/src/core/loader/3dsx.cpp
diff options
context:
space:
mode:
authorGravatar Yuri Kunde Schlesner2015-07-11 20:07:49 -0700
committerGravatar Yuri Kunde Schlesner2015-07-11 20:07:49 -0700
commit4e900d56f3dd2b5c9871b523689322028123d891 (patch)
tree2b233263cff7c001506f660373e2364c8e702637 /src/core/loader/3dsx.cpp
parentMerge pull request #914 from yuriks/bitfield-mask (diff)
parentCore: Properly configure address space when loading a binary (diff)
downloadyuzu-4e900d56f3dd2b5c9871b523689322028123d891.tar.gz
yuzu-4e900d56f3dd2b5c9871b523689322028123d891.tar.xz
yuzu-4e900d56f3dd2b5c9871b523689322028123d891.zip
Merge pull request #912 from yuriks/process-loading
Core: Properly configure address space during binary loading
Diffstat (limited to 'src/core/loader/3dsx.cpp')
-rw-r--r--src/core/loader/3dsx.cpp69
1 files changed, 45 insertions, 24 deletions
diff --git a/src/core/loader/3dsx.cpp b/src/core/loader/3dsx.cpp
index 14aeebebb..055661363 100644
--- a/src/core/loader/3dsx.cpp
+++ b/src/core/loader/3dsx.cpp
@@ -19,7 +19,7 @@
19 19
20namespace Loader { 20namespace Loader {
21 21
22/** 22/*
23 * File layout: 23 * File layout:
24 * - File header 24 * - File header
25 * - Code, rodata and data relocation table headers 25 * - Code, rodata and data relocation table headers
@@ -39,13 +39,16 @@ namespace Loader {
39 * The entrypoint is always the start of the code segment. 39 * The entrypoint is always the start of the code segment.
40 * The BSS section must be cleared manually by the application. 40 * The BSS section must be cleared manually by the application.
41 */ 41 */
42
42enum THREEDSX_Error { 43enum THREEDSX_Error {
43 ERROR_NONE = 0, 44 ERROR_NONE = 0,
44 ERROR_READ = 1, 45 ERROR_READ = 1,
45 ERROR_FILE = 2, 46 ERROR_FILE = 2,
46 ERROR_ALLOC = 3 47 ERROR_ALLOC = 3
47}; 48};
49
48static const u32 RELOCBUFSIZE = 512; 50static const u32 RELOCBUFSIZE = 512;
51static const unsigned int NUM_SEGMENTS = 3;
49 52
50// File header 53// File header
51#pragma pack(1) 54#pragma pack(1)
@@ -98,7 +101,10 @@ static u32 TranslateAddr(u32 addr, const THREEloadinfo *loadinfo, u32* offsets)
98 return loadinfo->seg_addrs[2] + addr - offsets[1]; 101 return loadinfo->seg_addrs[2] + addr - offsets[1];
99} 102}
100 103
101static THREEDSX_Error Load3DSXFile(FileUtil::IOFile& file, u32 base_addr) 104using Kernel::SharedPtr;
105using Kernel::CodeSet;
106
107static THREEDSX_Error Load3DSXFile(FileUtil::IOFile& file, u32 base_addr, SharedPtr<CodeSet>* out_codeset)
102{ 108{
103 if (!file.IsOpen()) 109 if (!file.IsOpen())
104 return ERROR_FILE; 110 return ERROR_FILE;
@@ -116,15 +122,13 @@ static THREEDSX_Error Load3DSXFile(FileUtil::IOFile& file, u32 base_addr)
116 loadinfo.seg_sizes[1] = (hdr.rodata_seg_size + 0xFFF) &~0xFFF; 122 loadinfo.seg_sizes[1] = (hdr.rodata_seg_size + 0xFFF) &~0xFFF;
117 loadinfo.seg_sizes[2] = (hdr.data_seg_size + 0xFFF) &~0xFFF; 123 loadinfo.seg_sizes[2] = (hdr.data_seg_size + 0xFFF) &~0xFFF;
118 u32 offsets[2] = { loadinfo.seg_sizes[0], loadinfo.seg_sizes[0] + loadinfo.seg_sizes[1] }; 124 u32 offsets[2] = { loadinfo.seg_sizes[0], loadinfo.seg_sizes[0] + loadinfo.seg_sizes[1] };
119 u32 data_load_size = (hdr.data_seg_size - hdr.bss_size + 0xFFF) &~0xFFF; 125 u32 n_reloc_tables = hdr.reloc_hdr_size / sizeof(u32);
120 u32 bss_load_size = loadinfo.seg_sizes[2] - data_load_size; 126 std::vector<u8> program_image(loadinfo.seg_sizes[0] + loadinfo.seg_sizes[1] + loadinfo.seg_sizes[2]);
121 u32 n_reloc_tables = hdr.reloc_hdr_size / 4;
122 std::vector<u8> all_mem(loadinfo.seg_sizes[0] + loadinfo.seg_sizes[1] + loadinfo.seg_sizes[2] + 3 * n_reloc_tables);
123 127
124 loadinfo.seg_addrs[0] = base_addr; 128 loadinfo.seg_addrs[0] = base_addr;
125 loadinfo.seg_addrs[1] = loadinfo.seg_addrs[0] + loadinfo.seg_sizes[0]; 129 loadinfo.seg_addrs[1] = loadinfo.seg_addrs[0] + loadinfo.seg_sizes[0];
126 loadinfo.seg_addrs[2] = loadinfo.seg_addrs[1] + loadinfo.seg_sizes[1]; 130 loadinfo.seg_addrs[2] = loadinfo.seg_addrs[1] + loadinfo.seg_sizes[1];
127 loadinfo.seg_ptrs[0] = &all_mem[0]; 131 loadinfo.seg_ptrs[0] = program_image.data();
128 loadinfo.seg_ptrs[1] = loadinfo.seg_ptrs[0] + loadinfo.seg_sizes[0]; 132 loadinfo.seg_ptrs[1] = loadinfo.seg_ptrs[0] + loadinfo.seg_sizes[0];
129 loadinfo.seg_ptrs[2] = loadinfo.seg_ptrs[1] + loadinfo.seg_sizes[1]; 133 loadinfo.seg_ptrs[2] = loadinfo.seg_ptrs[1] + loadinfo.seg_sizes[1];
130 134
@@ -132,10 +136,9 @@ static THREEDSX_Error Load3DSXFile(FileUtil::IOFile& file, u32 base_addr)
132 file.Seek(hdr.header_size, SEEK_SET); 136 file.Seek(hdr.header_size, SEEK_SET);
133 137
134 // Read the relocation headers 138 // Read the relocation headers
135 u32* relocs = (u32*)(loadinfo.seg_ptrs[2] + hdr.data_seg_size); 139 std::vector<u32> relocs(n_reloc_tables * NUM_SEGMENTS);
136 140 for (unsigned int current_segment = 0; current_segment < NUM_SEGMENTS; ++current_segment) {
137 for (unsigned current_segment : {0, 1, 2}) { 141 size_t size = n_reloc_tables * sizeof(u32);
138 size_t size = n_reloc_tables * 4;
139 if (file.ReadBytes(&relocs[current_segment * n_reloc_tables], size) != size) 142 if (file.ReadBytes(&relocs[current_segment * n_reloc_tables], size) != size)
140 return ERROR_READ; 143 return ERROR_READ;
141 } 144 }
@@ -152,7 +155,7 @@ static THREEDSX_Error Load3DSXFile(FileUtil::IOFile& file, u32 base_addr)
152 memset((char*)loadinfo.seg_ptrs[2] + hdr.data_seg_size - hdr.bss_size, 0, hdr.bss_size); 155 memset((char*)loadinfo.seg_ptrs[2] + hdr.data_seg_size - hdr.bss_size, 0, hdr.bss_size);
153 156
154 // Relocate the segments 157 // Relocate the segments
155 for (unsigned current_segment : {0, 1, 2}) { 158 for (unsigned int current_segment = 0; current_segment < NUM_SEGMENTS; ++current_segment) {
156 for (unsigned current_segment_reloc_table = 0; current_segment_reloc_table < n_reloc_tables; current_segment_reloc_table++) { 159 for (unsigned current_segment_reloc_table = 0; current_segment_reloc_table < n_reloc_tables; current_segment_reloc_table++) {
157 u32 n_relocs = relocs[current_segment * n_reloc_tables + current_segment_reloc_table]; 160 u32 n_relocs = relocs[current_segment * n_reloc_tables + current_segment_reloc_table];
158 if (current_segment_reloc_table >= 2) { 161 if (current_segment_reloc_table >= 2) {
@@ -160,7 +163,7 @@ static THREEDSX_Error Load3DSXFile(FileUtil::IOFile& file, u32 base_addr)
160 file.Seek(n_relocs*sizeof(THREEDSX_Reloc), SEEK_CUR); 163 file.Seek(n_relocs*sizeof(THREEDSX_Reloc), SEEK_CUR);
161 continue; 164 continue;
162 } 165 }
163 static THREEDSX_Reloc reloc_table[RELOCBUFSIZE]; 166 THREEDSX_Reloc reloc_table[RELOCBUFSIZE];
164 167
165 u32* pos = (u32*)loadinfo.seg_ptrs[current_segment]; 168 u32* pos = (u32*)loadinfo.seg_ptrs[current_segment];
166 const u32* end_pos = pos + (loadinfo.seg_sizes[current_segment] / 4); 169 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)
179 pos += table.skip; 182 pos += table.skip;
180 s32 num_patches = table.patch; 183 s32 num_patches = table.patch;
181 while (0 < num_patches && pos < end_pos) { 184 while (0 < num_patches && pos < end_pos) {
182 u32 in_addr = (char*)pos - (char*)&all_mem[0]; 185 u32 in_addr = (u8*)pos - program_image.data();
183 u32 addr = TranslateAddr(*pos, &loadinfo, offsets); 186 u32 addr = TranslateAddr(*pos, &loadinfo, offsets);
184 LOG_TRACE(Loader, "Patching %08X <-- rel(%08X,%d) (%08X)\n", 187 LOG_TRACE(Loader, "Patching %08X <-- rel(%08X,%d) (%08X)\n",
185 base_addr + in_addr, addr, current_segment_reloc_table, *pos); 188 base_addr + in_addr, addr, current_segment_reloc_table, *pos);
@@ -201,14 +204,29 @@ static THREEDSX_Error Load3DSXFile(FileUtil::IOFile& file, u32 base_addr)
201 } 204 }
202 } 205 }
203 206
204 // Write the data 207 // Create the CodeSet
205 memcpy(Memory::GetPointer(base_addr), &all_mem[0], loadinfo.seg_sizes[0] + loadinfo.seg_sizes[1] + loadinfo.seg_sizes[2]); 208 SharedPtr<CodeSet> code_set = CodeSet::Create("", 0);
209
210 code_set->code.offset = loadinfo.seg_ptrs[0] - program_image.data();
211 code_set->code.addr = loadinfo.seg_addrs[0];
212 code_set->code.size = loadinfo.seg_sizes[0];
213
214 code_set->rodata.offset = loadinfo.seg_ptrs[1] - program_image.data();
215 code_set->rodata.addr = loadinfo.seg_addrs[1];
216 code_set->rodata.size = loadinfo.seg_sizes[1];
206 217
207 LOG_DEBUG(Loader, "CODE: %u pages\n", loadinfo.seg_sizes[0] / 0x1000); 218 code_set->data.offset = loadinfo.seg_ptrs[2] - program_image.data();
208 LOG_DEBUG(Loader, "RODATA: %u pages\n", loadinfo.seg_sizes[1] / 0x1000); 219 code_set->data.addr = loadinfo.seg_addrs[2];
209 LOG_DEBUG(Loader, "DATA: %u pages\n", data_load_size / 0x1000); 220 code_set->data.size = loadinfo.seg_sizes[2];
210 LOG_DEBUG(Loader, "BSS: %u pages\n", bss_load_size / 0x1000);
211 221
222 code_set->entrypoint = code_set->code.addr;
223 code_set->memory = std::make_shared<std::vector<u8>>(std::move(program_image));
224
225 LOG_DEBUG(Loader, "code size: 0x%X", loadinfo.seg_sizes[0]);
226 LOG_DEBUG(Loader, "rodata size: 0x%X", loadinfo.seg_sizes[1]);
227 LOG_DEBUG(Loader, "data size: 0x%X (including 0x%X of bss)", loadinfo.seg_sizes[2], hdr.bss_size);
228
229 *out_codeset = code_set;
212 return ERROR_NONE; 230 return ERROR_NONE;
213} 231}
214 232
@@ -231,16 +249,19 @@ ResultStatus AppLoader_THREEDSX::Load() {
231 if (!file->IsOpen()) 249 if (!file->IsOpen())
232 return ResultStatus::Error; 250 return ResultStatus::Error;
233 251
234 Kernel::g_current_process = Kernel::Process::Create(filename, 0); 252 SharedPtr<CodeSet> codeset;
253 if (Load3DSXFile(*file, Memory::PROCESS_IMAGE_VADDR, &codeset) != ERROR_NONE)
254 return ResultStatus::Error;
255 codeset->name = filename;
256
257 Kernel::g_current_process = Kernel::Process::Create(std::move(codeset));
235 Kernel::g_current_process->svc_access_mask.set(); 258 Kernel::g_current_process->svc_access_mask.set();
236 Kernel::g_current_process->address_mappings = default_address_mappings; 259 Kernel::g_current_process->address_mappings = default_address_mappings;
237 260
238 // Attach the default resource limit (APPLICATION) to the process 261 // Attach the default resource limit (APPLICATION) to the process
239 Kernel::g_current_process->resource_limit = Kernel::ResourceLimit::GetForCategory(Kernel::ResourceLimitCategory::APPLICATION); 262 Kernel::g_current_process->resource_limit = Kernel::ResourceLimit::GetForCategory(Kernel::ResourceLimitCategory::APPLICATION);
240 263
241 Load3DSXFile(*file, Memory::PROCESS_IMAGE_VADDR); 264 Kernel::g_current_process->Run(48, Kernel::DEFAULT_STACK_SIZE);
242
243 Kernel::g_current_process->Run(Memory::PROCESS_IMAGE_VADDR, 48, Kernel::DEFAULT_STACK_SIZE);
244 265
245 is_loaded = true; 266 is_loaded = true;
246 return ResultStatus::Success; 267 return ResultStatus::Success;