2013-07-10 09:26:01 -04:00
|
|
|
/*******************************************************************************
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
Copyright(C) Jonas 'Sortie' Termansen 2011.
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
This file is part of Sortix.
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
Sortix is free software: you can redistribute it and/or modify it under the
|
|
|
|
terms of the GNU General Public License as published by the Free Software
|
|
|
|
Foundation, either version 3 of the License, or (at your option) any later
|
|
|
|
version.
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
Sortix is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
details.
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
You should have received a copy of the GNU General Public License along with
|
|
|
|
Sortix. If not, see <http://www.gnu.org/licenses/>.
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
x64/boot.s
|
|
|
|
Bootstraps the kernel and passes over control from the boot-loader to the
|
|
|
|
kernel main function. It also jumps into long mode!
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
*******************************************************************************/
|
2011-08-05 08:25:00 -04:00
|
|
|
|
|
|
|
.section .text
|
2013-05-22 16:06:18 -04:00
|
|
|
.text 0x100000
|
|
|
|
|
|
|
|
.global _start
|
2011-08-05 08:25:00 -04:00
|
|
|
.type _start, @function
|
|
|
|
.code32
|
|
|
|
_start:
|
2012-05-27 08:57:32 -04:00
|
|
|
jmp prepare_kernel_execution
|
2011-08-05 08:25:00 -04:00
|
|
|
|
|
|
|
# Align 32 bits boundary.
|
2011-09-21 15:08:43 -04:00
|
|
|
.align 4
|
2011-08-05 08:25:00 -04:00
|
|
|
|
|
|
|
# Multiboot header.
|
|
|
|
multiboot_header:
|
|
|
|
# Magic.
|
2011-09-21 15:08:43 -04:00
|
|
|
.long 0x1BADB002
|
2011-08-05 08:25:00 -04:00
|
|
|
# Flags.
|
2011-09-21 15:08:43 -04:00
|
|
|
.long 0x00000003
|
2011-08-05 08:25:00 -04:00
|
|
|
# Checksum.
|
2011-09-21 15:08:43 -04:00
|
|
|
.long -(0x1BADB002 + 0x00000003)
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2012-05-27 08:57:32 -04:00
|
|
|
prepare_kernel_execution:
|
2011-08-05 08:25:00 -04:00
|
|
|
# We got our multiboot information in various registers. But we are going
|
|
|
|
# to need these registers. But where can we store them then? Oh hey, let's
|
|
|
|
# store then in the code already run!
|
|
|
|
|
|
|
|
# Store the pointer to the Multiboot information structure.
|
|
|
|
mov %ebx, 0x100000
|
|
|
|
|
|
|
|
# Store the magic value.
|
|
|
|
mov %eax, 0x100004
|
|
|
|
|
2011-12-24 18:10:56 -05:00
|
|
|
# Clear the first $0xE000 bytes following 0x21000.
|
|
|
|
movl $0x21000, %edi
|
2011-08-05 08:25:00 -04:00
|
|
|
mov %edi, %cr3
|
|
|
|
xorl %eax, %eax
|
2011-10-02 09:58:08 -04:00
|
|
|
movl $0xE000, %ecx
|
2011-08-05 08:25:00 -04:00
|
|
|
rep stosl
|
|
|
|
movl %cr3, %edi
|
|
|
|
|
2011-09-21 15:08:43 -04:00
|
|
|
# Set the initial page tables.
|
2011-12-01 04:45:44 -05:00
|
|
|
# Note that we OR with 0x7 here to allow user-space access, except in the
|
|
|
|
# first 2 MiB. We also do this with 0x200 to allow forking the page.
|
2011-10-02 09:58:08 -04:00
|
|
|
|
|
|
|
# Page-Map Level 4
|
2011-12-24 18:10:56 -05:00
|
|
|
movl $0x22207, (%edi)
|
2011-08-05 08:25:00 -04:00
|
|
|
addl $0x1000, %edi
|
|
|
|
|
2011-10-02 09:58:08 -04:00
|
|
|
# Page-Directory Pointer Table
|
2011-12-24 18:10:56 -05:00
|
|
|
movl $0x23207, (%edi)
|
2011-08-05 08:25:00 -04:00
|
|
|
addl $0x1000, %edi
|
|
|
|
|
2011-12-01 17:05:08 -05:00
|
|
|
# Page-Directory (no user-space access here)
|
2012-03-17 13:14:57 -04:00
|
|
|
movl $0x24003, (%edi) # (First 2 MiB)
|
|
|
|
movl $0x25003, 8(%edi) # (Second 2 MiB)
|
2011-08-05 08:25:00 -04:00
|
|
|
addl $0x1000, %edi
|
|
|
|
|
2011-10-02 09:58:08 -04:00
|
|
|
# Page-Table
|
2011-12-01 17:05:08 -05:00
|
|
|
# Memory map the first 4 MiB.
|
2011-08-05 08:25:00 -04:00
|
|
|
movl $0x3, %ebx
|
2011-12-01 17:05:08 -05:00
|
|
|
movl $1024, %ecx
|
2011-08-05 08:25:00 -04:00
|
|
|
|
|
|
|
SetEntry:
|
|
|
|
mov %ebx, (%edi)
|
|
|
|
add $0x1000, %ebx
|
|
|
|
add $8, %edi
|
|
|
|
loop SetEntry
|
|
|
|
|
2011-09-21 15:08:43 -04:00
|
|
|
# Enable PAE.
|
2011-08-05 08:25:00 -04:00
|
|
|
mov %cr4, %eax
|
|
|
|
orl $0x20, %eax
|
|
|
|
mov %eax, %cr4
|
|
|
|
|
2011-09-21 15:08:43 -04:00
|
|
|
# Enable long mode.
|
2011-08-05 08:25:00 -04:00
|
|
|
mov $0xC0000080, %ecx
|
|
|
|
rdmsr
|
|
|
|
orl $0x100, %eax
|
|
|
|
wrmsr
|
|
|
|
|
2011-09-21 15:08:43 -04:00
|
|
|
# Enable paging and enter long mode (still 32-bit)
|
2011-08-05 08:25:00 -04:00
|
|
|
mov %cr0, %eax
|
|
|
|
orl $0x80000000, %eax
|
|
|
|
mov %eax, %cr0
|
|
|
|
|
2011-09-21 15:08:43 -04:00
|
|
|
# Load the long mode GDT.
|
2011-08-05 08:25:00 -04:00
|
|
|
mov GDTPointer, %eax
|
|
|
|
lgdtl GDTPointer
|
|
|
|
|
2011-09-21 15:08:43 -04:00
|
|
|
# Now use the 64-bit code segment, and we are in full 64-bit mode.
|
2011-08-05 08:25:00 -04:00
|
|
|
ljmp $0x10, $Realm64
|
|
|
|
|
|
|
|
.code64
|
|
|
|
Realm64:
|
|
|
|
|
2011-09-21 15:08:43 -04:00
|
|
|
# Now, set up the other segment registers.
|
2011-08-05 08:25:00 -04:00
|
|
|
cli
|
|
|
|
mov $0x18, %ax
|
|
|
|
mov %ax, %ds
|
|
|
|
mov %ax, %es
|
|
|
|
mov %ax, %fs
|
|
|
|
mov %ax, %gs
|
|
|
|
|
2012-05-27 08:57:32 -04:00
|
|
|
# Enable the floating point unit.
|
|
|
|
mov %cr0, %rax
|
|
|
|
and $0xFFFD, %ax
|
|
|
|
or $0x10, %ax
|
|
|
|
mov %rax, %cr0
|
|
|
|
fninit
|
|
|
|
|
|
|
|
# Enable Streaming SIMD Extensions.
|
|
|
|
mov %cr0, %rax
|
|
|
|
and $0xFFFB, %ax
|
|
|
|
or $0x2, %ax
|
|
|
|
mov %rax, %cr0
|
|
|
|
mov %cr4, %rax
|
|
|
|
or $0x600, %rax
|
|
|
|
mov %rax, %cr4
|
|
|
|
|
2011-09-21 15:08:43 -04:00
|
|
|
# Alright, that was the bootstrap code. Now begin preparing to run the
|
|
|
|
# actual 64-bit kernel.
|
2011-08-05 08:25:00 -04:00
|
|
|
jmp Main
|
2013-05-22 16:06:18 -04:00
|
|
|
.size _start, . - _start
|
2011-08-05 08:25:00 -04:00
|
|
|
|
|
|
|
.section .data
|
|
|
|
GDT64: # Global Descriptor Table (64-bit).
|
|
|
|
GDTNull: # The null descriptor.
|
|
|
|
.word 0 # Limit (low).
|
|
|
|
.word 0 # Base (low).
|
|
|
|
.byte 0 # Base (middle)
|
|
|
|
.byte 0 # Access.
|
|
|
|
.byte 0 # Granularity.
|
|
|
|
.byte 0 # Base (high).
|
|
|
|
GDTUnused: # The null descriptor.
|
|
|
|
.word 0 # Limit (low).
|
|
|
|
.word 0 # Base (low).
|
|
|
|
.byte 0 # Base (middle)
|
|
|
|
.byte 0 # Access.
|
|
|
|
.byte 0 # Granularity.
|
|
|
|
.byte 0 # Base (high).
|
|
|
|
GDTCode: # The code descriptor.
|
|
|
|
.word 0xFFFF # Limit (low).
|
|
|
|
.word 0 # Base (low).
|
|
|
|
.byte 0 # Base (middle)
|
|
|
|
.byte 0x9A # Access.
|
|
|
|
.byte 0xAF # Granularity.
|
|
|
|
.byte 0 # Base (high).
|
|
|
|
GDTData: # The data descriptor.
|
|
|
|
.word 0xFFFF # Limit (low).
|
|
|
|
.word 0 # Base (low).
|
|
|
|
.byte 0 # Base (middle)
|
|
|
|
.byte 0x92 # Access.
|
|
|
|
.byte 0x8F # Granularity.
|
|
|
|
.byte 0 # Base (high).
|
|
|
|
GDTPointer: # The GDT-pointer.
|
|
|
|
.word GDTPointer - GDT64 - 1 # Limit.
|
|
|
|
.long GDT64 # Base.
|
|
|
|
.long 0
|
2012-03-21 19:52:29 -04:00
|
|
|
|
2011-08-05 08:25:00 -04:00
|
|
|
Main:
|
|
|
|
# Copy the character B onto the screen so we know it works.
|
|
|
|
movq $0x242, %r15
|
|
|
|
movq %r15, %rax
|
|
|
|
movw %ax, 0xB8000
|
|
|
|
|
|
|
|
# Load the pointer to the Multiboot information structure.
|
|
|
|
mov 0x100000, %ebx
|
|
|
|
|
|
|
|
# Load the magic value.
|
|
|
|
mov 0x100004, %eax
|
|
|
|
|
2011-12-01 17:05:08 -05:00
|
|
|
jmp beginkernel
|
2013-05-22 16:06:18 -04:00
|
|
|
.size Main, . - Main
|