Commit Graph

2839 Commits

Author SHA1 Message Date
vasilito 19b936efff kernel: fix kfdwrite signature to match RB-modified trait (add arg param, cloexec field)
The RB fork's KernelScheme trait has an extra 'arg: u64' parameter in
kfdwrite (not in upstream). Also RB's FileDescriptor has a 'cloexec' field.
Both are now handled correctly.
2026-07-11 23:00:30 +03:00
vasilito 6933d715a2 kernel: proper merge onto upstream/master — restore kfdwrite + RB features
This merge uses -X ours to preserve ALL upstream functions while keeping
all RB-specific features (cpuinfo, iostat, msr, facs, fadt, s3_resume,
+rb0.3.1 version). The kfdwrite function that was lost during a bad
merge (7aa2a165) has been surgically restored from upstream/master.

Missing upstream functions still pending restoration:
- bulk_insert_files, resize in context.rs
- insert_hole, remove_hole, resize_hole in memory.rs
- eq, hash, get_event_stat, read_with_timeout in event.rs
- kcall in debug.rs
- __syscall_instruction_end in syscall.rs
2026-07-11 22:56:13 +03:00
vasilito c053bd5b5b Merge branch 'rb-backup-20260711-224412' into kernel-0.3.0 2026-07-11 22:50:59 +03:00
vasilito 00e7ffb7bc debug: add markers in dup_into to trace EEXIST source 2026-07-11 16:16:11 +03:00
vasilito f93ed40ed4 fix: remove AddrSpaceSwitchReadGuard from context switch path — eliminates deadlock
The AddrSpaceSwitchReadGuard was acquired in select_next_context() and stored
in percpu.new_addrsp_guard, but NEVER consumed/released. This held a read lock
on the address space forever, causing usermode_bootstrap's acquire_write() to
deadlock — the bootstrap process could never start.

