summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--.ci/yuzu-mainline-step2.yml10
-rw-r--r--src/android/app/src/main/jni/native.cpp6
-rw-r--r--src/audio_core/device/device_session.cpp14
-rw-r--r--src/audio_core/device/device_session.h12
-rw-r--r--src/audio_core/in/audio_in_system.cpp2
-rw-r--r--src/audio_core/in/audio_in_system.h13
-rw-r--r--src/audio_core/out/audio_out_system.cpp4
-rw-r--r--src/audio_core/out/audio_out_system.h13
-rw-r--r--src/common/page_table.cpp34
-rw-r--r--src/core/CMakeLists.txt4
-rw-r--r--src/core/arm/nce/patcher.cpp83
-rw-r--r--src/core/arm/nce/patcher.h27
-rw-r--r--src/core/core.cpp1
-rw-r--r--src/core/debugger/debugger.cpp39
-rw-r--r--src/core/debugger/gdbstub.cpp66
-rw-r--r--src/core/debugger/gdbstub.h15
-rw-r--r--src/core/file_sys/savedata_factory.cpp17
-rw-r--r--src/core/file_sys/savedata_factory.h10
-rw-r--r--src/core/hle/kernel/k_memory_block_manager.cpp4
-rw-r--r--src/core/hle/kernel/k_memory_block_manager.h4
-rw-r--r--src/core/hle/kernel/k_page_table_base.cpp34
-rw-r--r--src/core/hle/kernel/k_page_table_base.h1
-rw-r--r--src/core/hle/kernel/k_process.cpp10
-rw-r--r--src/core/hle/kernel/kernel.cpp31
-rw-r--r--src/core/hle/kernel/kernel.h6
-rw-r--r--src/core/hle/kernel/svc/svc_process.cpp8
-rw-r--r--src/core/hle/service/acc/profile_manager.cpp19
-rw-r--r--src/core/hle/service/acc/profile_manager.h1
-rw-r--r--src/core/hle/service/am/am.cpp7
-rw-r--r--src/core/hle/service/audio/audin_u.cpp36
-rw-r--r--src/core/hle/service/audio/audout_u.cpp26
-rw-r--r--src/core/hle/service/filesystem/filesystem.cpp227
-rw-r--r--src/core/hle/service/filesystem/filesystem.h59
-rw-r--r--src/core/hle/service/filesystem/fsp_srv.cpp55
-rw-r--r--src/core/hle/service/filesystem/fsp_srv.h6
-rw-r--r--src/core/hle/service/filesystem/romfs_controller.cpp37
-rw-r--r--src/core/hle/service/filesystem/romfs_controller.h31
-rw-r--r--src/core/hle/service/filesystem/save_data_controller.cpp99
-rw-r--r--src/core/hle/service/filesystem/save_data_controller.h35
-rw-r--r--src/core/hle/service/glue/arp.cpp7
-rw-r--r--src/core/hle/service/hid/hid.cpp10
-rw-r--r--src/core/hle/service/pm/pm.cpp85
-rw-r--r--src/core/hle/service/server_manager.cpp9
-rw-r--r--src/core/loader/deconstructed_rom_directory.cpp89
-rw-r--r--src/core/loader/nca.cpp6
-rw-r--r--src/core/loader/nro.cpp10
-rw-r--r--src/core/loader/nso.cpp41
-rw-r--r--src/core/loader/nso.h3
-rw-r--r--src/core/loader/nsp.cpp3
-rw-r--r--src/core/loader/xci.cpp3
-rw-r--r--src/shader_recompiler/backend/spirv/emit_spirv_memory.cpp40
-rw-r--r--src/shader_recompiler/backend/spirv/spirv_emit_context.cpp51
-rw-r--r--src/shader_recompiler/backend/spirv/spirv_emit_context.h3
-rw-r--r--src/yuzu/configuration/configure_profile_manager.cpp4
-rw-r--r--src/yuzu/main.cpp8
55 files changed, 929 insertions, 549 deletions
diff --git a/.ci/yuzu-mainline-step2.yml b/.ci/yuzu-mainline-step2.yml
index b294827f4..8bb0572f5 100644
--- a/.ci/yuzu-mainline-step2.yml
+++ b/.ci/yuzu-mainline-step2.yml
@@ -8,17 +8,7 @@ variables:
DisplayVersion: $[counter(variables['DisplayPrefix'], 1)]
stages:
-- stage: format
- displayName: 'format'
- jobs:
- - job: format
- displayName: 'clang'
- pool:
- vmImage: ubuntu-latest
- steps:
- - template: ./templates/format-check.yml
- stage: build
- dependsOn: format
displayName: 'build'
jobs:
- job: build
diff --git a/src/android/app/src/main/jni/native.cpp b/src/android/app/src/main/jni/native.cpp
index e436622e0..ed3b1353a 100644
--- a/src/android/app/src/main/jni/native.cpp
+++ b/src/android/app/src/main/jni/native.cpp
@@ -770,8 +770,8 @@ void Java_org_yuzu_yuzu_1emu_NativeLibrary_initializeEmptyUserDirectory(JNIEnv*
ASSERT(user_id);
const auto user_save_data_path = FileSys::SaveDataFactory::GetFullPath(
- EmulationSession::GetInstance().System(), vfs_nand_dir, FileSys::SaveDataSpaceId::NandUser,
- FileSys::SaveDataType::SaveData, 1, user_id->AsU128(), 0);
+ {}, vfs_nand_dir, FileSys::SaveDataSpaceId::NandUser, FileSys::SaveDataType::SaveData, 1,
+ user_id->AsU128(), 0);
const auto full_path = Common::FS::ConcatPathSafe(nand_dir, user_save_data_path);
if (!Common::FS::CreateParentDirs(full_path)) {
@@ -878,7 +878,7 @@ jstring Java_org_yuzu_yuzu_1emu_NativeLibrary_getSavePath(JNIEnv* env, jobject j
FileSys::Mode::Read);
const auto user_save_data_path = FileSys::SaveDataFactory::GetFullPath(
- system, vfsNandDir, FileSys::SaveDataSpaceId::NandUser, FileSys::SaveDataType::SaveData,
+ {}, vfsNandDir, FileSys::SaveDataSpaceId::NandUser, FileSys::SaveDataType::SaveData,
program_id, user_id->AsU128(), 0);
return ToJString(env, user_save_data_path);
}
diff --git a/src/audio_core/device/device_session.cpp b/src/audio_core/device/device_session.cpp
index ee42ae529..3c214ec00 100644
--- a/src/audio_core/device/device_session.cpp
+++ b/src/audio_core/device/device_session.cpp
@@ -10,6 +10,8 @@
#include "core/core_timing.h"
#include "core/memory.h"
+#include "core/hle/kernel/k_process.h"
+
namespace AudioCore {
using namespace std::literals;
@@ -25,7 +27,7 @@ DeviceSession::~DeviceSession() {
}
Result DeviceSession::Initialize(std::string_view name_, SampleFormat sample_format_,
- u16 channel_count_, size_t session_id_, u32 handle_,
+ u16 channel_count_, size_t session_id_, Kernel::KProcess* handle_,
u64 applet_resource_user_id_, Sink::StreamType type_) {
if (stream) {
Finalize();
@@ -36,6 +38,7 @@ Result DeviceSession::Initialize(std::string_view name_, SampleFormat sample_for
channel_count = channel_count_;
session_id = session_id_;
handle = handle_;
+ handle->Open();
applet_resource_user_id = applet_resource_user_id_;
if (type == Sink::StreamType::In) {
@@ -54,6 +57,11 @@ void DeviceSession::Finalize() {
sink->CloseStream(stream);
stream = nullptr;
}
+
+ if (handle) {
+ handle->Close();
+ handle = nullptr;
+ }
}
void DeviceSession::Start() {
@@ -91,7 +99,7 @@ void DeviceSession::AppendBuffers(std::span<const AudioBuffer> buffers) {
stream->AppendBuffer(new_buffer, tmp_samples);
} else {
Core::Memory::CpuGuestMemory<s16, Core::Memory::GuestMemoryFlags::UnsafeRead> samples(
- system.ApplicationMemory(), buffer.samples, buffer.size / sizeof(s16));
+ handle->GetMemory(), buffer.samples, buffer.size / sizeof(s16));
stream->AppendBuffer(new_buffer, samples);
}
}
@@ -100,7 +108,7 @@ void DeviceSession::AppendBuffers(std::span<const AudioBuffer> buffers) {
void DeviceSession::ReleaseBuffer(const AudioBuffer& buffer) const {
if (type == Sink::StreamType::In) {
auto samples{stream->ReleaseBuffer(buffer.size / sizeof(s16))};
- system.ApplicationMemory().WriteBlockUnsafe(buffer.samples, samples.data(), buffer.size);
+ handle->GetMemory().WriteBlockUnsafe(buffer.samples, samples.data(), buffer.size);
}
}
diff --git a/src/audio_core/device/device_session.h b/src/audio_core/device/device_session.h
index 7d52f362d..f3fae2617 100644
--- a/src/audio_core/device/device_session.h
+++ b/src/audio_core/device/device_session.h
@@ -20,6 +20,10 @@ struct EventType;
} // namespace Timing
} // namespace Core
+namespace Kernel {
+class KProcess;
+} // namespace Kernel
+
namespace AudioCore {
namespace Sink {
@@ -44,13 +48,13 @@ public:
* @param sample_format - Sample format for this device's output.
* @param channel_count - Number of channels for this device (2 or 6).
* @param session_id - This session's id.
- * @param handle - Handle for this device session (unused).
+ * @param handle - Process handle for this device session.
* @param applet_resource_user_id - Applet resource user id for this device session (unused).
* @param type - Type of this stream (Render, In, Out).
* @return Result code for this call.
*/
Result Initialize(std::string_view name, SampleFormat sample_format, u16 channel_count,
- size_t session_id, u32 handle, u64 applet_resource_user_id,
+ size_t session_id, Kernel::KProcess* handle, u64 applet_resource_user_id,
Sink::StreamType type);
/**
@@ -137,8 +141,8 @@ private:
u16 channel_count{};
/// Session id of this device session
size_t session_id{};
- /// Handle of this device session
- u32 handle{};
+ /// Process handle of device memory owner
+ Kernel::KProcess* handle{};
/// Applet resource user id of this device session
u64 applet_resource_user_id{};
/// Total number of samples played by this device session
diff --git a/src/audio_core/in/audio_in_system.cpp b/src/audio_core/in/audio_in_system.cpp
index 579129121..b2dd3ef9f 100644
--- a/src/audio_core/in/audio_in_system.cpp
+++ b/src/audio_core/in/audio_in_system.cpp
@@ -57,7 +57,7 @@ Result System::IsConfigValid(const std::string_view device_name,
}
Result System::Initialize(std::string device_name, const AudioInParameter& in_params,
- const u32 handle_, const u64 applet_resource_user_id_) {
+ Kernel::KProcess* handle_, const u64 applet_resource_user_id_) {
auto result{IsConfigValid(device_name, in_params)};
if (result.IsError()) {
return result;
diff --git a/src/audio_core/in/audio_in_system.h b/src/audio_core/in/audio_in_system.h
index 1c5154638..ee048190c 100644
--- a/src/audio_core/in/audio_in_system.h
+++ b/src/audio_core/in/audio_in_system.h
@@ -19,7 +19,8 @@ class System;
namespace Kernel {
class KEvent;
-}
+class KProcess;
+} // namespace Kernel
namespace AudioCore::AudioIn {
@@ -93,12 +94,12 @@ public:
*
* @param device_name - The name of the requested input device.
* @param in_params - Input parameters, see AudioInParameter.
- * @param handle - Unused.
+ * @param handle - Process handle.
* @param applet_resource_user_id - Unused.
* @return Result code.
*/
- Result Initialize(std::string device_name, const AudioInParameter& in_params, u32 handle,
- u64 applet_resource_user_id);
+ Result Initialize(std::string device_name, const AudioInParameter& in_params,
+ Kernel::KProcess* handle, u64 applet_resource_user_id);
/**
* Start this system.
@@ -244,8 +245,8 @@ public:
private:
/// Core system
Core::System& system;
- /// (Unused)
- u32 handle{};
+ /// Process handle
+ Kernel::KProcess* handle{};
/// (Unused)
u64 applet_resource_user_id{};
/// Buffer event, signalled when a buffer is ready
diff --git a/src/audio_core/out/audio_out_system.cpp b/src/audio_core/out/audio_out_system.cpp
index 0adf64bd3..7b3ff4e88 100644
--- a/src/audio_core/out/audio_out_system.cpp
+++ b/src/audio_core/out/audio_out_system.cpp
@@ -48,8 +48,8 @@ Result System::IsConfigValid(std::string_view device_name,
return Service::Audio::ResultInvalidChannelCount;
}
-Result System::Initialize(std::string device_name, const AudioOutParameter& in_params, u32 handle_,
- u64 applet_resource_user_id_) {
+Result System::Initialize(std::string device_name, const AudioOutParameter& in_params,
+ Kernel::KProcess* handle_, u64 applet_resource_user_id_) {
auto result = IsConfigValid(device_name, in_params);
if (result.IsError()) {
return result;
diff --git a/src/audio_core/out/audio_out_system.h b/src/audio_core/out/audio_out_system.h
index b95cb91be..82aada185 100644
--- a/src/audio_core/out/audio_out_system.h
+++ b/src/audio_core/out/audio_out_system.h
@@ -19,7 +19,8 @@ class System;
namespace Kernel {
class KEvent;
-}
+class KProcess;
+} // namespace Kernel
namespace AudioCore::AudioOut {
@@ -84,12 +85,12 @@ public:
*
* @param device_name - The name of the requested output device.
* @param in_params - Input parameters, see AudioOutParameter.
- * @param handle - Unused.
+ * @param handle - Process handle.
* @param applet_resource_user_id - Unused.
* @return Result code.
*/
- Result Initialize(std::string device_name, const AudioOutParameter& in_params, u32 handle,
- u64 applet_resource_user_id);
+ Result Initialize(std::string device_name, const AudioOutParameter& in_params,
+ Kernel::KProcess* handle, u64 applet_resource_user_id);
/**
* Start this system.
@@ -228,8 +229,8 @@ public:
private:
/// Core system
Core::System& system;
- /// (Unused)
- u32 handle{};
+ /// Process handle
+ Kernel::KProcess* handle{};
/// (Unused)
u64 applet_resource_user_id{};
/// Buffer event, signalled when a buffer is ready
diff --git a/src/common/page_table.cpp b/src/common/page_table.cpp
index 166dc3dce..85dc18c11 100644
--- a/src/common/page_table.cpp
+++ b/src/common/page_table.cpp
@@ -2,6 +2,7 @@
// SPDX-License-Identifier: GPL-2.0-or-later
#include "common/page_table.h"
+#include "common/scope_exit.h"
namespace Common {
@@ -11,29 +12,10 @@ PageTable::~PageTable() noexcept = default;
bool PageTable::BeginTraversal(TraversalEntry* out_entry, TraversalContext* out_context,
Common::ProcessAddress address) const {
- // Setup invalid defaults.
- out_entry->phys_addr = 0;
- out_entry->block_size = page_size;
- out_context->next_page = 0;
-
- // Validate that we can read the actual entry.
- const auto page = address / page_size;
- if (page >= backing_addr.size()) {
- return false;
- }
-
- // Validate that the entry is mapped.
- const auto phys_addr = backing_addr[page];
- if (phys_addr == 0) {
- return false;
- }
+ out_context->next_offset = GetInteger(address);
+ out_context->next_page = address / page_size;
- // Populate the results.
- out_entry->phys_addr = phys_addr + GetInteger(address);
- out_context->next_page = page + 1;
- out_context->next_offset = GetInteger(address) + page_size;
-
- return true;
+ return this->ContinueTraversal(out_entry, out_context);
}
bool PageTable::ContinueTraversal(TraversalEntry* out_entry, TraversalContext* context) const {
@@ -41,6 +23,12 @@ bool PageTable::ContinueTraversal(TraversalEntry* out_entry, TraversalContext* c
out_entry->phys_addr = 0;
out_entry->block_size = page_size;
+ // Regardless of whether the page was mapped, advance on exit.
+ SCOPE_EXIT({
+ context->next_page += 1;
+ context->next_offset += page_size;
+ });
+
// Validate that we can read the actual entry.
const auto page = context->next_page;
if (page >= backing_addr.size()) {
@@ -55,8 +43,6 @@ bool PageTable::ContinueTraversal(TraversalEntry* out_entry, TraversalContext* c
// Populate the results.
out_entry->phys_addr = phys_addr + context->next_offset;
- context->next_page = page + 1;
- context->next_offset += page_size;
return true;
}
diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt
index 753f55ebe..293d9647b 100644
--- a/src/core/CMakeLists.txt
+++ b/src/core/CMakeLists.txt
@@ -490,6 +490,10 @@ add_library(core STATIC
hle/service/filesystem/fsp_pr.h
hle/service/filesystem/fsp_srv.cpp
hle/service/filesystem/fsp_srv.h
+ hle/service/filesystem/romfs_controller.cpp
+ hle/service/filesystem/romfs_controller.h
+ hle/service/filesystem/save_data_controller.cpp
+ hle/service/filesystem/save_data_controller.h
hle/service/fgm/fgm.cpp
hle/service/fgm/fgm.h
hle/service/friend/friend.cpp
diff --git a/src/core/arm/nce/patcher.cpp b/src/core/arm/nce/patcher.cpp
index 47a7a8880..c7285e3a0 100644
--- a/src/core/arm/nce/patcher.cpp
+++ b/src/core/arm/nce/patcher.cpp
@@ -22,14 +22,10 @@ using NativeExecutionParameters = Kernel::KThread::NativeExecutionParameters;
constexpr size_t MaxRelativeBranch = 128_MiB;
constexpr u32 ModuleCodeIndex = 0x24 / sizeof(u32);
-Patcher::Patcher() : c(m_patch_instructions) {}
-
-Patcher::~Patcher() = default;
-
-void Patcher::PatchText(const Kernel::PhysicalMemory& program_image,
- const Kernel::CodeSet::Segment& code) {
- // Branch to the first instruction of the module.
- this->BranchToModule(0);
+Patcher::Patcher() : c(m_patch_instructions) {
+ // The first word of the patch section is always a branch to the first instruction of the
+ // module.
+ c.dw(0);
// Write save context helper function.
c.l(m_save_context);
@@ -38,6 +34,25 @@ void Patcher::PatchText(const Kernel::PhysicalMemory& program_image,
// Write load context helper function.
c.l(m_load_context);
WriteLoadContext();
+}
+
+Patcher::~Patcher() = default;
+
+bool Patcher::PatchText(const Kernel::PhysicalMemory& program_image,
+ const Kernel::CodeSet::Segment& code) {
+ // If we have patched modules but cannot reach the new module, then it needs its own patcher.
+ const size_t image_size = program_image.size();
+ if (total_program_size + image_size > MaxRelativeBranch && total_program_size > 0) {
+ return false;
+ }
+
+ // Add a new module patch to our list
+ modules.emplace_back();
+ curr_patch = &modules.back();
+
+ // The first word of the patch section is always a branch to the first instruction of the
+ // module.
+ curr_patch->m_branch_to_module_relocations.push_back({0, 0});
// Retrieve text segment data.
const auto text = std::span{program_image}.subspan(code.offset, code.size);
@@ -94,16 +109,17 @@ void Patcher::PatchText(const Kernel::PhysicalMemory& program_image,
}
if (auto exclusive = Exclusive{inst}; exclusive.Verify()) {
- m_exclusives.push_back(i);
+ curr_patch->m_exclusives.push_back(i);
}
}
// Determine patching mode for the final relocation step
- const size_t image_size = program_image.size();
+ total_program_size += image_size;
this->mode = image_size > MaxRelativeBranch ? PatchMode::PreText : PatchMode::PostData;
+ return true;
}
-void Patcher::RelocateAndCopy(Common::ProcessAddress load_base,
+bool Patcher::RelocateAndCopy(Common::ProcessAddress load_base,
const Kernel::CodeSet::Segment& code,
Kernel::PhysicalMemory& program_image,
EntryTrampolines* out_trampolines) {
@@ -120,7 +136,7 @@ void Patcher::RelocateAndCopy(Common::ProcessAddress load_base,
if (mode == PatchMode::PreText) {
rc.B(rel.patch_offset - patch_size - rel.module_offset);
} else {
- rc.B(image_size - rel.module_offset + rel.patch_offset);
+ rc.B(total_program_size - rel.module_offset + rel.patch_offset);
}
};
@@ -129,7 +145,7 @@ void Patcher::RelocateAndCopy(Common::ProcessAddress load_base,
if (mode == PatchMode::PreText) {
rc.B(patch_size - rel.patch_offset + rel.module_offset);
} else {
- rc.B(rel.module_offset - image_size - rel.patch_offset);
+ rc.B(rel.module_offset - total_program_size - rel.patch_offset);
}
};
@@ -137,7 +153,7 @@ void Patcher::RelocateAndCopy(Common::ProcessAddress load_base,
if (mode == PatchMode::PreText) {
return GetInteger(load_base) + patch_offset;
} else {
- return GetInteger(load_base) + image_size + patch_offset;
+ return GetInteger(load_base) + total_program_size + patch_offset;
}
};
@@ -150,39 +166,50 @@ void Patcher::RelocateAndCopy(Common::ProcessAddress load_base,
};
// We are now ready to relocate!
- for (const Relocation& rel : m_branch_to_patch_relocations) {
+ auto& patch = modules[m_relocate_module_index++];
+ for (const Relocation& rel : patch.m_branch_to_patch_relocations) {
ApplyBranchToPatchRelocation(text_words.data() + rel.module_offset / sizeof(u32), rel);
}
- for (const Relocation& rel : m_branch_to_module_relocations) {
+ for (const Relocation& rel : patch.m_branch_to_module_relocations) {
ApplyBranchToModuleRelocation(m_patch_instructions.data() + rel.patch_offset / sizeof(u32),
rel);
}
// Rewrite PC constants and record post trampolines
- for (const Relocation& rel : m_write_module_pc_relocations) {
+ for (const Relocation& rel : patch.m_write_module_pc_relocations) {
oaknut::CodeGenerator rc{m_patch_instructions.data() + rel.patch_offset / sizeof(u32)};
rc.dx(RebasePc(rel.module_offset));
}
- for (const Trampoline& rel : m_trampolines) {
+ for (const Trampoline& rel : patch.m_trampolines) {
out_trampolines->insert({RebasePc(rel.module_offset), RebasePatch(rel.patch_offset)});
}
// Cortex-A57 seems to treat all exclusives as ordered, but newer processors do not.
// Convert to ordered to preserve this assumption.
- for (const ModuleTextAddress i : m_exclusives) {
+ for (const ModuleTextAddress i : patch.m_exclusives) {
auto exclusive = Exclusive{text_words[i]};
text_words[i] = exclusive.AsOrdered();
}
- // Copy to program image
- if (this->mode == PatchMode::PreText) {
- std::memcpy(program_image.data(), m_patch_instructions.data(),
- m_patch_instructions.size() * sizeof(u32));
- } else {
- program_image.resize(image_size + patch_size);
- std::memcpy(program_image.data() + image_size, m_patch_instructions.data(),
- m_patch_instructions.size() * sizeof(u32));
+ // Remove the patched module size from the total. This is done so total_program_size
+ // always represents the distance from the currently patched module to the patch section.
+ total_program_size -= image_size;
+
+ // Only copy to the program image of the last module
+ if (m_relocate_module_index == modules.size()) {
+ if (this->mode == PatchMode::PreText) {
+ ASSERT(image_size == total_program_size);
+ std::memcpy(program_image.data(), m_patch_instructions.data(),
+ m_patch_instructions.size() * sizeof(u32));
+ } else {
+ program_image.resize(image_size + patch_size);
+ std::memcpy(program_image.data() + image_size, m_patch_instructions.data(),
+ m_patch_instructions.size() * sizeof(u32));
+ }
+ return true;
}
+
+ return false;
}
size_t Patcher::GetSectionSize() const noexcept {
@@ -322,7 +349,7 @@ void Patcher::WriteSvcTrampoline(ModuleDestLabel module_dest, u32 svc_id) {
// Write the post-SVC trampoline address, which will jump back to the guest after restoring its
// state.
- m_trampolines.push_back({c.offset(), module_dest});
+ curr_patch->m_trampolines.push_back({c.offset(), module_dest});
// Host called this location. Save the return address so we can
// unwind the stack properly when jumping back.
diff --git a/src/core/arm/nce/patcher.h b/src/core/arm/nce/patcher.h
index c6d1608c1..a44f385e2 100644
--- a/src/core/arm/nce/patcher.h
+++ b/src/core/arm/nce/patcher.h
@@ -31,9 +31,9 @@ public:
explicit Patcher();
~Patcher();
- void PatchText(const Kernel::PhysicalMemory& program_image,
+ bool PatchText(const Kernel::PhysicalMemory& program_image,
const Kernel::CodeSet::Segment& code);
- void RelocateAndCopy(Common::ProcessAddress load_base, const Kernel::CodeSet::Segment& code,
+ bool RelocateAndCopy(Common::ProcessAddress load_base, const Kernel::CodeSet::Segment& code,
Kernel::PhysicalMemory& program_image, EntryTrampolines* out_trampolines);
size_t GetSectionSize() const noexcept;
@@ -61,16 +61,16 @@ private:
private:
void BranchToPatch(uintptr_t module_dest) {
- m_branch_to_patch_relocations.push_back({c.offset(), module_dest});
+ curr_patch->m_branch_to_patch_relocations.push_back({c.offset(), module_dest});
}
void BranchToModule(uintptr_t module_dest) {
- m_branch_to_module_relocations.push_back({c.offset(), module_dest});
+ curr_patch->m_branch_to_module_relocations.push_back({c.offset(), module_dest});
c.dw(0);
}
void WriteModulePc(uintptr_t module_dest) {
- m_write_module_pc_relocations.push_back({c.offset(), module_dest});
+ curr_patch->m_write_module_pc_relocations.push_back({c.offset(), module_dest});
c.dx(0);
}
@@ -84,15 +84,22 @@ private:
uintptr_t module_offset; ///< Offset in bytes from the start of the text section.
};
+ struct ModulePatch {
+ std::vector<Trampoline> m_trampolines;
+ std::vector<Relocation> m_branch_to_patch_relocations{};
+ std::vector<Relocation> m_branch_to_module_relocations{};
+ std::vector<Relocation> m_write_module_pc_relocations{};
+ std::vector<ModuleTextAddress> m_exclusives{};
+ };
+
oaknut::VectorCodeGenerator c;
- std::vector<Trampoline> m_trampolines;
- std::vector<Relocation> m_branch_to_patch_relocations{};
- std::vector<Relocation> m_branch_to_module_relocations{};
- std::vector<Relocation> m_write_module_pc_relocations{};
- std::vector<ModuleTextAddress> m_exclusives{};
oaknut::Label m_save_context{};
oaknut::Label m_load_context{};
PatchMode mode{PatchMode::None};
+ size_t total_program_size{};
+ size_t m_relocate_module_index{};
+ std::vector<ModulePatch> modules;
+ ModulePatch* curr_patch;
};
} // namespace Core::NCE
diff --git a/src/core/core.cpp b/src/core/core.cpp
index c063f7719..461eea9c8 100644
--- a/src/core/core.cpp
+++ b/src/core/core.cpp
@@ -413,6 +413,7 @@ struct System::Impl {
kernel.ShutdownCores();
services.reset();
service_manager.reset();
+ fs_controller.Reset();
cheat_engine.reset();
telemetry_session.reset();
time_manager.Shutdown();
diff --git a/src/core/debugger/debugger.cpp b/src/core/debugger/debugger.cpp
index 0e270eb50..e86aae846 100644
--- a/src/core/debugger/debugger.cpp
+++ b/src/core/debugger/debugger.cpp
@@ -114,7 +114,7 @@ public:
}
Kernel::KThread* GetActiveThread() override {
- return state->active_thread;
+ return state->active_thread.GetPointerUnsafe();
}
private:
@@ -147,11 +147,14 @@ private:
std::scoped_lock lk{connection_lock};
+ // Find the process we are going to debug.
+ SetDebugProcess();
+
// Ensure everything is stopped.
PauseEmulation();
// Set up the new frontend.
- frontend = std::make_unique<GDBStub>(*this, system);
+ frontend = std::make_unique<GDBStub>(*this, system, debug_process.GetPointerUnsafe());
// Set the new state. This will tear down any existing state.
state = ConnectionState{
@@ -194,15 +197,20 @@ private:
UpdateActiveThread();
if (state->info.type == SignalType::Watchpoint) {
- frontend->Watchpoint(state->active_thread, *state->info.watchpoint);
+ frontend->Watchpoint(std::addressof(*state->active_thread),
+ *state->info.watchpoint);
} else {
- frontend->Stopped(state->active_thread);
+ frontend->Stopped(std::addressof(*state->active_thread));
}
break;
case SignalType::ShuttingDown:
frontend->ShuttingDown();
+ // Release members.
+ state->active_thread.Reset(nullptr);
+ debug_process.Reset(nullptr);
+
// Wait for emulation to shut down gracefully now.
state->signal_pipe.close();
state->client_socket.shutdown(boost::asio::socket_base::shutdown_both);
@@ -222,7 +230,7 @@ private:
stopped = true;
PauseEmulation();
UpdateActiveThread();
- frontend->Stopped(state->active_thread);
+ frontend->Stopped(state->active_thread.GetPointerUnsafe());
break;
}
case DebuggerAction::Continue:
@@ -232,7 +240,7 @@ private:
MarkResumed([&] {
state->active_thread->SetStepState(Kernel::StepState::StepPending);
state->active_thread->Resume(Kernel::SuspendType::Debug);
- ResumeEmulation(state->active_thread);
+ ResumeEmulation(state->active_thread.GetPointerUnsafe());
});
break;
case DebuggerAction::StepThreadLocked: {
@@ -255,6 +263,7 @@ private:
}
void PauseEmulation() {
+ Kernel::KScopedLightLock ll{debug_process->GetListLock()};
Kernel::KScopedSchedulerLock sl{system.Kernel()};
// Put all threads to sleep on next scheduler round.
@@ -264,6 +273,9 @@ private:
}
void ResumeEmulation(Kernel::KThread* except = nullptr) {
+ Kernel::KScopedLightLock ll{debug_process->GetListLock()};
+ Kernel::KScopedSchedulerLock sl{system.Kernel()};
+
// Wake up all threads.
for (auto& thread : ThreadList()) {
if (std::addressof(thread) == except) {
@@ -277,15 +289,16 @@ private:
template <typename Callback>
void MarkResumed(Callback&& cb) {
- Kernel::KScopedSchedulerLock sl{system.Kernel()};
stopped = false;
cb();
}
void UpdateActiveThread() {
+ Kernel::KScopedLightLock ll{debug_process->GetListLock()};
+
auto& threads{ThreadList()};
for (auto& thread : threads) {
- if (std::addressof(thread) == state->active_thread) {
+ if (std::addressof(thread) == state->active_thread.GetPointerUnsafe()) {
// Thread is still alive, no need to update.
return;
}
@@ -293,12 +306,18 @@ private:
state->active_thread = std::addressof(threads.front());
}
+private:
+ void SetDebugProcess() {
+ debug_process = std::move(system.Kernel().GetProcessList().back());
+ }
+
Kernel::KProcess::ThreadList& ThreadList() {
- return system.ApplicationProcess()->GetThreadList();
+ return debug_process->GetThreadList();
}
private:
System& system;
+ Kernel::KScopedAutoObject<Kernel::KProcess> debug_process;
std::unique_ptr<DebuggerFrontend> frontend;
boost::asio::io_context io_context;
@@ -310,7 +329,7 @@ private:
boost::process::async_pipe signal_pipe;
SignalInfo info;
- Kernel::KThread* active_thread;
+ Kernel::KScopedAutoObject<Kernel::KThread> active_thread;
std::array<u8, 4096> client_data;
bool pipe_data;
};
diff --git a/src/core/debugger/gdbstub.cpp b/src/core/debugger/gdbstub.cpp
index 4051ed4af..80091cc7e 100644
--- a/src/core/debugger/gdbstub.cpp
+++ b/src/core/debugger/gdbstub.cpp
@@ -108,9 +108,9 @@ static std::string EscapeXML(std::string_view data) {
return escaped;
}
-GDBStub::GDBStub(DebuggerBackend& backend_, Core::System& system_)
- : DebuggerFrontend(backend_), system{system_} {
- if (system.ApplicationProcess()->Is64Bit()) {
+GDBStub::GDBStub(DebuggerBackend& backend_, Core::System& system_, Kernel::KProcess* debug_process_)
+ : DebuggerFrontend(backend_), system{system_}, debug_process{debug_process_} {
+ if (GetProcess()->Is64Bit()) {
arch = std::make_unique<GDBStubA64>();
} else {
arch = std::make_unique<GDBStubA32>();
@@ -276,7 +276,7 @@ void GDBStub::ExecuteCommand(std::string_view packet, std::vector<DebuggerAction
const size_t size{static_cast<size_t>(strtoll(command.data() + sep, nullptr, 16))};
std::vector<u8> mem(size);
- if (system.ApplicationMemory().ReadBlock(addr, mem.data(), size)) {
+ if (GetMemory().ReadBlock(addr, mem.data(), size)) {
// Restore any bytes belonging to replaced instructions.
auto it = replaced_instructions.lower_bound(addr);
for (; it != replaced_instructions.end() && it->first < addr + size; it++) {
@@ -310,8 +310,8 @@ void GDBStub::ExecuteCommand(std::string_view packet, std::vector<DebuggerAction
const auto mem_substr{std::string_view(command).substr(mem_sep)};
const auto mem{Common::HexStringToVector(mem_substr, false)};
- if (system.ApplicationMemory().WriteBlock(addr, mem.data(), size)) {
- Core::InvalidateInstructionCacheRange(system.ApplicationProcess(), addr, size);
+ if (GetMemory().WriteBlock(addr, mem.data(), size)) {
+ Core::InvalidateInstructionCacheRange(GetProcess(), addr, size);
SendReply(GDB_STUB_REPLY_OK);
} else {
SendReply(GDB_STUB_REPLY_ERR);
@@ -353,7 +353,7 @@ void GDBStub::HandleBreakpointInsert(std::string_view command) {
const size_t addr{static_cast<size_t>(strtoll(command.data() + addr_sep, nullptr, 16))};
const size_t size{static_cast<size_t>(strtoll(command.data() + size_sep, nullptr, 16))};
- if (!system.ApplicationMemory().IsValidVirtualAddressRange(addr, size)) {
+ if (!GetMemory().IsValidVirtualAddressRange(addr, size)) {
SendReply(GDB_STUB_REPLY_ERR);
return;
}
@@ -362,22 +362,20 @@ void GDBStub::HandleBreakpointInsert(std::string_view command) {
switch (type) {
case BreakpointType::Software:
- replaced_instructions[addr] = system.ApplicationMemory().Read32(addr);
- system.ApplicationMemory().Write32(addr, arch->BreakpointInstruction());
- Core::InvalidateInstructionCacheRange(system.ApplicationProcess(), addr, sizeof(u32));
+ replaced_instructions[addr] = GetMemory().Read32(addr);
+ GetMemory().Write32(addr, arch->BreakpointInstruction());
+ Core::InvalidateInstructionCacheRange(GetProcess(), addr, sizeof(u32));
success = true;
break;
case BreakpointType::WriteWatch:
- success = system.ApplicationProcess()->InsertWatchpoint(addr, size,
- Kernel::DebugWatchpointType::Write);
+ success = GetProcess()->InsertWatchpoint(addr, size, Kernel::DebugWatchpointType::Write);
break;
case BreakpointType::ReadWatch:
- success = system.ApplicationProcess()->InsertWatchpoint(addr, size,
- Kernel::DebugWatchpointType::Read);
+ success = GetProcess()->InsertWatchpoint(addr, size, Kernel::DebugWatchpointType::Read);
break;
case BreakpointType::AccessWatch:
- success = system.ApplicationProcess()->InsertWatchpoint(
- addr, size, Kernel::DebugWatchpointType::ReadOrWrite);
+ success =
+ GetProcess()->InsertWatchpoint(addr, size, Kernel::DebugWatchpointType::ReadOrWrite);
break;
case BreakpointType::Hardware:
default:
@@ -400,7 +398,7 @@ void GDBStub::HandleBreakpointRemove(std::string_view command) {
const size_t addr{static_cast<size_t>(strtoll(command.data() + addr_sep, nullptr, 16))};
const size_t size{static_cast<size_t>(strtoll(command.data() + size_sep, nullptr, 16))};
- if (!system.ApplicationMemory().IsValidVirtualAddressRange(addr, size)) {
+ if (!GetMemory().IsValidVirtualAddressRange(addr, size)) {
SendReply(GDB_STUB_REPLY_ERR);
return;
}
@@ -411,24 +409,22 @@ void GDBStub::HandleBreakpointRemove(std::string_view command) {
case BreakpointType::Software: {
const auto orig_insn{replaced_instructions.find(addr)};
if (orig_insn != replaced_instructions.end()) {
- system.ApplicationMemory().Write32(addr, orig_insn->second);
- Core::InvalidateInstructionCacheRange(system.ApplicationProcess(), addr, sizeof(u32));
+ GetMemory().Write32(addr, orig_insn->second);
+ Core::InvalidateInstructionCacheRange(GetProcess(), addr, sizeof(u32));
replaced_instructions.erase(addr);
success = true;
}
break;
}
case BreakpointType::WriteWatch:
- success = system.ApplicationProcess()->RemoveWatchpoint(addr, size,
- Kernel::DebugWatchpointType::Write);
+ success = GetProcess()->RemoveWatchpoint(addr, size, Kernel::DebugWatchpointType::Write);
break;
case BreakpointType::ReadWatch:
- success = system.ApplicationProcess()->RemoveWatchpoint(addr, size,
- Kernel::DebugWatchpointType::Read);
+ success = GetProcess()->RemoveWatchpoint(addr, size, Kernel::DebugWatchpointType::Read);
break;
case BreakpointType::AccessWatch:
- success = system.ApplicationProcess()->RemoveWatchpoint(
- addr, size, Kernel::DebugWatchpointType::ReadOrWrite);
+ success =
+ GetProcess()->RemoveWatchpoint(addr, size, Kernel::DebugWatchpointType::ReadOrWrite);
break;
case BreakpointType::Hardware:
default:
@@ -466,10 +462,10 @@ void GDBStub::HandleQuery(std::string_view command) {
const auto target_xml{arch->GetTargetXML()};
SendReply(PaginateBuffer(target_xml, command.substr(30)));
} else if (command.starts_with("Offsets")) {
- const auto main_offset = Core::FindMainModuleEntrypoint(system.ApplicationProcess());
+ const auto main_offset = Core::FindMainModuleEntrypoint(GetProcess());
SendReply(fmt::format("TextSeg={:x}", GetInteger(main_offset)));
} else if (command.starts_with("Xfer:libraries:read::")) {
- auto modules = Core::FindModules(system.ApplicationProcess());
+ auto modules = Core::FindModules(GetProcess());
std::string buffer;
buffer += R"(<?xml version="1.0"?>)";
@@ -483,7 +479,7 @@ void GDBStub::HandleQuery(std::string_view command) {
SendReply(PaginateBuffer(buffer, command.substr(21)));
} else if (command.starts_with("fThreadInfo")) {
// beginning of list
- const auto& threads = system.ApplicationProcess()->GetThreadList();
+ const auto& threads = GetProcess()->GetThreadList();
std::vector<std::string> thread_ids;
for (const auto& thread : threads) {
thread_ids.push_back(fmt::format("{:x}", thread.GetThreadId()));
@@ -497,7 +493,7 @@ void GDBStub::HandleQuery(std::string_view command) {
buffer += R"(<?xml version="1.0"?>)";
buffer += "<threads>";
- const auto& threads = system.ApplicationProcess()->GetThreadList();
+ const auto& threads = GetProcess()->GetThreadList();
for (const auto& thread : threads) {
auto thread_name{Core::GetThreadName(&thread)};
if (!thread_name) {
@@ -613,7 +609,7 @@ void GDBStub::HandleRcmd(const std::vector<u8>& command) {
std::string_view command_str{reinterpret_cast<const char*>(&command[0]), command.size()};
std::string reply;
- auto* process = system.ApplicationProcess();
+ auto* process = GetProcess();
auto& page_table = process->GetPageTable();
const char* commands = "Commands:\n"
@@ -714,7 +710,7 @@ void GDBStub::HandleRcmd(const std::vector<u8>& command) {
}
Kernel::KThread* GDBStub::GetThreadByID(u64 thread_id) {
- auto& threads{system.ApplicationProcess()->GetThreadList()};
+ auto& threads{GetProcess()->GetThreadList()};
for (auto& thread : threads) {
if (thread.GetThreadId() == thread_id) {
return std::addressof(thread);
@@ -783,4 +779,12 @@ void GDBStub::SendStatus(char status) {
backend.WriteToClient(buf);
}
+Kernel::KProcess* GDBStub::GetProcess() {
+ return debug_process;
+}
+
+Core::Memory::Memory& GDBStub::GetMemory() {
+ return GetProcess()->GetMemory();
+}
+
} // namespace Core
diff --git a/src/core/debugger/gdbstub.h b/src/core/debugger/gdbstub.h
index 368197920..232dcf49f 100644
--- a/src/core/debugger/gdbstub.h
+++ b/src/core/debugger/gdbstub.h
@@ -12,13 +12,22 @@
#include "core/debugger/debugger_interface.h"
#include "core/debugger/gdbstub_arch.h"
+namespace Kernel {
+class KProcess;
+}
+
+namespace Core::Memory {
+class Memory;
+}
+
namespace Core {
class System;
class GDBStub : public DebuggerFrontend {
public:
- explicit GDBStub(DebuggerBackend& backend, Core::System& system);
+ explicit GDBStub(DebuggerBackend& backend, Core::System& system,
+ Kernel::KProcess* debug_process);
~GDBStub() override;
void Connected() override;
@@ -42,8 +51,12 @@ private:
void SendReply(std::string_view data);
void SendStatus(char status);
+ Kernel::KProcess* GetProcess();
+ Core::Memory::Memory& GetMemory();
+
private:
Core::System& system;
+ Kernel::KProcess* debug_process;
std::unique_ptr<GDBStubArch> arch;
std::vector<char> current_command;
std::map<VAddr, u32> replaced_instructions;
diff --git a/src/core/file_sys/savedata_factory.cpp b/src/core/file_sys/savedata_factory.cpp
index 12b3bd797..23196cd5f 100644
--- a/src/core/file_sys/savedata_factory.cpp
+++ b/src/core/file_sys/savedata_factory.cpp
@@ -97,8 +97,9 @@ std::string SaveDataAttribute::DebugInfo() const {
static_cast<u8>(rank), index);
}
-SaveDataFactory::SaveDataFactory(Core::System& system_, VirtualDir save_directory_)
- : dir{std::move(save_directory_)}, system{system_} {
+SaveDataFactory::SaveDataFactory(Core::System& system_, ProgramId program_id_,
+ VirtualDir save_directory_)
+ : system{system_}, program_id{program_id_}, dir{std::move(save_directory_)} {
// Delete all temporary storages
// On hardware, it is expected that temporary storage be empty at first use.
dir->DeleteSubdirectoryRecursive("temp");
@@ -110,7 +111,7 @@ VirtualDir SaveDataFactory::Create(SaveDataSpaceId space, const SaveDataAttribut
PrintSaveDataAttributeWarnings(meta);
const auto save_directory =
- GetFullPath(system, dir, space, meta.type, meta.title_id, meta.user_id, meta.save_id);
+ GetFullPath(program_id, dir, space, meta.type, meta.title_id, meta.user_id, meta.save_id);
return dir->CreateDirectoryRelative(save_directory);
}
@@ -118,7 +119,7 @@ VirtualDir SaveDataFactory::Create(SaveDataSpaceId space, const SaveDataAttribut
VirtualDir SaveDataFactory::Open(SaveDataSpaceId space, const SaveDataAttribute& meta) const {
const auto save_directory =
- GetFullPath(system, dir, space, meta.type, meta.title_id, meta.user_id, meta.save_id);
+ GetFullPath(program_id, dir, space, meta.type, meta.title_id, meta.user_id, meta.save_id);
auto out = dir->GetDirectoryRelative(save_directory);
@@ -147,14 +148,14 @@ std::string SaveDataFactory::GetSaveDataSpaceIdPath(SaveDataSpaceId space) {
}
}
-std::string SaveDataFactory::GetFullPath(Core::System& system, VirtualDir dir,
+std::string SaveDataFactory::GetFullPath(ProgramId program_id, VirtualDir dir,
SaveDataSpaceId space, SaveDataType type, u64 title_id,
u128 user_id, u64 save_id) {
// According to switchbrew, if a save is of type SaveData and the title id field is 0, it should
// be interpreted as the title id of the current process.
if (type == SaveDataType::SaveData || type == SaveDataType::DeviceSaveData) {
if (title_id == 0) {
- title_id = system.GetApplicationProcessProgramID();
+ title_id = program_id;
}
}
@@ -201,7 +202,7 @@ std::string SaveDataFactory::GetUserGameSaveDataRoot(u128 user_id, bool future)
SaveDataSize SaveDataFactory::ReadSaveDataSize(SaveDataType type, u64 title_id,
u128 user_id) const {
const auto path =
- GetFullPath(system, dir, SaveDataSpaceId::NandUser, type, title_id, user_id, 0);
+ GetFullPath(program_id, dir, SaveDataSpaceId::NandUser, type, title_id, user_id, 0);
const auto relative_dir = GetOrCreateDirectoryRelative(dir, path);
const auto size_file = relative_dir->GetFile(GetSaveDataSizeFileName());
@@ -220,7 +221,7 @@ SaveDataSize SaveDataFactory::ReadSaveDataSize(SaveDataType type, u64 title_id,
void SaveDataFactory::WriteSaveDataSize(SaveDataType type, u64 title_id, u128 user_id,
SaveDataSize new_value) const {
const auto path =
- GetFullPath(system, dir, SaveDataSpaceId::NandUser, type, title_id, user_id, 0);
+ GetFullPath(program_id, dir, SaveDataSpaceId::NandUser, type, title_id, user_id, 0);
const auto relative_dir = GetOrCreateDirectoryRelative(dir, path);
const auto size_file = relative_dir->CreateFile(GetSaveDataSizeFileName());
diff --git a/src/core/file_sys/savedata_factory.h b/src/core/file_sys/savedata_factory.h
index fd4887e99..30d96928e 100644
--- a/src/core/file_sys/savedata_factory.h
+++ b/src/core/file_sys/savedata_factory.h
@@ -87,10 +87,13 @@ constexpr const char* GetSaveDataSizeFileName() {
return ".yuzu_save_size";
}
+using ProgramId = u64;
+
/// File system interface to the SaveData archive
class SaveDataFactory {
public:
- explicit SaveDataFactory(Core::System& system_, VirtualDir save_directory_);
+ explicit SaveDataFactory(Core::System& system_, ProgramId program_id_,
+ VirtualDir save_directory_);
~SaveDataFactory();
VirtualDir Create(SaveDataSpaceId space, const SaveDataAttribute& meta) const;
@@ -99,7 +102,7 @@ public:
VirtualDir GetSaveDataSpaceDirectory(SaveDataSpaceId space) const;
static std::string GetSaveDataSpaceIdPath(SaveDataSpaceId space);
- static std::string GetFullPath(Core::System& system, VirtualDir dir, SaveDataSpaceId space,
+ static std::string GetFullPath(ProgramId program_id, VirtualDir dir, SaveDataSpaceId space,
SaveDataType type, u64 title_id, u128 user_id, u64 save_id);
static std::string GetUserGameSaveDataRoot(u128 user_id, bool future);
@@ -110,8 +113,9 @@ public:
void SetAutoCreate(bool state);
private:
- VirtualDir dir;
Core::System& system;
+ ProgramId program_id;
+ VirtualDir dir;
bool auto_create{true};
};
diff --git a/src/core/hle/kernel/k_memory_block_manager.cpp b/src/core/hle/kernel/k_memory_block_manager.cpp
index 58a1e7216..f08a6e448 100644
--- a/src/core/hle/kernel/k_memory_block_manager.cpp
+++ b/src/core/hle/kernel/k_memory_block_manager.cpp
@@ -28,14 +28,14 @@ Result KMemoryBlockManager::Initialize(KProcessAddress st, KProcessAddress nd,
}
void KMemoryBlockManager::Finalize(KMemoryBlockSlabManager* slab_manager,
- HostUnmapCallback&& host_unmap_callback) {
+ BlockCallback&& block_callback) {
// Erase every block until we have none left.
auto it = m_memory_block_tree.begin();
while (it != m_memory_block_tree.end()) {
KMemoryBlock* block = std::addressof(*it);
it = m_memory_block_tree.erase(it);
+ block_callback(block->GetAddress(), block->GetSize());
slab_manager->Free(block);
- host_unmap_callback(block->GetAddress(), block->GetSize());
}
ASSERT(m_memory_block_tree.empty());
diff --git a/src/core/hle/kernel/k_memory_block_manager.h b/src/core/hle/kernel/k_memory_block_manager.h
index cb7b6f430..377628504 100644
--- a/src/core/hle/kernel/k_memory_block_manager.h
+++ b/src/core/hle/kernel/k_memory_block_manager.h
@@ -85,11 +85,11 @@ public:
public:
KMemoryBlockManager();
- using HostUnmapCallback = std::function<void(Common::ProcessAddress, u64)>;
+ using BlockCallback = std::function<void(Common::ProcessAddress, u64)>;
Result Initialize(KProcessAddress st, KProcessAddress nd,
KMemoryBlockSlabManager* slab_manager);
- void Finalize(KMemoryBlockSlabManager* slab_manager, HostUnmapCallback&& host_unmap_callback);
+ void Finalize(KMemoryBlockSlabManager* slab_manager, BlockCallback&& block_callback);
iterator end() {
return m_memory_block_tree.end();
diff --git a/src/core/hle/kernel/k_page_table_base.cpp b/src/core/hle/kernel/k_page_table_base.cpp
index 73fbda331..3f0a39d33 100644
--- a/src/core/hle/kernel/k_page_table_base.cpp
+++ b/src/core/hle/kernel/k_page_table_base.cpp
@@ -431,15 +431,43 @@ Result KPageTableBase::InitializeForProcess(Svc::CreateProcessFlag as_type, bool
m_memory_block_slab_manager));
}
+Result KPageTableBase::FinalizeProcess() {
+ // Only process tables should be finalized.
+ ASSERT(!this->IsKernel());
+
+ // NOTE: Here Nintendo calls an unknown OnFinalize function.
+ // this->OnFinalize();
+
+ // NOTE: Here Nintendo calls a second unknown OnFinalize function.
+ // this->OnFinalize2();
+
+ // NOTE: Here Nintendo does a page table walk to discover heap pages to free.
+ // We will use the block manager finalization below to free them.
+
+ R_SUCCEED();
+}
+
void KPageTableBase::Finalize() {
- auto HostUnmapCallback = [&](KProcessAddress addr, u64 size) {
- if (Settings::IsFastmemEnabled()) {
+ this->FinalizeProcess();
+
+ auto BlockCallback = [&](KProcessAddress addr, u64 size) {
+ if (m_impl->fastmem_arena) {
m_system.DeviceMemory().buffer.Unmap(GetInteger(addr), size, false);
}
+
+ // Get physical pages.
+ KPageGroup pg(m_kernel, m_block_info_manager);
+ this->MakePageGroup(pg, addr, size / PageSize);
+
+ // Free the pages.
+ pg.CloseAndReset();
};
// Finalize memory blocks.
- m_memory_block_manager.Finalize(m_memory_block_slab_manager, std::move(HostUnmapCallback));
+ {
+ KScopedLightLock lk(m_general_lock);
+ m_memory_block_manager.Finalize(m_memory_block_slab_manager, std::move(BlockCallback));
+ }
// Free any unsafe mapped memory.
if (m_mapped_unsafe_physical_memory) {
diff --git a/src/core/hle/kernel/k_page_table_base.h b/src/core/hle/kernel/k_page_table_base.h
index 077cafc96..748419f86 100644
--- a/src/core/hle/kernel/k_page_table_base.h
+++ b/src/core/hle/kernel/k_page_table_base.h
@@ -241,6 +241,7 @@ public:
KResourceLimit* resource_limit, Core::Memory::Memory& memory,
KProcessAddress aslr_space_start);
+ Result FinalizeProcess();
void Finalize();
bool IsKernel() const {
diff --git a/src/core/hle/kernel/k_process.cpp b/src/core/hle/kernel/k_process.cpp
index 068e71dff..53735a225 100644
--- a/src/core/hle/kernel/k_process.cpp
+++ b/src/core/hle/kernel/k_process.cpp
@@ -171,6 +171,12 @@ void KProcess::Finalize() {
m_resource_limit->Close();
}
+ // Clear expensive resources, as the destructor is not called for guest objects.
+ for (auto& interface : m_arm_interfaces) {
+ interface.reset();
+ }
+ m_exclusive_monitor.reset();
+
// Perform inherited finalization.
KSynchronizationObject::Finalize();
}
@@ -1233,10 +1239,10 @@ void KProcess::LoadModule(CodeSet code_set, KProcessAddress base_addr) {
ReprotectSegment(code_set.DataSegment(), Svc::MemoryPermission::ReadWrite);
#ifdef HAS_NCE
- if (this->IsApplication() && Settings::IsNceEnabled()) {
+ const auto& patch = code_set.PatchSegment();
+ if (this->IsApplication() && Settings::IsNceEnabled() && patch.size != 0) {
auto& buffer = m_kernel.System().DeviceMemory().buffer;
const auto& code = code_set.CodeSegment();
- const auto& patch = code_set.PatchSegment();
buffer.Protect(GetInteger(base_addr + code.addr), code.size,
Common::MemoryPermission::Read | Common::MemoryPermission::Execute);
buffer.Protect(GetInteger(base_addr + patch.addr), patch.size,
diff --git a/src/core/hle/kernel/kernel.cpp b/src/core/hle/kernel/kernel.cpp
index 1030f0c12..f3683cdcc 100644
--- a/src/core/hle/kernel/kernel.cpp
+++ b/src/core/hle/kernel/kernel.cpp
@@ -112,7 +112,14 @@ struct KernelCore::Impl {
old_process->Close();
}
- process_list.clear();
+ {
+ std::scoped_lock lk{process_list_lock};
+ for (auto* const process : process_list) {
+ process->Terminate();
+ process->Close();
+ }
+ process_list.clear();
+ }
next_object_id = 0;
next_kernel_process_id = KProcess::InitialProcessIdMin;
@@ -770,6 +777,7 @@ struct KernelCore::Impl {
std::atomic<u64> next_thread_id{1};
// Lists all processes that exist in the current session.
+ std::mutex process_list_lock;
std::vector<KProcess*> process_list;
std::atomic<KProcess*> application_process{};
std::unique_ptr<Kernel::GlobalSchedulerContext> global_scheduler_context;
@@ -869,9 +877,19 @@ KResourceLimit* KernelCore::GetSystemResourceLimit() {
}
void KernelCore::AppendNewProcess(KProcess* process) {
+ process->Open();
+
+ std::scoped_lock lk{impl->process_list_lock};
impl->process_list.push_back(process);
}
+void KernelCore::RemoveProcess(KProcess* process) {
+ std::scoped_lock lk{impl->process_list_lock};
+ if (std::erase(impl->process_list, process)) {
+ process->Close();
+ }
+}
+
void KernelCore::MakeApplicationProcess(KProcess* process) {
impl->MakeApplicationProcess(process);
}
@@ -884,8 +902,15 @@ const KProcess* KernelCore::ApplicationProcess() const {
return impl->application_process;
}
-const std::vector<KProcess*>& KernelCore::GetProcessList() const {
- return impl->process_list;
+std::list<KScopedAutoObject<KProcess>> KernelCore::GetProcessList() {
+ std::list<KScopedAutoObject<KProcess>> processes;
+ std::scoped_lock lk{impl->process_list_lock};
+
+ for (auto* const process : impl->process_list) {
+ processes.emplace_back(process);
+ }
+
+ return processes;
}
Kernel::GlobalSchedulerContext& KernelCore::GlobalSchedulerContext() {
diff --git a/src/core/hle/kernel/kernel.h b/src/core/hle/kernel/kernel.h
index 5d4102145..8ea5bed1c 100644
--- a/src/core/hle/kernel/kernel.h
+++ b/src/core/hle/kernel/kernel.h
@@ -5,6 +5,7 @@
#include <array>
#include <functional>
+#include <list>
#include <memory>
#include <string>
#include <unordered_map>
@@ -116,8 +117,9 @@ public:
/// Retrieves a shared pointer to the system resource limit instance.
KResourceLimit* GetSystemResourceLimit();
- /// Adds the given shared pointer to an internal list of active processes.
+ /// Adds/removes the given pointer to an internal list of active processes.
void AppendNewProcess(KProcess* process);
+ void RemoveProcess(KProcess* process);
/// Makes the given process the new application process.
void MakeApplicationProcess(KProcess* process);
@@ -129,7 +131,7 @@ public:
const KProcess* ApplicationProcess() const;
/// Retrieves the list of processes.
- const std::vector<KProcess*>& GetProcessList() const;
+ std::list<KScopedAutoObject<KProcess>> GetProcessList();
/// Gets the sole instance of the global scheduler
Kernel::GlobalSchedulerContext& GlobalSchedulerContext();
diff --git a/src/core/hle/kernel/svc/svc_process.cpp b/src/core/hle/kernel/svc/svc_process.cpp
index caa8bee9a..5c3e8829f 100644
--- a/src/core/hle/kernel/svc/svc_process.cpp
+++ b/src/core/hle/kernel/svc/svc_process.cpp
@@ -74,13 +74,15 @@ Result GetProcessList(Core::System& system, s32* out_num_processes, u64 out_proc
}
auto& memory = GetCurrentMemory(kernel);
- const auto& process_list = kernel.GetProcessList();
+ auto process_list = kernel.GetProcessList();
+ auto it = process_list.begin();
+
const auto num_processes = process_list.size();
const auto copy_amount =
std::min(static_cast<std::size_t>(out_process_ids_size), num_processes);
- for (std::size_t i = 0; i < copy_amount; ++i) {
- memory.Write64(out_process_ids, process_list[i]->GetProcessId());
+ for (std::size_t i = 0; i < copy_amount && it != process_list.end(); ++i, ++it) {
+ memory.Write64(out_process_ids, (*it)->GetProcessId());
out_process_ids += sizeof(u64);
}
diff --git a/src/core/hle/service/acc/profile_manager.cpp b/src/core/hle/service/acc/profile_manager.cpp
index 5542d6cbc..683f44e27 100644
--- a/src/core/hle/service/acc/profile_manager.cpp
+++ b/src/core/hle/service/acc/profile_manager.cpp
@@ -61,9 +61,7 @@ ProfileManager::ProfileManager() {
OpenUser(*GetUser(current));
}
-ProfileManager::~ProfileManager() {
- WriteUserSaveFile();
-}
+ProfileManager::~ProfileManager() = default;
/// After a users creation it needs to be "registered" to the system. AddToProfiles handles the
/// internal management of the users profiles
@@ -113,6 +111,8 @@ Result ProfileManager::CreateNewUser(UUID uuid, const ProfileUsername& username)
return ERROR_USER_ALREADY_EXISTS;
}
+ is_save_needed = true;
+
return AddUser({
.user_uuid = uuid,
.username = username,
@@ -326,6 +326,9 @@ bool ProfileManager::RemoveUser(UUID uuid) {
profiles[*index] = ProfileInfo{};
std::stable_partition(profiles.begin(), profiles.end(),
[](const ProfileInfo& profile) { return profile.user_uuid.IsValid(); });
+
+ is_save_needed = true;
+
return true;
}
@@ -340,6 +343,8 @@ bool ProfileManager::SetProfileBase(UUID uuid, const ProfileBase& profile_new) {
profile.username = profile_new.username;
profile.creation_time = profile_new.timestamp;
+ is_save_needed = true;
+
return true;
}
@@ -348,6 +353,7 @@ bool ProfileManager::SetProfileBaseAndData(Common::UUID uuid, const ProfileBase&
const auto index = GetUserIndex(uuid);
if (index.has_value() && SetProfileBase(uuid, profile_new)) {
profiles[*index].data = data_new;
+ is_save_needed = true;
return true;
}
@@ -391,6 +397,10 @@ void ProfileManager::ParseUserSaveFile() {
}
void ProfileManager::WriteUserSaveFile() {
+ if (!is_save_needed) {
+ return;
+ }
+
ProfileDataRaw raw{};
for (std::size_t i = 0; i < MAX_USERS; ++i) {
@@ -423,7 +433,10 @@ void ProfileManager::WriteUserSaveFile() {
if (!save.IsOpen() || !save.SetSize(sizeof(ProfileDataRaw)) || !save.WriteObject(raw)) {
LOG_WARNING(Service_ACC, "Failed to write save data to file... No changes to user data "
"made in current session will be saved.");
+ return;
}
+
+ is_save_needed = false;
}
}; // namespace Service::Account
diff --git a/src/core/hle/service/acc/profile_manager.h b/src/core/hle/service/acc/profile_manager.h
index 900e32200..e21863e64 100644
--- a/src/core/hle/service/acc/profile_manager.h
+++ b/src/core/hle/service/acc/profile_manager.h
@@ -103,6 +103,7 @@ private:
std::optional<std::size_t> AddToProfiles(const ProfileInfo& profile);
bool RemoveProfileAtIndex(std::size_t index);
+ bool is_save_needed{};
std::array<ProfileInfo, MAX_USERS> profiles{};
std::array<ProfileInfo, MAX_USERS> stored_opened_profiles{};
std::size_t user_count{};
diff --git a/src/core/hle/service/am/am.cpp b/src/core/hle/service/am/am.cpp
index 9e05bdafa..a768bdc54 100644
--- a/src/core/hle/service/am/am.cpp
+++ b/src/core/hle/service/am/am.cpp
@@ -36,6 +36,7 @@
#include "core/hle/service/caps/caps_su.h"
#include "core/hle/service/caps/caps_types.h"
#include "core/hle/service/filesystem/filesystem.h"
+#include "core/hle/service/filesystem/save_data_controller.h"
#include "core/hle/service/ipc_helpers.h"
#include "core/hle/service/ns/ns.h"
#include "core/hle/service/nvnflinger/fb_share_buffer_manager.h"
@@ -2178,7 +2179,7 @@ void IApplicationFunctions::EnsureSaveData(HLERequestContext& ctx) {
attribute.type = FileSys::SaveDataType::SaveData;
FileSys::VirtualDir save_data{};
- const auto res = system.GetFileSystemController().CreateSaveData(
+ const auto res = system.GetFileSystemController().OpenSaveDataController()->CreateSaveData(
&save_data, FileSys::SaveDataSpaceId::NandUser, attribute);
IPC::ResponseBuilder rb{ctx, 4};
@@ -2353,7 +2354,7 @@ void IApplicationFunctions::ExtendSaveData(HLERequestContext& ctx) {
"new_journal={:016X}",
static_cast<u8>(type), user_id[1], user_id[0], new_normal_size, new_journal_size);
- system.GetFileSystemController().WriteSaveDataSize(
+ system.GetFileSystemController().OpenSaveDataController()->WriteSaveDataSize(
type, system.GetApplicationProcessProgramID(), user_id,
{new_normal_size, new_journal_size});
@@ -2378,7 +2379,7 @@ void IApplicationFunctions::GetSaveDataSize(HLERequestContext& ctx) {
LOG_DEBUG(Service_AM, "called with type={:02X}, user_id={:016X}{:016X}", type, user_id[1],
user_id[0]);
- const auto size = system.GetFileSystemController().ReadSaveDataSize(
+ const auto size = system.GetFileSystemController().OpenSaveDataController()->ReadSaveDataSize(
type, system.GetApplicationProcessProgramID(), user_id);
IPC::ResponseBuilder rb{ctx, 6};
diff --git a/src/core/hle/service/audio/audin_u.cpp b/src/core/hle/service/audio/audin_u.cpp
index 56fee4591..de2aa6906 100644
--- a/src/core/hle/service/audio/audin_u.cpp
+++ b/src/core/hle/service/audio/audin_u.cpp
@@ -18,11 +18,11 @@ using namespace AudioCore::AudioIn;
class IAudioIn final : public ServiceFramework<IAudioIn> {
public:
explicit IAudioIn(Core::System& system_, Manager& manager, size_t session_id,
- const std::string& device_name, const AudioInParameter& in_params, u32 handle,
- u64 applet_resource_user_id)
+ const std::string& device_name, const AudioInParameter& in_params,
+ Kernel::KProcess* handle, u64 applet_resource_user_id)
: ServiceFramework{system_, "IAudioIn"},
service_context{system_, "IAudioIn"}, event{service_context.CreateEvent("AudioInEvent")},
- impl{std::make_shared<In>(system_, manager, event, session_id)} {
+ process{handle}, impl{std::make_shared<In>(system_, manager, event, session_id)} {
// clang-format off
static const FunctionInfo functions[] = {
{0, &IAudioIn::GetAudioInState, "GetAudioInState"},
@@ -45,6 +45,8 @@ public:
RegisterHandlers(functions);
+ process->Open();
+
if (impl->GetSystem()
.Initialize(device_name, in_params, handle, applet_resource_user_id)
.IsError()) {
@@ -55,6 +57,7 @@ public:
~IAudioIn() override {
impl->Free();
service_context.CloseEvent(event);
+ process->Close();
}
[[nodiscard]] std::shared_ptr<In> GetImpl() {
@@ -196,6 +199,7 @@ private:
KernelHelpers::ServiceContext service_context;
Kernel::KEvent* event;
+ Kernel::KProcess* process;
std::shared_ptr<AudioCore::AudioIn::In> impl;
Common::ScratchBuffer<u64> released_buffer;
};
@@ -267,6 +271,14 @@ void AudInU::OpenAudioIn(HLERequestContext& ctx) {
auto device_name = Common::StringFromBuffer(device_name_data);
auto handle{ctx.GetCopyHandle(0)};
+ auto process{ctx.GetObjectFromHandle<Kernel::KProcess>(handle)};
+ if (process.IsNull()) {
+ LOG_ERROR(Service_Audio, "Failed to get process handle");
+ IPC::ResponseBuilder rb{ctx, 2};
+ rb.Push(ResultUnknown);
+ return;
+ }
+
std::scoped_lock l{impl->mutex};
auto link{impl->LinkToManager()};
if (link.IsError()) {
@@ -287,8 +299,9 @@ void AudInU::OpenAudioIn(HLERequestContext& ctx) {
LOG_DEBUG(Service_Audio, "Opening new AudioIn, sessionid={}, free sessions={}", new_session_id,
impl->num_free_sessions);
- auto audio_in = std::make_shared<IAudioIn>(system, *impl, new_session_id, device_name,
- in_params, handle, applet_resource_user_id);
+ auto audio_in =
+ std::make_shared<IAudioIn>(system, *impl, new_session_id, device_name, in_params,
+ process.GetPointerUnsafe(), applet_resource_user_id);
impl->sessions[new_session_id] = audio_in->GetImpl();
impl->applet_resource_user_ids[new_session_id] = applet_resource_user_id;
@@ -318,6 +331,14 @@ void AudInU::OpenAudioInProtocolSpecified(HLERequestContext& ctx) {
auto device_name = Common::StringFromBuffer(device_name_data);
auto handle{ctx.GetCopyHandle(0)};
+ auto process{ctx.GetObjectFromHandle<Kernel::KProcess>(handle)};
+ if (process.IsNull()) {
+ LOG_ERROR(Service_Audio, "Failed to get process handle");
+ IPC::ResponseBuilder rb{ctx, 2};
+ rb.Push(ResultUnknown);
+ return;
+ }
+
std::scoped_lock l{impl->mutex};
auto link{impl->LinkToManager()};
if (link.IsError()) {
@@ -338,8 +359,9 @@ void AudInU::OpenAudioInProtocolSpecified(HLERequestContext& ctx) {
LOG_DEBUG(Service_Audio, "Opening new AudioIn, sessionid={}, free sessions={}", new_session_id,
impl->num_free_sessions);
- auto audio_in = std::make_shared<IAudioIn>(system, *impl, new_session_id, device_name,
- in_params, handle, applet_resource_user_id);
+ auto audio_in =
+ std::make_shared<IAudioIn>(system, *impl, new_session_id, device_name, in_params,
+ process.GetPointerUnsafe(), applet_resource_user_id);
impl->sessions[new_session_id] = audio_in->GetImpl();
impl->applet_resource_user_ids[new_session_id] = applet_resource_user_id;
diff --git a/src/core/hle/service/audio/audout_u.cpp b/src/core/hle/service/audio/audout_u.cpp
index ca683d72c..8cc7b69f4 100644
--- a/src/core/hle/service/audio/audout_u.cpp
+++ b/src/core/hle/service/audio/audout_u.cpp
@@ -26,9 +26,10 @@ class IAudioOut final : public ServiceFramework<IAudioOut> {
public:
explicit IAudioOut(Core::System& system_, AudioCore::AudioOut::Manager& manager,
size_t session_id, const std::string& device_name,
- const AudioOutParameter& in_params, u32 handle, u64 applet_resource_user_id)
+ const AudioOutParameter& in_params, Kernel::KProcess* handle,
+ u64 applet_resource_user_id)
: ServiceFramework{system_, "IAudioOut"}, service_context{system_, "IAudioOut"},
- event{service_context.CreateEvent("AudioOutEvent")},
+ event{service_context.CreateEvent("AudioOutEvent")}, process{handle},
impl{std::make_shared<AudioCore::AudioOut::Out>(system_, manager, event, session_id)} {
// clang-format off
@@ -50,11 +51,14 @@ public:
};
// clang-format on
RegisterHandlers(functions);
+
+ process->Open();
}
~IAudioOut() override {
impl->Free();
service_context.CloseEvent(event);
+ process->Close();
}
[[nodiscard]] std::shared_ptr<AudioCore::AudioOut::Out> GetImpl() {
@@ -206,6 +210,7 @@ private:
KernelHelpers::ServiceContext service_context;
Kernel::KEvent* event;
+ Kernel::KProcess* process;
std::shared_ptr<AudioCore::AudioOut::Out> impl;
Common::ScratchBuffer<u64> released_buffer;
};
@@ -257,6 +262,14 @@ void AudOutU::OpenAudioOut(HLERequestContext& ctx) {
auto device_name = Common::StringFromBuffer(device_name_data);
auto handle{ctx.GetCopyHandle(0)};
+ auto process{ctx.GetObjectFromHandle<Kernel::KProcess>(handle)};
+ if (process.IsNull()) {
+ LOG_ERROR(Service_Audio, "Failed to get process handle");
+ IPC::ResponseBuilder rb{ctx, 2};
+ rb.Push(ResultUnknown);
+ return;
+ }
+
auto link{impl->LinkToManager()};
if (link.IsError()) {
LOG_ERROR(Service_Audio, "Failed to link Audio Out to Audio Manager");
@@ -276,10 +289,11 @@ void AudOutU::OpenAudioOut(HLERequestContext& ctx) {
LOG_DEBUG(Service_Audio, "Opening new AudioOut, sessionid={}, free sessions={}", new_session_id,
impl->num_free_sessions);
- auto audio_out = std::make_shared<IAudioOut>(system, *impl, new_session_id, device_name,
- in_params, handle, applet_resource_user_id);
- result = audio_out->GetImpl()->GetSystem().Initialize(device_name, in_params, handle,
- applet_resource_user_id);
+ auto audio_out =
+ std::make_shared<IAudioOut>(system, *impl, new_session_id, device_name, in_params,
+ process.GetPointerUnsafe(), applet_resource_user_id);
+ result = audio_out->GetImpl()->GetSystem().Initialize(
+ device_name, in_params, process.GetPointerUnsafe(), applet_resource_user_id);
if (result.IsError()) {
LOG_ERROR(Service_Audio, "Failed to initialize the AudioOut System!");
IPC::ResponseBuilder rb{ctx, 2};
diff --git a/src/core/hle/service/filesystem/filesystem.cpp b/src/core/hle/service/filesystem/filesystem.cpp
index 780f8c74d..ca6d8d607 100644
--- a/src/core/hle/service/filesystem/filesystem.cpp
+++ b/src/core/hle/service/filesystem/filesystem.cpp
@@ -24,15 +24,13 @@
#include "core/hle/service/filesystem/fsp_ldr.h"
#include "core/hle/service/filesystem/fsp_pr.h"
#include "core/hle/service/filesystem/fsp_srv.h"
+#include "core/hle/service/filesystem/romfs_controller.h"
+#include "core/hle/service/filesystem/save_data_controller.h"
#include "core/hle/service/server_manager.h"
#include "core/loader/loader.h"
namespace Service::FileSystem {
-// A default size for normal/journal save data size if application control metadata cannot be found.
-// This should be large enough to satisfy even the most extreme requirements (~4.2GB)
-constexpr u64 SUFFICIENT_SAVE_DATA_SIZE = 0xF0000000;
-
static FileSys::VirtualDir GetDirectoryRelativeWrapped(FileSys::VirtualDir base,
std::string_view dir_name_) {
std::string dir_name(Common::FS::SanitizePath(dir_name_));
@@ -297,145 +295,65 @@ FileSystemController::FileSystemController(Core::System& system_) : system{syste
FileSystemController::~FileSystemController() = default;
-Result FileSystemController::RegisterRomFS(std::unique_ptr<FileSys::RomFSFactory>&& factory) {
- romfs_factory = std::move(factory);
- LOG_DEBUG(Service_FS, "Registered RomFS");
- return ResultSuccess;
-}
-
-Result FileSystemController::RegisterSaveData(std::unique_ptr<FileSys::SaveDataFactory>&& factory) {
- ASSERT_MSG(save_data_factory == nullptr, "Tried to register a second save data");
- save_data_factory = std::move(factory);
- LOG_DEBUG(Service_FS, "Registered save data");
- return ResultSuccess;
-}
+Result FileSystemController::RegisterProcess(
+ ProcessId process_id, ProgramId program_id,
+ std::shared_ptr<FileSys::RomFSFactory>&& romfs_factory) {
+ std::scoped_lock lk{registration_lock};
-Result FileSystemController::RegisterSDMC(std::unique_ptr<FileSys::SDMCFactory>&& factory) {
- ASSERT_MSG(sdmc_factory == nullptr, "Tried to register a second SDMC");
- sdmc_factory = std::move(factory);
- LOG_DEBUG(Service_FS, "Registered SDMC");
- return ResultSuccess;
-}
+ registrations.emplace(process_id, Registration{
+ .program_id = program_id,
+ .romfs_factory = std::move(romfs_factory),
+ .save_data_factory = CreateSaveDataFactory(program_id),
+ });
-Result FileSystemController::RegisterBIS(std::unique_ptr<FileSys::BISFactory>&& factory) {
- ASSERT_MSG(bis_factory == nullptr, "Tried to register a second BIS");
- bis_factory = std::move(factory);
- LOG_DEBUG(Service_FS, "Registered BIS");
+ LOG_DEBUG(Service_FS, "Registered for process {}", process_id);
return ResultSuccess;
}
-void FileSystemController::SetPackedUpdate(FileSys::VirtualFile update_raw) {
- LOG_TRACE(Service_FS, "Setting packed update for romfs");
-
- if (romfs_factory == nullptr)
- return;
-
- romfs_factory->SetPackedUpdate(std::move(update_raw));
-}
-
-FileSys::VirtualFile FileSystemController::OpenRomFSCurrentProcess() const {
- LOG_TRACE(Service_FS, "Opening RomFS for current process");
-
- if (romfs_factory == nullptr) {
- return nullptr;
- }
-
- return romfs_factory->OpenCurrentProcess(system.GetApplicationProcessProgramID());
-}
-
-FileSys::VirtualFile FileSystemController::OpenPatchedRomFS(u64 title_id,
- FileSys::ContentRecordType type) const {
- LOG_TRACE(Service_FS, "Opening patched RomFS for title_id={:016X}", title_id);
-
- if (romfs_factory == nullptr) {
- return nullptr;
- }
-
- return romfs_factory->OpenPatchedRomFS(title_id, type);
-}
-
-FileSys::VirtualFile FileSystemController::OpenPatchedRomFSWithProgramIndex(
- u64 title_id, u8 program_index, FileSys::ContentRecordType type) const {
- LOG_TRACE(Service_FS, "Opening patched RomFS for title_id={:016X}, program_index={}", title_id,
- program_index);
-
- if (romfs_factory == nullptr) {
- return nullptr;
- }
-
- return romfs_factory->OpenPatchedRomFSWithProgramIndex(title_id, program_index, type);
-}
-
-FileSys::VirtualFile FileSystemController::OpenRomFS(u64 title_id, FileSys::StorageId storage_id,
- FileSys::ContentRecordType type) const {
- LOG_TRACE(Service_FS, "Opening RomFS for title_id={:016X}, storage_id={:02X}, type={:02X}",
- title_id, storage_id, type);
-
- if (romfs_factory == nullptr) {
- return nullptr;
- }
-
- return romfs_factory->Open(title_id, storage_id, type);
-}
-
-std::shared_ptr<FileSys::NCA> FileSystemController::OpenBaseNca(
- u64 title_id, FileSys::StorageId storage_id, FileSys::ContentRecordType type) const {
- return romfs_factory->GetEntry(title_id, storage_id, type);
-}
-
-Result FileSystemController::CreateSaveData(FileSys::VirtualDir* out_save_data,
- FileSys::SaveDataSpaceId space,
- const FileSys::SaveDataAttribute& save_struct) const {
- LOG_TRACE(Service_FS, "Creating Save Data for space_id={:01X}, save_struct={}", space,
- save_struct.DebugInfo());
+Result FileSystemController::OpenProcess(
+ ProgramId* out_program_id, std::shared_ptr<SaveDataController>* out_save_data_controller,
+ std::shared_ptr<RomFsController>* out_romfs_controller, ProcessId process_id) {
+ std::scoped_lock lk{registration_lock};
- if (save_data_factory == nullptr) {
+ const auto it = registrations.find(process_id);
+ if (it == registrations.end()) {
return FileSys::ERROR_ENTITY_NOT_FOUND;
}
- auto save_data = save_data_factory->Create(space, save_struct);
- if (save_data == nullptr) {
- return FileSys::ERROR_ENTITY_NOT_FOUND;
- }
-
- *out_save_data = save_data;
+ *out_program_id = it->second.program_id;
+ *out_save_data_controller =
+ std::make_shared<SaveDataController>(system, it->second.save_data_factory);
+ *out_romfs_controller =
+ std::make_shared<RomFsController>(it->second.romfs_factory, it->second.program_id);
return ResultSuccess;
}
-Result FileSystemController::OpenSaveData(FileSys::VirtualDir* out_save_data,
- FileSys::SaveDataSpaceId space,
- const FileSys::SaveDataAttribute& attribute) const {
- LOG_TRACE(Service_FS, "Opening Save Data for space_id={:01X}, save_struct={}", space,
- attribute.DebugInfo());
-
- if (save_data_factory == nullptr) {
- return FileSys::ERROR_ENTITY_NOT_FOUND;
- }
+void FileSystemController::SetPackedUpdate(ProcessId process_id, FileSys::VirtualFile update_raw) {
+ LOG_TRACE(Service_FS, "Setting packed update for romfs");
- auto save_data = save_data_factory->Open(space, attribute);
- if (save_data == nullptr) {
- return FileSys::ERROR_ENTITY_NOT_FOUND;
+ std::scoped_lock lk{registration_lock};
+ const auto it = registrations.find(process_id);
+ if (it == registrations.end()) {
+ return;
}
- *out_save_data = save_data;
- return ResultSuccess;
+ it->second.romfs_factory->SetPackedUpdate(std::move(update_raw));
}
-Result FileSystemController::OpenSaveDataSpace(FileSys::VirtualDir* out_save_data_space,
- FileSys::SaveDataSpaceId space) const {
- LOG_TRACE(Service_FS, "Opening Save Data Space for space_id={:01X}", space);
-
- if (save_data_factory == nullptr) {
- return FileSys::ERROR_ENTITY_NOT_FOUND;
- }
+std::shared_ptr<SaveDataController> FileSystemController::OpenSaveDataController() {
+ return std::make_shared<SaveDataController>(system, CreateSaveDataFactory(ProgramId{}));
+}
- auto save_data_space = save_data_factory->GetSaveDataSpaceDirectory(space);
- if (save_data_space == nullptr) {
- return FileSys::ERROR_ENTITY_NOT_FOUND;
- }
+std::shared_ptr<FileSys::SaveDataFactory> FileSystemController::CreateSaveDataFactory(
+ ProgramId program_id) {
+ using YuzuPath = Common::FS::YuzuPath;
+ const auto rw_mode = FileSys::Mode::ReadWrite;
- *out_save_data_space = save_data_space;
- return ResultSuccess;
+ auto vfs = system.GetFilesystem();
+ const auto nand_directory =
+ vfs->OpenDirectory(Common::FS::GetYuzuPathString(YuzuPath::NANDDir), rw_mode);
+ return std::make_shared<FileSys::SaveDataFactory>(system, program_id,
+ std::move(nand_directory));
}
Result FileSystemController::OpenSDMC(FileSys::VirtualDir* out_sdmc) const {
@@ -540,48 +458,6 @@ u64 FileSystemController::GetTotalSpaceSize(FileSys::StorageId id) const {
return 0;
}
-FileSys::SaveDataSize FileSystemController::ReadSaveDataSize(FileSys::SaveDataType type,
- u64 title_id, u128 user_id) const {
- if (save_data_factory == nullptr) {
- return {0, 0};
- }
-
- const auto value = save_data_factory->ReadSaveDataSize(type, title_id, user_id);
-
- if (value.normal == 0 && value.journal == 0) {
- FileSys::SaveDataSize new_size{SUFFICIENT_SAVE_DATA_SIZE, SUFFICIENT_SAVE_DATA_SIZE};
-
- FileSys::NACP nacp;
- const auto res = system.GetAppLoader().ReadControlData(nacp);
-
- if (res != Loader::ResultStatus::Success) {
- const FileSys::PatchManager pm{system.GetApplicationProcessProgramID(),
- system.GetFileSystemController(),
- system.GetContentProvider()};
- const auto metadata = pm.GetControlMetadata();
- const auto& nacp_unique = metadata.first;
-
- if (nacp_unique != nullptr) {
- new_size = {nacp_unique->GetDefaultNormalSaveSize(),
- nacp_unique->GetDefaultJournalSaveSize()};
- }
- } else {
- new_size = {nacp.GetDefaultNormalSaveSize(), nacp.GetDefaultJournalSaveSize()};
- }
-
- WriteSaveDataSize(type, title_id, user_id, new_size);
- return new_size;
- }
-
- return value;
-}
-
-void FileSystemController::WriteSaveDataSize(FileSys::SaveDataType type, u64 title_id, u128 user_id,
- FileSys::SaveDataSize new_value) const {
- if (save_data_factory != nullptr)
- save_data_factory->WriteSaveDataSize(type, title_id, user_id, new_value);
-}
-
void FileSystemController::SetGameCard(FileSys::VirtualFile file) {
gamecard = std::make_unique<FileSys::XCI>(file);
const auto dir = gamecard->ConcatenatedPseudoDirectory();
@@ -801,14 +677,9 @@ FileSys::VirtualDir FileSystemController::GetBCATDirectory(u64 title_id) const {
return bis_factory->GetBCATDirectory(title_id);
}
-void FileSystemController::SetAutoSaveDataCreation(bool enable) {
- save_data_factory->SetAutoCreate(enable);
-}
-
void FileSystemController::CreateFactories(FileSys::VfsFilesystem& vfs, bool overwrite) {
if (overwrite) {
bis_factory = nullptr;
- save_data_factory = nullptr;
sdmc_factory = nullptr;
}
@@ -836,11 +707,6 @@ void FileSystemController::CreateFactories(FileSys::VfsFilesystem& vfs, bool ove
bis_factory->GetUserNANDContents());
}
- if (save_data_factory == nullptr) {
- save_data_factory =
- std::make_unique<FileSys::SaveDataFactory>(system, std::move(nand_directory));
- }
-
if (sdmc_factory == nullptr) {
sdmc_factory = std::make_unique<FileSys::SDMCFactory>(std::move(sd_directory),
std::move(sd_load_directory));
@@ -849,12 +715,19 @@ void FileSystemController::CreateFactories(FileSys::VfsFilesystem& vfs, bool ove
}
}
+void FileSystemController::Reset() {
+ std::scoped_lock lk{registration_lock};
+ registrations.clear();
+}
+
void LoopProcess(Core::System& system) {
auto server_manager = std::make_unique<ServerManager>(system);
+ const auto FileSystemProxyFactory = [&] { return std::make_shared<FSP_SRV>(system); };
+
server_manager->RegisterNamedService("fsp-ldr", std::make_shared<FSP_LDR>(system));
server_manager->RegisterNamedService("fsp:pr", std::make_shared<FSP_PR>(system));
- server_manager->RegisterNamedService("fsp-srv", std::make_shared<FSP_SRV>(system));
+ server_manager->RegisterNamedService("fsp-srv", std::move(FileSystemProxyFactory));
ServerManager::RunServer(std::move(server_manager));
}
diff --git a/src/core/hle/service/filesystem/filesystem.h b/src/core/hle/service/filesystem/filesystem.h
index 276d264e1..48f37d289 100644
--- a/src/core/hle/service/filesystem/filesystem.h
+++ b/src/core/hle/service/filesystem/filesystem.h
@@ -43,6 +43,9 @@ class ServiceManager;
namespace FileSystem {
+class RomFsController;
+class SaveDataController;
+
enum class ContentStorageId : u32 {
System,
User,
@@ -61,32 +64,24 @@ enum class OpenDirectoryMode : u64 {
};
DECLARE_ENUM_FLAG_OPERATORS(OpenDirectoryMode);
+using ProcessId = u64;
+using ProgramId = u64;
+
class FileSystemController {
public:
explicit FileSystemController(Core::System& system_);
~FileSystemController();
- Result RegisterRomFS(std::unique_ptr<FileSys::RomFSFactory>&& factory);
- Result RegisterSaveData(std::unique_ptr<FileSys::SaveDataFactory>&& factory);
- Result RegisterSDMC(std::unique_ptr<FileSys::SDMCFactory>&& factory);
- Result RegisterBIS(std::unique_ptr<FileSys::BISFactory>&& factory);
-
- void SetPackedUpdate(FileSys::VirtualFile update_raw);
- FileSys::VirtualFile OpenRomFSCurrentProcess() const;
- FileSys::VirtualFile OpenPatchedRomFS(u64 title_id, FileSys::ContentRecordType type) const;
- FileSys::VirtualFile OpenPatchedRomFSWithProgramIndex(u64 title_id, u8 program_index,
- FileSys::ContentRecordType type) const;
- FileSys::VirtualFile OpenRomFS(u64 title_id, FileSys::StorageId storage_id,
- FileSys::ContentRecordType type) const;
- std::shared_ptr<FileSys::NCA> OpenBaseNca(u64 title_id, FileSys::StorageId storage_id,
- FileSys::ContentRecordType type) const;
-
- Result CreateSaveData(FileSys::VirtualDir* out_save_data, FileSys::SaveDataSpaceId space,
- const FileSys::SaveDataAttribute& save_struct) const;
- Result OpenSaveData(FileSys::VirtualDir* out_save_data, FileSys::SaveDataSpaceId space,
- const FileSys::SaveDataAttribute& save_struct) const;
- Result OpenSaveDataSpace(FileSys::VirtualDir* out_save_data_space,
- FileSys::SaveDataSpaceId space) const;
+ Result RegisterProcess(ProcessId process_id, ProgramId program_id,
+ std::shared_ptr<FileSys::RomFSFactory>&& factory);
+ Result OpenProcess(ProgramId* out_program_id,
+ std::shared_ptr<SaveDataController>* out_save_data_controller,
+ std::shared_ptr<RomFsController>* out_romfs_controller,
+ ProcessId process_id);
+ void SetPackedUpdate(ProcessId process_id, FileSys::VirtualFile update_raw);
+
+ std::shared_ptr<SaveDataController> OpenSaveDataController();
+
Result OpenSDMC(FileSys::VirtualDir* out_sdmc) const;
Result OpenBISPartition(FileSys::VirtualDir* out_bis_partition,
FileSys::BisPartitionId id) const;
@@ -96,11 +91,6 @@ public:
u64 GetFreeSpaceSize(FileSys::StorageId id) const;
u64 GetTotalSpaceSize(FileSys::StorageId id) const;
- FileSys::SaveDataSize ReadSaveDataSize(FileSys::SaveDataType type, u64 title_id,
- u128 user_id) const;
- void WriteSaveDataSize(FileSys::SaveDataType type, u64 title_id, u128 user_id,
- FileSys::SaveDataSize new_value) const;
-
void SetGameCard(FileSys::VirtualFile file);
FileSys::XCI* GetGameCard() const;
@@ -133,15 +123,24 @@ public:
FileSys::VirtualDir GetBCATDirectory(u64 title_id) const;
- void SetAutoSaveDataCreation(bool enable);
-
// Creates the SaveData, SDMC, and BIS Factories. Should be called once and before any function
// above is called.
void CreateFactories(FileSys::VfsFilesystem& vfs, bool overwrite = true);
+ void Reset();
+
private:
- std::unique_ptr<FileSys::RomFSFactory> romfs_factory;
- std::unique_ptr<FileSys::SaveDataFactory> save_data_factory;
+ std::shared_ptr<FileSys::SaveDataFactory> CreateSaveDataFactory(ProgramId program_id);
+
+ struct Registration {
+ ProgramId program_id;
+ std::shared_ptr<FileSys::RomFSFactory> romfs_factory;
+ std::shared_ptr<FileSys::SaveDataFactory> save_data_factory;
+ };
+
+ std::mutex registration_lock;
+ std::map<ProcessId, Registration> registrations;
+
std::unique_ptr<FileSys::SDMCFactory> sdmc_factory;
std::unique_ptr<FileSys::BISFactory> bis_factory;
diff --git a/src/core/hle/service/filesystem/fsp_srv.cpp b/src/core/hle/service/filesystem/fsp_srv.cpp
index 82ecc1b90..a2397bec4 100644
--- a/src/core/hle/service/filesystem/fsp_srv.cpp
+++ b/src/core/hle/service/filesystem/fsp_srv.cpp
@@ -27,6 +27,8 @@
#include "core/hle/result.h"
#include "core/hle/service/filesystem/filesystem.h"
#include "core/hle/service/filesystem/fsp_srv.h"
+#include "core/hle/service/filesystem/romfs_controller.h"
+#include "core/hle/service/filesystem/save_data_controller.h"
#include "core/hle/service/hle_ipc.h"
#include "core/hle/service/ipc_helpers.h"
#include "core/reporter.h"
@@ -577,9 +579,11 @@ private:
class ISaveDataInfoReader final : public ServiceFramework<ISaveDataInfoReader> {
public:
- explicit ISaveDataInfoReader(Core::System& system_, FileSys::SaveDataSpaceId space,
- FileSystemController& fsc_)
- : ServiceFramework{system_, "ISaveDataInfoReader"}, fsc{fsc_} {
+ explicit ISaveDataInfoReader(Core::System& system_,
+ std::shared_ptr<SaveDataController> save_data_controller_,
+ FileSys::SaveDataSpaceId space)
+ : ServiceFramework{system_, "ISaveDataInfoReader"}, save_data_controller{
+ save_data_controller_} {
static const FunctionInfo functions[] = {
{0, &ISaveDataInfoReader::ReadSaveDataInfo, "ReadSaveDataInfo"},
};
@@ -626,7 +630,7 @@ private:
void FindAllSaves(FileSys::SaveDataSpaceId space) {
FileSys::VirtualDir save_root{};
- const auto result = fsc.OpenSaveDataSpace(&save_root, space);
+ const auto result = save_data_controller->OpenSaveDataSpace(&save_root, space);
if (result != ResultSuccess || save_root == nullptr) {
LOG_ERROR(Service_FS, "The save root for the space_id={:02X} was invalid!", space);
@@ -723,7 +727,8 @@ private:
};
static_assert(sizeof(SaveDataInfo) == 0x60, "SaveDataInfo has incorrect size.");
- FileSystemController& fsc;
+ ProcessId process_id = 0;
+ std::shared_ptr<SaveDataController> save_data_controller;
std::vector<SaveDataInfo> info;
u64 next_entry_index = 0;
};
@@ -863,21 +868,20 @@ FSP_SRV::FSP_SRV(Core::System& system_)
if (Settings::values.enable_fs_access_log) {
access_log_mode = AccessLogMode::SdCard;
}
-
- // This should be true on creation
- fsc.SetAutoSaveDataCreation(true);
}
FSP_SRV::~FSP_SRV() = default;
void FSP_SRV::SetCurrentProcess(HLERequestContext& ctx) {
- IPC::RequestParser rp{ctx};
- current_process_id = rp.Pop<u64>();
+ current_process_id = ctx.GetPID();
LOG_DEBUG(Service_FS, "called. current_process_id=0x{:016X}", current_process_id);
+ const auto res =
+ fsc.OpenProcess(&program_id, &save_data_controller, &romfs_controller, current_process_id);
+
IPC::ResponseBuilder rb{ctx, 2};
- rb.Push(ResultSuccess);
+ rb.Push(res);
}
void FSP_SRV::OpenFileSystemWithPatch(HLERequestContext& ctx) {
@@ -916,7 +920,8 @@ void FSP_SRV::CreateSaveDataFileSystem(HLERequestContext& ctx) {
uid[1], uid[0]);
FileSys::VirtualDir save_data_dir{};
- fsc.CreateSaveData(&save_data_dir, FileSys::SaveDataSpaceId::NandUser, save_struct);
+ save_data_controller->CreateSaveData(&save_data_dir, FileSys::SaveDataSpaceId::NandUser,
+ save_struct);
IPC::ResponseBuilder rb{ctx, 2};
rb.Push(ResultSuccess);
@@ -931,7 +936,8 @@ void FSP_SRV::CreateSaveDataFileSystemBySystemSaveDataId(HLERequestContext& ctx)
LOG_DEBUG(Service_FS, "called save_struct = {}", save_struct.DebugInfo());
FileSys::VirtualDir save_data_dir{};
- fsc.CreateSaveData(&save_data_dir, FileSys::SaveDataSpaceId::NandSystem, save_struct);
+ save_data_controller->CreateSaveData(&save_data_dir, FileSys::SaveDataSpaceId::NandSystem,
+ save_struct);
IPC::ResponseBuilder rb{ctx, 2};
rb.Push(ResultSuccess);
@@ -950,7 +956,8 @@ void FSP_SRV::OpenSaveDataFileSystem(HLERequestContext& ctx) {
LOG_INFO(Service_FS, "called.");
FileSys::VirtualDir dir{};
- auto result = fsc.OpenSaveData(&dir, parameters.space_id, parameters.attribute);
+ auto result =
+ save_data_controller->OpenSaveData(&dir, parameters.space_id, parameters.attribute);
if (result != ResultSuccess) {
IPC::ResponseBuilder rb{ctx, 2, 0, 0};
rb.Push(FileSys::ERROR_ENTITY_NOT_FOUND);
@@ -1001,7 +1008,7 @@ void FSP_SRV::OpenSaveDataInfoReaderBySaveDataSpaceId(HLERequestContext& ctx) {
IPC::ResponseBuilder rb{ctx, 2, 0, 1};
rb.Push(ResultSuccess);
rb.PushIpcInterface<ISaveDataInfoReader>(
- std::make_shared<ISaveDataInfoReader>(system, space, fsc));
+ std::make_shared<ISaveDataInfoReader>(system, save_data_controller, space));
}
void FSP_SRV::OpenSaveDataInfoReaderOnlyCacheStorage(HLERequestContext& ctx) {
@@ -1009,8 +1016,8 @@ void FSP_SRV::OpenSaveDataInfoReaderOnlyCacheStorage(HLERequestContext& ctx) {
IPC::ResponseBuilder rb{ctx, 2, 0, 1};
rb.Push(ResultSuccess);
- rb.PushIpcInterface<ISaveDataInfoReader>(system, FileSys::SaveDataSpaceId::TemporaryStorage,
- fsc);
+ rb.PushIpcInterface<ISaveDataInfoReader>(system, save_data_controller,
+ FileSys::SaveDataSpaceId::TemporaryStorage);
}
void FSP_SRV::WriteSaveDataFileSystemExtraDataBySaveDataAttribute(HLERequestContext& ctx) {
@@ -1050,7 +1057,7 @@ void FSP_SRV::OpenDataStorageByCurrentProcess(HLERequestContext& ctx) {
LOG_DEBUG(Service_FS, "called");
if (!romfs) {
- auto current_romfs = fsc.OpenRomFSCurrentProcess();
+ auto current_romfs = romfs_controller->OpenRomFSCurrentProcess();
if (!current_romfs) {
// TODO (bunnei): Find the right error code to use here
LOG_CRITICAL(Service_FS, "no file system interface available!");
@@ -1078,7 +1085,7 @@ void FSP_SRV::OpenDataStorageByDataId(HLERequestContext& ctx) {
LOG_DEBUG(Service_FS, "called with storage_id={:02X}, unknown={:08X}, title_id={:016X}",
storage_id, unknown, title_id);
- auto data = fsc.OpenRomFS(title_id, storage_id, FileSys::ContentRecordType::Data);
+ auto data = romfs_controller->OpenRomFS(title_id, storage_id, FileSys::ContentRecordType::Data);
if (!data) {
const auto archive = FileSys::SystemArchive::SynthesizeSystemArchive(title_id);
@@ -1101,7 +1108,8 @@ void FSP_SRV::OpenDataStorageByDataId(HLERequestContext& ctx) {
const FileSys::PatchManager pm{title_id, fsc, content_provider};
- auto base = fsc.OpenBaseNca(title_id, storage_id, FileSys::ContentRecordType::Data);
+ auto base =
+ romfs_controller->OpenBaseNca(title_id, storage_id, FileSys::ContentRecordType::Data);
auto storage = std::make_shared<IStorage>(
system, pm.PatchRomFS(base.get(), std::move(data), FileSys::ContentRecordType::Data));
@@ -1129,9 +1137,8 @@ void FSP_SRV::OpenDataStorageWithProgramIndex(HLERequestContext& ctx) {
LOG_DEBUG(Service_FS, "called, program_index={}", program_index);
- auto patched_romfs =
- fsc.OpenPatchedRomFSWithProgramIndex(system.GetApplicationProcessProgramID(), program_index,
- FileSys::ContentRecordType::Program);
+ auto patched_romfs = romfs_controller->OpenPatchedRomFSWithProgramIndex(
+ program_id, program_index, FileSys::ContentRecordType::Program);
if (!patched_romfs) {
// TODO: Find the right error code to use here
@@ -1152,7 +1159,7 @@ void FSP_SRV::OpenDataStorageWithProgramIndex(HLERequestContext& ctx) {
void FSP_SRV::DisableAutoSaveDataCreation(HLERequestContext& ctx) {
LOG_DEBUG(Service_FS, "called");
- fsc.SetAutoSaveDataCreation(false);
+ save_data_controller->SetAutoCreate(false);
IPC::ResponseBuilder rb{ctx, 2};
rb.Push(ResultSuccess);
diff --git a/src/core/hle/service/filesystem/fsp_srv.h b/src/core/hle/service/filesystem/fsp_srv.h
index 280bc9867..26980af99 100644
--- a/src/core/hle/service/filesystem/fsp_srv.h
+++ b/src/core/hle/service/filesystem/fsp_srv.h
@@ -17,6 +17,9 @@ class FileSystemBackend;
namespace Service::FileSystem {
+class RomFsController;
+class SaveDataController;
+
enum class AccessLogVersion : u32 {
V7_0_0 = 2,
@@ -67,6 +70,9 @@ private:
u64 current_process_id = 0;
u32 access_log_program_index = 0;
AccessLogMode access_log_mode = AccessLogMode::None;
+ u64 program_id = 0;
+ std::shared_ptr<SaveDataController> save_data_controller;
+ std::shared_ptr<RomFsController> romfs_controller;
};
} // namespace Service::FileSystem
diff --git a/src/core/hle/service/filesystem/romfs_controller.cpp b/src/core/hle/service/filesystem/romfs_controller.cpp
new file mode 100644
index 000000000..19c9cec72
--- /dev/null
+++ b/src/core/hle/service/filesystem/romfs_controller.cpp
@@ -0,0 +1,37 @@
+// SPDX-FileCopyrightText: Copyright 2024 yuzu Emulator Project
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#include "core/hle/service/filesystem/romfs_controller.h"
+
+namespace Service::FileSystem {
+
+RomFsController::RomFsController(std::shared_ptr<FileSys::RomFSFactory> factory_, u64 program_id_)
+ : factory{std::move(factory_)}, program_id{program_id_} {}
+RomFsController::~RomFsController() = default;
+
+FileSys::VirtualFile RomFsController::OpenRomFSCurrentProcess() {
+ return factory->OpenCurrentProcess(program_id);
+}
+
+FileSys::VirtualFile RomFsController::OpenPatchedRomFS(u64 title_id,
+ FileSys::ContentRecordType type) {
+ return factory->OpenPatchedRomFS(title_id, type);
+}
+
+FileSys::VirtualFile RomFsController::OpenPatchedRomFSWithProgramIndex(
+ u64 title_id, u8 program_index, FileSys::ContentRecordType type) {
+ return factory->OpenPatchedRomFSWithProgramIndex(title_id, program_index, type);
+}
+
+FileSys::VirtualFile RomFsController::OpenRomFS(u64 title_id, FileSys::StorageId storage_id,
+ FileSys::ContentRecordType type) {
+ return factory->Open(title_id, storage_id, type);
+}
+
+std::shared_ptr<FileSys::NCA> RomFsController::OpenBaseNca(u64 title_id,
+ FileSys::StorageId storage_id,
+ FileSys::ContentRecordType type) {
+ return factory->GetEntry(title_id, storage_id, type);
+}
+
+} // namespace Service::FileSystem
diff --git a/src/core/hle/service/filesystem/romfs_controller.h b/src/core/hle/service/filesystem/romfs_controller.h
new file mode 100644
index 000000000..9a478f71d
--- /dev/null
+++ b/src/core/hle/service/filesystem/romfs_controller.h
@@ -0,0 +1,31 @@
+// SPDX-FileCopyrightText: Copyright 2024 yuzu Emulator Project
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#pragma once
+
+#include "core/file_sys/nca_metadata.h"
+#include "core/file_sys/romfs_factory.h"
+#include "core/file_sys/vfs_types.h"
+
+namespace Service::FileSystem {
+
+class RomFsController {
+public:
+ explicit RomFsController(std::shared_ptr<FileSys::RomFSFactory> factory_, u64 program_id_);
+ ~RomFsController();
+
+ FileSys::VirtualFile OpenRomFSCurrentProcess();
+ FileSys::VirtualFile OpenPatchedRomFS(u64 title_id, FileSys::ContentRecordType type);
+ FileSys::VirtualFile OpenPatchedRomFSWithProgramIndex(u64 title_id, u8 program_index,
+ FileSys::ContentRecordType type);
+ FileSys::VirtualFile OpenRomFS(u64 title_id, FileSys::StorageId storage_id,
+ FileSys::ContentRecordType type);
+ std::shared_ptr<FileSys::NCA> OpenBaseNca(u64 title_id, FileSys::StorageId storage_id,
+ FileSys::ContentRecordType type);
+
+private:
+ const std::shared_ptr<FileSys::RomFSFactory> factory;
+ const u64 program_id;
+};
+
+} // namespace Service::FileSystem
diff --git a/src/core/hle/service/filesystem/save_data_controller.cpp b/src/core/hle/service/filesystem/save_data_controller.cpp
new file mode 100644
index 000000000..d19b3ea1e
--- /dev/null
+++ b/src/core/hle/service/filesystem/save_data_controller.cpp
@@ -0,0 +1,99 @@
+// SPDX-FileCopyrightText: Copyright 2024 yuzu Emulator Project
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#include "core/core.h"
+#include "core/file_sys/control_metadata.h"
+#include "core/file_sys/errors.h"
+#include "core/file_sys/patch_manager.h"
+#include "core/hle/service/filesystem/save_data_controller.h"
+#include "core/loader/loader.h"
+
+namespace Service::FileSystem {
+
+namespace {
+
+// A default size for normal/journal save data size if application control metadata cannot be found.
+// This should be large enough to satisfy even the most extreme requirements (~4.2GB)
+constexpr u64 SufficientSaveDataSize = 0xF0000000;
+
+FileSys::SaveDataSize GetDefaultSaveDataSize(Core::System& system, u64 program_id) {
+ const FileSys::PatchManager pm{program_id, system.GetFileSystemController(),
+ system.GetContentProvider()};
+ const auto metadata = pm.GetControlMetadata();
+ const auto& nacp = metadata.first;
+
+ if (nacp != nullptr) {
+ return {nacp->GetDefaultNormalSaveSize(), nacp->GetDefaultJournalSaveSize()};
+ }
+
+ return {SufficientSaveDataSize, SufficientSaveDataSize};
+}
+
+} // namespace
+
+SaveDataController::SaveDataController(Core::System& system_,
+ std::shared_ptr<FileSys::SaveDataFactory> factory_)
+ : system{system_}, factory{std::move(factory_)} {}
+SaveDataController::~SaveDataController() = default;
+
+Result SaveDataController::CreateSaveData(FileSys::VirtualDir* out_save_data,
+ FileSys::SaveDataSpaceId space,
+ const FileSys::SaveDataAttribute& attribute) {
+ LOG_TRACE(Service_FS, "Creating Save Data for space_id={:01X}, save_struct={}", space,
+ attribute.DebugInfo());
+
+ auto save_data = factory->Create(space, attribute);
+ if (save_data == nullptr) {
+ return FileSys::ERROR_ENTITY_NOT_FOUND;
+ }
+
+ *out_save_data = save_data;
+ return ResultSuccess;
+}
+
+Result SaveDataController::OpenSaveData(FileSys::VirtualDir* out_save_data,
+ FileSys::SaveDataSpaceId space,
+ const FileSys::SaveDataAttribute& attribute) {
+ auto save_data = factory->Open(space, attribute);
+ if (save_data == nullptr) {
+ return FileSys::ERROR_ENTITY_NOT_FOUND;
+ }
+
+ *out_save_data = save_data;
+ return ResultSuccess;
+}
+
+Result SaveDataController::OpenSaveDataSpace(FileSys::VirtualDir* out_save_data_space,
+ FileSys::SaveDataSpaceId space) {
+ auto save_data_space = factory->GetSaveDataSpaceDirectory(space);
+ if (save_data_space == nullptr) {
+ return FileSys::ERROR_ENTITY_NOT_FOUND;
+ }
+
+ *out_save_data_space = save_data_space;
+ return ResultSuccess;
+}
+
+FileSys::SaveDataSize SaveDataController::ReadSaveDataSize(FileSys::SaveDataType type, u64 title_id,
+ u128 user_id) {
+ const auto value = factory->ReadSaveDataSize(type, title_id, user_id);
+
+ if (value.normal == 0 && value.journal == 0) {
+ const auto size = GetDefaultSaveDataSize(system, title_id);
+ factory->WriteSaveDataSize(type, title_id, user_id, size);
+ return size;
+ }
+
+ return value;
+}
+
+void SaveDataController::WriteSaveDataSize(FileSys::SaveDataType type, u64 title_id, u128 user_id,
+ FileSys::SaveDataSize new_value) {
+ factory->WriteSaveDataSize(type, title_id, user_id, new_value);
+}
+
+void SaveDataController::SetAutoCreate(bool state) {
+ factory->SetAutoCreate(state);
+}
+
+} // namespace Service::FileSystem
diff --git a/src/core/hle/service/filesystem/save_data_controller.h b/src/core/hle/service/filesystem/save_data_controller.h
new file mode 100644
index 000000000..863188e4c
--- /dev/null
+++ b/src/core/hle/service/filesystem/save_data_controller.h
@@ -0,0 +1,35 @@
+// SPDX-FileCopyrightText: Copyright 2024 yuzu Emulator Project
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#pragma once
+
+#include "core/file_sys/nca_metadata.h"
+#include "core/file_sys/savedata_factory.h"
+#include "core/file_sys/vfs_types.h"
+
+namespace Service::FileSystem {
+
+class SaveDataController {
+public:
+ explicit SaveDataController(Core::System& system,
+ std::shared_ptr<FileSys::SaveDataFactory> factory_);
+ ~SaveDataController();
+
+ Result CreateSaveData(FileSys::VirtualDir* out_save_data, FileSys::SaveDataSpaceId space,
+ const FileSys::SaveDataAttribute& attribute);
+ Result OpenSaveData(FileSys::VirtualDir* out_save_data, FileSys::SaveDataSpaceId space,
+ const FileSys::SaveDataAttribute& attribute);
+ Result OpenSaveDataSpace(FileSys::VirtualDir* out_save_data_space,
+ FileSys::SaveDataSpaceId space);
+
+ FileSys::SaveDataSize ReadSaveDataSize(FileSys::SaveDataType type, u64 title_id, u128 user_id);
+ void WriteSaveDataSize(FileSys::SaveDataType type, u64 title_id, u128 user_id,
+ FileSys::SaveDataSize new_value);
+ void SetAutoCreate(bool state);
+
+private:
+ Core::System& system;
+ const std::shared_ptr<FileSys::SaveDataFactory> factory;
+};
+
+} // namespace Service::FileSystem
diff --git a/src/core/hle/service/glue/arp.cpp b/src/core/hle/service/glue/arp.cpp
index 6f1151b03..1254b6d49 100644
--- a/src/core/hle/service/glue/arp.cpp
+++ b/src/core/hle/service/glue/arp.cpp
@@ -15,9 +15,10 @@
namespace Service::Glue {
namespace {
-std::optional<u64> GetTitleIDForProcessID(const Core::System& system, u64 process_id) {
- const auto& list = system.Kernel().GetProcessList();
- const auto iter = std::find_if(list.begin(), list.end(), [&process_id](const auto& process) {
+std::optional<u64> GetTitleIDForProcessID(Core::System& system, u64 process_id) {
+ auto list = system.Kernel().GetProcessList();
+
+ const auto iter = std::find_if(list.begin(), list.end(), [&process_id](auto& process) {
return process->GetProcessId() == process_id;
});
diff --git a/src/core/hle/service/hid/hid.cpp b/src/core/hle/service/hid/hid.cpp
index fc03a0a5f..4ce0a9834 100644
--- a/src/core/hle/service/hid/hid.cpp
+++ b/src/core/hle/service/hid/hid.cpp
@@ -22,12 +22,10 @@ void LoopProcess(Core::System& system) {
std::shared_ptr<HidFirmwareSettings> firmware_settings =
std::make_shared<HidFirmwareSettings>();
- // TODO: Remove this hack until this service is emulated properly.
- const auto process_list = system.Kernel().GetProcessList();
- if (!process_list.empty()) {
- resource_manager->Initialize();
- resource_manager->RegisterAppletResourceUserId(process_list[0]->GetId(), true);
- }
+ // TODO: Remove this hack when am is emulated properly.
+ resource_manager->Initialize();
+ resource_manager->RegisterAppletResourceUserId(system.ApplicationProcess()->GetProcessId(),
+ true);
server_manager->RegisterNamedService(
"hid", std::make_shared<IHidServer>(system, resource_manager, firmware_settings));
diff --git a/src/core/hle/service/pm/pm.cpp b/src/core/hle/service/pm/pm.cpp
index d92499f05..b52468e41 100644
--- a/src/core/hle/service/pm/pm.cpp
+++ b/src/core/hle/service/pm/pm.cpp
@@ -22,27 +22,26 @@ constexpr Result ResultProcessNotFound{ErrorModule::PM, 1};
constexpr u64 NO_PROCESS_FOUND_PID{0};
-std::optional<Kernel::KProcess*> SearchProcessList(
- const std::vector<Kernel::KProcess*>& process_list,
- std::function<bool(Kernel::KProcess*)> predicate) {
+using ProcessList = std::list<Kernel::KScopedAutoObject<Kernel::KProcess>>;
+
+template <typename F>
+Kernel::KScopedAutoObject<Kernel::KProcess> SearchProcessList(ProcessList& process_list,
+ F&& predicate) {
const auto iter = std::find_if(process_list.begin(), process_list.end(), predicate);
if (iter == process_list.end()) {
- return std::nullopt;
+ return nullptr;
}
- return *iter;
+ return iter->GetPointerUnsafe();
}
-void GetApplicationPidGeneric(HLERequestContext& ctx,
- const std::vector<Kernel::KProcess*>& process_list) {
- const auto process = SearchProcessList(process_list, [](const auto& proc) {
- return proc->GetProcessId() == Kernel::KProcess::ProcessIdMin;
- });
+void GetApplicationPidGeneric(HLERequestContext& ctx, ProcessList& process_list) {
+ auto process = SearchProcessList(process_list, [](auto& p) { return p->IsApplication(); });
IPC::ResponseBuilder rb{ctx, 4};
rb.Push(ResultSuccess);
- rb.Push(process.has_value() ? (*process)->GetProcessId() : NO_PROCESS_FOUND_PID);
+ rb.Push(process.IsNull() ? NO_PROCESS_FOUND_PID : process->GetProcessId());
}
} // Anonymous namespace
@@ -80,8 +79,7 @@ private:
class DebugMonitor final : public ServiceFramework<DebugMonitor> {
public:
- explicit DebugMonitor(Core::System& system_)
- : ServiceFramework{system_, "pm:dmnt"}, kernel{system_.Kernel()} {
+ explicit DebugMonitor(Core::System& system_) : ServiceFramework{system_, "pm:dmnt"} {
// clang-format off
static const FunctionInfo functions[] = {
{0, nullptr, "GetJitDebugProcessIdList"},
@@ -106,12 +104,11 @@ private:
LOG_DEBUG(Service_PM, "called, program_id={:016X}", program_id);
- const auto process =
- SearchProcessList(kernel.GetProcessList(), [program_id](const auto& proc) {
- return proc->GetProgramId() == program_id;
- });
+ auto list = kernel.GetProcessList();
+ auto process = SearchProcessList(
+ list, [program_id](auto& p) { return p->GetProgramId() == program_id; });
- if (!process.has_value()) {
+ if (process.IsNull()) {
IPC::ResponseBuilder rb{ctx, 2};
rb.Push(ResultProcessNotFound);
return;
@@ -119,12 +116,13 @@ private:
IPC::ResponseBuilder rb{ctx, 4};
rb.Push(ResultSuccess);
- rb.Push((*process)->GetProcessId());
+ rb.Push(process->GetProcessId());
}
void GetApplicationProcessId(HLERequestContext& ctx) {
LOG_DEBUG(Service_PM, "called");
- GetApplicationPidGeneric(ctx, kernel.GetProcessList());
+ auto list = kernel.GetProcessList();
+ GetApplicationPidGeneric(ctx, list);
}
void AtmosphereGetProcessInfo(HLERequestContext& ctx) {
@@ -135,11 +133,10 @@ private:
LOG_WARNING(Service_PM, "(Partial Implementation) called, pid={:016X}", pid);
- const auto process = SearchProcessList(kernel.GetProcessList(), [pid](const auto& proc) {
- return proc->GetProcessId() == pid;
- });
+ auto list = kernel.GetProcessList();
+ auto process = SearchProcessList(list, [pid](auto& p) { return p->GetProcessId() == pid; });
- if (!process.has_value()) {
+ if (process.IsNull()) {
IPC::ResponseBuilder rb{ctx, 2};
rb.Push(ResultProcessNotFound);
return;
@@ -159,7 +156,7 @@ private:
OverrideStatus override_status{};
ProgramLocation program_location{
- .program_id = (*process)->GetProgramId(),
+ .program_id = process->GetProgramId(),
.storage_id = 0,
};
@@ -169,14 +166,11 @@ private:
rb.PushRaw(program_location);
rb.PushRaw(override_status);
}
-
- const Kernel::KernelCore& kernel;
};
class Info final : public ServiceFramework<Info> {
public:
- explicit Info(Core::System& system_, const std::vector<Kernel::KProcess*>& process_list_)
- : ServiceFramework{system_, "pm:info"}, process_list{process_list_} {
+ explicit Info(Core::System& system_) : ServiceFramework{system_, "pm:info"} {
static const FunctionInfo functions[] = {
{0, &Info::GetProgramId, "GetProgramId"},
{65000, &Info::AtmosphereGetProcessId, "AtmosphereGetProcessId"},
@@ -193,11 +187,11 @@ private:
LOG_DEBUG(Service_PM, "called, process_id={:016X}", process_id);
- const auto process = SearchProcessList(process_list, [process_id](const auto& proc) {
- return proc->GetProcessId() == process_id;
- });
+ auto list = kernel.GetProcessList();
+ auto process = SearchProcessList(
+ list, [process_id](auto& p) { return p->GetProcessId() == process_id; });
- if (!process.has_value()) {
+ if (process.IsNull()) {
IPC::ResponseBuilder rb{ctx, 2};
rb.Push(ResultProcessNotFound);
return;
@@ -205,7 +199,7 @@ private:
IPC::ResponseBuilder rb{ctx, 4};
rb.Push(ResultSuccess);
- rb.Push((*process)->GetProgramId());
+ rb.Push(process->GetProgramId());
}
void AtmosphereGetProcessId(HLERequestContext& ctx) {
@@ -214,11 +208,11 @@ private:
LOG_DEBUG(Service_PM, "called, program_id={:016X}", program_id);
- const auto process = SearchProcessList(process_list, [program_id](const auto& proc) {
- return proc->GetProgramId() == program_id;
- });
+ auto list = system.Kernel().GetProcessList();
+ auto process = SearchProcessList(
+ list, [program_id](auto& p) { return p->GetProgramId() == program_id; });
- if (!process.has_value()) {
+ if (process.IsNull()) {
IPC::ResponseBuilder rb{ctx, 2};
rb.Push(ResultProcessNotFound);
return;
@@ -226,16 +220,13 @@ private:
IPC::ResponseBuilder rb{ctx, 4};
rb.Push(ResultSuccess);
- rb.Push((*process)->GetProcessId());
+ rb.Push(process->GetProcessId());
}
-
- const std::vector<Kernel::KProcess*>& process_list;
};
class Shell final : public ServiceFramework<Shell> {
public:
- explicit Shell(Core::System& system_)
- : ServiceFramework{system_, "pm:shell"}, kernel{system_.Kernel()} {
+ explicit Shell(Core::System& system_) : ServiceFramework{system_, "pm:shell"} {
// clang-format off
static const FunctionInfo functions[] = {
{0, nullptr, "LaunchProgram"},
@@ -257,10 +248,9 @@ public:
private:
void GetApplicationProcessIdForShell(HLERequestContext& ctx) {
LOG_DEBUG(Service_PM, "called");
- GetApplicationPidGeneric(ctx, kernel.GetProcessList());
+ auto list = kernel.GetProcessList();
+ GetApplicationPidGeneric(ctx, list);
}
-
- const Kernel::KernelCore& kernel;
};
void LoopProcess(Core::System& system) {
@@ -268,8 +258,7 @@ void LoopProcess(Core::System& system) {
server_manager->RegisterNamedService("pm:bm", std::make_shared<BootMode>(system));
server_manager->RegisterNamedService("pm:dmnt", std::make_shared<DebugMonitor>(system));
- server_manager->RegisterNamedService(
- "pm:info", std::make_shared<Info>(system, system.Kernel().GetProcessList()));
+ server_manager->RegisterNamedService("pm:info", std::make_shared<Info>(system));
server_manager->RegisterNamedService("pm:shell", std::make_shared<Shell>(system));
ServerManager::RunServer(std::move(server_manager));
}
diff --git a/src/core/hle/service/server_manager.cpp b/src/core/hle/service/server_manager.cpp
index 15edb23e0..8ef49387d 100644
--- a/src/core/hle/service/server_manager.cpp
+++ b/src/core/hle/service/server_manager.cpp
@@ -256,8 +256,13 @@ Result ServerManager::WaitAndProcessImpl() {
// Wait for a signal.
s32 out_index{-1};
- R_TRY(Kernel::KSynchronizationObject::Wait(m_system.Kernel(), &out_index, wait_objs.data(),
- num_objs, -1));
+ R_TRY_CATCH(Kernel::KSynchronizationObject::Wait(m_system.Kernel(), &out_index,
+ wait_objs.data(), num_objs, -1)) {
+ R_CATCH(Kernel::ResultSessionClosed) {
+ // On session closed, index is updated and we don't want to return an error.
+ }
+ }
+ R_END_TRY_CATCH;
ASSERT(out_index >= 0 && out_index < num_objs);
// Set the output index.
diff --git a/src/core/loader/deconstructed_rom_directory.cpp b/src/core/loader/deconstructed_rom_directory.cpp
index c9f8707b7..9b75c660c 100644
--- a/src/core/loader/deconstructed_rom_directory.cpp
+++ b/src/core/loader/deconstructed_rom_directory.cpp
@@ -19,8 +19,54 @@
#include "core/arm/nce/patcher.h"
#endif
+#ifndef HAS_NCE
+namespace Core::NCE {
+class Patcher {};
+} // namespace Core::NCE
+#endif
+
namespace Loader {
+struct PatchCollection {
+ explicit PatchCollection(bool is_application_) : is_application{is_application_} {
+ module_patcher_indices.fill(-1);
+ patchers.emplace_back();
+ }
+
+ std::vector<Core::NCE::Patcher>* GetPatchers() {
+ if (is_application && Settings::IsNceEnabled()) {
+ return &patchers;
+ }
+ return nullptr;
+ }
+
+ size_t GetTotalPatchSize() const {
+ size_t total_size{};
+#ifdef HAS_NCE
+ for (auto& patcher : patchers) {
+ total_size += patcher.GetSectionSize();
+ }
+#endif
+ return total_size;
+ }
+
+ void SaveIndex(size_t module) {
+ module_patcher_indices[module] = static_cast<s32>(patchers.size() - 1);
+ }
+
+ s32 GetIndex(size_t module) const {
+ return module_patcher_indices[module];
+ }
+
+ s32 GetLastIndex() const {
+ return static_cast<s32>(patchers.size()) - 1;
+ }
+
+ bool is_application;
+ std::vector<Core::NCE::Patcher> patchers;
+ std::array<s32, 13> module_patcher_indices{};
+};
+
AppLoader_DeconstructedRomDirectory::AppLoader_DeconstructedRomDirectory(FileSys::VirtualFile file_,
bool override_update_)
: AppLoader(std::move(file_)), override_update(override_update_), is_hbl(false) {
@@ -142,18 +188,7 @@ AppLoader_DeconstructedRomDirectory::LoadResult AppLoader_DeconstructedRomDirect
std::size_t code_size{};
// Define an nce patch context for each potential module.
-#ifdef HAS_NCE
- std::array<Core::NCE::Patcher, 13> module_patchers;
-#endif
-
- const auto GetPatcher = [&](size_t i) -> Core::NCE::Patcher* {
-#ifdef HAS_NCE
- if (is_application && Settings::IsNceEnabled()) {
- return &module_patchers[i];
- }
-#endif
- return nullptr;
- };
+ PatchCollection patch_ctx{is_application};
// Use the NSO module loader to figure out the code layout
for (size_t i = 0; i < static_modules.size(); i++) {
@@ -164,13 +199,14 @@ AppLoader_DeconstructedRomDirectory::LoadResult AppLoader_DeconstructedRomDirect
}
const bool should_pass_arguments = std::strcmp(module, "rtld") == 0;
- const auto tentative_next_load_addr =
- AppLoader_NSO::LoadModule(process, system, *module_file, code_size,
- should_pass_arguments, false, {}, GetPatcher(i));
+ const auto tentative_next_load_addr = AppLoader_NSO::LoadModule(
+ process, system, *module_file, code_size, should_pass_arguments, false, {},
+ patch_ctx.GetPatchers(), patch_ctx.GetLastIndex());
if (!tentative_next_load_addr) {
return {ResultStatus::ErrorLoadingNSO, {}};
}
+ patch_ctx.SaveIndex(i);
code_size = *tentative_next_load_addr;
}
@@ -184,6 +220,9 @@ AppLoader_DeconstructedRomDirectory::LoadResult AppLoader_DeconstructedRomDirect
return 0;
}();
+ // Add patch size to the total module size
+ code_size += patch_ctx.GetTotalPatchSize();
+
// Setup the process code layout
if (process.LoadFromMetadata(metadata, code_size, fastmem_base, is_hbl).IsError()) {
return {ResultStatus::ErrorUnableToParseKernelMetadata, {}};
@@ -204,9 +243,9 @@ AppLoader_DeconstructedRomDirectory::LoadResult AppLoader_DeconstructedRomDirect
const VAddr load_addr{next_load_addr};
const bool should_pass_arguments = std::strcmp(module, "rtld") == 0;
- const auto tentative_next_load_addr =
- AppLoader_NSO::LoadModule(process, system, *module_file, load_addr,
- should_pass_arguments, true, pm, GetPatcher(i));
+ const auto tentative_next_load_addr = AppLoader_NSO::LoadModule(
+ process, system, *module_file, load_addr, should_pass_arguments, true, pm,
+ patch_ctx.GetPatchers(), patch_ctx.GetIndex(i));
if (!tentative_next_load_addr) {
return {ResultStatus::ErrorLoadingNSO, {}};
}
@@ -216,20 +255,6 @@ AppLoader_DeconstructedRomDirectory::LoadResult AppLoader_DeconstructedRomDirect
LOG_DEBUG(Loader, "loaded module {} @ {:#X}", module, load_addr);
}
- // Find the RomFS by searching for a ".romfs" file in this directory
- const auto& files = dir->GetFiles();
- const auto romfs_iter =
- std::find_if(files.begin(), files.end(), [](const FileSys::VirtualFile& f) {
- return f->GetName().find(".romfs") != std::string::npos;
- });
-
- // Register the RomFS if a ".romfs" file was found
- if (romfs_iter != files.end() && *romfs_iter != nullptr) {
- romfs = *romfs_iter;
- system.GetFileSystemController().RegisterRomFS(std::make_unique<FileSys::RomFSFactory>(
- *this, system.GetContentProvider(), system.GetFileSystemController()));
- }
-
is_loaded = true;
return {ResultStatus::Success,
LoadParameters{metadata.GetMainThreadPriority(), metadata.GetMainThreadStackSize()}};
diff --git a/src/core/loader/nca.cpp b/src/core/loader/nca.cpp
index 814407535..2a32b1276 100644
--- a/src/core/loader/nca.cpp
+++ b/src/core/loader/nca.cpp
@@ -74,8 +74,10 @@ AppLoader_NCA::LoadResult AppLoader_NCA::Load(Kernel::KProcess& process, Core::S
return load_result;
}
- system.GetFileSystemController().RegisterRomFS(std::make_unique<FileSys::RomFSFactory>(
- *this, system.GetContentProvider(), system.GetFileSystemController()));
+ system.GetFileSystemController().RegisterProcess(
+ process.GetProcessId(), nca->GetTitleId(),
+ std::make_shared<FileSys::RomFSFactory>(*this, system.GetContentProvider(),
+ system.GetFileSystemController()));
is_loaded = true;
return load_result;
diff --git a/src/core/loader/nro.cpp b/src/core/loader/nro.cpp
index e74697cda..f8225d697 100644
--- a/src/core/loader/nro.cpp
+++ b/src/core/loader/nro.cpp
@@ -275,10 +275,12 @@ AppLoader_NRO::LoadResult AppLoader_NRO::Load(Kernel::KProcess& process, Core::S
return {ResultStatus::ErrorLoadingNRO, {}};
}
- if (romfs != nullptr) {
- system.GetFileSystemController().RegisterRomFS(std::make_unique<FileSys::RomFSFactory>(
- *this, system.GetContentProvider(), system.GetFileSystemController()));
- }
+ u64 program_id{};
+ ReadProgramId(program_id);
+ system.GetFileSystemController().RegisterProcess(
+ process.GetProcessId(), program_id,
+ std::make_unique<FileSys::RomFSFactory>(*this, system.GetContentProvider(),
+ system.GetFileSystemController()));
is_loaded = true;
return {ResultStatus::Success, LoadParameters{Kernel::KThread::DefaultThreadPriority,
diff --git a/src/core/loader/nso.cpp b/src/core/loader/nso.cpp
index b053a0d14..583b7e927 100644
--- a/src/core/loader/nso.cpp
+++ b/src/core/loader/nso.cpp
@@ -77,7 +77,8 @@ std::optional<VAddr> AppLoader_NSO::LoadModule(Kernel::KProcess& process, Core::
const FileSys::VfsFile& nso_file, VAddr load_base,
bool should_pass_arguments, bool load_into_process,
std::optional<FileSys::PatchManager> pm,
- Core::NCE::Patcher* patch) {
+ std::vector<Core::NCE::Patcher>* patches,
+ s32 patch_index) {
if (nso_file.GetSize() < sizeof(NSOHeader)) {
return std::nullopt;
}
@@ -94,8 +95,11 @@ std::optional<VAddr> AppLoader_NSO::LoadModule(Kernel::KProcess& process, Core::
// Allocate some space at the beginning if we are patching in PreText mode.
const size_t module_start = [&]() -> size_t {
#ifdef HAS_NCE
- if (patch && patch->GetPatchMode() == Core::NCE::PatchMode::PreText) {
- return patch->GetSectionSize();
+ if (patches && load_into_process) {
+ auto* patch = &patches->operator[](patch_index);
+ if (patch->GetPatchMode() == Core::NCE::PatchMode::PreText) {
+ return patch->GetSectionSize();
+ }
}
#endif
return 0;
@@ -160,27 +164,24 @@ std::optional<VAddr> AppLoader_NSO::LoadModule(Kernel::KProcess& process, Core::
#ifdef HAS_NCE
// If we are computing the process code layout and using nce backend, patch.
const auto& code = codeset.CodeSegment();
- if (patch && patch->GetPatchMode() == Core::NCE::PatchMode::None) {
+ auto* patch = patches ? &patches->operator[](patch_index) : nullptr;
+ if (patch && !load_into_process) {
// Patch SVCs and MRS calls in the guest code
- patch->PatchText(program_image, code);
-
- // Add patch section size to the module size.
- image_size += static_cast<u32>(patch->GetSectionSize());
+ while (!patch->PatchText(program_image, code)) {
+ patch = &patches->emplace_back();
+ }
} else if (patch) {
// Relocate code patch and copy to the program_image.
- patch->RelocateAndCopy(load_base, code, program_image, &process.GetPostHandlers());
-
- // Update patch section.
- auto& patch_segment = codeset.PatchSegment();
- patch_segment.addr =
- patch->GetPatchMode() == Core::NCE::PatchMode::PreText ? 0 : image_size;
- patch_segment.size = static_cast<u32>(patch->GetSectionSize());
-
- // Add patch section size to the module size. In PreText mode image_size
- // already contains the patch segment as part of module_start.
- if (patch->GetPatchMode() == Core::NCE::PatchMode::PostData) {
- image_size += patch_segment.size;
+ if (patch->RelocateAndCopy(load_base, code, program_image, &process.GetPostHandlers())) {
+ // Update patch section.
+ auto& patch_segment = codeset.PatchSegment();
+ patch_segment.addr =
+ patch->GetPatchMode() == Core::NCE::PatchMode::PreText ? 0 : image_size;
+ patch_segment.size = static_cast<u32>(patch->GetSectionSize());
}
+
+ // Refresh image_size to take account the patch section if it was added by RelocateAndCopy
+ image_size = static_cast<u32>(program_image.size());
}
#endif
diff --git a/src/core/loader/nso.h b/src/core/loader/nso.h
index 29b86ed4c..6356697e3 100644
--- a/src/core/loader/nso.h
+++ b/src/core/loader/nso.h
@@ -93,7 +93,8 @@ public:
const FileSys::VfsFile& nso_file, VAddr load_base,
bool should_pass_arguments, bool load_into_process,
std::optional<FileSys::PatchManager> pm = {},
- Core::NCE::Patcher* patch = nullptr);
+ std::vector<Core::NCE::Patcher>* patches = nullptr,
+ s32 patch_index = -1);
LoadResult Load(Kernel::KProcess& process, Core::System& system) override;
diff --git a/src/core/loader/nsp.cpp b/src/core/loader/nsp.cpp
index f4ab75b77..28116ff3a 100644
--- a/src/core/loader/nsp.cpp
+++ b/src/core/loader/nsp.cpp
@@ -111,7 +111,8 @@ AppLoader_NSP::LoadResult AppLoader_NSP::Load(Kernel::KProcess& process, Core::S
FileSys::VirtualFile update_raw;
if (ReadUpdateRaw(update_raw) == ResultStatus::Success && update_raw != nullptr) {
- system.GetFileSystemController().SetPackedUpdate(std::move(update_raw));
+ system.GetFileSystemController().SetPackedUpdate(process.GetProcessId(),
+ std::move(update_raw));
}
is_loaded = true;
diff --git a/src/core/loader/xci.cpp b/src/core/loader/xci.cpp
index 12d72c380..e9abb199a 100644
--- a/src/core/loader/xci.cpp
+++ b/src/core/loader/xci.cpp
@@ -78,7 +78,8 @@ AppLoader_XCI::LoadResult AppLoader_XCI::Load(Kernel::KProcess& process, Core::S
FileSys::VirtualFile update_raw;
if (ReadUpdateRaw(update_raw) == ResultStatus::Success && update_raw != nullptr) {
- system.GetFileSystemController().SetPackedUpdate(std::move(update_raw));
+ system.GetFileSystemController().SetPackedUpdate(process.GetProcessId(),
+ std::move(update_raw));
}
is_loaded = true;
diff --git a/src/shader_recompiler/backend/spirv/emit_spirv_memory.cpp b/src/shader_recompiler/backend/spirv/emit_spirv_memory.cpp
index 8693801c7..bdcbccfde 100644
--- a/src/shader_recompiler/backend/spirv/emit_spirv_memory.cpp
+++ b/src/shader_recompiler/backend/spirv/emit_spirv_memory.cpp
@@ -65,6 +65,14 @@ void WriteStorage32(EmitContext& ctx, const IR::Value& binding, const IR::Value&
WriteStorage(ctx, binding, offset, value, ctx.storage_types.U32, sizeof(u32),
&StorageDefinitions::U32, index_offset);
}
+
+void WriteStorageByCasLoop(EmitContext& ctx, const IR::Value& binding, const IR::Value& offset,
+ Id value, Id bit_offset, Id bit_count) {
+ const Id pointer{StoragePointer(ctx, binding, offset, ctx.storage_types.U32, sizeof(u32),
+ &StorageDefinitions::U32)};
+ ctx.OpFunctionCall(ctx.TypeVoid(), ctx.write_storage_cas_loop_func, pointer, value, bit_offset,
+ bit_count);
+}
} // Anonymous namespace
void EmitLoadGlobalU8(EmitContext&) {
@@ -219,26 +227,42 @@ Id EmitLoadStorage128(EmitContext& ctx, const IR::Value& binding, const IR::Valu
void EmitWriteStorageU8(EmitContext& ctx, const IR::Value& binding, const IR::Value& offset,
Id value) {
- WriteStorage(ctx, binding, offset, ctx.OpSConvert(ctx.U8, value), ctx.storage_types.U8,
- sizeof(u8), &StorageDefinitions::U8);
+ if (ctx.profile.support_int8) {
+ WriteStorage(ctx, binding, offset, ctx.OpSConvert(ctx.U8, value), ctx.storage_types.U8,
+ sizeof(u8), &StorageDefinitions::U8);
+ } else {
+ WriteStorageByCasLoop(ctx, binding, offset, value, ctx.BitOffset8(offset), ctx.Const(8u));
+ }
}
void EmitWriteStorageS8(EmitContext& ctx, const IR::Value& binding, const IR::Value& offset,
Id value) {
- WriteStorage(ctx, binding, offset, ctx.OpSConvert(ctx.S8, value), ctx.storage_types.S8,
- sizeof(s8), &StorageDefinitions::S8);
+ if (ctx.profile.support_int8) {
+ WriteStorage(ctx, binding, offset, ctx.OpSConvert(ctx.S8, value), ctx.storage_types.S8,
+ sizeof(s8), &StorageDefinitions::S8);
+ } else {
+ WriteStorageByCasLoop(ctx, binding, offset, value, ctx.BitOffset8(offset), ctx.Const(8u));
+ }
}
void EmitWriteStorageU16(EmitContext& ctx, const IR::Value& binding, const IR::Value& offset,
Id value) {
- WriteStorage(ctx, binding, offset, ctx.OpSConvert(ctx.U16, value), ctx.storage_types.U16,
- sizeof(u16), &StorageDefinitions::U16);
+ if (ctx.profile.support_int16) {
+ WriteStorage(ctx, binding, offset, ctx.OpSConvert(ctx.U16, value), ctx.storage_types.U16,
+ sizeof(u16), &StorageDefinitions::U16);
+ } else {
+ WriteStorageByCasLoop(ctx, binding, offset, value, ctx.BitOffset16(offset), ctx.Const(16u));
+ }
}
void EmitWriteStorageS16(EmitContext& ctx, const IR::Value& binding, const IR::Value& offset,
Id value) {
- WriteStorage(ctx, binding, offset, ctx.OpSConvert(ctx.S16, value), ctx.storage_types.S16,
- sizeof(s16), &StorageDefinitions::S16);
+ if (ctx.profile.support_int16) {
+ WriteStorage(ctx, binding, offset, ctx.OpSConvert(ctx.S16, value), ctx.storage_types.S16,
+ sizeof(s16), &StorageDefinitions::S16);
+ } else {
+ WriteStorageByCasLoop(ctx, binding, offset, value, ctx.BitOffset16(offset), ctx.Const(16u));
+ }
}
void EmitWriteStorage32(EmitContext& ctx, const IR::Value& binding, const IR::Value& offset,
diff --git a/src/shader_recompiler/backend/spirv/spirv_emit_context.cpp b/src/shader_recompiler/backend/spirv/spirv_emit_context.cpp
index 0442adc83..a27f2f73a 100644
--- a/src/shader_recompiler/backend/spirv/spirv_emit_context.cpp
+++ b/src/shader_recompiler/backend/spirv/spirv_emit_context.cpp
@@ -480,6 +480,7 @@ EmitContext::EmitContext(const Profile& profile_, const RuntimeInfo& runtime_inf
DefineTextures(program.info, texture_binding, bindings.texture_scaling_index);
DefineImages(program.info, image_binding, bindings.image_scaling_index);
DefineAttributeMemAccess(program.info);
+ DefineWriteStorageCasLoopFunction(program.info);
DefineGlobalMemoryFunctions(program.info);
DefineRescalingInput(program.info);
DefineRenderArea(program.info);
@@ -877,6 +878,56 @@ void EmitContext::DefineAttributeMemAccess(const Info& info) {
}
}
+void EmitContext::DefineWriteStorageCasLoopFunction(const Info& info) {
+ if (profile.support_int8 && profile.support_int16) {
+ return;
+ }
+ if (!info.uses_int8 && !info.uses_int16) {
+ return;
+ }
+
+ AddCapability(spv::Capability::VariablePointersStorageBuffer);
+
+ const Id ptr_type{TypePointer(spv::StorageClass::StorageBuffer, U32[1])};
+ const Id func_type{TypeFunction(void_id, ptr_type, U32[1], U32[1], U32[1])};
+ const Id func{OpFunction(void_id, spv::FunctionControlMask::MaskNone, func_type)};
+ const Id pointer{OpFunctionParameter(ptr_type)};
+ const Id value{OpFunctionParameter(U32[1])};
+ const Id bit_offset{OpFunctionParameter(U32[1])};
+ const Id bit_count{OpFunctionParameter(U32[1])};
+
+ AddLabel();
+ const Id scope_device{Const(1u)};
+ const Id ordering_relaxed{u32_zero_value};
+ const Id body_label{OpLabel()};
+ const Id continue_label{OpLabel()};
+ const Id endloop_label{OpLabel()};
+ const Id beginloop_label{OpLabel()};
+ OpBranch(beginloop_label);
+
+ AddLabel(beginloop_label);
+ OpLoopMerge(endloop_label, continue_label, spv::LoopControlMask::MaskNone);
+ OpBranch(body_label);
+
+ AddLabel(body_label);
+ const Id expected_value{OpLoad(U32[1], pointer)};
+ const Id desired_value{OpBitFieldInsert(U32[1], expected_value, value, bit_offset, bit_count)};
+ const Id actual_value{OpAtomicCompareExchange(U32[1], pointer, scope_device, ordering_relaxed,
+ ordering_relaxed, desired_value, expected_value)};
+ const Id store_successful{OpIEqual(U1, expected_value, actual_value)};
+ OpBranchConditional(store_successful, endloop_label, continue_label);
+
+ AddLabel(endloop_label);
+ OpReturn();
+
+ AddLabel(continue_label);
+ OpBranch(beginloop_label);
+
+ OpFunctionEnd();
+
+ write_storage_cas_loop_func = func;
+}
+
void EmitContext::DefineGlobalMemoryFunctions(const Info& info) {
if (!info.uses_global_memory || !profile.support_int64) {
return;
diff --git a/src/shader_recompiler/backend/spirv/spirv_emit_context.h b/src/shader_recompiler/backend/spirv/spirv_emit_context.h
index 56019ad89..40adcb6b6 100644
--- a/src/shader_recompiler/backend/spirv/spirv_emit_context.h
+++ b/src/shader_recompiler/backend/spirv/spirv_emit_context.h
@@ -325,6 +325,8 @@ public:
Id f32x2_min_cas{};
Id f32x2_max_cas{};
+ Id write_storage_cas_loop_func{};
+
Id load_global_func_u32{};
Id load_global_func_u32x2{};
Id load_global_func_u32x4{};
@@ -372,6 +374,7 @@ private:
void DefineTextures(const Info& info, u32& binding, u32& scaling_index);
void DefineImages(const Info& info, u32& binding, u32& scaling_index);
void DefineAttributeMemAccess(const Info& info);
+ void DefineWriteStorageCasLoopFunction(const Info& info);
void DefineGlobalMemoryFunctions(const Info& info);
void DefineRescalingInput(const Info& info);
void DefineRescalingInputPushConstant();
diff --git a/src/yuzu/configuration/configure_profile_manager.cpp b/src/yuzu/configuration/configure_profile_manager.cpp
index fa5f383d6..12a04b9a0 100644
--- a/src/yuzu/configuration/configure_profile_manager.cpp
+++ b/src/yuzu/configuration/configure_profile_manager.cpp
@@ -205,6 +205,7 @@ void ConfigureProfileManager::AddUser() {
const auto uuid = Common::UUID::MakeRandom();
profile_manager.CreateNewUser(uuid, username.toStdString());
+ profile_manager.WriteUserSaveFile();
item_model->appendRow(new QStandardItem{GetIcon(uuid), FormatUserEntryText(username, uuid)});
}
@@ -228,6 +229,7 @@ void ConfigureProfileManager::RenameUser() {
std::copy(username_std.begin(), username_std.end(), profile.username.begin());
profile_manager.SetProfileBase(*uuid, profile);
+ profile_manager.WriteUserSaveFile();
item_model->setItem(
user, 0,
@@ -256,6 +258,8 @@ void ConfigureProfileManager::DeleteUser(const Common::UUID& uuid) {
return;
}
+ profile_manager.WriteUserSaveFile();
+
item_model->removeRows(tree_view->currentIndex().row(), 1);
tree_view->clearSelection();
diff --git a/src/yuzu/main.cpp b/src/yuzu/main.cpp
index fd5342537..33756febf 100644
--- a/src/yuzu/main.cpp
+++ b/src/yuzu/main.cpp
@@ -2292,14 +2292,14 @@ void GMainWindow::OnGameListOpenFolder(u64 program_id, GameListOpenTarget target
ASSERT(user_id);
const auto user_save_data_path = FileSys::SaveDataFactory::GetFullPath(
- *system, vfs_nand_dir, FileSys::SaveDataSpaceId::NandUser,
+ {}, vfs_nand_dir, FileSys::SaveDataSpaceId::NandUser,
FileSys::SaveDataType::SaveData, program_id, user_id->AsU128(), 0);
path = Common::FS::ConcatPathSafe(nand_dir, user_save_data_path);
} else {
// Device save data
const auto device_save_data_path = FileSys::SaveDataFactory::GetFullPath(
- *system, vfs_nand_dir, FileSys::SaveDataSpaceId::NandUser,
+ {}, vfs_nand_dir, FileSys::SaveDataSpaceId::NandUser,
FileSys::SaveDataType::SaveData, program_id, {}, 0);
path = Common::FS::ConcatPathSafe(nand_dir, device_save_data_path);
@@ -2662,8 +2662,8 @@ void GMainWindow::RemoveCacheStorage(u64 program_id) {
vfs->OpenDirectory(Common::FS::PathToUTF8String(nand_dir), FileSys::Mode::Read);
const auto cache_storage_path = FileSys::SaveDataFactory::GetFullPath(
- *system, vfs_nand_dir, FileSys::SaveDataSpaceId::NandUser,
- FileSys::SaveDataType::CacheStorage, 0 /* program_id */, {}, 0);
+ {}, vfs_nand_dir, FileSys::SaveDataSpaceId::NandUser, FileSys::SaveDataType::CacheStorage,
+ 0 /* program_id */, {}, 0);
const auto path = Common::FS::ConcatPathSafe(nand_dir, cache_storage_path);