Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alexbevi
GitHub Repository: alexbevi/BizHawk
Path: blob/master/libsnes/bsnes/libco/amd64.c
2 views
1
/*
2
libco.amd64 (2009-10-12)
3
author: byuu
4
license: public domain
5
*/
6
7
#define LIBCO_C
8
#include "libco.h"
9
#include <assert.h>
10
#include <stdlib.h>
11
12
#ifdef __cplusplus
13
extern "C" {
14
#endif
15
16
static thread_local long long co_active_buffer[64];
17
static thread_local cothread_t co_active_handle = 0;
18
static void (*co_swap)(cothread_t, cothread_t) = 0;
19
20
#ifdef _WIN32
21
//ABI: Win64
22
static unsigned char co_swap_function[] = {
23
0x48, 0x89, 0x22, 0x48, 0x8B, 0x21, 0x58, 0x48, 0x89, 0x6A, 0x08, 0x48, 0x89, 0x72, 0x10, 0x48,
24
0x89, 0x7A, 0x18, 0x48, 0x89, 0x5A, 0x20, 0x4C, 0x89, 0x62, 0x28, 0x4C, 0x89, 0x6A, 0x30, 0x4C,
25
0x89, 0x72, 0x38, 0x4C, 0x89, 0x7A, 0x40, 0x48, 0x81, 0xC2, 0x80, 0x00, 0x00, 0x00, 0x48, 0x83,
26
0xE2, 0xF0, 0x0F, 0x29, 0x32, 0x0F, 0x29, 0x7A, 0x10, 0x44, 0x0F, 0x29, 0x42, 0x20, 0x44, 0x0F,
27
0x29, 0x4A, 0x30, 0x44, 0x0F, 0x29, 0x52, 0x40, 0x44, 0x0F, 0x29, 0x5A, 0x50, 0x44, 0x0F, 0x29,
28
0x62, 0x60, 0x44, 0x0F, 0x29, 0x6A, 0x70, 0x44, 0x0F, 0x29, 0xB2, 0x80, 0x00, 0x00, 0x00, 0x44,
29
0x0F, 0x29, 0xBA, 0x90, 0x00, 0x00, 0x00, 0x48, 0x8B, 0x69, 0x08, 0x48, 0x8B, 0x71, 0x10, 0x48,
30
0x8B, 0x79, 0x18, 0x48, 0x8B, 0x59, 0x20, 0x4C, 0x8B, 0x61, 0x28, 0x4C, 0x8B, 0x69, 0x30, 0x4C,
31
0x8B, 0x71, 0x38, 0x4C, 0x8B, 0x79, 0x40, 0x48, 0x81, 0xC1, 0x80, 0x00, 0x00, 0x00, 0x48, 0x83,
32
0xE1, 0xF0, 0x0F, 0x29, 0x31, 0x0F, 0x29, 0x79, 0x10, 0x44, 0x0F, 0x29, 0x41, 0x20, 0x44, 0x0F,
33
0x29, 0x49, 0x30, 0x44, 0x0F, 0x29, 0x51, 0x40, 0x44, 0x0F, 0x29, 0x59, 0x50, 0x44, 0x0F, 0x29,
34
0x61, 0x60, 0x44, 0x0F, 0x29, 0x69, 0x70, 0x44, 0x0F, 0x29, 0xB1, 0x80, 0x00, 0x00, 0x00, 0x44,
35
0x0F, 0x29, 0xB9, 0x90, 0x00, 0x00, 0x00, 0xFF, 0xE0,
36
};
37
38
#include <windows.h>
39
40
void co_init() {
41
DWORD old_privileges;
42
VirtualProtect(co_swap_function, sizeof co_swap_function, PAGE_EXECUTE_READWRITE, &old_privileges);
43
}
44
#else
45
//ABI: SystemV
46
static unsigned char co_swap_function[] = {
47
0x48, 0x89, 0x26, 0x48, 0x8B, 0x27, 0x58, 0x48, 0x89, 0x6E, 0x08, 0x48, 0x89, 0x5E, 0x10, 0x4C,
48
0x89, 0x66, 0x18, 0x4C, 0x89, 0x6E, 0x20, 0x4C, 0x89, 0x76, 0x28, 0x4C, 0x89, 0x7E, 0x30, 0x48,
49
0x8B, 0x6F, 0x08, 0x48, 0x8B, 0x5F, 0x10, 0x4C, 0x8B, 0x67, 0x18, 0x4C, 0x8B, 0x6F, 0x20, 0x4C,
50
0x8B, 0x77, 0x28, 0x4C, 0x8B, 0x7F, 0x30, 0xFF, 0xE0,
51
};
52
53
#include <unistd.h>
54
#include <sys/mman.h>
55
56
void co_init() {
57
unsigned long long addr = (unsigned long long)co_swap_function;
58
unsigned long long base = addr - (addr % sysconf(_SC_PAGESIZE));
59
unsigned long long size = (addr - base) + sizeof co_swap_function;
60
mprotect((void*)base, size, PROT_READ | PROT_WRITE | PROT_EXEC);
61
}
62
#endif
63
64
static void crash() {
65
assert(0); /* called only if cothread_t entrypoint returns */
66
}
67
68
cothread_t co_active() {
69
if(!co_active_handle) co_active_handle = &co_active_buffer;
70
return co_active_handle;
71
}
72
73
cothread_t co_create(unsigned int size, void (*entrypoint)(void)) {
74
cothread_t handle;
75
if(!co_swap) {
76
co_init();
77
co_swap = (void (*)(cothread_t, cothread_t))co_swap_function;
78
}
79
if(!co_active_handle) co_active_handle = &co_active_buffer;
80
size += 512; /* allocate additional space for storage */
81
size &= ~15; /* align stack to 16-byte boundary */
82
83
if(handle = (cothread_t)malloc(size)) {
84
long long *p = (long long*)((char*)handle + size); /* seek to top of stack */
85
*--p = (long long)crash; /* crash if entrypoint returns */
86
*--p = (long long)entrypoint; /* start of function */
87
*(long long*)handle = (long long)p; /* stack pointer */
88
}
89
90
return handle;
91
}
92
93
void co_delete(cothread_t handle) {
94
free(handle);
95
}
96
97
void co_switch(cothread_t handle) {
98
register cothread_t co_previous_handle = co_active_handle;
99
co_swap(co_active_handle = handle, co_previous_handle);
100
}
101
102
#ifdef __cplusplus
103
}
104
#endif
105
106