aboutsummaryrefslogtreecommitdiff
path: root/lib/darwin/inject-asm-raw.c
blob: 3b0f1ce1c69c7edca31ced4822903b99982dc3cc (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
#include "darwin/manual-syscall.h"

#ifdef __arm64__
#define _PAGE_SIZE 0x4000
#else
#define _PAGE_SIZE 0x1000
#endif

struct baton {
    int (*pthread_create)(int *, void *, void *(*)(void *), void *);
    void (*dlopen)(const char *, int);
    const char *path;
    long done;
};
struct baton2 {
    void (*dlopen)(const char *, int);
    const char *path;
    int port;
};
static void *bsd_thread_func(void *);
#if defined(__i386__)
__attribute__((fastcall))
#endif
/* xxx need to change this to have host allocate two pages - way easier */
void entry(struct baton *baton) {
    int pt;
    baton->pthread_create(&pt, 0, bsd_thread_func, baton);
    unsigned long ptr = (unsigned long) baton & ~(_PAGE_SIZE - 1);
    while (!baton->done)
        manual_syscall(-62 /*clock_sleep_trap */, 0, 1, 0, 8000 /*ns*/, -1);
    manual_syscall(361 /*bsdthread_terminate*/, ptr, 0x2000, 0, 0, 0);
    ((void (*)()) 0xbad)();
}
static void *bsd_thread_func(void *arg) {
    struct baton *baton = arg;
    baton->dlopen(baton->path, 0);
    baton->done = 1;
    return 0;
}