[1f5c9c96] | 1 | /*
|
---|
| 2 | * Copyright (c) 2011 Martin Decky
|
---|
| 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 |
|
---|
| 29 | #include <arch/boot/boot.h>
|
---|
| 30 | #include <arch/mm/page.h>
|
---|
| 31 | #include <arch/pm.h>
|
---|
| 32 | #include <arch/cpuid.h>
|
---|
| 33 | #include <arch/cpu.h>
|
---|
| 34 | #include <genarch/multiboot/multiboot2.h>
|
---|
| 35 |
|
---|
| 36 | #define START_STACK (BOOT_OFFSET - BOOT_STACK_SIZE)
|
---|
| 37 |
|
---|
| 38 | .section K_TEXT_START, "ax"
|
---|
| 39 |
|
---|
| 40 | .code32
|
---|
| 41 |
|
---|
| 42 | .align 8
|
---|
| 43 | .global multiboot2_image_start
|
---|
| 44 | multiboot2_header_start:
|
---|
| 45 | .long MULTIBOOT2_HEADER_MAGIC
|
---|
| 46 | .long MULTIBOOT2_HEADER_ARCH_I386
|
---|
| 47 | .long multiboot2_header_end - multiboot2_header_start
|
---|
| 48 | .long -(MULTIBOOT2_HEADER_MAGIC + MULTIBOOT2_HEADER_ARCH_I386 + (multiboot2_header_end - multiboot2_header_start))
|
---|
| 49 |
|
---|
| 50 | /* Information request tag */
|
---|
| 51 | tag_info_req_start:
|
---|
| 52 | .word MULTIBOOT2_TAG_INFO_REQ
|
---|
| 53 | .word MULTIBOOT2_FLAGS_REQUIRED
|
---|
| 54 | .long tag_info_req_end - tag_info_req_start
|
---|
| 55 | .long MULTIBOOT2_TAG_MODULE
|
---|
| 56 | .long MULTIBOOT2_TAG_MEMMAP
|
---|
[40898df] | 57 | #ifdef CONFIG_FB
|
---|
[1f5c9c96] | 58 | .long MULTIBOOT2_TAG_FBINFO
|
---|
[40898df] | 59 | #endif
|
---|
[1f5c9c96] | 60 | tag_info_req_end:
|
---|
| 61 |
|
---|
| 62 | /* Address tag */
|
---|
| 63 | tag_address_start:
|
---|
| 64 | .word MULTIBOOT2_TAG_ADDRESS
|
---|
| 65 | .word MULTIBOOT2_FLAGS_REQUIRED
|
---|
| 66 | .long tag_address_end - tag_address_start
|
---|
| 67 | .long multiboot2_header_start
|
---|
| 68 | .long unmapped_ktext_start
|
---|
| 69 | .long 0
|
---|
| 70 | .long 0
|
---|
| 71 | tag_address_end:
|
---|
| 72 |
|
---|
| 73 | /* Entry address tag */
|
---|
| 74 | tag_entry_address_start:
|
---|
| 75 | .word MULTIBOOT2_TAG_ENTRY_ADDRESS
|
---|
| 76 | .word MULTIBOOT2_FLAGS_REQUIRED
|
---|
| 77 | .long tag_entry_address_end - tag_entry_address_start
|
---|
| 78 | .long multiboot2_image_start
|
---|
| 79 | tag_entry_address_end:
|
---|
| 80 |
|
---|
| 81 | /* Flags tag */
|
---|
| 82 | tag_flags_start:
|
---|
| 83 | .word MULTIBOOT2_TAG_FLAGS
|
---|
| 84 | .word MULTIBOOT2_FLAGS_REQUIRED
|
---|
| 85 | .long tag_flags_end - tag_flags_start
|
---|
| 86 | .long MULTIBOOT2_FLAGS_CONSOLE
|
---|
| 87 | tag_flags_end:
|
---|
| 88 |
|
---|
[40898df] | 89 | #ifdef CONFIG_FB
|
---|
[1f5c9c96] | 90 | /* Framebuffer tag */
|
---|
| 91 | tag_framebuffer_start:
|
---|
| 92 | .word MULTIBOOT2_TAG_FRAMEBUFFER
|
---|
| 93 | .word MULTIBOOT2_FLAGS_REQUIRED
|
---|
| 94 | .long tag_framebuffer_end - tag_framebuffer_start
|
---|
| 95 | .long CONFIG_BFB_WIDTH
|
---|
| 96 | .long CONFIG_BFB_HEIGHT
|
---|
| 97 | .long CONFIG_BFB_BPP
|
---|
| 98 | tag_framebuffer_end:
|
---|
[40898df] | 99 | #endif
|
---|
[1f5c9c96] | 100 |
|
---|
| 101 | /* Module alignment tag */
|
---|
| 102 | tag_module_align_start:
|
---|
| 103 | .word MULTIBOOT2_TAG_MODULE_ALIGN
|
---|
| 104 | .word MULTIBOOT2_FLAGS_REQUIRED
|
---|
| 105 | .long tag_module_align_end - tag_module_align_start
|
---|
| 106 | .long 0
|
---|
| 107 | tag_module_align_end:
|
---|
| 108 |
|
---|
| 109 | /* Tag terminator */
|
---|
| 110 | tag_terminator_start:
|
---|
| 111 | .word MULTIBOOT2_TAG_TERMINATOR
|
---|
| 112 | .word MULTIBOOT2_FLAGS_REQUIRED
|
---|
| 113 | .long tag_terminator_end - tag_terminator_start
|
---|
| 114 | tag_terminator_end:
|
---|
| 115 | multiboot2_header_end:
|
---|
| 116 |
|
---|
| 117 | multiboot2_image_start:
|
---|
| 118 | cld
|
---|
| 119 |
|
---|
| 120 | /* Initialize stack pointer */
|
---|
| 121 | movl $START_STACK, %esp
|
---|
| 122 |
|
---|
| 123 | /* Initialize Global Descriptor Table register */
|
---|
| 124 | lgdtl bootstrap_gdtr
|
---|
| 125 |
|
---|
| 126 | /* Kernel data + stack */
|
---|
| 127 | movw $GDT_SELECTOR(KDATA_DES), %cx
|
---|
| 128 | movw %cx, %es
|
---|
| 129 | movw %cx, %ds
|
---|
| 130 | movw %cx, %ss
|
---|
| 131 |
|
---|
| 132 | /*
|
---|
| 133 | * Simics seems to remove hidden part of GS on entering user mode
|
---|
| 134 | * when _visible_ part of GS does not point to user-mode segment.
|
---|
| 135 | */
|
---|
| 136 | movw $GDT_SELECTOR(UDATA_DES), %cx
|
---|
| 137 | movw %cx, %fs
|
---|
| 138 | movw %cx, %gs
|
---|
| 139 |
|
---|
| 140 | jmpl $GDT_SELECTOR(KTEXT32_DES), $multiboot2_meeting_point
|
---|
| 141 | multiboot2_meeting_point:
|
---|
| 142 |
|
---|
| 143 | /*
|
---|
| 144 | * Protected 32-bit. We want to reuse the code-seg descriptor,
|
---|
| 145 | * the Default operand size must not be 1 when entering long mode.
|
---|
| 146 | */
|
---|
| 147 |
|
---|
| 148 | /* Save multiboot arguments */
|
---|
| 149 | movl %eax, multiboot_eax
|
---|
| 150 | movl %ebx, multiboot_ebx
|
---|
| 151 |
|
---|
| 152 | movl $(INTEL_CPUID_EXTENDED), %eax
|
---|
| 153 | cpuid
|
---|
| 154 | cmp $(INTEL_CPUID_EXTENDED), %eax
|
---|
| 155 | ja extended_cpuid_supported
|
---|
| 156 |
|
---|
| 157 | jmp pm_error_halt
|
---|
| 158 |
|
---|
| 159 | extended_cpuid_supported:
|
---|
| 160 |
|
---|
| 161 | movl $(AMD_CPUID_EXTENDED), %eax
|
---|
| 162 | cpuid
|
---|
| 163 | bt $(AMD_EXT_LONG_MODE), %edx
|
---|
| 164 | jc long_mode_supported
|
---|
| 165 |
|
---|
| 166 | jmp pm_error_halt
|
---|
| 167 |
|
---|
| 168 | long_mode_supported:
|
---|
| 169 |
|
---|
| 170 | bt $(AMD_EXT_NOEXECUTE), %edx
|
---|
| 171 | jc noexecute_supported
|
---|
| 172 |
|
---|
| 173 | jmp pm_error_halt
|
---|
| 174 |
|
---|
| 175 | noexecute_supported:
|
---|
| 176 |
|
---|
| 177 | movl $(INTEL_CPUID_STANDARD), %eax
|
---|
| 178 | cpuid
|
---|
| 179 | bt $(INTEL_FXSAVE), %edx
|
---|
| 180 | jc fx_supported
|
---|
| 181 |
|
---|
| 182 | jmp pm_error_halt
|
---|
| 183 |
|
---|
| 184 | fx_supported:
|
---|
| 185 |
|
---|
| 186 | bt $(INTEL_SSE2), %edx
|
---|
| 187 | jc sse2_supported
|
---|
| 188 |
|
---|
| 189 | jmp pm_error_halt
|
---|
| 190 |
|
---|
| 191 | sse2_supported:
|
---|
| 192 |
|
---|
| 193 | /*
|
---|
| 194 | * Enable 64-bit page translation entries - CR4.PAE = 1.
|
---|
| 195 | * Paging is not enabled until after long mode is enabled.
|
---|
| 196 | */
|
---|
| 197 |
|
---|
| 198 | movl %cr4, %eax
|
---|
| 199 | btsl $5, %eax
|
---|
| 200 | movl %eax, %cr4
|
---|
| 201 |
|
---|
| 202 | /* Set up paging tables */
|
---|
| 203 | leal ptl_0, %eax
|
---|
| 204 | movl %eax, %cr3
|
---|
| 205 |
|
---|
| 206 | /* Enable long mode */
|
---|
| 207 | movl $EFER_MSR_NUM, %ecx
|
---|
| 208 | rdmsr /* read EFER */
|
---|
| 209 | btsl $AMD_LME_FLAG, %eax /* set LME = 1 */
|
---|
| 210 | wrmsr
|
---|
| 211 |
|
---|
| 212 | /* Enable paging to activate long mode (set CR0.PG = 1) */
|
---|
| 213 | movl %cr0, %eax
|
---|
| 214 | btsl $31, %eax
|
---|
| 215 | movl %eax, %cr0
|
---|
| 216 |
|
---|
| 217 | /* At this point we are in compatibility mode */
|
---|
| 218 | jmpl $GDT_SELECTOR(KTEXT_DES), $start64
|
---|
| 219 |
|
---|
| 220 | pm_error_halt:
|
---|
| 221 | cli
|
---|
| 222 | hlt1:
|
---|
| 223 | hlt
|
---|
| 224 | jmp hlt1
|
---|
| 225 |
|
---|
| 226 | .code64
|
---|
| 227 |
|
---|
| 228 | start64:
|
---|
| 229 |
|
---|
| 230 | /*
|
---|
| 231 | * Long mode.
|
---|
| 232 | */
|
---|
| 233 |
|
---|
| 234 | movq $(PA2KA(START_STACK)), %rsp
|
---|
| 235 |
|
---|
| 236 | /* Create the first stack frame */
|
---|
| 237 | pushq $0
|
---|
| 238 | movq %rsp, %rbp
|
---|
| 239 |
|
---|
| 240 | /* Call arch_pre_main(multiboot_eax, multiboot_ebx) */
|
---|
| 241 | xorq %rdi, %rdi
|
---|
| 242 | movl multiboot_eax, %edi
|
---|
| 243 | xorq %rsi, %rsi
|
---|
| 244 | movl multiboot_ebx, %esi
|
---|
| 245 |
|
---|
| 246 | movabsq $arch_pre_main, %rax
|
---|
| 247 | callq *%rax
|
---|
| 248 |
|
---|
| 249 | /* Call main_bsp() */
|
---|
| 250 | movabsq $main_bsp, %rax
|
---|
| 251 | call *%rax
|
---|
| 252 |
|
---|
| 253 | /* Not reached */
|
---|
| 254 | cli
|
---|
| 255 | hlt0:
|
---|
| 256 | hlt
|
---|
| 257 | jmp hlt0
|
---|