From 79c0f7dfb063b5d29dc4547d37efbcb86a89fdac Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Sun, 21 Feb 2021 13:39:12 +0200 Subject: Call global constructors --- drivers/cga.cc | 4 +++- drivers/cga.h | 6 +++--- libk/stdlib.h | 1 - linker.ld | 4 ++++ src/boot.s | 3 +++ src/kernel.cc | 18 ++++++++++++++---- toolchain.makefile | 6 +++--- 7 files changed, 30 insertions(+), 12 deletions(-) diff --git a/drivers/cga.cc b/drivers/cga.cc index 961d7e4..0646ff7 100644 --- a/drivers/cga.cc +++ b/drivers/cga.cc @@ -22,7 +22,9 @@ constexpr uint8_t cursor_addr_l = 15; // 0xf */ constexpr uint8_t cursor_hide = 0b00100000; -CGA::CGA(Colour fg, Colour bg, uint32_t address) : colour_fg(fg), colour_bg(bg) { +constexpr uint32_t address = 0xB8000; + +CGA::CGA() { buffer = reinterpret_cast(address); // clear buffer diff --git a/drivers/cga.h b/drivers/cga.h index f0ca412..f4dfb3c 100644 --- a/drivers/cga.h +++ b/drivers/cga.h @@ -35,7 +35,7 @@ public: CGA_COLOR_WHITE = 15, }; - CGA(Colour fg = CGA_COLOR_BLACK, Colour bg = CGA_COLOR_LIGHT_GREY, uint32_t address = 0xB8000); + CGA(); ~CGA() = default; void set_colour(Colour fg, Colour bg); @@ -58,8 +58,8 @@ private: const size_t max_columns = 80, max_rows = 25; size_t column = 0, row = 0; - Colour colour_fg; - Colour colour_bg; + Colour colour_fg = CGA_COLOR_BLACK; + Colour colour_bg = CGA_COLOR_LIGHT_GREY; Entry* buffer; diff --git a/libk/stdlib.h b/libk/stdlib.h index 7213384..843cc14 100644 --- a/libk/stdlib.h +++ b/libk/stdlib.h @@ -21,7 +21,6 @@ private: class Console { public: - virtual ~Console() = default; virtual void write(char c) = 0; virtual void write(ViewIterator& msg) = 0; virtual void update_cursor() = 0; diff --git a/linker.ld b/linker.ld index c6c706e..eaf118c 100644 --- a/linker.ld +++ b/linker.ld @@ -27,6 +27,10 @@ SECTIONS /* Read-write data (initialized) */ .data : { + start_ctors = .; + KEEP(*(.init_array)); + KEEP(*(SORT_BY_INIT_PRIORITY(.init_array.*))); + end_ctors = .; *(.data) } diff --git a/src/boot.s b/src/boot.s index bb446d8..2286152 100644 --- a/src/boot.s +++ b/src/boot.s @@ -41,6 +41,8 @@ stack_top: bootloader will jump to this position once the kernel has been loaded. */ .section .text +.extern kernel_constructors +.extern kernel_main .global _start .type _start, @function _start: @@ -59,6 +61,7 @@ _start: 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 diff --git a/src/kernel.cc b/src/kernel.cc index 7aaaa37..37773e1 100644 --- a/src/kernel.cc +++ b/src/kernel.cc @@ -15,16 +15,26 @@ #include "idt.h" #include "serial.h" +SerialPort serial0; +CGA video0; + +typedef void (*constructor)(); + extern "C" { + +constructor start_ctors; +constructor end_ctors; +void kernel_constructors() { + for (constructor* i = &start_ctors; i != &end_ctors; ++i) (*i)(); +} + void dump_multiboot(uint32_t mb_magic, uint32_t mb_addr); void dump_gdt(); void kernel_main([[maybe_unused]] uint32_t mb_magic, [[maybe_unused]] uint32_t mb_addr) { - SerialPort s0; - if (s0.self_check()) Console::set(&s0); + if (serial0.self_check()) Console::set(&serial0); - CGA terminal; - Console::set(&terminal); + Console::set(&video0); printk("Hello, kernel World!\n"); diff --git a/toolchain.makefile b/toolchain.makefile index aa64c5b..ee9cc0e 100644 --- a/toolchain.makefile +++ b/toolchain.makefile @@ -8,11 +8,11 @@ LD := ld.lld LD_FLAGS := -T linker.ld CXX := clang++ -CXX_FLAGS := -std=c++20 -ffreestanding -nostdlib -nostdinc -nostdinc++ \ - -fno-exceptions -fno-rtti -Wall -Wextra -Werror=pedantic -O2 \ +CXX_FLAGS := -std=c++20 \ + -mkernel -ffreestanding -nostdlib -nostdinc -nostdinc++ \ + -Wall -Wextra -Werror=pedantic -O2 \ -Werror=date-time \ -Werror=shadow-all \ - -Wnon-virtual-dtor \ -Wold-style-cast -Wconversion \ -Wconsumed CXX_INCLUDE := $(CURDIR)/libk $(CURDIR)/drivers -- cgit v1.2.1