Relicense Sortix to the ISC license.
I hereby relicense all my work on Sortix under the ISC license as below.
All Sortix contributions by other people are already under this license,
are not substantial enough to be copyrightable, or have been removed.
All imported code from other projects is compatible with this license.
All GPL licensed code from other projects had previously been removed.
Copyright 2011-2016 Jonas 'Sortie' Termansen and contributors.
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2016-03-02 17:38:16 -05:00
|
|
|
/*
|
2018-10-14 16:09:07 -04:00
|
|
|
* Copyright (c) 2011, 2014, 2015, 2016, 2018 Jonas 'Sortie' Termansen.
|
Relicense Sortix to the ISC license.
I hereby relicense all my work on Sortix under the ISC license as below.
All Sortix contributions by other people are already under this license,
are not substantial enough to be copyrightable, or have been removed.
All imported code from other projects is compatible with this license.
All GPL licensed code from other projects had previously been removed.
Copyright 2011-2016 Jonas 'Sortie' Termansen and contributors.
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2016-03-02 17:38:16 -05:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
2018-10-14 16:09:07 -04:00
|
|
|
.section .text.unlikely
|
2013-05-22 16:06:18 -04:00
|
|
|
|
2015-03-15 19:14:21 -04:00
|
|
|
# Multiboot header.
|
|
|
|
.align 4
|
|
|
|
.long 0x1BADB002 # Magic.
|
2015-03-19 19:08:08 -04:00
|
|
|
.long 0x00000007 # Flags.
|
|
|
|
.long -(0x1BADB002 + 0x00000007) # Checksum
|
|
|
|
.skip 32-12
|
|
|
|
.long 0 # Mode
|
|
|
|
.long 0 # Width
|
|
|
|
.long 0 # Height
|
|
|
|
.long 0 # Depth
|
2015-03-15 19:14:21 -04:00
|
|
|
|
2015-03-16 12:24:42 -04:00
|
|
|
.section .bss, "aw", @nobits
|
|
|
|
.align 4096
|
|
|
|
bootpml4:
|
|
|
|
.skip 4096
|
|
|
|
bootpml3:
|
|
|
|
.skip 4096
|
|
|
|
bootpml2:
|
|
|
|
.skip 4096
|
|
|
|
bootpml1_a:
|
|
|
|
.skip 4096
|
|
|
|
bootpml1_b:
|
|
|
|
.skip 4096
|
|
|
|
fracpml3:
|
|
|
|
.skip 4096
|
|
|
|
fracpml2:
|
|
|
|
.skip 4096
|
|
|
|
fracpml1:
|
|
|
|
.skip 4096
|
|
|
|
forkpml2:
|
|
|
|
.skip 4096
|
|
|
|
forkpml1:
|
|
|
|
.skip 4096
|
|
|
|
physpml3:
|
|
|
|
.skip 4096
|
|
|
|
physpml2:
|
|
|
|
.skip 4096
|
|
|
|
physpml1:
|
|
|
|
.skip 4096
|
|
|
|
physpml0:
|
|
|
|
.skip 4096
|
|
|
|
nullpage: .global nullpage
|
|
|
|
.skip 4096
|
|
|
|
|
|
|
|
.section .text
|
2013-05-22 16:06:18 -04:00
|
|
|
.global _start
|
2014-12-28 13:18:45 -05:00
|
|
|
.global __start
|
2011-08-05 08:25:00 -04:00
|
|
|
.type _start, @function
|
2014-12-28 13:18:45 -05:00
|
|
|
.type __start, @function
|
2011-08-05 08:25:00 -04:00
|
|
|
.code32
|
|
|
|
_start:
|
2014-12-28 13:18:45 -05:00
|
|
|
__start:
|
2017-06-09 20:18:07 -04:00
|
|
|
# Clear the direction flag.
|
|
|
|
cld
|
|
|
|
|
2015-03-15 19:14:21 -04:00
|
|
|
# Initialize the stack pointer. The magic value is from kernel.cpp.
|
|
|
|
movl $(stack + 65536), %esp # 64 KiB, see kernel.cpp (See below also)
|
|
|
|
|
|
|
|
# Finish installing the kernel stack into the Task Switch Segment.
|
|
|
|
movl %esp, tss + 4
|
|
|
|
movl $0, tss + 8
|
|
|
|
|
|
|
|
# Finish installing the Task Switch Segment into the Global Descriptor Table.
|
|
|
|
movl $tss, %ecx
|
|
|
|
movw %cx, gdt + 0x28 + 2
|
|
|
|
shrl $16, %ecx
|
|
|
|
movb %cl, gdt + 0x28 + 4
|
|
|
|
shrl $8, %ecx
|
|
|
|
movb %cl, gdt + 0x28 + 7
|
|
|
|
movl $0, gdt + 0x28 + 8
|
|
|
|
|
|
|
|
# We got our multiboot information in various registers.
|
|
|
|
pushl $0
|
|
|
|
pushl %eax # Multiboot magic value.
|
|
|
|
pushl $0
|
|
|
|
pushl %ebx # Multiboot information structure pointer.
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2015-03-16 12:24:42 -04:00
|
|
|
movl $bootpml4, %edi
|
2015-03-15 19:14:21 -04:00
|
|
|
movl %edi, %cr3
|
2011-10-02 09:58:08 -04:00
|
|
|
|
2015-03-16 12:24:42 -04:00
|
|
|
# Page-Map Level 4.
|
|
|
|
movl $(bootpml3 + 0x207), bootpml4 + 0 * 8
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2015-03-16 12:24:42 -04:00
|
|
|
# Page Directory Pointer Table.
|
|
|
|
movl $(bootpml2 + 0x207), bootpml3 + 0 * 8
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2015-03-16 12:24:42 -04:00
|
|
|
# Page Directory (no user-space access here).
|
|
|
|
movl $(bootpml1_a + 0x003), bootpml2 + 0 * 8
|
|
|
|
movl $(bootpml1_b + 0x003), bootpml2 + 1 * 8
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2015-03-16 12:24:42 -04:00
|
|
|
# Page Table (identity map the first 4 MiB, except NULL).
|
2015-08-27 15:39:35 -04:00
|
|
|
# TODO: This is insecure as it doesn't restrict write & execute access to
|
|
|
|
# the code kernel code & variables appropriately.
|
2015-03-16 12:24:42 -04:00
|
|
|
movl $(bootpml1_a + 8), %edi
|
|
|
|
movl $0x1003, %esi
|
|
|
|
movl $1023, %ecx
|
2015-03-15 19:14:21 -04:00
|
|
|
1:
|
2015-03-16 12:24:42 -04:00
|
|
|
movl %esi, (%edi)
|
|
|
|
addl $0x1000, %esi
|
2015-03-15 19:14:21 -04:00
|
|
|
addl $8, %edi
|
|
|
|
loop 1b
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2015-03-16 12:24:42 -04:00
|
|
|
# Map the null page.
|
|
|
|
movl $nullpage, %edi
|
|
|
|
shrl $12, %edi
|
|
|
|
movl $0x0003, bootpml1_a(, %edi, 8)
|
|
|
|
|
|
|
|
# Fractal mapping.
|
|
|
|
movl $(bootpml4 + 0x003), bootpml4 + 511 * 8
|
|
|
|
movl $(fracpml3 + 0x203), bootpml4 + 510 * 8
|
|
|
|
movl $(bootpml4 + 0x003), fracpml3 + 511 * 8
|
|
|
|
movl $(fracpml2 + 0x203), fracpml3 + 510 * 8
|
|
|
|
movl $(bootpml4 + 0x003), fracpml2 + 511 * 8
|
|
|
|
movl $(fracpml1 + 0x203), fracpml2 + 510 * 8
|
|
|
|
movl $(bootpml4 + 0x003), fracpml1 + 511 * 8
|
|
|
|
|
|
|
|
# Predefined room for forking address spaces.
|
|
|
|
movl $(forkpml2 + 0x203), fracpml3 + 0 * 8
|
|
|
|
movl $(forkpml1 + 0x203), forkpml2 + 0 * 8
|
|
|
|
|
|
|
|
# Physical page allocator.
|
|
|
|
movl $(physpml3 + 0x003), bootpml4 + 509 * 8
|
|
|
|
movl $(physpml2 + 0x003), physpml3 + 0 * 8
|
|
|
|
movl $(physpml1 + 0x003), physpml2 + 0 * 8
|
|
|
|
movl $(physpml0 + 0x003), physpml1 + 0 * 8
|
|
|
|
|
2011-09-21 15:08:43 -04:00
|
|
|
# Enable PAE.
|
2015-03-15 19:14:21 -04:00
|
|
|
movl %cr4, %eax
|
2011-08-05 08:25:00 -04:00
|
|
|
orl $0x20, %eax
|
2015-03-15 19:14:21 -04:00
|
|
|
movl %eax, %cr4
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2015-08-27 15:39:35 -04:00
|
|
|
# Enable long mode and the No-Execute bit.
|
2015-03-15 19:14:21 -04:00
|
|
|
movl $0xC0000080, %ecx
|
2011-08-05 08:25:00 -04:00
|
|
|
rdmsr
|
2015-08-27 15:39:35 -04:00
|
|
|
orl $0x900, %eax
|
2011-08-05 08:25:00 -04:00
|
|
|
wrmsr
|
|
|
|
|
2015-08-27 15:39:35 -04:00
|
|
|
# Enable paging (with write protection) and enter long mode (still 32-bit)
|
2015-03-15 19:14:21 -04:00
|
|
|
movl %cr0, %eax
|
2015-08-27 15:39:35 -04:00
|
|
|
orl $0x80010000, %eax
|
2015-03-15 19:14:21 -04:00
|
|
|
movl %eax, %cr0
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2015-03-15 19:14:21 -04:00
|
|
|
# Load the Global Descriptor Table pointer register.
|
|
|
|
subl $6, %esp
|
|
|
|
movw gdt_size_minus_one, %cx
|
|
|
|
movw %cx, 0(%esp)
|
|
|
|
movl $gdt, %ecx
|
|
|
|
movl %ecx, 2(%esp)
|
|
|
|
lgdt 0(%esp)
|
|
|
|
addl $6, %esp
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2011-09-21 15:08:43 -04:00
|
|
|
# Now use the 64-bit code segment, and we are in full 64-bit mode.
|
2015-03-15 19:14:21 -04:00
|
|
|
ljmp $0x08, $2f
|
2011-08-05 08:25:00 -04:00
|
|
|
|
|
|
|
.code64
|
2015-03-15 19:14:21 -04:00
|
|
|
2:
|
2016-07-29 09:16:11 -04:00
|
|
|
# The upper 32 bits of the general purpose registers are *undefined* after
|
|
|
|
# switching into 64-bit mode (Intel Manual, Vol 1, 3.4.1.1 "General-Purpose
|
|
|
|
# Registers in 64-Bit Mode"). The lower 32 bits are preserved from 32-bit
|
|
|
|
# mode. Any registers whose value we wish to retain must have its upper 32
|
|
|
|
# bits masked off. Assigning a register to itself in a 32-bit operation will
|
|
|
|
# zero the upper 32 bits. The only register the below code assumes is
|
|
|
|
# retained is the stack register (esp/rsp).
|
|
|
|
mov %esp, %esp
|
|
|
|
|
2015-03-15 19:14:21 -04:00
|
|
|
# Switch ds, es, fs, gs, ss to the kernel data segment (0x10).
|
|
|
|
movw $0x10, %cx
|
|
|
|
movw %cx, %ds
|
|
|
|
movw %cx, %es
|
|
|
|
movw %cx, %ss
|
|
|
|
|
|
|
|
# Switch the task switch segment register to the task switch segment (0x28).
|
|
|
|
movw $(0x28 /* TSS */ | 0x3 /* RPL */), %cx
|
|
|
|
ltr %cx
|
|
|
|
|
|
|
|
# Switch to the thread local fs and gs segments.
|
|
|
|
movw $(0x20 /* DS */ | 0x3 /* RPL */), %cx
|
|
|
|
movw %cx, %fs
|
|
|
|
movw %cx, %gs
|
2011-08-05 08:25:00 -04:00
|
|
|
|
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
|
2016-03-26 11:04:05 -04:00
|
|
|
push $0x1F80
|
|
|
|
ldmxcsr (%rsp)
|
|
|
|
addq $8, %rsp
|
2012-05-27 08:57:32 -04:00
|
|
|
|
2014-03-03 18:11:13 -05:00
|
|
|
# Store a copy of the initialial floating point registers.
|
|
|
|
fxsave fpu_initialized_regs
|
|
|
|
|
2015-03-15 19:14:21 -04:00
|
|
|
# Enter the high-level kernel proper.
|
|
|
|
pop %rsi # Multiboot information structure pointer.
|
|
|
|
pop %rdi # Multiboot magic value.
|
|
|
|
call KernelInit
|
|
|
|
|
|
|
|
jmp HaltKernel
|
2013-05-22 16:06:18 -04:00
|
|
|
.size _start, . - _start
|
2014-12-28 13:18:45 -05:00
|
|
|
.size __start, . - __start
|
2015-03-15 19:14:21 -04:00
|
|
|
.global HaltKernel
|
|
|
|
.type HaltKernel, @function
|
|
|
|
HaltKernel:
|
|
|
|
cli
|
|
|
|
hlt
|
|
|
|
jmp HaltKernel
|
|
|
|
.size HaltKernel, . - HaltKernel
|