aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/A32/test_coprocessor.cpp
blob: 24d54efe4f90ce8aaa988528d4e175bc285e4131 (plain)
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/* This file is part of the dynarmic project.
 * Copyright (c) 2022 MerryMage
 * SPDX-License-Identifier: 0BSD
 */

#include <memory>

#include <catch2/catch_test_macros.hpp>

#include "./testenv.h"
#include "dynarmic/frontend/A32/a32_location_descriptor.h"
#include "dynarmic/interface/A32/a32.h"
#include "dynarmic/interface/A32/coprocessor.h"

using namespace Dynarmic;

struct CP15State {
    u32 cp15_thread_uprw = 0;
    u32 cp15_thread_uro = 0;
    u32 cp15_flush_prefetch_buffer = 0;  ///< dummy value
    u32 cp15_data_sync_barrier = 0;      ///< dummy value
    u32 cp15_data_memory_barrier = 0;    ///< dummy value
};

class TestCP15 final : public Dynarmic::A32::Coprocessor {
public:
    using CoprocReg = Dynarmic::A32::CoprocReg;

    explicit TestCP15(CP15State&);
    ~TestCP15() override;

    std::optional<Callback> CompileInternalOperation(bool two, unsigned opc1, CoprocReg CRd, CoprocReg CRn, CoprocReg CRm, unsigned opc2) override;
    CallbackOrAccessOneWord CompileSendOneWord(bool two, unsigned opc1, CoprocReg CRn, CoprocReg CRm, unsigned opc2) override;
    CallbackOrAccessTwoWords CompileSendTwoWords(bool two, unsigned opc, CoprocReg CRm) override;
    CallbackOrAccessOneWord CompileGetOneWord(bool two, unsigned opc1, CoprocReg CRn, CoprocReg CRm, unsigned opc2) override;
    CallbackOrAccessTwoWords CompileGetTwoWords(bool two, unsigned opc, CoprocReg CRm) override;
    std::optional<Callback> CompileLoadWords(bool two, bool long_transfer, CoprocReg CRd, std::optional<u8> option) override;
    std::optional<Callback> CompileStoreWords(bool two, bool long_transfer, CoprocReg CRd, std::optional<u8> option) override;

private:
    CP15State& state;
};

using Callback = Dynarmic::A32::Coprocessor::Callback;
using CallbackOrAccessOneWord = Dynarmic::A32::Coprocessor::CallbackOrAccessOneWord;
using CallbackOrAccessTwoWords = Dynarmic::A32::Coprocessor::CallbackOrAccessTwoWords;

TestCP15::TestCP15(CP15State& state)
        : state(state) {}

TestCP15::~TestCP15() = default;

std::optional<Callback> TestCP15::CompileInternalOperation([[maybe_unused]] bool two, [[maybe_unused]] unsigned opc1, [[maybe_unused]] CoprocReg CRd, [[maybe_unused]] CoprocReg CRn, [[maybe_unused]] CoprocReg CRm, [[maybe_unused]] unsigned opc2) {
    return std::nullopt;
}

CallbackOrAccessOneWord TestCP15::CompileSendOneWord(bool two, unsigned opc1, CoprocReg CRn, CoprocReg CRm, unsigned opc2) {
    if (!two && CRn == CoprocReg::C7 && opc1 == 0 && CRm == CoprocReg::C5 && opc2 == 4) {
        return Callback{
            [](void* user_arg, std::uint32_t, std::uint32_t) -> std::uint64_t {
                CP15State& state = *reinterpret_cast<CP15State*>(user_arg);
                state.cp15_flush_prefetch_buffer = 1;
                return 0;
            },
            reinterpret_cast<void*>(&state),
        };
    }

    if (!two && CRn == CoprocReg::C7 && opc1 == 0 && CRm == CoprocReg::C10) {
        switch (opc2) {
        case 4:
            return Callback{
                [](void* user_arg, std::uint32_t, std::uint32_t) -> std::uint64_t {
                    CP15State& state = *reinterpret_cast<CP15State*>(user_arg);
                    state.cp15_data_sync_barrier = 1;
                    return 0;
                },
                reinterpret_cast<void*>(&state),
            };
        case 5:
            return Callback{
                [](void* user_arg, std::uint32_t, std::uint32_t) -> std::uint64_t {
                    CP15State& state = *reinterpret_cast<CP15State*>(user_arg);
                    state.cp15_data_memory_barrier = 1;
                    return 0;
                },
                reinterpret_cast<void*>(&state),
            };
        default:
            return std::monostate{};
        }
    }

    if (!two && CRn == CoprocReg::C13 && opc1 == 0 && CRm == CoprocReg::C0 && opc2 == 2) {
        return &state.cp15_thread_uprw;
    }

    return std::monostate{};
}

CallbackOrAccessTwoWords TestCP15::CompileSendTwoWords([[maybe_unused]] bool two, [[maybe_unused]] unsigned opc, [[maybe_unused]] CoprocReg CRm) {
    return std::monostate{};
}

