oscall 0.0.0

Low-level cross-platform interface to operating system services: files, memory mapping, networking and more, with no libc on Linux
Documentation
# oscall: design and roadmap

oscall is the layer between a program and the operating system: one API for
files, memory, networking, time, threads and processes that behaves the same
on Linux, macOS and Windows, and that works in a
[fullrust](https://github.com/KarpelesLab/fullrust) binary, where there is no
libc at all.

It sits below `std`: no buffering, no hidden allocation, no global state. Every
call maps onto one OS operation, or onto the few that are needed to give the
same result everywhere.

## Design rules

1. **No dependencies.** Not `libc`, not `windows-sys`, not `rustix`. Every
   system call number, structure layout and import is declared in this crate.
   That is what lets one source tree build for fullrust, where `libc` cannot
   link, and it keeps the crate usable at the very bottom of a dependency graph.
2. **`no_std` first.** The core needs neither `std` nor an allocator. `alloc`
   adds the calls that must allocate (long paths, directory listings); `std`
   (default) adds conversions only: `std::io::Error`, `OwnedFd`/`OwnedHandle`,
   `Path`.
3. **Two layers.**
   - `oscall::sys`: the raw platform layer, public and platform-specific
     (`sys::linux::syscall3`, `sys::windows::CreateFileW`, ...). `unsafe`, no
     portability promise: it is the escape hatch for whatever the portable
     layer does not cover.
   - Everything else: portable and safe wherever the operation allows it,
     built only on `sys`.
4. **Portable by default, platform-specific by name.** A portable function
   behaves the same on every OS. What only one OS offers lives in an extension
   module named after it (`oscall::mem::linux::remap`), never as a portable
   function that fails elsewhere.
5. **Errors are the OS's own.** `Error` holds the raw code (`errno`, or
   `GetLastError`/`WSAGetLastError`/`NTSTATUS`) and maps it to a portable
   `ErrorKind` on request. Nothing is lost in translation and no thread-local
   `errno` is involved on Linux.

## Backends

### Linux and fullrust: raw system calls

One backend serves both, selected with
`any(target_os = "linux", target_os = "fullrust")`. fullrust is not
`cfg(unix)`, so nothing in the crate may be gated on `cfg(unix)`.

- System calls are made with inline assembly (`syscall`, `int 0x80`, `svc 0`,
  `ecall`): one small module per architecture, `syscall0`..`syscall6`. Stable
  inline assembly covers all six architectures on Rust 1.95.
- The system call numbers and kernel structures are per architecture. The
  tables are generated from the kernel headers by a script kept in the
  repository, and committed.
- 32-bit targets use the 64-bit time and offset calls (`clock_gettime64`,
  `statx`, `_llseek`/`pread64`, ...) so that the API is identical to 64-bit.
- Known hazards to design around:
  - `arm` in Thumb mode and `x86` with PIC reserve `r7`/`ebx`, which the
    system call convention needs: the assembly must save and restore them.
  - `clock_gettime` is only fast through the vDSO. The first version makes the
    system call; finding the vDSO (from the auxiliary vector) comes later.
  - When a libc is present in the process (plain Linux targets), oscall
    bypasses it. That is fine for the calls listed here, but not for anything
    libc keeps state about: `fork`, thread creation, signal handlers,
    `brk`. Those calls need a deliberate design each, and are late in the
    roadmap for that reason.

### macOS: libSystem

The kernel's system call interface is private and changes between releases;
`libSystem` is the supported boundary, and every Rust binary already links it.
oscall declares the functions it uses in its own `extern "C"` blocks, with the
structure layouts for x86_64 and aarch64. The backend is gated on
`target_os = "macos"`; iOS and the other Apple systems can share it later.

### Windows: kernel32, ntdll, ws2_32

