Higher half, baby!!!

This commit is contained in:
Kat R. 2022-10-29 10:03:56 -05:00
parent 87f8040eb2
commit d7889d3a84
2 changed files with 93 additions and 14 deletions

View file

@ -6,25 +6,93 @@
.set CHECKSUM, -(MAGIC + FLAGS) # checksum to prove we're multiboot .set CHECKSUM, -(MAGIC + FLAGS) # checksum to prove we're multiboot
# Declare header for multiboot # Declare header for multiboot
.section .multiboot .section .multiboot.data, "aw"
.align 4 .align 4
.long MAGIC .long MAGIC
.long FLAGS .long FLAGS
.long CHECKSUM .long CHECKSUM
# Reserve stack for initial thread # Reserve stack for initial thread
.section .bss .section .bootstrap_stack, "aw", @nobits
.align 16
stack_bottom: stack_bottom:
.skip 16384 # 16K .skip 16384 # 16K
stack_top: stack_top:
# Preallocate pages for paging
.section .bss, "aw", @nobits
.align 4096
boot_page_directory:
.skip 4096
boot_page_table1:
.skip 4096
# May need additional page for kernel > 3 MiB
# Kernel entry # Kernel entry
.section .text .section .multiboot.text, "a"
.global _start .global _start
.type _start, @function .type _start, @function
_start: _start:
movl $stack_top, %esp # Physical address of boot_page_table 1
movl $(boot_page_table1 - 0xC0000000), %edi
# Map address 0
movl $0, %esi
# Map first 1023 pages
movl $1023, %ecx
1:
# Only map the kernel
cmpl $_kernel_start, %esi
jl 2f
cmpl $(_kernel_end - 0xC0000000), %esi
jge 3f
# Map physical address as "present, writable"
# TODO: map .text and .rodata as non-writable
movl %esi, %edx
orl $0x003, %edx
movl %edx, (%edi)
2:
# Size of page is 4KiB
addl $4096, %esi
# Size of entries in boot_page_table1 is 4 bytes
addl $4, %edi
# Loop to next if not done
loop 1b
3:
# Map VGA memory to 0xC03FF000 as "present, writable"
movl $(0x000B8000 | 0x003), boot_page_table1 - 0xC0000000 + 1023 * 4
# Map page table to 0x0 and 0xC0000000
movl $(boot_page_table1 - 0xC0000000 + 0x003), boot_page_directory - 0xC0000000 + 0
movl $(boot_page_table1 - 0xC0000000 + 0x003), boot_page_directory - 0xC0000000 + 768 * 4
# Set cr3 to address of boot page directory
movl $(boot_page_directory - 0xC0000000), %ecx
movl %ecx, %cr3
# Enable paging and write-protect
movl %cr0, %ecx
orl $0x80010000, %ecx
movl %ecx, %cr0
# Jump to higher half
lea 4f, %ecx
jmp *%ecx
.section .text
4:
# Unmap identity mapping
movl $0, boot_page_directory + 0
# Reload cr3 to force TLB flush
movl %cr3, %ecx
movl %ecx, %cr3
# Set up stack
mov $stack_top, %esp
# Call global constructors # Call global constructors
call _init call _init
@ -36,4 +104,3 @@ _start:
cli cli
1: hlt 1: hlt
jmp 1b jmp 1b
.size _start, . - _start

View file

@ -4,21 +4,33 @@ SECTIONS
{ {
. = 1M; . = 1M;
.text BLOCK (4K) : ALIGN (4K) { _kernel_start = .;
*(.multiboot) .multiboot.data : {
*(.text) *(.multiboot.data)
} }
.rodata BLOCK (4K) : ALIGN (4K) { .multiboot.text : {
*(.multiboot.text)
}
. += 0xC0000000;
.text ALIGN (4K) : AT (ADDR (.text) - 0xC0000000) {
*(.text)
}
.rodata ALIGN (4K) : AT (ADDR (.rodata) - 0xC0000000) {
*(.rodata) *(.rodata)
} }
.data BLOCK (4K) : ALIGN (4K) { .data ALIGN (4K) : AT (ADDR(.data) - 0xC0000000) {
*(.data) *(.data)
} }
.bss BLOCK (4K) : ALIGN (4K) { .bss ALIGN (4K) : AT (ADDR (.bss) - 0xC0000000) {
*(COMMON) *(COMMON)
*(.bss) *(.bss)
*(.bootstrap_stack)
} }
_kernel_end = .;
} }