-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhead.S
318 lines (276 loc) · 8.54 KB
/
head.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
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
/*
* Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved.
*
* Author:
* Ross Philipson <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <defs.h>
/* Selectors, CS and SS compatible with initial state after SKINIT */
#define CS_SEL32 0x0008
#define DS_SEL 0x0010
#ifdef __x86_64__
#define CS_SEL64 0x0018
#endif
.section .headers, "ax", @progbits
GLOBAL(sl_header)
.word _entry /* SL header SKL offset to code start */
.word _end_of_measured /* SL header SKL measured length */
.word skl_info /* Offset to SKL info with UUID and version */
.word bootloader_data /* Offset to SLRT filled by the bootloader */
ENDDATA(sl_header)
.text
.code32
GLOBAL(_entry)
/*
* Per the spec:
* %eax - Beginning of SKL containing the SL header
* %edx - Family/Model/Stepping
* %esp - %eax + 64k (End of SLB)
* %cs - 32bit Flat, selector 0x08
* %ss - 32bit Flat, selector 0x10
* %ds/etc - 16bit Real Mode segments. Unusable.
*
* Restore the world, get back into long mode.
*
* The GDT needs relocating before we have a usable %ds, which must be
* done with an %ss-relative write. Therefore, we store the base
* address in %ebp.
*/
mov %eax, %ebp
/* Check if we have enough space available for stack. */
lea bootloader_data(%ebp), %ebx
mov %ss:8(%ebx), %edx /* bootloader_data.size */
add %edx, %ebx
add $MAX_STACK_SIZE, %ebx
cmp %ebx, %esp
jge .Lenough_stack
1:
/* XXX: is there any way of notifying the user about this? */
hlt
jmp 1b
.Lenough_stack:
/*
* Clobber IDTR.limit to prevent stray interrupts/exceptions/INT from
* vectoring via the IVT into unmeasured code.
*/
push $0
push $0
lidt (%esp)
/* Clear DIS_A20M. R_INIT is cleared by the kernel. */
mov $IA32_VM_CR, %ecx
rdmsr
and $~(VM_CR_DIS_A20M), %eax
wrmsr
/* Load GDT */
movl $gdt, 4(%esp) /* Base ... */
add %ebp, 4(%esp) /* ... relocated */
movw $(.Lgdt_end - gdt - 1), 2(%esp) /* Limit */
lgdt 2(%esp)
add $8, %esp
/* Load data segment regs. %ss is already flat and matches DS_SEL. */
mov $DS_SEL, %eax
mov %eax, %ds
mov %eax, %es
/* Clear .bss. Linker scripts ensures multiple of 4B size. */
xor %eax, %eax
cld
lea _bss(%ebp), %edi
lea _ebss(%ebp), %ecx
sub %edi, %ecx
shr $2, %ecx
rep stosl
#ifdef __x86_64__
/*
* Pagetables are located in .bss that was just cleared, and we are
* loaded to <4GB memory, so we don't have to bother with writing to
* higher dword of each entry.
*/
/* 1x L1 page, 512 entries mapping total of 2M. */
lea l1_identmap(%ebp), %edi
mov $512, %ecx
mov $(_PAGE_AD + _PAGE_RW + _PAGE_PRESENT + 512 * PAGE_SIZE), %edx
.Lfill_l1_identmap:
sub $PAGE_SIZE, %edx
/* Loop runs for ecx=[512..1] for entries [511..0], hence -8. */
mov %edx, -8(%edi,%ecx,8)
loop .Lfill_l1_identmap
/* 4x L2 pages, each page mapping 1G of RAM. */
lea l2_identmap(%ebp), %edi
/* 1st entry points to L1. */
lea (l1_identmap + _PAGE_AD + _PAGE_RW + _PAGE_PRESENT)(%ebp), %edx
mov %edx, (%edi)
/* Other entries are 2MB pages. */
mov $(4 * 512 - 1), %ecx
/*
* XXX: Value below should be 4GB + flags, which wouldn't fit in 32b
* register. To avoid warning from the assembler, 4GB is skipped here.
* Substitution in first iteration makes the value roll over and point
* to 4GB - 2MB + flags.
*/
mov $(_PAGE_PSE + _PAGE_AD + _PAGE_RW + _PAGE_PRESENT), %edx
.Lfill_l2_identmap:
sub $(1 << L2_PT_SHIFT), %edx
/* Loop runs for ecx=[2047..1] for entries [2047..1]. */
mov %edx, (%edi,%ecx,8)
loop .Lfill_l2_identmap
/* 1x L3 page, mapping the 4x L2 pages. */
lea l3_identmap(%ebp), %edi
mov $4, %ecx
lea (l2_identmap + 4 * PAGE_SIZE \
+ _PAGE_AD + _PAGE_RW + _PAGE_PRESENT)(%ebp), %edx
.Lfill_l3_identmap:
sub $PAGE_SIZE, %edx
/* Loop runs for ecx=[4..1] for entries [3..0], hence -8. */
mov %edx, -8(%edi,%ecx,8)
loop .Lfill_l3_identmap
/* 1x L4 page, mapping the L3 page. */
lea (l3_identmap + _PAGE_AD + _PAGE_RW + _PAGE_PRESENT)(%ebp), %edx
mov %edx, l4_identmap(%ebp)
/* Restore CR4, PAE must be enabled before IA-32e mode */
mov %cr4, %ecx
or $CR4_PAE, %ecx
mov %ecx, %cr4
/* Load PML4 table location into PT base register */
lea l4_identmap(%ebp), %eax
mov %eax, %cr3
/* Enable IA-32e mode and paging */
mov $IA32_EFER, %ecx
rdmsr
or $EFER_LME >> 8, %ah
wrmsr
mov %cr0, %eax
or $CR0_PG | CR0_NE | CR0_TS | CR0_MP, %eax
mov %eax, %cr0
/* Now in IA-32e compatibility mode, use lret to jump to 64b mode */
lea 1f(%ebp), %ecx
push $CS_SEL64
push %ecx
lret
.code64
1:
#endif /* 64bit setup. */
call skl_main
/*
* skl_main() is magic. It returns two pointers by register:
*
* %eax - protected mode kernel entry
* %edx - argument for kernel entry point, depends on type of kernel
*
* We stash the entry point in %edi and the argument in %esi to protect
* them from clobbering during teardown.
*/
mov %eax, %edi
mov %edx, %esi
#ifdef __x86_64__
/* Setup target to ret to compat mode */
lea 1f(%rip), %ecx
push $CS_SEL32
push %rcx
lretq
.code32
1: /* Now in IA-32e compatibility mode, next stop is protected mode */
/* Turn paging off - we are identity mapped so we will survive */
mov %cr0, %eax
and $~(CR0_PG | CR0_NE | CR0_TS | CR0_MP), %eax
mov %eax, %cr0
/* Disable IA-32e mode */
mov $IA32_EFER, %ecx
rdmsr
and $~(EFER_LME >> 8), %ah
wrmsr
/* Now in protected mode, make things look like TXT post launch */
mov %cr4, %eax
and $~CR4_PAE, %eax
mov %eax, %cr4
#endif /* 64bit teardown. */
push $0
popf
/*
* Various kernels use different boot protocols, SKL supports some of
* the common ones. Because of that, we are saving the same argument in
* every possible place that any of the supported kernel types may look
* for it. As of now, supported protocols include:
*
* - Linux x86 protected mode entry, not UEFI
* - Multiboot2, also not UEFI
* - simple payload started as 'entry(u32 arg)' function call. As we
* don't expect it to return, __cdecl, __stdcall and __pascal calling
* conventions work the same.
*/
/* Linux expects Zero Page address in %esi, it is already there */
/* Multiboot2 expects MBI address in %ebx and magic number in %eax */
mov %esi, %ebx
mov $MULTIBOOT2_BOOTLOADER_MAGIC, %eax
/* Simple payload expects argument on stack followed by return address */
push %esi
push $0
/* All set, jump to the kernel */
jmp *%edi
ENDFUNC(_entry)
.section .rodata, "a", @progbits
.align 8
gdt:
/* Null Segment */
.word 0
.word 0
.long 0
/* 32b Code Segment */
.word 0xffff /* Limit 1 */
.word 0x0000 /* Base 1 */
.byte 0x00 /* Base 2 */
.byte 0x9b /* P=1 DPL=0 S=1 Type=0010 C=0 W=1 A=1 */
.byte 0xcf /* G=1 D=1 L=0 AVL=0 Limit 2 */
.byte 0x00 /* Base 3 */
/* Data Segment, can be used both in 32b and 64b */
.word 0xffff /* Limit 1 */
.word 0x0000 /* Base 1 */
.byte 0x00 /* Base 2 */
.byte 0x93 /* P=1 DPL=0 S=1 Type=0010 C=0 W=1 A=1 */
.byte 0xcf /* G=1 D=1 L=0 AVL=0 Limit 2 */
.byte 0x00 /* Base 3 */
#ifdef __x86_64__
/* 64b Code Segment */
.word 0x0000 /* Limit 1 */
.word 0x0000 /* Base 1 */
.byte 0x00 /* Base 2 */
.byte 0x9b /* P=1 DPL=0 S=1 Type=1010 C=0 R=1 A=1 */
.byte 0x20 /* G=0 D=0 L=1 AVL=0 Limit 2 */
.byte 0x00 /* Base 3 */
#endif
.Lgdt_end:
ENDDATA(gdt)
#ifdef __x86_64__
/* 64bit Pagetables, identity map of the first 4G of RAM. */
.section .bss.page_data, "aw", @nobits
.align PAGE_SIZE
l1_identmap: /* 1x L1 page, mapping 2M of RAM. */
.skip PAGE_SIZE
ENDDATA(l1_identmap)
l2_identmap: /* 4x L2 pages, each mapping 1G of RAM. */
.skip 4 * PAGE_SIZE
ENDDATA(l2_identmap)
l3_identmap: /* 1x L3 page, mapping the 4x L2 pages. */
.skip PAGE_SIZE
ENDDATA(l3_identmap)
l4_identmap: /* 1x L4 page, mapping the L3 page. */
.skip PAGE_SIZE
ENDDATA(l4_identmap)
#endif
.section .bootloader_data, "a", @nobits
GLOBAL(bootloader_data)
/* Avoids "missing .note.GNU-stack section implies executable stack" warning. */
.section .note.GNU-stack