18 Commits

Author SHA1 Message Date
alnyan 4b844a8774 refactor: is_ready -> ready 2022-02-12 16:32:44 +02:00
alnyan a7a1639ff7 refactor: fix all warnings 2022-02-09 18:51:41 +02:00
alnyan 0f9f8fc1bf feat: mkdir(1) command 2022-02-09 16:57:33 +02:00
alnyan 97c591f58f fix: bcm2837 serial 2022-01-30 17:15:28 +02:00
alnyan d45a8adc34 refactor: move asm! usage to intrin module 2022-01-26 15:15:38 +02:00
alnyan 01da9e7ee5 refactor: new VFS design 2022-01-24 20:46:45 +02:00
alnyan a82751c146 refactor: update to rust 1.60.0 2022-01-23 20:58:38 +02:00
alnyan fcbe412732 feature: simple sysfs 2021-12-09 09:02:28 +02:00
alnyan b4b99915ef feature: verbose feature for libusr 2021-12-05 23:45:24 +02:00
alnyan afa7e4cecb feature: simple pseudo devices 2021-12-05 21:58:03 +02:00
alnyan 94450e6537 fix: use u128 for nanos in timer 2021-12-05 21:13:13 +02:00
alnyan 2985f1429e feature: update Orange Pi 3 code 2021-12-05 21:12:56 +02:00
alnyan 2b160343b7 fix: invalidate TLB on syscall CoW 2021-12-05 21:12:29 +02:00
alnyan b97db3a0c4 feature: better login program 2021-12-02 13:02:45 +02:00
alnyan bd3d4e964d fix: enter signal handler AFTER syscall abort 2021-12-01 12:37:49 +02:00
alnyan a577b2dcc4 refactor: fix clippy warnings 2021-11-30 20:34:17 +02:00
alnyan d0681eb589 refactor: replace u32 with Tid type 2021-11-30 18:35:10 +02:00
alnyan e965c25181 refactor: better Pid type 2021-11-30 18:07:39 +02:00
77 changed files with 2231 additions and 1244 deletions
Generated
+3 -2
View File
@@ -22,9 +22,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "cortex-a"
version = "6.1.0"
version = "7.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "509fc35485a2b4ddbacabe0bf2212cdfff88da93658608e5cc651afcb75b7733"
checksum = "5bd95fd055d118f77d4e4d527201b6ceccd13586b19b4dac1270f7081fef0f98"
dependencies = [
"tock-registers",
]
@@ -93,6 +93,7 @@ dependencies = [
"cfg-if",
"cortex-a",
"fdt-rs",
"fs-macros",
"kernel-macros",
"libsys",
"memfs",
+10 -5
View File
@@ -17,12 +17,12 @@ CARGO_COMMON_OPTS=
ifeq ($(PROFILE),release)
CARGO_COMMON_OPTS+=--release
endif
ifeq ($(VERBOSE),1)
CARGO_COMMON_OPTS+=--features verbose
endif
CARGO_BUILD_OPTS=$(CARGO_COMMON_OPTS) \
--target=../etc/$(ARCH)-$(MACH).json
ifeq ($(VERBOSE),1)
CARGO_BUILD_OPTS+=--features verbose
endif
ifneq ($(MACH),)
CARGO_BUILD_OPTS+=--features mach_$(MACH)
endif
@@ -45,6 +45,7 @@ endif
ifeq ($(MACH),rpi3)
QEMU_OPTS+=-kernel $(O)/kernel.bin \
-initrd $(O)/initrd.img \
-dtb etc/bcm2837-rpi-3-b-plus.dtb \
-M raspi3b \
-serial mon:stdio \
-display none \
@@ -92,14 +93,18 @@ initrd:
--target=../etc/$(ARCH)-osdev5.json \
-Z build-std=core,alloc,compiler_builtins \
$(CARGO_COMMON_OPTS)
mkdir -p $(O)/rootfs/bin $(O)/rootfs/sbin $(O)/rootfs/dev
touch $(O)/rootfs/dev/.do_no_remove
mkdir -p $(O)/rootfs/bin $(O)/rootfs/sbin $(O)/rootfs/dev $(O)/rootfs/etc $(O)/rootfs/sys
cp etc/initrd/passwd $(O)/rootfs/etc
cp etc/initrd/shadow $(O)/rootfs/etc
touch $(O)/rootfs/dev/.do_not_remove
touch $(O)/rootfs/sys/.do_not_remove
cp target/$(ARCH)-osdev5/$(PROFILE)/init $(O)/rootfs/init
cp target/$(ARCH)-osdev5/$(PROFILE)/shell $(O)/rootfs/bin
cp target/$(ARCH)-osdev5/$(PROFILE)/fuzzy $(O)/rootfs/bin
cp target/$(ARCH)-osdev5/$(PROFILE)/ls $(O)/rootfs/bin
cp target/$(ARCH)-osdev5/$(PROFILE)/cat $(O)/rootfs/bin
cp target/$(ARCH)-osdev5/$(PROFILE)/hexd $(O)/rootfs/bin
cp target/$(ARCH)-osdev5/$(PROFILE)/mkdir $(O)/rootfs/bin
cp target/$(ARCH)-osdev5/$(PROFILE)/login $(O)/rootfs/sbin
cd $(O)/rootfs && tar cf ../initrd.img `find -type f -printf "%P\n"`
ifeq ($(MACH),orangepi3)
Binary file not shown.
+2
View File
@@ -0,0 +1,2 @@
root:0:0:root:/:/bin/shell
alnyan:1000:1000:alnyan:/:/bin/shell
+2
View File
@@ -0,0 +1,2 @@
root:toor
alnyan:
+161 -161
View File
@@ -1,162 +1,162 @@
extern crate proc_macro;
extern crate syn;
#[macro_use]
extern crate quote;
// extern crate proc_macro;
// extern crate syn;
// #[macro_use]
// extern crate quote;
//
// use proc_macro::TokenStream;
// use quote::ToTokens;
// use std::collections::HashSet;
// use syn::{parse_macro_input, ImplItem, ItemImpl, Ident};
use proc_macro::TokenStream;
use quote::ToTokens;
use std::collections::HashSet;
use syn::{parse_macro_input, ImplItem, ItemImpl, Ident};
fn impl_inode_fn<T: ToTokens>(name: &str, behavior: T) -> ImplItem {
// TODO somehow know if current crate is vfs or not?
ImplItem::Verbatim(match name {
"create" => quote! {
fn create(&mut self, _at: VnodeRef, _name: &str, kind: VnodeKind) ->
Result<VnodeRef, libsys::error::Errno>
{
#behavior
}
},
"remove" => quote! {
fn remove(&mut self, _at: VnodeRef, _name: &str) -> Result<(), libsys::error::Errno> {
#behavior
}
},
"lookup" => quote! {
fn lookup(&mut self, _at: VnodeRef, _name: &str) ->
Result<VnodeRef, libsys::error::Errno>
{
#behavior
}
},
"stat" => quote! {
fn stat(&mut self, _at: VnodeRef) ->
Result<libsys::stat::Stat, libsys::error::Errno>
{
#behavior
}
},
"truncate" => quote! {
fn truncate(&mut self, _node: VnodeRef, _size: usize) ->
Result<(), libsys::error::Errno>
{
#behavior
}
},
"size" => quote! {
fn size(&mut self, _node: VnodeRef) -> Result<usize, libsys::error::Errno> {
#behavior
}
},
"read" => quote! {
fn read(&mut self, _node: VnodeRef, _pos: usize, _data: &mut [u8]) ->
Result<usize, libsys::error::Errno>
{
#behavior
}
},
"write" => quote! {
fn write(&mut self, _node: VnodeRef, _pos: usize, _data: &[u8]) ->
Result<usize, libsys::error::Errno>
{
#behavior
}
},
"open" => quote! {
fn open(&mut self, _node: VnodeRef, _flags: libsys::stat::OpenFlags) ->
Result<usize, libsys::error::Errno>
{
#behavior
}
},
"close" => quote! {
fn close(&mut self, _node: VnodeRef) -> Result<(), libsys::error::Errno> {
#behavior
}
},
"ioctl" => quote! {
fn ioctl(
&mut self,
_node: VnodeRef,
_cmd: libsys::ioctl::IoctlCmd,
_ptr: usize,
_len: usize) ->
Result<usize, libsys::error::Errno>
{
#behavior
}
},
"is_ready" => quote! {
fn is_ready(&mut self, _node: VnodeRef, _write: bool) ->
Result<bool, libsys::error::Errno>
{
#behavior
}
},
"readdir" => quote! {
fn readdir(
&mut self,
_node: VnodeRef,
_pos: usize,
_entries: &mut [libsys::stat::DirectoryEntry]
) ->
Result<usize, libsys::error::Errno>
{
#behavior
}
},
_ => panic!("TODO implement {:?}", name),
})
}
#[proc_macro_attribute]
pub fn auto_inode(attr: TokenStream, input: TokenStream) -> TokenStream {
let mut impl_item = parse_macro_input!(input as ItemImpl);
let mut missing = HashSet::<String>::new();
let behavior = if attr.is_empty() {
"unimplemented".to_string()
} else {
parse_macro_input!(attr as Ident).to_string()
};
let behavior = match behavior.as_str() {
"unimplemented" => quote! { unimplemented!() },
"panic" => quote! { panic!() },
"error" => quote! { Err(libsys::error::Errno::NotImplemented) },
_ => panic!("Unknown #[auto_inode] behavior: {:?}", behavior)
};
missing.insert("create".to_string());
missing.insert("remove".to_string());
missing.insert("lookup".to_string());
missing.insert("open".to_string());
missing.insert("close".to_string());
missing.insert("truncate".to_string());
missing.insert("read".to_string());
missing.insert("write".to_string());
missing.insert("stat".to_string());
missing.insert("size".to_string());
missing.insert("ioctl".to_string());
missing.insert("is_ready".to_string());
missing.insert("readdir".to_string());
for item in &impl_item.items {
match item {
ImplItem::Method(method) => {
let name = &method.sig.ident.to_string();
if missing.contains(name) {
missing.remove(name);
}
}
_ => panic!("Unexpected impl item"),
}
}
for item in &missing {
impl_item
.items
.push(impl_inode_fn(item, behavior.clone()));
}
impl_item.to_token_stream().into()
}
//fn impl_inode_fn<T: ToTokens>(name: &str, behavior: T) -> ImplItem {
// // TODO somehow know if current crate is vfs or not?
// ImplItem::Verbatim(match name {
// "create" => quote! {
// fn create(&mut self, _at: VnodeRef, _name: &str, kind: VnodeKind) ->
// Result<VnodeRef, libsys::error::Errno>
// {
// #behavior
// }
// },
// "remove" => quote! {
// fn remove(&mut self, _at: VnodeRef, _name: &str) -> Result<(), libsys::error::Errno> {
// #behavior
// }
// },
// "lookup" => quote! {
// fn lookup(&mut self, _at: VnodeRef, _name: &str) ->
// Result<VnodeRef, libsys::error::Errno>
// {
// #behavior
// }
// },
// "stat" => quote! {
// fn stat(&mut self, _at: VnodeRef) ->
// Result<libsys::stat::Stat, libsys::error::Errno>
// {
// #behavior
// }
// },
// "truncate" => quote! {
// fn truncate(&mut self, _node: VnodeRef, _size: usize) ->
// Result<(), libsys::error::Errno>
// {
// #behavior
// }
// },
// "size" => quote! {
// fn size(&mut self, _node: VnodeRef) -> Result<usize, libsys::error::Errno> {
// #behavior
// }
// },
// "read" => quote! {
// fn read(&mut self, _node: VnodeRef, _pos: usize, _data: &mut [u8]) ->
// Result<usize, libsys::error::Errno>
// {
// #behavior
// }
// },
// "write" => quote! {
// fn write(&mut self, _node: VnodeRef, _pos: usize, _data: &[u8]) ->
// Result<usize, libsys::error::Errno>
// {
// #behavior
// }
// },
// "open" => quote! {
// fn open(&mut self, _node: VnodeRef, _flags: libsys::stat::OpenFlags) ->
// Result<usize, libsys::error::Errno>
// {
// #behavior
// }
// },
// "close" => quote! {
// fn close(&mut self, _node: VnodeRef) -> Result<(), libsys::error::Errno> {
// #behavior
// }
// },
// "ioctl" => quote! {
// fn ioctl(
// &mut self,
// _node: VnodeRef,
// _cmd: libsys::ioctl::IoctlCmd,
// _ptr: usize,
// _len: usize) ->
// Result<usize, libsys::error::Errno>
// {
// #behavior
// }
// },
// "is_ready" => quote! {
// fn is_ready(&mut self, _node: VnodeRef, _write: bool) ->
// Result<bool, libsys::error::Errno>
// {
// #behavior
// }
// },
// "readdir" => quote! {
// fn readdir(
// &mut self,
// _node: VnodeRef,
// _pos: usize,
// _entries: &mut [libsys::stat::DirectoryEntry]
// ) ->
// Result<usize, libsys::error::Errno>
// {
// #behavior
// }
// },
// _ => panic!("TODO implement {:?}", name),
// })
//}
//
//#[proc_macro_attribute]
//pub fn auto_inode(attr: TokenStream, input: TokenStream) -> TokenStream {
// let mut impl_item = parse_macro_input!(input as ItemImpl);
// let mut missing = HashSet::<String>::new();
// let behavior = if attr.is_empty() {
// "unimplemented".to_string()
// } else {
// parse_macro_input!(attr as Ident).to_string()
// };
// let behavior = match behavior.as_str() {
// "unimplemented" => quote! { unimplemented!() },
// "panic" => quote! { panic!() },
// "error" => quote! { Err(libsys::error::Errno::NotImplemented) },
// _ => panic!("Unknown #[auto_inode] behavior: {:?}", behavior)
// };
//
// missing.insert("create".to_string());
// missing.insert("remove".to_string());
// missing.insert("lookup".to_string());
// missing.insert("open".to_string());
// missing.insert("close".to_string());
// missing.insert("truncate".to_string());
// missing.insert("read".to_string());
// missing.insert("write".to_string());
// missing.insert("stat".to_string());
// missing.insert("size".to_string());
// missing.insert("ioctl".to_string());
// missing.insert("is_ready".to_string());
// missing.insert("readdir".to_string());
//
// for item in &impl_item.items {
// match item {
// ImplItem::Method(method) => {
// let name = &method.sig.ident.to_string();
// if missing.contains(name) {
// missing.remove(name);
// }
// }
// _ => panic!("Unexpected impl item"),
// }
// }
//
// for item in &missing {
// impl_item
// .items
// .push(impl_inode_fn(item, behavior.clone()));
// }
//
// impl_item.to_token_stream().into()
//}
+3
View File
@@ -11,6 +11,9 @@ pub struct BlockRef<'a, A: BlockAllocator + Copy> {
alloc: MaybeUninit<A>,
}
/// # Safety
///
/// This trait is unsafe to implement due to its direct memory management
pub unsafe trait BlockAllocator {
fn alloc(&self) -> *mut u8;
/// # Safety
+55 -12
View File
@@ -1,27 +1,29 @@
use crate::{BlockAllocator, Bvec, FileInode};
use alloc::boxed::Box;
use libsys::{error::Errno, stat::Stat};
use vfs::{Vnode, VnodeImpl, VnodeKind, VnodeRef};
use libsys::{error::Errno, stat::{Stat, DirectoryEntry, OpenFlags}, ioctl::IoctlCmd};
use vfs::{Vnode, VnodeCommon, VnodeDirectory, VnodeRef, VnodeCreateKind, VnodeData};
use core::cell::RefCell;
pub struct DirInode<A: BlockAllocator + Copy + 'static> {
alloc: A,
}
#[auto_inode]
impl<A: BlockAllocator + Copy + 'static> VnodeImpl for DirInode<A> {
impl<A: BlockAllocator + Copy + 'static> VnodeDirectory for DirInode<A> {
fn create(
&mut self,
_parent: VnodeRef,
name: &str,
kind: VnodeKind,
kind: VnodeCreateKind,
) -> Result<VnodeRef, Errno> {
let vnode = Vnode::new(name, kind, Vnode::SEEKABLE | Vnode::CACHE_READDIR);
match kind {
VnodeKind::Directory => vnode.set_data(Box::new(DirInode { alloc: self.alloc })),
VnodeKind::Regular => vnode.set_data(Box::new(FileInode::new(Bvec::new(self.alloc)))),
_ => todo!(),
}
Ok(vnode)
let data = match kind {
VnodeCreateKind::Directory => VnodeData::Directory(RefCell::new(Some(Box::new(DirInode { alloc: self.alloc })))),
VnodeCreateKind::File => VnodeData::File(RefCell::new(Some(Box::new(FileInode::new(Bvec::new(self.alloc)))))),
};
Ok(Vnode::new(name, data, Vnode::SEEKABLE | Vnode::CACHE_READDIR))
// match kind {
// _ => todo!(),
// }
// Ok(vnode)
}
fn lookup(&mut self, _parent: VnodeRef, _name: &str) -> Result<VnodeRef, Errno> {
@@ -32,6 +34,18 @@ impl<A: BlockAllocator + Copy + 'static> VnodeImpl for DirInode<A> {
Ok(())
}
/// Read directory entries into target buffer
fn readdir(
&mut self,
_node: VnodeRef,
_pos: usize,
_data: &mut [DirectoryEntry],
) -> Result<usize, Errno> {
todo!()
}
}
impl<A: BlockAllocator + Copy + 'static> VnodeCommon for DirInode<A> {
fn stat(&mut self, node: VnodeRef) -> Result<Stat, Errno> {
let props = node.props();
Ok(Stat {
@@ -40,6 +54,35 @@ impl<A: BlockAllocator + Copy + 'static> VnodeImpl for DirInode<A> {
mode: props.mode,
})
}
/// Performs filetype-specific request
fn ioctl(
&mut self,
_node: VnodeRef,
_cmd: IoctlCmd,
_ptr: usize,
_len: usize,
) -> Result<usize, Errno> {
todo!()
}
/// Reports the size of this filesystem object in bytes
fn size(&mut self, _node: VnodeRef) -> Result<usize, Errno> {
todo!()
}
/// Returns `true` if node is ready for an operation
fn ready(&mut self, _node: VnodeRef, _write: bool) -> Result<bool, Errno> {
todo!()
}
fn open(&mut self, _node: VnodeRef, _flags: OpenFlags) -> Result<usize, Errno> {
Ok(0)
}
fn close(&mut self, _node: VnodeRef) -> Result<(), Errno> {
Ok(())
}
}
impl<A: BlockAllocator + Copy + 'static> DirInode<A> {
+35 -16
View File
@@ -2,15 +2,16 @@ use crate::{BlockAllocator, Bvec};
use libsys::{
error::Errno,
stat::{OpenFlags, Stat},
ioctl::IoctlCmd
};
use vfs::{VnodeImpl, VnodeKind, VnodeRef};
use vfs::{VnodeCommon, VnodeFile, VnodeRef};
pub struct FileInode<'a, A: BlockAllocator + Copy + 'static> {
data: Bvec<'a, A>,
}
#[auto_inode]
impl<'a, A: BlockAllocator + Copy + 'static> VnodeImpl for FileInode<'a, A> {
// #[auto_inode]
impl<'a, A: BlockAllocator + Copy + 'static> VnodeCommon for FileInode<'a, A> {
fn open(&mut self, _node: VnodeRef, _mode: OpenFlags) -> Result<usize, Errno> {
Ok(0)
}
@@ -18,19 +19,6 @@ impl<'a, A: BlockAllocator + Copy + 'static> VnodeImpl for FileInode<'a, A> {
fn close(&mut self, _node: VnodeRef) -> Result<(), Errno> {
Ok(())
}
fn read(&mut self, _node: VnodeRef, pos: usize, data: &mut [u8]) -> Result<usize, Errno> {
self.data.read(pos, data)
}
fn write(&mut self, _node: VnodeRef, pos: usize, data: &[u8]) -> Result<usize, Errno> {
self.data.write(pos, data)
}
fn truncate(&mut self, _node: VnodeRef, size: usize) -> Result<(), Errno> {
self.data.resize((size + 4095) / 4096)
}
fn size(&mut self, _node: VnodeRef) -> Result<usize, Errno> {
Ok(self.data.size())
}
@@ -43,6 +31,37 @@ impl<'a, A: BlockAllocator + Copy + 'static> VnodeImpl for FileInode<'a, A> {
mode: props.mode
})
}
/// Performs filetype-specific request
fn ioctl(
&mut self,
_node: VnodeRef,
_cmd: IoctlCmd,
_ptr: usize,
_len: usize,
) -> Result<usize, Errno> {
todo!()
}
/// Returns `true` if node is ready for an operation
fn ready(&mut self, _node: VnodeRef, _write: bool) -> Result<bool, Errno> {
todo!()
}
}
impl<'a, A: BlockAllocator + Copy + 'static> VnodeFile for FileInode<'a, A> {
fn read(&mut self, _node: VnodeRef, pos: usize, data: &mut [u8]) -> Result<usize, Errno> {
self.data.read(pos, data)
}
fn write(&mut self, _node: VnodeRef, pos: usize, data: &[u8]) -> Result<usize, Errno> {
self.data.write(pos, data)
}
fn truncate(&mut self, _node: VnodeRef, size: usize) -> Result<(), Errno> {
self.data.resize((size + 4095) / 4096)
}
}
impl<'a, A: BlockAllocator + Copy + 'static> FileInode<'a, A> {
+29 -37
View File
@@ -1,9 +1,4 @@
#![feature(
const_fn_trait_bound,
const_mut_refs,
maybe_uninit_extra,
maybe_uninit_uninit_array
)]
#![feature(const_fn_trait_bound, const_mut_refs, maybe_uninit_uninit_array)]
#![no_std]
extern crate alloc;
@@ -11,9 +6,6 @@ extern crate alloc;
#[macro_use]
extern crate std;
#[macro_use]
extern crate fs_macros;
use alloc::{boxed::Box, rc::Rc};
use core::any::Any;
use core::cell::{Ref, RefCell};
@@ -22,14 +14,14 @@ use libsys::{
path::{path_component_left, path_component_right},
stat::FileMode,
};
use vfs::{BlockDevice, Filesystem, Vnode, VnodeKind, VnodeRef};
use vfs::{BlockDevice, Filesystem, Vnode, VnodeCreateKind, VnodeData, VnodeRef};
mod block;
pub use block::{BlockAllocator, BlockRef};
mod bvec;
use bvec::Bvec;
mod tar;
use tar::{TarIterator, Tar};
use tar::{Tar, TarIterator};
mod file;
use file::FileInode;
mod dir;
@@ -68,16 +60,16 @@ impl<A: BlockAllocator + Copy + 'static> Ramfs<A> {
}
fn create_node_initial(self: Rc<Self>, name: &str, tar: &Tar) -> VnodeRef {
let kind = tar.node_kind();
let node = Vnode::new(name, kind, Vnode::SEEKABLE | Vnode::CACHE_READDIR);
node.props_mut().mode = tar.mode();
node.set_fs(self.clone());
match kind {
VnodeKind::Directory => node.set_data(Box::new(DirInode::new(self.alloc))),
VnodeKind::Regular => {}
VnodeKind::Char => todo!(),
VnodeKind::Block => todo!(),
let kind = tar.node_create_kind();
let data = match kind {
VnodeCreateKind::Directory => {
VnodeData::Directory(RefCell::new(Some(Box::new(DirInode::new(self.alloc)))))
}
VnodeCreateKind::File => VnodeData::File(RefCell::new(None)),
};
let node = Vnode::new(name, data, Vnode::SEEKABLE | Vnode::CACHE_READDIR);
node.props_mut().mode = tar.mode();
node.set_fs(self);
node
}
@@ -101,7 +93,8 @@ impl<A: BlockAllocator + Copy + 'static> Ramfs<A> {
return Err(Errno::DoesNotExist);
}
// TODO file modes
at.create(element, FileMode::default_dir(), VnodeKind::Directory)?
at.create(element, FileMode::default_dir(), VnodeCreateKind::Directory)?
// todo!();
}
};
@@ -113,9 +106,10 @@ impl<A: BlockAllocator + Copy + 'static> Ramfs<A> {
}
unsafe fn load_tar(self: Rc<Self>, base: *const u8, size: usize) -> Result<VnodeRef, Errno> {
let root = Vnode::new("", VnodeKind::Directory, Vnode::SEEKABLE | Vnode::CACHE_READDIR);
let root_data =
VnodeData::Directory(RefCell::new(Some(Box::new(DirInode::new(self.alloc)))));
let root = Vnode::new("", root_data, Vnode::SEEKABLE | Vnode::CACHE_READDIR);
root.set_fs(self.clone());
root.set_data(Box::new(DirInode::new(self.alloc)));
root.props_mut().mode = FileMode::default_dir();
// 1. Create all the paths in TAR
@@ -123,10 +117,7 @@ impl<A: BlockAllocator + Copy + 'static> Ramfs<A> {
let (dirname, basename) = path_component_right(block.path()?);
let parent = self.clone().make_path(root.clone(), dirname, true)?;
let node = self
.clone()
.create_node_initial(basename, block);
assert_eq!(node.kind(), block.node_kind());
let node = self.clone().create_node_initial(basename, block);
parent.attach(node);
}
@@ -135,16 +126,17 @@ impl<A: BlockAllocator + Copy + 'static> Ramfs<A> {
if block.is_file() {
// Will not create any dirs
let node = self.clone().make_path(root.clone(), block.path()?, false)?;
assert_eq!(node.kind(), block.node_kind());
#[cfg(feature = "cow")]
{
let data = block.data();
node.set_data(Box::new(FileInode::new(Bvec::new_copy_on_write(
self.alloc,
data.as_ptr(),
data.len(),
))));
node.as_file()
.unwrap()
.replace(Some(Box::new(FileInode::new(Bvec::new_copy_on_write(
self.alloc,
data.as_ptr(),
data.len(),
)))));
}
#[cfg(not(feature = "cow"))]
{
@@ -167,7 +159,7 @@ impl<A: BlockAllocator + Copy + 'static> Ramfs<A> {
mod tests {
use super::*;
use alloc::boxed::Box;
use libcommon::Read;
use libsys::{traits::Read, stat::{UserId, GroupId, OpenFlags}};
use vfs::Ioctx;
#[test]
@@ -189,15 +181,15 @@ mod tests {
let fs = unsafe { Ramfs::open(data.as_ptr(), data.bytes().len(), A {}).unwrap() };
let root = fs.root().unwrap();
let ioctx = Ioctx::new(root.clone());
let ioctx = Ioctx::new(root.clone(), UserId::root(), GroupId::root());
assert!(Rc::ptr_eq(&ioctx.find(None, "/", true).unwrap(), &root));
let node = ioctx.find(None, "/test1.txt", true).unwrap();
let mut file = node.open().unwrap();
let mut file = node.open(OpenFlags::O_RDONLY).unwrap();
let mut buf = [0u8; 1024];
assert_eq!(file.read(&mut buf).unwrap(), 20);
assert_eq!(file.borrow_mut().read(&mut buf).unwrap(), 20);
let s = core::str::from_utf8(&buf[..20]).unwrap();
assert_eq!(s, "This is a test file\n");
}
+7 -8
View File
@@ -1,5 +1,5 @@
use libsys::{error::Errno, stat::FileMode};
use vfs::VnodeKind;
use vfs::VnodeCreateKind;
#[repr(packed)]
#[allow(dead_code)]
@@ -73,19 +73,18 @@ impl Tar {
core::str::from_utf8(&self.name[..zero_index]).map_err(|_| Errno::InvalidArgument)
}
pub fn node_kind(&self) -> VnodeKind {
pub fn node_create_kind(&self) -> VnodeCreateKind {
match self.type_ {
0 | b'0' => VnodeKind::Regular,
b'5' => VnodeKind::Directory,
0 | b'0' => VnodeCreateKind::File,
b'5' => VnodeCreateKind::Directory,
p => panic!("Unrecognized tar entry type: '{}'", p as char),
}
}
pub fn mode(&self) -> FileMode {
let t = match self.node_kind() {
VnodeKind::Regular => FileMode::S_IFREG,
VnodeKind::Directory => FileMode::S_IFDIR,
_ => todo!()
let t = match self.node_create_kind() {
VnodeCreateKind::File => FileMode::S_IFREG,
VnodeCreateKind::Directory => FileMode::S_IFDIR,
};
FileMode::from_bits(from_octal(&self.mode) as u32).unwrap() | t
}
+1 -49
View File
@@ -1,5 +1,4 @@
use crate::{VnodeImpl, VnodeKind, VnodeRef};
use libsys::{error::Errno, ioctl::IoctlCmd, stat::OpenFlags};
use libsys::{error::Errno, ioctl::IoctlCmd};
/// Generic character device trait
pub trait CharDevice {
@@ -22,50 +21,3 @@ pub trait CharDevice {
/// Returns `true` if the device is ready for an operation
fn is_ready(&self, write: bool) -> Result<bool, Errno>;
}
/// Wrapper struct to attach [VnodeImpl] implementation
/// to [CharDevice]s
pub struct CharDeviceWrapper {
device: &'static dyn CharDevice,
}
#[auto_inode(error)]
impl VnodeImpl for CharDeviceWrapper {
fn open(&mut self, _node: VnodeRef, _opts: OpenFlags) -> Result<usize, Errno> {
Ok(0)
}
fn close(&mut self, _node: VnodeRef) -> Result<(), Errno> {
Ok(())
}
fn read(&mut self, _node: VnodeRef, _pos: usize, data: &mut [u8]) -> Result<usize, Errno> {
self.device.read(true, data)
}
fn write(&mut self, _node: VnodeRef, _pos: usize, data: &[u8]) -> Result<usize, Errno> {
self.device.write(true, data)
}
fn is_ready(&mut self, _node: VnodeRef, write: bool) -> Result<bool, Errno> {
self.device.is_ready(write)
}
fn ioctl(
&mut self,
_node: VnodeRef,
cmd: IoctlCmd,
ptr: usize,
len: usize,
) -> Result<usize, Errno> {
self.device.ioctl(cmd, ptr, len)
}
}
impl CharDeviceWrapper {
/// Creates a wrapper for static [CharDevice] trait object to
/// auto-implement [VnodeImpl] trait for the device
pub const fn new(device: &'static dyn CharDevice) -> Self {
Self { device }
}
}
+58 -28
View File
@@ -1,7 +1,8 @@
use crate::{VnodeKind, VnodeRef, Vnode};
use crate::{Vnode, VnodeRef};
use alloc::rc::Rc;
use core::cell::RefCell;
use core::cmp::min;
use core::str::FromStr;
use libsys::{
error::Errno,
stat::DirectoryEntry,
@@ -38,9 +39,10 @@ impl Read for File {
match &mut self.inner {
FileInner::Normal(inner) => {
let count = inner.vnode.read(inner.pos, data)?;
if inner.vnode.kind() != VnodeKind::Char {
inner.pos += count;
}
// TODO
// if inner.vnode.kind() != VnodeKind::Char {
inner.pos += count;
// }
Ok(count)
}
_ => unimplemented!(),
@@ -57,9 +59,10 @@ impl Write for File {
match &mut self.inner {
FileInner::Normal(inner) => {
let count = inner.vnode.write(inner.pos, data)?;
if inner.vnode.kind() != VnodeKind::Char {
inner.pos += count;
}
// TODO
// if inner.vnode.kind() != VnodeKind::Char {
inner.pos += count;
// }
Ok(count)
}
_ => unimplemented!(),
@@ -126,14 +129,17 @@ impl File {
}
/// Returns `true` if the file is ready for an operation
pub fn is_ready(&self, write: bool) -> Result<bool, Errno> {
pub fn ready(&self, write: bool) -> Result<bool, Errno> {
match &self.inner {
FileInner::Normal(inner) => inner.vnode.is_ready(write),
FileInner::Normal(inner) => inner.vnode.ready(write),
_ => todo!(),
}
}
fn cache_readdir(inner: &mut NormalFile, entries: &mut [DirectoryEntry]) -> Result<usize, Errno> {
fn cache_readdir(
inner: &mut NormalFile,
entries: &mut [DirectoryEntry],
) -> Result<usize, Errno> {
let mut count = entries.len();
let mut offset = 0usize;
@@ -142,7 +148,7 @@ impl File {
return Ok(offset);
}
entries[offset] = DirectoryEntry::from_str(".");
entries[offset] = DirectoryEntry::from_str(".").unwrap();
inner.pos = Self::POS_CACHE_DOT_DOT;
offset += 1;
@@ -154,7 +160,7 @@ impl File {
return Ok(offset);
}
entries[offset] = DirectoryEntry::from_str("..");
entries[offset] = DirectoryEntry::from_str("..").unwrap();
inner.pos = 0;
offset += 1;
@@ -166,7 +172,7 @@ impl File {
}
let count = inner.vnode.for_each_entry(inner.pos, count, |i, e| {
entries[offset + i] = DirectoryEntry::from_str(e.name());
entries[offset + i] = DirectoryEntry::from_str(e.name()).unwrap();
});
inner.pos += count;
Ok(offset + count)
@@ -176,7 +182,7 @@ impl File {
pub fn readdir(&mut self, entries: &mut [DirectoryEntry]) -> Result<usize, Errno> {
match &mut self.inner {
FileInner::Normal(inner) => {
assert_eq!(inner.vnode.kind(), VnodeKind::Directory);
// assert_eq!(inner.vnode.kind(), VnodeKind::Directory);
if inner.vnode.flags() & Vnode::CACHE_READDIR != 0 {
Self::cache_readdir(inner, entries)
@@ -203,24 +209,38 @@ impl Drop for File {
#[cfg(test)]
mod tests {
use super::*;
use crate::{Vnode, VnodeImpl, VnodeKind, VnodeRef};
use libsys::{stat::OpenFlags, ioctl::IoctlCmd, stat::Stat};
use alloc::boxed::Box;
use alloc::rc::Rc;
use crate::node::{VnodeCommon, VnodeFile};
use libsys::{ioctl::IoctlCmd, stat::OpenFlags, stat::Stat};
struct DummyInode;
#[auto_inode]
impl VnodeImpl for DummyInode {
fn create(
impl VnodeCommon for DummyInode {
/// Performs filetype-specific request
fn ioctl(
&mut self,
_at: VnodeRef,
name: &str,
kind: VnodeKind,
) -> Result<VnodeRef, Errno> {
let node = Vnode::new(name, kind, 0);
node.set_data(Box::new(DummyInode {}));
Ok(node)
node: VnodeRef,
cmd: IoctlCmd,
ptr: usize,
len: usize,
) -> Result<usize, Errno> {
todo!()
}
/// Retrieves file status
fn stat(&mut self, node: VnodeRef) -> Result<Stat, Errno> {
todo!()
}
/// Reports the size of this filesystem object in bytes
fn size(&mut self, node: VnodeRef) -> Result<usize, Errno> {
todo!()
}
/// Returns `true` if node is ready for an operation
fn is_ready(&mut self, node: VnodeRef, write: bool) -> Result<bool, Errno> {
todo!()
}
fn open(&mut self, _node: VnodeRef, _flags: OpenFlags) -> Result<usize, Errno> {
@@ -231,6 +251,9 @@ mod tests {
Ok(())
}
}
impl VnodeFile for DummyInode {
fn read(&mut self, _node: VnodeRef, pos: usize, data: &mut [u8]) -> Result<usize, Errno> {
#[cfg(test)]
println!("read {} at {}", data.len(), pos);
@@ -248,12 +271,19 @@ mod tests {
fn write(&mut self, _node: VnodeRef, _pos: usize, _data: &[u8]) -> Result<usize, Errno> {
Err(Errno::NotImplemented)
}
fn truncate(&mut self, node: VnodeRef, size: usize) -> Result<(), Errno> {
todo!()
}
}
#[test]
fn test_normal_read() {
let node = Vnode::new("", VnodeKind::Regular, 0);
node.set_data(Box::new(DummyInode {}));
let node = Vnode::new(
"",
VnodeData::File(RefCell::new(Some(Box::new(DummyInode {})))),
0,
);
let mut file = node.open(OpenFlags::O_RDONLY).unwrap();
let mut buf = [0u8; 4096];
+197 -198
View File
@@ -1,4 +1,4 @@
use crate::{FileRef, VnodeKind, VnodeRef};
use crate::{FileRef, VnodeRef, VnodeCreateKind};
use libsys::{
error::Errno,
path::{path_component_left, path_component_right},
@@ -34,9 +34,10 @@ impl Ioctx {
loop {
(element, rest) = path_component_left(rest);
if !at.is_directory() {
return Err(Errno::NotADirectory);
}
at.as_directory()?;
// if !at.is_directory() {
// return Err(Errno::NotADirectory);
// }
match element {
".." => {
@@ -48,7 +49,7 @@ impl Ioctx {
}
while let Some(target) = at.target() {
assert!(at.kind() == VnodeKind::Directory);
// assert!(at.kind() == VnodeKind::Directory);
at = target;
}
@@ -60,7 +61,7 @@ impl Ioctx {
let mut node = at.lookup_or_load(element)?;
while let Some(target) = node.target() {
assert!(node.kind() == VnodeKind::Directory);
// assert!(node.kind() == VnodeKind::Directory);
node = target;
}
@@ -101,7 +102,7 @@ impl Ioctx {
self.find(at, parent, true)?.create(
name.trim_start_matches('/'),
mode,
VnodeKind::Directory,
VnodeCreateKind::Directory
)
}
@@ -117,7 +118,7 @@ impl Ioctx {
Err(Errno::DoesNotExist) => {
let (parent, name) = path_component_right(path);
let at = self.find(at, parent, true)?;
at.create(name, mode, VnodeKind::Regular)
at.create(name, mode, VnodeCreateKind::File)
}
o => o,
}?;
@@ -128,198 +129,196 @@ impl Ioctx {
/// Changes current working directory of the process
pub fn chdir(&mut self, path: &str) -> Result<(), Errno> {
let node = self.find(None, path, true)?;
if !node.is_directory() {
return Err(Errno::NotADirectory);
}
let _dir = node.as_directory()?;
self.cwd = node;
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{Vnode, VnodeImpl, VnodeKind};
use alloc::{boxed::Box, rc::Rc};
use libsys::{ioctl::IoctlCmd, stat::OpenFlags, stat::Stat};
pub struct DummyInode;
#[auto_inode]
impl VnodeImpl for DummyInode {
fn create(
&mut self,
_at: VnodeRef,
name: &str,
kind: VnodeKind,
) -> Result<VnodeRef, Errno> {
let vnode = Vnode::new(name, kind, 0);
vnode.set_data(Box::new(DummyInode {}));
Ok(vnode)
}
fn lookup(&mut self, _at: VnodeRef, _name: &str) -> Result<VnodeRef, Errno> {
Err(Errno::DoesNotExist)
}
}
#[test]
fn test_find_existing_absolute() {
let root = Vnode::new("", VnodeKind::Directory, 0);
let d0 = Vnode::new("dir0", VnodeKind::Directory, 0);
let d1 = Vnode::new("dir1", VnodeKind::Directory, 0);
let d0d0 = Vnode::new("dir0", VnodeKind::Directory, 0);
let d0f0 = Vnode::new("file0", VnodeKind::Regular, 0);
let d1f0 = Vnode::new("file0", VnodeKind::Regular, 0);
root.attach(d0.clone());
root.attach(d1.clone());
d0.attach(d0d0.clone());
d0.attach(d0f0.clone());
d1.attach(d1f0.clone());
let ioctx = Ioctx::new(root.clone());
assert!(Rc::ptr_eq(&root, &ioctx.find(None, "/", false).unwrap()));
assert!(Rc::ptr_eq(&root, &ioctx.find(None, "/.", false).unwrap()));
assert!(Rc::ptr_eq(&root, &ioctx.find(None, "/./.", false).unwrap()));
assert!(Rc::ptr_eq(
&root,
&ioctx.find(None, "/.///.", false).unwrap()
));
assert!(Rc::ptr_eq(&root, &ioctx.find(None, "/..", false).unwrap()));
assert!(Rc::ptr_eq(&root, &ioctx.find(None, "/../", false).unwrap()));
assert!(Rc::ptr_eq(
&root,
&ioctx.find(None, "/../.", false).unwrap()
));
assert!(Rc::ptr_eq(
&root,
&ioctx.find(None, "/../..", false).unwrap()
));
assert!(Rc::ptr_eq(&d0, &ioctx.find(None, "/dir0", false).unwrap()));
assert!(Rc::ptr_eq(&d1, &ioctx.find(None, "/dir1", false).unwrap()));
assert!(Rc::ptr_eq(
&d0,
&ioctx.find(None, "/dir1/../dir0", false).unwrap()
));
assert!(Rc::ptr_eq(
&d1,
&ioctx
.find(None, "/dir1/../dir0/./../../.././dir1", false)
.unwrap()
));
assert!(Rc::ptr_eq(
&d0d0,
&ioctx.find(None, "/dir0/dir0", false).unwrap()
));
assert!(Rc::ptr_eq(
&d0d0,
&ioctx.find(None, "/dir0/dir0/.", false).unwrap()
));
assert!(Rc::ptr_eq(
&d0,
&ioctx.find(None, "/dir0/dir0/..", false).unwrap()
));
assert!(Rc::ptr_eq(
&d0,
&ioctx.find(None, "/dir0/dir0/../", false).unwrap()
));
assert!(Rc::ptr_eq(
&d0,
&ioctx.find(None, "/dir0/dir0/../.", false).unwrap()
));
assert!(Rc::ptr_eq(
&d0f0,
&ioctx.find(None, "/dir0/file0", false).unwrap()
));
assert!(Rc::ptr_eq(
&d0f0,
&ioctx.find(None, "/dir1/../dir0/./file0", false).unwrap()
));
}
#[test]
fn test_find_rejects_file_dots() {
let root = Vnode::new("", VnodeKind::Directory, 0);
let d0 = Vnode::new("dir0", VnodeKind::Directory, 0);
let d0f0 = Vnode::new("file0", VnodeKind::Regular, 0);
root.attach(d0.clone());
d0.attach(d0f0.clone());
let ioctx = Ioctx::new(root.clone());
assert_eq!(
ioctx.find(None, "/dir0/file0/.", false).unwrap_err(),
Errno::NotADirectory
);
assert_eq!(
ioctx.find(None, "/dir0/file0/..", false).unwrap_err(),
Errno::NotADirectory
);
// TODO handle this case
// assert_eq!(ioctx.find(None, "/dir0/file0/").unwrap_err(), Errno::NotADirectory);
}
#[test]
fn test_mkdir() {
let root = Vnode::new("", VnodeKind::Directory, 0);
let ioctx = Ioctx::new(root.clone());
root.set_data(Box::new(DummyInode {}));
assert!(ioctx.mkdir(None, "/dir0", FileMode::default_dir()).is_ok());
assert_eq!(
ioctx
.mkdir(None, "/dir0", FileMode::default_dir())
.unwrap_err(),
Errno::AlreadyExists
);
}
#[test]
fn test_find_mount() {
let root_outer = Vnode::new("", VnodeKind::Directory, 0);
let dir0 = Vnode::new("dir0", VnodeKind::Directory, 0);
let root_inner = Vnode::new("", VnodeKind::Directory, 0);
let dir1 = Vnode::new("dir1", VnodeKind::Directory, 0);
root_outer.clone().attach(dir0.clone());
root_inner.clone().attach(dir1.clone());
let ioctx = Ioctx::new(root_outer.clone());
assert_eq!(
ioctx.find(None, "/dir0/dir1", false).unwrap_err(),
Errno::DoesNotExist
);
dir0.mount(root_inner.clone()).unwrap();
assert!(Rc::ptr_eq(
&root_inner,
&ioctx.find(None, "/dir0", false).unwrap()
));
assert!(Rc::ptr_eq(
&dir1,
&ioctx.find(None, "/dir0/dir1", false).unwrap()
));
assert!(Rc::ptr_eq(
&root_inner,
&ioctx.find(None, "/dir0/dir1/..", false).unwrap()
));
assert!(Rc::ptr_eq(
&dir0,
&ioctx.find(None, "/dir0/dir1/../..", false).unwrap()
));
assert!(Rc::ptr_eq(
&root_outer,
&ioctx.find(None, "/dir0/dir1/../../..", false).unwrap()
));
}
}
// #[cfg(test)]
// mod tests {
// use super::*;
// use crate::{Vnode, VnodeImpl, VnodeKind};
// use alloc::{boxed::Box, rc::Rc};
// use libsys::{ioctl::IoctlCmd, stat::OpenFlags, stat::Stat};
//
// pub struct DummyInode;
//
// #[auto_inode]
// impl VnodeImpl for DummyInode {
// fn create(
// &mut self,
// _at: VnodeRef,
// name: &str,
// kind: VnodeKind,
// ) -> Result<VnodeRef, Errno> {
// let vnode = Vnode::new(name, kind, 0);
// vnode.set_data(Box::new(DummyInode {}));
// Ok(vnode)
// }
//
// fn lookup(&mut self, _at: VnodeRef, _name: &str) -> Result<VnodeRef, Errno> {
// Err(Errno::DoesNotExist)
// }
// }
//
// #[test]
// fn test_find_existing_absolute() {
// let root = Vnode::new("", VnodeKind::Directory, 0);
// let d0 = Vnode::new("dir0", VnodeKind::Directory, 0);
// let d1 = Vnode::new("dir1", VnodeKind::Directory, 0);
// let d0d0 = Vnode::new("dir0", VnodeKind::Directory, 0);
// let d0f0 = Vnode::new("file0", VnodeKind::Regular, 0);
// let d1f0 = Vnode::new("file0", VnodeKind::Regular, 0);
//
// root.attach(d0.clone());
// root.attach(d1.clone());
// d0.attach(d0d0.clone());
// d0.attach(d0f0.clone());
// d1.attach(d1f0.clone());
//
// let ioctx = Ioctx::new(root.clone());
//
// assert!(Rc::ptr_eq(&root, &ioctx.find(None, "/", false).unwrap()));
// assert!(Rc::ptr_eq(&root, &ioctx.find(None, "/.", false).unwrap()));
// assert!(Rc::ptr_eq(&root, &ioctx.find(None, "/./.", false).unwrap()));
// assert!(Rc::ptr_eq(
// &root,
// &ioctx.find(None, "/.///.", false).unwrap()
// ));
// assert!(Rc::ptr_eq(&root, &ioctx.find(None, "/..", false).unwrap()));
// assert!(Rc::ptr_eq(&root, &ioctx.find(None, "/../", false).unwrap()));
// assert!(Rc::ptr_eq(
// &root,
// &ioctx.find(None, "/../.", false).unwrap()
// ));
// assert!(Rc::ptr_eq(
// &root,
// &ioctx.find(None, "/../..", false).unwrap()
// ));
//
// assert!(Rc::ptr_eq(&d0, &ioctx.find(None, "/dir0", false).unwrap()));
// assert!(Rc::ptr_eq(&d1, &ioctx.find(None, "/dir1", false).unwrap()));
// assert!(Rc::ptr_eq(
// &d0,
// &ioctx.find(None, "/dir1/../dir0", false).unwrap()
// ));
// assert!(Rc::ptr_eq(
// &d1,
// &ioctx
// .find(None, "/dir1/../dir0/./../../.././dir1", false)
// .unwrap()
// ));
//
// assert!(Rc::ptr_eq(
// &d0d0,
// &ioctx.find(None, "/dir0/dir0", false).unwrap()
// ));
// assert!(Rc::ptr_eq(
// &d0d0,
// &ioctx.find(None, "/dir0/dir0/.", false).unwrap()
// ));
// assert!(Rc::ptr_eq(
// &d0,
// &ioctx.find(None, "/dir0/dir0/..", false).unwrap()
// ));
// assert!(Rc::ptr_eq(
// &d0,
// &ioctx.find(None, "/dir0/dir0/../", false).unwrap()
// ));
// assert!(Rc::ptr_eq(
// &d0,
// &ioctx.find(None, "/dir0/dir0/../.", false).unwrap()
// ));
//
// assert!(Rc::ptr_eq(
// &d0f0,
// &ioctx.find(None, "/dir0/file0", false).unwrap()
// ));
// assert!(Rc::ptr_eq(
// &d0f0,
// &ioctx.find(None, "/dir1/../dir0/./file0", false).unwrap()
// ));
// }
//
// #[test]
// fn test_find_rejects_file_dots() {
// let root = Vnode::new("", VnodeKind::Directory, 0);
// let d0 = Vnode::new("dir0", VnodeKind::Directory, 0);
// let d0f0 = Vnode::new("file0", VnodeKind::Regular, 0);
//
// root.attach(d0.clone());
// d0.attach(d0f0.clone());
//
// let ioctx = Ioctx::new(root.clone());
//
// assert_eq!(
// ioctx.find(None, "/dir0/file0/.", false).unwrap_err(),
// Errno::NotADirectory
// );
// assert_eq!(
// ioctx.find(None, "/dir0/file0/..", false).unwrap_err(),
// Errno::NotADirectory
// );
//
// // TODO handle this case
// // assert_eq!(ioctx.find(None, "/dir0/file0/").unwrap_err(), Errno::NotADirectory);
// }
//
// #[test]
// fn test_mkdir() {
// let root = Vnode::new("", VnodeKind::Directory, 0);
// let ioctx = Ioctx::new(root.clone());
//
// root.set_data(Box::new(DummyInode {}));
//
// assert!(ioctx.mkdir(None, "/dir0", FileMode::default_dir()).is_ok());
// assert_eq!(
// ioctx
// .mkdir(None, "/dir0", FileMode::default_dir())
// .unwrap_err(),
// Errno::AlreadyExists
// );
// }
//
// #[test]
// fn test_find_mount() {
// let root_outer = Vnode::new("", VnodeKind::Directory, 0);
// let dir0 = Vnode::new("dir0", VnodeKind::Directory, 0);
// let root_inner = Vnode::new("", VnodeKind::Directory, 0);
// let dir1 = Vnode::new("dir1", VnodeKind::Directory, 0);
//
// root_outer.clone().attach(dir0.clone());
// root_inner.clone().attach(dir1.clone());
//
// let ioctx = Ioctx::new(root_outer.clone());
//
// assert_eq!(
// ioctx.find(None, "/dir0/dir1", false).unwrap_err(),
// Errno::DoesNotExist
// );
//
// dir0.mount(root_inner.clone()).unwrap();
//
// assert!(Rc::ptr_eq(
// &root_inner,
// &ioctx.find(None, "/dir0", false).unwrap()
// ));
// assert!(Rc::ptr_eq(
// &dir1,
// &ioctx.find(None, "/dir0/dir1", false).unwrap()
// ));
// assert!(Rc::ptr_eq(
// &root_inner,
// &ioctx.find(None, "/dir0/dir1/..", false).unwrap()
// ));
// assert!(Rc::ptr_eq(
// &dir0,
// &ioctx.find(None, "/dir0/dir1/../..", false).unwrap()
// ));
// assert!(Rc::ptr_eq(
// &root_outer,
// &ioctx.find(None, "/dir0/dir1/../../..", false).unwrap()
// ));
// }
// }
+3 -6
View File
@@ -1,15 +1,12 @@
//! Virtual filesystem API and facilities
#![warn(missing_docs)]
#![feature(destructuring_assignment, const_fn_trait_bound)]
#![feature(const_fn_trait_bound, const_discriminant)]
#![no_std]
#[cfg(test)]
#[macro_use]
extern crate std;
#[macro_use]
extern crate fs_macros;
extern crate alloc;
// pub use libsys::stat::{FileMode, OpenFlags, Stat};
@@ -20,10 +17,10 @@ pub use block::BlockDevice;
mod fs;
pub use fs::Filesystem;
mod node;
pub use node::{Vnode, VnodeImpl, VnodeKind, VnodeRef};
pub use node::{Vnode, VnodeData, VnodeRef, VnodeCreateKind, VnodeCommon, VnodeFile, VnodeDirectory};
mod ioctx;
pub use ioctx::Ioctx;
mod file;
pub use file::{File, FileRef};
mod char;
pub use crate::char::{CharDevice, CharDeviceWrapper};
pub use crate::char::CharDevice;
+315 -211
View File
@@ -1,7 +1,8 @@
use crate::{File, FileRef, Filesystem, Ioctx};
use crate::{File, FileRef, Filesystem, Ioctx, CharDevice};
use alloc::{borrow::ToOwned, boxed::Box, rc::Rc, string::String, vec::Vec};
use core::cell::{RefCell, RefMut, Ref};
use core::cell::{Ref, RefCell, RefMut};
use core::fmt;
use core::mem::Discriminant;
use libsys::{
error::Errno,
ioctl::IoctlCmd,
@@ -10,20 +11,81 @@ use libsys::{
/// Convenience type alias for [Rc<Vnode>]
pub type VnodeRef = Rc<Vnode>;
pub type VnodeKind = Discriminant<VnodeData>;
/// List of possible vnode types
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum VnodeKind {
/// Node is a directory with create/lookup/remove operations
Directory,
/// Node is a regular file
Regular,
/// Node is a character device
Char,
/// Node is a block device
Block,
/// Trait implemented by both regular files and directories
pub trait VnodeCommon {
/// Performs filetype-specific request
fn ioctl(
&mut self,
node: VnodeRef,
cmd: IoctlCmd,
ptr: usize,
len: usize,
) -> Result<usize, Errno>;
/// Retrieves file status
fn stat(&mut self, node: VnodeRef) -> Result<Stat, Errno>;
/// Reports the size of this filesystem object in bytes
fn size(&mut self, node: VnodeRef) -> Result<usize, Errno>;
/// Returns `true` if node is ready for an operation
fn ready(&mut self, node: VnodeRef, write: bool) -> Result<bool, Errno>;
/// Opens a vnode for access. Returns initial file position.
fn open(&mut self, node: VnodeRef, opts: OpenFlags) -> Result<usize, Errno>;
/// Closes a vnode
fn close(&mut self, node: VnodeRef) -> Result<(), Errno>;
}
/// Regular file access interface
pub trait VnodeFile: VnodeCommon {
/// Changes file's underlying storage size
fn truncate(&mut self, node: VnodeRef, size: usize) -> Result<(), Errno>;
/// Reads `data.len()` bytes into the buffer from file offset `pos`
fn read(&mut self, node: VnodeRef, pos: usize, data: &mut [u8]) -> Result<usize, Errno>;
/// Writes `data.len()` bytes from the buffer to file offset `pos`.
/// Resizes the file storage if necessary.
fn write(&mut self, node: VnodeRef, pos: usize, data: &[u8]) -> Result<usize, Errno>;
}
/// Directory access interface
pub trait VnodeDirectory: VnodeCommon {
/// Creates entry `name` of type `kind` in directory `at`
fn create(
&mut self,
at: VnodeRef,
name: &str,
kind: VnodeCreateKind,
) -> Result<VnodeRef, Errno>;
/// Removes entry `name` for directory `at`
fn remove(&mut self, at: VnodeRef, name: &str) -> Result<(), Errno>;
/// Loads an entry `name` in directory `at` from filesystem
fn lookup(&mut self, at: VnodeRef, name: &str) -> Result<VnodeRef, Errno>;
/// Read directory entries into target buffer
fn readdir(
&mut self,
node: VnodeRef,
pos: usize,
data: &mut [DirectoryEntry],
) -> Result<usize, Errno>;
}
// /// List of possible vnode types
// #[derive(Debug, Clone, Copy, PartialEq)]
// pub enum VnodeKind {
// /// Node is a directory with create/lookup/remove operations
// Directory,
// /// Node is a regular file
// Regular,
// /// Node is a character device
// Char,
// /// Node is a block device
// Block,
// }
pub(crate) struct TreeNode {
parent: Option<VnodeRef>,
children: Vec<VnodeRef>,
@@ -35,6 +97,25 @@ pub struct VnodeProps {
pub mode: FileMode,
}
/// Specific node implementation data
pub enum VnodeData {
/// Directory node
Directory(RefCell<Option<Box<dyn VnodeDirectory>>>),
/// Regular file node
File(RefCell<Option<Box<dyn VnodeFile>>>),
/// Character device node
Char(&'static dyn CharDevice)
}
/// Node types for create() calls
#[derive(Debug, Clone, Copy)]
pub enum VnodeCreateKind {
/// Directory node
Directory,
/// Regular file node
File,
}
/// Virtual filesystem node struct, generalizes access to
/// underlying real filesystems
pub struct Vnode {
@@ -42,64 +123,13 @@ pub struct Vnode {
tree: RefCell<TreeNode>,
props: RefCell<VnodeProps>,
kind: VnodeKind,
// kind: VnodeKind,
data: VnodeData,
flags: u32,
target: RefCell<Option<VnodeRef>>,
fs: RefCell<Option<Rc<dyn Filesystem>>>,
data: RefCell<Option<Box<dyn VnodeImpl>>>,
}
/// Interface for "inode" of a real filesystem
pub trait VnodeImpl {
// Directory-only operations
/// Creates a new vnode, sets it up, attaches it (in real FS) to `at` with `name` and
/// returns it
fn create(&mut self, at: VnodeRef, name: &str, kind: VnodeKind) -> Result<VnodeRef, Errno>;
/// Removes the filesystem inode from its parent by erasing its directory entry
fn remove(&mut self, at: VnodeRef, name: &str) -> Result<(), Errno>;
/// Looks up a corresponding directory entry for `name`. If present, loads its inode from
/// storage medium and returns a new vnode associated with it.
fn lookup(&mut self, at: VnodeRef, name: &str) -> Result<VnodeRef, Errno>;
/// Opens a vnode for access. Returns initial file position.
fn open(&mut self, node: VnodeRef, opts: OpenFlags) -> Result<usize, Errno>;
/// Closes a vnode
fn close(&mut self, node: VnodeRef) -> Result<(), Errno>;
/// Changes file's underlying storage size
fn truncate(&mut self, node: VnodeRef, size: usize) -> Result<(), Errno>;
/// Reads `data.len()` bytes into the buffer from file offset `pos`
fn read(&mut self, node: VnodeRef, pos: usize, data: &mut [u8]) -> Result<usize, Errno>;
/// Writes `data.len()` bytes from the buffer to file offset `pos`.
/// Resizes the file storage if necessary.
fn write(&mut self, node: VnodeRef, pos: usize, data: &[u8]) -> Result<usize, Errno>;
/// Read directory entries into target buffer
fn readdir(
&mut self,
node: VnodeRef,
pos: usize,
data: &mut [DirectoryEntry],
) -> Result<usize, Errno>;
/// Retrieves file status
fn stat(&mut self, node: VnodeRef) -> Result<Stat, Errno>;
/// Reports the size of this filesystem object in bytes
fn size(&mut self, node: VnodeRef) -> Result<usize, Errno>;
/// Returns `true` if node is ready for an operation
fn is_ready(&mut self, node: VnodeRef, write: bool) -> Result<bool, Errno>;
/// Performs filetype-specific request
fn ioctl(
&mut self,
node: VnodeRef,
cmd: IoctlCmd,
ptr: usize,
len: usize,
) -> Result<usize, Errno>;
// data: RefCell<Option<Box<dyn VnodeImpl>>>,
}
impl Vnode {
@@ -114,10 +144,10 @@ impl Vnode {
/// Constructs a new [Vnode], wrapping it in [Rc]. The resulting node
/// then needs to have [Vnode::set_data()] called on it to be usable.
pub fn new(name: &str, kind: VnodeKind, flags: u32) -> VnodeRef {
pub fn new(name: &str, data: VnodeData, flags: u32) -> VnodeRef {
Rc::new(Self {
name: name.to_owned(),
kind,
data,
flags,
props: RefCell::new(VnodeProps {
mode: FileMode::empty(),
@@ -128,7 +158,6 @@ impl Vnode {
}),
target: RefCell::new(None),
fs: RefCell::new(None),
data: RefCell::new(None),
})
}
@@ -147,19 +176,30 @@ impl Vnode {
self.props.borrow()
}
/// Sets an associated [VnodeImpl] for the [Vnode]
pub fn set_data(&self, data: Box<dyn VnodeImpl>) {
*self.data.borrow_mut() = Some(data);
}
// /// Sets an associated [VnodeImpl] for the [Vnode]
// pub fn set_data(&self, data: Box<dyn VnodeImpl>) {
// *self.data.borrow_mut() = Some(data);
// }
/// Sets an associated [Filesystem] for the [Vnode]
pub fn set_fs(&self, fs: Rc<dyn Filesystem>) {
*self.fs.borrow_mut() = Some(fs);
}
/// Returns a reference to the associated [VnodeImpl]
pub fn data(&self) -> RefMut<Option<Box<dyn VnodeImpl>>> {
self.data.borrow_mut()
/// Returns node's directory implementation data
pub fn as_directory(&self) -> Result<&RefCell<Option<Box<dyn VnodeDirectory>>>, Errno> {
match &self.data {
VnodeData::Directory(data) => Ok(data),
_ => Err(Errno::NotADirectory),
}
}
/// Returns node's regular file implementation data
pub fn as_file(&self) -> Result<&RefCell<Option<Box<dyn VnodeFile>>>, Errno> {
match &self.data {
VnodeData::File(data) => Ok(data),
_ => Err(Errno::IsADirectory),
}
}
/// Returns the associated [Fileystem]
@@ -167,11 +207,6 @@ impl Vnode {
self.fs.borrow().clone()
}
/// Returns `true` if the vnode represents a directory
pub fn is_directory(&self) -> bool {
self.kind == VnodeKind::Directory
}
/// Returns `true` if the vnode allows arbitrary seeking
pub fn is_seekable(&self) -> bool {
self.flags & Self::SEEKABLE != 0
@@ -180,7 +215,7 @@ impl Vnode {
/// Returns kind of the vnode
#[inline(always)]
pub const fn kind(&self) -> VnodeKind {
self.kind
core::mem::discriminant(&self.data)
}
/// Returns flags of the vnode
@@ -220,12 +255,9 @@ impl Vnode {
/// Attaches some filesystem's root directory node at another directory
pub fn mount(self: &VnodeRef, root: VnodeRef) -> Result<(), Errno> {
if !self.is_directory() {
return Err(Errno::NotADirectory);
}
if !root.is_directory() {
return Err(Errno::NotADirectory);
}
let _dir = self.as_directory()?;
let _root_dir = root.as_directory()?;
if self.target.borrow().is_some() {
return Err(Errno::Busy);
}
@@ -252,7 +284,7 @@ impl Vnode {
/// Looks up a child `name` in in-memory tree cache
pub fn lookup(self: &VnodeRef, name: &str) -> Option<VnodeRef> {
assert!(self.is_directory());
// assert!(self.is_directory());
self.tree
.borrow()
.children
@@ -267,7 +299,8 @@ impl Vnode {
limit: usize,
mut f: F,
) -> usize {
assert!(self.is_directory());
// TODO
// assert!(self.is_directory());
let mut count = 0;
for (index, item) in self
.tree
@@ -287,12 +320,12 @@ impl Vnode {
/// Looks up a child `name` in `self`. Will first try looking up a cached
/// vnode and will load it from disk if it's missing.
pub fn lookup_or_load(self: &VnodeRef, name: &str) -> Result<VnodeRef, Errno> {
if let Some(node) = self.lookup(name) {
return Ok(node);
}
let dir = self.as_directory()?;
if let Some(ref mut data) = *self.data() {
let vnode = data.lookup(self.clone(), name)?;
if let Some(node) = self.lookup(name) {
Ok(node)
} else if let Some(ref mut dir) = *dir.borrow_mut() {
let vnode = dir.lookup(self.clone(), name)?;
if let Some(fs) = self.fs() {
vnode.set_fs(fs);
}
@@ -308,11 +341,10 @@ impl Vnode {
self: &VnodeRef,
name: &str,
mode: FileMode,
kind: VnodeKind,
kind: VnodeCreateKind,
) -> Result<VnodeRef, Errno> {
if self.kind != VnodeKind::Directory {
return Err(Errno::NotADirectory);
}
let dir = self.as_directory()?;
if name.contains('/') {
return Err(Errno::InvalidArgument);
}
@@ -323,8 +355,8 @@ impl Vnode {
e => return e,
};
if let Some(ref mut data) = *self.data() {
let vnode = data.create(self.clone(), name, kind)?;
if let Some(ref mut dir) = *dir.borrow_mut() {
let vnode = dir.create(self.clone(), name, kind)?;
if let Some(fs) = self.fs() {
vnode.set_fs(fs);
}
@@ -338,16 +370,14 @@ impl Vnode {
/// Removes a directory entry `name` from `self`
pub fn unlink(self: &VnodeRef, name: &str) -> Result<(), Errno> {
if self.kind != VnodeKind::Directory {
return Err(Errno::NotADirectory);
}
let dir = self.as_directory()?;
if name.contains('/') {
return Err(Errno::InvalidArgument);
}
if let Some(ref mut data) = *self.data() {
if let Some(ref mut dir) = *dir.borrow_mut() {
let vnode = self.lookup(name).ok_or(Errno::DoesNotExist)?;
data.remove(self.clone(), name)?;
dir.remove(self.clone(), name)?;
vnode.detach();
Ok(())
} else {
@@ -358,102 +388,119 @@ impl Vnode {
/// Opens a vnode for access
pub fn open(self: &VnodeRef, flags: OpenFlags) -> Result<FileRef, Errno> {
let mut open_flags = 0;
if flags.contains(OpenFlags::O_DIRECTORY) {
if self.kind != VnodeKind::Directory {
return Err(Errno::NotADirectory);
}
if flags & OpenFlags::O_ACCESS != OpenFlags::O_RDONLY {
return Err(Errno::IsADirectory);
}
open_flags = File::READ;
} else {
if self.kind == VnodeKind::Directory {
return Err(Errno::IsADirectory);
}
match flags & OpenFlags::O_ACCESS {
OpenFlags::O_RDONLY => open_flags |= File::READ,
OpenFlags::O_WRONLY => open_flags |= File::WRITE,
OpenFlags::O_RDWR => open_flags |= File::READ | File::WRITE,
_ => unimplemented!(),
}
}
if flags.contains(OpenFlags::O_CLOEXEC) {
open_flags |= File::CLOEXEC;
}
if self.kind == VnodeKind::Directory && self.flags & Vnode::CACHE_READDIR != 0 {
Ok(File::normal(self.clone(), File::POS_CACHE_DOT, open_flags))
} else {
if let Some(ref mut data) = *self.data() {
let pos = data.open(self.clone(), flags)?;
Ok(File::normal(self.clone(), pos, open_flags))
} else {
Err(Errno::NotImplemented)
match flags & OpenFlags::O_ACCESS {
OpenFlags::O_RDONLY => open_flags |= File::READ,
OpenFlags::O_WRONLY => open_flags |= File::WRITE,
OpenFlags::O_RDWR => open_flags |= File::READ | File::WRITE,
_ => unimplemented!(),
}
match &self.data {
VnodeData::Directory(_) => {
if !flags.contains(OpenFlags::O_DIRECTORY) {
return Err(Errno::NotADirectory);
}
if self.flags & Vnode::CACHE_READDIR != 0 {
Ok(File::normal(self.clone(), File::POS_CACHE_DOT, open_flags))
} else {
todo!()
}
},
VnodeData::File(file) => {
if flags.contains(OpenFlags::O_DIRECTORY) {
return Err(Errno::IsADirectory);
}
if let Some(ref mut file) = *file.borrow_mut() {
let pos = file.open(self.clone(), flags)?;
Ok(File::normal(self.clone(), pos, open_flags))
} else {
Err(Errno::NotImplemented)
}
},
VnodeData::Char(_) => {
if flags.contains(OpenFlags::O_DIRECTORY) {
return Err(Errno::IsADirectory);
}
Ok(File::normal(self.clone(), 0, open_flags))
}
}
}
/// Closes a vnode
pub fn close(self: &VnodeRef) -> Result<(), Errno> {
if self.kind == VnodeKind::Directory && self.flags & Vnode::CACHE_READDIR != 0 {
Ok(())
} else {
if let Some(ref mut data) = *self.data() {
data.close(self.clone())
} else {
Err(Errno::NotImplemented)
}
match &self.data {
VnodeData::Directory(dir) => {
if let Some(ref mut dir) = *dir.borrow_mut() {
return dir.close(self.clone());
}
},
VnodeData::File(file) => {
if let Some(ref mut file) = *file.borrow_mut() {
return file.close(self.clone());
}
},
_ => {}
}
Err(Errno::NotImplemented)
}
/// Reads data from offset `pos` into `buf`
pub fn read(self: &VnodeRef, pos: usize, buf: &mut [u8]) -> Result<usize, Errno> {
if self.kind == VnodeKind::Directory {
return Err(Errno::IsADirectory);
}
if let Some(ref mut data) = *self.data() {
data.read(self.clone(), pos, buf)
} else {
Err(Errno::NotImplemented)
match &self.data {
VnodeData::File(file) => {
file.borrow_mut().as_mut().ok_or(Errno::NotImplemented)?.read(self.clone(), pos, buf)
}
VnodeData::Char(chr) => {
chr.read(true, buf)
}
_ => Err(Errno::InvalidOperation)
}
}
/// Writes data from `buf` to offset `pos`
pub fn write(self: &VnodeRef, pos: usize, buf: &[u8]) -> Result<usize, Errno> {
if self.kind == VnodeKind::Directory {
return Err(Errno::IsADirectory);
}
if let Some(ref mut data) = *self.data() {
data.write(self.clone(), pos, buf)
} else {
Err(Errno::NotImplemented)
match &self.data {
VnodeData::File(file) => {
file.borrow_mut().as_mut().ok_or(Errno::NotImplemented)?.write(self.clone(), pos, buf)
}
VnodeData::Char(chr) => {
chr.write(true, buf)
}
_ => Err(Errno::InvalidOperation)
}
}
/// Resizes the vnode data
pub fn truncate(self: &VnodeRef, size: usize) -> Result<(), Errno> {
if self.kind != VnodeKind::Regular {
return Err(Errno::IsADirectory);
}
if let Some(ref mut data) = *self.data() {
data.truncate(self.clone(), size)
} else {
Err(Errno::NotImplemented)
match &self.data {
VnodeData::File(file) => {
file.borrow_mut().as_mut().ok_or(Errno::NotImplemented)?.truncate(self.clone(), size)
}
_ => Err(Errno::InvalidOperation)
}
}
/// Returns current vnode data size
pub fn size(self: &VnodeRef) -> Result<usize, Errno> {
if let Some(ref mut data) = *self.data() {
data.size(self.clone())
} else {
Err(Errno::NotImplemented)
match &self.data {
VnodeData::File(file) => {
file.borrow_mut().as_mut().ok_or(Errno::NotImplemented)?.size(self.clone())
},
VnodeData::Directory(dir) => {
dir.borrow_mut().as_mut().ok_or(Errno::NotImplemented)?.size(self.clone())
}
VnodeData::Char(_) => {
Ok(0)
}
}
}
@@ -464,30 +511,35 @@ impl Vnode {
Ok(Stat {
blksize: 0,
size: 0,
mode: props.mode
mode: props.mode,
})
} else if let Some(ref mut data) = *self.data() {
data.stat(self.clone())
} else {
Err(Errno::NotImplemented)
match &self.data {
VnodeData::File(file) => file.borrow_mut().as_mut().ok_or(Errno::NotADirectory)?.stat(self.clone()),
VnodeData::Directory(dir) => dir.borrow_mut().as_mut().ok_or(Errno::NotImplemented)?.stat(self.clone()),
// TODO stat() for char/blk devs
_ => todo!()
}
}
}
/// Performs node-specific requests
pub fn ioctl(self: &VnodeRef, cmd: IoctlCmd, ptr: usize, len: usize) -> Result<usize, Errno> {
if let Some(ref mut data) = *self.data() {
data.ioctl(self.clone(), cmd, ptr, len)
} else {
Err(Errno::NotImplemented)
match &self.data {
VnodeData::Char(chr) => {
chr.ioctl(cmd, ptr, len)
},
_ => Err(Errno::InvalidOperation)
}
}
/// Returns `true` if the node is ready for operation
pub fn is_ready(self: &VnodeRef, write: bool) -> Result<bool, Errno> {
if let Some(ref mut data) = *self.data() {
data.is_ready(self.clone(), write)
} else {
Err(Errno::NotImplemented)
pub fn ready(self: &VnodeRef, write: bool) -> Result<bool, Errno> {
match &self.data {
VnodeData::File(file) => {
file.borrow_mut().as_mut().ok_or(Errno::NotImplemented)?.ready(self.clone(), write)
},
_ => Err(Errno::InvalidOperation)
}
}
@@ -500,7 +552,6 @@ impl Vnode {
if access.intersects(AccessMode::R_OK | AccessMode::W_OK | AccessMode::X_OK) {
return Err(Errno::InvalidArgument);
}
return Ok(());
} else {
if access.contains(AccessMode::F_OK) {
return Err(Errno::InvalidArgument);
@@ -519,9 +570,9 @@ impl Vnode {
// TODO check group
// TODO check other
return Ok(());
}
Ok(())
}
}
@@ -538,32 +589,81 @@ mod tests {
use libsys::{ioctl::IoctlCmd, stat::OpenFlags, stat::Stat};
pub struct DummyInode;
#[auto_inode]
impl VnodeImpl for DummyInode {
fn create(
// TODO derive macro for this
impl VnodeCommon for DummyInode {
/// Performs filetype-specific request
fn ioctl(
&mut self,
_at: VnodeRef,
name: &str,
kind: VnodeKind,
) -> Result<VnodeRef, Errno> {
let node = Vnode::new(name, kind, 0);
node.set_data(Box::new(DummyInode {}));
Ok(node)
node: VnodeRef,
cmd: IoctlCmd,
ptr: usize,
len: usize,
) -> Result<usize, Errno> {
todo!()
}
fn remove(&mut self, _at: VnodeRef, _name: &str) -> Result<(), Errno> {
/// Retrieves file status
fn stat(&mut self, node: VnodeRef) -> Result<Stat, Errno> {
todo!()
}
/// Reports the size of this filesystem object in bytes
fn size(&mut self, node: VnodeRef) -> Result<usize, Errno> {
todo!()
}
/// Returns `true` if node is ready for an operation
fn ready(&mut self, node: VnodeRef, write: bool) -> Result<bool, Errno> {
todo!()
}
fn open(&mut self, _node: VnodeRef, _flags: OpenFlags) -> Result<usize, Errno> {
Ok(0)
}
fn close(&mut self, _node: VnodeRef) -> Result<(), Errno> {
Ok(())
}
fn lookup(&mut self, _at: VnodeRef, _name: &str) -> Result<VnodeRef, Errno> {
}
impl VnodeDirectory for DummyInode {
fn create(
&mut self,
at: VnodeRef,
name: &str,
kind: VnodeCreateKind,
) -> Result<VnodeRef, Errno> {
let data = match kind {
VnodeCreateKind::Directory => {
VnodeData::Directory(RefCell::new(Some(Box::new(DummyInode {}))))
}
_ => todo!(),
};
Ok(Vnode::new(name, data, 0))
}
fn remove(&mut self, at: VnodeRef, name: &str) -> Result<(), Errno> {
Ok(())
}
fn lookup(&mut self, at: VnodeRef, name: &str) -> Result<VnodeRef, Errno> {
Err(Errno::DoesNotExist)
}
/// Read directory entries into target buffer
fn readdir(
&mut self,
node: VnodeRef,
pos: usize,
data: &mut [DirectoryEntry],
) -> Result<usize, Errno> {
todo!()
}
}
#[test]
fn test_parent() {
let root = Vnode::new("", VnodeKind::Directory, 0);
let node = Vnode::new("dir0", VnodeKind::Directory, 0);
let root = Vnode::new("", VnodeData::Directory(RefCell::new(None)), 0);
let node = Vnode::new("dir0", VnodeData::Directory(RefCell::new(None)), 0);
root.attach(node.clone());
@@ -573,23 +673,27 @@ mod tests {
#[test]
fn test_mkdir_unlink() {
let root = Vnode::new("", VnodeKind::Directory, 0);
root.set_data(Box::new(DummyInode {}));
let root = Vnode::new(
"",
VnodeData::Directory(RefCell::new(Some(Box::new(DummyInode {})))),
0,
);
let node = root
.create("test", FileMode::default_dir(), VnodeKind::Directory)
.create("test", FileMode::default_dir(), VnodeCreateKind::Directory)
.unwrap();
assert_eq!(
root.create("test", FileMode::default_dir(), VnodeKind::Directory)
root.create("test", FileMode::default_dir(), VnodeCreateKind::Directory)
.unwrap_err(),
Errno::AlreadyExists
);
assert_eq!(node.props.borrow().mode, FileMode::default_dir());
assert!(Rc::ptr_eq(&node, &root.lookup("test").unwrap()));
assert!(node.data.borrow().is_some());
let inner = node.as_directory().unwrap();
assert!(matches!(*inner.borrow_mut(), Some(_)));
// assert!(node.data.borrow().is_some());
root.unlink("test").unwrap();
@@ -598,9 +702,9 @@ mod tests {
#[test]
fn test_lookup_attach_detach() {
let root = Vnode::new("", VnodeKind::Directory, 0);
let dir0 = Vnode::new("dir0", VnodeKind::Directory, 0);
let dir1 = Vnode::new("dir1", VnodeKind::Directory, 0);
let root = Vnode::new("", VnodeData::Directory(RefCell::new(None)), 0);
let dir0 = Vnode::new("dir0", VnodeData::Directory(RefCell::new(None)), 0);
let dir1 = Vnode::new("dir1", VnodeData::Directory(RefCell::new(None)), 0);
root.attach(dir0.clone());
root.attach(dir1.clone());
+2 -1
View File
@@ -18,9 +18,10 @@ tock-registers = "0.7.x"
fdt-rs = { version = "0.x.x", default-features = false }
bitflags = "^1.3.0"
kernel-macros = { path = "macros" }
fs-macros = { path = "../fs/macros" }
[target.'cfg(target_arch = "aarch64")'.dependencies]
cortex-a = { version = "6.x.x" }
cortex-a = { version = "7.0.x" }
[features]
default = ["aggressive_syscall"]
-16
View File
@@ -1,16 +0,0 @@
.macro MOV_L reg, value
mov \reg, #((\value) & 0xFFFF)
movk \reg, #((\value) >> 16), lsl #16
.endm
.macro ADR_REL reg, sym
adrp \reg, \sym
add \reg, \reg, #:lo12:\sym
.endm
.macro ADR_ABS reg, sym
movz \reg, #:abs_g3:\sym
movk \reg, #:abs_g2_nc:\sym
movk \reg, #:abs_g1_nc:\sym
movk \reg, #:abs_g0_nc:\sym
.endm
+17 -13
View File
@@ -4,13 +4,15 @@ use crate::arch::{
aarch64::reg::{CNTKCTL_EL1, CPACR_EL1},
machine,
};
use core::arch::global_asm;
use crate::config::{ConfigKey, CONFIG};
use crate::dev::{
fdt::{find_prop, DeviceTree},
irq::IntSource,
Device,
};
use crate::fs::devfs;
use crate::fs::{devfs, sysfs};
use crate::dev::pseudo;
use libsys::error::Errno;
//use crate::debug::Level;
use crate::mem::{
@@ -23,22 +25,16 @@ use cortex_a::asm::barrier::{self, dsb, isb};
use cortex_a::registers::{SCTLR_EL1, VBAR_EL1};
use tock_registers::interfaces::{ReadWriteable, Writeable};
fn init_device_tree(fdt_base_phys: usize) -> Result<(), Errno> {
fn init_device_tree(fdt_base_phys: usize) -> Result<Option<DeviceTree>, Errno> {
use fdt_rs::prelude::*;
let fdt = if fdt_base_phys != 0 {
DeviceTree::from_phys(fdt_base_phys + 0xFFFFFF8000000000)?
} else {
warnln!("No FDT present");
return Ok(());
return Ok(None);
};
#[cfg(feature = "verbose")]
{
use crate::debug::Level;
fdt.dump(Level::Debug);
}
let mut cfg = CONFIG.lock();
if let Some(chosen) = fdt.node_by_path("/chosen") {
@@ -56,7 +52,7 @@ fn init_device_tree(fdt_base_phys: usize) -> Result<(), Errno> {
}
}
Ok(())
Ok(Some(fdt))
}
#[no_mangle]
@@ -87,7 +83,7 @@ extern "C" fn __aa64_bsp_main(fdt_base: usize) -> ! {
// Enable MMU
virt::enable().expect("Failed to initialize virtual memory");
init_device_tree(fdt_base).expect("Device tree init failed");
let _fdt = init_device_tree(fdt_base).expect("Device tree init failed");
// Most basic machine init: initialize proper debug output
// physical memory
@@ -102,9 +98,19 @@ extern "C" fn __aa64_bsp_main(fdt_base: usize) -> ! {
}
devfs::init();
sysfs::init();
machine::init_board().unwrap();
#[cfg(feature = "verbose")]
if let Some(fdt) = fdt {
use crate::debug::Level;
fdt.dump(Level::Debug);
}
devfs::add_named_char_device(&pseudo::ZERO, "zero").unwrap();
devfs::add_named_char_device(&pseudo::RANDOM, "random").unwrap();
infoln!("Machine init finished");
unsafe {
@@ -115,6 +121,4 @@ extern "C" fn __aa64_bsp_main(fdt_base: usize) -> ! {
}
}
global_asm!(include_str!("macros.S"));
global_asm!(include_str!("uboot.S"));
global_asm!(include_str!("upper.S"));
+111
View File
@@ -1,5 +1,54 @@
// vi:ft=a64asm.asm:
.macro MOV_L reg, value
mov \reg, #((\value) & 0xFFFF)
movk \reg, #((\value) >> 16), lsl #16
.endm
.macro ADR_REL reg, sym
adrp \reg, \sym
add \reg, \reg, #:lo12:\sym
.endm
.macro ADR_ABS reg, sym
movz \reg, #:abs_g3:\sym
movk \reg, #:abs_g2_nc:\sym
movk \reg, #:abs_g1_nc:\sym
movk \reg, #:abs_g0_nc:\sym
.endm
.set PTE_BLOCK_AF, 1 << 10
.set PTE_BLOCK_ISH, 3 << 8
.set PTE_PRESENT, 1 << 0
.set MAIR_EL1_Attr0_Normal_Inner_NC, (4 << 0)
.set MAIR_EL1_Attr0_Normal_Outer_NC, (4 << 4)
.set MAIR_EL1_Attr1_Device, (0 << 12)
.set MAIR_EL1_Attr1_Device_nGnRE, (1 << 8)
.set ID_AA64MMFR0_EL1_TGran4, (0xF << 28)
.set TCR_EL1_IPS_SHIFT, 32
.set TCR_EL1_TG1_4K, (2 << 30)
.set TCR_EL1_SH1_Outer, (2 << 28)
.set TCR_EL1_ORGN1_NC, (0 << 26)
.set TCR_EL1_IRGN1_NC, (0 << 24)
.set TCR_EL1_T1SZ_SHIFT, 16
.set TCR_EL1_TG0_4K, (0 << 14)
.set TCR_EL1_SH0_Outer, (2 << 12)
.set TCR_EL1_ORGN0_NC, (0 << 10)
.set TCR_EL1_IRGN0_NC, (0 << 8)
.set TCR_EL1_T0SZ_SHIFT, 0
.set TCR_EL1_ATTRS, (TCR_EL1_TG1_4K | TCR_EL1_SH1_Outer | TCR_EL1_TG0_4K | TCR_EL1_SH0_Outer | (25 << TCR_EL1_T1SZ_SHIFT) | (25 << TCR_EL1_T0SZ_SHIFT))
.set SCTLR_EL1_I, (1 << 12)
.set SCTLR_EL1_C, (1 << 2)
.set SCTLR_EL1_M, (1 << 0)
.set SCTLR_EL2_RES1, 0x30C50830
.set SPSR_EL2_EL1h, 0x5
@@ -61,6 +110,68 @@ _entry:
ADR_ABS x9, __aa64_entry_upper
b __aa64_enter_upper
.global __aa64_enter_upper
.type __aa64_enter_upper, %function
__aa64_enter_upper:
// x8 -- FDT base
// x9 -- upper entry point
// Setup TTBR1_EL1
// TODO fix macros
ADR_ABS x5, KERNEL_TTBR1
ADR_ABS x6, KERNEL_OFFSET
// x5 = KERNEL_TTBR1 physical address
sub x5, x5, x6
// Fill KERNEL_TTBR1 table with upper-mapped Normal memory
.fill_ttbr1:
mov x2, #256
1:
sub x2, x2, #1
// x0 = (x2 << 30) | attrs...
lsl x1, x2, #30
mov x0, #(PTE_BLOCK_ISH | PTE_BLOCK_AF | PTE_PRESENT)
orr x0, x0, x1
str x0, [x5, x2, lsl #3]
cbnz x2, 1b
.init_mmu_regs:
mov x0, #(MAIR_EL1_Attr0_Normal_Outer_NC | MAIR_EL1_Attr0_Normal_Inner_NC | MAIR_EL1_Attr1_Device | MAIR_EL1_Attr1_Device_nGnRE)
msr mair_el1, x0
// Test for 4KiB page support
mrs x0, ID_AA64MMFR0_EL1
mov x1, ID_AA64MMFR0_EL1_TGran4
tst x0, x1
bne .no_4k_gran
// x0 = PARange
and x0, x0, #0xF
lsl x0, x0, #TCR_EL1_IPS_SHIFT
MOV_L x1, TCR_EL1_ATTRS
orr x0, x0, x1
msr tcr_el1, x0
msr ttbr0_el1, x5
msr ttbr1_el1, x5
dsb ish
isb
mrs x0, sctlr_el1
orr x0, x0, #SCTLR_EL1_M
msr sctlr_el1, x0
mov x0, x8
br x9
.no_4k_gran:
b .
.size __aa64_enter_upper, . - __aa64_enter_upper
.section .text._entry_upper
__aa64_entry_upper:
// x0 -- fdt address
-95
View File
@@ -1,95 +0,0 @@
// vi:ft=a64asm:
.set PTE_BLOCK_AF, 1 << 10
.set PTE_BLOCK_ISH, 3 << 8
.set PTE_PRESENT, 1 << 0
.set MAIR_EL1_Attr0_Normal_Inner_NC, (4 << 0)
.set MAIR_EL1_Attr0_Normal_Outer_NC, (4 << 4)
.set MAIR_EL1_Attr1_Device, (0 << 12)
.set MAIR_EL1_Attr1_Device_nGnRE, (1 << 8)
.set ID_AA64MMFR0_EL1_TGran4, (0xF << 28)
.set TCR_EL1_IPS_SHIFT, 32
.set TCR_EL1_TG1_4K, (2 << 30)
.set TCR_EL1_SH1_Outer, (2 << 28)
.set TCR_EL1_ORGN1_NC, (0 << 26)
.set TCR_EL1_IRGN1_NC, (0 << 24)
.set TCR_EL1_T1SZ_SHIFT, 16
.set TCR_EL1_TG0_4K, (0 << 14)
.set TCR_EL1_SH0_Outer, (2 << 12)
.set TCR_EL1_ORGN0_NC, (0 << 10)
.set TCR_EL1_IRGN0_NC, (0 << 8)
.set TCR_EL1_T0SZ_SHIFT, 0
.set TCR_EL1_ATTRS, (TCR_EL1_TG1_4K | TCR_EL1_SH1_Outer | TCR_EL1_TG0_4K | TCR_EL1_SH0_Outer | (25 << TCR_EL1_T1SZ_SHIFT) | (25 << TCR_EL1_T0SZ_SHIFT))
.set SCTLR_EL1_I, (1 << 12)
.set SCTLR_EL1_C, (1 << 2)
.set SCTLR_EL1_M, (1 << 0)
.section .text._entry
.global __aa64_enter_upper
.type __aa64_enter_upper, %function
__aa64_enter_upper:
// x8 -- FDT base
// x9 -- upper entry point
// Setup TTBR1_EL1
// TODO fix macros
ADR_ABS x5, KERNEL_TTBR1
ADR_ABS x6, KERNEL_OFFSET
// x5 = KERNEL_TTBR1 physical address
sub x5, x5, x6
// Fill KERNEL_TTBR1 table with upper-mapped Normal memory
.fill_ttbr1:
mov x2, #256
1:
sub x2, x2, #1
// x0 = (x2 << 30) | attrs...
lsl x1, x2, #30
mov x0, #(PTE_BLOCK_ISH | PTE_BLOCK_AF | PTE_PRESENT)
orr x0, x0, x1
str x0, [x5, x2, lsl #3]
cbnz x2, 1b
.init_mmu_regs:
mov x0, #(MAIR_EL1_Attr0_Normal_Outer_NC | MAIR_EL1_Attr0_Normal_Inner_NC | MAIR_EL1_Attr1_Device | MAIR_EL1_Attr1_Device_nGnRE)
msr mair_el1, x0
// Test for 4KiB page support
mrs x0, ID_AA64MMFR0_EL1
mov x1, ID_AA64MMFR0_EL1_TGran4
tst x0, x1
bne .no_4k_gran
// x0 = PARange
and x0, x0, #0xF
lsl x0, x0, #TCR_EL1_IPS_SHIFT
MOV_L x1, TCR_EL1_ATTRS
orr x0, x0, x1
msr tcr_el1, x0
msr ttbr0_el1, x5
msr ttbr1_el1, x5
dsb ish
isb
mrs x0, sctlr_el1
orr x0, x0, #SCTLR_EL1_M
msr sctlr_el1, x0
mov x0, x8
br x9
.no_4k_gran:
b .
.size __aa64_enter_upper, . - __aa64_enter_upper
+1
View File
@@ -6,6 +6,7 @@ use crate::mem::{
phys::{self, PageUsage},
};
use core::mem::size_of;
use core::arch::global_asm;
struct Stack {
bp: usize,
+15 -15
View File
@@ -1,14 +1,15 @@
//! AArch64 exception handling
use crate::arch::machine;
use crate::arch::{machine, intrin};
use crate::debug::Level;
use crate::dev::irq::{IntController, IrqContext};
use crate::mem;
use crate::proc::{sched, Thread};
use crate::syscall;
use cortex_a::registers::{ESR_EL1, FAR_EL1};
use libsys::{abi::SystemCall, signal::Signal};
use libsys::{abi::SystemCall, signal::Signal, error::Errno};
use tock_registers::interfaces::Readable;
use core::arch::global_asm;
/// Trapped SIMD/FP functionality
pub const EC_FP_TRAP: u64 = 0b000111;
@@ -93,24 +94,23 @@ extern "C" fn __aa64_exc_sync_handler(exc: &mut ExceptionFrame) {
if iss & (1 << 6) != 0 && far < mem::KERNEL_OFFSET && sched::is_ready() {
let thread = Thread::current();
let proc = thread.owner().unwrap();
let asid = proc.asid();
if proc
.manipulate_space(|space| space.try_cow_copy(far))
.is_err()
{
let res = proc.manipulate_space(|space| {
space.try_cow_copy(far)?;
unsafe {
intrin::flush_tlb_asid(asid);
}
Result::<(), Errno>::Ok(())
});
if res.is_err() {
// Kill program
errorln!("Data abort from {:#x}", exc.elr_el1);
dump_data_abort(Level::Error, esr, far as u64);
proc.enter_fault_signal(thread, Signal::SegmentationFault);
}
unsafe {
use cortex_a::registers::TTBR0_EL1;
let ttbr = TTBR0_EL1.get() as usize;
let asid = (ttbr >> 48) & 0xFF;
asm!("tlbi aside1, {}", in(reg) (asid << 48));
}
return;
}
@@ -127,7 +127,7 @@ extern "C" fn __aa64_exc_sync_handler(exc: &mut ExceptionFrame) {
if num == SystemCall::Fork {
match unsafe { syscall::sys_fork(exc) } {
Ok(pid) => exc.x[0] = pid.value() as usize,
Ok(pid) => exc.x[0] = u32::from(pid) as usize,
Err(err) => {
exc.x[0] = err.to_negative_isize() as usize;
}
@@ -150,7 +150,7 @@ extern "C" fn __aa64_exc_sync_handler(exc: &mut ExceptionFrame) {
if sched::is_ready() {
let thread = Thread::current();
errorln!(
"Unhandled exception in thread {}, {:?}",
"Unhandled exception in thread {:?}, {:?}",
thread.id(),
thread.owner().map(|e| e.id())
);
+34
View File
@@ -0,0 +1,34 @@
//! AArch64-specific assembly functions
use core::arch::asm;
/// Disables delievery of IRQs
///
/// # Safety
///
/// Unsafe: requires EL0
#[inline(always)]
pub unsafe fn irq_disable() {
asm!("msr daifset, {bits}", bits = const 2, options(nomem, nostack, preserves_flags));
}
/// Discards an entry related to `addr` from TLB cache
///
/// # Safety
///
/// Unsafe: requires EL0
#[inline(always)]
pub unsafe fn flush_tlb_virt(_addr: usize) {
todo!()
}
/// Discards all entries related to `asid` from TLB cache
///
/// # Safety
///
/// Only safe to use for known [Process]es and their ASIDs
// TODO non-portable
#[inline(always)]
pub unsafe fn flush_tlb_asid(asid: usize) {
asm!("tlbi aside1, {}", in(reg) asid);
}
@@ -96,4 +96,4 @@ static UART0: Uart = unsafe { Uart::new(UART0_BASE, IrqNumber::new(32)) };
static LOCAL_TIMER: GenericTimer = GenericTimer::new(LOCAL_TIMER_IRQ);
pub(super) static GPIO: Gpio = unsafe { Gpio::new(PIO_BASE) };
static RTC: Rtc = unsafe { Rtc::new(RTC_BASE, RTC_IRQ) };
static GIC: Gic = unsafe { Gic::new(GICD_BASE, GICC_BASE, LOCAL_TIMER_IRQ) };
static GIC: Gic = unsafe { Gic::new(GICD_BASE, GICC_BASE) };
@@ -1,5 +1,6 @@
use crate::dev::Device;
use crate::mem::virt::DeviceMemoryIo;
use crate::arch::intrin;
use crate::sync::IrqSafeSpinLock;
use crate::util::InitOnce;
use libsys::error::Errno;
@@ -72,7 +73,7 @@ impl RWdog {
regs.CTRL.write(CTRL::KEY::Value + CTRL::RESTART::SET);
loop {
asm!("wfe");
intrin::hang();
}
}
+2
View File
@@ -4,6 +4,7 @@ use crate::dev::{
serial::{pl011::Pl011, SerialDevice},
Device,
};
use crate::fs::devfs::{self, CharDeviceType};
use crate::mem::phys;
use libsys::error::Errno;
@@ -37,6 +38,7 @@ pub fn init_board() -> Result<(), Errno> {
unsafe {
IRQCHIP.enable()?;
UART.init_irqs()?;
devfs::add_char_device(&UART, CharDeviceType::TtySerial)?;
EMMC.enable()?;
}
+2 -1
View File
@@ -6,6 +6,7 @@ use tock_registers::interfaces::{Readable, Writeable};
pub mod boot;
pub mod context;
pub mod exception;
pub mod intrin;
pub mod irq;
pub mod reg;
pub mod timer;
@@ -34,7 +35,7 @@ cfg_if! {
#[inline(always)]
pub unsafe fn irq_mask_save() -> u64 {
let state = DAIF.get();
asm!("msr daifset, {bits}", bits = const 2, options(nomem, nostack, preserves_flags));
intrin::irq_disable();
state
}
@@ -1,48 +0,0 @@
//! CNTKCTL_EL1 register
#![allow(missing_docs)]
use tock_registers::{
interfaces::{Readable, Writeable},
register_bitfields,
};
register_bitfields! {
u64,
/// Counter-timer Kernel Control Register
pub CNTKCTL_EL1 [
/// If set, disables CNTPCT and CNTFRQ trapping from EL0
EL0PCTEN OFFSET(0) NUMBITS(1) []
]
}
/// CNTKCTL_EL1 register
pub struct Reg;
impl Readable for Reg {
type T = u64;
type R = CNTKCTL_EL1::Register;
#[inline(always)]
fn get(&self) -> Self::T {
let mut tmp;
unsafe {
asm!("mrs {}, cntkctl_el1", out(reg) tmp);
}
tmp
}
}
impl Writeable for Reg {
type T = u64;
type R = CNTKCTL_EL1::Register;
#[inline(always)]
fn set(&self, value: Self::T) {
unsafe {
asm!("msr cntkctl_el1, {}", in(reg) value);
}
}
}
/// CNTKCTL_EL1 register
pub const CNTKCTL_EL1: Reg = Reg;
-57
View File
@@ -1,57 +0,0 @@
//! CPACR_EL1 register
#![allow(missing_docs)]
use tock_registers::{
interfaces::{Readable, Writeable},
register_bitfields,
};
register_bitfields! {
u64,
/// EL1 Architectural Feature Access Control Register
pub CPACR_EL1 [
/// Enable EL0 and EL1 SIMD/FP accesses to EL1
FPEN OFFSET(20) NUMBITS(2) [
/// Trap both EL0 and EL1
TrapAll = 0,
/// Trap EL0
TrapEl0 = 1,
/// Trap EL1
TrapEl1 = 2,
/// Do not trap any SIMD/FP instructions
TrapNone = 3
]
]
}
/// CPACR_EL1 register
pub struct Reg;
impl Readable for Reg {
type T = u64;
type R = CPACR_EL1::Register;
#[inline(always)]
fn get(&self) -> Self::T {
let mut tmp;
unsafe {
asm!("mrs {}, cpacr_el1", out(reg) tmp);
}
tmp
}
}
impl Writeable for Reg {
type T = u64;
type R = CPACR_EL1::Register;
#[inline(always)]
fn set(&self, value: Self::T) {
unsafe {
asm!("msr cpacr_el1, {}", in(reg) value);
}
}
}
/// CPACR_EL1 register
pub const CPACR_EL1: Reg = Reg;
+65 -4
View File
@@ -1,7 +1,68 @@
//! AArch64 architectural registers
pub mod cpacr_el1;
pub use cpacr_el1::CPACR_EL1;
use core::arch::asm;
use tock_registers::{
interfaces::{Readable, Writeable},
register_bitfields,
};
pub mod cntkctl_el1;
pub use cntkctl_el1::CNTKCTL_EL1;
macro_rules! wrap_msr {
($struct_name:ident, $name:ident, $reg:literal, $fields:tt) => {
#[allow(missing_docs)]
pub struct $struct_name;
register_bitfields! {
u64,
#[allow(missing_docs)]
pub $name $fields
}
impl Readable for $struct_name {
type T = u64;
type R = $name::Register;
#[inline(always)]
fn get(&self) -> Self::T {
let mut value;
unsafe {
asm!(concat!("mrs {}, ", $reg), out(reg) value)
}
value
}
}
impl Writeable for $struct_name {
type T = u64;
type R = $name::Register;
#[inline(always)]
fn set(&self, value: Self::T) {
unsafe {
asm!(concat!("msr ", $reg, ", {}"), in(reg) value);
}
}
}
#[allow(missing_docs)]
pub const $name: $struct_name = $struct_name;
};
}
wrap_msr!(CpacrEl1, CPACR_EL1, "cpacr_el1", [
/// Enable EL0 and EL1 SIMD/FP accesses to EL1
FPEN OFFSET(20) NUMBITS(2) [
/// Trap both EL0 and EL1
TrapAll = 0,
/// Trap EL0
TrapEl0 = 1,
/// Trap EL1
TrapEl1 = 2,
/// Do not trap any SIMD/FP instructions
TrapNone = 3
]
]);
wrap_msr!(CntkctlEl1, CNTKCTL_EL1, "cntkctl_el1", [
/// If set, disables CNTPCT and CNTFRQ trapping from EL0
EL0PCTEN OFFSET(0) NUMBITS(1) []
]);
+8 -4
View File
@@ -1,7 +1,9 @@
//! ARM generic timer implementation
use crate::arch::machine::{self, IrqNumber};
use crate::proc;
use crate::dev::{
pseudo,
irq::{IntController, IntSource},
timer::TimestampSource,
Device,
@@ -34,9 +36,9 @@ impl IntSource for GenericTimer {
fn handle_irq(&self) -> Result<(), Errno> {
CNTP_TVAL_EL0.set(TIMER_TICK);
CNTP_CTL_EL0.write(CNTP_CTL_EL0::ENABLE::SET);
use crate::proc;
proc::wait::tick();
proc::switch();
pseudo::RANDOM.set_state(CNTPCT_EL0.get() as u32);
Ok(())
}
@@ -50,9 +52,11 @@ impl IntSource for GenericTimer {
impl TimestampSource for GenericTimer {
fn timestamp(&self) -> Result<Duration, Errno> {
let cnt = CNTPCT_EL0.get() * 1_000_000_000;
let frq = CNTFRQ_EL0.get();
Ok(Duration::from_nanos(cnt / frq))
let cnt = (CNTPCT_EL0.get() as u128) * 1_000_000_000u128;
let frq = CNTFRQ_EL0.get() as u128;
let secs = ((cnt / frq) / 1_000_000_000) as u64;
let nanos = ((cnt / frq) % 1_000_000_000) as u32;
Ok(Duration::new(secs, nanos))
}
}
+2
View File
@@ -15,6 +15,8 @@ cfg_if! {
pub use aarch64 as platform;
pub use aarch64::machine;
pub use aarch64::intrin;
}
}
+33 -11
View File
@@ -12,20 +12,40 @@
//! * [errorln!]
use crate::dev::serial::SerialDevice;
use libsys::debug::TraceLevel;
use libsys::{debug::TraceLevel, error::Errno};
use core::convert::TryFrom;
use core::fmt;
/// Currently active print level
pub static LEVEL: Level = Level::Debug;
/// Kernel logging levels
#[derive(Clone, Copy, PartialEq)]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)]
#[repr(u32)]
pub enum Level {
/// Debugging information
Debug,
Debug = 1,
/// General informational messages
Info,
Info = 2,
/// Non-critical warnings
Warn,
Warn = 3,
/// Critical errors
Error,
Error = 4,
}
impl TryFrom<u32> for Level {
type Error = Errno;
#[inline(always)]
fn try_from(l: u32) -> Result<Level, Errno> {
match l {
1 => Ok(Level::Debug),
2 => Ok(Level::Info),
3 => Ok(Level::Warn),
4 => Ok(Level::Error),
_ => Err(Errno::InvalidArgument)
}
}
}
impl From<TraceLevel> for Level {
@@ -114,13 +134,15 @@ macro_rules! errorln {
}
#[doc(hidden)]
pub fn _debug(_level: Level, args: fmt::Arguments) {
pub fn _debug(level: Level, args: fmt::Arguments) {
use crate::arch::machine;
use fmt::Write;
SerialOutput {
inner: machine::console(),
if level >= LEVEL {
SerialOutput {
inner: machine::console(),
}
.write_fmt(args)
.ok();
}
.write_fmt(args)
.ok();
}
+1
View File
@@ -11,6 +11,7 @@ pub mod rtc;
pub mod sd;
pub mod serial;
pub mod timer;
pub mod pseudo;
pub mod tty;
/// Generic device trait
+96
View File
@@ -0,0 +1,96 @@
//! Virtual (pseudo) device implemetation
use crate::dev::Device;
use core::sync::atomic::{AtomicU32, Ordering};
use libsys::{error::Errno, ioctl::IoctlCmd};
use vfs::CharDevice;
/// Pseudorandom number generator device
pub struct Random {
state: AtomicU32,
}
/// Zero device
pub struct Zero;
impl Device for Random {
fn name(&self) -> &'static str {
"Pseudo-random device"
}
unsafe fn enable(&self) -> Result<(), Errno> {
Ok(())
}
}
impl CharDevice for Random {
fn read(&self, _blocking: bool, data: &mut [u8]) -> Result<usize, Errno> {
for byte in data.iter_mut() {
*byte = self.read_single() as u8;
}
Ok(data.len())
}
fn write(&self, _blocking: bool, _data: &[u8]) -> Result<usize, Errno> {
Ok(0)
}
fn is_ready(&self, _write: bool) -> Result<bool, Errno> {
Ok(true)
}
fn ioctl(&self, _cmd: IoctlCmd, _ptr: usize, _lim: usize) -> Result<usize, Errno> {
Err(Errno::InvalidArgument)
}
}
impl Device for Zero {
fn name(&self) -> &'static str {
"Zero device"
}
unsafe fn enable(&self) -> Result<(), Errno> {
Ok(())
}
}
impl CharDevice for Zero {
fn read(&self, _blocking: bool, data: &mut [u8]) -> Result<usize, Errno> {
data.fill(0);
Ok(data.len())
}
fn write(&self, _blocking: bool, _data: &[u8]) -> Result<usize, Errno> {
Ok(0)
}
fn is_ready(&self, _write: bool) -> Result<bool, Errno> {
Ok(true)
}
fn ioctl(&self, _cmd: IoctlCmd, _ptr: usize, _lim: usize) -> Result<usize, Errno> {
Err(Errno::InvalidArgument)
}
}
impl Random {
/// Initializes PRNG with a seed value
pub fn set_state(&self, state: u32) {
self.state.store(state, Ordering::Release);
}
/// Returns a single pseudo-random value
pub fn read_single(&self) -> u32 {
let mut x = self.state.load(Ordering::Acquire);
x ^= x << 13;
x ^= x >> 7;
x ^= x << 17;
self.state.store(x, Ordering::Release);
x
}
}
/// Pseudorandom number generator device
pub static RANDOM: Random = Random {
state: AtomicU32::new(0),
};
/// Zero device
pub static ZERO: Zero = Zero;
+1 -1
View File
@@ -60,7 +60,7 @@ pub trait TtyDevice<const N: usize>: SerialDevice {
},
IoctlCmd::TtySetPgrp => {
let src = arg::struct_ref::<u32>(ptr)?;
self.ring().inner.lock().fg_pgid = Some(unsafe { Pid::from_raw(*src) });
self.ring().inner.lock().fg_pgid = Some(Pid::try_from(*src)?);
Ok(0)
},
_ => Err(Errno::InvalidArgument)
+16 -8
View File
@@ -1,9 +1,10 @@
//! Device list pseudo-filesystem
use crate::util::InitOnce;
use alloc::boxed::Box;
use core::cell::RefCell;
use vfs::CharDevice;
use core::sync::atomic::{AtomicUsize, Ordering};
use libsys::{stat::FileMode, error::Errno};
use vfs::{CharDevice, CharDeviceWrapper, Vnode, VnodeKind, VnodeRef};
use libsys::{error::Errno, stat::FileMode};
use vfs::{Vnode, VnodeData, VnodeRef};
/// Possible character device kinds
#[derive(Debug)]
@@ -16,7 +17,12 @@ static DEVFS_ROOT: InitOnce<VnodeRef> = InitOnce::new();
/// Initializes devfs
pub fn init() {
let node = Vnode::new("", VnodeKind::Directory, Vnode::CACHE_READDIR | Vnode::CACHE_STAT);
let node = Vnode::new(
"",
VnodeData::Directory(RefCell::new(None)),
Vnode::CACHE_READDIR | Vnode::CACHE_STAT,
);
// let node = Vnode::new("", VnodeKind::Directory, Vnode::CACHE_READDIR | Vnode::CACHE_STAT);
node.props_mut().mode = FileMode::default_dir();
DEVFS_ROOT.init(node);
}
@@ -26,12 +32,14 @@ pub fn root() -> &'static VnodeRef {
DEVFS_ROOT.get()
}
fn _add_char_device(dev: &'static dyn CharDevice, name: &str) -> Result<(), Errno> {
/// Adds device `dev` to devfs with `name`
pub fn add_named_char_device(dev: &'static dyn CharDevice, name: &str) -> Result<(), Errno> {
infoln!("Add char device: {}", name);
let node = Vnode::new(name, VnodeKind::Char, Vnode::CACHE_STAT);
let node = Vnode::new(name, VnodeData::Char(dev), Vnode::CACHE_STAT);
// let node = Vnode::new(name, VnodeKind::Char, Vnode::CACHE_STAT);
// node.set_data(Box::new(CharDeviceWrapper::new(dev)));
node.props_mut().mode = FileMode::from_bits(0o600).unwrap() | FileMode::S_IFCHR;
node.set_data(Box::new(CharDeviceWrapper::new(dev)));
DEVFS_ROOT.get().attach(node);
@@ -56,5 +64,5 @@ pub fn add_char_device(dev: &'static dyn CharDevice, kind: CharDeviceType) -> Re
let name = core::str::from_utf8(&buf[..=prefix.len()]).map_err(|_| Errno::InvalidArgument)?;
_add_char_device(dev, name)
add_named_char_device(dev, name)
}
+5 -4
View File
@@ -8,6 +8,7 @@ use vfs::VnodeRef;
use memfs::BlockAllocator;
pub mod devfs;
pub mod sysfs;
/// Allocator implementation for memfs
#[derive(Clone, Copy)]
@@ -32,9 +33,9 @@ unsafe impl BlockAllocator for MemfsBlockAlloc {
pub fn create_filesystem(options: &MountOptions) -> Result<VnodeRef, Errno> {
let fs_name = options.fs.unwrap();
if fs_name == "devfs" {
Ok(devfs::root().clone())
} else {
todo!();
match fs_name {
"devfs" => Ok(devfs::root().clone()),
"sysfs" => Ok(sysfs::root().clone()),
_ => todo!()
}
}
+232
View File
@@ -0,0 +1,232 @@
//! System control/info virtual filesystem
use crate::debug::{self, Level};
use crate::util::InitOnce;
use alloc::boxed::Box;
use core::cell::RefCell;
use core::fmt::{self, Write};
use core::str::FromStr;
use libsys::{
error::Errno,
ioctl::IoctlCmd,
stat::{FileMode, OpenFlags, Stat},
};
use vfs::{Vnode, VnodeCommon, VnodeData, VnodeFile, VnodeRef};
struct NodeData<R: Fn(&mut [u8]) -> Result<usize, Errno>, W: Fn(&[u8]) -> Result<usize, Errno>> {
read_func: R,
write_func: W,
}
struct BufferWriter<'a> {
dst: &'a mut [u8],
pos: usize,
}
impl<'a> fmt::Write for BufferWriter<'a> {
fn write_str(&mut self, s: &str) -> fmt::Result {
for byte in s.bytes() {
if self.pos == self.dst.len() {
todo!();
}
self.dst[self.pos] = byte;
self.pos += 1;
}
Ok(())
}
}
impl<'a> BufferWriter<'a> {
pub const fn new(dst: &'a mut [u8]) -> Self {
Self { dst, pos: 0 }
}
pub const fn count(&self) -> usize {
self.pos
}
}
impl<R: Fn(&mut [u8]) -> Result<usize, Errno>, W: Fn(&[u8]) -> Result<usize, Errno>> VnodeCommon
for NodeData<R, W>
{
fn open(&mut self, _node: VnodeRef, _mode: OpenFlags) -> Result<usize, Errno> {
Ok(0)
}
fn close(&mut self, _node: VnodeRef) -> Result<(), Errno> {
Ok(())
}
/// Performs filetype-specific request
fn ioctl(
&mut self,
_node: VnodeRef,
_cmd: IoctlCmd,
_ptr: usize,
_len: usize,
) -> Result<usize, Errno> {
todo!()
}
/// Retrieves file status
fn stat(&mut self, _node: VnodeRef) -> Result<Stat, Errno> {
todo!()
}
/// Reports the size of this filesystem object in bytes
fn size(&mut self, _node: VnodeRef) -> Result<usize, Errno> {
todo!()
}
/// Returns `true` if node is ready for an operation
fn ready(&mut self, _node: VnodeRef, _write: bool) -> Result<bool, Errno> {
todo!()
}
}
impl<R: Fn(&mut [u8]) -> Result<usize, Errno>, W: Fn(&[u8]) -> Result<usize, Errno>> VnodeFile
for NodeData<R, W>
{
fn read(&mut self, _node: VnodeRef, pos: usize, data: &mut [u8]) -> Result<usize, Errno> {
if pos != 0 {
// TODO handle this
Ok(0)
} else {
(self.read_func)(data)
}
}
fn write(&mut self, _node: VnodeRef, pos: usize, data: &[u8]) -> Result<usize, Errno> {
if pos != 0 {
todo!();
}
(self.write_func)(data)
}
fn truncate(&mut self, _node: VnodeRef, _size: usize) -> Result<(), Errno> {
todo!()
}
}
impl<R: Fn(&mut [u8]) -> Result<usize, Errno>, W: Fn(&[u8]) -> Result<usize, Errno>>
NodeData<R, W>
{
pub const fn new(read_func: R, write_func: W) -> Self {
Self {
read_func,
write_func,
}
}
}
static SYSFS_ROOT: InitOnce<VnodeRef> = InitOnce::new();
// TODO subdirs
fn add_generic_node<R, W>(parent: Option<VnodeRef>, name: &str, mode: FileMode, read: R, write: W)
where
R: Fn(&mut [u8]) -> Result<usize, Errno> + 'static,
W: Fn(&[u8]) -> Result<usize, Errno> + 'static,
{
let node = Vnode::new(
name,
VnodeData::File(RefCell::new(Some(Box::new(NodeData::new(read, write))))),
Vnode::CACHE_STAT,
);
node.props_mut().mode = mode | FileMode::S_IFREG;
if let Some(parent) = parent {
parent.attach(node);
} else {
SYSFS_ROOT.get().attach(node);
}
}
/// Adds a node with and `read` and `write` operations
pub fn add_read_write_node<R, W>(parent: Option<VnodeRef>, name: &str, read: R, write: W)
where
R: Fn(&mut [u8]) -> Result<usize, Errno> + 'static,
W: Fn(&[u8]) -> Result<usize, Errno> + 'static,
{
add_generic_node(
parent,
name,
FileMode::from_bits(0o600).unwrap(),
read,
write,
)
}
/// Adds `read`-only node
pub fn add_read_node<R>(parent: Option<VnodeRef>, name: &str, read: R)
where
R: Fn(&mut [u8]) -> Result<usize, Errno> + 'static,
{
add_generic_node(
parent,
name,
FileMode::from_bits(0o400).unwrap(),
read,
|_| Err(Errno::ReadOnly),
)
}
/// Creates a directory in sysfs structure
pub fn add_directory(parent: Option<VnodeRef>, name: &str) -> Result<VnodeRef, Errno> {
let node = Vnode::new(
name,
VnodeData::Directory(RefCell::new(None)),
Vnode::CACHE_READDIR | Vnode::CACHE_STAT,
);
node.props_mut().mode = FileMode::from_bits(0o500).unwrap() | FileMode::S_IFDIR;
if let Some(parent) = parent {
parent.attach(node.clone());
} else {
SYSFS_ROOT.get().attach(node.clone());
}
Ok(node)
}
/// Returns sysfs root node reference
pub fn root() -> &'static VnodeRef {
SYSFS_ROOT.get()
}
/// Sets up the sysfs tree
pub fn init() {
let node = Vnode::new(
"",
VnodeData::Directory(RefCell::new(None)),
Vnode::CACHE_READDIR | Vnode::CACHE_STAT,
);
node.props_mut().mode = FileMode::default_dir();
SYSFS_ROOT.init(node);
let debug_dir = add_directory(None, "debug").unwrap();
add_read_write_node(
Some(debug_dir),
"level",
|buf| {
let mut writer = BufferWriter::new(buf);
writeln!(&mut writer, "{}", debug::LEVEL as u32).map_err(|_| Errno::InvalidArgument)?;
Ok(writer.count())
},
|buf| {
let s = core::str::from_utf8(buf).map_err(|_| Errno::InvalidArgument)?;
let _value = u32::from_str(s)
.map_err(|_| Errno::InvalidArgument)
.and_then(Level::try_from)?;
todo!()
},
);
add_read_node(None, "uptime", |buf| {
use crate::arch::machine;
use crate::dev::timer::TimestampSource;
let mut writer = BufferWriter::new(buf);
let time = machine::local_timer().timestamp()?;
writeln!(&mut writer, "{} {}", time.as_secs(), time.subsec_nanos())
.map_err(|_| Errno::InvalidArgument)?;
Ok(writer.count())
});
}
+3 -6
View File
@@ -1,18 +1,15 @@
//! osdve5 crate (lol)
#![feature(
asm,
global_asm,
const_for,
const_mut_refs,
const_raw_ptr_deref,
const_fn_fn_ptr_basics,
const_fn_trait_bound,
const_trait_impl,
const_panic,
panic_info_message,
alloc_error_handler,
linked_list_cursors,
const_btree_new
const_btree_new,
asm_const,
)]
#![no_std]
#![no_main]
@@ -43,7 +40,7 @@ pub mod util;
#[panic_handler]
fn panic_handler(pi: &core::panic::PanicInfo) -> ! {
unsafe {
asm!("msr daifset, #2");
arch::intrin::irq_disable();
}
errorln!("Panic: {:?}", pi);
+16
View File
@@ -26,3 +26,19 @@ pub fn kernel_end_phys() -> usize {
}
unsafe { &__kernel_end as *const _ as usize - KERNEL_OFFSET }
}
// TODO cross-platform variant
/// Returns `true` if `virt` address is accessible for requested operation
#[inline(always)]
pub fn is_el0_accessible(virt: usize, write: bool) -> bool {
use core::arch::asm;
let mut res: usize;
unsafe {
if write {
asm!("at s1e0w, {}; mrs {}, par_el1", in(reg) virt, out(reg) res);
} else {
asm!("at s1e0r, {}; mrs {}, par_el1", in(reg) virt, out(reg) res);
}
}
res & 1 == 0
}
+3
View File
@@ -4,6 +4,9 @@ use crate::sync::IrqSafeSpinLock;
use core::mem;
use libsys::{error::Errno, mem::memcpy};
/// # Safety
///
/// Unsafe to implement because of direct memory manipulation
pub unsafe trait Manager {
fn alloc_page(&mut self, pu: PageUsage) -> Result<usize, Errno>;
fn alloc_contiguous_pages(&mut self, pu: PageUsage, count: usize) -> Result<usize, Errno>;
+1
View File
@@ -6,6 +6,7 @@ use crate::mem::{
};
use core::ops::{Index, IndexMut};
use libsys::{error::Errno, mem::memset};
use core::arch::asm;
/// Transparent wrapper structure representing a single
/// translation table entry
+15 -5
View File
@@ -1,7 +1,10 @@
//! Process file descriptors and I/O context
use alloc::collections::BTreeMap;
use libsys::{error::Errno, stat::{FileDescriptor, UserId, GroupId}};
use vfs::{FileRef, Ioctx, VnodeRef, VnodeKind};
use libsys::{
error::Errno,
stat::{FileDescriptor, GroupId, UserId},
};
use vfs::{FileRef, Ioctx, VnodeRef};
/// Process I/O context. Contains file tables, root/cwd info etc.
pub struct ProcessIo {
@@ -24,7 +27,7 @@ impl ProcessIo {
/// Sets controlling terminal for the process
pub fn set_ctty(&mut self, node: VnodeRef) {
assert_eq!(node.kind(), VnodeKind::Char);
// assert_eq!(node.kind(), VnodeKind::Char);
self.ctty = Some(node);
}
@@ -74,7 +77,11 @@ impl ProcessIo {
}
/// Clones a file descriptor into an available slot or, if specified, requested one
pub fn duplicate_file(&mut self, src: FileDescriptor, dst: Option<FileDescriptor>) -> Result<FileDescriptor, Errno> {
pub fn duplicate_file(
&mut self,
src: FileDescriptor,
dst: Option<FileDescriptor>,
) -> Result<FileDescriptor, Errno> {
let file_ref = self.file(src)?;
if let Some(dst) = dst {
let idx = u32::from(dst);
@@ -91,7 +98,10 @@ impl ProcessIo {
/// Returns [File] struct referred to by file descriptor `idx`
pub fn file(&mut self, fd: FileDescriptor) -> Result<FileRef, Errno> {
self.files.get(&u32::from(fd)).cloned().ok_or(Errno::InvalidFile)
self.files
.get(&u32::from(fd))
.cloned()
.ok_or(Errno::InvalidFile)
}
/// Returns [Ioctx] structure reference of this I/O context
+2 -2
View File
@@ -3,7 +3,7 @@
use crate::init;
use crate::sync::IrqSafeSpinLock;
use alloc::collections::BTreeMap;
use libsys::proc::Pid;
use libsys::proc::{Tid, Pid};
pub mod elf;
pub mod thread;
@@ -30,7 +30,7 @@ pub fn switch() {
pub(self) static PROCESSES: IrqSafeSpinLock<BTreeMap<Pid, ProcessRef>> =
IrqSafeSpinLock::new(BTreeMap::new());
pub(self) static THREADS: IrqSafeSpinLock<BTreeMap<u32, ThreadRef>> =
pub(self) static THREADS: IrqSafeSpinLock<BTreeMap<Tid, ThreadRef>> =
IrqSafeSpinLock::new(BTreeMap::new());
/// Sets up initial process and enters it.
+57 -12
View File
@@ -1,12 +1,12 @@
//! Process data and control
use crate::arch::aarch64::exception::ExceptionFrame;
use crate::arch::{aarch64::exception::ExceptionFrame, intrin};
use crate::mem::{
self,
phys::{self, PageUsage},
virt::{MapAttributes, Space},
};
use crate::proc::{
wait::Wait, Context, ProcessIo, Thread, ThreadRef, ThreadState, PROCESSES, SCHED,
wait::Wait, Context, ProcessIo, Thread, ThreadRef, ThreadState, Tid, PROCESSES, SCHED,
};
use crate::sync::IrqSafeSpinLock;
use alloc::{rc::Rc, vec::Vec};
@@ -39,7 +39,7 @@ struct ProcessInner {
ppid: Option<Pid>,
sid: Pid,
exit: Option<ExitCode>,
threads: Vec<u32>,
threads: Vec<Tid>,
}
/// Structure describing an operating system process
@@ -146,6 +146,34 @@ impl Process {
PROCESSES.lock().get(&pid).cloned()
}
fn find1(a: u32) -> Option<usize> {
for i in 0..32 {
if a & (1 << i) != 0 {
return Some(i);
}
}
None
}
/// Handles all pending signals (when returning from aborted syscall)
pub fn handle_pending_signals(&self) {
let mut lock = self.inner.lock();
let ttbr0 = lock.space.as_mut().unwrap().address_phys() | ((lock.id.asid() as usize) << 48);
let main_thread = Thread::get(lock.threads[0]).unwrap();
drop(lock);
loop {
let state = self.signal_state.load(Ordering::Acquire);
if let Some(signal) = Self::find1(state).map(|e| Signal::try_from(e as u32).unwrap()) {
self.signal_state
.fetch_and(!(1 << (signal as u32)), Ordering::Release);
main_thread.clone().enter_signal(signal, ttbr0);
} else {
break;
}
}
}
/// Sets a pending signal for a process
pub fn set_signal(&self, signal: Signal) {
let mut lock = self.inner.lock();
@@ -162,7 +190,8 @@ impl Process {
main_thread.enter_signal(signal, ttbr0);
}
ThreadState::Waiting => {
main_thread.clone().setup_signal(signal, ttbr0);
self.signal_state
.fetch_or(1 << (signal as u32), Ordering::Release);
main_thread.interrupt_wait(true);
}
ThreadState::Ready => {
@@ -180,11 +209,12 @@ impl Process {
pub fn enter_fault_signal(&self, thread: ThreadRef, signal: Signal) {
let mut lock = self.inner.lock();
let ttbr0 = lock.space.as_mut().unwrap().address_phys() | ((lock.id.asid() as usize) << 48);
drop(lock);
thread.enter_signal(signal, ttbr0);
}
/// Crates a new thread in the process
pub fn new_user_thread(&self, entry: usize, stack: usize, arg: usize) -> Result<u32, Errno> {
pub fn new_user_thread(&self, entry: usize, stack: usize, arg: usize) -> Result<Tid, Errno> {
let mut lock = self.inner.lock();
let space_phys = lock.space.as_mut().unwrap().address_phys();
@@ -249,14 +279,18 @@ impl Process {
lock.state = ProcessState::Finished;
for &tid in lock.threads.iter() {
Thread::get(tid).unwrap().terminate(status);
let thread = Thread::get(tid).unwrap();
if thread.state() == ThreadState::Waiting {
todo!()
}
thread.terminate(status);
SCHED.dequeue(tid);
}
if let Some(space) = lock.space.take() {
unsafe {
Space::release(space);
asm!("tlbi aside1, {}", in(reg) ((lock.id.asid() as usize) << 48));
intrin::flush_tlb_asid((lock.id.asid() as usize) << 48);
}
}
@@ -294,7 +328,7 @@ impl Process {
thread.terminate(status);
SCHED.dequeue(tid);
debugln!("Thread {} terminated", tid);
debugln!("Thread {:?} terminated", tid);
switch
};
@@ -410,7 +444,7 @@ impl Process {
let mut data_offset = 0usize;
for arg in argv.iter() {
// XXX this is really unsafe and I am not really sure ABI will stay like this XXX
Self::write_paged(space, base + offset + 0, base + data_offset)?;
Self::write_paged(space, base + offset, base + data_offset)?;
Self::write_paged(space, base + offset + 8, arg.len())?;
offset += 16;
data_offset += arg.len();
@@ -428,6 +462,18 @@ impl Process {
Ok(base + offset)
}
/// Returns the process's address space ID
pub fn asid(&self) -> usize {
(self.id().asid() as usize) << 48
}
/// Flushes TLB cache for the process address space
pub fn invalidate_tlb(&self) {
unsafe {
intrin::flush_tlb_asid(self.asid());
}
}
/// Loads a new program into current process address space
pub fn execve<F: FnOnce(&mut Space) -> Result<usize, Errno>>(
loader: F,
@@ -435,7 +481,7 @@ impl Process {
) -> Result<(), Errno> {
unsafe {
// Run with interrupts disabled
asm!("msr daifset, #2");
intrin::irq_disable();
}
let proc = Process::current();
@@ -485,7 +531,6 @@ impl Process {
let entry = loader(new_space)?;
let arg = Self::store_arguments(new_space, argv)?;
debugln!("Will now enter at {:#x}", entry);
// TODO drop old address space
process_lock.space = Some(new_space);
@@ -493,7 +538,7 @@ impl Process {
// TODO drop old context
let ctx = thread.ctx.get();
let asid = (process_lock.id.asid() as usize) << 48;
asm!("tlbi aside1, {}", in(reg) asid);
intrin::flush_tlb_asid(asid);
ctx.write(Context::user(
entry,
+14 -10
View File
@@ -1,13 +1,15 @@
//!
use crate::proc::{Thread, ThreadRef, THREADS};
use crate::sync::IrqSafeSpinLock;
use crate::arch::intrin;
use crate::util::InitOnce;
use libsys::proc::Tid;
use alloc::{collections::VecDeque, rc::Rc};
struct SchedulerInner {
queue: VecDeque<u32>,
idle: Option<u32>,
current: Option<u32>,
queue: VecDeque<Tid>,
idle: Option<Tid>,
current: Option<Tid>,
}
/// Process scheduler state and queues
@@ -23,7 +25,9 @@ impl SchedulerInner {
current: None,
};
this.idle = Some(Thread::new_kernel(None, idle_fn, 0).unwrap().id());
let idle = Thread::new_kernel(None, idle_fn, 0).unwrap().id();
assert_eq!(idle, Tid::IDLE);
this.idle = Some(idle);
this
}
@@ -39,12 +43,12 @@ impl Scheduler {
}
/// Schedules a thread for execution
pub fn enqueue(&self, tid: u32) {
pub fn enqueue(&self, tid: Tid) {
self.inner.get().lock().queue.push_back(tid);
}
/// Removes given `tid` from execution queue
pub fn dequeue(&self, tid: u32) {
pub fn dequeue(&self, tid: Tid) {
self.inner.get().lock().queue.retain(|&p| p != tid)
}
@@ -66,7 +70,7 @@ impl Scheduler {
THREADS.lock().get(&id).unwrap().clone()
};
asm!("msr daifset, #2");
intrin::irq_disable();
Thread::enter(thread)
}
@@ -76,7 +80,7 @@ impl Scheduler {
/// # Safety
///
/// Unsafe: only allowed to be called from Process::execve()
pub unsafe fn hack_current_tid(&self, old: u32, new: u32) {
pub unsafe fn hack_current_tid(&self, old: Tid, new: Tid) {
let mut lock = self.inner.get().lock();
match lock.current {
Some(t) if t == old => {
@@ -93,7 +97,7 @@ impl Scheduler {
let mut inner = self.inner.get().lock();
let current = inner.current.unwrap();
if !discard && current != 0 {
if !discard && current != Tid::IDLE {
// Put the process into the back of the queue
inner.queue.push_back(current);
}
@@ -118,7 +122,7 @@ impl Scheduler {
if !Rc::ptr_eq(&from, &to) {
unsafe {
asm!("msr daifset, #2");
intrin::irq_disable();
Thread::switch(from, to, discard);
}
}
+15 -10
View File
@@ -12,7 +12,7 @@ use core::cell::UnsafeCell;
use core::sync::atomic::{AtomicU32, Ordering};
use libsys::{
error::Errno,
proc::{ExitCode, Pid},
proc::{ExitCode, Pid, Tid},
signal::Signal,
};
@@ -35,7 +35,7 @@ pub enum State {
}
struct ThreadInner {
id: u32,
id: Tid,
state: State,
owner: Option<Pid>,
pending_wait: Option<&'static Wait>,
@@ -61,9 +61,14 @@ impl Thread {
SCHED.current_thread()
}
/// Returns `true` if the thread is currently executing a signal handler context
pub fn is_handling_signal(&self) -> bool {
self.signal_pending.load(Ordering::Acquire) != 0
}
/// Returns a reference to thread `tid`, if it exists
#[inline]
pub fn get(tid: u32) -> Option<ThreadRef> {
pub fn get(tid: Tid) -> Option<ThreadRef> {
THREADS.lock().get(&tid).cloned()
}
@@ -169,7 +174,7 @@ impl Thread {
/// Returns the thread ID
#[inline]
pub fn id(&self) -> u32 {
pub fn id(&self) -> Tid {
self.inner.lock().id
}
@@ -237,6 +242,7 @@ impl Thread {
/// Changes process wait condition status
pub fn setup_wait(&self, wait: *const Wait) {
#![allow(clippy::not_unsafe_ptr_arg_deref)]
let mut lock = self.inner.lock();
// FIXME this is not cool
lock.pending_wait = Some(unsafe { &*wait });
@@ -244,7 +250,7 @@ impl Thread {
}
/// Suspends current thread until thread `tid` terminates
pub fn waittid(tid: u32) -> Result<(), Errno> {
pub fn waittid(tid: Tid) -> Result<(), Errno> {
loop {
let thread = THREADS
.lock()
@@ -334,7 +340,7 @@ impl Thread {
let signal_ctx = unsafe { &mut *self.signal_ctx.get() };
debugln!(
"Signal entry: tid={}, pc={:#x}, sp={:#x}, ttbr0={:#x}",
"Signal entry: tid={:?}, pc={:#x}, sp={:#x}, ttbr0={:#x}",
lock.id,
lock.signal_entry,
lock.signal_stack,
@@ -398,9 +404,8 @@ impl Drop for Thread {
}
/// Allocates a new thread ID
pub fn new_tid() -> u32 {
static LAST: AtomicU32 = AtomicU32::new(1);
pub fn new_tid() -> Tid {
static LAST: AtomicU32 = AtomicU32::new(0);
let id = LAST.fetch_add(1, Ordering::Relaxed);
assert!(id < 256, "Out of user TIDs");
id
Tid::from(id)
}
+12 -8
View File
@@ -6,12 +6,12 @@ use crate::proc::{sched::SCHED, Thread, ThreadRef};
use crate::sync::IrqSafeSpinLock;
use alloc::collections::LinkedList;
use core::time::Duration;
use libsys::{error::Errno, stat::FdSet};
use libsys::{error::Errno, proc::Tid, stat::FdSet};
/// Wait channel structure. Contains a queue of processes
/// waiting for some event to happen.
pub struct Wait {
queue: IrqSafeSpinLock<LinkedList<u32>>,
queue: IrqSafeSpinLock<LinkedList<Tid>>,
#[allow(dead_code)]
name: &'static str
}
@@ -28,7 +28,7 @@ pub enum WaitStatus {
}
struct Timeout {
tid: u32,
tid: Tid,
deadline: Duration,
}
@@ -83,8 +83,12 @@ pub fn select(
}
let read = rfds.as_deref().map(FdSet::clone);
let write = wfds.as_deref().map(FdSet::clone);
rfds.as_deref_mut().map(FdSet::reset);
wfds.as_deref_mut().map(FdSet::reset);
if let Some(rfds) = &mut rfds {
rfds.reset();
}
if let Some(wfds) = &mut wfds {
wfds.reset();
}
let deadline = timeout.map(|v| v + machine::local_timer().timestamp().unwrap());
let proc = thread.owner().unwrap();
@@ -94,7 +98,7 @@ pub fn select(
if let Some(read) = &read {
for fd in read.iter() {
let file = io.file(fd)?;
if file.borrow().is_ready(false)? {
if file.borrow().ready(false)? {
rfds.as_mut().unwrap().set(fd);
return Ok(1);
}
@@ -103,7 +107,7 @@ pub fn select(
if let Some(write) = &write {
for fd in write.iter() {
let file = io.file(fd)?;
if file.borrow().is_ready(true)? {
if file.borrow().ready(true)? {
wfds.as_mut().unwrap().set(fd);
return Ok(1);
}
@@ -129,7 +133,7 @@ impl Wait {
}
/// Interrupt wait pending on the channel
pub fn abort(&self, tid: u32, enqueue: bool) {
pub fn abort(&self, tid: Tid, enqueue: bool) {
let mut queue = self.queue.lock();
let mut tick_lock = TICK_LIST.lock();
let mut cursor = tick_lock.cursor_front_mut();
+8 -15
View File
@@ -1,6 +1,7 @@
//! System call argument ABI helpers
use crate::mem;
use crate::arch::intrin;
use core::alloc::Layout;
use libsys::error::Errno;
use crate::proc::Process;
@@ -23,19 +24,6 @@ macro_rules! invalid_memory {
}
}
#[inline(always)]
fn is_el0_accessible(virt: usize, write: bool) -> bool {
let mut res: usize;
unsafe {
if write {
asm!("at s1e0w, {}; mrs {}, par_el1", in(reg) virt, out(reg) res);
} else {
asm!("at s1e0r, {}; mrs {}, par_el1", in(reg) virt, out(reg) res);
}
}
res & 1 == 0
}
/// Checks given argument and interprets it as a `T` reference
pub fn struct_ref<'a, T>(base: usize) -> Result<&'a T, Errno> {
let layout = Layout::new::<T>();
@@ -122,14 +110,19 @@ pub fn validate_ptr(base: usize, len: usize, write: bool) -> Result<(), Errno> {
}
let process = Process::current();
let asid = process.asid();
for i in (base / mem::PAGE_SIZE)..((base + len + mem::PAGE_SIZE - 1) / mem::PAGE_SIZE) {
if !is_el0_accessible(i * mem::PAGE_SIZE, write) {
if !mem::is_el0_accessible(i * mem::PAGE_SIZE, write) {
// It's possible a CoW page hasn't yet been cloned when trying
// a write access
let res = if write {
process.manipulate_space(|space| {
space.try_cow_copy(i * mem::PAGE_SIZE)
space.try_cow_copy(i * mem::PAGE_SIZE)?;
unsafe {
intrin::flush_tlb_asid(asid);
}
Ok(())
})
} else {
Err(Errno::DoesNotExist)
+66 -35
View File
@@ -1,10 +1,10 @@
//! System call implementation
use crate::arch::{machine, platform::exception::ExceptionFrame};
use crate::mem::{virt::MapAttributes, phys::PageUsage};
use crate::debug::Level;
use crate::dev::timer::TimestampSource;
use crate::fs::create_filesystem;
use crate::mem::{phys::PageUsage, virt::MapAttributes};
use crate::proc::{self, elf, wait, Process, ProcessIo, Thread};
use core::mem::size_of;
use core::ops::DerefMut;
@@ -14,7 +14,7 @@ use libsys::{
debug::TraceLevel,
error::Errno,
ioctl::IoctlCmd,
proc::{ExitCode, Pid, MemoryAccess},
proc::{ExitCode, MemoryAccess, Pid, Tid},
signal::{Signal, SignalDestination},
stat::{
AccessMode, DirectoryEntry, FdSet, FileDescriptor, FileMode, GroupId, MountOptions,
@@ -56,8 +56,7 @@ fn find_at_node<T: DerefMut<Target = ProcessIo>>(
}
}
/// Main system call dispatcher function
pub fn syscall(num: SystemCall, args: &[usize]) -> Result<usize, Errno> {
fn _syscall(num: SystemCall, args: &[usize]) -> Result<usize, Errno> {
match num {
// I/O
SystemCall::Read => {
@@ -210,7 +209,8 @@ pub fn syscall(num: SystemCall, args: &[usize]) -> Result<usize, Errno> {
let acc = MemoryAccess::from_bits(args[2] as u32).ok_or(Errno::InvalidArgument)?;
let _flags = MemoryAccess::from_bits(args[3] as u32).ok_or(Errno::InvalidArgument)?;
let mut attrs = MapAttributes::NOT_GLOBAL | MapAttributes::SH_OUTER | MapAttributes::PXN;
let mut attrs =
MapAttributes::NOT_GLOBAL | MapAttributes::SH_OUTER | MapAttributes::PXN;
if !acc.contains(MemoryAccess::READ) {
return Err(Errno::NotImplemented);
}
@@ -244,9 +244,27 @@ pub fn syscall(num: SystemCall, args: &[usize]) -> Result<usize, Errno> {
}
let proc = Process::current();
proc.manipulate_space(move |space| {
space.free(addr, len / 4096)
})?;
proc.manipulate_space(move |space| space.free(addr, len / 4096))?;
Ok(0)
}
SystemCall::CreateDirectory => {
let at_fd = FileDescriptor::from_i32(args[0] as i32)?;
let path = arg::str_ref(args[1], args[2])?;
let mode = FileMode::from_bits(args[3] as u32).ok_or(Errno::InvalidArgument)?;
// TODO honor this option
let _flags = args[4] as u32;
let proc = Process::current();
let mut io = proc.io.lock();
let at = if let Some(fd) = at_fd {
io.file(fd)?.borrow().node()
} else {
None
};
io.ioctx().mkdir(at, path, mode)?;
Ok(0)
}
@@ -258,7 +276,7 @@ pub fn syscall(num: SystemCall, args: &[usize]) -> Result<usize, Errno> {
Process::current()
.new_user_thread(entry, stack, arg)
.map(|e| e as usize)
.map(|e| u32::from(e) as usize)
}
SystemCall::Exec => {
let filename = arg::str_ref(args[0], args[1])?;
@@ -293,7 +311,7 @@ pub fn syscall(num: SystemCall, args: &[usize]) -> Result<usize, Errno> {
}
SystemCall::WaitPid => {
// TODO special "pid" values
let pid = unsafe { Pid::from_raw(args[0] as u32) };
let pid = Pid::try_from(args[0] as u32)?;
let status = arg::struct_mut::<i32>(args[1])?;
match Process::waitpid(pid) {
@@ -305,15 +323,15 @@ pub fn syscall(num: SystemCall, args: &[usize]) -> Result<usize, Errno> {
}
}
SystemCall::WaitTid => {
let tid = args[0] as u32;
let tid = Tid::from(args[0] as u32);
match Thread::waittid(tid) {
Ok(_) => Ok(0),
_ => todo!(),
}
}
SystemCall::GetPid => Ok(Process::current().id().value() as usize),
SystemCall::GetTid => Ok(Thread::current().id() as usize),
SystemCall::GetPid => Ok(u32::from(Process::current().id()) as usize),
SystemCall::GetTid => Ok(u32::from(Thread::current().id()) as usize),
SystemCall::Sleep => {
let rem_buf = arg::option_buf_ref(args[1], size_of::<u64>() * 2)?;
let mut rem = Duration::new(0, 0);
@@ -353,35 +371,33 @@ pub fn syscall(num: SystemCall, args: &[usize]) -> Result<usize, Errno> {
}
SystemCall::GetSid => {
// TODO handle kernel processes here?
let pid = args[0] as u32;
let pid = Pid::to_option(args[0] as u32);
let current = Process::current();
let proc = if pid == 0 {
current
} else {
let pid = unsafe { Pid::from_raw(pid) };
let proc = if let Some(pid) = pid {
let proc = Process::get(pid).ok_or(Errno::DoesNotExist)?;
if proc.sid() != current.sid() {
return Err(Errno::PermissionDenied);
}
proc
} else {
current
};
Ok(proc.sid().value() as usize)
Ok(u32::from(proc.sid()) as usize)
}
SystemCall::GetPgid => {
// TODO handle kernel processes here?
let pid = args[0] as u32;
let pid = Pid::to_option(args[0] as u32);
let current = Process::current();
let proc = if pid == 0 {
current
} else {
let pid = unsafe { Pid::from_raw(pid) };
let proc = if let Some(pid) = pid {
Process::get(pid).ok_or(Errno::DoesNotExist)?
} else {
current
};
Ok(proc.pgid().value() as usize)
Ok(u32::from(proc.pgid()) as usize)
}
SystemCall::GetPpid => Ok(Process::current().ppid().unwrap().value() as usize),
SystemCall::GetPpid => Ok(u32::from(Process::current().ppid().unwrap()) as usize),
SystemCall::SetSid => {
let proc = Process::current();
let mut io = proc.io.lock();
@@ -392,22 +408,26 @@ pub fn syscall(num: SystemCall, args: &[usize]) -> Result<usize, Errno> {
let id = proc.id();
proc.set_sid(id);
Ok(id.value() as usize)
Ok(u32::from(id) as usize)
}
SystemCall::SetPgid => {
let pid = args[0] as u32;
let pgid = args[1] as u32;
let pid = Pid::to_option(args[0] as u32);
let pgid = Pid::to_option(args[1] as u32);
let current = Process::current();
let proc = if pid == 0 { current } else { todo!() };
if pgid == 0 {
proc.set_pgid(proc.id());
let proc = if let Some(_pid) = pid {
todo!()
} else {
current
};
if let Some(_pgid) = pgid {
todo!();
} else {
proc.set_pgid(proc.id());
}
Ok(proc.pgid().value() as usize)
Ok(u32::from(proc.pgid()) as usize)
}
// System
@@ -440,7 +460,7 @@ pub fn syscall(num: SystemCall, args: &[usize]) -> Result<usize, Errno> {
let buf = arg::str_ref(args[1], args[2])?;
let thread = Thread::current();
let proc = thread.owner().unwrap();
println!(level, "[trace {:?}:{}] {}", proc.id(), thread.id(), buf);
println!(level, "[trace {:?}:{:?}] {}", proc.id(), thread.id(), buf);
Ok(args[1])
}
@@ -448,3 +468,14 @@ pub fn syscall(num: SystemCall, args: &[usize]) -> Result<usize, Errno> {
SystemCall::Fork => unreachable!(),
}
}
/// Main system call dispatcher function
pub fn syscall(num: SystemCall, args: &[usize]) -> Result<usize, Errno> {
let thread = Thread::current();
let process = thread.owner().unwrap();
let result = _syscall(num, args);
if !thread.is_handling_signal() {
process.handle_pending_signals();
}
result
}
+3 -2
View File
@@ -29,8 +29,9 @@ impl<T> InitOnce<T> {
/// Returns the initialized value. Will panic if the value has not
/// yet been initialized.
#[allow(clippy::mut_from_ref)]
#[track_caller]
pub fn get(&self) -> &mut T {
assert!(self.is_initialized(), "Access to uninitialized InitOnce<T>");
assert!(self.is_initialized(), "Access to uninitialized InitOnce<T>: {:?}", core::panic::Location::caller());
unsafe { (*self.inner.get()).assume_init_mut() }
}
@@ -70,6 +71,6 @@ macro_rules! block {
}};
($cond:expr, $timeout:expr) => {
crate::block!($cond, $timeout, return Err(error::Errno::TimedOut))
crate::block!($cond, $timeout, return Err(libsys::error::Errno::TimedOut))
};
}
+1
View File
@@ -23,6 +23,7 @@ pub enum SystemCall {
Seek = 17,
MapMemory = 18,
UnmapMemory = 19,
CreateDirectory = 20,
// Process manipulation
Fork = 32,
+41 -48
View File
@@ -3,13 +3,14 @@ use crate::{
debug::TraceLevel,
error::Errno,
ioctl::IoctlCmd,
proc::{ExitCode, MemoryAccess, MemoryMap, Pid},
proc::{ExitCode, MemoryAccess, MemoryMap, Pid, Tid},
signal::{Signal, SignalDestination},
stat::{
AccessMode, DirectoryEntry, FdSet, FileDescriptor, FileMode, GroupId, MountOptions,
OpenFlags, Stat, UserId,
},
};
use core::arch::asm;
use core::time::Duration;
// TODO document the syscall ABI
@@ -75,9 +76,6 @@ macro_rules! argp {
// ($a:expr) => ($a as *const core::ffi::c_void as usize)
// }
/// # Safety
///
/// System call
#[inline(always)]
pub fn sys_exit(code: ExitCode) -> ! {
unsafe {
@@ -86,17 +84,11 @@ pub fn sys_exit(code: ExitCode) -> ! {
unreachable!();
}
/// # Safety
///
/// System call
#[inline(always)]
pub fn sys_close(fd: FileDescriptor) -> Result<(), Errno> {
Errno::from_syscall_unit(unsafe { syscall!(SystemCall::Close, argn!(u32::from(fd))) })
}
/// # Safety
///
/// System call
#[inline(always)]
pub fn sys_ex_nanosleep(ns: u64, rem: &mut [u64; 2]) -> Result<(), Errno> {
Errno::from_syscall_unit(unsafe {
@@ -104,9 +96,6 @@ pub fn sys_ex_nanosleep(ns: u64, rem: &mut [u64; 2]) -> Result<(), Errno> {
})
}
/// # Safety
///
/// System call
#[inline(always)]
pub fn sys_ex_debug_trace(level: TraceLevel, msg: &[u8]) -> Result<(), Errno> {
Errno::from_syscall_unit(unsafe {
@@ -119,9 +108,6 @@ pub fn sys_ex_debug_trace(level: TraceLevel, msg: &[u8]) -> Result<(), Errno> {
})
}
/// # Safety
///
/// System call
#[inline(always)]
pub fn sys_openat(
at: Option<FileDescriptor>,
@@ -142,9 +128,6 @@ pub fn sys_openat(
.map(|e| FileDescriptor::from(e as u32))
}
/// # Safety
///
/// System call
#[inline(always)]
pub fn sys_read(fd: FileDescriptor, data: &mut [u8]) -> Result<usize, Errno> {
Errno::from_syscall(unsafe {
@@ -169,9 +152,6 @@ pub fn sys_write(fd: FileDescriptor, data: &[u8]) -> Result<usize, Errno> {
})
}
/// # Safety
///
/// System call
#[inline(always)]
pub fn sys_fstatat(
at: Option<FileDescriptor>,
@@ -196,18 +176,15 @@ pub fn sys_fstatat(
/// System call
#[inline(always)]
pub unsafe fn sys_fork() -> Result<Option<Pid>, Errno> {
Errno::from_syscall(syscall!(SystemCall::Fork)).map(|res| {
Errno::from_syscall(syscall!(SystemCall::Fork)).and_then(|res| {
if res != 0 {
Some(unsafe { Pid::from_raw(res as u32) })
Pid::try_from(res as u32).map(Some)
} else {
None
Ok(None)
}
})
}
/// # Safety
///
/// System call
#[inline(always)]
pub fn sys_execve(pathname: &str, argv: &[&str]) -> Result<(), Errno> {
Errno::from_syscall_unit(unsafe {
@@ -221,23 +198,17 @@ pub fn sys_execve(pathname: &str, argv: &[&str]) -> Result<(), Errno> {
})
}
/// # Safety
///
/// System call
#[inline(always)]
pub fn sys_waitpid(pid: Pid, status: &mut i32) -> Result<(), Errno> {
Errno::from_syscall_unit(unsafe {
syscall!(
SystemCall::WaitPid,
argn!(pid.value()),
argn!(u32::from(pid)),
argp!(status as *mut i32)
)
})
}
/// # Safety
///
/// System call
#[inline(always)]
pub fn sys_ioctl(
fd: FileDescriptor,
@@ -289,10 +260,11 @@ pub fn sys_ex_kill(pid: SignalDestination, signum: Signal) -> Result<(), Errno>
}
#[inline(always)]
pub fn sys_ex_clone(entry: usize, stack: usize, arg: usize) -> Result<usize, Errno> {
pub fn sys_ex_clone(entry: usize, stack: usize, arg: usize) -> Result<Tid, Errno> {
Errno::from_syscall(unsafe {
syscall!(SystemCall::Clone, argn!(entry), argn!(stack), argn!(arg))
})
.map(|e| Tid::from(e as u32))
}
#[inline(always)]
@@ -304,8 +276,8 @@ pub fn sys_ex_thread_exit(status: ExitCode) -> ! {
}
#[inline(always)]
pub fn sys_ex_thread_wait(tid: u32) -> Result<ExitCode, Errno> {
Errno::from_syscall(unsafe { syscall!(SystemCall::WaitTid, argn!(tid)) })
pub fn sys_ex_thread_wait(tid: Tid) -> Result<ExitCode, Errno> {
Errno::from_syscall(unsafe { syscall!(SystemCall::WaitTid, argn!(u32::from(tid))) })
.map(|_| ExitCode::from(0))
}
@@ -356,27 +328,31 @@ pub fn sys_faccessat(
}
#[inline(always)]
pub fn sys_ex_gettid() -> u32 {
unsafe { syscall!(SystemCall::GetTid) as u32 }
pub fn sys_ex_gettid() -> Tid {
Tid::from(unsafe { syscall!(SystemCall::GetTid) as u32 })
}
#[inline(always)]
pub fn sys_getpid() -> Pid {
unsafe { Pid::from_raw(syscall!(SystemCall::GetPid) as u32) }
Pid::try_from(unsafe { syscall!(SystemCall::GetPid) as u32 }).unwrap()
}
#[inline(always)]
pub fn sys_getpgid(pid: Pid) -> Result<Pid, Errno> {
Errno::from_syscall(unsafe { syscall!(SystemCall::GetPgid, argn!(pid.value())) })
.map(|e| unsafe { Pid::from_raw(e as u32) })
pub fn sys_getpgid(pid: Option<Pid>) -> Result<Pid, Errno> {
Errno::from_syscall(unsafe { syscall!(SystemCall::GetPgid, argn!(Pid::from_option(pid))) })
.and_then(|e| Pid::try_from(e as u32))
}
#[inline(always)]
pub fn sys_setpgid(pid: Pid, pgid: Pid) -> Result<Pid, Errno> {
pub fn sys_setpgid(pid: Option<Pid>, pgid: Option<Pid>) -> Result<Pid, Errno> {
Errno::from_syscall(unsafe {
syscall!(SystemCall::SetPgid, argn!(pid.value()), argn!(pgid.value()))
syscall!(
SystemCall::SetPgid,
argn!(Pid::from_option(pid)),
argn!(Pid::from_option(pgid))
)
})
.map(|e| unsafe { Pid::from_raw(e as u32) })
.and_then(|e| Pid::try_from(e as u32))
}
#[inline(always)]
@@ -404,7 +380,7 @@ pub fn sys_getgid() -> GroupId {
#[inline(always)]
pub fn sys_setsid() -> Result<Pid, Errno> {
Errno::from_syscall(unsafe { syscall!(SystemCall::SetSid) })
.map(|e| unsafe { Pid::from_raw(e as u32) })
.and_then(|e| Pid::try_from(e as u32))
}
#[inline(always)]
@@ -470,7 +446,24 @@ pub fn sys_mmap(
})
}
/// # Safety
///
/// System call
#[inline(always)]
pub unsafe fn sys_munmap(addr: usize, len: usize) -> Result<(), Errno> {
Errno::from_syscall_unit(syscall!(SystemCall::UnmapMemory, argn!(addr), argn!(len)))
}
#[inline(always)]
pub fn sys_mkdirat(fd: Option<FileDescriptor>, path: &str, mode: FileMode, flags: u32) -> Result<(), Errno> {
Errno::from_syscall_unit(unsafe {
syscall!(
SystemCall::CreateDirectory,
argn!(FileDescriptor::into_i32(fd)),
argp!(path.as_ptr()),
argn!(path.len()),
argn!(mode.bits()),
argn!(flags)
)
})
}
+49 -1
View File
@@ -1,4 +1,3 @@
#![feature(asm, const_panic)]
#![no_std]
#[macro_use]
@@ -24,6 +23,55 @@ pub struct ProgramArgs {
pub size: usize
}
// TODO utils
use core::fmt;
#[derive(Clone, Copy)]
pub struct FixedStr<const N: usize> {
len: usize,
data: [u8; N],
}
impl<const N: usize> FixedStr<N> {
pub const fn empty() -> Self {
Self {
len: 0,
data: [0; N]
}
}
pub fn copy_from_str(&mut self, src: &str) {
if src.len() > self.data.len() {
panic!("copy_from_str: src len > data len");
}
self.len = src.len();
self.data[..self.len].copy_from_slice(src.as_bytes());
}
pub fn as_str(&self) -> &str {
unsafe {
core::str::from_utf8_unchecked(&self.data[..self.len])
}
}
}
impl<const N: usize> fmt::Debug for FixedStr<N> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "\"")?;
fmt::Display::fmt(self, f)?;
write!(f, "\"")
}
}
impl<const N: usize> fmt::Display for FixedStr<N> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for &byte in &self.data[..self.len] {
write!(f, "{}", byte as char)?;
}
Ok(())
}
}
#[cfg(feature = "user")]
pub mod calls;
#[cfg(feature = "user")]
+66 -16
View File
@@ -12,6 +12,11 @@ pub struct ExitCode(i32);
#[repr(transparent)]
pub struct Pid(u32);
/// Wrapper type for thread ID
#[derive(Clone, Copy, PartialOrd, Ord, PartialEq, Eq)]
#[repr(transparent)]
pub struct Tid(u32);
#[derive(Clone, Copy, PartialOrd, Ord, PartialEq, Eq, Debug)]
#[repr(transparent)]
pub struct Pgid(u32);
@@ -26,8 +31,8 @@ bitflags! {
bitflags! {
pub struct MemoryMap: u32 {
const BACKEND = 0x3 << 0;
const ANONYMOUS = 1 << 0;
const BACKEND = 0x3;
const ANONYMOUS = 1;
const SHARING = 0x3 << 2;
const PRIVATE = 1 << 2;
@@ -53,14 +58,15 @@ impl From<ExitCode> for i32 {
}
impl Pid {
/// Kernel idle process always has PID of zero
pub const IDLE: Self = Self(Self::KERNEL_BIT);
const KERNEL_BIT: u32 = 1 << 31;
const USER_MAX: u32 = 256;
/// Constructs an instance of user-space PID
pub const fn user(id: u32) -> Self {
assert!(id < 256, "PID is too high");
assert!(id < Self::USER_MAX, "PID is too high");
if id == 0 {
panic!("User PID cannot be zero");
}
Self(id)
}
@@ -83,18 +89,20 @@ impl Pid {
self.0 as u8
}
/// Returns bit value of this pid
pub const fn value(self) -> u32 {
self.0
pub fn from_option(m: Option<Self>) -> u32 {
if let Some(pid) = m {
u32::from(pid)
} else {
0
}
}
/// Constructs [Pid] from raw [u32] value
///
/// # Safety
///
/// Unsafe: does not check `num`
pub const unsafe fn from_raw(num: u32) -> Self {
Self(num)
pub fn to_option(m: u32) -> Option<Self> {
if m != 0 {
Some(Self::try_from(m).unwrap())
} else {
None
}
}
}
@@ -109,6 +117,26 @@ impl fmt::Debug for Pid {
}
}
impl TryFrom<u32> for Pid {
type Error = Errno;
fn try_from(raw: u32) -> Result<Pid, Errno> {
if raw & Self::KERNEL_BIT != 0 {
Ok(Pid::kernel(raw & !Self::KERNEL_BIT))
} else if raw != 0 && raw < Self::USER_MAX {
Ok(Pid::user(raw))
} else {
Err(Errno::InvalidArgument)
}
}
}
impl From<Pid> for u32 {
fn from(pid: Pid) -> u32 {
pid.0
}
}
impl TryFrom<Pid> for Pgid {
type Error = Errno;
@@ -132,3 +160,25 @@ impl From<Pgid> for u32 {
p.0
}
}
impl Tid {
pub const IDLE: Tid = Tid(0);
}
impl fmt::Debug for Tid {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Tid(#{})", self.0)
}
}
impl From<u32> for Tid {
fn from(p: u32) -> Tid {
Self(p)
}
}
impl From<Tid> for u32 {
fn from(p: Tid) -> u32 {
p.0
}
}
+1 -1
View File
@@ -37,7 +37,7 @@ impl From<isize> for SignalDestination {
impl From<SignalDestination> for isize {
fn from(p: SignalDestination) -> isize {
match p {
SignalDestination::Process(pid) => pid.value() as isize,
SignalDestination::Process(pid) => u32::from(pid) as isize,
SignalDestination::Group(pgid) => -(u32::from(pgid) as isize),
SignalDestination::This => 0,
SignalDestination::All => -1
+12 -7
View File
@@ -1,5 +1,6 @@
// TODO split up this file
use crate::error::Errno;
use core::str::FromStr;
use core::fmt;
const AT_FDCWD: i32 = -2;
@@ -143,19 +144,23 @@ impl DirectoryEntry {
Self { name: [0; 64] }
}
pub fn from_str(i: &str) -> DirectoryEntry {
let mut res = DirectoryEntry { name: [0; 64] };
let bytes = i.as_bytes();
res.name[..bytes.len()].copy_from_slice(bytes);
res
}
pub fn as_str(&self) -> &str {
let zero = self.name.iter().position(|&c| c == 0).unwrap();
core::str::from_utf8(&self.name[..zero]).unwrap()
}
}
impl FromStr for DirectoryEntry {
type Err = Errno;
fn from_str(i: &str) -> Result<Self, Errno> {
let mut res = DirectoryEntry { name: [0; 64] };
let bytes = i.as_bytes();
res.name[..bytes.len()].copy_from_slice(bytes);
Ok(res)
}
}
impl fmt::Debug for DirectoryEntry {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("DirectoryEntry")
+3
View File
@@ -9,3 +9,6 @@ edition = "2021"
libsys = { path = "../libsys", features = ["user"] }
lazy_static = { version = "^1.4.0", features = ["spin_no_std"] }
memoffset = "^0.6.4"
[features]
verbose = []
+13 -9
View File
@@ -82,6 +82,7 @@ impl Zone {
MemoryAccess::READ | MemoryAccess::WRITE,
MemoryMap::ANONYMOUS | MemoryMap::PRIVATE,
)?;
#[cfg(feature = "verbose")]
trace_debug!("Zone::alloc({}) => {:#x}", size, pages);
let zone_ptr = pages as *mut Zone;
@@ -101,8 +102,9 @@ impl Zone {
}
unsafe fn free(zone: *mut Self) {
#[cfg(feature = "verbose")]
trace_debug!("Zone::free({:p})", zone);
sys_munmap(zone as usize, (&*zone).size + size_of::<Zone>())
sys_munmap(zone as usize, (*zone).size + size_of::<Zone>())
.expect("Failed to unmap heap pages");
}
@@ -165,7 +167,7 @@ unsafe fn alloc_from(list: &mut ZoneList, zone_size: usize, size: usize) -> *mut
if !ptr.is_null() {
return ptr;
}
zone = (&mut *zone).next;
zone = (*zone).next;
}
let zone = match Zone::alloc(zone_size) {
@@ -175,7 +177,7 @@ unsafe fn alloc_from(list: &mut ZoneList, zone_size: usize, size: usize) -> *mut
return null_mut();
}
};
list.add(&mut (&mut *zone).list);
list.add(&mut (*zone).list);
}
}
@@ -183,6 +185,7 @@ unsafe impl GlobalAlloc for Allocator {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
assert!(layout.align() < 16);
let size = (layout.size() + 15) & !15;
#[cfg(feature = "verbose")]
trace_debug!("alloc({:?})", layout);
if size <= SMALL_ZONE_ELEM {
alloc_from(SMALL_ZONE_LIST.assume_init_mut(), SMALL_ZONE_SIZE, size)
@@ -195,8 +198,9 @@ unsafe impl GlobalAlloc for Allocator {
}
}
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
trace_debug!("free({:p}, {:?})", ptr, layout);
unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {
#[cfg(feature = "verbose")]
trace_debug!("free({:p}, {:?})", ptr, _layout);
assert!(!ptr.is_null());
let mut block = ptr.sub(size_of::<Block>()) as *mut Block;
let mut block_ref = &mut *block;
@@ -232,7 +236,7 @@ unsafe impl GlobalAlloc for Allocator {
if !next.is_null() && next_ref.flags & BLOCK_ALLOC == 0 {
next_ref.flags = 0;
if !next_ref.next.is_null() {
(&mut *(next_ref.next)).prev = block;
(*next_ref.next).prev = block;
}
block_ref.next = next_ref.next;
block_ref.size += (next_ref.size as usize + size_of::<Block>()) as u32;
@@ -241,15 +245,15 @@ unsafe impl GlobalAlloc for Allocator {
if block_ref.prev.is_null() && block_ref.next.is_null() {
let zone = (block as usize - size_of::<Zone>()) as *mut Zone;
assert_eq!((zone as usize) & 0xFFF, 0);
(&mut *zone).list.del();
(*zone).list.del();
Zone::free(zone);
}
}
}
#[alloc_error_handler]
fn alloc_error_handler(_layout: Layout) -> ! {
loop {}
fn alloc_error_handler(layout: Layout) -> ! {
panic!("Allocation failed: {:?}", layout);
}
#[global_allocator]
+14 -3
View File
@@ -1,6 +1,16 @@
use libsys::{debug::TraceLevel, ProgramArgs};
use alloc::vec::Vec;
#[cfg(feature = "verbose")]
use crate::trace;
use alloc::vec::Vec;
#[cfg(feature = "verbose")]
use libsys::debug::TraceLevel;
use libsys::{
ProgramArgs,
};
mod passwd;
pub use passwd::UserInfo;
mod shadow;
pub use shadow::UserShadow;
static mut PROGRAM_ARGS: Vec<&'static str> = Vec::new();
@@ -10,12 +20,13 @@ pub fn args() -> &'static [&'static str] {
pub(crate) unsafe fn setup_env(arg: &ProgramArgs) {
for i in 0..arg.argc {
let base = core::ptr::read((arg.argv + i * 16 + 0) as *const *const u8);
let base = core::ptr::read((arg.argv + i * 16) as *const *const u8);
let len = core::ptr::read((arg.argv + i * 16 + 8) as *const usize);
let string = core::str::from_utf8(core::slice::from_raw_parts(base, len)).unwrap();
PROGRAM_ARGS.push(string);
}
#[cfg(feature = "verbose")]
trace!(TraceLevel::Debug, "args = {:?}", PROGRAM_ARGS);
}
+101
View File
@@ -0,0 +1,101 @@
use crate::file::File;
use crate::io::{self, read_line};
use core::str::FromStr;
use libsys::{
stat::{GroupId, UserId},
error::Errno,
FixedStr,
};
#[derive(Debug, Clone, Copy)]
pub struct UserInfo {
name: FixedStr<32>,
uid: UserId,
gid: GroupId,
home: FixedStr<64>,
shell: FixedStr<64>,
}
impl UserInfo {
pub fn name(&self) -> &str {
self.name.as_str()
}
pub fn home(&self) -> &str {
self.home.as_str()
}
pub fn shell(&self) -> &str {
self.shell.as_str()
}
pub fn uid(&self) -> UserId {
self.uid
}
pub fn gid(&self) -> GroupId {
self.gid
}
pub fn find<F: Fn(&Self) -> bool>(pred: F) -> Result<Self, io::Error> {
let mut file = File::open("/etc/passwd")?;
let mut buf = [0; 128];
loop {
let line = read_line(&mut file, &mut buf)?;
if let Some(line) = line {
let ent = UserInfo::from_str(line)?;
if pred(&ent) {
return Ok(ent);
}
} else {
break;
}
}
Err(io::Error::from(Errno::InvalidArgument))
}
pub fn by_name(name: &str) -> Result<Self, io::Error> {
Self::find(|ent| ent.name() == name)
}
}
impl FromStr for UserInfo {
type Err = io::Error;
fn from_str(s: &str) -> Result<Self, io::Error> {
let mut iter = s.split(':');
let name = iter.next().ok_or_else(|| io::Error::from(Errno::InvalidArgument))?;
let uid = iter
.next()
.ok_or_else(|| io::Error::from(Errno::InvalidArgument))
.and_then(|e| u32::from_str(e).map_err(|_| io::Error::from(Errno::InvalidArgument)))
.map(UserId::from)?;
let gid = iter
.next()
.ok_or_else(|| io::Error::from(Errno::InvalidArgument))
.and_then(|e| u32::from_str(e).map_err(|_| io::Error::from(Errno::InvalidArgument)))
.map(GroupId::from)?;
let _comment = iter.next().ok_or_else(|| io::Error::from(Errno::InvalidArgument))?;
let home = iter.next().ok_or_else(|| io::Error::from(Errno::InvalidArgument))?;
let shell = iter.next().ok_or_else(|| io::Error::from(Errno::InvalidArgument))?;
if iter.next().is_some() {
return Err(io::Error::from(Errno::InvalidArgument));
}
let mut res = Self {
uid,
gid,
name: FixedStr::empty(),
home: FixedStr::empty(),
shell: FixedStr::empty(),
};
res.name.copy_from_str(name);
res.home.copy_from_str(home);
res.shell.copy_from_str(shell);
Ok(res)
}
}
+66
View File
@@ -0,0 +1,66 @@
use crate::file::File;
use crate::io::{self, read_line};
use core::str::FromStr;
use libsys::{FixedStr, error::Errno};
#[derive(Debug, Clone, Copy)]
pub struct UserShadow {
name: FixedStr<32>,
password: FixedStr<64>,
}
impl UserShadow {
pub fn name(&self) -> &str {
self.name.as_str()
}
pub fn password(&self) -> &str {
self.password.as_str()
}
pub fn find<F: Fn(&Self) -> bool>(pred: F) -> Result<Self, io::Error> {
let mut file = File::open("/etc/shadow")?;
let mut buf = [0; 128];
loop {
let line = read_line(&mut file, &mut buf)?;
if let Some(line) = line {
let ent = UserShadow::from_str(line)?;
if pred(&ent) {
return Ok(ent);
}
} else {
break;
}
}
Err(io::Error::from(Errno::DoesNotExist))
}
pub fn by_name(name: &str) -> Result<Self, io::Error> {
Self::find(|ent| ent.name() == name)
}
}
impl FromStr for UserShadow {
type Err = io::Error;
fn from_str(s: &str) -> Result<Self, io::Error> {
let mut iter = s.split(':');
let name = iter.next().ok_or_else(|| io::Error::from(Errno::InvalidArgument))?;
let password = iter.next().ok_or_else(|| io::Error::from(Errno::InvalidArgument))?;
if iter.next().is_some() {
return Err(io::Error::from(Errno::InvalidArgument));
}
let mut res = Self {
name: FixedStr::empty(),
password: FixedStr::empty(),
};
res.name.copy_from_str(name);
res.password.copy_from_str(password);
Ok(res)
}
}
+24
View File
@@ -42,3 +42,27 @@ pub fn stat(pathname: &str) -> Result<Stat, Error> {
sys_fstatat(None, pathname, &mut buf, 0).unwrap();
Ok(buf)
}
// TODO use BufRead instead once it's implemented
pub(crate) fn read_line<'a, F: Read>(f: &mut F, buf: &'a mut [u8]) -> Result<Option<&'a str>, Error> {
let mut pos = 0;
loop {
if pos == buf.len() {
return Err(Error::from(Errno::OutOfMemory));
}
let count = f.read(&mut buf[pos..=pos])?;
if count == 0 {
if pos == 0 {
return Ok(None);
}
break;
}
if buf[pos] == b'\n' {
break;
}
pos += 1;
}
core::str::from_utf8(&buf[..pos]).map_err(|_| Error::from(Errno::InvalidArgument)).map(Some)
}
+4 -2
View File
@@ -8,7 +8,8 @@ macro_rules! print {
#[macro_export]
macro_rules! println {
($($args:tt)+) => (print!("{}\n", format_args!($($args)+)))
($($args:tt)+) => (print!("{}\n", format_args!($($args)+)));
() => (print!("\n"));
}
#[macro_export]
@@ -18,7 +19,8 @@ macro_rules! eprint {
#[macro_export]
macro_rules! eprintln {
($($args:tt)+) => (eprint!("{}\n", format_args!($($args)+)))
($($args:tt)+) => (eprint!("{}\n", format_args!($($args)+)));
() => (eprint!("\n"));
}
pub fn _print<T: Write>(out: fn() -> T, args: fmt::Arguments) {
+1 -1
View File
@@ -1,4 +1,4 @@
#![feature(asm, alloc_error_handler)]
#![feature(alloc_error_handler)]
#![no_std]
#[macro_use]
+8 -8
View File
@@ -6,8 +6,9 @@ use core::fmt;
use core::mem::MaybeUninit;
use libsys::{
calls::{sys_ex_clone, sys_ex_gettid, sys_ex_signal, sys_ex_thread_exit, sys_ex_thread_wait},
proc::ExitCode,
proc::{ExitCode, Tid},
};
use core::arch::asm;
struct NativeData<F, T>
where
@@ -22,19 +23,19 @@ where
#[derive(Clone)]
pub struct Thread {
id: u32,
id: Tid,
}
pub type ThreadResult<T> = Result<T, Box<dyn Any + Send + Sync>>;
pub type ThreadPacket<T> = Arc<UnsafeCell<MaybeUninit<ThreadResult<T>>>>;
pub struct JoinHandle<T> {
native: u32,
native: Tid,
result: ThreadPacket<T>,
}
impl Thread {
pub const fn id(&self) -> u32 {
pub const fn id(&self) -> Tid {
self.id
}
}
@@ -60,7 +61,7 @@ impl<T> JoinHandle<T> {
}
unsafe fn init_common(signal_stack_pointer: *mut u8) {
let tid = sys_ex_gettid() as u64;
let tid = u32::from(sys_ex_gettid()) as u64;
asm!("msr tpidr_el0, {:x}", in(reg) tid);
// thread::current() should be valid at this point
@@ -86,8 +87,7 @@ pub fn current() -> Thread {
unsafe {
asm!("mrs {:x}, tpidr_el0", out(reg) id);
}
Thread { id: id as u32 }
Thread { id: Tid::from(id as u32) }
}
pub fn spawn<F, T>(f: F) -> JoinHandle<T>
@@ -136,7 +136,7 @@ where
result: result.clone(),
}));
sys_ex_clone(thread_entry::<F, T> as usize, stack, data as usize).unwrap() as u32
sys_ex_clone(thread_entry::<F, T> as usize, stack, data as usize).unwrap()
};
JoinHandle { native, result }
+7
View File
@@ -9,6 +9,10 @@ edition = "2021"
name = "init"
path = "src/init/main.rs"
[[bin]]
name = "mkdir"
path = "src/bin/mkdir.rs"
[[bin]]
name = "shell"
path = "src/bin/shell.rs"
@@ -37,3 +41,6 @@ path = "src/sbin/login.rs"
libusr = { path = "../libusr" }
libsys = { path = "../libsys" }
lazy_static = { version = "*", features = ["spin_no_std"] }
[features]
verbose = ["libusr/verbose"]
+1 -1
View File
@@ -30,7 +30,7 @@ fn main() -> i32 {
if args.len() == 1 {
if let Err(e) = do_cat(io::stdin()) {
eprintln!("{}: {:?}", ".", e);
eprintln!(".: {:?}", e);
res = -1;
}
} else {
+1 -1
View File
@@ -1,4 +1,3 @@
#![feature(asm)]
#![no_std]
#![no_main]
@@ -9,6 +8,7 @@
extern crate libusr;
use libusr::sys::{abi::SystemCall, stat::Stat};
use core::arch::asm;
static mut STATE: u64 = 0;
+2 -2
View File
@@ -27,7 +27,7 @@ fn line_print(off: usize, line: &[u8]) {
print!(".");
}
}
println!("");
println!();
}
fn do_hexd<F: Read>(mut fd: F) -> Result<(), io::Error> {
@@ -53,7 +53,7 @@ fn main() -> i32 {
if args.len() == 1 {
if let Err(e) = do_hexd(io::stdin()) {
eprintln!("{}: {:?}", ".", e);
eprintln!(".: {:?}", e);
res = -1;
}
} else {
+1 -1
View File
@@ -57,7 +57,7 @@ fn main() -> i32 {
if args.len() == 1 {
if let Err(e) = list_directory(".") {
eprintln!("{}: {:?}", ".", e);
eprintln!(".: {:?}", e);
res = -1;
}
} else {
+26
View File
@@ -0,0 +1,26 @@
#![no_std]
#![no_main]
#[macro_use]
extern crate libusr;
use libsys::{sys_mkdirat, stat::FileMode};
#[no_mangle]
fn main() -> i32 {
let args = libusr::env::args();
if args.len() < 2 {
eprintln!("Usage: {} DIR1 ...", args[0]);
return -1;
}
let mut status = 0;
for &item in args.iter().skip(1) {
if let Err(err) = sys_mkdirat(None, item, FileMode::default_dir(), 0) {
eprintln!("{}: {:?}", item, err);
status = -1;
}
}
status
}
+8 -10
View File
@@ -9,7 +9,7 @@ use alloc::{borrow::ToOwned, vec::Vec};
use libusr::io::{self, Read};
use libusr::signal::{self, SignalHandler};
use libusr::sys::{
proc::Pid, sys_chdir, sys_execve, sys_exit, sys_faccessat, sys_fork, sys_getpgid, sys_setpgid,
sys_chdir, sys_execve, sys_exit, sys_faccessat, sys_fork, sys_getpgid, sys_setpgid,
sys_waitpid, AccessMode, Errno, ExitCode, FileDescriptor, Signal,
};
@@ -22,13 +22,11 @@ fn cmd_cd(args: &[&str]) -> ExitCode {
if args.len() != 2 {
eprintln!("Usage: cd DIR");
ExitCode::from(-1)
} else if let Err(err) = sys_chdir(args[1]) {
eprintln!("{}: {:?}", args[1], err);
ExitCode::from(-1)
} else {
if let Err(err) = sys_chdir(args[1]) {
eprintln!("{}: {:?}", args[1], err);
ExitCode::from(-1)
} else {
ExitCode::from(0)
}
ExitCode::from(0)
}
}
@@ -67,11 +65,11 @@ fn execute(line: &str) -> Result<ExitCode, Errno> {
if let Some(pid) = unsafe { sys_fork()? } {
let mut status = 0;
sys_waitpid(pid, &mut status)?;
let pgid = sys_getpgid(unsafe { Pid::from_raw(0) }).unwrap();
let pgid = sys_getpgid(None).unwrap();
io::tcsetpgrp(FileDescriptor::STDIN, pgid).unwrap();
Ok(ExitCode::from(status))
} else {
let pgid = sys_setpgid(unsafe { Pid::from_raw(0) }, unsafe { Pid::from_raw(0) }).unwrap();
let pgid = sys_setpgid(None, None).unwrap();
io::tcsetpgrp(FileDescriptor::STDIN, pgid).unwrap();
sys_execve(&filename, &args).unwrap();
sys_exit(ExitCode::from(-1));
@@ -84,7 +82,7 @@ fn main() -> i32 {
let mut stdin = io::stdin();
signal::set_handler(Signal::Interrupt, SignalHandler::Ignore);
let pgid = sys_setpgid(unsafe { Pid::from_raw(0) }, unsafe { Pid::from_raw(0) }).unwrap();
let pgid = sys_setpgid(None, None).unwrap();
io::tcsetpgrp(FileDescriptor::STDIN, pgid).unwrap();
loop {
+11 -2
View File
@@ -1,10 +1,11 @@
#![feature(asm)]
#![no_std]
#![no_main]
#[macro_use]
extern crate libusr;
use core::arch::asm;
use libusr::sys::{stat::MountOptions, sys_execve, sys_fork, sys_mount, sys_waitpid};
#[no_mangle]
@@ -17,6 +18,14 @@ fn main() -> i32 {
},
)
.expect("Failed to mount devfs");
sys_mount(
"/sys",
&MountOptions {
device: None,
fs: Some("sysfs"),
},
)
.expect("Failed to mount sysfs");
if let Some(pid) = unsafe { sys_fork().unwrap() } {
let mut status = 0;
@@ -30,6 +39,6 @@ fn main() -> i32 {
}
} else {
sys_execve("/sbin/login", &["/sbin/login", "/dev/ttyS0"]).unwrap();
loop {}
unreachable!();
}
}
+28 -13
View File
@@ -11,11 +11,10 @@ use libsys::{
},
error::Errno,
ioctl::IoctlCmd,
proc::Pid,
stat::{FileDescriptor, FileMode, GroupId, OpenFlags, UserId},
termios::{Termios, TermiosLflag},
};
use libusr::{env, io};
use libusr::{env::{self, UserInfo, UserShadow}, io};
struct HiddenInput {
fd: FileDescriptor,
@@ -74,23 +73,28 @@ fn readline(fd: FileDescriptor, buf: &mut [u8]) -> Result<&str, Errno> {
}
}
fn login_as(uid: UserId, gid: GroupId, shell: &str) -> Result<(), Errno> {
fn login(uid: UserId, gid: GroupId, shell: &str) -> Result<(), Errno> {
if let Some(pid) = unsafe { sys_fork() }? {
let mut status = 0;
sys_waitpid(pid, &mut status).ok();
let pgid = sys_getpgid(unsafe { Pid::from_raw(0) }).unwrap();
let pgid = sys_getpgid(None).unwrap();
io::tcsetpgrp(FileDescriptor::STDIN, pgid).unwrap();
Ok(())
} else {
sys_setuid(uid).expect("setuid failed");
sys_setgid(gid).expect("setgid failed");
let pgid = sys_setpgid(unsafe { Pid::from_raw(0) }, unsafe { Pid::from_raw(0) }).unwrap();
let pgid = sys_setpgid(None, None).unwrap();
io::tcsetpgrp(FileDescriptor::STDIN, pgid).unwrap();
sys_execve(shell, &[shell]).expect("execve() failed");
panic!();
}
}
fn login_as(name: &str) -> Result<(), Errno> {
let ent = UserInfo::by_name(name).map_err(|_| Errno::DoesNotExist)?;
login(ent.uid(), ent.gid(), ent.shell())
}
// TODO baud rate and misc port settings
#[no_mangle]
fn main() -> i32 {
@@ -133,15 +137,26 @@ fn main() -> i32 {
loop {
print!("login: ");
let username = readline(FileDescriptor::STDIN, &mut user_buf).expect("Login read failed");
print!("password: ");
let password = {
let mut input = HiddenInput::open(FileDescriptor::STDIN).unwrap();
input.readline(&mut password_buf)
}
.expect("Password read failed");
if username == "root" && password == "toor" {
login_as(UserId::from(0), GroupId::from(0), "/bin/shell").unwrap();
let shadow = match UserShadow::by_name(username) {
Ok(e) => e,
Err(_) => continue
};
if !shadow.password().is_empty() {
print!("password: ");
let password = {
let mut input = HiddenInput::open(FileDescriptor::STDIN).unwrap();
input.readline(&mut password_buf)
}
.expect("Password read failed");
if password != shadow.password() {
eprintln!("Incorrect password");
continue;
}
}
login_as(username).ok();
}
}