summaryrefslogtreecommitdiff
path: root/scripts/Kconfig.mk
blob: 3db7a2f8d1de7b0ea9a5e188b4ca80f1d0cd13d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# 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 `$(O)/.config` (or runs `make O=... defconfig`)
#   2.  This file reads that configuration and applies missing defaults,
#       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 an empty output
#     directory 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)
# ------------------------------------------------------------------
KCONFIG_CONFIG ?= $(BUILD)/.config
-include $(KCONFIG_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_KASLR_POLICY_ID  ?= 0
CONFIG_KASLR_LOG_LEVEL  ?= 1
CONFIG_STACK_CANARY     ?= y
CONFIG_KERNEL_COMPRESSED ?= y

# --- Debug -----------------------------------------------------------
CONFIG_DEBUG            ?= y
CONFIG_DEBUG_LOCKS      ?= y
CONFIG_SELFTEST         ?= y
CONFIG_KLOG             ?= y

# --- Initramfs ------------------------------------------------------
# Source for the initramfs image.  Accepted forms (auto-detected by
# scripts/gen_initramfs.sh):
#   - a listing file (gen_init_cpio format)  → built into a cpio
#   - a directory                            → packed recursively
#   - a *.cpio file                          → used as-is
# Override on the command line or pin in .config:
#   make all CONFIG_INITRAMFS_SOURCE=/path/to/rootfs
CONFIG_INITRAMFS_SOURCE   ?= usr/default_cpio_list
CONFIG_INITRAMFS_ROOT_UID ?= 0
CONFIG_INITRAMFS_ROOT_GID ?= 0

# ------------------------------------------------------------------
# §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
KCPPFLAGS += -DCONFIG_KASLR_POLICY_ID=$(CONFIG_KASLR_POLICY_ID)
KCPPFLAGS += -DCONFIG_KASLR_LOG_LEVEL=$(CONFIG_KASLR_LOG_LEVEL)
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
ifeq ($(CONFIG_KERNEL_COMPRESSED),y)
KCPPFLAGS += -DCONFIG_KERNEL_COMPRESSED=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

# ------------------------------------------------------------------
# §6  Utility targets
# ------------------------------------------------------------------

# 'make defconfig' — reset .config to the arch default
.PHONY: defconfig savedefconfig showconfig

defconfig:
	@echo "  DEFCFG  arch/$(CONFIG_ARCH)/defconfig → $(KCONFIG_CONFIG)"
	@mkdir -p $(dir $(KCONFIG_CONFIG))
	@cp arch/$(CONFIG_ARCH)/defconfig $(KCONFIG_CONFIG)

# 'make savedefconfig' — save current effective config
savedefconfig:
	@echo "  SAVE    $(KCONFIG_CONFIG)"
	@mkdir -p $(dir $(KCONFIG_CONFIG))
	@echo "# LumeOS Kernel Configuration (saved $$(date +%Y-%m-%d))" > $(KCONFIG_CONFIG)
	@echo "CONFIG_ARCH             = $(CONFIG_ARCH)"             >> $(KCONFIG_CONFIG)
	@echo "CONFIG_NR_CPUS          = $(CONFIG_NR_CPUS)"          >> $(KCONFIG_CONFIG)
	@echo "CONFIG_HZ               = $(CONFIG_HZ)"               >> $(KCONFIG_CONFIG)
	@echo "CONFIG_NR_PRIORITIES    = $(CONFIG_NR_PRIORITIES)"     >> $(KCONFIG_CONFIG)
	@echo "CONFIG_PAGE_SIZE        = $(CONFIG_PAGE_SIZE)"         >> $(KCONFIG_CONFIG)
	@echo "CONFIG_MAX_ORDER        = $(CONFIG_MAX_ORDER)"         >> $(KCONFIG_CONFIG)
	@echo "CONFIG_KERNEL_STACK_SIZE = $(CONFIG_KERNEL_STACK_SIZE)" >> $(KCONFIG_CONFIG)
	@echo "CONFIG_KASLR            = $(CONFIG_KASLR)"             >> $(KCONFIG_CONFIG)
	@echo "CONFIG_KASLR_POLICY_ID  = $(CONFIG_KASLR_POLICY_ID)"   >> $(KCONFIG_CONFIG)
	@echo "CONFIG_KASLR_LOG_LEVEL  = $(CONFIG_KASLR_LOG_LEVEL)"   >> $(KCONFIG_CONFIG)
	@echo "CONFIG_STACK_CANARY     = $(CONFIG_STACK_CANARY)"      >> $(KCONFIG_CONFIG)
	@echo "CONFIG_DEBUG            = $(CONFIG_DEBUG)"              >> $(KCONFIG_CONFIG)
	@echo "CONFIG_DEBUG_LOCKS      = $(CONFIG_DEBUG_LOCKS)"        >> $(KCONFIG_CONFIG)
	@echo "CONFIG_SELFTEST         = $(CONFIG_SELFTEST)"           >> $(KCONFIG_CONFIG)
	@echo "CONFIG_KLOG             = $(CONFIG_KLOG)"               >> $(KCONFIG_CONFIG)
	@echo "CONFIG_KERNEL_COMPRESSED = $(CONFIG_KERNEL_COMPRESSED)" >> $(KCONFIG_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 "  KASLR_POLICY_ID   = $(CONFIG_KASLR_POLICY_ID)"
	@echo "  KASLR_LOG_LEVEL   = $(CONFIG_KASLR_LOG_LEVEL)"
	@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 "  KERNEL_COMPRESSED = $(CONFIG_KERNEL_COMPRESSED)"
	@echo "========================================"