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