[53634f9] | 1 | #
|
---|
[df4ed85] | 2 | # Copyright (c) 2005 Ondrej Palkovsky
|
---|
| 3 | # Copyright (c) 2006 Martin Decky
|
---|
[19077a5] | 4 | # Copyright (c) 2008 Jakub Jermar
|
---|
[1141c1a] | 5 | # All rights reserved.
|
---|
| 6 | #
|
---|
| 7 | # Redistribution and use in source and binary forms, with or without
|
---|
| 8 | # modification, are permitted provided that the following conditions
|
---|
| 9 | # are met:
|
---|
| 10 | #
|
---|
| 11 | # - Redistributions of source code must retain the above copyright
|
---|
| 12 | # notice, this list of conditions and the following disclaimer.
|
---|
| 13 | # - Redistributions in binary form must reproduce the above copyright
|
---|
| 14 | # notice, this list of conditions and the following disclaimer in the
|
---|
| 15 | # documentation and/or other materials provided with the distribution.
|
---|
| 16 | # - The name of the author may not be used to endorse or promote products
|
---|
| 17 | # derived from this software without specific prior written permission.
|
---|
| 18 | #
|
---|
| 19 | # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 20 | # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 21 | # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 22 | # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 23 | # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 24 | # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 25 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 26 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 27 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 28 | # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 29 | #
|
---|
| 30 |
|
---|
[46d26ee] | 31 | #include <arch/boot/boot.h>
|
---|
| 32 | #include <arch/boot/memmap.h>
|
---|
[8fc0d455] | 33 | #include <arch/mm/page.h>
|
---|
[6f878b7] | 34 | #include <arch/mm/ptl.h>
|
---|
[8fc0d455] | 35 | #include <arch/pm.h>
|
---|
[89344d85] | 36 | #include <arch/cpu.h>
|
---|
[c4b3e3e] | 37 | #include <arch/cpuid.h>
|
---|
[1141c1a] | 38 |
|
---|
[8725fb4] | 39 | #define START_STACK (BOOT_OFFSET - BOOT_STACK_SIZE)
|
---|
[421c833] | 40 |
|
---|
[874e312a] | 41 | .section K_TEXT_START, "ax"
|
---|
[46d26ee] | 42 |
|
---|
[8ccec3c1] | 43 | .code32
|
---|
| 44 | .align 4
|
---|
[46d26ee] | 45 | .global multiboot_image_start
|
---|
[8ccec3c1] | 46 | multiboot_header:
|
---|
| 47 | .long MULTIBOOT_HEADER_MAGIC
|
---|
| 48 | .long MULTIBOOT_HEADER_FLAGS
|
---|
[421c833] | 49 | .long -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS) # checksum
|
---|
[8725fb4] | 50 | .long multiboot_header
|
---|
| 51 | .long unmapped_ktext_start
|
---|
[8ccec3c1] | 52 | .long 0
|
---|
| 53 | .long 0
|
---|
[8725fb4] | 54 | .long multiboot_image_start
|
---|
[8ccec3c1] | 55 |
|
---|
| 56 | multiboot_image_start:
|
---|
[e13daa5d] | 57 | cld
|
---|
[421c833] | 58 | movl $START_STACK, %esp # initialize stack pointer
|
---|
| 59 | lgdtl bootstrap_gdtr # initialize Global Descriptor Table register
|
---|
| 60 |
|
---|
[1a67595] | 61 | movw $gdtselector(KDATA_DES), %cx
|
---|
| 62 | movw %cx, %es
|
---|
[421c833] | 63 | movw %cx, %ds # kernel data + stack
|
---|
[1a67595] | 64 | movw %cx, %ss
|
---|
[421c833] | 65 |
|
---|
| 66 | #
|
---|
[65640fef] | 67 | # Simics seems to remove hidden part of GS on entering user mode
|
---|
[421c833] | 68 | # when _visible_ part of GS does not point to user-mode segment.
|
---|
| 69 | #
|
---|
| 70 |
|
---|
[65640fef] | 71 | movw $gdtselector(UDATA_DES), %cx
|
---|
| 72 | movw %cx, %fs
|
---|
| 73 | movw %cx, %gs
|
---|
[1a67595] | 74 |
|
---|
[46d26ee] | 75 | jmpl $gdtselector(KTEXT32_DES), $multiboot_meeting_point
|
---|
[1a67595] | 76 | multiboot_meeting_point:
|
---|
| 77 |
|
---|
[421c833] | 78 | movl %eax, grub_eax # save parameters from GRUB
|
---|
[46d26ee] | 79 | movl %ebx, grub_ebx
|
---|
| 80 |
|
---|
[421c833] | 81 | #
|
---|
[ac88c93] | 82 | # Protected 32-bit. We want to reuse the code-seg descriptor,
|
---|
[421c833] | 83 | # the Default operand size must not be 1 when entering long mode.
|
---|
| 84 | #
|
---|
[ac88c93] | 85 |
|
---|
[421c833] | 86 | movl $(INTEL_CPUID_EXTENDED), %eax
|
---|
| 87 | cpuid
|
---|
| 88 | cmp $(INTEL_CPUID_EXTENDED), %eax
|
---|
[4fb6bf36] | 89 | ja extended_cpuid_supported
|
---|
[421c833] | 90 |
|
---|
[4fb6bf36] | 91 | movl $extended_cpuid_msg, %esi
|
---|
| 92 | jmp error_halt
|
---|
| 93 |
|
---|
| 94 | extended_cpuid_supported:
|
---|
| 95 |
|
---|
| 96 | movl $(AMD_CPUID_EXTENDED), %eax
|
---|
[ac88c93] | 97 | cpuid
|
---|
[4fb6bf36] | 98 | bt $(AMD_EXT_LONG_MODE), %edx
|
---|
[421c833] | 99 | jc long_mode_supported
|
---|
| 100 |
|
---|
[ac88c93] | 101 | movl $long_mode_msg, %esi
|
---|
| 102 | jmp error_halt
|
---|
[421c833] | 103 |
|
---|
[ac88c93] | 104 | long_mode_supported:
|
---|
| 105 |
|
---|
[4fb6bf36] | 106 | bt $(AMD_EXT_NOEXECUTE), %edx
|
---|
| 107 | jc noexecute_supported
|
---|
| 108 |
|
---|
| 109 | movl $noexecute_msg, %esi
|
---|
| 110 | jmp error_halt
|
---|
| 111 |
|
---|
| 112 | noexecute_supported:
|
---|
| 113 |
|
---|
| 114 | movl $(INTEL_CPUID_STANDARD), %eax
|
---|
| 115 | cpuid
|
---|
| 116 | bt $(INTEL_FXSAVE), %edx
|
---|
[421c833] | 117 | jc fx_supported
|
---|
[4fb6bf36] | 118 |
|
---|
| 119 | movl $fx_msg, %esi
|
---|
| 120 | jmp error_halt
|
---|
| 121 |
|
---|
| 122 | fx_supported:
|
---|
| 123 |
|
---|
| 124 | bt $(INTEL_SSE2), %edx
|
---|
[421c833] | 125 | jc sse2_supported
|
---|
[4fb6bf36] | 126 |
|
---|
| 127 | movl $sse2_msg, %esi
|
---|
| 128 | jmp error_halt
|
---|
| 129 |
|
---|
| 130 | sse2_supported:
|
---|
[de07bcf] | 131 |
|
---|
[421c833] | 132 | #include "vesa_prot.inc"
|
---|
| 133 |
|
---|
| 134 | #
|
---|
[4fb6bf36] | 135 | # Enable 64-bit page translation entries - CR4.PAE = 1.
|
---|
[421c833] | 136 | # Paging is not enabled until after long mode is enabled.
|
---|
| 137 | #
|
---|
[8725fb4] | 138 |
|
---|
[6f878b7] | 139 | movl %cr4, %eax
|
---|
| 140 | btsl $5, %eax
|
---|
| 141 | movl %eax, %cr4
|
---|
[421c833] | 142 |
|
---|
| 143 | # set up paging tables
|
---|
[8725fb4] | 144 |
|
---|
[6f878b7] | 145 | leal ptl_0, %eax
|
---|
| 146 | movl %eax, %cr3
|
---|
[c4b3e3e] | 147 |
|
---|
[421c833] | 148 | # enable long mode
|
---|
[8725fb4] | 149 |
|
---|
[421c833] | 150 | movl $EFER_MSR_NUM, %ecx # EFER MSR number
|
---|
| 151 | rdmsr # read EFER
|
---|
| 152 | btsl $AMD_LME_FLAG, %eax # set LME = 1
|
---|
| 153 | wrmsr # write EFER
|
---|
[c245372b] | 154 |
|
---|
[421c833] | 155 | # enable paging to activate long mode (set CR0.PG = 1)
|
---|
[8725fb4] | 156 |
|
---|
[6f878b7] | 157 | movl %cr0, %eax
|
---|
| 158 | btsl $31, %eax
|
---|
| 159 | movl %eax, %cr0
|
---|
[c245372b] | 160 |
|
---|
[421c833] | 161 | # at this point we are in compatibility mode
|
---|
[8725fb4] | 162 |
|
---|
[b9e97fb] | 163 | jmpl $gdtselector(KTEXT_DES), $start64
|
---|
[c245372b] | 164 |
|
---|
[6f878b7] | 165 | .code64
|
---|
| 166 | start64:
|
---|
[c4b3e3e] | 167 | movq $(PA2KA(START_STACK)), %rsp
|
---|
[421c833] | 168 |
|
---|
| 169 | # call arch_pre_main(grub_eax, grub_ebx)
|
---|
[5d8d71e] | 170 | xorq %rdi, %rdi
|
---|
| 171 | movl grub_eax, %edi
|
---|
| 172 | xorq %rsi, %rsi
|
---|
| 173 | movl grub_ebx, %esi
|
---|
| 174 | call arch_pre_main
|
---|
[8778271] | 175 |
|
---|
[304342e] | 176 | # create the first stack frame
|
---|
| 177 | pushq $0
|
---|
| 178 | movq %rsp, %rbp
|
---|
| 179 |
|
---|
[421c833] | 180 | call main_bsp
|
---|
[7cb567cd] | 181 |
|
---|
[421c833] | 182 | # not reached
|
---|
[8778271] | 183 |
|
---|
[421c833] | 184 | cli
|
---|
| 185 | hlt0:
|
---|
| 186 | hlt
|
---|
| 187 | jmp hlt0
|
---|
[ac88c93] | 188 |
|
---|
| 189 | # Print string from %esi to EGA display (in red) and halt
|
---|
| 190 | error_halt:
|
---|
[421c833] | 191 | movl $0xb8000, %edi # base of EGA text mode memory
|
---|
[ac88c93] | 192 | xorl %eax, %eax
|
---|
| 193 |
|
---|
[421c833] | 194 | movw $0x3d4, %dx # read bits 8 - 15 of the cursor address
|
---|
[ac88c93] | 195 | movb $0xe, %al
|
---|
| 196 | outb %al, %dx
|
---|
| 197 |
|
---|
| 198 | movw $0x3d5, %dx
|
---|
| 199 | inb %dx, %al
|
---|
| 200 | shl $8, %ax
|
---|
| 201 |
|
---|
[421c833] | 202 | movw $0x3d4, %dx # read bits 0 - 7 of the cursor address
|
---|
[ac88c93] | 203 | movb $0xf, %al
|
---|
| 204 | outb %al, %dx
|
---|
| 205 |
|
---|
| 206 | movw $0x3d5, %dx
|
---|
| 207 | inb %dx, %al
|
---|
| 208 |
|
---|
| 209 | cmp $1920, %ax
|
---|
| 210 | jbe cursor_ok
|
---|
[421c833] | 211 |
|
---|
| 212 | movw $1920, %ax # sanity check for the cursor on the last line
|
---|
| 213 |
|
---|
[ac88c93] | 214 | cursor_ok:
|
---|
| 215 |
|
---|
| 216 | movw %ax, %bx
|
---|
| 217 | shl $1, %eax
|
---|
| 218 | addl %eax, %edi
|
---|
| 219 |
|
---|
[421c833] | 220 | movw $0x0c00, %ax # black background, light red foreground
|
---|
[ac88c93] | 221 |
|
---|
| 222 | ploop:
|
---|
| 223 | lodsb
|
---|
| 224 | cmp $0, %al
|
---|
| 225 | je ploop_end
|
---|
| 226 | stosw
|
---|
[421c833] | 227 | inc %bx
|
---|
[ac88c93] | 228 | jmp ploop
|
---|
| 229 | ploop_end:
|
---|
| 230 |
|
---|
[421c833] | 231 | movw $0x3d4, %dx # write bits 8 - 15 of the cursor address
|
---|
[ac88c93] | 232 | movb $0xe, %al
|
---|
| 233 | outb %al, %dx
|
---|
| 234 |
|
---|
| 235 | movw $0x3d5, %dx
|
---|
| 236 | movb %bh, %al
|
---|
| 237 | outb %al, %dx
|
---|
| 238 |
|
---|
[421c833] | 239 | movw $0x3d4, %dx # write bits 0 - 7 of the cursor address
|
---|
[ac88c93] | 240 | movb $0xf, %al
|
---|
| 241 | outb %al, %dx
|
---|
| 242 |
|
---|
| 243 | movw $0x3d5, %dx
|
---|
| 244 | movb %bl, %al
|
---|
| 245 | outb %al, %dx
|
---|
[421c833] | 246 |
|
---|
[ac88c93] | 247 | cli
|
---|
[421c833] | 248 | hlt1:
|
---|
| 249 | hlt
|
---|
| 250 | jmp hlt1
|
---|
| 251 |
|
---|
| 252 | #include "vesa_real.inc"
|
---|
[19077a5] | 253 |
|
---|
| 254 | .section K_INI_PTLS, "aw", @progbits
|
---|
| 255 |
|
---|
| 256 | #
|
---|
| 257 | # Macro for generating initial page table contents.
|
---|
[421c833] | 258 | # @param cnt Number of entries to generat. Must be multiple of 8.
|
---|
| 259 | # @param g Number of GB that will be added to the mapping.
|
---|
[19077a5] | 260 | #
|
---|
| 261 | .macro ptl2gen cnt g
|
---|
| 262 | .if \cnt
|
---|
| 263 | ptl2gen "\cnt - 8" \g
|
---|
| 264 | .quad ((\cnt - 8) * 0x200000) + (\g * 1024 * 1024 * 1024) | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
|
---|
| 265 | .quad ((\cnt - 7) * 0x200000) + (\g * 1024 * 1024 * 1024) | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
|
---|
| 266 | .quad ((\cnt - 6) * 0x200000) + (\g * 1024 * 1024 * 1024) | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
|
---|
| 267 | .quad ((\cnt - 5) * 0x200000) + (\g * 1024 * 1024 * 1024) | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
|
---|
| 268 | .quad ((\cnt - 4) * 0x200000) + (\g * 1024 * 1024 * 1024) | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
|
---|
| 269 | .quad ((\cnt - 3) * 0x200000) + (\g * 1024 * 1024 * 1024) | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
|
---|
| 270 | .quad ((\cnt - 2) * 0x200000) + (\g * 1024 * 1024 * 1024) | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
|
---|
| 271 | .quad ((\cnt - 1) * 0x200000) + (\g * 1024 * 1024 * 1024) | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
|
---|
| 272 | .endif
|
---|
| 273 | .endm
|
---|
| 274 |
|
---|
| 275 | # Page table for pages in the first gigabyte.
|
---|
[1141c1a] | 276 | .align 4096
|
---|
[19077a5] | 277 | .global ptl_2_0g
|
---|
| 278 | ptl_2_0g:
|
---|
| 279 | ptl2gen 512 0
|
---|
| 280 |
|
---|
| 281 | # Page table for pages in the second gigabyte.
|
---|
| 282 | .align 4096
|
---|
| 283 | .global ptl_2_1g
|
---|
| 284 | ptl_2_1g:
|
---|
| 285 | ptl2gen 512 1
|
---|
| 286 |
|
---|
| 287 | # Page table for pages in the third gigabyte.
|
---|
| 288 | .align 4096
|
---|
| 289 | .global ptl_2_2g
|
---|
| 290 | ptl_2_2g:
|
---|
| 291 | ptl2gen 512 2
|
---|
| 292 |
|
---|
| 293 | # Page table for pages in the fourth gigabyte.
|
---|
| 294 | .align 4096
|
---|
| 295 | .global ptl_2_3g
|
---|
| 296 | ptl_2_3g:
|
---|
| 297 | ptl2gen 512 3
|
---|
[b9e97fb] | 298 |
|
---|
[6f878b7] | 299 | .align 4096
|
---|
| 300 | .global ptl_1
|
---|
| 301 | ptl_1:
|
---|
[19077a5] | 302 | # Identity mapping for [0; 4G)
|
---|
| 303 | .quad ptl_2_0g + (PTL_WRITABLE | PTL_PRESENT)
|
---|
| 304 | .quad ptl_2_1g + (PTL_WRITABLE | PTL_PRESENT)
|
---|
| 305 | .quad ptl_2_2g + (PTL_WRITABLE | PTL_PRESENT)
|
---|
| 306 | .quad ptl_2_3g + (PTL_WRITABLE | PTL_PRESENT)
|
---|
| 307 | .fill 506, 8, 0
|
---|
| 308 | # Mapping of [0; 1G) at -2G
|
---|
| 309 | .quad ptl_2_0g + (PTL_WRITABLE | PTL_PRESENT)
|
---|
| 310 | .fill 1, 8, 0
|
---|
[421c833] | 311 |
|
---|
[6f878b7] | 312 | .align 4096
|
---|
| 313 | .global ptl_0
|
---|
| 314 | ptl_0:
|
---|
| 315 | .quad ptl_1 + (PTL_WRITABLE | PTL_PRESENT)
|
---|
[93165be] | 316 | .fill 255,8,0
|
---|
| 317 | .quad ptl_1 + (PTL_WRITABLE | PTL_PRESENT)
|
---|
| 318 | .fill 254,8,0
|
---|
[6f878b7] | 319 | .quad ptl_1 + (PTL_WRITABLE | PTL_PRESENT)
|
---|
| 320 |
|
---|
[19077a5] | 321 | .section K_DATA_START, "aw", @progbits
|
---|
| 322 |
|
---|
[46d26ee] | 323 | .global bootstrap_gdtr
|
---|
| 324 | bootstrap_gdtr:
|
---|
[1a67595] | 325 | .word gdtselector(GDT_ITEMS)
|
---|
| 326 | .long KA2PA(gdt)
|
---|
[46d26ee] | 327 |
|
---|
| 328 | grub_eax:
|
---|
| 329 | .long 0
|
---|
| 330 |
|
---|
| 331 | grub_ebx:
|
---|
| 332 | .long 0
|
---|
[ac88c93] | 333 |
|
---|
[4fb6bf36] | 334 | extended_cpuid_msg:
|
---|
| 335 | .asciz "Extended CPUID not supported. System halted."
|
---|
[ac88c93] | 336 | long_mode_msg:
|
---|
[4f42d52] | 337 | .asciz "64 bit long mode not supported. System halted."
|
---|
[4fb6bf36] | 338 | noexecute_msg:
|
---|
| 339 | .asciz "No-execute pages not supported. System halted."
|
---|
| 340 | fx_msg:
|
---|
| 341 | .asciz "FXSAVE/FXRESTORE instructions not supported. System halted."
|
---|
| 342 | sse2_msg:
|
---|
| 343 | .asciz "SSE2 instructions not supported. System halted."
|
---|