diff --git a/app/src/main/cpp/skyline/kernel/types/KSharedMemory.cpp b/app/src/main/cpp/skyline/kernel/types/KSharedMemory.cpp index 75601c69..cb0b05e3 100644 --- a/app/src/main/cpp/skyline/kernel/types/KSharedMemory.cpp +++ b/app/src/main/cpp/skyline/kernel/types/KSharedMemory.cpp @@ -9,7 +9,7 @@ #include "KProcess.h" namespace skyline::kernel::type { - KSharedMemory::KSharedMemory(const DeviceState &state, u64 address, size_t size, memory::Permission permission, memory::MemoryState memState, int mmapFlags) : initialState(memState), KMemory(state, KType::KSharedMemory) { + KSharedMemory::KSharedMemory(const DeviceState &state, u64 address, size_t size, memory::Permission permission, memory::MemoryState memState, int mmapFlags, bool shared) : initialState(memState), KMemory(state, KType::KSharedMemory) { if (address && !util::PageAligned(address)) throw exception("KSharedMemory was created with non-page-aligned address: 0x{:X}", address); @@ -22,6 +22,26 @@ namespace skyline::kernel::type { throw exception("An occurred while mapping shared memory: {}", strerror(errno)); kernel = {.address = address, .size = size, .permission = permission}; + + if (shared) { + guest = kernel; + + BlockDescriptor block{ + .address = address, + .size = size, + .permission = permission, + }; + + ChunkDescriptor chunk{ + .address = address, + .host = address, + .size = size, + .state = initialState, + .blockList = {block}, + }; + + state.os->memory.InsertChunk(chunk); + } } u64 KSharedMemory::Map(const u64 address, const u64 size, memory::Permission permission) { diff --git a/app/src/main/cpp/skyline/kernel/types/KSharedMemory.h b/app/src/main/cpp/skyline/kernel/types/KSharedMemory.h index da325d56..15aa056b 100644 --- a/app/src/main/cpp/skyline/kernel/types/KSharedMemory.h +++ b/app/src/main/cpp/skyline/kernel/types/KSharedMemory.h @@ -38,7 +38,7 @@ namespace skyline::kernel::type { * @param memState The MemoryState of the chunk of memory * @param mmapFlags Additional flags to pass to mmap */ - KSharedMemory(const DeviceState &state, u64 address, size_t size, memory::Permission permission, memory::MemoryState memState = memory::states::SharedMemory, int mmapFlags = 0); + KSharedMemory(const DeviceState &state, u64 address, size_t size, memory::Permission permission, memory::MemoryState memState = memory::states::SharedMemory, int mmapFlags = 0, bool shared = false); /** * @brief Maps the shared memory in the guest diff --git a/app/src/main/cpp/skyline/os.cpp b/app/src/main/cpp/skyline/os.cpp index 1e7cee29..bf23f6ac 100644 --- a/app/src/main/cpp/skyline/os.cpp +++ b/app/src/main/cpp/skyline/os.cpp @@ -36,8 +36,9 @@ namespace skyline::kernel { } std::shared_ptr OS::CreateProcess(u64 entry, u64 argument, size_t stackSize) { - auto stack = std::make_shared(state, 0, stackSize, memory::Permission{true, true, false}, memory::states::Reserved, MAP_NORESERVE | MAP_STACK); + auto stack = std::make_shared(state, memory.stack.address, stackSize, memory::Permission{true, true, false}, memory::states::Stack, MAP_NORESERVE | MAP_STACK, true); stack->guest = stack->kernel; + if (mprotect(reinterpret_cast(stack->guest.address), PAGE_SIZE, PROT_NONE)) throw exception("Failed to create guard pages");