Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,8 @@ musl-gcc -static -O2 -o /tmp/t userland/tests/NAME.c && /tmp/t > userland/tests/
| `bootinfo/` | the boot protocol: what segboot hands the kernel |
| `kernel/src/hal/` | x86-64: instructions, serial, GDT/TSS, IDT and the trap path, `syscall` entry, ACPI tables (CPUs, I/O APIC, power off), local APIC and timer, I/O APIC, RTC, per-CPU data, starting CPUs |
| `kernel/src/mm/` | frame allocator with reference counts, page tables, kernel heap allocator, process address spaces with copy-on-write (`vm.rs`) |
| `kernel/src/proc/` | processes: `fork`/`clone`, `execve` (with `#!`), exit and `wait4`, process groups and sessions, signals, the ELF loader, user faults |
| `kernel/src/proc/` | processes: `fork`/`clone`, `execve` (with `#!`), exit and `wait4`, process groups and sessions, signals, the ELF loader, user faults, namespaces (user, PID, mount, network, UTS, IPC) |
| `kernel/src/ipc.rs` | System V shared memory segments, one table per IPC namespace |
| `kernel/src/syscall/` | the Linux system calls, by Linux number |
| `kernel/src/fs/` | the VFS with symbolic links, tmpfs, `/dev` devices, pipes, `/proc`, the initramfs (cpio) unpacker |
| `kernel/src/tty.rs` | the console terminal: line discipline, termios, job control |
Expand Down
35 changes: 27 additions & 8 deletions docs/plan.md
Original file line number Diff line number Diff line change
Expand Up @@ -422,9 +422,10 @@ The demo: Firefox and Chromium run on the Wayland desktop.
and real AMD/Intel GPUs. Local QEMU has no virtio-gpu device, so only the
command layouts are covered by kernel tests; the device path is not
exercised here.
2. **What browsers need**: seccomp-BPF, `inotify`, user, PID, network and
mount namespaces, `/dev/shm`, fontconfig, a D-Bus session bus, `chroot`
and CLONE_FS (all done, below).
2. **What browsers need**: seccomp-BPF, `inotify`, user, PID, network,
mount, UTS and IPC namespaces, `/dev/shm` and System V shared memory,
fontconfig, a D-Bus session bus, `chroot` and CLONE_FS (all done,
below).

*Done so far.* Firefox renders a page headless (a screenshot), and
the shell session has a step that runs it on weston (it runs where
Expand All @@ -447,9 +448,27 @@ The demo: Firefox and Chromium run on the Wayland desktop.
namespaces (CLONE_NEWNS with CLONE_FS or CLONE_THREAD is EINVAL, as on
Linux); `/proc/<pid>/ns/mnt`, the caller's `/proc/mounts` and
`/proc/<pid>/mounts` show the right table. `tests/namespaces` mounts a
tmpfs in one namespace and checks it is invisible in the parent's. Not
yet: UTS, IPC and cgroup namespaces (EINVAL), `setns`, `/proc` mounted
per PID namespace, capabilities (everything is root). **`/dev/shm`**, **fontconfig**
tmpfs in one namespace and checks it is invisible in the parent's.
**UTS and IPC namespaces** (CLONE_NEWUTS, CLONE_NEWIPC): a new UTS
namespace starts with its parent's names; `sethostname` and
`setdomainname` change only it (`uname`, `/proc/sys/kernel/hostname`
and `domainname`) and need privilege over the user namespace that owns
it, as on Linux. A new IPC namespace has its own, empty set of
System V objects; `/proc/<pid>/ns/{uts,ipc}` name them. **System V
shared memory** (`ipc.rs`, `syscall/ipc.rs`): `shmget` (keys,
IPC_PRIVATE, IPC_CREAT/IPC_EXCL), `shmat` (anywhere or at an address,
SHM_RND, SHM_REMAP, SHM_RDONLY), `shmdt` and `shmctl` (IPC_STAT,
IPC_SET, IPC_RMID, IPC_INFO, SHM_INFO, SHM_STAT, SHM_LOCK). A
segment is an unnamed tmpfs file mapped shared, so `fork` shares it;
the attach count is counted from the live address spaces, so `fork`,
`munmap` and exit change it as on Linux; IPC_RMID frees the key at
once and the memory after the last detach. Ids are allocated as Linux
6.x does (cyclic indexes, a sequence number that moves on when an
index is reused); `/proc/sysvipc/shm` and `/proc/sys/kernel/shm*`
are there. `userland/tests/sysvipc.c` prints the same on Linux, as
root or not, and on SegOS. Not yet: cgroup namespaces (EINVAL),
`setns`, `/proc` mounted per PID namespace, SysV semaphores and
message queues, capabilities (everything is root). **`/dev/shm`**, **fontconfig**
and **D-Bus**: the shell session creates POSIX shared memory from
Python, `fc-match` finds DejaVu Sans, and `dbus-run-session` starts
Ubuntu's `dbus-daemon`, which answers `dbus-send`. **Network
Expand All @@ -475,8 +494,8 @@ The demo: Firefox and Chromium run on the Wayland desktop.
poll). Ubuntu's `aplay` plays through libasound unchanged; QEMU
records the card's output and `cargo xtask test` measures the 440 Hz
tone in it. Not yet: capture, mmap transfers, mixer controls,
interrupts (the position is followed by a kernel thread), dmix (no
SysV shared memory).
interrupts (the position is followed by a kernel thread), dmix (SysV
shared memory is there now; its semaphores are not).
4. **USB** (`drivers/xhci.rs`, done): the xHCI controller is taken from
the firmware, reset, and given a device context array, scratchpads, a
command ring and an event ring; connected root ports are reset, their
Expand Down
123 changes: 123 additions & 0 deletions kernel/src/fs/procfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,14 @@ fn ns_dir(pid: u32, parent: &Arc<dyn Inode>) -> Arc<dyn Inode> {
"user:[{}]",
p.user_ns.lock().ino
))),
entry("uts", 0x2a, |p| Ok(format!(
"uts:[{}]",
p.uts_ns.lock().ino
))),
entry("ipc", 0x2b, |p| Ok(format!(
"ipc:[{}]",
p.ipc_ns.lock().ino
))),
]
})
}
Expand Down Expand Up @@ -352,6 +360,8 @@ pub fn root(root: &Arc<dyn Inode>) -> Arc<dyn Inode> {
]
})
),
("sys".into(), sys_dir(me)),
("sysvipc".into(), sysvipc_dir(me)),
];
for p in proc::all() {
let pid = p.pid;
Expand All @@ -361,6 +371,119 @@ pub fn root(root: &Arc<dyn Inode>) -> Arc<dyn Inode> {
})
}

