forked from KernelTestFramework/ktf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasm-macros.h
More file actions
333 lines (291 loc) · 6.84 KB
/
asm-macros.h
File metadata and controls
333 lines (291 loc) · 6.84 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
/*
* 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.
*/
#ifndef KTF_ASM_MACROS_H
#define KTF_ASM_MACROS_H
#if defined(__i386__)
#define _ASM_REG(reg) e##reg
#elif defined(__x86_64__)
#define _ASM_REG(reg) r##reg
#else
#define _ASM_REG(reg) reg
#endif
#define _ASM_AX _ASM_REG(ax)
#define _ASM_BX _ASM_REG(bx)
#define _ASM_CX _ASM_REG(cx)
#define _ASM_DX _ASM_REG(dx)
#define _ASM_SI _ASM_REG(si)
#define _ASM_DI _ASM_REG(di)
#define _ASM_BP _ASM_REG(bp)
#define _ASM_SP _ASM_REG(sp)
#define _ASM_IP _ASM_REG(ip)
#define _ASM_FLAGS _ASM_REG(flags)
#define PT_PADDR(table, shift) (((.- (table)) / PTE_SIZE) << (shift))
#define PUSHSECTION(name) ".pushsection " STR(name) " ;\n"
#define POPSECTION ".popsection;\n"
#ifdef __ASSEMBLY__
/* clang-format off */
.macro putc val
movw (com_ports), %dx;
movb $\val, %al;
outb %al, %dx;
.endm
.macro puts addr len
movw (com_ports), %dx
mov $\addr, %si
mov $\len, %cx
cld
rep outsb
.endm
.macro SAVE_ALL_REGS32
push %eax
push %ebx
push %ecx
push %edx
push %esi
push %edi
push %ebp
.endm
.macro RESTORE_ALL_REGS32
pop %ebp
pop %edi
pop %esi
pop %edx
pop %ecx
pop %ebx
pop %eax
.endm
.macro SAVE_ALL_REGS include_ax=1
.if \include_ax
push %_ASM_AX
.endif
push %_ASM_BX
push %_ASM_CX
push %_ASM_DX
push %_ASM_SI
push %_ASM_DI
push %_ASM_BP
#if defined(__x86_64__)
push %r8
push %r9
push %r10
push %r11
push %r12
push %r13
push %r14
push %r15
#endif
.endm
.macro RESTORE_ALL_REGS include_ax=1
#if defined(__x86_64__)
pop %r15
pop %r14
pop %r13
pop %r12
pop %r11
pop %r10
pop %r9
pop %r8
#endif
pop %_ASM_BP
pop %_ASM_DI
pop %_ASM_SI
pop %_ASM_DX
pop %_ASM_CX
pop %_ASM_BX
.if \include_ax
pop %_ASM_AX
.endif
.endm
.macro SAVE_CALLEE_SAVED_REGS
push %_ASM_BX
push %_ASM_BP
#if defined(__x86_64__)
push %r12
push %r13
push %r14
push %r15
#endif
.endm
.macro RESTORE_CALLEE_SAVED_REGS
#if defined(__x86_64__)
pop %r15
pop %r14
pop %r13
pop %r12
#endif
pop %_ASM_BP
pop %_ASM_BX
.endm
.macro PUSHF
#if defined(__x86_64__)
pushfq
#else
pushf
#endif
.endm
.macro POPF
#if defined(__x86_64__)
popfq
#else
popf
#endif
.endm
.macro IRET
#if defined(__x86_64__)
iretq
#else
iret
#endif
.endm
.macro SYSRET
#if defined(__x86_64__)
sysretq
#else
sysret
#endif
.endm
.macro SYSEXIT
#if defined(__x86_64__)
sysexitq
#else
sysexit
#endif
.endm
.macro SET_CR3 val
push %_ASM_AX
mov (\val), %_ASM_AX
mov %_ASM_AX, %cr3
pop %_ASM_AX
.endm
.macro SWITCH_STACK
xchg %_ASM_SP, %gs:usermode_private
.endm
#define GLOBAL(name) \
.global name; \
name:
#define ENTRY(name) \
.align 16; \
GLOBAL(name)
#define SECTION(name, flags, alignment) \
.section name, flags; \
.align alignment
#define SIZE(name) \
.size name, (. - name);
#define END_OBJECT(name) \
.type name, STT_OBJECT; \
SIZE(name)
#define END_FUNC(name) \
.type name, STT_FUNC; \
SIZE(name)
#define STRING(name, text) \
name:; \
.asciz #text; \
name ## _end:
#define STRING_LEN(name) (name ## _end - name)
#define ELF_NOTE(name, type, size, addr) \
.section .note.name, "a"; \
.align 4; \
.long 2f - 1f; /* namesz */ \
.long 4f - 3f; /* descsz */ \
.long type; /* type */ \
1:.asciz #name; /* name */ \
2:.align 4; \
3:size addr; /* desc */ \
4:.align 4;
/* clang-format on */
#else
/* clang-format off */
#if defined(__x86_64__)
#define SAVE_ALL_REGS64() \
"push %%r8\n" \
"push %%r9\n" \
"push %%r10\n" \
"push %%r11\n" \
"push %%r12\n" \
"push %%r13\n" \
"push %%r14\n" \
"push %%r15\n"
#define RESTORE_ALL_REGS64() \
"pop %%r15\n" \
"pop %%r14\n" \
"pop %%r13\n" \
"pop %%r12\n" \
"pop %%r11\n" \
"pop %%r10\n" \
"pop %%r9\n" \
"pop %%r8\n"
#else
#define SAVE_ALL_REGS64()
#define RESTORE_ALL_REGS64()
#endif
#define SAVE_ALL_REGS() \
"push %%" STR(_ASM_AX) "\n" \
"push %%" STR(_ASM_BX) "\n" \
"push %%" STR(_ASM_CX) "\n" \
"push %%" STR(_ASM_DX) "\n" \
"push %%" STR(_ASM_SI) "\n" \
"push %%" STR(_ASM_DI) "\n" \
"push %%" STR(_ASM_BP) "\n" \
SAVE_ALL_REGS64()
#define RESTORE_ALL_REGS() \
RESTORE_ALL_REGS64() \
"pop %%" STR(_ASM_BP) "\n" \
"pop %%" STR(_ASM_DI) "\n" \
"pop %%" STR(_ASM_SI) "\n" \
"pop %%" STR(_ASM_DX) "\n" \
"pop %%" STR(_ASM_CX) "\n" \
"pop %%" STR(_ASM_BX) "\n" \
"pop %%" STR(_ASM_AX) "\n"
#if defined(__x86_64__)
#define SAVE_CALLEE_SAVED_REGS64() \
"push %%r12\n" \
"push %%r13\n" \
"push %%r14\n" \
"push %%r15\n"
#define RESTORE_CALLEE_SAVED_REGS64() \
"pop %%r15\n" \
"pop %%r14\n" \
"pop %%r13\n" \
"pop %%r12\n"
#else
#define SAVE_CALLEE_SAVED_REGS64()
#define RESTORE_CALLEE_SAVED_REGS64()
#endif
#define SAVE_CALLEE_SAVED_REGS() \
"push %%" STR(_ASM_BX) "\n" \
"push %%" STR(_ASM_BP) "\n" \
SAVE_CALLEE_SAVED_REGS64()
#define RESTORE_CALLEE_SAVED_REGS() \
RESTORE_CALLEE_SAVED_REGS64() \
"pop %%" STR(_ASM_BP) "\n" \
"pop %%" STR(_ASM_BX) "\n"
#if defined(__x86_64__)
#define POPF() "popfq\n"
#else
#define POPF() "popf\n"
#endif
#endif
#endif /* KTF_ASM_MACROS_H */