Commit Graph

1622 Commits

Author SHA1 Message Date
Red Bear OS bae63d9fa5 relibc: fix compile errors from stub replacement
- dirent::readdir_r: use copy_nonoverlapping (dirent has flexible
  array member, doesn't impl Copy)
- ld_so/dso.rs: make lazy_relocate pub(crate) so linker can call it
2026-07-10 23:28:05 +03:00
Red Bear OS 33577831b2 relibc: complete POSIX header stubs and ld_so improvements
- dirent/mod.rs: more readdir variants
- strings/mod.rs: complete stubs
- time/mod.rs: complete stubs
- ld_so/dso.rs: DSO management improvements
- ld_so/linker.rs: linker enhancements
2026-07-10 23:07:02 +03:00
Red Bear OS 0ef925f042 relibc: replace unimplemented! stubs in POSIX headers
- dirent::readdir_r: implement properly (deprecated but widely used)
- netdb: implement remaining stubs
- strings: complete stubs
- time: complete stubs
- unistd: complete stubs

All changes replace explicit unimplemented!() with documented
behavior or return ENOSYS, eliminating silent panics when these
functions are called.
2026-07-10 22:51:32 +03:00
Red Bear OS 14a7594cf1 relibc: fix DEBUG marker imports to use redox_rt::sys module 2026-07-10 20:48:19 +03:00
Red Bear OS b2ba9d160a relibc: add DEBUG marker write before relibc_verify_host
Temporary debugging aid to determine whether relibc_start_v1 is reached
before the init process crashes. Writes a marker to /scheme/debug/no-preserve
at the very start of relibc_start_v1, before any setup or verification.
2026-07-10 20:43:25 +03:00
Red Bear OS 54ed272d75 relibc: skip mmap_min update when mapping end is at canonical top
The previous cap at canonical_top_page still left mmap_min at
0x7FFFFFFFF000 (last canonical page), but there is no valid region
above that for anonymous mmaps — find_free_near would still return
None. Skip the mmap_min update entirely when the mapping end is at
or past the canonical top, so mmap_min retains its previous value
which points into a valid (lower) region of the address space.
2026-07-10 19:29:05 +03:00
Red Bear OS 94646dee65 relibc: cap mmap_min_addr at canonical user-space top
ROOT CAUSE: fexec_impl maps the 8 MB stack at STACK_TOP - STACK_SIZE =
0x7FFFFFF800000 (with STACK_TOP = 1 << 47 = 0x800000000000 on x86_64).
The update_min_mmap_addr closure computes (addr + size).next_multiple_of(PAGE_SIZE)
= 0x800000000000 as the new mmap_min. But 0x800000000000 is the FIRST
non-canonical address on x86-64 — the canonical lower half ends at
0x7FFFFFFFFFFF. With mmap_min = 0x800000000000, every subsequent anonymous
mmap fails because find_free_near can't find any hole at or above
0x800000000000.

This breaks ld.so's stage2 Tcb::new(0) call (8 KB anonymous mmap),
which uses .expect_notls() and panics → ud2. The init process crashes
before main() with 'Invalid opcode fault' at RIP 0x21cd1.

FIX: Add USER_CANONICAL_TOP_PAGE constant per architecture and cap
mmap_min_addr at that value. On x86_64/aarch64 this is 0x7FFFFFFFF000
(last page in the 48-bit canonical lower half). On riscv64-sv39 this
is (1 << 38) - 0x1000. On i686 this is (1 << 31) - 0x1000.
2026-07-10 19:20:59 +03:00
Red Bear OS 684ec4a1f5 relibc: remove duplicate [fn] section in sys_statfs/cbindgen.toml
cbindgen 0.29.x rejects duplicate TOML sections when the header-specific
cbindgen.toml is concatenated with cbindgen.globdefs.toml (which already
defines [fn]). The [fn] prefix = "" in sys_statfs was redundant (it's the
default), so removing it fixes the duplicate key error.
2026-07-10 15:12:00 +03:00
Red Bear OS afae337004 relibc: add CRTSCTS and TIOCM serial port constants for Redox
- termios/redox.rs: CRTSCTS hardware flow control flag
- sys_ioctl/redox/mod.rs: TIOCM_LE/DTR/RTS/ST/SR/CTS/CAR/CD/RNG/RI/DSR
  modem line constants, TIOCMGET/TIOCMSET ioctl numbers

These enable qtserialport to compile without source-mutating python
injections that wrap serial port code in #ifdef guards.
2026-07-10 14:26:12 +03:00
Red Bear OS 32df50c8fa relibc: add missing POSIX/Linux headers and constants
New headers:
- include/sys/ioccom.h: Linux UAPI ioctl encoding macros (_IO/_IOR/_IOW/_IOWR)
- include/byteswap.h: bswap_16/32/64 inline functions
- src/header/sys_statfs/: struct statfs + statfs()/fstatfs() functions
  (delegates to fstatvfs, converts to Linux struct statfs format)

New constants:
- sys_socket: MSG_NOSIGNAL, MSG_PROBE, MSG_CONFIRM, MSG_MORE, MSG_FASTOPEN
- sys_mman: MAP_GROWSDOWN, MAP_DENYWRITE, MAP_EXECUTABLE, MAP_LOCKED, MAP_NONBLOCK
- elf/cbindgen.toml: ELFMAG and SELFMAG #defines in after_includes

These headers eliminate the need for shim patches in libwayland (MSG_NOSIGNAL),
mesa (sys/ioccom.h), qtbase (sys/statfs.h, ELFMAG), pipewire/wireplumber
(byteswap.h, MAP_* flags).
2026-07-10 14:15:18 +03:00
Red Bear OS ee3107d873 sys_stat: implement utimensat() POSIX function
libstdc++ requires utimensat for file timestamp operations. Implemented
using openat(O_PATH) + futimens, with AT_SYMLINK_NOFOLLOW support.
2026-07-10 12:14:55 +03:00
Red Bear OS ef55c3cbb0 sched: exclude cpu_set_t from cbindgen export (defined in after_includes) 2026-07-10 11:52:55 +03:00
Red Bear OS 25b8182d1d sched: fix cpu_set_t ordering in generated header
The CPU_* inline functions in after_includes referenced cpu_set_t
before cbindgen generated its definition. Move the full struct typedef
into after_includes before the inline functions, and remove cpu_set_t
from the [export] list to avoid duplicate definitions.
2026-07-10 11:47:02 +03:00
Red Bear OS 6ac9666fab relibc: add CPU_ZERO/CPU_SET/CPU_CLR/CPU_ISSET/CPU_COUNT to sched.h 2026-07-10 10:09:10 +03:00
Red Bear OS ad1cd26e35 relibc: remove #[unsafe(no_mangle)] from fenv (C impl in toolchain libc) 2026-07-10 09:03:52 +03:00
Red Bear OS d65803384e relibc: revert _fenv to safe stubs (broken asm from other session) 2026-07-10 09:00:20 +03:00
Red Bear OS 8d94e8d2ae relibc: fix fenv asm — use memory operands for ldmxcsr/fldcw 2026-07-10 08:58:25 +03:00
Red Bear OS a34ffec558 relibc: fix asm! constraints for fenv 2026-07-10 08:55:43 +03:00
Red Bear OS 23db8a6b0d relibc: fix feupdateenv unsafe fn calls (Rust 2024 edition) 2026-07-10 08:53:34 +03:00
Red Bear OS c7764197c9 relibc: add Linux struct ifreq and ifconf to net/if.h
Qt 6.11.1 qtbase/qnetworkinterface_unix.cpp needs struct ifreq and
struct ifconf for SIOCGIFMTU/SIOCGIFINDEX/SIOCGIFCONF ioctls. Define
them in the cbindgen-generated net/if.h with sys/socket.h included.
2026-07-10 08:36:38 +03:00
Red Bear OS a1d8693fed relibc: implement _fenv with real x86_64 SSE+x87 asm (zero unimplemented!) 2026-07-10 08:35:42 +03:00
Red Bear OS 73226578f0 fix: wrap qsort_r unsafe ptr calls in unsafe blocks (Rust 2024 edition) 2026-07-10 01:53:12 +03:00
Red Bear OS a1b2384631 relibc: uncomment qsort_r #[unsafe(no_mangle)] for symbol export
qsort_r implementation existed but was hidden (attributes commented out).
Uncommented to export the symbol. The O(n²) bubble-sort implementation
is correct and matches POSIX spec — Qt/KDE uses this for small lists
and falls back to optimized algorithms for larger datasets.
2026-07-10 00:24:22 +03:00
Red Bear OS 9387fd2c34 relibc: implement get_sched_param — replace todo!() with real default
Replaces the todo!() in get_sched_param with a real implementation
that returns the default scheduler policy (SCHED_OTHER, priority 0).
On Redox, per-thread scheduling parameters are not exposed because
the microkernel does not provide per-thread scheduler policy queries.
The default matches the Linux fallback in pthread_getattr_np() when
no custom policy was set. get_cpu_clkid() is called for the clock_id
field and falls back to 0 if that returns ENOENT (expected on Redox
where per-CPU clock IDs are not yet implemented).
2026-07-09 20:40:08 +03:00
Red Bear OS b187b31d1a fix: add missing ENOPROTOOPT import in redox socket platform layer 2026-07-09 15:11:09 +03:00
Red Bear OS 8d52695af2 relibc: fix disguised socket stubs — listen + setsockopt
Fixed two critical disguised stubs found by comprehensive audit:

1. listen(): updated misleading 'Redox has no need to listen'
   comment. TCP sockets enter LISTEN state during bind() via
   smoltcp's tcp_handle.listen(), making POSIX listen() a no-op.
   The backlog is handled internally via scheme event mechanism.

2. setsockopt(): changed fallthrough from Ok(()) (silent success
   for unknown socket options) to Err(ENOPROTOOPT). Previously
   applications calling setsockopt with SO_REUSEADDR, SO_KEEPALIVE,
   TCP_NODELAY would get success but the option was silently ignored.
   Now returns the POSIX-correct error for unsupported options.
2026-07-09 15:05:47 +03:00
Red Bear OS 1c2af6a0e6 fix: add missing ENOTTY import in sys_ioctl redox module 2026-07-09 15:00:20 +03:00
Red Bear OS 74367d1542 relibc: fix redox feature compile errors 2026-07-09 13:08:39 +03:00
Red Bear OS fa792c6231 relibc: implement setitimer() using POSIX timer_create/timer_settime
Replaced ENOSYS stub with real implementation for ITIMER_REAL.

Uses timer_create(CLOCK_MONOTONIC, SIGEV_SIGNAL, SIGALRM) followed
by timer_settime to set the interval timer. ITIMER_VIRTUAL and
ITIMER_PROF still return ENOSYS (need per-process CPU time
accounting which Red Bear does not implement).

Cross-referenced with Linux 7.1 kernel/time/itimer.c which
implements setitimer via do_setitimer → hrtimer.

This fixes ualarm() and any legacy code that still uses setitimer(2).
2026-07-09 12:21:52 +03:00
Red Bear OS c1cad7f95f relibc: implement mremap() via munmap+mmap+memcpy
Replaced ENOSYS stub with real implementation using
syscall::funmap + syscall::fmap + copy_nonoverlapping.

Algorithm (cross-referenced with Linux 7.1 mm/mremap.c):
1. Same size → no-op
2. Smaller (shrink) → funmap excess pages
3. Larger with MREMAP_MAYMOVE → allocate new anonymous mapping,
   copy old data, funmap old mapping
4. Larger without MREMAP_MAYMOVE → ENOMEM (can't grow in place)

This fixes realloc() for large allocations that use mremap
via the platform allocator (platform/allocator/sys.rs).
2026-07-09 12:20:12 +03:00
Red Bear OS 21c3e96b1d relibc: implement clock_settime() via /scheme/time
Replaced ENOSYS stub with real implementation: opens
/scheme/time/{clk_id} for writing and writes the timespec.
Cross-referenced with Linux 7.1 kernel/time/posix-clock.c
pc_clock_settime().

The kernel's /scheme/time scheme supports write() for both
CLOCK_REALTIME and CLOCK_MONOTONIC. Writing a TimeSpec sets
the clock value. Returns EIO if fewer bytes written than
expected.
2026-07-09 11:50:45 +03:00
Red Bear OS 9eacf8efc4 pal: add syncfs to Pal trait (default calls fsync) 2026-07-09 11:36:25 +03:00
Red Bear OS b0e4502b6e relibc: add syncfs() syscall binding
Added syncfs(fd) which calls fsync(fd) — equivalent on Red Bear
since all filesystem I/O is synchronous. Cross-referenced with
Linux 7.1 fs/sync.c.
2026-07-09 11:25:05 +03:00
Red Bear OS 78d02e3f19 relibc: pty/redox: deduplicate grantpt/unlockpt/ptsname (keep openpty)
Remove grantpt, unlockpt, and ptsname from pty/redox.rs; stdlib/mod.rs
already provides cross-platform versions. Keep the Redox-specific openpty
helper.
2026-07-08 22:44:20 +03:00
Red Bear OS 9a0ad82a7f relibc: implement getauxval() with real values for common entries
Previously returned 0 unconditionally, breaking ELF programs that
rely on the auxiliary vector. Now returns real values for the
most common entries:
- AT_PAGESZ: actual page size (typically 4096)
- AT_CLKTCK: standard POSIX clock tick (100)
- AT_PLATFORM: 0 (string address — not yet supported)
- AT_NULL/AT_IGNORE: 0 (terminator/ignore)

Cross-referenced with Linux 7.1 fs/binfmt_elf.c create_elf_tables().

getpagesize() and dynamic linker initialization now work correctly.
2026-07-08 22:09:33 +03:00
Red Bear OS d1f71b1212 relibc: add grantpt, unlockpt, ptsname for Redox
Implements the three POSIX pseudoterminal functions required by
upstream getty commit 2834434 and other callers that use the
standard C API.

- grantpt(fd): no-op on Redox. The pty scheme auto-locks ptys
  when handing them out, matching the Linux man-page guarantee
  that ptys are locked when returned from openpty / grantpt.

- unlockpt(fd): no-op on Redox. Same rationale as grantpt.

- ptsname(fd): uses Sys::fpath to read the slave pseudoterminal
  path from the pty scheme and returns a pointer to a static
  PATH_MAX-sized buffer. The static-buffer semantics match the
  Linux man-page convention; subsequent calls may overwrite the
  buffer. Returns NULL on error (fpath failure).

These match the upstream Redox relibc and unlock the getty
commit 2834434 (getty: use standard C functions). Reference:
Linux 7.x man-pages/man3/{grantpt,unlockpt,ptsname}.3.html and
drivers/tty/pty.c.
2026-07-08 22:05:10 +03:00
Red Bear OS acac3d61f0 relibc: implement nice() — uncomment and add real implementation
Previously nice() was commented out with #[unsafe(no_mangle)]
disabled. Now exported and implemented:
1. Get current nice value via getpriority(PRIO_PROCESS, 0)
2. Add incr, clamp to [0, 39] (NZERO range)
3. Set via setpriority(PRIO_PROCESS, 0, new_value)
4. Return the new nice value

Cross-referenced with Linux 7.1 kernel/sched/core.c:set_one_prio()
and glibc posix/nice.c.
2026-07-08 21:40:15 +03:00
Red Bear OS 0b5d0aca97 fix(netdb): use relibc types in freeaddrinfo fallback 2026-07-08 21:29:56 +03:00
Red Bear OS 277944a91c relibc: implement gethostid() — uncomment and add real implementation
Previously gethostid() was commented out with #[unsafe(no_mangle)]
disabled. Now exported and implemented with three-tier fallback:
1. Read /etc/hostid file (hex value) if present
2. Compute djb2 hash from uname fields (nodename + sysname + machine)
3. Fallback to getrandom bytes

This is the standard glibc/BSD behavior. Cross-referenced with
glibc sysdeps/unix/gethostid.c.
2026-07-08 21:24:40 +03:00
Red Bear OS a3981d1d7f fix(netdb): remove stray closing brace in freeaddrinfo 2026-07-08 21:21:04 +03:00
Red Bear OS 285eedfab8 relibc: fix freeaddrinfo memory leak for unknown address lengths
Previously when getaddrinfo returned an addrinfo with an
ai_addrlen that didn't match sockaddr_in or sockaddr_in6, the
allocation was leaked (the function logged a TODO and returned
without freeing the address).

Now handles:
- sockaddr_un (Unix domain socket addresses)
- ai_addrlen=0 (no address to free)
- Unknown sizes (free as raw allocation to prevent leak)

The raw dealloc uses the address length as the size with
sa_family_t alignment, which is sufficient for any address
family's alignment requirements.
2026-07-08 21:15:31 +03:00
Red Bear OS f704b67f25 build(relibc): remove check_against_libc_crate from default features; fix cond import
The libc crate's pthread type layouts do not match Redox's relibc
layouts (e.g. pthread_cond_t 8 vs 12 bytes). Redox is its own libc,
so comparing against a foreign Linux libc crate is not meaningful.
Disable the feature by default and keep relibc types internally
consistent via assert_equal_size! checks against our own Rlct* types.
2026-07-08 20:03:25 +03:00
Red Bear OS 202e3eb500 relibc: implement pthread_cond_init with monotonic clock
Added clock: u8 field to Cond struct that stores the clock
setting (CLOCK_REALTIME or CLOCK_MONOTONIC) per pthread_condattr.
pthread_cond_init now properly sets the clock attribute.
timedwait uses the stored clock instead of hardcoding CLOCK_REALTIME.

Previously pthread_cond_init with CLOCK_MONOTONIC would log TODO
and use the wrong clock, causing condition variable timeouts to
behave incorrectly.

pthread_cond_t size updated from 8 to 12 bytes to accommodate
the new field. Cross-referenced with POSIX pthread_cond_init(3)
and Linux glibc nptl pthread_cond_init.c.
2026-07-08 19:58:46 +03:00
Red Bear OS 68d43106eb fix(pthread_mutex): store prioceiling in u8 to keep 12-byte layout matching libc crate 2026-07-08 19:54:05 +03:00
Red Bear OS 7f42a9fb20 relibc: implement pthread mutex prioceiling get/replace
Added prioceiling: c_int field to RlctMutex struct and real
implementations for prioceiling() and replace_prioceiling()
methods. Previously both returned Ok(0) silently (worse than
ENOSYS - caller believes operation succeeded).

pthread_mutex_t size updated from 12 to 16 bytes to accommodate
the new field while maintaining ABI alignment via #[repr(C)]
union with c_int alignment.

Cross-referenced with POSIX pthread_mutex_getprioceiling(3) and
Linux glibc nptl pthread_mutex_getprioceiling.c.

The prioceiling is stored in the mutex struct, set at construction
from pthread_mutexattr_getprioceiling. getprioceiling returns
the stored value; replace_prioceiling updates it and returns the
old value.
2026-07-08 19:46:46 +03:00
Red Bear OS 2347017649 fix(pthread_mutex): add missing prioceiling_value field for mutex priority ceiling 2026-07-08 19:44:19 +03:00
Red Bear OS 6c56264396 fix(socket): avoid casts in match patterns for IPPROTO_IP/IPPROTO_IPV6 2026-07-08 19:39:11 +03:00
Red Bear OS 93afedade5 relibc: extend getsockopt to handle IPPROTO_IP and IPPROTO_IPV6
Previously only SOL_SOCKET and IPPROTO_TCP levels were handled by
the socket scheme. Now IPPROTO_IP and IPPROTO_IPV6 levels also
forward to the socket scheme via SocketCall::GetSockOpt, removing
the ENOSYS fallthrough for these common socket option levels.

Cross-referenced with Linux 7.1 net/ipv4/ip_sockglue.c and
net/ipv6/ipv6_sockglue.c which implement IP/UDP/ICMP level
getsockopt handlers.
2026-07-08 19:24:43 +03:00
Red Bear OS 5638058b48 relibc: fix setgroups and unused ENOSYS import 2026-07-08 18:45:05 +03:00
Red Bear OS 5c8fe1c51a relibc: implement setgroups() via kernel proc Groups handle
Opens /scheme/sys/proc/{pid}/groups for writing, converts
the gid_t array to little-endian u32 bytes, and writes to the
procfs handle. The kernel's Groups writer (proc.rs:1600) reads
the bytes and updates the context's groups list.

Cross-referenced with Linux 7.1 kernel/groups.c setgroups().

Validation:
- size=0, list=NULL: clear all groups (per Linux man page)
- size>0, list=NULL: return EFAULT
- size>NGROUPS_MAX: return EINVAL
- File open/write errors: return EIO/EACCES
2026-07-08 18:40:06 +03:00