Commit Graph

2813 Commits

Author SHA1 Message Date
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
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
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
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
vasilito 6048df4e91 kernel: remove todo!() on NonfatalInternalError in page fault handler
The page fault handler for user-mode faults had a separate
arms for Segv, RecursionLimitExceeded, and NonfatalInternalError from
try_correcting_page_tables. The first two fell through to return
Segv to the process; NonfatalInternalError called todo!() which
causes a kernel panic. This is a kernel-level crash triggered by
a userspace page table correction attempt that reports an internal
(non-fatal) error.

Fix: collapse NonfatalInternalError into the same fall-through arm
as Segv and RecursionLimitExceeded. The error name says 'nonfatal'
— it should not crash the kernel. The userspace process receives
a segmentation fault signal instead.
2026-07-09 20:39:47 +03:00
vasilito eab576b7ed kernel: log unknown syscall numbers before returning ENOSYS
Added println! to the syscall dispatch catch-all to log the
unknown syscall number and arguments when returning ENOSYS.
Previously any unrecognized syscall number silently returned
ENOSYS with no diagnostic, making it impossible to discover
missing syscall implementations without application-level
debugging.

Found by comprehensive disguised-stub audit (47 patterns, 37
actionable). This is the most impactful remaining fix from
that audit.
2026-07-09 15:08:26 +03:00
vasilito ea16a1b551 acpi: map FACS before parsing (not identity-mapped on all firmwares) 2026-07-09 12:04:39 +03:00
vasilito feb8dc26aa kernel: implement SYS_SYNC and SYS_SYNCFS handlers
Added sync() and syncfs() syscall handlers. Both are no-ops
since Red Bear filesystem I/O is synchronous (data is always
on disk). syncfs(fd) delegates to scheme.fsync() for the
filesystem containing the fd.

Cross-referenced with Linux 7.1 fs/sync.c ksys_sync() and
do_syncfs(). SYS_SYNC uses number 119, SYS_SYNCFS uses 120.
2026-07-09 11:25:04 +03:00
vasilito d64482f1a2 fadt: use FADT base address for fixed-register offsets (not data_address); futex: import FUTEX_REQUEUE 2026-07-09 11:16:45 +03:00
vasilito 6b69974f70 kernel: resolve /proc/self/fd/N and /proc/self/stat etc.
Extended /proc/self resolution to handle sub-paths like
/proc/self/fd/0, /proc/self/stat, /proc/self/status, etc.

Previously only /proc/self (the directory itself) was resolved.
Now /proc/self/fd, /proc/self/stat, /proc/self/maps, and all
other sub-paths are resolved by replacing 'self' with the
current PID and delegating to the standard proc_open() parser.

Cross-referenced with Linux 7.1 fs/proc/self.c proc_self_get_link()
which creates a symlink to the current PID directory. This
completes the /proc/self implementation for all sub-entries.
2026-07-09 11:16:13 +03:00
vasilito ffa502b2cb kernel: fix FADT field offset — read from SDT base, not data area
The FADT fields (PM1a_CNT, PM1a_STS, FIRMWARE_CTRL) are at fixed
offsets from the START of the SDT (including the 36-byte ACPI header),
not from the data area. Previously using sdt.data_address() caused
reads from the wrong offset, making shutdown/reboot fail on real
hardware.

Fixed by reading from the SDT base pointer instead of data_address().
Cross-referenced with Linux 7.1 drivers/acpi/acpica/tbfadt.c which
reads FADT fields from the table base, accounting for the header.
2026-07-09 11:13:36 +03:00
vasilito 2fc628fcdf kernel: implement FUTEX_REQUEUE operation
Added FUTEX_REQUEUE (opcode 2) to the futex syscall implementation.
Cross-referenced with Linux 7.1 kernel/futex/requeue.c futex_requeue().

