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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since df4ed85 was df4ed85, checked in by Jakub Jermar <jakub@…>, 19 years ago

© versus ©

  • Property mode set to 100644
File size: 12.0 KB
RevLine 
[1141c1a]1#
[df4ed85]2# Copyright (c) 2005 Ondrej Palkovsky
3# Copyright (c) 2006 Martin Decky
[1141c1a]4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9#
10# - Redistributions of source code must retain the above copyright
11# notice, this list of conditions and the following disclaimer.
12# - Redistributions in binary form must reproduce the above copyright
13# notice, this list of conditions and the following disclaimer in the
14# documentation and/or other materials provided with the distribution.
15# - The name of the author may not be used to endorse or promote products
16# derived from this software without specific prior written permission.
17#
18# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28#
29
[46d26ee]30#include <arch/boot/boot.h>
31#include <arch/boot/memmap.h>
[8fc0d455]32#include <arch/mm/page.h>
[6f878b7]33#include <arch/mm/ptl.h>
[8fc0d455]34#include <arch/pm.h>
[89344d85]35#include <arch/cpu.h>
[c4b3e3e]36#include <arch/cpuid.h>
[1141c1a]37
[8725fb4]38#define START_STACK (BOOT_OFFSET - BOOT_STACK_SIZE)
[c4b3e3e]39
[874e312a]40.section K_TEXT_START, "ax"
[46d26ee]41
[8ccec3c1]42.code32
43.align 4
[46d26ee]44.global multiboot_image_start
[8ccec3c1]45multiboot_header:
46 .long MULTIBOOT_HEADER_MAGIC
47 .long MULTIBOOT_HEADER_FLAGS
48 .long -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS) # checksum
[8725fb4]49 .long multiboot_header
50 .long unmapped_ktext_start
[8ccec3c1]51 .long 0
52 .long 0
[8725fb4]53 .long multiboot_image_start
[8ccec3c1]54
55multiboot_image_start:
[46d26ee]56 movl $START_STACK, %esp # initialize stack pointer
57 lgdt bootstrap_gdtr # initialize Global Descriptor Table register
[1a67595]58
59 movw $gdtselector(KDATA_DES), %cx
60 movw %cx, %es
61 movw %cx, %ds # kernel data + stack
62 movw %cx, %ss
[65640fef]63 # Simics seems to remove hidden part of GS on entering user mode
64 # when _visible_ part of GS does not point to user-mode segment
65 movw $gdtselector(UDATA_DES), %cx
66 movw %cx, %fs
67 movw %cx, %gs
[1a67595]68
[46d26ee]69 jmpl $gdtselector(KTEXT32_DES), $multiboot_meeting_point
[1a67595]70 multiboot_meeting_point:
71
[46d26ee]72 movl %eax, grub_eax # save parameters from GRUB
73 movl %ebx, grub_ebx
74
[de07bcf]75#ifdef CONFIG_FB
[8778271]76 mov $vesa_init, %esi;
77 mov $VESA_INIT_SEGMENT << 4, %edi
78 mov $e_vesa_init - vesa_init, %ecx
79 cld
80 rep movsb
[de07bcf]81
[8778271]82 mov $VESA_INIT_SEGMENT << 4, %edi
83 jmpl *%edi
84
85 vesa_meeting_point:
86
87 mov %esi, KA2PA(vesa_ph_addr)
88 mov %di, KA2PA(vesa_height)
89 shr $16, %edi
90 mov %di, KA2PA(vesa_width)
91 mov %bx, KA2PA(vesa_scanline)
92 shr $16, %ebx
93 mov %bx, KA2PA(vesa_bpp)
[de07bcf]94#endif
95
[8725fb4]96 # Protected 32-bit. We want to reuse the code-seg descriptor,
97 # the Default operand size must not be 1 when entering long mode
98
[42edee68]99 movl $0x80000000, %eax
100 cpuid
101 cmp $0x80000000, %eax # any function > 80000000h?
102 jbe long_mode_unsupported
103 movl $(AMD_CPUID_EXTENDED), %eax # Extended function code 80000001
104 cpuid
105 bt $29, %edx # Test if long mode is supported.
106 jc long_mode_supported
107
108 long_mode_unsupported:
109 cli
110 hlt
111
112 long_mode_supported:
113
[6f878b7]114 # Enable 64-bit page transaltion entries - CR4.PAE = 1.
115 # Paging is not enabled until after long mode is enabled
[8725fb4]116
[6f878b7]117 movl %cr4, %eax
118 btsl $5, %eax
119 movl %eax, %cr4
120
121 # Set up paging tables
[8725fb4]122
[6f878b7]123 leal ptl_0, %eax
124 movl %eax, %cr3
[c4b3e3e]125
[6f878b7]126 # Enable long mode
[8725fb4]127
128 movl $EFER_MSR_NUM, %ecx # EFER MSR number
129 rdmsr # Read EFER
130 btsl $AMD_LME_FLAG, %eax # Set LME=1
131 wrmsr # Write EFER
[c245372b]132
[6f878b7]133 # Enable paging to activate long mode (set CR0.PG=1)
[8725fb4]134
[6f878b7]135 movl %cr0, %eax
136 btsl $31, %eax
137 movl %eax, %cr0
[c245372b]138
[6f878b7]139 # At this point we are in compatibility mode
[8725fb4]140
[b9e97fb]141 jmpl $gdtselector(KTEXT_DES), $start64
[c245372b]142
[6f878b7]143.code64
144start64:
[c4b3e3e]145 movq $(PA2KA(START_STACK)), %rsp
[46d26ee]146 movl grub_eax, %eax
147 movl grub_ebx, %ebx
148
149 cmpl $MULTIBOOT_LOADER_MAGIC, %eax # compare GRUB signature
150 je valid_boot
151
152 xorl %ecx, %ecx # no memory size or map available
153 movl %ecx, e801memorysize
154 movl %ecx, e820counter
155
156 jmp invalid_boot
157
158 valid_boot:
159
160 movl (%ebx), %eax # ebx = physical address of struct multiboot_info
161
162 bt $0, %eax # mbi->flags[0] (mem_lower, mem_upper valid)
163 jc mem_valid
164
165 xorl %ecx, %ecx
166 jmp mem_invalid
167
168 mem_valid:
169 movl 4(%ebx), %ecx # mbi->mem_lower
170 addl 8(%ebx), %ecx # mbi->mem_upper
171
172 mem_invalid:
173 movl %ecx, e801memorysize
174
[d764ddc]175 bt $3, %eax # mbi->flags[3] (mods_count, mods_addr valid)
[46d26ee]176 jc mods_valid
177
[6eb103c]178 xorq %rcx, %rcx
179 movq %rcx, init
[d764ddc]180 jmp mods_end
[46d26ee]181
182 mods_valid:
[d764ddc]183
184 xorq %rcx, %rcx
[46d26ee]185 movl 20(%ebx), %ecx # mbi->mods_count
[6eb103c]186 movq %rcx, init
[d764ddc]187
[46d26ee]188 cmpl $0, %ecx
[d764ddc]189 je mods_end
[46d26ee]190
191 movl 24(%ebx), %esi # mbi->mods_addr
[d764ddc]192 movq $init, %rdi
[46d26ee]193
[d764ddc]194 mods_loop:
195
196 xorq %rdx, %rdx
197 movl 0(%esi), %edx # mods->mod_start
[93165be]198 movq $0xffff800000000000, %r10
199 addq %r10, %rdx
[6eb103c]200 movq %rdx, 8(%rdi)
[d764ddc]201
202 xorq %rdx, %rdx
203 movl 4(%esi), %edx
204 subl 0(%esi), %edx # mods->mod_end - mods->mod_start
[6eb103c]205 movq %rdx, 16(%rdi)
[d764ddc]206
207 addl $16, %esi
208 addq $16, %rdi
209
210 loop mods_loop
211
212 mods_end:
[46d26ee]213
214 bt $6, %eax # mbi->flags[6] (mmap_length, mmap_addr valid)
215 jc mmap_valid
216
217 xorl %edx, %edx
218 jmp mmap_invalid
219
220 mmap_valid:
221 movl 44(%ebx), %ecx # mbi->mmap_length
222 movl 48(%ebx), %esi # mbi->mmap_addr
223 movq $e820table, %rdi
224 xorl %edx, %edx
225
226 mmap_loop:
227 cmpl $0, %ecx
228 jle mmap_end
229
230 movl 4(%esi), %eax # mmap->base_addr_low
231 movl %eax, (%rdi)
232
233 movl 8(%esi), %eax # mmap->base_addr_high
234 movl %eax, 4(%rdi)
235
236 movl 12(%esi), %eax # mmap->length_low
237 movl %eax, 8(%rdi)
238
239 movl 16(%esi), %eax # mmap->length_high
240 movl %eax, 12(%rdi)
241
242 movl 20(%esi), %eax # mmap->type
243 movl %eax, 16(%rdi)
244
245 movl (%esi), %eax # mmap->size
246 addl $0x4, %eax
247 addl %eax, %esi
248 subl %eax, %ecx
249 addq $MEMMAP_E820_RECORD_SIZE, %rdi
250 incl %edx
251 jmp mmap_loop
252
253 mmap_end:
254
255 mmap_invalid:
256 movl %edx, e820counter
257
258 invalid_boot:
259
[42edee68]260#ifdef CONFIG_SMP
261
262 # copy AP bootstrap routines below 1 MB
263
264 movq $BOOT_OFFSET, %rsi
265 movq $AP_BOOT_OFFSET, %rdi
266 movq $_hardcoded_unmapped_size, %rcx
267 cld
268 rep movsb
269
270#endif
271
[6f878b7]272 call main_bsp # never returns
[8725fb4]273
274 cli
275 hlt
[de07bcf]276
277#ifdef CONFIG_FB
278.code32
279vesa_init:
[8778271]280 jmp $gdtselector(VESA_INIT_DES), $vesa_init_real - vesa_init
281
[de07bcf]282.code16
[8778271]283vesa_init_real:
284
285 mov %cr0, %eax
286 and $~1, %eax
287 mov %eax, %cr0
288
289 jmp $VESA_INIT_SEGMENT, $vesa_init_real2 - vesa_init
290
291vesa_init_real2:
292
293 mov $VESA_INIT_SEGMENT, %bx
294
295 mov %bx, %es
296 mov %bx, %fs
297 mov %bx, %gs
298 mov %bx, %ds
299 mov %bx, %ss
300
301 movl $0x0000fffc, %esp
302 movl $0x0000fffc, %ebp
[de07bcf]303
304#define VESA_INFO_SIZE 1024
305
[8778271]306#define VESA_MODE_LIST_PTR_OFFSET 14
[de07bcf]307#define VESA_MODE_WIDTH_OFFSET 18
308#define VESA_MODE_HEIGHT_OFFSET 20
309#define VESA_MODE_BPP_OFFSET 25
310#define VESA_MODE_SCANLINE_OFFSET 16
311#define VESA_MODE_PHADDR_OFFSET 40
312
[8778271]313#define VESA_END_OF_MODES 0xffff
[de07bcf]314
[8778271]315#define VESA_OK 0x4f
[de07bcf]316
[8778271]317#define VESA_GET_INFO 0x4f00
[de07bcf]318#define VESA_GET_MODE_INFO 0x4f01
319#define VESA_SET_MODE 0x4f02
320
321#define CONFIG_VESA_BPP_a 255
322
[8778271]323#if CONFIG_VESA_BPP == 24
[de07bcf]324#undef CONFIG_VESA_BPP_a
325#define CONFIG_VESA_BPP_a 32
326#endif
[8778271]327
328 mov $VESA_GET_INFO, %ax
329 mov $e_vesa_init - vesa_init, %di
330 push %di
331 int $0x10
332
333 pop %di
334 cmp $VESA_OK, %al
335 jnz 0f
336
337 mov 2 + VESA_MODE_LIST_PTR_OFFSET(%di), %si
338 mov %si, %gs
339 mov VESA_MODE_LIST_PTR_OFFSET(%di), %si
340
341 add $VESA_INFO_SIZE, %di
342
[de07bcf]3431:# Try next mode
[8778271]344 mov %gs:(%si), %cx
345 cmp $VESA_END_OF_MODES, %cx
346 jz 0f
347
348 inc %si
349 inc %si
350 push %cx
351 push %di
352 push %si
353 mov $VESA_GET_MODE_INFO, %ax
354 int $0x10
355
356 pop %si
357 pop %di
358 pop %cx
359 cmp $VESA_OK, %al
360 jnz 0f
361
362 mov $CONFIG_VESA_WIDTH, %ax
363 cmp VESA_MODE_WIDTH_OFFSET(%di), %ax
364 jnz 1b
365
366 mov $CONFIG_VESA_HEIGHT, %ax
367 cmp VESA_MODE_HEIGHT_OFFSET(%di), %ax
368 jnz 1b
369
370 mov $CONFIG_VESA_BPP, %al
371 cmp VESA_MODE_BPP_OFFSET(%di), %al
372 jz 2f
373
374 mov $CONFIG_VESA_BPP_a, %al
375 cmp VESA_MODE_BPP_OFFSET(%di), %al
376 jnz 1b
377
[de07bcf]3782:
379
[8778271]380 mov %cx, %bx
381 or $0xc000, %bx
382 push %di
383 mov $VESA_SET_MODE, %ax
384 int $0x10
[de07bcf]385
[8778271]386 pop %di
387 cmp $VESA_OK, %al
388 jnz 0f
389
390 mov VESA_MODE_PHADDR_OFFSET(%di), %esi
391 mov VESA_MODE_WIDTH_OFFSET(%di), %ax
392 shl $16, %eax
393 mov VESA_MODE_HEIGHT_OFFSET(%di), %ax
394 mov VESA_MODE_BPP_OFFSET(%di), %bl
395 xor %bh, %bh
396 shl $16, %ebx
397 mov VESA_MODE_SCANLINE_OFFSET(%di), %bx
398 mov %eax, %edi
399
4008:
401
402 mov %cr0, %eax
403 or $1, %eax
404 mov %eax, %cr0
405
406 jmp 9f
[de07bcf]4079:
[8778271]408
409 ljmpl $gdtselector(KTEXT32_DES), $(vesa_init_protect - vesa_init + VESA_INIT_SEGMENT << 4)
410
4110:# No prefered mode found
412 mov $0x111, %cx
413 push %di
414 push %cx
415 mov $VESA_GET_MODE_INFO, %ax
416 int $0x10
417
418 pop %cx
419 pop %di
420 cmp $VESA_OK, %al
421 jnz 1f
422 jz 2b # Force relative jump
423
4241:
425 mov $0x0003, %ax
426 int $0x10
427 mov $0xffffffff, %edi # EGA text mode used, because of problems with VESA
428 xor %ax, %ax
429 jz 8b # Force relative jump
430
431
[de07bcf]432.code32
[8778271]433vesa_init_protect:
434 movw $gdtselector(KDATA_DES), %cx
435 movw %cx, %es
436 movw %cx, %ds # kernel data + stack
437 movw %cx, %ss
438 # Simics seems to remove hidden part of GS on entering user mode
439 # when _visible_ part of GS does not point to user-mode segment
440 movw $gdtselector(UDATA_DES), %cx
441 movw %cx, %fs
442 movw %cx, %gs
443
444 jmpl $gdtselector(KTEXT32_DES), $vesa_meeting_point
445
[de07bcf]446.align 4
447e_vesa_init:
448#endif
[b9e97fb]449
[874e312a]450.section K_DATA_START, "aw", @progbits
[1141c1a]451.align 4096
[b9e97fb]452
[a16bfd0]453# Identical mapping of first 64MB and the same of -2GB -> 0
[6f878b7]454.global ptl_2
455ptl_2:
456 .quad 0x0 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
457 .quad 0x200000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
458 .quad 0x400000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
459 .quad 0x600000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
460 .quad 0x800000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
461 .quad 0xa00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
462 .quad 0xc00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
463 .quad 0xe00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
[a16bfd0]464 .quad 0x1000000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
465 .quad 0x1200000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
466 .quad 0x1400000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
467 .quad 0x1600000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
468 .quad 0x1800000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
469 .quad 0x1a00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
470 .quad 0x1c00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
471 .quad 0x1e00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
472 .quad 0x2000000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
473 .quad 0x2200000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
474 .quad 0x2400000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
475 .quad 0x2600000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
476 .quad 0x2800000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
477 .quad 0x2a00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
478 .quad 0x2c00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
479 .quad 0x2e00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
480 .quad 0x3000000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
481 .quad 0x3200000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
482 .quad 0x3400000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
483 .quad 0x3600000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
484 .quad 0x3800000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
485 .quad 0x3a00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
486 .quad 0x3c00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
487 .quad 0x3e00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
[6f878b7]488
489.align 4096
490.global ptl_1
491ptl_1:
492 .quad ptl_2 + (PTL_WRITABLE | PTL_PRESENT)
493 .fill 509,8,0
494 .quad ptl_2 + (PTL_WRITABLE | PTL_PRESENT)
[ae9624e]495 .fill 1,8,0
[6f878b7]496
497.align 4096
498.global ptl_0
499ptl_0:
500 .quad ptl_1 + (PTL_WRITABLE | PTL_PRESENT)
[93165be]501 .fill 255,8,0
502 .quad ptl_1 + (PTL_WRITABLE | PTL_PRESENT)
503 .fill 254,8,0
[6f878b7]504 .quad ptl_1 + (PTL_WRITABLE | PTL_PRESENT)
505
[46d26ee]506.global bootstrap_gdtr
507bootstrap_gdtr:
[1a67595]508 .word gdtselector(GDT_ITEMS)
509 .long KA2PA(gdt)
[46d26ee]510
511grub_eax:
512 .long 0
513
514grub_ebx:
515 .long 0
Note: See TracBrowser for help on using the repository browser.