Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
3c8920d
kernelx
LRache Aug 31, 2025
65329a7
kernelx
LRache Sep 2, 2025
7e653aa
kernelx: moved kernel code to /kernel
LRache Sep 14, 2025
aac1da9
docs: KXOS -> KernelX
LRache Sep 14, 2025
e73fa88
docs: mkdir
LRache Sep 14, 2025
63df68a
fixed: removed build dir
LRache Sep 14, 2025
92463c2
kernelx
LRache Sep 17, 2025
eb93151
test: glibc mkdir, open and write
LRache Sep 17, 2025
a4f5e7c
fixed: changed program path
LRache Sep 17, 2025
c82e556
fixed: fini all inode when panic
LRache Sep 17, 2025
7473931
feat: syscall dup2
LRache Sep 18, 2025
217c169
feat: menuconfig
LRache Sep 18, 2025
b44f62b
refactor
LRache Sep 22, 2025
5eff878
kernelx
LRache Sep 24, 2025
be27401
kernelx
LRache Sep 26, 2025
ac4469e
kernelx
LRache Sep 30, 2025
2034a24
kernelx
LRache Oct 5, 2025
049bbf4
kernelx
LRache Oct 10, 2025
941a571
kernelx
LRache Oct 11, 2025
688d0fd
kernelx signal
LRache Oct 12, 2025
beb6abb
kernelx
LRache Oct 15, 2025
2e60e3c
kernelx
LRache Oct 19, 2025
7fdf468
libfdt
LRache Oct 22, 2025
23bc241
kernelx
LRache Oct 22, 2025
8a0a99b
kernelx
LRache Oct 26, 2025
4deb4e5
kernelx
LRache Oct 29, 2025
556cf0b
kernelx
LRache Nov 2, 2025
8128c16
kernelx
LRache Nov 5, 2025
9fab9e0
kernelx
LRache Nov 12, 2025
8c6d202
kernelx
LRache Nov 16, 2025
199be87
lib lwext4_rust
LRache Nov 20, 2025
7e75d7a
kernelx
LRache Nov 20, 2025
6444833
kernelx
LRache Nov 23, 2025
7101f13
kernelx
LRache Nov 24, 2025
a99f31a
kernelx
LRache Nov 24, 2025
e0d29e6
swap-memory
LRache Nov 26, 2025
78c3ce6
feat: swap memory for anonymous area
LRache Nov 30, 2025
56a279c
feat: riscv SVADU extension
LRache Nov 30, 2025
c320b2f
feat: riscv svadu extension
LRache Dec 1, 2025
015eb02
kernelx
LRache Dec 3, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
kernelx
  • Loading branch information
LRache committed Oct 19, 2025
commit 2e60e3cd98071ef30f67bc7400f42a1e61f0221f
5 changes: 4 additions & 1 deletion .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
[env]
KERNELX_HOME = { value = ".", relative = true }
ARCH = { value = "riscv" }
ARCH_BITS = { value = "64" }
ARCH_BITS = { value = "64" }

[build]
target = "riscv64gc-unknown-none-elf"
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ edition = "2024"
build = "scripts/build.rs"

[features]
default = ["platform-qemu-virt-riscv64"]
default = ["platform-qemu-virt-riscv64", "deadlock-detect", "log-info"]

platform-riscv-common = []
platform-qemu-virt-riscv64=["platform-riscv-common"]
Expand All @@ -16,6 +16,9 @@ log-info = ["log-warn"]
log-debug = ["log-info"]
log-trace = ["log-debug"]
log-trace-syscall = []
warn-unimplemented-syscall = []

deadlock-detect = []

[profile.dev]
panic = "abort"
Expand Down
22 changes: 16 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
include config/config.mk

KERNEL = build/$(PLATFORM)/kernelx
KERNEL_BIN = build/$(PLATFORM)/kernel.bin

all: run
all: kernel

include config/config.mk

init:
@ git submodule init
Expand Down Expand Up @@ -33,13 +34,22 @@ qemu-dts:
gdb: kernel
@ make -f scripts/qemu.mk qemu-gdb KERNEL=$(KERNEL)

objdump: kernel
@ $(CROSS_COMPILE)objdump -d $(KERNEL) > kernel.asm
@ echo "Generated kernel.asm"

objcopy:
@ make -f build.mk objcopy $(KERNEL_CONFIG)
@ $(CROSS_COMPILE)objcopy -O binary $(KERNEL) kernel.bin
@ echo "Generated kernel.bin"

objdump:
@ $(CROSS_COMPILE)objdump -d $(KERNEL) > kernel.asm
@ echo "Generated kernel.asm"
$(KERNEL_BIN): kernel
@ $(CROSS_COMPILE)objcopy -O binary $(KERNEL) $(KERNEL_BIN)
@ echo "Generated $(KERNEL_BIN)"

