Skip to content
Closed
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
2 changes: 1 addition & 1 deletion CosmOS-rootfs
Submodule CosmOS-rootfs updated 503 files
44 changes: 35 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ USER_BUILD_STAMP := $(STAMP_DIR)/user-build.stamp
KERNEL_BUILD_STAMP := $(STAMP_DIR)/kernel-build.stamp
USER_BUILD_DEPS := user/Makefile user/Cargo.toml $(shell find user/src -type f | sort)
KERNEL_BUILD_DEPS := os/Makefile os/Cargo.toml os/build.rs $(shell find os/src fs/src -type f | sort)
ROOTFS_FILES := $(shell find rootfs -type f | sort)
ROOTFS_DIR := CosmOS-rootfs/rootfs
ROOTFS_FILES := $(shell find $(ROOTFS_DIR) -type f | sort)
OPTIONAL_RUNTIME_FILES := $(wildcard lib/musl/ar lib/glibc/ar)

.PHONY: all submodules docker build_docker fmt user-apps clean run run-trace run-comp-rv debug gdbserver gdbclient
.PHONY: all submodules docker build_docker fmt user-apps rootfs clean run run-trace run-comp-rv debug gdbserver gdbclient check-kernel check-user-apps check-rootfs

all:
$(MAKE) submodules
$(MAKE) kernel-rv kernel-la disk.img
$(MAKE) user-apps kernel-rv kernel-la rootfs

# 拉取所有子模块,确保后续构建依赖完整。
submodules:
Expand Down Expand Up @@ -55,21 +56,46 @@ kernel-la: kernel-rv
@echo "warning: LoongArch kernel is not implemented in this repository yet; using kernel-rv as a temporary placeholder." >&2
cp kernel-rv $@

disk.img: $(USER_BUILD_STAMP) $(ROOTFS_FILES) $(OPTIONAL_RUNTIME_FILES) scripts/pack-disk-img.sh
./scripts/pack-disk-img.sh rootfs $(USER_BIN_DIR) $@

run: kernel-rv disk.img
rootfs:
$(MAKE) -C CosmOS-rootfs rootfs-init

check-kernel:
@test -x kernel-rv || { \
echo "missing kernel-rv; run 'make all' first" >&2; \
exit 1; \
}

check-user-apps:
@test -d "$(USER_BIN_DIR)" || { \
echo "missing user binaries in $(USER_BIN_DIR); run 'make all' first" >&2; \
exit 1; \
}

check-rootfs:
@test -d "$(ROOTFS_DIR)" || { \
echo "missing rootfs directory $(ROOTFS_DIR); run 'make all' first" >&2; \
exit 1; \
}
@test -d "$(ROOTFS_DIR)/root" || { \
echo "rootfs is incomplete under $(ROOTFS_DIR); run 'make all' first" >&2; \
exit 1; \
}

disk.img: check-user-apps check-rootfs $(OPTIONAL_RUNTIME_FILES) $(ROOTFS_FILES) scripts/pack-disk-img.sh
./scripts/pack-disk-img.sh $(ROOTFS_DIR) $(USER_BIN_DIR) $@

run: check-kernel disk.img
$(QEMU) -machine virt -kernel kernel-rv -m $(MEM) -nographic -smp $(SMP) -bios default $(QEMU_COMP_BLK_ARGS) -device virtio-net-device,netdev=net -netdev $(QEMU_NETDEV) -no-reboot -rtc base=utc $(QEMU_COMP_EXTRA_BLK_ARGS) $(QEMU_TRACE_ARGS)

run-trace: QEMU_TRACE_ARGS = -d int,in_asm -D qemu.log
run-trace: run

run-comp-rv: run

debug: kernel-rv disk.img
debug: check-kernel disk.img
$(MAKE) -C os debug

gdbserver: kernel-rv disk.img
gdbserver: check-kernel disk.img
$(MAKE) -C os gdbserver