Operation: wake up to  waiters on the primary futex, and
requeue up to  waiters to a secondary futex at addr2.
This avoids the thundering herd problem in pthread condition
variable broadcast: instead of waking all waiters and having
them immediately contend, most are moved to a private futex
where they wake one at a time.

Previously the futex syscall would return EINVAL for FUTEX_REQUEUE,
breaking pthread_cond_broadcast on contended condition variables.
2026-07-09 10:48:47 +03:00
vasilito 6f7752892b kernel: implement /proc/self — symlink to current process
Added /proc/self resolution in open_inner(). When /proc/self
is opened, it resolves to /proc/<current-pid> by reading
context::current().pid and returning a ProcDir handle.

Cross-referenced with Linux 7.1 fs/proc/self.c proc_self_get_link()
which creates a symlink inode pointing to the current PID.

This enables 'ls /proc/self', 'cat /proc/self/status', and all
other tools that use /proc/self to access their own process info.
2026-07-09 10:42:37 +03:00
vasilito 85347a5678 kernel: implement /proc/filesystems — supported filesystem types
Added ProcFilesystems ContextHandle that lists supported filesystems
in Linux /proc/filesystems format. Lists sysfs, proc, devtmpfs,
and redoxfs as supported types.

Cross-referenced with Linux 7.1 fs/filesystems.c filesystems_proc_show().
'nodev' prefix indicates filesystems not requiring a block device.

Also added to ProcRoot directory listing.
2026-07-09 10:15:57 +03:00
vasilito 06fcdaf54f kernel: implement /proc/version — kernel version string
Added ProcVersion ContextHandle that outputs the kernel version
string using CARGO_PKG_VERSION, TARGET, and COOKBOOK_SOURCE_IDENT
environment variables from the build system.

Cross-referenced with Linux 7.1 fs/proc/version.c version_proc_show().
Format: 'Red Bear OS version X.Y.Z (arch) source_ident'

Also added to ProcRoot directory listing.
2026-07-09 10:12:59 +03:00
vasilito a12b823396 kernel: implement /proc/loadavg — system load average
Added ProcLoadavg ContextHandle that counts runnable contexts
using context::contexts() iterator and Status::is_runnable().
Outputs Linux /proc/loadavg format:

  0.00 0.00 0.00 nr_runnable/total 0

Cross-referenced with Linux 7.1 fs/proc/loadavg.c loadavg_proc_show().
Load averages are 0.00 (Red Bear does not track 1/5/15-min EMA).
Last PID is 0 (no PID tracking yet).

Also added to ProcRoot directory listing.
2026-07-09 10:08:32 +03:00
vasilito 2b5e1e03fd kernel: implement /proc/uptime — system uptime
Added ProcUptime ContextHandle that reads monotonic() time
from the kernel and outputs it as uptime_seconds idle_seconds
in Linux /proc/uptime format (two floats with newline).

Cross-referenced with Linux 7.1 fs/proc/uptime.c uptime_proc_show().
Idle time is 0.0 since Red Bear does not track per-CPU idle time.

Also added to ProcRoot directory listing.
2026-07-09 10:04:10 +03:00
vasilito 24a7d990d4 kernel: implement /proc/meminfo — system memory information
Added ProcMeminfo ContextHandle that reads total_frames/free_frames
from the kernel memory subsystem and outputs them in Linux
/proc/meminfo format (MemTotal, MemFree, MemAvailable, Buffers,
Cached, SwapTotal, etc. all in kB).

Cross-referenced with Linux 7.1 fs/proc/meminfo.c meminfo_proc_show().

Also added to ProcRoot directory listing alongside cpuinfo.
2026-07-09 09:57:21 +03:00
vasilito 046ad5d87b kernel: implement /proc/cpuinfo — CPU information file
Added ProcCpuinfo ContextHandle that calls the arch-specific
cpu_info() function to output processor information (model,
features, cache, topology). Appears as a regular file in
/proc/cpuinfo and is listed in the ProcRoot directory.

