/* Declare a multiboot header that marks the program as a kernel. https://www.gnu.org/software/grub/manual/multiboot2/html_node/index.html The Multiboot2 header must be contained completely within the first 32kB of the OS image, and must be 64-bit (8 byte) aligned. */ .set MULTIBOOT_HEADER_MAGIC, 0xe85250d6 # multiboot2 magic number .set MULTIBOOT_ARCHITECTURE, 0 # protected mode i386 .set MULTIBOOT_HEADER_TAG_END, 0 .section .multiboot.header, "a" .align 8 header_start: .int MULTIBOOT_HEADER_MAGIC .int MULTIBOOT_ARCHITECTURE .int header_end - header_start .int -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_ARCHITECTURE + (header_end - header_start)) # TODO tags go here .short MULTIBOOT_HEADER_TAG_END .short 0 .int 8 header_end: /* The stack on x86 must be 16-byte aligned according to the System V ABI standard and de-facto extensions. The compiler will assume the stack is properly aligned and failure to align the stack will result in undefined behavior. */ .section .stack, "aw", @nobits .align 16 stack_bottom: .skip 16 * 1024 stack_top: .section .pages, "aw", @nobits .align 4096 boot_page_directory: .skip 4096 boot_page_table1: .skip 4096 /* The linker script specifies _start as the entry point to the kernel and the bootloader will jump to this position once the kernel has been loaded. */ .section .multiboot.text, "ax" .set VADDR_BASE, 0xc0000000 .global _start .type _start, @function _start: cli # Physical address of boot_page_table1. movl $(boot_page_table1 - VADDR_BASE), %edi # First address to map is address 0. movl $0, %esi # Map 1023 pages. The 1024th will be the VGA text buffer. movl $1023, %ecx 1: # Only map the kernel. cmpl $_kernel_start, %esi jl 2f cmpl $(_kernel_end - VADDR_BASE), %esi jge 3f # Map physical address as "present, writable". Note that this maps # .text and .rodata as writable. Mind security and map them as non-writable. movl %esi, %edx orl $0x003, %edx movl %edx, (%edi) 2: # Size of page is 4096 bytes. addl $4096, %esi # Size of entries in boot_page_table1 is 4 bytes. addl $4, %edi # Loop to the next entry if we haven't finished. loop 1b 3: # Map VGA video memory to 0xC03FF000 as "present, writable". movl $(0x000B8000 | 0x003), boot_page_table1 - VADDR_BASE + 1023 * 4 # The page table is used at both page directory entry 0 (virtually from 0x0 # to 0x3FFFFF) (thus identity mapping the kernel) and page directory entry # 768 (virtually from VADDR_BASE to 0xC03FFFFF) (thus mapping it in the # higher half). The kernel is identity mapped because enabling paging does # not change the next instruction, which continues to be physical. The CPU # would instead page fault if there was no identity mapping. # Map the page table to both virtual addresses 0x00000000 and VADDR_BASE. movl $(boot_page_table1 - VADDR_BASE + 0x003), boot_page_directory - VADDR_BASE + 0 movl $(boot_page_table1 - VADDR_BASE + 0x003), boot_page_directory - VADDR_BASE + 768 * 4 # Set cr3 to the address of the boot_page_directory. movl $(boot_page_directory - VADDR_BASE), %ecx movl %ecx, %cr3 # Enable paging and the write-protect bit. movl %cr0, %ecx orl $0x80010000, %ecx movl %ecx, %cr0 # Jump to higher half with an absolute jump. lea (kinit), %ecx jmp *%ecx .section .text .extern kernel_constructors .extern kernel_main kinit: # At this point, paging is fully set up and enabled. # Unmap the identity mapping as it is now unnecessary. movl $0, boot_page_directory + 0 # Reload crc3 to force a TLB flush so the changes to take effect. movl %cr3, %ecx movl %ecx, %cr3 #mov $stack_bottom, %ebp mov $stack_top, %esp # point the stack pointer to the stack /* This is a good place to initialize crucial processor state before the high-level kernel is entered. It's best to minimize the early environment where crucial features are offline. Note that the processor is not fully initialized yet: Features such as floating point instructions and instruction set extensions are not initialized yet. The GDT should be loaded here. Paging should be enabled here. C++ features such as global constructors and exceptions will require runtime support to work as well. */ pushl %ebx # push the pointer to the multiboot structure pushl %eax # push the multiboot magic value call kernel_constructors /* Enter the high-level kernel. The ABI requires the stack is 16-byte aligned at the time of the call instruction (which afterwards pushes the return pointer of size 4 bytes). The stack was originally 16-byte aligned above and we've pushed a multiple of 16 bytes to the stack since (pushed 0 bytes so far), so the alignment has thus been preserved and the call is well defined. */ call kernel_main /* If the system has nothing more to do, put the computer into an infinite loop. To do that: 1) Disable interrupts with cli (clear interrupt enable in eflags). They are already disabled by the bootloader, so this is not needed. Mind that you might later enable interrupts and return from kernel_main (which is sort of nonsensical to do). 2) Wait for the next interrupt to arrive with hlt (halt instruction). Since they are disabled, this will lock up the computer. 3) Jump to the hlt instruction if it ever wakes up due to a non-maskable interrupt occurring or due to system management mode. */ cli hang: hlt jmp hang