2020-02-09 11:45:45 +00:00
|
|
|
/*
|
|
|
|
* 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 <mesosphere/kern_common.hpp>
|
|
|
|
#include <mesosphere/kern_select_page_table_impl.hpp>
|
|
|
|
#include <mesosphere/kern_k_light_lock.hpp>
|
|
|
|
#include <mesosphere/kern_k_page_group.hpp>
|
|
|
|
#include <mesosphere/kern_k_memory_manager.hpp>
|
|
|
|
#include <mesosphere/kern_k_memory_layout.hpp>
|
2020-02-10 09:50:23 +00:00
|
|
|
#include <mesosphere/kern_k_memory_block_manager.hpp>
|
2020-02-09 11:45:45 +00:00
|
|
|
|
|
|
|
namespace ams::kern {
|
|
|
|
|
2020-12-01 11:33:46 +00:00
|
|
|
enum DisableMergeAttribute : u8 {
|
|
|
|
DisableMergeAttribute_None = (0u << 0),
|
|
|
|
|
|
|
|
DisableMergeAttribute_DisableHead = (1u << 0),
|
|
|
|
DisableMergeAttribute_DisableHeadAndBody = (1u << 1),
|
|
|
|
DisableMergeAttribute_EnableHeadAndBody = (1u << 2),
|
|
|
|
DisableMergeAttribute_DisableTail = (1u << 3),
|
|
|
|
DisableMergeAttribute_EnableTail = (1u << 4),
|
|
|
|
DisableMergeAttribute_EnableAndMergeHeadBodyTail = (1u << 5),
|
|
|
|
|
|
|
|
DisableMergeAttribute_EnableHeadBodyTail = DisableMergeAttribute_EnableHeadAndBody | DisableMergeAttribute_EnableTail,
|
|
|
|
DisableMergeAttribute_DisableHeadBodyTail = DisableMergeAttribute_DisableHeadAndBody | DisableMergeAttribute_DisableTail,
|
|
|
|
};
|
|
|
|
|
2020-02-14 01:38:56 +00:00
|
|
|
struct KPageProperties {
|
|
|
|
KMemoryPermission perm;
|
|
|
|
bool io;
|
|
|
|
bool uncached;
|
2020-12-01 11:33:46 +00:00
|
|
|
DisableMergeAttribute disable_merge_attributes;
|
2020-02-14 01:38:56 +00:00
|
|
|
};
|
|
|
|
static_assert(std::is_trivial<KPageProperties>::value);
|
2020-12-01 11:33:46 +00:00
|
|
|
static_assert(sizeof(KPageProperties) == sizeof(u32));
|
2020-02-14 01:38:56 +00:00
|
|
|
|
2020-02-09 11:45:45 +00:00
|
|
|
class KPageTableBase {
|
|
|
|
NON_COPYABLE(KPageTableBase);
|
|
|
|
NON_MOVEABLE(KPageTableBase);
|
2020-02-18 09:44:40 +00:00
|
|
|
public:
|
|
|
|
using TraversalEntry = KPageTableImpl::TraversalEntry;
|
|
|
|
using TraversalContext = KPageTableImpl::TraversalContext;
|
2020-02-10 02:10:13 +00:00
|
|
|
protected:
|
|
|
|
enum MemoryFillValue {
|
|
|
|
MemoryFillValue_Zero = 0,
|
|
|
|
MemoryFillValue_Stack = 'X',
|
|
|
|
MemoryFillValue_Ipc = 'Y',
|
|
|
|
MemoryFillValue_Heap = 'Z',
|
|
|
|
};
|
2020-02-14 01:38:56 +00:00
|
|
|
|
|
|
|
enum OperationType {
|
2020-02-19 16:07:44 +00:00
|
|
|
OperationType_Map = 0,
|
|
|
|
OperationType_MapGroup = 1,
|
|
|
|
OperationType_Unmap = 2,
|
|
|
|
OperationType_ChangePermissions = 3,
|
|
|
|
OperationType_ChangePermissionsAndRefresh = 4,
|
2020-02-14 01:38:56 +00:00
|
|
|
};
|
|
|
|
|
2020-02-19 13:35:22 +00:00
|
|
|
static constexpr size_t MaxPhysicalMapAlignment = 1_GB;
|
|
|
|
static constexpr size_t RegionAlignment = 2_MB;
|
|
|
|
static_assert(RegionAlignment == KernelAslrAlignment);
|
2020-02-14 01:38:56 +00:00
|
|
|
|
|
|
|
struct PageLinkedList {
|
|
|
|
private:
|
|
|
|
struct Node {
|
2020-12-18 01:18:47 +00:00
|
|
|
Node *m_next;
|
|
|
|
u8 m_buffer[PageSize - sizeof(Node *)];
|
2020-02-14 01:38:56 +00:00
|
|
|
};
|
2020-05-11 22:02:10 +00:00
|
|
|
static_assert(util::is_pod<Node>::value);
|
2020-02-14 01:38:56 +00:00
|
|
|
private:
|
2020-12-18 01:18:47 +00:00
|
|
|
Node *m_root;
|
2020-02-14 01:38:56 +00:00
|
|
|
public:
|
2020-12-18 01:18:47 +00:00
|
|
|
constexpr PageLinkedList() : m_root(nullptr) { /* ... */ }
|
2020-02-14 01:38:56 +00:00
|
|
|
|
|
|
|
void Push(Node *n) {
|
|
|
|
MESOSPHERE_ASSERT(util::IsAligned(reinterpret_cast<uintptr_t>(n), PageSize));
|
2020-12-18 01:18:47 +00:00
|
|
|
n->m_next = m_root;
|
|
|
|
m_root = n;
|
2020-02-14 01:38:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Push(KVirtualAddress addr) {
|
|
|
|
this->Push(GetPointer<Node>(addr));
|
|
|
|
}
|
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
Node *Peek() const { return m_root; }
|
2020-02-14 01:38:56 +00:00
|
|
|
|
|
|
|
Node *Pop() {
|
2020-12-18 01:18:47 +00:00
|
|
|
Node *r = m_root;
|
|
|
|
m_root = m_root->m_next;
|
2020-02-14 01:38:56 +00:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
static_assert(std::is_trivially_destructible<PageLinkedList>::value);
|
|
|
|
|
2020-04-19 00:10:26 +00:00
|
|
|
static constexpr u32 DefaultMemoryIgnoreAttr = KMemoryAttribute_IpcLocked | KMemoryAttribute_DeviceShared;
|
2020-02-19 09:22:27 +00:00
|
|
|
|
|
|
|
static constexpr size_t GetAddressSpaceWidth(ams::svc::CreateProcessFlag as_type) {
|
|
|
|
switch (static_cast<ams::svc::CreateProcessFlag>(as_type & ams::svc::CreateProcessFlag_AddressSpaceMask)) {
|
|
|
|
case ams::svc::CreateProcessFlag_AddressSpace64Bit:
|
|
|
|
return 39;
|
|
|
|
case ams::svc::CreateProcessFlag_AddressSpace64BitDeprecated:
|
|
|
|
return 36;
|
|
|
|
case ams::svc::CreateProcessFlag_AddressSpace32Bit:
|
|
|
|
case ams::svc::CreateProcessFlag_AddressSpace32BitWithoutAlias:
|
|
|
|
return 32;
|
|
|
|
MESOSPHERE_UNREACHABLE_DEFAULT_CASE();
|
|
|
|
}
|
|
|
|
}
|
2020-02-14 01:38:56 +00:00
|
|
|
private:
|
|
|
|
class KScopedPageTableUpdater {
|
|
|
|
private:
|
2020-12-18 01:18:47 +00:00
|
|
|
KPageTableBase *m_pt;
|
|
|
|
PageLinkedList m_ll;
|
2020-02-14 01:38:56 +00:00
|
|
|
public:
|
2020-12-18 01:18:47 +00:00
|
|
|
ALWAYS_INLINE explicit KScopedPageTableUpdater(KPageTableBase *pt) : m_pt(pt), m_ll() { /* ... */ }
|
2020-02-14 01:38:56 +00:00
|
|
|
ALWAYS_INLINE explicit KScopedPageTableUpdater(KPageTableBase &pt) : KScopedPageTableUpdater(std::addressof(pt)) { /* ... */ }
|
2020-12-18 01:18:47 +00:00
|
|
|
ALWAYS_INLINE ~KScopedPageTableUpdater() { m_pt->FinalizeUpdate(this->GetPageList()); }
|
2020-02-14 01:38:56 +00:00
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
PageLinkedList *GetPageList() { return std::addressof(m_ll); }
|
2020-02-14 01:38:56 +00:00
|
|
|
};
|
2020-02-09 11:45:45 +00:00
|
|
|
private:
|
2020-12-18 01:18:47 +00:00
|
|
|
KProcessAddress m_address_space_start{};
|
|
|
|
KProcessAddress m_address_space_end{};
|
|
|
|
KProcessAddress m_heap_region_start{};
|
|
|
|
KProcessAddress m_heap_region_end{};
|
|
|
|
KProcessAddress m_current_heap_end{};
|
|
|
|
KProcessAddress m_alias_region_start{};
|
|
|
|
KProcessAddress m_alias_region_end{};
|
|
|
|
KProcessAddress m_stack_region_start{};
|
|
|
|
KProcessAddress m_stack_region_end{};
|
|
|
|
KProcessAddress m_kernel_map_region_start{};
|
|
|
|
KProcessAddress m_kernel_map_region_end{};
|
|
|
|
KProcessAddress m_alias_code_region_start{};
|
|
|
|
KProcessAddress m_alias_code_region_end{};
|
|
|
|
KProcessAddress m_code_region_start{};
|
|
|
|
KProcessAddress m_code_region_end{};
|
|
|
|
size_t m_max_heap_size{};
|
|
|
|
size_t m_mapped_physical_memory_size{};
|
|
|
|
size_t m_mapped_unsafe_physical_memory{};
|
|
|
|
mutable KLightLock m_general_lock{};
|
|
|
|
mutable KLightLock m_map_physical_memory_lock{};
|
|
|
|
KPageTableImpl m_impl{};
|
|
|
|
KMemoryBlockManager m_memory_block_manager{};
|
|
|
|
u32 m_allocate_option{};
|
|
|
|
u32 m_address_space_width{};
|
|
|
|
bool m_is_kernel{};
|
|
|
|
bool m_enable_aslr{};
|
|
|
|
bool m_enable_device_address_space_merge{};
|
|
|
|
KMemoryBlockSlabManager *m_memory_block_slab_manager{};
|
|
|
|
KBlockInfoManager *m_block_info_manager{};
|
|
|
|
const KMemoryRegion *m_cached_physical_linear_region{};
|
|
|
|
const KMemoryRegion *m_cached_physical_heap_region{};
|
|
|
|
const KMemoryRegion *m_cached_virtual_heap_region{};
|
|
|
|
MemoryFillValue m_heap_fill_value{};
|
|
|
|
MemoryFillValue m_ipc_fill_value{};
|
|
|
|
MemoryFillValue m_stack_fill_value{};
|
2020-02-09 11:45:45 +00:00
|
|
|
public:
|
2020-12-18 01:18:47 +00:00
|
|
|
constexpr KPageTableBase() { /* ... */ }
|
2020-02-09 11:45:45 +00:00
|
|
|
|
2020-02-10 02:10:13 +00:00
|
|
|
NOINLINE Result InitializeForKernel(bool is_64_bit, void *table, KVirtualAddress start, KVirtualAddress end);
|
2020-12-01 14:53:22 +00:00
|
|
|
NOINLINE Result InitializeForProcess(ams::svc::CreateProcessFlag as_type, bool enable_aslr, bool enable_device_address_space_merge, bool from_back, KMemoryManager::Pool pool, void *table, KProcessAddress start, KProcessAddress end, KProcessAddress code_address, size_t code_size, KMemoryBlockSlabManager *mem_block_slab_manager, KBlockInfoManager *block_info_manager);
|
2020-02-09 11:45:45 +00:00
|
|
|
|
|
|
|
void Finalize();
|
2020-02-14 01:38:56 +00:00
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
constexpr bool IsKernel() const { return m_is_kernel; }
|
|
|
|
constexpr bool IsAslrEnabled() const { return m_enable_aslr; }
|
2020-02-14 01:38:56 +00:00
|
|
|
|
2020-02-20 17:05:01 +00:00
|
|
|
constexpr bool Contains(KProcessAddress addr) const {
|
2020-12-18 01:18:47 +00:00
|
|
|
return m_address_space_start <= addr && addr <= m_address_space_end - 1;
|
2020-02-14 01:38:56 +00:00
|
|
|
}
|
|
|
|
|
2020-02-20 17:05:01 +00:00
|
|
|
constexpr bool Contains(KProcessAddress addr, size_t size) const {
|
2020-12-18 01:18:47 +00:00
|
|
|
return m_address_space_start <= addr && addr < addr + size && addr + size - 1 <= m_address_space_end - 1;
|
2020-02-14 01:38:56 +00:00
|
|
|
}
|
|
|
|
|
2020-07-24 15:07:34 +00:00
|
|
|
constexpr bool IsInAliasRegion(KProcessAddress addr, size_t size) const {
|
2020-12-18 01:18:47 +00:00
|
|
|
return this->Contains(addr, size) && m_alias_region_start <= addr && addr + size - 1 <= m_alias_region_end - 1;
|
2020-07-24 15:07:34 +00:00
|
|
|
}
|
|
|
|
|
2020-07-25 05:04:04 +00:00
|
|
|
bool IsInUnsafeAliasRegion(KProcessAddress addr, size_t size) const {
|
|
|
|
/* Even though Unsafe physical memory is KMemoryState_Normal, it must be mapped inside the alias code region. */
|
|
|
|
return this->CanContain(addr, size, KMemoryState_AliasCode);
|
|
|
|
}
|
|
|
|
|
2020-02-14 01:38:56 +00:00
|
|
|
KProcessAddress GetRegionAddress(KMemoryState state) const;
|
|
|
|
size_t GetRegionSize(KMemoryState state) const;
|
2020-02-19 09:22:27 +00:00
|
|
|
bool CanContain(KProcessAddress addr, size_t size, KMemoryState state) const;
|
2020-02-14 01:38:56 +00:00
|
|
|
protected:
|
|
|
|
virtual Result Operate(PageLinkedList *page_list, KProcessAddress virt_addr, size_t num_pages, KPhysicalAddress phys_addr, bool is_pa_valid, const KPageProperties properties, OperationType operation, bool reuse_ll) = 0;
|
2020-02-20 03:38:20 +00:00
|
|
|
virtual Result Operate(PageLinkedList *page_list, KProcessAddress virt_addr, size_t num_pages, const KPageGroup &page_group, const KPageProperties properties, OperationType operation, bool reuse_ll) = 0;
|
2020-02-14 01:38:56 +00:00
|
|
|
virtual void FinalizeUpdate(PageLinkedList *page_list) = 0;
|
|
|
|
|
2021-01-08 10:13:36 +00:00
|
|
|
ALWAYS_INLINE KPageTableImpl &GetImpl() { return m_impl; }
|
|
|
|
ALWAYS_INLINE const KPageTableImpl &GetImpl() const { return m_impl; }
|
2020-02-14 01:38:56 +00:00
|
|
|
|
2021-01-08 10:13:36 +00:00
|
|
|
ALWAYS_INLINE bool IsLockedByCurrentThread() const { return m_general_lock.IsLockedByCurrentThread(); }
|
2020-02-14 01:38:56 +00:00
|
|
|
|
2021-01-08 10:13:36 +00:00
|
|
|
ALWAYS_INLINE bool IsLinearMappedPhysicalAddress(KPhysicalAddress phys_addr) {
|
2020-07-11 03:09:06 +00:00
|
|
|
MESOSPHERE_ASSERT(this->IsLockedByCurrentThread());
|
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
return KMemoryLayout::IsLinearMappedPhysicalAddress(m_cached_physical_linear_region, phys_addr);
|
2020-07-11 03:09:06 +00:00
|
|
|
}
|
|
|
|
|
2021-01-08 10:13:36 +00:00
|
|
|
ALWAYS_INLINE bool IsLinearMappedPhysicalAddress(KPhysicalAddress phys_addr, size_t size) {
|
2020-07-11 03:09:06 +00:00
|
|
|
MESOSPHERE_ASSERT(this->IsLockedByCurrentThread());
|
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
return KMemoryLayout::IsLinearMappedPhysicalAddress(m_cached_physical_linear_region, phys_addr, size);
|
2020-07-11 03:09:06 +00:00
|
|
|
}
|
|
|
|
|
2021-01-08 10:13:36 +00:00
|
|
|
ALWAYS_INLINE bool IsHeapPhysicalAddress(KPhysicalAddress phys_addr) {
|
2020-02-17 10:49:21 +00:00
|
|
|
MESOSPHERE_ASSERT(this->IsLockedByCurrentThread());
|
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
return KMemoryLayout::IsHeapPhysicalAddress(m_cached_physical_heap_region, phys_addr);
|
2020-02-17 10:49:21 +00:00
|
|
|
}
|
|
|
|
|
2021-01-08 10:13:36 +00:00
|
|
|
ALWAYS_INLINE bool IsHeapPhysicalAddress(KPhysicalAddress phys_addr, size_t size) {
|
2020-08-03 19:06:24 +00:00
|
|
|
MESOSPHERE_ASSERT(this->IsLockedByCurrentThread());
|
2020-07-23 06:52:29 +00:00
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
return KMemoryLayout::IsHeapPhysicalAddress(m_cached_physical_heap_region, phys_addr, size);
|
2020-07-23 06:52:29 +00:00
|
|
|
}
|
|
|
|
|
2021-01-08 10:13:36 +00:00
|
|
|
ALWAYS_INLINE bool IsHeapPhysicalAddressForFinalize(KPhysicalAddress phys_addr) {
|
2020-08-03 19:06:24 +00:00
|
|
|
MESOSPHERE_ASSERT(!this->IsLockedByCurrentThread());
|
2020-02-17 10:49:21 +00:00
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
return KMemoryLayout::IsHeapPhysicalAddress(m_cached_physical_heap_region, phys_addr);
|
2020-02-17 10:49:21 +00:00
|
|
|
}
|
|
|
|
|
2021-01-08 10:13:36 +00:00
|
|
|
ALWAYS_INLINE bool IsHeapVirtualAddress(KVirtualAddress virt_addr) {
|
2020-02-17 10:49:21 +00:00
|
|
|
MESOSPHERE_ASSERT(this->IsLockedByCurrentThread());
|
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
return KMemoryLayout::IsHeapVirtualAddress(m_cached_virtual_heap_region, virt_addr);
|
2020-02-17 10:49:21 +00:00
|
|
|
}
|
|
|
|
|
2021-01-08 10:13:36 +00:00
|
|
|
ALWAYS_INLINE bool IsHeapVirtualAddress(KVirtualAddress virt_addr, size_t size) {
|
2020-02-17 10:49:21 +00:00
|
|
|
MESOSPHERE_ASSERT(this->IsLockedByCurrentThread());
|
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
return KMemoryLayout::IsHeapVirtualAddress(m_cached_virtual_heap_region, virt_addr, size);
|
2020-02-14 01:38:56 +00:00
|
|
|
}
|
|
|
|
|
2021-01-08 10:13:36 +00:00
|
|
|
ALWAYS_INLINE bool ContainsPages(KProcessAddress addr, size_t num_pages) const {
|
2020-12-18 01:18:47 +00:00
|
|
|
return (m_address_space_start <= addr) && (num_pages <= (m_address_space_end - m_address_space_start) / PageSize) && (addr + num_pages * PageSize - 1 <= m_address_space_end - 1);
|
2020-02-14 01:38:56 +00:00
|
|
|
}
|
|
|
|
private:
|
|
|
|
constexpr size_t GetNumGuardPages() const { return this->IsKernel() ? 1 : 4; }
|
|
|
|
ALWAYS_INLINE KProcessAddress FindFreeArea(KProcessAddress region_start, size_t region_num_pages, size_t num_pages, size_t alignment, size_t offset, size_t guard_pages) const;
|
|
|
|
|
2020-12-01 12:24:43 +00:00
|
|
|
Result CheckMemoryStateContiguous(size_t *out_blocks_needed, KProcessAddress addr, size_t size, u32 state_mask, u32 state, u32 perm_mask, u32 perm, u32 attr_mask, u32 attr) const;
|
|
|
|
Result CheckMemoryStateContiguous(KProcessAddress addr, size_t size, u32 state_mask, u32 state, u32 perm_mask, u32 perm, u32 attr_mask, u32 attr) const {
|
|
|
|
return this->CheckMemoryStateContiguous(nullptr, addr, size, state_mask, state, perm_mask, perm, attr_mask, attr);
|
|
|
|
}
|
2020-07-11 03:09:06 +00:00
|
|
|
|
2020-02-14 01:38:56 +00:00
|
|
|
Result CheckMemoryState(const KMemoryInfo &info, u32 state_mask, u32 state, u32 perm_mask, u32 perm, u32 attr_mask, u32 attr) const;
|
2020-12-01 12:24:43 +00:00
|
|
|
Result CheckMemoryState(KMemoryState *out_state, KMemoryPermission *out_perm, KMemoryAttribute *out_attr, size_t *out_blocks_needed, KProcessAddress addr, size_t size, u32 state_mask, u32 state, u32 perm_mask, u32 perm, u32 attr_mask, u32 attr, u32 ignore_attr = DefaultMemoryIgnoreAttr) const;
|
|
|
|
Result CheckMemoryState(size_t *out_blocks_needed, KProcessAddress addr, size_t size, u32 state_mask, u32 state, u32 perm_mask, u32 perm, u32 attr_mask, u32 attr, u32 ignore_attr = DefaultMemoryIgnoreAttr) const {
|
|
|
|
return this->CheckMemoryState(nullptr, nullptr, nullptr, out_blocks_needed, addr, size, state_mask, state, perm_mask, perm, attr_mask, attr, ignore_attr);
|
|
|
|
}
|
2020-02-14 01:38:56 +00:00
|
|
|
Result CheckMemoryState(KProcessAddress addr, size_t size, u32 state_mask, u32 state, u32 perm_mask, u32 perm, u32 attr_mask, u32 attr, u32 ignore_attr = DefaultMemoryIgnoreAttr) const {
|
2020-12-01 12:24:43 +00:00
|
|
|
return this->CheckMemoryState(nullptr, addr, size, state_mask, state, perm_mask, perm, attr_mask, attr, ignore_attr);
|
2020-02-14 01:38:56 +00:00
|
|
|
}
|
|
|
|
|
2020-07-10 04:30:29 +00:00
|
|
|
Result LockMemoryAndOpen(KPageGroup *out_pg, KPhysicalAddress *out_paddr, KProcessAddress addr, size_t size, u32 state_mask, u32 state, u32 perm_mask, u32 perm, u32 attr_mask, u32 attr, KMemoryPermission new_perm, u32 lock_attr);
|
2020-07-09 23:32:37 +00:00
|
|
|
Result UnlockMemory(KProcessAddress addr, size_t size, u32 state_mask, u32 state, u32 perm_mask, u32 perm, u32 attr_mask, u32 attr, KMemoryPermission new_perm, u32 lock_attr, const KPageGroup *pg);
|
|
|
|
|
2020-02-14 01:38:56 +00:00
|
|
|
Result QueryInfoImpl(KMemoryInfo *out_info, ams::svc::PageInfo *out_page, KProcessAddress address) const;
|
2020-07-13 20:24:32 +00:00
|
|
|
|
|
|
|
Result QueryMappingImpl(KProcessAddress *out, KPhysicalAddress address, size_t size, KMemoryState state) const;
|
|
|
|
|
2020-12-01 11:33:46 +00:00
|
|
|
Result AllocateAndMapPagesImpl(PageLinkedList *page_list, KProcessAddress address, size_t num_pages, KMemoryPermission perm);
|
2020-02-17 10:49:21 +00:00
|
|
|
Result MapPageGroupImpl(PageLinkedList *page_list, KProcessAddress address, const KPageGroup &pg, const KPageProperties properties, bool reuse_ll);
|
2020-02-14 01:38:56 +00:00
|
|
|
|
2020-12-01 11:33:46 +00:00
|
|
|
void RemapPageGroup(PageLinkedList *page_list, KProcessAddress address, size_t size, const KPageGroup &pg);
|
|
|
|
|
2020-02-19 16:07:44 +00:00
|
|
|
Result MakePageGroup(KPageGroup &pg, KProcessAddress addr, size_t num_pages);
|
2020-02-18 09:44:40 +00:00
|
|
|
bool IsValidPageGroup(const KPageGroup &pg, KProcessAddress addr, size_t num_pages);
|
2020-02-18 09:04:44 +00:00
|
|
|
|
2020-02-14 01:38:56 +00:00
|
|
|
NOINLINE Result MapPages(KProcessAddress *out_addr, size_t num_pages, size_t alignment, KPhysicalAddress phys_addr, bool is_pa_valid, KProcessAddress region_start, size_t region_num_pages, KMemoryState state, KMemoryPermission perm);
|
2020-07-12 22:42:47 +00:00
|
|
|
|
2020-12-01 12:24:43 +00:00
|
|
|
Result SetupForIpcClient(PageLinkedList *page_list, size_t *out_blocks_needed, KProcessAddress address, size_t size, KMemoryPermission test_perm, KMemoryState dst_state);
|
2020-07-12 22:42:47 +00:00
|
|
|
Result SetupForIpcServer(KProcessAddress *out_addr, size_t size, KProcessAddress src_addr, KMemoryPermission test_perm, KMemoryState dst_state, KPageTableBase &src_page_table, bool send);
|
2020-12-01 11:33:46 +00:00
|
|
|
void CleanupForIpcClientOnServerSetupFailure(PageLinkedList *page_list, KProcessAddress address, size_t size, KMemoryPermission prot_perm);
|
2020-12-11 00:31:47 +00:00
|
|
|
|
|
|
|
size_t GetSize(KMemoryState state) const;
|
2020-02-14 01:38:56 +00:00
|
|
|
public:
|
2020-02-15 03:58:57 +00:00
|
|
|
bool GetPhysicalAddress(KPhysicalAddress *out, KProcessAddress virt_addr) const {
|
|
|
|
return this->GetImpl().GetPhysicalAddress(out, virt_addr);
|
|
|
|
}
|
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
KBlockInfoManager *GetBlockInfoManager() const { return m_block_info_manager; }
|
2020-03-10 06:23:38 +00:00
|
|
|
|
2020-02-19 16:07:44 +00:00
|
|
|
Result SetMemoryPermission(KProcessAddress addr, size_t size, ams::svc::MemoryPermission perm);
|
|
|
|
Result SetProcessMemoryPermission(KProcessAddress addr, size_t size, ams::svc::MemoryPermission perm);
|
2020-07-23 01:46:28 +00:00
|
|
|
Result SetMemoryAttribute(KProcessAddress addr, size_t size, u32 mask, u32 attr);
|
2020-02-20 03:38:20 +00:00
|
|
|
Result SetHeapSize(KProcessAddress *out, size_t size);
|
|
|
|
Result SetMaxHeapSize(size_t size);
|
2020-02-20 17:05:01 +00:00
|
|
|
Result QueryInfo(KMemoryInfo *out_info, ams::svc::PageInfo *out_page_info, KProcessAddress addr) const;
|
2020-07-31 00:26:09 +00:00
|
|
|
Result QueryPhysicalAddress(ams::svc::PhysicalMemoryInfo *out, KProcessAddress address) const;
|
2020-07-13 20:24:32 +00:00
|
|
|
Result QueryStaticMapping(KProcessAddress *out, KPhysicalAddress address, size_t size) const { return this->QueryMappingImpl(out, address, size, KMemoryState_Static); }
|
|
|
|
Result QueryIoMapping(KProcessAddress *out, KPhysicalAddress address, size_t size) const { return this->QueryMappingImpl(out, address, size, KMemoryState_Io); }
|
2020-07-10 01:12:04 +00:00
|
|
|
Result MapMemory(KProcessAddress dst_address, KProcessAddress src_address, size_t size);
|
|
|
|
Result UnmapMemory(KProcessAddress dst_address, KProcessAddress src_address, size_t size);
|
2020-07-24 00:22:27 +00:00
|
|
|
Result MapCodeMemory(KProcessAddress dst_address, KProcessAddress src_address, size_t size);
|
|
|
|
Result UnmapCodeMemory(KProcessAddress dst_address, KProcessAddress src_address, size_t size);
|
2020-02-19 12:55:00 +00:00
|
|
|
Result MapIo(KPhysicalAddress phys_addr, size_t size, KMemoryPermission perm);
|
|
|
|
Result MapStatic(KPhysicalAddress phys_addr, size_t size, KMemoryPermission perm);
|
|
|
|
Result MapRegion(KMemoryRegionType region_type, KMemoryPermission perm);
|
|
|
|
|
2020-02-14 01:38:56 +00:00
|
|
|
Result MapPages(KProcessAddress *out_addr, size_t num_pages, size_t alignment, KPhysicalAddress phys_addr, KProcessAddress region_start, size_t region_num_pages, KMemoryState state, KMemoryPermission perm) {
|
|
|
|
return this->MapPages(out_addr, num_pages, alignment, phys_addr, true, region_start, region_num_pages, state, perm);
|
|
|
|
}
|
2020-02-17 10:49:21 +00:00
|
|
|
|
2020-02-19 14:46:59 +00:00
|
|
|
Result MapPages(KProcessAddress *out_addr, size_t num_pages, size_t alignment, KPhysicalAddress phys_addr, KMemoryState state, KMemoryPermission perm) {
|
|
|
|
return this->MapPages(out_addr, num_pages, alignment, phys_addr, true, this->GetRegionAddress(state), this->GetRegionSize(state) / PageSize, state, perm);
|
|
|
|
}
|
|
|
|
|
2020-02-20 03:38:20 +00:00
|
|
|
Result MapPages(KProcessAddress *out_addr, size_t num_pages, KMemoryState state, KMemoryPermission perm) {
|
|
|
|
return this->MapPages(out_addr, num_pages, PageSize, Null<KPhysicalAddress>, false, this->GetRegionAddress(state), this->GetRegionSize(state) / PageSize, state, perm);
|
|
|
|
}
|
|
|
|
|
2020-07-22 05:13:16 +00:00
|
|
|
Result MapPages(KProcessAddress address, size_t num_pages, KMemoryState state, KMemoryPermission perm);
|
2020-02-17 10:49:21 +00:00
|
|
|
Result UnmapPages(KProcessAddress address, size_t num_pages, KMemoryState state);
|
2020-07-22 05:13:16 +00:00
|
|
|
|
2020-02-17 10:49:21 +00:00
|
|
|
Result MapPageGroup(KProcessAddress *out_addr, const KPageGroup &pg, KProcessAddress region_start, size_t region_num_pages, KMemoryState state, KMemoryPermission perm);
|
2020-02-19 09:22:27 +00:00
|
|
|
Result MapPageGroup(KProcessAddress address, const KPageGroup &pg, KMemoryState state, KMemoryPermission perm);
|
2020-02-17 10:49:21 +00:00
|
|
|
Result UnmapPageGroup(KProcessAddress address, const KPageGroup &pg, KMemoryState state);
|
2020-03-10 06:23:38 +00:00
|
|
|
|
|
|
|
Result MakeAndOpenPageGroup(KPageGroup *out, KProcessAddress address, size_t num_pages, u32 state_mask, u32 state, u32 perm_mask, u32 perm, u32 attr_mask, u32 attr);
|
2020-07-09 23:32:37 +00:00
|
|
|
|
2020-07-24 03:07:40 +00:00
|
|
|
Result InvalidateProcessDataCache(KProcessAddress address, size_t size);
|
|
|
|
|
2020-07-30 10:37:40 +00:00
|
|
|
Result ReadDebugMemory(void *buffer, KProcessAddress address, size_t size);
|
|
|
|
Result WriteDebugMemory(KProcessAddress address, const void *buffer, size_t size);
|
|
|
|
|
2020-07-15 01:46:25 +00:00
|
|
|
Result LockForDeviceAddressSpace(KPageGroup *out, KProcessAddress address, size_t size, KMemoryPermission perm, bool is_aligned);
|
|
|
|
Result UnlockForDeviceAddressSpace(KProcessAddress address, size_t size);
|
2020-12-01 14:53:22 +00:00
|
|
|
|
|
|
|
Result MakePageGroupForUnmapDeviceAddressSpace(KPageGroup *out, KProcessAddress address, size_t size);
|
|
|
|
Result UnlockForDeviceAddressSpacePartialMap(KProcessAddress address, size_t size, size_t mapped_size);
|
|
|
|
|
2020-07-10 04:30:29 +00:00
|
|
|
Result LockForIpcUserBuffer(KPhysicalAddress *out, KProcessAddress address, size_t size);
|
2020-07-09 23:32:37 +00:00
|
|
|
Result UnlockForIpcUserBuffer(KProcessAddress address, size_t size);
|
2020-07-10 08:15:14 +00:00
|
|
|
|
2020-07-23 07:44:33 +00:00
|
|
|
Result LockForTransferMemory(KPageGroup *out, KProcessAddress address, size_t size, KMemoryPermission perm);
|
|
|
|
Result UnlockForTransferMemory(KProcessAddress address, size_t size, const KPageGroup &pg);
|
2020-07-28 22:09:07 +00:00
|
|
|
Result LockForCodeMemory(KPageGroup *out, KProcessAddress address, size_t size);
|
2020-07-23 07:44:33 +00:00
|
|
|
Result UnlockForCodeMemory(KProcessAddress address, size_t size, const KPageGroup &pg);
|
|
|
|
|
2020-07-10 08:15:14 +00:00
|
|
|
Result CopyMemoryFromLinearToUser(KProcessAddress dst_addr, size_t size, KProcessAddress src_addr, u32 src_state_mask, u32 src_state, KMemoryPermission src_test_perm, u32 src_attr_mask, u32 src_attr);
|
|
|
|
Result CopyMemoryFromLinearToKernel(KProcessAddress dst_addr, size_t size, KProcessAddress src_addr, u32 src_state_mask, u32 src_state, KMemoryPermission src_test_perm, u32 src_attr_mask, u32 src_attr);
|
|
|
|
Result CopyMemoryFromUserToLinear(KProcessAddress dst_addr, size_t size, u32 dst_state_mask, u32 dst_state, KMemoryPermission dst_test_perm, u32 dst_attr_mask, u32 dst_attr, KProcessAddress src_addr);
|
|
|
|
Result CopyMemoryFromKernelToLinear(KProcessAddress dst_addr, size_t size, u32 dst_state_mask, u32 dst_state, KMemoryPermission dst_test_perm, u32 dst_attr_mask, u32 dst_attr, KProcessAddress src_addr);
|
2020-07-11 04:37:56 +00:00
|
|
|
Result CopyMemoryFromHeapToHeap(KPageTableBase &dst_page_table, KProcessAddress dst_addr, size_t size, u32 dst_state_mask, u32 dst_state, KMemoryPermission dst_test_perm, u32 dst_attr_mask, u32 dst_attr, KProcessAddress src_addr, u32 src_state_mask, u32 src_state, KMemoryPermission src_test_perm, u32 src_attr_mask, u32 src_attr);
|
|
|
|
Result CopyMemoryFromHeapToHeapWithoutCheckDestination(KPageTableBase &dst_page_table, KProcessAddress dst_addr, size_t size, u32 dst_state_mask, u32 dst_state, KMemoryPermission dst_test_perm, u32 dst_attr_mask, u32 dst_attr, KProcessAddress src_addr, u32 src_state_mask, u32 src_state, KMemoryPermission src_test_perm, u32 src_attr_mask, u32 src_attr);
|
2020-07-10 15:49:10 +00:00
|
|
|
|
|
|
|
Result SetupForIpc(KProcessAddress *out_dst_addr, size_t size, KProcessAddress src_addr, KPageTableBase &src_page_table, KMemoryPermission test_perm, KMemoryState dst_state, bool send);
|
|
|
|
Result CleanupForIpcServer(KProcessAddress address, size_t size, KMemoryState dst_state, KProcess *server_process);
|
|
|
|
Result CleanupForIpcClient(KProcessAddress address, size_t size, KMemoryState dst_state);
|
2020-07-21 07:56:13 +00:00
|
|
|
|
2020-07-24 15:07:34 +00:00
|
|
|
Result MapPhysicalMemory(KProcessAddress address, size_t size);
|
|
|
|
Result UnmapPhysicalMemory(KProcessAddress address, size_t size);
|
|
|
|
|
|
|
|
Result MapPhysicalMemoryUnsafe(KProcessAddress address, size_t size);
|
|
|
|
Result UnmapPhysicalMemoryUnsafe(KProcessAddress address, size_t size);
|
|
|
|
|
2020-12-10 11:31:57 +00:00
|
|
|
void DumpMemoryBlocksLocked() const {
|
|
|
|
MESOSPHERE_ASSERT(this->IsLockedByCurrentThread());
|
2020-12-18 01:18:47 +00:00
|
|
|
m_memory_block_manager.DumpBlocks();
|
2020-07-21 07:56:13 +00:00
|
|
|
}
|
2020-07-29 22:29:01 +00:00
|
|
|
|
|
|
|
void DumpMemoryBlocks() const {
|
2020-12-18 01:18:47 +00:00
|
|
|
KScopedLightLock lk(m_general_lock);
|
2020-07-29 22:29:01 +00:00
|
|
|
this->DumpMemoryBlocksLocked();
|
|
|
|
}
|
|
|
|
|
2020-12-10 11:31:57 +00:00
|
|
|
void DumpPageTable() const {
|
2020-12-18 01:18:47 +00:00
|
|
|
KScopedLightLock lk(m_general_lock);
|
|
|
|
this->GetImpl().Dump(GetInteger(m_address_space_start), m_address_space_end - m_address_space_start);
|
2020-12-10 11:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t CountPageTables() const {
|
2020-12-18 01:18:47 +00:00
|
|
|
KScopedLightLock lk(m_general_lock);
|
2020-12-10 11:31:57 +00:00
|
|
|
return this->GetImpl().CountPageTables();
|
2020-07-29 22:29:01 +00:00
|
|
|
}
|
2020-02-19 14:46:59 +00:00
|
|
|
public:
|
2020-12-18 01:18:47 +00:00
|
|
|
KProcessAddress GetAddressSpaceStart() const { return m_address_space_start; }
|
|
|
|
KProcessAddress GetHeapRegionStart() const { return m_heap_region_start; }
|
|
|
|
KProcessAddress GetAliasRegionStart() const { return m_alias_region_start; }
|
|
|
|
KProcessAddress GetStackRegionStart() const { return m_stack_region_start; }
|
|
|
|
KProcessAddress GetKernelMapRegionStart() const { return m_kernel_map_region_start; }
|
|
|
|
KProcessAddress GetAliasCodeRegionStart() const { return m_alias_code_region_start; }
|
|
|
|
|
|
|
|
size_t GetAddressSpaceSize() const { return m_address_space_end - m_address_space_start; }
|
|
|
|
size_t GetHeapRegionSize() const { return m_heap_region_end - m_heap_region_start; }
|
|
|
|
size_t GetAliasRegionSize() const { return m_alias_region_end - m_alias_region_start; }
|
|
|
|
size_t GetStackRegionSize() const { return m_stack_region_end - m_stack_region_start; }
|
|
|
|
size_t GetKernelMapRegionSize() const { return m_kernel_map_region_end - m_kernel_map_region_start; }
|
|
|
|
size_t GetAliasCodeRegionSize() const { return m_alias_code_region_end - m_alias_code_region_start; }
|
2020-05-29 07:57:25 +00:00
|
|
|
|
|
|
|
size_t GetNormalMemorySize() const {
|
|
|
|
/* Lock the table. */
|
2020-12-18 01:18:47 +00:00
|
|
|
KScopedLightLock lk(m_general_lock);
|
2020-05-29 07:57:25 +00:00
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
return (m_current_heap_end - m_heap_region_start) + m_mapped_physical_memory_size;
|
2020-05-29 07:57:25 +00:00
|
|
|
}
|
2020-07-22 09:50:19 +00:00
|
|
|
|
2020-12-11 00:31:47 +00:00
|
|
|
size_t GetCodeSize() const;
|
|
|
|
size_t GetCodeDataSize() const;
|
|
|
|
size_t GetAliasCodeSize() const;
|
|
|
|
size_t GetAliasCodeDataSize() const;
|
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
u32 GetAllocateOption() const { return m_allocate_option; }
|
2020-02-09 11:45:45 +00:00
|
|
|
public:
|
2020-07-11 03:09:06 +00:00
|
|
|
static ALWAYS_INLINE KVirtualAddress GetLinearMappedVirtualAddress(KPhysicalAddress addr) {
|
2020-02-09 11:45:45 +00:00
|
|
|
return KMemoryLayout::GetLinearVirtualAddress(addr);
|
|
|
|
}
|
|
|
|
|
2020-07-11 03:09:06 +00:00
|
|
|
static ALWAYS_INLINE KPhysicalAddress GetLinearMappedPhysicalAddress(KVirtualAddress addr) {
|
2020-02-09 11:45:45 +00:00
|
|
|
return KMemoryLayout::GetLinearPhysicalAddress(addr);
|
|
|
|
}
|
2020-02-14 01:38:56 +00:00
|
|
|
|
|
|
|
static ALWAYS_INLINE KVirtualAddress GetHeapVirtualAddress(KPhysicalAddress addr) {
|
2020-07-11 03:09:06 +00:00
|
|
|
return GetLinearMappedVirtualAddress(addr);
|
2020-02-14 01:38:56 +00:00
|
|
|
}
|
|
|
|
|
2020-02-15 08:00:35 +00:00
|
|
|
static ALWAYS_INLINE KPhysicalAddress GetHeapPhysicalAddress(KVirtualAddress addr) {
|
2020-07-11 03:09:06 +00:00
|
|
|
return GetLinearMappedPhysicalAddress(addr);
|
2020-02-15 08:00:35 +00:00
|
|
|
}
|
|
|
|
|
2020-02-14 01:38:56 +00:00
|
|
|
static ALWAYS_INLINE KVirtualAddress GetPageTableVirtualAddress(KPhysicalAddress addr) {
|
2020-07-11 03:09:06 +00:00
|
|
|
return GetLinearMappedVirtualAddress(addr);
|
2020-02-14 01:38:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static ALWAYS_INLINE KPhysicalAddress GetPageTablePhysicalAddress(KVirtualAddress addr) {
|
2020-07-11 03:09:06 +00:00
|
|
|
return GetLinearMappedPhysicalAddress(addr);
|
2020-02-14 01:38:56 +00:00
|
|
|
}
|
2020-02-09 11:45:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|