[1141c1a] | 1 | #
|
---|
[ee7e6ace] | 2 | # Copyright (C) 2005 Ondrej Palkovsky
|
---|
[1141c1a] | 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 |
|
---|
[8fc0d455] | 29 | #include <arch/mm/page.h>
|
---|
[6f878b7] | 30 | #include <arch/mm/ptl.h>
|
---|
[8fc0d455] | 31 | #include <arch/pm.h>
|
---|
[89344d85] | 32 | #include <arch/cpu.h>
|
---|
[c4b3e3e] | 33 | #include <arch/cpuid.h>
|
---|
| 34 | #include <arch/boot/boot.h>
|
---|
[1141c1a] | 35 |
|
---|
[7df54df] | 36 | #define START_STACK_64 0xffffffff80007c00
|
---|
[6f878b7] | 37 |
|
---|
[1141c1a] | 38 | #
|
---|
| 39 | # This is where we require any SPARTAN-kernel-compatible boot loader
|
---|
| 40 | # to pass control in real mode.
|
---|
| 41 | #
|
---|
| 42 | # Protected mode tables are statically initialised during compile
|
---|
| 43 | # time. So we can just load the respective table registers and
|
---|
| 44 | # switch to protected mode.
|
---|
| 45 | #
|
---|
[c4b3e3e] | 46 |
|
---|
| 47 | #define START_STACK (BOOTSTRAP_OFFSET-0x400)
|
---|
| 48 |
|
---|
[874e312a] | 49 | .section K_TEXT_START, "ax"
|
---|
[6f878b7] | 50 | .code16
|
---|
| 51 | .global kernel_image_start
|
---|
[1141c1a] | 52 | kernel_image_start:
|
---|
[c245372b] | 53 | cli
|
---|
| 54 | xorw %ax,%ax
|
---|
| 55 | movw %ax,%ds
|
---|
[b1cf98c] | 56 | movw %ax,%es
|
---|
[c245372b] | 57 | movw %ax,%ss # initialize stack segment register
|
---|
[c4b3e3e] | 58 | movl $(START_STACK), %esp # initialize stack pointer
|
---|
[c245372b] | 59 |
|
---|
[7df54df] | 60 | call memmap_arch_init
|
---|
[c245372b] | 61 |
|
---|
[8fc0d455] | 62 | movl $0x80000000, %eax
|
---|
[c245372b] | 63 | cpuid
|
---|
| 64 | cmp $0x80000000, %eax # any function > 80000000h?
|
---|
| 65 | jbe no_long_mode
|
---|
[c4b3e3e] | 66 | movl $(AMD_CPUID_EXTENDED), %eax # Extended function code 80000001
|
---|
[c245372b] | 67 | cpuid
|
---|
| 68 | bt $29, %edx # Test if long mode is supported.
|
---|
| 69 | jnc no_long_mode
|
---|
[1141c1a] | 70 |
|
---|
[6f878b7] | 71 | # Load gdtr, idtr
|
---|
[3396f59] | 72 | lgdt real_bootstrap_gdtr_boot
|
---|
[6f878b7] | 73 |
|
---|
| 74 | movl %cr0,%eax
|
---|
| 75 | orl $0x1,%eax
|
---|
| 76 | movl %eax,%cr0 # switch to protected mode
|
---|
[1141c1a] | 77 |
|
---|
[b9e97fb] | 78 | jmpl $gdtselector(KTEXT32_DES), $now_in_prot
|
---|
[1141c1a] | 79 |
|
---|
[6f878b7] | 80 | no_long_mode:
|
---|
| 81 | 1:
|
---|
| 82 | jmp 1b
|
---|
[8ccec3c1] | 83 |
|
---|
| 84 | .code32
|
---|
| 85 | .align 4
|
---|
| 86 | multiboot_header:
|
---|
| 87 | .long MULTIBOOT_HEADER_MAGIC
|
---|
| 88 | .long MULTIBOOT_HEADER_FLAGS
|
---|
| 89 | .long -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS) # checksum
|
---|
| 90 | .long multiboot_header + BOOT_OFFSET
|
---|
| 91 | .long unmapped_ktext_start + BOOT_OFFSET
|
---|
| 92 | .long 0
|
---|
| 93 | .long 0
|
---|
| 94 | .long multiboot_image_start + BOOT_OFFSET
|
---|
| 95 |
|
---|
| 96 | multiboot_image_start:
|
---|
| 97 | movl $START_STACK, %esp # initialize stack pointer
|
---|
| 98 | # FIXME TODO
|
---|
[1141c1a] | 99 |
|
---|
[946b630] | 100 | # Protected 32-bit. We want to reuse the code-seg descriptor,
|
---|
| 101 | # the Default operand size must not be 1 when entering long mode
|
---|
[6f878b7] | 102 | now_in_prot:
|
---|
| 103 | # Set up stack & data descriptors
|
---|
[b9e97fb] | 104 | movw $gdtselector(KDATA_DES), %ax
|
---|
[6f878b7] | 105 | movw %ax, %ds
|
---|
| 106 | movw %ax, %ss
|
---|
| 107 |
|
---|
[c4b3e3e] | 108 | movb $0xd1, %al # enable A20 using the keyboard controller
|
---|
| 109 | outb %al, $0x64
|
---|
| 110 | movb $0xdf, %al
|
---|
| 111 | outb %al, $0x60
|
---|
| 112 |
|
---|
| 113 |
|
---|
[6f878b7] | 114 | # Enable 64-bit page transaltion entries - CR4.PAE = 1.
|
---|
| 115 | # Paging is not enabled until after long mode is enabled
|
---|
| 116 | movl %cr4, %eax
|
---|
| 117 | btsl $5, %eax
|
---|
| 118 | movl %eax, %cr4
|
---|
| 119 |
|
---|
| 120 | # Set up paging tables
|
---|
| 121 | leal ptl_0, %eax
|
---|
| 122 | movl %eax, %cr3
|
---|
[c4b3e3e] | 123 |
|
---|
[6f878b7] | 124 | # Enable long mode
|
---|
[89344d85] | 125 | movl $EFER_MSR_NUM, %ecx # EFER MSR number
|
---|
[6f878b7] | 126 | rdmsr # Read EFER
|
---|
[89344d85] | 127 | btsl $AMD_LME_FLAG, %eax # Set LME=1
|
---|
[6f878b7] | 128 | wrmsr # Write EFER
|
---|
[c245372b] | 129 |
|
---|
[6f878b7] | 130 | # Enable paging to activate long mode (set CR0.PG=1)
|
---|
| 131 | movl %cr0, %eax
|
---|
| 132 | btsl $31, %eax
|
---|
| 133 | movl %eax, %cr0
|
---|
[c245372b] | 134 |
|
---|
[6f878b7] | 135 | # At this point we are in compatibility mode
|
---|
[b9e97fb] | 136 | jmpl $gdtselector(KTEXT_DES), $start64
|
---|
[c245372b] | 137 |
|
---|
[6f878b7] | 138 | .code64
|
---|
| 139 | start64:
|
---|
[c4b3e3e] | 140 | movq $(PA2KA(START_STACK)), %rsp
|
---|
| 141 |
|
---|
| 142 | # Copy kernel to higher physical memory
|
---|
| 143 | movq $BOOTSTRAP_OFFSET, %rsi
|
---|
| 144 | movq $BOOTSTRAP_OFFSET + BOOT_OFFSET, %rdi
|
---|
| 145 | movq $_hardcoded_kernel_size, %rcx
|
---|
| 146 | cld
|
---|
| 147 | rep movsb
|
---|
[6f878b7] | 148 |
|
---|
| 149 | call main_bsp # never returns
|
---|
[c245372b] | 150 | 1:
|
---|
| 151 | jmp 1b
|
---|
[b9e97fb] | 152 |
|
---|
[874e312a] | 153 | .section K_DATA_START, "aw", @progbits
|
---|
[1141c1a] | 154 | .align 4096
|
---|
[b9e97fb] | 155 |
|
---|
[a16bfd0] | 156 | # Identical mapping of first 64MB and the same of -2GB -> 0
|
---|
[6f878b7] | 157 | .global ptl_2
|
---|
| 158 | ptl_2:
|
---|
| 159 | .quad 0x0 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
|
---|
| 160 | .quad 0x200000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
|
---|
| 161 | .quad 0x400000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
|
---|
| 162 | .quad 0x600000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
|
---|
| 163 | .quad 0x800000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
|
---|
| 164 | .quad 0xa00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
|
---|
| 165 | .quad 0xc00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
|
---|
| 166 | .quad 0xe00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
|
---|
[a16bfd0] | 167 | .quad 0x1000000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
|
---|
| 168 | .quad 0x1200000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
|
---|
| 169 | .quad 0x1400000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
|
---|
| 170 | .quad 0x1600000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
|
---|
| 171 | .quad 0x1800000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
|
---|
| 172 | .quad 0x1a00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
|
---|
| 173 | .quad 0x1c00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
|
---|
| 174 | .quad 0x1e00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
|
---|
| 175 | .quad 0x2000000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
|
---|
| 176 | .quad 0x2200000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
|
---|
| 177 | .quad 0x2400000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
|
---|
| 178 | .quad 0x2600000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
|
---|
| 179 | .quad 0x2800000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
|
---|
| 180 | .quad 0x2a00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
|
---|
| 181 | .quad 0x2c00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
|
---|
| 182 | .quad 0x2e00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
|
---|
| 183 | .quad 0x3000000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
|
---|
| 184 | .quad 0x3200000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
|
---|
| 185 | .quad 0x3400000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
|
---|
| 186 | .quad 0x3600000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
|
---|
| 187 | .quad 0x3800000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
|
---|
| 188 | .quad 0x3a00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
|
---|
| 189 | .quad 0x3c00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
|
---|
| 190 | .quad 0x3e00000 | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
|
---|
[6f878b7] | 191 |
|
---|
| 192 | .align 4096
|
---|
| 193 | .global ptl_1
|
---|
| 194 | ptl_1:
|
---|
| 195 | .quad ptl_2 + (PTL_WRITABLE | PTL_PRESENT)
|
---|
| 196 | .fill 509,8,0
|
---|
| 197 | .quad ptl_2 + (PTL_WRITABLE | PTL_PRESENT)
|
---|
[ae9624e] | 198 | .fill 1,8,0
|
---|
[6f878b7] | 199 |
|
---|
| 200 | .align 4096
|
---|
| 201 | .global ptl_0
|
---|
| 202 | ptl_0:
|
---|
| 203 | .quad ptl_1 + (PTL_WRITABLE | PTL_PRESENT)
|
---|
| 204 | .fill 510,8,0
|
---|
| 205 | .quad ptl_1 + (PTL_WRITABLE | PTL_PRESENT)
|
---|
| 206 |
|
---|
[3396f59] | 207 | .global real_bootstrap_gdtr_boot
|
---|
| 208 | real_bootstrap_gdtr_boot:
|
---|
[b9e97fb] | 209 | .word gdtselector(GDT_ITEMS)
|
---|
[c4b3e3e] | 210 | .long KA2PA(gdt)-BOOT_OFFSET
|
---|