[873c681] | 1 | /*
|
---|
| 2 | * Copyright (c) 2005 Ondrej Palkovsky
|
---|
| 3 | * Copyright (c) 2006 Martin Decky
|
---|
| 4 | * Copyright (c) 2008 Jakub Jermar
|
---|
| 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 | */
|
---|
[1141c1a] | 30 |
|
---|
[46d26ee] | 31 | #include <arch/boot/boot.h>
|
---|
[a1f60f3] | 32 | #include <arch/mm/page.h>
|
---|
[6f878b7] | 33 | #include <arch/mm/ptl.h>
|
---|
[8fc0d455] | 34 | #include <arch/pm.h>
|
---|
[1f5c9c96] | 35 | #include <genarch/multiboot/multiboot.h>
|
---|
[c4b3e3e] | 36 | #include <arch/cpuid.h>
|
---|
[1f5c9c96] | 37 | #include <arch/cpu.h>
|
---|
[1141c1a] | 38 |
|
---|
[873c681] | 39 | #define START_STACK (BOOT_OFFSET - BOOT_STACK_SIZE)
|
---|
[421c833] | 40 |
|
---|
[874e312a] | 41 | .section K_TEXT_START, "ax"
|
---|
[46d26ee] | 42 |
|
---|
[8ccec3c1] | 43 | .code32
|
---|
[873c681] | 44 |
|
---|
| 45 | .macro pm_error msg
|
---|
| 46 | movl \msg, %esi
|
---|
| 47 | jmp pm_error_halt
|
---|
| 48 | .endm
|
---|
| 49 |
|
---|
| 50 | .macro pm_status msg
|
---|
| 51 | #ifdef CONFIG_EGA
|
---|
| 52 | pushl %esi
|
---|
| 53 | movl \msg, %esi
|
---|
| 54 | call pm_early_puts
|
---|
| 55 | popl %esi
|
---|
| 56 | #endif
|
---|
| 57 | .endm
|
---|
| 58 |
|
---|
| 59 | .macro pm2_status msg
|
---|
| 60 | #ifndef CONFIG_FB
|
---|
| 61 | pm_status \msg
|
---|
| 62 | #endif
|
---|
| 63 | .endm
|
---|
| 64 |
|
---|
[8ccec3c1] | 65 | .align 4
|
---|
[46d26ee] | 66 | .global multiboot_image_start
|
---|
[8ccec3c1] | 67 | multiboot_header:
|
---|
| 68 | .long MULTIBOOT_HEADER_MAGIC
|
---|
| 69 | .long MULTIBOOT_HEADER_FLAGS
|
---|
[873c681] | 70 | .long -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS) /* checksum */
|
---|
[8725fb4] | 71 | .long multiboot_header
|
---|
| 72 | .long unmapped_ktext_start
|
---|
[8ccec3c1] | 73 | .long 0
|
---|
| 74 | .long 0
|
---|
[8725fb4] | 75 | .long multiboot_image_start
|
---|
[8ccec3c1] | 76 |
|
---|
| 77 | multiboot_image_start:
|
---|
[f66c203d] | 78 | cli
|
---|
[e13daa5d] | 79 | cld
|
---|
[421c833] | 80 |
|
---|
[873c681] | 81 | /* Initialize stack pointer */
|
---|
| 82 | movl $START_STACK, %esp
|
---|
| 83 |
|
---|
[f66c203d] | 84 | /*
|
---|
| 85 | * Initialize Global Descriptor Table and
|
---|
| 86 | * Interrupt Descriptor Table registers
|
---|
| 87 | */
|
---|
[873c681] | 88 | lgdtl bootstrap_gdtr
|
---|
[f66c203d] | 89 | lidtl bootstrap_idtr
|
---|
[873c681] | 90 |
|
---|
| 91 | /* Kernel data + stack */
|
---|
[1d3d2cf] | 92 | movw $GDT_SELECTOR(KDATA_DES), %cx
|
---|
[1a67595] | 93 | movw %cx, %es
|
---|
[873c681] | 94 | movw %cx, %ds
|
---|
[1a67595] | 95 | movw %cx, %ss
|
---|
[421c833] | 96 |
|
---|
[873c681] | 97 | /*
|
---|
| 98 | * Simics seems to remove hidden part of GS on entering user mode
|
---|
| 99 | * when _visible_ part of GS does not point to user-mode segment.
|
---|
| 100 | */
|
---|
[1d3d2cf] | 101 | movw $GDT_SELECTOR(UDATA_DES), %cx
|
---|
[65640fef] | 102 | movw %cx, %fs
|
---|
| 103 | movw %cx, %gs
|
---|
[1a67595] | 104 |
|
---|
[1d3d2cf] | 105 | jmpl $GDT_SELECTOR(KTEXT32_DES), $multiboot_meeting_point
|
---|
[1a67595] | 106 | multiboot_meeting_point:
|
---|
| 107 |
|
---|
[1f5c9c96] | 108 | /*
|
---|
| 109 | * Protected 32-bit. We want to reuse the code-seg descriptor,
|
---|
| 110 | * the Default operand size must not be 1 when entering long mode.
|
---|
| 111 | */
|
---|
| 112 |
|
---|
| 113 | /* Save multiboot arguments */
|
---|
| 114 | movl %eax, multiboot_eax
|
---|
| 115 | movl %ebx, multiboot_ebx
|
---|
[46d26ee] | 116 |
|
---|
[873c681] | 117 | pm_status $status_prot
|
---|
[ac88c93] | 118 |
|
---|
[421c833] | 119 | movl $(INTEL_CPUID_EXTENDED), %eax
|
---|
| 120 | cpuid
|
---|
| 121 | cmp $(INTEL_CPUID_EXTENDED), %eax
|
---|
[4fb6bf36] | 122 | ja extended_cpuid_supported
|
---|
[421c833] | 123 |
|
---|
[873c681] | 124 | pm_error $err_extended_cpuid
|
---|
[4fb6bf36] | 125 |
|
---|
| 126 | extended_cpuid_supported:
|
---|
| 127 |
|
---|
| 128 | movl $(AMD_CPUID_EXTENDED), %eax
|
---|
[ac88c93] | 129 | cpuid
|
---|
[4fb6bf36] | 130 | bt $(AMD_EXT_LONG_MODE), %edx
|
---|
[421c833] | 131 | jc long_mode_supported
|
---|
| 132 |
|
---|
[873c681] | 133 | pm_error $err_long_mode
|
---|
[421c833] | 134 |
|
---|
[ac88c93] | 135 | long_mode_supported:
|
---|
| 136 |
|
---|
[4fb6bf36] | 137 | bt $(AMD_EXT_NOEXECUTE), %edx
|
---|
| 138 | jc noexecute_supported
|
---|
| 139 |
|
---|
[873c681] | 140 | pm_error $err_noexecute
|
---|
[4fb6bf36] | 141 |
|
---|
| 142 | noexecute_supported:
|
---|
| 143 |
|
---|
| 144 | movl $(INTEL_CPUID_STANDARD), %eax
|
---|
| 145 | cpuid
|
---|
| 146 | bt $(INTEL_FXSAVE), %edx
|
---|
[421c833] | 147 | jc fx_supported
|
---|
[4fb6bf36] | 148 |
|
---|
[873c681] | 149 | pm_error $err_fx
|
---|
[4fb6bf36] | 150 |
|
---|
| 151 | fx_supported:
|
---|
| 152 |
|
---|
| 153 | bt $(INTEL_SSE2), %edx
|
---|
[421c833] | 154 | jc sse2_supported
|
---|
[4fb6bf36] | 155 |
|
---|
[873c681] | 156 | pm_error $err_sse2
|
---|
[4fb6bf36] | 157 |
|
---|
| 158 | sse2_supported:
|
---|
[873c681] | 159 |
|
---|
[421c833] | 160 | #include "vesa_prot.inc"
|
---|
[873c681] | 161 |
|
---|
| 162 | pm2_status $status_prot2
|
---|
| 163 |
|
---|
| 164 | /*
|
---|
| 165 | * Enable 64-bit page translation entries - CR4.PAE = 1.
|
---|
| 166 | * Paging is not enabled until after long mode is enabled.
|
---|
| 167 | */
|
---|
[8725fb4] | 168 |
|
---|
[6f878b7] | 169 | movl %cr4, %eax
|
---|
| 170 | btsl $5, %eax
|
---|
| 171 | movl %eax, %cr4
|
---|
[421c833] | 172 |
|
---|
[873c681] | 173 | /* Set up paging tables */
|
---|
[6f878b7] | 174 | leal ptl_0, %eax
|
---|
| 175 | movl %eax, %cr3
|
---|
[c4b3e3e] | 176 |
|
---|
[873c681] | 177 | /* Enable long mode */
|
---|
| 178 | movl $EFER_MSR_NUM, %ecx
|
---|
| 179 | rdmsr /* read EFER */
|
---|
| 180 | btsl $AMD_LME_FLAG, %eax /* set LME = 1 */
|
---|
| 181 | wrmsr
|
---|
[8725fb4] | 182 |
|
---|
[873c681] | 183 | /* Enable paging to activate long mode (set CR0.PG = 1) */
|
---|
[6f878b7] | 184 | movl %cr0, %eax
|
---|
| 185 | btsl $31, %eax
|
---|
| 186 | movl %eax, %cr0
|
---|
[c245372b] | 187 |
|
---|
[873c681] | 188 | /* At this point we are in compatibility mode */
|
---|
[1d3d2cf] | 189 | jmpl $GDT_SELECTOR(KTEXT_DES), $start64
|
---|
[c245372b] | 190 |
|
---|
[873c681] | 191 | /** Print string to EGA display (in light red) and halt.
|
---|
| 192 | *
|
---|
| 193 | * Should be executed from 32 bit protected mode with paging
|
---|
| 194 | * turned off. Stack is not required. This routine is used even
|
---|
| 195 | * if CONFIG_EGA is not enabled. Since we are going to halt the
|
---|
| 196 | * CPU anyway, it is always better to at least try to print
|
---|
| 197 | * some hints.
|
---|
| 198 | *
|
---|
[da52547] | 199 | * @param %esi Pointer to the NULL-terminated string
|
---|
| 200 | * to be print.
|
---|
[873c681] | 201 | *
|
---|
| 202 | */
|
---|
| 203 | pm_error_halt:
|
---|
| 204 | movl $0xb8000, %edi /* base of EGA text mode memory */
|
---|
| 205 | xorl %eax, %eax
|
---|
| 206 |
|
---|
| 207 | /* Read bits 8 - 15 of the cursor address */
|
---|
| 208 | movw $0x3d4, %dx
|
---|
| 209 | movb $0xe, %al
|
---|
| 210 | outb %al, %dx
|
---|
| 211 |
|
---|
| 212 | movw $0x3d5, %dx
|
---|
| 213 | inb %dx, %al
|
---|
| 214 | shl $8, %ax
|
---|
| 215 |
|
---|
| 216 | /* Read bits 0 - 7 of the cursor address */
|
---|
| 217 | movw $0x3d4, %dx
|
---|
| 218 | movb $0xf, %al
|
---|
| 219 | outb %al, %dx
|
---|
| 220 |
|
---|
| 221 | movw $0x3d5, %dx
|
---|
| 222 | inb %dx, %al
|
---|
| 223 |
|
---|
| 224 | /* Sanity check for the cursor on screen */
|
---|
| 225 | cmp $2000, %ax
|
---|
| 226 | jb err_cursor_ok
|
---|
| 227 |
|
---|
| 228 | movw $1998, %ax
|
---|
| 229 |
|
---|
| 230 | err_cursor_ok:
|
---|
| 231 |
|
---|
| 232 | movw %ax, %bx
|
---|
| 233 | shl $1, %eax
|
---|
| 234 | addl %eax, %edi
|
---|
| 235 |
|
---|
| 236 | err_ploop:
|
---|
| 237 | lodsb
|
---|
| 238 |
|
---|
| 239 | cmp $0, %al
|
---|
| 240 | je err_ploop_end
|
---|
| 241 |
|
---|
| 242 | movb $0x0c, %ah /* black background, light red foreground */
|
---|
| 243 | stosw
|
---|
| 244 |
|
---|
| 245 | /* Sanity check for the cursor on the last line */
|
---|
| 246 | inc %bx
|
---|
| 247 | cmp $2000, %bx
|
---|
| 248 | jb err_ploop
|
---|
| 249 |
|
---|
| 250 | /* Scroll the screen (24 rows) */
|
---|
| 251 | movl %esi, %edx
|
---|
| 252 | movl $0xb80a0, %esi
|
---|
| 253 | movl $0xb8000, %edi
|
---|
[22c3444] | 254 | movl $960, %ecx
|
---|
| 255 | rep movsl
|
---|
[873c681] | 256 |
|
---|
| 257 | /* Clear the 24th row */
|
---|
| 258 | xorl %eax, %eax
|
---|
[22c3444] | 259 | movl $40, %ecx
|
---|
| 260 | rep stosl
|
---|
[873c681] | 261 |
|
---|
| 262 | /* Go to row 24 */
|
---|
| 263 | movl %edx, %esi
|
---|
| 264 | movl $0xb8f00, %edi
|
---|
| 265 | movw $1920, %bx
|
---|
| 266 |
|
---|
| 267 | jmp err_ploop
|
---|
| 268 | err_ploop_end:
|
---|
| 269 |
|
---|
| 270 | /* Write bits 8 - 15 of the cursor address */
|
---|
| 271 | movw $0x3d4, %dx
|
---|
| 272 | movb $0xe, %al
|
---|
| 273 | outb %al, %dx
|
---|
| 274 |
|
---|
| 275 | movw $0x3d5, %dx
|
---|
| 276 | movb %bh, %al
|
---|
| 277 | outb %al, %dx
|
---|
| 278 |
|
---|
| 279 | /* Write bits 0 - 7 of the cursor address */
|
---|
| 280 | movw $0x3d4, %dx
|
---|
| 281 | movb $0xf, %al
|
---|
| 282 | outb %al, %dx
|
---|
| 283 |
|
---|
| 284 | movw $0x3d5, %dx
|
---|
| 285 | movb %bl, %al
|
---|
| 286 | outb %al, %dx
|
---|
| 287 |
|
---|
| 288 | cli
|
---|
| 289 | hlt1:
|
---|
| 290 | hlt
|
---|
| 291 | jmp hlt1
|
---|
| 292 |
|
---|
| 293 | /** Print string to EGA display (in light green).
|
---|
| 294 | *
|
---|
| 295 | * Should be called from 32 bit protected mode with paging
|
---|
| 296 | * turned off. A stack space of at least 24 bytes is required,
|
---|
| 297 | * but the function does not establish a stack frame.
|
---|
| 298 | *
|
---|
| 299 | * Macros such as pm_status and pm2_status take care that
|
---|
| 300 | * this function is used only when CONFIG_EGA is enabled
|
---|
| 301 | * and CONFIG_FB is disabled.
|
---|
| 302 | *
|
---|
[da52547] | 303 | * @param %esi Pointer to the NULL-terminated string
|
---|
| 304 | * to be print.
|
---|
[873c681] | 305 | *
|
---|
| 306 | */
|
---|
| 307 | pm_early_puts:
|
---|
| 308 | pushl %eax
|
---|
| 309 | pushl %ebx
|
---|
| 310 | pushl %ecx
|
---|
| 311 | pushl %edx
|
---|
| 312 | pushl %edi
|
---|
| 313 |
|
---|
| 314 | movl $0xb8000, %edi /* base of EGA text mode memory */
|
---|
| 315 | xorl %eax, %eax
|
---|
| 316 |
|
---|
| 317 | /* Read bits 8 - 15 of the cursor address */
|
---|
| 318 | movw $0x3d4, %dx
|
---|
| 319 | movb $0xe, %al
|
---|
| 320 | outb %al, %dx
|
---|
| 321 |
|
---|
| 322 | movw $0x3d5, %dx
|
---|
| 323 | inb %dx, %al
|
---|
| 324 | shl $8, %ax
|
---|
| 325 |
|
---|
| 326 | /* Read bits 0 - 7 of the cursor address */
|
---|
| 327 | movw $0x3d4, %dx
|
---|
| 328 | movb $0xf, %al
|
---|
| 329 | outb %al, %dx
|
---|
| 330 |
|
---|
| 331 | movw $0x3d5, %dx
|
---|
| 332 | inb %dx, %al
|
---|
| 333 |
|
---|
| 334 | /* Sanity check for the cursor on screen */
|
---|
| 335 | cmp $2000, %ax
|
---|
| 336 | jb pm_puts_cursor_ok
|
---|
| 337 |
|
---|
| 338 | movw $1998, %ax
|
---|
| 339 |
|
---|
| 340 | pm_puts_cursor_ok:
|
---|
| 341 |
|
---|
| 342 | movw %ax, %bx
|
---|
| 343 | shl $1, %eax
|
---|
| 344 | addl %eax, %edi
|
---|
| 345 |
|
---|
| 346 | pm_puts_ploop:
|
---|
| 347 | lodsb
|
---|
| 348 |
|
---|
| 349 | cmp $0, %al
|
---|
| 350 | je pm_puts_ploop_end
|
---|
| 351 |
|
---|
| 352 | movb $0x0a, %ah /* black background, light green foreground */
|
---|
| 353 | stosw
|
---|
| 354 |
|
---|
| 355 | /* Sanity check for the cursor on the last line */
|
---|
| 356 | inc %bx
|
---|
| 357 | cmp $2000, %bx
|
---|
| 358 | jb pm_puts_ploop
|
---|
| 359 |
|
---|
| 360 | /* Scroll the screen (24 rows) */
|
---|
| 361 | movl %esi, %edx
|
---|
| 362 | movl $0xb80a0, %esi
|
---|
| 363 | movl $0xb8000, %edi
|
---|
[22c3444] | 364 | movl $960, %ecx
|
---|
| 365 | rep movsl
|
---|
[873c681] | 366 |
|
---|
| 367 | /* Clear the 24th row */
|
---|
| 368 | xorl %eax, %eax
|
---|
[22c3444] | 369 | movl $40, %ecx
|
---|
| 370 | rep stosl
|
---|
[873c681] | 371 |
|
---|
| 372 | /* Go to row 24 */
|
---|
| 373 | movl %edx, %esi
|
---|
| 374 | movl $0xb8f00, %edi
|
---|
| 375 | movw $1920, %bx
|
---|
| 376 |
|
---|
| 377 | jmp pm_puts_ploop
|
---|
| 378 | pm_puts_ploop_end:
|
---|
| 379 |
|
---|
| 380 | /* Write bits 8 - 15 of the cursor address */
|
---|
| 381 | movw $0x3d4, %dx
|
---|
| 382 | movb $0xe, %al
|
---|
| 383 | outb %al, %dx
|
---|
| 384 |
|
---|
| 385 | movw $0x3d5, %dx
|
---|
| 386 | movb %bh, %al
|
---|
| 387 | outb %al, %dx
|
---|
| 388 |
|
---|
| 389 | /* Write bits 0 - 7 of the cursor address */
|
---|
| 390 | movw $0x3d4, %dx
|
---|
| 391 | movb $0xf, %al
|
---|
| 392 | outb %al, %dx
|
---|
| 393 |
|
---|
| 394 | movw $0x3d5, %dx
|
---|
| 395 | movb %bl, %al
|
---|
| 396 | outb %al, %dx
|
---|
| 397 |
|
---|
| 398 | popl %edi
|
---|
| 399 | popl %edx
|
---|
| 400 | popl %ecx
|
---|
| 401 | popl %ebx
|
---|
| 402 | popl %eax
|
---|
| 403 |
|
---|
| 404 | ret
|
---|
| 405 |
|
---|
[6f878b7] | 406 | .code64
|
---|
[873c681] | 407 |
|
---|
| 408 | .macro long_status msg
|
---|
| 409 | pushq %rdi
|
---|
| 410 | movq \msg, %rdi
|
---|
| 411 | call early_puts
|
---|
| 412 | popq %rdi
|
---|
| 413 | .endm
|
---|
| 414 |
|
---|
[6f878b7] | 415 | start64:
|
---|
[873c681] | 416 |
|
---|
| 417 | /*
|
---|
| 418 | * Long mode.
|
---|
| 419 | */
|
---|
| 420 |
|
---|
[c4b3e3e] | 421 | movq $(PA2KA(START_STACK)), %rsp
|
---|
[421c833] | 422 |
|
---|
[873c681] | 423 | /* Create the first stack frame */
|
---|
| 424 | pushq $0
|
---|
| 425 | movq %rsp, %rbp
|
---|
| 426 |
|
---|
| 427 | long_status $status_long
|
---|
| 428 |
|
---|
[1f5c9c96] | 429 | /* Call arch_pre_main(multiboot_eax, multiboot_ebx) */
|
---|
| 430 | movl multiboot_eax, %edi
|
---|
| 431 | movl multiboot_ebx, %esi
|
---|
[17af882] | 432 | callq arch_pre_main
|
---|
| 433 |
|
---|
[873c681] | 434 | long_status $status_main
|
---|
[a1f60f3] | 435 |
|
---|
[873c681] | 436 | /* Call main_bsp() */
|
---|
[17af882] | 437 | callq main_bsp
|
---|
[7cb567cd] | 438 |
|
---|
[873c681] | 439 | /* Not reached */
|
---|
[421c833] | 440 | cli
|
---|
| 441 | hlt0:
|
---|
| 442 | hlt
|
---|
| 443 | jmp hlt0
|
---|
[ac88c93] | 444 |
|
---|
[873c681] | 445 | /** Print string to EGA display.
|
---|
| 446 | *
|
---|
| 447 | * Should be called from long mode (with paging enabled
|
---|
| 448 | * and stack established). This function is ABI compliant
|
---|
| 449 | * (without red-zone).
|
---|
| 450 | *
|
---|
| 451 | * If CONFIG_EGA is undefined or CONFIG_FB is defined
|
---|
| 452 | * then this function does nothing.
|
---|
| 453 | *
|
---|
[da52547] | 454 | * @param %rdi Pointer to the NULL-terminated string
|
---|
| 455 | * to be printed.
|
---|
[873c681] | 456 | *
|
---|
| 457 | */
|
---|
| 458 | early_puts:
|
---|
| 459 |
|
---|
| 460 | #if ((defined(CONFIG_EGA)) && (!defined(CONFIG_FB)))
|
---|
| 461 |
|
---|
| 462 | /* Prologue, save preserved registers */
|
---|
| 463 | pushq %rbp
|
---|
| 464 | movq %rsp, %rbp
|
---|
| 465 | pushq %rbx
|
---|
[ac88c93] | 466 |
|
---|
[873c681] | 467 | movq %rdi, %rsi
|
---|
| 468 | movq $(PA2KA(0xb8000)), %rdi /* base of EGA text mode memory */
|
---|
| 469 | xorq %rax, %rax
|
---|
| 470 |
|
---|
| 471 | /* Read bits 8 - 15 of the cursor address */
|
---|
| 472 | movw $0x3d4, %dx
|
---|
[ac88c93] | 473 | movb $0xe, %al
|
---|
| 474 | outb %al, %dx
|
---|
| 475 |
|
---|
| 476 | movw $0x3d5, %dx
|
---|
| 477 | inb %dx, %al
|
---|
| 478 | shl $8, %ax
|
---|
| 479 |
|
---|
[873c681] | 480 | /* Read bits 0 - 7 of the cursor address */
|
---|
| 481 | movw $0x3d4, %dx
|
---|
[ac88c93] | 482 | movb $0xf, %al
|
---|
| 483 | outb %al, %dx
|
---|
| 484 |
|
---|
| 485 | movw $0x3d5, %dx
|
---|
| 486 | inb %dx, %al
|
---|
| 487 |
|
---|
[873c681] | 488 | /* Sanity check for the cursor on screen */
|
---|
| 489 | cmp $2000, %ax
|
---|
| 490 | jb early_puts_cursor_ok
|
---|
[421c833] | 491 |
|
---|
[873c681] | 492 | movw $1998, %ax
|
---|
[421c833] | 493 |
|
---|
[873c681] | 494 | early_puts_cursor_ok:
|
---|
[ac88c93] | 495 |
|
---|
| 496 | movw %ax, %bx
|
---|
[873c681] | 497 | shl $1, %rax
|
---|
| 498 | addq %rax, %rdi
|
---|
[ac88c93] | 499 |
|
---|
[873c681] | 500 | early_puts_ploop:
|
---|
[ac88c93] | 501 | lodsb
|
---|
[873c681] | 502 |
|
---|
[ac88c93] | 503 | cmp $0, %al
|
---|
[873c681] | 504 | je early_puts_ploop_end
|
---|
| 505 |
|
---|
| 506 | movb $0x0e, %ah /* black background, yellow foreground */
|
---|
[ac88c93] | 507 | stosw
|
---|
[873c681] | 508 |
|
---|
| 509 | /* Sanity check for the cursor on the last line */
|
---|
[421c833] | 510 | inc %bx
|
---|
[873c681] | 511 | cmp $2000, %bx
|
---|
| 512 | jb early_puts_ploop
|
---|
| 513 |
|
---|
| 514 | /* Scroll the screen (24 rows) */
|
---|
| 515 | movq %rsi, %rdx
|
---|
| 516 | movq $(PA2KA(0xb80a0)), %rsi
|
---|
| 517 | movq $(PA2KA(0xb8000)), %rdi
|
---|
[e80329d6] | 518 | movl $480, %ecx
|
---|
[22c3444] | 519 | rep movsq
|
---|
[873c681] | 520 |
|
---|
| 521 | /* Clear the 24th row */
|
---|
[e80329d6] | 522 | xorl %eax, %eax
|
---|
| 523 | movl $20, %ecx
|
---|
[22c3444] | 524 | rep stosq
|
---|
[873c681] | 525 |
|
---|
| 526 | /* Go to row 24 */
|
---|
| 527 | movq %rdx, %rsi
|
---|
| 528 | movq $(PA2KA(0xb8f00)), %rdi
|
---|
| 529 | movw $1920, %bx
|
---|
| 530 |
|
---|
| 531 | jmp early_puts_ploop
|
---|
| 532 | early_puts_ploop_end:
|
---|
| 533 |
|
---|
| 534 | /* Write bits 8 - 15 of the cursor address */
|
---|
| 535 | movw $0x3d4, %dx
|
---|
[ac88c93] | 536 | movb $0xe, %al
|
---|
| 537 | outb %al, %dx
|
---|
| 538 |
|
---|
| 539 | movw $0x3d5, %dx
|
---|
| 540 | movb %bh, %al
|
---|
| 541 | outb %al, %dx
|
---|
| 542 |
|
---|
[873c681] | 543 | /* Write bits 0 - 7 of the cursor address */
|
---|
| 544 | movw $0x3d4, %dx
|
---|
[ac88c93] | 545 | movb $0xf, %al
|
---|
| 546 | outb %al, %dx
|
---|
| 547 |
|
---|
| 548 | movw $0x3d5, %dx
|
---|
| 549 | movb %bl, %al
|
---|
| 550 | outb %al, %dx
|
---|
[421c833] | 551 |
|
---|
[873c681] | 552 | /* Epilogue, restore preserved registers */
|
---|
| 553 | popq %rbx
|
---|
| 554 | leave
|
---|
| 555 |
|
---|
| 556 | #endif
|
---|
| 557 |
|
---|
| 558 | ret
|
---|
[421c833] | 559 |
|
---|
| 560 | #include "vesa_real.inc"
|
---|
[19077a5] | 561 |
|
---|
| 562 | .section K_INI_PTLS, "aw", @progbits
|
---|
| 563 |
|
---|
[873c681] | 564 | /** Generate initial page table contents.
|
---|
| 565 | *
|
---|
| 566 | * @param cnt Number of entries to generate. Must be multiple of 8.
|
---|
| 567 | * @param g Number of GB that will be added to the mapping.
|
---|
| 568 | *
|
---|
| 569 | */
|
---|
[a1f60f3] | 570 | .macro ptl2gen cnt g
|
---|
[64f6ef04] | 571 | .if \cnt
|
---|
| 572 | ptl2gen "\cnt - 8" \g
|
---|
| 573 | .quad ((\cnt - 8) * 0x200000) + (\g * 1024 * 1024 * 1024) | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
|
---|
| 574 | .quad ((\cnt - 7) * 0x200000) + (\g * 1024 * 1024 * 1024) | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
|
---|
| 575 | .quad ((\cnt - 6) * 0x200000) + (\g * 1024 * 1024 * 1024) | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
|
---|
| 576 | .quad ((\cnt - 5) * 0x200000) + (\g * 1024 * 1024 * 1024) | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
|
---|
| 577 | .quad ((\cnt - 4) * 0x200000) + (\g * 1024 * 1024 * 1024) | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
|
---|
| 578 | .quad ((\cnt - 3) * 0x200000) + (\g * 1024 * 1024 * 1024) | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
|
---|
| 579 | .quad ((\cnt - 2) * 0x200000) + (\g * 1024 * 1024 * 1024) | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
|
---|
| 580 | .quad ((\cnt - 1) * 0x200000) + (\g * 1024 * 1024 * 1024) | (PTL_WRITABLE | PTL_PRESENT | PTL_2MB_PAGE)
|
---|
| 581 | .endif
|
---|
[19077a5] | 582 | .endm
|
---|
| 583 |
|
---|
[873c681] | 584 | /* Page table for pages in the 1st gigabyte. */
|
---|
[1141c1a] | 585 | .align 4096
|
---|
[a1f60f3] | 586 | ptl_2_0g:
|
---|
[19077a5] | 587 | ptl2gen 512 0
|
---|
| 588 |
|
---|
[873c681] | 589 | /* Page table for pages in the 2nd gigabyte. */
|
---|
[19077a5] | 590 | .align 4096
|
---|
| 591 | ptl_2_1g:
|
---|
| 592 | ptl2gen 512 1
|
---|
| 593 |
|
---|
[873c681] | 594 | /* Page table for pages in the 3rd gigabyte. */
|
---|
[19077a5] | 595 | .align 4096
|
---|
| 596 | ptl_2_2g:
|
---|
| 597 | ptl2gen 512 2
|
---|
| 598 |
|
---|
[873c681] | 599 | /* Page table for pages in the 4th gigabyte. */
|
---|
[19077a5] | 600 | .align 4096
|
---|
| 601 | ptl_2_3g:
|
---|
| 602 | ptl2gen 512 3
|
---|
[b9e97fb] | 603 |
|
---|
[873c681] | 604 | /* Page table for pages in the 5th gigabyte. */
|
---|
[64f6ef04] | 605 | .align 4096
|
---|
| 606 | ptl_2_4g:
|
---|
[fef504a] | 607 | ptl2gen 512 4
|
---|
[64f6ef04] | 608 |
|
---|
[873c681] | 609 | /* Page table for pages in the 6th gigabyte. */
|
---|
[64f6ef04] | 610 | .align 4096
|
---|
| 611 | ptl_2_5g:
|
---|
[fef504a] | 612 | ptl2gen 512 5
|
---|
[64f6ef04] | 613 |
|
---|
[873c681] | 614 | /* Page table for pages in the 7th gigabyte. */
|
---|
[64f6ef04] | 615 | .align 4096
|
---|
| 616 | ptl_2_6g:
|
---|
[fef504a] | 617 | ptl2gen 512 6
|
---|
[64f6ef04] | 618 |
|
---|
[873c681] | 619 | /* Page table for pages in the 8th gigabyte. */
|
---|
[64f6ef04] | 620 | .align 4096
|
---|
| 621 | ptl_2_7g:
|
---|
[fef504a] | 622 | ptl2gen 512 7
|
---|
[64f6ef04] | 623 |
|
---|
[6f878b7] | 624 | .align 4096
|
---|
| 625 | ptl_1:
|
---|
[873c681] | 626 | /* Identity mapping for [0; 8G) */
|
---|
[19077a5] | 627 | .quad ptl_2_0g + (PTL_WRITABLE | PTL_PRESENT)
|
---|
[a1f60f3] | 628 | .quad ptl_2_1g + (PTL_WRITABLE | PTL_PRESENT)
|
---|
[19077a5] | 629 | .quad ptl_2_2g + (PTL_WRITABLE | PTL_PRESENT)
|
---|
| 630 | .quad ptl_2_3g + (PTL_WRITABLE | PTL_PRESENT)
|
---|
[64f6ef04] | 631 | .quad ptl_2_4g + (PTL_WRITABLE | PTL_PRESENT)
|
---|
| 632 | .quad ptl_2_5g + (PTL_WRITABLE | PTL_PRESENT)
|
---|
| 633 | .quad ptl_2_6g + (PTL_WRITABLE | PTL_PRESENT)
|
---|
| 634 | .quad ptl_2_7g + (PTL_WRITABLE | PTL_PRESENT)
|
---|
[17af882] | 635 | .fill 502, 8, 0
|
---|
| 636 | /* Mapping of [0; 2G) at -2G */
|
---|
| 637 | .quad ptl_2_0g + (PTL_WRITABLE | PTL_PRESENT)
|
---|
| 638 | .quad ptl_2_1g + (PTL_WRITABLE | PTL_PRESENT)
|
---|
| 639 |
|
---|
[421c833] | 640 |
|
---|
[6f878b7] | 641 | .align 4096
|
---|
| 642 | .global ptl_0
|
---|
| 643 | ptl_0:
|
---|
| 644 | .quad ptl_1 + (PTL_WRITABLE | PTL_PRESENT)
|
---|
[17af882] | 645 | .fill 510, 8, 0
|
---|
[6f878b7] | 646 | .quad ptl_1 + (PTL_WRITABLE | PTL_PRESENT)
|
---|
| 647 |
|
---|
[19077a5] | 648 | .section K_DATA_START, "aw", @progbits
|
---|
| 649 |
|
---|
[f66c203d] | 650 | .global bootstrap_idtr
|
---|
| 651 | bootstrap_idtr:
|
---|
| 652 | .word 0
|
---|
| 653 | .long 0
|
---|
| 654 |
|
---|
[1f5c9c96] | 655 | .global bootstrap_gdtr
|
---|
[46d26ee] | 656 | bootstrap_gdtr:
|
---|
[1d3d2cf] | 657 | .word GDT_SELECTOR(GDT_ITEMS)
|
---|
[1a67595] | 658 | .long KA2PA(gdt)
|
---|
[46d26ee] | 659 |
|
---|
[1f5c9c96] | 660 | .global multiboot_eax
|
---|
| 661 | multiboot_eax:
|
---|
[46d26ee] | 662 | .long 0
|
---|
| 663 |
|
---|
[1f5c9c96] | 664 | .global multiboot_ebx
|
---|
| 665 | multiboot_ebx:
|
---|
[46d26ee] | 666 | .long 0
|
---|
[ac88c93] | 667 |
|
---|
[873c681] | 668 | err_extended_cpuid:
|
---|
[fccc236] | 669 | .asciz "Error: Extended CPUID not supported -- CPU is not 64-bit. System halted."
|
---|
[873c681] | 670 | err_long_mode:
|
---|
[fccc236] | 671 | .asciz "Error: 64-bit long mode not supported. System halted."
|
---|
[873c681] | 672 | err_noexecute:
|
---|
[fccc236] | 673 | .asciz "Error: No-execute pages not supported. System halted."
|
---|
[873c681] | 674 | err_fx:
|
---|
[fccc236] | 675 | .asciz "Error: FXSAVE/FXRESTORE instructions not supported. System halted."
|
---|
[873c681] | 676 | err_sse2:
|
---|
[fccc236] | 677 | .asciz "Error: SSE2 instructions not supported. System halted."
|
---|
[873c681] | 678 |
|
---|
| 679 | status_prot:
|
---|
| 680 | .asciz "[prot] "
|
---|
| 681 | status_vesa_copy:
|
---|
| 682 | .asciz "[vesa_copy] "
|
---|
[1f5c9c96] | 683 | status_multiboot_cmdline:
|
---|
| 684 | .asciz "[multiboot_cmdline] "
|
---|
[873c681] | 685 | status_vesa_real:
|
---|
| 686 | .asciz "[vesa_real] "
|
---|
| 687 | status_prot2:
|
---|
| 688 | .asciz "[prot2] "
|
---|
| 689 | status_long:
|
---|
| 690 | .asciz "[long] "
|
---|
| 691 | status_main:
|
---|
| 692 | .asciz "[main] "
|
---|