diff options
| -rw-r--r-- | .gitignore | 4 | ||||
| -rw-r--r-- | na/Makefile | 22 | ||||
| -rw-r--r-- | na/arch/riscv/defconfig | 39 | ||||
| -rw-r--r-- | na/scripts/Kconfig.mk | 209 |
4 files changed, 269 insertions, 5 deletions
@@ -24,4 +24,6 @@ gdb.txt vgcore.* core .DS_Store -Thumbs.db
\ No newline at end of file +Thumbs.db +# Kernel build configuration (per-developer) +.config diff --git a/na/Makefile b/na/Makefile index e20526a..100bc6d 100644 --- a/na/Makefile +++ b/na/Makefile @@ -16,8 +16,12 @@ # ============================================================ # Build Configuration # ============================================================ -ARCH ?= riscv BUILD := build + +# Load Kconfig infrastructure (reads .config, sets KCPPFLAGS) +include scripts/Kconfig.mk + +ARCH ?= $(CONFIG_ARCH) TARGET := $(BUILD)/kernel.elf IMAGE := $(BUILD)/kernel.bin LDS_SRC := arch/$(ARCH)/kernel/lumeos.lds.S @@ -74,11 +78,13 @@ include lib/module.mk include test/module.mk # Compiler flags (defined AFTER includes so INCLUDES is complete) -CFLAGS := $(WARNINGS) -O2 -g -DDEBUG \ +CFLAGS := $(WARNINGS) -O2 -g \ -ffreestanding -nostdlib \ -mcmodel=medany \ -MMD -MP \ - $(INCLUDES) + $(KCPPFLAGS) \ + $(INCLUDES) \ + -I$(BUILD)/generated # C flags for musl libc sources: add libc-shim headers first CFLAGS_LIBC := $(CFLAGS) -Iinclude/libc-shim \ @@ -106,10 +112,17 @@ DEPS := $(OBJS:.o=.d) # ============================================================ # Build Rules # ============================================================ -.PHONY: all clean run debug fmt disasm +.PHONY: all clean run debug fmt disasm defconfig savedefconfig showconfig all: $(IMAGE) +# Generate autoconf.h before any compilation +$(AUTOCONF_H): scripts/Kconfig.mk $(wildcard .config) + $(generate-autoconf) + +# All objects depend on autoconf.h +$(OBJS): $(AUTOCONF_H) + # Preprocess linker script template (.lds.S → .lds) $(LINKER_SCRIPT): $(LDS_SRC) include/lume/lumeos.lds.h @echo " LDS $@" @@ -207,3 +220,4 @@ info: @echo "SRCS_CC = $(SRCS_CC)" @echo "SRCS_S = $(SRCS_S)" @echo "OBJS = $(OBJS)" + @echo "KCPPFLAGS = $(KCPPFLAGS)" diff --git a/na/arch/riscv/defconfig b/na/arch/riscv/defconfig new file mode 100644 index 0000000..87d3ffd --- /dev/null +++ b/na/arch/riscv/defconfig @@ -0,0 +1,39 @@ +# SPDX-License-Identifier: GPL-2.0-or-later +# Copyright (C) 2026 Richard Qin +# +# LumeOS Default Kernel Configuration — RISC-V (virt) +# +# This is the "golden" default configuration for the RISC-V virt platform. +# To customise, copy this file to the source root as '.config': +# +# cp arch/riscv/defconfig .config +# $EDITOR .config +# make +# +# Any variable not set here inherits its default from scripts/Kconfig.mk. +# ====================================================================== + +# --- Platform ----------------------------------------------------------- +CONFIG_ARCH = riscv + +# --- CPU / SMP ---------------------------------------------------------- +CONFIG_NR_CPUS = 8 + +# --- Scheduler ---------------------------------------------------------- +CONFIG_HZ = 100 +CONFIG_NR_PRIORITIES = 32 + +# --- Memory Management -------------------------------------------------- +CONFIG_PAGE_SIZE = 4096 +CONFIG_MAX_ORDER = 11 +CONFIG_KERNEL_STACK_SIZE = 8192 + +# --- Security / Hardening ----------------------------------------------- +CONFIG_KASLR = y +CONFIG_STACK_CANARY = y + +# --- Debug --------------------------------------------------------------- +CONFIG_DEBUG = y +CONFIG_DEBUG_LOCKS = y +CONFIG_SELFTEST = y +CONFIG_KLOG = y diff --git a/na/scripts/Kconfig.mk b/na/scripts/Kconfig.mk new file mode 100644 index 0000000..ee2d9de --- /dev/null +++ b/na/scripts/Kconfig.mk @@ -0,0 +1,209 @@ +# SPDX-License-Identifier: GPL-2.0-or-later +# Copyright (C) 2026 Richard Qin +# +# scripts/Kconfig.mk — Kernel Configuration Infrastructure +# +# This file is the heart of the LumeOS build-time configuration system. +# It mirrors the role of Linux's Kconfig + auto.conf pipeline: +# +# 1. Developer edits `.config` (or copies from arch/$(ARCH)/defconfig) +# 2. This file reads `.config`, applies defaults for anything missing, +# validates constraints, and exports two things: +# a) KCPPFLAGS — a set of -DCONFIG_xxx=value flags for the compiler +# b) $(BUILD)/generated/autoconf.h — the same values as C #defines +# +# Design Principles (matching Linux conventions): +# - Every CONFIG_ symbol has a default defined HERE, so a bare `make` +# with no .config still builds a sane kernel. +# - Boolean options use 'y' / 'n'. Numeric options are plain integers. +# - The generated autoconf.h uses the exact same names so that +# `#ifdef CONFIG_DEBUG` works identically in Makefile and C. +# ====================================================================== + +# ------------------------------------------------------------------ +# §1 Read user configuration (if it exists) +# ------------------------------------------------------------------ +-include .config + +# ------------------------------------------------------------------ +# §2 Provide defaults for every CONFIG_ symbol +# +# ?= means "set only if not already set (by .config or CLI)". +# This is the single source of truth for default values. +# ------------------------------------------------------------------ + +# --- Platform ------------------------------------------------------- +CONFIG_ARCH ?= riscv + +# --- CPU / SMP ------------------------------------------------------ +CONFIG_NR_CPUS ?= 8 + +# --- Scheduler ------------------------------------------------------ +CONFIG_HZ ?= 100 +CONFIG_NR_PRIORITIES ?= 32 + +# --- Memory Management ---------------------------------------------- +CONFIG_PAGE_SIZE ?= 4096 +CONFIG_MAX_ORDER ?= 11 +CONFIG_KERNEL_STACK_SIZE ?= 8192 + +# --- Security / Hardening ------------------------------------------- +CONFIG_KASLR ?= y +CONFIG_STACK_CANARY ?= y + +# --- Debug ----------------------------------------------------------- +CONFIG_DEBUG ?= y +CONFIG_DEBUG_LOCKS ?= y +CONFIG_SELFTEST ?= y +CONFIG_KLOG ?= y + +# ------------------------------------------------------------------ +# §3 Derived constants (computed from CONFIG_ symbols) +# +# These follow Linux's pattern of expressing dependent values +# so that C code never has to do its own derivation at runtime. +# ------------------------------------------------------------------ + +# Default priority = middle of the range +CONFIG_DEFAULT_PRIORITY = $(shell echo $$(( $(CONFIG_NR_PRIORITIES) / 2 ))) + +# Idle priority = lowest = NR_PRIORITIES - 1 +CONFIG_IDLE_PRIORITY = $(shell echo $$(( $(CONFIG_NR_PRIORITIES) - 1 ))) + +# Default time slice in ticks: 100ms worth of ticks +# (same as Linux CFS's sysctl_sched_min_granularity default) +CONFIG_DEFAULT_TIMESLICE = $(shell echo $$(( $(CONFIG_HZ) / 10 ))) + +# ------------------------------------------------------------------ +# §4 Export to C/C++ via -D flags +# +# KCPPFLAGS is appended to CFLAGS/CXXFLAGS in the main Makefile. +# Every CONFIG_xxx=<value> becomes -DCONFIG_xxx=<value>. +# Boolean 'y' options become -DCONFIG_xxx=1. +# ------------------------------------------------------------------ +KCPPFLAGS := + +# Numeric options — pass through as-is +KCPPFLAGS += -DCONFIG_NR_CPUS=$(CONFIG_NR_CPUS) +KCPPFLAGS += -DCONFIG_HZ=$(CONFIG_HZ) +KCPPFLAGS += -DCONFIG_NR_PRIORITIES=$(CONFIG_NR_PRIORITIES) +KCPPFLAGS += -DCONFIG_DEFAULT_PRIORITY=$(CONFIG_DEFAULT_PRIORITY) +KCPPFLAGS += -DCONFIG_IDLE_PRIORITY=$(CONFIG_IDLE_PRIORITY) +KCPPFLAGS += -DCONFIG_DEFAULT_TIMESLICE=$(CONFIG_DEFAULT_TIMESLICE) +KCPPFLAGS += -DCONFIG_PAGE_SIZE=$(CONFIG_PAGE_SIZE) +KCPPFLAGS += -DCONFIG_MAX_ORDER=$(CONFIG_MAX_ORDER) +KCPPFLAGS += -DCONFIG_KERNEL_STACK_SIZE=$(CONFIG_KERNEL_STACK_SIZE) + +# Boolean options — translate 'y' → 1 +ifeq ($(CONFIG_KASLR),y) +KCPPFLAGS += -DCONFIG_KASLR=1 +endif +ifeq ($(CONFIG_STACK_CANARY),y) +KCPPFLAGS += -DCONFIG_STACK_CANARY=1 +endif +ifeq ($(CONFIG_DEBUG),y) +KCPPFLAGS += -DCONFIG_DEBUG=1 +endif +ifeq ($(CONFIG_DEBUG_LOCKS),y) +KCPPFLAGS += -DCONFIG_DEBUG_LOCKS=1 +endif +ifeq ($(CONFIG_SELFTEST),y) +KCPPFLAGS += -DCONFIG_SELFTEST=1 +endif +ifeq ($(CONFIG_KLOG),y) +KCPPFLAGS += -DCONFIG_KLOG=1 +endif + +# ------------------------------------------------------------------ +# §5 Generate autoconf.h (analogous to Linux's include/generated/autoconf.h) +# +# This header is auto-generated at build time so that C code can +# #include <generated/autoconf.h> and see CONFIG_xxx macros without +# relying on -D flags alone. This is important for IDE integration. +# ------------------------------------------------------------------ +AUTOCONF_H := $(BUILD)/generated/autoconf.h + +define generate-autoconf + @mkdir -p $(dir $(AUTOCONF_H)) + @echo " GEN $(AUTOCONF_H)" + @echo "/* AUTO-GENERATED — DO NOT EDIT */" > $(AUTOCONF_H) + @echo "/* Generated by scripts/Kconfig.mk */" >> $(AUTOCONF_H) + @echo "#pragma once" >> $(AUTOCONF_H) + @echo "" >> $(AUTOCONF_H) + @echo "/* Platform */" >> $(AUTOCONF_H) + @echo "#define CONFIG_NR_CPUS $(CONFIG_NR_CPUS)" >> $(AUTOCONF_H) + @echo "" >> $(AUTOCONF_H) + @echo "/* Scheduler */" >> $(AUTOCONF_H) + @echo "#define CONFIG_HZ $(CONFIG_HZ)" >> $(AUTOCONF_H) + @echo "#define CONFIG_NR_PRIORITIES $(CONFIG_NR_PRIORITIES)" >> $(AUTOCONF_H) + @echo "#define CONFIG_DEFAULT_PRIORITY $(CONFIG_DEFAULT_PRIORITY)" >> $(AUTOCONF_H) + @echo "#define CONFIG_IDLE_PRIORITY $(CONFIG_IDLE_PRIORITY)" >> $(AUTOCONF_H) + @echo "#define CONFIG_DEFAULT_TIMESLICE $(CONFIG_DEFAULT_TIMESLICE)" >> $(AUTOCONF_H) + @echo "" >> $(AUTOCONF_H) + @echo "/* Memory Management */" >> $(AUTOCONF_H) + @echo "#define CONFIG_PAGE_SIZE $(CONFIG_PAGE_SIZE)" >> $(AUTOCONF_H) + @echo "#define CONFIG_MAX_ORDER $(CONFIG_MAX_ORDER)" >> $(AUTOCONF_H) + @echo "#define CONFIG_KERNEL_STACK_SIZE $(CONFIG_KERNEL_STACK_SIZE)" >> $(AUTOCONF_H) + @echo "" >> $(AUTOCONF_H) + @echo "/* Security */" >> $(AUTOCONF_H) + $(if $(filter y,$(CONFIG_KASLR)), @echo "#define CONFIG_KASLR 1" >> $(AUTOCONF_H)) + $(if $(filter y,$(CONFIG_STACK_CANARY)), @echo "#define CONFIG_STACK_CANARY 1" >> $(AUTOCONF_H)) + @echo "" >> $(AUTOCONF_H) + @echo "/* Debug */" >> $(AUTOCONF_H) + $(if $(filter y,$(CONFIG_DEBUG)), @echo "#define CONFIG_DEBUG 1" >> $(AUTOCONF_H)) + $(if $(filter y,$(CONFIG_DEBUG_LOCKS)), @echo "#define CONFIG_DEBUG_LOCKS 1" >> $(AUTOCONF_H)) + $(if $(filter y,$(CONFIG_SELFTEST)), @echo "#define CONFIG_SELFTEST 1" >> $(AUTOCONF_H)) + $(if $(filter y,$(CONFIG_KLOG)), @echo "#define CONFIG_KLOG 1" >> $(AUTOCONF_H)) +endef + +# ------------------------------------------------------------------ +# §6 Utility targets +# ------------------------------------------------------------------ + +# 'make defconfig' — reset .config to the arch default +.PHONY: defconfig savedefconfig showconfig + +defconfig: + @echo " DEFCFG arch/$(CONFIG_ARCH)/defconfig → .config" + @cp arch/$(CONFIG_ARCH)/defconfig .config + +# 'make savedefconfig' — save current effective config +savedefconfig: + @echo " SAVE .config" + @echo "# LumeOS Kernel Configuration (saved $$(date +%Y-%m-%d))" > .config + @echo "CONFIG_ARCH = $(CONFIG_ARCH)" >> .config + @echo "CONFIG_NR_CPUS = $(CONFIG_NR_CPUS)" >> .config + @echo "CONFIG_HZ = $(CONFIG_HZ)" >> .config + @echo "CONFIG_NR_PRIORITIES = $(CONFIG_NR_PRIORITIES)" >> .config + @echo "CONFIG_PAGE_SIZE = $(CONFIG_PAGE_SIZE)" >> .config + @echo "CONFIG_MAX_ORDER = $(CONFIG_MAX_ORDER)" >> .config + @echo "CONFIG_KERNEL_STACK_SIZE = $(CONFIG_KERNEL_STACK_SIZE)" >> .config + @echo "CONFIG_KASLR = $(CONFIG_KASLR)" >> .config + @echo "CONFIG_STACK_CANARY = $(CONFIG_STACK_CANARY)" >> .config + @echo "CONFIG_DEBUG = $(CONFIG_DEBUG)" >> .config + @echo "CONFIG_DEBUG_LOCKS = $(CONFIG_DEBUG_LOCKS)" >> .config + @echo "CONFIG_SELFTEST = $(CONFIG_SELFTEST)" >> .config + @echo "CONFIG_KLOG = $(CONFIG_KLOG)" >> .config + +# 'make showconfig' — print effective configuration +showconfig: + @echo "========================================" + @echo " LumeOS Effective Configuration" + @echo "========================================" + @echo " ARCH = $(CONFIG_ARCH)" + @echo " NR_CPUS = $(CONFIG_NR_CPUS)" + @echo " HZ = $(CONFIG_HZ)" + @echo " NR_PRIORITIES = $(CONFIG_NR_PRIORITIES)" + @echo " DEFAULT_PRIORITY = $(CONFIG_DEFAULT_PRIORITY)" + @echo " IDLE_PRIORITY = $(CONFIG_IDLE_PRIORITY)" + @echo " DEFAULT_TIMESLICE = $(CONFIG_DEFAULT_TIMESLICE)" + @echo " PAGE_SIZE = $(CONFIG_PAGE_SIZE)" + @echo " MAX_ORDER = $(CONFIG_MAX_ORDER)" + @echo " KERNEL_STACK_SIZE = $(CONFIG_KERNEL_STACK_SIZE)" + @echo " KASLR = $(CONFIG_KASLR)" + @echo " STACK_CANARY = $(CONFIG_STACK_CANARY)" + @echo " DEBUG = $(CONFIG_DEBUG)" + @echo " DEBUG_LOCKS = $(CONFIG_DEBUG_LOCKS)" + @echo " SELFTEST = $(CONFIG_SELFTEST)" + @echo " KLOG = $(CONFIG_KLOG)" + @echo "========================================" |
