Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * linux/boot/head.S | |
3 | * | |
4 | * Copyright (C) 1991, 1992, 1993 Linus Torvalds | |
1da177e4 LT |
5 | */ |
6 | ||
7 | /* | |
8 | * head.S contains the 32-bit startup code. | |
9 | * | |
10 | * NOTE!!! Startup happens at absolute address 0x00001000, which is also where | |
11 | * the page directory will exist. The startup code will be overwritten by | |
12 | * the page directory. [According to comments etc elsewhere on a compressed | |
13 | * kernel it will end up at 0x1000 + 1Mb I hope so as I assume this. - AC] | |
14 | * | |
15 | * Page 0 is deliberately kept safe, since System Management Mode code in | |
16 | * laptops may need to access the BIOS data stored there. This is also | |
17 | * useful for future device drivers that either access the BIOS via VM86 | |
18 | * mode. | |
19 | */ | |
20 | ||
21 | /* | |
f4549448 | 22 | * High loaded stuff by Hans Lermen & Werner Almesberger, Feb. 1996 |
1da177e4 | 23 | */ |
b40d68d5 PA |
24 | .code32 |
25 | .text | |
1da177e4 | 26 | |
1dc818c1 | 27 | #include <linux/init.h> |
1da177e4 LT |
28 | #include <linux/linkage.h> |
29 | #include <asm/segment.h> | |
7c539764 | 30 | #include <asm/boot.h> |
1ab60e0f | 31 | #include <asm/msr.h> |
e83e31f4 | 32 | #include <asm/processor-flags.h> |
bd53147d | 33 | #include <asm/asm-offsets.h> |
fb148d83 | 34 | #include <asm/bootparam.h> |
1da177e4 | 35 | |
1dc818c1 | 36 | __HEAD |
1da177e4 | 37 | .code32 |
2d4eeecb | 38 | ENTRY(startup_32) |
8ee2f2df YL |
39 | /* |
40 | * 32bit entry is 0 and it is ABI so immutable! | |
41 | * If we come here directly from a bootloader, | |
42 | * kernel(text+data+bss+brk) ramdisk, zero_page, command line | |
43 | * all need to be under the 4G limit. | |
44 | */ | |
1da177e4 | 45 | cld |
b40d68d5 PA |
46 | /* |
47 | * Test KEEP_SEGMENTS flag to see if the bootloader is asking | |
48 | * us to not reload segments | |
49 | */ | |
fb148d83 | 50 | testb $KEEP_SEGMENTS, BP_loadflags(%esi) |
bd53147d EB |
51 | jnz 1f |
52 | ||
1da177e4 | 53 | cli |
2dead15f | 54 | movl $(__BOOT_DS), %eax |
1ab60e0f VG |
55 | movl %eax, %ds |
56 | movl %eax, %es | |
57 | movl %eax, %ss | |
bd53147d | 58 | 1: |
1ab60e0f | 59 | |
b40d68d5 PA |
60 | /* |
61 | * Calculate the delta between where we were compiled to run | |
1ab60e0f VG |
62 | * at and where we were actually loaded at. This can only be done |
63 | * with a short local call on x86. Nothing else will tell us what | |
64 | * address we are running at. The reserved chunk of the real-mode | |
85414b69 PA |
65 | * data at 0x1e4 (defined as a scratch field) are used as the stack |
66 | * for this calculation. Only 4 bytes are needed. | |
1ab60e0f | 67 | */ |
bd2a3698 | 68 | leal (BP_scratch+4)(%esi), %esp |
1ab60e0f VG |
69 | call 1f |
70 | 1: popl %ebp | |
71 | subl $1b, %ebp | |
72 | ||
a4831e08 | 73 | /* setup a stack and make sure cpu supports long mode. */ |
7c539764 | 74 | movl $boot_stack_end, %eax |
a4831e08 VG |
75 | addl %ebp, %eax |
76 | movl %eax, %esp | |
77 | ||
78 | call verify_cpu | |
79 | testl %eax, %eax | |
80 | jnz no_longmode | |
81 | ||
b40d68d5 PA |
82 | /* |
83 | * Compute the delta between where we were compiled to run at | |
1ab60e0f | 84 | * and where the code will actually run at. |
b40d68d5 PA |
85 | * |
86 | * %ebp contains the address we are loaded at by the boot loader and %ebx | |
1ab60e0f VG |
87 | * contains the address where we should move the kernel image temporarily |
88 | * for safe in-place decompression. | |
89 | */ | |
90 | ||
91 | #ifdef CONFIG_RELOCATABLE | |
92 | movl %ebp, %ebx | |
37ba7ab5 PA |
93 | movl BP_kernel_alignment(%esi), %eax |
94 | decl %eax | |
95 | addl %eax, %ebx | |
96 | notl %eax | |
97 | andl %eax, %ebx | |
8ab3820f KC |
98 | cmpl $LOAD_PHYSICAL_ADDR, %ebx |
99 | jge 1f | |
1ab60e0f | 100 | #endif |
8ab3820f KC |
101 | movl $LOAD_PHYSICAL_ADDR, %ebx |
102 | 1: | |
1ab60e0f | 103 | |
02a884c0 PA |
104 | /* Target address to relocate to for decompression */ |
105 | addl $z_extract_offset, %ebx | |
1da177e4 LT |
106 | |
107 | /* | |
1ab60e0f | 108 | * Prepare for entering 64 bit mode |
1da177e4 | 109 | */ |
1ab60e0f VG |
110 | |
111 | /* Load new GDT with the 64bit segments using 32bit descriptor */ | |
112 | leal gdt(%ebp), %eax | |
113 | movl %eax, gdt+2(%ebp) | |
114 | lgdt gdt(%ebp) | |
115 | ||
116 | /* Enable PAE mode */ | |
108d3f44 MF |
117 | movl %cr4, %eax |
118 | orl $X86_CR4_PAE, %eax | |
1ab60e0f VG |
119 | movl %eax, %cr4 |
120 | ||
121 | /* | |
122 | * Build early 4G boot pagetable | |
123 | */ | |
b40d68d5 | 124 | /* Initialize Page tables to 0 */ |
1ab60e0f VG |
125 | leal pgtable(%ebx), %edi |
126 | xorl %eax, %eax | |
127 | movl $((4096*6)/4), %ecx | |
128 | rep stosl | |
129 | ||
130 | /* Build Level 4 */ | |
131 | leal pgtable + 0(%ebx), %edi | |
132 | leal 0x1007 (%edi), %eax | |
133 | movl %eax, 0(%edi) | |
134 | ||
135 | /* Build Level 3 */ | |
136 | leal pgtable + 0x1000(%ebx), %edi | |
137 | leal 0x1007(%edi), %eax | |
138 | movl $4, %ecx | |
139 | 1: movl %eax, 0x00(%edi) | |
140 | addl $0x00001000, %eax | |
141 | addl $8, %edi | |
142 | decl %ecx | |
143 | jnz 1b | |
144 | ||
145 | /* Build Level 2 */ | |
146 | leal pgtable + 0x2000(%ebx), %edi | |
147 | movl $0x00000183, %eax | |
148 | movl $2048, %ecx | |
149 | 1: movl %eax, 0(%edi) | |
150 | addl $0x00200000, %eax | |
151 | addl $8, %edi | |
152 | decl %ecx | |
153 | jnz 1b | |
154 | ||
155 | /* Enable the boot page tables */ | |
156 | leal pgtable(%ebx), %eax | |
157 | movl %eax, %cr3 | |
158 | ||
159 | /* Enable Long mode in EFER (Extended Feature Enable Register) */ | |
160 | movl $MSR_EFER, %ecx | |
161 | rdmsr | |
162 | btsl $_EFER_LME, %eax | |
163 | wrmsr | |
164 | ||
d3c433bf YL |
165 | /* After gdt is loaded */ |
166 | xorl %eax, %eax | |
167 | lldt %ax | |
40e4f2d1 | 168 | movl $__BOOT_TSS, %eax |
d3c433bf YL |
169 | ltr %ax |
170 | ||
b40d68d5 PA |
171 | /* |
172 | * Setup for the jump to 64bit mode | |
1ab60e0f VG |
173 | * |
174 | * When the jump is performend we will be in long mode but | |
175 | * in 32bit compatibility mode with EFER.LME = 1, CS.L = 0, CS.D = 1 | |
176 | * (and in turn EFER.LMA = 1). To jump into 64bit mode we use | |
177 | * the new gdt/idt that has __KERNEL_CS with CS.L = 1. | |
178 | * We place all of the values on our mini stack so lret can | |
179 | * used to perform that far jump. | |
180 | */ | |
181 | pushl $__KERNEL_CS | |
182 | leal startup_64(%ebp), %eax | |
b8ff87a6 MF |
183 | #ifdef CONFIG_EFI_MIXED |
184 | movl efi32_config(%ebp), %ebx | |
185 | cmp $0, %ebx | |
186 | jz 1f | |
187 | leal handover_entry(%ebp), %eax | |
188 | 1: | |
189 | #endif | |
1ab60e0f VG |
190 | pushl %eax |
191 | ||
192 | /* Enter paged protected Mode, activating Long Mode */ | |
e83e31f4 | 193 | movl $(X86_CR0_PG | X86_CR0_PE), %eax /* Enable Paging and Protected mode */ |
1ab60e0f VG |
194 | movl %eax, %cr0 |
195 | ||
196 | /* Jump from 32bit compatibility mode into 64bit mode. */ | |
197 | lret | |
2d4eeecb | 198 | ENDPROC(startup_32) |
1ab60e0f | 199 | |
b8ff87a6 MF |
200 | #ifdef CONFIG_EFI_MIXED |
201 | .org 0x190 | |
202 | ENTRY(efi32_stub_entry) | |
203 | add $0x4, %esp /* Discard return address */ | |
204 | popl %ecx | |
205 | popl %edx | |
206 | popl %esi | |
207 | ||
208 | leal (BP_scratch+4)(%esi), %esp | |
209 | call 1f | |
210 | 1: pop %ebp | |
211 | subl $1b, %ebp | |
212 | ||
213 | movl %ecx, efi32_config(%ebp) | |
214 | movl %edx, efi32_config+8(%ebp) | |
215 | sgdtl efi32_boot_gdt(%ebp) | |
216 | ||
217 | leal efi32_config(%ebp), %eax | |
218 | movl %eax, efi_config(%ebp) | |
219 | ||
220 | jmp startup_32 | |
221 | ENDPROC(efi32_stub_entry) | |
222 | #endif | |
223 | ||
1ab60e0f | 224 | .code64 |
a4831e08 | 225 | .org 0x200 |
1ab60e0f | 226 | ENTRY(startup_64) |
b40d68d5 | 227 | /* |
8ee2f2df | 228 | * 64bit entry is 0x200 and it is ABI so immutable! |
b40d68d5 | 229 | * We come here either from startup_32 or directly from a |
8ee2f2df YL |
230 | * 64bit bootloader. |
231 | * If we come here from a bootloader, kernel(text+data+bss+brk), | |
232 | * ramdisk, zero_page, command line could be above 4G. | |
233 | * We depend on an identity mapped page table being provided | |
234 | * that maps our entire kernel(text+data+bss+brk), zero page | |
235 | * and command line. | |
1ab60e0f | 236 | */ |
291f3632 | 237 | #ifdef CONFIG_EFI_STUB |
b1994304 | 238 | /* |
99f857db DW |
239 | * The entry point for the PE/COFF executable is efi_pe_entry, so |
240 | * only legacy boot loaders will execute this jmp. | |
b1994304 MF |
241 | */ |
242 | jmp preferred_addr | |
243 | ||
99f857db | 244 | ENTRY(efi_pe_entry) |
54b52d87 MF |
245 | movq %rcx, efi64_config(%rip) /* Handle */ |
246 | movq %rdx, efi64_config+8(%rip) /* EFI System table pointer */ | |
247 | ||
248 | leaq efi64_config(%rip), %rax | |
249 | movq %rax, efi_config(%rip) | |
250 | ||
251 | call 1f | |
252 | 1: popq %rbp | |
253 | subq $1b, %rbp | |
254 | ||
255 | /* | |
f3670394 | 256 | * Relocate efi_config->call(). |
54b52d87 MF |
257 | */ |
258 | addq %rbp, efi64_config+88(%rip) | |
259 | ||
260 | movq %rax, %rdi | |
9ca8f72a MF |
261 | call make_boot_params |
262 | cmpq $0,%rax | |
54b52d87 MF |
263 | je fail |
264 | mov %rax, %rsi | |
7e8213c1 MF |
265 | leaq startup_32(%rip), %rax |
266 | movl %eax, BP_code32_start(%rsi) | |
54b52d87 | 267 | jmp 2f /* Skip the relocation */ |
9ca8f72a | 268 | |
b8ff87a6 | 269 | handover_entry: |
54b52d87 MF |
270 | call 1f |
271 | 1: popq %rbp | |
272 | subq $1b, %rbp | |
273 | ||
274 | /* | |
f3670394 | 275 | * Relocate efi_config->call(). |
54b52d87 MF |
276 | */ |
277 | movq efi_config(%rip), %rax | |
278 | addq %rbp, 88(%rax) | |
54b52d87 MF |
279 | 2: |
280 | movq efi_config(%rip), %rdi | |
291f3632 | 281 | call efi_main |
291f3632 | 282 | movq %rax,%rsi |
b1994304 MF |
283 | cmpq $0,%rax |
284 | jne 2f | |
54b52d87 | 285 | fail: |
b1994304 MF |
286 | /* EFI init failed, so hang. */ |
287 | hlt | |
54b52d87 | 288 | jmp fail |
b1994304 | 289 | 2: |
7e8213c1 | 290 | movl BP_code32_start(%esi), %eax |
291f3632 MF |
291 | leaq preferred_addr(%rax), %rax |
292 | jmp *%rax | |
293 | ||
294 | preferred_addr: | |
295 | #endif | |
1ab60e0f VG |
296 | |
297 | /* Setup data segments. */ | |
298 | xorl %eax, %eax | |
299 | movl %eax, %ds | |
300 | movl %eax, %es | |
301 | movl %eax, %ss | |
08da5a2c ZA |
302 | movl %eax, %fs |
303 | movl %eax, %gs | |
1ab60e0f | 304 | |
b40d68d5 PA |
305 | /* |
306 | * Compute the decompressed kernel start address. It is where | |
1ab60e0f VG |
307 | * we were loaded at aligned to a 2M boundary. %rbp contains the |
308 | * decompressed kernel start address. | |
309 | * | |
310 | * If it is a relocatable kernel then decompress and run the kernel | |
311 | * from load address aligned to 2MB addr, otherwise decompress and | |
40b387a8 | 312 | * run the kernel from LOAD_PHYSICAL_ADDR |
02a884c0 PA |
313 | * |
314 | * We cannot rely on the calculation done in 32-bit mode, since we | |
315 | * may have been invoked via the 64-bit entry point. | |
1ab60e0f VG |
316 | */ |
317 | ||
318 | /* Start with the delta to where the kernel will run at. */ | |
319 | #ifdef CONFIG_RELOCATABLE | |
320 | leaq startup_32(%rip) /* - $startup_32 */, %rbp | |
37ba7ab5 PA |
321 | movl BP_kernel_alignment(%rsi), %eax |
322 | decl %eax | |
323 | addq %rax, %rbp | |
324 | notq %rax | |
325 | andq %rax, %rbp | |
8ab3820f KC |
326 | cmpq $LOAD_PHYSICAL_ADDR, %rbp |
327 | jge 1f | |
1ab60e0f | 328 | #endif |
8ab3820f KC |
329 | movq $LOAD_PHYSICAL_ADDR, %rbp |
330 | 1: | |
1ab60e0f | 331 | |
02a884c0 PA |
332 | /* Target address to relocate to for decompression */ |
333 | leaq z_extract_offset(%rbp), %rbx | |
1ab60e0f | 334 | |
0a137736 PA |
335 | /* Set up the stack */ |
336 | leaq boot_stack_end(%rbx), %rsp | |
337 | ||
338 | /* Zero EFLAGS */ | |
339 | pushq $0 | |
340 | popfq | |
341 | ||
b40d68d5 PA |
342 | /* |
343 | * Copy the compressed kernel to the end of our buffer | |
1ab60e0f VG |
344 | * where decompression in place becomes safe. |
345 | */ | |
36d3793c PA |
346 | pushq %rsi |
347 | leaq (_bss-8)(%rip), %rsi | |
348 | leaq (_bss-8)(%rbx), %rdi | |
5b11f1ce | 349 | movq $_bss /* - $startup_32 */, %rcx |
36d3793c PA |
350 | shrq $3, %rcx |
351 | std | |
352 | rep movsq | |
353 | cld | |
354 | popq %rsi | |
1ab60e0f VG |
355 | |
356 | /* | |
357 | * Jump to the relocated address. | |
358 | */ | |
359 | leaq relocated(%rbx), %rax | |
360 | jmp *%rax | |
361 | ||
b8ff87a6 MF |
362 | #ifdef CONFIG_EFI_STUB |
363 | .org 0x390 | |
364 | ENTRY(efi64_stub_entry) | |
365 | movq %rdi, efi64_config(%rip) /* Handle */ | |
366 | movq %rsi, efi64_config+8(%rip) /* EFI System table pointer */ | |
367 | ||
368 | leaq efi64_config(%rip), %rax | |
369 | movq %rax, efi_config(%rip) | |
370 | ||
371 | movq %rdx, %rsi | |
372 | jmp handover_entry | |
373 | ENDPROC(efi64_stub_entry) | |
374 | #endif | |
375 | ||
b40d68d5 | 376 | .text |
1ab60e0f VG |
377 | relocated: |
378 | ||
1da177e4 | 379 | /* |
0a137736 | 380 | * Clear BSS (stack is currently empty) |
1da177e4 | 381 | */ |
36d3793c PA |
382 | xorl %eax, %eax |
383 | leaq _bss(%rip), %rdi | |
384 | leaq _ebss(%rip), %rcx | |
1ab60e0f | 385 | subq %rdi, %rcx |
36d3793c PA |
386 | shrq $3, %rcx |
387 | rep stosq | |
1ab60e0f | 388 | |
f3670394 LT |
389 | /* |
390 | * Adjust our own GOT | |
391 | */ | |
392 | leaq _got(%rip), %rdx | |
393 | leaq _egot(%rip), %rcx | |
394 | 1: | |
395 | cmpq %rcx, %rdx | |
396 | jae 2f | |
397 | addq %rbx, (%rdx) | |
398 | addq $8, %rdx | |
399 | jmp 1b | |
400 | 2: | |
401 | ||
1da177e4 LT |
402 | /* |
403 | * Do the decompression, and jump to the new kernel.. | |
404 | */ | |
02a884c0 | 405 | pushq %rsi /* Save the real mode argument */ |
e6023367 JM |
406 | movq $z_run_size, %r9 /* size of kernel with .bss and .brk */ |
407 | pushq %r9 | |
02a884c0 PA |
408 | movq %rsi, %rdi /* real mode address */ |
409 | leaq boot_heap(%rip), %rsi /* malloc area for uncompression */ | |
410 | leaq input_data(%rip), %rdx /* input_data */ | |
411 | movl $z_input_len, %ecx /* input_len */ | |
412 | movq %rbp, %r8 /* output target address */ | |
e6023367 | 413 | movq $z_output_len, %r9 /* decompressed length, end of relocs */ |
8ab3820f | 414 | call decompress_kernel /* returns kernel location in %rax */ |
e6023367 | 415 | popq %r9 |
1ab60e0f | 416 | popq %rsi |
1da177e4 | 417 | |
1da177e4 | 418 | /* |
1ab60e0f | 419 | * Jump to the decompressed kernel. |
1da177e4 | 420 | */ |
8ab3820f | 421 | jmp *%rax |
1da177e4 | 422 | |
187a8a73 YL |
423 | .code32 |
424 | no_longmode: | |
425 | /* This isn't an x86-64 CPU so hang */ | |
426 | 1: | |
427 | hlt | |
428 | jmp 1b | |
429 | ||
430 | #include "../../kernel/verify_cpu.S" | |
431 | ||
1ab60e0f VG |
432 | .data |
433 | gdt: | |
434 | .word gdt_end - gdt | |
435 | .long gdt | |
436 | .word 0 | |
437 | .quad 0x0000000000000000 /* NULL descriptor */ | |
438 | .quad 0x00af9a000000ffff /* __KERNEL_CS */ | |
439 | .quad 0x00cf92000000ffff /* __KERNEL_DS */ | |
08da5a2c ZA |
440 | .quad 0x0080890000000000 /* TS descriptor */ |
441 | .quad 0x0000000000000000 /* TS continued */ | |
1ab60e0f | 442 | gdt_end: |
7c539764 | 443 | |
3db4cafd | 444 | #ifdef CONFIG_EFI_STUB |
54b52d87 MF |
445 | efi_config: |
446 | .quad 0 | |
447 | ||
b8ff87a6 MF |
448 | #ifdef CONFIG_EFI_MIXED |
449 | .global efi32_config | |
450 | efi32_config: | |
451 | .fill 11,8,0 | |
452 | .quad efi64_thunk | |
453 | .byte 0 | |
454 | #endif | |
455 | ||
54b52d87 MF |
456 | .global efi64_config |
457 | efi64_config: | |
458 | .fill 11,8,0 | |
62fa6e69 | 459 | .quad efi_call |
54b52d87 | 460 | .byte 1 |
3db4cafd MF |
461 | #endif /* CONFIG_EFI_STUB */ |
462 | ||
b40d68d5 PA |
463 | /* |
464 | * Stack and heap for uncompression | |
465 | */ | |
466 | .bss | |
467 | .balign 4 | |
7c539764 AH |
468 | boot_heap: |
469 | .fill BOOT_HEAP_SIZE, 1, 0 | |
470 | boot_stack: | |
471 | .fill BOOT_STACK_SIZE, 1, 0 | |
472 | boot_stack_end: | |
5b11f1ce PA |
473 | |
474 | /* | |
475 | * Space for page tables (not in .bss so not zeroed) | |
476 | */ | |
477 | .section ".pgtable","a",@nobits | |
478 | .balign 4096 | |
479 | pgtable: | |
480 | .fill 6*4096, 1, 0 |