[f761f1eb] | 1 | #
|
---|
| 2 | # Copyright (C) 2001-2004 Jakub Jermar
|
---|
| 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 |
|
---|
[f9447155] | 29 | #include <arch/boot/boot.h>
|
---|
[aa72859] | 30 | #include <arch/boot/memmap.h>
|
---|
[8f2153b] | 31 | #include <arch/mm/page.h>
|
---|
| 32 | #include <arch/pm.h>
|
---|
[f9447155] | 33 |
|
---|
[874e312a] | 34 | .section K_TEXT_START, "ax"
|
---|
[f761f1eb] | 35 | .global kernel_image_start
|
---|
| 36 |
|
---|
[f6297e0] | 37 | KTEXT=8
|
---|
| 38 | KDATA=16
|
---|
| 39 |
|
---|
[f761f1eb] | 40 | .code16
|
---|
| 41 | #
|
---|
| 42 | # This is where we require any SPARTAN-kernel-compatible boot loader
|
---|
| 43 | # to pass control in real mode.
|
---|
| 44 | #
|
---|
| 45 | # Protected mode tables are statically initialised during compile
|
---|
| 46 | # time. So we can just load the respective table registers and
|
---|
| 47 | # switch to protected mode.
|
---|
| 48 | #
|
---|
| 49 | kernel_image_start:
|
---|
[5d721f0] | 50 | cli
|
---|
[5dce48b9] | 51 | xorw %ax, %ax
|
---|
| 52 | movw %ax, %ds
|
---|
[b1cf98c] | 53 | movw %ax, %es
|
---|
[f6297e0] | 54 | movw %ax, %ss # initialize stack segment register
|
---|
[b0edf3b2] | 55 | movl $BOOTSTRAP_OFFSET - 0x400, %esp # initialize stack pointer
|
---|
[229d5fc1] | 56 |
|
---|
| 57 | call memmap_arch_init
|
---|
| 58 |
|
---|
[b0edf3b2] | 59 | lgdt real_bootstrap_gdtr_boot # initialize Global Descriptor Table register
|
---|
[b52da8d7] | 60 |
|
---|
[5dce48b9] | 61 | movl %cr0, %eax
|
---|
| 62 | orl $0x1, %eax
|
---|
[f6297e0] | 63 | movl %eax, %cr0 # switch to protected mode
|
---|
[5dce48b9] | 64 |
|
---|
[f6297e0] | 65 | jmpl $KTEXT, $boot_image_start
|
---|
[5dce48b9] | 66 |
|
---|
[b0bf501] | 67 | .code32
|
---|
[f9447155] | 68 | .align 4
|
---|
| 69 | multiboot_header:
|
---|
| 70 | .long MULTIBOOT_HEADER_MAGIC
|
---|
| 71 | .long MULTIBOOT_HEADER_FLAGS
|
---|
| 72 | .long -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS) # checksum
|
---|
[5dce48b9] | 73 | .long multiboot_header + BOOT_OFFSET
|
---|
| 74 | .long unmapped_ktext_start + BOOT_OFFSET
|
---|
[f9447155] | 75 | .long 0
|
---|
| 76 | .long 0
|
---|
[5dce48b9] | 77 | .long multiboot_image_start + BOOT_OFFSET
|
---|
| 78 |
|
---|
| 79 | boot_image_start:
|
---|
[f6297e0] | 80 | movw $KDATA, %ax
|
---|
[5dce48b9] | 81 | movw %ax, %es
|
---|
| 82 | movw %ax, %gs
|
---|
| 83 | movw %ax, %fs
|
---|
[b0edf3b2] | 84 | movw %ax, %ds # kernel data + stack
|
---|
[5dce48b9] | 85 | movw %ax, %ss
|
---|
| 86 |
|
---|
[b1cf98c] | 87 | movb $0xd1, %al # enable A20 using i8042 controller
|
---|
[5dce48b9] | 88 | outb %al, $0x64
|
---|
| 89 | movb $0xdf, %al
|
---|
| 90 | outb %al, $0x60
|
---|
| 91 |
|
---|
[f6297e0] | 92 | movl $BOOTSTRAP_OFFSET, %esi
|
---|
| 93 | movl $BOOTSTRAP_OFFSET + BOOT_OFFSET, %edi
|
---|
[5dce48b9] | 94 | movl $_hardcoded_kernel_size, %ecx
|
---|
| 95 | cld
|
---|
| 96 | rep movsb
|
---|
| 97 |
|
---|
[b0edf3b2] | 98 | call map_kernel # map kernel and turn paging on
|
---|
[339e053] | 99 |
|
---|
[b0edf3b2] | 100 | call main_bsp # never returns
|
---|
[dd80fc6] | 101 |
|
---|
| 102 | cli
|
---|
| 103 | hlt
|
---|
[f9447155] | 104 |
|
---|
| 105 | multiboot_image_start:
|
---|
[b0edf3b2] | 106 | movl $BOOTSTRAP_OFFSET - 0x400, %esp # initialize stack pointer
|
---|
[f9447155] | 107 |
|
---|
[b0edf3b2] | 108 | lgdt protected_bootstrap_gdtr - 0x80000000 # initialize Global Descriptor Table register
|
---|
[b0bf501] | 109 |
|
---|
[5eb1379] | 110 | movw $KDATA, %cx
|
---|
| 111 | movw %cx, %es
|
---|
| 112 | movw %cx, %gs
|
---|
| 113 | movw %cx, %fs
|
---|
[b0edf3b2] | 114 | movw %cx, %ds # kernel data + stack
|
---|
[5eb1379] | 115 | movw %cx, %ss
|
---|
[5dce48b9] | 116 |
|
---|
[dd80fc6] | 117 | jmpl $KTEXT, $multiboot_meeting_point + BOOT_OFFSET
|
---|
[4533601] | 118 | multiboot_meeting_point:
|
---|
| 119 |
|
---|
[b0edf3b2] | 120 | pushl %ebx # save parameters from GRUB
|
---|
[5eb1379] | 121 | pushl %eax
|
---|
| 122 |
|
---|
[339e053] | 123 | movl $BOOTSTRAP_OFFSET + BOOT_OFFSET, %esi
|
---|
| 124 | movl $BOOTSTRAP_OFFSET, %edi
|
---|
| 125 | movl $_hardcoded_unmapped_size, %ecx
|
---|
| 126 | cld
|
---|
| 127 | rep movsb
|
---|
| 128 |
|
---|
[b0edf3b2] | 129 | call map_kernel # map kernel and turn paging on
|
---|
[5dce48b9] | 130 |
|
---|
[5eb1379] | 131 | popl %eax
|
---|
| 132 | popl %ebx
|
---|
[b0edf3b2] | 133 | cmpl $MULTIBOOT_LOADER_MAGIC, %eax # compare GRUB signature
|
---|
[5eb1379] | 134 | je valid_boot
|
---|
[339e053] | 135 |
|
---|
| 136 | xorl %ecx, %ecx # no memory size or map available
|
---|
| 137 | movl %ecx, e801memorysize
|
---|
| 138 | movl %ecx, e820counter
|
---|
[5eb1379] | 139 |
|
---|
| 140 | jmp invalid_boot
|
---|
[339e053] | 141 |
|
---|
[5eb1379] | 142 | valid_boot:
|
---|
| 143 |
|
---|
[339e053] | 144 | movl (%ebx), %eax # ebx = physical address of struct multiboot_info
|
---|
[5eb1379] | 145 |
|
---|
[339e053] | 146 | bt $0, %eax # mbi->flags[0] (mem_lower, mem_upper valid)
|
---|
| 147 | jc mem_valid
|
---|
| 148 |
|
---|
| 149 | xorl %ecx, %ecx
|
---|
| 150 | jmp mem_invalid
|
---|
| 151 |
|
---|
| 152 | mem_valid:
|
---|
| 153 | movl 4(%ebx), %ecx # mbi->mem_lower
|
---|
| 154 | addl 8(%ebx), %ecx # mbi->mem_upper
|
---|
[5eb1379] | 155 |
|
---|
[339e053] | 156 | mem_invalid:
|
---|
[5eb1379] | 157 | movl %ecx, e801memorysize
|
---|
| 158 |
|
---|
[aa72859] | 159 | bt $3, %eax # mbi->flags[3] (mods_count, mods_addr valid)
|
---|
| 160 | jc mods_valid
|
---|
| 161 |
|
---|
| 162 | xorl %ecx, %ecx
|
---|
| 163 | xorl %edx, %edx
|
---|
| 164 | jmp mods_invalid
|
---|
| 165 |
|
---|
| 166 | mods_valid:
|
---|
| 167 |
|
---|
| 168 | mods_invalid:
|
---|
| 169 | movl %ecx, init_addr
|
---|
| 170 | movl %edx, init_size
|
---|
| 171 |
|
---|
[339e053] | 172 | bt $6, %eax # mbi->flags[6] (mmap_length, mmap_addr valid)
|
---|
| 173 | jc mmap_valid
|
---|
| 174 |
|
---|
| 175 | xorl %edx, %edx
|
---|
| 176 | jmp mmap_invalid
|
---|
| 177 |
|
---|
| 178 | mmap_valid:
|
---|
| 179 | movl 44(%ebx), %ecx # mbi->mmap_length
|
---|
| 180 | movl 48(%ebx), %esi # mbi->mmap_addr
|
---|
| 181 | movl $e820table, %edi
|
---|
| 182 | xorl %edx, %edx
|
---|
| 183 |
|
---|
| 184 | mmap_loop:
|
---|
| 185 | cmpl $0, %ecx
|
---|
| 186 | jle mmap_end
|
---|
| 187 |
|
---|
| 188 | movl 4(%esi), %eax # mmap->base_addr_low
|
---|
| 189 | movl %eax, (%edi)
|
---|
| 190 |
|
---|
| 191 | movl 8(%esi), %eax # mmap->base_addr_high
|
---|
| 192 | movl %eax, 4(%edi)
|
---|
| 193 |
|
---|
| 194 | movl 12(%esi), %eax # mmap->length_low
|
---|
| 195 | movl %eax, 8(%edi)
|
---|
| 196 |
|
---|
| 197 | movl 16(%esi), %eax # mmap->length_high
|
---|
| 198 | movl %eax, 12(%edi)
|
---|
| 199 |
|
---|
| 200 | movl 20(%esi), %eax # mmap->type
|
---|
| 201 | movl %eax, 16(%edi)
|
---|
| 202 |
|
---|
| 203 | movl (%esi), %eax # mmap->size
|
---|
| 204 | addl $0x4, %eax
|
---|
| 205 | addl %eax, %esi
|
---|
| 206 | subl %eax, %ecx
|
---|
| 207 | addl $MEMMAP_E820_RECORD_SIZE, %edi
|
---|
| 208 | incl %edx
|
---|
| 209 | jmp mmap_loop
|
---|
| 210 |
|
---|
| 211 | mmap_end:
|
---|
| 212 |
|
---|
| 213 | mmap_invalid:
|
---|
| 214 | movl %edx, e820counter
|
---|
[5eb1379] | 215 |
|
---|
| 216 | invalid_boot:
|
---|
| 217 |
|
---|
[b0edf3b2] | 218 | call main_bsp - BOOT_OFFSET # never returns
|
---|
[d47f0e1] | 219 |
|
---|
| 220 | cli
|
---|
| 221 | hlt
|
---|
| 222 |
|
---|
| 223 | .global map_kernel
|
---|
| 224 | map_kernel:
|
---|
[dcbc8be] | 225 | #
|
---|
| 226 | # Here we setup mapping for both the unmapped and mapped sections of the kernel.
|
---|
[cf27a6cb] | 227 | # For simplicity, we map the entire 4G space.
|
---|
[dcbc8be] | 228 | #
|
---|
| 229 | movl %cr4, %ecx
|
---|
| 230 | orl $(1<<4), %ecx
|
---|
[b0edf3b2] | 231 | movl %ecx, %cr4 # turn PSE on
|
---|
[dcbc8be] | 232 |
|
---|
[cf27a6cb] | 233 | movl $(page_directory+0), %esi
|
---|
| 234 | movl $(page_directory+2048), %edi
|
---|
| 235 | xorl %ecx, %ecx
|
---|
| 236 | xorl %ebx, %ebx
|
---|
| 237 | 0:
|
---|
[dcbc8be] | 238 | movl $((1<<7)|(1<<0)), %eax
|
---|
[cf27a6cb] | 239 | orl %ebx, %eax
|
---|
| 240 | movl %eax, (%esi,%ecx,4) # mapping 0x00000000+%ecx*4M => 0x00000000+%ecx*4M
|
---|
| 241 | movl %eax, (%edi,%ecx,4) # mapping 0x80000000+%ecx*4M => 0x00000000+%ecx*4M
|
---|
| 242 | addl $(4*1024*1024), %ebx
|
---|
[dcbc8be] | 243 |
|
---|
[cf27a6cb] | 244 | incl %ecx
|
---|
| 245 | cmpl $512, %ecx
|
---|
| 246 | jl 0b
|
---|
[dcbc8be] | 247 |
|
---|
[cf27a6cb] | 248 | movl %esi, %cr3
|
---|
[f761f1eb] | 249 |
|
---|
[b52da8d7] | 250 | # turn paging on
|
---|
[dcbc8be] | 251 | movl %cr0, %ebx
|
---|
| 252 | orl $(1<<31), %ebx
|
---|
| 253 | movl %ebx, %cr0
|
---|
[d47f0e1] | 254 | ret
|
---|
[dcbc8be] | 255 |
|
---|
| 256 |
|
---|
[874e312a] | 257 | .section K_DATA_START, "aw", @progbits
|
---|
[dcbc8be] | 258 |
|
---|
| 259 | .align 4096
|
---|
| 260 | page_directory:
|
---|
| 261 | .space 4096, 0
|
---|
[8f2153b] | 262 |
|
---|
| 263 | .global real_bootstrap_gdtr_boot
|
---|
| 264 | real_bootstrap_gdtr_boot:
|
---|
| 265 | .word selector(GDT_ITEMS)
|
---|
| 266 | .long KA2PA(gdt)-BOOT_OFFSET
|
---|
[0b512a8] | 267 |
|
---|