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