aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAqua-sama <aqua@iserlohn-fortress.net>2021-02-03 23:39:03 +0200
committerAqua-sama <aqua@iserlohn-fortress.net>2021-02-03 23:39:43 +0200
commit039df4c04d83fc26ca60d4f870d2e58d381818ad (patch)
tree0896053cffebfe97ac98c98386d62ef7f2e980b1
parentUpdate readme with required packages (diff)
downloadkernel.cpp-039df4c04d83fc26ca60d4f870d2e58d381818ad.tar.xz
Rewrite boot.s to use clang instead of nasm
-rw-r--r--boot.asm98
-rw-r--r--boot.s93
-rw-r--r--linker.ld13
-rw-r--r--makefile15
-rw-r--r--readme.md1
5 files changed, 105 insertions, 115 deletions
diff --git a/boot.asm b/boot.asm
deleted file mode 100644
index d787d64..0000000
--- a/boot.asm
+++ /dev/null
@@ -1,98 +0,0 @@
-; Declare constants for the multiboot header.
-MAGIC equ 0xE85250D6 ; multiboot2 magic
-ARCH equ 0 ; protected mode i386
-
-; Declare a multiboot header that marks the program as a kernel. These are magic
-; values that are documented in the multiboot standard. The bootloader will
-; search for this signature in the first 8 KiB of the kernel file, aligned at a
-; 32-bit boundary. The signature is in its own section so the header can be
-; forced to be within the first 8 KiB of the kernel file.
-section .multiboot
-header_start:
- dd MAGIC
- dd ARCH
- dd header_end - header_start ; header length
- dd 0x100000000 - (MAGIC + ARCH + (header_end - header_start))
-
- ; optional multiboot tags here
-
- ; required end tag (u16, u16, u32) = (0, 0, 8)
- dw 0 ; type
- dw 0 ; flags
- dd 8 ; size
-header_end:
-
-; The multiboot standard does not define the value of the stack pointer register
-; (esp) and it is up to the kernel to provide a stack. This allocates room for a
-; small stack by creating a symbol at the bottom of it, then allocating 16384
-; bytes for it, and finally creating a symbol at the top. The stack grows
-; downwards on x86. The stack is in its own section so it can be marked nobits,
-; which means the kernel file is smaller because it does not contain an
-; uninitialized stack. 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 .bss
-align 16
-stack_bottom:
- resb 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.
-; Declare _start as a function symbol with the given symbol size.
-section .text
-global _start:function (_start.end - _start)
-_start:
- ; The bootloader has loaded us into 32-bit protected mode on a x86
- ; machine. Interrupts are disabled. Paging is disabled. The processor
- ; state is as defined in the multiboot standard. The kernel has full
- ; control of the CPU. The kernel can only make use of hardware features
- ; and any code it provides as part of itself. There's no printf
- ; function, unless the kernel provides its own <stdio.h> header and a
- ; printf implementation. There are no security restrictions, no
- ; safeguards, no debugging mechanisms, only what the kernel provides
- ; itself. It has absolute and complete power over the
- ; machine.
-
- ; To set up a stack, we set the esp register to point to the top of our
- ; stack (as it grows downwards on x86 systems). This is necessarily done
- ; in assembly as languages such as C cannot function without a stack.
- mov esp, stack_top
- push ebx
- push eax
-
- ; 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.
-
- ; 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 since pushed a multiple of 16 bytes to the
- ; stack since (pushed 0 bytes so far) and the alignment is thus
- ; preserved and the call is well defined.
- ; note, that if you are building on Windows, C functions may have "_" prefix in assembly: _kernel_main
- extern kernel_main
- 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
-.end:
diff --git a/boot.s b/boot.s
new file mode 100644
index 0000000..bb446d8
--- /dev/null
+++ b/boot.s
@@ -0,0 +1,93 @@
+/*
+ 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
+.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:
+
+/*
+ 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 .text
+.global _start
+.type _start, @function
+_start:
+ 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
+
+ /*
+ 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
+
+/*
+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
diff --git a/linker.ld b/linker.ld
index 7e07efc..a0bb74f 100644
--- a/linker.ld
+++ b/linker.ld
@@ -1,9 +1,8 @@
-/* The bootloader will look at this image and start execution at the symbol
- designated as the entry point. */
ENTRY(_start)
OUTPUT_FORMAT(elf32-i386)
-OUTPUT_ARCH(i386:i386)
-
+
+SEARCH_DIR(libk)
+
/* Tell where the various sections of the object files will be put in the final
kernel image. */
SECTIONS
@@ -15,12 +14,9 @@ SECTIONS
/* First put the multiboot header, as it is required to be put very early
early in the image or the bootloader won't recognize the file format.
Next we'll put the .text section. */
- .boot :
- {
- *(.multiboot)
- }
.text :
{
+ *(.multiboot)
*(.text)
}
@@ -41,6 +37,7 @@ SECTIONS
{
*(COMMON)
*(.bss)
+ *(.stack)
}
/* The compiler may produce other sections, by default it will put them in
diff --git a/makefile b/makefile
index b149337..9c2728c 100644
--- a/makefile
+++ b/makefile
@@ -6,12 +6,11 @@
export TARGET := i686-elf
export AR := llvm-ar
-export NASM := nasm
+export AS := clang
export LD := ld.lld
-export CXX := clang++
-export CXX_FLAGS := -std=c++20 -ffreestanding \
- -nostdlib -nostdinc -nostdinc++ \
- -fno-exceptions -fno-rtti -Wall -Wextra -O2
+export CXX := clang
+export CXX_FLAGS := -std=c++20 -ffreestanding -nostdlib -nostdinc -nostdinc++ \
+ -fno-exceptions -fno-rtti -Wall -Wextra -O2
CXX_INCLUDE := $(CURDIR)/libk
CXX_OBJ := kernel.o vga.o
@@ -25,10 +24,10 @@ check-grub: glitch.elf
grub-file --is-x86-multiboot2 glitch.elf
glitch.elf: boot.o $(CXX_OBJ) libk/libk.a
- $(LD) -T linker.ld -o glitch.elf boot.o $(CXX_OBJ) -Llibk -lk
+ $(LD) -T linker.ld -o glitch.elf boot.o $(CXX_OBJ) -lk
-boot.o: boot.asm
- $(NASM) -felf32 -o $@ $^
+boot.o: boot.s
+ $(AS) -target $(TARGET) -nostdlib -Wall -Wextra -c $^ -o $@
$(CXX_OBJ): %.o : %.cc
$(CXX) -target $(TARGET) $(CXX_FLAGS) -I$(CXX_INCLUDE) -c $^ -o $@
diff --git a/readme.md b/readme.md
index 5f6a1ad..59bbbcb 100644
--- a/readme.md
+++ b/readme.md
@@ -5,7 +5,6 @@
build tools | arch package
------------|--------------
GNU make | make
- nasm | nasm
clang++ | clang
llvm-ar | llvm
ld.lld | lld