-
Notifications
You must be signed in to change notification settings - Fork 203
/
Copy pathhost_profile.c
448 lines (360 loc) · 13.1 KB
/
host_profile.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
/* SPDX-License-Identifier: LGPL-3.0-or-later */
/* Copyright (C) 2020 Intel Corporation
* Paweł Marczewski <pawel@invisiblethingslab.com>
*/
/*
* SGX profiling. This code takes samples of running code and writes them out to a perf.data file
* (see also sgx_perf_data.c).
*/
#ifdef DEBUG
#define USE_STDLIB
#include <assert.h>
#include <errno.h>
#include <linux/fs.h>
#include <linux/limits.h>
#include <stddef.h>
#include "cpu.h"
#include "debug_map.h"
#include "elf/elf.h"
#include "host_internal.h"
#include "host_log.h"
#include "linux_utils.h"
#include "pal_tcb.h"
#include "spinlock.h"
#ifdef SGX_VTUNE_PROFILE
#include "ittnotify.h" // for __itt_module_load(...) which is defined as a macro
#endif
// FIXME: this is glibc realpath, declared here because the headers will conflict with PAL
char* realpath(const char* path, char* resolved_path);
#define NSEC_IN_SEC 1000000000
/* Filenames for saved data */
#define SGX_PROFILE_FILENAME_WITH_PID "sgx-perf-%d-%lu.data"
static spinlock_t g_perf_data_lock = INIT_SPINLOCK_UNLOCKED;
static struct perf_data* g_perf_data = NULL;
static bool g_profile_enabled = false;
static int g_profile_mode;
static uint64_t g_profile_period;
static int g_mem_fd = -1;
/* Read memory from inside enclave (using /proc/self/mem). */
static ssize_t debug_read(void* dest, void* addr, size_t size) {
ssize_t ret;
size_t total = 0;
while (total < size) {
ret = DO_SYSCALL(pread64, g_mem_fd, (uint8_t*)dest + total, size - total,
(off_t)addr + total);
if (ret == -EINTR)
continue;
if (ret < 0)
return ret;
if (ret == 0)
break;
assert(ret > 0);
assert((size_t)ret + total <= size);
total += ret;
}
return total;
}
static int debug_read_all(void* dest, void* addr, size_t size) {
ssize_t ret = debug_read(dest, addr, size);
if (ret < 0)
return ret;
if ((size_t)ret < size)
return -EINVAL;
return 0;
}
static int get_sgx_gpr(sgx_pal_gpr_t* gpr, void* tcs) {
int ret;
uint64_t ossa;
uint32_t cssa;
ret = debug_read_all(&ossa, tcs + 16, sizeof(ossa));
if (ret < 0)
return ret;
ret = debug_read_all(&cssa, tcs + 24, sizeof(cssa));
if (ret < 0)
return ret;
void* gpr_addr = (void*)(
g_pal_enclave.baseaddr
+ ossa + cssa * g_pal_enclave.ssa_frame_size
- sizeof(*gpr));
ret = debug_read_all(gpr, gpr_addr, sizeof(*gpr));
if (ret < 0)
return ret;
return 0;
}
int sgx_profile_init(void) {
int ret;
assert(!g_profile_enabled);
assert(!g_perf_data);
g_profile_period = NSEC_IN_SEC / g_pal_enclave.profile_frequency;
g_profile_mode = g_pal_enclave.profile_mode;
// File `/proc/self/mem` is open once and remain open during the lifecycle of the process
if (g_mem_fd < 0) {
ret = DO_SYSCALL(open, "/proc/self/mem", O_RDONLY | O_LARGEFILE | O_CLOEXEC, 0);
if (ret < 0) {
log_error("sgx_profile_init: opening /proc/self/mem failed: %s", unix_strerror(ret));
return ret;
}
g_mem_fd = ret;
}
struct timespec ts;
ret = DO_SYSCALL(clock_gettime, CLOCK_REALTIME, &ts);
if (ret < 0) {
log_error("sgx_profile_init: clock_gettime failed: %s", unix_strerror(ret));
return ret;
}
snprintf(g_pal_enclave.profile_filename, ARRAY_SIZE(g_pal_enclave.profile_filename),
SGX_PROFILE_FILENAME_WITH_PID, (int)g_host_pid, ts.tv_sec);
struct perf_data* pd = pd_open(g_pal_enclave.profile_filename, g_pal_enclave.profile_with_stack);
if (!pd) {
log_error("sgx_profile_init: pd_open failed");
return -EINVAL;
}
g_perf_data = pd;
ret = pd_event_command(pd, "pal-sgx", g_host_pid, /*tid=*/g_host_pid);
if (!pd) {
log_error("sgx_profile_init: reporting command failed: %s", unix_strerror(ret));
goto out;
}
g_profile_enabled = true;
return 0;
out:
if (g_perf_data) {
ssize_t close_ret = pd_close(g_perf_data);
if (close_ret < 0) {
log_error("sgx_profile_init: pd_close failed: %s", unix_strerror(close_ret));
}
g_perf_data = NULL;
}
return ret;
}
static void sgx_profile_finish_thread_unsafe(void) {
ssize_t size;
if (!g_profile_enabled)
return;
size = pd_close(g_perf_data);
if (size < 0)
log_error("sgx_profile_finish: pd_close failed: %s", unix_strerror(size));
g_perf_data = NULL;
log_always("Profile data written to %s (%lu bytes)", g_pal_enclave.profile_filename, size);
g_profile_enabled = false;
}
void sgx_profile_finish(void) {
spinlock_lock(&g_perf_data_lock);
sgx_profile_finish_thread_unsafe();
spinlock_unlock(&g_perf_data_lock);
}
static void sgx_profile_report_elf_thread_unsafe(const char* filename, void* addr) {
int ret;
if (!g_profile_enabled && !g_vtune_profile_enabled)
return;
if (!strcmp(filename, ""))
filename = get_main_exec_path();
if (!strcmp(filename, "[vdso]") || !strcmp(filename, "[vdso_libos]")) {
/*
* Our SGX profiler currently does not support reporting perf events in vDSO binaries:
* host-Linux `[vdso]` and enclave-LibOS `[vdso_libos]`. It is hard to find these vDSO
* binaries: the former is embedded in host Linux and the latter is embedded in LibOS
* `libsysdb.so` binary. The assumption is that vDSO is never a perf bottleneck in SGX
* environments, so we don't lose much precision in our profiler.
*/
return;
}
// Convert filename to absolute path - some tools (e.g. libunwind in 'perf report') refuse to
// process relative paths.
char buf[PATH_MAX];
char* path = realpath(filename, buf);
if (!path) {
log_error("sgx_profile_report_elf(%s): realpath failed", filename);
return;
}
// Open the file and mmap it.
int fd = DO_SYSCALL(open, path, O_RDONLY | O_CLOEXEC, 0);
if (fd < 0) {
log_error("sgx_profile_report_elf(%s): open failed: %s", filename, unix_strerror(fd));
return;
}
off_t elf_length = DO_SYSCALL(lseek, fd, 0, SEEK_END);
if (elf_length < 0) {
log_error("sgx_profile_report_elf(%s): lseek failed: %s", filename,
unix_strerror(elf_length));
goto out_close;
}
void* elf_addr = (void*)DO_SYSCALL(mmap, NULL, elf_length, PROT_READ, MAP_PRIVATE, fd, 0);
if (IS_PTR_ERR(elf_addr)) {
log_error("sgx_profile_report_elf(%s): mmap failed: %s", filename,
unix_strerror(PTR_TO_ERR(addr)));
goto out_close;
}
// Perform a simple sanity check to verify if this looks like ELF (see TODO for PalDebugMapAdd
// in pal/src/pal_rtld.c).
const elf_ehdr_t* ehdr = elf_addr;
if (elf_length < (off_t)sizeof(*ehdr) || memcmp(ehdr->e_ident, ELFMAG, SELFMAG) != 0) {
log_error("sgx_profile_report_elf(%s): invalid ELF binary", filename);
goto out_unmap;
}
// Read the program headers and record mmap events for the segments that should be mapped as
// executable.
const elf_phdr_t* phdr = (const elf_phdr_t*)((uintptr_t)elf_addr + ehdr->e_phoff);
ret = 0;
for (unsigned int i = 0; i < ehdr->e_phnum; i++) {
if (phdr[i].p_type == PT_LOAD && phdr[i].p_flags & PF_X) {
uint64_t mapstart = ALLOC_ALIGN_DOWN(phdr[i].p_vaddr);
uint64_t mapend = ALLOC_ALIGN_UP(phdr[i].p_vaddr + phdr[i].p_filesz);
uint64_t offset = ALLOC_ALIGN_DOWN(phdr[i].p_offset);
if (g_profile_enabled) {
ret = pd_event_mmap(g_perf_data, path, g_host_pid,
(uint64_t)addr + mapstart, mapend - mapstart, offset);
if (ret < 0)
break;
}
#ifdef SGX_VTUNE_PROFILE
if (g_vtune_profile_enabled)
__itt_module_load((void*)addr + mapstart, (void*)addr + mapend - 1, path);
#endif
}
}
if (ret < 0) {
log_error("sgx_profile_report_elf(%s): pd_event_mmap failed: %s", filename,
unix_strerror(ret));
}
// Clean up.
out_unmap:
ret = DO_SYSCALL(munmap, elf_addr, elf_length);
if (ret < 0)
log_error("sgx_profile_report_elf(%s): munmap failed: %s", filename, unix_strerror(ret));
out_close:
ret = DO_SYSCALL(close, fd);
if (ret < 0)
log_error("sgx_profile_report_elf(%s): close failed: %s", filename, unix_strerror(ret));
}
void sgx_profile_report_elf(const char* filename, void* addr) {
spinlock_lock(&g_perf_data_lock);
sgx_profile_report_elf_thread_unsafe(filename, addr);
spinlock_unlock(&g_perf_data_lock);
}
/* Re-initialize based on g_pal_enclave settings */
static int sgx_profile_reinit(void) {
assert(spinlock_is_locked(&g_perf_data_lock));
int ret;
sgx_profile_finish_thread_unsafe();
ret = sgx_profile_init();
if (ret < 0) {
log_error("sgx_profile_reinit failed: %s", unix_strerror(ret));
return ret;
}
/* Report all ELFs already loaded */
struct debug_map* map = g_debug_map;
while (map) {
sgx_profile_report_elf_thread_unsafe(map->name, map->addr);
map = map->next;
}
return 0;
}
static void sample_simple(uint64_t rip) {
int ret;
spinlock_lock(&g_perf_data_lock);
if (__atomic_exchange_n(&g_pal_enclave.profile_delayed_reinit, false, __ATOMIC_ACQ_REL) == true)
sgx_profile_reinit();
// Report all events as the same PID so that they are grouped in report.
ret = pd_event_sample_simple(g_perf_data, rip, g_host_pid, /*tid=*/g_host_pid,
g_profile_period);
spinlock_unlock(&g_perf_data_lock);
if (ret < 0) {
log_error("recording sample failed: %s", unix_strerror(ret));
}
}
static void sample_stack(sgx_pal_gpr_t* gpr) {
int ret;
uint8_t stack[PD_STACK_SIZE];
size_t stack_size;
ret = debug_read(stack, (void*)gpr->rsp, sizeof(stack));
if (ret < 0) {
log_error("reading stack failed: %s", unix_strerror(ret));
return;
}
stack_size = ret;
spinlock_lock(&g_perf_data_lock);
if (__atomic_exchange_n(&g_pal_enclave.profile_delayed_reinit, false, __ATOMIC_ACQ_REL) == true)
sgx_profile_reinit();
// Report all events as the same PID so that they are grouped in report.
ret = pd_event_sample_stack(g_perf_data, gpr->rip, g_host_pid, /*tid=*/g_host_pid,
g_profile_period, gpr, stack, stack_size);
spinlock_unlock(&g_perf_data_lock);
if (ret < 0) {
log_error("recording sample failed: %s", unix_strerror(ret));
}
}
/*
* Update sample time, and return true if we should record a new sample.
*
* In case of AEX, we can use it to record a sample approximately every 'g_profile_period'
* nanoseconds. Note that we rely on Linux scheduler to generate an AEX event 250 times per second
* (although other events may cause an AEX to happen more often), so sampling frequency greater than
* 250 cannot be reliably achieved.
*/
static bool update_time(void) {
// Check current CPU time
struct timespec ts;
int ret = DO_SYSCALL(clock_gettime, CLOCK_THREAD_CPUTIME_ID, &ts);
if (ret < 0) {
log_error("sgx_profile_sample: clock_gettime failed: %s", unix_strerror(ret));
return false;
}
uint64_t sample_time = ts.tv_sec * NSEC_IN_SEC + ts.tv_nsec;
// Compare and update last recorded time per thread
PAL_HOST_TCB* tcb = pal_get_host_tcb();
if (tcb->profile_sample_time == 0) {
tcb->profile_sample_time = sample_time;
return false;
}
if (sample_time - tcb->profile_sample_time >= g_profile_period) {
tcb->profile_sample_time = sample_time;
assert(sample_time >= tcb->profile_sample_time);
return true;
}
return false;
}
void sgx_profile_sample_aex(void* tcs) {
int ret;
if (!(g_profile_enabled && g_profile_mode == SGX_PROFILE_MODE_AEX))
return;
if (!update_time())
return;
sgx_pal_gpr_t gpr;
ret = get_sgx_gpr(&gpr, tcs);
if (ret < 0) {
log_error("sgx_profile_sample_aex: error reading GPR: %s", unix_strerror(ret));
return;
}
if (g_pal_enclave.profile_with_stack) {
sample_stack(&gpr);
} else {
sample_simple(gpr.rip);
}
}
void sgx_profile_sample_ocall_inner(void* enclave_gpr) {
int ret;
if (!(g_profile_enabled && g_profile_mode == SGX_PROFILE_MODE_OCALL_INNER))
return;
if (!enclave_gpr)
return;
sgx_pal_gpr_t gpr;
ret = debug_read_all(&gpr, enclave_gpr, sizeof(gpr));
if (ret < 0) {
log_error("sgx_profile_sample_ocall_inner: error reading GPR: %s", unix_strerror(ret));
return;
}
if (g_pal_enclave.profile_with_stack) {
sample_stack(&gpr);
} else {
sample_simple(gpr.rip);
}
}
void sgx_profile_sample_ocall_outer(void* ocall_func) {
if (!(g_profile_enabled && g_profile_mode == SGX_PROFILE_MODE_OCALL_OUTER))
return;
assert(ocall_func);
assert(!g_pal_enclave.profile_with_stack);
sample_simple((uint64_t)ocall_func);
}
#endif /* DEBUG */