Add a few comments to the Memory manager

This commit is contained in:
TheASVigilante 2023-05-05 20:43:47 +02:00
parent 242e39dc4d
commit 5f88077406
2 changed files with 20 additions and 8 deletions

View File

@ -43,7 +43,7 @@ namespace skyline::kernel {
if ((firstChunk.state == memory::states::Unmapped) != isUnmapping) if ((firstChunk.state == memory::states::Unmapped) != isUnmapping)
needsReprotection = true; needsReprotection = true;
// We edit the chunk's first half // We reduce the size of the first half
firstChunk.size = static_cast<size_t>(newDesc.first - firstChunkBase->first); firstChunk.size = static_cast<size_t>(newDesc.first - firstChunkBase->first);
chunks[firstChunkBase->first] = firstChunk; chunks[firstChunkBase->first] = firstChunk;
@ -69,12 +69,13 @@ namespace skyline::kernel {
bool shouldInsert{true}; bool shouldInsert{true};
// We check if the new chunk and the first chunk is mergable
if (firstChunk.IsCompatible(newDesc.second)) { if (firstChunk.IsCompatible(newDesc.second)) {
shouldInsert = false; shouldInsert = false;
firstChunk.size = static_cast<size_t>((newDesc.first + newDesc.second.size) - firstChunkBase->first); firstChunk.size = static_cast<size_t>((newDesc.first + newDesc.second.size) - firstChunkBase->first);
chunks[firstChunkBase->first] = firstChunk; chunks[firstChunkBase->first] = firstChunk;
} else if ((firstChunkBase->first + firstChunk.size) != newDesc.first) { } else if ((firstChunkBase->first + firstChunk.size) != newDesc.first) { // If it's not mergable check if it needs resizing
firstChunk.size = static_cast<size_t>(newDesc.first - firstChunkBase->first); firstChunk.size = static_cast<size_t>(newDesc.first - firstChunkBase->first);
chunks[firstChunkBase->first] = firstChunk; chunks[firstChunkBase->first] = firstChunk;
@ -83,6 +84,7 @@ namespace skyline::kernel {
needsReprotection = true; needsReprotection = true;
} }
// We check if the new chunk and the last chunk is mergable
if (lastChunk.IsCompatible(newDesc.second)) { if (lastChunk.IsCompatible(newDesc.second)) {
u8 *oldBase{lastChunkBase->first}; u8 *oldBase{lastChunkBase->first};
chunks.erase(lastChunkBase); chunks.erase(lastChunkBase);
@ -97,7 +99,7 @@ namespace skyline::kernel {
firstChunk.size = static_cast<size_t>((lastChunk.size + oldBase) - firstChunkBase->first); firstChunk.size = static_cast<size_t>((lastChunk.size + oldBase) - firstChunkBase->first);
chunks[firstChunkBase->first] = firstChunk; chunks[firstChunkBase->first] = firstChunk;
} }
} else if ((newDesc.first + newDesc.second.size) != lastChunkBase->first) { } else if ((newDesc.first + newDesc.second.size) != lastChunkBase->first) { // If it's not mergable check if it needs resizing
lastChunk.size = static_cast<size_t>((lastChunk.size + lastChunkBase->first) - (newDesc.first + newDesc.second.size)); lastChunk.size = static_cast<size_t>((lastChunk.size + lastChunkBase->first) - (newDesc.first + newDesc.second.size));
chunks.erase(lastChunkBase); chunks.erase(lastChunkBase);
@ -377,7 +379,7 @@ namespace skyline::kernel {
if (!addressSpace.contains(addr)) [[unlikely]] if (!addressSpace.contains(addr)) [[unlikely]]
return std::nullopt; return std::nullopt;
auto chunkBase = chunks.lower_bound(addr); auto chunkBase{chunks.lower_bound(addr)};
if (addr < chunkBase->first) if (addr < chunkBase->first)
--chunkBase; --chunkBase;

View File

@ -63,7 +63,9 @@ namespace skyline {
* @url https://switchbrew.org/wiki/SVC#MemoryAttribute * @url https://switchbrew.org/wiki/SVC#MemoryAttribute
*/ */
union MemoryAttribute { union MemoryAttribute {
/**
* @brief Initializes all atrributes to false
*/
constexpr MemoryAttribute() : value{} {} constexpr MemoryAttribute() : value{} {}
constexpr explicit MemoryAttribute(u8 value) : value{value} {} constexpr explicit MemoryAttribute(u8 value) : value{value} {}
@ -259,7 +261,7 @@ namespace skyline {
/** /**
* @brief Mirrors a page-aligned mapping in the guest address space to the host address space * @brief Mirrors a page-aligned mapping in the guest address space to the host address space
* @return A span to the host address space mirror mapped as RWX, unmapping it is the responsibility of the caller * @return A span to the host address space mirror mapped as RW, unmapping it is the responsibility of the caller
* @note The supplied mapping **must** be page-aligned and inside the guest address space * @note The supplied mapping **must** be page-aligned and inside the guest address space
*/ */
span<u8> CreateMirror(span<u8> mapping); span<u8> CreateMirror(span<u8> mapping);
@ -267,7 +269,7 @@ namespace skyline {
/** /**
* @brief Mirrors multiple page-aligned mapping in the guest address space to the host address space * @brief Mirrors multiple page-aligned mapping in the guest address space to the host address space
* @param totalSize The total size of all the regions to be mirrored combined * @param totalSize The total size of all the regions to be mirrored combined
* @return A span to the host address space mirror mapped as RWX, unmapping it is the responsibility of the caller * @return A span to the host address space mirror mapped as RW, unmapping it is the responsibility of the caller
* @note The supplied mapping **must** be page-aligned and inside the guest address space * @note The supplied mapping **must** be page-aligned and inside the guest address space
* @note If a single mapping is mirrored, it is recommended to use CreateMirror instead * @note If a single mapping is mirrored, it is recommended to use CreateMirror instead
*/ */
@ -294,7 +296,7 @@ namespace skyline {
*/ */
std::optional<std::pair<u8 *, ChunkDescriptor>> GetChunk(u8 *addr); std::optional<std::pair<u8 *, ChunkDescriptor>> GetChunk(u8 *addr);
// Various mapping functions for use by the guest // Various mapping functions for use by the guest, argument validity must be checked by the caller
void MapCodeMemory(span<u8> memory, memory::Permission permission); void MapCodeMemory(span<u8> memory, memory::Permission permission);
void MapMutableCodeMemory(span<u8> memory); void MapMutableCodeMemory(span<u8> memory);
@ -322,8 +324,16 @@ namespace skyline {
*/ */
void FreeMemory(span<u8> memory); void FreeMemory(span<u8> memory);
/**
* Implements the memory manager side functionality of svcMapMemory
* @note Argument validity must be checked by the caller
*/
void SvcMapMemory(span<u8> source, span<u8> destination); void SvcMapMemory(span<u8> source, span<u8> destination);
/**
* Implements the memory manager side functionality of svcUnmapMemory
* @note Argument validity must be checked by the caller
*/
void SvcUnmapMemory(span<u8> source, span<u8> destination); void SvcUnmapMemory(span<u8> source, span<u8> destination);
/** /**