summaryrefslogtreecommitdiffstats
path: root/src/core/arm/nce/patch.h
blob: b727d4e488e498eb3054bbac9de5a9c605b0a38c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#pragma once

#include <span>
#include <unordered_map>
#include <vector>

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wshorten-64-to-32"
#include <oaknut/code_block.hpp>
#include <oaknut/oaknut.hpp>
#pragma clang diagnostic pop

#include "common/common_types.h"
#include "core/hle/kernel/code_set.h"
#include "core/hle/kernel/k_typed_address.h"
#include "core/hle/kernel/physical_memory.h"

#include <signal.h>

namespace Core {
struct GuestContext;
}

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:
    explicit Patcher();
    ~Patcher();

    void PatchText(const Kernel::PhysicalMemory& program_image,
                   const Kernel::CodeSet::Segment& code);
    void RelocateAndCopy(Common::ProcessAddress load_base, const Kernel::CodeSet::Segment& code,
                         Kernel::PhysicalMemory& program_image, EntryTrampolines* out_trampolines);
    size_t SectionSize() const noexcept;

    [[nodiscard]] PatchMode Mode() const noexcept {
        return mode;
    }

private:
    using ModuleDestLabel = uintptr_t;

    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:
    void BranchToPatch(uintptr_t module_dest) {
        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});
        c.dw(0);
    }

    void WriteModulePc(uintptr_t module_dest) {
        m_write_module_pc_relocations.push_back({c.offset(), module_dest});
        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.
    };

    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{};
    oaknut::Label m_save_context{};
    oaknut::Label m_load_context{};
    PatchMode mode{PatchMode::None};
};

} // namespace Core::NCE