1 | /*
|
---|
2 | * Copyright (c) 2011 Martin Decky
|
---|
3 | * All rights reserved.
|
---|
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 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | #include <abi/asmtool.h>
|
---|
30 | #include <arch/boot/boot.h>
|
---|
31 | #include <arch/mm/page.h>
|
---|
32 | #include <arch/pm.h>
|
---|
33 | #include <arch/cpuid.h>
|
---|
34 | #include <arch/cpu.h>
|
---|
35 | #include <genarch/multiboot/multiboot2.h>
|
---|
36 |
|
---|
37 | #define START_STACK (BOOT_OFFSET - BOOT_STACK_SIZE)
|
---|
38 |
|
---|
39 | .section K_TEXT_START, "ax"
|
---|
40 |
|
---|
41 | .code32
|
---|
42 |
|
---|
43 | .align 8
|
---|
44 | multiboot2_header_start:
|
---|
45 | .long MULTIBOOT2_HEADER_MAGIC
|
---|
46 | .long MULTIBOOT2_HEADER_ARCH_I386
|
---|
47 | .long multiboot2_header_end - multiboot2_header_start
|
---|
48 | .long -(MULTIBOOT2_HEADER_MAGIC + MULTIBOOT2_HEADER_ARCH_I386 + (multiboot2_header_end - multiboot2_header_start))
|
---|
49 |
|
---|
50 | /* Information request tag */
|
---|
51 | .align 8
|
---|
52 | tag_info_req_start:
|
---|
53 | .word MULTIBOOT2_TAG_INFO_REQ
|
---|
54 | .word MULTIBOOT2_FLAGS_REQUIRED
|
---|
55 | .long tag_info_req_end - tag_info_req_start
|
---|
56 | .long MULTIBOOT2_TAG_MODULE
|
---|
57 | .long MULTIBOOT2_TAG_MEMMAP
|
---|
58 | #ifdef CONFIG_FB
|
---|
59 | .long MULTIBOOT2_TAG_FBINFO
|
---|
60 | #endif
|
---|
61 | tag_info_req_end:
|
---|
62 |
|
---|
63 | /* Address tag */
|
---|
64 | .align 8
|
---|
65 | tag_address_start:
|
---|
66 | .word MULTIBOOT2_TAG_ADDRESS
|
---|
67 | .word MULTIBOOT2_FLAGS_REQUIRED
|
---|
68 | .long tag_address_end - tag_address_start
|
---|
69 | .long multiboot2_header_start
|
---|
70 | .long unmapped_ktext_start
|
---|
71 | .long 0
|
---|
72 | .long 0
|
---|
73 | tag_address_end:
|
---|
74 |
|
---|
75 | /* Entry address tag */
|
---|
76 | .align 8
|
---|
77 | tag_entry_address_start:
|
---|
78 | .word MULTIBOOT2_TAG_ENTRY_ADDRESS
|
---|
79 | .word MULTIBOOT2_FLAGS_REQUIRED
|
---|
80 | .long tag_entry_address_end - tag_entry_address_start
|
---|
81 | .long multiboot2_image_start
|
---|
82 | tag_entry_address_end:
|
---|
83 |
|
---|
84 | /* Flags tag */
|
---|
85 | .align 8
|
---|
86 | tag_flags_start:
|
---|
87 | .word MULTIBOOT2_TAG_FLAGS
|
---|
88 | .word MULTIBOOT2_FLAGS_REQUIRED
|
---|
89 | .long tag_flags_end - tag_flags_start
|
---|
90 | .long MULTIBOOT2_FLAGS_CONSOLE
|
---|
91 | tag_flags_end:
|
---|
92 |
|
---|
93 | #ifdef CONFIG_FB
|
---|
94 | /* Framebuffer tag */
|
---|
95 | .align 8
|
---|
96 | tag_framebuffer_start:
|
---|
97 | .word MULTIBOOT2_TAG_FRAMEBUFFER
|
---|
98 | .word MULTIBOOT2_FLAGS_REQUIRED
|
---|
99 | .long tag_framebuffer_end - tag_framebuffer_start
|
---|
100 | .long CONFIG_BFB_WIDTH
|
---|
101 | .long CONFIG_BFB_HEIGHT
|
---|
102 | .long CONFIG_BFB_BPP
|
---|
103 | tag_framebuffer_end:
|
---|
104 | #endif
|
---|
105 |
|
---|
106 | /* Module alignment tag */
|
---|
107 | .align 8
|
---|
108 | tag_module_align_start:
|
---|
109 | .word MULTIBOOT2_TAG_MODULE_ALIGN
|
---|
110 | .word MULTIBOOT2_FLAGS_REQUIRED
|
---|
111 | .long tag_module_align_end - tag_module_align_start
|
---|
112 | .long 0
|
---|
113 | tag_module_align_end:
|
---|
114 |
|
---|
115 | /* Tag terminator */
|
---|
116 | .align 8
|
---|
117 | tag_terminator_start:
|
---|
118 | .word MULTIBOOT2_TAG_TERMINATOR
|
---|
119 | .word MULTIBOOT2_FLAGS_REQUIRED
|
---|
120 | .long tag_terminator_end - tag_terminator_start
|
---|
121 | tag_terminator_end:
|
---|
122 | multiboot2_header_end:
|
---|
123 |
|
---|
124 | SYMBOL(multiboot2_image_start)
|
---|
125 | cli
|
---|
126 | cld
|
---|
127 |
|
---|
128 | /* Initialize stack pointer */
|
---|
129 | movl $START_STACK, %esp
|
---|
130 |
|
---|
131 | /*
|
---|
132 | * Initialize Global Descriptor Table and
|
---|
133 | * Interrupt Descriptor Table registers
|
---|
134 | */
|
---|
135 | lgdtl bootstrap_gdtr
|
---|
136 | lidtl bootstrap_idtr
|
---|
137 |
|
---|
138 | /* Kernel data + stack */
|
---|
139 | movw $GDT_SELECTOR(KDATA_DES), %cx
|
---|
140 | movw %cx, %es
|
---|
141 | movw %cx, %ds
|
---|
142 | movw %cx, %ss
|
---|
143 |
|
---|
144 | /*
|
---|
145 | * Simics seems to remove hidden part of GS on entering user mode
|
---|
146 | * when _visible_ part of GS does not point to user-mode segment.
|
---|
147 | */
|
---|
148 | movw $GDT_SELECTOR(UDATA_DES), %cx
|
---|
149 | movw %cx, %fs
|
---|
150 | movw %cx, %gs
|
---|
151 |
|
---|
152 | jmpl $GDT_SELECTOR(KTEXT32_DES), $multiboot2_meeting_point
|
---|
153 | multiboot2_meeting_point:
|
---|
154 |
|
---|
155 | /*
|
---|
156 | * Protected 32-bit. We want to reuse the code-seg descriptor,
|
---|
157 | * the Default operand size must not be 1 when entering long mode.
|
---|
158 | */
|
---|
159 |
|
---|
160 | /* Save multiboot arguments */
|
---|
161 | movl %eax, multiboot_eax
|
---|
162 | movl %ebx, multiboot_ebx
|
---|
163 |
|
---|
164 | movl $(INTEL_CPUID_EXTENDED), %eax
|
---|
165 | cpuid
|
---|
166 | cmp $(INTEL_CPUID_EXTENDED), %eax
|
---|
167 | ja extended_cpuid_supported
|
---|
168 |
|
---|
169 | jmp pm_error_halt
|
---|
170 |
|
---|
171 | extended_cpuid_supported:
|
---|
172 |
|
---|
173 | movl $(AMD_CPUID_EXTENDED), %eax
|
---|
174 | cpuid
|
---|
175 | bt $(AMD_EXT_LONG_MODE), %edx
|
---|
176 | jc long_mode_supported
|
---|
177 |
|
---|
178 | jmp pm_error_halt
|
---|
179 |
|
---|
180 | long_mode_supported:
|
---|
181 |
|
---|
182 | bt $(AMD_EXT_NOEXECUTE), %edx
|
---|
183 | jc noexecute_supported
|
---|
184 |
|
---|
185 | jmp pm_error_halt
|
---|
186 |
|
---|
187 | noexecute_supported:
|
---|
188 |
|
---|
189 | movl $(INTEL_CPUID_STANDARD), %eax
|
---|
190 | cpuid
|
---|
191 | bt $(INTEL_FXSAVE), %edx
|
---|
192 | jc fx_supported
|
---|
193 |
|
---|
194 | jmp pm_error_halt
|
---|
195 |
|
---|
196 | fx_supported:
|
---|
197 |
|
---|
198 | bt $(INTEL_SSE2), %edx
|
---|
199 | jc sse2_supported
|
---|
200 |
|
---|
201 | jmp pm_error_halt
|
---|
202 |
|
---|
203 | sse2_supported:
|
---|
204 |
|
---|
205 | /*
|
---|
206 | * Enable 64-bit page translation entries - CR4.PAE = 1.
|
---|
207 | * Paging is not enabled until after long mode is enabled.
|
---|
208 | */
|
---|
209 |
|
---|
210 | movl %cr4, %eax
|
---|
211 | btsl $5, %eax
|
---|
212 | movl %eax, %cr4
|
---|
213 |
|
---|
214 | /* Set up paging tables */
|
---|
215 | leal ptl_0, %eax
|
---|
216 | movl %eax, %cr3
|
---|
217 |
|
---|
218 | /* Enable long mode */
|
---|
219 | movl $EFER_MSR_NUM, %ecx
|
---|
220 | rdmsr /* read EFER */
|
---|
221 | btsl $AMD_LME_FLAG, %eax /* set LME = 1 */
|
---|
222 | wrmsr
|
---|
223 |
|
---|
224 | /* Enable paging to activate long mode (set CR0.PG = 1) */
|
---|
225 | movl %cr0, %eax
|
---|
226 | btsl $31, %eax
|
---|
227 | movl %eax, %cr0
|
---|
228 |
|
---|
229 | /* At this point we are in compatibility mode */
|
---|
230 | jmpl $GDT_SELECTOR(KTEXT_DES), $start64
|
---|
231 |
|
---|
232 | pm_error_halt:
|
---|
233 | cli
|
---|
234 | hlt1:
|
---|
235 | hlt
|
---|
236 | jmp hlt1
|
---|
237 |
|
---|
238 | .code64
|
---|
239 |
|
---|
240 | start64:
|
---|
241 |
|
---|
242 | /*
|
---|
243 | * Long mode.
|
---|
244 | */
|
---|
245 |
|
---|
246 | movq $(PA2KA(START_STACK)), %rsp
|
---|
247 |
|
---|
248 | /* Create the first stack frame */
|
---|
249 | pushq $0
|
---|
250 | movq %rsp, %rbp
|
---|
251 |
|
---|
252 | /* Call arch_pre_main(multiboot_eax, multiboot_ebx) */
|
---|
253 | movl multiboot_eax, %edi
|
---|
254 | movl multiboot_ebx, %esi
|
---|
255 | callq arch_pre_main
|
---|
256 |
|
---|
257 | /* Call main_bsp() */
|
---|
258 | callq main_bsp
|
---|
259 |
|
---|
260 | /* Not reached */
|
---|
261 | cli
|
---|
262 | hlt0:
|
---|
263 | hlt
|
---|
264 | jmp hlt0
|
---|