CallbackOrAccessOneWord TestCP15::CompileGetOneWord(bool two, unsigned opc1, CoprocReg CRn, CoprocReg CRm, unsigned opc2) {
    // TODO(merry): Privileged CP15 registers

    if (!two && CRn == CoprocReg::C13 && opc1 == 0 && CRm == CoprocReg::C0) {
        switch (opc2) {
        case 2:
            return &state.cp15_thread_uprw;
        case 3:
            return &state.cp15_thread_uro;
        default:
            return std::monostate{};
        }
    }

    return std::monostate{};
}

CallbackOrAccessTwoWords TestCP15::CompileGetTwoWords([[maybe_unused]] bool two, [[maybe_unused]] unsigned opc, [[maybe_unused]] CoprocReg CRm) {
    return std::monostate{};
}

std::optional<Callback> TestCP15::CompileLoadWords([[maybe_unused]] bool two, [[maybe_unused]] bool long_transfer, [[maybe_unused]] CoprocReg CRd, [[maybe_unused]] std::optional<u8> option) {
    return std::nullopt;
}

std::optional<Callback> TestCP15::CompileStoreWords([[maybe_unused]] bool two, [[maybe_unused]] bool long_transfer, [[maybe_unused]] CoprocReg CRd, [[maybe_unused]] std::optional<u8> option) {
    return std::nullopt;
}

static A32::UserConfig GetUserConfig(ArmTestEnv* testenv, CP15State& cp15_state) {
    A32::UserConfig user_config;
    user_config.optimizations &= ~OptimizationFlag::FastDispatch;
    user_config.callbacks = testenv;
    user_config.coprocessors[15] = std::make_unique<TestCP15>(cp15_state);
    return user_config;
}

TEST_CASE("arm: Test coprocessor (Read TPIDRURO)", "[arm][A32]") {
    ArmTestEnv test_env;
    CP15State cp15_state;
    A32::Jit jit{GetUserConfig(&test_env, cp15_state)};

    cp15_state.cp15_thread_uro = 0xf00d;
    cp15_state.cp15_thread_uprw = 0xcafe;
    jit.Regs()[0] = 0xaaaa;

    test_env.code_mem = {
        0xee1d1f70,  // mrc p15, 0, r1, c13, c0, 3 (Read TPIDRURO into R1)
        0xeafffffe,  // b +#0
    };

    jit.SetCpsr(0x000001d0);  // User-mode

    test_env.ticks_left = 2;
    jit.Run();

    REQUIRE(jit.Regs()[1] == 0xf00d);
}

TEST_CASE("arm: Test coprocessor (Read TPIDRURW)", "[arm][A32]") {
    ArmTestEnv test_env;
    CP15State cp15_state;
    A32::Jit jit{GetUserConfig(&test_env, cp15_state)};

    cp15_state.cp15_thread_uro = 0xf00d;
    cp15_state.cp15_thread_uprw = 0xcafe;
    jit.Regs()[0] = 0xaaaa;

    test_env.code_mem = {
        0xee1d1f50,  // mrc p15, 0, r1, c13, c0, 2 (Read TPIDRURW into R1)
        0xeafffffe,  // b +#0
    };

    jit.SetCpsr(0x000001d0);  // User-mode

    test_env.ticks_left = 2;
    jit.Run();

    REQUIRE(jit.Regs()[1] == 0xcafe);
}

TEST_CASE("arm: Test coprocessor (Write TPIDRURW)", "[arm][A32]") {
    ArmTestEnv test_env;
    CP15State cp15_state;
    A32::Jit jit{GetUserConfig(&test_env, cp15_state)};

    cp15_state.cp15_thread_uro = 0xf00d;
    cp15_state.cp15_thread_uprw = 0xcafe;
    jit.Regs()[0] = 0xaaaa;

    test_env.code_mem = {
        0xee0d0f50,  // mcr p15, 0, r0, c13, c0, 2 (Write R0 into TPIDRURW)
        0xeafffffe,  // b +#0
    };

    jit.SetCpsr(0x000001d0);  // User-mode

    test_env.ticks_left = 2;
    jit.Run();

    REQUIRE(cp15_state.cp15_thread_uprw == 0xaaaa);
}

TEST_CASE("arm: Test coprocessor (DMB)", "[arm][A32]") {
    ArmTestEnv test_env;
    CP15State cp15_state;
    A32::Jit jit{GetUserConfig(&test_env, cp15_state)};

    cp15_state.cp15_thread_uro = 0xf00d;
    cp15_state.cp15_thread_uprw = 0xcafe;
    jit.Regs()[0] = 0xaaaa;

    test_env.code_mem = {
        0xee070fba,  // mcr p15, 0, r0, c7, c10, 5 (Data Memory Barrier)
        0xeafffffe,  // b +#0
    };

    jit.SetCpsr(0x000001d0);  // User-mode

    test_env.ticks_left = 2;
    jit.Run();

    REQUIRE(cp15_state.cp15_data_memory_barrier == 1);
}