-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.cpp
386 lines (327 loc) · 11 KB
/
Main.cpp
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
#include <private/system/user_runtime.h>
#include <private/kernel/ksyscalls.h>
#include "syscall_table.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libgen.h>
#include <time.h>
#include <algorithm>
#include <dlfcn.h>
#include <signal.h>
#include <OS.h>
#include <image.h>
#include <TLS.h>
#include <private/system/tls.h>
#include <private/system/syscalls.h>
#include <private/system/thread_defs.h>
#include <private/system/user_thread_defs.h>
#include <private/libroot/pthread_private.h>
#include <private/libroot/libroot_private.h>
#include <private/system/commpage_defs.h>
#include <private/system/arch/riscv64/arch_commpage_defs.h>
#include <private/system/real_time_data.h>
#include "Loader.h"
#include "Syscalls.h"
#include "VirtualCpuRiscV.h"
#define CheckRet(err) {status_t _err = (err); if (_err < B_OK) return _err;}
struct riscv64_real_time_data {
bigtime_t system_time_offset;
uint64 system_time_conversion_factor;
};
static VirtualCpuRiscV *(*sInstantiateVirtualCpuRiscv)();
extern void *__gCommPageAddress;
thread_local VirtualCpuRiscV *tCpu = NULL;
thread_local bool tThreadExit = false;
thread_local status_t tThreadReturnValue = B_OK;
bool gInSignalHandler = false;
area_id vm32_create_area(const char *name, void **address, uint32 addressSpec, size_t size, uint32 lock, uint32 protection)
{
if (addressSpec != B_ANY_ADDRESS) {
return _kern_create_area(name, address, addressSpec, size, lock, protection);
}
// crazy random allocator :D
size = (size + (B_PAGE_SIZE - 1)) / B_PAGE_SIZE * B_PAGE_SIZE;
for (int i = 0; ; i++) {
void *address32 = (void*)(rand() % (0xffffffff - size + 1));
area_id area = _kern_create_area(name, &address32, B_EXACT_ADDRESS, size, lock, protection);
if (area >= B_OK || !(i < 100)) {
*address = address32;
return area;
}
}
}
void *GetImageBase(const char *name)
{
int32 cookie = 0;
image_info info;
while (get_next_image_info(B_CURRENT_TEAM, &cookie, &info) >= B_OK) {
if (strcmp(basename(info.name), name) == 0) return info.text;
}
return NULL;
}
void WritePC(uint64 pc)
{
int32 cookie = 0;
image_info info;
while (get_next_image_info(B_CURRENT_TEAM, &cookie, &info) >= B_OK) {
if (pc >= (addr_t)info.text && pc <= (addr_t)info.text + info.text_size - 1) {
printf("<%s> %#" B_PRIx64, info.name, pc - (addr_t)info.text);
return;
}
}
printf("%#" B_PRIx64, pc);
}
void StackTrace()
{
uint64 pc = tCpu->Pc();
uint64 sp = tCpu->Regs()[2];
uint64 fp = tCpu->Regs()[8];
for (;;) {
printf("FP: %#" B_PRIx64 ", PC: ", fp); WritePC(pc); printf("\n");
if (fp == 0) break;
pc = *((uint64*)fp - 1);
fp = *((uint64*)fp - 2);
}
}
static void HaikuTrap()
{
//printf("HaikuTrap: Hart %p trap at %#08" PRIxXLEN ", cause %x, tval %#08" PRIxXLEN "\n", tCpu, tCpu->Pc(), tCpu->Cause(), tCpu->Tval());
switch (tCpu->Cause()) {
case -1:
break;
case causeMEcall:
case causeSEcall:
case causeUEcall:
{
uint64 syscall = tCpu->Regs()[5];
//printf("syscall %" B_PRIu64 "(%s)\n", syscall, syscall < (uint64)kSyscallCount ? kExtendedSyscallInfos[syscall].name : "?");
uint64 args[20];
if (syscall < (uint64)kSyscallCount) {
uint32 argCnt = kExtendedSyscallInfos[syscall].parameter_count;
memcpy(&args[0], &tCpu->Regs()[10], sizeof(uint64)*std::min<uint32>(argCnt, 8));
if (argCnt > 8) {
memcpy(&args[8], (void*)tCpu->Regs()[2], sizeof(uint64)*(argCnt - 8));
}
}
DispatchSyscall(syscall, args, &tCpu->Regs()[10]);
break;
}
default:
printf("[!] unhandled trap: Hart %p trap at %#08" B_PRIx64 ", cause %x, tval %#08" B_PRIx64 "\n", tCpu, tCpu->Pc(), tCpu->Cause(), tCpu->Tval());
StackTrace();
fgetc(stdin);
_exit(1);
}
}
status_t HaikuThreadStart(pthread_t pthread, uint64 entry, uint64 arg1, uint64 arg2)
{
size_t stackSize = 0x100000;
void *stack;
AreaDeleter stackArea(create_area("thread", &stack, B_ANY_ADDRESS, stackSize, B_NO_LOCK, B_READ_AREA | B_WRITE_AREA));
user_thread userThread {
.pthread = pthread
};
uint64 tls[TLS_MAX_KEYS]{};
tls[TLS_BASE_ADDRESS_SLOT] = (addr_t)&tls;
tls[TLS_THREAD_ID_SLOT] = find_thread(NULL);
tls[TLS_USER_THREAD_SLOT] = (addr_t)&userThread;
static uint32 threadExitCode[] = {
0x03b00293, // addi t0, zero, 59 // _kern_exit_thread
0x00000073, // ecall
};
ObjectDeleter<VirtualCpuRiscV> cpu(sInstantiateVirtualCpuRiscv());
tCpu = cpu.Get();
cpu->Regs()[1] = (addr_t)&threadExitCode;
cpu->Regs()[2] = (addr_t)stack + stackSize;
cpu->Regs()[4] = (addr_t)&tls;
cpu->Regs()[10] = arg1;
cpu->Regs()[11] = arg2;
cpu->Pc() = entry;
while (!tThreadExit) {
cpu->Run();
HaikuTrap();
}
tCpu = NULL;
return tThreadReturnValue;
}
int32 ThreadEntry(void *arg)
{
ObjectDeleter<thread_creation_attributes> attributes((thread_creation_attributes*)arg);
return HaikuThreadStart((pthread_t)attributes->args2, (addr_t)attributes->entry, (addr_t)attributes->args1, (addr_t)attributes->args2);
}
// #pragma mark - syscall overrides
thread_id vm_spawn_thread(struct thread_creation_attributes* attributes)
{
ObjectDeleter<struct thread_creation_attributes> guestAttrs(new struct thread_creation_attributes());
memcpy(guestAttrs.Get(), attributes, sizeof(thread_creation_attributes));
thread_id thread = spawn_thread(ThreadEntry, attributes->name, attributes->priority, guestAttrs.Detach());
return thread;
}
void vm_exit_thread(status_t returnValue)
{
tThreadExit = true;
tThreadReturnValue = returnValue;
}
thread_id vm_fork()
{
VirtualCpuRiscV *cpu = tCpu;
thread_id res = fork();
if (res == 0) tCpu = cpu;
return res;
}
static const char sAppPath[] = "UserlandVM";
thread_id vm_load_image(const char* const* flatArgs, size_t flatArgsSize, int32 argCount, int32 envCount, int32 priority, uint32 flags, port_id errorPort, uint32 errorToken)
{
printf("[!] vm_load_image\n");
char** outFlatArgs;
size_t outFlatSize;
int32 outEnvCount = envCount;
{
ArrayDeleter<const char*> args2(new(std::nothrow) const char*[argCount + 2]);
if (!args2.IsSet()) return B_NO_MEMORY;
args2[0] = sAppPath;
memcpy(&args2[1], flatArgs, sizeof(void*)*(argCount + 1));
CheckRet(__flatten_process_args(args2.Get(), argCount + 1, flatArgs + argCount + 1, &outEnvCount, NULL, &outFlatArgs, &outFlatSize));
}
status_t res = _kern_load_image(outFlatArgs, outFlatSize, argCount + 1, outEnvCount, priority, flags, errorPort, errorToken);
free(outFlatArgs);
return res;
}
thread_id vm_exec(const char *path, const char* const* flatArgs, size_t flatArgsSize, int32 argCount, int32 envCount, mode_t umask)
{
char** outFlatArgs;
size_t outFlatSize;
int32 outEnvCount = envCount;
{
ArrayDeleter<const char*> args2(new(std::nothrow) const char*[argCount + 2]);
if (!args2.IsSet()) return B_NO_MEMORY;
args2[0] = sAppPath;
memcpy(&args2[1], flatArgs, sizeof(void*)*(argCount + 1));
CheckRet(__flatten_process_args(args2.Get(), argCount + 1, flatArgs + argCount + 1, &outEnvCount, NULL, &outFlatArgs, &outFlatSize));
}
status_t res = _kern_exec(sAppPath, outFlatArgs, outFlatSize, argCount + 1, outEnvCount, umask);
free(outFlatArgs);
return res;
}
status_t vm_sigaction(int sig, const struct sigaction *action, struct sigaction *oldAction)
{
//printf("[!] vm_sigaction: unimplemented\n");
return B_OK;
}
// #pragma mark -
void SignalHandler(int sig, siginfo_t *info, ucontext_t *ctx, void *arg)
{
printf("SignalHandler(%d)\n", sig);
printf("PC: %#" B_PRIx64 "\n", tCpu->Pc());
if (gInSignalHandler) {
exit(0);
}
gInSignalHandler = true;
StackTrace();
gInSignalHandler = false;
exit(0);
}
void BuildArgs(ArrayDeleter<uint8> &mem, user_space_program_args &args, char **argv, char **env)
{
size_t argCnt = 0;
while (argv[argCnt] != NULL) argCnt++;
size_t envCnt = 0;
while (env[envCnt] != NULL) envCnt++;
size_t argSize = 0;
for (size_t i = 0; i < argCnt; i++) {
argSize += strlen(argv[i]) + 1;
}
for (size_t i = 0; i < envCnt; i++) {
argSize += strlen(env[i]) + 1;
}
mem.SetTo(new uint8[sizeof(void*)*(argCnt + envCnt + 2) + argSize]);
char **outArgs = (char**)&mem[0];
char *outChars = (char*)&mem[sizeof(void*)*(argCnt + envCnt + 2)];
for (size_t i = 0; i < argCnt; i++) {
size_t len = strlen(argv[i]) + 1;
*outArgs = outChars; outArgs++;
memcpy(outChars, argv[i], len);
outChars += len;
}
*outArgs = NULL; outArgs++;
for (size_t i = 0; i < envCnt; i++) {
size_t len = strlen(env[i]) + 1;
*outArgs = outChars; outArgs++;
memcpy(outChars, env[i], len);
outChars += len;
}
*outArgs = NULL; outArgs++;
char buf[B_PATH_NAME_LENGTH];
strcpy(buf, argv[0]);
strcpy(args.program_name, basename(buf));
strcpy(args.program_path, argv[0]);
args.error_port = -1;
args.arg_count = argCnt;
args.env_count = envCnt;
args.args = (char**)&mem[0];
args.env = (char**)&mem[0] + (argCnt + 1);
}
void VirtualCpuX86Test();
int main(int argc, char **argv, char **env)
{
srand(time(NULL));
char **curArgv = argv;
char **endArgv = argv + argc;
// skip program self name
curArgv++;
if (endArgv - curArgv <= 0) {
VirtualCpuX86Test();
return 0;
}
CObjectDeleter<void, int, dlclose> virtualCpuAddon;
const char *addonName = "./librvvm2.so";
if (endArgv - curArgv >= 1 && strcmp(curArgv[0], "--engine") == 0) {
curArgv++;
if (endArgv - curArgv <= 0) {
fprintf(stderr, "argument missing\n");
return 1;
}
addonName = curArgv[0];
curArgv++;
}
virtualCpuAddon.SetTo(dlopen(addonName, 0));
if (!virtualCpuAddon.IsSet()) {
fprintf(stderr, "can't load virtual CPU add-on \"%s\"\n", addonName);
return 1;
}
*(void**)&sInstantiateVirtualCpuRiscv = dlsym(virtualCpuAddon.Get(), "instantiate_virtual_cpu_riscv");
if (sInstantiateVirtualCpuRiscv == NULL) {
fprintf(stderr, "bad virtual CPU add-on \"%s\"\n", addonName);
return 1;
}
ObjectDeleter<ElfImage> image(ElfImage::Load("../runtime_loader.riscv64"));
user_space_program_args args{};
ArrayDeleter<uint8> argsMem;
//char *env[] = {"A=1", NULL};
BuildArgs(argsMem, args, curArgv, env);
void *hostCommpage = __gCommPageAddress;
real_time_data *hostRtData = (real_time_data*)(((uint64*)hostCommpage)[COMMPAGE_ENTRY_REAL_TIME_DATA] + (uint8*)hostCommpage);
void *commpage;
AreaDeleter commpageArea(create_area("guest commpage", &commpage, B_ANY_ADDRESS, COMMPAGE_SIZE, B_NO_LOCK, B_READ_AREA | B_WRITE_AREA));
riscv64_real_time_data *rtData = (riscv64_real_time_data*)((uint64*)commpage + COMMPAGE_TABLE_ENTRIES);
rtData->system_time_offset = hostRtData->arch_data.system_time_offset;
rtData->system_time_conversion_factor = 1ULL << 32;
((uint64*)commpage)[COMMPAGE_ENTRY_MAGIC] = COMMPAGE_SIGNATURE;
((uint64*)commpage)[COMMPAGE_ENTRY_VERSION] = COMMPAGE_VERSION;
((uint64*)commpage)[COMMPAGE_ENTRY_REAL_TIME_DATA] = (uint8*)rtData - (uint8*)commpage;
int signals[] = {/*SIGILL, SIGSEGV,*/ 0};
struct sigaction oldAction[B_COUNT_OF(signals)];
struct sigaction action {
.sa_sigaction = (__siginfo_handler_t)SignalHandler,
.sa_flags = SA_SIGINFO
};
for (int i = 0; signals[i] != 0; i++) {
sigaction(signals[i], &action, &oldAction[i]);
}
status_t res = HaikuThreadStart(NULL, (addr_t)image->GetEntry(), (addr_t)&args, (addr_t)commpage);
for (int i = 0; signals[i] != 0; i++) {
sigaction(signals[i], &oldAction[i], NULL);
}
return res;
}