From 65ba98a0893c3f6c47247980b09993440ed6eef6 Mon Sep 17 00:00:00 2001 From: Kevin Alavik Date: Thu, 15 May 2025 20:47:49 +0200 Subject: [PATCH 1/2] feat/kernel: Added support for Kconfig and flanterm if needed --- .gitmodules | 3 +++ external/flanterm | 1 + 2 files changed, 4 insertions(+) create mode 100644 .gitmodules create mode 160000 external/flanterm diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..da7b995 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "external/flanterm"] + path = external/flanterm + url = https://codeberg.org/mintsuki/flanterm diff --git a/external/flanterm b/external/flanterm new file mode 160000 index 0000000..bdecdcb --- /dev/null +++ b/external/flanterm @@ -0,0 +1 @@ +Subproject commit bdecdcb1b7dad05ab5249e2c3e4d2c27a661e864 From 89795c4ad8b30454a3be9b21a1494546d44730db Mon Sep 17 00:00:00 2001 From: Kevin Alavik Date: Thu, 15 May 2025 20:47:52 +0200 Subject: [PATCH 2/2] feat/kernel: Added support for Kconfig and flanterm if needed --- .gitignore | 3 +- .vscode/c_cpp_properties.json | 4 +- .vscode/settings.json | 6 ++- GNUmakefile | 2 +- kernel/GNUmakefile | 87 +++++++++++++++++++++++++++++++---- kernel/Kconfig | 33 +++++++++++++ kernel/src/boot/emk.h | 15 ++++++ kernel/src/emk.c | 35 +++++++++++++- kernel/src/util/kprintf.c | 10 +++- 9 files changed, 179 insertions(+), 16 deletions(-) create mode 100644 kernel/Kconfig diff --git a/.gitignore b/.gitignore index e2113f6..8050cef 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ bin/ limine/ ovmf/ release/ -*.log \ No newline at end of file +*.log +.config* \ No newline at end of file diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json index df35a0b..aae99e6 100644 --- a/.vscode/c_cpp_properties.json +++ b/.vscode/c_cpp_properties.json @@ -10,7 +10,9 @@ "cppStandard": "${default}", "intelliSenseMode": "linux-gcc-x64", "defines": [ - "LIMINE_API_REVISION=3" + "LIMINE_API_REVISION=3", + "FLANTERM_SUPPORT=1", + "BUILD_MODE=\"\"" ] } ], diff --git a/.vscode/settings.json b/.vscode/settings.json index ef55fb4..3d27437 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -13,6 +13,10 @@ "emk.h": "c", "heap.h": "c", "vmm.h": "c", - "align.h": "c" + "align.h": "c", + "cpu.h": "c", + "nanoprintf.h": "c", + "flanterm.h": "c", + "serial.h": "c" } } \ No newline at end of file diff --git a/GNUmakefile b/GNUmakefile index aa9b207..8154114 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -2,7 +2,7 @@ MAKEFLAGS += -rR .SUFFIXES: -QEMUFLAGS := -m 2G -serial stdio -display none +QEMUFLAGS := -m 2G -serial stdio USER_QEMUFLAGS ?= IMAGE_NAME := release/emk diff --git a/kernel/GNUmakefile b/kernel/GNUmakefile index 587e15c..04a90ba 100644 --- a/kernel/GNUmakefile +++ b/kernel/GNUmakefile @@ -8,19 +8,34 @@ BINDIR := bin TARGET := $(BINDIR)/emk.elf +KCONFIG ?= kconfig-mconf +CONFIG := .config + +ifneq ("$(wildcard $(CONFIG))","") + include $(CONFIG) +endif + CC := gcc AS := gcc NASM := nasm -# Build mode configuration -BUILD_MODE ?= dev +ifeq ($(BUILD_MODE),) + ifeq ($(CONFIG_BUILD_MODE_DEBUG),y) + BUILD_MODE := debug + else ifeq ($(CONFIG_BUILD_MODE_RELEASE),y) + BUILD_MODE := release + else + BUILD_MODE := debug + endif +endif + ifeq ($(BUILD_MODE),release) CFLAGS := -Os -pipe -Wall -Wextra -Werror -std=gnu11 -ffreestanding \ -fno-stack-protector -fno-stack-check -fno-PIC \ -ffunction-sections -fdata-sections -m64 -march=x86-64 \ -mno-80387 -mno-mmx -mno-sse -mno-sse2 -mno-red-zone \ -mcmodel=kernel -fno-unwind-tables -fno-asynchronous-unwind-tables \ - -s -Wno-unused-variable + -s -Wno-unused-variable -DBUILD_MODE=\"release\" NASMFLAGS := LDFLAGS := -nostdlib -static -z max-page-size=0x1000 -Wl,--gc-sections \ -T linker.ld -Wl,-m,elf_x86_64 -Wl,--strip-all @@ -29,7 +44,7 @@ else -fno-stack-protector -fno-stack-check -fno-PIC \ -ffunction-sections -fdata-sections -m64 -march=x86-64 \ -mno-80387 -mno-mmx -mno-sse -mno-sse2 -mno-red-zone \ - -mcmodel=kernel -Wno-unused-variable + -mcmodel=kernel -Wno-unused-variable -DBUILD_MODE=\"dev\" NASMFLAGS := -F dwarf -g LDFLAGS := -nostdlib -static -z max-page-size=0x1000 -Wl,--gc-sections \ -T linker.ld -Wl,-m,elf_x86_64 @@ -37,14 +52,48 @@ endif CPPFLAGS := -I../external -I$(SRCDIR) -MMD -MP -DLIMINE_API_REVISION=3 -SRCS := $(shell find $(SRCDIR) -type f \( -name '*.c' -o -name '*.S' -o -name '*.asm' \)) -OBJS := $(patsubst %.c,$(OBJDIR)/%.o,$(filter %.c,$(SRCS))) \ - $(patsubst %.S,$(OBJDIR)/%.o,$(filter %.S,$(SRCS))) \ - $(patsubst %.asm,$(OBJDIR)/%.o,$(filter %.asm,$(SRCS))) +IMPLICIT_SRCS := +ifeq ($(CONFIG_KERNEL_HEAP_FF),y) + IMPLICIT_SRCS += src/mm/heap/ff.c +else + $(error Error: No heap algorithm was defined. Please run "make menuconfig" and select one.) +endif + +ifeq ($(CONFIG_ENABLE_FLANTERM),y) + IMPLICIT_SRCS += ../external/flanterm/flanterm.c + IMPLICIT_SRCS += ../external/flanterm/backends/fb.c + CFLAGS += -I../external/flanterm/ -DFLANTERM_SUPPORT=1 +endif + +EXCLUDE_SRCS := \ + src/mm/heap/ff.c \ + ../external/flanterm/flanterm.c \ + ../external/flanterm/backends/fb.c +EXCLUDE_PATTERNS := $(foreach file,$(EXCLUDE_SRCS),! -path "$(file)") + +SRCS := $(IMPLICIT_SRCS) \ + $(shell find $(SRCDIR) -type f \( -name '*.c' -o -name '*.S' -o -name '*.asm' \) $(EXCLUDE_PATTERNS)) + +define src_to_obj +$(OBJDIR)/$(patsubst ../%,%,$(1:.c=.o)) +endef + +define src_to_obj_s +$(OBJDIR)/$(patsubst ../%,%,$(1:.S=.o)) +endef + +define src_to_obj_asm +$(OBJDIR)/$(patsubst ../%,%,$(1:.asm=.o)) +endef + +OBJS := +OBJS += $(foreach f,$(filter %.c,$(SRCS)),$(call src_to_obj,$(f))) +OBJS += $(foreach f,$(filter %.S,$(SRCS)),$(call src_to_obj_s,$(f))) +OBJS += $(foreach f,$(filter %.asm,$(SRCS)),$(call src_to_obj_asm,$(f))) DEPS := $(OBJS:.o=.d) -.PHONY: all clean distclean +.PHONY: all clean distclean menuconfig all: $(TARGET) $(OBJDIR) $(BINDIR): @@ -54,16 +103,31 @@ $(TARGET): $(OBJS) | $(BINDIR) @echo " LD $@" @$(CC) $(LDFLAGS) -o $@ $(OBJS) +$(OBJDIR)/%.o: ../%.c | $(OBJDIR) + @mkdir -p $(dir $@) + @echo " CC ../$*" + @$(CC) $(CPPFLAGS) $(CFLAGS) -c ../$*.c -o $@ + $(OBJDIR)/%.o: %.c | $(OBJDIR) @mkdir -p $(dir $@) @echo " CC $<" @$(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@ +$(OBJDIR)/%.o: ../%.S | $(OBJDIR) + @mkdir -p $(dir $@) + @echo " AS ../$*" + @$(AS) $(CPPFLAGS) $(CFLAGS) -c ../$*.S -o $@ + $(OBJDIR)/%.o: %.S | $(OBJDIR) @mkdir -p $(dir $@) @echo " AS $<" @$(AS) $(CPPFLAGS) $(CFLAGS) -c $< -o $@ +$(OBJDIR)/%.o: ../%.asm | $(OBJDIR) + @mkdir -p $(dir $@) + @echo " NASM ../$*" + @$(NASM) $(NASMFLAGS) ../$*.asm -o $@ + $(OBJDIR)/%.o: %.asm | $(OBJDIR) @mkdir -p $(dir $@) @echo " NASM $<" @@ -77,4 +141,7 @@ distclean: clean @echo " DISTCLEAN" @rm -rf $(BINDIR) --include $(DEPS) \ No newline at end of file +menuconfig: clean + @$(KCONFIG) Kconfig + +-include $(DEPS) diff --git a/kernel/Kconfig b/kernel/Kconfig new file mode 100644 index 0000000..af19094 --- /dev/null +++ b/kernel/Kconfig @@ -0,0 +1,33 @@ +mainmenu "EMK Build Configuration" + +choice + prompt "Build Mode" + default BUILD_MODE_DEBUG + +config BUILD_MODE_DEBUG + bool "Debug" + help + Build with debug symbols and no optimizations. + +config BUILD_MODE_RELEASE + bool "Release" + help + Build with optimizations and no debug symbols. + +endchoice + +choice + prompt "Kernel Heap Algorithm" + default KERNEL_HEAP_FF + +config KERNEL_HEAP_FF + bool "First-Fit" + help + Use the First-Fit memory allocation algorithm (ff.c). + +endchoice + +config ENABLE_FLANTERM + bool "Enable Flanterm Support" + help + Includes support for the Flanterm terminal emulator. Useful for debugging real hardware. diff --git a/kernel/src/boot/emk.h b/kernel/src/boot/emk.h index a083268..f78d7d5 100644 --- a/kernel/src/boot/emk.h +++ b/kernel/src/boot/emk.h @@ -5,6 +5,9 @@ #include #include #include +#if FLANTERM_SUPPORT +#include +#endif // FLANTERM_SUPPORT extern uint64_t hhdm_offset; extern struct limine_memmap_response *memmap; @@ -18,4 +21,16 @@ extern vctx_t *kvm_ctx; #define BIT(x) (1ULL << (x)) +#ifndef FLANTERM_SUPPORT +#define FLANTERM_SUPPORT 0 +#endif // FLANTERM_SUPPORT + +#ifndef BUILD_MODE +#define BUILD_MODE "unknown" +#endif // BUILD_MODE + +#if FLANTERM_SUPPORT +extern struct flanterm_context *ft_ctx; +#endif // FLANTERM_SUPPORT + #endif // EMK_H \ No newline at end of file diff --git a/kernel/src/emk.c b/kernel/src/emk.c index 7eab3cf..77c81b1 100644 --- a/kernel/src/emk.c +++ b/kernel/src/emk.c @@ -13,6 +13,10 @@ #include #include #include +#if FLANTERM_SUPPORT +#include +#include +#endif // FLANTERM_SUPPORT __attribute__((used, section(".limine_requests"))) static volatile LIMINE_BASE_REVISION(3); __attribute__((used, section(".limine_requests"))) static volatile struct limine_memmap_request memmap_request = { @@ -24,6 +28,11 @@ __attribute__((used, section(".limine_requests"))) static volatile struct limine __attribute__((used, section(".limine_requests"))) volatile struct limine_executable_address_request kernel_address_request = { .id = LIMINE_EXECUTABLE_ADDRESS_REQUEST, .response = 0}; +#if FLANTERM_SUPPORT +__attribute__((used, section(".limine_requests"))) volatile struct limine_framebuffer_request framebuffer_request = { + .id = LIMINE_FRAMEBUFFER_REQUEST, + .response = 0}; +#endif // FLANTERM_SUPPORT __attribute__((used, section(".limine_requests_start"))) static volatile LIMINE_REQUESTS_START_MARKER; __attribute__((used, section(".limine_requests_end"))) static volatile LIMINE_REQUESTS_END_MARKER; @@ -34,6 +43,10 @@ uint64_t kphys = 0; uint64_t kstack_top = 0; vctx_t *kvm_ctx = NULL; +#if FLANTERM_SUPPORT +struct flanterm_context *ft_ctx = NULL; +#endif // FLANTERM_SUPPORT + void emk_entry(void) { __asm__ volatile("movq %%rsp, %0" : "=r"(kstack_top)); @@ -42,8 +55,28 @@ void emk_entry(void) /* Just halt and say nothing */ hcf(); } + + /* Init flanterm if we compiled with support */ +#if FLANTERM_SUPPORT + struct limine_framebuffer *framebuffer = framebuffer_request.response->framebuffers[0]; + ft_ctx = flanterm_fb_init( + NULL, + NULL, + framebuffer->address, framebuffer->width, framebuffer->height, framebuffer->pitch, + framebuffer->red_mask_size, framebuffer->red_mask_shift, + framebuffer->green_mask_size, framebuffer->green_mask_shift, + framebuffer->blue_mask_size, framebuffer->blue_mask_shift, + NULL, + NULL, NULL, + NULL, NULL, + NULL, NULL, + NULL, 0, 0, 1, + 0, 0, + 0); +#endif // FLANTERM_SUPPORT + log_early("Experimental Micro Kernel (EMK) 1.0 Copytright (c) 2025 Piraterna"); - log_early("Compiled at %s %s", __TIME__, __DATE__); + log_early("Compiled at %s %s, emk1.0-%s, flanterm support: %s", __TIME__, __DATE__, BUILD_MODE, FLANTERM_SUPPORT ? "yes" : "no"); if (!LIMINE_BASE_REVISION_SUPPORTED) { diff --git a/kernel/src/util/kprintf.c b/kernel/src/util/kprintf.c index d15c989..be9a8b3 100644 --- a/kernel/src/util/kprintf.c +++ b/kernel/src/util/kprintf.c @@ -1,6 +1,10 @@ /* EMK 1.0 Copyright (c) 2025 Piraterna */ -#include #include +#include +#include +#if FLANTERM_SUPPORT +#include +#endif // FLANTERM_SUPPORT #define NANOPRINTF_USE_FIELD_WIDTH_FORMAT_SPECIFIERS 1 #define NANOPRINTF_USE_PRECISION_FORMAT_SPECIFIERS 1 @@ -22,6 +26,10 @@ int kprintf(const char *fmt, ...) if (length >= 0 && length < (int)sizeof(buffer)) { serial_write(COM1, (uint8_t *)buffer, length); +#if FLANTERM_SUPPORT + if (ft_ctx) + flanterm_write(ft_ctx, (char *)buffer, length); +#endif // FLANTERM_SUPPORT } va_end(args);