Skip to content

Commit aef46df

Browse files
committed
use kbuild system replace platform config first
Signed-off-by: Weikang Guo <guoweikang@kylinos.cn>
1 parent 3c8208c commit aef46df

14 files changed

Lines changed: 1534 additions & 4 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,6 @@
2020

2121
# Cargo lock
2222
Cargo.lock
23+
24+
# kernel config
25+
.config

Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ members = [
55
"xtask/cargo-platconfig",
66
"xtask/crate_rootfs",
77
"xtask/kconfig-gen",
8+
# build config system
9+
"xtask/xconfig",
10+
"xtask/cargo-kbuild",
11+
"util/kbuild_config",
12+
813
"platforms/bootloader",
914
"tee_apps/sh",
1015
"tee_apps/tee_app1",
@@ -139,5 +144,7 @@ klogger = { path = "util/klogger" }
139144
backtrace = { path = "util/backtrace" }
140145
kerrno = { path = "util/kerrno" }
141146
unittest = { path = "util/unittest" }
147+
kbuild-config = { path = "util/kbuild_config" }
148+
142149
kconfig-gen = { path = "xtask/kconfig-gen" }
143150
smoltcp = { version = "0.12.0", package = "x-smoltcp", default-features = false }

Kconfig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
mainmenu "X-kernel Configuration"
2+
3+
menu "Platform Selection"
4+
5+
source "platforms/Kconfig"
6+
7+
endmenu

Makefile

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ endif
9090

9191
.DEFAULT_GOAL := all
9292

93-
ifneq ($(filter $(or $(MAKECMDGOALS), $(.DEFAULT_GOAL)), all build disasm run justrun debug defconfig oldconfig),)
93+
ifneq ($(filter $(or $(MAKECMDGOALS), $(.DEFAULT_GOAL)), all build disasm run justrun debug defconfig oldconfig menuconfig),)
9494
# Install dependencies
9595
include scripts/make/deps.mk
9696
# Platform resolving
@@ -168,6 +168,19 @@ endif
168168
ROOTFS_URL = https://github.com/Starry-OS/rootfs/releases/download/20250917
169169
ROOTFS_IMG = rootfs-$(ARCH).img
170170

171+
check_config:
172+
@if [ ! -f .config ]; then \
173+
echo "Error: .config not found."; \
174+
echo "Please run one of the following commands first:"; \
175+
echo " make menuconfig"; \
176+
echo " cp defconfig .config"; \
177+
exit 1; \
178+
fi
179+
180+
181+
menuconfig:
182+
xconf menuconfig
183+
171184
rootfs:
172185
@if [ ! -f $(ROOTFS_IMG) ]; then \
173186
echo "Image not found, downloading..."; \
@@ -182,7 +195,7 @@ teefs:
182195
defconfig:
183196
$(call defconfig)
184197

185-
oldconfig:
198+
oldconfig: check_config
186199
$(call oldconfig)
187200

188201
build: $(OUT_DIR) $(FINAL_IMG)
@@ -239,7 +252,7 @@ clean: clean_c
239252
clean_c::
240253
rm -rf $(app-objs)
241254

242-
.PHONY: all defconfig oldconfig \
255+
.PHONY: all check_config defconfig oldconfig menuconfig \
243256
build disasm run justrun debug \
244257
clippy doc doc_check_missing fmt fmt_c unittest unittest_no_fail_fast \
245258
disk_img clean clean_c

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,24 @@ make ARCH=loongarch64 rootfs
4444
```
4545
Or you can build your own root filesystem image(only supported ext4 and musl for now)
4646

47+
### 3. Configuration kernel
48+
You can configure the kernel from the starter configuration with the following command:
49+
```bash
50+
make menuconfig
51+
```
52+
or you can directly copy your own .config file to the kernel source directory,
53+
if you use the command
54+
```bash
55+
make build
56+
```
57+
the build system will copy the qemu default config file to the source directory automatically.
58+
59+
if you use the command with `PLAT`(in platfroms directory) option
60+
```bash
61+
make ARCH=xxx PLAT=[xxxxx] build
62+
```
63+
the build system will copy the specified platform config file to the source directory automatically.
64+
4765
### 3. Build and run on QEMU
4866
You can build and run the kernel on QEMU with the following commands:
4967

platforms/Kconfig

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
menu "Platform Basic Configuration"
2+
3+
config PLATFORM_CPU_NUM
4+
int "Number of CPUs"
5+
range 1 256
6+
default 4
7+
help
8+
Number of CPUs available on the platform.
9+
10+
config PLATFORM_PHYS_MEM_BASE
11+
hex "Physical memory base address"
12+
default 0x40000000
13+
help
14+
Base physical address of the system memory.
15+
16+
config PLATFORM_PHYS_MEM_SIZE
17+
hex "Physical memory size"
18+
default 0x08000000
19+
help
20+
Total size of the physical memory.
21+
22+
config PLATFORM_KERNEL_BASE_PADDR
23+
hex "Kernel base physical address"
24+
default 0x40200000
25+
help
26+
Physical load address of the kernel image.
27+
28+
config PLATFORM_KERNEL_BASE_VADDR
29+
hex "Kernel base virtual address"
30+
default 0xffff000040200000
31+
help
32+
Virtual address where the kernel is mapped.
33+
34+
config PLATFORM_PHYS_VIRT_OFFSET
35+
hex "Physical to virtual address offset"
36+
default 0xffff000000000000
37+
help
38+
Offset used to convert physical addresses to virtual addresses.
39+
40+
config PLATFORM_PHYS_BUS_OFFSET
41+
hex "Physical to bus address offset"
42+
default 0x0
43+
help
44+
Offset between physical address and bus address.
45+
46+
config PLATFORM_KERNEL_ASPACE_BASE
47+
hex "Kernel address space base"
48+
default 0xffff000000000000
49+
help
50+
Base address of the kernel virtual address space.
51+
52+
config PLATFORM_KERNEL_ASPACE_SIZE
53+
hex "Kernel address space size"
54+
default 0x0000fffffffff000
55+
help
56+
Size of the kernel virtual address space.
57+
58+
config PLATFORM_BOOT_STACK_SIZE
59+
hex "Boot stack size"
60+
default 0x40000
61+
help
62+
Stack size used during kernel boot.
63+
64+
endmenu
65+
66+
menu "Platform DMA and Power Management"
67+
68+
config PLATFORM_DMA_MEM_BASE
69+
hex "DMA memory base address"
70+
default 0x40000000
71+
help
72+
Base physical address of DMA memory.
73+
74+
config PLATFORM_DMA_MEM_SIZE
75+
hex "DMA memory size"
76+
default 0x00200000
77+
help
78+
Size of the DMA memory region.
79+
80+
choice
81+
prompt "PSCI invocation method"
82+
default PLATFORM_PSCI_HVC
83+
84+
config PLATFORM_PSCI_HVC
85+
bool "HVC (Hypervisor Call)"
86+
help
87+
Use HVC instruction for PSCI calls.
88+
89+
config PLATFORM_PSCI_SMC
90+
bool "SMC (Secure Monitor Call)"
91+
help
92+
Use SMC instruction for PSCI calls.
93+
94+
endchoice
95+
96+
endmenu
97+
98+
menu "Platform Devices and Interrupts"
99+
100+
config PLATFORM_UART_PADDR
101+
hex "UART physical address"
102+
default 0x09000000
103+
help
104+
Physical address of the UART controller.
105+
106+
config PLATFORM_UART_IRQ
107+
int "UART IRQ number"
108+
default 33
109+
help
110+
Interrupt number for UART device.
111+
112+
config PLATFORM_TIMER_IRQ
113+
int "Timer IRQ number"
114+
default 30
115+
help
116+
Interrupt number for the physical timer.
117+
118+
config PLATFORM_IPI_IRQ
119+
int "IPI IRQ number"
120+
default 1
121+
help
122+
Inter-Processor Interrupt number.
123+
124+
config PLATFORM_PMU_IRQ
125+
int "PMU IRQ number"
126+
default 23
127+
help
128+
Performance Monitoring Unit interrupt number.
129+
130+
endmenu
131+
132+
menu "Generic Interrupt Controller (GIC)"
133+
134+
config PLATFORM_GICD_PADDR
135+
hex "GIC Distributor base address"
136+
default 0x08000000
137+
help
138+
Base physical address of the GIC Distributor.
139+
140+
config PLATFORM_GICC_PADDR
141+
hex "GIC CPU Interface base address"
142+
default 0x08010000
143+
help
144+
Base physical address of the GIC CPU Interface.
145+
146+
endmenu
147+
148+
menu "RTC Configuration"
149+
150+
config PLATFORM_RTC_PL031
151+
bool "PL031 RTC"
152+
default y
153+
help
154+
Enable PL031 Real-Time Clock support.
155+
156+
config PLATFORM_RTC_PADDR
157+
hex "RTC physical address"
158+
default 0x09010000
159+
depends on PLATFORM_RTC_PL031
160+
help
161+
Physical address of the PL031 RTC device.
162+
163+
endmenu
164+

scripts/make/config.mk

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@ config_args := \
55
-w 'platform="$(PLAT_NAME)"' \
66
-o "$(OUT_CONFIG)"
77

8+
.PHONY: check-config
9+
check-config:
10+
@if [ ! -f .config ]; then \
11+
echo "Error: .config not found."; \
12+
echo "Please run one of the following commands first:"; \
13+
echo " xconfig menuconfig"; \
14+
echo " cp defconfig .config"; \
15+
exit 1; \
16+
fi
17+
818
ifneq ($(MEM),)
919
config_args += -w 'plat.phys-memory-size=$(shell ./scripts/make/strtosz.py $(MEM))'
1020
else

scripts/make/deps.mk

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
# Necessary dependencies for the build system
22

3-
# Tool to generate platform configuration files
3+
# Tool to generate xconfig
44
ifeq ($(shell xconfig --version 2>/dev/null),)
55
$(info Installing xconfig...)
66
$(shell cargo install --path xtask/xconfig)
77
endif
88

9+
ifeq ($(shell cargo kbuild --version 2>/dev/null),)
10+
$(info Installing cargo-kbuild...)
11+
$(shell cargo install --path xtask/cargo-kbuild)
12+
endif
13+
914
# Tool to parse information about the target package
1015
ifeq ($(shell cargo platconfig --version 2>/dev/null),)
1116
$(info Installing cargo-platconfig...)

util/kbuild_config/Cargo.toml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[package]
2+
name = "kbuild_config"
3+
version.workspace = true
4+
edition.workspace = true
5+
authors.workspace = true
6+
description = "Configuration for kbuild"
7+
license.workspace = true
8+
homepage.workspace = true
9+
repository.workspace = true
10+
documentation.workspace = true
11+
keywords = ["kbuild"]
12+
categories = ["os", "no-std"]
13+
14+
[package.metadata.kbuild]
15+
enabled = true

util/kbuild_config/build.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use std::env;
2+
use std::fs;
3+
use std::path::Path;
4+
5+
fn main() {
6+
// Get the path to the generated config.rs
7+
let out_dir = env::var("OUT_DIR").unwrap();
8+
let config_rs_path = Path::new(&out_dir).join("config.rs");
9+
10+
// If cargo-kbuild has generated the config.rs, copy it to OUT_DIR
11+
// Otherwise, generate an empty one
12+
let workspace_root = env::var("CARGO_MANIFEST_DIR").unwrap();
13+
let workspace_root = Path::new(&workspace_root).parent().unwrap().parent().unwrap();
14+
let target_config_path = workspace_root.join("target/kbuild/config.rs");
15+
16+
if target_config_path.exists() {
17+
// Copy the generated config
18+
let config_content = fs::read_to_string(&target_config_path)
19+
.expect("Failed to read generated config.rs");
20+
fs::write(&config_rs_path, config_content)
21+
.expect("Failed to write config.rs to OUT_DIR");
22+
} else {
23+
// Generate empty config if not available
24+
fs::write(&config_rs_path, "// No config.rs generated yet\n")
25+
.expect("Failed to write empty config.rs");
26+
}
27+
28+
// Set environment variable for inclusion
29+
println!("cargo:rustc-env=CONFIG_RS_PATH={}", config_rs_path.display());
30+
println!("cargo:rerun-if-changed={}", target_config_path.display());
31+
}

0 commit comments

Comments
 (0)