Debian Codegraph @

payloads/libpayload/arch/x86/head_64.S

raw
1 /*
2 *
3 * Copyright (C) 2024 Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
15 *
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
26 * SUCH DAMAGE.
27 */
28
29 #define IA32_EFER 0xC0000080
30 #define EFER_LME (1 << 8)
31
32 .code32
33 .global _entry
34 .section .text._entry
35 .align 4
36
37 /*
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
42 * - 64-bit long 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
45 * environments.
46 */
47
48 _entry:
49
50 /* Add multiboot header and jump around it when building with multiboot support. */
51 #if CONFIG(LP_MULTIBOOT)
52 #include "multiboot_header.inc"
53 #endif
54 /* No interrupts, please. */
55 cli
56
57 movl $IA32_EFER, %ecx
58 rdmsr
59 testl $EFER_LME, %eax
60 jnz _init64
61
62 lgdt %cs:gdt_ptr
63
64 /* save pointer to coreboot tables */
65 movl 4(%esp), %eax
66 /*
67 * NOTE: coreboot tables has passed over the top of the stack
68 * while calling in protected mode.
69 */
70 movl %eax, cb_header_ptr
71
72 call init_page_table
73 movl $pml4e, %eax
74
75 /* load identity mapped page tables */
76 movl %eax, %cr3
77
78 /* enable PAE */
79 movl %cr4, %eax
80 btsl $5, %eax
81 movl %eax, %cr4
82
83 /* enable long mode */
84 movl $(IA32_EFER), %ecx
85 rdmsr
86 btsl $8, %eax
87 wrmsr
88
89 /* enable paging */
90 movl %cr0, %eax
91 btsl $31, %eax
92 movl %eax, %cr0
93
94 /* Jump to selgdt 0x20, flat x64 code segment */
95 ljmp $0x20, $_entry64
96
97 .code64
98 .align 16
99 _init64:
100 movabs $gdt_ptr, %rax
101 lgdt (%rax)
102
103 /*
104 * Note: The `cb_header_ptr` has passed as the first argument
105 * to the x86-64 calling convention.
106 */
107 movq %rdi, cb_header_ptr
108
109 call init_page_table
110 movq $pml4e, %rax
111
112 /* load identity mapped page tables */
113 movq %rax, %cr3
114
115 _entry64:
116 /* Store current stack pointer and set up new stack. */
117 movq %rsp, %rax
118 movabs $_estack, %rsp
119
120 push %rax
121
122 fninit
123 movq %cr0, %rax
124 andq $0xFFFFFFFFFFFFFFFB, %rax /* clear EM */
125 orq $0x00000022, %rax /* set MP, NE */
126 movq %rax, %cr0
127
128 movq %cr4, %rax
129 orq $0x00000600, %rax /* set OSFXSR, OSXMMEXCPT */
130 movq %rax, %cr4
131
132 /* Let's rock. */
133 call start_main
134
135 /* %rax has the return value - pass it on unmolested */
136 _leave:
137 /* Restore old stack. */
138 pop %rsp
139
140 /* Return to the original context. */
141 ret