2025-06-26 18:55:34 +00:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2025 Eden Emulator Project
|
2025-06-21 19:32:32 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
2023-11-17 23:44:53 +02:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <span>
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <vector>
|
|
|
|
#include <oaknut/code_block.hpp>
|
|
|
|
#include <oaknut/oaknut.hpp>
|
|
|
|
|
|
|
|
#include "common/common_types.h"
|
2025-04-30 03:41:46 -03:00
|
|
|
#include "common/settings.h"
|
2023-11-17 23:44:53 +02:00
|
|
|
#include "core/hle/kernel/code_set.h"
|
|
|
|
#include "core/hle/kernel/k_typed_address.h"
|
|
|
|
#include "core/hle/kernel/physical_memory.h"
|
2025-04-02 02:11:32 -03:00
|
|
|
#include "lru_cache.h"
|
2025-06-21 19:32:32 +00:00
|
|
|
#include <utility>
|
2025-08-04 18:41:28 +02:00
|
|
|
using ModuleID = std::array<u8, 32>; // NSO build ID
|
|
|
|
struct PatchCacheKey {
|
|
|
|
ModuleID module_id;
|
|
|
|
uintptr_t offset;
|
|
|
|
bool operator==(const PatchCacheKey&) const = default;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct std::hash<PatchCacheKey> {
|
|
|
|
size_t operator()(const PatchCacheKey& key) const {
|
|
|
|
// Simple XOR hash of first few bytes
|
2025-09-26 21:46:56 +02:00
|
|
|
size_t hash_ = 0;
|
2025-08-04 18:41:28 +02:00
|
|
|
for (size_t i = 0; i < key.module_id.size(); ++i) {
|
2025-09-26 21:46:56 +02:00
|
|
|
hash_ ^= static_cast<size_t>(key.module_id[i]) << ((i % sizeof(size_t)) * 8);
|
2025-08-04 18:41:28 +02:00
|
|
|
}
|
2025-09-26 21:46:56 +02:00
|
|
|
return hash_ ^ std::hash<uintptr_t>{}(key.offset);
|
2025-08-04 18:41:28 +02:00
|
|
|
}
|
|
|
|
};
|
2023-11-17 23:44:53 +02:00
|
|
|
|
|
|
|
namespace Core::NCE {
|
|
|
|
|
|
|
|
enum class PatchMode : u32 {
|
|
|
|
None,
|
|
|
|
PreText, ///< Patch section is inserted before .text
|
|
|
|
PostData, ///< Patch section is inserted after .data
|
|
|
|
};
|
|
|
|
|
|
|
|
using ModuleTextAddress = u64;
|
|
|
|
using PatchTextAddress = u64;
|
|
|
|
using EntryTrampolines = std::unordered_map<ModuleTextAddress, PatchTextAddress>;
|
|
|
|
|
|
|
|
class Patcher {
|
|
|
|
public:
|
2025-08-04 18:41:28 +02:00
|
|
|
void SetModuleID(const ModuleID& id) {
|
|
|
|
module_id = id;
|
|
|
|
}
|
2025-06-21 19:32:32 +00:00
|
|
|
Patcher(const Patcher&) = delete;
|
|
|
|
Patcher& operator=(const Patcher&) = delete;
|
|
|
|
Patcher(Patcher&& other) noexcept;
|
|
|
|
Patcher& operator=(Patcher&&) noexcept = delete;
|
2023-11-17 23:44:53 +02:00
|
|
|
explicit Patcher();
|
|
|
|
~Patcher();
|
2024-01-03 23:37:41 +02:00
|
|
|
bool PatchText(const Kernel::PhysicalMemory& program_image,
|
2023-11-17 23:44:53 +02:00
|
|
|
const Kernel::CodeSet::Segment& code);
|
2024-01-03 23:37:41 +02:00
|
|
|
bool RelocateAndCopy(Common::ProcessAddress load_base, const Kernel::CodeSet::Segment& code,
|
2023-11-17 23:44:53 +02:00
|
|
|
Kernel::PhysicalMemory& program_image, EntryTrampolines* out_trampolines);
|
2023-11-19 11:21:53 +02:00
|
|
|
size_t GetSectionSize() const noexcept;
|
2023-11-17 23:44:53 +02:00
|
|
|
|
2023-11-19 11:21:53 +02:00
|
|
|
[[nodiscard]] PatchMode GetPatchMode() const noexcept {
|
2023-11-17 23:44:53 +02:00
|
|
|
return mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
using ModuleDestLabel = uintptr_t;
|
2025-08-04 18:41:28 +02:00
|
|
|
ModuleID module_id{};
|
2023-11-17 23:44:53 +02:00
|
|
|
struct Trampoline {
|
|
|
|
ptrdiff_t patch_offset;
|
|
|
|
uintptr_t module_offset;
|
|
|
|
};
|
|
|
|
|
|
|
|
void WriteLoadContext();
|
|
|
|
void WriteSaveContext();
|
|
|
|
void LockContext();
|
|
|
|
void UnlockContext();
|
|
|
|
void WriteSvcTrampoline(ModuleDestLabel module_dest, u32 svc_id);
|
|
|
|
void WriteMrsHandler(ModuleDestLabel module_dest, oaknut::XReg dest_reg,
|
|
|
|
oaknut::SystemReg src_reg);
|
|
|
|
void WriteMsrHandler(ModuleDestLabel module_dest, oaknut::XReg src_reg);
|
|
|
|
void WriteCntpctHandler(ModuleDestLabel module_dest, oaknut::XReg dest_reg);
|
|
|
|
|
|
|
|
private:
|
2025-06-21 19:32:32 +00:00
|
|
|
static constexpr size_t CACHE_SIZE = 16384; // Cache size for patch entries
|
2025-08-04 18:41:28 +02:00
|
|
|
LRUCache<PatchCacheKey, PatchTextAddress> patch_cache{CACHE_SIZE, Settings::values.lru_cache_enabled.GetValue()};
|
2025-04-02 02:11:32 -03:00
|
|
|
|
2023-11-17 23:44:53 +02:00
|
|
|
void BranchToPatch(uintptr_t module_dest) {
|
2025-04-30 16:11:15 -03:00
|
|
|
if (patch_cache.isEnabled()) {
|
2025-08-04 18:41:28 +02:00
|
|
|
PatchCacheKey key{module_id, module_dest};
|
|
|
|
LOG_DEBUG(Core_ARM, "LRU cache lookup for module={}, offset={:#x}", fmt::ptr(module_id.data()), module_dest);
|
2025-04-30 16:11:15 -03:00
|
|
|
// Try to get existing patch entry from cache
|
2025-08-04 18:41:28 +02:00
|
|
|
if (auto* cached_patch = patch_cache.get(key)) {
|
|
|
|
LOG_WARNING(Core_ARM, "LRU cache hit for module offset {:#x}", module_dest);
|
2025-04-30 16:11:15 -03:00
|
|
|
curr_patch->m_branch_to_patch_relocations.push_back({c.offset(), *cached_patch});
|
|
|
|
return;
|
|
|
|
}
|
2025-08-04 18:41:28 +02:00
|
|
|
LOG_DEBUG(Core_ARM, "LRU cache miss for module offset {:#x}, creating new patch", module_dest);
|
|
|
|
// Not in cache: create and store
|
2025-04-30 16:11:15 -03:00
|
|
|
const auto patch_addr = c.offset();
|
|
|
|
curr_patch->m_branch_to_patch_relocations.push_back({patch_addr, module_dest});
|
2025-08-04 18:41:28 +02:00
|
|
|
patch_cache.put(key, patch_addr);
|
2025-04-30 16:11:15 -03:00
|
|
|
} else {
|
2025-08-04 18:41:28 +02:00
|
|
|
LOG_DEBUG(Core_ARM, "LRU cache disabled - direct patch for offset {:#x}", module_dest);
|
2025-04-30 16:11:15 -03:00
|
|
|
curr_patch->m_branch_to_patch_relocations.push_back({c.offset(), module_dest});
|
2025-04-02 02:11:32 -03:00
|
|
|
}
|
2023-11-17 23:44:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void BranchToModule(uintptr_t module_dest) {
|
2024-01-03 23:37:41 +02:00
|
|
|
curr_patch->m_branch_to_module_relocations.push_back({c.offset(), module_dest});
|
2023-11-17 23:44:53 +02:00
|
|
|
c.dw(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WriteModulePc(uintptr_t module_dest) {
|
2024-01-03 23:37:41 +02:00
|
|
|
curr_patch->m_write_module_pc_relocations.push_back({c.offset(), module_dest});
|
2023-11-17 23:44:53 +02:00
|
|
|
c.dx(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// List of patch instructions we have generated.
|
|
|
|
std::vector<u32> m_patch_instructions{};
|
|
|
|
|
|
|
|
// Relocation type for relative branch from module to patch.
|
|
|
|
struct Relocation {
|
|
|
|
ptrdiff_t patch_offset; ///< Offset in bytes from the start of the patch section.
|
|
|
|
uintptr_t module_offset; ///< Offset in bytes from the start of the text section.
|
|
|
|
};
|
|
|
|
|
2024-01-03 23:37:41 +02:00
|
|
|
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{};
|
|
|
|
};
|
|
|
|
|
2023-11-17 23:44:53 +02:00
|
|
|
oaknut::VectorCodeGenerator c;
|
|
|
|
oaknut::Label m_save_context{};
|
|
|
|
oaknut::Label m_load_context{};
|
|
|
|
PatchMode mode{PatchMode::None};
|
2024-01-03 23:37:41 +02:00
|
|
|
size_t total_program_size{};
|
|
|
|
size_t m_relocate_module_index{};
|
|
|
|
std::vector<ModulePatch> modules;
|
|
|
|
ModulePatch* curr_patch;
|
2023-11-17 23:44:53 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace Core::NCE
|