3 * Copyright (C) 2024 Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #define IA32_EFER 0xC0000080
30 #define EFER_LME (1 << 8)
38 * WARNING: Critical Code Section - 32/64-bit Compatibility
39 * This code between `_entry` and `jnz _init64` is executed during system initialization.
40 * It MUST function correctly regardless of whether the system is booting in:
41 * - 32-bit protected mode
43 * To achieve this, ONLY use instructions that produce identical binary output in both modes.
44 * Thoroughly test ALL modifications to this section in BOTH 32-bit and 64-bit boot
50 /* Add multiboot header and jump around it when building with multiboot support. */
51 #if CONFIG(LP_MULTIBOOT)
52 #include "multiboot_header.inc"
54 /* No interrupts, please. */
64 /* save pointer to coreboot tables */
67 * NOTE: coreboot tables has passed over the top of the stack
68 * while calling in protected mode.
70 movl %eax, cb_header_ptr
75 /* load identity mapped page tables */
83 /* enable long mode */
84 movl $(IA32_EFER), %ecx
94 /* Jump to selgdt 0x20, flat x64 code segment */
100 movabs $gdt_ptr, %rax
104 * Note: The `cb_header_ptr` has passed as the first argument
105 * to the x86-64 calling convention.
107 movq %rdi, cb_header_ptr
112 /* load identity mapped page tables */
116 /* Store current stack pointer and set up new stack. */
118 movabs $_estack, %rsp
124 andq $0xFFFFFFFFFFFFFFFB, %rax /* clear EM */
125 orq $0x00000022, %rax /* set MP, NE */
129 orq $0x00000600, %rax /* set OSFXSR, OSXMMEXCPT */
135 /* %rax has the return value - pass it on unmolested */
137 /* Restore old stack. */
140 /* Return to the original context. */