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