2 # Copyright (c) 2001-2004 Jakub Jermar
3 # Copyright (c) 2005-2006 Martin Decky
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
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.
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.
30 #include <arch/boot/boot.h>
31 #include <arch/boot/memmap.h>
32 #include <arch/mm/page.h>
35 #define START_STACK (BOOT_OFFSET - BOOT_STACK_SIZE)
37 .section K_TEXT_START, "ax"
41 .global multiboot_image_start
43 .long MULTIBOOT_HEADER_MAGIC
44 .long MULTIBOOT_HEADER_FLAGS
45 .long -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS) # checksum
46 .long multiboot_header
47 .long unmapped_ktext_start
50 .long multiboot_image_start
52 multiboot_image_start:
53 movl $START_STACK, %esp # initialize stack pointer
54 lgdt KA2PA(bootstrap_gdtr) # initialize Global Descriptor Table register
56 movw $selector(KDATA_DES), %cx
60 movw %cx, %ds # kernel data + stack
63 jmpl $selector(KTEXT_DES), $multiboot_meeting_point
64 multiboot_meeting_point:
66 pushl %ebx # save parameters from GRUB
71 mov $VESA_INIT_SEGMENT << 4, %edi
72 mov $e_vesa_init - vesa_init, %ecx
76 mov $VESA_INIT_SEGMENT << 4, %edi
81 mov %esi, KA2PA(vesa_ph_addr)
82 mov %di, KA2PA(vesa_height)
84 mov %di, KA2PA(vesa_width)
85 mov %bx, KA2PA(vesa_scanline)
87 mov %bx, KA2PA(vesa_bpp)
90 call map_kernel # map kernel and turn paging on
94 cmpl $MULTIBOOT_LOADER_MAGIC, %eax # compare GRUB signature
97 xorl %ecx, %ecx # no memory size or map available
98 movl %ecx, e801memorysize
99 movl %ecx, e820counter
105 movl (%ebx), %eax # ebx = physical address of struct multiboot_info
107 bt $0, %eax # mbi->flags[0] (mem_lower, mem_upper valid)
114 movl 4(%ebx), %ecx # mbi->mem_lower
115 addl 8(%ebx), %ecx # mbi->mem_upper
118 movl %ecx, e801memorysize
120 bt $3, %eax # mbi->flags[3] (mods_count, mods_addr valid)
129 movl 20(%ebx), %ecx # mbi->mods_count
135 movl 24(%ebx), %esi # mbi->mods_addr
140 movl 0(%esi), %edx # mods->mod_start
141 addl $0x80000000, %edx
145 subl 0(%esi), %edx # mods->mod_end - mods->mod_start
155 bt $6, %eax # mbi->flags[6] (mmap_length, mmap_addr valid)
162 movl 44(%ebx), %ecx # mbi->mmap_length
163 movl 48(%ebx), %esi # mbi->mmap_addr
164 movl $e820table, %edi
171 movl 4(%esi), %eax # mmap->base_addr_low
174 movl 8(%esi), %eax # mmap->base_addr_high
177 movl 12(%esi), %eax # mmap->length_low
180 movl 16(%esi), %eax # mmap->length_high
183 movl 20(%esi), %eax # mmap->type
186 movl (%esi), %eax # mmap->size
190 addl $MEMMAP_E820_RECORD_SIZE, %edi
197 movl %edx, e820counter
203 # copy AP bootstrap routines below 1 MB
205 movl $BOOT_OFFSET, %esi
206 movl $AP_BOOT_OFFSET, %edi
207 movl $_hardcoded_unmapped_size, %ecx
213 call main_bsp # never returns
221 # Here we setup mapping for both the unmapped and mapped sections of the kernel.
222 # For simplicity, we map the entire 4G space.
226 movl %ecx, %cr4 # turn PSE on
228 movl $(page_directory+0), %esi
229 movl $(page_directory+2048), %edi
233 movl $((1<<7)|(1<<0)), %eax
235 movl %eax, (%esi,%ecx,4) # mapping 0x00000000+%ecx*4M => 0x00000000+%ecx*4M
236 movl %eax, (%edi,%ecx,4) # mapping 0x80000000+%ecx*4M => 0x00000000+%ecx*4M
237 addl $(4*1024*1024), %ebx
253 jmp $selector(VESA_INIT_DES), $vesa_init_real - vesa_init
262 jmp $VESA_INIT_SEGMENT, $vesa_init_real2 - vesa_init
266 mov $VESA_INIT_SEGMENT, %bx
275 movl $0x0000fffc, %esp
276 movl $0x0000fffc, %ebp
279 #define VESA_INFO_SIZE 1024
281 #define VESA_MODE_LIST_PTR_OFFSET 14
282 #define VESA_MODE_WIDTH_OFFSET 18
283 #define VESA_MODE_HEIGHT_OFFSET 20
284 #define VESA_MODE_BPP_OFFSET 25
285 #define VESA_MODE_SCANLINE_OFFSET 16
286 #define VESA_MODE_PHADDR_OFFSET 40
288 #define VESA_END_OF_MODES 0xffff
292 #define VESA_GET_INFO 0x4f00
293 #define VESA_GET_MODE_INFO 0x4f01
294 #define VESA_SET_MODE 0x4f02
296 #define CONFIG_VESA_BPP_a 255
298 #if CONFIG_VESA_BPP == 24
299 #undef CONFIG_VESA_BPP_a
300 #define CONFIG_VESA_BPP_a 32
303 mov $VESA_GET_INFO, %ax
304 mov $e_vesa_init - vesa_init, %di
312 mov 2 + VESA_MODE_LIST_PTR_OFFSET(%di), %si
314 mov VESA_MODE_LIST_PTR_OFFSET(%di), %si
316 add $VESA_INFO_SIZE, %di
320 cmp $VESA_END_OF_MODES, %cx
328 mov $VESA_GET_MODE_INFO, %ax
337 mov $CONFIG_VESA_WIDTH, %ax
338 cmp VESA_MODE_WIDTH_OFFSET(%di), %ax
341 mov $CONFIG_VESA_HEIGHT,%ax
342 cmp VESA_MODE_HEIGHT_OFFSET(%di), %ax
345 mov $CONFIG_VESA_BPP, %al
346 cmp VESA_MODE_BPP_OFFSET(%di), %al
349 mov $CONFIG_VESA_BPP_a, %al
350 cmp VESA_MODE_BPP_OFFSET(%di), %al
358 mov $VESA_SET_MODE, %ax
365 mov VESA_MODE_PHADDR_OFFSET(%di), %esi
366 mov VESA_MODE_WIDTH_OFFSET(%di), %ax
368 mov VESA_MODE_HEIGHT_OFFSET(%di), %ax
369 mov VESA_MODE_BPP_OFFSET(%di), %bl
372 mov VESA_MODE_SCANLINE_OFFSET(%di), %bx
384 ljmpl $selector(KTEXT_DES), $(vesa_init_protect - vesa_init + VESA_INIT_SEGMENT << 4)
386 0:# No prefered mode found
390 mov $VESA_GET_MODE_INFO, %ax
397 jz 2b # Force relative jump
402 mov $0xffffffff, %edi # EGA text mode used, because of problems with VESA
404 jz 8b # Force relative jump
411 movw $selector(KDATA_DES), %cx
415 movw %cx, %ds # kernel data + stack
418 jmpl $selector(KTEXT_DES), $vesa_meeting_point
424 .section K_DATA_START, "aw", @progbits