summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/common/CMakeLists.txt6
-rw-r--r--src/common/stb.cpp8
-rw-r--r--src/common/stb.h8
-rw-r--r--src/common/thread.cpp24
-rw-r--r--src/core/debugger/gdbstub.cpp10
-rw-r--r--src/core/file_sys/system_archive/system_version.cpp4
-rw-r--r--src/core/hle/kernel/init/init_slab_setup.cpp9
-rw-r--r--src/core/hle/kernel/initial_process.h4
-rw-r--r--src/core/hle/kernel/k_memory_block.h52
-rw-r--r--src/core/hle/kernel/k_memory_block_manager.cpp70
-rw-r--r--src/core/hle/kernel/k_memory_block_manager.h6
-rw-r--r--src/core/hle/kernel/k_memory_layout.h6
-rw-r--r--src/core/hle/kernel/k_memory_manager.cpp9
-rw-r--r--src/core/hle/kernel/k_memory_region_type.h105
-rw-r--r--src/core/hle/kernel/k_page_group.h11
-rw-r--r--src/core/hle/kernel/k_page_table.cpp314
-rw-r--r--src/core/hle/kernel/k_page_table.h33
-rw-r--r--src/core/hle/kernel/k_process.cpp2
-rw-r--r--src/core/hle/kernel/kernel.cpp23
-rw-r--r--src/core/hle/kernel/svc/svc_memory.cpp10
-rw-r--r--src/core/hle/kernel/svc_types.h1
-rw-r--r--src/core/hle/service/caps/caps.cpp7
-rw-r--r--src/core/hle/service/caps/caps_manager.cpp87
-rw-r--r--src/core/hle/service/caps/caps_manager.h11
-rw-r--r--src/core/hle/service/caps/caps_ss.cpp75
-rw-r--r--src/core/hle/service/caps/caps_ss.h8
-rw-r--r--src/core/hle/service/caps/caps_su.cpp69
-rw-r--r--src/core/hle/service/caps/caps_su.h8
-rw-r--r--src/core/hle/service/caps/caps_types.h2
-rw-r--r--src/core/hle/service/hle_ipc.cpp6
-rw-r--r--src/core/hle/service/nvnflinger/fb_share_buffer_manager.cpp2
-rw-r--r--src/core/hle/service/ptm/ts.cpp40
-rw-r--r--src/core/hle/service/ptm/ts.h6
-rw-r--r--src/core/hle/service/set/set_sys.cpp49
-rw-r--r--src/core/memory/cheat_engine.cpp23
-rw-r--r--src/shader_recompiler/backend/glsl/emit_glsl.cpp1
-rw-r--r--src/shader_recompiler/backend/glsl/emit_glsl_warp.cpp12
-rw-r--r--src/shader_recompiler/backend/spirv/emit_spirv_context_get_set.cpp23
-rw-r--r--src/shader_recompiler/backend/spirv/spirv_emit_context.h36
-rw-r--r--src/shader_recompiler/profile.h4
-rw-r--r--src/video_core/fence_manager.h5
-rw-r--r--src/video_core/renderer_vulkan/renderer_vulkan.cpp14
-rw-r--r--src/video_core/renderer_vulkan/vk_pipeline_cache.cpp6
-rw-r--r--src/video_core/renderer_vulkan/vk_rasterizer.cpp10
-rw-r--r--src/video_core/renderer_vulkan/vk_rasterizer.h4
-rw-r--r--src/video_core/vulkan_common/vulkan_device.cpp35
-rw-r--r--src/video_core/vulkan_common/vulkan_device.h18
-rw-r--r--src/video_core/vulkan_common/vulkan_wrapper.cpp2
-rw-r--r--src/yuzu/applets/qt_controller.cpp52
-rw-r--r--src/yuzu/applets/qt_controller.h4
-rw-r--r--src/yuzu/configuration/config.cpp3
-rw-r--r--src/yuzu/configuration/config.h2
-rw-r--r--src/yuzu/configuration/configure_hotkeys.cpp7
-rw-r--r--src/yuzu/configuration/configure_input.cpp59
-rw-r--r--src/yuzu/configuration/configure_input.h7
-rw-r--r--src/yuzu/configuration/configure_input_player.h5
-rw-r--r--src/yuzu/configuration/configure_vibration.cpp10
57 files changed, 1042 insertions, 385 deletions
diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt
index 8a1861051..e216eb3de 100644
--- a/src/common/CMakeLists.txt
+++ b/src/common/CMakeLists.txt
@@ -120,6 +120,8 @@ add_library(common STATIC
120 socket_types.h 120 socket_types.h
121 spin_lock.cpp 121 spin_lock.cpp
122 spin_lock.h 122 spin_lock.h
123 stb.cpp
124 stb.h
123 steady_clock.cpp 125 steady_clock.cpp
124 steady_clock.h 126 steady_clock.h
125 stream.cpp 127 stream.cpp
@@ -208,6 +210,8 @@ if (MSVC)
208 /we4254 # 'operator': conversion from 'type1:field_bits' to 'type2:field_bits', possible loss of data 210 /we4254 # 'operator': conversion from 'type1:field_bits' to 'type2:field_bits', possible loss of data
209 /we4800 # Implicit conversion from 'type' to bool. Possible information loss 211 /we4800 # Implicit conversion from 'type' to bool. Possible information loss
210 ) 212 )
213else()
214 set_source_files_properties(stb.cpp PROPERTIES COMPILE_OPTIONS "-Wno-implicit-fallthrough;-Wno-missing-declarations;-Wno-missing-field-initializers")
211endif() 215endif()
212 216
213if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang") 217if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
@@ -223,7 +227,7 @@ endif()
223 227
224create_target_directory_groups(common) 228create_target_directory_groups(common)
225 229
226target_link_libraries(common PUBLIC Boost::context Boost::headers fmt::fmt microprofile Threads::Threads) 230target_link_libraries(common PUBLIC Boost::context Boost::headers fmt::fmt microprofile stb::headers Threads::Threads)
227target_link_libraries(common PRIVATE lz4::lz4 zstd::zstd LLVM::Demangle) 231target_link_libraries(common PRIVATE lz4::lz4 zstd::zstd LLVM::Demangle)
228 232
229if (ANDROID) 233if (ANDROID)
diff --git a/src/common/stb.cpp b/src/common/stb.cpp
new file mode 100644
index 000000000..d3b16665d
--- /dev/null
+++ b/src/common/stb.cpp
@@ -0,0 +1,8 @@
1// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
2// SPDX-License-Identifier: GPL-2.0-or-later
3
4#define STB_IMAGE_IMPLEMENTATION
5#define STB_IMAGE_RESIZE_IMPLEMENTATION
6#define STB_IMAGE_WRITE_IMPLEMENTATION
7
8#include "common/stb.h"
diff --git a/src/common/stb.h b/src/common/stb.h
new file mode 100644
index 000000000..e5c197c11
--- /dev/null
+++ b/src/common/stb.h
@@ -0,0 +1,8 @@
1// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
2// SPDX-License-Identifier: GPL-2.0-or-later
3
4#pragma once
5
6#include <stb_image.h>
7#include <stb_image_resize.h>
8#include <stb_image_write.h>
diff --git a/src/common/thread.cpp b/src/common/thread.cpp
index 919e33af9..34cc1527b 100644
--- a/src/common/thread.cpp
+++ b/src/common/thread.cpp
@@ -11,6 +11,7 @@
11#include <mach/mach.h> 11#include <mach/mach.h>
12#elif defined(_WIN32) 12#elif defined(_WIN32)
13#include <windows.h> 13#include <windows.h>
14#include "common/string_util.h"
14#else 15#else
15#if defined(__Bitrig__) || defined(__DragonFly__) || defined(__FreeBSD__) || defined(__OpenBSD__) 16#if defined(__Bitrig__) || defined(__DragonFly__) || defined(__FreeBSD__) || defined(__OpenBSD__)
16#include <pthread_np.h> 17#include <pthread_np.h>
@@ -82,29 +83,8 @@ void SetCurrentThreadPriority(ThreadPriority new_priority) {
82#ifdef _MSC_VER 83#ifdef _MSC_VER
83 84
84// Sets the debugger-visible name of the current thread. 85// Sets the debugger-visible name of the current thread.
85// Uses trick documented in:
86// https://docs.microsoft.com/en-us/visualstudio/debugger/how-to-set-a-thread-name-in-native-code
87void SetCurrentThreadName(const char* name) { 86void SetCurrentThreadName(const char* name) {
88 static const DWORD MS_VC_EXCEPTION = 0x406D1388; 87 SetThreadDescription(GetCurrentThread(), UTF8ToUTF16W(name).data());
89
90#pragma pack(push, 8)
91 struct THREADNAME_INFO {
92 DWORD dwType; // must be 0x1000
93 LPCSTR szName; // pointer to name (in user addr space)
94 DWORD dwThreadID; // thread ID (-1=caller thread)
95 DWORD dwFlags; // reserved for future use, must be zero
96 } info;
97#pragma pack(pop)
98
99 info.dwType = 0x1000;
100 info.szName = name;
101 info.dwThreadID = std::numeric_limits<DWORD>::max();
102 info.dwFlags = 0;
103
104 __try {
105 RaiseException(MS_VC_EXCEPTION, 0, sizeof(info) / sizeof(ULONG_PTR), (ULONG_PTR*)&info);
106 } __except (EXCEPTION_CONTINUE_EXECUTION) {
107 }
108} 88}
109 89
110#else // !MSVC_VER, so must be POSIX threads 90#else // !MSVC_VER, so must be POSIX threads
diff --git a/src/core/debugger/gdbstub.cpp b/src/core/debugger/gdbstub.cpp
index 82964f0a1..2076aa8a2 100644
--- a/src/core/debugger/gdbstub.cpp
+++ b/src/core/debugger/gdbstub.cpp
@@ -822,11 +822,13 @@ void GDBStub::HandleRcmd(const std::vector<u8>& command) {
822 const char i = True(mem_info.attribute & MemoryAttribute::IpcLocked) ? 'I' : '-'; 822 const char i = True(mem_info.attribute & MemoryAttribute::IpcLocked) ? 'I' : '-';
823 const char d = True(mem_info.attribute & MemoryAttribute::DeviceShared) ? 'D' : '-'; 823 const char d = True(mem_info.attribute & MemoryAttribute::DeviceShared) ? 'D' : '-';
824 const char u = True(mem_info.attribute & MemoryAttribute::Uncached) ? 'U' : '-'; 824 const char u = True(mem_info.attribute & MemoryAttribute::Uncached) ? 'U' : '-';
825 const char p =
826 True(mem_info.attribute & MemoryAttribute::PermissionLocked) ? 'P' : '-';
825 827
826 reply += 828 reply += fmt::format(" {:#012x} - {:#012x} {} {} {}{}{}{}{} [{}, {}]\n",
827 fmt::format(" {:#012x} - {:#012x} {} {} {}{}{}{} [{}, {}]\n", 829 mem_info.base_address,
828 mem_info.base_address, mem_info.base_address + mem_info.size - 1, 830 mem_info.base_address + mem_info.size - 1, perm, state, l, i,
829 perm, state, l, i, d, u, mem_info.ipc_count, mem_info.device_count); 831 d, u, p, mem_info.ipc_count, mem_info.device_count);
830 } 832 }
831 833
832 const uintptr_t next_address = mem_info.base_address + mem_info.size; 834 const uintptr_t next_address = mem_info.base_address + mem_info.size;
diff --git a/src/core/file_sys/system_archive/system_version.cpp b/src/core/file_sys/system_archive/system_version.cpp
index bd493ecca..e4751c2b4 100644
--- a/src/core/file_sys/system_archive/system_version.cpp
+++ b/src/core/file_sys/system_archive/system_version.cpp
@@ -1,6 +1,7 @@
1// SPDX-FileCopyrightText: Copyright 2019 yuzu Emulator Project 1// SPDX-FileCopyrightText: Copyright 2019 yuzu Emulator Project
2// SPDX-License-Identifier: GPL-2.0-or-later 2// SPDX-License-Identifier: GPL-2.0-or-later
3 3
4#include "common/logging/log.h"
4#include "core/file_sys/system_archive/system_version.h" 5#include "core/file_sys/system_archive/system_version.h"
5#include "core/file_sys/vfs_vector.h" 6#include "core/file_sys/vfs_vector.h"
6#include "core/hle/api_version.h" 7#include "core/hle/api_version.h"
@@ -12,6 +13,9 @@ std::string GetLongDisplayVersion() {
12} 13}
13 14
14VirtualDir SystemVersion() { 15VirtualDir SystemVersion() {
16 LOG_WARNING(Common_Filesystem, "called - Using hardcoded firmware version '{}'",
17 GetLongDisplayVersion());
18
15 VirtualFile file = std::make_shared<VectorVfsFile>(std::vector<u8>(0x100), "file"); 19 VirtualFile file = std::make_shared<VectorVfsFile>(std::vector<u8>(0x100), "file");
16 file->WriteObject(HLE::ApiVersion::HOS_VERSION_MAJOR, 0); 20 file->WriteObject(HLE::ApiVersion::HOS_VERSION_MAJOR, 0);
17 file->WriteObject(HLE::ApiVersion::HOS_VERSION_MINOR, 1); 21 file->WriteObject(HLE::ApiVersion::HOS_VERSION_MINOR, 1);
diff --git a/src/core/hle/kernel/init/init_slab_setup.cpp b/src/core/hle/kernel/init/init_slab_setup.cpp
index 1f2db673c..a0e20bbbb 100644
--- a/src/core/hle/kernel/init/init_slab_setup.cpp
+++ b/src/core/hle/kernel/init/init_slab_setup.cpp
@@ -106,7 +106,7 @@ static_assert(KernelPageBufferAdditionalSize ==
106/// memory. 106/// memory.
107static KPhysicalAddress TranslateSlabAddrToPhysical(KMemoryLayout& memory_layout, 107static KPhysicalAddress TranslateSlabAddrToPhysical(KMemoryLayout& memory_layout,
108 KVirtualAddress slab_addr) { 108 KVirtualAddress slab_addr) {
109 slab_addr -= GetInteger(memory_layout.GetSlabRegionAddress()); 109 slab_addr -= memory_layout.GetSlabRegion().GetAddress();
110 return GetInteger(slab_addr) + Core::DramMemoryMap::SlabHeapBase; 110 return GetInteger(slab_addr) + Core::DramMemoryMap::SlabHeapBase;
111} 111}
112 112
@@ -196,7 +196,12 @@ void InitializeSlabHeaps(Core::System& system, KMemoryLayout& memory_layout) {
196 auto& kernel = system.Kernel(); 196 auto& kernel = system.Kernel();
197 197
198 // Get the start of the slab region, since that's where we'll be working. 198 // Get the start of the slab region, since that's where we'll be working.
199 KVirtualAddress address = memory_layout.GetSlabRegionAddress(); 199 const KMemoryRegion& slab_region = memory_layout.GetSlabRegion();
200 KVirtualAddress address = slab_region.GetAddress();
201
202 // Clear the slab region.
203 // TODO: implement access to kernel VAs.
204 // std::memset(device_ptr, 0, slab_region.GetSize());
200 205
201 // Initialize slab type array to be in sorted order. 206 // Initialize slab type array to be in sorted order.
202 std::array<KSlabType, KSlabType_Count> slab_types; 207 std::array<KSlabType, KSlabType_Count> slab_types;
diff --git a/src/core/hle/kernel/initial_process.h b/src/core/hle/kernel/initial_process.h
index 82195f4f7..2c95269fc 100644
--- a/src/core/hle/kernel/initial_process.h
+++ b/src/core/hle/kernel/initial_process.h
@@ -19,4 +19,8 @@ static inline KPhysicalAddress GetInitialProcessBinaryPhysicalAddress() {
19 MainMemoryAddress); 19 MainMemoryAddress);
20} 20}
21 21
22static inline size_t GetInitialProcessBinarySize() {
23 return InitialProcessBinarySizeMax;
24}
25
22} // namespace Kernel 26} // namespace Kernel
diff --git a/src/core/hle/kernel/k_memory_block.h b/src/core/hle/kernel/k_memory_block.h
index 41a29da24..ef3f61321 100644
--- a/src/core/hle/kernel/k_memory_block.h
+++ b/src/core/hle/kernel/k_memory_block.h
@@ -36,6 +36,7 @@ enum class KMemoryState : u32 {
36 FlagCanChangeAttribute = (1 << 24), 36 FlagCanChangeAttribute = (1 << 24),
37 FlagCanCodeMemory = (1 << 25), 37 FlagCanCodeMemory = (1 << 25),
38 FlagLinearMapped = (1 << 26), 38 FlagLinearMapped = (1 << 26),
39 FlagCanPermissionLock = (1 << 27),
39 40
40 FlagsData = FlagCanReprotect | FlagCanUseIpc | FlagCanUseNonDeviceIpc | FlagCanUseNonSecureIpc | 41 FlagsData = FlagCanReprotect | FlagCanUseIpc | FlagCanUseNonDeviceIpc | FlagCanUseNonSecureIpc |
41 FlagMapped | FlagCanAlias | FlagCanTransfer | FlagCanQueryPhysical | 42 FlagMapped | FlagCanAlias | FlagCanTransfer | FlagCanQueryPhysical |
@@ -50,12 +51,16 @@ enum class KMemoryState : u32 {
50 FlagLinearMapped, 51 FlagLinearMapped,
51 52
52 Free = static_cast<u32>(Svc::MemoryState::Free), 53 Free = static_cast<u32>(Svc::MemoryState::Free),
53 Io = static_cast<u32>(Svc::MemoryState::Io) | FlagMapped | FlagCanDeviceMap | 54
54 FlagCanAlignedDeviceMap, 55 IoMemory = static_cast<u32>(Svc::MemoryState::Io) | FlagMapped | FlagCanDeviceMap |
56 FlagCanAlignedDeviceMap,
57 IoRegister =
58 static_cast<u32>(Svc::MemoryState::Io) | FlagCanDeviceMap | FlagCanAlignedDeviceMap,
59
55 Static = static_cast<u32>(Svc::MemoryState::Static) | FlagMapped | FlagCanQueryPhysical, 60 Static = static_cast<u32>(Svc::MemoryState::Static) | FlagMapped | FlagCanQueryPhysical,
56 Code = static_cast<u32>(Svc::MemoryState::Code) | FlagsCode | FlagCanMapProcess, 61 Code = static_cast<u32>(Svc::MemoryState::Code) | FlagsCode | FlagCanMapProcess,
57 CodeData = static_cast<u32>(Svc::MemoryState::CodeData) | FlagsData | FlagCanMapProcess | 62 CodeData = static_cast<u32>(Svc::MemoryState::CodeData) | FlagsData | FlagCanMapProcess |
58 FlagCanCodeMemory, 63 FlagCanCodeMemory | FlagCanPermissionLock,
59 Normal = static_cast<u32>(Svc::MemoryState::Normal) | FlagsData | FlagCanCodeMemory, 64 Normal = static_cast<u32>(Svc::MemoryState::Normal) | FlagsData | FlagCanCodeMemory,
60 Shared = static_cast<u32>(Svc::MemoryState::Shared) | FlagMapped | FlagReferenceCounted | 65 Shared = static_cast<u32>(Svc::MemoryState::Shared) | FlagMapped | FlagReferenceCounted |
61 FlagLinearMapped, 66 FlagLinearMapped,
@@ -65,7 +70,8 @@ enum class KMemoryState : u32 {
65 AliasCode = static_cast<u32>(Svc::MemoryState::AliasCode) | FlagsCode | FlagCanMapProcess | 70 AliasCode = static_cast<u32>(Svc::MemoryState::AliasCode) | FlagsCode | FlagCanMapProcess |
66 FlagCanCodeAlias, 71 FlagCanCodeAlias,
67 AliasCodeData = static_cast<u32>(Svc::MemoryState::AliasCodeData) | FlagsData | 72 AliasCodeData = static_cast<u32>(Svc::MemoryState::AliasCodeData) | FlagsData |
68 FlagCanMapProcess | FlagCanCodeAlias | FlagCanCodeMemory, 73 FlagCanMapProcess | FlagCanCodeAlias | FlagCanCodeMemory |
74 FlagCanPermissionLock,
69 75
70 Ipc = static_cast<u32>(Svc::MemoryState::Ipc) | FlagsMisc | FlagCanAlignedDeviceMap | 76 Ipc = static_cast<u32>(Svc::MemoryState::Ipc) | FlagsMisc | FlagCanAlignedDeviceMap |
71 FlagCanUseIpc | FlagCanUseNonSecureIpc | FlagCanUseNonDeviceIpc, 77 FlagCanUseIpc | FlagCanUseNonSecureIpc | FlagCanUseNonDeviceIpc,
@@ -73,7 +79,7 @@ enum class KMemoryState : u32 {
73 Stack = static_cast<u32>(Svc::MemoryState::Stack) | FlagsMisc | FlagCanAlignedDeviceMap | 79 Stack = static_cast<u32>(Svc::MemoryState::Stack) | FlagsMisc | FlagCanAlignedDeviceMap |
74 FlagCanUseIpc | FlagCanUseNonSecureIpc | FlagCanUseNonDeviceIpc, 80 FlagCanUseIpc | FlagCanUseNonSecureIpc | FlagCanUseNonDeviceIpc,
75 81
76 ThreadLocal = static_cast<u32>(Svc::MemoryState::ThreadLocal) | FlagMapped | FlagLinearMapped, 82 ThreadLocal = static_cast<u32>(Svc::MemoryState::ThreadLocal) | FlagLinearMapped,
77 83
78 Transfered = static_cast<u32>(Svc::MemoryState::Transfered) | FlagsMisc | 84 Transfered = static_cast<u32>(Svc::MemoryState::Transfered) | FlagsMisc |
79 FlagCanAlignedDeviceMap | FlagCanChangeAttribute | FlagCanUseIpc | 85 FlagCanAlignedDeviceMap | FlagCanChangeAttribute | FlagCanUseIpc |
@@ -94,7 +100,7 @@ enum class KMemoryState : u32 {
94 NonDeviceIpc = 100 NonDeviceIpc =
95 static_cast<u32>(Svc::MemoryState::NonDeviceIpc) | FlagsMisc | FlagCanUseNonDeviceIpc, 101 static_cast<u32>(Svc::MemoryState::NonDeviceIpc) | FlagsMisc | FlagCanUseNonDeviceIpc,
96 102
97 Kernel = static_cast<u32>(Svc::MemoryState::Kernel) | FlagMapped, 103 Kernel = static_cast<u32>(Svc::MemoryState::Kernel),
98 104
99 GeneratedCode = static_cast<u32>(Svc::MemoryState::GeneratedCode) | FlagMapped | 105 GeneratedCode = static_cast<u32>(Svc::MemoryState::GeneratedCode) | FlagMapped |
100 FlagReferenceCounted | FlagCanDebug | FlagLinearMapped, 106 FlagReferenceCounted | FlagCanDebug | FlagLinearMapped,
@@ -105,34 +111,36 @@ enum class KMemoryState : u32 {
105 111
106 Insecure = static_cast<u32>(Svc::MemoryState::Insecure) | FlagMapped | FlagReferenceCounted | 112 Insecure = static_cast<u32>(Svc::MemoryState::Insecure) | FlagMapped | FlagReferenceCounted |
107 FlagLinearMapped | FlagCanChangeAttribute | FlagCanDeviceMap | 113 FlagLinearMapped | FlagCanChangeAttribute | FlagCanDeviceMap |
108 FlagCanAlignedDeviceMap | FlagCanUseNonSecureIpc | FlagCanUseNonDeviceIpc, 114 FlagCanAlignedDeviceMap | FlagCanQueryPhysical | FlagCanUseNonSecureIpc |
115 FlagCanUseNonDeviceIpc,
109}; 116};
110DECLARE_ENUM_FLAG_OPERATORS(KMemoryState); 117DECLARE_ENUM_FLAG_OPERATORS(KMemoryState);
111 118
112static_assert(static_cast<u32>(KMemoryState::Free) == 0x00000000); 119static_assert(static_cast<u32>(KMemoryState::Free) == 0x00000000);
113static_assert(static_cast<u32>(KMemoryState::Io) == 0x00182001); 120static_assert(static_cast<u32>(KMemoryState::IoMemory) == 0x00182001);
121static_assert(static_cast<u32>(KMemoryState::IoRegister) == 0x00180001);
114static_assert(static_cast<u32>(KMemoryState::Static) == 0x00042002); 122static_assert(static_cast<u32>(KMemoryState::Static) == 0x00042002);
115static_assert(static_cast<u32>(KMemoryState::Code) == 0x04DC7E03); 123static_assert(static_cast<u32>(KMemoryState::Code) == 0x04DC7E03);
116static_assert(static_cast<u32>(KMemoryState::CodeData) == 0x07FEBD04); 124static_assert(static_cast<u32>(KMemoryState::CodeData) == 0x0FFEBD04);
117static_assert(static_cast<u32>(KMemoryState::Normal) == 0x077EBD05); 125static_assert(static_cast<u32>(KMemoryState::Normal) == 0x077EBD05);
118static_assert(static_cast<u32>(KMemoryState::Shared) == 0x04402006); 126static_assert(static_cast<u32>(KMemoryState::Shared) == 0x04402006);
119 127
120static_assert(static_cast<u32>(KMemoryState::AliasCode) == 0x04DD7E08); 128static_assert(static_cast<u32>(KMemoryState::AliasCode) == 0x04DD7E08);
121static_assert(static_cast<u32>(KMemoryState::AliasCodeData) == 0x07FFBD09); 129static_assert(static_cast<u32>(KMemoryState::AliasCodeData) == 0x0FFFBD09);
122static_assert(static_cast<u32>(KMemoryState::Ipc) == 0x045C3C0A); 130static_assert(static_cast<u32>(KMemoryState::Ipc) == 0x045C3C0A);
123static_assert(static_cast<u32>(KMemoryState::Stack) == 0x045C3C0B); 131static_assert(static_cast<u32>(KMemoryState::Stack) == 0x045C3C0B);
124static_assert(static_cast<u32>(KMemoryState::ThreadLocal) == 0x0400200C); 132static_assert(static_cast<u32>(KMemoryState::ThreadLocal) == 0x0400000C);
125static_assert(static_cast<u32>(KMemoryState::Transfered) == 0x055C3C0D); 133static_assert(static_cast<u32>(KMemoryState::Transfered) == 0x055C3C0D);
126static_assert(static_cast<u32>(KMemoryState::SharedTransfered) == 0x045C380E); 134static_assert(static_cast<u32>(KMemoryState::SharedTransfered) == 0x045C380E);
127static_assert(static_cast<u32>(KMemoryState::SharedCode) == 0x0440380F); 135static_assert(static_cast<u32>(KMemoryState::SharedCode) == 0x0440380F);
128static_assert(static_cast<u32>(KMemoryState::Inaccessible) == 0x00000010); 136static_assert(static_cast<u32>(KMemoryState::Inaccessible) == 0x00000010);
129static_assert(static_cast<u32>(KMemoryState::NonSecureIpc) == 0x045C3811); 137static_assert(static_cast<u32>(KMemoryState::NonSecureIpc) == 0x045C3811);
130static_assert(static_cast<u32>(KMemoryState::NonDeviceIpc) == 0x044C2812); 138static_assert(static_cast<u32>(KMemoryState::NonDeviceIpc) == 0x044C2812);
131static_assert(static_cast<u32>(KMemoryState::Kernel) == 0x00002013); 139static_assert(static_cast<u32>(KMemoryState::Kernel) == 0x00000013);
132static_assert(static_cast<u32>(KMemoryState::GeneratedCode) == 0x04402214); 140static_assert(static_cast<u32>(KMemoryState::GeneratedCode) == 0x04402214);
133static_assert(static_cast<u32>(KMemoryState::CodeOut) == 0x04402015); 141static_assert(static_cast<u32>(KMemoryState::CodeOut) == 0x04402015);
134static_assert(static_cast<u32>(KMemoryState::Coverage) == 0x00002016); 142static_assert(static_cast<u32>(KMemoryState::Coverage) == 0x00002016);
135static_assert(static_cast<u32>(KMemoryState::Insecure) == 0x05583817); 143static_assert(static_cast<u32>(KMemoryState::Insecure) == 0x055C3817);
136 144
137enum class KMemoryPermission : u8 { 145enum class KMemoryPermission : u8 {
138 None = 0, 146 None = 0,
@@ -182,8 +190,9 @@ enum class KMemoryAttribute : u8 {
182 IpcLocked = static_cast<u8>(Svc::MemoryAttribute::IpcLocked), 190 IpcLocked = static_cast<u8>(Svc::MemoryAttribute::IpcLocked),
183 DeviceShared = static_cast<u8>(Svc::MemoryAttribute::DeviceShared), 191 DeviceShared = static_cast<u8>(Svc::MemoryAttribute::DeviceShared),
184 Uncached = static_cast<u8>(Svc::MemoryAttribute::Uncached), 192 Uncached = static_cast<u8>(Svc::MemoryAttribute::Uncached),
193 PermissionLocked = static_cast<u8>(Svc::MemoryAttribute::PermissionLocked),
185 194
186 SetMask = Uncached, 195 SetMask = Uncached | PermissionLocked,
187}; 196};
188DECLARE_ENUM_FLAG_OPERATORS(KMemoryAttribute); 197DECLARE_ENUM_FLAG_OPERATORS(KMemoryAttribute);
189 198
@@ -261,6 +270,10 @@ struct KMemoryInfo {
261 return m_state; 270 return m_state;
262 } 271 }
263 272
273 constexpr Svc::MemoryState GetSvcState() const {
274 return static_cast<Svc::MemoryState>(m_state & KMemoryState::Mask);
275 }
276
264 constexpr KMemoryPermission GetPermission() const { 277 constexpr KMemoryPermission GetPermission() const {
265 return m_permission; 278 return m_permission;
266 } 279 }
@@ -326,6 +339,10 @@ public:
326 return this->GetEndAddress() - 1; 339 return this->GetEndAddress() - 1;
327 } 340 }
328 341
342 constexpr KMemoryState GetState() const {
343 return m_memory_state;
344 }
345
329 constexpr u16 GetIpcLockCount() const { 346 constexpr u16 GetIpcLockCount() const {
330 return m_ipc_lock_count; 347 return m_ipc_lock_count;
331 } 348 }
@@ -443,6 +460,13 @@ public:
443 } 460 }
444 } 461 }
445 462
463 constexpr void UpdateAttribute(KMemoryAttribute mask, KMemoryAttribute attr) {
464 ASSERT(False(mask & KMemoryAttribute::IpcLocked));
465 ASSERT(False(mask & KMemoryAttribute::DeviceShared));
466
467 m_attribute = (m_attribute & ~mask) | attr;
468 }
469
446 constexpr void Split(KMemoryBlock* block, KProcessAddress addr) { 470 constexpr void Split(KMemoryBlock* block, KProcessAddress addr) {
447 ASSERT(this->GetAddress() < addr); 471 ASSERT(this->GetAddress() < addr);
448 ASSERT(this->Contains(addr)); 472 ASSERT(this->Contains(addr));
diff --git a/src/core/hle/kernel/k_memory_block_manager.cpp b/src/core/hle/kernel/k_memory_block_manager.cpp
index ab75f550e..58a1e7216 100644
--- a/src/core/hle/kernel/k_memory_block_manager.cpp
+++ b/src/core/hle/kernel/k_memory_block_manager.cpp
@@ -160,8 +160,8 @@ void KMemoryBlockManager::Update(KMemoryBlockManagerUpdateAllocator* allocator,
160 } 160 }
161 161
162 // Update block state. 162 // Update block state.
163 it->Update(state, perm, attr, cur_address == address, static_cast<u8>(set_disable_attr), 163 it->Update(state, perm, attr, it->GetAddress() == address,
164 static_cast<u8>(clear_disable_attr)); 164 static_cast<u8>(set_disable_attr), static_cast<u8>(clear_disable_attr));
165 cur_address += cur_info.GetSize(); 165 cur_address += cur_info.GetSize();
166 remaining_pages -= cur_info.GetNumPages(); 166 remaining_pages -= cur_info.GetNumPages();
167 } 167 }
@@ -175,7 +175,9 @@ void KMemoryBlockManager::UpdateIfMatch(KMemoryBlockManagerUpdateAllocator* allo
175 KProcessAddress address, size_t num_pages, 175 KProcessAddress address, size_t num_pages,
176 KMemoryState test_state, KMemoryPermission test_perm, 176 KMemoryState test_state, KMemoryPermission test_perm,
177 KMemoryAttribute test_attr, KMemoryState state, 177 KMemoryAttribute test_attr, KMemoryState state,
178 KMemoryPermission perm, KMemoryAttribute attr) { 178 KMemoryPermission perm, KMemoryAttribute attr,
179 KMemoryBlockDisableMergeAttribute set_disable_attr,
180 KMemoryBlockDisableMergeAttribute clear_disable_attr) {
179 // Ensure for auditing that we never end up with an invalid tree. 181 // Ensure for auditing that we never end up with an invalid tree.
180 KScopedMemoryBlockManagerAuditor auditor(this); 182 KScopedMemoryBlockManagerAuditor auditor(this);
181 ASSERT(Common::IsAligned(GetInteger(address), PageSize)); 183 ASSERT(Common::IsAligned(GetInteger(address), PageSize));
@@ -214,7 +216,8 @@ void KMemoryBlockManager::UpdateIfMatch(KMemoryBlockManagerUpdateAllocator* allo
214 } 216 }
215 217
216 // Update block state. 218 // Update block state.
217 it->Update(state, perm, attr, false, 0, 0); 219 it->Update(state, perm, attr, false, static_cast<u8>(set_disable_attr),
220 static_cast<u8>(clear_disable_attr));
218 cur_address += cur_info.GetSize(); 221 cur_address += cur_info.GetSize();
219 remaining_pages -= cur_info.GetNumPages(); 222 remaining_pages -= cur_info.GetNumPages();
220 } else { 223 } else {
@@ -284,6 +287,65 @@ void KMemoryBlockManager::UpdateLock(KMemoryBlockManagerUpdateAllocator* allocat
284 this->CoalesceForUpdate(allocator, address, num_pages); 287 this->CoalesceForUpdate(allocator, address, num_pages);
285} 288}
286 289
290void KMemoryBlockManager::UpdateAttribute(KMemoryBlockManagerUpdateAllocator* allocator,
291 KProcessAddress address, size_t num_pages,
292 KMemoryAttribute mask, KMemoryAttribute attr) {
293 // Ensure for auditing that we never end up with an invalid tree.
294 KScopedMemoryBlockManagerAuditor auditor(this);
295 ASSERT(Common::IsAligned(GetInteger(address), PageSize));
296
297 KProcessAddress cur_address = address;
298 size_t remaining_pages = num_pages;
299 iterator it = this->FindIterator(address);
300
301 while (remaining_pages > 0) {
302 const size_t remaining_size = remaining_pages * PageSize;
303 KMemoryInfo cur_info = it->GetMemoryInfo();
304
305 if ((it->GetAttribute() & mask) != attr) {
306 // If we need to, create a new block before and insert it.
307 if (cur_info.GetAddress() != GetInteger(cur_address)) {
308 KMemoryBlock* new_block = allocator->Allocate();
309
310 it->Split(new_block, cur_address);
311 it = m_memory_block_tree.insert(*new_block);
312 it++;
313
314 cur_info = it->GetMemoryInfo();
315 cur_address = cur_info.GetAddress();
316 }
317
318 // If we need to, create a new block after and insert it.
319 if (cur_info.GetSize() > remaining_size) {
320 KMemoryBlock* new_block = allocator->Allocate();
321
322 it->Split(new_block, cur_address + remaining_size);
323 it = m_memory_block_tree.insert(*new_block);
324
325 cur_info = it->GetMemoryInfo();
326 }
327
328 // Update block state.
329 it->UpdateAttribute(mask, attr);
330 cur_address += cur_info.GetSize();
331 remaining_pages -= cur_info.GetNumPages();
332 } else {
333 // If we already have the right attributes, just advance.
334 if (cur_address + remaining_size < cur_info.GetEndAddress()) {
335 remaining_pages = 0;
336 cur_address += remaining_size;
337 } else {
338 remaining_pages =
339 (cur_address + remaining_size - cur_info.GetEndAddress()) / PageSize;
340 cur_address = cur_info.GetEndAddress();
341 }
342 }
343 it++;
344 }
345
346 this->CoalesceForUpdate(allocator, address, num_pages);
347}
348
287// Debug. 349// Debug.
288bool KMemoryBlockManager::CheckState() const { 350bool KMemoryBlockManager::CheckState() const {
289 // Loop over every block, ensuring that we are sorted and coalesced. 351 // Loop over every block, ensuring that we are sorted and coalesced.
diff --git a/src/core/hle/kernel/k_memory_block_manager.h b/src/core/hle/kernel/k_memory_block_manager.h
index 96496e990..cb7b6f430 100644
--- a/src/core/hle/kernel/k_memory_block_manager.h
+++ b/src/core/hle/kernel/k_memory_block_manager.h
@@ -115,7 +115,11 @@ public:
115 void UpdateIfMatch(KMemoryBlockManagerUpdateAllocator* allocator, KProcessAddress address, 115 void UpdateIfMatch(KMemoryBlockManagerUpdateAllocator* allocator, KProcessAddress address,
116 size_t num_pages, KMemoryState test_state, KMemoryPermission test_perm, 116 size_t num_pages, KMemoryState test_state, KMemoryPermission test_perm,
117 KMemoryAttribute test_attr, KMemoryState state, KMemoryPermission perm, 117 KMemoryAttribute test_attr, KMemoryState state, KMemoryPermission perm,
118 KMemoryAttribute attr); 118 KMemoryAttribute attr, KMemoryBlockDisableMergeAttribute set_disable_attr,
119 KMemoryBlockDisableMergeAttribute clear_disable_attr);
120
121 void UpdateAttribute(KMemoryBlockManagerUpdateAllocator* allocator, KProcessAddress address,
122 size_t num_pages, KMemoryAttribute mask, KMemoryAttribute attr);
119 123
120 iterator FindIterator(KProcessAddress address) const { 124 iterator FindIterator(KProcessAddress address) const {
121 return m_memory_block_tree.find(KMemoryBlock( 125 return m_memory_block_tree.find(KMemoryBlock(
diff --git a/src/core/hle/kernel/k_memory_layout.h b/src/core/hle/kernel/k_memory_layout.h
index 54a71df56..c8122644f 100644
--- a/src/core/hle/kernel/k_memory_layout.h
+++ b/src/core/hle/kernel/k_memory_layout.h
@@ -137,11 +137,9 @@ public:
137 return GetStackTopAddress(core_id, KMemoryRegionType_KernelMiscExceptionStack); 137 return GetStackTopAddress(core_id, KMemoryRegionType_KernelMiscExceptionStack);
138 } 138 }
139 139
140 KVirtualAddress GetSlabRegionAddress() const { 140 const KMemoryRegion& GetSlabRegion() const {
141 return Dereference(GetVirtualMemoryRegionTree().FindByType(KMemoryRegionType_KernelSlab)) 141 return Dereference(GetVirtualMemoryRegionTree().FindByType(KMemoryRegionType_KernelSlab));
142 .GetAddress();
143 } 142 }
144
145 const KMemoryRegion& GetDeviceRegion(KMemoryRegionType type) const { 143 const KMemoryRegion& GetDeviceRegion(KMemoryRegionType type) const {
146 return Dereference(GetPhysicalMemoryRegionTree().FindFirstDerived(type)); 144 return Dereference(GetPhysicalMemoryRegionTree().FindFirstDerived(type));
147 } 145 }
diff --git a/src/core/hle/kernel/k_memory_manager.cpp b/src/core/hle/kernel/k_memory_manager.cpp
index 74d8169e0..637558e10 100644
--- a/src/core/hle/kernel/k_memory_manager.cpp
+++ b/src/core/hle/kernel/k_memory_manager.cpp
@@ -119,7 +119,8 @@ void KMemoryManager::Initialize(KVirtualAddress management_region, size_t manage
119 // Free each region to its corresponding heap. 119 // Free each region to its corresponding heap.
120 size_t reserved_sizes[MaxManagerCount] = {}; 120 size_t reserved_sizes[MaxManagerCount] = {};
121 const KPhysicalAddress ini_start = GetInitialProcessBinaryPhysicalAddress(); 121 const KPhysicalAddress ini_start = GetInitialProcessBinaryPhysicalAddress();
122 const KPhysicalAddress ini_end = ini_start + InitialProcessBinarySizeMax; 122 const size_t ini_size = GetInitialProcessBinarySize();
123 const KPhysicalAddress ini_end = ini_start + ini_size;
123 const KPhysicalAddress ini_last = ini_end - 1; 124 const KPhysicalAddress ini_last = ini_end - 1;
124 for (const auto& it : m_system.Kernel().MemoryLayout().GetPhysicalMemoryRegionTree()) { 125 for (const auto& it : m_system.Kernel().MemoryLayout().GetPhysicalMemoryRegionTree()) {
125 if (it.IsDerivedFrom(KMemoryRegionType_DramUserPool)) { 126 if (it.IsDerivedFrom(KMemoryRegionType_DramUserPool)) {
@@ -137,13 +138,13 @@ void KMemoryManager::Initialize(KVirtualAddress management_region, size_t manage
137 } 138 }
138 139
139 // Open/reserve the ini memory. 140 // Open/reserve the ini memory.
140 manager.OpenFirst(ini_start, InitialProcessBinarySizeMax / PageSize); 141 manager.OpenFirst(ini_start, ini_size / PageSize);
141 reserved_sizes[it.GetAttributes()] += InitialProcessBinarySizeMax; 142 reserved_sizes[it.GetAttributes()] += ini_size;
142 143
143 // Free memory after the ini to the heap. 144 // Free memory after the ini to the heap.
144 if (ini_last != cur_last) { 145 if (ini_last != cur_last) {
145 ASSERT(cur_end != 0); 146 ASSERT(cur_end != 0);
146 manager.Free(ini_end, cur_end - ini_end); 147 manager.Free(ini_end, (cur_end - ini_end) / PageSize);
147 } 148 }
148 } else { 149 } else {
149 // Ensure there's no partial overlap with the ini image. 150 // Ensure there's no partial overlap with the ini image.
diff --git a/src/core/hle/kernel/k_memory_region_type.h b/src/core/hle/kernel/k_memory_region_type.h
index e5630c1ac..bcbf450f0 100644
--- a/src/core/hle/kernel/k_memory_region_type.h
+++ b/src/core/hle/kernel/k_memory_region_type.h
@@ -190,9 +190,15 @@ static_assert(KMemoryRegionType_DramKernelInitPt.GetValue() ==
190constexpr inline auto KMemoryRegionType_DramKernelSecureAppletMemory = 190constexpr inline auto KMemoryRegionType_DramKernelSecureAppletMemory =
191 KMemoryRegionType_DramKernelBase.DeriveSparse(1, 3, 0).SetAttribute( 191 KMemoryRegionType_DramKernelBase.DeriveSparse(1, 3, 0).SetAttribute(
192 KMemoryRegionAttr_LinearMapped); 192 KMemoryRegionAttr_LinearMapped);
193constexpr inline const auto KMemoryRegionType_DramKernelSecureUnknown =
194 KMemoryRegionType_DramKernelBase.DeriveSparse(1, 3, 1).SetAttribute(
195 KMemoryRegionAttr_LinearMapped);
193static_assert(KMemoryRegionType_DramKernelSecureAppletMemory.GetValue() == 196static_assert(KMemoryRegionType_DramKernelSecureAppletMemory.GetValue() ==
194 (0x18E | KMemoryRegionAttr_CarveoutProtected | KMemoryRegionAttr_NoUserMap | 197 (0x18E | KMemoryRegionAttr_CarveoutProtected | KMemoryRegionAttr_NoUserMap |
195 KMemoryRegionAttr_LinearMapped)); 198 KMemoryRegionAttr_LinearMapped));
199static_assert(KMemoryRegionType_DramKernelSecureUnknown.GetValue() ==
200 (0x28E | KMemoryRegionAttr_CarveoutProtected | KMemoryRegionAttr_NoUserMap |
201 KMemoryRegionAttr_LinearMapped));
196 202
197constexpr inline auto KMemoryRegionType_DramReservedEarly = 203constexpr inline auto KMemoryRegionType_DramReservedEarly =
198 KMemoryRegionType_DramReservedBase.DeriveAttribute(KMemoryRegionAttr_NoUserMap); 204 KMemoryRegionType_DramReservedBase.DeriveAttribute(KMemoryRegionAttr_NoUserMap);
@@ -217,16 +223,18 @@ constexpr inline auto KMemoryRegionType_DramPoolPartition =
217static_assert(KMemoryRegionType_DramPoolPartition.GetValue() == 223static_assert(KMemoryRegionType_DramPoolPartition.GetValue() ==
218 (0x26 | KMemoryRegionAttr_LinearMapped | KMemoryRegionAttr_NoUserMap)); 224 (0x26 | KMemoryRegionAttr_LinearMapped | KMemoryRegionAttr_NoUserMap));
219 225
220constexpr inline auto KMemoryRegionType_DramPoolManagement = 226// UNUSED: .Derive(4, 1);
221 KMemoryRegionType_DramPoolPartition.DeriveTransition(0, 2).DeriveTransition().SetAttribute( 227// UNUSED: .Derive(4, 2);
228constexpr inline const auto KMemoryRegionType_DramPoolManagement =
229 KMemoryRegionType_DramPoolPartition.Derive(4, 0).SetAttribute(
222 KMemoryRegionAttr_CarveoutProtected); 230 KMemoryRegionAttr_CarveoutProtected);
223constexpr inline auto KMemoryRegionType_DramUserPool = 231constexpr inline const auto KMemoryRegionType_DramUserPool =
224 KMemoryRegionType_DramPoolPartition.DeriveTransition(1, 2).DeriveTransition(); 232 KMemoryRegionType_DramPoolPartition.Derive(4, 3);
225static_assert(KMemoryRegionType_DramPoolManagement.GetValue() == 233static_assert(KMemoryRegionType_DramPoolManagement.GetValue() ==
226 (0x166 | KMemoryRegionAttr_LinearMapped | KMemoryRegionAttr_NoUserMap | 234 (0xE6 | KMemoryRegionAttr_LinearMapped | KMemoryRegionAttr_NoUserMap |
227 KMemoryRegionAttr_CarveoutProtected)); 235 KMemoryRegionAttr_CarveoutProtected));
228static_assert(KMemoryRegionType_DramUserPool.GetValue() == 236static_assert(KMemoryRegionType_DramUserPool.GetValue() ==
229 (0x1A6 | KMemoryRegionAttr_LinearMapped | KMemoryRegionAttr_NoUserMap)); 237 (0x266 | KMemoryRegionAttr_LinearMapped | KMemoryRegionAttr_NoUserMap));
230 238
231constexpr inline auto KMemoryRegionType_DramApplicationPool = 239constexpr inline auto KMemoryRegionType_DramApplicationPool =
232 KMemoryRegionType_DramUserPool.Derive(4, 0); 240 KMemoryRegionType_DramUserPool.Derive(4, 0);
@@ -237,60 +245,63 @@ constexpr inline auto KMemoryRegionType_DramSystemNonSecurePool =
237constexpr inline auto KMemoryRegionType_DramSystemPool = 245constexpr inline auto KMemoryRegionType_DramSystemPool =
238 KMemoryRegionType_DramUserPool.Derive(4, 3).SetAttribute(KMemoryRegionAttr_CarveoutProtected); 246 KMemoryRegionType_DramUserPool.Derive(4, 3).SetAttribute(KMemoryRegionAttr_CarveoutProtected);
239static_assert(KMemoryRegionType_DramApplicationPool.GetValue() == 247static_assert(KMemoryRegionType_DramApplicationPool.GetValue() ==
240 (0x7A6 | KMemoryRegionAttr_LinearMapped | KMemoryRegionAttr_NoUserMap)); 248 (0xE66 | KMemoryRegionAttr_LinearMapped | KMemoryRegionAttr_NoUserMap));
241static_assert(KMemoryRegionType_DramAppletPool.GetValue() == 249static_assert(KMemoryRegionType_DramAppletPool.GetValue() ==
242 (0xBA6 | KMemoryRegionAttr_LinearMapped | KMemoryRegionAttr_NoUserMap)); 250 (0x1666 | KMemoryRegionAttr_LinearMapped | KMemoryRegionAttr_NoUserMap));
243static_assert(KMemoryRegionType_DramSystemNonSecurePool.GetValue() == 251static_assert(KMemoryRegionType_DramSystemNonSecurePool.GetValue() ==
244 (0xDA6 | KMemoryRegionAttr_LinearMapped | KMemoryRegionAttr_NoUserMap)); 252 (0x1A66 | KMemoryRegionAttr_LinearMapped | KMemoryRegionAttr_NoUserMap));
245static_assert(KMemoryRegionType_DramSystemPool.GetValue() == 253static_assert(KMemoryRegionType_DramSystemPool.GetValue() ==
246 (0x13A6 | KMemoryRegionAttr_LinearMapped | KMemoryRegionAttr_NoUserMap | 254 (0x2666 | KMemoryRegionAttr_LinearMapped | KMemoryRegionAttr_NoUserMap |
247 KMemoryRegionAttr_CarveoutProtected)); 255 KMemoryRegionAttr_CarveoutProtected));
248 256
249constexpr inline auto KMemoryRegionType_VirtualDramHeapBase = 257constexpr inline auto KMemoryRegionType_VirtualDramHeapBase =
250 KMemoryRegionType_Dram.DeriveSparse(1, 3, 0); 258 KMemoryRegionType_Dram.DeriveSparse(1, 4, 0);
251constexpr inline auto KMemoryRegionType_VirtualDramKernelPtHeap = 259constexpr inline auto KMemoryRegionType_VirtualDramKernelPtHeap =
252 KMemoryRegionType_Dram.DeriveSparse(1, 3, 1); 260 KMemoryRegionType_Dram.DeriveSparse(1, 4, 1);
253constexpr inline auto KMemoryRegionType_VirtualDramKernelTraceBuffer = 261constexpr inline auto KMemoryRegionType_VirtualDramKernelTraceBuffer =
254 KMemoryRegionType_Dram.DeriveSparse(1, 3, 2); 262 KMemoryRegionType_Dram.DeriveSparse(1, 4, 2);
255static_assert(KMemoryRegionType_VirtualDramHeapBase.GetValue() == 0x1A); 263static_assert(KMemoryRegionType_VirtualDramHeapBase.GetValue() == 0x1A);
256static_assert(KMemoryRegionType_VirtualDramKernelPtHeap.GetValue() == 0x2A); 264static_assert(KMemoryRegionType_VirtualDramKernelPtHeap.GetValue() == 0x2A);
257static_assert(KMemoryRegionType_VirtualDramKernelTraceBuffer.GetValue() == 0x4A); 265static_assert(KMemoryRegionType_VirtualDramKernelTraceBuffer.GetValue() == 0x4A);
258 266
259// UNUSED: .DeriveSparse(2, 2, 0); 267// UNUSED: .Derive(4, 2);
260constexpr inline auto KMemoryRegionType_VirtualDramUnknownDebug = 268constexpr inline const auto KMemoryRegionType_VirtualDramUnknownDebug =
261 KMemoryRegionType_Dram.DeriveSparse(2, 2, 1); 269 KMemoryRegionType_Dram.Advance(2).Derive(4, 0);
262static_assert(KMemoryRegionType_VirtualDramUnknownDebug.GetValue() == (0x52)); 270constexpr inline const auto KMemoryRegionType_VirtualDramKernelSecureAppletMemory =
263 271 KMemoryRegionType_Dram.Advance(2).Derive(4, 1);
264constexpr inline auto KMemoryRegionType_VirtualDramKernelSecureAppletMemory = 272constexpr inline const auto KMemoryRegionType_VirtualDramKernelSecureUnknown =
265 KMemoryRegionType_Dram.DeriveSparse(3, 1, 0); 273 KMemoryRegionType_Dram.Advance(2).Derive(4, 3);
266static_assert(KMemoryRegionType_VirtualDramKernelSecureAppletMemory.GetValue() == (0x62)); 274static_assert(KMemoryRegionType_VirtualDramUnknownDebug.GetValue() == (0x32));
267 275static_assert(KMemoryRegionType_VirtualDramKernelSecureAppletMemory.GetValue() == (0x52));
268constexpr inline auto KMemoryRegionType_VirtualDramKernelInitPt = 276static_assert(KMemoryRegionType_VirtualDramKernelSecureUnknown.GetValue() == (0x92));
269 KMemoryRegionType_VirtualDramHeapBase.Derive(3, 0); 277
270constexpr inline auto KMemoryRegionType_VirtualDramPoolManagement = 278// UNUSED: .Derive(4, 3);
271 KMemoryRegionType_VirtualDramHeapBase.Derive(3, 1); 279constexpr inline const auto KMemoryRegionType_VirtualDramKernelInitPt =
272constexpr inline auto KMemoryRegionType_VirtualDramUserPool = 280 KMemoryRegionType_VirtualDramHeapBase.Derive(4, 0);
273 KMemoryRegionType_VirtualDramHeapBase.Derive(3, 2); 281constexpr inline const auto KMemoryRegionType_VirtualDramPoolManagement =
274static_assert(KMemoryRegionType_VirtualDramKernelInitPt.GetValue() == 0x19A); 282 KMemoryRegionType_VirtualDramHeapBase.Derive(4, 1);
275static_assert(KMemoryRegionType_VirtualDramPoolManagement.GetValue() == 0x29A); 283constexpr inline const auto KMemoryRegionType_VirtualDramUserPool =
276static_assert(KMemoryRegionType_VirtualDramUserPool.GetValue() == 0x31A); 284 KMemoryRegionType_VirtualDramHeapBase.Derive(4, 2);
285static_assert(KMemoryRegionType_VirtualDramKernelInitPt.GetValue() == 0x31A);
286static_assert(KMemoryRegionType_VirtualDramPoolManagement.GetValue() == 0x51A);
287static_assert(KMemoryRegionType_VirtualDramUserPool.GetValue() == 0x61A);
277 288
278// NOTE: For unknown reason, the pools are derived out-of-order here. 289// NOTE: For unknown reason, the pools are derived out-of-order here.
279// It's worth eventually trying to understand why Nintendo made this choice. 290// It's worth eventually trying to understand why Nintendo made this choice.
280// UNUSED: .Derive(6, 0); 291// UNUSED: .Derive(6, 0);
281// UNUSED: .Derive(6, 1); 292// UNUSED: .Derive(6, 1);
282constexpr inline auto KMemoryRegionType_VirtualDramAppletPool = 293constexpr inline const auto KMemoryRegionType_VirtualDramApplicationPool =
283 KMemoryRegionType_VirtualDramUserPool.Derive(6, 2); 294 KMemoryRegionType_VirtualDramUserPool.Derive(4, 0);
284constexpr inline auto KMemoryRegionType_VirtualDramApplicationPool = 295constexpr inline const auto KMemoryRegionType_VirtualDramAppletPool =
285 KMemoryRegionType_VirtualDramUserPool.Derive(6, 3); 296 KMemoryRegionType_VirtualDramUserPool.Derive(4, 1);
286constexpr inline auto KMemoryRegionType_VirtualDramSystemNonSecurePool = 297constexpr inline const auto KMemoryRegionType_VirtualDramSystemNonSecurePool =
287 KMemoryRegionType_VirtualDramUserPool.Derive(6, 4); 298 KMemoryRegionType_VirtualDramUserPool.Derive(4, 2);
288constexpr inline auto KMemoryRegionType_VirtualDramSystemPool = 299constexpr inline const auto KMemoryRegionType_VirtualDramSystemPool =
289 KMemoryRegionType_VirtualDramUserPool.Derive(6, 5); 300 KMemoryRegionType_VirtualDramUserPool.Derive(4, 3);
290static_assert(KMemoryRegionType_VirtualDramAppletPool.GetValue() == 0x1B1A); 301static_assert(KMemoryRegionType_VirtualDramApplicationPool.GetValue() == 0x361A);
291static_assert(KMemoryRegionType_VirtualDramApplicationPool.GetValue() == 0x271A); 302static_assert(KMemoryRegionType_VirtualDramAppletPool.GetValue() == 0x561A);
292static_assert(KMemoryRegionType_VirtualDramSystemNonSecurePool.GetValue() == 0x2B1A); 303static_assert(KMemoryRegionType_VirtualDramSystemNonSecurePool.GetValue() == 0x661A);
293static_assert(KMemoryRegionType_VirtualDramSystemPool.GetValue() == 0x331A); 304static_assert(KMemoryRegionType_VirtualDramSystemPool.GetValue() == 0x961A);
294 305
295constexpr inline auto KMemoryRegionType_ArchDeviceBase = 306constexpr inline auto KMemoryRegionType_ArchDeviceBase =
296 KMemoryRegionType_Kernel.DeriveTransition(0, 1).SetSparseOnly(); 307 KMemoryRegionType_Kernel.DeriveTransition(0, 1).SetSparseOnly();
@@ -354,12 +365,14 @@ constexpr inline auto KMemoryRegionType_KernelTemp =
354static_assert(KMemoryRegionType_KernelTemp.GetValue() == 0x31); 365static_assert(KMemoryRegionType_KernelTemp.GetValue() == 0x31);
355 366
356constexpr KMemoryRegionType GetTypeForVirtualLinearMapping(u32 type_id) { 367constexpr KMemoryRegionType GetTypeForVirtualLinearMapping(u32 type_id) {
357 if (KMemoryRegionType_KernelTraceBuffer.IsAncestorOf(type_id)) { 368 if (KMemoryRegionType_DramKernelPtHeap.IsAncestorOf(type_id)) {
358 return KMemoryRegionType_VirtualDramKernelTraceBuffer;
359 } else if (KMemoryRegionType_DramKernelPtHeap.IsAncestorOf(type_id)) {
360 return KMemoryRegionType_VirtualDramKernelPtHeap; 369 return KMemoryRegionType_VirtualDramKernelPtHeap;
361 } else if (KMemoryRegionType_DramKernelSecureAppletMemory.IsAncestorOf(type_id)) { 370 } else if (KMemoryRegionType_DramKernelSecureAppletMemory.IsAncestorOf(type_id)) {
362 return KMemoryRegionType_VirtualDramKernelSecureAppletMemory; 371 return KMemoryRegionType_VirtualDramKernelSecureAppletMemory;
372 } else if (KMemoryRegionType_DramKernelSecureUnknown.IsAncestorOf(type_id)) {
373 return KMemoryRegionType_VirtualDramKernelSecureUnknown;
374 } else if (KMemoryRegionType_KernelTraceBuffer.IsAncestorOf(type_id)) {
375 return KMemoryRegionType_VirtualDramKernelTraceBuffer;
363 } else if ((type_id | KMemoryRegionAttr_ShouldKernelMap) == type_id) { 376 } else if ((type_id | KMemoryRegionAttr_ShouldKernelMap) == type_id) {
364 return KMemoryRegionType_VirtualDramUnknownDebug; 377 return KMemoryRegionType_VirtualDramUnknownDebug;
365 } else { 378 } else {
diff --git a/src/core/hle/kernel/k_page_group.h b/src/core/hle/kernel/k_page_group.h
index b32909f05..de9d63a8d 100644
--- a/src/core/hle/kernel/k_page_group.h
+++ b/src/core/hle/kernel/k_page_group.h
@@ -183,12 +183,17 @@ private:
183 183
184class KScopedPageGroup { 184class KScopedPageGroup {
185public: 185public:
186 explicit KScopedPageGroup(const KPageGroup* gp) : m_pg(gp) { 186 explicit KScopedPageGroup(const KPageGroup* gp, bool not_first = true) : m_pg(gp) {
187 if (m_pg) { 187 if (m_pg) {
188 m_pg->Open(); 188 if (not_first) {
189 m_pg->Open();
190 } else {
191 m_pg->OpenFirst();
192 }
189 } 193 }
190 } 194 }
191 explicit KScopedPageGroup(const KPageGroup& gp) : KScopedPageGroup(std::addressof(gp)) {} 195 explicit KScopedPageGroup(const KPageGroup& gp, bool not_first = true)
196 : KScopedPageGroup(std::addressof(gp), not_first) {}
192 ~KScopedPageGroup() { 197 ~KScopedPageGroup() {
193 if (m_pg) { 198 if (m_pg) {
194 m_pg->Close(); 199 m_pg->Close();
diff --git a/src/core/hle/kernel/k_page_table.cpp b/src/core/hle/kernel/k_page_table.cpp
index 0b0cef984..217ccbae3 100644
--- a/src/core/hle/kernel/k_page_table.cpp
+++ b/src/core/hle/kernel/k_page_table.cpp
@@ -505,7 +505,7 @@ Result KPageTable::UnmapCodeMemory(KProcessAddress dst_address, KProcessAddress
505 R_TRY(this->CheckMemoryStateContiguous( 505 R_TRY(this->CheckMemoryStateContiguous(
506 std::addressof(num_dst_allocator_blocks), dst_address, size, KMemoryState::FlagCanCodeAlias, 506 std::addressof(num_dst_allocator_blocks), dst_address, size, KMemoryState::FlagCanCodeAlias,
507 KMemoryState::FlagCanCodeAlias, KMemoryPermission::None, KMemoryPermission::None, 507 KMemoryState::FlagCanCodeAlias, KMemoryPermission::None, KMemoryPermission::None,
508 KMemoryAttribute::All, KMemoryAttribute::None)); 508 KMemoryAttribute::All & ~KMemoryAttribute::PermissionLocked, KMemoryAttribute::None));
509 509
510 // Determine whether any pages being unmapped are code. 510 // Determine whether any pages being unmapped are code.
511 bool any_code_pages = false; 511 bool any_code_pages = false;
@@ -1724,29 +1724,43 @@ Result KPageTable::MapPhysicalMemory(KProcessAddress address, size_t size) {
1724 PageSize; 1724 PageSize;
1725 1725
1726 // While we have pages to map, map them. 1726 // While we have pages to map, map them.
1727 while (map_pages > 0) { 1727 {
1728 // Check if we're at the end of the physical block. 1728 // Create a page group for the current mapping range.
1729 if (pg_pages == 0) { 1729 KPageGroup cur_pg(m_kernel, m_block_info_manager);
1730 // Ensure there are more pages to map. 1730 {
1731 ASSERT(pg_it != pg.end()); 1731 ON_RESULT_FAILURE_2 {
1732 1732 cur_pg.OpenFirst();
1733 // Advance our physical block. 1733 cur_pg.Close();
1734 ++pg_it; 1734 };
1735 pg_phys_addr = pg_it->GetAddress(); 1735
1736 pg_pages = pg_it->GetNumPages(); 1736 size_t remain_pages = map_pages;
1737 while (remain_pages > 0) {
1738 // Check if we're at the end of the physical block.
1739 if (pg_pages == 0) {
1740 // Ensure there are more pages to map.
1741 ASSERT(pg_it != pg.end());
1742
1743 // Advance our physical block.
1744 ++pg_it;
1745 pg_phys_addr = pg_it->GetAddress();
1746 pg_pages = pg_it->GetNumPages();
1747 }
1748
1749 // Add whatever we can to the current block.
1750 const size_t cur_pages = std::min(pg_pages, remain_pages);
1751 R_TRY(cur_pg.AddBlock(pg_phys_addr +
1752 ((pg_pages - cur_pages) * PageSize),
1753 cur_pages));
1754
1755 // Advance.
1756 remain_pages -= cur_pages;
1757 pg_pages -= cur_pages;
1758 }
1737 } 1759 }
1738 1760
1739 // Map whatever we can. 1761 // Map the pages.
1740 const size_t cur_pages = std::min(pg_pages, map_pages); 1762 R_TRY(this->Operate(cur_address, map_pages, cur_pg,
1741 R_TRY(Operate(cur_address, cur_pages, KMemoryPermission::UserReadWrite, 1763 OperationType::MapFirstGroup));
1742 OperationType::MapFirst, pg_phys_addr));
1743
1744 // Advance.
1745 cur_address += cur_pages * PageSize;
1746 map_pages -= cur_pages;
1747
1748 pg_phys_addr += cur_pages * PageSize;
1749 pg_pages -= cur_pages;
1750 } 1764 }
1751 } 1765 }
1752 1766
@@ -1770,7 +1784,11 @@ Result KPageTable::MapPhysicalMemory(KProcessAddress address, size_t size) {
1770 m_memory_block_manager.UpdateIfMatch( 1784 m_memory_block_manager.UpdateIfMatch(
1771 std::addressof(allocator), address, size / PageSize, KMemoryState::Free, 1785 std::addressof(allocator), address, size / PageSize, KMemoryState::Free,
1772 KMemoryPermission::None, KMemoryAttribute::None, KMemoryState::Normal, 1786 KMemoryPermission::None, KMemoryAttribute::None, KMemoryState::Normal,
1773 KMemoryPermission::UserReadWrite, KMemoryAttribute::None); 1787 KMemoryPermission::UserReadWrite, KMemoryAttribute::None,
1788 address == this->GetAliasRegionStart()
1789 ? KMemoryBlockDisableMergeAttribute::Normal
1790 : KMemoryBlockDisableMergeAttribute::None,
1791 KMemoryBlockDisableMergeAttribute::None);
1774 1792
1775 R_SUCCEED(); 1793 R_SUCCEED();
1776 } 1794 }
@@ -1868,6 +1886,13 @@ Result KPageTable::UnmapPhysicalMemory(KProcessAddress address, size_t size) {
1868 1886
1869 // Iterate over the memory, unmapping as we go. 1887 // Iterate over the memory, unmapping as we go.
1870 auto it = m_memory_block_manager.FindIterator(cur_address); 1888 auto it = m_memory_block_manager.FindIterator(cur_address);
1889
1890 const auto clear_merge_attr =
1891 (it->GetState() == KMemoryState::Normal &&
1892 it->GetAddress() == this->GetAliasRegionStart() && it->GetAddress() == address)
1893 ? KMemoryBlockDisableMergeAttribute::Normal
1894 : KMemoryBlockDisableMergeAttribute::None;
1895
1871 while (true) { 1896 while (true) {
1872 // Check that the iterator is valid. 1897 // Check that the iterator is valid.
1873 ASSERT(it != m_memory_block_manager.end()); 1898 ASSERT(it != m_memory_block_manager.end());
@@ -1905,7 +1930,7 @@ Result KPageTable::UnmapPhysicalMemory(KProcessAddress address, size_t size) {
1905 m_memory_block_manager.Update(std::addressof(allocator), address, size / PageSize, 1930 m_memory_block_manager.Update(std::addressof(allocator), address, size / PageSize,
1906 KMemoryState::Free, KMemoryPermission::None, 1931 KMemoryState::Free, KMemoryPermission::None,
1907 KMemoryAttribute::None, KMemoryBlockDisableMergeAttribute::None, 1932 KMemoryAttribute::None, KMemoryBlockDisableMergeAttribute::None,
1908 KMemoryBlockDisableMergeAttribute::None); 1933 clear_merge_attr);
1909 1934
1910 // We succeeded. 1935 // We succeeded.
1911 R_SUCCEED(); 1936 R_SUCCEED();
@@ -2379,8 +2404,7 @@ Result KPageTable::MapPageGroup(KProcessAddress* out_addr, const KPageGroup& pg,
2379 KScopedPageTableUpdater updater(this); 2404 KScopedPageTableUpdater updater(this);
2380 2405
2381 // Perform mapping operation. 2406 // Perform mapping operation.
2382 const KPageProperties properties = {perm, state == KMemoryState::Io, false, 2407 const KPageProperties properties = {perm, false, false, DisableMergeAttribute::DisableHead};
2383 DisableMergeAttribute::DisableHead};
2384 R_TRY(this->MapPageGroupImpl(updater.GetPageList(), addr, pg, properties, false)); 2408 R_TRY(this->MapPageGroupImpl(updater.GetPageList(), addr, pg, properties, false));
2385 2409
2386 // Update the blocks. 2410 // Update the blocks.
@@ -2422,8 +2446,7 @@ Result KPageTable::MapPageGroup(KProcessAddress addr, const KPageGroup& pg, KMem
2422 KScopedPageTableUpdater updater(this); 2446 KScopedPageTableUpdater updater(this);
2423 2447
2424 // Perform mapping operation. 2448 // Perform mapping operation.
2425 const KPageProperties properties = {perm, state == KMemoryState::Io, false, 2449 const KPageProperties properties = {perm, false, false, DisableMergeAttribute::DisableHead};
2426 DisableMergeAttribute::DisableHead};
2427 R_TRY(this->MapPageGroupImpl(updater.GetPageList(), addr, pg, properties, false)); 2450 R_TRY(this->MapPageGroupImpl(updater.GetPageList(), addr, pg, properties, false));
2428 2451
2429 // Update the blocks. 2452 // Update the blocks.
@@ -2652,11 +2675,18 @@ Result KPageTable::SetMemoryAttribute(KProcessAddress addr, size_t size, u32 mas
2652 size_t num_allocator_blocks; 2675 size_t num_allocator_blocks;
2653 constexpr auto AttributeTestMask = 2676 constexpr auto AttributeTestMask =
2654 ~(KMemoryAttribute::SetMask | KMemoryAttribute::DeviceShared); 2677 ~(KMemoryAttribute::SetMask | KMemoryAttribute::DeviceShared);
2655 R_TRY(this->CheckMemoryState( 2678 const KMemoryState state_test_mask =
2656 std::addressof(old_state), std::addressof(old_perm), std::addressof(old_attr), 2679 static_cast<KMemoryState>(((mask & static_cast<u32>(KMemoryAttribute::Uncached))
2657 std::addressof(num_allocator_blocks), addr, size, KMemoryState::FlagCanChangeAttribute, 2680 ? static_cast<u32>(KMemoryState::FlagCanChangeAttribute)
2658 KMemoryState::FlagCanChangeAttribute, KMemoryPermission::None, KMemoryPermission::None, 2681 : 0) |
2659 AttributeTestMask, KMemoryAttribute::None, ~AttributeTestMask)); 2682 ((mask & static_cast<u32>(KMemoryAttribute::PermissionLocked))
2683 ? static_cast<u32>(KMemoryState::FlagCanPermissionLock)
2684 : 0));
2685 R_TRY(this->CheckMemoryState(std::addressof(old_state), std::addressof(old_perm),
2686 std::addressof(old_attr), std::addressof(num_allocator_blocks),
2687 addr, size, state_test_mask, state_test_mask,
2688 KMemoryPermission::None, KMemoryPermission::None,
2689 AttributeTestMask, KMemoryAttribute::None, ~AttributeTestMask));
2660 2690
2661 // Create an update allocator. 2691 // Create an update allocator.
2662 Result allocator_result{ResultSuccess}; 2692 Result allocator_result{ResultSuccess};
@@ -2664,18 +2694,17 @@ Result KPageTable::SetMemoryAttribute(KProcessAddress addr, size_t size, u32 mas
2664 m_memory_block_slab_manager, num_allocator_blocks); 2694 m_memory_block_slab_manager, num_allocator_blocks);
2665 R_TRY(allocator_result); 2695 R_TRY(allocator_result);
2666 2696
2667 // Determine the new attribute. 2697 // If we need to, perform a change attribute operation.
2668 const KMemoryAttribute new_attr = 2698 if (True(KMemoryAttribute::Uncached & static_cast<KMemoryAttribute>(mask))) {
2669 static_cast<KMemoryAttribute>(((old_attr & static_cast<KMemoryAttribute>(~mask)) | 2699 // Perform operation.
2670 static_cast<KMemoryAttribute>(attr & mask))); 2700 R_TRY(this->Operate(addr, num_pages, old_perm,
2671 2701 OperationType::ChangePermissionsAndRefreshAndFlush, 0));
2672 // Perform operation. 2702 }
2673 this->Operate(addr, num_pages, old_perm, OperationType::ChangePermissionsAndRefresh);
2674 2703
2675 // Update the blocks. 2704 // Update the blocks.
2676 m_memory_block_manager.Update(std::addressof(allocator), addr, num_pages, old_state, old_perm, 2705 m_memory_block_manager.UpdateAttribute(std::addressof(allocator), addr, num_pages,
2677 new_attr, KMemoryBlockDisableMergeAttribute::None, 2706 static_cast<KMemoryAttribute>(mask),
2678 KMemoryBlockDisableMergeAttribute::None); 2707 static_cast<KMemoryAttribute>(attr));
2679 2708
2680 R_SUCCEED(); 2709 R_SUCCEED();
2681} 2710}
@@ -2863,7 +2892,8 @@ Result KPageTable::LockForMapDeviceAddressSpace(bool* out_is_io, KProcessAddress
2863 &KMemoryBlock::ShareToDevice, KMemoryPermission::None); 2892 &KMemoryBlock::ShareToDevice, KMemoryPermission::None);
2864 2893
2865 // Set whether the locked memory was io. 2894 // Set whether the locked memory was io.
2866 *out_is_io = old_state == KMemoryState::Io; 2895 *out_is_io =
2896 static_cast<Svc::MemoryState>(old_state & KMemoryState::Mask) == Svc::MemoryState::Io;
2867 2897
2868 R_SUCCEED(); 2898 R_SUCCEED();
2869} 2899}
@@ -3021,9 +3051,10 @@ Result KPageTable::Operate(KProcessAddress addr, size_t num_pages, const KPageGr
3021 ASSERT(num_pages == page_group.GetNumPages()); 3051 ASSERT(num_pages == page_group.GetNumPages());
3022 3052
3023 switch (operation) { 3053 switch (operation) {
3024 case OperationType::MapGroup: { 3054 case OperationType::MapGroup:
3055 case OperationType::MapFirstGroup: {
3025 // We want to maintain a new reference to every page in the group. 3056 // We want to maintain a new reference to every page in the group.
3026 KScopedPageGroup spg(page_group); 3057 KScopedPageGroup spg(page_group, operation != OperationType::MapFirstGroup);
3027 3058
3028 for (const auto& node : page_group) { 3059 for (const auto& node : page_group) {
3029 const size_t size{node.GetNumPages() * PageSize}; 3060 const size_t size{node.GetNumPages() * PageSize};
@@ -3065,7 +3096,6 @@ Result KPageTable::Operate(KProcessAddress addr, size_t num_pages, KMemoryPermis
3065 m_memory->UnmapRegion(*m_page_table_impl, addr, num_pages * PageSize); 3096 m_memory->UnmapRegion(*m_page_table_impl, addr, num_pages * PageSize);
3066 break; 3097 break;
3067 } 3098 }
3068 case OperationType::MapFirst:
3069 case OperationType::Map: { 3099 case OperationType::Map: {
3070 ASSERT(map_addr); 3100 ASSERT(map_addr);
3071 ASSERT(Common::IsAligned(GetInteger(map_addr), PageSize)); 3101 ASSERT(Common::IsAligned(GetInteger(map_addr), PageSize));
@@ -3073,11 +3103,7 @@ Result KPageTable::Operate(KProcessAddress addr, size_t num_pages, KMemoryPermis
3073 3103
3074 // Open references to pages, if we should. 3104 // Open references to pages, if we should.
3075 if (IsHeapPhysicalAddress(m_kernel.MemoryLayout(), map_addr)) { 3105 if (IsHeapPhysicalAddress(m_kernel.MemoryLayout(), map_addr)) {
3076 if (operation == OperationType::MapFirst) { 3106 m_kernel.MemoryManager().Open(map_addr, num_pages);
3077 m_kernel.MemoryManager().OpenFirst(map_addr, num_pages);
3078 } else {
3079 m_kernel.MemoryManager().Open(map_addr, num_pages);
3080 }
3081 } 3107 }
3082 break; 3108 break;
3083 } 3109 }
@@ -3087,6 +3113,7 @@ Result KPageTable::Operate(KProcessAddress addr, size_t num_pages, KMemoryPermis
3087 } 3113 }
3088 case OperationType::ChangePermissions: 3114 case OperationType::ChangePermissions:
3089 case OperationType::ChangePermissionsAndRefresh: 3115 case OperationType::ChangePermissionsAndRefresh:
3116 case OperationType::ChangePermissionsAndRefreshAndFlush:
3090 break; 3117 break;
3091 default: 3118 default:
3092 ASSERT(false); 3119 ASSERT(false);
@@ -3106,79 +3133,79 @@ void KPageTable::FinalizeUpdate(PageLinkedList* page_list) {
3106 } 3133 }
3107} 3134}
3108 3135
3109KProcessAddress KPageTable::GetRegionAddress(KMemoryState state) const { 3136KProcessAddress KPageTable::GetRegionAddress(Svc::MemoryState state) const {
3110 switch (state) { 3137 switch (state) {
3111 case KMemoryState::Free: 3138 case Svc::MemoryState::Free:
3112 case KMemoryState::Kernel: 3139 case Svc::MemoryState::Kernel:
3113 return m_address_space_start; 3140 return m_address_space_start;
3114 case KMemoryState::Normal: 3141 case Svc::MemoryState::Normal:
3115 return m_heap_region_start; 3142 return m_heap_region_start;
3116 case KMemoryState::Ipc: 3143 case Svc::MemoryState::Ipc:
3117 case KMemoryState::NonSecureIpc: 3144 case Svc::MemoryState::NonSecureIpc:
3118 case KMemoryState::NonDeviceIpc: 3145 case Svc::MemoryState::NonDeviceIpc:
3119 return m_alias_region_start; 3146 return m_alias_region_start;
3120 case KMemoryState::Stack: 3147 case Svc::MemoryState::Stack:
3121 return m_stack_region_start; 3148 return m_stack_region_start;
3122 case KMemoryState::Static: 3149 case Svc::MemoryState::Static:
3123 case KMemoryState::ThreadLocal: 3150 case Svc::MemoryState::ThreadLocal:
3124 return m_kernel_map_region_start; 3151 return m_kernel_map_region_start;
3125 case KMemoryState::Io: 3152 case Svc::MemoryState::Io:
3126 case KMemoryState::Shared: 3153 case Svc::MemoryState::Shared:
3127 case KMemoryState::AliasCode: 3154 case Svc::MemoryState::AliasCode:
3128 case KMemoryState::AliasCodeData: 3155 case Svc::MemoryState::AliasCodeData:
3129 case KMemoryState::Transfered: 3156 case Svc::MemoryState::Transfered:
3130 case KMemoryState::SharedTransfered: 3157 case Svc::MemoryState::SharedTransfered:
3131 case KMemoryState::SharedCode: 3158 case Svc::MemoryState::SharedCode:
3132 case KMemoryState::GeneratedCode: 3159 case Svc::MemoryState::GeneratedCode:
3133 case KMemoryState::CodeOut: 3160 case Svc::MemoryState::CodeOut:
3134 case KMemoryState::Coverage: 3161 case Svc::MemoryState::Coverage:
3135 case KMemoryState::Insecure: 3162 case Svc::MemoryState::Insecure:
3136 return m_alias_code_region_start; 3163 return m_alias_code_region_start;
3137 case KMemoryState::Code: 3164 case Svc::MemoryState::Code:
3138 case KMemoryState::CodeData: 3165 case Svc::MemoryState::CodeData:
3139 return m_code_region_start; 3166 return m_code_region_start;
3140 default: 3167 default:
3141 UNREACHABLE(); 3168 UNREACHABLE();
3142 } 3169 }
3143} 3170}
3144 3171
3145size_t KPageTable::GetRegionSize(KMemoryState state) const { 3172size_t KPageTable::GetRegionSize(Svc::MemoryState state) const {
3146 switch (state) { 3173 switch (state) {
3147 case KMemoryState::Free: 3174 case Svc::MemoryState::Free:
3148 case KMemoryState::Kernel: 3175 case Svc::MemoryState::Kernel:
3149 return m_address_space_end - m_address_space_start; 3176 return m_address_space_end - m_address_space_start;
3150 case KMemoryState::Normal: 3177 case Svc::MemoryState::Normal:
3151 return m_heap_region_end - m_heap_region_start; 3178 return m_heap_region_end - m_heap_region_start;
3152 case KMemoryState::Ipc: 3179 case Svc::MemoryState::Ipc:
3153 case KMemoryState::NonSecureIpc: 3180 case Svc::MemoryState::NonSecureIpc:
3154 case KMemoryState::NonDeviceIpc: 3181 case Svc::MemoryState::NonDeviceIpc:
3155 return m_alias_region_end - m_alias_region_start; 3182 return m_alias_region_end - m_alias_region_start;
3156 case KMemoryState::Stack: 3183 case Svc::MemoryState::Stack:
3157 return m_stack_region_end - m_stack_region_start; 3184 return m_stack_region_end - m_stack_region_start;
3158 case KMemoryState::Static: 3185 case Svc::MemoryState::Static:
3159 case KMemoryState::ThreadLocal: 3186 case Svc::MemoryState::ThreadLocal:
3160 return m_kernel_map_region_end - m_kernel_map_region_start; 3187 return m_kernel_map_region_end - m_kernel_map_region_start;
3161 case KMemoryState::Io: 3188 case Svc::MemoryState::Io:
3162 case KMemoryState::Shared: 3189 case Svc::MemoryState::Shared:
3163 case KMemoryState::AliasCode: 3190 case Svc::MemoryState::AliasCode:
3164 case KMemoryState::AliasCodeData: 3191 case Svc::MemoryState::AliasCodeData:
3165 case KMemoryState::Transfered: 3192 case Svc::MemoryState::Transfered:
3166 case KMemoryState::SharedTransfered: 3193 case Svc::MemoryState::SharedTransfered:
3167 case KMemoryState::SharedCode: 3194 case Svc::MemoryState::SharedCode:
3168 case KMemoryState::GeneratedCode: 3195 case Svc::MemoryState::GeneratedCode:
3169 case KMemoryState::CodeOut: 3196 case Svc::MemoryState::CodeOut:
3170 case KMemoryState::Coverage: 3197 case Svc::MemoryState::Coverage:
3171 case KMemoryState::Insecure: 3198 case Svc::MemoryState::Insecure:
3172 return m_alias_code_region_end - m_alias_code_region_start; 3199 return m_alias_code_region_end - m_alias_code_region_start;
3173 case KMemoryState::Code: 3200 case Svc::MemoryState::Code:
3174 case KMemoryState::CodeData: 3201 case Svc::MemoryState::CodeData:
3175 return m_code_region_end - m_code_region_start; 3202 return m_code_region_end - m_code_region_start;
3176 default: 3203 default:
3177 UNREACHABLE(); 3204 UNREACHABLE();
3178 } 3205 }
3179} 3206}
3180 3207
3181bool KPageTable::CanContain(KProcessAddress addr, size_t size, KMemoryState state) const { 3208bool KPageTable::CanContain(KProcessAddress addr, size_t size, Svc::MemoryState state) const {
3182 const KProcessAddress end = addr + size; 3209 const KProcessAddress end = addr + size;
3183 const KProcessAddress last = end - 1; 3210 const KProcessAddress last = end - 1;
3184 3211
@@ -3192,32 +3219,32 @@ bool KPageTable::CanContain(KProcessAddress addr, size_t size, KMemoryState stat
3192 const bool is_in_alias = !(end <= m_alias_region_start || m_alias_region_end <= addr || 3219 const bool is_in_alias = !(end <= m_alias_region_start || m_alias_region_end <= addr ||
3193 m_alias_region_start == m_alias_region_end); 3220 m_alias_region_start == m_alias_region_end);
3194 switch (state) { 3221 switch (state) {
3195 case KMemoryState::Free: 3222 case Svc::MemoryState::Free:
3196 case KMemoryState::Kernel: 3223 case Svc::MemoryState::Kernel:
3197 return is_in_region; 3224 return is_in_region;
3198 case KMemoryState::Io: 3225 case Svc::MemoryState::Io:
3199 case KMemoryState::Static: 3226 case Svc::MemoryState::Static:
3200 case KMemoryState::Code: 3227 case Svc::MemoryState::Code:
3201 case KMemoryState::CodeData: 3228 case Svc::MemoryState::CodeData:
3202 case KMemoryState::Shared: 3229 case Svc::MemoryState::Shared:
3203 case KMemoryState::AliasCode: 3230 case Svc::MemoryState::AliasCode:
3204 case KMemoryState::AliasCodeData: 3231 case Svc::MemoryState::AliasCodeData:
3205 case KMemoryState::Stack: 3232 case Svc::MemoryState::Stack:
3206 case KMemoryState::ThreadLocal: 3233 case Svc::MemoryState::ThreadLocal:
3207 case KMemoryState::Transfered: 3234 case Svc::MemoryState::Transfered:
3208 case KMemoryState::SharedTransfered: 3235 case Svc::MemoryState::SharedTransfered:
3209 case KMemoryState::SharedCode: 3236 case Svc::MemoryState::SharedCode:
3210 case KMemoryState::GeneratedCode: 3237 case Svc::MemoryState::GeneratedCode:
3211 case KMemoryState::CodeOut: 3238 case Svc::MemoryState::CodeOut:
3212 case KMemoryState::Coverage: 3239 case Svc::MemoryState::Coverage:
3213 case KMemoryState::Insecure: 3240 case Svc::MemoryState::Insecure:
3214 return is_in_region && !is_in_heap && !is_in_alias; 3241 return is_in_region && !is_in_heap && !is_in_alias;
3215 case KMemoryState::Normal: 3242 case Svc::MemoryState::Normal:
3216 ASSERT(is_in_heap); 3243 ASSERT(is_in_heap);
3217 return is_in_region && !is_in_alias; 3244 return is_in_region && !is_in_alias;
3218 case KMemoryState::Ipc: 3245 case Svc::MemoryState::Ipc:
3219 case KMemoryState::NonSecureIpc: 3246 case Svc::MemoryState::NonSecureIpc:
3220 case KMemoryState::NonDeviceIpc: 3247 case Svc::MemoryState::NonDeviceIpc:
3221 ASSERT(is_in_alias); 3248 ASSERT(is_in_alias);
3222 return is_in_region && !is_in_heap; 3249 return is_in_region && !is_in_heap;
3223 default: 3250 default:
@@ -3281,21 +3308,16 @@ Result KPageTable::CheckMemoryStateContiguous(size_t* out_blocks_needed, KProces
3281 3308
3282Result KPageTable::CheckMemoryState(KMemoryState* out_state, KMemoryPermission* out_perm, 3309Result KPageTable::CheckMemoryState(KMemoryState* out_state, KMemoryPermission* out_perm,
3283 KMemoryAttribute* out_attr, size_t* out_blocks_needed, 3310 KMemoryAttribute* out_attr, size_t* out_blocks_needed,
3284 KProcessAddress addr, size_t size, KMemoryState state_mask, 3311 KMemoryBlockManager::const_iterator it,
3312 KProcessAddress last_addr, KMemoryState state_mask,
3285 KMemoryState state, KMemoryPermission perm_mask, 3313 KMemoryState state, KMemoryPermission perm_mask,
3286 KMemoryPermission perm, KMemoryAttribute attr_mask, 3314 KMemoryPermission perm, KMemoryAttribute attr_mask,
3287 KMemoryAttribute attr, KMemoryAttribute ignore_attr) const { 3315 KMemoryAttribute attr, KMemoryAttribute ignore_attr) const {
3288 ASSERT(this->IsLockedByCurrentThread()); 3316 ASSERT(this->IsLockedByCurrentThread());
3289 3317
3290 // Get information about the first block. 3318 // Get information about the first block.
3291 const KProcessAddress last_addr = addr + size - 1;
3292 KMemoryBlockManager::const_iterator it = m_memory_block_manager.FindIterator(addr);
3293 KMemoryInfo info = it->GetMemoryInfo(); 3319 KMemoryInfo info = it->GetMemoryInfo();
3294 3320
3295 // If the start address isn't aligned, we need a block.
3296 const size_t blocks_for_start_align =
3297 (Common::AlignDown(GetInteger(addr), PageSize) != info.GetAddress()) ? 1 : 0;
3298
3299 // Validate all blocks in the range have correct state. 3321 // Validate all blocks in the range have correct state.
3300 const KMemoryState first_state = info.m_state; 3322 const KMemoryState first_state = info.m_state;
3301 const KMemoryPermission first_perm = info.m_permission; 3323 const KMemoryPermission first_perm = info.m_permission;
@@ -3321,10 +3343,6 @@ Result KPageTable::CheckMemoryState(KMemoryState* out_state, KMemoryPermission*
3321 info = it->GetMemoryInfo(); 3343 info = it->GetMemoryInfo();
3322 } 3344 }
3323 3345
3324 // If the end address isn't aligned, we need a block.
3325 const size_t blocks_for_end_align =
3326 (Common::AlignUp(GetInteger(addr) + size, PageSize) != info.GetEndAddress()) ? 1 : 0;
3327
3328 // Write output state. 3346 // Write output state.
3329 if (out_state != nullptr) { 3347 if (out_state != nullptr) {
3330 *out_state = first_state; 3348 *out_state = first_state;
@@ -3335,9 +3353,39 @@ Result KPageTable::CheckMemoryState(KMemoryState* out_state, KMemoryPermission*
3335 if (out_attr != nullptr) { 3353 if (out_attr != nullptr) {
3336 *out_attr = static_cast<KMemoryAttribute>(first_attr & ~ignore_attr); 3354 *out_attr = static_cast<KMemoryAttribute>(first_attr & ~ignore_attr);
3337 } 3355 }
3356
3357 // If the end address isn't aligned, we need a block.
3338 if (out_blocks_needed != nullptr) { 3358 if (out_blocks_needed != nullptr) {
3339 *out_blocks_needed = blocks_for_start_align + blocks_for_end_align; 3359 const size_t blocks_for_end_align =
3360 (Common::AlignDown(GetInteger(last_addr), PageSize) + PageSize != info.GetEndAddress())
3361 ? 1
3362 : 0;
3363 *out_blocks_needed = blocks_for_end_align;
3364 }
3365
3366 R_SUCCEED();
3367}
3368
3369Result KPageTable::CheckMemoryState(KMemoryState* out_state, KMemoryPermission* out_perm,
3370 KMemoryAttribute* out_attr, size_t* out_blocks_needed,
3371 KProcessAddress addr, size_t size, KMemoryState state_mask,
3372 KMemoryState state, KMemoryPermission perm_mask,
3373 KMemoryPermission perm, KMemoryAttribute attr_mask,
3374 KMemoryAttribute attr, KMemoryAttribute ignore_attr) const {
3375 ASSERT(this->IsLockedByCurrentThread());
3376
3377 // Check memory state.
3378 const KProcessAddress last_addr = addr + size - 1;
3379 KMemoryBlockManager::const_iterator it = m_memory_block_manager.FindIterator(addr);
3380 R_TRY(this->CheckMemoryState(out_state, out_perm, out_attr, out_blocks_needed, it, last_addr,
3381 state_mask, state, perm_mask, perm, attr_mask, attr, ignore_attr));
3382
3383 // If the start address isn't aligned, we need a block.
3384 if (out_blocks_needed != nullptr &&
3385 Common::AlignDown(GetInteger(addr), PageSize) != it->GetAddress()) {
3386 ++(*out_blocks_needed);
3340 } 3387 }
3388
3341 R_SUCCEED(); 3389 R_SUCCEED();
3342} 3390}
3343 3391
diff --git a/src/core/hle/kernel/k_page_table.h b/src/core/hle/kernel/k_page_table.h
index 7da675f27..3d64b6fb0 100644
--- a/src/core/hle/kernel/k_page_table.h
+++ b/src/core/hle/kernel/k_page_table.h
@@ -126,8 +126,6 @@ public:
126 return m_block_info_manager; 126 return m_block_info_manager;
127 } 127 }
128 128
129 bool CanContain(KProcessAddress addr, size_t size, KMemoryState state) const;
130
131 Result MapPages(KProcessAddress* out_addr, size_t num_pages, size_t alignment, 129 Result MapPages(KProcessAddress* out_addr, size_t num_pages, size_t alignment,
132 KPhysicalAddress phys_addr, KProcessAddress region_start, 130 KPhysicalAddress phys_addr, KProcessAddress region_start,
133 size_t region_num_pages, KMemoryState state, KMemoryPermission perm) { 131 size_t region_num_pages, KMemoryState state, KMemoryPermission perm) {
@@ -162,6 +160,21 @@ public:
162 void RemapPageGroup(PageLinkedList* page_list, KProcessAddress address, size_t size, 160 void RemapPageGroup(PageLinkedList* page_list, KProcessAddress address, size_t size,
163 const KPageGroup& pg); 161 const KPageGroup& pg);
164 162
163 KProcessAddress GetRegionAddress(Svc::MemoryState state) const;
164 size_t GetRegionSize(Svc::MemoryState state) const;
165 bool CanContain(KProcessAddress addr, size_t size, Svc::MemoryState state) const;
166
167 KProcessAddress GetRegionAddress(KMemoryState state) const {
168 return this->GetRegionAddress(static_cast<Svc::MemoryState>(state & KMemoryState::Mask));
169 }
170 size_t GetRegionSize(KMemoryState state) const {
171 return this->GetRegionSize(static_cast<Svc::MemoryState>(state & KMemoryState::Mask));
172 }
173 bool CanContain(KProcessAddress addr, size_t size, KMemoryState state) const {
174 return this->CanContain(addr, size,
175 static_cast<Svc::MemoryState>(state & KMemoryState::Mask));
176 }
177
165protected: 178protected:
166 struct PageLinkedList { 179 struct PageLinkedList {
167 private: 180 private:
@@ -204,12 +217,13 @@ protected:
204private: 217private:
205 enum class OperationType : u32 { 218 enum class OperationType : u32 {
206 Map = 0, 219 Map = 0,
207 MapFirst = 1, 220 MapGroup = 1,
208 MapGroup = 2, 221 MapFirstGroup = 2,
209 Unmap = 3, 222 Unmap = 3,
210 ChangePermissions = 4, 223 ChangePermissions = 4,
211 ChangePermissionsAndRefresh = 5, 224 ChangePermissionsAndRefresh = 5,
212 Separate = 6, 225 ChangePermissionsAndRefreshAndFlush = 6,
226 Separate = 7,
213 }; 227 };
214 228
215 static constexpr KMemoryAttribute DefaultMemoryIgnoreAttr = 229 static constexpr KMemoryAttribute DefaultMemoryIgnoreAttr =
@@ -228,8 +242,6 @@ private:
228 Result Operate(KProcessAddress addr, size_t num_pages, KMemoryPermission perm, 242 Result Operate(KProcessAddress addr, size_t num_pages, KMemoryPermission perm,
229 OperationType operation, KPhysicalAddress map_addr = 0); 243 OperationType operation, KPhysicalAddress map_addr = 0);
230 void FinalizeUpdate(PageLinkedList* page_list); 244 void FinalizeUpdate(PageLinkedList* page_list);
231 KProcessAddress GetRegionAddress(KMemoryState state) const;
232 size_t GetRegionSize(KMemoryState state) const;
233 245
234 KProcessAddress FindFreeArea(KProcessAddress region_start, size_t region_num_pages, 246 KProcessAddress FindFreeArea(KProcessAddress region_start, size_t region_num_pages,
235 size_t num_pages, size_t alignment, size_t offset, 247 size_t num_pages, size_t alignment, size_t offset,
@@ -252,6 +264,13 @@ private:
252 KMemoryAttribute attr_mask, KMemoryAttribute attr) const; 264 KMemoryAttribute attr_mask, KMemoryAttribute attr) const;
253 Result CheckMemoryState(KMemoryState* out_state, KMemoryPermission* out_perm, 265 Result CheckMemoryState(KMemoryState* out_state, KMemoryPermission* out_perm,
254 KMemoryAttribute* out_attr, size_t* out_blocks_needed, 266 KMemoryAttribute* out_attr, size_t* out_blocks_needed,
267 KMemoryBlockManager::const_iterator it, KProcessAddress last_addr,
268 KMemoryState state_mask, KMemoryState state,
269 KMemoryPermission perm_mask, KMemoryPermission perm,
270 KMemoryAttribute attr_mask, KMemoryAttribute attr,
271 KMemoryAttribute ignore_attr = DefaultMemoryIgnoreAttr) const;
272 Result CheckMemoryState(KMemoryState* out_state, KMemoryPermission* out_perm,
273 KMemoryAttribute* out_attr, size_t* out_blocks_needed,
255 KProcessAddress addr, size_t size, KMemoryState state_mask, 274 KProcessAddress addr, size_t size, KMemoryState state_mask,
256 KMemoryState state, KMemoryPermission perm_mask, KMemoryPermission perm, 275 KMemoryState state, KMemoryPermission perm_mask, KMemoryPermission perm,
257 KMemoryAttribute attr_mask, KMemoryAttribute attr, 276 KMemoryAttribute attr_mask, KMemoryAttribute attr,
diff --git a/src/core/hle/kernel/k_process.cpp b/src/core/hle/kernel/k_process.cpp
index 4a099286b..7fa34d693 100644
--- a/src/core/hle/kernel/k_process.cpp
+++ b/src/core/hle/kernel/k_process.cpp
@@ -149,7 +149,7 @@ u64 KProcess::GetTotalPhysicalMemoryUsed() {
149} 149}
150 150
151u64 KProcess::GetTotalPhysicalMemoryUsedWithoutSystemResource() { 151u64 KProcess::GetTotalPhysicalMemoryUsedWithoutSystemResource() {
152 return this->GetTotalPhysicalMemoryUsed() - this->GetSystemResourceUsage(); 152 return this->GetTotalPhysicalMemoryUsed() - this->GetSystemResourceSize();
153} 153}
154 154
155bool KProcess::ReleaseUserException(KThread* thread) { 155bool KProcess::ReleaseUserException(KThread* thread) {
diff --git a/src/core/hle/kernel/kernel.cpp b/src/core/hle/kernel/kernel.cpp
index cb025c3d6..24433d32b 100644
--- a/src/core/hle/kernel/kernel.cpp
+++ b/src/core/hle/kernel/kernel.cpp
@@ -623,14 +623,33 @@ struct KernelCore::Impl {
623 ASSERT(memory_layout->GetPhysicalMemoryRegionTree().Insert( 623 ASSERT(memory_layout->GetPhysicalMemoryRegionTree().Insert(
624 GetInteger(slab_start_phys_addr), slab_region_size, KMemoryRegionType_DramKernelSlab)); 624 GetInteger(slab_start_phys_addr), slab_region_size, KMemoryRegionType_DramKernelSlab));
625 625
626 // Insert a physical region for the secure applet memory.
627 const auto secure_applet_end_phys_addr =
628 slab_end_phys_addr + KSystemControl::SecureAppletMemorySize;
629 if constexpr (KSystemControl::SecureAppletMemorySize > 0) {
630 ASSERT(memory_layout->GetPhysicalMemoryRegionTree().Insert(
631 GetInteger(slab_end_phys_addr), KSystemControl::SecureAppletMemorySize,
632 KMemoryRegionType_DramKernelSecureAppletMemory));
633 }
634
635 // Insert a physical region for the unknown debug2 region.
636 constexpr size_t SecureUnknownRegionSize = 0;
637 const size_t secure_unknown_size = SecureUnknownRegionSize;
638 const auto secure_unknown_end_phys_addr = secure_applet_end_phys_addr + secure_unknown_size;
639 if constexpr (SecureUnknownRegionSize > 0) {
640 ASSERT(memory_layout->GetPhysicalMemoryRegionTree().Insert(
641 GetInteger(secure_applet_end_phys_addr), secure_unknown_size,
642 KMemoryRegionType_DramKernelSecureUnknown));
643 }
644
626 // Determine size available for kernel page table heaps, requiring > 8 MB. 645 // Determine size available for kernel page table heaps, requiring > 8 MB.
627 const KPhysicalAddress resource_end_phys_addr = slab_start_phys_addr + resource_region_size; 646 const KPhysicalAddress resource_end_phys_addr = slab_start_phys_addr + resource_region_size;
628 const size_t page_table_heap_size = resource_end_phys_addr - slab_end_phys_addr; 647 const size_t page_table_heap_size = resource_end_phys_addr - secure_unknown_end_phys_addr;
629 ASSERT(page_table_heap_size / 4_MiB > 2); 648 ASSERT(page_table_heap_size / 4_MiB > 2);
630 649
631 // Insert a physical region for the kernel page table heap region 650 // Insert a physical region for the kernel page table heap region
632 ASSERT(memory_layout->GetPhysicalMemoryRegionTree().Insert( 651 ASSERT(memory_layout->GetPhysicalMemoryRegionTree().Insert(
633 GetInteger(slab_end_phys_addr), page_table_heap_size, 652 GetInteger(secure_unknown_end_phys_addr), page_table_heap_size,
634 KMemoryRegionType_DramKernelPtHeap)); 653 KMemoryRegionType_DramKernelPtHeap));
635 654
636 // All DRAM regions that we haven't tagged by this point will be mapped under the linear 655 // All DRAM regions that we haven't tagged by this point will be mapped under the linear
diff --git a/src/core/hle/kernel/svc/svc_memory.cpp b/src/core/hle/kernel/svc/svc_memory.cpp
index 2cab74127..97f1210de 100644
--- a/src/core/hle/kernel/svc/svc_memory.cpp
+++ b/src/core/hle/kernel/svc/svc_memory.cpp
@@ -76,7 +76,7 @@ Result MapUnmapMemorySanityChecks(const KPageTable& manager, u64 dst_addr, u64 s
76} // namespace 76} // namespace
77 77
78Result SetMemoryPermission(Core::System& system, u64 address, u64 size, MemoryPermission perm) { 78Result SetMemoryPermission(Core::System& system, u64 address, u64 size, MemoryPermission perm) {
79 LOG_DEBUG(Kernel_SVC, "called, address=0x{:016X}, size=0x{:X}, perm=0x{:08X", address, size, 79 LOG_DEBUG(Kernel_SVC, "called, address=0x{:016X}, size=0x{:X}, perm=0x{:08X}", address, size,
80 perm); 80 perm);
81 81
82 // Validate address / size. 82 // Validate address / size.
@@ -108,10 +108,16 @@ Result SetMemoryAttribute(Core::System& system, u64 address, u64 size, u32 mask,
108 R_UNLESS((address < address + size), ResultInvalidCurrentMemory); 108 R_UNLESS((address < address + size), ResultInvalidCurrentMemory);
109 109
110 // Validate the attribute and mask. 110 // Validate the attribute and mask.
111 constexpr u32 SupportedMask = static_cast<u32>(MemoryAttribute::Uncached); 111 constexpr u32 SupportedMask =
112 static_cast<u32>(MemoryAttribute::Uncached | MemoryAttribute::PermissionLocked);
112 R_UNLESS((mask | attr) == mask, ResultInvalidCombination); 113 R_UNLESS((mask | attr) == mask, ResultInvalidCombination);
113 R_UNLESS((mask | attr | SupportedMask) == SupportedMask, ResultInvalidCombination); 114 R_UNLESS((mask | attr | SupportedMask) == SupportedMask, ResultInvalidCombination);
114 115
116 // Check that permission locked is either being set or not masked.
117 R_UNLESS((static_cast<Svc::MemoryAttribute>(mask) & Svc::MemoryAttribute::PermissionLocked) ==
118 (static_cast<Svc::MemoryAttribute>(attr) & Svc::MemoryAttribute::PermissionLocked),
119 ResultInvalidCombination);
120
115 // Validate that the region is in range for the current process. 121 // Validate that the region is in range for the current process.
116 auto& page_table{GetCurrentProcess(system.Kernel()).GetPageTable()}; 122 auto& page_table{GetCurrentProcess(system.Kernel()).GetPageTable()};
117 R_UNLESS(page_table.Contains(address, size), ResultInvalidCurrentMemory); 123 R_UNLESS(page_table.Contains(address, size), ResultInvalidCurrentMemory);
diff --git a/src/core/hle/kernel/svc_types.h b/src/core/hle/kernel/svc_types.h
index 7f380ca4f..251e6013c 100644
--- a/src/core/hle/kernel/svc_types.h
+++ b/src/core/hle/kernel/svc_types.h
@@ -46,6 +46,7 @@ enum class MemoryAttribute : u32 {
46 IpcLocked = (1 << 1), 46 IpcLocked = (1 << 1),
47 DeviceShared = (1 << 2), 47 DeviceShared = (1 << 2),
48 Uncached = (1 << 3), 48 Uncached = (1 << 3),
49 PermissionLocked = (1 << 4),
49}; 50};
50DECLARE_ENUM_FLAG_OPERATORS(MemoryAttribute); 51DECLARE_ENUM_FLAG_OPERATORS(MemoryAttribute);
51 52
diff --git a/src/core/hle/service/caps/caps.cpp b/src/core/hle/service/caps/caps.cpp
index 31dd98140..cd1dfe993 100644
--- a/src/core/hle/service/caps/caps.cpp
+++ b/src/core/hle/service/caps/caps.cpp
@@ -25,11 +25,12 @@ void LoopProcess(Core::System& system) {
25 server_manager->RegisterNamedService( 25 server_manager->RegisterNamedService(
26 "caps:u", std::make_shared<IAlbumApplicationService>(system, album_manager)); 26 "caps:u", std::make_shared<IAlbumApplicationService>(system, album_manager));
27 27
28 server_manager->RegisterNamedService("caps:ss", std::make_shared<IScreenShotService>(system)); 28 server_manager->RegisterNamedService(
29 "caps:ss", std::make_shared<IScreenShotService>(system, album_manager));
29 server_manager->RegisterNamedService("caps:sc", 30 server_manager->RegisterNamedService("caps:sc",
30 std::make_shared<IScreenShotControlService>(system)); 31 std::make_shared<IScreenShotControlService>(system));
31 server_manager->RegisterNamedService("caps:su", 32 server_manager->RegisterNamedService(
32 std::make_shared<IScreenShotApplicationService>(system)); 33 "caps:su", std::make_shared<IScreenShotApplicationService>(system, album_manager));
33 34
34 ServerManager::RunServer(std::move(server_manager)); 35 ServerManager::RunServer(std::move(server_manager));
35} 36}
diff --git a/src/core/hle/service/caps/caps_manager.cpp b/src/core/hle/service/caps/caps_manager.cpp
index 2b4e3f076..7d733eb54 100644
--- a/src/core/hle/service/caps/caps_manager.cpp
+++ b/src/core/hle/service/caps/caps_manager.cpp
@@ -2,12 +2,11 @@
2// SPDX-License-Identifier: GPL-2.0-or-later 2// SPDX-License-Identifier: GPL-2.0-or-later
3 3
4#include <sstream> 4#include <sstream>
5#include <stb_image.h>
6#include <stb_image_resize.h>
7 5
8#include "common/fs/file.h" 6#include "common/fs/file.h"
9#include "common/fs/path_util.h" 7#include "common/fs/path_util.h"
10#include "common/logging/log.h" 8#include "common/logging/log.h"
9#include "common/stb.h"
11#include "core/core.h" 10#include "core/core.h"
12#include "core/hle/service/caps/caps_manager.h" 11#include "core/hle/service/caps/caps_manager.h"
13#include "core/hle/service/caps/caps_result.h" 12#include "core/hle/service/caps/caps_result.h"
@@ -227,6 +226,49 @@ Result AlbumManager::LoadAlbumScreenShotThumbnail(
227 +static_cast<int>(out_image_output.height), decoder_options.flags); 226 +static_cast<int>(out_image_output.height), decoder_options.flags);
228} 227}
229 228
229Result AlbumManager::SaveScreenShot(ApplicationAlbumEntry& out_entry,
230 const ScreenShotAttribute& attribute,
231 std::span<const u8> image_data, u64 aruid) {
232 return SaveScreenShot(out_entry, attribute, {}, image_data, aruid);
233}
234
235Result AlbumManager::SaveScreenShot(ApplicationAlbumEntry& out_entry,
236 const ScreenShotAttribute& attribute,
237 const ApplicationData& app_data, std::span<const u8> image_data,
238 u64 aruid) {
239 const u64 title_id = system.GetApplicationProcessProgramID();
240 const auto& user_clock = system.GetTimeManager().GetStandardUserSystemClockCore();
241
242 s64 posix_time{};
243 Result result = user_clock.GetCurrentTime(system, posix_time);
244
245 if (result.IsError()) {
246 return result;
247 }
248
249 const auto date = ConvertToAlbumDateTime(posix_time);
250
251 return SaveImage(out_entry, image_data, title_id, date);
252}
253
254Result AlbumManager::SaveEditedScreenShot(ApplicationAlbumEntry& out_entry,
255 const ScreenShotAttribute& attribute,
256 const AlbumFileId& file_id,
257 std::span<const u8> image_data) {
258 const auto& user_clock = system.GetTimeManager().GetStandardUserSystemClockCore();
259
260 s64 posix_time{};
261 Result result = user_clock.GetCurrentTime(system, posix_time);
262
263 if (result.IsError()) {
264 return result;
265 }
266
267 const auto date = ConvertToAlbumDateTime(posix_time);
268
269 return SaveImage(out_entry, image_data, file_id.application_id, date);
270}
271
230Result AlbumManager::GetFile(std::filesystem::path& out_path, const AlbumFileId& file_id) const { 272Result AlbumManager::GetFile(std::filesystem::path& out_path, const AlbumFileId& file_id) const {
231 const auto file = album_files.find(file_id); 273 const auto file = album_files.find(file_id);
232 274
@@ -365,6 +407,47 @@ Result AlbumManager::LoadImage(std::span<u8> out_image, const std::filesystem::p
365 return ResultSuccess; 407 return ResultSuccess;
366} 408}
367 409
410static void PNGToMemory(void* context, void* png, int len) {
411 std::vector<u8>* png_image = static_cast<std::vector<u8>*>(context);
412 png_image->reserve(len);
413 std::memcpy(png_image->data(), png, len);
414}
415
416Result AlbumManager::SaveImage(ApplicationAlbumEntry& out_entry, std::span<const u8> image,
417 u64 title_id, const AlbumFileDateTime& date) const {
418 const auto screenshot_path =
419 Common::FS::GetYuzuPathString(Common::FS::YuzuPath::ScreenshotsDir);
420 const std::string formatted_date =
421 fmt::format("{:04}-{:02}-{:02}_{:02}-{:02}-{:02}-{:03}", date.year, date.month, date.day,
422 date.hour, date.minute, date.second, 0);
423 const std::string file_path =
424 fmt::format("{}/{:016x}_{}.png", screenshot_path, title_id, formatted_date);
425
426 const Common::FS::IOFile db_file{file_path, Common::FS::FileAccessMode::Write,
427 Common::FS::FileType::BinaryFile};
428
429 std::vector<u8> png_image;
430 if (!stbi_write_png_to_func(PNGToMemory, &png_image, 1280, 720, STBI_rgb_alpha, image.data(),
431 0)) {
432 return ResultFileCountLimit;
433 }
434
435 if (db_file.Write(png_image) != png_image.size()) {
436 return ResultFileCountLimit;
437 }
438
439 out_entry = {
440 .size = png_image.size(),
441 .hash = {},
442 .datetime = date,
443 .storage = AlbumStorage::Sd,
444 .content = ContentType::Screenshot,
445 .unknown = 1,
446 };
447
448 return ResultSuccess;
449}
450
368AlbumFileDateTime AlbumManager::ConvertToAlbumDateTime(u64 posix_time) const { 451AlbumFileDateTime AlbumManager::ConvertToAlbumDateTime(u64 posix_time) const {
369 Time::TimeZone::CalendarInfo calendar_date{}; 452 Time::TimeZone::CalendarInfo calendar_date{};
370 const auto& time_zone_manager = 453 const auto& time_zone_manager =
diff --git a/src/core/hle/service/caps/caps_manager.h b/src/core/hle/service/caps/caps_manager.h
index f65eb12c1..44d85117f 100644
--- a/src/core/hle/service/caps/caps_manager.h
+++ b/src/core/hle/service/caps/caps_manager.h
@@ -58,6 +58,15 @@ public:
58 std::vector<u8>& out_image, const AlbumFileId& file_id, 58 std::vector<u8>& out_image, const AlbumFileId& file_id,
59 const ScreenShotDecodeOption& decoder_options) const; 59 const ScreenShotDecodeOption& decoder_options) const;
60 60
61 Result SaveScreenShot(ApplicationAlbumEntry& out_entry, const ScreenShotAttribute& attribute,
62 std::span<const u8> image_data, u64 aruid);
63 Result SaveScreenShot(ApplicationAlbumEntry& out_entry, const ScreenShotAttribute& attribute,
64 const ApplicationData& app_data, std::span<const u8> image_data,
65 u64 aruid);
66 Result SaveEditedScreenShot(ApplicationAlbumEntry& out_entry,
67 const ScreenShotAttribute& attribute, const AlbumFileId& file_id,
68 std::span<const u8> image_data);
69
61private: 70private:
62 static constexpr std::size_t NandAlbumFileLimit = 1000; 71 static constexpr std::size_t NandAlbumFileLimit = 1000;
63 static constexpr std::size_t SdAlbumFileLimit = 10000; 72 static constexpr std::size_t SdAlbumFileLimit = 10000;
@@ -67,6 +76,8 @@ private:
67 Result GetAlbumEntry(AlbumEntry& out_entry, const std::filesystem::path& path) const; 76 Result GetAlbumEntry(AlbumEntry& out_entry, const std::filesystem::path& path) const;
68 Result LoadImage(std::span<u8> out_image, const std::filesystem::path& path, int width, 77 Result LoadImage(std::span<u8> out_image, const std::filesystem::path& path, int width,
69 int height, ScreenShotDecoderFlag flag) const; 78 int height, ScreenShotDecoderFlag flag) const;
79 Result SaveImage(ApplicationAlbumEntry& out_entry, std::span<const u8> image, u64 title_id,
80 const AlbumFileDateTime& date) const;
70 81
71 AlbumFileDateTime ConvertToAlbumDateTime(u64 posix_time) const; 82 AlbumFileDateTime ConvertToAlbumDateTime(u64 posix_time) const;
72 83
diff --git a/src/core/hle/service/caps/caps_ss.cpp b/src/core/hle/service/caps/caps_ss.cpp
index d0d1b5425..1ba2b7972 100644
--- a/src/core/hle/service/caps/caps_ss.cpp
+++ b/src/core/hle/service/caps/caps_ss.cpp
@@ -1,19 +1,25 @@
1// SPDX-FileCopyrightText: Copyright 2020 yuzu Emulator Project 1// SPDX-FileCopyrightText: Copyright 2020 yuzu Emulator Project
2// SPDX-License-Identifier: GPL-2.0-or-later 2// SPDX-License-Identifier: GPL-2.0-or-later
3 3
4#include "common/logging/log.h"
5#include "core/hle/service/caps/caps_manager.h"
6#include "core/hle/service/caps/caps_types.h"
7#include "core/hle/service/ipc_helpers.h"
8
4#include "core/hle/service/caps/caps_ss.h" 9#include "core/hle/service/caps/caps_ss.h"
5 10
6namespace Service::Capture { 11namespace Service::Capture {
7 12
8IScreenShotService::IScreenShotService(Core::System& system_) 13IScreenShotService::IScreenShotService(Core::System& system_,
9 : ServiceFramework{system_, "caps:ss"} { 14 std::shared_ptr<AlbumManager> album_manager)
15 : ServiceFramework{system_, "caps:ss"}, manager{album_manager} {
10 // clang-format off 16 // clang-format off
11 static const FunctionInfo functions[] = { 17 static const FunctionInfo functions[] = {
12 {201, nullptr, "SaveScreenShot"}, 18 {201, nullptr, "SaveScreenShot"},
13 {202, nullptr, "SaveEditedScreenShot"}, 19 {202, nullptr, "SaveEditedScreenShot"},
14 {203, nullptr, "SaveScreenShotEx0"}, 20 {203, &IScreenShotService::SaveScreenShotEx0, "SaveScreenShotEx0"},
15 {204, nullptr, "SaveEditedScreenShotEx0"}, 21 {204, nullptr, "SaveEditedScreenShotEx0"},
16 {206, nullptr, "Unknown206"}, 22 {206, &IScreenShotService::SaveEditedScreenShotEx1, "SaveEditedScreenShotEx1"},
17 {208, nullptr, "SaveScreenShotOfMovieEx1"}, 23 {208, nullptr, "SaveScreenShotOfMovieEx1"},
18 {1000, nullptr, "Unknown1000"}, 24 {1000, nullptr, "Unknown1000"},
19 }; 25 };
@@ -24,4 +30,65 @@ IScreenShotService::IScreenShotService(Core::System& system_)
24 30
25IScreenShotService::~IScreenShotService() = default; 31IScreenShotService::~IScreenShotService() = default;
26 32
33void IScreenShotService::SaveScreenShotEx0(HLERequestContext& ctx) {
34 IPC::RequestParser rp{ctx};
35 struct Parameters {
36 ScreenShotAttribute attribute{};
37 u32 report_option{};
38 INSERT_PADDING_BYTES(0x4);
39 u64 applet_resource_user_id{};
40 };
41 static_assert(sizeof(Parameters) == 0x50, "Parameters has incorrect size.");
42
43 const auto parameters{rp.PopRaw<Parameters>()};
44 const auto image_data_buffer = ctx.ReadBuffer();
45
46 LOG_INFO(Service_Capture,
47 "called, report_option={}, image_data_buffer_size={}, applet_resource_user_id={}",
48 parameters.report_option, image_data_buffer.size(),
49 parameters.applet_resource_user_id);
50
51 ApplicationAlbumEntry entry{};
52 const auto result = manager->SaveScreenShot(entry, parameters.attribute, image_data_buffer,
53 parameters.applet_resource_user_id);
54
55 IPC::ResponseBuilder rb{ctx, 10};
56 rb.Push(result);
57 rb.PushRaw(entry);
58}
59void IScreenShotService::SaveEditedScreenShotEx1(HLERequestContext& ctx) {
60 IPC::RequestParser rp{ctx};
61 struct Parameters {
62 ScreenShotAttribute attribute;
63 u64 width;
64 u64 height;
65 u64 thumbnail_width;
66 u64 thumbnail_height;
67 AlbumFileId file_id;
68 };
69 static_assert(sizeof(Parameters) == 0x78, "Parameters has incorrect size.");
70
71 const auto parameters{rp.PopRaw<Parameters>()};
72 const auto application_data_buffer = ctx.ReadBuffer(0);
73 const auto image_data_buffer = ctx.ReadBuffer(1);
74 const auto thumbnail_image_data_buffer = ctx.ReadBuffer(2);
75
76 LOG_INFO(Service_Capture,
77 "called, width={}, height={}, thumbnail_width={}, thumbnail_height={}, "
78 "application_id={:016x}, storage={}, type={}, app_data_buffer_size={}, "
79 "image_data_buffer_size={}, thumbnail_image_buffer_size={}",
80 parameters.width, parameters.height, parameters.thumbnail_width,
81 parameters.thumbnail_height, parameters.file_id.application_id,
82 parameters.file_id.storage, parameters.file_id.type, application_data_buffer.size(),
83 image_data_buffer.size(), thumbnail_image_data_buffer.size());
84
85 ApplicationAlbumEntry entry{};
86 const auto result = manager->SaveEditedScreenShot(entry, parameters.attribute,
87 parameters.file_id, image_data_buffer);
88
89 IPC::ResponseBuilder rb{ctx, 10};
90 rb.Push(result);
91 rb.PushRaw(entry);
92}
93
27} // namespace Service::Capture 94} // namespace Service::Capture
diff --git a/src/core/hle/service/caps/caps_ss.h b/src/core/hle/service/caps/caps_ss.h
index 381e44fd4..a7e9972ab 100644
--- a/src/core/hle/service/caps/caps_ss.h
+++ b/src/core/hle/service/caps/caps_ss.h
@@ -13,8 +13,14 @@ namespace Service::Capture {
13 13
14class IScreenShotService final : public ServiceFramework<IScreenShotService> { 14class IScreenShotService final : public ServiceFramework<IScreenShotService> {
15public: 15public:
16 explicit IScreenShotService(Core::System& system_); 16 explicit IScreenShotService(Core::System& system_, std::shared_ptr<AlbumManager> album_manager);
17 ~IScreenShotService() override; 17 ~IScreenShotService() override;
18
19private:
20 void SaveScreenShotEx0(HLERequestContext& ctx);
21 void SaveEditedScreenShotEx1(HLERequestContext& ctx);
22
23 std::shared_ptr<AlbumManager> manager;
18}; 24};
19 25
20} // namespace Service::Capture 26} // namespace Service::Capture
diff --git a/src/core/hle/service/caps/caps_su.cpp b/src/core/hle/service/caps/caps_su.cpp
index cad173dc7..e85625ee4 100644
--- a/src/core/hle/service/caps/caps_su.cpp
+++ b/src/core/hle/service/caps/caps_su.cpp
@@ -2,19 +2,22 @@
2// SPDX-License-Identifier: GPL-2.0-or-later 2// SPDX-License-Identifier: GPL-2.0-or-later
3 3
4#include "common/logging/log.h" 4#include "common/logging/log.h"
5#include "core/hle/service/caps/caps_manager.h"
5#include "core/hle/service/caps/caps_su.h" 6#include "core/hle/service/caps/caps_su.h"
7#include "core/hle/service/caps/caps_types.h"
6#include "core/hle/service/ipc_helpers.h" 8#include "core/hle/service/ipc_helpers.h"
7 9
8namespace Service::Capture { 10namespace Service::Capture {
9 11
10IScreenShotApplicationService::IScreenShotApplicationService(Core::System& system_) 12IScreenShotApplicationService::IScreenShotApplicationService(
11 : ServiceFramework{system_, "caps:su"} { 13 Core::System& system_, std::shared_ptr<AlbumManager> album_manager)
14 : ServiceFramework{system_, "caps:su"}, manager{album_manager} {
12 // clang-format off 15 // clang-format off
13 static const FunctionInfo functions[] = { 16 static const FunctionInfo functions[] = {
14 {32, &IScreenShotApplicationService::SetShimLibraryVersion, "SetShimLibraryVersion"}, 17 {32, &IScreenShotApplicationService::SetShimLibraryVersion, "SetShimLibraryVersion"},
15 {201, nullptr, "SaveScreenShot"}, 18 {201, nullptr, "SaveScreenShot"},
16 {203, nullptr, "SaveScreenShotEx0"}, 19 {203, &IScreenShotApplicationService::SaveScreenShotEx0, "SaveScreenShotEx0"},
17 {205, nullptr, "SaveScreenShotEx1"}, 20 {205, &IScreenShotApplicationService::SaveScreenShotEx1, "SaveScreenShotEx1"},
18 {210, nullptr, "SaveScreenShotEx2"}, 21 {210, nullptr, "SaveScreenShotEx2"},
19 }; 22 };
20 // clang-format on 23 // clang-format on
@@ -36,4 +39,62 @@ void IScreenShotApplicationService::SetShimLibraryVersion(HLERequestContext& ctx
36 rb.Push(ResultSuccess); 39 rb.Push(ResultSuccess);
37} 40}
38 41
42void IScreenShotApplicationService::SaveScreenShotEx0(HLERequestContext& ctx) {
43 IPC::RequestParser rp{ctx};
44 struct Parameters {
45 ScreenShotAttribute attribute{};
46 AlbumReportOption report_option{};
47 INSERT_PADDING_BYTES(0x4);
48 u64 applet_resource_user_id{};
49 };
50 static_assert(sizeof(Parameters) == 0x50, "Parameters has incorrect size.");
51
52 const auto parameters{rp.PopRaw<Parameters>()};
53 const auto image_data_buffer = ctx.ReadBuffer();
54
55 LOG_INFO(Service_Capture,
56 "called, report_option={}, image_data_buffer_size={}, applet_resource_user_id={}",
57 parameters.report_option, image_data_buffer.size(),
58 parameters.applet_resource_user_id);
59
60 ApplicationAlbumEntry entry{};
61 const auto result = manager->SaveScreenShot(entry, parameters.attribute, image_data_buffer,
62 parameters.applet_resource_user_id);
63
64 IPC::ResponseBuilder rb{ctx, 10};
65 rb.Push(result);
66 rb.PushRaw(entry);
67}
68
69void IScreenShotApplicationService::SaveScreenShotEx1(HLERequestContext& ctx) {
70 IPC::RequestParser rp{ctx};
71 struct Parameters {
72 ScreenShotAttribute attribute{};
73 AlbumReportOption report_option{};
74 INSERT_PADDING_BYTES(0x4);
75 u64 applet_resource_user_id{};
76 };
77 static_assert(sizeof(Parameters) == 0x50, "Parameters has incorrect size.");
78
79 const auto parameters{rp.PopRaw<Parameters>()};
80 const auto app_data_buffer = ctx.ReadBuffer(0);
81 const auto image_data_buffer = ctx.ReadBuffer(1);
82
83 LOG_INFO(Service_Capture,
84 "called, report_option={}, image_data_buffer_size={}, applet_resource_user_id={}",
85 parameters.report_option, image_data_buffer.size(),
86 parameters.applet_resource_user_id);
87
88 ApplicationAlbumEntry entry{};
89 ApplicationData app_data{};
90 std::memcpy(&app_data, app_data_buffer.data(), sizeof(ApplicationData));
91 const auto result =
92 manager->SaveScreenShot(entry, parameters.attribute, app_data, image_data_buffer,
93 parameters.applet_resource_user_id);
94
95 IPC::ResponseBuilder rb{ctx, 10};
96 rb.Push(result);
97 rb.PushRaw(entry);
98}
99
39} // namespace Service::Capture 100} // namespace Service::Capture
diff --git a/src/core/hle/service/caps/caps_su.h b/src/core/hle/service/caps/caps_su.h
index 647e3059d..89e71f506 100644
--- a/src/core/hle/service/caps/caps_su.h
+++ b/src/core/hle/service/caps/caps_su.h
@@ -10,14 +10,20 @@ class System;
10} 10}
11 11
12namespace Service::Capture { 12namespace Service::Capture {
13class AlbumManager;
13 14
14class IScreenShotApplicationService final : public ServiceFramework<IScreenShotApplicationService> { 15class IScreenShotApplicationService final : public ServiceFramework<IScreenShotApplicationService> {
15public: 16public:
16 explicit IScreenShotApplicationService(Core::System& system_); 17 explicit IScreenShotApplicationService(Core::System& system_,
18 std::shared_ptr<AlbumManager> album_manager);
17 ~IScreenShotApplicationService() override; 19 ~IScreenShotApplicationService() override;
18 20
19private: 21private:
20 void SetShimLibraryVersion(HLERequestContext& ctx); 22 void SetShimLibraryVersion(HLERequestContext& ctx);
23 void SaveScreenShotEx0(HLERequestContext& ctx);
24 void SaveScreenShotEx1(HLERequestContext& ctx);
25
26 std::shared_ptr<AlbumManager> manager;
21}; 27};
22 28
23} // namespace Service::Capture 29} // namespace Service::Capture
diff --git a/src/core/hle/service/caps/caps_types.h b/src/core/hle/service/caps/caps_types.h
index 7fd357954..589ac28d3 100644
--- a/src/core/hle/service/caps/caps_types.h
+++ b/src/core/hle/service/caps/caps_types.h
@@ -20,6 +20,8 @@ enum class AlbumImageOrientation {
20enum class AlbumReportOption : s32 { 20enum class AlbumReportOption : s32 {
21 Disable, 21 Disable,
22 Enable, 22 Enable,
23 Unknown2,
24 Unknown3,
23}; 25};
24 26
25enum class ContentType : u8 { 27enum class ContentType : u8 {
diff --git a/src/core/hle/service/hle_ipc.cpp b/src/core/hle/service/hle_ipc.cpp
index 6f3ae3cc4..ff374ae39 100644
--- a/src/core/hle/service/hle_ipc.cpp
+++ b/src/core/hle/service/hle_ipc.cpp
@@ -27,10 +27,12 @@ namespace {
27static thread_local std::array read_buffer_data_a{ 27static thread_local std::array read_buffer_data_a{
28 Common::ScratchBuffer<u8>(), 28 Common::ScratchBuffer<u8>(),
29 Common::ScratchBuffer<u8>(), 29 Common::ScratchBuffer<u8>(),
30 Common::ScratchBuffer<u8>(),
30}; 31};
31static thread_local std::array read_buffer_data_x{ 32static thread_local std::array read_buffer_data_x{
32 Common::ScratchBuffer<u8>(), 33 Common::ScratchBuffer<u8>(),
33 Common::ScratchBuffer<u8>(), 34 Common::ScratchBuffer<u8>(),
35 Common::ScratchBuffer<u8>(),
34}; 36};
35} // Anonymous namespace 37} // Anonymous namespace
36 38
@@ -343,6 +345,7 @@ std::span<const u8> HLERequestContext::ReadBufferA(std::size_t buffer_index) con
343 static thread_local std::array read_buffer_a{ 345 static thread_local std::array read_buffer_a{
344 Core::Memory::CpuGuestMemory<u8, Core::Memory::GuestMemoryFlags::SafeRead>(memory, 0, 0), 346 Core::Memory::CpuGuestMemory<u8, Core::Memory::GuestMemoryFlags::SafeRead>(memory, 0, 0),
345 Core::Memory::CpuGuestMemory<u8, Core::Memory::GuestMemoryFlags::SafeRead>(memory, 0, 0), 347 Core::Memory::CpuGuestMemory<u8, Core::Memory::GuestMemoryFlags::SafeRead>(memory, 0, 0),
348 Core::Memory::CpuGuestMemory<u8, Core::Memory::GuestMemoryFlags::SafeRead>(memory, 0, 0),
346 }; 349 };
347 350
348 ASSERT_OR_EXECUTE_MSG( 351 ASSERT_OR_EXECUTE_MSG(
@@ -358,6 +361,7 @@ std::span<const u8> HLERequestContext::ReadBufferX(std::size_t buffer_index) con
358 static thread_local std::array read_buffer_x{ 361 static thread_local std::array read_buffer_x{
359 Core::Memory::CpuGuestMemory<u8, Core::Memory::GuestMemoryFlags::SafeRead>(memory, 0, 0), 362 Core::Memory::CpuGuestMemory<u8, Core::Memory::GuestMemoryFlags::SafeRead>(memory, 0, 0),
360 Core::Memory::CpuGuestMemory<u8, Core::Memory::GuestMemoryFlags::SafeRead>(memory, 0, 0), 363 Core::Memory::CpuGuestMemory<u8, Core::Memory::GuestMemoryFlags::SafeRead>(memory, 0, 0),
364 Core::Memory::CpuGuestMemory<u8, Core::Memory::GuestMemoryFlags::SafeRead>(memory, 0, 0),
361 }; 365 };
362 366
363 ASSERT_OR_EXECUTE_MSG( 367 ASSERT_OR_EXECUTE_MSG(
@@ -373,10 +377,12 @@ std::span<const u8> HLERequestContext::ReadBuffer(std::size_t buffer_index) cons
373 static thread_local std::array read_buffer_a{ 377 static thread_local std::array read_buffer_a{
374 Core::Memory::CpuGuestMemory<u8, Core::Memory::GuestMemoryFlags::SafeRead>(memory, 0, 0), 378 Core::Memory::CpuGuestMemory<u8, Core::Memory::GuestMemoryFlags::SafeRead>(memory, 0, 0),
375 Core::Memory::CpuGuestMemory<u8, Core::Memory::GuestMemoryFlags::SafeRead>(memory, 0, 0), 379 Core::Memory::CpuGuestMemory<u8, Core::Memory::GuestMemoryFlags::SafeRead>(memory, 0, 0),
380 Core::Memory::CpuGuestMemory<u8, Core::Memory::GuestMemoryFlags::SafeRead>(memory, 0, 0),
376 }; 381 };
377 static thread_local std::array read_buffer_x{ 382 static thread_local std::array read_buffer_x{
378 Core::Memory::CpuGuestMemory<u8, Core::Memory::GuestMemoryFlags::SafeRead>(memory, 0, 0), 383 Core::Memory::CpuGuestMemory<u8, Core::Memory::GuestMemoryFlags::SafeRead>(memory, 0, 0),
379 Core::Memory::CpuGuestMemory<u8, Core::Memory::GuestMemoryFlags::SafeRead>(memory, 0, 0), 384 Core::Memory::CpuGuestMemory<u8, Core::Memory::GuestMemoryFlags::SafeRead>(memory, 0, 0),
385 Core::Memory::CpuGuestMemory<u8, Core::Memory::GuestMemoryFlags::SafeRead>(memory, 0, 0),
380 }; 386 };
381 387
382 const bool is_buffer_a{BufferDescriptorA().size() > buffer_index && 388 const bool is_buffer_a{BufferDescriptorA().size() > buffer_index &&
diff --git a/src/core/hle/service/nvnflinger/fb_share_buffer_manager.cpp b/src/core/hle/service/nvnflinger/fb_share_buffer_manager.cpp
index 469a53244..2e29bc848 100644
--- a/src/core/hle/service/nvnflinger/fb_share_buffer_manager.cpp
+++ b/src/core/hle/service/nvnflinger/fb_share_buffer_manager.cpp
@@ -46,7 +46,7 @@ Result AllocateIoForProcessAddressSpace(Common::ProcessAddress* out_map_address,
46 // Get bounds of where mapping is possible. 46 // Get bounds of where mapping is possible.
47 const VAddr alias_code_begin = GetInteger(page_table.GetAliasCodeRegionStart()); 47 const VAddr alias_code_begin = GetInteger(page_table.GetAliasCodeRegionStart());
48 const VAddr alias_code_size = page_table.GetAliasCodeRegionSize() / YUZU_PAGESIZE; 48 const VAddr alias_code_size = page_table.GetAliasCodeRegionSize() / YUZU_PAGESIZE;
49 const auto state = Kernel::KMemoryState::Io; 49 const auto state = Kernel::KMemoryState::IoMemory;
50 const auto perm = Kernel::KMemoryPermission::UserReadWrite; 50 const auto perm = Kernel::KMemoryPermission::UserReadWrite;
51 std::mt19937_64 rng{process->GetRandomEntropy(0)}; 51 std::mt19937_64 rng{process->GetRandomEntropy(0)};
52 52
diff --git a/src/core/hle/service/ptm/ts.cpp b/src/core/hle/service/ptm/ts.cpp
index ca064dd90..652f38b97 100644
--- a/src/core/hle/service/ptm/ts.cpp
+++ b/src/core/hle/service/ptm/ts.cpp
@@ -9,6 +9,35 @@
9 9
10namespace Service::PTM { 10namespace Service::PTM {
11 11
12enum class Location : u8 {
13 Internal,
14 External,
15};
16
17class ISession : public ServiceFramework<ISession> {
18public:
19 explicit ISession(Core::System& system_) : ServiceFramework{system_, "ISession"} {
20 // clang-format off
21 static const FunctionInfo functions[] = {
22 {0, nullptr, "GetTemperatureRange"},
23 {2, nullptr, "SetMeasurementMode"},
24 {4, &ISession::GetTemperature, "GetTemperature"},
25 };
26 // clang-format on
27
28 RegisterHandlers(functions);
29 }
30
31private:
32 void GetTemperature(HLERequestContext& ctx) {
33 constexpr f32 temperature = 35;
34
35 IPC::ResponseBuilder rb{ctx, 3};
36 rb.Push(ResultSuccess);
37 rb.Push(temperature);
38 }
39};
40
12TS::TS(Core::System& system_) : ServiceFramework{system_, "ts"} { 41TS::TS(Core::System& system_) : ServiceFramework{system_, "ts"} {
13 // clang-format off 42 // clang-format off
14 static const FunctionInfo functions[] = { 43 static const FunctionInfo functions[] = {
@@ -16,7 +45,7 @@ TS::TS(Core::System& system_) : ServiceFramework{system_, "ts"} {
16 {1, &TS::GetTemperature, "GetTemperature"}, 45 {1, &TS::GetTemperature, "GetTemperature"},
17 {2, nullptr, "SetMeasurementMode"}, 46 {2, nullptr, "SetMeasurementMode"},
18 {3, &TS::GetTemperatureMilliC, "GetTemperatureMilliC"}, 47 {3, &TS::GetTemperatureMilliC, "GetTemperatureMilliC"},
19 {4, nullptr, "OpenSession"}, 48 {4, &TS::OpenSession, "OpenSession"},
20 }; 49 };
21 // clang-format on 50 // clang-format on
22 51
@@ -47,4 +76,13 @@ void TS::GetTemperatureMilliC(HLERequestContext& ctx) {
47 rb.Push(temperature); 76 rb.Push(temperature);
48} 77}
49 78
79void TS::OpenSession(HLERequestContext& ctx) {
80 IPC::RequestParser rp{ctx};
81 [[maybe_unused]] const u32 device_code = rp.Pop<u32>();
82
83 IPC::ResponseBuilder rb{ctx, 2, 0, 1};
84 rb.Push(ResultSuccess);
85 rb.PushIpcInterface<ISession>(system);
86}
87
50} // namespace Service::PTM 88} // namespace Service::PTM
diff --git a/src/core/hle/service/ptm/ts.h b/src/core/hle/service/ptm/ts.h
index c3f43d5a3..a10a91a64 100644
--- a/src/core/hle/service/ptm/ts.h
+++ b/src/core/hle/service/ptm/ts.h
@@ -14,13 +14,9 @@ public:
14 ~TS() override; 14 ~TS() override;
15 15
16private: 16private:
17 enum class Location : u8 {
18 Internal,
19 External,
20 };
21
22 void GetTemperature(HLERequestContext& ctx); 17 void GetTemperature(HLERequestContext& ctx);
23 void GetTemperatureMilliC(HLERequestContext& ctx); 18 void GetTemperatureMilliC(HLERequestContext& ctx);
19 void OpenSession(HLERequestContext& ctx);
24}; 20};
25 21
26} // namespace Service::PTM 22} // namespace Service::PTM
diff --git a/src/core/hle/service/set/set_sys.cpp b/src/core/hle/service/set/set_sys.cpp
index 165b97dad..ec3af80af 100644
--- a/src/core/hle/service/set/set_sys.cpp
+++ b/src/core/hle/service/set/set_sys.cpp
@@ -5,8 +5,13 @@
5#include "common/logging/log.h" 5#include "common/logging/log.h"
6#include "common/settings.h" 6#include "common/settings.h"
7#include "common/string_util.h" 7#include "common/string_util.h"
8#include "core/core.h"
9#include "core/file_sys/content_archive.h"
8#include "core/file_sys/errors.h" 10#include "core/file_sys/errors.h"
9#include "core/file_sys/system_archive/system_version.h" 11#include "core/file_sys/nca_metadata.h"
12#include "core/file_sys/registered_cache.h"
13#include "core/file_sys/romfs.h"
14#include "core/file_sys/system_archive/system_archive.h"
10#include "core/hle/service/filesystem/filesystem.h" 15#include "core/hle/service/filesystem/filesystem.h"
11#include "core/hle/service/ipc_helpers.h" 16#include "core/hle/service/ipc_helpers.h"
12#include "core/hle/service/set/set.h" 17#include "core/hle/service/set/set.h"
@@ -22,18 +27,30 @@ enum class GetFirmwareVersionType {
22 Version2, 27 Version2,
23}; 28};
24 29
25void GetFirmwareVersionImpl(HLERequestContext& ctx, GetFirmwareVersionType type) { 30void GetFirmwareVersionImpl(Core::System& system, HLERequestContext& ctx,
26 LOG_WARNING(Service_SET, "called - Using hardcoded firmware version '{}'", 31 GetFirmwareVersionType type) {
27 FileSys::SystemArchive::GetLongDisplayVersion());
28
29 ASSERT_MSG(ctx.GetWriteBufferSize() == 0x100, 32 ASSERT_MSG(ctx.GetWriteBufferSize() == 0x100,
30 "FirmwareVersion output buffer must be 0x100 bytes in size!"); 33 "FirmwareVersion output buffer must be 0x100 bytes in size!");
31 34
32 // Instead of using the normal procedure of checking for the real system archive and if it 35 constexpr u64 FirmwareVersionSystemDataId = 0x0100000000000809;
33 // doesn't exist, synthesizing one, I feel that that would lead to strange bugs because a 36 auto& fsc = system.GetFileSystemController();
34 // used is using a really old or really new SystemVersion title. The synthesized one ensures 37
35 // consistence (currently reports as 5.1.0-0.0) 38 // Attempt to load version data from disk
36 const auto archive = FileSys::SystemArchive::SystemVersion(); 39 const FileSys::RegisteredCache* bis_system{};
40 std::unique_ptr<FileSys::NCA> nca{};
41 FileSys::VirtualDir romfs{};
42
43 bis_system = fsc.GetSystemNANDContents();
44 if (bis_system) {
45 nca = bis_system->GetEntry(FirmwareVersionSystemDataId, FileSys::ContentRecordType::Data);
46 }
47 if (nca) {
48 romfs = FileSys::ExtractRomFS(nca->GetRomFS());
49 }
50 if (!romfs) {
51 romfs = FileSys::ExtractRomFS(
52 FileSys::SystemArchive::SynthesizeSystemArchive(FirmwareVersionSystemDataId));
53 }
37 54
38 const auto early_exit_failure = [&ctx](std::string_view desc, Result code) { 55 const auto early_exit_failure = [&ctx](std::string_view desc, Result code) {
39 LOG_ERROR(Service_SET, "General failure while attempting to resolve firmware version ({}).", 56 LOG_ERROR(Service_SET, "General failure while attempting to resolve firmware version ({}).",
@@ -42,13 +59,7 @@ void GetFirmwareVersionImpl(HLERequestContext& ctx, GetFirmwareVersionType type)
42 rb.Push(code); 59 rb.Push(code);
43 }; 60 };
44 61
45 if (archive == nullptr) { 62 const auto ver_file = romfs->GetFile("file");
46 early_exit_failure("The system version archive couldn't be synthesized.",
47 FileSys::ERROR_FAILED_MOUNT_ARCHIVE);
48 return;
49 }
50
51 const auto ver_file = archive->GetFile("file");
52 if (ver_file == nullptr) { 63 if (ver_file == nullptr) {
53 early_exit_failure("The system version archive didn't contain the file 'file'.", 64 early_exit_failure("The system version archive didn't contain the file 'file'.",
54 FileSys::ERROR_INVALID_ARGUMENT); 65 FileSys::ERROR_INVALID_ARGUMENT);
@@ -87,12 +98,12 @@ void SET_SYS::SetLanguageCode(HLERequestContext& ctx) {
87 98
88void SET_SYS::GetFirmwareVersion(HLERequestContext& ctx) { 99void SET_SYS::GetFirmwareVersion(HLERequestContext& ctx) {
89 LOG_DEBUG(Service_SET, "called"); 100 LOG_DEBUG(Service_SET, "called");
90 GetFirmwareVersionImpl(ctx, GetFirmwareVersionType::Version1); 101 GetFirmwareVersionImpl(system, ctx, GetFirmwareVersionType::Version1);
91} 102}
92 103
93void SET_SYS::GetFirmwareVersion2(HLERequestContext& ctx) { 104void SET_SYS::GetFirmwareVersion2(HLERequestContext& ctx) {
94 LOG_DEBUG(Service_SET, "called"); 105 LOG_DEBUG(Service_SET, "called");
95 GetFirmwareVersionImpl(ctx, GetFirmwareVersionType::Version2); 106 GetFirmwareVersionImpl(system, ctx, GetFirmwareVersionType::Version2);
96} 107}
97 108
98void SET_SYS::GetAccountSettings(HLERequestContext& ctx) { 109void SET_SYS::GetAccountSettings(HLERequestContext& ctx) {
diff --git a/src/core/memory/cheat_engine.cpp b/src/core/memory/cheat_engine.cpp
index a06e99166..53a89cc8f 100644
--- a/src/core/memory/cheat_engine.cpp
+++ b/src/core/memory/cheat_engine.cpp
@@ -19,16 +19,23 @@ namespace Core::Memory {
19namespace { 19namespace {
20constexpr auto CHEAT_ENGINE_NS = std::chrono::nanoseconds{1000000000 / 12}; 20constexpr auto CHEAT_ENGINE_NS = std::chrono::nanoseconds{1000000000 / 12};
21 21
22std::string_view ExtractName(std::string_view data, std::size_t start_index, char match) { 22std::string_view ExtractName(std::size_t& out_name_size, std::string_view data,
23 std::size_t start_index, char match) {
23 auto end_index = start_index; 24 auto end_index = start_index;
24 while (data[end_index] != match) { 25 while (data[end_index] != match) {
25 ++end_index; 26 ++end_index;
26 if (end_index > data.size() || 27 if (end_index > data.size()) {
27 (end_index - start_index - 1) > sizeof(CheatDefinition::readable_name)) {
28 return {}; 28 return {};
29 } 29 }
30 } 30 }
31 31
32 out_name_size = end_index - start_index;
33
34 // Clamp name if it's too big
35 if (out_name_size > sizeof(CheatDefinition::readable_name)) {
36 end_index = start_index + sizeof(CheatDefinition::readable_name);
37 }
38
32 return data.substr(start_index, end_index - start_index); 39 return data.substr(start_index, end_index - start_index);
33} 40}
34} // Anonymous namespace 41} // Anonymous namespace
@@ -113,7 +120,8 @@ std::vector<CheatEntry> TextCheatParser::Parse(std::string_view data) const {
113 return {}; 120 return {};
114 } 121 }
115 122
116 const auto name = ExtractName(data, i + 1, '}'); 123 std::size_t name_size{};
124 const auto name = ExtractName(name_size, data, i + 1, '}');
117 if (name.empty()) { 125 if (name.empty()) {
118 return {}; 126 return {};
119 } 127 }
@@ -125,12 +133,13 @@ std::vector<CheatEntry> TextCheatParser::Parse(std::string_view data) const {
125 .definition.readable_name[out[*current_entry].definition.readable_name.size() - 1] = 133 .definition.readable_name[out[*current_entry].definition.readable_name.size() - 1] =
126 '\0'; 134 '\0';
127 135
128 i += name.length() + 1; 136 i += name_size + 1;
129 } else if (data[i] == '[') { 137 } else if (data[i] == '[') {
130 current_entry = out.size(); 138 current_entry = out.size();
131 out.emplace_back(); 139 out.emplace_back();
132 140
133 const auto name = ExtractName(data, i + 1, ']'); 141 std::size_t name_size{};
142 const auto name = ExtractName(name_size, data, i + 1, ']');
134 if (name.empty()) { 143 if (name.empty()) {
135 return {}; 144 return {};
136 } 145 }
@@ -142,7 +151,7 @@ std::vector<CheatEntry> TextCheatParser::Parse(std::string_view data) const {
142 .definition.readable_name[out[*current_entry].definition.readable_name.size() - 1] = 151 .definition.readable_name[out[*current_entry].definition.readable_name.size() - 1] =
143 '\0'; 152 '\0';
144 153
145 i += name.length() + 1; 154 i += name_size + 1;
146 } else if (::isxdigit(data[i])) { 155 } else if (::isxdigit(data[i])) {
147 if (!current_entry || out[*current_entry].definition.num_opcodes >= 156 if (!current_entry || out[*current_entry].definition.num_opcodes >=
148 out[*current_entry].definition.opcodes.size()) { 157 out[*current_entry].definition.opcodes.size()) {
diff --git a/src/shader_recompiler/backend/glsl/emit_glsl.cpp b/src/shader_recompiler/backend/glsl/emit_glsl.cpp
index d91e04446..66ecfc9f7 100644
--- a/src/shader_recompiler/backend/glsl/emit_glsl.cpp
+++ b/src/shader_recompiler/backend/glsl/emit_glsl.cpp
@@ -242,6 +242,7 @@ std::string EmitGLSL(const Profile& profile, const RuntimeInfo& runtime_info, IR
242 } 242 }
243 if (program.info.uses_subgroup_shuffles) { 243 if (program.info.uses_subgroup_shuffles) {
244 ctx.header += "bool shfl_in_bounds;"; 244 ctx.header += "bool shfl_in_bounds;";
245 ctx.header += "uint shfl_result;";
245 } 246 }
246 ctx.code.insert(0, ctx.header); 247 ctx.code.insert(0, ctx.header);
247 ctx.code += '}'; 248 ctx.code += '}';
diff --git a/src/shader_recompiler/backend/glsl/emit_glsl_warp.cpp b/src/shader_recompiler/backend/glsl/emit_glsl_warp.cpp
index 1245c9429..f9be5de1c 100644
--- a/src/shader_recompiler/backend/glsl/emit_glsl_warp.cpp
+++ b/src/shader_recompiler/backend/glsl/emit_glsl_warp.cpp
@@ -141,7 +141,8 @@ void EmitShuffleIndex(EmitContext& ctx, IR::Inst& inst, std::string_view value,
141 const auto src_thread_id{fmt::format("({})|({})", lhs, min_thread_id)}; 141 const auto src_thread_id{fmt::format("({})|({})", lhs, min_thread_id)};
142 ctx.Add("shfl_in_bounds=int({})<=int({});", src_thread_id, max_thread_id); 142 ctx.Add("shfl_in_bounds=int({})<=int({});", src_thread_id, max_thread_id);
143 SetInBoundsFlag(ctx, inst); 143 SetInBoundsFlag(ctx, inst);
144 ctx.AddU32("{}=shfl_in_bounds?readInvocationARB({},{}):{};", inst, value, src_thread_id, value); 144 ctx.Add("shfl_result=readInvocationARB({},{});", value, src_thread_id);
145 ctx.AddU32("{}=shfl_in_bounds?shfl_result:{};", inst, value);
145} 146}
146 147
147void EmitShuffleUp(EmitContext& ctx, IR::Inst& inst, std::string_view value, std::string_view index, 148void EmitShuffleUp(EmitContext& ctx, IR::Inst& inst, std::string_view value, std::string_view index,
@@ -158,7 +159,8 @@ void EmitShuffleUp(EmitContext& ctx, IR::Inst& inst, std::string_view value, std
158 const auto src_thread_id{fmt::format("({}-{})", THREAD_ID, index)}; 159 const auto src_thread_id{fmt::format("({}-{})", THREAD_ID, index)};
159 ctx.Add("shfl_in_bounds=int({})>=int({});", src_thread_id, max_thread_id); 160 ctx.Add("shfl_in_bounds=int({})>=int({});", src_thread_id, max_thread_id);
160 SetInBoundsFlag(ctx, inst); 161 SetInBoundsFlag(ctx, inst);
161 ctx.AddU32("{}=shfl_in_bounds?readInvocationARB({},{}):{};", inst, value, src_thread_id, value); 162 ctx.Add("shfl_result=readInvocationARB({},{});", value, src_thread_id);
163 ctx.AddU32("{}=shfl_in_bounds?shfl_result:{};", inst, value);
162} 164}
163 165
164void EmitShuffleDown(EmitContext& ctx, IR::Inst& inst, std::string_view value, 166void EmitShuffleDown(EmitContext& ctx, IR::Inst& inst, std::string_view value,
@@ -175,7 +177,8 @@ void EmitShuffleDown(EmitContext& ctx, IR::Inst& inst, std::string_view value,
175 const auto src_thread_id{fmt::format("({}+{})", THREAD_ID, index)}; 177 const auto src_thread_id{fmt::format("({}+{})", THREAD_ID, index)};
176 ctx.Add("shfl_in_bounds=int({})<=int({});", src_thread_id, max_thread_id); 178 ctx.Add("shfl_in_bounds=int({})<=int({});", src_thread_id, max_thread_id);
177 SetInBoundsFlag(ctx, inst); 179 SetInBoundsFlag(ctx, inst);
178 ctx.AddU32("{}=shfl_in_bounds?readInvocationARB({},{}):{};", inst, value, src_thread_id, value); 180 ctx.Add("shfl_result=readInvocationARB({},{});", value, src_thread_id);
181 ctx.AddU32("{}=shfl_in_bounds?shfl_result:{};", inst, value);
179} 182}
180 183
181void EmitShuffleButterfly(EmitContext& ctx, IR::Inst& inst, std::string_view value, 184void EmitShuffleButterfly(EmitContext& ctx, IR::Inst& inst, std::string_view value,
@@ -193,7 +196,8 @@ void EmitShuffleButterfly(EmitContext& ctx, IR::Inst& inst, std::string_view val
193 const auto src_thread_id{fmt::format("({}^{})", THREAD_ID, index)}; 196 const auto src_thread_id{fmt::format("({}^{})", THREAD_ID, index)};
194 ctx.Add("shfl_in_bounds=int({})<=int({});", src_thread_id, max_thread_id); 197 ctx.Add("shfl_in_bounds=int({})<=int({});", src_thread_id, max_thread_id);
195 SetInBoundsFlag(ctx, inst); 198 SetInBoundsFlag(ctx, inst);
196 ctx.AddU32("{}=shfl_in_bounds?readInvocationARB({},{}):{};", inst, value, src_thread_id, value); 199 ctx.Add("shfl_result=readInvocationARB({},{});", value, src_thread_id);
200 ctx.AddU32("{}=shfl_in_bounds?shfl_result:{};", inst, value);
197} 201}
198 202
199void EmitFSwizzleAdd(EmitContext& ctx, IR::Inst& inst, std::string_view op_a, std::string_view op_b, 203void EmitFSwizzleAdd(EmitContext& ctx, IR::Inst& inst, std::string_view op_a, std::string_view op_b,
diff --git a/src/shader_recompiler/backend/spirv/emit_spirv_context_get_set.cpp b/src/shader_recompiler/backend/spirv/emit_spirv_context_get_set.cpp
index 2868fc57d..1d77426e0 100644
--- a/src/shader_recompiler/backend/spirv/emit_spirv_context_get_set.cpp
+++ b/src/shader_recompiler/backend/spirv/emit_spirv_context_get_set.cpp
@@ -111,16 +111,33 @@ Id GetCbuf(EmitContext& ctx, Id result_type, Id UniformDefinitions::*member_ptr,
111 } else if (element_size > 1) { 111 } else if (element_size > 1) {
112 const u32 log2_element_size{static_cast<u32>(std::countr_zero(element_size))}; 112 const u32 log2_element_size{static_cast<u32>(std::countr_zero(element_size))};
113 const Id shift{ctx.Const(log2_element_size)}; 113 const Id shift{ctx.Const(log2_element_size)};
114 buffer_offset = ctx.OpShiftRightArithmetic(ctx.U32[1], ctx.Def(offset), shift); 114 buffer_offset = ctx.OpShiftRightLogical(ctx.U32[1], ctx.Def(offset), shift);
115 } else { 115 } else {
116 buffer_offset = ctx.Def(offset); 116 buffer_offset = ctx.Def(offset);
117 } 117 }
118 if (!binding.IsImmediate()) { 118 if (!binding.IsImmediate()) {
119 return ctx.OpFunctionCall(result_type, indirect_func, ctx.Def(binding), buffer_offset); 119 return ctx.OpFunctionCall(result_type, indirect_func, ctx.Def(binding), buffer_offset);
120 } 120 }
121
121 const Id cbuf{ctx.cbufs[binding.U32()].*member_ptr}; 122 const Id cbuf{ctx.cbufs[binding.U32()].*member_ptr};
122 const Id access_chain{ctx.OpAccessChain(uniform_type, cbuf, ctx.u32_zero_value, buffer_offset)}; 123 const Id access_chain{ctx.OpAccessChain(uniform_type, cbuf, ctx.u32_zero_value, buffer_offset)};
123 return ctx.OpLoad(result_type, access_chain); 124 const Id val = ctx.OpLoad(result_type, access_chain);
125
126 if (offset.IsImmediate() || !ctx.profile.has_broken_robust) {
127 return val;
128 }
129
130 const auto is_float = UniformDefinitions::IsFloat(member_ptr);
131 const auto num_elements = UniformDefinitions::NumElements(member_ptr);
132 const std::array zero_vec{
133 is_float ? ctx.Const(0.0f) : ctx.Const(0u),
134 is_float ? ctx.Const(0.0f) : ctx.Const(0u),
135 is_float ? ctx.Const(0.0f) : ctx.Const(0u),
136 is_float ? ctx.Const(0.0f) : ctx.Const(0u),
137 };
138 const Id cond = ctx.OpULessThanEqual(ctx.TypeBool(), buffer_offset, ctx.Const(0xFFFFu));
139 const Id zero = ctx.OpCompositeConstruct(result_type, std::span(zero_vec.data(), num_elements));
140 return ctx.OpSelect(result_type, cond, val, zero);
124} 141}
125 142
126Id GetCbufU32(EmitContext& ctx, const IR::Value& binding, const IR::Value& offset) { 143Id GetCbufU32(EmitContext& ctx, const IR::Value& binding, const IR::Value& offset) {
@@ -138,7 +155,7 @@ Id GetCbufElement(EmitContext& ctx, Id vector, const IR::Value& offset, u32 inde
138 const u32 element{(offset.U32() / 4) % 4 + index_offset}; 155 const u32 element{(offset.U32() / 4) % 4 + index_offset};
139 return ctx.OpCompositeExtract(ctx.U32[1], vector, element); 156 return ctx.OpCompositeExtract(ctx.U32[1], vector, element);
140 } 157 }
141 const Id shift{ctx.OpShiftRightArithmetic(ctx.U32[1], ctx.Def(offset), ctx.Const(2u))}; 158 const Id shift{ctx.OpShiftRightLogical(ctx.U32[1], ctx.Def(offset), ctx.Const(2u))};
142 Id element{ctx.OpBitwiseAnd(ctx.U32[1], shift, ctx.Const(3u))}; 159 Id element{ctx.OpBitwiseAnd(ctx.U32[1], shift, ctx.Const(3u))};
143 if (index_offset > 0) { 160 if (index_offset > 0) {
144 element = ctx.OpIAdd(ctx.U32[1], element, ctx.Const(index_offset)); 161 element = ctx.OpIAdd(ctx.U32[1], element, ctx.Const(index_offset));
diff --git a/src/shader_recompiler/backend/spirv/spirv_emit_context.h b/src/shader_recompiler/backend/spirv/spirv_emit_context.h
index 7c49fd504..1aa79863d 100644
--- a/src/shader_recompiler/backend/spirv/spirv_emit_context.h
+++ b/src/shader_recompiler/backend/spirv/spirv_emit_context.h
@@ -64,6 +64,42 @@ struct UniformDefinitions {
64 Id F32{}; 64 Id F32{};
65 Id U32x2{}; 65 Id U32x2{};
66 Id U32x4{}; 66 Id U32x4{};
67
68 constexpr static size_t NumElements(Id UniformDefinitions::*member_ptr) {
69 if (member_ptr == &UniformDefinitions::U8) {
70 return 1;
71 }
72 if (member_ptr == &UniformDefinitions::S8) {
73 return 1;
74 }
75 if (member_ptr == &UniformDefinitions::U16) {
76 return 1;
77 }
78 if (member_ptr == &UniformDefinitions::S16) {
79 return 1;
80 }
81 if (member_ptr == &UniformDefinitions::U32) {
82 return 1;
83 }
84 if (member_ptr == &UniformDefinitions::F32) {
85 return 1;
86 }
87 if (member_ptr == &UniformDefinitions::U32x2) {
88 return 2;
89 }
90 if (member_ptr == &UniformDefinitions::U32x4) {
91 return 4;
92 }
93 ASSERT(false);
94 return 1;
95 }
96
97 constexpr static bool IsFloat(Id UniformDefinitions::*member_ptr) {
98 if (member_ptr == &UniformDefinitions::F32) {
99 return true;
100 }
101 return false;
102 }
67}; 103};
68 104
69struct StorageTypeDefinition { 105struct StorageTypeDefinition {
diff --git a/src/shader_recompiler/profile.h b/src/shader_recompiler/profile.h
index 9ca97f6a4..38d820db2 100644
--- a/src/shader_recompiler/profile.h
+++ b/src/shader_recompiler/profile.h
@@ -9,7 +9,6 @@ namespace Shader {
9 9
10struct Profile { 10struct Profile {
11 u32 supported_spirv{0x00010000}; 11 u32 supported_spirv{0x00010000};
12
13 bool unified_descriptor_binding{}; 12 bool unified_descriptor_binding{};
14 bool support_descriptor_aliasing{}; 13 bool support_descriptor_aliasing{};
15 bool support_int8{}; 14 bool support_int8{};
@@ -82,6 +81,9 @@ struct Profile {
82 bool has_broken_spirv_subgroup_mask_vector_extract_dynamic{}; 81 bool has_broken_spirv_subgroup_mask_vector_extract_dynamic{};
83 82
84 u32 gl_max_compute_smem_size{}; 83 u32 gl_max_compute_smem_size{};
84
85 /// Maxwell and earlier nVidia architectures have broken robust support
86 bool has_broken_robust{};
85}; 87};
86 88
87} // namespace Shader 89} // namespace Shader
diff --git a/src/video_core/fence_manager.h b/src/video_core/fence_manager.h
index 805a89900..c0e6471fe 100644
--- a/src/video_core/fence_manager.h
+++ b/src/video_core/fence_manager.h
@@ -86,7 +86,10 @@ public:
86 uncommitted_operations.emplace_back(std::move(func)); 86 uncommitted_operations.emplace_back(std::move(func));
87 } 87 }
88 pending_operations.emplace_back(std::move(uncommitted_operations)); 88 pending_operations.emplace_back(std::move(uncommitted_operations));
89 QueueFence(new_fence); 89 {
90 std::scoped_lock lock{buffer_cache.mutex, texture_cache.mutex};
91 QueueFence(new_fence);
92 }
90 if (!delay_fence) { 93 if (!delay_fence) {
91 func(); 94 func();
92 } 95 }
diff --git a/src/video_core/renderer_vulkan/renderer_vulkan.cpp b/src/video_core/renderer_vulkan/renderer_vulkan.cpp
index c4c30d807..7e7a80740 100644
--- a/src/video_core/renderer_vulkan/renderer_vulkan.cpp
+++ b/src/video_core/renderer_vulkan/renderer_vulkan.cpp
@@ -132,12 +132,16 @@ void RendererVulkan::SwapBuffers(const Tegra::FramebufferConfig* framebuffer) {
132 const bool use_accelerated = 132 const bool use_accelerated =
133 rasterizer.AccelerateDisplay(*framebuffer, framebuffer_addr, framebuffer->stride); 133 rasterizer.AccelerateDisplay(*framebuffer, framebuffer_addr, framebuffer->stride);
134 const bool is_srgb = use_accelerated && screen_info.is_srgb; 134 const bool is_srgb = use_accelerated && screen_info.is_srgb;
135 RenderScreenshot(*framebuffer, use_accelerated);
136 135
137 Frame* frame = present_manager.GetRenderFrame(); 136 {
138 blit_screen.DrawToSwapchain(frame, *framebuffer, use_accelerated, is_srgb); 137 std::scoped_lock lock{rasterizer.LockCaches()};
139 scheduler.Flush(*frame->render_ready); 138 RenderScreenshot(*framebuffer, use_accelerated);
140 present_manager.Present(frame); 139
140 Frame* frame = present_manager.GetRenderFrame();
141 blit_screen.DrawToSwapchain(frame, *framebuffer, use_accelerated, is_srgb);
142 scheduler.Flush(*frame->render_ready);
143 present_manager.Present(frame);
144 }
141 145
142 gpu.RendererFrameEndNotify(); 146 gpu.RendererFrameEndNotify();
143 rasterizer.TickFrame(); 147 rasterizer.TickFrame();
diff --git a/src/video_core/renderer_vulkan/vk_pipeline_cache.cpp b/src/video_core/renderer_vulkan/vk_pipeline_cache.cpp
index a1ec1a100..804b95989 100644
--- a/src/video_core/renderer_vulkan/vk_pipeline_cache.cpp
+++ b/src/video_core/renderer_vulkan/vk_pipeline_cache.cpp
@@ -356,7 +356,11 @@ PipelineCache::PipelineCache(RasterizerVulkan& rasterizer_, const Device& device
356 .has_broken_fp16_float_controls = driver_id == VK_DRIVER_ID_NVIDIA_PROPRIETARY, 356 .has_broken_fp16_float_controls = driver_id == VK_DRIVER_ID_NVIDIA_PROPRIETARY,
357 .ignore_nan_fp_comparisons = false, 357 .ignore_nan_fp_comparisons = false,
358 .has_broken_spirv_subgroup_mask_vector_extract_dynamic = 358 .has_broken_spirv_subgroup_mask_vector_extract_dynamic =
359 driver_id == VK_DRIVER_ID_QUALCOMM_PROPRIETARY}; 359 driver_id == VK_DRIVER_ID_QUALCOMM_PROPRIETARY,
360 .has_broken_robust =
361 device.IsNvidia() && device.GetNvidiaArch() <= NvidiaArchitecture::Arch_Maxwell,
362 };
363
360 host_info = Shader::HostTranslateInfo{ 364 host_info = Shader::HostTranslateInfo{
361 .support_float64 = device.IsFloat64Supported(), 365 .support_float64 = device.IsFloat64Supported(),
362 .support_float16 = device.IsFloat16Supported(), 366 .support_float16 = device.IsFloat16Supported(),
diff --git a/src/video_core/renderer_vulkan/vk_rasterizer.cpp b/src/video_core/renderer_vulkan/vk_rasterizer.cpp
index 61d03daae..465eac37e 100644
--- a/src/video_core/renderer_vulkan/vk_rasterizer.cpp
+++ b/src/video_core/renderer_vulkan/vk_rasterizer.cpp
@@ -198,7 +198,7 @@ void RasterizerVulkan::PrepareDraw(bool is_indexed, Func&& draw_func) {
198 if (!pipeline) { 198 if (!pipeline) {
199 return; 199 return;
200 } 200 }
201 std::scoped_lock lock{buffer_cache.mutex, texture_cache.mutex}; 201 std::scoped_lock lock{LockCaches()};
202 // update engine as channel may be different. 202 // update engine as channel may be different.
203 pipeline->SetEngine(maxwell3d, gpu_memory); 203 pipeline->SetEngine(maxwell3d, gpu_memory);
204 pipeline->Configure(is_indexed); 204 pipeline->Configure(is_indexed);
@@ -708,6 +708,7 @@ void RasterizerVulkan::TiledCacheBarrier() {
708} 708}
709 709
710void RasterizerVulkan::FlushCommands() { 710void RasterizerVulkan::FlushCommands() {
711 std::scoped_lock lock{LockCaches()};
711 if (draw_counter == 0) { 712 if (draw_counter == 0) {
712 return; 713 return;
713 } 714 }
@@ -805,6 +806,7 @@ void RasterizerVulkan::FlushWork() {
805 if ((++draw_counter & 7) != 7) { 806 if ((++draw_counter & 7) != 7) {
806 return; 807 return;
807 } 808 }
809 std::scoped_lock lock{LockCaches()};
808 if (draw_counter < DRAWS_TO_DISPATCH) { 810 if (draw_counter < DRAWS_TO_DISPATCH) {
809 // Send recorded tasks to the worker thread 811 // Send recorded tasks to the worker thread
810 scheduler.DispatchWork(); 812 scheduler.DispatchWork();
@@ -1499,7 +1501,7 @@ void RasterizerVulkan::UpdateVertexInput(Tegra::Engines::Maxwell3D::Regs& regs)
1499void RasterizerVulkan::InitializeChannel(Tegra::Control::ChannelState& channel) { 1501void RasterizerVulkan::InitializeChannel(Tegra::Control::ChannelState& channel) {
1500 CreateChannel(channel); 1502 CreateChannel(channel);
1501 { 1503 {
1502 std::scoped_lock lock{buffer_cache.mutex, texture_cache.mutex}; 1504 std::scoped_lock lock{LockCaches()};
1503 texture_cache.CreateChannel(channel); 1505 texture_cache.CreateChannel(channel);
1504 buffer_cache.CreateChannel(channel); 1506 buffer_cache.CreateChannel(channel);
1505 } 1507 }
@@ -1512,7 +1514,7 @@ void RasterizerVulkan::BindChannel(Tegra::Control::ChannelState& channel) {
1512 const s32 channel_id = channel.bind_id; 1514 const s32 channel_id = channel.bind_id;
1513 BindToChannel(channel_id); 1515 BindToChannel(channel_id);
1514 { 1516 {
1515 std::scoped_lock lock{buffer_cache.mutex, texture_cache.mutex}; 1517 std::scoped_lock lock{LockCaches()};
1516 texture_cache.BindToChannel(channel_id); 1518 texture_cache.BindToChannel(channel_id);
1517 buffer_cache.BindToChannel(channel_id); 1519 buffer_cache.BindToChannel(channel_id);
1518 } 1520 }
@@ -1525,7 +1527,7 @@ void RasterizerVulkan::BindChannel(Tegra::Control::ChannelState& channel) {
1525void RasterizerVulkan::ReleaseChannel(s32 channel_id) { 1527void RasterizerVulkan::ReleaseChannel(s32 channel_id) {
1526 EraseChannel(channel_id); 1528 EraseChannel(channel_id);
1527 { 1529 {
1528 std::scoped_lock lock{buffer_cache.mutex, texture_cache.mutex}; 1530 std::scoped_lock lock{LockCaches()};
1529 texture_cache.EraseChannel(channel_id); 1531 texture_cache.EraseChannel(channel_id);
1530 buffer_cache.EraseChannel(channel_id); 1532 buffer_cache.EraseChannel(channel_id);
1531 } 1533 }
diff --git a/src/video_core/renderer_vulkan/vk_rasterizer.h b/src/video_core/renderer_vulkan/vk_rasterizer.h
index ad069556c..ce3dfbaab 100644
--- a/src/video_core/renderer_vulkan/vk_rasterizer.h
+++ b/src/video_core/renderer_vulkan/vk_rasterizer.h
@@ -133,6 +133,10 @@ public:
133 133
134 void ReleaseChannel(s32 channel_id) override; 134 void ReleaseChannel(s32 channel_id) override;
135 135
136 std::scoped_lock<std::recursive_mutex, std::recursive_mutex> LockCaches() {
137 return std::scoped_lock{buffer_cache.mutex, texture_cache.mutex};
138 }
139
136private: 140private:
137 static constexpr size_t MAX_TEXTURES = 192; 141 static constexpr size_t MAX_TEXTURES = 192;
138 static constexpr size_t MAX_IMAGES = 48; 142 static constexpr size_t MAX_IMAGES = 48;
diff --git a/src/video_core/vulkan_common/vulkan_device.cpp b/src/video_core/vulkan_common/vulkan_device.cpp
index 876cec2e8..e518756d2 100644
--- a/src/video_core/vulkan_common/vulkan_device.cpp
+++ b/src/video_core/vulkan_common/vulkan_device.cpp
@@ -83,15 +83,6 @@ constexpr std::array VK_FORMAT_A4B4G4R4_UNORM_PACK16{
83 83
84} // namespace Alternatives 84} // namespace Alternatives
85 85
86enum class NvidiaArchitecture {
87 KeplerOrOlder,
88 Maxwell,
89 Pascal,
90 Volta,
91 Turing,
92 AmpereOrNewer,
93};
94
95template <typename T> 86template <typename T>
96void SetNext(void**& next, T& data) { 87void SetNext(void**& next, T& data) {
97 *next = &data; 88 *next = &data;
@@ -326,9 +317,9 @@ NvidiaArchitecture GetNvidiaArchitecture(vk::PhysicalDevice physical,
326 if (shading_rate_props.primitiveFragmentShadingRateWithMultipleViewports) { 317 if (shading_rate_props.primitiveFragmentShadingRateWithMultipleViewports) {
327 // Only Ampere and newer support this feature 318 // Only Ampere and newer support this feature
328 // TODO: Find a way to differentiate Ampere and Ada 319 // TODO: Find a way to differentiate Ampere and Ada
329 return NvidiaArchitecture::AmpereOrNewer; 320 return NvidiaArchitecture::Arch_AmpereOrNewer;
330 } 321 }
331 return NvidiaArchitecture::Turing; 322 return NvidiaArchitecture::Arch_Turing;
332 } 323 }
333 324
334 if (exts.contains(VK_EXT_BLEND_OPERATION_ADVANCED_EXTENSION_NAME)) { 325 if (exts.contains(VK_EXT_BLEND_OPERATION_ADVANCED_EXTENSION_NAME)) {
@@ -340,7 +331,7 @@ NvidiaArchitecture GetNvidiaArchitecture(vk::PhysicalDevice physical,
340 physical_properties.pNext = &advanced_blending_props; 331 physical_properties.pNext = &advanced_blending_props;
341 physical.GetProperties2(physical_properties); 332 physical.GetProperties2(physical_properties);
342 if (advanced_blending_props.advancedBlendMaxColorAttachments == 1) { 333 if (advanced_blending_props.advancedBlendMaxColorAttachments == 1) {
343 return NvidiaArchitecture::Maxwell; 334 return NvidiaArchitecture::Arch_Maxwell;
344 } 335 }
345 336
346 if (exts.contains(VK_EXT_CONSERVATIVE_RASTERIZATION_EXTENSION_NAME)) { 337 if (exts.contains(VK_EXT_CONSERVATIVE_RASTERIZATION_EXTENSION_NAME)) {
@@ -350,13 +341,13 @@ NvidiaArchitecture GetNvidiaArchitecture(vk::PhysicalDevice physical,
350 physical_properties.pNext = &conservative_raster_props; 341 physical_properties.pNext = &conservative_raster_props;
351 physical.GetProperties2(physical_properties); 342 physical.GetProperties2(physical_properties);
352 if (conservative_raster_props.degenerateLinesRasterized) { 343 if (conservative_raster_props.degenerateLinesRasterized) {
353 return NvidiaArchitecture::Volta; 344 return NvidiaArchitecture::Arch_Volta;
354 } 345 }
355 return NvidiaArchitecture::Pascal; 346 return NvidiaArchitecture::Arch_Pascal;
356 } 347 }
357 } 348 }
358 349
359 return NvidiaArchitecture::KeplerOrOlder; 350 return NvidiaArchitecture::Arch_KeplerOrOlder;
360} 351}
361 352
362std::vector<const char*> ExtensionListForVulkan( 353std::vector<const char*> ExtensionListForVulkan(
@@ -436,6 +427,10 @@ Device::Device(VkInstance instance_, vk::PhysicalDevice physical_, VkSurfaceKHR
436 throw vk::Exception(VK_ERROR_INCOMPATIBLE_DRIVER); 427 throw vk::Exception(VK_ERROR_INCOMPATIBLE_DRIVER);
437 } 428 }
438 429
430 if (is_nvidia) {
431 nvidia_arch = GetNvidiaArchitecture(physical, supported_extensions);
432 }
433
439 SetupFamilies(surface); 434 SetupFamilies(surface);
440 const auto queue_cis = GetDeviceQueueCreateInfos(); 435 const auto queue_cis = GetDeviceQueueCreateInfos();
441 436
@@ -532,11 +527,11 @@ Device::Device(VkInstance instance_, vk::PhysicalDevice physical_, VkSurfaceKHR
532 527
533 if (is_nvidia) { 528 if (is_nvidia) {
534 const u32 nv_major_version = (properties.properties.driverVersion >> 22) & 0x3ff; 529 const u32 nv_major_version = (properties.properties.driverVersion >> 22) & 0x3ff;
535 const auto arch = GetNvidiaArchitecture(physical, supported_extensions); 530 const auto arch = GetNvidiaArch();
536 if (arch >= NvidiaArchitecture::AmpereOrNewer) { 531 if (arch >= NvidiaArchitecture::Arch_AmpereOrNewer) {
537 LOG_WARNING(Render_Vulkan, "Ampere and newer have broken float16 math"); 532 LOG_WARNING(Render_Vulkan, "Ampere and newer have broken float16 math");
538 features.shader_float16_int8.shaderFloat16 = false; 533 features.shader_float16_int8.shaderFloat16 = false;
539 } else if (arch <= NvidiaArchitecture::Volta) { 534 } else if (arch <= NvidiaArchitecture::Arch_Volta) {
540 if (nv_major_version < 527) { 535 if (nv_major_version < 527) {
541 LOG_WARNING(Render_Vulkan, "Volta and older have broken VK_KHR_push_descriptor"); 536 LOG_WARNING(Render_Vulkan, "Volta and older have broken VK_KHR_push_descriptor");
542 RemoveExtension(extensions.push_descriptor, VK_KHR_PUSH_DESCRIPTOR_EXTENSION_NAME); 537 RemoveExtension(extensions.push_descriptor, VK_KHR_PUSH_DESCRIPTOR_EXTENSION_NAME);
@@ -686,8 +681,8 @@ Device::Device(VkInstance instance_, vk::PhysicalDevice physical_, VkSurfaceKHR
686 RemoveExtension(extensions.push_descriptor, VK_KHR_PUSH_DESCRIPTOR_EXTENSION_NAME); 681 RemoveExtension(extensions.push_descriptor, VK_KHR_PUSH_DESCRIPTOR_EXTENSION_NAME);
687 } 682 }
688 } else if (extensions.push_descriptor && is_nvidia) { 683 } else if (extensions.push_descriptor && is_nvidia) {
689 const auto arch = GetNvidiaArchitecture(physical, supported_extensions); 684 const auto arch = GetNvidiaArch();
690 if (arch <= NvidiaArchitecture::Pascal) { 685 if (arch <= NvidiaArchitecture::Arch_Pascal) {
691 LOG_WARNING(Render_Vulkan, 686 LOG_WARNING(Render_Vulkan,
692 "Pascal and older architectures have broken VK_KHR_push_descriptor"); 687 "Pascal and older architectures have broken VK_KHR_push_descriptor");
693 RemoveExtension(extensions.push_descriptor, VK_KHR_PUSH_DESCRIPTOR_EXTENSION_NAME); 688 RemoveExtension(extensions.push_descriptor, VK_KHR_PUSH_DESCRIPTOR_EXTENSION_NAME);
diff --git a/src/video_core/vulkan_common/vulkan_device.h b/src/video_core/vulkan_common/vulkan_device.h
index 282a2925d..b213ed7dd 100644
--- a/src/video_core/vulkan_common/vulkan_device.h
+++ b/src/video_core/vulkan_common/vulkan_device.h
@@ -177,6 +177,15 @@ enum class FormatType { Linear, Optimal, Buffer };
177/// Subgroup size of the guest emulated hardware (Nvidia has 32 threads per subgroup). 177/// Subgroup size of the guest emulated hardware (Nvidia has 32 threads per subgroup).
178const u32 GuestWarpSize = 32; 178const u32 GuestWarpSize = 32;
179 179
180enum class NvidiaArchitecture {
181 Arch_KeplerOrOlder,
182 Arch_Maxwell,
183 Arch_Pascal,
184 Arch_Volta,
185 Arch_Turing,
186 Arch_AmpereOrNewer,
187};
188
180/// Handles data specific to a physical device. 189/// Handles data specific to a physical device.
181class Device { 190class Device {
182public: 191public:
@@ -670,6 +679,14 @@ public:
670 return false; 679 return false;
671 } 680 }
672 681
682 bool IsNvidia() const noexcept {
683 return properties.driver.driverID == VK_DRIVER_ID_NVIDIA_PROPRIETARY;
684 }
685
686 NvidiaArchitecture GetNvidiaArch() const noexcept {
687 return nvidia_arch;
688 }
689
673private: 690private:
674 /// Checks if the physical device is suitable and configures the object state 691 /// Checks if the physical device is suitable and configures the object state
675 /// with all necessary info about its properties. 692 /// with all necessary info about its properties.
@@ -788,6 +805,7 @@ private:
788 bool supports_conditional_barriers{}; ///< Allows barriers in conditional control flow. 805 bool supports_conditional_barriers{}; ///< Allows barriers in conditional control flow.
789 u64 device_access_memory{}; ///< Total size of device local memory in bytes. 806 u64 device_access_memory{}; ///< Total size of device local memory in bytes.
790 u32 sets_per_pool{}; ///< Sets per Description Pool 807 u32 sets_per_pool{}; ///< Sets per Description Pool
808 NvidiaArchitecture nvidia_arch{NvidiaArchitecture::Arch_AmpereOrNewer};
791 809
792 // Telemetry parameters 810 // Telemetry parameters
793 std::set<std::string, std::less<>> supported_extensions; ///< Reported Vulkan extensions. 811 std::set<std::string, std::less<>> supported_extensions; ///< Reported Vulkan extensions.
diff --git a/src/video_core/vulkan_common/vulkan_wrapper.cpp b/src/video_core/vulkan_common/vulkan_wrapper.cpp
index 2f3254a97..70cf14afa 100644
--- a/src/video_core/vulkan_common/vulkan_wrapper.cpp
+++ b/src/video_core/vulkan_common/vulkan_wrapper.cpp
@@ -522,7 +522,7 @@ Instance Instance::Create(u32 version, Span<const char*> layers, Span<const char
522 .applicationVersion = VK_MAKE_VERSION(0, 1, 0), 522 .applicationVersion = VK_MAKE_VERSION(0, 1, 0),
523 .pEngineName = "yuzu Emulator", 523 .pEngineName = "yuzu Emulator",
524 .engineVersion = VK_MAKE_VERSION(0, 1, 0), 524 .engineVersion = VK_MAKE_VERSION(0, 1, 0),
525 .apiVersion = version, 525 .apiVersion = VK_API_VERSION_1_3,
526 }; 526 };
527 const VkInstanceCreateInfo ci{ 527 const VkInstanceCreateInfo ci{
528 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, 528 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
diff --git a/src/yuzu/applets/qt_controller.cpp b/src/yuzu/applets/qt_controller.cpp
index ca0e14fad..515cb7ce6 100644
--- a/src/yuzu/applets/qt_controller.cpp
+++ b/src/yuzu/applets/qt_controller.cpp
@@ -155,18 +155,27 @@ QtControllerSelectorDialog::QtControllerSelectorDialog(
155 UpdateBorderColor(i); 155 UpdateBorderColor(i);
156 156
157 connect(player_groupboxes[i], &QGroupBox::toggled, [this, i](bool checked) { 157 connect(player_groupboxes[i], &QGroupBox::toggled, [this, i](bool checked) {
158 if (checked) { 158 // Reconnect current controller if it was the last one checked
159 // Hide eventual error message about number of controllers 159 // (player number was reduced by more than one)
160 ui->labelError->setVisible(false); 160 const bool reconnect_first = !checked && i < player_groupboxes.size() - 1 &&
161 for (std::size_t index = 0; index <= i; ++index) { 161 player_groupboxes[i + 1]->isChecked();
162 connected_controller_checkboxes[index]->setChecked(checked); 162
163 } 163 // Ensures that connecting a controller changes the number of players
164 } else { 164 if (connected_controller_checkboxes[i]->isChecked() != checked) {
165 for (std::size_t index = i; index < NUM_PLAYERS; ++index) { 165 // Ensures that the players are always connected in sequential order
166 connected_controller_checkboxes[index]->setChecked(checked); 166 PropagatePlayerNumberChanged(i, checked, reconnect_first);
167 }
168 } 167 }
169 }); 168 });
169 connect(connected_controller_checkboxes[i], &QCheckBox::clicked, [this, i](bool checked) {
170 // Reconnect current controller if it was the last one checked
171 // (player number was reduced by more than one)
172 const bool reconnect_first = !checked &&
173 i < connected_controller_checkboxes.size() - 1 &&
174 connected_controller_checkboxes[i + 1]->isChecked();
175
176 // Ensures that the players are always connected in sequential order
177 PropagatePlayerNumberChanged(i, checked, reconnect_first);
178 });
170 179
171 connect(emulated_controllers[i], qOverload<int>(&QComboBox::currentIndexChanged), 180 connect(emulated_controllers[i], qOverload<int>(&QComboBox::currentIndexChanged),
172 [this, i](int) { 181 [this, i](int) {
@@ -668,6 +677,29 @@ void QtControllerSelectorDialog::UpdateDockedState(bool is_handheld) {
668 } 677 }
669} 678}
670 679
680void QtControllerSelectorDialog::PropagatePlayerNumberChanged(size_t player_index, bool checked,
681 bool reconnect_current) {
682 connected_controller_checkboxes[player_index]->setChecked(checked);
683 // Hide eventual error message about number of controllers
684 ui->labelError->setVisible(false);
685
686 if (checked) {
687 // Check all previous buttons when checked
688 if (player_index > 0) {
689 PropagatePlayerNumberChanged(player_index - 1, checked);
690 }
691 } else {
692 // Unchecked all following buttons when unchecked
693 if (player_index < connected_controller_checkboxes.size() - 1) {
694 PropagatePlayerNumberChanged(player_index + 1, checked);
695 }
696 }
697
698 if (reconnect_current) {
699 connected_controller_checkboxes[player_index]->setCheckState(Qt::Checked);
700 }
701}
702
671void QtControllerSelectorDialog::DisableUnsupportedPlayers() { 703void QtControllerSelectorDialog::DisableUnsupportedPlayers() {
672 const auto max_supported_players = parameters.enable_single_mode ? 1 : parameters.max_players; 704 const auto max_supported_players = parameters.enable_single_mode ? 1 : parameters.max_players;
673 705
diff --git a/src/yuzu/applets/qt_controller.h b/src/yuzu/applets/qt_controller.h
index 7f0673d06..e5372495d 100644
--- a/src/yuzu/applets/qt_controller.h
+++ b/src/yuzu/applets/qt_controller.h
@@ -100,6 +100,10 @@ private:
100 // Updates the console mode. 100 // Updates the console mode.
101 void UpdateDockedState(bool is_handheld); 101 void UpdateDockedState(bool is_handheld);
102 102
103 // Enable preceding controllers or disable following ones
104 void PropagatePlayerNumberChanged(size_t player_index, bool checked,
105 bool reconnect_current = false);
106
103 // Disables and disconnects unsupported players based on the given parameters. 107 // Disables and disconnects unsupported players based on the given parameters.
104 void DisableUnsupportedPlayers(); 108 void DisableUnsupportedPlayers();
105 109
diff --git a/src/yuzu/configuration/config.cpp b/src/yuzu/configuration/config.cpp
index d5157c502..baa3e55f3 100644
--- a/src/yuzu/configuration/config.cpp
+++ b/src/yuzu/configuration/config.cpp
@@ -114,7 +114,7 @@ const std::map<Settings::ShaderBackend, QString> Config::shader_backend_texts_ma
114// This must be in alphabetical order according to action name as it must have the same order as 114// This must be in alphabetical order according to action name as it must have the same order as
115// UISetting::values.shortcuts, which is alphabetically ordered. 115// UISetting::values.shortcuts, which is alphabetically ordered.
116// clang-format off 116// clang-format off
117const std::array<UISettings::Shortcut, 22> Config::default_hotkeys{{ 117const std::array<UISettings::Shortcut, 23> Config::default_hotkeys{{
118 {QStringLiteral(QT_TRANSLATE_NOOP("Hotkeys", "Audio Mute/Unmute")), QStringLiteral(QT_TRANSLATE_NOOP("Hotkeys", "Main Window")), {QStringLiteral("Ctrl+M"), QStringLiteral("Home+Dpad_Right"), Qt::WindowShortcut, false}}, 118 {QStringLiteral(QT_TRANSLATE_NOOP("Hotkeys", "Audio Mute/Unmute")), QStringLiteral(QT_TRANSLATE_NOOP("Hotkeys", "Main Window")), {QStringLiteral("Ctrl+M"), QStringLiteral("Home+Dpad_Right"), Qt::WindowShortcut, false}},
119 {QStringLiteral(QT_TRANSLATE_NOOP("Hotkeys", "Audio Volume Down")), QStringLiteral(QT_TRANSLATE_NOOP("Hotkeys", "Main Window")), {QStringLiteral("-"), QStringLiteral("Home+Dpad_Down"), Qt::ApplicationShortcut, true}}, 119 {QStringLiteral(QT_TRANSLATE_NOOP("Hotkeys", "Audio Volume Down")), QStringLiteral(QT_TRANSLATE_NOOP("Hotkeys", "Main Window")), {QStringLiteral("-"), QStringLiteral("Home+Dpad_Down"), Qt::ApplicationShortcut, true}},
120 {QStringLiteral(QT_TRANSLATE_NOOP("Hotkeys", "Audio Volume Up")), QStringLiteral(QT_TRANSLATE_NOOP("Hotkeys", "Main Window")), {QStringLiteral("="), QStringLiteral("Home+Dpad_Up"), Qt::ApplicationShortcut, true}}, 120 {QStringLiteral(QT_TRANSLATE_NOOP("Hotkeys", "Audio Volume Up")), QStringLiteral(QT_TRANSLATE_NOOP("Hotkeys", "Main Window")), {QStringLiteral("="), QStringLiteral("Home+Dpad_Up"), Qt::ApplicationShortcut, true}},
@@ -136,6 +136,7 @@ const std::array<UISettings::Shortcut, 22> Config::default_hotkeys{{
136 {QStringLiteral(QT_TRANSLATE_NOOP("Hotkeys", "Toggle Filter Bar")), QStringLiteral(QT_TRANSLATE_NOOP("Hotkeys", "Main Window")), {QStringLiteral("Ctrl+F"), QStringLiteral(""), Qt::WindowShortcut, false}}, 136 {QStringLiteral(QT_TRANSLATE_NOOP("Hotkeys", "Toggle Filter Bar")), QStringLiteral(QT_TRANSLATE_NOOP("Hotkeys", "Main Window")), {QStringLiteral("Ctrl+F"), QStringLiteral(""), Qt::WindowShortcut, false}},
137 {QStringLiteral(QT_TRANSLATE_NOOP("Hotkeys", "Toggle Framerate Limit")), QStringLiteral(QT_TRANSLATE_NOOP("Hotkeys", "Main Window")), {QStringLiteral("Ctrl+U"), QStringLiteral("Home+Y"), Qt::ApplicationShortcut, false}}, 137 {QStringLiteral(QT_TRANSLATE_NOOP("Hotkeys", "Toggle Framerate Limit")), QStringLiteral(QT_TRANSLATE_NOOP("Hotkeys", "Main Window")), {QStringLiteral("Ctrl+U"), QStringLiteral("Home+Y"), Qt::ApplicationShortcut, false}},
138 {QStringLiteral(QT_TRANSLATE_NOOP("Hotkeys", "Toggle Mouse Panning")), QStringLiteral(QT_TRANSLATE_NOOP("Hotkeys", "Main Window")), {QStringLiteral("Ctrl+F9"), QStringLiteral(""), Qt::ApplicationShortcut, false}}, 138 {QStringLiteral(QT_TRANSLATE_NOOP("Hotkeys", "Toggle Mouse Panning")), QStringLiteral(QT_TRANSLATE_NOOP("Hotkeys", "Main Window")), {QStringLiteral("Ctrl+F9"), QStringLiteral(""), Qt::ApplicationShortcut, false}},
139 {QStringLiteral(QT_TRANSLATE_NOOP("Hotkeys", "Toggle Renderdoc Capture")), QStringLiteral(QT_TRANSLATE_NOOP("Hotkeys", "Main Window")), {QStringLiteral(""), QStringLiteral(""), Qt::ApplicationShortcut, false}},
139 {QStringLiteral(QT_TRANSLATE_NOOP("Hotkeys", "Toggle Status Bar")), QStringLiteral(QT_TRANSLATE_NOOP("Hotkeys", "Main Window")), {QStringLiteral("Ctrl+S"), QStringLiteral(""), Qt::WindowShortcut, false}}, 140 {QStringLiteral(QT_TRANSLATE_NOOP("Hotkeys", "Toggle Status Bar")), QStringLiteral(QT_TRANSLATE_NOOP("Hotkeys", "Main Window")), {QStringLiteral("Ctrl+S"), QStringLiteral(""), Qt::WindowShortcut, false}},
140}}; 141}};
141// clang-format on 142// clang-format on
diff --git a/src/yuzu/configuration/config.h b/src/yuzu/configuration/config.h
index 727feebfb..74ec4f771 100644
--- a/src/yuzu/configuration/config.h
+++ b/src/yuzu/configuration/config.h
@@ -48,7 +48,7 @@ public:
48 default_mouse_buttons; 48 default_mouse_buttons;
49 static const std::array<int, Settings::NativeKeyboard::NumKeyboardKeys> default_keyboard_keys; 49 static const std::array<int, Settings::NativeKeyboard::NumKeyboardKeys> default_keyboard_keys;
50 static const std::array<int, Settings::NativeKeyboard::NumKeyboardMods> default_keyboard_mods; 50 static const std::array<int, Settings::NativeKeyboard::NumKeyboardMods> default_keyboard_mods;
51 static const std::array<UISettings::Shortcut, 22> default_hotkeys; 51 static const std::array<UISettings::Shortcut, 23> default_hotkeys;
52 52
53 static const std::map<Settings::AntiAliasing, QString> anti_aliasing_texts_map; 53 static const std::map<Settings::AntiAliasing, QString> anti_aliasing_texts_map;
54 static const std::map<Settings::ScalingFilter, QString> scaling_filter_texts_map; 54 static const std::map<Settings::ScalingFilter, QString> scaling_filter_texts_map;
diff --git a/src/yuzu/configuration/configure_hotkeys.cpp b/src/yuzu/configuration/configure_hotkeys.cpp
index 0b2a965f8..68e21cd84 100644
--- a/src/yuzu/configuration/configure_hotkeys.cpp
+++ b/src/yuzu/configuration/configure_hotkeys.cpp
@@ -319,6 +319,13 @@ void ConfigureHotkeys::ApplyConfiguration(HotkeyRegistry& registry) {
319void ConfigureHotkeys::RestoreDefaults() { 319void ConfigureHotkeys::RestoreDefaults() {
320 for (int r = 0; r < model->rowCount(); ++r) { 320 for (int r = 0; r < model->rowCount(); ++r) {
321 const QStandardItem* parent = model->item(r, 0); 321 const QStandardItem* parent = model->item(r, 0);
322 const int hotkey_size = static_cast<int>(Config::default_hotkeys.size());
323
324 if (hotkey_size != parent->rowCount()) {
325 QMessageBox::warning(this, tr("Invalid hotkey settings"),
326 tr("An error occurred. Please report this issue on github."));
327 return;
328 }
322 329
323 for (int r2 = 0; r2 < parent->rowCount(); ++r2) { 330 for (int r2 = 0; r2 < parent->rowCount(); ++r2) {
324 model->item(r, 0) 331 model->item(r, 0)
diff --git a/src/yuzu/configuration/configure_input.cpp b/src/yuzu/configuration/configure_input.cpp
index 5a48e388b..3dcad2701 100644
--- a/src/yuzu/configuration/configure_input.cpp
+++ b/src/yuzu/configuration/configure_input.cpp
@@ -101,13 +101,13 @@ void ConfigureInput::Initialize(InputCommon::InputSubsystem* input_subsystem,
101 ui->tabPlayer5, ui->tabPlayer6, ui->tabPlayer7, ui->tabPlayer8, 101 ui->tabPlayer5, ui->tabPlayer6, ui->tabPlayer7, ui->tabPlayer8,
102 }; 102 };
103 103
104 player_connected = { 104 connected_controller_checkboxes = {
105 ui->checkboxPlayer1Connected, ui->checkboxPlayer2Connected, ui->checkboxPlayer3Connected, 105 ui->checkboxPlayer1Connected, ui->checkboxPlayer2Connected, ui->checkboxPlayer3Connected,
106 ui->checkboxPlayer4Connected, ui->checkboxPlayer5Connected, ui->checkboxPlayer6Connected, 106 ui->checkboxPlayer4Connected, ui->checkboxPlayer5Connected, ui->checkboxPlayer6Connected,
107 ui->checkboxPlayer7Connected, ui->checkboxPlayer8Connected, 107 ui->checkboxPlayer7Connected, ui->checkboxPlayer8Connected,
108 }; 108 };
109 109
110 std::array<QLabel*, 8> player_connected_labels = { 110 std::array<QLabel*, 8> connected_controller_labels = {
111 ui->label, ui->label_3, ui->label_4, ui->label_5, 111 ui->label, ui->label_3, ui->label_4, ui->label_5,
112 ui->label_6, ui->label_7, ui->label_8, ui->label_9, 112 ui->label_6, ui->label_7, ui->label_8, ui->label_9,
113 }; 113 };
@@ -115,23 +115,37 @@ void ConfigureInput::Initialize(InputCommon::InputSubsystem* input_subsystem,
115 for (std::size_t i = 0; i < player_tabs.size(); ++i) { 115 for (std::size_t i = 0; i < player_tabs.size(); ++i) {
116 player_tabs[i]->setLayout(new QHBoxLayout(player_tabs[i])); 116 player_tabs[i]->setLayout(new QHBoxLayout(player_tabs[i]));
117 player_tabs[i]->layout()->addWidget(player_controllers[i]); 117 player_tabs[i]->layout()->addWidget(player_controllers[i]);
118 connect(player_connected[i], &QCheckBox::clicked, [this, i](int checked) { 118 connect(player_controllers[i], &ConfigureInputPlayer::Connected, [this, i](bool checked) {
119 // Ensures that the controllers are always connected in sequential order 119 // Ensures that connecting a controller changes the number of players
120 this->propagateMouseClickOnPlayers(i, checked, true); 120 if (connected_controller_checkboxes[i]->isChecked() != checked) {
121 // Ensures that the players are always connected in sequential order
122 PropagatePlayerNumberChanged(i, checked);
123 }
124 });
125 connect(connected_controller_checkboxes[i], &QCheckBox::clicked, [this, i](bool checked) {
126 // Reconnect current controller if it was the last one checked
127 // (player number was reduced by more than one)
128 const bool reconnect_first = !checked &&
129 i < connected_controller_checkboxes.size() - 1 &&
130 connected_controller_checkboxes[i + 1]->isChecked();
131
132 // Ensures that the players are always connected in sequential order
133 PropagatePlayerNumberChanged(i, checked, reconnect_first);
121 }); 134 });
122 connect(player_controllers[i], &ConfigureInputPlayer::RefreshInputDevices, this, 135 connect(player_controllers[i], &ConfigureInputPlayer::RefreshInputDevices, this,
123 &ConfigureInput::UpdateAllInputDevices); 136 &ConfigureInput::UpdateAllInputDevices);
124 connect(player_controllers[i], &ConfigureInputPlayer::RefreshInputProfiles, this, 137 connect(player_controllers[i], &ConfigureInputPlayer::RefreshInputProfiles, this,
125 &ConfigureInput::UpdateAllInputProfiles, Qt::QueuedConnection); 138 &ConfigureInput::UpdateAllInputProfiles, Qt::QueuedConnection);
126 connect(player_connected[i], &QCheckBox::stateChanged, [this, i](int state) { 139 connect(connected_controller_checkboxes[i], &QCheckBox::stateChanged, [this, i](int state) {
140 // Keep activated controllers synced with the "Connected Controllers" checkboxes
127 player_controllers[i]->ConnectPlayer(state == Qt::Checked); 141 player_controllers[i]->ConnectPlayer(state == Qt::Checked);
128 }); 142 });
129 143
130 // Remove/hide all the elements that exceed max_players, if applicable. 144 // Remove/hide all the elements that exceed max_players, if applicable.
131 if (i >= max_players) { 145 if (i >= max_players) {
132 ui->tabWidget->removeTab(static_cast<int>(max_players)); 146 ui->tabWidget->removeTab(static_cast<int>(max_players));
133 player_connected[i]->hide(); 147 connected_controller_checkboxes[i]->hide();
134 player_connected_labels[i]->hide(); 148 connected_controller_labels[i]->hide();
135 } 149 }
136 } 150 }
137 // Only the first player can choose handheld mode so connect the signal just to player 1 151 // Only the first player can choose handheld mode so connect the signal just to player 1
@@ -175,28 +189,25 @@ void ConfigureInput::Initialize(InputCommon::InputSubsystem* input_subsystem,
175 LoadConfiguration(); 189 LoadConfiguration();
176} 190}
177 191
178void ConfigureInput::propagateMouseClickOnPlayers(size_t player_index, bool checked, bool origin) { 192void ConfigureInput::PropagatePlayerNumberChanged(size_t player_index, bool checked,
179 // Origin has already been toggled 193 bool reconnect_current) {
180 if (!origin) { 194 connected_controller_checkboxes[player_index]->setChecked(checked);
181 player_connected[player_index]->setChecked(checked);
182 }
183 195
184 if (checked) { 196 if (checked) {
185 // Check all previous buttons when checked 197 // Check all previous buttons when checked
186 if (player_index > 0) { 198 if (player_index > 0) {
187 propagateMouseClickOnPlayers(player_index - 1, checked, false); 199 PropagatePlayerNumberChanged(player_index - 1, checked);
188 } 200 }
189 } else { 201 } else {
190 // Unchecked all following buttons when unchecked 202 // Unchecked all following buttons when unchecked
191 if (player_index < player_tabs.size() - 1) { 203 if (player_index < connected_controller_checkboxes.size() - 1) {
192 // Reconnect current player if it was the last one checked 204 PropagatePlayerNumberChanged(player_index + 1, checked);
193 // (player number was reduced by more than one)
194 if (origin && player_connected[player_index + 1]->checkState() == Qt::Checked) {
195 player_connected[player_index]->setCheckState(Qt::Checked);
196 }
197 propagateMouseClickOnPlayers(player_index + 1, checked, false);
198 } 205 }
199 } 206 }
207
208 if (reconnect_current) {
209 connected_controller_checkboxes[player_index]->setCheckState(Qt::Checked);
210 }
200} 211}
201 212
202QList<QWidget*> ConfigureInput::GetSubTabs() const { 213QList<QWidget*> ConfigureInput::GetSubTabs() const {
@@ -249,17 +260,17 @@ void ConfigureInput::LoadConfiguration() {
249} 260}
250 261
251void ConfigureInput::LoadPlayerControllerIndices() { 262void ConfigureInput::LoadPlayerControllerIndices() {
252 for (std::size_t i = 0; i < player_connected.size(); ++i) { 263 for (std::size_t i = 0; i < connected_controller_checkboxes.size(); ++i) {
253 if (i == 0) { 264 if (i == 0) {
254 auto* handheld = 265 auto* handheld =
255 system.HIDCore().GetEmulatedController(Core::HID::NpadIdType::Handheld); 266 system.HIDCore().GetEmulatedController(Core::HID::NpadIdType::Handheld);
256 if (handheld->IsConnected()) { 267 if (handheld->IsConnected()) {
257 player_connected[i]->setChecked(true); 268 connected_controller_checkboxes[i]->setChecked(true);
258 continue; 269 continue;
259 } 270 }
260 } 271 }
261 const auto* controller = system.HIDCore().GetEmulatedControllerByIndex(i); 272 const auto* controller = system.HIDCore().GetEmulatedControllerByIndex(i);
262 player_connected[i]->setChecked(controller->IsConnected()); 273 connected_controller_checkboxes[i]->setChecked(controller->IsConnected());
263 } 274 }
264} 275}
265 276
diff --git a/src/yuzu/configuration/configure_input.h b/src/yuzu/configuration/configure_input.h
index abb7f7089..136cd3a0a 100644
--- a/src/yuzu/configuration/configure_input.h
+++ b/src/yuzu/configuration/configure_input.h
@@ -56,7 +56,9 @@ private:
56 void UpdateDockedState(bool is_handheld); 56 void UpdateDockedState(bool is_handheld);
57 void UpdateAllInputDevices(); 57 void UpdateAllInputDevices();
58 void UpdateAllInputProfiles(std::size_t player_index); 58 void UpdateAllInputProfiles(std::size_t player_index);
59 void propagateMouseClickOnPlayers(size_t player_index, bool origin, bool checked); 59 // Enable preceding controllers or disable following ones
60 void PropagatePlayerNumberChanged(size_t player_index, bool checked,
61 bool reconnect_current = false);
60 62
61 /// Load configuration settings. 63 /// Load configuration settings.
62 void LoadConfiguration(); 64 void LoadConfiguration();
@@ -71,7 +73,8 @@ private:
71 73
72 std::array<ConfigureInputPlayer*, 8> player_controllers; 74 std::array<ConfigureInputPlayer*, 8> player_controllers;
73 std::array<QWidget*, 8> player_tabs; 75 std::array<QWidget*, 8> player_tabs;
74 std::array<QCheckBox*, 8> player_connected; 76 // Checkboxes representing the "Connected Controllers".
77 std::array<QCheckBox*, 8> connected_controller_checkboxes;
75 ConfigureInputAdvanced* advanced; 78 ConfigureInputAdvanced* advanced;
76 79
77 Core::System& system; 80 Core::System& system;
diff --git a/src/yuzu/configuration/configure_input_player.h b/src/yuzu/configuration/configure_input_player.h
index d4df43d73..d3255d2b4 100644
--- a/src/yuzu/configuration/configure_input_player.h
+++ b/src/yuzu/configuration/configure_input_player.h
@@ -75,7 +75,7 @@ public:
75 void ClearAll(); 75 void ClearAll();
76 76
77signals: 77signals:
78 /// Emitted when this controller is connected by the user. 78 /// Emitted when this controller is (dis)connected by the user.
79 void Connected(bool connected); 79 void Connected(bool connected);
80 /// Emitted when the Handheld mode is selected (undocked with dual joycons attached). 80 /// Emitted when the Handheld mode is selected (undocked with dual joycons attached).
81 void HandheldStateChanged(bool is_handheld); 81 void HandheldStateChanged(bool is_handheld);
@@ -183,9 +183,6 @@ private:
183 /// Stores a pair of "Connected Controllers" combobox index and Controller Type enum. 183 /// Stores a pair of "Connected Controllers" combobox index and Controller Type enum.
184 std::vector<std::pair<int, Core::HID::NpadStyleIndex>> index_controller_type_pairs; 184 std::vector<std::pair<int, Core::HID::NpadStyleIndex>> index_controller_type_pairs;
185 185
186 static constexpr int PLAYER_COUNT = 8;
187 std::array<QCheckBox*, PLAYER_COUNT> player_connected_checkbox;
188
189 /// This will be the the setting function when an input is awaiting configuration. 186 /// This will be the the setting function when an input is awaiting configuration.
190 std::optional<std::function<void(const Common::ParamPackage&)>> input_setter; 187 std::optional<std::function<void(const Common::ParamPackage&)>> input_setter;
191 188
diff --git a/src/yuzu/configuration/configure_vibration.cpp b/src/yuzu/configuration/configure_vibration.cpp
index d765e808a..68c28b320 100644
--- a/src/yuzu/configuration/configure_vibration.cpp
+++ b/src/yuzu/configuration/configure_vibration.cpp
@@ -89,7 +89,7 @@ void ConfigureVibration::VibrateController(Core::HID::ControllerTriggerType type
89 89
90 auto& player = Settings::values.players.GetValue()[player_index]; 90 auto& player = Settings::values.players.GetValue()[player_index];
91 auto controller = hid_core.GetEmulatedControllerByIndex(player_index); 91 auto controller = hid_core.GetEmulatedControllerByIndex(player_index);
92 const int vibration_strenght = vibration_spinboxes[player_index]->value(); 92 const int vibration_strength = vibration_spinboxes[player_index]->value();
93 const auto& buttons = controller->GetButtonsValues(); 93 const auto& buttons = controller->GetButtonsValues();
94 94
95 bool button_is_pressed = false; 95 bool button_is_pressed = false;
@@ -105,10 +105,10 @@ void ConfigureVibration::VibrateController(Core::HID::ControllerTriggerType type
105 return; 105 return;
106 } 106 }
107 107
108 const int old_vibration_enabled = player.vibration_enabled; 108 const bool old_vibration_enabled = player.vibration_enabled;
109 const bool old_vibration_strenght = player.vibration_strength; 109 const int old_vibration_strength = player.vibration_strength;
110 player.vibration_enabled = true; 110 player.vibration_enabled = true;
111 player.vibration_strength = vibration_strenght; 111 player.vibration_strength = vibration_strength;
112 112
113 const Core::HID::VibrationValue vibration{ 113 const Core::HID::VibrationValue vibration{
114 .low_amplitude = 1.0f, 114 .low_amplitude = 1.0f,
@@ -121,7 +121,7 @@ void ConfigureVibration::VibrateController(Core::HID::ControllerTriggerType type
121 121
122 // Restore previous values 122 // Restore previous values
123 player.vibration_enabled = old_vibration_enabled; 123 player.vibration_enabled = old_vibration_enabled;
124 player.vibration_strength = old_vibration_strenght; 124 player.vibration_strength = old_vibration_strength;
125} 125}
126 126
127void ConfigureVibration::StopVibrations() { 127void ConfigureVibration::StopVibrations() {