forked from TrenchBoot/landing-zone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
head.S
273 lines (232 loc) · 6.97 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
/*
* 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>
#include <multiboot2.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 LZ offset to code start */
.word bootloader_data /* SL header LZ measured length */
.word lz_info
ENDDATA(sl_header)
GLOBAL(lz_stack_canary)
.long STACK_CANARY
ENDDATA(lz_stack_canary)
lz_stack:
.fill 0x280, 1, 0xcc /* Enough space for now */
.align 0x10, 0 /* Ensure proper alignment for 64bit */
.L_stack_base:
ENDDATA(lz_stack)
.text
.code32
GLOBAL(_entry)
/*
* Per the spec:
* %eax - Beginning of LZ 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
/* Set up the Stage 1 stack. */
lea .L_stack_base(%ebp), %esp
/*
* Clobber IDTR.limit to prevent stray interrupts/exceptions/INT from
* vectoring via the IVT into unmeasured code.
*/
push $0
push $0
lidt (%esp)
add $8, %esp
/* Clear DIS_A20M. R_INIT is cleared by the kernel. */
mov $IA32_VM_CR, %ecx
rdmsr
and $~(VM_CR_DIS_A20M), %eax
wrmsr
/* Relocate GDT.base, 64bit ljmp offset, and pagetables. */
add %ebp, 2 + gdtr(%ebp)
#ifdef __x86_64__
add %ebp, 1 + .Ljump64(%ebp)
add %ebp, l4_identmap(%ebp) /* L4[0] => L3 */
add %ebp, l3_identmap(%ebp) /* L3[0] => First L2 */
add %ebp, 0x08 + l3_identmap(%ebp) /* L3[1] => Second L2 */
add %ebp, 0x10 + l3_identmap(%ebp) /* L3[2] => Third L2 */
add %ebp, 0x18 + l3_identmap(%ebp) /* L3[3] => Fourth L2 */
add %ebp, l2_identmap(%ebp) /* L2[0] => L1 */
#endif
/* Load GDT */
lgdt gdtr(%ebp)
/* Load data segment regs. %ss is already flat and matches DS_SEL. */
mov $DS_SEL, %eax
mov %eax, %ds
mov %eax, %es
#ifdef __x86_64__
/* 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, ljmp to 64b mode */
.Ljump64:
ljmp $CS_SEL64, $1f /* Offset - dynamically relocated. */
.code64
1:
#endif /* 64bit setup. */
call lz_main
/*
* lz_main() is magic. It returns two pointers by register:
*
* %eax - protected mode kernel entry
* %edx - ZP base
*
* We stash the entry point in %ebx and the Zero Page pointer in %esi.
* This both protects them from clobbering during teardown, and
* matches the ABI for entering Linux.
*/
mov %eax, %ebx
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
cmpl $MULTIBOOT2, boot_protocol(%ebp)
je multiboot2_kernel
/* Jump to entry target - EBX: startup_32, ESI: ZP base, EDX: LZ base */
mov %ebp, %edx
jmp *%ebx
multiboot2_kernel:
/* Jump to entry target - EBX: MBI pointer, EAX: Multiboot2 magic number */
xchg %esi, %ebx
mov $MULTIBOOT2_BOOTLOADER_MAGIC, %eax
jmp *%esi
ENDFUNC(_entry)
.data
.align 8
gdt:
/* Null Segment. Reused for 32bit GDTR */
.word 0
gdtr:
.word .Lgdt_end - gdt - 1 /* Limit */
.long gdt /* Base - dynamically relocated. */
ENDDATA(gdtr)
/* 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)
.section .page_data, "a", @progbits
.align PAGE_SIZE
#ifdef __x86_64__
/* 64bit Pagetables, identity map of the first 4G of RAM. */
l1_identmap: /* 1x L1 page, mapping 2M of RAM. No relocations. */
idx = 0
.rept 512
.quad (idx << L1_PT_SHIFT) + _PAGE_AD + _PAGE_RW + _PAGE_PRESENT
idx = idx + 1
.endr
ENDDATA(l1_identmap)
l2_identmap: /* 4x L2 pages, each mapping 1G of RAM. 1 relocation. */
.quad l1_identmap + _PAGE_AD + _PAGE_RW + _PAGE_PRESENT
idx = 1
.rept (512 * 4) - 1
.quad (idx << L2_PT_SHIFT) + _PAGE_PSE + _PAGE_AD + _PAGE_RW + _PAGE_PRESENT
idx = idx + 1
.endr
ENDDATA(l2_identmap)
l3_identmap: /* 1x L3 page, mapping the 4x L2 pages. 4 relocations. */
idx = 0
.rept 4
.quad l2_identmap + (idx * PAGE_SIZE) + _PAGE_AD + _PAGE_RW + _PAGE_PRESENT
idx = idx + 1
.endr
.align PAGE_SIZE
ENDDATA(l3_identmap)
l4_identmap: /* 1x L4 page, mapping the L3 page. 1 relocation. */
.quad l3_identmap + _PAGE_AD + _PAGE_RW + _PAGE_PRESENT
.align PAGE_SIZE
ENDDATA(l4_identmap)
#endif
.section .bootloader_data, "a", @progbits
GLOBAL(bootloader_data)
.word 0 /* Dummy data to make sure that file isn't truncated */