/// `/proc/sys`: the `kernel` settings programs read (host and domain
/// names, release, SysV shared memory limits).
fn sys_dir(proc_dir: &Arc<dyn Inode>) -> Arc<dyn Inode> {
GenDir::create(INO_BASE | 17, Arc::downgrade(proc_dir), |sys| {
alloc::vec![(
"kernel".into(),
GenDir::create(INO_BASE | 18, Arc::downgrade(sys), |k| {
use crate::ipc::{SHMALL, SHMMAX, SHMMNI};
alloc::vec![
("hostname".into(), uts_file(INO_BASE | 19, k, false)),
("domainname".into(), uts_file(INO_BASE | 20, k, true)),
(
"osrelease".into(),
file(INO_BASE | 21, k, || format!(
"6.6.0-segos-{}\n",
crate::VERSION
))
),
("ostype".into(), file(INO_BASE | 22, k, || "Linux\n".into())),
(
"shmmax".into(),
file(INO_BASE | 23, k, || format!("{SHMMAX}\n"))
),
(
"shmall".into(),
file(INO_BASE | 24, k, || format!("{SHMALL}\n"))
),
(
"shmmni".into(),
file(INO_BASE | 25, k, || format!("{SHMMNI}\n"))
),
]
})
)]
})
}

/// `/proc/sys/kernel/hostname` or `domainname`: the caller's UTS
/// namespace's name; a write sets it (up to a newline, at most 64 bytes),
/// with `sethostname`'s permission rule.
fn uts_file(ino: u64, parent: &Arc<dyn Inode>, domain: bool) -> Arc<dyn Inode> {
Arc::new(RwFile {
ino,
generate: Box::new(move || {
let n = crate::proc::ns::current_uts().names();
let mut v = if domain { n.domainname } else { n.hostname };
if let Some(i) = v.iter().position(|&b| b == 0) {
v.truncate(i);
}
v.push(b'\n');
v
}),
write: Box::new(move |off, data| {
if off != 0 {
return Ok(data.len());
}
let line = data.split(|&b| b == b'\n').next().unwrap_or(&[]);
let name = &line[..line.len().min(crate::proc::ns::UTS_LEN)];
crate::proc::ns::current_uts().set(domain, name, &caller_user_ns()?)?;
Ok(data.len())
}),
parent: Arc::downgrade(parent),
})
}

/// `/proc/sysvipc/shm`: the caller's IPC namespace's shared memory
/// segments, in Linux's columns (util-linux `ipcs` reads it).
fn sysvipc_dir(proc_dir: &Arc<dyn Inode>) -> Arc<dyn Inode> {
GenDir::create(INO_BASE | 26, Arc::downgrade(proc_dir), |d| {
alloc::vec![("shm".into(), file(INO_BASE | 27, d, sysvipc_shm))]
})
}

fn sysvipc_shm() -> String {
let mut out = String::from(
" key shmid perms size cpid lpid nattch uid gid cuid cgid atime dtime ctime rss swap\n",
);
let Some(p) = crate::task::current().user().map(|u| u.process.clone()) else {
return out;
};
let ns = p.ipc_ns.lock().clone();
let caller = crate::ipc::Caller {
user_ns: p.user_ns.lock().clone(),
pid: p.pid,
};
let segments = ns.shm.lock().segments();
for seg in segments {
let st = crate::ipc::stat(&seg, &caller, &p.pid_ns);
let rss = seg.size.next_multiple_of(4096);
let _ = writeln!(
out,
"{:10} {:10} {:4o} {:21} {:5} {:5} {:5} {:5} {:5} {:5} {:5} {:10} {:10} {:10} {:21} {:21}",
st.perm.key,
seg.id,
st.perm.mode,
st.segsz,
st.cpid,
st.lpid,
st.nattch,
st.perm.uid,
st.perm.gid,
st.perm.cuid,
st.perm.cgid,
st.atime,
st.dtime,
st.ctime,
rss,
0
);
}
out
}

fn meminfo() -> String {
let total = crate::mm::frame::total_count() * 4;
let free = crate::mm::frame::free_count() * 4;
Expand Down
Loading
Loading