From d4003f1d6daf6883acb83a398e3fdba5336aefef Mon Sep 17 00:00:00 2001 From: Danny Holman Date: Tue, 20 Jul 2021 20:49:03 -0500 Subject: arch: i386: move the kernel to high memory Move the start of the kernel image to the "higher half" of memory. This also enables paging. --- arch/i386/boot/boot.s | 55 +++++++++++++++++++++++++++++++++++++++++++++------ arch/i386/boot/tty.c | 2 +- arch/i386/linker.ld | 22 +++++++++++++++------ 3 files changed, 66 insertions(+), 13 deletions(-) (limited to 'arch/i386') diff --git a/arch/i386/boot/boot.s b/arch/i386/boot/boot.s index 56ac227..792a790 100644 --- a/arch/i386/boot/boot.s +++ b/arch/i386/boot/boot.s @@ -4,29 +4,72 @@ .set MAGIC, 0x1BADB002 .set CHECKSUM, -(MAGIC + FLAGS) -.section .multiboot +.section .multiboot.data, "aw" .align 4 .long MAGIC .long FLAGS .long CHECKSUM -.section .bss -.align 16 +.section .bootstrap_stack, "aw", @nobits stack_bottom: .skip 16384 stack_top: -.section .text +.section .bss, "aw", @nobits + .align 4096 +boot_page_directory: + .skip 4096 +boot_page_table0: + .skip 4096 + +.section .multiboot.text, "a" .global _start .type _start, @function _start: + movl $(boot_page_table0 - 0xC0000000), %edi + movl $0, %esi + movl $1023, %ecx + +1: cmpl $_kernel_start, %esi + jl 2f + cmpl $(_kernel_end - 0xC0000000), %esi + jge 3f + + movl %esi, %edx + orl $0x003, %edx + movl %edx, (%edi) + +2: addl $4096, %esi + addl $4, %edi + loop 1b + +3: movl $(0x000B8000 | 0x003), boot_page_table0 - 0xC0000000 + 1023 * 4 + + movl $(boot_page_table0 - 0xC0000000 + 0x003), boot_page_directory - 0xC0000000 + movl $(boot_page_table0 - 0xC0000000 + 0x003), boot_page_directory - 0xC0000000 + 768 * 4 + + movl $(boot_page_directory - 0xC0000000), %ecx + movl %ecx, %cr3 + + movl %cr0, %ecx + orl $0x80010000, %ecx + movl %ecx, %cr0 + + lea 4f, %ecx + jmp *%ecx + +.section .text + +4: movl $0, boot_page_directory + 0 + + movl %cr3, %ecx + movl %ecx, %cr3 + movl $stack_top, %esp - call _init call kernel_main cli 1: hlt jmp 1b -.size _start, . - _start diff --git a/arch/i386/boot/tty.c b/arch/i386/boot/tty.c index 9244415..2c75760 100644 --- a/arch/i386/boot/tty.c +++ b/arch/i386/boot/tty.c @@ -6,7 +6,7 @@ static const size_t VGA_WIDTH = 80; static const size_t VGA_HEIGHT = 25; -static uint16_t *const VGA_MEMORY = (uint16_t*)0xB8000; +static uint16_t *const VGA_MEMORY = (uint16_t*)0xC03FF000; static size_t terminal_row; static size_t terminal_column; diff --git a/arch/i386/linker.ld b/arch/i386/linker.ld index a864c34..5d06e79 100644 --- a/arch/i386/linker.ld +++ b/arch/i386/linker.ld @@ -1,19 +1,29 @@ ENTRY(_start) SECTIONS { - . = 1M; - .text BLOCK(4K) : ALIGN(4K) { - *(.multiboot) + . = 0x00100000; + _kernel_start = .; + .multiboot.data : { + *(.multiboot.data) + } + .multiboot.text : { + *(.multiboot.text) + } + + . += 0xC0000000; + .text ALIGN(4K) : AT(ADDR(.text) - 0xC0000000) { *(.text) } - .rodata BLOCK(4K) : ALIGN(4K) { + .rodata ALIGN(4K) : AT(ADDR(.rodata) - 0xC0000000) { *(.rodata) } - .data BLOCK(4K) : ALIGN(4K) { + .data ALIGN(4K) : AT(ADDR(.data) - 0xC0000000) { *(.data) } - .bss BLOCK(4K) : ALIGN(4K) { + .bss ALIGN(4K) : AT(ADDR(.bss) - 0xC0000000) { *(COMMON) *(.bss) + *(.bootstrap_stack) } + _kernel_end = .; } -- cgit v1.2.3