| 1 | #
|
|---|
| 2 | # Copyright (c) 2001-2004 Jakub Jermar
|
|---|
| 3 | # Copyright (c) 2005-2006 Martin Decky
|
|---|
| 4 | # All rights reserved.
|
|---|
| 5 | #
|
|---|
| 6 | # Redistribution and use in source and binary forms, with or without
|
|---|
| 7 | # modification, are permitted provided that the following conditions
|
|---|
| 8 | # are met:
|
|---|
| 9 | #
|
|---|
| 10 | # - Redistributions of source code must retain the above copyright
|
|---|
| 11 | # notice, this list of conditions and the following disclaimer.
|
|---|
| 12 | # - Redistributions in binary form must reproduce the above copyright
|
|---|
| 13 | # notice, this list of conditions and the following disclaimer in the
|
|---|
| 14 | # documentation and/or other materials provided with the distribution.
|
|---|
| 15 | # - The name of the author may not be used to endorse or promote products
|
|---|
| 16 | # derived from this software without specific prior written permission.
|
|---|
| 17 | #
|
|---|
| 18 | # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 19 | # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 20 | # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 21 | # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 22 | # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 23 | # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 24 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 25 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 26 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 27 | # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 28 | #
|
|---|
| 29 |
|
|---|
| 30 | #include <arch/boot/boot.h>
|
|---|
| 31 | #include <arch/boot/memmap.h>
|
|---|
| 32 | #include <arch/mm/page.h>
|
|---|
| 33 | #include <arch/pm.h>
|
|---|
| 34 | #include <arch/cpuid.h>
|
|---|
| 35 |
|
|---|
| 36 | #define START_STACK (BOOT_OFFSET - BOOT_STACK_SIZE)
|
|---|
| 37 |
|
|---|
| 38 | .section K_TEXT_START, "ax"
|
|---|
| 39 |
|
|---|
| 40 | .code32
|
|---|
| 41 | .align 4
|
|---|
| 42 | .global multiboot_image_start
|
|---|
| 43 | multiboot_header:
|
|---|
| 44 | .long MULTIBOOT_HEADER_MAGIC
|
|---|
| 45 | .long MULTIBOOT_HEADER_FLAGS
|
|---|
| 46 | .long -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS) # checksum
|
|---|
| 47 | .long multiboot_header
|
|---|
| 48 | .long unmapped_ktext_start
|
|---|
| 49 | .long 0
|
|---|
| 50 | .long 0
|
|---|
| 51 | .long multiboot_image_start
|
|---|
| 52 |
|
|---|
| 53 | multiboot_image_start:
|
|---|
| 54 | cld
|
|---|
| 55 | movl $START_STACK, %esp # initialize stack pointer
|
|---|
| 56 | lgdt KA2PA(bootstrap_gdtr) # initialize Global Descriptor Table register
|
|---|
| 57 |
|
|---|
| 58 | movw $gdtselector(KDATA_DES), %cx
|
|---|
| 59 | movw %cx, %es
|
|---|
| 60 | movw %cx, %fs
|
|---|
| 61 | movw %cx, %gs
|
|---|
| 62 | movw %cx, %ds # kernel data + stack
|
|---|
| 63 | movw %cx, %ss
|
|---|
| 64 |
|
|---|
| 65 | jmpl $gdtselector(KTEXT_DES), $multiboot_meeting_point
|
|---|
| 66 | multiboot_meeting_point:
|
|---|
| 67 |
|
|---|
| 68 | movl %eax, grub_eax # save parameters from GRUB
|
|---|
| 69 | movl %ebx, grub_ebx
|
|---|
| 70 |
|
|---|
| 71 | movl $(INTEL_CPUID_LEVEL), %eax
|
|---|
| 72 | cpuid
|
|---|
| 73 | cmp $0x0, %eax # any function > 0?
|
|---|
| 74 | jbe pse_unsupported
|
|---|
| 75 |
|
|---|
| 76 | movl $(INTEL_CPUID_STANDARD), %eax
|
|---|
| 77 | cpuid
|
|---|
| 78 | bt $(INTEL_PSE), %edx
|
|---|
| 79 | jc pse_supported
|
|---|
| 80 |
|
|---|
| 81 | pse_unsupported:
|
|---|
| 82 | movl $pse_msg, %esi
|
|---|
| 83 | jmp error_halt
|
|---|
| 84 |
|
|---|
| 85 | pse_supported:
|
|---|
| 86 |
|
|---|
| 87 | #include "vesa_prot.inc"
|
|---|
| 88 |
|
|---|
| 89 | # map kernel and turn paging on
|
|---|
| 90 | call map_kernel
|
|---|
| 91 |
|
|---|
| 92 | # call arch_pre_main(grub_eax, grub_ebx)
|
|---|
| 93 | pushl grub_ebx
|
|---|
| 94 | pushl grub_eax
|
|---|
| 95 | call arch_pre_main
|
|---|
| 96 |
|
|---|
| 97 | # Create the first stack frame
|
|---|
| 98 | pushl $0
|
|---|
| 99 | movl %esp, %ebp
|
|---|
| 100 |
|
|---|
| 101 | call main_bsp
|
|---|
| 102 |
|
|---|
| 103 | # not reached
|
|---|
| 104 | cli
|
|---|
| 105 | hlt0:
|
|---|
| 106 | hlt
|
|---|
| 107 | jmp hlt0
|
|---|
| 108 |
|
|---|
| 109 | .global map_kernel
|
|---|
| 110 | map_kernel:
|
|---|
| 111 | #
|
|---|
| 112 | # Here we setup mapping for both the unmapped and mapped sections of the kernel.
|
|---|
| 113 | # For simplicity, we map the entire 4G space.
|
|---|
| 114 | #
|
|---|
| 115 | movl %cr4, %ecx
|
|---|
| 116 | orl $(1 << 4), %ecx # turn PSE on
|
|---|
| 117 | andl $(~(1 << 5)), %ecx # turn PAE off
|
|---|
| 118 | movl %ecx, %cr4
|
|---|
| 119 |
|
|---|
| 120 | movl $(page_directory + 0), %esi
|
|---|
| 121 | movl $(page_directory + 2048), %edi
|
|---|
| 122 | xorl %ecx, %ecx
|
|---|
| 123 | xorl %ebx, %ebx
|
|---|
| 124 |
|
|---|
| 125 | floop:
|
|---|
| 126 | movl $((1 << 7) | (1 << 1) | (1 << 0)), %eax
|
|---|
| 127 | orl %ebx, %eax
|
|---|
| 128 | movl %eax, (%esi, %ecx, 4) # mapping 0x00000000 + %ecx * 4M => 0x00000000 + %ecx * 4M
|
|---|
| 129 | movl %eax, (%edi, %ecx, 4) # mapping 0x80000000 + %ecx * 4M => 0x00000000 + %ecx * 4M
|
|---|
| 130 | addl $(4 * 1024 * 1024), %ebx
|
|---|
| 131 |
|
|---|
| 132 | incl %ecx
|
|---|
| 133 | cmpl $512, %ecx
|
|---|
| 134 | jl floop
|
|---|
| 135 |
|
|---|
| 136 | movl %esi, %cr3
|
|---|
| 137 |
|
|---|
| 138 | movl %cr0, %ebx
|
|---|
| 139 | orl $(1 << 31), %ebx # turn paging on
|
|---|
| 140 | movl %ebx, %cr0
|
|---|
| 141 | ret
|
|---|
| 142 |
|
|---|
| 143 | # Print string from %esi to EGA display (in red) and halt
|
|---|
| 144 | error_halt:
|
|---|
| 145 | movl $0xb8000, %edi # base of EGA text mode memory
|
|---|
| 146 | xorl %eax, %eax
|
|---|
| 147 |
|
|---|
| 148 | movw $0x3d4, %dx # read bits 8 - 15 of the cursor address
|
|---|
| 149 | movb $0xe, %al
|
|---|
| 150 | outb %al, %dx
|
|---|
| 151 |
|
|---|
| 152 | movw $0x3d5, %dx
|
|---|
| 153 | inb %dx, %al
|
|---|
| 154 | shl $8, %ax
|
|---|
| 155 |
|
|---|
| 156 | movw $0x3d4, %dx # read bits 0 - 7 of the cursor address
|
|---|
| 157 | movb $0xf, %al
|
|---|
| 158 | outb %al, %dx
|
|---|
| 159 |
|
|---|
| 160 | movw $0x3d5, %dx
|
|---|
| 161 | inb %dx, %al
|
|---|
| 162 |
|
|---|
| 163 | cmp $1920, %ax
|
|---|
| 164 | jbe cursor_ok
|
|---|
| 165 |
|
|---|
| 166 | movw $1920, %ax # sanity check for the cursor on the last line
|
|---|
| 167 |
|
|---|
| 168 | cursor_ok:
|
|---|
| 169 |
|
|---|
| 170 | movw %ax, %bx
|
|---|
| 171 | shl $1, %eax
|
|---|
| 172 | addl %eax, %edi
|
|---|
| 173 |
|
|---|
| 174 | movw $0x0c00, %ax # black background, light red foreground
|
|---|
| 175 |
|
|---|
| 176 | ploop:
|
|---|
| 177 | lodsb
|
|---|
| 178 | cmp $0, %al
|
|---|
| 179 | je ploop_end
|
|---|
| 180 | stosw
|
|---|
| 181 | inc %bx
|
|---|
| 182 | jmp ploop
|
|---|
| 183 | ploop_end:
|
|---|
| 184 |
|
|---|
| 185 | movw $0x3d4, %dx # write bits 8 - 15 of the cursor address
|
|---|
| 186 | movb $0xe, %al
|
|---|
| 187 | outb %al, %dx
|
|---|
| 188 |
|
|---|
| 189 | movw $0x3d5, %dx
|
|---|
| 190 | movb %bh, %al
|
|---|
| 191 | outb %al, %dx
|
|---|
| 192 |
|
|---|
| 193 | movw $0x3d4, %dx # write bits 0 - 7 of the cursor address
|
|---|
| 194 | movb $0xf, %al
|
|---|
| 195 | outb %al, %dx
|
|---|
| 196 |
|
|---|
| 197 | movw $0x3d5, %dx
|
|---|
| 198 | movb %bl, %al
|
|---|
| 199 | outb %al, %dx
|
|---|
| 200 |
|
|---|
| 201 | cli
|
|---|
| 202 | hlt1:
|
|---|
| 203 | hlt
|
|---|
| 204 | jmp hlt1
|
|---|
| 205 |
|
|---|
| 206 | #include "vesa_real.inc"
|
|---|
| 207 |
|
|---|
| 208 | .section K_DATA_START, "aw", @progbits
|
|---|
| 209 |
|
|---|
| 210 | .align 4096
|
|---|
| 211 | page_directory:
|
|---|
| 212 | .space 4096, 0
|
|---|
| 213 |
|
|---|
| 214 | grub_eax:
|
|---|
| 215 | .long 0
|
|---|
| 216 |
|
|---|
| 217 | grub_ebx:
|
|---|
| 218 | .long 0
|
|---|
| 219 |
|
|---|
| 220 | pse_msg:
|
|---|
| 221 | .asciz "Page Size Extension not supported. System halted."
|
|---|
| 222 |
|
|---|