1/* SPDX-License-Identifier: GPL-2.0 */ 2/* 3 * linux/boot/head.S 4 * 5 * Copyright (C) 1991, 1992, 1993 Linus Torvalds 6 */ 7 8/* 9 * head.S contains the 32-bit startup code. 10 * 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] 15 * 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 19 * mode. 20 */ 21 22/* 23 * High loaded stuff by Hans Lermen & Werner Almesberger, Feb. 1996 24 */ 25 .code32 26 .text 27 28#include <linux/init.h> 29#include <linux/linkage.h> 30#include <asm/segment.h> 31#include <asm/boot.h> 32#include <asm/msr.h> 33#include <asm/processor-flags.h> 34#include <asm/asm-offsets.h> 35#include <asm/bootparam.h> 36#include <asm/desc_defs.h> 37#include "pgtable.h" 38 39/* 40 * Locally defined symbols should be marked hidden: 41 */ 42 .hidden _bss 43 .hidden _ebss 44 .hidden _end 45 46 __HEAD 47 48/* 49 * This macro gives the relative virtual address of X, i.e. the offset of X 50 * from startup_32. This is the same as the link-time virtual address of X, 51 * since startup_32 is at 0, but defining it this way tells the 52 * assembler/linker that we do not want the actual run-time address of X. This 53 * prevents the linker from trying to create unwanted run-time relocation 54 * entries for the reference when the compressed kernel is linked as PIE. 55 * 56 * A reference X(%reg) will result in the link-time VA of X being stored with 57 * the instruction, and a run-time R_X86_64_RELATIVE relocation entry that 58 * adds the 64-bit base address where the kernel is loaded. 59 * 60 * Replacing it with (X-startup_32)(%reg) results in the offset being stored, 61 * and no run-time relocation. 62 * 63 * The macro should be used as a displacement with a base register containing 64 * the run-time address of startup_32 [i.e. rva(X)(%reg)], or as an immediate 65 * [$ rva(X)]. 66 * 67 * This macro can only be used from within the .head.text section, since the 68 * expression requires startup_32 to be in the same section as the code being 69 * assembled. 70 */ 71#define rva(X) ((X) - startup_32) 72 73 .code32 74SYM_FUNC_START(startup_32) 75 /* 76 * 32bit entry is 0 and it is ABI so immutable! 77 * If we come here directly from a bootloader, 78 * kernel(text+data+bss+brk) ramdisk, zero_page, command line 79 * all need to be under the 4G limit. 80 */ 81 cld 82 cli 83 84/* 85 * Calculate the delta between where we were compiled to run 86 * at and where we were actually loaded at. This can only be done 87 * with a short local call on x86. Nothing else will tell us what 88 * address we are running at. The reserved chunk of the real-mode 89 * data at 0x1e4 (defined as a scratch field) are used as the stack 90 * for this calculation. Only 4 bytes are needed. 91 */ 92 leal (BP_scratch+4)(%esi), %esp 93 call 1f 941: popl %ebp 95 subl $ rva(1b), %ebp 96 97 /* Load new GDT with the 64bit segments using 32bit descriptor */ 98 leal rva(gdt)(%ebp), %eax 99 movl %eax, 2(%eax) 100 lgdt (%eax) 101 102 /* Load segment registers with our descriptors */ 103 movl $__BOOT_DS, %eax 104 movl %eax, %ds 105 movl %eax, %es 106 movl %eax, %fs 107 movl %eax, %gs 108 movl %eax, %ss 109 110/* setup a stack and make sure cpu supports long mode. */ 111 leal rva(boot_stack_end)(%ebp), %esp 112 113 call verify_cpu 114 testl %eax, %eax 115 jnz .Lno_longmode 116 117/* 118 * Compute the delta between where we were compiled to run at 119 * and where the code will actually run at. 120 * 121 * %ebp contains the address we are loaded at by the boot loader and %ebx 122 * contains the address where we should move the kernel image temporarily 123 * for safe in-place decompression. 124 */ 125 126#ifdef CONFIG_RELOCATABLE 127 movl %ebp, %ebx 128 129#ifdef CONFIG_EFI_STUB 130/* 131 * If we were loaded via the EFI LoadImage service, startup_32 will be at an 132 * offset to the start of the space allocated for the image. efi_pe_entry will 133 * set up image_offset to tell us where the image actually starts, so that we 134 * can use the full available buffer. 135 * image_offset = startup_32 - image_base 136 * Otherwise image_offset will be zero and has no effect on the calculations. 137 */ 138 subl rva(image_offset)(%ebp), %ebx 139#endif 140 141 movl BP_kernel_alignment(%esi), %eax 142 decl %eax 143 addl %eax, %ebx 144 notl %eax 145 andl %eax, %ebx 146 cmpl $LOAD_PHYSICAL_ADDR, %ebx 147 jae 1f 148#endif 149 movl $LOAD_PHYSICAL_ADDR, %ebx 1501: 151 152 /* Target address to relocate to for decompression */ 153 addl BP_init_size(%esi), %ebx 154 subl $ rva(_end), %ebx 155 156/* 157 * Prepare for entering 64 bit mode 158 */ 159 160 /* Enable PAE mode */ 161 movl %cr4, %eax 162 orl $X86_CR4_PAE, %eax 163 movl %eax, %cr4 164 165 /* 166 * Build early 4G boot pagetable 167 */ 168 /* 169 * If SEV is active then set the encryption mask in the page tables. 170 * This will insure that when the kernel is copied and decompressed 171 * it will be done so encrypted. 172 */ 173 call get_sev_encryption_bit 174 xorl %edx, %edx 175#ifdef CONFIG_AMD_MEM_ENCRYPT 176 testl %eax, %eax 177 jz 1f 178 subl $32, %eax /* Encryption bit is always above bit 31 */ 179 bts %eax, %edx /* Set encryption mask for page tables */ 180 /* 181 * Mark SEV as active in sev_status so that startup32_check_sev_cbit() 182 * will do a check. The sev_status memory will be fully initialized 183 * with the contents of MSR_AMD_SEV_STATUS later in 184 * set_sev_encryption_mask(). For now it is sufficient to know that SEV 185 * is active. 186 */ 187 movl $1, rva(sev_status)(%ebp) 1881: 189#endif 190 191 /* Initialize Page tables to 0 */ 192 leal rva(pgtable)(%ebx), %edi 193 xorl %eax, %eax 194 movl $(BOOT_INIT_PGT_SIZE/4), %ecx 195 rep stosl 196 197 /* Build Level 4 */ 198 leal rva(pgtable + 0)(%ebx), %edi 199 leal 0x1007 (%edi), %eax 200 movl %eax, 0(%edi) 201 addl %edx, 4(%edi) 202 203 /* Build Level 3 */ 204 leal rva(pgtable + 0x1000)(%ebx), %edi 205 leal 0x1007(%edi), %eax 206 movl $4, %ecx 2071: movl %eax, 0x00(%edi) 208 addl %edx, 0x04(%edi) 209 addl $0x00001000, %eax 210 addl $8, %edi 211 decl %ecx 212 jnz 1b 213 214 /* Build Level 2 */ 215 leal rva(pgtable + 0x2000)(%ebx), %edi 216 movl $0x00000183, %eax 217 movl $2048, %ecx 2181: movl %eax, 0(%edi) 219 addl %edx, 4(%edi) 220 addl $0x00200000, %eax 221 addl $8, %edi 222 decl %ecx 223 jnz 1b 224 225 /* Enable the boot page tables */ 226 leal rva(pgtable)(%ebx), %eax 227 movl %eax, %cr3 228 229 /* Enable Long mode in EFER (Extended Feature Enable Register) */ 230 movl $MSR_EFER, %ecx 231 rdmsr 232 btsl $_EFER_LME, %eax 233 wrmsr 234 235 /* After gdt is loaded */ 236 xorl %eax, %eax 237 lldt %ax 238 movl $__BOOT_TSS, %eax 239 ltr %ax 240 241 /* 242 * Setup for the jump to 64bit mode 243 * 244 * When the jump is performend we will be in long mode but 245 * in 32bit compatibility mode with EFER.LME = 1, CS.L = 0, CS.D = 1 246 * (and in turn EFER.LMA = 1). To jump into 64bit mode we use 247 * the new gdt/idt that has __KERNEL_CS with CS.L = 1. 248 * We place all of the values on our mini stack so lret can 249 * used to perform that far jump. 250 */ 251 leal rva(startup_64)(%ebp), %eax 252#ifdef CONFIG_EFI_MIXED 253 movl rva(efi32_boot_args)(%ebp), %edi 254 cmp $0, %edi 255 jz 1f 256 leal rva(efi64_stub_entry)(%ebp), %eax 257 movl rva(efi32_boot_args+4)(%ebp), %esi 258 movl rva(efi32_boot_args+8)(%ebp), %edx // saved bootparams pointer 259 cmpl $0, %edx 260 jnz 1f 261 /* 262 * efi_pe_entry uses MS calling convention, which requires 32 bytes of 263 * shadow space on the stack even if all arguments are passed in 264 * registers. We also need an additional 8 bytes for the space that 265 * would be occupied by the return address, and this also results in 266 * the correct stack alignment for entry. 267 */ 268 subl $40, %esp 269 leal rva(efi_pe_entry)(%ebp), %eax 270 movl %edi, %ecx // MS calling convention 271 movl %esi, %edx 2721: 273#endif 274 /* Check if the C-bit position is correct when SEV is active */ 275 call startup32_check_sev_cbit 276 277 pushl $__KERNEL_CS 278 pushl %eax 279 280 /* Enter paged protected Mode, activating Long Mode */ 281 movl $(X86_CR0_PG | X86_CR0_PE), %eax /* Enable Paging and Protected mode */ 282 movl %eax, %cr0 283 284 /* Jump from 32bit compatibility mode into 64bit mode. */ 285 lret 286SYM_FUNC_END(startup_32) 287 288#ifdef CONFIG_EFI_MIXED 289 .org 0x190 290SYM_FUNC_START(efi32_stub_entry) 291 add $0x4, %esp /* Discard return address */ 292 popl %ecx 293 popl %edx 294 popl %esi 295 296 call 1f 2971: pop %ebp 298 subl $ rva(1b), %ebp 299 300 movl %esi, rva(efi32_boot_args+8)(%ebp) 301SYM_INNER_LABEL(efi32_pe_stub_entry, SYM_L_LOCAL) 302 movl %ecx, rva(efi32_boot_args)(%ebp) 303 movl %edx, rva(efi32_boot_args+4)(%ebp) 304 movb $0, rva(efi_is64)(%ebp) 305 306 /* Save firmware GDTR and code/data selectors */ 307 sgdtl rva(efi32_boot_gdt)(%ebp) 308 movw %cs, rva(efi32_boot_cs)(%ebp) 309 movw %ds, rva(efi32_boot_ds)(%ebp) 310 311 /* Disable paging */ 312 movl %cr0, %eax 313 btrl $X86_CR0_PG_BIT, %eax 314 movl %eax, %cr0 315 316 jmp startup_32 317SYM_FUNC_END(efi32_stub_entry) 318#endif 319 320 .code64 321 .org 0x200 322SYM_CODE_START(startup_64) 323 /* 324 * 64bit entry is 0x200 and it is ABI so immutable! 325 * We come here either from startup_32 or directly from a 326 * 64bit bootloader. 327 * If we come here from a bootloader, kernel(text+data+bss+brk), 328 * ramdisk, zero_page, command line could be above 4G. 329 * We depend on an identity mapped page table being provided 330 * that maps our entire kernel(text+data+bss+brk), zero page 331 * and command line. 332 */ 333 334 cld 335 cli 336 337 /* Setup data segments. */ 338 xorl %eax, %eax 339 movl %eax, %ds 340 movl %eax, %es 341 movl %eax, %ss 342 movl %eax, %fs 343 movl %eax, %gs 344 345 /* 346 * Compute the decompressed kernel start address. It is where 347 * we were loaded at aligned to a 2M boundary. %rbp contains the 348 * decompressed kernel start address. 349 * 350 * If it is a relocatable kernel then decompress and run the kernel 351 * from load address aligned to 2MB addr, otherwise decompress and 352 * run the kernel from LOAD_PHYSICAL_ADDR 353 * 354 * We cannot rely on the calculation done in 32-bit mode, since we 355 * may have been invoked via the 64-bit entry point. 356 */ 357 358 /* Start with the delta to where the kernel will run at. */ 359#ifdef CONFIG_RELOCATABLE 360 leaq startup_32(%rip) /* - $startup_32 */, %rbp 361 362#ifdef CONFIG_EFI_STUB 363/* 364 * If we were loaded via the EFI LoadImage service, startup_32 will be at an 365 * offset to the start of the space allocated for the image. efi_pe_entry will 366 * set up image_offset to tell us where the image actually starts, so that we 367 * can use the full available buffer. 368 * image_offset = startup_32 - image_base 369 * Otherwise image_offset will be zero and has no effect on the calculations. 370 */ 371 movl image_offset(%rip), %eax 372 subq %rax, %rbp 373#endif 374 375 movl BP_kernel_alignment(%rsi), %eax 376 decl %eax 377 addq %rax, %rbp 378 notq %rax 379 andq %rax, %rbp 380 cmpq $LOAD_PHYSICAL_ADDR, %rbp 381 jae 1f 382#endif 383 movq $LOAD_PHYSICAL_ADDR, %rbp 3841: 385 386 /* Target address to relocate to for decompression */ 387 movl BP_init_size(%rsi), %ebx 388 subl $ rva(_end), %ebx 389 addq %rbp, %rbx 390 391 /* Set up the stack */ 392 leaq rva(boot_stack_end)(%rbx), %rsp 393 394 /* 395 * At this point we are in long mode with 4-level paging enabled, 396 * but we might want to enable 5-level paging or vice versa. 397 * 398 * The problem is that we cannot do it directly. Setting or clearing 399 * CR4.LA57 in long mode would trigger #GP. So we need to switch off 400 * long mode and paging first. 401 * 402 * We also need a trampoline in lower memory to switch over from 403 * 4- to 5-level paging for cases when the bootloader puts the kernel 404 * above 4G, but didn't enable 5-level paging for us. 405 * 406 * The same trampoline can be used to switch from 5- to 4-level paging 407 * mode, like when starting 4-level paging kernel via kexec() when 408 * original kernel worked in 5-level paging mode. 409 * 410 * For the trampoline, we need the top page table to reside in lower 411 * memory as we don't have a way to load 64-bit values into CR3 in 412 * 32-bit mode. 413 * 414 * We go though the trampoline even if we don't have to: if we're 415 * already in a desired paging mode. This way the trampoline code gets 416 * tested on every boot. 417 */ 418 419 /* Make sure we have GDT with 32-bit code segment */ 420 leaq gdt64(%rip), %rax 421 addq %rax, 2(%rax) 422 lgdt (%rax) 423 424 /* Reload CS so IRET returns to a CS actually in the GDT */ 425 pushq $__KERNEL_CS 426 leaq .Lon_kernel_cs(%rip), %rax 427 pushq %rax 428 lretq 429 430.Lon_kernel_cs: 431 432 pushq %rsi 433 call load_stage1_idt 434 popq %rsi 435 436 /* 437 * paging_prepare() sets up the trampoline and checks if we need to 438 * enable 5-level paging. 439 * 440 * paging_prepare() returns a two-quadword structure which lands 441 * into RDX:RAX: 442 * - Address of the trampoline is returned in RAX. 443 * - Non zero RDX means trampoline needs to enable 5-level 444 * paging. 445 * 446 * RSI holds real mode data and needs to be preserved across 447 * this function call. 448 */ 449 pushq %rsi 450 movq %rsi, %rdi /* real mode address */ 451 call paging_prepare 452 popq %rsi 453 454 /* Save the trampoline address in RCX */ 455 movq %rax, %rcx 456 457 /* Set up 32-bit addressable stack */ 458 leaq TRAMPOLINE_32BIT_STACK_END(%rcx), %rsp 459 460 /* 461 * Preserve live 64-bit registers on the stack: this is necessary 462 * because the architecture does not guarantee that GPRs will retain 463 * their full 64-bit values across a 32-bit mode switch. 464 */ 465 pushq %rbp 466 pushq %rbx 467 pushq %rsi 468 469 /* 470 * Push the 64-bit address of trampoline_return() onto the new stack. 471 * It will be used by the trampoline to return to the main code. Due to 472 * the 32-bit mode switch, it cannot be kept it in a register either. 473 */ 474 leaq trampoline_return(%rip), %rdi 475 pushq %rdi 476 477 /* Switch to compatibility mode (CS.L = 0 CS.D = 1) via far return */ 478 pushq $__KERNEL32_CS 479 leaq TRAMPOLINE_32BIT_CODE_OFFSET(%rax), %rax 480 pushq %rax 481 lretq 482trampoline_return: 483 /* Restore live 64-bit registers */ 484 popq %rsi 485 popq %rbx 486 popq %rbp 487 488 /* Restore the stack, the 32-bit trampoline uses its own stack */ 489 leaq rva(boot_stack_end)(%rbx), %rsp 490 491 /* 492 * cleanup_trampoline() would restore trampoline memory. 493 * 494 * RDI is address of the page table to use instead of page table 495 * in trampoline memory (if required). 496 * 497 * RSI holds real mode data and needs to be preserved across 498 * this function call. 499 */ 500 pushq %rsi 501 leaq rva(top_pgtable)(%rbx), %rdi 502 call cleanup_trampoline 503 popq %rsi 504 505 /* Zero EFLAGS */ 506 pushq $0 507 popfq 508 509/* 510 * Copy the compressed kernel to the end of our buffer 511 * where decompression in place becomes safe. 512 */ 513 pushq %rsi 514 leaq (_bss-8)(%rip), %rsi 515 leaq rva(_bss-8)(%rbx), %rdi 516 movl $(_bss - startup_32), %ecx 517 shrl $3, %ecx 518 std 519 rep movsq 520 cld 521 popq %rsi 522 523 /* 524 * The GDT may get overwritten either during the copy we just did or 525 * during extract_kernel below. To avoid any issues, repoint the GDTR 526 * to the new copy of the GDT. 527 */ 528 leaq rva(gdt64)(%rbx), %rax 529 leaq rva(gdt)(%rbx), %rdx 530 movq %rdx, 2(%rax) 531 lgdt (%rax) 532 533/* 534 * Jump to the relocated address. 535 */ 536 leaq rva(.Lrelocated)(%rbx), %rax 537 jmp *%rax 538SYM_CODE_END(startup_64) 539 540#ifdef CONFIG_EFI_STUB 541 .org 0x390 542SYM_FUNC_START(efi64_stub_entry) 543SYM_FUNC_START_ALIAS(efi_stub_entry) 544 and $~0xf, %rsp /* realign the stack */ 545 movq %rdx, %rbx /* save boot_params pointer */ 546 call efi_main 547 movq %rbx,%rsi 548 leaq rva(startup_64)(%rax), %rax 549 jmp *%rax 550SYM_FUNC_END(efi64_stub_entry) 551SYM_FUNC_END_ALIAS(efi_stub_entry) 552#endif 553 554 .text 555SYM_FUNC_START_LOCAL_NOALIGN(.Lrelocated) 556 557/* 558 * Clear BSS (stack is currently empty) 559 */ 560 xorl %eax, %eax 561 leaq _bss(%rip), %rdi 562 leaq _ebss(%rip), %rcx 563 subq %rdi, %rcx 564 shrq $3, %rcx 565 rep stosq 566 567/* 568 * If running as an SEV guest, the encryption mask is required in the 569 * page-table setup code below. When the guest also has SEV-ES enabled 570 * set_sev_encryption_mask() will cause #VC exceptions, but the stage2 571 * handler can't map its GHCB because the page-table is not set up yet. 572 * So set up the encryption mask here while still on the stage1 #VC 573 * handler. Then load stage2 IDT and switch to the kernel's own 574 * page-table. 575 */ 576 pushq %rsi 577 call set_sev_encryption_mask 578 call load_stage2_idt 579 580 /* Pass boot_params to initialize_identity_maps() */ 581 movq (%rsp), %rdi 582 call initialize_identity_maps 583 popq %rsi 584 585/* 586 * Do the extraction, and jump to the new kernel.. 587 */ 588 pushq %rsi /* Save the real mode argument */ 589 movq %rsi, %rdi /* real mode address */ 590 leaq boot_heap(%rip), %rsi /* malloc area for uncompression */ 591 leaq input_data(%rip), %rdx /* input_data */ 592 movl input_len(%rip), %ecx /* input_len */ 593 movq %rbp, %r8 /* output target address */ 594 movl output_len(%rip), %r9d /* decompressed length, end of relocs */ 595 call extract_kernel /* returns kernel location in %rax */ 596 popq %rsi 597 598/* 599 * Jump to the decompressed kernel. 600 */ 601 jmp *%rax 602SYM_FUNC_END(.Lrelocated) 603 604 .code32 605/* 606 * This is the 32-bit trampoline that will be copied over to low memory. 607 * 608 * Return address is at the top of the stack (might be above 4G). 609 * ECX contains the base address of the trampoline memory. 610 * Non zero RDX means trampoline needs to enable 5-level paging. 611 */ 612SYM_CODE_START(trampoline_32bit_src) 613 /* Set up data and stack segments */ 614 movl $__KERNEL_DS, %eax 615 movl %eax, %ds 616 movl %eax, %ss 617 618 /* Disable paging */ 619 movl %cr0, %eax 620 btrl $X86_CR0_PG_BIT, %eax 621 movl %eax, %cr0 622 623 /* Check what paging mode we want to be in after the trampoline */ 624 cmpl $0, %edx 625 jz 1f 626 627 /* We want 5-level paging: don't touch CR3 if it already points to 5-level page tables */ 628 movl %cr4, %eax 629 testl $X86_CR4_LA57, %eax 630 jnz 3f 631 jmp 2f 6321: 633 /* We want 4-level paging: don't touch CR3 if it already points to 4-level page tables */ 634 movl %cr4, %eax 635 testl $X86_CR4_LA57, %eax 636 jz 3f 6372: 638 /* Point CR3 to the trampoline's new top level page table */ 639 leal TRAMPOLINE_32BIT_PGTABLE_OFFSET(%ecx), %eax 640 movl %eax, %cr3 6413: 642 /* Set EFER.LME=1 as a precaution in case hypervsior pulls the rug */ 643 pushl %ecx 644 pushl %edx 645 movl $MSR_EFER, %ecx 646 rdmsr 647 btsl $_EFER_LME, %eax 648 wrmsr 649 popl %edx 650 popl %ecx 651 652 /* Enable PAE and LA57 (if required) paging modes */ 653 movl $X86_CR4_PAE, %eax 654 cmpl $0, %edx 655 jz 1f 656 orl $X86_CR4_LA57, %eax 6571: 658 movl %eax, %cr4 659 660 /* Calculate address of paging_enabled() once we are executing in the trampoline */ 661 leal .Lpaging_enabled - trampoline_32bit_src + TRAMPOLINE_32BIT_CODE_OFFSET(%ecx), %eax 662 663 /* Prepare the stack for far return to Long Mode */ 664 pushl $__KERNEL_CS 665 pushl %eax 666 667 /* Enable paging again */ 668 movl $(X86_CR0_PG | X86_CR0_PE), %eax 669 movl %eax, %cr0 670 671 lret 672SYM_CODE_END(trampoline_32bit_src) 673 674 .code64 675SYM_FUNC_START_LOCAL_NOALIGN(.Lpaging_enabled) 676 /* Return from the trampoline */ 677 retq 678SYM_FUNC_END(.Lpaging_enabled) 679 680 /* 681 * The trampoline code has a size limit. 682 * Make sure we fail to compile if the trampoline code grows 683 * beyond TRAMPOLINE_32BIT_CODE_SIZE bytes. 684 */ 685 .org trampoline_32bit_src + TRAMPOLINE_32BIT_CODE_SIZE 686 687 .code32 688SYM_FUNC_START_LOCAL_NOALIGN(.Lno_longmode) 689 /* This isn't an x86-64 CPU, so hang intentionally, we cannot continue */ 6901: 691 hlt 692 jmp 1b 693SYM_FUNC_END(.Lno_longmode) 694 695#include "../../kernel/verify_cpu.S" 696 697 .data 698SYM_DATA_START_LOCAL(gdt64) 699 .word gdt_end - gdt - 1 700 .quad gdt - gdt64 701SYM_DATA_END(gdt64) 702 .balign 8 703SYM_DATA_START_LOCAL(gdt) 704 .word gdt_end - gdt - 1 705 .long 0 706 .word 0 707 .quad 0x00cf9a000000ffff /* __KERNEL32_CS */ 708 .quad 0x00af9a000000ffff /* __KERNEL_CS */ 709 .quad 0x00cf92000000ffff /* __KERNEL_DS */ 710 .quad 0x0080890000000000 /* TS descriptor */ 711 .quad 0x0000000000000000 /* TS continued */ 712SYM_DATA_END_LABEL(gdt, SYM_L_LOCAL, gdt_end) 713 714SYM_DATA_START(boot_idt_desc) 715 .word boot_idt_end - boot_idt - 1 716 .quad 0 717SYM_DATA_END(boot_idt_desc) 718 .balign 8 719SYM_DATA_START(boot_idt) 720 .rept BOOT_IDT_ENTRIES 721 .quad 0 722 .quad 0 723 .endr 724SYM_DATA_END_LABEL(boot_idt, SYM_L_GLOBAL, boot_idt_end) 725 726#ifdef CONFIG_EFI_STUB 727SYM_DATA(image_offset, .long 0) 728#endif 729#ifdef CONFIG_EFI_MIXED 730SYM_DATA_LOCAL(efi32_boot_args, .long 0, 0, 0) 731SYM_DATA(efi_is64, .byte 1) 732 733#define ST32_boottime 60 // offsetof(efi_system_table_32_t, boottime) 734#define BS32_handle_protocol 88 // offsetof(efi_boot_services_32_t, handle_protocol) 735#define LI32_image_base 32 // offsetof(efi_loaded_image_32_t, image_base) 736 737 __HEAD 738 .code32 739SYM_FUNC_START(efi32_pe_entry) 740/* 741 * efi_status_t efi32_pe_entry(efi_handle_t image_handle, 742 * efi_system_table_32_t *sys_table) 743 */ 744 745 pushl %ebp 746 movl %esp, %ebp 747 pushl %eax // dummy push to allocate loaded_image 748 749 pushl %ebx // save callee-save registers 750 pushl %edi 751 752 call verify_cpu // check for long mode support 753 testl %eax, %eax 754 movl $0x80000003, %eax // EFI_UNSUPPORTED 755 jnz 2f 756 757 call 1f 7581: pop %ebx 759 subl $ rva(1b), %ebx 760 761 /* Get the loaded image protocol pointer from the image handle */ 762 leal -4(%ebp), %eax 763 pushl %eax // &loaded_image 764 leal rva(loaded_image_proto)(%ebx), %eax 765 pushl %eax // pass the GUID address 766 pushl 8(%ebp) // pass the image handle 767 768 /* 769 * Note the alignment of the stack frame. 770 * sys_table 771 * handle <-- 16-byte aligned on entry by ABI 772 * return address 773 * frame pointer 774 * loaded_image <-- local variable 775 * saved %ebx <-- 16-byte aligned here 776 * saved %edi 777 * &loaded_image 778 * &loaded_image_proto 779 * handle <-- 16-byte aligned for call to handle_protocol 780 */ 781 782 movl 12(%ebp), %eax // sys_table 783 movl ST32_boottime(%eax), %eax // sys_table->boottime 784 call *BS32_handle_protocol(%eax) // sys_table->boottime->handle_protocol 785 addl $12, %esp // restore argument space 786 testl %eax, %eax 787 jnz 2f 788 789 movl 8(%ebp), %ecx // image_handle 790 movl 12(%ebp), %edx // sys_table 791 movl -4(%ebp), %esi // loaded_image 792 movl LI32_image_base(%esi), %esi // loaded_image->image_base 793 movl %ebx, %ebp // startup_32 for efi32_pe_stub_entry 794 /* 795 * We need to set the image_offset variable here since startup_32() will 796 * use it before we get to the 64-bit efi_pe_entry() in C code. 797 */ 798 subl %esi, %ebx 799 movl %ebx, rva(image_offset)(%ebp) // save image_offset 800 jmp efi32_pe_stub_entry 801 8022: popl %edi // restore callee-save registers 803 popl %ebx 804 leave 805 RET 806SYM_FUNC_END(efi32_pe_entry) 807 808 .section ".rodata" 809 /* EFI loaded image protocol GUID */ 810 .balign 4 811SYM_DATA_START_LOCAL(loaded_image_proto) 812 .long 0x5b1b31a1 813 .word 0x9562, 0x11d2 814 .byte 0x8e, 0x3f, 0x00, 0xa0, 0xc9, 0x69, 0x72, 0x3b 815SYM_DATA_END(loaded_image_proto) 816#endif 817 818/* 819 * Check for the correct C-bit position when the startup_32 boot-path is used. 820 * 821 * The check makes use of the fact that all memory is encrypted when paging is 822 * disabled. The function creates 64 bits of random data using the RDRAND 823 * instruction. RDRAND is mandatory for SEV guests, so always available. If the 824 * hypervisor violates that the kernel will crash right here. 825 * 826 * The 64 bits of random data are stored to a memory location and at the same 827 * time kept in the %eax and %ebx registers. Since encryption is always active 828 * when paging is off the random data will be stored encrypted in main memory. 829 * 830 * Then paging is enabled. When the C-bit position is correct all memory is 831 * still mapped encrypted and comparing the register values with memory will 832 * succeed. An incorrect C-bit position will map all memory unencrypted, so that 833 * the compare will use the encrypted random data and fail. 834 */ 835 __HEAD 836 .code32 837SYM_FUNC_START(startup32_check_sev_cbit) 838#ifdef CONFIG_AMD_MEM_ENCRYPT 839 pushl %eax 840 pushl %ebx 841 pushl %ecx 842 pushl %edx 843 844 /* Check for non-zero sev_status */ 845 movl rva(sev_status)(%ebp), %eax 846 testl %eax, %eax 847 jz 4f 848 849 /* 850 * Get two 32-bit random values - Don't bail out if RDRAND fails 851 * because it is better to prevent forward progress if no random value 852 * can be gathered. 853 */ 8541: rdrand %eax 855 jnc 1b 8562: rdrand %ebx 857 jnc 2b 858 859 /* Store to memory and keep it in the registers */ 860 movl %eax, rva(sev_check_data)(%ebp) 861 movl %ebx, rva(sev_check_data+4)(%ebp) 862 863 /* Enable paging to see if encryption is active */ 864 movl %cr0, %edx /* Backup %cr0 in %edx */ 865 movl $(X86_CR0_PG | X86_CR0_PE), %ecx /* Enable Paging and Protected mode */ 866 movl %ecx, %cr0 867 868 cmpl %eax, rva(sev_check_data)(%ebp) 869 jne 3f 870 cmpl %ebx, rva(sev_check_data+4)(%ebp) 871 jne 3f 872 873 movl %edx, %cr0 /* Restore previous %cr0 */ 874 875 jmp 4f 876 8773: /* Check failed - hlt the machine */ 878 hlt 879 jmp 3b 880 8814: 882 popl %edx 883 popl %ecx 884 popl %ebx 885 popl %eax 886#endif 887 RET 888SYM_FUNC_END(startup32_check_sev_cbit) 889 890/* 891 * Stack and heap for uncompression 892 */ 893 .bss 894 .balign 4 895SYM_DATA_LOCAL(boot_heap, .fill BOOT_HEAP_SIZE, 1, 0) 896 897SYM_DATA_START_LOCAL(boot_stack) 898 .fill BOOT_STACK_SIZE, 1, 0 899 .balign 16 900SYM_DATA_END_LABEL(boot_stack, SYM_L_LOCAL, boot_stack_end) 901 902/* 903 * Space for page tables (not in .bss so not zeroed) 904 */ 905 .section ".pgtable","aw",@nobits 906 .balign 4096 907SYM_DATA_LOCAL(pgtable, .fill BOOT_PGT_SIZE, 1, 0) 908 909/* 910 * The page table is going to be used instead of page table in the trampoline 911 * memory. 912 */ 913SYM_DATA_LOCAL(top_pgtable, .fill PAGE_SIZE, 1, 0) 914