-
Notifications
You must be signed in to change notification settings - Fork 203
/
Copy pathhost_entry.S
201 lines (157 loc) · 4.56 KB
/
host_entry.S
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
#include "sgx_arch.h"
#include "asm-offsets.h"
.extern tcs_base
.extern g_in_aex_profiling
.extern dump_and_reset_stats
.global sgx_ecall
.type sgx_ecall, @function
sgx_ecall:
.cfi_startproc
# put entry address in RDX
leaq .Lsgx_entry(%rip), %rdx
# other arguments: RDI - code, RSI - ms
.Ldo_ecall_callee_save:
pushq %rbx
.cfi_adjust_cfa_offset 8
pushq %rbp
.cfi_adjust_cfa_offset 8
pushq %r12
.cfi_adjust_cfa_offset 8
pushq %r13
.cfi_adjust_cfa_offset 8
pushq %r14
.cfi_adjust_cfa_offset 8
pushq %r15
.cfi_adjust_cfa_offset 8
.Ldo_ecall:
# increment per-thread EENTER counter for stats
lock incq %gs:PAL_HOST_TCB_EENTER_CNT
# RBX has to be the TCS of the thread
movq %gs:PAL_HOST_TCB_TCS, %rbx
# RCX has to be the AEP (Asynchronous Exit Pointer)
leaq async_exit_pointer(%rip), %rcx
movq $EENTER, %rax
enclu
# currently only ECALL_THREAD_RESET returns
.Lafter_resume:
popq %r15
.cfi_adjust_cfa_offset -8
popq %r14
.cfi_adjust_cfa_offset -8
popq %r13
.cfi_adjust_cfa_offset -8
popq %r12
.cfi_adjust_cfa_offset -8
popq %rbp
.cfi_adjust_cfa_offset -8
popq %rbx
.cfi_adjust_cfa_offset -8
retq
.cfi_endproc
.global async_exit_pointer
.type async_exit_pointer, @function
async_exit_pointer:
.cfi_startproc
.cfi_undefined %rip
# increment per-thread AEX counter for stats
lock incq %gs:PAL_HOST_TCB_AEX_CNT
# Inform that we are in AEX profiling code
movb $1, %gs:PAL_HOST_TCB_IN_AEX_PROF
subq $RED_ZONE_SIZE, %rsp
.cfi_adjust_cfa_offset RED_ZONE_SIZE
# FIXME: This second RSP adjustment is because Exitless re-uses the same stack to create
# RPC request objects, as the AEX flow here. It just so happens that the total size of
# allocated RPC request objects is less than 128B (red zone size), so I reuse this const.
# This double-stack-usage needs a proper fix.
subq $RED_ZONE_SIZE, %rsp
.cfi_adjust_cfa_offset RED_ZONE_SIZE
# Save ERESUME parameters
pushq %rax
.cfi_adjust_cfa_offset 8
pushq %rbx
.cfi_adjust_cfa_offset 8
pushq %rcx
.cfi_adjust_cfa_offset 8
pushq %rbp
.cfi_adjust_cfa_offset 8
movq %rsp, %rbp
.cfi_offset %rbp, -16
.cfi_def_cfa_register %rbp
andq $~0xF, %rsp # Required by System V AMD64 ABI.
#ifdef DEBUG
# Call sgx_profile_sample_aex with %rdi = TCS
movq %rbx, %rdi
call sgx_profile_sample_aex
#endif
call dump_and_reset_stats
# Restore stack
movq %rbp, %rsp
popq %rbp
.cfi_def_cfa %rsp, 8
# Restore ERESUME parameters
popq %rcx
.cfi_adjust_cfa_offset -8
popq %rbx
.cfi_adjust_cfa_offset -8
popq %rax
.cfi_adjust_cfa_offset -8
addq $RED_ZONE_SIZE, %rsp
.cfi_adjust_cfa_offset -RED_ZONE_SIZE
# FIXME: See above for explanation for this second RSP adjustment.
addq $RED_ZONE_SIZE, %rsp
.cfi_adjust_cfa_offset -RED_ZONE_SIZE
movb $0, %gs:PAL_HOST_TCB_IN_AEX_PROF
.cfi_endproc
# fall-through to ERESUME
.global eresume_pointer
.type eresume_pointer, @function
eresume_pointer:
enclu # perform ERESUME
.global async_exit_pointer_end
.type async_exit_pointer_end, @function
async_exit_pointer_end:
.global sgx_raise
.type sgx_raise, @function
sgx_raise:
leaq .Lafter_resume(%rip), %rdx
jmp .Ldo_ecall_callee_save
.Lsgx_entry:
# arguments: RDI - code, RSI - ms
.cfi_startproc
# increment per-thread EEXIT counter for stats
lock incq %gs:PAL_HOST_TCB_EEXIT_CNT
leaq ocall_table(%rip), %rbx
movq (%rbx,%rdi,8), %rbx
movq %rsi, %rdi
pushq %rbp
.cfi_adjust_cfa_offset 8
movq %rsp, %rbp
.cfi_offset %rbp, -16
.cfi_def_cfa_register %rbp
# Adjust stack and save RDI
subq $8, %rsp
andq $~0xF, %rsp # Required by System V AMD64 ABI.
movq %rdi, -8(%rbp)
#if DEBUG
# Call sgx_profile_sample_ocall_outer with RBX (ocall handler)
movq %rbx, %rdi
call sgx_profile_sample_ocall_outer
# Call sgx_profile_sample_ocall_inner with RDX (pointer to in-enclave context)
movq %rdx, %rdi
call sgx_profile_sample_ocall_inner
#endif
call dump_and_reset_stats
# Restore RDI
movq -8(%rbp), %rdi
callq *%rbx
movq %rbp, %rsp
popq %rbp
.cfi_def_cfa %rsp, 8
movq %rax, %rdi
movq $PAL_EVENT_NO_EVENT, %rsi
lock xchgl %esi, %gs:PAL_HOST_TCB_LAST_ASYNC_EVENT
# return to enclave, arguments:
# RDI - return value
# RSI - external event
jmp .Ldo_ecall
.cfi_endproc