summaryrefslogtreecommitdiffstats
path: root/src/core
diff options
context:
space:
mode:
Diffstat (limited to 'src/core')
-rw-r--r--src/core/CMakeLists.txt1
-rw-r--r--src/core/arm/arm_interface.cpp2
-rw-r--r--src/core/core.cpp32
-rw-r--r--src/core/core.h11
-rw-r--r--src/core/core_timing.cpp12
-rw-r--r--src/core/core_timing.h10
-rw-r--r--src/core/file_sys/content_archive.cpp39
-rw-r--r--src/core/file_sys/fsmitm_romfsbuild.cpp38
-rw-r--r--src/core/file_sys/vfs_real.cpp3
-rw-r--r--src/core/gpu_dirty_memory_manager.h122
-rw-r--r--src/core/hid/emulated_controller.cpp12
-rw-r--r--src/core/hid/emulated_controller.h8
-rw-r--r--src/core/hle/kernel/k_server_session.cpp165
-rw-r--r--src/core/hle/kernel/k_thread.h10
-rw-r--r--src/core/hle/kernel/message_buffer.h612
-rw-r--r--src/core/hle/kernel/svc/svc_ipc.cpp37
-rw-r--r--src/core/hle/kernel/svc/svc_synchronization.cpp41
-rw-r--r--src/core/hle/service/audio/audin_u.cpp16
-rw-r--r--src/core/hle/service/audio/audout_u.cpp20
-rw-r--r--src/core/hle/service/audio/audren_u.cpp23
-rw-r--r--src/core/hle/service/audio/hwopus.cpp9
-rw-r--r--src/core/hle/service/hle_ipc.cpp32
-rw-r--r--src/core/hle/service/nfc/common/device.cpp32
-rw-r--r--src/core/hle/service/nvdrv/nvdrv_interface.cpp25
-rw-r--r--src/core/hle/service/nvdrv/nvdrv_interface.h5
-rw-r--r--src/core/hle/service/nvnflinger/parcel.h24
-rw-r--r--src/core/loader/loader.h2
-rw-r--r--src/core/memory.cpp94
-rw-r--r--src/core/memory.h218
29 files changed, 1491 insertions, 164 deletions
diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt
index 8a66aa8ea..c3b688c5d 100644
--- a/src/core/CMakeLists.txt
+++ b/src/core/CMakeLists.txt
@@ -285,6 +285,7 @@ add_library(core STATIC
hle/kernel/kernel.cpp
hle/kernel/kernel.h
hle/kernel/memory_types.h
+ hle/kernel/message_buffer.h
hle/kernel/physical_core.cpp
hle/kernel/physical_core.h
hle/kernel/physical_memory.h
diff --git a/src/core/arm/arm_interface.cpp b/src/core/arm/arm_interface.cpp
index beaea64b3..aa0eb9791 100644
--- a/src/core/arm/arm_interface.cpp
+++ b/src/core/arm/arm_interface.cpp
@@ -185,7 +185,7 @@ void ARM_Interface::Run() {
// Notify the debugger and go to sleep if a breakpoint was hit,
// or if the thread is unable to continue for any reason.
if (True(hr & HaltReason::InstructionBreakpoint) || True(hr & HaltReason::PrefetchAbort)) {
- if (!True(hr & HaltReason::InstructionBreakpoint)) {
+ if (!True(hr & HaltReason::PrefetchAbort)) {
RewindBreakpointInstruction();
}
if (system.DebuggerEnabled()) {
diff --git a/src/core/core.cpp b/src/core/core.cpp
index b74fd0a58..9e3eb3795 100644
--- a/src/core/core.cpp
+++ b/src/core/core.cpp
@@ -27,6 +27,7 @@
#include "core/file_sys/savedata_factory.h"
#include "core/file_sys/vfs_concat.h"
#include "core/file_sys/vfs_real.h"
+#include "core/gpu_dirty_memory_manager.h"
#include "core/hid/hid_core.h"
#include "core/hle/kernel/k_memory_manager.h"
#include "core/hle/kernel/k_process.h"
@@ -130,7 +131,10 @@ FileSys::VirtualFile GetGameFileFromPath(const FileSys::VirtualFilesystem& vfs,
struct System::Impl {
explicit Impl(System& system)
: kernel{system}, fs_controller{system}, memory{system}, hid_core{}, room_network{},
- cpu_manager{system}, reporter{system}, applet_manager{system}, time_manager{system} {}
+ cpu_manager{system}, reporter{system}, applet_manager{system}, time_manager{system},
+ gpu_dirty_memory_write_manager{} {
+ memory.SetGPUDirtyManagers(gpu_dirty_memory_write_manager);
+ }
void Initialize(System& system) {
device_memory = std::make_unique<Core::DeviceMemory>();
@@ -234,6 +238,8 @@ struct System::Impl {
// Setting changes may require a full system reinitialization (e.g., disabling multicore).
ReinitializeIfNecessary(system);
+ memory.SetGPUDirtyManagers(gpu_dirty_memory_write_manager);
+
kernel.Initialize();
cpu_manager.Initialize();
@@ -540,6 +546,9 @@ struct System::Impl {
std::array<u64, Core::Hardware::NUM_CPU_CORES> dynarmic_ticks{};
std::array<MicroProfileToken, Core::Hardware::NUM_CPU_CORES> microprofile_cpu{};
+
+ std::array<Core::GPUDirtyMemoryManager, Core::Hardware::NUM_CPU_CORES>
+ gpu_dirty_memory_write_manager{};
};
System::System() : impl{std::make_unique<Impl>(*this)} {}
@@ -629,10 +638,31 @@ void System::PrepareReschedule(const u32 core_index) {
impl->kernel.PrepareReschedule(core_index);
}
+Core::GPUDirtyMemoryManager& System::CurrentGPUDirtyMemoryManager() {
+ const std::size_t core = impl->kernel.GetCurrentHostThreadID();
+ return impl->gpu_dirty_memory_write_manager[core < Core::Hardware::NUM_CPU_CORES
+ ? core
+ : Core::Hardware::NUM_CPU_CORES - 1];
+}
+
+/// Provides a constant reference to the current gou dirty memory manager.
+const Core::GPUDirtyMemoryManager& System::CurrentGPUDirtyMemoryManager() const {
+ const std::size_t core = impl->kernel.GetCurrentHostThreadID();
+ return impl->gpu_dirty_memory_write_manager[core < Core::Hardware::NUM_CPU_CORES
+ ? core
+ : Core::Hardware::NUM_CPU_CORES - 1];
+}
+
size_t System::GetCurrentHostThreadID() const {
return impl->kernel.GetCurrentHostThreadID();
}
+void System::GatherGPUDirtyMemory(std::function<void(VAddr, size_t)>& callback) {
+ for (auto& manager : impl->gpu_dirty_memory_write_manager) {
+ manager.Gather(callback);
+ }
+}
+
PerfStatsResults System::GetAndResetPerfStats() {
return impl->GetAndResetPerfStats();
}
diff --git a/src/core/core.h b/src/core/core.h
index 93afc9303..14b2f7785 100644
--- a/src/core/core.h
+++ b/src/core/core.h
@@ -108,9 +108,10 @@ class CpuManager;
class Debugger;
class DeviceMemory;
class ExclusiveMonitor;
-class SpeedLimiter;
+class GPUDirtyMemoryManager;
class PerfStats;
class Reporter;
+class SpeedLimiter;
class TelemetrySession;
struct PerfStatsResults;
@@ -225,6 +226,14 @@ public:
/// Prepare the core emulation for a reschedule
void PrepareReschedule(u32 core_index);
+ /// Provides a reference to the gou dirty memory manager.
+ [[nodiscard]] Core::GPUDirtyMemoryManager& CurrentGPUDirtyMemoryManager();
+
+ /// Provides a constant reference to the current gou dirty memory manager.
+ [[nodiscard]] const Core::GPUDirtyMemoryManager& CurrentGPUDirtyMemoryManager() const;
+
+ void GatherGPUDirtyMemory(std::function<void(VAddr, size_t)>& callback);
+
[[nodiscard]] size_t GetCurrentHostThreadID() const;
/// Gets and resets core performance statistics
diff --git a/src/core/core_timing.cpp b/src/core/core_timing.cpp
index 4f0a3f8ea..b98a0cb33 100644
--- a/src/core/core_timing.cpp
+++ b/src/core/core_timing.cpp
@@ -70,7 +70,7 @@ void CoreTiming::Initialize(std::function<void()>&& on_thread_init_) {
-> std::optional<std::chrono::nanoseconds> { return std::nullopt; };
ev_lost = CreateEvent("_lost_event", empty_timed_callback);
if (is_multicore) {
- timer_thread = std::make_unique<std::thread>(ThreadEntry, std::ref(*this));
+ timer_thread = std::make_unique<std::jthread>(ThreadEntry, std::ref(*this));
}
}
@@ -253,12 +253,8 @@ void CoreTiming::ThreadLoop() {
auto wait_time = *next_time - GetGlobalTimeNs().count();
if (wait_time > 0) {
#ifdef _WIN32
- const auto timer_resolution_ns =
- Common::Windows::GetCurrentTimerResolution().count();
-
while (!paused && !event.IsSet() && wait_time > 0) {
wait_time = *next_time - GetGlobalTimeNs().count();
-
if (wait_time >= timer_resolution_ns) {
Common::Windows::SleepForOneTick();
} else {
@@ -316,4 +312,10 @@ std::chrono::microseconds CoreTiming::GetGlobalTimeUs() const {
return std::chrono::microseconds{Common::WallClock::CPUTickToUS(cpu_ticks)};
}
+#ifdef _WIN32
+void CoreTiming::SetTimerResolutionNs(std::chrono::nanoseconds ns) {
+ timer_resolution_ns = ns.count();
+}
+#endif
+
} // namespace Core::Timing
diff --git a/src/core/core_timing.h b/src/core/core_timing.h
index 10db1de55..c20e906fb 100644
--- a/src/core/core_timing.h
+++ b/src/core/core_timing.h
@@ -131,6 +131,10 @@ public:
/// Checks for events manually and returns time in nanoseconds for next event, threadsafe.
std::optional<s64> Advance();
+#ifdef _WIN32
+ void SetTimerResolutionNs(std::chrono::nanoseconds ns);
+#endif
+
private:
struct Event;
@@ -143,6 +147,10 @@ private:
s64 global_timer = 0;
+#ifdef _WIN32
+ s64 timer_resolution_ns;
+#endif
+
// The queue is a min-heap using std::make_heap/push_heap/pop_heap.
// We don't use std::priority_queue because we need to be able to serialize, unserialize and
// erase arbitrary events (RemoveEvent()) regardless of the queue order. These aren't
@@ -155,7 +163,7 @@ private:
Common::Event pause_event{};
std::mutex basic_lock;
std::mutex advance_lock;
- std::unique_ptr<std::thread> timer_thread;
+ std::unique_ptr<std::jthread> timer_thread;
std::atomic<bool> paused{};
std::atomic<bool> paused_set{};
std::atomic<bool> wait_set{};
diff --git a/src/core/file_sys/content_archive.cpp b/src/core/file_sys/content_archive.cpp
index 50303fe42..06efab46d 100644
--- a/src/core/file_sys/content_archive.cpp
+++ b/src/core/file_sys/content_archive.cpp
@@ -57,11 +57,34 @@ struct NCASectionHeaderBlock {
};
static_assert(sizeof(NCASectionHeaderBlock) == 0x8, "NCASectionHeaderBlock has incorrect size.");
+struct NCABucketInfo {
+ u64 table_offset;
+ u64 table_size;
+ std::array<u8, 0x10> table_header;
+};
+static_assert(sizeof(NCABucketInfo) == 0x20, "NCABucketInfo has incorrect size.");
+
+struct NCASparseInfo {
+ NCABucketInfo bucket;
+ u64 physical_offset;
+ u16 generation;
+ INSERT_PADDING_BYTES_NOINIT(0x6);
+};
+static_assert(sizeof(NCASparseInfo) == 0x30, "NCASparseInfo has incorrect size.");
+
+struct NCACompressionInfo {
+ NCABucketInfo bucket;
+ INSERT_PADDING_BYTES_NOINIT(0x8);
+};
+static_assert(sizeof(NCACompressionInfo) == 0x28, "NCACompressionInfo has incorrect size.");
+
struct NCASectionRaw {
NCASectionHeaderBlock header;
std::array<u8, 0x138> block_data;
std::array<u8, 0x8> section_ctr;
- INSERT_PADDING_BYTES_NOINIT(0xB8);
+ NCASparseInfo sparse_info;
+ NCACompressionInfo compression_info;
+ INSERT_PADDING_BYTES_NOINIT(0x60);
};
static_assert(sizeof(NCASectionRaw) == 0x200, "NCASectionRaw has incorrect size.");
@@ -225,6 +248,20 @@ bool NCA::ReadSections(const std::vector<NCASectionHeader>& sections, u64 bktr_b
for (std::size_t i = 0; i < sections.size(); ++i) {
const auto& section = sections[i];
+ if (section.raw.sparse_info.bucket.table_offset != 0 &&
+ section.raw.sparse_info.bucket.table_size != 0) {
+ LOG_ERROR(Loader, "Sparse NCAs are not supported.");
+ status = Loader::ResultStatus::ErrorSparseNCA;
+ return false;
+ }
+
+ if (section.raw.compression_info.bucket.table_offset != 0 &&
+ section.raw.compression_info.bucket.table_size != 0) {
+ LOG_ERROR(Loader, "Compressed NCAs are not supported.");
+ status = Loader::ResultStatus::ErrorCompressedNCA;
+ return false;
+ }
+
if (section.raw.header.filesystem_type == NCASectionFilesystemType::ROMFS) {
if (!ReadRomFSSection(section, header.section_tables[i], bktr_base_ivfc_offset)) {
return false;
diff --git a/src/core/file_sys/fsmitm_romfsbuild.cpp b/src/core/file_sys/fsmitm_romfsbuild.cpp
index 1ff83c08c..e39c7b62b 100644
--- a/src/core/file_sys/fsmitm_romfsbuild.cpp
+++ b/src/core/file_sys/fsmitm_romfsbuild.cpp
@@ -105,19 +105,11 @@ static u64 romfs_get_hash_table_count(u64 num_entries) {
return count;
}
-void RomFSBuildContext::VisitDirectory(VirtualDir root_romfs, VirtualDir ext_dir,
+void RomFSBuildContext::VisitDirectory(VirtualDir romfs_dir, VirtualDir ext_dir,
std::shared_ptr<RomFSBuildDirectoryContext> parent) {
std::vector<std::shared_ptr<RomFSBuildDirectoryContext>> child_dirs;
- VirtualDir dir;
-
- if (parent->path_len == 0) {
- dir = root_romfs;
- } else {
- dir = root_romfs->GetDirectoryRelative(parent->path);
- }
-
- const auto entries = dir->GetEntries();
+ const auto entries = romfs_dir->GetEntries();
for (const auto& kv : entries) {
if (kv.second == VfsEntryType::Directory) {
@@ -127,7 +119,7 @@ void RomFSBuildContext::VisitDirectory(VirtualDir root_romfs, VirtualDir ext_dir
child->path_len = child->cur_path_ofs + static_cast<u32>(kv.first.size());
child->path = parent->path + "/" + kv.first;
- if (ext_dir != nullptr && ext_dir->GetFileRelative(child->path + ".stub") != nullptr) {
+ if (ext_dir != nullptr && ext_dir->GetFile(kv.first + ".stub") != nullptr) {
continue;
}
@@ -144,17 +136,17 @@ void RomFSBuildContext::VisitDirectory(VirtualDir root_romfs, VirtualDir ext_dir
child->path_len = child->cur_path_ofs + static_cast<u32>(kv.first.size());
child->path = parent->path + "/" + kv.first;
- if (ext_dir != nullptr && ext_dir->GetFileRelative(child->path + ".stub") != nullptr) {
+ if (ext_dir != nullptr && ext_dir->GetFile(kv.first + ".stub") != nullptr) {
continue;
}
// Sanity check on path_len
ASSERT(child->path_len < FS_MAX_PATH);
- child->source = root_romfs->GetFileRelative(child->path);
+ child->source = romfs_dir->GetFile(kv.first);
if (ext_dir != nullptr) {
- if (const auto ips = ext_dir->GetFileRelative(child->path + ".ips")) {
+ if (const auto ips = ext_dir->GetFile(kv.first + ".ips")) {
if (auto patched = PatchIPS(child->source, ips)) {
child->source = std::move(patched);
}
@@ -168,23 +160,27 @@ void RomFSBuildContext::VisitDirectory(VirtualDir root_romfs, VirtualDir ext_dir
}
for (auto& child : child_dirs) {
- this->VisitDirectory(root_romfs, ext_dir, child);
+ auto subdir_name = std::string_view(child->path).substr(child->cur_path_ofs);
+ auto child_romfs_dir = romfs_dir->GetSubdirectory(subdir_name);
+ auto child_ext_dir = ext_dir != nullptr ? ext_dir->GetSubdirectory(subdir_name) : nullptr;
+ this->VisitDirectory(child_romfs_dir, child_ext_dir, child);
}
}
bool RomFSBuildContext::AddDirectory(std::shared_ptr<RomFSBuildDirectoryContext> parent_dir_ctx,
std::shared_ptr<RomFSBuildDirectoryContext> dir_ctx) {
// Check whether it's already in the known directories.
- const auto existing = directories.find(dir_ctx->path);
- if (existing != directories.end())
+ const auto [it, is_new] = directories.emplace(dir_ctx->path, nullptr);
+ if (!is_new) {
return false;
+ }
// Add a new directory.
num_dirs++;
dir_table_size +=
sizeof(RomFSDirectoryEntry) + Common::AlignUp(dir_ctx->path_len - dir_ctx->cur_path_ofs, 4);
dir_ctx->parent = parent_dir_ctx;
- directories.emplace(dir_ctx->path, dir_ctx);
+ it->second = dir_ctx;
return true;
}
@@ -192,8 +188,8 @@ bool RomFSBuildContext::AddDirectory(std::shared_ptr<RomFSBuildDirectoryContext>
bool RomFSBuildContext::AddFile(std::shared_ptr<RomFSBuildDirectoryContext> parent_dir_ctx,
std::shared_ptr<RomFSBuildFileContext> file_ctx) {
// Check whether it's already in the known files.
- const auto existing = files.find(file_ctx->path);
- if (existing != files.end()) {
+ const auto [it, is_new] = files.emplace(file_ctx->path, nullptr);
+ if (!is_new) {
return false;
}
@@ -202,7 +198,7 @@ bool RomFSBuildContext::AddFile(std::shared_ptr<RomFSBuildDirectoryContext> pare
file_table_size +=
sizeof(RomFSFileEntry) + Common::AlignUp(file_ctx->path_len - file_ctx->cur_path_ofs, 4);
file_ctx->parent = parent_dir_ctx;
- files.emplace(file_ctx->path, file_ctx);
+ it->second = file_ctx;
return true;
}
diff --git a/src/core/file_sys/vfs_real.cpp b/src/core/file_sys/vfs_real.cpp
index b0515ec05..1c706e4d8 100644
--- a/src/core/file_sys/vfs_real.cpp
+++ b/src/core/file_sys/vfs_real.cpp
@@ -283,7 +283,8 @@ std::size_t RealVfsFile::GetSize() const {
if (size) {
return *size;
}
- return FS::GetSize(path);
+ auto lk = base.RefreshReference(path, perms, *reference);
+ return reference->file ? reference->file->GetSize() : 0;
}
bool RealVfsFile::Resize(std::size_t new_size) {
diff --git a/src/core/gpu_dirty_memory_manager.h b/src/core/gpu_dirty_memory_manager.h
new file mode 100644
index 000000000..9687531e8
--- /dev/null
+++ b/src/core/gpu_dirty_memory_manager.h
@@ -0,0 +1,122 @@
+// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
+// SPDX-License-Identifier: GPL-3.0-or-later
+
+#pragma once
+
+#include <atomic>
+#include <bit>
+#include <functional>
+#include <mutex>
+#include <utility>
+#include <vector>
+
+#include "core/memory.h"
+
+namespace Core {
+
+class GPUDirtyMemoryManager {
+public:
+ GPUDirtyMemoryManager() : current{default_transform} {
+ back_buffer.reserve(256);
+ front_buffer.reserve(256);
+ }
+
+ ~GPUDirtyMemoryManager() = default;
+
+ void Collect(VAddr address, size_t size) {
+ TransformAddress t = BuildTransform(address, size);
+ TransformAddress tmp, original;
+ do {
+ tmp = current.load(std::memory_order_acquire);
+ original = tmp;
+ if (tmp.address != t.address) {
+ if (IsValid(tmp.address)) {
+ std::scoped_lock lk(guard);
+ back_buffer.emplace_back(tmp);
+ current.exchange(t, std::memory_order_relaxed);
+ return;
+ }
+ tmp.address = t.address;
+ tmp.mask = 0;
+ }
+ if ((tmp.mask | t.mask) == tmp.mask) {
+ return;
+ }
+ tmp.mask |= t.mask;
+ } while (!current.compare_exchange_weak(original, tmp, std::memory_order_release,
+ std::memory_order_relaxed));
+ }
+
+ void Gather(std::function<void(VAddr, size_t)>& callback) {
+ {
+ std::scoped_lock lk(guard);
+ TransformAddress t = current.exchange(default_transform, std::memory_order_relaxed);
+ front_buffer.swap(back_buffer);
+ if (IsValid(t.address)) {
+ front_buffer.emplace_back(t);
+ }
+ }
+ for (auto& transform : front_buffer) {
+ size_t offset = 0;
+ u64 mask = transform.mask;
+ while (mask != 0) {
+ const size_t empty_bits = std::countr_zero(mask);
+ offset += empty_bits << align_bits;
+ mask = mask >> empty_bits;
+
+ const size_t continuous_bits = std::countr_one(mask);
+ callback((static_cast<VAddr>(transform.address) << page_bits) + offset,
+ continuous_bits << align_bits);
+ mask = continuous_bits < align_size ? (mask >> continuous_bits) : 0;
+ offset += continuous_bits << align_bits;
+ }
+ }
+ front_buffer.clear();
+ }
+
+private:
+ struct alignas(8) TransformAddress {
+ u32 address;
+ u32 mask;
+ };
+
+ constexpr static size_t page_bits = Memory::YUZU_PAGEBITS - 1;
+ constexpr static size_t page_size = 1ULL << page_bits;
+ constexpr static size_t page_mask = page_size - 1;
+
+ constexpr static size_t align_bits = 6U;
+ constexpr static size_t align_size = 1U << align_bits;
+ constexpr static size_t align_mask = align_size - 1;
+ constexpr static TransformAddress default_transform = {.address = ~0U, .mask = 0U};
+
+ bool IsValid(VAddr address) {
+ return address < (1ULL << 39);
+ }
+
+ template <typename T>
+ T CreateMask(size_t top_bit, size_t minor_bit) {
+ T mask = ~T(0);
+ mask <<= (sizeof(T) * 8 - top_bit);
+ mask >>= (sizeof(T) * 8 - top_bit);
+ mask >>= minor_bit;
+ mask <<= minor_bit;
+ return mask;
+ }
+
+ TransformAddress BuildTransform(VAddr address, size_t size) {
+ const size_t minor_address = address & page_mask;
+ const size_t minor_bit = minor_address >> align_bits;
+ const size_t top_bit = (minor_address + size + align_mask) >> align_bits;
+ TransformAddress result{};
+ result.address = static_cast<u32>(address >> page_bits);
+ result.mask = CreateMask<u32>(top_bit, minor_bit);
+ return result;
+ }
+
+ std::atomic<TransformAddress> current{};
+ std::mutex guard;
+ std::vector<TransformAddress> back_buffer;
+ std::vector<TransformAddress> front_buffer;
+};
+
+} // namespace Core
diff --git a/src/core/hid/emulated_controller.cpp b/src/core/hid/emulated_controller.cpp
index 1ebc32c1e..94bd656fe 100644
--- a/src/core/hid/emulated_controller.cpp
+++ b/src/core/hid/emulated_controller.cpp
@@ -1243,10 +1243,12 @@ Common::Input::DriverResult EmulatedController::SetPollingMode(
auto& nfc_output_device = output_devices[3];
if (device_index == EmulatedDeviceIndex::LeftIndex) {
+ controller.left_polling_mode = polling_mode;
return left_output_device->SetPollingMode(polling_mode);
}
if (device_index == EmulatedDeviceIndex::RightIndex) {
+ controller.right_polling_mode = polling_mode;
const auto virtual_nfc_result = nfc_output_device->SetPollingMode(polling_mode);
const auto mapped_nfc_result = right_output_device->SetPollingMode(polling_mode);
@@ -1261,12 +1263,22 @@ Common::Input::DriverResult EmulatedController::SetPollingMode(
return mapped_nfc_result;
}
+ controller.left_polling_mode = polling_mode;
+ controller.right_polling_mode = polling_mode;
left_output_device->SetPollingMode(polling_mode);
right_output_device->SetPollingMode(polling_mode);
nfc_output_device->SetPollingMode(polling_mode);
return Common::Input::DriverResult::Success;
}
+Common::Input::PollingMode EmulatedController::GetPollingMode(
+ EmulatedDeviceIndex device_index) const {
+ if (device_index == EmulatedDeviceIndex::LeftIndex) {
+ return controller.left_polling_mode;
+ }
+ return controller.right_polling_mode;
+}
+
bool EmulatedController::SetCameraFormat(
Core::IrSensor::ImageTransferProcessorFormat camera_format) {
LOG_INFO(Service_HID, "Set camera format {}", camera_format);
diff --git a/src/core/hid/emulated_controller.h b/src/core/hid/emulated_controller.h
index d511e5fac..88d77db8d 100644
--- a/src/core/hid/emulated_controller.h
+++ b/src/core/hid/emulated_controller.h
@@ -143,6 +143,8 @@ struct ControllerStatus {
CameraState camera_state{};
RingSensorForce ring_analog_state{};
NfcState nfc_state{};
+ Common::Input::PollingMode left_polling_mode{};
+ Common::Input::PollingMode right_polling_mode{};
};
enum class ControllerTriggerType {
@@ -370,6 +372,12 @@ public:
*/
Common::Input::DriverResult SetPollingMode(EmulatedDeviceIndex device_index,
Common::Input::PollingMode polling_mode);
+ /**
+ * Get the current polling mode from a controller
+ * @param device_index index of the controller to set the polling mode
+ * @return current polling mode
+ */
+ Common::Input::PollingMode GetPollingMode(EmulatedDeviceIndex device_index) const;
/**
* Sets the desired camera format to be polled from a controller
diff --git a/src/core/hle/kernel/k_server_session.cpp b/src/core/hle/kernel/k_server_session.cpp
index c66aff501..c64ceb530 100644
--- a/src/core/hle/kernel/k_server_session.cpp
+++ b/src/core/hle/kernel/k_server_session.cpp
@@ -20,12 +20,132 @@
#include "core/hle/kernel/k_thread.h"
#include "core/hle/kernel/k_thread_queue.h"
#include "core/hle/kernel/kernel.h"
+#include "core/hle/kernel/message_buffer.h"
#include "core/hle/service/hle_ipc.h"
#include "core/hle/service/ipc_helpers.h"
#include "core/memory.h"
namespace Kernel {
+namespace {
+
+template <bool MoveHandleAllowed>
+Result ProcessMessageSpecialData(KProcess& dst_process, KProcess& src_process, KThread& src_thread,
+ MessageBuffer& dst_msg, const MessageBuffer& src_msg,
+ MessageBuffer::SpecialHeader& src_special_header) {
+ // Copy the special header to the destination.
+ s32 offset = dst_msg.Set(src_special_header);
+
+ // Copy the process ID.
+ if (src_special_header.GetHasProcessId()) {
+ offset = dst_msg.SetProcessId(offset, src_process.GetProcessId());
+ }
+
+ // Prepare to process handles.
+ auto& dst_handle_table = dst_process.GetHandleTable();
+ auto& src_handle_table = src_process.GetHandleTable();
+ Result result = ResultSuccess;
+
+ // Process copy handles.
+ for (auto i = 0; i < src_special_header.GetCopyHandleCount(); ++i) {
+ // Get the handles.
+ const Handle src_handle = src_msg.GetHandle(offset);
+ Handle dst_handle = Svc::InvalidHandle;
+
+ // If we're in a success state, try to move the handle to the new table.
+ if (R_SUCCEEDED(result) && src_handle != Svc::InvalidHandle) {
+ KScopedAutoObject obj =
+ src_handle_table.GetObjectForIpc(src_handle, std::addressof(src_thread));
+ if (obj.IsNotNull()) {
+ Result add_result =
+ dst_handle_table.Add(std::addressof(dst_handle), obj.GetPointerUnsafe());
+ if (R_FAILED(add_result)) {
+ result = add_result;
+ dst_handle = Svc::InvalidHandle;
+ }
+ } else {
+ result = ResultInvalidHandle;
+ }
+ }
+
+ // Set the handle.
+ offset = dst_msg.SetHandle(offset, dst_handle);
+ }
+
+ // Process move handles.
+ if constexpr (MoveHandleAllowed) {
+ for (auto i = 0; i < src_special_header.GetMoveHandleCount(); ++i) {
+ // Get the handles.
+ const Handle src_handle = src_msg.GetHandle(offset);
+ Handle dst_handle = Svc::InvalidHandle;
+
+ // Whether or not we've succeeded, we need to remove the handles from the source table.
+ if (src_handle != Svc::InvalidHandle) {
+ if (R_SUCCEEDED(result)) {
+ KScopedAutoObject obj =
+ src_handle_table.GetObjectForIpcWithoutPseudoHandle(src_handle);
+ if (obj.IsNotNull()) {
+ Result add_result = dst_handle_table.Add(std::addressof(dst_handle),
+ obj.GetPointerUnsafe());
+
+ src_handle_table.Remove(src_handle);
+
+ if (R_FAILED(add_result)) {
+ result = add_result;
+ dst_handle = Svc::InvalidHandle;
+ }
+ } else {
+ result = ResultInvalidHandle;
+ }
+ } else {
+ src_handle_table.Remove(src_handle);
+ }
+ }
+
+ // Set the handle.
+ offset = dst_msg.SetHandle(offset, dst_handle);
+ }
+ }
+
+ R_RETURN(result);
+}
+
+void CleanupSpecialData(KProcess& dst_process, u32* dst_msg_ptr, size_t dst_buffer_size) {
+ // Parse the message.
+ const MessageBuffer dst_msg(dst_msg_ptr, dst_buffer_size);
+ const MessageBuffer::MessageHeader dst_header(dst_msg);
+ const MessageBuffer::SpecialHeader dst_special_header(dst_msg, dst_header);
+
+ // Check that the size is big enough.
+ if (MessageBuffer::GetMessageBufferSize(dst_header, dst_special_header) > dst_buffer_size) {
+ return;
+ }
+
+ // Set the special header.
+ int offset = dst_msg.Set(dst_special_header);
+
+ // Clear the process id, if needed.
+ if (dst_special_header.GetHasProcessId()) {
+ offset = dst_msg.SetProcessId(offset, 0);
+ }
+
+ // Clear handles, as relevant.
+ auto& dst_handle_table = dst_process.GetHandleTable();
+ for (auto i = 0;
+ i < (dst_special_header.GetCopyHandleCount() + dst_special_header.GetMoveHandleCount());
+ ++i) {
+ const Handle handle = dst_msg.GetHandle(offset);
+
+ if (handle != Svc::InvalidHandle) {
+ dst_handle_table.Remove(handle);
+ }
+
+ offset = dst_msg.SetHandle(offset, Svc::InvalidHandle);
+ }
+}
+
+} // namespace
+
using ThreadQueueImplForKServerSessionRequest = KThreadQueue;
KServerSession::KServerSession(KernelCore& kernel)
@@ -223,12 +343,27 @@ Result KServerSession::SendReply(bool is_hle) {
// the reply has already been written in this case.
} else {
Core::Memory::Memory& memory{client_thread->GetOwnerProcess()->GetMemory()};
- KThread* server_thread{GetCurrentThreadPointer(m_kernel)};
+ KThread* server_thread = GetCurrentThreadPointer(m_kernel);
+ KProcess& src_process = *client_thread->GetOwnerProcess();
+ KProcess& dst_process = *server_thread->GetOwnerProcess();
UNIMPLEMENTED_IF(server_thread->GetOwnerProcess() != client_thread->GetOwnerProcess());
- auto* src_msg_buffer = memory.GetPointer(server_thread->GetTlsAddress());
- auto* dst_msg_buffer = memory.GetPointer(client_message);
+ auto* src_msg_buffer = memory.GetPointer<u32>(server_thread->GetTlsAddress());
+ auto* dst_msg_buffer = memory.GetPointer<u32>(client_message);
std::memcpy(dst_msg_buffer, src_msg_buffer, client_buffer_size);
+
+ // Translate special header ad-hoc.
+ MessageBuffer src_msg(src_msg_buffer, client_buffer_size);
+ MessageBuffer::MessageHeader src_header(src_msg);
+ MessageBuffer::SpecialHeader src_special_header(src_msg, src_header);
+ if (src_header.GetHasSpecialHeader()) {
+ MessageBuffer dst_msg(dst_msg_buffer, client_buffer_size);
+ result = ProcessMessageSpecialData<true>(dst_process, src_process, *server_thread,
+ dst_msg, src_msg, src_special_header);
+ if (R_FAILED(result)) {
+ CleanupSpecialData(dst_process, dst_msg_buffer, client_buffer_size);
+ }
+ }
}
} else {
result = ResultSessionClosed;
@@ -330,12 +465,28 @@ Result KServerSession::ReceiveRequest(std::shared_ptr<Service::HLERequestContext
->PopulateFromIncomingCommandBuffer(client_thread->GetOwnerProcess()->GetHandleTable(),
cmd_buf);
} else {
- KThread* server_thread{GetCurrentThreadPointer(m_kernel)};
- UNIMPLEMENTED_IF(server_thread->GetOwnerProcess() != client_thread->GetOwnerProcess());
+ KThread* server_thread = GetCurrentThreadPointer(m_kernel);
+ KProcess& src_process = *client_thread->GetOwnerProcess();
+ KProcess& dst_process = *server_thread->GetOwnerProcess();
+ UNIMPLEMENTED_IF(client_thread->GetOwnerProcess() != server_thread->GetOwnerProcess());
- auto* src_msg_buffer = memory.GetPointer(client_message);
- auto* dst_msg_buffer = memory.GetPointer(server_thread->GetTlsAddress());
+ auto* src_msg_buffer = memory.GetPointer<u32>(client_message);
+ auto* dst_msg_buffer = memory.GetPointer<u32>(server_thread->GetTlsAddress());
std::memcpy(dst_msg_buffer, src_msg_buffer, client_buffer_size);
+
+ // Translate special header ad-hoc.
+ // TODO: fix this mess
+ MessageBuffer src_msg(src_msg_buffer, client_buffer_size);
+ MessageBuffer::MessageHeader src_header(src_msg);
+ MessageBuffer::SpecialHeader src_special_header(src_msg, src_header);
+ if (src_header.GetHasSpecialHeader()) {
+ MessageBuffer dst_msg(dst_msg_buffer, client_buffer_size);
+ Result res = ProcessMessageSpecialData<false>(dst_process, src_process, *client_thread,
+ dst_msg, src_msg, src_special_header);
+ if (R_FAILED(res)) {
+ CleanupSpecialData(dst_process, dst_msg_buffer, client_buffer_size);
+ }
+ }
}
// We succeeded.
diff --git a/src/core/hle/kernel/k_thread.h b/src/core/hle/kernel/k_thread.h
index dd662b3f8..d178c2453 100644
--- a/src/core/hle/kernel/k_thread.h
+++ b/src/core/hle/kernel/k_thread.h
@@ -338,6 +338,15 @@ public:
return m_parent != nullptr;
}
+ std::span<KSynchronizationObject*> GetSynchronizationObjectBuffer() {
+ return m_sync_object_buffer.sync_objects;
+ }
+
+ std::span<Handle> GetHandleBuffer() {
+ return {m_sync_object_buffer.handles.data() + Svc::ArgumentHandleCountMax,
+ Svc::ArgumentHandleCountMax};
+ }
+
u16 GetUserDisableCount() const;
void SetInterruptFlag();
void ClearInterruptFlag();
@@ -855,6 +864,7 @@ private:
u32* m_light_ipc_data{};
KProcessAddress m_tls_address{};
KLightLock m_activity_pause_lock;
+ SyncObjectBuffer m_sync_object_buffer{};
s64 m_schedule_count{};
s64 m_last_scheduled_tick{};
std::array<QueueEntry, Core::Hardware::NUM_CPU_CORES> m_per_core_priority_queue_entry{};
diff --git a/src/core/hle/kernel/message_buffer.h b/src/core/hle/kernel/message_buffer.h
new file mode 100644
index 000000000..75b275310
--- /dev/null
+++ b/src/core/hle/kernel/message_buffer.h
@@ -0,0 +1,612 @@
+// SPDX-FileCopyrightText: 2023 yuzu Emulator Project
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#pragma once
+
+#include "common/alignment.h"
+#include "common/bit_field.h"
+#include "core/hle/kernel/k_thread.h"
+
+namespace Kernel {
+
+constexpr inline size_t MessageBufferSize = 0x100;
+
+class MessageBuffer {
+public:
+ class MessageHeader {
+ private:
+ static constexpr inline u64 NullTag = 0;
+
+ public:
+ enum class ReceiveListCountType : u32 {
+ None = 0,
+ ToMessageBuffer = 1,
+ ToSingleBuffer = 2,
+
+ CountOffset = 2,
+ CountMax = 13,
+ };
+
+ private:
+ union {
+ std::array<u32, 2> raw;
+
+ struct {
+ // Define fields for the first header word.
+ union {
+ BitField<0, 16, u16> tag;
+ BitField<16, 4, u32> pointer_count;
+ BitField<20, 4, u32> send_count;
+ BitField<24, 4, u32> receive_count;
+ BitField<28, 4, u32> exchange_count;
+ };
+
+ // Define fields for the second header word.
+ union {
+ BitField<0, 10, u32> raw_count;
+ BitField<10, 4, ReceiveListCountType> receive_list_count;
+ BitField<14, 6, u32> reserved0;
+ BitField<20, 11, u32> receive_list_offset;
+ BitField<31, 1, u32> has_special_header;
+ };
+ };
+ } m_header;
+
+ public:
+ constexpr MessageHeader() : m_header{} {}
+
+ constexpr MessageHeader(u16 tag, bool special, s32 ptr, s32 send, s32 recv, s32 exch,
+ s32 raw, ReceiveListCountType recv_list)
+ : m_header{} {
+ m_header.raw[0] = 0;
+ m_header.raw[1] = 0;
+
+ m_header.tag.Assign(tag);
+ m_header.pointer_count.Assign(ptr);
+ m_header.send_count.Assign(send);
+ m_header.receive_count.Assign(recv);
+ m_header.exchange_count.Assign(exch);
+
+ m_header.raw_count.Assign(raw);
+ m_header.receive_list_count.Assign(recv_list);
+ m_header.has_special_header.Assign(special);
+ }
+
+ explicit MessageHeader(const MessageBuffer& buf) : m_header{} {
+ buf.Get(0, m_header.raw.data(), 2);
+ }
+
+ explicit MessageHeader(const u32* msg) : m_header{{msg[0], msg[1]}} {}
+
+ constexpr u16 GetTag() const {
+ return m_header.tag;
+ }
+
+ constexpr s32 GetPointerCount() const {
+ return m_header.pointer_count;
+ }
+
+ constexpr s32 GetSendCount() const {
+ return m_header.send_count;
+ }
+
+ constexpr s32 GetReceiveCount() const {
+ return m_header.receive_count;
+ }
+
+ constexpr s32 GetExchangeCount() const {
+ return m_header.exchange_count;
+ }
+
+ constexpr s32 GetMapAliasCount() const {
+ return this->GetSendCount() + this->GetReceiveCount() + this->GetExchangeCount();
+ }
+
+ constexpr s32 GetRawCount() const {
+ return m_header.raw_count;
+ }
+
+ constexpr ReceiveListCountType GetReceiveListCount() const {
+ return m_header.receive_list_count;
+ }
+
+ constexpr s32 GetReceiveListOffset() const {
+ return m_header.receive_list_offset;
+ }
+
+ constexpr bool GetHasSpecialHeader() const {
+ return m_header.has_special_header.Value() != 0;
+ }
+
+ constexpr void SetReceiveListCount(ReceiveListCountType recv_list) {
+ m_header.receive_list_count.Assign(recv_list);
+ }
+
+ constexpr const u32* GetData() const {
+ return m_header.raw.data();
+ }
+
+ static constexpr size_t GetDataSize() {
+ return sizeof(m_header);
+ }
+ };
+
+ class SpecialHeader {
+ private:
+ union {
+ std::array<u32, 1> raw;
+
+ // Define fields for the header word.
+ BitField<0, 1, u32> has_process_id;
+ BitField<1, 4, u32> copy_handle_count;
+ BitField<5, 4, u32> move_handle_count;
+ } m_header;
+ bool m_has_header;
+
+ public:
+ constexpr explicit SpecialHeader(bool pid, s32 copy, s32 move)
+ : m_header{}, m_has_header(true) {
+ m_header.has_process_id.Assign(pid);
+ m_header.copy_handle_count.Assign(copy);
+ m_header.move_handle_count.Assign(move);
+ }
+
+ constexpr explicit SpecialHeader(bool pid, s32 copy, s32 move, bool _has_header)
+ : m_header{}, m_has_header(_has_header) {
+ m_header.has_process_id.Assign(pid);
+ m_header.copy_handle_count.Assign(copy);
+ m_header.move_handle_count.Assign(move);
+ }
+
+ explicit SpecialHeader(const MessageBuffer& buf, const MessageHeader& hdr)
+ : m_header{}, m_has_header(hdr.GetHasSpecialHeader()) {
+ if (m_has_header) {
+ buf.Get(static_cast<s32>(MessageHeader::GetDataSize() / sizeof(u32)),
+ m_header.raw.data(), sizeof(m_header) / sizeof(u32));
+ }
+ }
+
+ constexpr bool GetHasProcessId() const {
+ return m_header.has_process_id.Value() != 0;
+ }
+
+ constexpr s32 GetCopyHandleCount() const {
+ return m_header.copy_handle_count;
+ }
+
+ constexpr s32 GetMoveHandleCount() const {
+ return m_header.move_handle_count;
+ }
+
+ constexpr const u32* GetHeader() const {
+ return m_header.raw.data();
+ }
+
+ constexpr size_t GetHeaderSize() const {
+ if (m_has_header) {
+ return sizeof(m_header);
+ } else {
+ return 0;
+ }
+ }
+
+ constexpr size_t GetDataSize() const {
+ if (m_has_header) {
+ return (this->GetHasProcessId() ? sizeof(u64) : 0) +
+ (this->GetCopyHandleCount() * sizeof(Handle)) +
+ (this->GetMoveHandleCount() * sizeof(Handle));
+ } else {
+ return 0;
+ }
+ }
+ };
+
+ class MapAliasDescriptor {
+ public:
+ enum class Attribute : u32 {
+ Ipc = 0,
+ NonSecureIpc = 1,
+ NonDeviceIpc = 3,
+ };
+
+ private:
+ static constexpr u32 SizeLowCount = 32;
+ static constexpr u32 SizeHighCount = 4;
+ static constexpr u32 AddressLowCount = 32;
+ static constexpr u32 AddressMidCount = 4;
+
+ constexpr u32 GetAddressMid(u64 address) {
+ return static_cast<u32>(address >> AddressLowCount) & ((1U << AddressMidCount) - 1);
+ }
+
+ constexpr u32 GetAddressHigh(u64 address) {
+ return static_cast<u32>(address >> (AddressLowCount + AddressMidCount));
+ }
+
+ private:
+ union {
+ std::array<u32, 3> raw;
+
+ struct {
+ // Define fields for the first two words.
+ u32 size_low;
+ u32 address_low;
+
+ // Define fields for the packed descriptor word.
+ union {
+ BitField<0, 2, Attribute> attributes;
+ BitField<2, 3, u32> address_high;
+ BitField<5, 19, u32> reserved;
+ BitField<24, 4, u32> size_high;
+ BitField<28, 4, u32> address_mid;
+ };
+ };
+ } m_data;
+
+ public:
+ constexpr MapAliasDescriptor() : m_data{} {}
+
+ MapAliasDescriptor(const void* buffer, size_t _size, Attribute attr = Attribute::Ipc)
+ : m_data{} {
+ const u64 address = reinterpret_cast<u64>(buffer);
+ const u64 size = static_cast<u64>(_size);
+ m_data.size_low = static_cast<u32>(size);
+ m_data.address_low = static_cast<u32>(address);
+ m_data.attributes.Assign(attr);
+ m_data.address_mid.Assign(GetAddressMid(address));
+ m_data.size_high.Assign(static_cast<u32>(size >> SizeLowCount));
+ m_data.address_high.Assign(GetAddressHigh(address));
+ }
+
+ MapAliasDescriptor(const MessageBuffer& buf, s32 index) : m_data{} {
+ buf.Get(index, m_data.raw.data(), 3);
+ }
+
+ constexpr uintptr_t GetAddress() const {
+ return (static_cast<u64>((m_data.address_high << AddressMidCount) | m_data.address_mid)
+ << AddressLowCount) |
+ m_data.address_low;
+ }
+
+ constexpr uintptr_t GetSize() const {
+ return (static_cast<u64>(m_data.size_high) << SizeLowCount) | m_data.size_low;
+ }
+
+ constexpr Attribute GetAttribute() const {
+ return m_data.attributes;
+ }
+
+ constexpr const u32* GetData() const {
+ return m_data.raw.data();
+ }
+
+ static constexpr size_t GetDataSize() {
+ return sizeof(m_data);
+ }
+ };
+
+ class PointerDescriptor {
+ private:
+ static constexpr u32 AddressLowCount = 32;
+ static constexpr u32 AddressMidCount = 4;
+
+ constexpr u32 GetAddressMid(u64 address) {
+ return static_cast<u32>(address >> AddressLowCount) & ((1u << AddressMidCount) - 1);
+ }
+
+ constexpr u32 GetAddressHigh(u64 address) {
+ return static_cast<u32>(address >> (AddressLowCount + AddressMidCount));
+ }
+
+ private:
+ union {
+ std::array<u32, 2> raw;
+
+ struct {
+ // Define fields for the packed descriptor word.
+ union {
+ BitField<0, 4, u32> index;
+ BitField<4, 2, u32> reserved0;
+ BitField<6, 3, u32> address_high;
+ BitField<9, 3, u32> reserved1;
+ BitField<12, 4, u32> address_mid;
+ BitField<16, 16, u32> size;
+ };
+
+ // Define fields for the second word.
+ u32 address_low;
+ };
+ } m_data;
+
+ public:
+ constexpr PointerDescriptor() : m_data{} {}
+
+ PointerDescriptor(const void* buffer, size_t size, s32 index) : m_data{} {
+ const u64 address = reinterpret_cast<u64>(buffer);
+
+ m_data.index.Assign(index);
+ m_data.address_high.Assign(GetAddressHigh(address));
+ m_data.address_mid.Assign(GetAddressMid(address));
+ m_data.size.Assign(static_cast<u32>(size));
+
+ m_data.address_low = static_cast<u32>(address);
+ }
+
+ PointerDescriptor(const MessageBuffer& buf, s32 index) : m_data{} {
+ buf.Get(index, m_data.raw.data(), 2);
+ }
+
+ constexpr s32 GetIndex() const {
+ return m_data.index;
+ }
+
+ constexpr uintptr_t GetAddress() const {
+ return (static_cast<u64>((m_data.address_high << AddressMidCount) | m_data.address_mid)
+ << AddressLowCount) |
+ m_data.address_low;
+ }
+
+ constexpr size_t GetSize() const {
+ return m_data.size;
+ }
+
+ constexpr const u32* GetData() const {
+ return m_data.raw.data();
+ }
+
+ static constexpr size_t GetDataSize() {
+ return sizeof(m_data);
+ }
+ };
+
+ class ReceiveListEntry {
+ private:
+ static constexpr u32 AddressLowCount = 32;
+
+ constexpr u32 GetAddressHigh(u64 address) {
+ return static_cast<u32>(address >> (AddressLowCount));
+ }
+
+ private:
+ union {
+ std::array<u32, 2> raw;
+
+ struct {
+ // Define fields for the first word.
+ u32 address_low;
+
+ // Define fields for the packed descriptor word.
+ union {
+ BitField<0, 7, u32> address_high;
+ BitField<7, 9, u32> reserved;
+ BitField<16, 16, u32> size;
+ };
+ };
+ } m_data;
+
+ public:
+ constexpr ReceiveListEntry() : m_data{} {}
+
+ ReceiveListEntry(const void* buffer, size_t size) : m_data{} {
+ const u64 address = reinterpret_cast<u64>(buffer);
+
+ m_data.address_low = static_cast<u32>(address);
+
+ m_data.address_high.Assign(GetAddressHigh(address));
+ m_data.size.Assign(static_cast<u32>(size));
+ }
+
+ ReceiveListEntry(u32 a, u32 b) : m_data{{a, b}} {}
+
+ constexpr uintptr_t GetAddress() const {
+ return (static_cast<u64>(m_data.address_high) << AddressLowCount) | m_data.address_low;
+ }
+
+ constexpr size_t GetSize() const {
+ return m_data.size;
+ }
+
+ constexpr const u32* GetData() const {
+ return m_data.raw.data();
+ }
+
+ static constexpr size_t GetDataSize() {
+ return sizeof(m_data);
+ }
+ };
+
+private:
+ u32* m_buffer;
+ size_t m_size;
+
+public:
+ constexpr MessageBuffer(u32* b, size_t sz) : m_buffer(b), m_size(sz) {}
+ constexpr explicit MessageBuffer(u32* b) : m_buffer(b), m_size(MessageBufferSize) {}
+
+ constexpr void* GetBufferForDebug() const {
+ return m_buffer;
+ }
+
+ constexpr size_t GetBufferSize() const {
+ return m_size;
+ }
+
+ void Get(s32 index, u32* dst, size_t count) const {
+ // Ensure that this doesn't get re-ordered.
+ std::atomic_thread_fence(std::memory_order_seq_cst);
+
+ // Get the words.
+ static_assert(sizeof(*dst) == sizeof(*m_buffer));
+
+ memcpy(dst, m_buffer + index, count * sizeof(*dst));
+ }
+
+ s32 Set(s32 index, u32* src, size_t count) const {
+ // Ensure that this doesn't get re-ordered.
+ std::atomic_thread_fence(std::memory_order_seq_cst);
+
+ // Set the words.
+ memcpy(m_buffer + index, src, count * sizeof(*src));
+
+ // Ensure that this doesn't get re-ordered.
+ std::atomic_thread_fence(std::memory_order_seq_cst);
+
+ return static_cast<s32>(index + count);
+ }
+
+ template <typename T>
+ const T& GetRaw(s32 index) const {
+ return *reinterpret_cast<const T*>(m_buffer + index);
+ }
+
+ template <typename T>
+ s32 SetRaw(s32 index, const T& val) const {
+ *reinterpret_cast<const T*>(m_buffer + index) = val;
+ return index + (Common::AlignUp(sizeof(val), sizeof(*m_buffer)) / sizeof(*m_buffer));
+ }
+
+ void GetRawArray(s32 index, void* dst, size_t len) const {
+ memcpy(dst, m_buffer + index, len);
+ }
+
+ void SetRawArray(s32 index, const void* src, size_t len) const {
+ memcpy(m_buffer + index, src, len);
+ }
+
+ void SetNull() const {
+ this->Set(MessageHeader());
+ }
+
+ s32 Set(const MessageHeader& hdr) const {
+ memcpy(m_buffer, hdr.GetData(), hdr.GetDataSize());
+ return static_cast<s32>(hdr.GetDataSize() / sizeof(*m_buffer));
+ }
+
+ s32 Set(const SpecialHeader& spc) const {
+ const s32 index = static_cast<s32>(MessageHeader::GetDataSize() / sizeof(*m_buffer));
+ memcpy(m_buffer + index, spc.GetHeader(), spc.GetHeaderSize());
+ return static_cast<s32>(index + (spc.GetHeaderSize() / sizeof(*m_buffer)));
+ }
+
+ s32 SetHandle(s32 index, const Handle& hnd) const {
+ memcpy(m_buffer + index, std::addressof(hnd), sizeof(hnd));
+ return static_cast<s32>(index + (sizeof(hnd) / sizeof(*m_buffer)));
+ }
+
+ s32 SetProcessId(s32 index, const u64 pid) const {
+ memcpy(m_buffer + index, std::addressof(pid), sizeof(pid));
+ return static_cast<s32>(index + (sizeof(pid) / sizeof(*m_buffer)));
+ }
+
+ s32 Set(s32 index, const MapAliasDescriptor& desc) const {
+ memcpy(m_buffer + index, desc.GetData(), desc.GetDataSize());
+ return static_cast<s32>(index + (desc.GetDataSize() / sizeof(*m_buffer)));
+ }
+
+ s32 Set(s32 index, const PointerDescriptor& desc) const {
+ memcpy(m_buffer + index, desc.GetData(), desc.GetDataSize());
+ return static_cast<s32>(index + (desc.GetDataSize() / sizeof(*m_buffer)));
+ }
+
+ s32 Set(s32 index, const ReceiveListEntry& desc) const {
+ memcpy(m_buffer + index, desc.GetData(), desc.GetDataSize());
+ return static_cast<s32>(index + (desc.GetDataSize() / sizeof(*m_buffer)));
+ }
+
+ s32 Set(s32 index, const u32 val) const {
+ memcpy(m_buffer + index, std::addressof(val), sizeof(val));
+ return static_cast<s32>(index + (sizeof(val) / sizeof(*m_buffer)));
+ }
+
+ Result GetAsyncResult() const {
+ MessageHeader hdr(m_buffer);
+ MessageHeader null{};
+ if (memcmp(hdr.GetData(), null.GetData(), MessageHeader::GetDataSize()) != 0) [[unlikely]] {
+ R_SUCCEED();
+ }
+ return Result(m_buffer[MessageHeader::GetDataSize() / sizeof(*m_buffer)]);
+ }
+
+ void SetAsyncResult(Result res) const {
+ const s32 index = this->Set(MessageHeader());
+ const auto value = res.raw;
+ memcpy(m_buffer + index, std::addressof(value), sizeof(value));
+ }
+
+ u32 Get32(s32 index) const {
+ return m_buffer[index];
+ }
+
+ u64 Get64(s32 index) const {
+ u64 value;
+ memcpy(std::addressof(value), m_buffer + index, sizeof(value));
+ return value;
+ }
+
+ u64 GetProcessId(s32 index) const {
+ return this->Get64(index);
+ }
+
+ Handle GetHandle(s32 index) const {
+ static_assert(sizeof(Handle) == sizeof(*m_buffer));
+ return Handle(m_buffer[index]);
+ }
+
+ static constexpr s32 GetSpecialDataIndex(const MessageHeader& hdr, const SpecialHeader& spc) {
+ return static_cast<s32>((MessageHeader::GetDataSize() / sizeof(u32)) +
+ (spc.GetHeaderSize() / sizeof(u32)));
+ }
+
+ static constexpr s32 GetPointerDescriptorIndex(const MessageHeader& hdr,
+ const SpecialHeader& spc) {
+ return static_cast<s32>(GetSpecialDataIndex(hdr, spc) + (spc.GetDataSize() / sizeof(u32)));
+ }
+
+ static constexpr s32 GetMapAliasDescriptorIndex(const MessageHeader& hdr,
+ const SpecialHeader& spc) {
+ return GetPointerDescriptorIndex(hdr, spc) +
+ static_cast<s32>(hdr.GetPointerCount() * PointerDescriptor::GetDataSize() /
+ sizeof(u32));
+ }
+
+ static constexpr s32 GetRawDataIndex(const MessageHeader& hdr, const SpecialHeader& spc) {
+ return GetMapAliasDescriptorIndex(hdr, spc) +
+ static_cast<s32>(hdr.GetMapAliasCount() * MapAliasDescriptor::GetDataSize() /
+ sizeof(u32));
+ }
+
+ static constexpr s32 GetReceiveListIndex(const MessageHeader& hdr, const SpecialHeader& spc) {
+ if (const s32 recv_list_index = hdr.GetReceiveListOffset()) {
+ return recv_list_index;
+ } else {
+ return GetRawDataIndex(hdr, spc) + hdr.GetRawCount();
+ }
+ }
+
+ static constexpr size_t GetMessageBufferSize(const MessageHeader& hdr,
+ const SpecialHeader& spc) {
+ // Get the size of the plain message.
+ size_t msg_size = GetReceiveListIndex(hdr, spc) * sizeof(u32);
+
+ // Add the size of the receive list.
+ const auto count = hdr.GetReceiveListCount();
+ switch (count) {
+ case MessageHeader::ReceiveListCountType::None:
+ break;
+ case MessageHeader::ReceiveListCountType::ToMessageBuffer:
+ break;
+ case MessageHeader::ReceiveListCountType::ToSingleBuffer:
+ msg_size += ReceiveListEntry::GetDataSize();
+ break;
+ default:
+ msg_size += (static_cast<s32>(count) -
+ static_cast<s32>(MessageHeader::ReceiveListCountType::CountOffset)) *
+ ReceiveListEntry::GetDataSize();
+ break;
+ }
+
+ return msg_size;
+ }
+};
+
+} // namespace Kernel
diff --git a/src/core/hle/kernel/svc/svc_ipc.cpp b/src/core/hle/kernel/svc/svc_ipc.cpp
index 60247df2e..bb94f6934 100644
--- a/src/core/hle/kernel/svc/svc_ipc.cpp
+++ b/src/core/hle/kernel/svc/svc_ipc.cpp
@@ -38,22 +38,31 @@ Result SendAsyncRequestWithUserBuffer(Core::System& system, Handle* out_event_ha
Result ReplyAndReceive(Core::System& system, s32* out_index, uint64_t handles_addr, s32 num_handles,
Handle reply_target, s64 timeout_ns) {
+ // Ensure number of handles is valid.
+ R_UNLESS(0 <= num_handles && num_handles <= ArgumentHandleCountMax, ResultOutOfRange);
+
+ // Get the synchronization context.
auto& kernel = system.Kernel();
auto& handle_table = GetCurrentProcess(kernel).GetHandleTable();
-
- R_UNLESS(0 <= num_handles && num_handles <= ArgumentHandleCountMax, ResultOutOfRange);
- R_UNLESS(GetCurrentMemory(kernel).IsValidVirtualAddressRange(
- handles_addr, static_cast<u64>(sizeof(Handle) * num_handles)),
- ResultInvalidPointer);
-
- std::array<Handle, Svc::ArgumentHandleCountMax> handles;
- GetCurrentMemory(kernel).ReadBlock(handles_addr, handles.data(), sizeof(Handle) * num_handles);
-
- // Convert handle list to object table.
- std::array<KSynchronizationObject*, Svc::ArgumentHandleCountMax> objs;
- R_UNLESS(handle_table.GetMultipleObjects<KSynchronizationObject>(objs.data(), handles.data(),
- num_handles),
- ResultInvalidHandle);
+ auto objs = GetCurrentThread(kernel).GetSynchronizationObjectBuffer();
+ auto handles = GetCurrentThread(kernel).GetHandleBuffer();
+
+ // Copy user handles.
+ if (num_handles > 0) {
+ // Ensure we can try to get the handles.
+ R_UNLESS(GetCurrentMemory(kernel).IsValidVirtualAddressRange(
+ handles_addr, static_cast<u64>(sizeof(Handle) * num_handles)),
+ ResultInvalidPointer);
+
+ // Get the handles.
+ GetCurrentMemory(kernel).ReadBlock(handles_addr, handles.data(),
+ sizeof(Handle) * num_handles);
+
+ // Convert the handles to objects.
+ R_UNLESS(handle_table.GetMultipleObjects<KSynchronizationObject>(
+ objs.data(), handles.data(), num_handles),
+ ResultInvalidHandle);
+ }
// Ensure handles are closed when we're done.
SCOPE_EXIT({
diff --git a/src/core/hle/kernel/svc/svc_synchronization.cpp b/src/core/hle/kernel/svc/svc_synchronization.cpp
index 53df5bcd8..f02d03f30 100644
--- a/src/core/hle/kernel/svc/svc_synchronization.cpp
+++ b/src/core/hle/kernel/svc/svc_synchronization.cpp
@@ -47,21 +47,35 @@ Result ResetSignal(Core::System& system, Handle handle) {
R_THROW(ResultInvalidHandle);
}
-static Result WaitSynchronization(Core::System& system, int32_t* out_index, const Handle* handles,
- int32_t num_handles, int64_t timeout_ns) {
+/// Wait for the given handles to synchronize, timeout after the specified nanoseconds
+Result WaitSynchronization(Core::System& system, int32_t* out_index, u64 user_handles,
+ int32_t num_handles, int64_t timeout_ns) {
+ LOG_TRACE(Kernel_SVC, "called user_handles={:#x}, num_handles={}, timeout_ns={}", user_handles,
+ num_handles, timeout_ns);
+
// Ensure number of handles is valid.
R_UNLESS(0 <= num_handles && num_handles <= Svc::ArgumentHandleCountMax, ResultOutOfRange);
// Get the synchronization context.
auto& kernel = system.Kernel();
auto& handle_table = GetCurrentProcess(kernel).GetHandleTable();
- std::array<KSynchronizationObject*, Svc::ArgumentHandleCountMax> objs;
+ auto objs = GetCurrentThread(kernel).GetSynchronizationObjectBuffer();
+ auto handles = GetCurrentThread(kernel).GetHandleBuffer();
// Copy user handles.
if (num_handles > 0) {
+ // Ensure we can try to get the handles.
+ R_UNLESS(GetCurrentMemory(kernel).IsValidVirtualAddressRange(
+ user_handles, static_cast<u64>(sizeof(Handle) * num_handles)),
+ ResultInvalidPointer);
+
+ // Get the handles.
+ GetCurrentMemory(kernel).ReadBlock(user_handles, handles.data(),
+ sizeof(Handle) * num_handles);
+
// Convert the handles to objects.
- R_UNLESS(handle_table.GetMultipleObjects<KSynchronizationObject>(objs.data(), handles,
- num_handles),
+ R_UNLESS(handle_table.GetMultipleObjects<KSynchronizationObject>(
+ objs.data(), handles.data(), num_handles),
ResultInvalidHandle);
}
@@ -80,23 +94,6 @@ static Result WaitSynchronization(Core::System& system, int32_t* out_index, cons
R_RETURN(res);
}
-/// Wait for the given handles to synchronize, timeout after the specified nanoseconds
-Result WaitSynchronization(Core::System& system, int32_t* out_index, u64 user_handles,
- int32_t num_handles, int64_t timeout_ns) {
- LOG_TRACE(Kernel_SVC, "called user_handles={:#x}, num_handles={}, timeout_ns={}", user_handles,
- num_handles, timeout_ns);
-
- // Ensure number of handles is valid.
- R_UNLESS(0 <= num_handles && num_handles <= Svc::ArgumentHandleCountMax, ResultOutOfRange);
- std::array<Handle, Svc::ArgumentHandleCountMax> handles;
- if (num_handles > 0) {
- GetCurrentMemory(system.Kernel())
- .ReadBlock(user_handles, handles.data(), num_handles * sizeof(Handle));
- }
-
- R_RETURN(WaitSynchronization(system, out_index, handles.data(), num_handles, timeout_ns));
-}
-
/// Resumes a thread waiting on WaitSynchronization
Result CancelSynchronization(Core::System& system, Handle handle) {
LOG_TRACE(Kernel_SVC, "called handle=0x{:X}", handle);
diff --git a/src/core/hle/service/audio/audin_u.cpp b/src/core/hle/service/audio/audin_u.cpp
index c8d574993..526a39130 100644
--- a/src/core/hle/service/audio/audin_u.cpp
+++ b/src/core/hle/service/audio/audin_u.cpp
@@ -5,7 +5,7 @@
#include "audio_core/renderer/audio_device.h"
#include "common/common_funcs.h"
#include "common/logging/log.h"
-#include "common/settings.h"
+#include "common/scratch_buffer.h"
#include "common/string_util.h"
#include "core/core.h"
#include "core/hle/kernel/k_event.h"
@@ -124,12 +124,15 @@ private:
void GetReleasedAudioInBuffer(HLERequestContext& ctx) {
const auto write_buffer_size = ctx.GetWriteBufferNumElements<u64>();
- tmp_buffer.resize_destructive(write_buffer_size);
- tmp_buffer[0] = 0;
+ released_buffer.resize_destructive(write_buffer_size);
+ released_buffer[0] = 0;
- const auto count = impl->GetReleasedBuffers(tmp_buffer);
+ const auto count = impl->GetReleasedBuffers(released_buffer);
- ctx.WriteBuffer(tmp_buffer);
+ LOG_TRACE(Service_Audio, "called. Session {} released {} buffers",
+ impl->GetSystem().GetSessionId(), count);
+
+ ctx.WriteBuffer(released_buffer);
IPC::ResponseBuilder rb{ctx, 3};
rb.Push(ResultSuccess);
@@ -155,7 +158,6 @@ private:
LOG_DEBUG(Service_Audio, "called. Buffer count={}", buffer_count);
IPC::ResponseBuilder rb{ctx, 3};
-
rb.Push(ResultSuccess);
rb.Push(buffer_count);
}
@@ -195,7 +197,7 @@ private:
KernelHelpers::ServiceContext service_context;
Kernel::KEvent* event;
std::shared_ptr<AudioCore::AudioIn::In> impl;
- Common::ScratchBuffer<u64> tmp_buffer;
+ Common::ScratchBuffer<u64> released_buffer;
};
AudInU::AudInU(Core::System& system_)
diff --git a/src/core/hle/service/audio/audout_u.cpp b/src/core/hle/service/audio/audout_u.cpp
index 032c8c11f..23f84a29f 100644
--- a/src/core/hle/service/audio/audout_u.cpp
+++ b/src/core/hle/service/audio/audout_u.cpp
@@ -9,6 +9,7 @@
#include "audio_core/renderer/audio_device.h"
#include "common/common_funcs.h"
#include "common/logging/log.h"
+#include "common/scratch_buffer.h"
#include "common/string_util.h"
#include "common/swap.h"
#include "core/core.h"
@@ -102,8 +103,8 @@ private:
AudioOutBuffer buffer{};
std::memcpy(&buffer, in_buffer.data(), sizeof(AudioOutBuffer));
- [[maybe_unused]] auto sessionid{impl->GetSystem().GetSessionId()};
- LOG_TRACE(Service_Audio, "called. Session {} Appending buffer {:08X}", sessionid, tag);
+ LOG_TRACE(Service_Audio, "called. Session {} Appending buffer {:08X}",
+ impl->GetSystem().GetSessionId(), tag);
auto result = impl->AppendBuffer(buffer, tag);
@@ -123,12 +124,15 @@ private:
void GetReleasedAudioOutBuffers(HLERequestContext& ctx) {
const auto write_buffer_size = ctx.GetWriteBufferNumElements<u64>();
- tmp_buffer.resize_destructive(write_buffer_size);
- tmp_buffer[0] = 0;
+ released_buffer.resize_destructive(write_buffer_size);
+ released_buffer[0] = 0;
- const auto count = impl->GetReleasedBuffers(tmp_buffer);
+ const auto count = impl->GetReleasedBuffers(released_buffer);
- ctx.WriteBuffer(tmp_buffer);
+ ctx.WriteBuffer(released_buffer);
+
+ LOG_TRACE(Service_Audio, "called. Session {} released {} buffers",
+ impl->GetSystem().GetSessionId(), count);
IPC::ResponseBuilder rb{ctx, 3};
rb.Push(ResultSuccess);
@@ -154,7 +158,6 @@ private:
LOG_DEBUG(Service_Audio, "called. Buffer count={}", buffer_count);
IPC::ResponseBuilder rb{ctx, 3};
-
rb.Push(ResultSuccess);
rb.Push(buffer_count);
}
@@ -165,7 +168,6 @@ private:
LOG_DEBUG(Service_Audio, "called. Played samples={}", samples_played);
IPC::ResponseBuilder rb{ctx, 4};
-
rb.Push(ResultSuccess);
rb.Push(samples_played);
}
@@ -205,7 +207,7 @@ private:
KernelHelpers::ServiceContext service_context;
Kernel::KEvent* event;
std::shared_ptr<AudioCore::AudioOut::Out> impl;
- Common::ScratchBuffer<u64> tmp_buffer;
+ Common::ScratchBuffer<u64> released_buffer;
};
AudOutU::AudOutU(Core::System& system_)
diff --git a/src/core/hle/service/audio/audren_u.cpp b/src/core/hle/service/audio/audren_u.cpp
index 12845c23a..003870176 100644
--- a/src/core/hle/service/audio/audren_u.cpp
+++ b/src/core/hle/service/audio/audren_u.cpp
@@ -15,6 +15,7 @@
#include "common/common_funcs.h"
#include "common/logging/log.h"
#include "common/polyfill_ranges.h"
+#include "common/scratch_buffer.h"
#include "common/string_util.h"
#include "core/core.h"
#include "core/hle/kernel/k_event.h"
@@ -119,23 +120,23 @@ private:
auto is_buffer_b{ctx.BufferDescriptorB()[0].Size() != 0};
if (is_buffer_b) {
const auto buffersB{ctx.BufferDescriptorB()};
- tmp_output.resize_destructive(buffersB[0].Size());
- tmp_performance.resize_destructive(buffersB[1].Size());
+ output_buffer.resize_destructive(buffersB[0].Size());
+ performance_buffer.resize_destructive(buffersB[1].Size());
} else {
const auto buffersC{ctx.BufferDescriptorC()};
- tmp_output.resize_destructive(buffersC[0].Size());
- tmp_performance.resize_destructive(buffersC[1].Size());
+ output_buffer.resize_destructive(buffersC[0].Size());
+ performance_buffer.resize_destructive(buffersC[1].Size());
}
- auto result = impl->RequestUpdate(input, tmp_performance, tmp_output);
+ auto result = impl->RequestUpdate(input, performance_buffer, output_buffer);
if (result.IsSuccess()) {
if (is_buffer_b) {
- ctx.WriteBufferB(tmp_output.data(), tmp_output.size(), 0);
- ctx.WriteBufferB(tmp_performance.data(), tmp_performance.size(), 1);
+ ctx.WriteBufferB(output_buffer.data(), output_buffer.size(), 0);
+ ctx.WriteBufferB(performance_buffer.data(), performance_buffer.size(), 1);
} else {
- ctx.WriteBufferC(tmp_output.data(), tmp_output.size(), 0);
- ctx.WriteBufferC(tmp_performance.data(), tmp_performance.size(), 1);
+ ctx.WriteBufferC(output_buffer.data(), output_buffer.size(), 0);
+ ctx.WriteBufferC(performance_buffer.data(), performance_buffer.size(), 1);
}
} else {
LOG_ERROR(Service_Audio, "RequestUpdate failed error 0x{:02X}!", result.description);
@@ -233,8 +234,8 @@ private:
Kernel::KEvent* rendered_event;
Manager& manager;
std::unique_ptr<Renderer> impl;
- Common::ScratchBuffer<u8> tmp_output;
- Common::ScratchBuffer<u8> tmp_performance;
+ Common::ScratchBuffer<u8> output_buffer;
+ Common::ScratchBuffer<u8> performance_buffer;
};
class IAudioDevice final : public ServiceFramework<IAudioDevice> {
diff --git a/src/core/hle/service/audio/hwopus.cpp b/src/core/hle/service/audio/hwopus.cpp
index c835f6cb7..fa77007f3 100644
--- a/src/core/hle/service/audio/hwopus.cpp
+++ b/src/core/hle/service/audio/hwopus.cpp
@@ -11,6 +11,7 @@
#include "common/assert.h"
#include "common/logging/log.h"
+#include "common/scratch_buffer.h"
#include "core/hle/service/audio/hwopus.h"
#include "core/hle/service/ipc_helpers.h"
@@ -68,13 +69,13 @@ private:
ExtraBehavior extra_behavior) {
u32 consumed = 0;
u32 sample_count = 0;
- tmp_samples.resize_destructive(ctx.GetWriteBufferNumElements<opus_int16>());
+ samples.resize_destructive(ctx.GetWriteBufferNumElements<opus_int16>());
if (extra_behavior == ExtraBehavior::ResetContext) {
ResetDecoderContext();
}
- if (!DecodeOpusData(consumed, sample_count, ctx.ReadBuffer(), tmp_samples, performance)) {
+ if (!DecodeOpusData(consumed, sample_count, ctx.ReadBuffer(), samples, performance)) {
LOG_ERROR(Audio, "Failed to decode opus data");
IPC::ResponseBuilder rb{ctx, 2};
// TODO(ogniK): Use correct error code
@@ -90,7 +91,7 @@ private:
if (performance) {
rb.Push<u64>(*performance);
}
- ctx.WriteBuffer(tmp_samples);
+ ctx.WriteBuffer(samples);
}
bool DecodeOpusData(u32& consumed, u32& sample_count, std::span<const u8> input,
@@ -154,7 +155,7 @@ private:
OpusDecoderPtr decoder;
u32 sample_rate;
u32 channel_count;
- Common::ScratchBuffer<opus_int16> tmp_samples;
+ Common::ScratchBuffer<opus_int16> samples;
};
class IHardwareOpusDecoderManager final : public ServiceFramework<IHardwareOpusDecoderManager> {
diff --git a/src/core/hle/service/hle_ipc.cpp b/src/core/hle/service/hle_ipc.cpp
index 2290df705..f6a1e54f2 100644
--- a/src/core/hle/service/hle_ipc.cpp
+++ b/src/core/hle/service/hle_ipc.cpp
@@ -329,8 +329,22 @@ std::vector<u8> HLERequestContext::ReadBufferCopy(std::size_t buffer_index) cons
}
std::span<const u8> HLERequestContext::ReadBuffer(std::size_t buffer_index) const {
- static thread_local std::array<Common::ScratchBuffer<u8>, 2> read_buffer_a;
- static thread_local std::array<Common::ScratchBuffer<u8>, 2> read_buffer_x;
+ static thread_local std::array read_buffer_a{
+ Core::Memory::CpuGuestMemory<u8, Core::Memory::GuestMemoryFlags::SafeRead>(memory, 0, 0),
+ Core::Memory::CpuGuestMemory<u8, Core::Memory::GuestMemoryFlags::SafeRead>(memory, 0, 0),
+ };
+ static thread_local std::array read_buffer_data_a{
+ Common::ScratchBuffer<u8>(),
+ Common::ScratchBuffer<u8>(),
+ };
+ static thread_local std::array read_buffer_x{
+ Core::Memory::CpuGuestMemory<u8, Core::Memory::GuestMemoryFlags::SafeRead>(memory, 0, 0),
+ Core::Memory::CpuGuestMemory<u8, Core::Memory::GuestMemoryFlags::SafeRead>(memory, 0, 0),
+ };
+ static thread_local std::array read_buffer_data_x{
+ Common::ScratchBuffer<u8>(),
+ Common::ScratchBuffer<u8>(),
+ };
const bool is_buffer_a{BufferDescriptorA().size() > buffer_index &&
BufferDescriptorA()[buffer_index].Size()};
@@ -339,19 +353,17 @@ std::span<const u8> HLERequestContext::ReadBuffer(std::size_t buffer_index) cons
BufferDescriptorA().size() > buffer_index, { return {}; },
"BufferDescriptorA invalid buffer_index {}", buffer_index);
auto& read_buffer = read_buffer_a[buffer_index];
- read_buffer.resize_destructive(BufferDescriptorA()[buffer_index].Size());
- memory.ReadBlock(BufferDescriptorA()[buffer_index].Address(), read_buffer.data(),
- read_buffer.size());
- return read_buffer;
+ return read_buffer.Read(BufferDescriptorA()[buffer_index].Address(),
+ BufferDescriptorA()[buffer_index].Size(),
+ &read_buffer_data_a[buffer_index]);
} else {
ASSERT_OR_EXECUTE_MSG(
BufferDescriptorX().size() > buffer_index, { return {}; },
"BufferDescriptorX invalid buffer_index {}", buffer_index);
auto& read_buffer = read_buffer_x[buffer_index];
- read_buffer.resize_destructive(BufferDescriptorX()[buffer_index].Size());
- memory.ReadBlock(BufferDescriptorX()[buffer_index].Address(), read_buffer.data(),
- read_buffer.size());
- return read_buffer;
+ return read_buffer.Read(BufferDescriptorX()[buffer_index].Address(),
+ BufferDescriptorX()[buffer_index].Size(),
+ &read_buffer_data_x[buffer_index]);
}
}
diff --git a/src/core/hle/service/nfc/common/device.cpp b/src/core/hle/service/nfc/common/device.cpp
index 5bf289818..2d633b03f 100644
--- a/src/core/hle/service/nfc/common/device.cpp
+++ b/src/core/hle/service/nfc/common/device.cpp
@@ -66,10 +66,6 @@ NfcDevice::~NfcDevice() {
};
void NfcDevice::NpadUpdate(Core::HID::ControllerTriggerType type) {
- if (!is_initalized) {
- return;
- }
-
if (type == Core::HID::ControllerTriggerType::Connected) {
Initialize();
availability_change_event->Signal();
@@ -77,12 +73,12 @@ void NfcDevice::NpadUpdate(Core::HID::ControllerTriggerType type) {
}
if (type == Core::HID::ControllerTriggerType::Disconnected) {
- device_state = DeviceState::Unavailable;
+ Finalize();
availability_change_event->Signal();
return;
}
- if (type != Core::HID::ControllerTriggerType::Nfc) {
+ if (!is_initalized) {
return;
}
@@ -90,6 +86,17 @@ void NfcDevice::NpadUpdate(Core::HID::ControllerTriggerType type) {
return;
}
+ // Ensure nfc mode is always active
+ if (npad_device->GetPollingMode(Core::HID::EmulatedDeviceIndex::RightIndex) ==
+ Common::Input::PollingMode::Active) {
+ npad_device->SetPollingMode(Core::HID::EmulatedDeviceIndex::RightIndex,
+ Common::Input::PollingMode::NFC);
+ }
+
+ if (type != Core::HID::ControllerTriggerType::Nfc) {
+ return;
+ }
+
const auto nfc_status = npad_device->GetNfc();
switch (nfc_status.state) {
case Common::Input::NfcState::NewAmiibo:
@@ -207,11 +214,14 @@ void NfcDevice::Initialize() {
}
void NfcDevice::Finalize() {
- if (device_state == DeviceState::TagMounted) {
- Unmount();
- }
- if (device_state == DeviceState::SearchingForTag || device_state == DeviceState::TagRemoved) {
- StopDetection();
+ if (npad_device->IsConnected()) {
+ if (device_state == DeviceState::TagMounted) {
+ Unmount();
+ }
+ if (device_state == DeviceState::SearchingForTag ||
+ device_state == DeviceState::TagRemoved) {
+ StopDetection();
+ }
}
if (device_state != DeviceState::Unavailable) {
diff --git a/src/core/hle/service/nvdrv/nvdrv_interface.cpp b/src/core/hle/service/nvdrv/nvdrv_interface.cpp
index 348207e25..c8a880e84 100644
--- a/src/core/hle/service/nvdrv/nvdrv_interface.cpp
+++ b/src/core/hle/service/nvdrv/nvdrv_interface.cpp
@@ -2,7 +2,6 @@
// SPDX-FileCopyrightText: 2021 Skyline Team and Contributors
// SPDX-License-Identifier: GPL-3.0-or-later
-#include <cinttypes>
#include "common/logging/log.h"
#include "core/core.h"
#include "core/hle/kernel/k_event.h"
@@ -63,12 +62,12 @@ void NVDRV::Ioctl1(HLERequestContext& ctx) {
}
// Check device
- tmp_output.resize_destructive(ctx.GetWriteBufferSize(0));
+ output_buffer.resize_destructive(ctx.GetWriteBufferSize(0));
const auto input_buffer = ctx.ReadBuffer(0);
- const auto nv_result = nvdrv->Ioctl1(fd, command, input_buffer, tmp_output);
+ const auto nv_result = nvdrv->Ioctl1(fd, command, input_buffer, output_buffer);
if (command.is_out != 0) {
- ctx.WriteBuffer(tmp_output);
+ ctx.WriteBuffer(output_buffer);
}
IPC::ResponseBuilder rb{ctx, 3};
@@ -90,12 +89,12 @@ void NVDRV::Ioctl2(HLERequestContext& ctx) {
const auto input_buffer = ctx.ReadBuffer(0);
const auto input_inlined_buffer = ctx.ReadBuffer(1);
- tmp_output.resize_destructive(ctx.GetWriteBufferSize(0));
+ output_buffer.resize_destructive(ctx.GetWriteBufferSize(0));
const auto nv_result =
- nvdrv->Ioctl2(fd, command, input_buffer, input_inlined_buffer, tmp_output);
+ nvdrv->Ioctl2(fd, command, input_buffer, input_inlined_buffer, output_buffer);
if (command.is_out != 0) {
- ctx.WriteBuffer(tmp_output);
+ ctx.WriteBuffer(output_buffer);
}
IPC::ResponseBuilder rb{ctx, 3};
@@ -116,12 +115,14 @@ void NVDRV::Ioctl3(HLERequestContext& ctx) {
}
const auto input_buffer = ctx.ReadBuffer(0);
- tmp_output.resize_destructive(ctx.GetWriteBufferSize(0));
- tmp_output_inline.resize_destructive(ctx.GetWriteBufferSize(1));
- const auto nv_result = nvdrv->Ioctl3(fd, command, input_buffer, tmp_output, tmp_output_inline);
+ output_buffer.resize_destructive(ctx.GetWriteBufferSize(0));
+ inline_output_buffer.resize_destructive(ctx.GetWriteBufferSize(1));
+
+ const auto nv_result =
+ nvdrv->Ioctl3(fd, command, input_buffer, output_buffer, inline_output_buffer);
if (command.is_out != 0) {
- ctx.WriteBuffer(tmp_output, 0);
- ctx.WriteBuffer(tmp_output_inline, 1);
+ ctx.WriteBuffer(output_buffer, 0);
+ ctx.WriteBuffer(inline_output_buffer, 1);
}
IPC::ResponseBuilder rb{ctx, 3};
diff --git a/src/core/hle/service/nvdrv/nvdrv_interface.h b/src/core/hle/service/nvdrv/nvdrv_interface.h
index 4b593ff90..6e98115dc 100644
--- a/src/core/hle/service/nvdrv/nvdrv_interface.h
+++ b/src/core/hle/service/nvdrv/nvdrv_interface.h
@@ -4,6 +4,7 @@
#pragma once
#include <memory>
+
#include "common/scratch_buffer.h"
#include "core/hle/service/nvdrv/nvdrv.h"
#include "core/hle/service/service.h"
@@ -34,8 +35,8 @@ private:
u64 pid{};
bool is_initialized{};
- Common::ScratchBuffer<u8> tmp_output;
- Common::ScratchBuffer<u8> tmp_output_inline;
+ Common::ScratchBuffer<u8> output_buffer;
+ Common::ScratchBuffer<u8> inline_output_buffer;
};
} // namespace Service::Nvidia
diff --git a/src/core/hle/service/nvnflinger/parcel.h b/src/core/hle/service/nvnflinger/parcel.h
index 23ba315a0..e2c9bbd50 100644
--- a/src/core/hle/service/nvnflinger/parcel.h
+++ b/src/core/hle/service/nvnflinger/parcel.h
@@ -6,6 +6,7 @@
#include <memory>
#include <span>
#include <vector>
+
#include <boost/container/small_vector.hpp>
#include "common/alignment.h"
@@ -148,9 +149,9 @@ public:
this->WriteImpl(0U, m_object_buffer);
}
- std::vector<u8> Serialize() const {
- std::vector<u8> output_buffer(sizeof(ParcelHeader) + m_data_buffer.size() +
- m_object_buffer.size());
+ std::span<u8> Serialize() {
+ m_output_buffer.resize(sizeof(ParcelHeader) + m_data_buffer.size() +
+ m_object_buffer.size());
ParcelHeader header{};
header.data_size = static_cast<u32>(m_data_buffer.size());
@@ -158,17 +159,17 @@ public:
header.objects_size = static_cast<u32>(m_object_buffer.size());
header.objects_offset = header.data_offset + header.data_size;
- std::memcpy(output_buffer.data(), &header, sizeof(header));
- std::ranges::copy(m_data_buffer, output_buffer.data() + header.data_offset);
- std::ranges::copy(m_object_buffer, output_buffer.data() + header.objects_offset);
+ std::memcpy(m_output_buffer.data(), &header, sizeof(ParcelHeader));
+ std::ranges::copy(m_data_buffer, m_output_buffer.data() + header.data_offset);
+ std::ranges::copy(m_object_buffer, m_output_buffer.data() + header.objects_offset);
- return output_buffer;
+ return m_output_buffer;
}
private:
- template <typename T>
+ template <typename T, size_t BufferSize>
requires(std::is_trivially_copyable_v<T>)
- void WriteImpl(const T& val, boost::container::small_vector<u8, 0x200>& buffer) {
+ void WriteImpl(const T& val, boost::container::small_vector<u8, BufferSize>& buffer) {
const size_t aligned_size = Common::AlignUp(sizeof(T), 4);
const size_t old_size = buffer.size();
buffer.resize(old_size + aligned_size);
@@ -177,8 +178,9 @@ private:
}
private:
- boost::container::small_vector<u8, 0x200> m_data_buffer;
- boost::container::small_vector<u8, 0x200> m_object_buffer;
+ boost::container::small_vector<u8, 0x1B0> m_data_buffer;
+ boost::container::small_vector<u8, 0x40> m_object_buffer;
+ boost::container::small_vector<u8, 0x200> m_output_buffer;
};
} // namespace Service::android
diff --git a/src/core/loader/loader.h b/src/core/loader/loader.h
index 7b43f70ed..7a2a52fd4 100644
--- a/src/core/loader/loader.h
+++ b/src/core/loader/loader.h
@@ -79,6 +79,8 @@ enum class ResultStatus : u16 {
ErrorBadPFSHeader,
ErrorIncorrectPFSFileSize,
ErrorBadNCAHeader,
+ ErrorCompressedNCA,
+ ErrorSparseNCA,
ErrorMissingProductionKeyFile,
ErrorMissingHeaderKey,
ErrorIncorrectHeaderKey,
diff --git a/src/core/memory.cpp b/src/core/memory.cpp
index 514ba0d66..805963178 100644
--- a/src/core/memory.cpp
+++ b/src/core/memory.cpp
@@ -3,6 +3,7 @@
#include <algorithm>
#include <cstring>
+#include <span>
#include "common/assert.h"
#include "common/atomic_ops.h"
@@ -13,6 +14,7 @@
#include "common/swap.h"
#include "core/core.h"
#include "core/device_memory.h"
+#include "core/gpu_dirty_memory_manager.h"
#include "core/hardware_properties.h"
#include "core/hle/kernel/k_page_table.h"
#include "core/hle/kernel/k_process.h"
@@ -264,6 +266,22 @@ struct Memory::Impl {
ReadBlockImpl<true>(*system.ApplicationProcess(), src_addr, dest_buffer, size);
}
+ const u8* GetSpan(const VAddr src_addr, const std::size_t size) const {
+ if (current_page_table->blocks[src_addr >> YUZU_PAGEBITS] ==
+ current_page_table->blocks[(src_addr + size) >> YUZU_PAGEBITS]) {
+ return GetPointerSilent(src_addr);
+ }
+ return nullptr;
+ }
+
+ u8* GetSpan(const VAddr src_addr, const std::size_t size) {
+ if (current_page_table->blocks[src_addr >> YUZU_PAGEBITS] ==
+ current_page_table->blocks[(src_addr + size) >> YUZU_PAGEBITS]) {
+ return GetPointerSilent(src_addr);
+ }
+ return nullptr;
+ }
+
template <bool UNSAFE>
void WriteBlockImpl(const Kernel::KProcess& process, const Common::ProcessAddress dest_addr,
const void* src_buffer, const std::size_t size) {
@@ -557,7 +575,7 @@ struct Memory::Impl {
}
}
- const Common::ProcessAddress end = base + size;
+ const auto end = base + size;
ASSERT_MSG(end <= page_table.pointers.size(), "out of range mapping at {:016X}",
base + page_table.pointers.size());
@@ -568,14 +586,18 @@ struct Memory::Impl {
while (base != end) {
page_table.pointers[base].Store(nullptr, type);
page_table.backing_addr[base] = 0;
-
+ page_table.blocks[base] = 0;
base += 1;
}
} else {
+ auto orig_base = base;
while (base != end) {
- page_table.pointers[base].Store(
- system.DeviceMemory().GetPointer<u8>(target) - (base << YUZU_PAGEBITS), type);
- page_table.backing_addr[base] = GetInteger(target) - (base << YUZU_PAGEBITS);
+ auto host_ptr =
+ system.DeviceMemory().GetPointer<u8>(target) - (base << YUZU_PAGEBITS);
+ auto backing = GetInteger(target) - (base << YUZU_PAGEBITS);
+ page_table.pointers[base].Store(host_ptr, type);
+ page_table.backing_addr[base] = backing;
+ page_table.blocks[base] = orig_base << YUZU_PAGEBITS;
ASSERT_MSG(page_table.pointers[base].Pointer(),
"memory mapping base yield a nullptr within the table");
@@ -678,7 +700,7 @@ struct Memory::Impl {
LOG_ERROR(HW_Memory, "Unmapped Write{} @ 0x{:016X} = 0x{:016X}", sizeof(T) * 8,
GetInteger(vaddr), static_cast<u64>(data));
},
- [&]() { system.GPU().InvalidateRegion(GetInteger(vaddr), sizeof(T)); });
+ [&]() { HandleRasterizerWrite(GetInteger(vaddr), sizeof(T)); });
if (ptr) {
std::memcpy(ptr, &data, sizeof(T));
}
@@ -692,7 +714,7 @@ struct Memory::Impl {
LOG_ERROR(HW_Memory, "Unmapped WriteExclusive{} @ 0x{:016X} = 0x{:016X}",
sizeof(T) * 8, GetInteger(vaddr), static_cast<u64>(data));
},
- [&]() { system.GPU().InvalidateRegion(GetInteger(vaddr), sizeof(T)); });
+ [&]() { HandleRasterizerWrite(GetInteger(vaddr), sizeof(T)); });
if (ptr) {
const auto volatile_pointer = reinterpret_cast<volatile T*>(ptr);
return Common::AtomicCompareAndSwap(volatile_pointer, data, expected);
@@ -707,7 +729,7 @@ struct Memory::Impl {
LOG_ERROR(HW_Memory, "Unmapped WriteExclusive128 @ 0x{:016X} = 0x{:016X}{:016X}",
GetInteger(vaddr), static_cast<u64>(data[1]), static_cast<u64>(data[0]));
},
- [&]() { system.GPU().InvalidateRegion(GetInteger(vaddr), sizeof(u128)); });
+ [&]() { HandleRasterizerWrite(GetInteger(vaddr), sizeof(u128)); });
if (ptr) {
const auto volatile_pointer = reinterpret_cast<volatile u64*>(ptr);
return Common::AtomicCompareAndSwap(volatile_pointer, data, expected);
@@ -717,7 +739,7 @@ struct Memory::Impl {
void HandleRasterizerDownload(VAddr address, size_t size) {
const size_t core = system.GetCurrentHostThreadID();
- auto& current_area = rasterizer_areas[core];
+ auto& current_area = rasterizer_read_areas[core];
const VAddr end_address = address + size;
if (current_area.start_address <= address && end_address <= current_area.end_address)
[[likely]] {
@@ -726,9 +748,39 @@ struct Memory::Impl {
current_area = system.GPU().OnCPURead(address, size);
}
- Common::PageTable* current_page_table = nullptr;
- std::array<VideoCore::RasterizerDownloadArea, Core::Hardware::NUM_CPU_CORES> rasterizer_areas{};
+ void HandleRasterizerWrite(VAddr address, size_t size) {
+ const size_t core = system.GetCurrentHostThreadID();
+ auto& current_area = rasterizer_write_areas[core];
+ VAddr subaddress = address >> YUZU_PAGEBITS;
+ bool do_collection = current_area.last_address == subaddress;
+ if (!do_collection) [[unlikely]] {
+ do_collection = system.GPU().OnCPUWrite(address, size);
+ if (!do_collection) {
+ return;
+ }
+ current_area.last_address = subaddress;
+ }
+ gpu_dirty_managers[core].Collect(address, size);
+ }
+
+ struct GPUDirtyState {
+ VAddr last_address;
+ };
+
+ void InvalidateRegion(Common::ProcessAddress dest_addr, size_t size) {
+ system.GPU().InvalidateRegion(GetInteger(dest_addr), size);
+ }
+
+ void FlushRegion(Common::ProcessAddress dest_addr, size_t size) {
+ system.GPU().FlushRegion(GetInteger(dest_addr), size);
+ }
+
Core::System& system;
+ Common::PageTable* current_page_table = nullptr;
+ std::array<VideoCore::RasterizerDownloadArea, Core::Hardware::NUM_CPU_CORES>
+ rasterizer_read_areas{};
+ std::array<GPUDirtyState, Core::Hardware::NUM_CPU_CORES> rasterizer_write_areas{};
+ std::span<Core::GPUDirtyMemoryManager> gpu_dirty_managers;
};
Memory::Memory(Core::System& system_) : system{system_} {
@@ -857,6 +909,14 @@ void Memory::ReadBlockUnsafe(const Common::ProcessAddress src_addr, void* dest_b
impl->ReadBlockUnsafe(src_addr, dest_buffer, size);
}
+const u8* Memory::GetSpan(const VAddr src_addr, const std::size_t size) const {
+ return impl->GetSpan(src_addr, size);
+}
+
+u8* Memory::GetSpan(const VAddr src_addr, const std::size_t size) {
+ return impl->GetSpan(src_addr, size);
+}
+
void Memory::WriteBlock(const Common::ProcessAddress dest_addr, const void* src_buffer,
const std::size_t size) {
impl->WriteBlock(dest_addr, src_buffer, size);
@@ -876,6 +936,10 @@ void Memory::ZeroBlock(Common::ProcessAddress dest_addr, const std::size_t size)
impl->ZeroBlock(*system.ApplicationProcess(), dest_addr, size);
}
+void Memory::SetGPUDirtyManagers(std::span<Core::GPUDirtyMemoryManager> managers) {
+ impl->gpu_dirty_managers = managers;
+}
+
Result Memory::InvalidateDataCache(Common::ProcessAddress dest_addr, const std::size_t size) {
return impl->InvalidateDataCache(*system.ApplicationProcess(), dest_addr, size);
}
@@ -896,4 +960,12 @@ void Memory::MarkRegionDebug(Common::ProcessAddress vaddr, u64 size, bool debug)
impl->MarkRegionDebug(GetInteger(vaddr), size, debug);
}
+void Memory::InvalidateRegion(Common::ProcessAddress dest_addr, size_t size) {
+ impl->InvalidateRegion(dest_addr, size);
+}
+
+void Memory::FlushRegion(Common::ProcessAddress dest_addr, size_t size) {
+ impl->FlushRegion(dest_addr, size);
+}
+
} // namespace Core::Memory
diff --git a/src/core/memory.h b/src/core/memory.h
index 72a0be813..ea33c769c 100644
--- a/src/core/memory.h
+++ b/src/core/memory.h
@@ -5,7 +5,12 @@
#include <cstddef>
#include <memory>
+#include <optional>
+#include <span>
#include <string>
+#include <vector>
+
+#include "common/scratch_buffer.h"
#include "common/typed_address.h"
#include "core/hle/result.h"
@@ -15,13 +20,18 @@ struct PageTable;
namespace Core {
class System;
-}
+class GPUDirtyMemoryManager;
+} // namespace Core
namespace Kernel {
class PhysicalMemory;
class KProcess;
} // namespace Kernel
+namespace Tegra {
+class MemoryManager;
+}
+
namespace Core::Memory {
/**
@@ -341,6 +351,9 @@ public:
*/
void ReadBlockUnsafe(Common::ProcessAddress src_addr, void* dest_buffer, std::size_t size);
+ const u8* GetSpan(const VAddr src_addr, const std::size_t size) const;
+ u8* GetSpan(const VAddr src_addr, const std::size_t size);
+
/**
* Writes a range of bytes into the current process' address space at the specified
* virtual address.
@@ -458,6 +471,10 @@ public:
*/
void MarkRegionDebug(Common::ProcessAddress vaddr, u64 size, bool debug);
+ void SetGPUDirtyManagers(std::span<Core::GPUDirtyMemoryManager> managers);
+ void InvalidateRegion(Common::ProcessAddress dest_addr, size_t size);
+ void FlushRegion(Common::ProcessAddress dest_addr, size_t size);
+
private:
Core::System& system;
@@ -465,4 +482,203 @@ private:
std::unique_ptr<Impl> impl;
};
+enum GuestMemoryFlags : u32 {
+ Read = 1 << 0,
+ Write = 1 << 1,
+ Safe = 1 << 2,
+ Cached = 1 << 3,
+
+ SafeRead = Read | Safe,
+ SafeWrite = Write | Safe,
+ SafeReadWrite = SafeRead | SafeWrite,
+ SafeReadCachedWrite = SafeReadWrite | Cached,
+
+ UnsafeRead = Read,
+ UnsafeWrite = Write,
+ UnsafeReadWrite = UnsafeRead | UnsafeWrite,
+ UnsafeReadCachedWrite = UnsafeReadWrite | Cached,
+};
+
+namespace {
+template <typename M, typename T, GuestMemoryFlags FLAGS>
+class GuestMemory {
+ using iterator = T*;
+ using const_iterator = const T*;
+ using value_type = T;
+ using element_type = T;
+ using iterator_category = std::contiguous_iterator_tag;
+
+public:
+ GuestMemory() = delete;
+ explicit GuestMemory(M& memory_, u64 addr_, std::size_t size_,
+ Common::ScratchBuffer<T>* backup = nullptr)
+ : memory{memory_}, addr{addr_}, size{size_} {
+ static_assert(FLAGS & GuestMemoryFlags::Read || FLAGS & GuestMemoryFlags::Write);
+ if constexpr (FLAGS & GuestMemoryFlags::Read) {
+ Read(addr, size, backup);
+ }
+ }
+
+ ~GuestMemory() = default;
+
+ T* data() noexcept {
+ return data_span.data();
+ }
+
+ const T* data() const noexcept {
+ return data_span.data();
+ }
+
+ [[nodiscard]] T* begin() noexcept {
+ return data();
+ }
+
+ [[nodiscard]] const T* begin() const noexcept {
+ return data();
+ }
+
+ [[nodiscard]] T* end() noexcept {
+ return data() + size;
+ }
+
+ [[nodiscard]] const T* end() const noexcept {
+ return data() + size;
+ }
+
+ T& operator[](size_t index) noexcept {
+ return data_span[index];
+ }
+
+ const T& operator[](size_t index) const noexcept {
+ return data_span[index];
+ }
+
+ void SetAddressAndSize(u64 addr_, std::size_t size_) noexcept {
+ addr = addr_;
+ size = size_;
+ addr_changed = true;
+ }
+
+ std::span<T> Read(u64 addr_, std::size_t size_,
+ Common::ScratchBuffer<T>* backup = nullptr) noexcept {
+ addr = addr_;
+ size = size_;
+ if (size == 0) {
+ is_data_copy = true;
+ return {};
+ }
+
+ if (TrySetSpan()) {
+ if constexpr (FLAGS & GuestMemoryFlags::Safe) {
+ memory.FlushRegion(addr, size * sizeof(T));
+ }
+ } else {
+ if (backup) {
+ backup->resize_destructive(size);
+ data_span = *backup;
+ } else {
+ data_copy.resize(size);
+ data_span = std::span(data_copy);
+ }
+ is_data_copy = true;
+ span_valid = true;
+ if constexpr (FLAGS & GuestMemoryFlags::Safe) {
+ memory.ReadBlock(addr, data_span.data(), size * sizeof(T));
+ } else {
+ memory.ReadBlockUnsafe(addr, data_span.data(), size * sizeof(T));
+ }
+ }
+ return data_span;
+ }
+
+ void Write(std::span<T> write_data) noexcept {
+ if constexpr (FLAGS & GuestMemoryFlags::Cached) {
+ memory.WriteBlockCached(addr, write_data.data(), size * sizeof(T));
+ } else if constexpr (FLAGS & GuestMemoryFlags::Safe) {
+ memory.WriteBlock(addr, write_data.data(), size * sizeof(T));
+ } else {
+ memory.WriteBlockUnsafe(addr, write_data.data(), size * sizeof(T));
+ }
+ }
+
+ bool TrySetSpan() noexcept {
+ if (u8* ptr = memory.GetSpan(addr, size * sizeof(T)); ptr) {
+ data_span = {reinterpret_cast<T*>(ptr), size};
+ span_valid = true;
+ return true;
+ }
+ return false;
+ }
+
+protected:
+ bool IsDataCopy() const noexcept {
+ return is_data_copy;
+ }
+
+ bool AddressChanged() const noexcept {
+ return addr_changed;
+ }
+
+ M& memory;
+ u64 addr;
+ size_t size;
+ std::span<T> data_span{};
+ std::vector<T> data_copy;
+ bool span_valid{false};
+ bool is_data_copy{false};
+ bool addr_changed{false};
+};
+
+template <typename M, typename T, GuestMemoryFlags FLAGS>
+class GuestMemoryScoped : public GuestMemory<M, T, FLAGS> {
+public:
+ GuestMemoryScoped() = delete;
+ explicit GuestMemoryScoped(M& memory_, u64 addr_, std::size_t size_,
+ Common::ScratchBuffer<T>* backup = nullptr)
+ : GuestMemory<M, T, FLAGS>(memory_, addr_, size_, backup) {
+ if constexpr (!(FLAGS & GuestMemoryFlags::Read)) {
+ if (!this->TrySetSpan()) {
+ if (backup) {
+ this->data_span = *backup;
+ this->span_valid = true;
+ this->is_data_copy = true;
+ }
+ }
+ }
+ }
+
+ ~GuestMemoryScoped() {
+ if constexpr (FLAGS & GuestMemoryFlags::Write) {
+ if (this->size == 0) [[unlikely]] {
+ return;
+ }
+
+ if (this->AddressChanged() || this->IsDataCopy()) {
+ ASSERT(this->span_valid);
+ if constexpr (FLAGS & GuestMemoryFlags::Cached) {
+ this->memory.WriteBlockCached(this->addr, this->data_span.data(),
+ this->size * sizeof(T));
+ } else if constexpr (FLAGS & GuestMemoryFlags::Safe) {
+ this->memory.WriteBlock(this->addr, this->data_span.data(),
+ this->size * sizeof(T));
+ } else {
+ this->memory.WriteBlockUnsafe(this->addr, this->data_span.data(),
+ this->size * sizeof(T));
+ }
+ } else if constexpr (FLAGS & GuestMemoryFlags::Safe) {
+ this->memory.InvalidateRegion(this->addr, this->size * sizeof(T));
+ }
+ }
+ }
+};
+} // namespace
+
+template <typename T, GuestMemoryFlags FLAGS>
+using CpuGuestMemory = GuestMemory<Memory, T, FLAGS>;
+template <typename T, GuestMemoryFlags FLAGS>
+using CpuGuestMemoryScoped = GuestMemoryScoped<Memory, T, FLAGS>;
+template <typename T, GuestMemoryFlags FLAGS>
+using GpuGuestMemory = GuestMemory<Tegra::MemoryManager, T, FLAGS>;
+template <typename T, GuestMemoryFlags FLAGS>
+using GpuGuestMemoryScoped = GuestMemoryScoped<Tegra::MemoryManager, T, FLAGS>;
} // namespace Core::Memory