-
Notifications
You must be signed in to change notification settings - Fork 1
/
uintr.cc
353 lines (300 loc) · 6.21 KB
/
uintr.cc
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
#include "uintr.h"
#include "dbcore/sm-thread.h"
thread_local volatile uint32_t worker_id = ~uint32_t{0};
thread_local std::atomic<uint64_t> lock_counter;
pcontext* curr_ctx[MAX_CORES];
pcontext::pcontext() {
lock_counter.store(0);
start_timestamp = 0;
preempted_cycles = 0;
}
uint32_t GetWorkerId() {
ASSERT(worker_id != ~uint32_t{0});
return worker_id;
}
void pcontext::Set_Worker_Id(uint32_t id) {
ASSERT(worker_id == ~uint32_t{0});
worker_id = id;
}
pcontext* pcontext::get_current_context() {
if(curr_ctx[GetWorkerId()] == nullptr){
pcontext::set_current_context(ermia::thread::Thread::MainContext());
}
return curr_ctx[GetWorkerId()];
}
void pcontext::set_current_context(pcontext* ctx) {
curr_ctx[GetWorkerId()] = ctx;
}
uint64_t ReadFSBase() {
return _readfsbase_u64();
}
void pcontext::set_lock_counter(uint64_t counter) {
lock_counter.store(counter);
}
uint64_t pcontext::get_lock_counter() {
return lock_counter.load();
}
bool pcontext::locked() {
return lock_counter.load() > 0;
}
void pcontext::lock() {
lock_counter.fetch_add(1);
}
void pcontext::unlock() {
lock_counter.fetch_sub(1);
}
void pcontext::reset_timer() {
start_timestamp = rdtsc();
preempted_cycles = 0;
}
void pcontext::add_preempted_time(uint64_t cycles) {
preempted_cycles += cycles;
}
bool pcontext::starved() {
uint64_t total_cycles = rdtsc() - start_timestamp;
return 100 * (float)preempted_cycles / (float)total_cycles > ermia::config::max_preempted_cycle_pct;
}
void pcontext::SetRSP(void *rsp){
reg[0] = rsp;
}
bool pcontext::ValidRSP(void *rsp){
return (uint64_t)rsp >= stack_start && (uint64_t)rsp <= stack_end;
}
void pcontext::xsave() {
_xsaveopt64(xsave_area, ~uint64_t{0});
}
void pcontext::xrstor() {
if (!new_context) {
_xrstor64(xsave_area, ~uint64_t{0});
} else {
new_context = false;
}
}
extern "C" void* handler_helper(void* rsp) {
if (pcontext::locked()) {
return rsp;
}
auto wid = worker_id;
pcontext::get_current_context()->SetRSP(rsp);
pcontext::get_current_context()->xsave();
pcontext::set_current_context(ermia::thread::Thread::PreemptiveContext());
pcontext::get_current_context()->xrstor();
void* sp = pcontext::get_current_context()->reg[0];
auto fs = pcontext::get_current_context()->fs;
auto gs = pcontext::get_current_context()->gs;
ASSERT(fs != 0);
_writefsbase_u64(fs);
_writegsbase_u64(gs);
if (worker_id == ~uint32_t{0}) {
worker_id = wid;
}
return sp;
}
extern "C" void swap_helper(pcontext* next_ctx) {
auto wid = worker_id;
pcontext::get_current_context()->xsave();
pcontext::set_current_context(next_ctx);
next_ctx->xrstor();
auto fs = next_ctx->fs;
auto gs = next_ctx->gs;
ASSERT(fs != 0);
_writefsbase_u64(fs);
_writegsbase_u64(gs);
if (worker_id == ~uint32_t{0}) {
worker_id = wid;
}
}
asm(R"(
.text
.globl interrupt_handler_func
.type interrupt_handler_func, @function
interrupt_handler_func:
.cfi_startproc
.cfi_def_cfa_offset 16
endbr64
// stack
// ui_frame: rsp
// rflags
// rip
// vector <- rsp point to
// so need to add 8 to point to skip vector
addq $8, %rsp # pop vector
pushq %rax
pushq %rbx
// check if rip is in swap_context, if so, quick exit
movq 0x10(%rsp), %rax # rax = rip
.check_swap_context:
leaq .l_end_swap_context(%rip), %rbx
cmpq %rbx, %rax #
jg .continue_uintr_handler # if rip > .l_end_swap_context, then continue uintr
leaq .l_swap_context(%rip), %rbx
cmpq %rbx, %rax
jg .uintr_quick_exit # if rip > .l_swap_context, then quick exit uintr
.continue_uintr_handler:
pushq %rcx
pushq %rdx
pushq %rbp
pushq %rsi
pushq %rdi
pushq %r8
pushq %r9
pushq %r10
pushq %r11
pushq %r12
pushq %r13
pushq %r14
pushq %r15
# call handler_helper, pass current rsp as first param
movq %rsp, %rdi
cld
call handler_helper
# rax = curr_ctx
# switch stack, rsp = curr_ctx->reg[0]
movq %rax, %rsp
popq %r15
popq %r14
popq %r13
popq %r12
popq %r11
popq %r10
popq %r9
popq %r8
popq %rdi
popq %rsi
popq %rbp
popq %rdx
popq %rcx
popq %rbx
popq %rax
uiret
.uintr_quick_exit:
popq %rbx
popq %rax
uiret
.cfi_endproc
.size interrupt_handler_func, .-interrupt_handler_func
.section .rodata
.text
)");
asm(R"(
.text
.globl init_stack
.type init_stack, @function
init_stack:
.cfi_startproc
endbr64
# temp save rsp
movq %rsp, %rcx
movq %rdi, %rsp
# build ui_frame
andq $0xfffffffffffffff0, %rsp
pushq %rdi
pushfq
pushq %rsi # important: 2nd paramater, function pointer
pushq %rax
pushq %rbx
pushq %rcx
pushq %rdx
pushq %rdi # important: rdi is rbp
pushq %rsi
pushq %rdi
pushq %r8
pushq %r9
pushq %r10
pushq %r11
pushq %r12
pushq %r13
pushq %r14
pushq %r15
# save rsp as return value
movq %rsp, %rax
movq %rcx, %rsp
ret
.cfi_endproc
.size init_stack, .-init_stack
.section .rodata
.text
)");
asm(R"(
.text
.globl swap_context
.type swap_context, @function
swap_context:
.l_swap_context:
.cfi_startproc
endbr64
clui
# simulate uintr frame
// stack
// ui_frame: rsp
// rflags
// rip
// use caller saved register
// %rcx = rip
popq %rcx
pushq %rsp
pushfq
pushq %rcx
# caller saved registers are dummy data,
# still need to save them, but they don't need to be accurate
pushq %rax
pushq %rbx
pushq %rcx
pushq %rdx
pushq %rbp
pushq %rsi
pushq %rdi
pushq %r8
pushq %r9
pushq %r10
pushq %r11
pushq %r12
pushq %r13
pushq %r14
pushq %r15
# current_context.reg[0] = rsp
movq %rsp, 0x2000(%rdi)
# temperatory save rsi (2nd param)
pushq %rsi
movq %rsi, %rdi
cld
call swap_helper
popq %rsi
# switch stack
movq 0x2000(%rsi), %rsp
popq %r15
popq %r14
popq %r13
popq %r12
popq %r11
popq %r10
popq %r9
popq %r8
popq %rdi
popq %rsi
popq %rbp
popq %rdx
popq %rcx
// rsp
// rflags
// rip
// rax
// rbx <- rsp points to
movq 32(%rsp), %rax
// rbx = rip
movq 16(%rsp), %rbx
movq %rbx, -0x80(%rax)
popq %rbx
popq %rax
addq $8, %rsp
popfq
popq %rsp
stui
jmp *-0x80(%rsp)
.l_end_swap_context:
nop
.cfi_endproc
.size swap_context, .-swap_context
.section .rodata
.text
)");