2020-02-07 12:58:35 +00:00
|
|
|
/*
|
2021-10-04 19:59:10 +00:00
|
|
|
* Copyright (c) Atmosphère-NX
|
2020-02-07 12:58:35 +00:00
|
|
|
*
|
|
|
|
* 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_k_typed_address.hpp>
|
|
|
|
#include <mesosphere/kern_slab_helpers.hpp>
|
|
|
|
|
|
|
|
namespace ams::kern {
|
|
|
|
|
|
|
|
enum KMemoryState : u32 {
|
|
|
|
KMemoryState_None = 0,
|
|
|
|
KMemoryState_Mask = 0xFF,
|
2020-02-14 01:38:56 +00:00
|
|
|
KMemoryState_All = ~KMemoryState_None,
|
2020-02-07 12:58:35 +00:00
|
|
|
|
|
|
|
KMemoryState_FlagCanReprotect = (1 << 8),
|
|
|
|
KMemoryState_FlagCanDebug = (1 << 9),
|
|
|
|
KMemoryState_FlagCanUseIpc = (1 << 10),
|
|
|
|
KMemoryState_FlagCanUseNonDeviceIpc = (1 << 11),
|
|
|
|
KMemoryState_FlagCanUseNonSecureIpc = (1 << 12),
|
|
|
|
KMemoryState_FlagMapped = (1 << 13),
|
|
|
|
KMemoryState_FlagCode = (1 << 14),
|
|
|
|
KMemoryState_FlagCanAlias = (1 << 15),
|
|
|
|
KMemoryState_FlagCanCodeAlias = (1 << 16),
|
|
|
|
KMemoryState_FlagCanTransfer = (1 << 17),
|
|
|
|
KMemoryState_FlagCanQueryPhysical = (1 << 18),
|
|
|
|
KMemoryState_FlagCanDeviceMap = (1 << 19),
|
|
|
|
KMemoryState_FlagCanAlignedDeviceMap = (1 << 20),
|
|
|
|
KMemoryState_FlagCanIpcUserBuffer = (1 << 21),
|
|
|
|
KMemoryState_FlagReferenceCounted = (1 << 22),
|
|
|
|
KMemoryState_FlagCanMapProcess = (1 << 23),
|
|
|
|
KMemoryState_FlagCanChangeAttribute = (1 << 24),
|
|
|
|
KMemoryState_FlagCanCodeMemory = (1 << 25),
|
2022-10-12 06:14:15 +00:00
|
|
|
KMemoryState_FlagLinearMapped = (1 << 26),
|
2023-10-11 12:00:23 +00:00
|
|
|
KMemoryState_FlagCanPermissionLock = (1 << 27),
|
2020-02-07 12:58:35 +00:00
|
|
|
|
|
|
|
KMemoryState_FlagsData = KMemoryState_FlagCanReprotect | KMemoryState_FlagCanUseIpc |
|
|
|
|
KMemoryState_FlagCanUseNonDeviceIpc | KMemoryState_FlagCanUseNonSecureIpc |
|
|
|
|
KMemoryState_FlagMapped | KMemoryState_FlagCanAlias |
|
|
|
|
KMemoryState_FlagCanTransfer | KMemoryState_FlagCanQueryPhysical |
|
|
|
|
KMemoryState_FlagCanDeviceMap | KMemoryState_FlagCanAlignedDeviceMap |
|
|
|
|
KMemoryState_FlagCanIpcUserBuffer | KMemoryState_FlagReferenceCounted |
|
2022-10-12 06:14:15 +00:00
|
|
|
KMemoryState_FlagCanChangeAttribute | KMemoryState_FlagLinearMapped,
|
2020-02-07 12:58:35 +00:00
|
|
|
|
|
|
|
KMemoryState_FlagsCode = KMemoryState_FlagCanDebug | KMemoryState_FlagCanUseIpc |
|
|
|
|
KMemoryState_FlagCanUseNonDeviceIpc | KMemoryState_FlagCanUseNonSecureIpc |
|
|
|
|
KMemoryState_FlagMapped | KMemoryState_FlagCode |
|
|
|
|
KMemoryState_FlagCanQueryPhysical | KMemoryState_FlagCanDeviceMap |
|
2022-10-12 06:14:15 +00:00
|
|
|
KMemoryState_FlagCanAlignedDeviceMap | KMemoryState_FlagReferenceCounted |
|
|
|
|
KMemoryState_FlagLinearMapped,
|
2020-02-07 12:58:35 +00:00
|
|
|
|
|
|
|
KMemoryState_FlagsMisc = KMemoryState_FlagMapped | KMemoryState_FlagReferenceCounted |
|
2022-10-12 06:14:15 +00:00
|
|
|
KMemoryState_FlagCanQueryPhysical | KMemoryState_FlagCanDeviceMap |
|
|
|
|
KMemoryState_FlagLinearMapped,
|
2020-02-07 12:58:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
KMemoryState_Free = ams::svc::MemoryState_Free,
|
2023-10-11 12:00:23 +00:00
|
|
|
|
|
|
|
KMemoryState_IoMemory = ams::svc::MemoryState_Io | KMemoryState_FlagMapped | KMemoryState_FlagCanDeviceMap | KMemoryState_FlagCanAlignedDeviceMap,
|
|
|
|
KMemoryState_IoRegister = ams::svc::MemoryState_Io | KMemoryState_FlagCanDeviceMap | KMemoryState_FlagCanAlignedDeviceMap,
|
|
|
|
|
|
|
|
|
|
|
|
KMemoryState_Static = ams::svc::MemoryState_Static | KMemoryState_FlagCanQueryPhysical,
|
2020-02-07 12:58:35 +00:00
|
|
|
KMemoryState_Code = ams::svc::MemoryState_Code | KMemoryState_FlagsCode | KMemoryState_FlagCanMapProcess,
|
2023-10-11 12:00:23 +00:00
|
|
|
KMemoryState_CodeData = ams::svc::MemoryState_CodeData | KMemoryState_FlagsData | KMemoryState_FlagCanMapProcess | KMemoryState_FlagCanCodeMemory | KMemoryState_FlagCanPermissionLock,
|
2020-02-07 12:58:35 +00:00
|
|
|
KMemoryState_Normal = ams::svc::MemoryState_Normal | KMemoryState_FlagsData | KMemoryState_FlagCanCodeMemory,
|
2022-10-12 06:14:15 +00:00
|
|
|
KMemoryState_Shared = ams::svc::MemoryState_Shared | KMemoryState_FlagMapped | KMemoryState_FlagReferenceCounted | KMemoryState_FlagLinearMapped,
|
2020-02-07 12:58:35 +00:00
|
|
|
|
|
|
|
/* KMemoryState_Alias was removed after 1.0.0. */
|
|
|
|
|
|
|
|
KMemoryState_AliasCode = ams::svc::MemoryState_AliasCode | KMemoryState_FlagsCode | KMemoryState_FlagCanMapProcess | KMemoryState_FlagCanCodeAlias,
|
2023-10-11 12:00:23 +00:00
|
|
|
KMemoryState_AliasCodeData = ams::svc::MemoryState_AliasCodeData | KMemoryState_FlagsData | KMemoryState_FlagCanMapProcess | KMemoryState_FlagCanCodeAlias | KMemoryState_FlagCanCodeMemory
|
|
|
|
| KMemoryState_FlagCanPermissionLock,
|
2020-02-07 12:58:35 +00:00
|
|
|
|
|
|
|
KMemoryState_Ipc = ams::svc::MemoryState_Ipc | KMemoryState_FlagsMisc | KMemoryState_FlagCanAlignedDeviceMap
|
|
|
|
| KMemoryState_FlagCanUseIpc | KMemoryState_FlagCanUseNonSecureIpc | KMemoryState_FlagCanUseNonDeviceIpc,
|
|
|
|
|
|
|
|
KMemoryState_Stack = ams::svc::MemoryState_Stack | KMemoryState_FlagsMisc | KMemoryState_FlagCanAlignedDeviceMap
|
|
|
|
| KMemoryState_FlagCanUseIpc | KMemoryState_FlagCanUseNonSecureIpc | KMemoryState_FlagCanUseNonDeviceIpc,
|
|
|
|
|
2023-10-11 12:00:23 +00:00
|
|
|
KMemoryState_ThreadLocal = ams::svc::MemoryState_ThreadLocal | KMemoryState_FlagLinearMapped,
|
2020-02-07 12:58:35 +00:00
|
|
|
|
|
|
|
KMemoryState_Transfered = ams::svc::MemoryState_Transfered | KMemoryState_FlagsMisc | KMemoryState_FlagCanAlignedDeviceMap | KMemoryState_FlagCanChangeAttribute
|
|
|
|
| KMemoryState_FlagCanUseIpc | KMemoryState_FlagCanUseNonSecureIpc | KMemoryState_FlagCanUseNonDeviceIpc,
|
|
|
|
|
|
|
|
KMemoryState_SharedTransfered = ams::svc::MemoryState_SharedTransfered | KMemoryState_FlagsMisc | KMemoryState_FlagCanAlignedDeviceMap
|
|
|
|
| KMemoryState_FlagCanUseNonSecureIpc | KMemoryState_FlagCanUseNonDeviceIpc,
|
|
|
|
|
2022-10-12 06:14:15 +00:00
|
|
|
KMemoryState_SharedCode = ams::svc::MemoryState_SharedCode | KMemoryState_FlagMapped | KMemoryState_FlagReferenceCounted | KMemoryState_FlagLinearMapped
|
2020-02-07 12:58:35 +00:00
|
|
|
| KMemoryState_FlagCanUseNonSecureIpc | KMemoryState_FlagCanUseNonDeviceIpc,
|
|
|
|
|
|
|
|
KMemoryState_Inaccessible = ams::svc::MemoryState_Inaccessible,
|
|
|
|
|
|
|
|
KMemoryState_NonSecureIpc = ams::svc::MemoryState_NonSecureIpc | KMemoryState_FlagsMisc | KMemoryState_FlagCanAlignedDeviceMap
|
|
|
|
| KMemoryState_FlagCanUseNonSecureIpc | KMemoryState_FlagCanUseNonDeviceIpc,
|
|
|
|
|
|
|
|
KMemoryState_NonDeviceIpc = ams::svc::MemoryState_NonDeviceIpc | KMemoryState_FlagsMisc | KMemoryState_FlagCanUseNonDeviceIpc,
|
|
|
|
|
|
|
|
|
2023-10-11 12:00:23 +00:00
|
|
|
KMemoryState_Kernel = ams::svc::MemoryState_Kernel,
|
2020-02-07 12:58:35 +00:00
|
|
|
|
2022-10-12 06:14:15 +00:00
|
|
|
KMemoryState_GeneratedCode = ams::svc::MemoryState_GeneratedCode | KMemoryState_FlagMapped | KMemoryState_FlagReferenceCounted | KMemoryState_FlagCanDebug | KMemoryState_FlagLinearMapped,
|
|
|
|
KMemoryState_CodeOut = ams::svc::MemoryState_CodeOut | KMemoryState_FlagMapped | KMemoryState_FlagReferenceCounted | KMemoryState_FlagLinearMapped,
|
2021-09-17 23:26:01 +00:00
|
|
|
|
|
|
|
KMemoryState_Coverage = ams::svc::MemoryState_Coverage | KMemoryState_FlagMapped,
|
2022-10-12 06:44:22 +00:00
|
|
|
|
2023-10-11 12:00:23 +00:00
|
|
|
KMemoryState_Insecure = ams::svc::MemoryState_Insecure | KMemoryState_FlagMapped | KMemoryState_FlagReferenceCounted | KMemoryState_FlagLinearMapped | KMemoryState_FlagCanChangeAttribute
|
|
|
|
| KMemoryState_FlagCanDeviceMap | KMemoryState_FlagCanAlignedDeviceMap | KMemoryState_FlagCanQueryPhysical
|
2022-10-12 06:44:22 +00:00
|
|
|
| KMemoryState_FlagCanUseNonSecureIpc | KMemoryState_FlagCanUseNonDeviceIpc,
|
2020-02-07 12:58:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#if 1
|
|
|
|
static_assert(KMemoryState_Free == 0x00000000);
|
2023-10-11 12:00:23 +00:00
|
|
|
static_assert(KMemoryState_IoMemory == 0x00182001);
|
|
|
|
static_assert(KMemoryState_IoRegister == 0x00180001);
|
|
|
|
static_assert(KMemoryState_Static == 0x00040002);
|
2022-10-12 06:14:15 +00:00
|
|
|
static_assert(KMemoryState_Code == 0x04DC7E03);
|
2023-10-11 12:00:23 +00:00
|
|
|
static_assert(KMemoryState_CodeData == 0x0FFEBD04);
|
2022-10-12 06:14:15 +00:00
|
|
|
static_assert(KMemoryState_Normal == 0x077EBD05);
|
|
|
|
static_assert(KMemoryState_Shared == 0x04402006);
|
|
|
|
|
|
|
|
static_assert(KMemoryState_AliasCode == 0x04DD7E08);
|
2023-10-11 12:00:23 +00:00
|
|
|
static_assert(KMemoryState_AliasCodeData == 0x0FFFBD09);
|
2022-10-12 06:14:15 +00:00
|
|
|
static_assert(KMemoryState_Ipc == 0x045C3C0A);
|
|
|
|
static_assert(KMemoryState_Stack == 0x045C3C0B);
|
2023-10-11 12:00:23 +00:00
|
|
|
static_assert(KMemoryState_ThreadLocal == 0x0400000C);
|
2022-10-12 06:14:15 +00:00
|
|
|
static_assert(KMemoryState_Transfered == 0x055C3C0D);
|
|
|
|
static_assert(KMemoryState_SharedTransfered == 0x045C380E);
|
|
|
|
static_assert(KMemoryState_SharedCode == 0x0440380F);
|
2020-02-07 12:58:35 +00:00
|
|
|
static_assert(KMemoryState_Inaccessible == 0x00000010);
|
2022-10-12 06:14:15 +00:00
|
|
|
static_assert(KMemoryState_NonSecureIpc == 0x045C3811);
|
|
|
|
static_assert(KMemoryState_NonDeviceIpc == 0x044C2812);
|
2023-10-11 12:00:23 +00:00
|
|
|
static_assert(KMemoryState_Kernel == 0x00000013);
|
2022-10-12 06:14:15 +00:00
|
|
|
static_assert(KMemoryState_GeneratedCode == 0x04402214);
|
|
|
|
static_assert(KMemoryState_CodeOut == 0x04402015);
|
2023-10-11 12:00:23 +00:00
|
|
|
static_assert(KMemoryState_Coverage == 0x00002016); /* TODO: Is this correct? */
|
|
|
|
static_assert(KMemoryState_Insecure == 0x055C3817);
|
2020-02-07 12:58:35 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
enum KMemoryPermission : u8 {
|
|
|
|
KMemoryPermission_None = 0,
|
2020-02-14 01:38:56 +00:00
|
|
|
KMemoryPermission_All = static_cast<u8>(~KMemoryPermission_None),
|
2020-02-07 12:58:35 +00:00
|
|
|
|
|
|
|
KMemoryPermission_KernelShift = 3,
|
|
|
|
|
2020-02-14 01:38:56 +00:00
|
|
|
KMemoryPermission_KernelRead = ams::svc::MemoryPermission_Read << KMemoryPermission_KernelShift,
|
|
|
|
KMemoryPermission_KernelWrite = ams::svc::MemoryPermission_Write << KMemoryPermission_KernelShift,
|
|
|
|
KMemoryPermission_KernelExecute = ams::svc::MemoryPermission_Execute << KMemoryPermission_KernelShift,
|
2020-02-07 12:58:35 +00:00
|
|
|
|
2020-04-20 00:16:19 +00:00
|
|
|
KMemoryPermission_NotMapped = (1 << (2 * KMemoryPermission_KernelShift)),
|
|
|
|
|
2020-02-07 12:58:35 +00:00
|
|
|
KMemoryPermission_KernelReadWrite = KMemoryPermission_KernelRead | KMemoryPermission_KernelWrite,
|
|
|
|
KMemoryPermission_KernelReadExecute = KMemoryPermission_KernelRead | KMemoryPermission_KernelExecute,
|
2020-02-14 01:38:56 +00:00
|
|
|
|
|
|
|
KMemoryPermission_UserRead = ams::svc::MemoryPermission_Read | KMemoryPermission_KernelRead,
|
|
|
|
KMemoryPermission_UserWrite = ams::svc::MemoryPermission_Write | KMemoryPermission_KernelWrite,
|
|
|
|
KMemoryPermission_UserExecute = ams::svc::MemoryPermission_Execute,
|
|
|
|
|
|
|
|
KMemoryPermission_UserReadWrite = KMemoryPermission_UserRead | KMemoryPermission_UserWrite,
|
|
|
|
KMemoryPermission_UserReadExecute = KMemoryPermission_UserRead | KMemoryPermission_UserExecute,
|
|
|
|
|
|
|
|
KMemoryPermission_UserMask = ams::svc::MemoryPermission_Read | ams::svc::MemoryPermission_Write | ams::svc::MemoryPermission_Execute,
|
2020-07-12 22:42:47 +00:00
|
|
|
|
|
|
|
KMemoryPermission_IpcLockChangeMask = KMemoryPermission_NotMapped | KMemoryPermission_UserReadWrite,
|
2020-02-07 12:58:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
constexpr KMemoryPermission ConvertToKMemoryPermission(ams::svc::MemoryPermission perm) {
|
2024-03-28 07:25:48 +00:00
|
|
|
return static_cast<KMemoryPermission>((util::ToUnderlying(perm) & KMemoryPermission_UserMask) | KMemoryPermission_KernelRead | ((util::ToUnderlying(perm) & ams::svc::MemoryPermission_Write) ? KMemoryPermission_KernelWrite : KMemoryPermission_None) | (perm == ams::svc::MemoryPermission_None ? KMemoryPermission_NotMapped : KMemoryPermission_None));
|
2020-02-07 12:58:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
enum KMemoryAttribute : u8 {
|
2023-10-11 12:00:23 +00:00
|
|
|
KMemoryAttribute_None = 0x00,
|
|
|
|
KMemoryAttribute_All = 0xFF,
|
|
|
|
KMemoryAttribute_UserMask = KMemoryAttribute_All,
|
2020-02-07 12:58:35 +00:00
|
|
|
|
2023-10-11 12:00:23 +00:00
|
|
|
KMemoryAttribute_Locked = ams::svc::MemoryAttribute_Locked,
|
|
|
|
KMemoryAttribute_IpcLocked = ams::svc::MemoryAttribute_IpcLocked,
|
|
|
|
KMemoryAttribute_DeviceShared = ams::svc::MemoryAttribute_DeviceShared,
|
|
|
|
KMemoryAttribute_Uncached = ams::svc::MemoryAttribute_Uncached,
|
|
|
|
KMemoryAttribute_PermissionLocked = ams::svc::MemoryAttribute_PermissionLocked,
|
2020-05-29 07:57:25 +00:00
|
|
|
|
2023-10-11 14:59:37 +00:00
|
|
|
KMemoryAttribute_SetMask = KMemoryAttribute_Uncached | KMemoryAttribute_PermissionLocked,
|
2020-02-07 12:58:35 +00:00
|
|
|
};
|
|
|
|
|
2020-12-01 12:57:09 +00:00
|
|
|
enum KMemoryBlockDisableMergeAttribute : u8 {
|
|
|
|
KMemoryBlockDisableMergeAttribute_None = 0,
|
|
|
|
KMemoryBlockDisableMergeAttribute_Normal = (1u << 0),
|
|
|
|
KMemoryBlockDisableMergeAttribute_DeviceLeft = (1u << 1),
|
|
|
|
KMemoryBlockDisableMergeAttribute_IpcLeft = (1u << 2),
|
|
|
|
KMemoryBlockDisableMergeAttribute_Locked = (1u << 3),
|
|
|
|
KMemoryBlockDisableMergeAttribute_DeviceRight = (1u << 4),
|
|
|
|
|
|
|
|
KMemoryBlockDisableMergeAttribute_AllLeft = KMemoryBlockDisableMergeAttribute_Normal | KMemoryBlockDisableMergeAttribute_DeviceLeft | KMemoryBlockDisableMergeAttribute_IpcLeft | KMemoryBlockDisableMergeAttribute_Locked,
|
|
|
|
KMemoryBlockDisableMergeAttribute_AllRight = KMemoryBlockDisableMergeAttribute_DeviceRight,
|
|
|
|
};
|
|
|
|
|
2020-02-10 17:57:00 +00:00
|
|
|
struct KMemoryInfo {
|
2020-12-18 01:18:47 +00:00
|
|
|
uintptr_t m_address;
|
|
|
|
size_t m_size;
|
|
|
|
KMemoryState m_state;
|
|
|
|
u16 m_device_disable_merge_left_count;
|
|
|
|
u16 m_device_disable_merge_right_count;
|
|
|
|
u16 m_ipc_lock_count;
|
|
|
|
u16 m_device_use_count;
|
|
|
|
u16 m_ipc_disable_merge_count;
|
2021-10-05 22:16:54 +00:00
|
|
|
KMemoryPermission m_permission;
|
2020-12-18 01:18:47 +00:00
|
|
|
KMemoryAttribute m_attribute;
|
2021-10-05 22:16:54 +00:00
|
|
|
KMemoryPermission m_original_permission;
|
2020-12-18 01:18:47 +00:00
|
|
|
KMemoryBlockDisableMergeAttribute m_disable_merge_attribute;
|
2020-02-10 17:57:00 +00:00
|
|
|
|
|
|
|
constexpr ams::svc::MemoryInfo GetSvcMemoryInfo() const {
|
2020-02-14 01:38:56 +00:00
|
|
|
return {
|
2021-10-05 22:16:54 +00:00
|
|
|
.base_address = m_address,
|
|
|
|
.size = m_size,
|
|
|
|
.state = static_cast<ams::svc::MemoryState>(m_state & KMemoryState_Mask),
|
|
|
|
.attribute = static_cast<ams::svc::MemoryAttribute>(m_attribute & KMemoryAttribute_UserMask),
|
|
|
|
.permission = static_cast<ams::svc::MemoryPermission>(m_permission & KMemoryPermission_UserMask),
|
|
|
|
.ipc_count = m_ipc_lock_count,
|
|
|
|
.device_count = m_device_use_count,
|
|
|
|
.padding = {},
|
2020-02-14 01:38:56 +00:00
|
|
|
};
|
2020-02-10 17:57:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
constexpr uintptr_t GetAddress() const {
|
2020-12-18 01:18:47 +00:00
|
|
|
return m_address;
|
2020-02-10 17:57:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
constexpr size_t GetSize() const {
|
2020-12-18 01:18:47 +00:00
|
|
|
return m_size;
|
2020-02-10 17:57:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
constexpr size_t GetNumPages() const {
|
|
|
|
return this->GetSize() / PageSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr uintptr_t GetEndAddress() const {
|
|
|
|
return this->GetAddress() + this->GetSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr uintptr_t GetLastAddress() const {
|
|
|
|
return this->GetEndAddress() - 1;
|
|
|
|
}
|
2020-07-12 22:42:47 +00:00
|
|
|
|
|
|
|
constexpr u16 GetIpcLockCount() const {
|
2020-12-18 01:18:47 +00:00
|
|
|
return m_ipc_lock_count;
|
2020-07-12 22:42:47 +00:00
|
|
|
}
|
|
|
|
|
2020-12-01 12:57:09 +00:00
|
|
|
constexpr u16 GetIpcDisableMergeCount() const {
|
2020-12-18 01:18:47 +00:00
|
|
|
return m_ipc_disable_merge_count;
|
2020-12-01 12:57:09 +00:00
|
|
|
}
|
|
|
|
|
2020-07-24 15:07:34 +00:00
|
|
|
constexpr KMemoryState GetState() const {
|
2020-12-18 01:18:47 +00:00
|
|
|
return m_state;
|
2020-07-24 15:07:34 +00:00
|
|
|
}
|
|
|
|
|
2023-10-11 12:00:23 +00:00
|
|
|
constexpr ams::svc::MemoryState GetSvcState() const {
|
|
|
|
return static_cast<ams::svc::MemoryState>(m_state & KMemoryState_Mask);
|
|
|
|
}
|
|
|
|
|
2020-07-12 22:42:47 +00:00
|
|
|
constexpr KMemoryPermission GetPermission() const {
|
2021-10-05 22:16:54 +00:00
|
|
|
return m_permission;
|
2020-07-12 22:42:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
constexpr KMemoryPermission GetOriginalPermission() const {
|
2021-10-05 22:16:54 +00:00
|
|
|
return m_original_permission;
|
2020-07-12 22:42:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
constexpr KMemoryAttribute GetAttribute() const {
|
2020-12-18 01:18:47 +00:00
|
|
|
return m_attribute;
|
2020-07-12 22:42:47 +00:00
|
|
|
}
|
2020-12-01 12:57:09 +00:00
|
|
|
|
|
|
|
constexpr KMemoryBlockDisableMergeAttribute GetDisableMergeAttribute() const {
|
2020-12-18 01:18:47 +00:00
|
|
|
return m_disable_merge_attribute;
|
2020-12-01 12:57:09 +00:00
|
|
|
}
|
2020-02-10 17:57:00 +00:00
|
|
|
};
|
|
|
|
|
2020-02-07 12:58:35 +00:00
|
|
|
class KMemoryBlock : public util::IntrusiveRedBlackTreeBaseNode<KMemoryBlock> {
|
|
|
|
private:
|
2020-12-18 01:18:47 +00:00
|
|
|
u16 m_device_disable_merge_left_count;
|
|
|
|
u16 m_device_disable_merge_right_count;
|
|
|
|
KProcessAddress m_address;
|
|
|
|
size_t m_num_pages;
|
|
|
|
KMemoryState m_memory_state;
|
|
|
|
u16 m_ipc_lock_count;
|
|
|
|
u16 m_device_use_count;
|
|
|
|
u16 m_ipc_disable_merge_count;
|
2021-10-05 22:16:54 +00:00
|
|
|
KMemoryPermission m_permission;
|
|
|
|
KMemoryPermission m_original_permission;
|
2020-12-18 01:18:47 +00:00
|
|
|
KMemoryAttribute m_attribute;
|
|
|
|
KMemoryBlockDisableMergeAttribute m_disable_merge_attribute;
|
2020-02-10 09:50:23 +00:00
|
|
|
public:
|
|
|
|
static constexpr ALWAYS_INLINE int Compare(const KMemoryBlock &lhs, const KMemoryBlock &rhs) {
|
|
|
|
if (lhs.GetAddress() < rhs.GetAddress()) {
|
|
|
|
return -1;
|
|
|
|
} else if (lhs.GetAddress() <= rhs.GetLastAddress()) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public:
|
|
|
|
constexpr KProcessAddress GetAddress() const {
|
2020-12-18 01:18:47 +00:00
|
|
|
return m_address;
|
2020-02-10 09:50:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
constexpr size_t GetNumPages() const {
|
2020-12-18 01:18:47 +00:00
|
|
|
return m_num_pages;
|
2020-02-10 09:50:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
constexpr size_t GetSize() const {
|
|
|
|
return this->GetNumPages() * PageSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr KProcessAddress GetEndAddress() const {
|
|
|
|
return this->GetAddress() + this->GetSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr KProcessAddress GetLastAddress() const {
|
|
|
|
return this->GetEndAddress() - 1;
|
|
|
|
}
|
2020-02-10 17:57:00 +00:00
|
|
|
|
2023-10-11 14:59:37 +00:00
|
|
|
constexpr KMemoryState GetState() const {
|
|
|
|
return m_memory_state;
|
|
|
|
}
|
|
|
|
|
2020-07-12 22:42:47 +00:00
|
|
|
constexpr u16 GetIpcLockCount() const {
|
2020-12-18 01:18:47 +00:00
|
|
|
return m_ipc_lock_count;
|
2020-07-12 22:42:47 +00:00
|
|
|
}
|
|
|
|
|
2020-12-01 12:57:09 +00:00
|
|
|
constexpr u16 GetIpcDisableMergeCount() const {
|
2020-12-18 01:18:47 +00:00
|
|
|
return m_ipc_disable_merge_count;
|
2020-12-01 12:57:09 +00:00
|
|
|
}
|
|
|
|
|
2020-07-12 22:42:47 +00:00
|
|
|
constexpr KMemoryPermission GetPermission() const {
|
2021-10-05 22:16:54 +00:00
|
|
|
return m_permission;
|
2020-07-12 22:42:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
constexpr KMemoryPermission GetOriginalPermission() const {
|
2021-10-05 22:16:54 +00:00
|
|
|
return m_original_permission;
|
2020-07-12 22:42:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
constexpr KMemoryAttribute GetAttribute() const {
|
2020-12-18 01:18:47 +00:00
|
|
|
return m_attribute;
|
2020-07-12 22:42:47 +00:00
|
|
|
}
|
|
|
|
|
2020-02-10 17:57:00 +00:00
|
|
|
constexpr KMemoryInfo GetMemoryInfo() const {
|
2020-02-14 01:38:56 +00:00
|
|
|
return {
|
2020-12-18 01:18:47 +00:00
|
|
|
.m_address = GetInteger(this->GetAddress()),
|
|
|
|
.m_size = this->GetSize(),
|
|
|
|
.m_state = m_memory_state,
|
|
|
|
.m_device_disable_merge_left_count = m_device_disable_merge_left_count,
|
|
|
|
.m_device_disable_merge_right_count = m_device_disable_merge_right_count,
|
|
|
|
.m_ipc_lock_count = m_ipc_lock_count,
|
|
|
|
.m_device_use_count = m_device_use_count,
|
|
|
|
.m_ipc_disable_merge_count = m_ipc_disable_merge_count,
|
2021-10-05 22:16:54 +00:00
|
|
|
.m_permission = m_permission,
|
2020-12-18 01:18:47 +00:00
|
|
|
.m_attribute = m_attribute,
|
2021-10-05 22:16:54 +00:00
|
|
|
.m_original_permission = m_original_permission,
|
2020-12-18 01:18:47 +00:00
|
|
|
.m_disable_merge_attribute = m_disable_merge_attribute,
|
2020-02-14 01:38:56 +00:00
|
|
|
};
|
2020-02-10 17:57:00 +00:00
|
|
|
}
|
2020-02-07 12:58:35 +00:00
|
|
|
public:
|
2021-10-23 22:25:20 +00:00
|
|
|
explicit KMemoryBlock() { /* ... */ }
|
2020-02-10 09:50:23 +00:00
|
|
|
|
2021-10-23 22:25:20 +00:00
|
|
|
constexpr KMemoryBlock(util::ConstantInitializeTag, KProcessAddress addr, size_t np, KMemoryState ms, KMemoryPermission p, KMemoryAttribute attr)
|
|
|
|
: util::IntrusiveRedBlackTreeBaseNode<KMemoryBlock>(util::ConstantInitialize), m_device_disable_merge_left_count(),
|
|
|
|
m_device_disable_merge_right_count(), m_address(addr), m_num_pages(np), m_memory_state(ms), m_ipc_lock_count(0),
|
|
|
|
m_device_use_count(0), m_ipc_disable_merge_count(), m_permission(p), m_original_permission(KMemoryPermission_None),
|
|
|
|
m_attribute(attr), m_disable_merge_attribute()
|
2020-02-10 17:57:00 +00:00
|
|
|
{
|
|
|
|
/* ... */
|
|
|
|
}
|
|
|
|
|
2020-02-10 09:50:23 +00:00
|
|
|
constexpr void Initialize(KProcessAddress addr, size_t np, KMemoryState ms, KMemoryPermission p, KMemoryAttribute attr) {
|
|
|
|
MESOSPHERE_ASSERT_THIS();
|
2021-10-23 22:25:20 +00:00
|
|
|
m_device_disable_merge_left_count = 0;
|
|
|
|
m_device_disable_merge_right_count = 0;
|
|
|
|
m_address = addr;
|
|
|
|
m_num_pages = np;
|
|
|
|
m_memory_state = ms;
|
|
|
|
m_ipc_lock_count = 0;
|
|
|
|
m_device_use_count = 0;
|
|
|
|
m_permission = p;
|
|
|
|
m_original_permission = KMemoryPermission_None;
|
|
|
|
m_attribute = attr;
|
|
|
|
m_disable_merge_attribute = KMemoryBlockDisableMergeAttribute_None;
|
2020-02-10 09:50:23 +00:00
|
|
|
}
|
2020-02-10 17:57:00 +00:00
|
|
|
|
|
|
|
constexpr bool HasProperties(KMemoryState s, KMemoryPermission p, KMemoryAttribute a) const {
|
|
|
|
MESOSPHERE_ASSERT_THIS();
|
2020-04-19 00:10:26 +00:00
|
|
|
constexpr auto AttributeIgnoreMask = KMemoryAttribute_IpcLocked | KMemoryAttribute_DeviceShared;
|
2021-10-05 22:16:54 +00:00
|
|
|
return m_memory_state == s && m_permission == p && (m_attribute | AttributeIgnoreMask) == (a | AttributeIgnoreMask);
|
2020-02-10 17:57:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
constexpr bool HasSameProperties(const KMemoryBlock &rhs) const {
|
|
|
|
MESOSPHERE_ASSERT_THIS();
|
|
|
|
|
2021-10-05 22:16:54 +00:00
|
|
|
return m_memory_state == rhs.m_memory_state &&
|
|
|
|
m_permission == rhs.m_permission &&
|
|
|
|
m_original_permission == rhs.m_original_permission &&
|
|
|
|
m_attribute == rhs.m_attribute &&
|
|
|
|
m_ipc_lock_count == rhs.m_ipc_lock_count &&
|
|
|
|
m_device_use_count == rhs.m_device_use_count;
|
2020-02-10 17:57:00 +00:00
|
|
|
}
|
|
|
|
|
2020-12-01 12:57:09 +00:00
|
|
|
constexpr bool CanMergeWith(const KMemoryBlock &rhs) const {
|
|
|
|
return this->HasSameProperties(rhs) &&
|
2020-12-18 01:18:47 +00:00
|
|
|
(m_disable_merge_attribute & KMemoryBlockDisableMergeAttribute_AllRight) == 0 &&
|
|
|
|
(rhs.m_disable_merge_attribute & KMemoryBlockDisableMergeAttribute_AllLeft) == 0;
|
2020-12-01 12:57:09 +00:00
|
|
|
}
|
|
|
|
|
2020-02-10 17:57:00 +00:00
|
|
|
constexpr bool Contains(KProcessAddress addr) const {
|
|
|
|
MESOSPHERE_ASSERT_THIS();
|
|
|
|
|
|
|
|
return this->GetAddress() <= addr && addr <= this->GetEndAddress();
|
|
|
|
}
|
|
|
|
|
2020-12-01 12:57:09 +00:00
|
|
|
constexpr void Add(const KMemoryBlock &added_block) {
|
2020-02-10 17:57:00 +00:00
|
|
|
MESOSPHERE_ASSERT_THIS();
|
2020-12-01 12:57:09 +00:00
|
|
|
MESOSPHERE_ASSERT(added_block.GetNumPages() > 0);
|
|
|
|
MESOSPHERE_ASSERT(this->GetAddress() + added_block.GetSize() - 1 < this->GetEndAddress() + added_block.GetSize() - 1);
|
2020-02-10 17:57:00 +00:00
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
m_num_pages += added_block.GetNumPages();
|
|
|
|
m_disable_merge_attribute = static_cast<KMemoryBlockDisableMergeAttribute>(m_disable_merge_attribute | added_block.m_disable_merge_attribute);
|
|
|
|
m_device_disable_merge_right_count = added_block.m_device_disable_merge_right_count;
|
2020-02-10 17:57:00 +00:00
|
|
|
}
|
|
|
|
|
2020-12-01 12:57:09 +00:00
|
|
|
constexpr void Update(KMemoryState s, KMemoryPermission p, KMemoryAttribute a, bool set_disable_merge_attr, u8 set_mask, u8 clear_mask) {
|
2020-02-10 17:57:00 +00:00
|
|
|
MESOSPHERE_ASSERT_THIS();
|
2021-10-05 22:16:54 +00:00
|
|
|
MESOSPHERE_ASSERT(m_original_permission == KMemoryPermission_None);
|
2020-12-18 01:18:47 +00:00
|
|
|
MESOSPHERE_ASSERT((m_attribute & KMemoryAttribute_IpcLocked) == 0);
|
2020-02-10 17:57:00 +00:00
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
m_memory_state = s;
|
2021-10-05 22:16:54 +00:00
|
|
|
m_permission = p;
|
2020-12-18 01:18:47 +00:00
|
|
|
m_attribute = static_cast<KMemoryAttribute>(a | (m_attribute & (KMemoryAttribute_IpcLocked | KMemoryAttribute_DeviceShared)));
|
2020-12-01 12:57:09 +00:00
|
|
|
|
|
|
|
if (set_disable_merge_attr && set_mask != 0) {
|
2020-12-18 01:18:47 +00:00
|
|
|
m_disable_merge_attribute = static_cast<KMemoryBlockDisableMergeAttribute>(m_disable_merge_attribute | set_mask);
|
2020-12-01 12:57:09 +00:00
|
|
|
}
|
|
|
|
if (clear_mask != 0) {
|
2020-12-18 01:18:47 +00:00
|
|
|
m_disable_merge_attribute = static_cast<KMemoryBlockDisableMergeAttribute>(m_disable_merge_attribute & ~clear_mask);
|
2020-12-01 12:57:09 +00:00
|
|
|
}
|
2020-02-10 17:57:00 +00:00
|
|
|
}
|
|
|
|
|
2023-10-11 14:59:37 +00:00
|
|
|
constexpr void UpdateAttribute(u32 mask, u32 attr) {
|
|
|
|
MESOSPHERE_ASSERT_THIS();
|
|
|
|
MESOSPHERE_ASSERT((mask & KMemoryAttribute_IpcLocked) == 0);
|
|
|
|
MESOSPHERE_ASSERT((mask & KMemoryAttribute_DeviceShared) == 0);
|
|
|
|
|
|
|
|
m_attribute = static_cast<KMemoryAttribute>((m_attribute & ~mask) | attr);
|
|
|
|
}
|
|
|
|
|
2020-02-10 17:57:00 +00:00
|
|
|
constexpr void Split(KMemoryBlock *block, KProcessAddress addr) {
|
|
|
|
MESOSPHERE_ASSERT_THIS();
|
|
|
|
MESOSPHERE_ASSERT(this->GetAddress() < addr);
|
|
|
|
MESOSPHERE_ASSERT(this->Contains(addr));
|
|
|
|
MESOSPHERE_ASSERT(util::IsAligned(GetInteger(addr), PageSize));
|
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
block->m_address = m_address;
|
|
|
|
block->m_num_pages = (addr - this->GetAddress()) / PageSize;
|
|
|
|
block->m_memory_state = m_memory_state;
|
|
|
|
block->m_ipc_lock_count = m_ipc_lock_count;
|
|
|
|
block->m_device_use_count = m_device_use_count;
|
2021-10-05 22:16:54 +00:00
|
|
|
block->m_permission = m_permission;
|
|
|
|
block->m_original_permission = m_original_permission;
|
2020-12-18 01:18:47 +00:00
|
|
|
block->m_attribute = m_attribute;
|
|
|
|
block->m_disable_merge_attribute = static_cast<KMemoryBlockDisableMergeAttribute>(m_disable_merge_attribute & KMemoryBlockDisableMergeAttribute_AllLeft);
|
|
|
|
block->m_ipc_disable_merge_count = m_ipc_disable_merge_count;
|
|
|
|
block->m_device_disable_merge_left_count = m_device_disable_merge_left_count;
|
|
|
|
block->m_device_disable_merge_right_count = 0;
|
2020-02-10 17:57:00 +00:00
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
m_address = addr;
|
|
|
|
m_num_pages -= block->m_num_pages;
|
2020-12-01 12:57:09 +00:00
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
m_ipc_disable_merge_count = 0;
|
|
|
|
m_device_disable_merge_left_count = 0;
|
|
|
|
m_disable_merge_attribute = static_cast<KMemoryBlockDisableMergeAttribute>(m_disable_merge_attribute & KMemoryBlockDisableMergeAttribute_AllRight);
|
2020-12-01 12:57:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
constexpr void UpdateDeviceDisableMergeStateForShareLeft(KMemoryPermission new_perm, bool left, bool right) {
|
|
|
|
/* New permission/right aren't used. */
|
|
|
|
MESOSPHERE_UNUSED(new_perm, right);
|
|
|
|
|
|
|
|
if (left) {
|
2020-12-18 01:18:47 +00:00
|
|
|
m_disable_merge_attribute = static_cast<KMemoryBlockDisableMergeAttribute>(m_disable_merge_attribute | KMemoryBlockDisableMergeAttribute_DeviceLeft);
|
|
|
|
const u16 new_device_disable_merge_left_count = ++m_device_disable_merge_left_count;
|
2020-12-01 14:55:50 +00:00
|
|
|
MESOSPHERE_ABORT_UNLESS(new_device_disable_merge_left_count > 0);
|
2020-12-01 12:57:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr void UpdateDeviceDisableMergeStateForShareRight(KMemoryPermission new_perm, bool left, bool right) {
|
|
|
|
/* New permission/left aren't used. */
|
|
|
|
MESOSPHERE_UNUSED(new_perm, left);
|
|
|
|
|
|
|
|
if (right) {
|
2020-12-18 01:18:47 +00:00
|
|
|
m_disable_merge_attribute = static_cast<KMemoryBlockDisableMergeAttribute>(m_disable_merge_attribute | KMemoryBlockDisableMergeAttribute_DeviceRight);
|
|
|
|
const u16 new_device_disable_merge_right_count = ++m_device_disable_merge_right_count;
|
2020-12-01 12:57:09 +00:00
|
|
|
MESOSPHERE_ABORT_UNLESS(new_device_disable_merge_right_count > 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr void UpdateDeviceDisableMergeStateForShare(KMemoryPermission new_perm, bool left, bool right) {
|
|
|
|
this->UpdateDeviceDisableMergeStateForShareLeft(new_perm, left, right);
|
|
|
|
this->UpdateDeviceDisableMergeStateForShareRight(new_perm, left, right);
|
2020-02-10 17:57:00 +00:00
|
|
|
}
|
2020-07-12 22:42:47 +00:00
|
|
|
|
2020-12-01 12:57:09 +00:00
|
|
|
constexpr void ShareToDevice(KMemoryPermission new_perm, bool left, bool right) {
|
2020-08-21 00:29:10 +00:00
|
|
|
/* New permission isn't used. */
|
|
|
|
MESOSPHERE_UNUSED(new_perm);
|
|
|
|
|
2020-07-15 01:46:25 +00:00
|
|
|
/* We must either be shared or have a zero lock count. */
|
2020-12-18 01:18:47 +00:00
|
|
|
MESOSPHERE_ASSERT((m_attribute & KMemoryAttribute_DeviceShared) == KMemoryAttribute_DeviceShared || m_device_use_count == 0);
|
2020-07-15 01:46:25 +00:00
|
|
|
|
|
|
|
/* Share. */
|
2020-12-18 01:18:47 +00:00
|
|
|
const u16 new_count = ++m_device_use_count;
|
2020-07-15 01:46:25 +00:00
|
|
|
MESOSPHERE_ABORT_UNLESS(new_count > 0);
|
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
m_attribute = static_cast<KMemoryAttribute>(m_attribute | KMemoryAttribute_DeviceShared);
|
2020-12-01 12:57:09 +00:00
|
|
|
|
|
|
|
this->UpdateDeviceDisableMergeStateForShare(new_perm, left, right);
|
2020-07-15 01:46:25 +00:00
|
|
|
}
|
|
|
|
|
2020-12-01 12:57:09 +00:00
|
|
|
constexpr void UpdateDeviceDisableMergeStateForUnshareLeft(KMemoryPermission new_perm, bool left, bool right) {
|
|
|
|
/* New permission/right aren't used. */
|
|
|
|
MESOSPHERE_UNUSED(new_perm, right);
|
|
|
|
|
|
|
|
if (left) {
|
2020-12-18 01:18:47 +00:00
|
|
|
if (!m_device_disable_merge_left_count) {
|
2020-12-01 12:57:09 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-12-18 01:18:47 +00:00
|
|
|
--m_device_disable_merge_left_count;
|
2020-12-01 12:57:09 +00:00
|
|
|
}
|
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
m_device_disable_merge_left_count = std::min(m_device_disable_merge_left_count, m_device_use_count);
|
2020-12-01 12:57:09 +00:00
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
if (m_device_disable_merge_left_count == 0) {
|
|
|
|
m_disable_merge_attribute = static_cast<KMemoryBlockDisableMergeAttribute>(m_disable_merge_attribute & ~KMemoryBlockDisableMergeAttribute_DeviceLeft);
|
2020-12-01 12:57:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr void UpdateDeviceDisableMergeStateForUnshareRight(KMemoryPermission new_perm, bool left, bool right) {
|
|
|
|
/* New permission/left aren't used. */
|
|
|
|
MESOSPHERE_UNUSED(new_perm, left);
|
|
|
|
|
|
|
|
if (right) {
|
2020-12-18 01:18:47 +00:00
|
|
|
const u16 old_device_disable_merge_right_count = m_device_disable_merge_right_count--;
|
2020-12-01 12:57:09 +00:00
|
|
|
MESOSPHERE_ASSERT(old_device_disable_merge_right_count > 0);
|
|
|
|
if (old_device_disable_merge_right_count == 1) {
|
2020-12-18 01:18:47 +00:00
|
|
|
m_disable_merge_attribute = static_cast<KMemoryBlockDisableMergeAttribute>(m_disable_merge_attribute & ~KMemoryBlockDisableMergeAttribute_DeviceRight);
|
2020-12-01 12:57:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr void UpdateDeviceDisableMergeStateForUnshare(KMemoryPermission new_perm, bool left, bool right) {
|
|
|
|
this->UpdateDeviceDisableMergeStateForUnshareLeft(new_perm, left, right);
|
|
|
|
this->UpdateDeviceDisableMergeStateForUnshareRight(new_perm, left, right);
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr void UnshareToDevice(KMemoryPermission new_perm, bool left, bool right) {
|
|
|
|
/* New permission isn't used. */
|
|
|
|
MESOSPHERE_UNUSED(new_perm);
|
|
|
|
|
|
|
|
/* We must be shared. */
|
2020-12-18 01:18:47 +00:00
|
|
|
MESOSPHERE_ASSERT((m_attribute & KMemoryAttribute_DeviceShared) == KMemoryAttribute_DeviceShared);
|
2020-12-01 12:57:09 +00:00
|
|
|
|
|
|
|
/* Unhare. */
|
2020-12-18 01:18:47 +00:00
|
|
|
const u16 old_count = m_device_use_count--;
|
2020-12-01 12:57:09 +00:00
|
|
|
MESOSPHERE_ABORT_UNLESS(old_count > 0);
|
|
|
|
|
|
|
|
if (old_count == 1) {
|
2020-12-18 01:18:47 +00:00
|
|
|
m_attribute = static_cast<KMemoryAttribute>(m_attribute & ~KMemoryAttribute_DeviceShared);
|
2020-12-01 12:57:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this->UpdateDeviceDisableMergeStateForUnshare(new_perm, left, right);
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr void UnshareToDeviceRight(KMemoryPermission new_perm, bool left, bool right) {
|
2020-08-21 00:29:10 +00:00
|
|
|
/* New permission isn't used. */
|
|
|
|
MESOSPHERE_UNUSED(new_perm);
|
|
|
|
|
2020-07-15 01:46:25 +00:00
|
|
|
/* We must be shared. */
|
2020-12-18 01:18:47 +00:00
|
|
|
MESOSPHERE_ASSERT((m_attribute & KMemoryAttribute_DeviceShared) == KMemoryAttribute_DeviceShared);
|
2020-07-15 01:46:25 +00:00
|
|
|
|
|
|
|
/* Unhare. */
|
2020-12-18 01:18:47 +00:00
|
|
|
const u16 old_count = m_device_use_count--;
|
2020-07-15 01:46:25 +00:00
|
|
|
MESOSPHERE_ABORT_UNLESS(old_count > 0);
|
|
|
|
|
|
|
|
if (old_count == 1) {
|
2020-12-18 01:18:47 +00:00
|
|
|
m_attribute = static_cast<KMemoryAttribute>(m_attribute & ~KMemoryAttribute_DeviceShared);
|
2020-07-15 01:46:25 +00:00
|
|
|
}
|
2020-12-01 12:57:09 +00:00
|
|
|
|
|
|
|
this->UpdateDeviceDisableMergeStateForUnshareRight(new_perm, left, right);
|
2020-07-15 01:46:25 +00:00
|
|
|
}
|
|
|
|
|
2020-12-01 12:57:09 +00:00
|
|
|
constexpr void LockForIpc(KMemoryPermission new_perm, bool left, bool right) {
|
2020-07-12 22:42:47 +00:00
|
|
|
/* We must either be locked or have a zero lock count. */
|
2020-12-18 01:18:47 +00:00
|
|
|
MESOSPHERE_ASSERT((m_attribute & KMemoryAttribute_IpcLocked) == KMemoryAttribute_IpcLocked || m_ipc_lock_count == 0);
|
2020-07-12 22:42:47 +00:00
|
|
|
|
|
|
|
/* Lock. */
|
2020-12-18 01:18:47 +00:00
|
|
|
const u16 new_lock_count = ++m_ipc_lock_count;
|
2020-07-12 22:42:47 +00:00
|
|
|
MESOSPHERE_ABORT_UNLESS(new_lock_count > 0);
|
|
|
|
|
|
|
|
/* If this is our first lock, update our permissions. */
|
|
|
|
if (new_lock_count == 1) {
|
2021-10-05 22:16:54 +00:00
|
|
|
MESOSPHERE_ASSERT(m_original_permission == KMemoryPermission_None);
|
|
|
|
MESOSPHERE_ASSERT((m_permission | new_perm | KMemoryPermission_NotMapped) == (m_permission | KMemoryPermission_NotMapped));
|
|
|
|
MESOSPHERE_ASSERT((m_permission & KMemoryPermission_UserExecute) != KMemoryPermission_UserExecute || (new_perm == KMemoryPermission_UserRead));
|
|
|
|
m_original_permission = m_permission;
|
|
|
|
m_permission = static_cast<KMemoryPermission>((new_perm & KMemoryPermission_IpcLockChangeMask) | (m_original_permission & ~KMemoryPermission_IpcLockChangeMask));
|
2020-07-12 22:42:47 +00:00
|
|
|
}
|
2020-12-18 01:18:47 +00:00
|
|
|
m_attribute = static_cast<KMemoryAttribute>(m_attribute | KMemoryAttribute_IpcLocked);
|
2020-12-01 12:57:09 +00:00
|
|
|
|
|
|
|
if (left) {
|
2020-12-18 01:18:47 +00:00
|
|
|
m_disable_merge_attribute = static_cast<KMemoryBlockDisableMergeAttribute>(m_disable_merge_attribute | KMemoryBlockDisableMergeAttribute_IpcLeft);
|
|
|
|
const u16 new_ipc_disable_merge_count = ++m_ipc_disable_merge_count;
|
2020-12-01 12:57:09 +00:00
|
|
|
MESOSPHERE_ABORT_UNLESS(new_ipc_disable_merge_count > 0);
|
|
|
|
}
|
|
|
|
MESOSPHERE_UNUSED(right);
|
2020-07-12 22:42:47 +00:00
|
|
|
}
|
|
|
|
|
2020-12-01 12:57:09 +00:00
|
|
|
constexpr void UnlockForIpc(KMemoryPermission new_perm, bool left, bool right) {
|
2020-08-21 00:29:10 +00:00
|
|
|
/* New permission isn't used. */
|
|
|
|
MESOSPHERE_UNUSED(new_perm);
|
|
|
|
|
2020-07-12 22:42:47 +00:00
|
|
|
/* We must be locked. */
|
2020-12-18 01:18:47 +00:00
|
|
|
MESOSPHERE_ASSERT((m_attribute & KMemoryAttribute_IpcLocked) == KMemoryAttribute_IpcLocked);
|
2020-07-12 22:42:47 +00:00
|
|
|
|
|
|
|
/* Unlock. */
|
2020-12-18 01:18:47 +00:00
|
|
|
const u16 old_lock_count = m_ipc_lock_count--;
|
2020-07-12 22:42:47 +00:00
|
|
|
MESOSPHERE_ABORT_UNLESS(old_lock_count > 0);
|
|
|
|
|
|
|
|
/* If this is our last unlock, update our permissions. */
|
|
|
|
if (old_lock_count == 1) {
|
2021-10-05 22:16:54 +00:00
|
|
|
MESOSPHERE_ASSERT(m_original_permission != KMemoryPermission_None);
|
|
|
|
m_permission = m_original_permission;
|
|
|
|
m_original_permission = KMemoryPermission_None;
|
2020-12-18 01:18:47 +00:00
|
|
|
m_attribute = static_cast<KMemoryAttribute>(m_attribute & ~KMemoryAttribute_IpcLocked);
|
2020-07-12 22:42:47 +00:00
|
|
|
}
|
2020-12-01 12:57:09 +00:00
|
|
|
|
|
|
|
if (left) {
|
2020-12-18 01:18:47 +00:00
|
|
|
const u16 old_ipc_disable_merge_count = m_ipc_disable_merge_count--;
|
2020-12-01 12:57:09 +00:00
|
|
|
MESOSPHERE_ASSERT(old_ipc_disable_merge_count > 0);
|
|
|
|
if (old_ipc_disable_merge_count == 1) {
|
2020-12-18 01:18:47 +00:00
|
|
|
m_disable_merge_attribute = static_cast<KMemoryBlockDisableMergeAttribute>(m_disable_merge_attribute & ~KMemoryBlockDisableMergeAttribute_IpcLeft);
|
2020-12-01 12:57:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
MESOSPHERE_UNUSED(right);
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr KMemoryBlockDisableMergeAttribute GetDisableMergeAttribute() const {
|
2020-12-18 01:18:47 +00:00
|
|
|
return m_disable_merge_attribute;
|
2020-07-12 22:42:47 +00:00
|
|
|
}
|
2020-02-07 12:58:35 +00:00
|
|
|
};
|
2020-02-10 17:57:00 +00:00
|
|
|
static_assert(std::is_trivially_destructible<KMemoryBlock>::value);
|
2020-02-07 12:58:35 +00:00
|
|
|
|
|
|
|
}
|