forked from KernelTestFramework/ktf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentry.S
More file actions
303 lines (250 loc) · 7.3 KB
/
entry.S
File metadata and controls
303 lines (250 loc) · 7.3 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
/*
* Copyright © 2020 Amazon.com, Inc. or its affiliates.
* Copyright © 2014,2015 Citrix Systems Ltd.
* All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <asm-offsets.h>
#include <asm-macros.h>
#include <processor.h>
#include <segment.h>
#include <page.h>
#include <traps.h>
#include <usermode.h>
#include <errno.h>
.macro MASK_USER_FLAGS
PUSHF
andl $~(USERMODE_FLAGS_MASK), (%_ASM_SP)
POPF
.endm
.macro _from_usermode switch_stack=0
SET_CR3 cr3
swapgs
.if \switch_stack == 1
SWITCH_STACK
.endif
.endm
.macro _to_usermode switch_stack=0
.if \switch_stack == 1
SWITCH_STACK
.endif
swapgs
SET_CR3 user_cr3
.endm
.macro cond_from_usermode
testb $0x3, cpu_exc_cs(%_ASM_SP)
jz 1f /* skip if from kernel mode */
MASK_USER_FLAGS
_from_usermode
1:
.endm
.macro cond_to_usermode
testb $0x3, cpu_exc_cs(%_ASM_SP)
jz 1f /* skip if from kernel mode */
_to_usermode
1:
.endm
.macro syscall_from_usermode switch_stack=1
_from_usermode switch_stack=\switch_stack
.endm
.macro syscall_to_usermode switch_stack=1
_to_usermode switch_stack=\switch_stack
.endm
.macro exception_handler sym vec has_error_code
ENTRY(entry_\sym)
.if \has_error_code == 0
push $0
.endif
push $\vec
cond_from_usermode
jmp handle_exception
END_FUNC(entry_\sym)
.endm
.macro check_syscall_exit
cmp $SYSCALL_EXIT, %_ASM_AX
jnz 1f
_from_usermode
jmp syscall_exit
1:
.endm
.macro interrupt_handler sym func vec
ENTRY(asm_interrupt_handler_\sym)
push $0 /* fake error_code */
push $\vec /* IRQ vector */
cond_from_usermode
cld
SAVE_ALL_REGS
call \func
RESTORE_ALL_REGS
cond_to_usermode
/* Popping error_code and vector machine words added on entry. */
/* By using cpu_exc_t offset we make it arch-agnostic. */
add $cpu_exc_ip, %_ASM_SP
IRET
END_FUNC(asm_interrupt_handler_\sym)
.endm
ENTRY(handle_exception)
cld
SAVE_ALL_REGS
mov %_ASM_SP, %_ASM_DI
call do_exception
RESTORE_ALL_REGS
cond_to_usermode
/* Popping error_code and vector machine words added on entry. */
/* By using cpu_exc_t offset we make it arch-agnostic. */
add $cpu_exc_ip, %_ASM_SP
IRET
END_FUNC(handle_exception)
.align PAGE_SIZE
GLOBAL(exception_handlers)
exception_handler DE X86_EX_DE 0
exception_handler DB X86_EX_DB 0
exception_handler NMI X86_EX_NMI 0
exception_handler BP X86_EX_BP 0
exception_handler OF X86_EX_OF 0
exception_handler BR X86_EX_BR 0
exception_handler UD X86_EX_UD 0
exception_handler NM X86_EX_NM 0
exception_handler DF X86_EX_DF 1
exception_handler CS X86_EX_CS 0
exception_handler TS X86_EX_TS 1
exception_handler NP X86_EX_NP 1
exception_handler SS X86_EX_SS 1
exception_handler GP X86_EX_GP 1
exception_handler PF X86_EX_PF 1
exception_handler SPV X86_EX_SPV 0
exception_handler MF X86_EX_MF 0
exception_handler AC X86_EX_AC 1
exception_handler MC X86_EX_MC 0
exception_handler XM X86_EX_XM 0
exception_handler VE X86_EX_VE 0
exception_handler SE X86_EX_SE 1
GLOBAL(end_exception_handlers)
GLOBAL(interrupt_handlers)
interrupt_handler timer timer_interrupt_handler timer_irq
interrupt_handler uart1 uart_interrupt_handler serial_com1_irq
interrupt_handler uart2 uart_interrupt_handler serial_com2_irq
interrupt_handler keyboard keyboard_interrupt_handler kb_port1_irq
#ifdef KTF_ACPICA
interrupt_handler acpi acpi_interrupt_handler acpi_sci_irq
#endif
GLOBAL(end_interrupt_handlers)
.align PAGE_SIZE
ENTRY(syscall_exit)
SWITCH_STACK
POPF
/* Save exit code to return value register (AX) */
mov %_ASM_SI, cpu_regs_ax(%_ASM_SP)
RESTORE_ALL_REGS
ret
END_FUNC(syscall_exit)
ENTRY(terminate_user_task)
mov $-EFAULT, %_ASM_SI
jmp syscall_exit
END_FUNC(terminate_user_task)
.align PAGE_SIZE
GLOBAL(usermode_helpers)
ENTRY(enter_usermode)
/* FIXME: Add 32-bit support */
/* will be restored on entering back in kernel mode */
SAVE_ALL_REGS
PUSHF
/* Save user stack pointer onto per-cpu */
mov %_ASM_DX, %gs:(usermode_private)
/* Now switch stack and address space */
_to_usermode switch_stack=1
/* SS + SP */
push $__USER_DS
push %_ASM_DX
/* EFLAGS */
PUSHF
orl $X86_EFLAGS_IOPL, (%_ASM_SP)
/* CS + IP */
push $__USER_CS
push $usermode_stub
IRET
END_FUNC(enter_usermode)
ENTRY(syscall_handler_entry)
check_syscall_exit
SAVE_ALL_REGS include_ax=0
syscall_from_usermode
push %_ASM_CX
push %r11
mov %_ASM_DI, %_ASM_CX
mov %_ASM_AX, %_ASM_DI
call syscall_handler
pop %r11
pop %_ASM_CX
syscall_to_usermode
RESTORE_ALL_REGS include_ax=0
SYSRET
END_FUNC(syscall_handler_entry)
ENTRY(sysenter_handler_entry)
check_syscall_exit
SAVE_ALL_REGS include_ax=0
syscall_from_usermode switch_stack=0
MASK_USER_FLAGS
mov %_ASM_DI, %_ASM_DX
mov %r10, %_ASM_CX
mov %_ASM_AX, %_ASM_DI
call syscall_handler
syscall_to_usermode switch_stack=0
RESTORE_ALL_REGS include_ax=0
SYSEXIT
END_FUNC(sysenter_handler_entry)
ENTRY(int80_handler_entry)
check_syscall_exit
SAVE_ALL_REGS include_ax=0
syscall_from_usermode switch_stack=0
MASK_USER_FLAGS
mov %_ASM_DI, %_ASM_CX
mov %_ASM_AX, %_ASM_DI
call syscall_handler
syscall_to_usermode switch_stack=0
RESTORE_ALL_REGS include_ax=0
IRET
END_FUNC(int80_handler_entry)
GLOBAL(end_usermode_helpers)
SECTION(.text.user, "ax", 16)
ENTRY(usermode_stub)
/* DI: User function to be called
* SI: Parameters
* ...
*/
xchg %_ASM_DI, %_ASM_SI
call *%_ASM_SI
/* sys_exit */
mov %_ASM_AX, %_ASM_SI
mov $SYSCALL_EXIT, %_ASM_AX
syscall
END_FUNC(usermode_stub)
SECTION(.data.rmode, "aw", 16)
STRING(rmode_exception_str, Exception happened during real mode call! This is not supported.\n)
SECTION(.text.rmode, "ax", 16)
.code16
ENTRY(rmode_exception)
puts rmode_exception_str STRING_LEN(rmode_exception_str)
cli
.Lhalt_loop:
hlt
jmp .Lhalt_loop
END_FUNC(rmode_exception)