[1141c1a] | 1 | #
|
---|
[ee7e6ace] | 2 | # Copyright (C) 2005 Ondrej Palkovsky
|
---|
[42edee68] | 3 | # Copyright (C) 2006 Martin Decky
|
---|
[1141c1a] | 4 | # All rights reserved.
|
---|
| 5 | #
|
---|
| 6 | # Redistribution and use in source and binary forms, with or without
|
---|
| 7 | # modification, are permitted provided that the following conditions
|
---|
| 8 | # are met:
|
---|
| 9 | #
|
---|
| 10 | # - Redistributions of source code must retain the above copyright
|
---|
| 11 | # notice, this list of conditions and the following disclaimer.
|
---|
| 12 | # - Redistributions in binary form must reproduce the above copyright
|
---|
| 13 | # notice, this list of conditions and the following disclaimer in the
|
---|
| 14 | # documentation and/or other materials provided with the distribution.
|
---|
| 15 | # - The name of the author may not be used to endorse or promote products
|
---|
| 16 | # derived from this software without specific prior written permission.
|
---|
| 17 | #
|
---|
| 18 | # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 19 | # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 20 | # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 21 | # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 22 | # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 23 | # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 24 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 25 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 26 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 27 | # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 28 | #
|
---|
| 29 |
|
---|
[46d26ee] | 30 | #include <arch/boot/boot.h>
|
---|
| 31 | #include <arch/boot/memmap.h>
|
---|
[8fc0d455] | 32 | #include <arch/mm/page.h>
|
---|
[6f878b7] | 33 | #include <arch/mm/ptl.h>
|
---|
[8fc0d455] | 34 | #include <arch/pm.h>
|
---|
[89344d85] | 35 | #include <arch/cpu.h>
|
---|
[c4b3e3e] | 36 | #include <arch/cpuid.h>
|
---|
[1141c1a] | 37 |
|
---|
[8725fb4] | 38 | #define START_STACK (BOOT_OFFSET - BOOT_STACK_SIZE)
|
---|
[c4b3e3e] | 39 |
|
---|
[874e312a] | 40 | .section K_TEXT_START, "ax"
|
---|
[46d26ee] | 41 |
|
---|
[8ccec3c1] | 42 | .code32
|
---|
| 43 | .align 4
|
---|
[46d26ee] | 44 | .global multiboot_image_start
|
---|
[8ccec3c1] | 45 | multiboot_header:
|
---|
| 46 | .long MULTIBOOT_HEADER_MAGIC
|
---|
| 47 | .long MULTIBOOT_HEADER_FLAGS
|
---|
| 48 | .long -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS) # checksum
|
---|
[8725fb4] | 49 | .long multiboot_header
|
---|
| 50 | .long unmapped_ktext_start
|
---|
[8ccec3c1] | 51 | .long 0
|
---|
| 52 | .long 0
|
---|
[8725fb4] | 53 | .long multiboot_image_start
|
---|
[8ccec3c1] | 54 |
|
---|
| 55 | multiboot_image_start:
|
---|
[46d26ee] | 56 | movl $START_STACK, %esp # initialize stack pointer
|
---|
| 57 | lgdt bootstrap_gdtr # initialize Global Descriptor Table register
|
---|
[1a67595] | 58 |
|
---|
| 59 | movw $gdtselector(KDATA_DES), %cx
|
---|
| 60 | movw %cx, %es
|
---|
| 61 | movw %cx, %ds # kernel data + stack
|
---|
| 62 | movw %cx, %ss
|
---|
[65640fef] | 63 | # Simics seems to remove hidden part of GS on entering user mode
|
---|
| 64 | # when _visible_ part of GS does not point to user-mode segment
|
---|
| 65 | movw $gdtselector(UDATA_DES), %cx
|
---|
| 66 | movw %cx, %fs
|
---|
| 67 | movw %cx, %gs
|
---|
[1a67595] | 68 |
|
---|
[46d26ee] | 69 | jmpl $gdtselector(KTEXT32_DES), $multiboot_meeting_point
|
---|
[1a67595] | 70 | multiboot_meeting_point:
|
---|
| 71 |
|
---|
[46d26ee] | 72 | movl %eax, grub_eax # save parameters from GRUB
|
---|
| 73 | movl %ebx, grub_ebx
|
---|
| 74 |
|
---|
[8725fb4] | 75 | # Protected 32-bit. We want to reuse the code-seg descriptor,
|
---|
| 76 | # the Default operand size must not be 1 when entering long mode
|
---|
| 77 |
|
---|
[42edee68] | 78 | movl $0x80000000, %eax
|
---|
| 79 | cpuid
|
---|
| 80 | cmp $0x80000000, %eax # any function > 80000000h?
|
---|
| 81 | jbe long_mode_unsupported
|
---|
| 82 | movl $(AMD_CPUID_EXTENDED), %eax # Extended function code 80000001
|
---|
| 83 | cpuid
|
---|
| 84 | bt $29, %edx # Test if long mode is supported.
|
---|
| 85 | jc long_mode_supported
|
---|
| 86 |
|
---|
| 87 | long_mode_unsupported:
|
---|
| 88 | cli
|
---|
| 89 | hlt
|
---|
| 90 |
|
---|
| 91 | long_mode_supported:
|
---|
| 92 |
|
---|
[6f878b7] | 93 | # Enable 64-bit page transaltion entries - CR4.PAE = 1.
|
---|
| 94 | # Paging is not enabled until after long mode is enabled
|
---|
[8725fb4] | 95 |
|
---|
[6f878b7] | 96 | movl %cr4, %eax
|
---|
| 97 | btsl $5, %eax
|
---|
| 98 | movl %eax, %cr4
|
---|
| 99 |
|
---|
| 100 | # Set up paging tables
|
---|
[8725fb4] | 101 |
|
---|
[6f878b7] | 102 | leal ptl_0, %eax
|
---|
| 103 | movl %eax, %cr3
|
---|
[c4b3e3e] | 104 |
|
---|
[6f878b7] | 105 | # Enable long mode
|
---|
[8725fb4] | 106 |
|
---|
| 107 | movl $EFER_MSR_NUM, %ecx # EFER MSR number
|
---|
| 108 | rdmsr # Read EFER
|
---|
| 109 | btsl $AMD_LME_FLAG, %eax # Set LME=1
|
---|
| 110 | wrmsr # Write EFER
|
---|
[c245372b] | 111 |
|
---|
[6f878b7] | 112 | # Enable paging to activate long mode (set CR0.PG=1)
|
---|
[8725fb4] | 113 |
|
---|
[6f878b7] | 114 | movl %cr0, %eax
|
---|
| 115 | btsl $31, %eax
|
---|
| 116 | movl %eax, %cr0
|
---|
[c245372b] | 117 |
|
---|
[6f878b7] | 118 | # At this point we are in compatibility mode
|
---|
[8725fb4] | 119 |
|
---|
[b9e97fb] | 120 | jmpl $gdtselector(KTEXT_DES), $start64
|
---|
[c245372b] | 121 |
|
---|
[6f878b7] | 122 | .code64
|
---|
| 123 | start64:
|
---|
[c4b3e3e] | 124 | movq $(PA2KA(START_STACK)), %rsp
|
---|
[46d26ee] | 125 | movl grub_eax, %eax
|
---|
| 126 | movl grub_ebx, %ebx
|
---|
| 127 |
|
---|
| 128 | cmpl $MULTIBOOT_LOADER_MAGIC, %eax # compare GRUB signature
|
---|
| 129 | je valid_boot
|
---|
| 130 |
|
---|
| 131 | xorl %ecx, %ecx # no memory size or map available
|
---|
| 132 | movl %ecx, e801memorysize
|
---|
| 133 | movl %ecx, e820counter
|
---|
| 134 |
|
---|
| 135 | jmp invalid_boot
|
---|
| 136 |
|
---|
| 137 | valid_boot:
|
---|
| 138 |
|
---|
| 139 | movl (%ebx), %eax # ebx = physical address of struct multiboot_info
|
---|
| 140 |
|
---|
| 141 | bt $0, %eax # mbi->flags[0] (mem_lower, mem_upper valid)
|
---|
| 142 | jc mem_valid
|
---|
| 143 |
|
---|
| 144 | xorl %ecx, %ecx
|
---|
| 145 | jmp mem_invalid
|
---|
| 146 |
|
---|
| 147 | mem_valid:
|
---|
| 148 | movl 4(%ebx), %ecx # mbi->mem_lower
|
---|
| 149 | addl 8(%ebx), %ecx # mbi->mem_upper
|
---|
| 150 |
|
---|
| 151 | mem_invalid:
|
---|
| 152 | movl %ecx, e801memorysize
|
---|
| 153 |
|
---|
| 154 | bt $3, %eax # mbi->flags[3] (mods_count, mods_addr valid)
|
---|
| 155 | jc mods_valid
|
---|
| 156 |
|
---|
| 157 | xorl %ecx, %ecx
|
---|
| 158 | xorl %edx, %edx
|
---|
| 159 | jmp mods_invalid
|
---|
| 160 |
|
---|
| 161 | mods_valid:
|
---|
| 162 | movl 20(%ebx), %ecx # mbi->mods_count
|
---|
| 163 | cmpl $0, %ecx
|
---|
| 164 | je mods_invalid
|
---|
| 165 |
|
---|
[7febdde5] | 166 | xorq %rdx, %rdx
|
---|
[46d26ee] | 167 | movl 24(%ebx), %esi # mbi->mods_addr
|
---|
| 168 | movl 0(%esi), %edx # mods->mod_start
|
---|
| 169 | movl 4(%esi), %ecx # mods->mod_end
|
---|
| 170 | subl %edx, %ecx
|
---|
[7febdde5] | 171 | addq $0xffffffff80000000, %rdx
|
---|
[46d26ee] | 172 |
|
---|
| 173 | mods_invalid:
|
---|
| 174 | movl %ecx, init_size
|
---|
[7febdde5] | 175 | movq %rdx, init_addr
|
---|
[46d26ee] | 176 |
|
---|
| 177 | bt $6, %eax # mbi->flags[6] (mmap_length, mmap_addr valid)
|
---|
| 178 | jc mmap_valid
|
---|
| 179 |
|
---|
| 180 | xorl %edx, %edx
|
---|
| 181 | jmp mmap_invalid
|
---|
| 182 |
|
---|
| 183 | mmap_valid:
|
---|
| 184 | movl 44(%ebx), %ecx # mbi->mmap_length
|
---|
| 185 | movl 48(%ebx), %esi # mbi->mmap_addr
|
---|
| 186 | movq $e820table, %rdi
|
---|
| 187 | xorl %edx, %edx
|
---|
| 188 |
|
---|
| 189 | mmap_loop:
|
---|
| 190 | cmpl $0, %ecx
|
---|
| 191 | jle mmap_end
|
---|
| 192 |
|
---|
| 193 | movl 4(%esi), %eax # mmap->base_addr_low
|
---|
| 194 | movl %eax, (%rdi)
|
---|
| 195 |
|
---|
| 196 | movl 8(%esi), %eax # mmap->base_addr_high
|
---|
| 197 | movl %eax, 4(%rdi)
|
---|
| 198 |
|
---|
| 199 | movl 12(%esi), %eax # mmap->length_low
|
---|
| 200 | movl %eax, 8(%rdi)
|
---|
| 201 |
|
---|
| 202 | movl 16(%esi), %eax # mmap->length_high
|
---|
| 203 | movl %eax, 12(%rdi)
|
---|
| 204 |
|
---|
| 205 | movl 20(%esi), %eax # mmap->type
|
---|
| 206 | movl %eax, 16(%rdi)
|
---|
| 207 |
|
---|
| 208 | movl (%esi), %eax # mmap->size
|
---|
| 209 | addl $0x4, %eax
|
---|
| 210 | addl %eax, %esi
|
---|
| 211 | subl %eax, %ecx
|
---|
| 212 | addq $MEMMAP_E820_RECORD_SIZE, %rdi
|
---|
| 213 | incl %edx
|
---|
| 214 | jmp mmap_loop
|
---|
| 215 |
|
---|
| 216 | mmap_end:
|
---|
| 217 |
|
---|
| 218 | mmap_invalid:
|
---|
| 219 | movl %edx, e820counter
|
---|
| 220 |
|
---|
| 221 | invalid_boot:
|
---|
| 222 |
|
---|
[42edee68] | 223 | #ifdef CONFIG_SMP
|
---|
| 224 |
|
---|
| 225 | # copy AP bootstrap routines below 1 MB
|
---|
| 226 |
|
---|
| 227 | movq $BOOT_OFFSET, %rsi
|
---|
| 228 | movq $AP_BOOT_OFFSET, %rdi
|
---|
| 229 | movq $_hardcoded_unmapped_size, %rcx
|
---|
| 230 | cld
|
---|
| 231 | rep movsb
|
---|
| 232 |
|
---|
| 233 | #endif
|
---|
| 234 |
|
---|
[6f878b7] | 235 | call main_bsp # never returns
|
---|
[8725fb4] | 236 |
|
---|
| 237 | cli
|
---|
| 238 | hlt
|
---|
[b9e97fb] | 239 |
|
---|
[874e312a] | 240 | .section K_DATA_START, "aw", @progbits
|
---|
[1141c1a] | 241 | .align 4096
|
---|
[b9e97fb] | 242 |
|
---|
[a16bfd0] | 243 | # Identical mapping of first 64MB and the same of -2GB -> 0
|
---|
[6f878b7] | 244 | .global ptl_2
|
---|
| 245 | ptl_2:
|
---|
| 246 | .quad 0x0 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
|
---|
| 247 | .quad 0x200000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
|
---|
| 248 | .quad 0x400000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
|
---|
| 249 | .quad 0x600000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
|
---|
| 250 | .quad 0x800000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
|
---|
| 251 | .quad 0xa00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
|
---|
| 252 | .quad 0xc00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
|
---|
| 253 | .quad 0xe00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
|
---|
[a16bfd0] | 254 | .quad 0x1000000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
|
---|
| 255 | .quad 0x1200000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
|
---|
| 256 | .quad 0x1400000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
|
---|
| 257 | .quad 0x1600000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
|
---|
| 258 | .quad 0x1800000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
|
---|
| 259 | .quad 0x1a00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
|
---|
| 260 | .quad 0x1c00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
|
---|
| 261 | .quad 0x1e00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
|
---|
| 262 | .quad 0x2000000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
|
---|
| 263 | .quad 0x2200000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
|
---|
| 264 | .quad 0x2400000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
|
---|
| 265 | .quad 0x2600000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
|
---|
| 266 | .quad 0x2800000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
|
---|
| 267 | .quad 0x2a00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
|
---|
| 268 | .quad 0x2c00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
|
---|
| 269 | .quad 0x2e00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
|
---|
| 270 | .quad 0x3000000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
|
---|
| 271 | .quad 0x3200000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
|
---|
| 272 | .quad 0x3400000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
|
---|
| 273 | .quad 0x3600000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
|
---|
| 274 | .quad 0x3800000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
|
---|
| 275 | .quad 0x3a00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
|
---|
| 276 | .quad 0x3c00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
|
---|
| 277 | .quad 0x3e00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
|
---|
[6f878b7] | 278 |
|
---|
| 279 | .align 4096
|
---|
| 280 | .global ptl_1
|
---|
| 281 | ptl_1:
|
---|
| 282 | .quad ptl_2 + (PTL_WRITABLE | PTL_PRESENT)
|
---|
| 283 | .fill 509,8,0
|
---|
| 284 | .quad ptl_2 + (PTL_WRITABLE | PTL_PRESENT)
|
---|
[ae9624e] | 285 | .fill 1,8,0
|
---|
[6f878b7] | 286 |
|
---|
| 287 | .align 4096
|
---|
| 288 | .global ptl_0
|
---|
| 289 | ptl_0:
|
---|
| 290 | .quad ptl_1 + (PTL_WRITABLE | PTL_PRESENT)
|
---|
| 291 | .fill 510,8,0
|
---|
| 292 | .quad ptl_1 + (PTL_WRITABLE | PTL_PRESENT)
|
---|
| 293 |
|
---|
[46d26ee] | 294 | .global bootstrap_gdtr
|
---|
| 295 | bootstrap_gdtr:
|
---|
[1a67595] | 296 | .word gdtselector(GDT_ITEMS)
|
---|
| 297 | .long KA2PA(gdt)
|
---|
[46d26ee] | 298 |
|
---|
| 299 | grub_eax:
|
---|
| 300 | .long 0
|
---|
| 301 |
|
---|
| 302 | grub_ebx:
|
---|
| 303 | .long 0
|
---|