| 1 | #
|
|---|
| 2 | # Copyright (C) 2001-2004 Jakub Jermar
|
|---|
| 3 | # Copyright (C) 2005-2006 Martin Decky
|
|---|
| 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 |
|
|---|
| 30 | #include <arch/boot/boot.h>
|
|---|
| 31 | #include <arch/boot/memmap.h>
|
|---|
| 32 | #include <arch/mm/page.h>
|
|---|
| 33 | #include <arch/pm.h>
|
|---|
| 34 |
|
|---|
| 35 | #define START_STACK (BOOT_OFFSET - BOOT_STACK_SIZE)
|
|---|
| 36 |
|
|---|
| 37 | .section K_TEXT_START, "ax"
|
|---|
| 38 |
|
|---|
| 39 | KTEXT=8
|
|---|
| 40 | KDATA=16
|
|---|
| 41 |
|
|---|
| 42 | .code32
|
|---|
| 43 | .align 4
|
|---|
| 44 | .global multiboot_image_start
|
|---|
| 45 | multiboot_header:
|
|---|
| 46 | .long MULTIBOOT_HEADER_MAGIC
|
|---|
| 47 | .long MULTIBOOT_HEADER_FLAGS
|
|---|
| 48 | .long -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS) # checksum
|
|---|
| 49 | .long multiboot_header
|
|---|
| 50 | .long unmapped_ktext_start
|
|---|
| 51 | .long 0
|
|---|
| 52 | .long 0
|
|---|
| 53 | .long multiboot_image_start
|
|---|
| 54 |
|
|---|
| 55 | multiboot_image_start:
|
|---|
| 56 | movl $START_STACK, %esp # initialize stack pointer
|
|---|
| 57 | lgdt KA2PA(bootstrap_gdtr) # initialize Global Descriptor Table register
|
|---|
| 58 |
|
|---|
| 59 | movw $KDATA, %cx
|
|---|
| 60 | movw %cx, %es
|
|---|
| 61 | movw %cx, %gs
|
|---|
| 62 | movw %cx, %fs
|
|---|
| 63 | movw %cx, %ds # kernel data + stack
|
|---|
| 64 | movw %cx, %ss
|
|---|
| 65 |
|
|---|
| 66 | jmpl $KTEXT, $multiboot_meeting_point
|
|---|
| 67 | multiboot_meeting_point:
|
|---|
| 68 |
|
|---|
| 69 | pushl %ebx # save parameters from GRUB
|
|---|
| 70 | pushl %eax
|
|---|
| 71 |
|
|---|
| 72 | call map_kernel # map kernel and turn paging on
|
|---|
| 73 |
|
|---|
| 74 | popl %eax
|
|---|
| 75 | popl %ebx
|
|---|
| 76 | cmpl $MULTIBOOT_LOADER_MAGIC, %eax # compare GRUB signature
|
|---|
| 77 | je valid_boot
|
|---|
| 78 |
|
|---|
| 79 | xorl %ecx, %ecx # no memory size or map available
|
|---|
| 80 | movl %ecx, e801memorysize
|
|---|
| 81 | movl %ecx, e820counter
|
|---|
| 82 |
|
|---|
| 83 | jmp invalid_boot
|
|---|
| 84 |
|
|---|
| 85 | valid_boot:
|
|---|
| 86 |
|
|---|
| 87 | movl (%ebx), %eax # ebx = physical address of struct multiboot_info
|
|---|
| 88 |
|
|---|
| 89 | bt $0, %eax # mbi->flags[0] (mem_lower, mem_upper valid)
|
|---|
| 90 | jc mem_valid
|
|---|
| 91 |
|
|---|
| 92 | xorl %ecx, %ecx
|
|---|
| 93 | jmp mem_invalid
|
|---|
| 94 |
|
|---|
| 95 | mem_valid:
|
|---|
| 96 | movl 4(%ebx), %ecx # mbi->mem_lower
|
|---|
| 97 | addl 8(%ebx), %ecx # mbi->mem_upper
|
|---|
| 98 |
|
|---|
| 99 | mem_invalid:
|
|---|
| 100 | movl %ecx, e801memorysize
|
|---|
| 101 |
|
|---|
| 102 | bt $3, %eax # mbi->flags[3] (mods_count, mods_addr valid)
|
|---|
| 103 | jc mods_valid
|
|---|
| 104 |
|
|---|
| 105 | xorl %ecx, %ecx
|
|---|
| 106 | movl %ecx, init
|
|---|
| 107 | jmp mods_end
|
|---|
| 108 |
|
|---|
| 109 | mods_valid:
|
|---|
| 110 |
|
|---|
| 111 | movl 20(%ebx), %ecx # mbi->mods_count
|
|---|
| 112 | movl %ecx, init
|
|---|
| 113 |
|
|---|
| 114 | cmpl $0, %ecx
|
|---|
| 115 | je mods_end
|
|---|
| 116 |
|
|---|
| 117 | movl 24(%ebx), %esi # mbi->mods_addr
|
|---|
| 118 | movl $init, %edi
|
|---|
| 119 |
|
|---|
| 120 | mods_loop:
|
|---|
| 121 |
|
|---|
| 122 | movl 0(%esi), %edx # mods->mod_start
|
|---|
| 123 | addl $0x80000000, %edx
|
|---|
| 124 | movl %edx, 4(%edi)
|
|---|
| 125 |
|
|---|
| 126 | movl 4(%esi), %edx
|
|---|
| 127 | subl 0(%esi), %edx # mods->mod_end - mods->mod_start
|
|---|
| 128 | movl %edx, 8(%edi)
|
|---|
| 129 |
|
|---|
| 130 | addl $16, %esi
|
|---|
| 131 | addl $8 , %edi
|
|---|
| 132 |
|
|---|
| 133 | loop mods_loop
|
|---|
| 134 |
|
|---|
| 135 | mods_end:
|
|---|
| 136 |
|
|---|
| 137 | bt $6, %eax # mbi->flags[6] (mmap_length, mmap_addr valid)
|
|---|
| 138 | jc mmap_valid
|
|---|
| 139 |
|
|---|
| 140 | xorl %edx, %edx
|
|---|
| 141 | jmp mmap_invalid
|
|---|
| 142 |
|
|---|
| 143 | mmap_valid:
|
|---|
| 144 | movl 44(%ebx), %ecx # mbi->mmap_length
|
|---|
| 145 | movl 48(%ebx), %esi # mbi->mmap_addr
|
|---|
| 146 | movl $e820table, %edi
|
|---|
| 147 | xorl %edx, %edx
|
|---|
| 148 |
|
|---|
| 149 | mmap_loop:
|
|---|
| 150 | cmpl $0, %ecx
|
|---|
| 151 | jle mmap_end
|
|---|
| 152 |
|
|---|
| 153 | movl 4(%esi), %eax # mmap->base_addr_low
|
|---|
| 154 | movl %eax, (%edi)
|
|---|
| 155 |
|
|---|
| 156 | movl 8(%esi), %eax # mmap->base_addr_high
|
|---|
| 157 | movl %eax, 4(%edi)
|
|---|
| 158 |
|
|---|
| 159 | movl 12(%esi), %eax # mmap->length_low
|
|---|
| 160 | movl %eax, 8(%edi)
|
|---|
| 161 |
|
|---|
| 162 | movl 16(%esi), %eax # mmap->length_high
|
|---|
| 163 | movl %eax, 12(%edi)
|
|---|
| 164 |
|
|---|
| 165 | movl 20(%esi), %eax # mmap->type
|
|---|
| 166 | movl %eax, 16(%edi)
|
|---|
| 167 |
|
|---|
| 168 | movl (%esi), %eax # mmap->size
|
|---|
| 169 | addl $0x4, %eax
|
|---|
| 170 | addl %eax, %esi
|
|---|
| 171 | subl %eax, %ecx
|
|---|
| 172 | addl $MEMMAP_E820_RECORD_SIZE, %edi
|
|---|
| 173 | incl %edx
|
|---|
| 174 | jmp mmap_loop
|
|---|
| 175 |
|
|---|
| 176 | mmap_end:
|
|---|
| 177 |
|
|---|
| 178 | mmap_invalid:
|
|---|
| 179 | movl %edx, e820counter
|
|---|
| 180 |
|
|---|
| 181 | invalid_boot:
|
|---|
| 182 |
|
|---|
| 183 | #ifdef CONFIG_SMP
|
|---|
| 184 |
|
|---|
| 185 | # copy AP bootstrap routines below 1 MB
|
|---|
| 186 |
|
|---|
| 187 | movl $BOOT_OFFSET, %esi
|
|---|
| 188 | movl $AP_BOOT_OFFSET, %edi
|
|---|
| 189 | movl $_hardcoded_unmapped_size, %ecx
|
|---|
| 190 | cld
|
|---|
| 191 | rep movsb
|
|---|
| 192 |
|
|---|
| 193 | #endif
|
|---|
| 194 |
|
|---|
| 195 | call main_bsp # never returns
|
|---|
| 196 |
|
|---|
| 197 | cli
|
|---|
| 198 | hlt
|
|---|
| 199 |
|
|---|
| 200 | .global map_kernel
|
|---|
| 201 | map_kernel:
|
|---|
| 202 | #
|
|---|
| 203 | # Here we setup mapping for both the unmapped and mapped sections of the kernel.
|
|---|
| 204 | # For simplicity, we map the entire 4G space.
|
|---|
| 205 | #
|
|---|
| 206 | movl %cr4, %ecx
|
|---|
| 207 | orl $(1<<4), %ecx
|
|---|
| 208 | movl %ecx, %cr4 # turn PSE on
|
|---|
| 209 |
|
|---|
| 210 | movl $(page_directory+0), %esi
|
|---|
| 211 | movl $(page_directory+2048), %edi
|
|---|
| 212 | xorl %ecx, %ecx
|
|---|
| 213 | xorl %ebx, %ebx
|
|---|
| 214 | 0:
|
|---|
| 215 | movl $((1<<7)|(1<<0)), %eax
|
|---|
| 216 | orl %ebx, %eax
|
|---|
| 217 | movl %eax, (%esi,%ecx,4) # mapping 0x00000000+%ecx*4M => 0x00000000+%ecx*4M
|
|---|
| 218 | movl %eax, (%edi,%ecx,4) # mapping 0x80000000+%ecx*4M => 0x00000000+%ecx*4M
|
|---|
| 219 | addl $(4*1024*1024), %ebx
|
|---|
| 220 |
|
|---|
| 221 | incl %ecx
|
|---|
| 222 | cmpl $512, %ecx
|
|---|
| 223 | jl 0b
|
|---|
| 224 |
|
|---|
| 225 | movl %esi, %cr3
|
|---|
| 226 |
|
|---|
| 227 | # turn paging on
|
|---|
| 228 | movl %cr0, %ebx
|
|---|
| 229 | orl $(1<<31), %ebx
|
|---|
| 230 | movl %ebx, %cr0
|
|---|
| 231 | ret
|
|---|
| 232 |
|
|---|
| 233 |
|
|---|
| 234 | .section K_DATA_START, "aw", @progbits
|
|---|
| 235 |
|
|---|
| 236 | .align 4096
|
|---|
| 237 | page_directory:
|
|---|
| 238 | .space 4096, 0
|
|---|