gdbclient:
Expand Down
Binary file removed lib/glibc/ar
Binary file not shown.
Binary file removed lib/musl/ar
Binary file not shown.
2 changes: 2 additions & 0 deletions os/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ pub const PAGE_SIZE: usize = 0x1000;
pub const PAGE_SIZE_BITS: usize = 0xc;
/// default base address for anonymous mmap allocations
pub const USER_MMAP_BASE: usize = 0x1000_0000;
/// fixed load bias used for PIE main executables without an interpreter
pub const USER_PIE_BASE: usize = 0x0020_0000;
/// default base address for the main thread's user stack region
pub const USER_STACK_BASE: usize = 0x0800_0000;
/// base address for loading dynamic linker (interpreter)
Expand Down
22 changes: 17 additions & 5 deletions os/src/fs/pipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ use crate::poll::{notify_poll_source, POLLHUP, POLLIN, POLLOUT};
use crate::sync::SpinNoIrqLock;
use crate::syscall::errno::ERRNO;
use alloc::sync::{Arc, Weak};
use crate::fs::{Stat,StatMode};
use crate::fs::{empty_statfs, Stat, StatFs64, StatMode};
use crate::task::{WaitQueue, WaitReason};
use core::any::Any;
use fs::{STATFS_MAGIC_PIPEFS, STATFS_NAMELEN_DEFAULT};

const PIPE_DEV_ID: u64 = STATFS_MAGIC_PIPEFS;