package: $(KERNEL_BIN)
@ KERNEL_IMAGE=$(KERNEL_BIN) IMAGE=$(IMAGE) scripts/package.sh
@ echo "Packaged image: $(IMAGE)"

count:
@ find src c/src -type f -name "*.rs" -o -name "*.c" -o -name "*.h" | xargs wc -l
Expand Down
8 changes: 8 additions & 0 deletions build.mk
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ ifeq ($(LOG_SYSCALL),y)
RUST_FEATURES += log-trace-syscall
endif

ifeq ($(CONFIG_WARN_UNIMPLEMENTED_SYSCALL),y)
RUST_FEATURES += warn-unimplemented-syscall
endif

all: kernel

kernel: $(KERNEL)
Expand All @@ -55,6 +59,10 @@ $(KERNEL): clib
check:
@ $(BUILD_ENV) cargo check --target $(RUST_TARGET) --features "$(RUST_FEATURES)"

objcopy:
@ $(CROSS_COMPILE)objcopy -O binary $(KERNEL) build/$(PLATFORM)/kernel.bin
@ echo "Generated kernel.bin"

clean:
@ make -C clib clean
@ cargo clean
Expand Down
1 change: 1 addition & 0 deletions clib/src/arch/riscv/entry.S
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
.section .text.entry

entry:
li tp, 0
la sp, __stack_end
call main
.spin:
Expand Down
12 changes: 8 additions & 4 deletions clib/src/platform/qemu-virt-riscv64/boot.S
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@
.align 12
pagetable:
.dword 0, 0
.dword (0x80000 << 10) | 0xcf // MAP 0x80000000 ~ 0xc0000000 DIRECTLY
.rept 512 - 3 - 2
.dword (0x80000 << 10) | 0xcf // 2: VA 0x8000_0000 ~ 0xc000_0000 -> PA 0x8000_0000 ~ 0xc000_0000
.rept 258 - 2 - 1
.dword 0
.endr
.dword (0x80000 << 10) | 0xcf // 258: VA 0xffff_ffc0_8000_0000 ~ 0xffff_ffc0_c000_0000 to 0x8000_0000 ~ 0xc000_0000
.rept 512 - 258 - 1
.dword 0
.endr
.dword (0x80000 << 10) | 0xcf // MAP 0xffffffff80000000 ~ 0xffffffffc0000000 to 0x80000000 ~ 0xc0000000

#define PAGETABLE_ADDR 0x80201000
#define ENTRY_ADDR 0xffffffc080205000

.global bootloader
.section .text.bootloader
Expand All @@ -22,5 +26,5 @@ bootloader:

li tp, 0

li t0, 0xffffffff80205000
li t0, ENTRY_ADDR
jr t0
65 changes: 27 additions & 38 deletions clib/src/platform/starfive-jh7110-riscv64/boot.S
Original file line number Diff line number Diff line change
@@ -1,48 +1,37 @@
.section .bss.bootloader
#define PTE_FLAG 0xcf

.section .rodata.bootloader
.align 12
pagetable:
.space 4096
.dword 0
.dword (0x040000 << 10) | PTE_FLAG // 001: VA 0x4000_0000 ~ 0x8000_0000 -> PA 0x4000_0000 ~ 0x8000_4000
.dword (0x080000 << 10) | PTE_FLAG // 002: VA 0x8000_0000 ~ 0x0c0000000 -> PA 0x8000_0000 ~ 0x0c0004000
.dword (0x0c0000 << 10) | PTE_FLAG // 003: VA 0xc000_0000 ~ 0x100000000 -> PA 0xc000_0000 ~ 0x1000004000
.dword (0x100000 << 10) | PTE_FLAG // 004: VA 0x1000_0000 ~ 0x140000000 -> PA 0x1000_0000 ~ 0x1400004000
.rept 257 - 4 - 1
.dword 0
.endr
.dword (0x040000 << 10) | PTE_FLAG // 257: VA 0xffff_ffc0_4000_0000 ~ 0xffff_fff0_8000_0000 to 0x040000000 ~ 0x080000000
.dword (0x080000 << 10) | PTE_FLAG // 258: VA 0xffff_ffc0_8000_0000 ~ 0xffff_fff0_c000_0000 to 0x080000000 ~ 0x0c0000000
.dword (0x0c0000 << 10) | PTE_FLAG // 259: VA 0xffff_ffc0_c000_0000 ~ 0xffff_fff1_0000_0000 to 0x0c0000000 ~ 0x100000000
.dword (0x100000 << 10) | PTE_FLAG // 260: VA 0xffff_ffc1_0000_0000 ~ 0xffff_fff1_4000_0000 to 0x100000000 ~ 0x140000000
.rept 512 - 260 - 1
.dword 0
.endr

