Rev 694 | Rev 696 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 164 | palkovsky | 1 | # |
| 288 | jermar | 2 | # Copyright (C) 2005 Ondrej Palkovsky |
| 164 | palkovsky | 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 | |||
| 695 | decky | 29 | #include <arch/boot/boot.h> |
| 30 | #include <arch/boot/memmap.h> |
||
| 194 | palkovsky | 31 | #include <arch/mm/page.h> |
| 188 | palkovsky | 32 | #include <arch/mm/ptl.h> |
| 194 | palkovsky | 33 | #include <arch/pm.h> |
| 251 | palkovsky | 34 | #include <arch/cpu.h> |
| 275 | palkovsky | 35 | #include <arch/cpuid.h> |
| 164 | palkovsky | 36 | |
| 694 | decky | 37 | #define START_STACK (BOOT_OFFSET - BOOT_STACK_SIZE) |
| 275 | palkovsky | 38 | |
| 406 | jermar | 39 | .section K_TEXT_START, "ax" |
| 694 | decky | 40 | # .code16 |
| 41 | # .global kernel_image_start |
||
| 42 | # .global multiboot_image_start |
||
| 43 | # kernel_image_start: |
||
| 695 | decky | 44 | |
| 694 | decky | 45 | # movl $0x80000000, %eax |
| 46 | # cpuid |
||
| 47 | # cmp $0x80000000, %eax # any function > 80000000h? |
||
| 48 | # jbe no_long_mode |
||
| 49 | # movl $(AMD_CPUID_EXTENDED), %eax # Extended function code 80000001 |
||
| 50 | # cpuid |
||
| 51 | # bt $29, %edx # Test if long mode is supported. |
||
| 52 | # jnc no_long_mode |
||
| 53 | # |
||
| 54 | # |
||
| 55 | # no_long_mode: |
||
| 56 | # 1: |
||
| 57 | # jmp 1b |
||
| 58 | # |
||
| 680 | decky | 59 | .code32 |
| 60 | .align 4 |
||
| 695 | decky | 61 | .global multiboot_image_start |
| 680 | decky | 62 | multiboot_header: |
| 63 | .long MULTIBOOT_HEADER_MAGIC |
||
| 64 | .long MULTIBOOT_HEADER_FLAGS |
||
| 65 | .long -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS) # checksum |
||
| 694 | decky | 66 | .long multiboot_header |
| 67 | .long unmapped_ktext_start |
||
| 680 | decky | 68 | .long 0 |
| 69 | .long 0 |
||
| 694 | decky | 70 | .long multiboot_image_start |
| 188 | palkovsky | 71 | |
| 680 | decky | 72 | multiboot_image_start: |
| 695 | decky | 73 | movl $START_STACK, %esp # initialize stack pointer |
| 74 | lgdt bootstrap_gdtr # initialize Global Descriptor Table register |
||
| 680 | decky | 75 | |
| 685 | decky | 76 | movw $gdtselector(KDATA_DES), %cx |
| 77 | movw %cx, %es |
||
| 78 | movw %cx, %gs |
||
| 79 | movw %cx, %fs |
||
| 80 | movw %cx, %ds # kernel data + stack |
||
| 81 | movw %cx, %ss |
||
| 82 | |||
| 695 | decky | 83 | jmpl $gdtselector(KTEXT32_DES), $multiboot_meeting_point |
| 685 | decky | 84 | multiboot_meeting_point: |
| 85 | |||
| 695 | decky | 86 | movl %eax, grub_eax # save parameters from GRUB |
| 87 | movl %ebx, grub_ebx |
||
| 88 | |||
| 694 | decky | 89 | # Protected 32-bit. We want to reuse the code-seg descriptor, |
| 90 | # the Default operand size must not be 1 when entering long mode |
||
| 275 | palkovsky | 91 | |
| 188 | palkovsky | 92 | # Enable 64-bit page transaltion entries - CR4.PAE = 1. |
| 93 | # Paging is not enabled until after long mode is enabled |
||
| 694 | decky | 94 | |
| 188 | palkovsky | 95 | movl %cr4, %eax |
| 96 | btsl $5, %eax |
||
| 97 | movl %eax, %cr4 |
||
| 98 | |||
| 99 | # Set up paging tables |
||
| 694 | decky | 100 | |
| 188 | palkovsky | 101 | leal ptl_0, %eax |
| 102 | movl %eax, %cr3 |
||
| 275 | palkovsky | 103 | |
| 188 | palkovsky | 104 | # Enable long mode |
| 178 | palkovsky | 105 | |
| 694 | decky | 106 | movl $EFER_MSR_NUM, %ecx # EFER MSR number |
| 107 | rdmsr # Read EFER |
||
| 108 | btsl $AMD_LME_FLAG, %eax # Set LME=1 |
||
| 109 | wrmsr # Write EFER |
||
| 110 | |||
| 188 | palkovsky | 111 | # Enable paging to activate long mode (set CR0.PG=1) |
| 694 | decky | 112 | |
| 188 | palkovsky | 113 | movl %cr0, %eax |
| 114 | btsl $31, %eax |
||
| 115 | movl %eax, %cr0 |
||
| 116 | |||
| 117 | # At this point we are in compatibility mode |
||
| 694 | decky | 118 | |
| 206 | palkovsky | 119 | jmpl $gdtselector(KTEXT_DES), $start64 |
| 164 | palkovsky | 120 | |
| 188 | palkovsky | 121 | .code64 |
| 122 | start64: |
||
| 275 | palkovsky | 123 | movq $(PA2KA(START_STACK)), %rsp |
| 695 | decky | 124 | movl grub_eax, %eax |
| 125 | movl grub_ebx, %ebx |
||
| 126 | |||
| 127 | cmpl $MULTIBOOT_LOADER_MAGIC, %eax # compare GRUB signature |
||
| 128 | je valid_boot |
||
| 129 | |||
| 130 | xorl %ecx, %ecx # no memory size or map available |
||
| 131 | movl %ecx, e801memorysize |
||
| 132 | movl %ecx, e820counter |
||
| 133 | |||
| 134 | jmp invalid_boot |
||
| 135 | |||
| 136 | valid_boot: |
||
| 137 | |||
| 138 | movl (%ebx), %eax # ebx = physical address of struct multiboot_info |
||
| 139 | |||
| 140 | bt $0, %eax # mbi->flags[0] (mem_lower, mem_upper valid) |
||
| 141 | jc mem_valid |
||
| 142 | |||
| 143 | xorl %ecx, %ecx |
||
| 144 | jmp mem_invalid |
||
| 145 | |||
| 146 | mem_valid: |
||
| 147 | movl 4(%ebx), %ecx # mbi->mem_lower |
||
| 148 | addl 8(%ebx), %ecx # mbi->mem_upper |
||
| 149 | |||
| 150 | mem_invalid: |
||
| 151 | movl %ecx, e801memorysize |
||
| 152 | |||
| 153 | bt $3, %eax # mbi->flags[3] (mods_count, mods_addr valid) |
||
| 154 | jc mods_valid |
||
| 155 | |||
| 156 | xorl %ecx, %ecx |
||
| 157 | xorl %edx, %edx |
||
| 158 | jmp mods_invalid |
||
| 159 | |||
| 160 | mods_valid: |
||
| 161 | movl 20(%ebx), %ecx # mbi->mods_count |
||
| 162 | cmpl $0, %ecx |
||
| 163 | je mods_invalid |
||
| 164 | |||
| 165 | movl 24(%ebx), %esi # mbi->mods_addr |
||
| 166 | movl 0(%esi), %edx # mods->mod_start |
||
| 167 | movl 4(%esi), %ecx # mods->mod_end |
||
| 168 | subl %edx, %ecx |
||
| 169 | addl $0x80000000, %edx |
||
| 170 | |||
| 171 | mods_invalid: |
||
| 172 | movl %ecx, init_size |
||
| 173 | movl %edx, init_addr |
||
| 174 | |||
| 175 | bt $6, %eax # mbi->flags[6] (mmap_length, mmap_addr valid) |
||
| 176 | jc mmap_valid |
||
| 177 | |||
| 178 | xorl %edx, %edx |
||
| 179 | jmp mmap_invalid |
||
| 180 | |||
| 181 | mmap_valid: |
||
| 182 | movl 44(%ebx), %ecx # mbi->mmap_length |
||
| 183 | movl 48(%ebx), %esi # mbi->mmap_addr |
||
| 184 | movq $e820table, %rdi |
||
| 185 | xorl %edx, %edx |
||
| 186 | |||
| 187 | mmap_loop: |
||
| 188 | cmpl $0, %ecx |
||
| 189 | jle mmap_end |
||
| 190 | |||
| 191 | movl 4(%esi), %eax # mmap->base_addr_low |
||
| 192 | movl %eax, (%rdi) |
||
| 193 | |||
| 194 | movl 8(%esi), %eax # mmap->base_addr_high |
||
| 195 | movl %eax, 4(%rdi) |
||
| 196 | |||
| 197 | movl 12(%esi), %eax # mmap->length_low |
||
| 198 | movl %eax, 8(%rdi) |
||
| 199 | |||
| 200 | movl 16(%esi), %eax # mmap->length_high |
||
| 201 | movl %eax, 12(%rdi) |
||
| 202 | |||
| 203 | movl 20(%esi), %eax # mmap->type |
||
| 204 | movl %eax, 16(%rdi) |
||
| 205 | |||
| 206 | movl (%esi), %eax # mmap->size |
||
| 207 | addl $0x4, %eax |
||
| 208 | addl %eax, %esi |
||
| 209 | subl %eax, %ecx |
||
| 210 | addq $MEMMAP_E820_RECORD_SIZE, %rdi |
||
| 211 | incl %edx |
||
| 212 | jmp mmap_loop |
||
| 213 | |||
| 214 | mmap_end: |
||
| 215 | |||
| 216 | mmap_invalid: |
||
| 217 | movl %edx, e820counter |
||
| 218 | |||
| 219 | invalid_boot: |
||
| 220 | |||
| 694 | decky | 221 | call main_bsp # never returns |
| 178 | palkovsky | 222 | |
| 694 | decky | 223 | cli |
| 224 | hlt |
||
| 206 | palkovsky | 225 | |
| 406 | jermar | 226 | .section K_DATA_START, "aw", @progbits |
| 164 | palkovsky | 227 | .align 4096 |
| 206 | palkovsky | 228 | |
| 417 | palkovsky | 229 | # Identical mapping of first 64MB and the same of -2GB -> 0 |
| 188 | palkovsky | 230 | .global ptl_2 |
| 231 | ptl_2: |
||
| 232 | .quad 0x0 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE) |
||
| 233 | .quad 0x200000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE) |
||
| 234 | .quad 0x400000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE) |
||
| 235 | .quad 0x600000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE) |
||
| 236 | .quad 0x800000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE) |
||
| 237 | .quad 0xa00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE) |
||
| 238 | .quad 0xc00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE) |
||
| 239 | .quad 0xe00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE) |
||
| 417 | palkovsky | 240 | .quad 0x1000000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE) |
| 241 | .quad 0x1200000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE) |
||
| 242 | .quad 0x1400000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE) |
||
| 243 | .quad 0x1600000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE) |
||
| 244 | .quad 0x1800000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE) |
||
| 245 | .quad 0x1a00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE) |
||
| 246 | .quad 0x1c00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE) |
||
| 247 | .quad 0x1e00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE) |
||
| 248 | .quad 0x2000000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE) |
||
| 249 | .quad 0x2200000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE) |
||
| 250 | .quad 0x2400000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE) |
||
| 251 | .quad 0x2600000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE) |
||
| 252 | .quad 0x2800000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE) |
||
| 253 | .quad 0x2a00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE) |
||
| 254 | .quad 0x2c00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE) |
||
| 255 | .quad 0x2e00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE) |
||
| 256 | .quad 0x3000000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE) |
||
| 257 | .quad 0x3200000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE) |
||
| 258 | .quad 0x3400000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE) |
||
| 259 | .quad 0x3600000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE) |
||
| 260 | .quad 0x3800000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE) |
||
| 261 | .quad 0x3a00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE) |
||
| 262 | .quad 0x3c00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE) |
||
| 263 | .quad 0x3e00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE) |
||
| 188 | palkovsky | 264 | |
| 265 | .align 4096 |
||
| 266 | .global ptl_1 |
||
| 267 | ptl_1: |
||
| 268 | .quad ptl_2 + (PTL_WRITABLE | PTL_PRESENT) |
||
| 269 | .fill 509,8,0 |
||
| 270 | .quad ptl_2 + (PTL_WRITABLE | PTL_PRESENT) |
||
| 293 | palkovsky | 271 | .fill 1,8,0 |
| 188 | palkovsky | 272 | |
| 273 | .align 4096 |
||
| 274 | .global ptl_0 |
||
| 275 | ptl_0: |
||
| 276 | .quad ptl_1 + (PTL_WRITABLE | PTL_PRESENT) |
||
| 277 | .fill 510,8,0 |
||
| 278 | .quad ptl_1 + (PTL_WRITABLE | PTL_PRESENT) |
||
| 178 | palkovsky | 279 | |
| 695 | decky | 280 | .global bootstrap_gdtr |
| 281 | bootstrap_gdtr: |
||
| 685 | decky | 282 | .word gdtselector(GDT_ITEMS) |
| 283 | .long KA2PA(gdt) |
||
| 695 | decky | 284 | |
| 285 | grub_eax: |
||
| 286 | .long 0 |
||
| 287 | |||
| 288 | grub_ebx: |
||
| 289 | .long 0 |