1 /* SPDX-License-Identifier: GPL-2.0 */
5 * Copyright (C) 1991, 1992, 1993 Linus Torvalds
9 * head.S contains the 32-bit startup code.
11 * NOTE!!! Startup happens at absolute address 0x00001000, which is also where
12 * the page directory will exist. The startup code will be overwritten by
13 * the page directory. [According to comments etc elsewhere on a compressed
14 * kernel it will end up at 0x1000 + 1Mb I hope so as I assume this. - AC]
16 * Page 0 is deliberately kept safe, since System Management Mode code in
17 * laptops may need to access the BIOS data stored there. This is also
18 * useful for future device drivers that either access the BIOS via VM86
23 * High loaded stuff by Hans Lermen & Werner Almesberger, Feb. 1996
28 #include <linux/init.h>
29 #include <linux/linkage.h>
30 #include <asm/segment.h>
33 #include <asm/processor-flags.h>
34 #include <asm/asm-offsets.h>
35 #include <asm/bootparam.h>
38 * Locally defined symbols should be marked hidden:
50 * 32bit entry is 0 and it is ABI so immutable!
51 * If we come here directly from a bootloader,
52 * kernel(text+data+bss+brk) ramdisk, zero_page, command line
53 * all need to be under the 4G limit.
57 * Test KEEP_SEGMENTS flag to see if the bootloader is asking
58 * us to not reload segments
60 testb $KEEP_SEGMENTS, BP_loadflags(%esi)
64 movl $(__BOOT_DS), %eax
71 * Calculate the delta between where we were compiled to run
72 * at and where we were actually loaded at. This can only be done
73 * with a short local call on x86. Nothing else will tell us what
74 * address we are running at. The reserved chunk of the real-mode
75 * data at 0x1e4 (defined as a scratch field) are used as the stack
76 * for this calculation. Only 4 bytes are needed.
78 leal (BP_scratch+4)(%esi), %esp
83 /* setup a stack and make sure cpu supports long mode. */
84 movl $boot_stack_end, %eax
93 * Compute the delta between where we were compiled to run at
94 * and where the code will actually run at.
96 * %ebp contains the address we are loaded at by the boot loader and %ebx
97 * contains the address where we should move the kernel image temporarily
98 * for safe in-place decompression.
101 #ifdef CONFIG_RELOCATABLE
103 movl BP_kernel_alignment(%esi), %eax
108 cmpl $LOAD_PHYSICAL_ADDR, %ebx
111 movl $LOAD_PHYSICAL_ADDR, %ebx
114 /* Target address to relocate to for decompression */
115 movl BP_init_size(%esi), %eax
120 * Prepare for entering 64 bit mode
123 /* Load new GDT with the 64bit segments using 32bit descriptor */
124 addl %ebp, gdt+2(%ebp)
127 /* Enable PAE mode */
129 orl $X86_CR4_PAE, %eax
133 * Build early 4G boot pagetable
135 /* Initialize Page tables to 0 */
136 leal pgtable(%ebx), %edi
138 movl $(BOOT_INIT_PGT_SIZE/4), %ecx
142 leal pgtable + 0(%ebx), %edi
143 leal 0x1007 (%edi), %eax
147 leal pgtable + 0x1000(%ebx), %edi
148 leal 0x1007(%edi), %eax
150 1: movl %eax, 0x00(%edi)
151 addl $0x00001000, %eax
157 leal pgtable + 0x2000(%ebx), %edi
158 movl $0x00000183, %eax
160 1: movl %eax, 0(%edi)
161 addl $0x00200000, %eax
166 /* Enable the boot page tables */
167 leal pgtable(%ebx), %eax
170 /* Enable Long mode in EFER (Extended Feature Enable Register) */
173 btsl $_EFER_LME, %eax
176 /* After gdt is loaded */
179 movl $__BOOT_TSS, %eax
183 * Setup for the jump to 64bit mode
185 * When the jump is performend we will be in long mode but
186 * in 32bit compatibility mode with EFER.LME = 1, CS.L = 0, CS.D = 1
187 * (and in turn EFER.LMA = 1). To jump into 64bit mode we use
188 * the new gdt/idt that has __KERNEL_CS with CS.L = 1.
189 * We place all of the values on our mini stack so lret can
190 * used to perform that far jump.
193 leal startup_64(%ebp), %eax
194 #ifdef CONFIG_EFI_MIXED
195 movl efi32_config(%ebp), %ebx
198 leal handover_entry(%ebp), %eax
203 /* Enter paged protected Mode, activating Long Mode */
204 movl $(X86_CR0_PG | X86_CR0_PE), %eax /* Enable Paging and Protected mode */
207 /* Jump from 32bit compatibility mode into 64bit mode. */
211 #ifdef CONFIG_EFI_MIXED
213 ENTRY(efi32_stub_entry)
214 add $0x4, %esp /* Discard return address */
219 leal (BP_scratch+4)(%esi), %esp
224 movl %ecx, efi32_config(%ebp)
225 movl %edx, efi32_config+8(%ebp)
226 sgdtl efi32_boot_gdt(%ebp)
228 leal efi32_config(%ebp), %eax
229 movl %eax, efi_config(%ebp)
233 btrl $X86_CR0_PG_BIT, %eax
237 ENDPROC(efi32_stub_entry)
244 * 64bit entry is 0x200 and it is ABI so immutable!
245 * We come here either from startup_32 or directly from a
247 * If we come here from a bootloader, kernel(text+data+bss+brk),
248 * ramdisk, zero_page, command line could be above 4G.
249 * We depend on an identity mapped page table being provided
250 * that maps our entire kernel(text+data+bss+brk), zero page
254 /* Setup data segments. */
263 * Compute the decompressed kernel start address. It is where
264 * we were loaded at aligned to a 2M boundary. %rbp contains the
265 * decompressed kernel start address.
267 * If it is a relocatable kernel then decompress and run the kernel
268 * from load address aligned to 2MB addr, otherwise decompress and
269 * run the kernel from LOAD_PHYSICAL_ADDR
271 * We cannot rely on the calculation done in 32-bit mode, since we
272 * may have been invoked via the 64-bit entry point.
275 /* Start with the delta to where the kernel will run at. */
276 #ifdef CONFIG_RELOCATABLE
277 leaq startup_32(%rip) /* - $startup_32 */, %rbp
278 movl BP_kernel_alignment(%rsi), %eax
283 cmpq $LOAD_PHYSICAL_ADDR, %rbp
286 movq $LOAD_PHYSICAL_ADDR, %rbp
289 /* Target address to relocate to for decompression */
290 movl BP_init_size(%rsi), %ebx
294 /* Set up the stack */
295 leaq boot_stack_end(%rbx), %rsp
297 #ifdef CONFIG_X86_5LEVEL
299 * Check if we need to enable 5-level paging.
300 * RSI holds real mode data and need to be preserved across
304 call l5_paging_required
307 /* If l5_paging_required() returned zero, we're done here. */
312 * At this point we are in long mode with 4-level paging enabled,
313 * but we want to enable 5-level paging.
315 * The problem is that we cannot do it directly. Setting LA57 in
316 * long mode would trigger #GP. So we need to switch off long mode
319 * NOTE: This is not going to work if bootloader put us above 4G
322 * The first step is go into compatibility mode.
325 /* Clear additional page table */
326 leaq lvl5_pgtable(%rbx), %rdi
328 movq $(PAGE_SIZE/8), %rcx
332 * Setup current CR3 as the first and only entry in a new top level
336 leaq 0x7 (%rdi), %rax
337 movq %rax, lvl5_pgtable(%rbx)
339 /* Switch to compatibility mode (CS.L = 0 CS.D = 1) via far return */
341 leaq compatible_mode(%rip), %rax
352 * Copy the compressed kernel to the end of our buffer
353 * where decompression in place becomes safe.
356 leaq (_bss-8)(%rip), %rsi
357 leaq (_bss-8)(%rbx), %rdi
358 movq $_bss /* - $startup_32 */, %rcx
366 * Jump to the relocated address.
368 leaq relocated(%rbx), %rax
371 #ifdef CONFIG_EFI_STUB
373 /* The entry point for the PE/COFF executable is efi_pe_entry. */
375 movq %rcx, efi64_config(%rip) /* Handle */
376 movq %rdx, efi64_config+8(%rip) /* EFI System table pointer */
378 leaq efi64_config(%rip), %rax
379 movq %rax, efi_config(%rip)
386 * Relocate efi_config->call().
388 addq %rbp, efi64_config+40(%rip)
391 call make_boot_params
395 leaq startup_32(%rip), %rax
396 movl %eax, BP_code32_start(%rsi)
397 jmp 2f /* Skip the relocation */
405 * Relocate efi_config->call().
407 movq efi_config(%rip), %rax
410 movq efi_config(%rip), %rdi
416 /* EFI init failed, so hang. */
420 movl BP_code32_start(%esi), %eax
421 leaq startup_64(%rax), %rax
423 ENDPROC(efi_pe_entry)
426 ENTRY(efi64_stub_entry)
427 movq %rdi, efi64_config(%rip) /* Handle */
428 movq %rsi, efi64_config+8(%rip) /* EFI System table pointer */
430 leaq efi64_config(%rip), %rax
431 movq %rax, efi_config(%rip)
435 ENDPROC(efi64_stub_entry)
442 * Clear BSS (stack is currently empty)
445 leaq _bss(%rip), %rdi
446 leaq _ebss(%rip), %rcx
454 leaq _got(%rip), %rdx
455 leaq _egot(%rip), %rcx
465 * Do the extraction, and jump to the new kernel..
467 pushq %rsi /* Save the real mode argument */
468 movq %rsi, %rdi /* real mode address */
469 leaq boot_heap(%rip), %rsi /* malloc area for uncompression */
470 leaq input_data(%rip), %rdx /* input_data */
471 movl $z_input_len, %ecx /* input_len */
472 movq %rbp, %r8 /* output target address */
473 movq $z_output_len, %r9 /* decompressed length, end of relocs */
474 call extract_kernel /* returns kernel location in %rax */
478 * Jump to the decompressed kernel.
483 #ifdef CONFIG_X86_5LEVEL
485 /* Setup data and stack segments */
486 movl $__KERNEL_DS, %eax
492 btrl $X86_CR0_PG_BIT, %eax
495 /* Point CR3 to 5-level paging */
496 leal lvl5_pgtable(%ebx), %eax
499 /* Enable PAE and LA57 mode */
501 orl $(X86_CR4_PAE | X86_CR4_LA57), %eax
504 /* Calculate address we are running at */
509 /* Prepare stack for far return to Long Mode */
511 leal lvl5(%edi), %eax
514 /* Enable paging back */
515 movl $(X86_CR0_PG | X86_CR0_PE), %eax
522 /* This isn't an x86-64 CPU so hang */
527 #include "../../kernel/verify_cpu.S"
534 .quad 0x00cf9a000000ffff /* __KERNEL32_CS */
535 .quad 0x00af9a000000ffff /* __KERNEL_CS */
536 .quad 0x00cf92000000ffff /* __KERNEL_DS */
537 .quad 0x0080890000000000 /* TS descriptor */
538 .quad 0x0000000000000000 /* TS continued */
541 #ifdef CONFIG_EFI_STUB
545 #ifdef CONFIG_EFI_MIXED
558 #endif /* CONFIG_EFI_STUB */
561 * Stack and heap for uncompression
566 .fill BOOT_HEAP_SIZE, 1, 0
568 .fill BOOT_STACK_SIZE, 1, 0
572 * Space for page tables (not in .bss so not zeroed)
574 .section ".pgtable","a",@nobits
577 .fill BOOT_PGT_SIZE, 1, 0
578 #ifdef CONFIG_X86_5LEVEL
580 .fill PAGE_SIZE, 1, 0