#define PAGETABLE_ADDR 0xa0001000
#define PAGETABLE_ADDR (0x40080000 + 0x1000)
#define ENTRY_ADDR (0xffffffc040080000 + 0x5000)

.global bootloader
.global bootloader

.section .text.bootloader
.align 2
bootloader:
li a6, 0x0
li a7, 0x1
li a0, 'A'
li a1, 0x0
ecall

# la t0, pagetable
li t0, PAGETABLE_ADDR

// map 0x80000000 ~ 0xc0000000 DIRECTLY
li t1, 0x80000
slli t1, t1, 10
ori t1, t1, 0xf
sd t1, (2 * 8)(t0)

// map 0x80000000 ~ 0xc0000000 to 0xffffffff80000000
li t2, 510 * 8
add t0, t0, t2
sd t1, 0x0(t0)
.section .text.bootloader
.align 2
bootloader:
li t0, (PAGETABLE_ADDR >> 12) | (8 << 60)

li t0, PAGETABLE_ADDR

srli t0, t0, 12
li t1, 8 << 60
or t0, t0, t1
sfence.vma
csrw satp, t0
sfence.vma

li a6, 0x0
li a7, 0x1
li a0, 'A'
li a1, 0x0
ecall

li t0, 0xffffffffa0005000
li t0, ENTRY_ADDR
jr t0
6 changes: 6 additions & 0 deletions config/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
*
*.*

!.gitignore
!.config.mk
!Kconfig
8 changes: 7 additions & 1 deletion config/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ config COMPILE_MODE

endmenu

menu "Logging Configuration"
menu "Debug Configuration"

choice
prompt "Log Level"
Expand Down Expand Up @@ -143,6 +143,12 @@ config LOG_SYSCALL
help
Enable detailed tracing of all system calls.

config WARN_UNIMPLEMENTED_SYSCALL
bool "Warn on Unimplemented Syscalls"
default y
help
Enable warnings for unimplemented syscalls.

endmenu

menu "Init Process Configuration"
Expand Down
1 change: 1 addition & 0 deletions config/config.mk
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ KERNEL_CONFIG = \
KERNELX_RELEASE=$(KERNELX_RELEASE) \
LOG_LEVEL=$(LOG_LEVEL) \
LOG_SYSCALL=$(CONFIG_LOG_SYSCALL) \
WARN_UNIMPLEMENTED_SYSCALL=$(CONFIG_WARN_UNIMPLEMENTED_SYSCALL) \
COMPILE_MODE=$(COMPILE_MODE) \

