Barest of bare bones
This commit is contained in:
parent
3350ea2151
commit
d71037a775
5 changed files with 149 additions and 0 deletions
60
boot.s
Normal file
60
boot.s
Normal file
|
@ -0,0 +1,60 @@
|
|||
# Declare some constants for the multiboot header.
|
||||
.set ALIGN, 1<<0 # Align loaded modules on page boundaries
|
||||
.set MEMINFO, 1<<1 # Provide memory map
|
||||
.set FLAGS, ALIGN | MEMINFO # This is the multiboot 'flag' field
|
||||
.set MAGIC, 0x1BADB002 # Magic number lets bootloader find the header
|
||||
.set CHECKSUM, -(MAGIC + FLAGS) # Checksum of above, to prove we are multiboot
|
||||
|
||||
# Declare a header as in the Multiboot Standard. We put this into a special
|
||||
# section so we can force the header to be in the start of the final program.
|
||||
# You don't need to understand all these details as it is just magic values that
|
||||
# is documented in the multiboot standard. The bootloader will search for this
|
||||
# magic sequence and recognize us as a multiboot kernel.
|
||||
.section .multiboot
|
||||
.align 4
|
||||
.long MAGIC
|
||||
.long FLAGS
|
||||
.long CHECKSUM
|
||||
|
||||
# Currently the stack pointer register (esp) points at anything and using it may
|
||||
# cause massive harm. Instead, we'll provide our own stack. We will allocate
|
||||
# room for a small temporary stack by creating a symbol at the bottom of it,
|
||||
# then allocating 16384 bytes for it, and finally creating a symbol at the top.
|
||||
.section .bootstrap_stack
|
||||
stack_buttom:
|
||||
.skip 16384 # 16 KiB
|
||||
stack_top:
|
||||
|
||||
# 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. It
|
||||
# doesn't make sense to return from this function as the bootloader is gone.
|
||||
.section .text
|
||||
.global _start
|
||||
.type _start, @function
|
||||
_start:
|
||||
# Set up some space for a call stack. The stack grows downwards, so esp gets set to the top of the stack.
|
||||
movl $stack_top, %esp
|
||||
|
||||
# Very early initialization done here.
|
||||
call kearly
|
||||
|
||||
# Global initialization done here.
|
||||
call _init
|
||||
|
||||
# Here we go...
|
||||
call kmain
|
||||
|
||||
# In case the function returns, we'll want to put the computer into an
|
||||
# infinite loop. To do that, we use the clear interrupt ('cli') instruction
|
||||
# to disable interrupts, the halt instruction ('hlt') to stop the CPU until
|
||||
# the next interrupt arrives, and jumping to the halt instruction if it ever
|
||||
# continues execution, just to be safe. We will create a local label rather
|
||||
# than real symbol and jump to there endlessly.
|
||||
cli
|
||||
hlt
|
||||
.Lhang:
|
||||
jmp .Lhang
|
||||
|
||||
# Set the size of the _start symbol to the current location '.' minus its start.
|
||||
# This is useful when debugging or when you implement call tracing.
|
||||
.size _start, . - _start
|
15
crti.s
Normal file
15
crti.s
Normal file
|
@ -0,0 +1,15 @@
|
|||
.section .init
|
||||
.global _init
|
||||
.type _init, @function
|
||||
_init:
|
||||
push %ebp
|
||||
movl %esp, %ebp
|
||||
# crtbegin.o's .init section goes here.
|
||||
|
||||
.section .fini
|
||||
.global _fini
|
||||
.type _fini, @function
|
||||
_fini:
|
||||
push %ebp
|
||||
movl %esp, %ebp
|
||||
# crtbegin.o's .fini section goes here.
|
9
crtn.s
Normal file
9
crtn.s
Normal file
|
@ -0,0 +1,9 @@
|
|||
.section .init
|
||||
# crtend.o's .init section goes here.
|
||||
popl %ebp
|
||||
ret
|
||||
|
||||
.section .fini
|
||||
# crtend.o's .fini section goes here.
|
||||
popl %ebp
|
||||
ret
|
38
linker.ld
Normal file
38
linker.ld
Normal file
|
@ -0,0 +1,38 @@
|
|||
/* The bootloader will look at this image and start execution at the symbol designated as the entry point. */
|
||||
ENTRY(_start)
|
||||
|
||||
/* Tell where the various sections of the object files will be put in the final kernel image. */
|
||||
SECTIONS
|
||||
{
|
||||
/* Begin putting sections at 1 MiB, a conventional place for kernels to be loaded at by the bootloader. */
|
||||
. = 1M;
|
||||
|
||||
/* First put the multiboot header, as it is required to be put very early in the image or the bootloader won't recognize the file format. Next we'll put the .text section. */
|
||||
.text BLOCK(4K) : ALIGN(4K)
|
||||
{
|
||||
*(.multiboot)
|
||||
*(.text)
|
||||
}
|
||||
|
||||
/* Read-only data. */
|
||||
.rodata BLOCK(4K) : ALIGN(4K)
|
||||
{
|
||||
*(.rodata)
|
||||
}
|
||||
|
||||
/* Read-write data (initialized) */
|
||||
.data BLOCK(4K) : ALIGN(4K)
|
||||
{
|
||||
*(.data)
|
||||
}
|
||||
|
||||
/* Read-write data (uninitialized) and stack */
|
||||
.bss BLOCK(4K) : ALIGN(4K)
|
||||
{
|
||||
*(COMMON)
|
||||
*(.bss)
|
||||
*(.bootstrap_stack)
|
||||
}
|
||||
|
||||
/* The compiler may produce other sections, by default it will put them in a segment with the same name. Simply add stuff here as needed. */
|
||||
}
|
27
main.cc
Normal file
27
main.cc
Normal file
|
@ -0,0 +1,27 @@
|
|||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#if defined(__linux__)
|
||||
#error "This file should be compiled with a cross-compiler, not the Linux system compiler!"
|
||||
#endif
|
||||
|
||||
#if !defined(__i386__)
|
||||
#error "This file should be compiled with an ix86-elf compiler!"
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Called very early, before global initialization.
|
||||
*/
|
||||
extern "C"
|
||||
void
|
||||
kearly()
|
||||
{ }
|
||||
|
||||
|
||||
/**
|
||||
* The beginning of the world...
|
||||
*/
|
||||
extern "C"
|
||||
void
|
||||
kmain()
|
||||
{ }
|
Loading…
Add table
Add a link
Reference in a new issue