-
Notifications
You must be signed in to change notification settings - Fork 203
/
Copy pathhost_thread.c
461 lines (389 loc) · 15.2 KB
/
host_thread.c
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
/* SPDX-License-Identifier: LGPL-3.0-or-later */
#include <stddef.h> /* needed by <linux/signal.h> for size_t */
#include <asm/errno.h>
#include <asm/prctl.h>
#include <asm/signal.h>
#include <linux/signal.h>
#include "asan.h"
#include "assert.h"
#include "gdb_integration/sgx_gdb.h"
#include "host_ecalls.h"
#include "host_internal.h"
#include "spinlock.h"
struct enclave_thread_map {
unsigned int tid;
sgx_arch_tcs_t* tcs;
};
static struct enclave_thread_map* g_enclave_thread_map = NULL;
static spinlock_t g_enclave_thread_map_lock = INIT_SPINLOCK_UNLOCKED;
/* total number of items in g_enclave_thread_map; protected by g_enclave_thread_map_lock */
static size_t g_enclave_thread_num = 0;
bool g_sgx_enable_stats = false;
void update_print_and_reset_stats(bool process_wide) {
static atomic_ulong g_eenter_cnt = 0;
static atomic_ulong g_eexit_cnt = 0;
static atomic_ulong g_aex_cnt = 0;
static atomic_ulong g_sync_signal_cnt = 0;
static atomic_ulong g_async_signal_cnt = 0;
if (!g_sgx_enable_stats)
return;
PAL_HOST_TCB* tcb = pal_get_host_tcb();
int tid = DO_SYSCALL(gettid);
assert(tid > 0);
log_always("----- SGX stats for thread %d -----\n"
" # of EENTERs: %lu\n"
" # of EEXITs: %lu\n"
" # of AEXs: %lu\n"
" # of sync signals: %lu\n"
" # of async signals: %lu",
tid, tcb->eenter_cnt, tcb->eexit_cnt, tcb->aex_cnt,
tcb->sync_signal_cnt, tcb->async_signal_cnt);
g_eenter_cnt += tcb->eenter_cnt;
g_eexit_cnt += tcb->eexit_cnt;
g_aex_cnt += tcb->aex_cnt;
g_sync_signal_cnt += tcb->sync_signal_cnt;
g_async_signal_cnt += tcb->async_signal_cnt;
tcb->eenter_cnt = 0;
tcb->eexit_cnt = 0;
tcb->aex_cnt = 0;
tcb->sync_signal_cnt = 0;
tcb->async_signal_cnt = 0;
tcb->reset_stats = false;
if (process_wide) {
int pid = g_host_pid;
assert(pid > 0);
log_always("----- Total SGX stats for process %d -----\n"
" # of EENTERs: %lu\n"
" # of EEXITs: %lu\n"
" # of AEXs: %lu\n"
" # of sync signals: %lu\n"
" # of async signals: %lu",
pid, g_eenter_cnt, g_eexit_cnt, g_aex_cnt,
g_sync_signal_cnt, g_async_signal_cnt);
g_eenter_cnt = 0;
g_eexit_cnt = 0;
g_aex_cnt = 0;
g_sync_signal_cnt = 0;
g_async_signal_cnt = 0;
}
}
void pal_host_tcb_init(PAL_HOST_TCB* tcb, void* stack, void* alt_stack) {
tcb->self = tcb;
tcb->tcs = NULL; /* initialized by child thread */
tcb->stack = stack;
tcb->alt_stack = alt_stack;
tcb->eenter_cnt = 0;
tcb->eexit_cnt = 0;
tcb->aex_cnt = 0;
tcb->sync_signal_cnt = 0;
tcb->async_signal_cnt = 0;
tcb->reset_stats = false;
tcb->profile_sample_time = 0;
tcb->last_async_event = PAL_EVENT_NO_EVENT;
}
int create_tcs_mapper(void* tcs_base, unsigned int thread_num) {
sgx_arch_tcs_t* enclave_tcs = tcs_base;
g_enclave_thread_map = malloc(sizeof(struct enclave_thread_map) * thread_num);
if (!g_enclave_thread_map) {
return -ENOMEM;
}
for (uint32_t i = 0; i < thread_num; i++) {
g_enclave_thread_map[i].tid = 0;
g_enclave_thread_map[i].tcs = &enclave_tcs[i];
}
g_enclave_thread_num = thread_num;
return 0;
}
static int add_dynamic_tcs(sgx_arch_tcs_t* tcs) {
int ret;
struct enclave_dbginfo* dbginfo = (struct enclave_dbginfo*)DBGINFO_ADDR;
ret = set_tcs_debug_flag_if_debugging((void**)&tcs, /*count=*/1);
if (ret < 0) {
return ret;
}
size_t i = 0;
spinlock_lock(&g_enclave_thread_map_lock);
for (i = 0; i < g_enclave_thread_num; i++) {
if (g_enclave_thread_map[i].tcs == tcs) {
log_error("Dynamic TCS page %p was already added to the list of enclave threads", tcs);
BUG();
}
if (!g_enclave_thread_map[i].tcs) {
g_enclave_thread_map[i].tcs = tcs;
dbginfo->tcs_addrs[i] = tcs;
break;
}
}
if (i == g_enclave_thread_num) {
/* Current map is full. */
if (g_enclave_thread_num >= MAX_DBG_THREADS) {
log_error("Number of simultaneous enclave threads equals to or exceeds %u, "
"not supported", MAX_DBG_THREADS);
ret = -EOVERFLOW;
goto out;
}
size_t new_enclave_thread_num = MIN(g_enclave_thread_num * 2, (size_t)MAX_DBG_THREADS);
struct enclave_thread_map* new_enclave_thread_map = realloc(
g_enclave_thread_map, sizeof(struct enclave_thread_map) * new_enclave_thread_num);
if (!new_enclave_thread_map) {
ret = -ENOMEM;
goto out;
}
memset(new_enclave_thread_map + g_enclave_thread_num, 0,
sizeof(struct enclave_thread_map) * (new_enclave_thread_num - g_enclave_thread_num));
g_enclave_thread_num = new_enclave_thread_num;
g_enclave_thread_map = new_enclave_thread_map;
g_enclave_thread_map[i].tcs = tcs;
dbginfo->tcs_addrs[i] = tcs;
}
ret = 0;
out:
spinlock_unlock(&g_enclave_thread_map_lock);
return ret;
}
void map_tcs(unsigned int tid) {
while (true) {
spinlock_lock(&g_enclave_thread_map_lock);
for (size_t i = 0; i < g_enclave_thread_num; i++) {
if (!g_enclave_thread_map[i].tcs)
continue;
if (!g_enclave_thread_map[i].tid) {
g_enclave_thread_map[i].tid = tid;
pal_get_host_tcb()->tcs = g_enclave_thread_map[i].tcs;
((struct enclave_dbginfo*)DBGINFO_ADDR)->thread_tids[i] = tid;
spinlock_unlock(&g_enclave_thread_map_lock);
return;
}
}
if (!g_pal_enclave.edmm_enabled) {
/* no static or dynamic TCS pages available, bail out */
spinlock_unlock(&g_enclave_thread_map_lock);
return;
}
spinlock_unlock(&g_enclave_thread_map_lock);
/*
* At least one dynamic TCS is available in the in-enclave map of TCSs. However,
* the host-enclave map of TCSs may be briefly out of sync with the in-enclave map
* because the enclave decided to reuse some TCS that is being currently unmapped
* by another thread -- in this case, the host-enclave map may still have all TCS slots
* occupied, but only for a small window of time until the exiting thread calls
* `unmap_my_tcs()`.
*/
CPU_RELAX();
}
}
void unmap_my_tcs(void) {
size_t i = 0;
spinlock_lock(&g_enclave_thread_map_lock);
for (i = 0; i < g_enclave_thread_num; i++)
if (g_enclave_thread_map[i].tcs == pal_get_host_tcb()->tcs) {
g_enclave_thread_map[i].tid = 0;
((struct enclave_dbginfo*)DBGINFO_ADDR)->thread_tids[i] = 0;
break;
}
assert(i < g_enclave_thread_num);
pal_get_host_tcb()->tcs = NULL;
spinlock_unlock(&g_enclave_thread_map_lock);
}
int current_enclave_thread_cnt(void) {
int ret = 0;
spinlock_lock(&g_enclave_thread_map_lock);
for (size_t i = 0; i < g_enclave_thread_num; i++)
if (g_enclave_thread_map[i].tid)
ret++;
spinlock_unlock(&g_enclave_thread_map_lock);
return ret;
}
/*
* pal_thread_init(): An initialization wrapper of a newly-created thread (including
* the first thread). This function accepts a TCB pointer to be set to the GS register
* of the thread. The rest of the TCB is used as the alternative stack for signal
* handling. Notice that this sets up the untrusted thread -- an enclave thread is set
* up by other means (e.g., the GS register is set by an SGX-enforced TCS.OGSBASGX).
*/
__attribute_no_sanitize_address
int pal_thread_init(void* tcbptr) {
PAL_HOST_TCB* tcb = tcbptr;
int ret;
/* set GS reg of this thread to thread's TCB; after this point, can use pal_get_host_tcb() */
ret = DO_SYSCALL(arch_prctl, ARCH_SET_GS, tcb);
if (ret < 0) {
ret = -EPERM;
goto out;
}
if (tcb->alt_stack) {
stack_t ss = {
.ss_sp = tcb->alt_stack,
.ss_flags = 0,
.ss_size = ALT_STACK_SIZE - sizeof(*tcb)
};
ret = DO_SYSCALL(sigaltstack, &ss, NULL);
if (ret < 0) {
ret = -EPERM;
goto out;
}
}
int tid = DO_SYSCALL(gettid);
map_tcs(tid); /* updates tcb->tcs */
if (!tcb->tcs) {
log_error("There are no available TCS pages left for a new thread. Please try to increase"
" sgx.max_threads in the manifest. The current value is %lu",
g_pal_enclave.thread_num);
ret = -ENOMEM;
goto out;
}
if (!tcb->stack) {
/* only first thread doesn't have a stack (it uses the one provided by Linux); first
* thread calls ecall_enclave_start() instead of ecall_thread_start() so just exit */
return 0;
}
__atomic_store_n(tcb->start_status_ptr, 0, __ATOMIC_RELAXED);
/* not-first (child) thread, start it */
ecall_thread_start();
unmap_my_tcs();
ret = 0;
out:
if (ret != 0)
__atomic_store_n(tcb->start_status_ptr, ret, __ATOMIC_RELAXED);
return ret;
}
__attribute_no_sanitize_address
noreturn void thread_exit(int status) {
PAL_HOST_TCB* tcb = pal_get_host_tcb();
/* technically, async signals were already blocked before calling this function
* (by sgx_ocall_exit()) but we keep it here for future proof */
block_async_signals(true);
#ifdef DEBUG
abort_current_reset_stats(DO_SYSCALL(gettid));
update_print_and_reset_stats(/*process_wide=*/false);
#endif /* DEBUG */
if (tcb->alt_stack) {
stack_t ss;
ss.ss_sp = NULL;
ss.ss_flags = SS_DISABLE;
ss.ss_size = 0;
/* take precautions to unset the TCB and alternative stack first */
DO_SYSCALL(arch_prctl, ARCH_SET_GS, 0);
DO_SYSCALL(sigaltstack, &ss, NULL);
}
#ifdef ASAN
asan_unpoison_current_stack((uintptr_t)tcb->stack, THREAD_STACK_SIZE + ALT_STACK_SIZE);
#endif
/* free the thread stack (via munmap) and exit; note that exit() needs a "status" arg
* but it could be allocated on a stack, so we must put it in register and do asm */
__asm__ volatile("cmpq $0, %%rdi \n" /* check if tcb->stack != NULL */
"je 1f \n"
"syscall \n" /* all args are already prepared, call munmap */
"1: \n"
"mov %[nr_exit], %%rax \n"
"mov %[exit_code], %%edi \n"
"syscall \n" /* all args are prepared, call exit */
"ud2 \n"
"jmp 1b \n"
:
: "a" (__NR_munmap), "D" (tcb->stack), "S" (THREAD_STACK_SIZE + ALT_STACK_SIZE),
[nr_exit] "i" (__NR_exit), [exit_code] "r" (status)
: "memory", "rcx", "r11"
);
__builtin_unreachable();
}
int clone_thread(void* dynamic_tcs) {
int ret = 0;
if (dynamic_tcs) {
/* enclave decided to add a new TCS page (to accommodate more enclave threads) */
ret = add_dynamic_tcs(dynamic_tcs);
if (ret < 0) {
return ret;
}
}
void* stack = (void*)DO_SYSCALL(mmap, NULL, THREAD_STACK_SIZE + ALT_STACK_SIZE,
PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (IS_PTR_ERR(stack))
return -ENOMEM;
/* Stack layout for the new thread looks like this (recall that stacks grow towards lower
* addresses on Linux on x86-64):
*
* stack +--> +-------------------+
* | child stack | THREAD_STACK_SIZE
* child_stack +--> +-------------------+
* | alternate stack | ALT_STACK_SIZE - sizeof(PAL_HOST_TCB)
* tcb +--> +-------------------+
* | PAL TCB | sizeof(PAL_HOST_TCB)
* +-------------------+
*
* Note that this whole memory region is zeroed out because we use mmap(). */
void* child_stack_top = stack + THREAD_STACK_SIZE;
/* initialize TCB at the top of the alternative stack */
PAL_HOST_TCB* tcb = child_stack_top + ALT_STACK_SIZE - sizeof(PAL_HOST_TCB);
pal_host_tcb_init(tcb, stack, child_stack_top);
/* align child_stack to 16 */
child_stack_top = ALIGN_DOWN_PTR(child_stack_top, 16);
int start_status = 1;
tcb->start_status_ptr = &start_status;
ret = clone(pal_thread_init, child_stack_top,
CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SYSVSEM | CLONE_THREAD | CLONE_SIGHAND,
tcb, /*parent_tid=*/NULL, /*tls=*/NULL, /*child_tid=*/NULL, thread_exit);
if (ret < 0) {
DO_SYSCALL(munmap, stack, THREAD_STACK_SIZE + ALT_STACK_SIZE);
return ret;
}
while ((ret = __atomic_load_n(&start_status, __ATOMIC_RELAXED)) == 1)
CPU_RELAX();
return ret;
}
int get_tid_from_tcs(void* tcs) {
int tid = 0;
spinlock_lock(&g_enclave_thread_map_lock);
for (size_t i = 0; i < g_enclave_thread_num; i++) {
if (g_enclave_thread_map[i].tcs == tcs) {
tid = g_enclave_thread_map[i].tid;
break;
}
}
spinlock_unlock(&g_enclave_thread_map_lock);
return tid ? tid : -EINVAL;
}
int set_tcs_debug_flag_if_debugging(void* tcs_addrs[], size_t count) {
if (!g_sgx_enable_stats && !g_vtune_profile_enabled)
return 0;
/* set TCS.FLAGS.DBGOPTIN in enclave threads to enable perf counters, Intel PT, etc */
int ret = DO_SYSCALL(open, "/proc/self/mem", O_RDWR | O_LARGEFILE | O_CLOEXEC, 0);
if (ret < 0) {
log_error("Setting TCS.FLAGS.DBGOPTIN failed: %s", unix_strerror(ret));
return ret;
}
int enclave_mem = ret;
for (size_t i = 0; i < count; i++) {
uint64_t tcs_flags;
uint64_t* tcs_flags_ptr = tcs_addrs[i] + offsetof(sgx_arch_tcs_t, flags);
ret = DO_SYSCALL(pread64, enclave_mem, &tcs_flags, sizeof(tcs_flags), (off_t)tcs_flags_ptr);
if (ret < 0) {
log_error("Reading TCS.FLAGS.DBGOPTIN failed: %s", unix_strerror(ret));
goto out;
}
tcs_flags |= TCS_FLAGS_DBGOPTIN;
ret = DO_SYSCALL(pwrite64, enclave_mem, &tcs_flags, sizeof(tcs_flags),
(off_t)tcs_flags_ptr);
if (ret < 0) {
log_error("Writing TCS.FLAGS.DBGOPTIN failed: %s", unix_strerror(ret));
goto out;
}
}
ret = 0;
out:
DO_SYSCALL(close, enclave_mem);
return ret;
}
size_t broadcast_signal_to_threads(int sig, int exclude_tid) {
size_t threads_num = 0;
spinlock_lock(&g_enclave_thread_map_lock);
for (size_t i = 0; i < g_enclave_thread_num; i++) {
int thread_tid = g_enclave_thread_map[i].tid;
if (!thread_tid || thread_tid == exclude_tid)
continue;
DO_SYSCALL(tgkill, g_host_pid, thread_tid, sig);
threads_num++;
}
spinlock_unlock(&g_enclave_thread_map_lock);
return threads_num;
}