summaryrefslogtreecommitdiffstats
path: root/src/core/hle/kernel/svc_wrap.h
blob: efae4fdfe0ffaccc04c721941c772dae15b631aa (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
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
229
230
231
232
233
234
// Copyright 2018 yuzu emulator team
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.

#pragma once

#include "common/common_types.h"
#include "core/arm/arm_interface.h"
#include "core/core.h"
#include "core/hle/kernel/svc.h"
#include "core/hle/result.h"
#include "core/memory.h"

namespace Kernel {

#define PARAM(n) Core::CurrentArmInterface().GetReg(n)

/**
 * HLE a function return from the current ARM userland process
 * @param res Result to return
 */
static inline void FuncReturn(u64 res) {
    Core::CurrentArmInterface().SetReg(0, res);
}

////////////////////////////////////////////////////////////////////////////////////////////////////
// Function wrappers that return type ResultCode

template <ResultCode func(u64)>
void SvcWrap() {
    FuncReturn(func(PARAM(0)).raw);
}

template <ResultCode func(u32)>
void SvcWrap() {
    FuncReturn(func((u32)PARAM(0)).raw);
}

template <ResultCode func(u32, u32)>
void SvcWrap() {
    FuncReturn(func((u32)PARAM(0), (u32)PARAM(1)).raw);
}

template <ResultCode func(u32*, u32)>
void SvcWrap() {
    u32 param_1 = 0;
    u32 retval = func(&param_1, (u32)PARAM(1)).raw;
    Core::CurrentArmInterface().SetReg(1, param_1);
    FuncReturn(retval);
}

template <ResultCode func(u32*, u64)>
void SvcWrap() {
    u32 param_1 = 0;
    u32 retval = func(&param_1, PARAM(1)).raw;
    Core::CurrentArmInterface().SetReg(1, param_1);
    FuncReturn(retval);
}

template <ResultCode func(u64, s32)>
void SvcWrap() {
    FuncReturn(func(PARAM(0), (s32)PARAM(1)).raw);
}

template <ResultCode func(u64*, u64)>
void SvcWrap() {
    u64 param_1 = 0;
    u32 retval = func(&param_1, PARAM(1)).raw;
    Core::CurrentArmInterface().SetReg(1, param_1);
    FuncReturn(retval);
}

template <ResultCode func(u32, u64)>
void SvcWrap() {
    FuncReturn(func((u32)(PARAM(0) & 0xFFFFFFFF), PARAM(1)).raw);
}

template <ResultCode func(u32, u32, u64)>
void SvcWrap() {
    FuncReturn(func((u32)(PARAM(0) & 0xFFFFFFFF), (u32)(PARAM(1) & 0xFFFFFFFF), PARAM(2)).raw);
}

template <ResultCode func(u32, u32*, u64*)>
void SvcWrap() {
    u32 param_1 = 0;
    u64 param_2 = 0;
    ResultCode retval = func((u32)(PARAM(2) & 0xFFFFFFFF), &param_1, &param_2);
    Core::CurrentArmInterface().SetReg(1, param_1);
    Core::CurrentArmInterface().SetReg(2, param_2);
    FuncReturn(retval.raw);
}

template <ResultCode func(u64, u64, u32, u32)>
void SvcWrap() {
    FuncReturn(
        func(PARAM(0), PARAM(1), (u32)(PARAM(3) & 0xFFFFFFFF), (u32)(PARAM(3) & 0xFFFFFFFF)).raw);
}

template <ResultCode func(u32, u64, u32)>
void SvcWrap() {
    FuncReturn(func((u32)PARAM(0), PARAM(1), (u32)PARAM(2)).raw);
}

template <ResultCode func(u64, u64, u64)>
void SvcWrap() {
    FuncReturn(func(PARAM(0), PARAM(1), PARAM(2)).raw);
}

template <ResultCode func(u32, u64, u64, u32)>
void SvcWrap() {
    FuncReturn(func((u32)PARAM(0), PARAM(1), PARAM(2), (u32)PARAM(3)).raw);
}

template <ResultCode func(u32, u64, u64)>
void SvcWrap() {
    FuncReturn(func((u32)PARAM(0), PARAM(1), PARAM(2)).raw);
}

template <ResultCode func(u32*, u64, u64, s64)>
void SvcWrap() {
    u32 param_1 = 0;
    ResultCode retval = func(&param_1, PARAM(1), (u32)(PARAM(2) & 0xFFFFFFFF), (s64)PARAM(3));
    Core::CurrentArmInterface().SetReg(1, param_1);
    FuncReturn(retval.raw);
}

template <ResultCode func(u64, u64, u32, s64)>
void SvcWrap() {
    FuncReturn(func(PARAM(0), PARAM(1), (u32)PARAM(2), (s64)PARAM(3)).raw);
}

template <ResultCode func(u64*, u64, u64, u64)>
void SvcWrap() {
    u64 param_1 = 0;
    u32 retval = func(&param_1, PARAM(1), PARAM(2), PARAM(3)).raw;
    Core::CurrentArmInterface().SetReg(1, param_1);
    FuncReturn(retval);
}

template <ResultCode func(u32*, u64, u64, u64, u32, s32)>
void SvcWrap() {
    u32 param_1 = 0;
    u32 retval =
        func(&param_1, PARAM(1), PARAM(2), PARAM(3), (u32)PARAM(4), (s32)(PARAM(5) & 0xFFFFFFFF))
            .raw;
    Core::CurrentArmInterface().SetReg(1, param_1);
    FuncReturn(retval);
}

template <ResultCode func(MemoryInfo*, PageInfo*, u64)>
void SvcWrap() {
    MemoryInfo memory_info = {};
    PageInfo page_info = {};
    u32 retval = func(&memory_info, &page_info, PARAM(2)).raw;

    Memory::Write64(PARAM(0), memory_info.base_address);
    Memory::Write64(PARAM(0) + 8, memory_info.size);
    Memory::Write32(PARAM(0) + 16, memory_info.type);
    Memory::Write32(PARAM(0) + 20, memory_info.attributes);
    Memory::Write32(PARAM(0) + 24, memory_info.permission);

    FuncReturn(retval);
}

template <ResultCode func(u32*, u64, u64, u32)>
void SvcWrap() {
    u32 param_1 = 0;
    u32 retval = func(&param_1, PARAM(1), PARAM(2), (u32)(PARAM(3) & 0xFFFFFFFF)).raw;
    Core::CurrentArmInterface().SetReg(1, param_1);
    FuncReturn(retval);
}

template <ResultCode func(Handle*, u64, u32, u32)>
void SvcWrap() {
    u32 param_1 = 0;
    u32 retval =
        func(&param_1, PARAM(1), (u32)(PARAM(2) & 0xFFFFFFFF), (u32)(PARAM(3) & 0xFFFFFFFF)).raw;
    Core::CurrentArmInterface().SetReg(1, param_1);
    FuncReturn(retval);
}

template <ResultCode func(u64, u32, s32, s64)>
void SvcWrap() {
    FuncReturn(func(PARAM(0), (u32)(PARAM(1) & 0xFFFFFFFF), (s32)(PARAM(2) & 0xFFFFFFFF), (s64)PARAM(3)).raw);
}

template <ResultCode func(u64, u32, s32, s32)>
void SvcWrap() {
    FuncReturn(func(PARAM(0), (u32)(PARAM(1) & 0xFFFFFFFF), (s32)(PARAM(2) & 0xFFFFFFFF), (s32)(PARAM(3) & 0xFFFFFFFF)).raw);
}

////////////////////////////////////////////////////////////////////////////////////////////////////
// Function wrappers that return type u32

template <u32 func()>
void SvcWrap() {
    FuncReturn(func());
}

////////////////////////////////////////////////////////////////////////////////////////////////////
// Function wrappers that return type u64

template <u64 func()>
void SvcWrap() {
    FuncReturn(func());
}

////////////////////////////////////////////////////////////////////////////////////////////////////
/// Function wrappers that return type void

template <void func()>
void SvcWrap() {
    func();
}

template <void func(s64)>
void SvcWrap() {
    func((s64)PARAM(0));
}

template <void func(u64, s32 len)>
void SvcWrap() {
    func(PARAM(0), (s32)(PARAM(1) & 0xFFFFFFFF));
}

template <void func(u64, u64, u64)>
void SvcWrap() {
    func(PARAM(0), PARAM(1), PARAM(2));
}

#undef PARAM
#undef FuncReturn

} // namespace Kernel