[f761f1eb] | 1 | #
|
---|
[df4ed85] | 2 | # Copyright (c) 2001-2004 Jakub Jermar
|
---|
| 3 | # Copyright (c) 2005-2006 Martin Decky
|
---|
[f761f1eb] | 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 |
|
---|
[f9447155] | 30 | #include <arch/boot/boot.h>
|
---|
[aa72859] | 31 | #include <arch/boot/memmap.h>
|
---|
[8f2153b] | 32 | #include <arch/mm/page.h>
|
---|
| 33 | #include <arch/pm.h>
|
---|
[c18e666] | 34 | #include <arch/cpuid.h>
|
---|
[f9447155] | 35 |
|
---|
[66def8d] | 36 | #define START_STACK (BOOT_OFFSET - BOOT_STACK_SIZE)
|
---|
| 37 |
|
---|
[874e312a] | 38 | .section K_TEXT_START, "ax"
|
---|
[f761f1eb] | 39 |
|
---|
[b0bf501] | 40 | .code32
|
---|
[f9447155] | 41 | .align 4
|
---|
[66def8d] | 42 | .global multiboot_image_start
|
---|
[f9447155] | 43 | multiboot_header:
|
---|
| 44 | .long MULTIBOOT_HEADER_MAGIC
|
---|
| 45 | .long MULTIBOOT_HEADER_FLAGS
|
---|
[421c833] | 46 | .long -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS) # checksum
|
---|
[66def8d] | 47 | .long multiboot_header
|
---|
| 48 | .long unmapped_ktext_start
|
---|
[f9447155] | 49 | .long 0
|
---|
| 50 | .long 0
|
---|
[66def8d] | 51 | .long multiboot_image_start
|
---|
[421c833] | 52 |
|
---|
[f9447155] | 53 | multiboot_image_start:
|
---|
[e13daa5d] | 54 | cld
|
---|
[421c833] | 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
|
---|
[5eb1379] | 59 | movw %cx, %es
|
---|
| 60 | movw %cx, %fs
|
---|
[ff133e2] | 61 | movw %cx, %gs
|
---|
[421c833] | 62 | movw %cx, %ds # kernel data + stack
|
---|
[5eb1379] | 63 | movw %cx, %ss
|
---|
[5dce48b9] | 64 |
|
---|
[421c833] | 65 | jmpl $gdtselector(KTEXT_DES), $multiboot_meeting_point
|
---|
[4533601] | 66 | multiboot_meeting_point:
|
---|
| 67 |
|
---|
[421c833] | 68 | movl %eax, grub_eax # save parameters from GRUB
|
---|
[7cb567cd] | 69 | movl %ebx, grub_ebx
|
---|
[ff133e2] | 70 |
|
---|
[c18e666] | 71 | movl $(INTEL_CPUID_LEVEL), %eax
|
---|
[421c833] | 72 | cpuid
|
---|
| 73 | cmp $0x0, %eax # any function > 0?
|
---|
[eaf6cd6] | 74 | jbe pse_unsupported
|
---|
[c18e666] | 75 |
|
---|
| 76 | movl $(INTEL_CPUID_STANDARD), %eax
|
---|
[eaf6cd6] | 77 | cpuid
|
---|
[c18e666] | 78 | bt $(INTEL_PSE), %edx
|
---|
[eaf6cd6] | 79 | jc pse_supported
|
---|
[421c833] | 80 |
|
---|
[eaf6cd6] | 81 | pse_unsupported:
|
---|
| 82 | movl $pse_msg, %esi
|
---|
| 83 | jmp error_halt
|
---|
| 84 |
|
---|
| 85 | pse_supported:
|
---|
[c18e666] | 86 |
|
---|
[421c833] | 87 | #include "vesa_prot.inc"
|
---|
[4b241f3] | 88 |
|
---|
[421c833] | 89 | # map kernel and turn paging on
|
---|
| 90 | call map_kernel
|
---|
| 91 |
|
---|
| 92 | # call arch_pre_main(grub_eax, grub_ebx)
|
---|
[bf25efb] | 93 | pushl grub_ebx
|
---|
| 94 | pushl grub_eax
|
---|
[deca67b] | 95 | call arch_pre_main
|
---|
[bac86377] | 96 |
|
---|
| 97 | # Create the first stack frame
|
---|
| 98 | pushl $0
|
---|
| 99 | movl %esp, %ebp
|
---|
[deca67b] | 100 |
|
---|
| 101 | call main_bsp
|
---|
| 102 |
|
---|
[421c833] | 103 | # not reached
|
---|
[d47f0e1] | 104 | cli
|
---|
[421c833] | 105 | hlt0:
|
---|
| 106 | hlt
|
---|
| 107 | jmp hlt0
|
---|
[d47f0e1] | 108 |
|
---|
| 109 | .global map_kernel
|
---|
| 110 | map_kernel:
|
---|
[dcbc8be] | 111 | #
|
---|
| 112 | # Here we setup mapping for both the unmapped and mapped sections of the kernel.
|
---|
[cf27a6cb] | 113 | # For simplicity, we map the entire 4G space.
|
---|
[dcbc8be] | 114 | #
|
---|
| 115 | movl %cr4, %ecx
|
---|
[421c833] | 116 | orl $(1 << 4), %ecx # turn PSE on
|
---|
| 117 | andl $(~(1 << 5)), %ecx # turn PAE off
|
---|
[eaf6cd6] | 118 | movl %ecx, %cr4
|
---|
[dcbc8be] | 119 |
|
---|
[eaf6cd6] | 120 | movl $(page_directory + 0), %esi
|
---|
| 121 | movl $(page_directory + 2048), %edi
|
---|
[cf27a6cb] | 122 | xorl %ecx, %ecx
|
---|
| 123 | xorl %ebx, %ebx
|
---|
[421c833] | 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 |
|
---|
[cf27a6cb] | 136 | movl %esi, %cr3
|
---|
[f761f1eb] | 137 |
|
---|
[dcbc8be] | 138 | movl %cr0, %ebx
|
---|
[421c833] | 139 | orl $(1 << 31), %ebx # turn paging on
|
---|
[dcbc8be] | 140 | movl %ebx, %cr0
|
---|
[d47f0e1] | 141 | ret
|
---|
[dcbc8be] | 142 |
|
---|
[eaf6cd6] | 143 | # Print string from %esi to EGA display (in red) and halt
|
---|
| 144 | error_halt:
|
---|
[421c833] | 145 | movl $0xb8000, %edi # base of EGA text mode memory
|
---|
[eaf6cd6] | 146 | xorl %eax, %eax
|
---|
| 147 |
|
---|
[421c833] | 148 | movw $0x3d4, %dx # read bits 8 - 15 of the cursor address
|
---|
[eaf6cd6] | 149 | movb $0xe, %al
|
---|
| 150 | outb %al, %dx
|
---|
| 151 |
|
---|
| 152 | movw $0x3d5, %dx
|
---|
| 153 | inb %dx, %al
|
---|
| 154 | shl $8, %ax
|
---|
| 155 |
|
---|
[421c833] | 156 | movw $0x3d4, %dx # read bits 0 - 7 of the cursor address
|
---|
[eaf6cd6] | 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
|
---|
[421c833] | 165 |
|
---|
| 166 | movw $1920, %ax # sanity check for the cursor on the last line
|
---|
| 167 |
|
---|
[eaf6cd6] | 168 | cursor_ok:
|
---|
| 169 |
|
---|
| 170 | movw %ax, %bx
|
---|
| 171 | shl $1, %eax
|
---|
| 172 | addl %eax, %edi
|
---|
| 173 |
|
---|
[421c833] | 174 | movw $0x0c00, %ax # black background, light red foreground
|
---|
[eaf6cd6] | 175 |
|
---|
| 176 | ploop:
|
---|
| 177 | lodsb
|
---|
| 178 | cmp $0, %al
|
---|
| 179 | je ploop_end
|
---|
| 180 | stosw
|
---|
[421c833] | 181 | inc %bx
|
---|
[eaf6cd6] | 182 | jmp ploop
|
---|
| 183 | ploop_end:
|
---|
| 184 |
|
---|
[421c833] | 185 | movw $0x3d4, %dx # write bits 8 - 15 of the cursor address
|
---|
[eaf6cd6] | 186 | movb $0xe, %al
|
---|
| 187 | outb %al, %dx
|
---|
| 188 |
|
---|
| 189 | movw $0x3d5, %dx
|
---|
| 190 | movb %bh, %al
|
---|
| 191 | outb %al, %dx
|
---|
| 192 |
|
---|
[421c833] | 193 | movw $0x3d4, %dx # write bits 0 - 7 of the cursor address
|
---|
[eaf6cd6] | 194 | movb $0xf, %al
|
---|
| 195 | outb %al, %dx
|
---|
| 196 |
|
---|
| 197 | movw $0x3d5, %dx
|
---|
| 198 | movb %bl, %al
|
---|
| 199 | outb %al, %dx
|
---|
[ff133e2] | 200 |
|
---|
[421c833] | 201 | cli
|
---|
| 202 | hlt1:
|
---|
| 203 | hlt
|
---|
| 204 | jmp hlt1
|
---|
[22cf454d] | 205 |
|
---|
[421c833] | 206 | #include "vesa_real.inc"
|
---|
[22cf454d] | 207 |
|
---|
[874e312a] | 208 | .section K_DATA_START, "aw", @progbits
|
---|
[dcbc8be] | 209 |
|
---|
| 210 | .align 4096
|
---|
| 211 | page_directory:
|
---|
| 212 | .space 4096, 0
|
---|
[eaf6cd6] | 213 |
|
---|
[7cb567cd] | 214 | grub_eax:
|
---|
| 215 | .long 0
|
---|
| 216 |
|
---|
| 217 | grub_ebx:
|
---|
| 218 | .long 0
|
---|
| 219 |
|
---|
[eaf6cd6] | 220 | pse_msg:
|
---|
[4f42d52] | 221 | .asciz "Page Size Extension not supported. System halted."
|
---|
[c18e666] | 222 |
|
---|