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