diff options
| author | 2019-03-06 11:43:07 -0500 | |
|---|---|---|
| committer | 2019-03-06 11:43:07 -0500 | |
| commit | 234f00bdd481c9ec3db68f81872512d7a02f4a3b (patch) | |
| tree | 7780c348be35d5d7b967a956774830f0e851cb51 /src | |
| parent | Merge pull request #2200 from lioncash/audio (diff) | |
| parent | vm_manager: Use range helpers in HeapAlloc() and HeapFree() (diff) | |
| download | yuzu-234f00bdd481c9ec3db68f81872512d7a02f4a3b.tar.gz yuzu-234f00bdd481c9ec3db68f81872512d7a02f4a3b.tar.xz yuzu-234f00bdd481c9ec3db68f81872512d7a02f4a3b.zip | |
Merge pull request #2194 from lioncash/mem
svc: Move memory range checking functions to the VMManager class
Diffstat (limited to 'src')
| -rw-r--r-- | src/core/hle/kernel/svc.cpp | 25 | ||||
| -rw-r--r-- | src/core/hle/kernel/vm_manager.cpp | 47 | ||||
| -rw-r--r-- | src/core/hle/kernel/vm_manager.h | 24 |
3 files changed, 66 insertions, 30 deletions
diff --git a/src/core/hle/kernel/svc.cpp b/src/core/hle/kernel/svc.cpp index c5d399bab..223d717e2 100644 --- a/src/core/hle/kernel/svc.cpp +++ b/src/core/hle/kernel/svc.cpp | |||
| @@ -47,23 +47,6 @@ constexpr bool IsValidAddressRange(VAddr address, u64 size) { | |||
| 47 | return address + size > address; | 47 | return address + size > address; |
| 48 | } | 48 | } |
| 49 | 49 | ||
| 50 | // Checks if a given address range lies within a larger address range. | ||
| 51 | constexpr bool IsInsideAddressRange(VAddr address, u64 size, VAddr address_range_begin, | ||
| 52 | VAddr address_range_end) { | ||
| 53 | const VAddr end_address = address + size - 1; | ||
| 54 | return address_range_begin <= address && end_address <= address_range_end - 1; | ||
| 55 | } | ||
| 56 | |||
| 57 | bool IsInsideAddressSpace(const VMManager& vm, VAddr address, u64 size) { | ||
| 58 | return IsInsideAddressRange(address, size, vm.GetAddressSpaceBaseAddress(), | ||
| 59 | vm.GetAddressSpaceEndAddress()); | ||
| 60 | } | ||
| 61 | |||
| 62 | bool IsInsideNewMapRegion(const VMManager& vm, VAddr address, u64 size) { | ||
| 63 | return IsInsideAddressRange(address, size, vm.GetNewMapRegionBaseAddress(), | ||
| 64 | vm.GetNewMapRegionEndAddress()); | ||
| 65 | } | ||
| 66 | |||
| 67 | // 8 GiB | 50 | // 8 GiB |
| 68 | constexpr u64 MAIN_MEMORY_SIZE = 0x200000000; | 51 | constexpr u64 MAIN_MEMORY_SIZE = 0x200000000; |
| 69 | 52 | ||
| @@ -105,14 +88,14 @@ ResultCode MapUnmapMemorySanityChecks(const VMManager& vm_manager, VAddr dst_add | |||
| 105 | return ERR_INVALID_ADDRESS_STATE; | 88 | return ERR_INVALID_ADDRESS_STATE; |
| 106 | } | 89 | } |
| 107 | 90 | ||
| 108 | if (!IsInsideAddressSpace(vm_manager, src_addr, size)) { | 91 | if (!vm_manager.IsWithinAddressSpace(src_addr, size)) { |
| 109 | LOG_ERROR(Kernel_SVC, | 92 | LOG_ERROR(Kernel_SVC, |
| 110 | "Source is not within the address space, addr=0x{:016X}, size=0x{:016X}", | 93 | "Source is not within the address space, addr=0x{:016X}, size=0x{:016X}", |
| 111 | src_addr, size); | 94 | src_addr, size); |
| 112 | return ERR_INVALID_ADDRESS_STATE; | 95 | return ERR_INVALID_ADDRESS_STATE; |
| 113 | } | 96 | } |
| 114 | 97 | ||
| 115 | if (!IsInsideNewMapRegion(vm_manager, dst_addr, size)) { | 98 | if (!vm_manager.IsWithinNewMapRegion(dst_addr, size)) { |
| 116 | LOG_ERROR(Kernel_SVC, | 99 | LOG_ERROR(Kernel_SVC, |
| 117 | "Destination is not within the new map region, addr=0x{:016X}, size=0x{:016X}", | 100 | "Destination is not within the new map region, addr=0x{:016X}, size=0x{:016X}", |
| 118 | dst_addr, size); | 101 | dst_addr, size); |
| @@ -238,7 +221,7 @@ static ResultCode SetMemoryPermission(VAddr addr, u64 size, u32 prot) { | |||
| 238 | auto* const current_process = Core::CurrentProcess(); | 221 | auto* const current_process = Core::CurrentProcess(); |
| 239 | auto& vm_manager = current_process->VMManager(); | 222 | auto& vm_manager = current_process->VMManager(); |
| 240 | 223 | ||
| 241 | if (!IsInsideAddressSpace(vm_manager, addr, size)) { | 224 | if (!vm_manager.IsWithinAddressSpace(addr, size)) { |
| 242 | LOG_ERROR(Kernel_SVC, | 225 | LOG_ERROR(Kernel_SVC, |
| 243 | "Source is not within the address space, addr=0x{:016X}, size=0x{:016X}", addr, | 226 | "Source is not within the address space, addr=0x{:016X}, size=0x{:016X}", addr, |
| 244 | size); | 227 | size); |
| @@ -299,7 +282,7 @@ static ResultCode SetMemoryAttribute(VAddr address, u64 size, u32 mask, u32 attr | |||
| 299 | } | 282 | } |
| 300 | 283 | ||
| 301 | auto& vm_manager = Core::CurrentProcess()->VMManager(); | 284 | auto& vm_manager = Core::CurrentProcess()->VMManager(); |
| 302 | if (!IsInsideAddressSpace(vm_manager, address, size)) { | 285 | if (!vm_manager.IsWithinAddressSpace(address, size)) { |
| 303 | LOG_ERROR(Kernel_SVC, | 286 | LOG_ERROR(Kernel_SVC, |
| 304 | "Given address (0x{:016X}) is outside the bounds of the address space.", address); | 287 | "Given address (0x{:016X}) is outside the bounds of the address space.", address); |
| 305 | return ERR_INVALID_ADDRESS_STATE; | 288 | return ERR_INVALID_ADDRESS_STATE; |
diff --git a/src/core/hle/kernel/vm_manager.cpp b/src/core/hle/kernel/vm_manager.cpp index 10ad94aa6..05c59af34 100644 --- a/src/core/hle/kernel/vm_manager.cpp +++ b/src/core/hle/kernel/vm_manager.cpp | |||
| @@ -17,8 +17,8 @@ | |||
| 17 | #include "core/memory_setup.h" | 17 | #include "core/memory_setup.h" |
| 18 | 18 | ||
| 19 | namespace Kernel { | 19 | namespace Kernel { |
| 20 | 20 | namespace { | |
| 21 | static const char* GetMemoryStateName(MemoryState state) { | 21 | const char* GetMemoryStateName(MemoryState state) { |
| 22 | static constexpr const char* names[] = { | 22 | static constexpr const char* names[] = { |
| 23 | "Unmapped", "Io", | 23 | "Unmapped", "Io", |
| 24 | "Normal", "CodeStatic", | 24 | "Normal", "CodeStatic", |
| @@ -35,6 +35,14 @@ static const char* GetMemoryStateName(MemoryState state) { | |||
| 35 | return names[ToSvcMemoryState(state)]; | 35 | return names[ToSvcMemoryState(state)]; |
| 36 | } | 36 | } |
| 37 | 37 | ||
| 38 | // Checks if a given address range lies within a larger address range. | ||
| 39 | constexpr bool IsInsideAddressRange(VAddr address, u64 size, VAddr address_range_begin, | ||
| 40 | VAddr address_range_end) { | ||
| 41 | const VAddr end_address = address + size - 1; | ||
| 42 | return address_range_begin <= address && end_address <= address_range_end - 1; | ||
| 43 | } | ||
| 44 | } // Anonymous namespace | ||
| 45 | |||
| 38 | bool VirtualMemoryArea::CanBeMergedWith(const VirtualMemoryArea& next) const { | 46 | bool VirtualMemoryArea::CanBeMergedWith(const VirtualMemoryArea& next) const { |
| 39 | ASSERT(base + size == next.base); | 47 | ASSERT(base + size == next.base); |
| 40 | if (permissions != next.permissions || state != next.state || attribute != next.attribute || | 48 | if (permissions != next.permissions || state != next.state || attribute != next.attribute || |
| @@ -249,8 +257,7 @@ ResultCode VMManager::ReprotectRange(VAddr target, u64 size, VMAPermission new_p | |||
| 249 | } | 257 | } |
| 250 | 258 | ||
| 251 | ResultVal<VAddr> VMManager::HeapAllocate(VAddr target, u64 size, VMAPermission perms) { | 259 | ResultVal<VAddr> VMManager::HeapAllocate(VAddr target, u64 size, VMAPermission perms) { |
| 252 | if (target < GetHeapRegionBaseAddress() || target + size > GetHeapRegionEndAddress() || | 260 | if (!IsWithinHeapRegion(target, size)) { |
| 253 | target + size < target) { | ||
| 254 | return ERR_INVALID_ADDRESS; | 261 | return ERR_INVALID_ADDRESS; |
| 255 | } | 262 | } |
| 256 | 263 | ||
| @@ -285,8 +292,7 @@ ResultVal<VAddr> VMManager::HeapAllocate(VAddr target, u64 size, VMAPermission p | |||
| 285 | } | 292 | } |
| 286 | 293 | ||
| 287 | ResultCode VMManager::HeapFree(VAddr target, u64 size) { | 294 | ResultCode VMManager::HeapFree(VAddr target, u64 size) { |
| 288 | if (target < GetHeapRegionBaseAddress() || target + size > GetHeapRegionEndAddress() || | 295 | if (!IsWithinHeapRegion(target, size)) { |
| 289 | target + size < target) { | ||
| 290 | return ERR_INVALID_ADDRESS; | 296 | return ERR_INVALID_ADDRESS; |
| 291 | } | 297 | } |
| 292 | 298 | ||
| @@ -706,6 +712,11 @@ u64 VMManager::GetAddressSpaceWidth() const { | |||
| 706 | return address_space_width; | 712 | return address_space_width; |
| 707 | } | 713 | } |
| 708 | 714 | ||
| 715 | bool VMManager::IsWithinAddressSpace(VAddr address, u64 size) const { | ||
| 716 | return IsInsideAddressRange(address, size, GetAddressSpaceBaseAddress(), | ||
| 717 | GetAddressSpaceEndAddress()); | ||
| 718 | } | ||
| 719 | |||
| 709 | VAddr VMManager::GetASLRRegionBaseAddress() const { | 720 | VAddr VMManager::GetASLRRegionBaseAddress() const { |
| 710 | return aslr_region_base; | 721 | return aslr_region_base; |
| 711 | } | 722 | } |
| @@ -750,6 +761,11 @@ u64 VMManager::GetCodeRegionSize() const { | |||
| 750 | return code_region_end - code_region_base; | 761 | return code_region_end - code_region_base; |
| 751 | } | 762 | } |
| 752 | 763 | ||
| 764 | bool VMManager::IsWithinCodeRegion(VAddr address, u64 size) const { | ||
| 765 | return IsInsideAddressRange(address, size, GetCodeRegionBaseAddress(), | ||
| 766 | GetCodeRegionEndAddress()); | ||
| 767 | } | ||
| 768 | |||
| 753 | VAddr VMManager::GetHeapRegionBaseAddress() const { | 769 | VAddr VMManager::GetHeapRegionBaseAddress() const { |
| 754 | return heap_region_base; | 770 | return heap_region_base; |
| 755 | } | 771 | } |
| @@ -762,6 +778,11 @@ u64 VMManager::GetHeapRegionSize() const { | |||
| 762 | return heap_region_end - heap_region_base; | 778 | return heap_region_end - heap_region_base; |
| 763 | } | 779 | } |
| 764 | 780 | ||
| 781 | bool VMManager::IsWithinHeapRegion(VAddr address, u64 size) const { | ||
| 782 | return IsInsideAddressRange(address, size, GetHeapRegionBaseAddress(), | ||
| 783 | GetHeapRegionEndAddress()); | ||
| 784 | } | ||
| 785 | |||
| 765 | VAddr VMManager::GetMapRegionBaseAddress() const { | 786 | VAddr VMManager::GetMapRegionBaseAddress() const { |
| 766 | return map_region_base; | 787 | return map_region_base; |
| 767 | } | 788 | } |
| @@ -774,6 +795,10 @@ u64 VMManager::GetMapRegionSize() const { | |||
| 774 | return map_region_end - map_region_base; | 795 | return map_region_end - map_region_base; |
| 775 | } | 796 | } |
| 776 | 797 | ||
| 798 | bool VMManager::IsWithinMapRegion(VAddr address, u64 size) const { | ||
| 799 | return IsInsideAddressRange(address, size, GetMapRegionBaseAddress(), GetMapRegionEndAddress()); | ||
| 800 | } | ||
| 801 | |||
| 777 | VAddr VMManager::GetNewMapRegionBaseAddress() const { | 802 | VAddr VMManager::GetNewMapRegionBaseAddress() const { |
| 778 | return new_map_region_base; | 803 | return new_map_region_base; |
| 779 | } | 804 | } |
| @@ -786,6 +811,11 @@ u64 VMManager::GetNewMapRegionSize() const { | |||
| 786 | return new_map_region_end - new_map_region_base; | 811 | return new_map_region_end - new_map_region_base; |
| 787 | } | 812 | } |
| 788 | 813 | ||
| 814 | bool VMManager::IsWithinNewMapRegion(VAddr address, u64 size) const { | ||
| 815 | return IsInsideAddressRange(address, size, GetNewMapRegionBaseAddress(), | ||
| 816 | GetNewMapRegionEndAddress()); | ||
| 817 | } | ||
| 818 | |||
| 789 | VAddr VMManager::GetTLSIORegionBaseAddress() const { | 819 | VAddr VMManager::GetTLSIORegionBaseAddress() const { |
| 790 | return tls_io_region_base; | 820 | return tls_io_region_base; |
| 791 | } | 821 | } |
| @@ -798,4 +828,9 @@ u64 VMManager::GetTLSIORegionSize() const { | |||
| 798 | return tls_io_region_end - tls_io_region_base; | 828 | return tls_io_region_end - tls_io_region_base; |
| 799 | } | 829 | } |
| 800 | 830 | ||
| 831 | bool VMManager::IsWithinTLSIORegion(VAddr address, u64 size) const { | ||
| 832 | return IsInsideAddressRange(address, size, GetTLSIORegionBaseAddress(), | ||
| 833 | GetTLSIORegionEndAddress()); | ||
| 834 | } | ||
| 835 | |||
| 801 | } // namespace Kernel | 836 | } // namespace Kernel |
diff --git a/src/core/hle/kernel/vm_manager.h b/src/core/hle/kernel/vm_manager.h index 6091533bc..88e0b3c02 100644 --- a/src/core/hle/kernel/vm_manager.h +++ b/src/core/hle/kernel/vm_manager.h | |||
| @@ -432,18 +432,21 @@ public: | |||
| 432 | /// Gets the address space width in bits. | 432 | /// Gets the address space width in bits. |
| 433 | u64 GetAddressSpaceWidth() const; | 433 | u64 GetAddressSpaceWidth() const; |
| 434 | 434 | ||
| 435 | /// Determines whether or not the given address range lies within the address space. | ||
| 436 | bool IsWithinAddressSpace(VAddr address, u64 size) const; | ||
| 437 | |||
| 435 | /// Gets the base address of the ASLR region. | 438 | /// Gets the base address of the ASLR region. |
| 436 | VAddr GetASLRRegionBaseAddress() const; | 439 | VAddr GetASLRRegionBaseAddress() const; |
| 437 | 440 | ||
| 438 | /// Gets the end address of the ASLR region. | 441 | /// Gets the end address of the ASLR region. |
| 439 | VAddr GetASLRRegionEndAddress() const; | 442 | VAddr GetASLRRegionEndAddress() const; |
| 440 | 443 | ||
| 441 | /// Determines whether or not the specified address range is within the ASLR region. | ||
| 442 | bool IsWithinASLRRegion(VAddr address, u64 size) const; | ||
| 443 | |||
| 444 | /// Gets the size of the ASLR region | 444 | /// Gets the size of the ASLR region |
| 445 | u64 GetASLRRegionSize() const; | 445 | u64 GetASLRRegionSize() const; |
| 446 | 446 | ||
| 447 | /// Determines whether or not the specified address range is within the ASLR region. | ||
| 448 | bool IsWithinASLRRegion(VAddr address, u64 size) const; | ||
| 449 | |||
| 447 | /// Gets the base address of the code region. | 450 | /// Gets the base address of the code region. |
| 448 | VAddr GetCodeRegionBaseAddress() const; | 451 | VAddr GetCodeRegionBaseAddress() const; |
| 449 | 452 | ||
| @@ -453,6 +456,9 @@ public: | |||
| 453 | /// Gets the total size of the code region in bytes. | 456 | /// Gets the total size of the code region in bytes. |
| 454 | u64 GetCodeRegionSize() const; | 457 | u64 GetCodeRegionSize() const; |
| 455 | 458 | ||
| 459 | /// Determines whether or not the specified range is within the code region. | ||
| 460 | bool IsWithinCodeRegion(VAddr address, u64 size) const; | ||
| 461 | |||
| 456 | /// Gets the base address of the heap region. | 462 | /// Gets the base address of the heap region. |
| 457 | VAddr GetHeapRegionBaseAddress() const; | 463 | VAddr GetHeapRegionBaseAddress() const; |
| 458 | 464 | ||
| @@ -462,6 +468,9 @@ public: | |||
| 462 | /// Gets the total size of the heap region in bytes. | 468 | /// Gets the total size of the heap region in bytes. |
| 463 | u64 GetHeapRegionSize() const; | 469 | u64 GetHeapRegionSize() const; |
| 464 | 470 | ||
| 471 | /// Determines whether or not the specified range is within the heap region. | ||
| 472 | bool IsWithinHeapRegion(VAddr address, u64 size) const; | ||
| 473 | |||
| 465 | /// Gets the base address of the map region. | 474 | /// Gets the base address of the map region. |
| 466 | VAddr GetMapRegionBaseAddress() const; | 475 | VAddr GetMapRegionBaseAddress() const; |
| 467 | 476 | ||
| @@ -471,6 +480,9 @@ public: | |||
| 471 | /// Gets the total size of the map region in bytes. | 480 | /// Gets the total size of the map region in bytes. |
| 472 | u64 GetMapRegionSize() const; | 481 | u64 GetMapRegionSize() const; |
| 473 | 482 | ||
| 483 | /// Determines whether or not the specified range is within the map region. | ||
| 484 | bool IsWithinMapRegion(VAddr address, u64 size) const; | ||
| 485 | |||
| 474 | /// Gets the base address of the new map region. | 486 | /// Gets the base address of the new map region. |
| 475 | VAddr GetNewMapRegionBaseAddress() const; | 487 | VAddr GetNewMapRegionBaseAddress() const; |
| 476 | 488 | ||
| @@ -480,6 +492,9 @@ public: | |||
| 480 | /// Gets the total size of the new map region in bytes. | 492 | /// Gets the total size of the new map region in bytes. |
| 481 | u64 GetNewMapRegionSize() const; | 493 | u64 GetNewMapRegionSize() const; |
| 482 | 494 | ||
| 495 | /// Determines whether or not the given address range is within the new map region | ||
| 496 | bool IsWithinNewMapRegion(VAddr address, u64 size) const; | ||
| 497 | |||
| 483 | /// Gets the base address of the TLS IO region. | 498 | /// Gets the base address of the TLS IO region. |
| 484 | VAddr GetTLSIORegionBaseAddress() const; | 499 | VAddr GetTLSIORegionBaseAddress() const; |
| 485 | 500 | ||
| @@ -489,6 +504,9 @@ public: | |||
| 489 | /// Gets the total size of the TLS IO region in bytes. | 504 | /// Gets the total size of the TLS IO region in bytes. |
| 490 | u64 GetTLSIORegionSize() const; | 505 | u64 GetTLSIORegionSize() const; |
| 491 | 506 | ||
| 507 | /// Determines if the given address range is within the TLS IO region. | ||
| 508 | bool IsWithinTLSIORegion(VAddr address, u64 size) const; | ||
| 509 | |||
| 492 | /// Each VMManager has its own page table, which is set as the main one when the owning process | 510 | /// Each VMManager has its own page table, which is set as the main one when the owning process |
| 493 | /// is scheduled. | 511 | /// is scheduled. |
| 494 | Memory::PageTable page_table; | 512 | Memory::PageTable page_table; |