source: mainline/kernel/arch/amd64/src/boot/boot.S@ c19aa612

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c19aa612 was 64f6ef04, checked in by Martin Decky <martin@…>, 16 years ago

amd64: create identity mapping of the first 8 GB of physical memory (instead of just 4 GB) during the bootstrap process
remove the crude on-demand identity mapping creation which could lead to livelocks (unmapping the page pointing to 0 MB physical while trying to map the page pointing to 4096 MB physical)

(currently the implementation is in no way closer to the ideal solution of ticket #3, but at least it allows to debug non-trivial cases of physical memory sizes > 4 GB during the approach to solving #3)

  • Property mode set to 100644
File size: 8.6 KB
Line 
1#
2# Copyright (c) 2005 Ondrej Palkovsky
3# Copyright (c) 2006 Martin Decky
4# Copyright (c) 2008 Jakub Jermar
5# All rights reserved.
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions
9# are met:
10#
11# - Redistributions of source code must retain the above copyright
12# notice, this list of conditions and the following disclaimer.
13# - Redistributions in binary form must reproduce the above copyright
14# notice, this list of conditions and the following disclaimer in the
15# documentation and/or other materials provided with the distribution.
16# - The name of the author may not be used to endorse or promote products
17# derived from this software without specific prior written permission.
18#
19# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29#
30
31#include <arch/boot/boot.h>
32#include <arch/boot/memmap.h>
33#include <arch/mm/page.h>
34#include <arch/mm/ptl.h>
35#include <arch/pm.h>
36#include <arch/cpu.h>
37#include <arch/cpuid.h>
38
39#define START_STACK (BOOT_OFFSET - BOOT_STACK_SIZE)
40
41.section K_TEXT_START, "ax"
42
43.code32
44.align 4
45.global multiboot_image_start
46multiboot_header:
47 .long MULTIBOOT_HEADER_MAGIC
48 .long MULTIBOOT_HEADER_FLAGS
49 .long -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS) # checksum
50 .long multiboot_header
51 .long unmapped_ktext_start
52 .long 0
53 .long 0
54 .long multiboot_image_start
55
56multiboot_image_start:
57 cld
58 movl $START_STACK, %esp # initialize stack pointer
59 lgdtl bootstrap_gdtr # initialize Global Descriptor Table register
60
61 movw $gdtselector(KDATA_DES), %cx
62 movw %cx, %es
63 movw %cx, %ds # kernel data + stack
64 movw %cx, %ss
65
66 #
67 # Simics seems to remove hidden part of GS on entering user mode
68 # when _visible_ part of GS does not point to user-mode segment.
69 #
70
71 movw $gdtselector(UDATA_DES), %cx
72 movw %cx, %fs
73 movw %cx, %gs
74
75 jmpl $gdtselector(KTEXT32_DES), $multiboot_meeting_point
76 multiboot_meeting_point:
77
78 movl %eax, grub_eax # save parameters from GRUB
79 movl %ebx, grub_ebx
80
81 #
82 # Protected 32-bit. We want to reuse the code-seg descriptor,
83 # the Default operand size must not be 1 when entering long mode.
84 #
85
86 movl $(INTEL_CPUID_EXTENDED), %eax
87 cpuid
88 cmp $(INTEL_CPUID_EXTENDED), %eax
89 ja extended_cpuid_supported
90
91 movl $extended_cpuid_msg, %esi
92 jmp error_halt
93
94 extended_cpuid_supported:
95
96 movl $(AMD_CPUID_EXTENDED), %eax
97 cpuid
98 bt $(AMD_EXT_LONG_MODE), %edx
99 jc long_mode_supported
100
101 movl $long_mode_msg, %esi
102 jmp error_halt
103
104 long_mode_supported:
105
106 bt $(AMD_EXT_NOEXECUTE), %edx
107 jc noexecute_supported
108
109 movl $noexecute_msg, %esi
110 jmp error_halt
111
112 noexecute_supported:
113
114 movl $(INTEL_CPUID_STANDARD), %eax
115 cpuid
116 bt $(INTEL_FXSAVE), %edx
117 jc fx_supported
118
119 movl $fx_msg, %esi
120 jmp error_halt
121
122 fx_supported:
123
124 bt $(INTEL_SSE2), %edx
125 jc sse2_supported
126
127 movl $sse2_msg, %esi
128 jmp error_halt
129
130 sse2_supported:
131
132#include "vesa_prot.inc"
133
134 #
135 # Enable 64-bit page translation entries - CR4.PAE = 1.
136 # Paging is not enabled until after long mode is enabled.
137 #
138
139 movl %cr4, %eax
140 btsl $5, %eax
141 movl %eax, %cr4
142
143 # set up paging tables
144
145 leal ptl_0, %eax
146 movl %eax, %cr3
147
148 # enable long mode
149
150 movl $EFER_MSR_NUM, %ecx # EFER MSR number
151 rdmsr # read EFER
152 btsl $AMD_LME_FLAG, %eax # set LME = 1
153 wrmsr # write EFER
154
155 # enable paging to activate long mode (set CR0.PG = 1)
156
157 movl %cr0, %eax
158 btsl $31, %eax
159 movl %eax, %cr0
160
161 # at this point we are in compatibility mode
162
163 jmpl $gdtselector(KTEXT_DES), $start64
164
165.code64
166start64:
167 movq $(PA2KA(START_STACK)), %rsp
168
169 # call arch_pre_main(grub_eax, grub_ebx)
170 xorq %rdi, %rdi
171 movl grub_eax, %edi
172 xorq %rsi, %rsi
173 movl grub_ebx, %esi
174
175 movabsq $arch_pre_main, %rax
176 callq *%rax
177
178 # create the first stack frame
179 pushq $0
180 movq %rsp, %rbp
181
182 movabsq $main_bsp, %rax
183 call *%rax
184
185 # not reached
186
187 cli
188 hlt0:
189 hlt
190 jmp hlt0
191
192# Print string from %esi to EGA display (in red) and halt
193error_halt:
194 movl $0xb8000, %edi # base of EGA text mode memory
195 xorl %eax, %eax
196
197 movw $0x3d4, %dx # read bits 8 - 15 of the cursor address
198 movb $0xe, %al
199 outb %al, %dx
200
201 movw $0x3d5, %dx
202 inb %dx, %al
203 shl $8, %ax
204
205 movw $0x3d4, %dx # read bits 0 - 7 of the cursor address
206 movb $0xf, %al
207 outb %al, %dx
208
209 movw $0x3d5, %dx
210 inb %dx, %al
211
212 cmp $1920, %ax
213 jbe cursor_ok
214
215 movw $1920, %ax # sanity check for the cursor on the last line
216
217 cursor_ok:
218
219 movw %ax, %bx
220 shl $1, %eax
221 addl %eax, %edi
222
223 movw $0x0c00, %ax # black background, light red foreground
224
225 ploop:
226 lodsb
227 cmp $0, %al
228 je ploop_end
229 stosw
230 inc %bx
231 jmp ploop
232 ploop_end:
233
234 movw $0x3d4, %dx # write bits 8 - 15 of the cursor address
235 movb $0xe, %al
236 outb %al, %dx
237
238 movw $0x3d5, %dx
239 movb %bh, %al
240 outb %al, %dx
241
242 movw $0x3d4, %dx # write bits 0 - 7 of the cursor address
243 movb $0xf, %al
244 outb %al, %dx
245
246 movw $0x3d5, %dx
247 movb %bl, %al
248 outb %al, %dx
249
250 cli
251 hlt1:
252 hlt
253 jmp hlt1
254
255#include "vesa_real.inc"
256
257.section K_INI_PTLS, "aw", @progbits
258
259#
260# Macro for generating initial page table contents.
261# @param cnt Number of entries to generate. Must be multiple of 8.
262# @param g Number of GB that will be added to the mapping.
263#
264.macro ptl2gen cnt g
265 .if \cnt
266 ptl2gen "\cnt - 8" \g
267 .quad ((\cnt - 8) * 0x200000) + (\g * 1024 * 1024 * 1024) | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
268 .quad ((\cnt - 7) * 0x200000) + (\g * 1024 * 1024 * 1024) | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
269 .quad ((\cnt - 6) * 0x200000) + (\g * 1024 * 1024 * 1024) | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
270 .quad ((\cnt - 5) * 0x200000) + (\g * 1024 * 1024 * 1024) | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
271 .quad ((\cnt - 4) * 0x200000) + (\g * 1024 * 1024 * 1024) | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
272 .quad ((\cnt - 3) * 0x200000) + (\g * 1024 * 1024 * 1024) | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
273 .quad ((\cnt - 2) * 0x200000) + (\g * 1024 * 1024 * 1024) | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
274 .quad ((\cnt - 1) * 0x200000) + (\g * 1024 * 1024 * 1024) | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
275 .endif
276.endm
277
278# Page table for pages in the 1st gigabyte.
279.align 4096
280ptl_2_0g:
281 ptl2gen 512 0
282
283# Page table for pages in the 2nd gigabyte.
284.align 4096
285ptl_2_1g:
286 ptl2gen 512 1
287
288# Page table for pages in the 3rd gigabyte.
289.align 4096
290ptl_2_2g:
291 ptl2gen 512 2
292
293# Page table for pages in the 4th gigabyte.
294.align 4096
295ptl_2_3g:
296 ptl2gen 512 3
297
298# Page table for pages in the 5th gigabyte.
299.align 4096
300ptl_2_4g:
301 ptl2gen 512 3
302
303# Page table for pages in the 6th gigabyte.
304.align 4096
305ptl_2_5g:
306 ptl2gen 512 3
307
308# Page table for pages in the 7th gigabyte.
309.align 4096
310ptl_2_6g:
311 ptl2gen 512 3
312
313# Page table for pages in the 8th gigabyte.
314.align 4096
315ptl_2_7g:
316 ptl2gen 512 3
317
318.align 4096
319ptl_1:
320 # Identity mapping for [0; 8G)
321 .quad ptl_2_0g + (PTL_WRITABLE | PTL_PRESENT)
322 .quad ptl_2_1g + (PTL_WRITABLE | PTL_PRESENT)
323 .quad ptl_2_2g + (PTL_WRITABLE | PTL_PRESENT)
324 .quad ptl_2_3g + (PTL_WRITABLE | PTL_PRESENT)
325 .quad ptl_2_4g + (PTL_WRITABLE | PTL_PRESENT)
326 .quad ptl_2_5g + (PTL_WRITABLE | PTL_PRESENT)
327 .quad ptl_2_6g + (PTL_WRITABLE | PTL_PRESENT)
328 .quad ptl_2_7g + (PTL_WRITABLE | PTL_PRESENT)
329 .fill 504, 8, 0
330
331.align 4096
332.global ptl_0
333ptl_0:
334 .quad ptl_1 + (PTL_WRITABLE | PTL_PRESENT)
335 .fill 255, 8, 0
336 .quad ptl_1 + (PTL_WRITABLE | PTL_PRESENT)
337 .fill 255, 8, 0
338
339.section K_DATA_START, "aw", @progbits
340
341.global bootstrap_gdtr
342bootstrap_gdtr:
343 .word gdtselector(GDT_ITEMS)
344 .long KA2PA(gdt)
345
346grub_eax:
347 .long 0
348
349grub_ebx:
350 .long 0
351
352extended_cpuid_msg:
353 .asciz "Error: Extended CPUID not supported -- CPU is not 64-bit. System halted."
354long_mode_msg:
355 .asciz "Error: 64-bit long mode not supported. System halted."
356noexecute_msg:
357 .asciz "Error: No-execute pages not supported. System halted."
358fx_msg:
359 .asciz "Error: FXSAVE/FXRESTORE instructions not supported. System halted."
360sse2_msg:
361 .asciz "Error: SSE2 instructions not supported. System halted."
Note: See TracBrowser for help on using the repository browser.