os: implement SharedMemory, update AslrSpaceManager

This commit is contained in:
Michael Scire
2021-10-01 00:36:18 -07:00
parent 101e3087fe
commit 82f3416799
20 changed files with 737 additions and 196 deletions

View File

@@ -34,6 +34,7 @@
#include <stratosphere/os/os_busy_mutex.hpp>
#include <stratosphere/os/os_rw_busy_mutex.hpp>
#include <stratosphere/os/os_rw_lock.hpp>
#include <stratosphere/os/os_shared_memory.hpp>
#include <stratosphere/os/os_transfer_memory.hpp>
#include <stratosphere/os/os_semaphore.hpp>
#include <stratosphere/os/os_event.hpp>

View File

@@ -0,0 +1,86 @@
/*
* Copyright (c) 2018-2020 Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <vapours.hpp>
#include <stratosphere/os/os_shared_memory_types.hpp>
#include <stratosphere/os/os_shared_memory_api.hpp>
namespace ams::os {
class SharedMemory {
NON_COPYABLE(SharedMemory);
NON_MOVEABLE(SharedMemory);
private:
SharedMemoryType m_shared_memory;
public:
constexpr SharedMemory() : m_shared_memory{ .state = SharedMemoryType::State_NotInitialized } {
/* ... */
}
SharedMemory(size_t size, MemoryPermission my_perm, MemoryPermission other_perm) {
R_ABORT_UNLESS(CreateSharedMemory(std::addressof(m_shared_memory), size, my_perm, other_perm));
}
SharedMemory(size_t size, Handle handle, bool managed) {
this->Attach(size, handle, managed);
}
~SharedMemory() {
if (m_shared_memory.state == SharedMemoryType::State_NotInitialized) {
return;
}
DestroySharedMemory(std::addressof(m_shared_memory));
}
void Attach(size_t size, Handle handle, bool managed) {
return AttachSharedMemory(std::addressof(m_shared_memory), size, handle, managed);
}
void *Map(MemoryPermission perm) {
return MapSharedMemory(std::addressof(m_shared_memory), perm);
}
void Unmap() {
return UnmapSharedMemory(std::addressof(m_shared_memory));
}
void *GetMappedAddress() const {
return GetSharedMemoryAddress(std::addressof(m_shared_memory));
}
size_t GetSize() const {
return GetSharedMemorySize(std::addressof(m_shared_memory));
}
Handle GetHandle() const {
return GetSharedMemoryHandle(std::addressof(m_shared_memory));
}
operator SharedMemoryType &() {
return m_shared_memory;
}
operator const SharedMemoryType &() const {
return m_shared_memory;
}
SharedMemoryType *GetBase() {
return std::addressof(m_shared_memory);
}
};
}

View File

@@ -0,0 +1,38 @@
/*
* Copyright (c) 2018-2020 Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <vapours.hpp>
#include <stratosphere/os/os_memory_permission.hpp>
namespace ams::os {
struct SharedMemoryType;
Result CreateSharedMemory(SharedMemoryType *shared_memory, size_t size, MemoryPermission my_perm, MemoryPermission other_perm);
void AttachSharedMemory(SharedMemoryType *shared_memory, size_t size, Handle handle, bool managed);
void DestroySharedMemory(SharedMemoryType *shared_memory);
void *MapSharedMemory(SharedMemoryType *shared_memory, MemoryPermission perm);
void UnmapSharedMemory(SharedMemoryType *shared_memory);
void *GetSharedMemoryAddress(const SharedMemoryType *shared_memory);
size_t GetSharedMemorySize(const SharedMemoryType *shared_memory);
Handle GetSharedMemoryHandle(const SharedMemoryType *shared_memory);
}

View File

@@ -0,0 +1,42 @@
/*
* Copyright (c) 2018-2020 Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <vapours.hpp>
#include <stratosphere/os/impl/os_internal_critical_section.hpp>
namespace ams::os {
struct SharedMemoryType {
enum State {
State_NotInitialized = 0,
State_Initialized = 1,
State_Mapped = 2,
};
u8 state;
bool handle_managed;
bool allocated;
void *address;
size_t size;
Handle handle;
mutable impl::InternalCriticalSectionStorage cs_shared_memory;
};
static_assert(std::is_trivial<SharedMemoryType>::value);
}