Imports are declared with `#[link(kind = "raw-dylib")]`, so no import library
and no `windows-sys` is needed, on MSVC and GNU toolchains alike. Paths are
UTF-8 in the API and converted to UTF-16 at the call (on the stack when short,
on the heap with `alloc`). Sockets are a separate handle type from files, as
they are in the OS.

## Targets

| | x86_64 | x86 | aarch64 | arm | riscv64 | riscv32 |
|---|---|---|---|---|---|---|
| Linux | tested | tested | tested | tested (qemu) | tested (qemu) | builds¹ |
| fullrust | tested | tested | tested (qemu) | tested (qemu) | —² | — |
| macOS | tested | — | tested | — | — | — |
| Windows | tested | tested | tested | best effort³ | — | — |

1. Rust has no `std` build for riscv32 Linux (tier 3). The architecture module
   is written and type-checked, but CI cannot run it.
2. fullrust has no RISC-V target yet. Since the Linux backend is the same,
   supporting it will only need the CI entry.
3. 32-bit ARM Windows is tier 3 in Rust and no longer shipped by Microsoft.
   Nothing in the crate excludes it; nothing tests it.

"—" means the OS does not exist on that architecture.

## API outline

Names are indicative; each milestone settles its own.

| Module | Contents |
|---|---|
| `oscall` | `Error`, `ErrorKind`, `Result`, `Handle` (owned) and `BorrowedHandle` |
| `oscall::sys` | the raw layer of the current platform |
| `oscall::mem` | page size, map/unmap/protect/advise, anonymous and file mappings, locking |
| `oscall::fs` | open/create, read/write at offset, seek, metadata, sync, truncate, directories, links, rename, remove |
| `oscall::io` | read/write on any handle, vectored I/O, pipes, duplicate, standard handles |
| `oscall::time` | monotonic and wall clocks, sleep |
| `oscall::rand` | the OS random generator |
| `oscall::net` | sockets (TCP, UDP, Unix), addresses, options, shutdown |
| `oscall::event` | readiness/completion polling (epoll, kqueue, IOCP), non-blocking I/O |
| `oscall::sync` | wait/wake on an address (futex, `os_sync_wait_on_address`, `WaitOnAddress`) |
| `oscall::thread` | create/join, thread id, yield, CPU affinity |
| `oscall::process` | ids, exit, environment, spawn/wait, signals/console events |

## Roadmap

Each milestone lands on all three backends and every tested target at once: a
portable API that exists on one OS only is not done.

- **M0. Scaffold.** *(this commit)* Crate, backend selection, CI on every
  target, release-plz.
- **M1. Core.** `Error`/`ErrorKind`, handles, the Linux system call layer for
  six architectures with generated number tables, the libSystem and Windows
  import plumbing. First real calls end to end: process id, page size, write
  to a standard handle.
- **M2. Memory.** `mem`: anonymous and file mappings, protect, advise, lock.
- **M3. Files.** `fs` and `io`: files, metadata, directories, pipes.
- **M4. Time and randomness.** `time`, `rand`.
- **M5. Network.** `net`: blocking sockets, addresses, options.
- **M6. Events.** `event`: polling and non-blocking I/O. The hardest portable
  design in the crate (readiness on Unix against completion on Windows), so it
  comes after the calls it has to drive.
- **M7. Threads.** `sync`, then `thread`. Thread creation without libc's
  knowledge is the first of the "libc keeps state" hazards.
- **M8. Processes.** `process`: spawn, wait, signals.

After M3 the crate is useful on its own (files and memory maps); 0.1.0 is cut
once M1 to M5 are in and the API has been used from a fullrust program.

## Testing

- Every portable function has tests that run on all tested targets: natively
  on the Linux, macOS and Windows runners (x86_64 and aarch64 each), as static
  musl binaries under qemu-user for arm and riscv64, and with the fullrust
  action for its four targets.
- Where `std` offers the same operation, tests compare oscall's result with
  `std`'s (same file contents, same metadata, same error kind).
- The MSRV (1.95) is checked on every push.