From 5e3c17ee56730f14c8c6a1f89adc077fd5b612c2 Mon Sep 17 00:00:00 2001 From: Victor Timofei Date: Mon, 25 Apr 2022 22:16:07 +0300 Subject: [PATCH] Create bare bones kernel The kernel supports writing to Graphics framebuffer in UEFI machines. --- .gitignore | 3 + Makefile | 57 ++++++++++++++-- fonts/Makefile | 26 ++++++++ fonts/gen_srd_file.sh | 19 ++++++ kernel/Makefile | 29 ++++++++ kernel/boot.s | 152 ++++++++++++++++++++++++++++++++++++++++++ kernel/kernel.c | 71 ++++++++++++++++++++ kernel/linker.ld | 43 ++++++++++++ kernel/multiboot.h | 65 ++++++++++++++++++ kernel/psf.h | 20 ++++++ 10 files changed, 478 insertions(+), 7 deletions(-) create mode 100644 fonts/Makefile create mode 100755 fonts/gen_srd_file.sh create mode 100644 kernel/Makefile create mode 100644 kernel/boot.s create mode 100644 kernel/kernel.c create mode 100644 kernel/linker.ld create mode 100644 kernel/multiboot.h create mode 100644 kernel/psf.h diff --git a/.gitignore b/.gitignore index cc01102..2585d0c 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,6 @@ binutils.tar.gz cross/ gcc-*/ binutils-*/ +*.o +build/ +.ccls-cache/ diff --git a/Makefile b/Makefile index cf1ea79..9f3e56d 100644 --- a/Makefile +++ b/Makefile @@ -1,12 +1,44 @@ -BINUTILS_VERSION=2.38 -GCC_VERSION=11.2.0 -PREFIX=${PWD}/cross -CROSS_BIN=${PREFIX}/bin -TARGET=i686-elf -export PATH:=${CROSS_BIN}:$(PATH) +BINUTILS_VERSION := 2.38 +GCC_VERSION := 11.2.0 +PREFIX := ${PWD}/cross +CROSS_BIN := ${PREFIX}/bin +BUILD_DIR := ./build +TARGET := i686-elf +FONTS := default8x16.o + +BUILD_DIR_ABS := $(abspath $(BUILD_DIR)) + +FONT_OBJS := $(patsubst %.o,$(BUILD_DIR_ABS)/%.o, $(FONTS)) + +CROSS_AS := ${TARGET}-as +CROSS_CC := ${TARGET}-gcc +CROSS_LD := ${TARGET}-ld +CFLAGS := "-std=gnu99 -ffreestanding -O2 -Wall -Wextra -I$$PWD/kernel" +LDFLAGS := "-ffreestanding -O2 -nostdlib -lgcc" + +export PATH := ${CROSS_BIN}:$(PATH) + +.PHONY: all +all: $(BUILD_DIR)/kernel.bin $(BUILD_DIR)/$(FONTS) + +$(BUILD_DIR)/kernel.bin: $(BUILD_DIR) $(BUILD_DIR)/$(FONTS) + @$(MAKE) -C kernel \ + BUILD_DIR=$(abspath $(BUILD_DIR)) \ + CROSS_AS=${CROSS_AS} \ + CROSS_CC=${CROSS_CC} \ + CROSS_LD=${CROSS_LD} \ + CFLAGS=${CFLAGS} \ + EXTRA_OBJS=${FONT_OBJS} \ + LDFLAGS=${LDFLAGS} + +$(BUILD_DIR): + @mkdir -p $@ + +$(BUILD_DIR)/$(FONTS): + @$(MAKE) -C fonts BUILD_DIR=$(abspath $(BUILD_DIR)) OBJECTS=$(FONTS) .PHONY: crossdev -crossdev: install-binutils install-gcc install-target-libgcc +crossdev: install-binutils install-gcc install-target-libgcc clean-cross .PHONY: install-binutils install-binutils: build-binutils ${CROSS_BIN} @@ -77,3 +109,14 @@ binutils.tar.gz: gcc.tar.gz: @curl -o $@ https://ftp.gnu.org/gnu/gcc/gcc-${GCC_VERSION}/gcc-${GCC_VERSION}.tar.gz +.PHONY: clean-cross +clean-cross: + @rm -f gcc.tar.gz + @rm -f binutils.tar.gz + @rm -rf gcc-$(GCC_VERSION) + @rm -rf binutils-$(BINUTILS_VERSION) + +.PHONY: clean +clean: + @rm -rf $(BUILD_DIR) + diff --git a/fonts/Makefile b/fonts/Makefile new file mode 100644 index 0000000..ad0b993 --- /dev/null +++ b/fonts/Makefile @@ -0,0 +1,26 @@ +OBJECTS_GZ := $(patsubst %.o, /usr/share/kbd/consolefonts/%.psfu.gz, $(OBJECTS)) +OBJECTS_PSF := $(patsubst %.o, $(BUILD_DIR)/%.psfu, $(OBJECTS)) +SYMS_PREFIX := consolefonts + +all: $(BUILD_DIR)/$(OBJECTS) + +$(BUILD_DIR)/%.o: $(OBJECTS_PSF) + @objcopy \ + -O elf32-i386 \ + -B i386 \ + -I binary \ + $< $@ + @./gen_srd_file.sh \ + $<.srd \ + $< \ + $(SYMS_PREFIX) + @objcopy \ + --redefine-syms $<.srd \ + $@ + @rm $<.srd + +$(BUILD_DIR)/%.psfu: $(BUILD_DIR)/%.psfu.gz + @gzip -d $@ + +$(BUILD_DIR)/%.psfu.gz: $(OBJECTS_GZ) + @cp $< $(BUILD_DIR) diff --git a/fonts/gen_srd_file.sh b/fonts/gen_srd_file.sh new file mode 100755 index 0000000..0df2025 --- /dev/null +++ b/fonts/gen_srd_file.sh @@ -0,0 +1,19 @@ +#!/usr/bin/bash + +# Generates a symbol redefenition file for `ojbcopy` + +# The symbol redefenition filename +sym_redefinition_filename=$1 + +# The original filename from which the object was created +origin_filename=$2 + +# The symbol prefix +prefix=$3 + +path_prefix=$(readlink -f "${origin_filename}" | sed -E 's#/|\.#_#g') +syms_prefix="_binary_${path_prefix}" + +echo "${syms_prefix}_start ${prefix}_binary__start" >> ${sym_redefinition_filename} +echo "${syms_prefix}_end ${prefix}_binary__end" >> ${sym_redefinition_filename} +echo "${syms_prefix}_size ${prefix}_binary__size" >> ${sym_redefinition_filename} diff --git a/kernel/Makefile b/kernel/Makefile new file mode 100644 index 0000000..a3c0330 --- /dev/null +++ b/kernel/Makefile @@ -0,0 +1,29 @@ + +SOURCES_S := boot.s +OBJECTS_S := $(patsubst %.s, $(BUILD_DIR)/kernel/%.o, $(SOURCES_S)) + +SOURCES_C := kernel.c +HEADERS_C := multiboot.h psf.h +OBJECTS_C := $(patsubst %.c, $(BUILD_DIR)/kernel/%.o, $(SOURCES_C)) + +LINKER_LD := linker.ld + +.PHONY: all +all: $(BUILD_DIR)/kernel.bin + +$(BUILD_DIR)/kernel.bin: $(OBJECTS_S) $(OBJECTS_C) $(LINKER_LD) + $(CROSS_CC) \ + -T $(LINKER_LD) \ + $(LDFLAGS) \ + $(OBJECTS_C) \ + $(OBJECTS_S) \ + $(EXTRA_OBJS) \ + -o $@ + +$(BUILD_DIR)/kernel/%.o: %.c $(SOURCES_C) + @mkdir -p $(dir $@) + @$(CROSS_CC) -c $< -o $@ $(CFLAGS) + +$(BUILD_DIR)/kernel/%.o: %.s $(SOURCES_S) + @mkdir -p $(dir $@) + @$(CROSS_AS) $< -o $@ diff --git a/kernel/boot.s b/kernel/boot.s new file mode 100644 index 0000000..d6f19e4 --- /dev/null +++ b/kernel/boot.s @@ -0,0 +1,152 @@ +/* Declare constants for the multiboot header */ +.set ALIGN, 1<<0 /* align loaded modules on page boundaries */ +.set MEMINFO, 1<<1 /* provide memory map */ +.set VIDEO, 1<<2 /* enable graphics framebuffer */ +.set FLAGS, ALIGN | MEMINFO | VIDEO /* 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 */ + +/* +These are not used, but are needed for padding since we enable video and we +are interested in the graphics field of the multiboot header. +*/ +.set HEADER_ADDR, 0x0 +.set LOAD_ADDR, 0x0 +.set LOAD_END_ADDR, 0x0 +.set BSS_END_ADDR, 0x0 +.set ENTRY_ADDR, 0x0 + +/* Graphics field of the multiboot header */ +.set MODE_TYPE, 0x0 /* Contains ‘0’ for linear graphics mode or ‘1’ for EGA-standard text mode */ +.set WIDTH, 0x0 /* Contains the number of the columns */ +.set HEIGHT, 0x0 /* Contains the number of the lines */ +.set DEPTH, 0x0 /* Contains the number of bits per pixel in a graphics mode, and zero in a text mode */ + +/* +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 +.align 4 +.long MAGIC +.long FLAGS +.long CHECKSUM +.long HEADER_ADDR +.long LOAD_ADDR +.long LOAD_END_ADDR +.long BSS_END_ADDR +.long ENTRY_ADDR +.long MODE_TYPE +.long WIDTH +.long HEIGHT +.long DEPTH + +/* +The multiboot header layout is as follows: +Offset Type Field Name Note +0 u32 magic required +4 u32 flags required +8 u32 checksum required +12 u32 header_addr if flags[16] is set +16 u32 load_addr if flags[16] is set +20 u32 load_end_addr if flags[16] is set +24 u32 bss_end_addr if flags[16] is set +28 u32 entry_addr if flags[16] is set +32 u32 mode_type if flags[2] is set +36 u32 width if flags[2] is set +40 u32 height if flags[2] is set +44 u32 depth if flags[2] is set +*/ + +/* +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, the 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 no 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: +.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: + /* + 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 header and a + printf implementation. There are no security restrictions, no + safeguard, no debugging mechanisms, only what the kernel provides + itself. It has absolute and comlete power over the machine. + */ + + /* + To set up a stack, we set the esp register to point to the top of the + 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 $stack_top, %esp + + /* + 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 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. + */ + pushl %ebx /* pass the Multiboot Info struct addr to 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 +1: hlt + jmp 1b + +/* +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/kernel/kernel.c b/kernel/kernel.c new file mode 100644 index 0000000..4d1b050 --- /dev/null +++ b/kernel/kernel.c @@ -0,0 +1,71 @@ +#include "multiboot.h" +#include "psf.h" +#include + +#if defined (__linux__) +#error "You are not using a cross-compiler, you will most certainly run into trouble" +#endif + +#if !defined (__i386__) +#error "The kernel needs to be compiled with a ix86-elf compiler" +#endif + +/* graphics framebuffer */ +char *fb; +/* number of bytes in each line */ +int scanline; +/* extern the symbols in the psf object */ +extern char consolefonts_binary__start; +extern char consolefonts_binary__end; + +multiboot_info_t *mbi; +uint32_t console_y; +uint32_t console_x; + +#define PIXEL uint32_t + +void putchar (uint16_t c, int32_t cx, int32_t cy, uint32_t fg, uint32_t bg) +{ + psf_font_t *font = (psf_font_t *)&consolefonts_binary__start; + uint32_t bytesperline = (font->width + 7) / 8; + + unsigned char *glyph = (unsigned char *)&consolefonts_binary__start + font->headersize + + (c > 0 && c < font->numglyphs ? c : 0) * font->bytesperglyph; + + uint32_t offs = (cy * font->height * scanline) + (cx * (font->width + 1) * sizeof (PIXEL)); + + uint32_t x, y, line, mask; + for (y = 0; y < font->height; y++) { + line = offs; + mask = 1 << (font->width -1); + for (x = 0; x < font->width; x++) { + *((PIXEL *)(fb+line)) = *((uint32_t *)glyph) & mask ? fg : bg; + mask >>= 1; + line += sizeof (PIXEL); + } + glyph += bytesperline; + offs += scanline; + } + +} + +void kernel_main (uint64_t multiboot_struct_addr) +{ + mbi = (multiboot_info_t *)multiboot_struct_addr; + + fb = (char *)mbi->framebuffer_addr; + scanline = mbi->framebuffer_pitch; + + putchar ((uint16_t)'H', 0, 0, 0xFFFFFFFF, 0); + putchar ((uint16_t)'e', 1, 0, 0xFFFFFFFF, 0); + putchar ((uint16_t)'l', 2, 0, 0xFFFFFFFF, 0); + putchar ((uint16_t)'l', 3, 0, 0xFFFFFFFF, 0); + putchar ((uint16_t)'o', 4, 0, 0xFFFFFFFF, 0); + putchar ((uint16_t)' ', 5, 0, 0xFFFFFFFF, 0); + putchar ((uint16_t)'W', 6, 0, 0xFFFFFFFF, 0); + putchar ((uint16_t)'o', 7, 0, 0xFFFFFFFF, 0); + putchar ((uint16_t)'r', 8, 0, 0xFFFFFFFF, 0); + putchar ((uint16_t)'l', 9, 0, 0xFFFFFFFF, 0); + putchar ((uint16_t)'d', 10, 0, 0xFFFFFFFF, 0); + putchar ((uint16_t)'!', 11, 0, 0xFFFFFFFF, 0); +} diff --git a/kernel/linker.ld b/kernel/linker.ld new file mode 100644 index 0000000..4bb5a45 --- /dev/null +++ b/kernel/linker.ld @@ -0,0 +1,43 @@ +/* The bootloader will look at this image and start executing 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 1MiB, 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 recongnize 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) + } + + /* 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. */ +} diff --git a/kernel/multiboot.h b/kernel/multiboot.h new file mode 100644 index 0000000..3a8fa83 --- /dev/null +++ b/kernel/multiboot.h @@ -0,0 +1,65 @@ +#include + +#ifndef MULTIBOOT_HEADER +#define MULTIBOOT_HEADER 1 + +struct multiboot_info { + uint32_t flags; + + /* present if flags[0] is set */ + uint32_t mem_lower; + uint32_t mem_upper; + + /* present if flags[1] is set */ + uint32_t boot_device; + + /* present if flags[2] is set */ + uint32_t cmd_line; + + /* present if flags[3] is set */ + uint32_t mods_count; + uint32_t mods_addr; + + /* present if flags[4] or flags[5] is set */ + uint32_t syms1; + uint32_t syms2; + uint32_t syms3; + + /* present if flags[6] is set */ + uint32_t mmap_length; + uint32_t mmap_addr; + + /* present if flags[7] is set */ + uint32_t drives_length; + uint32_t drives_addr; + + /* present if flags[8] is set */ + uint32_t config_table; + + /* present if flags[9] is set */ + uint32_t boot_loader_name; + + /* present if flags[10] is set */ + uint32_t apm_table; + + /* present if flags[11] is set */ + uint32_t vbe_control_info; + uint32_t vbe_mode_info; + uint16_t vbe_mode; + uint16_t vbe_interface_seg; + uint16_t vbe_interface_off; + uint16_t vbe_interface_len; + + /* present if flags[12] is set */ + uint64_t framebuffer_addr; + uint32_t framebuffer_pitch; + uint32_t framebuffer_width; + uint32_t framebuffer_height; + uint8_t framebuffer_bpp; + uint8_t framebuffer_type; + uint32_t color_info1; + uint16_t color_info2; +}__attribute__((packed)); +typedef struct multiboot_info multiboot_info_t; + +#endif diff --git a/kernel/psf.h b/kernel/psf.h new file mode 100644 index 0000000..d2334a1 --- /dev/null +++ b/kernel/psf.h @@ -0,0 +1,20 @@ +#include + +#ifndef PSF_HEADER +#define PSF_HEADER 1 + +#define PSF_FONT_MAGIC 0x864ab572 + +struct psf_font { + uint32_t magic; /* magic bytes to identify psf */ + uint32_t version; /* zero */ + uint32_t headersize; /* offset of bitmaps in file, 32 */ + uint32_t flags; /* 0 if there's no unicode table */ + uint32_t numglyphs; /* number of glyphs */ + uint32_t bytesperglyph; /* size of each glyph */ + uint32_t height; /* height in pixels */ + uint32_t width; /* width in pixels */ +}; +typedef struct psf_font psf_font_t; + +#endif