-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathtrap.S
More file actions
433 lines (385 loc) · 8.33 KB
/
Copy pathtrap.S
File metadata and controls
433 lines (385 loc) · 8.33 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
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
#include <config.h>
#include <trap.h>
.intel_syntax noprefix
.global syscall_entry
syscall_entry:
//
// Current register values:
//
// RDI: syscall
// RSI: arg1
// RDX: arg2
// RCX: user RIP (set by SYSCALL)
// R8: arg4
// R9: arg5
// R10: arg3
// R11: user RFLAGS (set by SYSCALL)
// The others: user values (need to be preserved)
//
// Interrupts are automatically disabled by MSR_FMASK. If interrupts are
// enabled, an interrupt may occurs before SWAPGS and as a result, an
// interrupt handler mistakenly use the user's GS base because the interrupt
// has occurred in the kernel mode and SWAPGS won't be performed.
//
// Note that we can't re-enable interrupts until we go back into the user
// mode or resume the next thread: the (per-thread) kernel stack is shared
// with exception/interrupt handlers!
swapgs
// Switch to the kernel stack.
mov gs:[GS_RSP3], rsp
mov rsp, gs:[GS_RSP0]
#ifdef CONFIG_ABI_EMU
test byte ptr gs:[GS_ABI_EMU], 1
jnz abi_emu_entry
#endif
// Save SYSRET context and registers onto the kernel stack.
push gs:[GS_RSP3] // User RSP.
push r11 // User RFLAGS.
push rcx // User RIP.
push rbp
push rbx
push rdx
push rdi
push rsi
push r8
push r9
push r10
push r12
push r13
push r14
push r15
mov rcx, r10 // arg3
call x64_handle_syscall
// Restore registers.
pop r15
pop r14
pop r13
pop r12
pop r10
pop r9
pop r8
pop rsi
pop rdi
pop rdx
pop rbx
pop rbp
pop rcx
pop r11
pop rsp
// Ensure that interrupts are disabled since SWAPGS and SYSRETQ are not a
// atomic operation. If an interrupt occurred between SWAPGS and SYSRETQ
// (thus the GS base points to the user one), the interrupt handler won't
// perform SWAPGS since the interrupt has occurred in kernel mode.
cli
swapgs
sysretq
#ifdef CONFIG_HYPERVISOR
.global x64_vmexit_enty
x64_vmexit_enty:
// RSP is automatically set through VMCS_HOST_RSP.
push r15
push r14
push r13
push r12
push r11
push r10
push r9
push r8
push rbp
push rsi
push rdi
push rdx
push rcx
push rbx
push rax
mov rdi, rsp
mov rbp, 0
call x64_handle_vmexit
// Unreachable.
ud2
#endif
#ifdef CONFIG_ABI_EMU
abi_emu_entry:
push gs:[GS_RSP3] // User RSP.
push r15
push r14
push r13
push r12
push r10
push r9
push r8
push rsi
push rdi
push rdx
push rbx
push rax
push rbp
push r11 // User RFLAGS.
push rcx // User RIP.
// User GS base.
swapgs
rdgsbase rax
swapgs
push rax
// User FS base.
rdfsbase rax
push rax
mov rdi, rsp
call x64_abi_emu_hook
// User FS base.
pop rax
wrfsbase rax
// User GS base.
pop rax
swapgs
wrgsbase rax
swapgs
pop rcx // User RIP.
pop r11 // User RFLAGS.
pop rbp
pop rax
pop rbx
pop rdx
pop rdi
pop rsi
pop r8
pop r9
pop r10
pop r12
pop r13
pop r14
pop r15
pop rsp
cli
swapgs
sysretq
#endif
//
// Interrupt/exception handlers
//
.align INTERRUPT_HANDLER_SIZE
.global interrupt_handlers
interrupt_handlers:
.set i, 0
.rept 256
.set handler_start, .
// Exceptions with error code.
.if i == 8 || 10 <= i && i <= 14 || i == 17
.align INTERRUPT_HANDLER_SIZE
cli
push i
jmp interrupt_common
.align INTERRUPT_HANDLER_SIZE
// Interrupts and exceptions without error code.
.else
.align INTERRUPT_HANDLER_SIZE
cli
push 0 // Dummy value as error code.
push i
jmp interrupt_common
.align INTERRUPT_HANDLER_SIZE
.endif
// Increment the counter.
.set i, i + 1
.endr
.extern x64_handle_interrupt
interrupt_common:
//
// The current stack frame:
//
// +--------------------+
// 48 | SS |
// +--------------------+
// 40 | RSP |
// +--------------------+
// 32 | RFLAGS |
// +--------------------+
// 24 | CS |
// +--------------------+
// 16 | RIP |
// +--------------------+
// 8 | Error code |
// +--------------------+
// 0 | IRQ Number | <- RSP
// +--------------------+
//
// Check CS register in the IRET frame to determine if the interrupt has
// occurred in user mode.
test qword ptr [rsp + 24], 3
jz 1f
swapgs
1:
// Save RDI and set the IRQ number to RDI at once.
xchg rdi, [rsp]
// Save registers except RDI (we have already saved it above).
push r15
push r14
push r13
push r12
push r11
push r10
push r9
push r8
push rbp
push rsi
push rdx
push rcx
push rbx
push rax
mov rsi, rsp
call x64_handle_interrupt
pop rax
pop rbx
pop rcx
pop rdx
pop rsi
pop rbp
pop r8
pop r9
pop r10
pop r11
pop r12
pop r13
pop r14
pop r15
pop rdi
// Skip error code.
add rsp, 8
// Check CS register in the IRET frame to determine whether the exception
// occur in the userspace. If so, do SWAPGS.
test qword ptr [rsp + 8], 3
jz 1f
cli
swapgs
1:
iretq
.global userland_entry
userland_entry:
// Set the stack canary in the interrupt stack.
mov rbp, rsp
call stack_set_canary
// Temporarily switch into the syscall stack and set the stack canary.
mov rbx, rsp
mov rsp, gs:[GS_RSP0]
mov rbp, rsp
call stack_set_canary
mov rsp, rbx
#ifdef CONFIG_ABI_EMU
test byte ptr gs:[GS_ABI_EMU], 1
jz 1f
sub rsp, 18 * 8
mov rdi, rsp
mov rsi, 1 // ABI_HOOK_TYPE_INITIAL
call x64_abi_emu_hook_initial
call unlock
// User FS base.
pop rax
wrfsbase rax
// User GS base.
pop rax
swapgs
wrgsbase rax
swapgs
pop rcx // User RIP.
pop r11 // User RFLAGS.
pop rbp
pop rax
pop rbx
pop rdx
pop rdi
pop rsi
pop r8
pop r9
pop r10
pop r12
pop r13
pop r14
pop r15
mov [rsp + 8], rcx // RIP in a IRET frame
mov [rsp + 24], r11 // RFLAGS in a IRET frame
pop rcx
mov [rsp + 24], rcx // RSP in a IRET frame
mov rcx, [rsp + 0]
jmp 2f
#endif
#ifdef CONFIG_HYPERVISOR
test byte ptr gs:[GS_HV], 1
jz 1f
// Launch a VM using Intel VT.
call x64_hv_start_guest
// Unrechable here!
ud2
#endif
1:
call unlock
// Sanitize registers to prevent information leak.
xor rax, rax
xor rbx, rbx
xor rcx, rcx
xor rdx, rdx
xor rdi, rdi
xor rsi, rsi
xor r8, r8
xor r9, r9
xor r10, r10
xor r11, r11
xor r12, r12
xor r13, r13
xor r14, r14
xor r15, r15
xor rbp, rbp
2:
cli
swapgs
iretq
/// void switch_context(uint64_t *prev_rsp, uint64_t *next_rsp);
///
/// Saves the current context into `prev` and restore the context from `next`.
///
/// ``arch'' is a state of registers including:
///
/// - Instruction Pointer (implicitly saved and restored by CALL/RET)
/// - Stack Pointer (RSP)
/// - callee-saved registers (RBP, RBX, R12-R15)
/// - RFLAGS
///
.global switch_context
switch_context:
/* Save callee-saved registers. */
push rbp
push rbx
push r12
push r13
push r14
push r15
pushfq
/* Save and restore the stack pointer. */
mov [rdi], rsp
mov rsp, [rsi]
/* Restore callee-saved registers. */
popfq
pop r15
pop r14
pop r13
pop r12
pop rbx
pop rbp
/* Resume the next thread. */
ret
/// void arch_memcpy_from_user(void *dst, const void *src, size_t len);
/// void arch_memcpy_to_user(void *dst, const void *src, size_t len);
///
/// Copies a memory buffer from/to the user space. We don't check the validity
/// of the user's addresses, instead, we handles a page fault occurred at
/// `usercopy` as a user's fault.
///
/// Note that caller MUST check that the memory range does not overlaps with the
/// kernel sapce!
.global arch_memcpy_from_user, arch_memcpy_to_user, usercopy
arch_memcpy_from_user:
arch_memcpy_to_user:
mov rcx, rdx
cld
usercopy:
rep movsb
ret