1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
/*
*
* Copyright (C) 2024 Google Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#define IA32_EFER 0xC0000080
#define EFER_LME (1 << 8)
.code32
.global _entry
.section .text._entry
.align 4
/*
* WARNING: Critical Code Section - 32/64-bit Compatibility
* This code between `_entry` and `jnz _init64` is executed during system initialization.
* It MUST function correctly regardless of whether the system is booting in:
* - 32-bit protected mode
* - 64-bit long mode
* To achieve this, ONLY use instructions that produce identical binary output in both modes.
* Thoroughly test ALL modifications to this section in BOTH 32-bit and 64-bit boot
* environments.
*/
_entry:
/* Add multiboot header and jump around it when building with multiboot support. */
#if CONFIG(LP_MULTIBOOT)
#include "multiboot_header.inc"
#endif
/* No interrupts, please. */
cli
movl $IA32_EFER, %ecx
rdmsr
testl $EFER_LME, %eax
jnz _init64
lgdt %cs:gdt_ptr
/* save pointer to coreboot tables */
movl 4(%esp), %eax
/*
* NOTE: coreboot tables has passed over the top of the stack
* while calling in protected mode.
*/
movl %eax, cb_header_ptr
call init_page_table
movl $pm4le, %eax
/* load identity mapped page tables */
movl %eax, %cr3
/* enable PAE */
movl %cr4, %eax
btsl $5, %eax
movl %eax, %cr4
/* enable long mode */
movl $(IA32_EFER), %ecx
rdmsr
btsl $8, %eax
wrmsr
/* enable paging */
movl %cr0, %eax
btsl $31, %eax
movl %eax, %cr0
/* Jump to selgdt 0x20, flat x64 code segment */
ljmp $0x20, $_entry64
.code64
.align 16
_init64:
movabs $gdt_ptr, %rax
lgdt (%rax)
/*
* Note: The `cb_header_ptr` has passed as the first argument
* to the x86-64 calling convention.
*/
movq %rdi, cb_header_ptr
call init_page_table
movq $pm4le, %rax
/* load identity mapped page tables */
movq %rax, %cr3
_entry64:
/* Store current stack pointer and set up new stack. */
movq %rsp, %rax
movabs $_estack, %rsp
push %rax
fninit
movq %cr0, %rax
andq $0xFFFFFFFFFFFFFFFB, %rax /* clear EM */
orq $0x00000022, %rax /* set MP, NE */
movq %rax, %cr0
movq %cr4, %rax
orq $0x00000600, %rax /* set OSFXSR, OSXMMEXCPT */
movq %rax, %cr4
/* Let's rock. */
call start_main
/* %rax has the return value - pass it on unmolested */
_leave:
/* Restore old stack. */
pop %rsp
/* Return to the original context. */
ret
|