The guard is redundant: CONTEXT_SWITCH_LOCK + context write guards in
SwitchResultInner already protect the address space during the switch.
Matching upstream commit ca67b1da which has no AddrSpaceSwitchReadGuard.
2026-07-11 15:38:36 +03:00
vasilito 788882b7a5 debug: finer-grained markers in usermode_bootstrap mmap section 2026-07-11 15:19:05 +03:00
vasilito 69d5bf92c6 debug: add ENTER/EXIT markers in usermode_bootstrap 2026-07-11 15:12:54 +03:00
vasilito ccae87cf90 fix: use info! macro instead of log::info! in startup/mod.rs 2026-07-11 15:02:30 +03:00
vasilito 501275f1f5 debug: add boot stage markers in startup/mod.rs for context switch diagnosis 2026-07-11 14:58:38 +03:00
vasilito ddf314f3e1 debug: boot stage markers 2026-07-11 14:38:52 +03:00
vasilito 9493db389a kernel: merge-fix — restore upstream scheduler fields, NUMA init, ordered lock try_write_arc
Merge resolution artifacts fixed:
- context.rs: restore EEVDF scheduler fields (vtime, vd, rem_slice, is_active, queue_key)
- context.rs: restore Status::is_dead() method and cmp::Reverse import
- context.rs: restore pub const CONTEXT_NAME_CAPAC
- memory.rs: restore AddrSpaceSwitchReadGuard struct
- percpu.rs: restore new_addrsp_guard field
- ordered.rs: restore try_write_arc method
- startup/memory.rs: return BumpAllocator instead of calling init_mm internally
- acpi/mod.rs: restore fadt/facs module declarations, slit/srat from upstream
2026-07-11 13:16:54 +03:00
vasilito 7aa2a165df Merge remote-tracking branch 'upstream/master' into kernel-0.3.0
# Conflicts:
#	src/acpi/mod.rs
#	src/acpi/rsdt.rs
#	src/acpi/rxsdt.rs
#	src/acpi/srat/aarch64.rs
#	src/acpi/xsdt.rs
#	src/arch/x86_shared/start.rs
#	src/context/mod.rs
#	src/context/switch.rs
#	src/main.rs
#	src/memory/mod.rs
#	src/numa.rs
#	src/scheme/sys/mod.rs
#	src/startup/mod.rs
2026-07-11 11:31:19 +03:00
vasilito 8e8dcfb09d 0.3.1: bump version suffix to +rb0.3.1 2026-07-11 11:22:32 +03:00
vasilito aca5b3f2ce debug: trace sys_write ENTER for low FDs and upper FDs 2026-07-11 09:06:27 +03:00
vasilito 7b7db479f0 FIX: AddrSpace dup handles must NOT be POSITIONED
AddrSpace handles from dup(b'empty') and dup(b'exclusive') use the write
syscall for mmap commands, not seekable I/O. The kwriteoff handler returns
the mapped address (not bytes written), which sys_write would incorrectly
add to the file offset when POSITIONED is set. This corrupted the offset,
causing the next write to fail with EINVAL (offset_word skip exhausted
the buffer). Only mmap-min-addr needs POSITIONED for actual read/write.
2026-07-11 08:06:32 +03:00
vasilito 3322970697 debug: trace EINVAL in AddrSpace kwriteoff mmap handler 2026-07-11 07:56:47 +03:00
vasilito 879b58be13 FIX: default kreadoff/kwriteoff must accept offset=0 for non-positioned I/O
The previous default only accepted u64::MAX as 'stream write' offset, but
sys_write/sys_read pass offset=0 for non-positioned I/O. This caused
ESPIPE on all writes to non-positioned FDs (Debug/stdout). Now both
u64::MAX and 0 are accepted as 'no offset' for the default implementation.
Schemes that override kwriteoff (AddrSpace) still handle offset=0 correctly.
2026-07-11 07:32:17 +03:00
vasilito 4b6d9bdb58 FIX: sys_read/sys_write must pass u64::MAX for non-positioned I/O
The default kreadoff/kwriteoff implementations in scheme/mod.rs require
offset=u64::MAX for stream (non-positioned) I/O. Passing offset=0 causes
ESPIPE (Illegal seek), which breaks ALL writes to non-positioned FDs
including Debug/stdout. This was the root cause of the procmgr child
appearing to 'panic' — its log output silently failed with ESPIPE.
2026-07-11 06:45:09 +03:00
vasilito 8f34d2e2ad trace sys_write errors for low FDs 2026-07-11 06:35:23 +03:00
vasilito be8b1a9f79 add posix FD list to AwaitingFiletableChange and EBADF logs 2026-07-11 06:18:41 +03:00
R Aadarsh 00cfa2d225 Add support for querying dom-node map 2026-07-11 08:33:17 +05:30
vasilito 8d127d3796 log context name in Context::remove_file for upper FD tracing 2026-07-11 05:53:21 +03:00
vasilito f821d2190a log upper FD removal in FdTbl::remove_file 2026-07-11 05:39:45 +03:00
vasilito 63d7dc0470 add filetable state logging at EBADF point 2026-07-11 05:30:31 +03:00
vasilito f6ef98755c fix borrow issue in filetable logging 2026-07-11 05:04:47 +03:00
vasilito ed76375e7e fix ArrayString type for context name logging 2026-07-11 05:03:47 +03:00
vasilito 86f4f8758e add context name to openat log and filetable change logging 2026-07-11 04:51:40 +03:00
vasilito 182cc74c2d kernel: fix duplicate closing brace in openat 2026-07-11 04:25:19 +03:00
vasilito 2dac8e3326 kernel: fix openat log - revert to simple version (no context name) 2026-07-11 04:17:30 +03:00
vasilito e3fbd6670c kernel: add context name to openat log 2026-07-11 04:09:38 +03:00
vasilito 7939799536 kernel: add openat logging to trace bootstrap syscalls 2026-07-11 03:57:57 +03:00
vasilito 9444893c1d kernel: add INFO logging for bootstrap FD insertion and entry point 2026-07-11 03:38:54 +03:00
MJ Pooladkhay 985bc2623f Fix a race in get_round_robin_index. 2026-07-10 18:13:36 -06:00
vasilito 77e745a303 kernel: ignore mmap result in bootstrap stack setup
The addr_space_lock.mmap() result must be used or explicitly ignored.
Use let _ = ... to discard it — if mmap fails here, the process
crash will be more informative than a Result warning.
2026-07-10 23:34:53 +03:00
vasilito 74f8c118f8 kernel: fix compile errors from futex_wake_index and stack mmap
- syscall/futex.rs: scope the context write guard so futexes.swap_remove
  can borrow mutably after the guard drops
- syscall/process.rs: add missing 'shared' arg to Grant::zeroed
2026-07-10 23:28:07 +03:00
vasilito fcfdd2ad09 kernel/rmm: add free frame tracking to bump allocator
The bump allocator previously had no way to track which frames are
free for reuse. Add a FreeNode struct that records (next, count,
phys) for freed regions, enabling frame recycling after initial
identity-mapping setup is complete.
2026-07-10 23:04:05 +03:00
vasilito ce640bea73 kernel: fix usermode_bootstrap stack mmap error handling
The .map_err().?() pattern doesn't work in a function returning (),
because ? requires the enclosing function to return Result. Replace
with plain .expect() (these are bootstrap initialization failures —
if mmap fails here, the kernel cannot continue).
2026-07-10 23:00:45 +03:00
vasilito 957392830a kernel: add futex_wake_index field and complete futex work 2026-07-10 22:51:32 +03:00
vasilito 397c701d45 kernel: map and set user stack for initial bootstrap init
The initial bootstrap init (started directly by the kernel via
usermode_bootstrap) had RSP = 0 from InterruptStack::init(), which
does NOT set RSP. This caused any push/call in the binary to fault
on address 0.

The Rust panic handler caught the resulting page fault and generated
ud2 — visible as 'Invalid opcode fault' at a low RIP before main()
ever runs. This was the root cause of the init crash.