Cross-referenced with Linux 7.1 arch/x86/kernel/cpu/proc.c
show_cpuinfo() which formats /proc/cpuinfo from cpu_data.
2026-07-09 01:41:58 +03:00
vasilito 62a1b0beac kernel: implement /proc/[pid]/fd/ directory listing
Added ProcFdDir ContextHandle that lists open file descriptors
as symlink directory entries. Reads from posix_fdtbl and outputs
fd numbers (0, 1, 2, ...) as directory entries.

Cross-referenced with Linux 7.1 fs/proc/fd.c proc_fd_link()
and tid_fd_revalidate(). Listed fds are open (non-None) entries
from the process's POSIX file descriptor table.

Each entry appears as /proc/[pid]/fd/[n] for use by tools
like ls, lsof, and ps.
2026-07-09 01:30:07 +03:00
vasilito 2cc512a3b9 kernel: add I/O accounting to SYS_READ2/SYS_WRITE2 (pread/pwrite)
SYS_READ2 and SYS_WRITE2 are the positioned read/write syscalls
(equivalent to pread/pwrite in POSIX). Previously only SYS_READ and
SYS_WRITE were counted. Now all four I/O paths are tracked.

This makes /proc/[pid]/io accurate for programs that use
pread/pwrite (common in random-access file I/O, memory-mapped
I/O bypass, and async I/O libraries).

Cross-referenced with Linux 7.1 mm/filemap.c:rw_verify_area
which tracks these accounting fields for all read/write variants.
2026-07-08 22:02:33 +03:00
vasilito 28c65bfcfb kernel: wire I/O accounting into sys_read/sys_write
sys_read increments io_rchar, io_syscr, io_read_bytes.
sys_write increments io_wchar, io_syscw, io_write_bytes.
This makes /proc/[pid]/io show actual I/O activity.

Cross-referenced with Linux 7.1 mm/filemap.c:filemap_get_pages
and include/linux/task_io_accounting.h. The fields are saturated
additions to avoid overflow on long-running processes.
2026-07-08 21:18:00 +03:00
vasilito 2d26e7442b kernel: implement /proc/[pid]/io — process I/O statistics
Added 7 I/O accounting fields to Context (rchar, wchar, syscr, syscw,
read_bytes, write_bytes, cancelled_write_bytes) and new ProcIo
ContextHandle that outputs them in Linux key=value format:

  rchar: 0
  wchar: 0
  syscr: 0
  syscw: 0
  read_bytes: 0
  write_bytes: 0
  cancelled_write_bytes: 0

Cross-referenced with Linux 7.1 fs/proc/base.c do_task_io_accounting().
All values start at 0 and will be incremented as the syscall
interface is extended to track I/O operations.
2026-07-08 19:18:47 +03:00
vasilito 207cf8707c kernel: implement /proc/[pid]/limits — resource limits
Added rlimits: [u64; 16] to Context (initialized to RLIM_INFINITY)
and new ProcLimits ContextHandle that outputs in Linux format:

  Limit                     Soft Limit          Hard Limit          Units
  Max cpu time              unlimited            unlimited            seconds
  Max file size             unlimited            unlimited            bytes
  ...

Cross-referenced with Linux 7.1 fs/proc/array.c proc_pid_limits().
All 16 RLIMIT_* resource limits are listed with their units.
2026-07-08 18:57:12 +03:00
vasilito 0b089c7582 kernel: implement /proc/[pid]/statm — memory summary
New ProcStatm ContextHandle that reports the 7-field memory summary
matching Linux /proc/[pid]/statm format:

  size resident shared text lib data dt

All values in pages. Cross-referenced with Linux 7.1
fs/proc/array.c:proc_pid_statm().

Resident count is approximated as the total physical grants
(GRANT_PHYS), since Red Bear has no paging/swapping and all
memory is resident. Other fields (shared, lib, dt) are 0 since
Red Bear's memory model doesn't distinguish these.
2026-07-08 18:54:32 +03:00