-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathciallo.h
More file actions
154 lines (140 loc) · 4.1 KB
/
ciallo.h
File metadata and controls
154 lines (140 loc) · 4.1 KB
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
#pragma once
typedef unsigned long long uint64;
typedef struct context {
uint64 rbx;
uint64 rcx;
uint64 rdx;
uint64 rsi;
uint64 rdi;
uint64 rbp;
uint64 rsp;
uint64 r8;
uint64 r9;
uint64 r10;
uint64 r11;
uint64 r12;
uint64 r13;
uint64 r14;
uint64 r15;
uint64 eflags;
uint64 rip;
} context;
typedef struct function {
void (*function_ptr)(void *);
void *function_arg;
} function;
int contexts_is_empty(void);
int contexts_is_full(void);
int contexts_append(const context *);
int contexts_remove(context *);
int functions_is_empty(void);
int functions_is_full(void);
int functions_append(const function *);
int functions_remove(function *);
int is_stack_top(void);
int update_stack_top(void);
#define CONTEXTS_MAX 0x100
#define FUNCTIONS_MAX 0x100
#define CONTEXTS_SAVE(curr) \
asm volatile ( \
"movq %%rbx, 0x00(%0)\n\t" \
"movq %%rcx, 0x08(%0)\n\t" \
"movq %%rdx, 0x10(%0)\n\t" \
"movq %%rsi, 0x18(%0)\n\t" \
"movq %%rdi, 0x20(%0)\n\t" \
"movq %%rbp, 0x28(%0)\n\t" \
"movq %%rsp, 0x30(%0)\n\t" \
"movq %%r8, 0x38(%0)\n\t" \
"movq %%r9, 0x40(%0)\n\t" \
"movq %%r10, 0x48(%0)\n\t" \
"movq %%r11, 0x50(%0)\n\t" \
"movq %%r12, 0x58(%0)\n\t" \
"movq %%r13, 0x60(%0)\n\t" \
"movq %%r14, 0x68(%0)\n\t" \
"movq %%r15, 0x70(%0)\n\t" \
"pushfq \n\t" \
"popq 0x78(%0) \n\t" \
"leaq 1f(%%rip), %%rcx \n\t" \
"movq %%rcx, 0x80(%0)\n\t" \
: \
: "a" (&curr) \
: "memory")
#define CONTEXTS_LOAD(next) \
asm volatile ( \
"movq 0x00(%0), %%rbx\n\t" \
"movq 0x08(%0), %%rcx\n\t" \
"movq 0x10(%0), %%rdx\n\t" \
"movq 0x18(%0), %%rsi\n\t" \
"movq 0x20(%0), %%rdi\n\t" \
"movq 0x28(%0), %%rbp\n\t" \
"movq 0x30(%0), %%rsp\n\t" \
"movq 0x38(%0), %%r8 \n\t" \
"movq 0x40(%0), %%r9 \n\t" \
"movq 0x48(%0), %%r10\n\t" \
"movq 0x50(%0), %%r11\n\t" \
"movq 0x58(%0), %%r12\n\t" \
"movq 0x60(%0), %%r13\n\t" \
"movq 0x68(%0), %%r14\n\t" \
"movq 0x70(%0), %%r15\n\t" \
"pushq 0x78(%0) \n\t" \
"popfq \n\t" \
"jmp *0x80(%0) \n\t" \
"1: \n\t" \
: \
: "a" (&next) \
: "memory")
#define INIT { \
context curr; \
context next; \
CONTEXTS_SAVE(curr); \
contexts_append(&curr); \
if (!functions_is_empty()) { \
function func; \
functions_remove(&func); \
func.function_ptr(func.function_arg); \
} \
update_stack_top(); \
contexts_remove(&next); \
CONTEXTS_LOAD(next); \
}
#define NEXT { \
context curr; \
context next; \
CONTEXTS_SAVE(curr); \
contexts_append(&curr); \
if (is_stack_top()) { \
if (!functions_is_empty()) { \
function func; \
functions_remove(&func); \
func.function_ptr(func.function_arg); \
} \
update_stack_top(); \
} \
contexts_remove(&next); \
CONTEXTS_LOAD(next); \
}
#define CALL(fun, arg) { \
context curr; \
context next; \
CONTEXTS_SAVE(curr); \
contexts_append(&curr); \
if (is_stack_top()) { \
fun(arg); \
} \
function func = {fun, arg}; \
functions_append(&func); \
contexts_remove(&next); \
CONTEXTS_LOAD(next); \
}
#define EXIT { \
context next = {0}; \
if (is_stack_top()) { \
update_stack_top(); \
} \
contexts_remove(&next); \
CONTEXTS_LOAD(next); \
}
#define Ciaolima do { INIT } while (0)
#define Ciallo(fun, arg) do { CALL(fun, arg) } while (0)
#define Ciallo_ do { NEXT } while (0)
#define Ciavanllo do { EXIT } while (0)