Fix:
- Map 8 pages (32 KiB) of user stack just above the initfs image
- Set RSP to the top of the stack
- Add debug log showing the mapped region

The stack is intentionally small (32 KiB) — enough for Rust _start
to run but small enough to detect stack overflow immediately. Once
init calls fexec_impl to spawn the real init, the real init gets its
own full 8 MB stack via the redox-rt exec path.
2026-07-10 22:48:21 +03:00
vasilito 62d5c08d06 kernel: implement EmulateArch::invalidate and clean up futex TODOs
- rmm/src/arch/emulate.rs: replace unimplemented!() with actual TLB
  invalidation (remove the address from the map). Without this the
  emulate arch silently fails to invalidate TLB entries during
  context switches, which can cause stale mappings.
- src/syscall/futex.rs: remove completed TODO marker for FUTEX_REQUEUE
  (work done in a prior commit).
2026-07-10 22:32:08 +03:00
vasilito 1c9bb1524e kernel: import UserSliceRo and FileHandle in syscall/process.rs
The sys_mkns and sys_setns functions added in 48fc2f1c reference
UserSliceRo and FileHandle but the imports were not updated, causing
the build to fail with 'cannot find type' errors. This commit adds
the missing imports.
2026-07-10 16:54:37 +03:00
vasilito 48fc2f1c90 kernel: implement SYS_MKNS and SYS_SETNS for namespace management
Adds proper kernel-level namespace syscall handling:

- SYS_MKNS: dups the current namespace fd with the caller-supplied
  buffer (NsDup::ForkNs + scheme names). The dup is handled by the
  bootstrap's userspace namespace manager (initnsmgr) which creates
  the new namespace and returns the fd.

- SYS_SETNS: switches the calling context's ns_fd to the given fd.
  This makes the new namespace the default for all subsequent scheme
  lookups by this context.

- Context gains an ns_fd: Option<usize> field, initialized to None,
  meaning "use the global namespace" (default).

- Both syscalls are wired into the dispatch table in syscall/mod.rs.

Per local/docs/LOCAL-FORK-SUPREMACY-POLICY.md: the kernel fork must
be complete. Previously SYS_SETNS was undefined and SYS_MKNS returned
ENOSYS; init's setrens() panicked on bare metal.
2026-07-10 16:13:25 +03:00
vasilito edfd5722fc kernel: normalize non-positioned read/write offset to 0 instead of u64::MAX
sys_read and sys_write passed u64::MAX as the offset for non-positioned
file descriptors. Many scheme handlers (AddrSpace, proc:Start, etc.)
check offset != 0 or offset % word_size != 0, causing EINVAL when
receiving u64::MAX. Passing 0 instead fixes all non-positioned read/write
operations across all scheme handlers uniformly.

This replaces the per-handler u64::MAX workarounds with a root-cause fix.
2026-07-10 15:24:01 +03:00
vasilito 4a7dab0f20 kernel: fix ContextHandle::Start offset check for non-positioned writes
sys_write passes u64::MAX as offset for non-positioned file descriptors.
ContextHandle::Start used an inline 'offset != 0' check which rejected
u64::MAX, causing EINVAL when bootstrap called start_fd.write(&[0]) during
fork_inner(). Replaced with require_zero_offset() which accepts both 0 and
u64::MAX (fixed in the previous commit for the same class of bug).
2026-07-10 15:12:16 +03:00
vasilito f913167973 proc: accept u64::MAX offset in require_zero_offset for non-positioned writes
sys_write passes u64::MAX as the offset sentinel for non-positioned file
descriptors. The proc scheme's require_zero_offset rejected this value,
causing bootstrap's tcb_activate to crash with EINVAL when writing
EnvRegisters (fsbase) back to the regs/env handle.

This was the root cause of the 'Invalid opcode fault' during bootstrap
(PID 0) that prevented redbear-mini from reaching a login prompt.
2026-07-10 13:42:11 +03:00
R Aadarsh bb06db93ce Add support for querying numa info from userspace 2026-07-10 15:46:02 +05:30
vasilito 165e3c478f kernel: remove log crate dependency from OOM handler 2026-07-10 01:50:34 +03:00
vasilito faa61f70fa kernel: replace todo!(oom) with proper ENOMEM-based signal path 2026-07-10 01:07:21 +03:00
vasilito ba2f78e12b kernel: replace OOM panic with graceful log-and-continue
Previously: PfError::Oom during page fault recovery caused the
entire kernel to panic via todo!("oom"). This means a single
OOM allocation in a user process would crash the whole OS.

Now: OOM is logged as a warning, the faulting process receives
SIGSEGV (same path as other non-fatal page faults), and the
kernel continues. The process may be killed, but the system
stays up. This is consistent with how Linux handles OOM during
page fault handling: send SIGSEGV/SIGKILL, don't panic.
2026-07-10 00:45:40 +03:00
vasilito 62a3b56840 kernel: handle HardBlocked(AwaitingMmap) in proc stop — don't panic 2026-07-09 23:54:10 +03:00