summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/core/hle/kernel/k_page_table.h8
-rw-r--r--src/core/hle/service/ldr/ldr.cpp78
2 files changed, 62 insertions, 24 deletions
diff --git a/src/core/hle/kernel/k_page_table.h b/src/core/hle/kernel/k_page_table.h
index aea1b8f63..e99abe36a 100644
--- a/src/core/hle/kernel/k_page_table.h
+++ b/src/core/hle/kernel/k_page_table.h
@@ -253,7 +253,9 @@ public:
253 constexpr bool IsInsideASLRRegion(VAddr address, std::size_t size) const { 253 constexpr bool IsInsideASLRRegion(VAddr address, std::size_t size) const {
254 return !IsOutsideASLRRegion(address, size); 254 return !IsOutsideASLRRegion(address, size);
255 } 255 }
256 256 constexpr std::size_t GetNumGuardPages() const {
257 return IsKernel() ? 1 : 4;
258 }
257 PAddr GetPhysicalAddr(VAddr addr) const { 259 PAddr GetPhysicalAddr(VAddr addr) const {
258 const auto backing_addr = page_table_impl.backing_addr[addr >> PageBits]; 260 const auto backing_addr = page_table_impl.backing_addr[addr >> PageBits];
259 ASSERT(backing_addr); 261 ASSERT(backing_addr);
@@ -275,10 +277,6 @@ private:
275 return is_aslr_enabled; 277 return is_aslr_enabled;
276 } 278 }
277 279
278 constexpr std::size_t GetNumGuardPages() const {
279 return IsKernel() ? 1 : 4;
280 }
281
282 constexpr bool ContainsPages(VAddr addr, std::size_t num_pages) const { 280 constexpr bool ContainsPages(VAddr addr, std::size_t num_pages) const {
283 return (address_space_start <= addr) && 281 return (address_space_start <= addr) &&
284 (num_pages <= (address_space_end - address_space_start) / PageSize) && 282 (num_pages <= (address_space_end - address_space_start) / PageSize) &&
diff --git a/src/core/hle/service/ldr/ldr.cpp b/src/core/hle/service/ldr/ldr.cpp
index 9fc7bb1b1..099276420 100644
--- a/src/core/hle/service/ldr/ldr.cpp
+++ b/src/core/hle/service/ldr/ldr.cpp
@@ -288,7 +288,7 @@ public:
288 } 288 }
289 289
290 bool ValidateRegionForMap(Kernel::KPageTable& page_table, VAddr start, std::size_t size) const { 290 bool ValidateRegionForMap(Kernel::KPageTable& page_table, VAddr start, std::size_t size) const {
291 constexpr std::size_t padding_size{4 * Kernel::PageSize}; 291 const std::size_t padding_size{page_table.GetNumGuardPages() * Kernel::PageSize};
292 const auto start_info{page_table.QueryInfo(start - 1)}; 292 const auto start_info{page_table.QueryInfo(start - 1)};
293 293
294 if (start_info.state != Kernel::KMemoryState::Free) { 294 if (start_info.state != Kernel::KMemoryState::Free) {
@@ -308,31 +308,69 @@ public:
308 return (start + size + padding_size) <= (end_info.GetAddress() + end_info.GetSize()); 308 return (start + size + padding_size) <= (end_info.GetAddress() + end_info.GetSize());
309 } 309 }
310 310
311 VAddr GetRandomMapRegion(const Kernel::KPageTable& page_table, std::size_t size) const { 311 ResultCode GetAvailableMapRegion(Kernel::KPageTable& page_table, u64 size, VAddr& out_addr) {
312 VAddr addr{}; 312 size = Common::AlignUp(size, Kernel::PageSize);
313 const std::size_t end_pages{(page_table.GetAliasCodeRegionSize() - size) >> 313 size += page_table.GetNumGuardPages() * Kernel::PageSize * 4;
314 Kernel::PageBits}; 314
315 do { 315 const auto is_region_available = [&](VAddr addr) {
316 addr = page_table.GetAliasCodeRegionStart() + 316 const auto end_addr = addr + size;
317 (Kernel::KSystemControl::GenerateRandomRange(0, end_pages) << Kernel::PageBits); 317 while (addr < end_addr) {
318 } while (!page_table.IsInsideAddressSpace(addr, size) || 318 if (system.Memory().IsValidVirtualAddress(addr)) {
319 page_table.IsInsideHeapRegion(addr, size) || 319 return false;
320 page_table.IsInsideAliasRegion(addr, size)); 320 }
321 return addr; 321
322 if (!page_table.IsInsideAddressSpace(out_addr, size)) {
323 return false;
324 }
325
326 if (page_table.IsInsideHeapRegion(out_addr, size)) {
327 return false;
328 }
329
330 if (page_table.IsInsideAliasRegion(out_addr, size)) {
331 return false;
332 }
333
334 addr += Kernel::PageSize;
335 }
336 return true;
337 };
338
339 bool succeeded = false;
340 const auto map_region_end =
341 page_table.GetAliasCodeRegionStart() + page_table.GetAliasCodeRegionSize();
342 while (current_map_addr < map_region_end) {
343 if (is_region_available(current_map_addr)) {
344 succeeded = true;
345 break;
346 }
347 current_map_addr += 0x100000;
348 }
349
350 if (!succeeded) {
351 UNREACHABLE_MSG("Out of address space!");
352 return Kernel::ResultOutOfMemory;
353 }
354
355 out_addr = current_map_addr;
356 current_map_addr += size;
357
358 return ResultSuccess;
322 } 359 }
323 360
324 ResultVal<VAddr> MapProcessCodeMemory(Kernel::KProcess* process, VAddr baseAddress, 361 ResultVal<VAddr> MapProcessCodeMemory(Kernel::KProcess* process, VAddr base_addr, u64 size) {
325 u64 size) const { 362 auto& page_table{process->PageTable()};
363 VAddr addr{};
364
326 for (std::size_t retry = 0; retry < MAXIMUM_MAP_RETRIES; retry++) { 365 for (std::size_t retry = 0; retry < MAXIMUM_MAP_RETRIES; retry++) {
327 auto& page_table{process->PageTable()}; 366 R_TRY(GetAvailableMapRegion(page_table, size, addr));
328 const VAddr addr{GetRandomMapRegion(page_table, size)};
329 const ResultCode result{page_table.MapCodeMemory(addr, baseAddress, size)};
330 367
368 const ResultCode result{page_table.MapCodeMemory(addr, base_addr, size)};
331 if (result == Kernel::ResultInvalidCurrentMemory) { 369 if (result == Kernel::ResultInvalidCurrentMemory) {
332 continue; 370 continue;
333 } 371 }
334 372
335 CASCADE_CODE(result); 373 R_TRY(result);
336 374
337 if (ValidateRegionForMap(page_table, addr, size)) { 375 if (ValidateRegionForMap(page_table, addr, size)) {
338 return addr; 376 return addr;
@@ -343,7 +381,7 @@ public:
343 } 381 }
344 382
345 ResultVal<VAddr> MapNro(Kernel::KProcess* process, VAddr nro_addr, std::size_t nro_size, 383 ResultVal<VAddr> MapNro(Kernel::KProcess* process, VAddr nro_addr, std::size_t nro_size,
346 VAddr bss_addr, std::size_t bss_size, std::size_t size) const { 384 VAddr bss_addr, std::size_t bss_size, std::size_t size) {
347 for (std::size_t retry = 0; retry < MAXIMUM_MAP_RETRIES; retry++) { 385 for (std::size_t retry = 0; retry < MAXIMUM_MAP_RETRIES; retry++) {
348 auto& page_table{process->PageTable()}; 386 auto& page_table{process->PageTable()};
349 VAddr addr{}; 387 VAddr addr{};
@@ -597,6 +635,7 @@ public:
597 LOG_WARNING(Service_LDR, "(STUBBED) called"); 635 LOG_WARNING(Service_LDR, "(STUBBED) called");
598 636
599 initialized = true; 637 initialized = true;
638 current_map_addr = system.CurrentProcess()->PageTable().GetAliasCodeRegionStart();
600 639
601 IPC::ResponseBuilder rb{ctx, 2}; 640 IPC::ResponseBuilder rb{ctx, 2};
602 rb.Push(ResultSuccess); 641 rb.Push(ResultSuccess);
@@ -607,6 +646,7 @@ private:
607 646
608 std::map<VAddr, NROInfo> nro; 647 std::map<VAddr, NROInfo> nro;
609 std::map<VAddr, std::vector<SHA256Hash>> nrr; 648 std::map<VAddr, std::vector<SHA256Hash>> nrr;
649 VAddr current_map_addr{};
610 650
611 bool IsValidNROHash(const SHA256Hash& hash) const { 651 bool IsValidNROHash(const SHA256Hash& hash) const {
612 return std::any_of(nrr.begin(), nrr.end(), [&hash](const auto& p) { 652 return std::any_of(nrr.begin(), nrr.end(), [&hash](const auto& p) {