| 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/pm.h>
|
|---|
| 31 | #include <arch/cpuid.h>
|
|---|
| 32 | #include <genarch/multiboot/multiboot2.h>
|
|---|
| 33 |
|
|---|
| 34 | #define START_STACK (BOOT_OFFSET - BOOT_STACK_SIZE)
|
|---|
| 35 |
|
|---|
| 36 | .section K_TEXT_START, "ax"
|
|---|
| 37 |
|
|---|
| 38 | .code32
|
|---|
| 39 |
|
|---|
| 40 | .align 8
|
|---|
| 41 | .global multiboot2_image_start
|
|---|
| 42 | multiboot2_header_start:
|
|---|
| 43 | .long MULTIBOOT2_HEADER_MAGIC
|
|---|
| 44 | .long MULTIBOOT2_HEADER_ARCH_I386
|
|---|
| 45 | .long multiboot2_header_end - multiboot2_header_start
|
|---|
| 46 | .long -(MULTIBOOT2_HEADER_MAGIC + MULTIBOOT2_HEADER_ARCH_I386 + (multiboot2_header_end - multiboot2_header_start))
|
|---|
| 47 |
|
|---|
| 48 | /* Information request tag */
|
|---|
| 49 | tag_info_req_start:
|
|---|
| 50 | .word MULTIBOOT2_TAG_INFO_REQ
|
|---|
| 51 | .word MULTIBOOT2_FLAGS_REQUIRED
|
|---|
| 52 | .long tag_info_req_end - tag_info_req_start
|
|---|
| 53 | .long MULTIBOOT2_TAG_MODULE
|
|---|
| 54 | .long MULTIBOOT2_TAG_MEMMAP
|
|---|
| 55 | .long MULTIBOOT2_TAG_FBINFO
|
|---|
| 56 | tag_info_req_end:
|
|---|
| 57 |
|
|---|
| 58 | /* Address tag */
|
|---|
| 59 | tag_address_start:
|
|---|
| 60 | .word MULTIBOOT2_TAG_ADDRESS
|
|---|
| 61 | .word MULTIBOOT2_FLAGS_REQUIRED
|
|---|
| 62 | .long tag_address_end - tag_address_start
|
|---|
| 63 | .long multiboot2_header_start
|
|---|
| 64 | .long unmapped_ktext_start
|
|---|
| 65 | .long 0
|
|---|
| 66 | .long 0
|
|---|
| 67 | tag_address_end:
|
|---|
| 68 |
|
|---|
| 69 | /* Entry address tag */
|
|---|
| 70 | tag_entry_address_start:
|
|---|
| 71 | .word MULTIBOOT2_TAG_ENTRY_ADDRESS
|
|---|
| 72 | .word MULTIBOOT2_FLAGS_REQUIRED
|
|---|
| 73 | .long tag_entry_address_end - tag_entry_address_start
|
|---|
| 74 | .long multiboot2_image_start
|
|---|
| 75 | tag_entry_address_end:
|
|---|
| 76 |
|
|---|
| 77 | /* Flags tag */
|
|---|
| 78 | tag_flags_start:
|
|---|
| 79 | .word MULTIBOOT2_TAG_FLAGS
|
|---|
| 80 | .word MULTIBOOT2_FLAGS_REQUIRED
|
|---|
| 81 | .long tag_flags_end - tag_flags_start
|
|---|
| 82 | .long MULTIBOOT2_FLAGS_CONSOLE
|
|---|
| 83 | tag_flags_end:
|
|---|
| 84 |
|
|---|
| 85 | /* Framebuffer tag */
|
|---|
| 86 | tag_framebuffer_start:
|
|---|
| 87 | .word MULTIBOOT2_TAG_FRAMEBUFFER
|
|---|
| 88 | .word MULTIBOOT2_FLAGS_REQUIRED
|
|---|
| 89 | .long tag_framebuffer_end - tag_framebuffer_start
|
|---|
| 90 | .long CONFIG_BFB_WIDTH
|
|---|
| 91 | .long CONFIG_BFB_HEIGHT
|
|---|
| 92 | .long CONFIG_BFB_BPP
|
|---|
| 93 | tag_framebuffer_end:
|
|---|
| 94 |
|
|---|
| 95 | /* Module alignment tag */
|
|---|
| 96 | tag_module_align_start:
|
|---|
| 97 | .word MULTIBOOT2_TAG_MODULE_ALIGN
|
|---|
| 98 | .word MULTIBOOT2_FLAGS_REQUIRED
|
|---|
| 99 | .long tag_module_align_end - tag_module_align_start
|
|---|
| 100 | .long 0
|
|---|
| 101 | tag_module_align_end:
|
|---|
| 102 |
|
|---|
| 103 | /* Tag terminator */
|
|---|
| 104 | tag_terminator_start:
|
|---|
| 105 | .word MULTIBOOT2_TAG_TERMINATOR
|
|---|
| 106 | .word MULTIBOOT2_FLAGS_REQUIRED
|
|---|
| 107 | .long tag_terminator_end - tag_terminator_start
|
|---|
| 108 | tag_terminator_end:
|
|---|
| 109 | multiboot2_header_end:
|
|---|
| 110 |
|
|---|
| 111 | multiboot2_image_start:
|
|---|
| 112 | cld
|
|---|
| 113 |
|
|---|
| 114 | /* Initialize stack pointer */
|
|---|
| 115 | movl $START_STACK, %esp
|
|---|
| 116 |
|
|---|
| 117 | /* Initialize Global Descriptor Table register */
|
|---|
| 118 | lgdtl bootstrap_gdtr
|
|---|
| 119 |
|
|---|
| 120 | /* Kernel data + stack */
|
|---|
| 121 | movw $GDT_SELECTOR(KDATA_DES), %cx
|
|---|
| 122 | movw %cx, %es
|
|---|
| 123 | movw %cx, %fs
|
|---|
| 124 | movw %cx, %gs
|
|---|
| 125 | movw %cx, %ds
|
|---|
| 126 | movw %cx, %ss
|
|---|
| 127 |
|
|---|
| 128 | jmpl $GDT_SELECTOR(KTEXT_DES), $multiboot2_meeting_point
|
|---|
| 129 | multiboot2_meeting_point:
|
|---|
| 130 |
|
|---|
| 131 | /* Save multiboot arguments */
|
|---|
| 132 | movl %eax, multiboot_eax
|
|---|
| 133 | movl %ebx, multiboot_ebx
|
|---|
| 134 |
|
|---|
| 135 | #ifndef PROCESSOR_i486
|
|---|
| 136 |
|
|---|
| 137 | movl $(INTEL_CPUID_LEVEL), %eax
|
|---|
| 138 | cpuid
|
|---|
| 139 | cmp $0x0, %eax /* any function > 0? */
|
|---|
| 140 | jbe pse_unsupported
|
|---|
| 141 |
|
|---|
| 142 | movl $(INTEL_CPUID_STANDARD), %eax
|
|---|
| 143 | cpuid
|
|---|
| 144 | bt $(INTEL_PSE), %edx
|
|---|
| 145 | jnc pse_unsupported
|
|---|
| 146 |
|
|---|
| 147 | /* Map kernel and turn paging on */
|
|---|
| 148 | call map_kernel_pse
|
|---|
| 149 | jmp stack_init
|
|---|
| 150 |
|
|---|
| 151 | #endif /* PROCESSOR_i486 */
|
|---|
| 152 |
|
|---|
| 153 | pse_unsupported:
|
|---|
| 154 |
|
|---|
| 155 | /* Map kernel and turn paging on */
|
|---|
| 156 | call map_kernel_non_pse
|
|---|
| 157 |
|
|---|
| 158 | stack_init:
|
|---|
| 159 |
|
|---|
| 160 | /* Create the first stack frame */
|
|---|
| 161 | pushl $0
|
|---|
| 162 | movl %esp, %ebp
|
|---|
| 163 |
|
|---|
| 164 | /* Call arch_pre_main(multiboot_eax, multiboot_ebx) */
|
|---|
| 165 | pushl multiboot_ebx
|
|---|
| 166 | pushl multiboot_eax
|
|---|
| 167 | call arch_pre_main
|
|---|
| 168 |
|
|---|
| 169 | /* Call main_bsp() */
|
|---|
| 170 | call main_bsp
|
|---|
| 171 |
|
|---|
| 172 | /* Not reached */
|
|---|
| 173 | cli
|
|---|
| 174 | hlt0:
|
|---|
| 175 | hlt
|
|---|
| 176 | jmp hlt0
|
|---|