Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
7c5bab8
feat(starry-kernel): port LKM loader + cargo xtask starry kmod build
LorenzLorentz May 21, 2026
ac66918
fix(kmod): resolve review compilation errors
LorenzLorentz May 21, 2026
ecbd96a
style(kmod): apply cargo fmt
LorenzLorentz May 21, 2026
64aa704
fix(kmod): address non-blocking review suggestions
LorenzLorentz May 21, 2026
86c89d6
fix(kmod): resolve compilation errors
LorenzLorentz May 21, 2026
4fb8c89
fix(starry-kernel): import vec! macro and bind slice as mut in finit_…
LorenzLorentz May 21, 2026
c884d8c
chore(starry-kernel): drop WORKFLOW_LINUX_SEMANTICS.md from PR-B
LorenzLorentz May 21, 2026
fba9b49
fix(starry-kernel): use is_multiple_of for kmod vmalloc page-align check
LorenzLorentz May 21, 2026
fc374b2
fix(starry-kernel): make SpecialFsFile::register a no-op
LorenzLorentz May 21, 2026
3a62c72
fix(starry-kernel): log kmod init failure at warn!, success at info!
LorenzLorentz May 21, 2026
4a071da
chore(starry-kernel): drop stray .docker-run.sh dev helper from PR-B
LorenzLorentz May 31, 2026
8f923fa
fix(kmod): adapt HAL imports to dev's ax_runtime::hal layout
LorenzLorentz May 31, 2026
d4872a2
chore(starry-kernel): regenerate Cargo.lock for lwprintf-rs
LorenzLorentz May 31, 2026
f206f26
fix(starry-kernel): reject duplicate kmod name before init + flush ic…
LorenzLorentz Jun 1, 2026
ab2ff28
fix(starry-kernel): forward varargs correctly in printk shims
LorenzLorentz Jun 1, 2026
e16df7c
fix(axbuild): drive rust-lld as gnu ELF driver for kmod partial link
LorenzLorentz Jun 1, 2026
8f8fd31
chore(starry-kernel): retrigger CI (apt aarch64 qemu network-timeout …
LorenzLorentz Jun 1, 2026
ec8e6a1
chore(starry-kernel): retrigger CI
LorenzLorentz Jun 1, 2026
068faf0
fix(starry-kernel): follow section perms for kmod sections + propagat…
LorenzLorentz Jun 1, 2026
ee4b233
fix(starry-kernel): restore RW kernel mapping before freeing kmod sec…
LorenzLorentz Jun 2, 2026
0e2e4fc
chore(starry-kernel): retrigger CI (flaky duplicate ax-errno resolution)
LorenzLorentz Jun 2, 2026
e11a390
fix(axerrno): keep fork ahead of published ax-errno to fix kmod build
LorenzLorentz Jun 2, 2026
73a4e16
merge: bring feat/starry-lkm up to date with dev
LorenzLorentz Jun 2, 2026
c63fde2
fix(starry-kernel): adapt kmod loader errno boundary
LorenzLorentz Jun 2, 2026
58db53a
chore(starry-kernel): retrigger CI
LorenzLorentz Jun 2, 2026
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
22 changes: 17 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion os/StarryOS/kernel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,9 @@ ax-ipi = { workspace = true }
static-keys = "0.8"
ddebug = "0.5"
kprobe = "0.5"
kmod-loader = "0.2"
kmod = { version = "0.2", package = "kmod-tools" }
kmod-loader = "0.2.1"
lwprintf-rs = "0.3"
some-serial = { workspace = true, optional = true }
sg200x-bsp = { workspace = true, optional = true }
# 0.9 to match sg200x-bsp's internal tock-registers; cannot use workspace (0.10)
Expand Down
1 change: 1 addition & 0 deletions os/StarryOS/kernel/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub fn init(args: &[String], envs: &[String]) {
crate::ebpf::init_ebpf();
crate::perf::perf_event_init();
}
crate::kmod::init_kmod();

// FIXME: loongarch64 selftest hangs on QEMU; the kprobe crate's loongarch64
// breakpoint handling needs upstream fixes before selftest can be enabled.
Expand Down
136 changes: 136 additions & 0 deletions os/StarryOS/kernel/src/kmod/kprint.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
//! `printk`/`snprintf`/`sprintf`/`memset` C-ABI shims used by every
//! loadable kernel module that calls into the Linux-side logging API.
//!
//! Ported from `Starry-OS/StarryOS:ebpf-kmod`
//! (`kernel/src/kmod/shim/kprint.rs`). Each exported function is
//! resolved by `KmodHelper::resolve_symbol` via the global kallsyms
//! table at module load time, so they must be referenced (not just
//! declared `pub`) in the kernel image — that's what `#[capi_fn]` from
//! `kmod-tools` arranges.

use core::{
ffi::{VaList, c_char, c_int},
ptr::null_mut,
};

use kmod::capi_fn;
use lwprintf_rs::SIZE_MAX;

/// ASCII SOH character. See
/// <https://elixir.bootlin.com/linux/v6.17/source/include/linux/kern_levels.h#L5>
#[allow(dead_code)]
const KERN_SOH: u8 = 0x01;

const KERN_EMERG: &[u8; 2] = &[0x01, b'0'];
const KERN_ALERT: &[u8; 2] = &[0x01, b'1'];
const KERN_CRIT: &[u8; 2] = &[0x01, b'2'];
const KERN_ERR: &[u8; 2] = &[0x01, b'3'];
const KERN_WARNING: &[u8; 2] = &[0x01, b'4'];
const KERN_NOTICE: &[u8; 2] = &[0x01, b'5'];
const KERN_INFO: &[u8; 2] = &[0x01, b'6'];
const KERN_DEBUG: &[u8; 2] = &[0x01, b'7'];

const LOG_LEVELS: &[&[u8; 2]] = &[
KERN_EMERG,
KERN_ALERT,
KERN_CRIT,
KERN_ERR,
KERN_WARNING,
KERN_NOTICE,
KERN_INFO,
KERN_DEBUG,
];

/// `memset` shim. C ABI; some modules call this directly rather than
/// through the compiler's intrinsic.
#[capi_fn]
pub unsafe extern "C" fn memset(s: *mut core::ffi::c_void, c: c_int, n: usize) -> *mut c_char {
let xs = s as *mut u8;
let byte = c as u8;
for i in 0..n {
// SAFETY: caller-guaranteed validity of `s` for `n` bytes (C contract).
unsafe { *xs.add(i) = byte };
}
s as *mut c_char
}

/// Per-character writer that `lwprintf_vprintf_ex` calls back into.
#[capi_fn]
unsafe extern "C" fn write_char(c: u8) {
ax_print!("{}", c as char);
}

/// Shared body for the `printk`-family entry points. Takes an already
/// `va_start`-ed `VaList` so that both `_printk` and `__warn_printk` can
/// forward their *own* variadic arguments here. Strips a leading `KERN_*`
/// level prefix and re-routes to `ax_print!`-backed `lwprintf`.
unsafe fn vprintk(fmt: *const c_char, args: VaList) -> i32 {
// SAFETY: `fmt` is C-ABI; we treat it as a NUL-terminated string.
let c_str_fmt = unsafe { core::ffi::CStr::from_ptr(fmt) };
let fmt_bytes = c_str_fmt.to_bytes();
let level_prefix = LOG_LEVELS
.iter()
.find(|&&level| fmt_bytes.starts_with(level))
.copied();
let trimmed = if let Some(level) = level_prefix {
&fmt_bytes[level.len()..]
} else {
fmt_bytes
};
match level_prefix {
Some(KERN_EMERG) | Some(KERN_ALERT) | Some(KERN_CRIT) | Some(KERN_ERR) => {
ax_print!("[ERROR] ");
}
Some(KERN_WARNING) => {
ax_print!("[WARN] ");
}
Some(KERN_NOTICE) | Some(KERN_INFO) => {
ax_print!("[INFO] ");
}
Some(KERN_DEBUG) => {
ax_print!("[DEBUG] ");
}
_ => {
ax_print!("[INFO] ");
}
}
// SAFETY: `trimmed` points into the same NUL-terminated string
// (offset by the level prefix length, which is shorter than the
// total). The caller's `VaList` is forwarded as-is.
unsafe { lwprintf_rs::lwprintf_vprintf_ex(null_mut(), trimmed.as_ptr() as _, args) }
}

/// Linux `printk(fmt, ...)`.
#[capi_fn]
unsafe extern "C" fn _printk(fmt: *const c_char, args: ...) -> i32 {
// SAFETY: forward this call's own variadic list to the shared body.
unsafe { vprintk(fmt, args) }
}

/// `__warn_printk` alias used by `WARN_ON_ONCE` and friends.
#[capi_fn]
unsafe extern "C" fn __warn_printk(fmt: *const c_char, args: ...) -> i32 {
// SAFETY: forward this call's own variadic list — *not* `_printk`'s — to
// the shared body, so the arguments are not mis-shifted by one slot.
unsafe { vprintk(fmt, args) }
}

/// `snprintf(buf, size, fmt, ...)`.
#[capi_fn]
unsafe extern "C" fn snprintf(
buf: *mut c_char,
size: usize,
fmt: *const c_char,
args: ...
) -> c_int {
// SAFETY: caller-supplied buffer + format. We hand them through to
// the C-side lwprintf which is itself memory-safe-by-contract.
unsafe { lwprintf_rs::lwprintf_vsnprintf_ex(null_mut(), buf, size, fmt, args) }
}

/// `sprintf(buf, fmt, ...)`. Equivalent to `snprintf` with `SIZE_MAX`.
#[capi_fn]
unsafe extern "C" fn sprintf(buf: *mut c_char, fmt: *const c_char, args: ...) -> c_int {
// SAFETY: same as `snprintf`.
unsafe { lwprintf_rs::lwprintf_vsnprintf_ex(null_mut(), buf, SIZE_MAX as _, fmt, args) }
}
Loading