-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[LINUX/BASE] Fixed mmap usage and (re)-implemented most parts of memory_posix.cc #2230
Open
guccigang420
wants to merge
1
commit into
xenia-project:master
Choose a base branch
from
guccigang420:mmap-fix
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,9 @@ | |
#include <sys/mman.h> | ||
#include <unistd.h> | ||
#include <cstddef> | ||
#include <fstream> | ||
#include <mutex> | ||
#include <sstream> | ||
|
||
#include "xenia/base/math.h" | ||
#include "xenia/base/platform.h" | ||
|
@@ -79,14 +82,53 @@ uint32_t ToPosixProtectFlags(PageAccess access) { | |
} | ||
} | ||
|
||
PageAccess ToXeniaProtectFlags(char* protection) { | ||
if (protection[0] == 'r' && protection[1] == 'w' && protection[2] == 'x') { | ||
return PageAccess::kExecuteReadWrite; | ||
} else if (protection[0] == 'r' && protection[1] == '-' && | ||
protection[2] == 'x') { | ||
return PageAccess::kExecuteReadOnly; | ||
} else if (protection[0] == 'r' && protection[1] == 'w' && | ||
protection[2] == '-') { | ||
return PageAccess::kReadWrite; | ||
} else if (protection[0] == 'r' && protection[1] == '-' && | ||
protection[2] == '-') { | ||
return PageAccess::kReadOnly; | ||
} else { | ||
return PageAccess::kNoAccess; | ||
} | ||
} | ||
|
||
bool IsWritableExecutableMemorySupported() { return true; } | ||
|
||
struct MappedFileRange { | ||
uintptr_t region_begin; | ||
uintptr_t region_end; | ||
}; | ||
|
||
std::vector<struct MappedFileRange> mapped_file_ranges; | ||
std::mutex g_mapped_file_ranges_mutex; | ||
|
||
void* AllocFixed(void* base_address, size_t length, | ||
AllocationType allocation_type, PageAccess access) { | ||
// mmap does not support reserve / commit, so ignore allocation_type. | ||
uint32_t prot = ToPosixProtectFlags(access); | ||
void* result = mmap(base_address, length, prot, | ||
MAP_PRIVATE | MAP_FIXED | MAP_ANONYMOUS, -1, 0); | ||
int flags = MAP_PRIVATE | MAP_ANONYMOUS; | ||
|
||
if (base_address != nullptr) { | ||
bool should_protect = allocation_type == AllocationType::kCommit; | ||
if (should_protect) { | ||
if (Protect(base_address, length, access)){ | ||
return base_address; | ||
} else { | ||
return nullptr; | ||
} | ||
} | ||
flags |= MAP_FIXED_NOREPLACE; | ||
} | ||
|
||
void* result = mmap(base_address, length, prot, flags, -1, 0); | ||
|
||
if (result == MAP_FAILED) { | ||
return nullptr; | ||
} else { | ||
|
@@ -96,20 +138,97 @@ void* AllocFixed(void* base_address, size_t length, | |
|
||
bool DeallocFixed(void* base_address, size_t length, | ||
DeallocationType deallocation_type) { | ||
return munmap(base_address, length) == 0; | ||
const uintptr_t region_begin = (uintptr_t)base_address; | ||
const uintptr_t region_end = (uintptr_t)base_address + length; | ||
|
||
std::lock_guard<std::mutex> guard(g_mapped_file_ranges_mutex); | ||
for (const auto& mapped_range : mapped_file_ranges) { | ||
if (region_begin >= mapped_range.region_begin && | ||
region_end <= mapped_range.region_end) { | ||
|
||
switch(deallocation_type) { | ||
case DeallocationType::kDecommit: | ||
return Protect(base_address, length, PageAccess::kNoAccess); | ||
case DeallocationType::kRelease: | ||
assert_always("Error: Tried to release mapped memory!"); | ||
default: | ||
assert_unhandled_case(deallocation_type); | ||
} | ||
|
||
} | ||
} | ||
|
||
switch(deallocation_type) { | ||
case DeallocationType::kDecommit: | ||
return Protect(base_address, length, PageAccess::kNoAccess); | ||
case DeallocationType::kRelease: | ||
return munmap(base_address, length) == 0; | ||
default: | ||
assert_unhandled_case(deallocation_type); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In a release build the compiler here complained that there is no return value for all control paths. Just add a return false at the end of the function just to make it happy for now. |
||
} | ||
|
||
bool Protect(void* base_address, size_t length, PageAccess access, | ||
PageAccess* out_old_access) { | ||
// Linux does not have a syscall to query memory permissions. | ||
assert_null(out_old_access); | ||
if (out_old_access) { | ||
size_t length_copy = length; | ||
QueryProtect(base_address, length_copy, *out_old_access); | ||
} | ||
|
||
uint32_t prot = ToPosixProtectFlags(access); | ||
return mprotect(base_address, length, prot) == 0; | ||
} | ||
|
||
bool QueryProtect(void* base_address, size_t& length, PageAccess& access_out) { | ||
// No generic POSIX solution exists. The Linux solution should work on all Linux | ||
// kernel based OS, including Android. | ||
#if XE_PLATFORM_LINUX | ||
std::ifstream memory_maps; | ||
memory_maps.open("/proc/self/maps", std::ios_base::in); | ||
std::string maps_entry_string; | ||
|
||
while (std::getline(memory_maps, maps_entry_string)) { | ||
std::stringstream entry_stream(maps_entry_string); | ||
uintptr_t map_region_begin, map_region_end; | ||
char separator, protection[4]; | ||
|
||
entry_stream >> std::hex >> map_region_begin >> separator >> | ||
map_region_end >> protection; | ||
|
||
if (map_region_begin <= (uintptr_t)base_address && | ||
map_region_end > (uintptr_t)base_address) { | ||
length = map_region_end - reinterpret_cast<uintptr_t>(base_address); | ||
|
||
access_out = ToXeniaProtectFlags(protection); | ||
|
||
// Look at the next consecutive mappings | ||
while (std::getline(memory_maps, maps_entry_string)) { | ||
std::stringstream next_entry_stream(maps_entry_string); | ||
uintptr_t next_map_region_begin, next_map_region_end; | ||
char next_protection[4]; | ||
|
||
next_entry_stream >> std::hex >> next_map_region_begin >> separator >> | ||
next_map_region_end >> next_protection; | ||
if (map_region_end == next_map_region_begin && | ||
access_out == ToXeniaProtectFlags(next_protection)) { | ||
length = | ||
next_map_region_end - reinterpret_cast<uintptr_t>(base_address); | ||
continue; | ||
} else { | ||
break; | ||
} | ||
} | ||
|
||
memory_maps.close(); | ||
return true; | ||
} | ||
} | ||
|
||
memory_maps.close(); | ||
return false; | ||
#else | ||
return false; | ||
#endif | ||
} | ||
|
||
FileMappingHandle CreateFileMappingHandle(const std::filesystem::path& path, | ||
|
@@ -178,12 +297,40 @@ void CloseFileMappingHandle(FileMappingHandle handle, | |
void* MapFileView(FileMappingHandle handle, void* base_address, size_t length, | ||
PageAccess access, size_t file_offset) { | ||
uint32_t prot = ToPosixProtectFlags(access); | ||
return mmap64(base_address, length, prot, MAP_PRIVATE | MAP_ANONYMOUS, handle, | ||
|
||
int flags = MAP_SHARED; | ||
if (base_address != nullptr) { | ||
flags |= MAP_FIXED_NOREPLACE; | ||
} | ||
|
||
void* result = mmap(base_address, length, prot, flags, handle, | ||
file_offset); | ||
|
||
if (result == MAP_FAILED) { | ||
return nullptr; | ||
} else { | ||
std::lock_guard<std::mutex> guard(g_mapped_file_ranges_mutex); | ||
mapped_file_ranges.push_back( | ||
{(uintptr_t)result, (uintptr_t)result + length}); | ||
return result; | ||
} | ||
} | ||
|
||
bool UnmapFileView(FileMappingHandle handle, void* base_address, | ||
size_t length) { | ||
std::lock_guard<std::mutex> guard(g_mapped_file_ranges_mutex); | ||
for (auto mapped_range = mapped_file_ranges.begin(); | ||
mapped_range != mapped_file_ranges.end();) { | ||
if (mapped_range->region_begin == (uintptr_t)base_address && | ||
mapped_range->region_end == (uintptr_t)base_address + length) { | ||
mapped_file_ranges.erase(mapped_range); | ||
return munmap(base_address, length) == 0; | ||
} else { | ||
mapped_range++; | ||
} | ||
} | ||
// TODO: Implement partial file unmapping. | ||
assert_always("Error: Partial unmapping of files not yet supported."); | ||
return munmap(base_address, length) == 0; | ||
} | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would personally reverse this statement to prevent nesting.
aka.