/// IPC pipe
pub struct Pipe {
Expand Down Expand Up @@ -427,16 +430,16 @@ impl File for Pipe {

fn stat(&self) -> Stat {
Stat {
dev: 0,
ino: 0,
mode: StatMode::FIFO,
dev: PIPE_DEV_ID,
ino: self.source_id() as u64,
mode: StatMode::FIFO | StatMode::from_bits_truncate(0o600),
nlink: 1,
uid: 0,
gid: 0,
rdev: 0,
pad0: 0,
size: 0,
blksize: 0,
blksize: crate::config::PAGE_SIZE as u32,
pad1: 0,
blocks: 0,
atime_sec: 0,
Expand All @@ -448,6 +451,15 @@ impl File for Pipe {
unused: [0; 2],
}
}

fn statfs(&self) -> Result<StatFs64, ERRNO> {
Ok(empty_statfs(
STATFS_MAGIC_PIPEFS,
crate::config::PAGE_SIZE as u64,
self.source_id() as u64,
STATFS_NAMELEN_DEFAULT,
))
}
}

impl Drop for Pipe {
Expand Down
99 changes: 99 additions & 0 deletions os/src/fs/procfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ fn parse_proc_usize(buf: &[u8]) -> Result<usize, FS_ERRNO> {
text.parse::<usize>().map_err(|_| FS_ERRNO::EINVAL)
}

fn parse_proc_bool(buf: &[u8]) -> Result<bool, FS_ERRNO> {
match parse_proc_u32(buf)? {
0 => Ok(false),
1 => Ok(true),
_ => Err(FS_ERRNO::EINVAL),
}
}

fn read_string_at(data: String, offset: usize, buf: &mut [u8]) -> usize {
if buf.is_empty() {
return 0;
Expand Down Expand Up @@ -683,6 +691,7 @@ impl VfsNode for ProcStaticDirNode {
ProcStaticDirKind::Sys => alloc::vec![(String::from("kernel"), VfsFileType::Directory)],
ProcStaticDirKind::Kernel => alloc::vec![
(String::from("keys"), VfsFileType::Directory),
(String::from("sched_autogroup_enabled"), VfsFileType::Regular),
(String::from("tainted"), VfsFileType::Regular),
],
ProcStaticDirKind::Keys => alloc::vec![
Expand All @@ -701,6 +710,10 @@ impl VfsNode for ProcStaticDirNode {
(ProcStaticDirKind::Kernel, "keys") => {
Some(Arc::new(ProcStaticDirNode::new(ProcStaticDirKind::Keys)) as Arc<dyn VfsNode>)
}
(ProcStaticDirKind::Kernel, "sched_autogroup_enabled") => Some(
Arc::new(ProcKernelSysctlNode::new(ProcKernelSysctlKind::SchedAutogroupEnabled))
as Arc<dyn VfsNode>,
),
(ProcStaticDirKind::Kernel, "tainted") => {
Some(Arc::new(ProcKernelTaintedNode::new()) as Arc<dyn VfsNode>)
}
Expand Down Expand Up @@ -794,6 +807,92 @@ impl VfsNode for ProcKernelTaintedNode {
}
}

#[derive(Debug, Clone, Copy)]
enum ProcKernelSysctlKind {
SchedAutogroupEnabled,
}

#[derive(Debug)]
struct ProcKernelSysctlNode {
kind: ProcKernelSysctlKind,
}

impl ProcKernelSysctlNode {
fn new(kind: ProcKernelSysctlKind) -> Self {
Self { kind }
}

fn render(&self) -> String {
match self.kind {
ProcKernelSysctlKind::SchedAutogroupEnabled => {
alloc::format!("{}\n", crate::sched::autogroup_enabled() as u8)
}
}
}
}

impl VfsNode for ProcKernelSysctlNode {
fn as_any(&self) -> &dyn Any {
self
}

fn file_type(&self) -> VfsFileType {
VfsFileType::Regular
}

fn size(&self) -> usize {
self.render().len()
}

fn ls(&self) -> Vec<(String, VfsFileType)> {
Vec::new()
}

fn find(&self, _name: &str) -> Option<Arc<dyn VfsNode>> {
None
}

fn create(&self, _name: &str) -> Option<Arc<dyn VfsNode>> {
None
}

fn mkdir(&self, _name: &str) -> Option<Arc<dyn VfsNode>> {
None
}

fn clear(&self) {}

fn truncate(&self, _new_size: usize) -> Result<(), FS_ERRNO> {
Ok(())
}

fn read_at(&self, offset: usize, buf: &mut [u8]) -> usize {
read_string_at(self.render(), offset, buf)
}

fn write_at(&self, _offset: usize, buf: &[u8]) -> usize {
let result = match self.kind {
ProcKernelSysctlKind::SchedAutogroupEnabled => {
parse_proc_bool(buf).map(crate::sched::set_autogroup_enabled)
}
};
if result.is_ok() {
buf.len()
} else {
0
}
}

fn statfs(&self) -> Result<fs::VfsStatFs, fs::errno::FS_ERRNO> {
Ok(crate::fs::empty_statfs(
fs::STATFS_MAGIC_PROC,
crate::config::PAGE_SIZE as u64,
0x9fa0,
255,
))
}
}

#[derive(Default, Debug)]
struct ProcKeyUsersNode;

Expand Down
19 changes: 13 additions & 6 deletions os/src/fs/tty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ const TIOCSCTTY: usize = 0x540E;
const TIOCGPGRP: usize = 0x540F;
/// `ioctl(TIOCSPGRP)`:设置前台进程组。
const TIOCSPGRP: usize = 0x5410;
/// Device ID of the devfs-like filesystem that contains the tty node.
const TTY_FS_DEV_ID: u64 = 0;
const TTY_INO: u64 = 1;
/// Linux-compatible `/dev/tty` character device number: major 5, minor 0.
const TTY_RDEV_MAJOR: u64 = 5;
const TTY_RDEV_MINOR: u64 = 0;
const TTY_RDEV: u64 = (TTY_RDEV_MAJOR << 8) | TTY_RDEV_MINOR;
/// `ioctl(TIOCGWINSZ)`:读取窗口大小。
const TIOCGWINSZ: usize = 0x5413;
/// `ioctl(TIOCSWINSZ)`:设置窗口大小。
Expand Down Expand Up @@ -698,16 +705,16 @@ impl TtyFile {
/// 构造字符设备类型的 `stat` 结果。
fn stat_impl() -> Stat {
Stat {
dev: 0,
ino: 0,
mode: StatMode::CHAR,
dev: TTY_FS_DEV_ID,
ino: TTY_INO,
mode: StatMode::CHAR | StatMode::from_bits_truncate(0o666),
nlink: 1,
uid: 0,
gid: 0,
rdev: 0,
rdev: TTY_RDEV,
pad0: 0,
size: 0,
blksize: 0,
blksize: crate::config::PAGE_SIZE as u32,
pad1: 0,
blocks: 0,
atime_sec: 0,
Expand Down Expand Up @@ -999,7 +1006,7 @@ impl VfsNode for TtyDeviceNode {
}

fn mode(&self) -> Option<u32> {
Some(StatMode::CHAR.bits())
Some((StatMode::CHAR | StatMode::from_bits_truncate(0o666)).bits())
}

fn uid(&self) -> Option<u32> {
Expand Down
Loading
Loading