# Configuration targets
Expand Down
13 changes: 9 additions & 4 deletions scripts/build.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
fn main() {
let platform = std::env::var("PLATFORM").unwrap_or_else(|_| "qemu-virt-riscv64".to_string());
// let arch = std::env::var("ARCH").unwrap_or_else(|_| "riscv".into());
let arch = std::env::var("ARCH").unwrap();
// let arch_bits = std::env::var("ARCH_BITS").unwrap_or_else(|_| "64".into());
let arch_bits = std::env::var("ARCH_BITS").unwrap();

match platform.as_str() {
Expand All @@ -13,17 +11,24 @@ fn main() {
println!("cargo:warning=Unknown platform: {}", platform);
}
}
println!("cargo:rustc-cfg=arch_{}{}", arch, arch_bits);

// Link C library
println!("cargo:rustc-link-search=native=clib/build/{}{}", arch, arch_bits);
println!("cargo:rustc-link-lib=static=kernelx_clib");

println!("cargo:rustc-cfg=arch_{}", std::env::var("ARCH").unwrap_or_else(|_| "riscv".to_string()));

// Link vdso
let vdso_path = format!("vdso/build/{}{}/vdso.o", arch, arch_bits);
println!("cargo:rustc-link-arg={}", vdso_path);
println!("cargo:rerun-if-changed={}", vdso_path);

// Copy vDSO symbols
let out_dir = std::env::var("OUT_DIR").unwrap();
let symbols_src = format!("vdso/build/{}{}/symbols.inc", arch, arch_bits);
let symbols_dst = format!("{}/symbols.inc", out_dir);
std::fs::copy(&symbols_src, &symbols_dst).expect("Failed to copy vDSO symbols");
println!("cargo:rerun-if-changed={}", symbols_src);

// Linker script
let linker = format!("scripts/linker/{}.ld", platform);
println!("cargo:rustc-link-arg=-T{}", linker);
Expand Down
2 changes: 1 addition & 1 deletion scripts/cmake/env.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ include(${KERNELX_HOME}/scripts/cmake/parse_config.cmake)

if(ARCH STREQUAL "riscv")
if (ARCH_BITS STREQUAL "64")
parse_config(${KERNELX_HOME}/scripts/flags/riscv64.env)
parse_config(${KERNELX_HOME}/scripts/env/riscv64.env)
message("ARCH_COMMON_FLAGS=${ARCH_COMMON_FLAGS}")
else()
message(FATAL_ERROR "Unsupported riscv architecture bits: ${ARCH_BITS}")
Expand Down
File renamed without changes.
File renamed without changes.
1 change: 0 additions & 1 deletion scripts/flags/riscv64.cmake

This file was deleted.

2 changes: 1 addition & 1 deletion scripts/linker/qemu-virt-riscv64.ld
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PHYS_BASE = 0x80200000;
VIRT_BASE = 0xffffffff80200000;
VIRT_BASE = 0xffffffc080200000;

ENTRY(bootloader)

Expand Down
6 changes: 3 additions & 3 deletions scripts/linker/starfive-jh7110-riscv64.ld
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PHYS_BASE = 0xa0000000;
VIRT_BASE = 0xffffffffa0000000;
PHYS_BASE = 0x40080000;
VIRT_BASE = 0xffffffc040080000;

ENTRY(bootloader)

Expand All @@ -9,7 +9,7 @@ SECTIONS {
. = PHYS_BASE;
.bootloader : {
KEEP(*(.text.bootloader))
KEEP(*(.bss.bootloader))
KEEP(*(.rodata.bootloader))
}

. = VIRT_BASE + 0x5000;
Expand Down
10 changes: 10 additions & 0 deletions scripts/package.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

set -e
mkdir -p temp_image
sudo mount -o loop $IMAGE temp_image

sudo mkdir -p temp_image/boot
sudo cp $KERNEL_IMAGE temp_image/boot/kernel

sudo umount temp_image
3 changes: 2 additions & 1 deletion src/arch/arch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub trait ArchTrait {
/* ----- Context Switching ----- */
fn kernel_switch(from: *mut KernelContext, to: *mut KernelContext);
fn get_user_pc() -> usize;
fn return_to_user() -> !;

/* ----- Interrupt ------ */
fn wait_for_interrupt();
Expand All @@ -43,7 +44,7 @@ pub trait UserContextTrait: Clone {
fn set_addrspace(&mut self, addrspace: &crate::kernel::mm::AddrSpace);

fn set_sigaction_restorer(&mut self, uptr_restorer: usize) -> &mut Self;
fn restore_from_signal(&mut self, sigcontext: &SigContext);
fn restore_from_signal(&mut self, sigcontext: &SigContext) -> &mut Self;

fn set_user_entry(&mut self, entry: usize) -> &mut Self;
fn get_user_entry(&self) -> usize;
Expand Down
3 changes: 2 additions & 1 deletion src/arch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ pub type KernelContext = arch_impl::KernelContext;
pub type SigContext = arch_impl::SigContext;
pub type PageTable = arch_impl::PageTable;

// pub const PGBITS: usize = arch_impl::PGBITS;
pub const PGSIZE: usize = arch_impl::PGSIZE;
pub const PGMASK: usize = arch_impl::PGMASK;
pub const KADDR_OFFSET: usize = arch_impl::KADDR_OFFSET;

mod arch;
pub use arch::{PageTableTrait, UserContextTrait};
Expand All @@ -40,6 +40,7 @@ arch_export! {
/* ----- Context Switching ----- */
kernel_switch(from: *mut KernelContext, to: *mut KernelContext) -> ();
get_user_pc() -> usize;
return_to_user() -> !;

/* ----- Interrupt ------ */
wait_for_interrupt() -> ();
Expand Down
6 changes: 6 additions & 0 deletions src/arch/riscv/arch.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::arch::riscv::process;
use crate::arch::{Arch, ArchTrait, UserContextTrait};
use crate::kernel::scheduler::current;

Expand Down Expand Up @@ -31,6 +32,11 @@ impl ArchTrait for Arch {
current::tcb().user_context().get_user_entry()
}

#[inline(always)]
fn return_to_user() -> ! {
process::traphandle::return_to_user();
}

#[inline(always)]
fn kernel_switch(from: *mut KernelContext, to: *mut KernelContext) {
kernel_switch(from, to);
Expand Down
1 change: 1 addition & 0 deletions src/arch/riscv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub use pagetable::PageTable;
pub const PGBITS: usize = 12; // 4KB page size
pub const PGSIZE: usize = 1 << PGBITS; // 4096 bytes
pub const PGMASK: usize = PGSIZE - 1; // 0xfff
pub const KADDR_OFFSET: usize = 0xffff_ffc0_0000_0000; // Kernel virtual address offset

unsafe extern "C" {
fn asm_kerneltrap_entry() -> !;
Expand Down
Loading