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