Basic finish C interface. Debugging ...

master
WangRunji 7 years ago
parent 97de1b4bc1
commit ed751bd8a1

@ -1,11 +1,13 @@
//! C Interfaces for ucore //! C Interfaces for ucore
use alloc::{rc::Rc, boxed::Box}; use alloc::{rc::Rc, boxed::Box, BTreeMap};
use core::cell::RefCell; use core::cell::RefCell;
use core::slice; use core::slice;
use core::ops::Deref;
/// Global allocator defined in root use core::cmp::Ordering;
pub use self::allocator::UcoreAllocator; use alloc::heap::{Alloc, AllocErr, Layout};
use core::mem::size_of;
use vfs;
/// Lang items for bare lib /// Lang items for bare lib
mod lang { mod lang {
@ -31,6 +33,8 @@ mod lang {
mod ucore { mod ucore {
use super::*; use super::*;
extern { extern {
pub fn kmalloc(size: usize) -> *mut u8;
pub fn kfree(ptr: *mut u8);
pub fn inode_kill(inode: &mut INode); pub fn inode_kill(inode: &mut INode);
pub fn create_inode_for_sfs(ops: &INodeOps, fs: &mut Fs) -> *mut INode; pub fn create_inode_for_sfs(ops: &INodeOps, fs: &mut Fs) -> *mut INode;
pub fn create_fs_for_sfs(ops: &FsOps) -> *mut Fs; pub fn create_fs_for_sfs(ops: &FsOps) -> *mut Fs;
@ -46,13 +50,10 @@ macro_rules! cprintf {
// Exports for ucore // Exports for ucore
static SFS_INODE_OPS: INodeOps = INodeOps::from_rust_inode::<sfs::INode>();
static SFS_FS_OPS: FsOps = FsOps::from_rust_fs::<sfs::SimpleFileSystem>();
#[no_mangle] #[no_mangle]
pub extern fn sfs_do_mount(dev: *mut Device, fs_store: &mut *mut Fs) -> ErrorCode { pub extern fn sfs_do_mount(dev: *mut Device, fs_store: &mut *mut Fs) -> ErrorCode {
use self::ucore::*; use sfs;
let fs = unsafe{create_fs_for_sfs(&SFS_FS_OPS)}; let fs = unsafe{ ucore::create_fs_for_sfs(&FS_OPS) };
debug_assert!(!dev.is_null()); debug_assert!(!dev.is_null());
let mut device = unsafe{ Box::from_raw(dev) }; // TODO: fix unsafe let mut device = unsafe{ Box::from_raw(dev) }; // TODO: fix unsafe
device.open(); device.open();
@ -68,7 +69,7 @@ pub extern fn sfs_do_mount(dev: *mut Device, fs_store: &mut *mut Fs) -> ErrorCod
/// Match struct `inode` in ucore `kern/fs/vfs/inode.h` /// Match struct `inode` in ucore `kern/fs/vfs/inode.h`
#[repr(C)] #[repr(C)]
struct INode { struct INode {
inode: Rc<RefCell<vfs::INode>>, inode: vfs::INodePtr,
// ... fields handled extern // ... fields handled extern
} }
@ -249,9 +250,6 @@ pub enum ErrorCode {
// Wrapper functions // Wrapper functions
use vfs;
use sfs;
impl AsRef<[u8]> for IoBuf { impl AsRef<[u8]> for IoBuf {
fn as_ref(&self) -> &[u8] { fn as_ref(&self) -> &[u8] {
unsafe{ slice::from_raw_parts(self.base, self.resident as usize) } unsafe{ slice::from_raw_parts(self.base, self.resident as usize) }
@ -332,15 +330,28 @@ impl Device {
} }
impl INode { impl INode {
fn new(fs: &mut Fs) -> &mut Self { fn get_or_create(vfs_inode: vfs::INodePtr, fs: &mut Fs) -> *mut Self {
use self::ucore::*; static mut MAPPER: *mut BTreeMap<usize, *mut INode> = 0 as *mut _;
let inode = unsafe{ create_inode_for_sfs(&SFS_INODE_OPS, fs) };
assert!(!inode.is_null()); unsafe {if MAPPER.is_null() {
unsafe{ &mut *inode } MAPPER = Box::into_raw(Box::new(BTreeMap::<usize, *mut INode>::new()));
} }
let mapper = unsafe{ &mut *MAPPER };
let addr = vfs_inode.as_ptr() as *const () as usize;
match mapper.get(&addr) {
Some(&ptr) => ptr,
None => {
let inode = unsafe{ ucore::create_inode_for_sfs(&INODE_OPS, fs) };
assert!(!inode.is_null());
mapper.insert(addr, inode);
inode
},
}
} }
fn drop(&mut self) { fn drop(&mut self) {
use self::ucore::*; unsafe{ ucore::inode_kill(self) };
unsafe{ inode_kill(self) };
} }
} }
@ -355,114 +366,116 @@ impl From<vfs::FileInfo> for Stat {
} }
} }
impl INodeOps { static INODE_OPS: INodeOps = {
const fn from_rust_inode<T: vfs::INode>() -> Self { impl Deref for INode {
extern fn open(inode: &mut INode, flags: u32) -> ErrorCode { type Target = vfs::INodePtr;
ErrorCode::Unimplemented
}
extern fn close(inode: &mut INode) -> ErrorCode {
ErrorCode::Unimplemented
}
extern fn read(inode: &mut INode, buf: &mut IoBuf) -> ErrorCode {
let inode = &inode.inode;
let len = inode.borrow().read_at(buf.offset as usize, buf.as_mut()).unwrap();
buf.skip(len);
ErrorCode::Ok
}
extern fn write(inode: &mut INode, buf: &mut IoBuf) -> ErrorCode {
let inode = &inode.inode;
let len = inode.borrow().write_at(buf.offset as usize, buf.as_ref()).unwrap();
buf.skip(len);
ErrorCode::Ok
}
extern fn fstat(inode: &mut INode, stat: &mut Stat) -> ErrorCode {
let inode = &inode.inode;
let info = inode.borrow().info().unwrap();
*stat = Stat::from(info);
ErrorCode::Ok
}
extern fn fsync(inode: &mut INode) -> ErrorCode {
inode.inode.borrow_mut().sync().unwrap();
ErrorCode::Ok
}
extern fn namefile(inode: &mut INode, buf: &mut IoBuf) -> ErrorCode {
ErrorCode::Unimplemented
}
extern fn getdirentry(inode: &mut INode, buf: &mut IoBuf) -> ErrorCode {
ErrorCode::Unimplemented
}
extern fn reclaim(inode: &mut INode) -> ErrorCode {
ErrorCode::Unimplemented
}
extern fn gettype(inode: &mut INode, type_store: &mut u32) -> ErrorCode {
let inode = &inode.inode;
let info = inode.borrow().info().unwrap();
*type_store = info.type_ as u32;
ErrorCode::Ok
}
extern fn tryseek(inode: &mut INode, pos: i32) -> ErrorCode {
ErrorCode::Unimplemented
}
extern fn truncate(inode: &mut INode, len: i32) -> ErrorCode {
ErrorCode::Unimplemented
}
extern fn create(inode: &mut INode, name: *const u8, excl: bool, inode_store: &mut &mut INode) -> ErrorCode {
ErrorCode::Unimplemented
}
extern fn lookup(inode: &mut INode, path: &mut u8, inode_store: &mut &mut INode) -> ErrorCode {
ErrorCode::Unimplemented
}
extern fn ioctl(inode: &mut INode, op: i32, data: &mut u8) -> ErrorCode {
ErrorCode::Unimplemented
}
INodeOps {
magic: 0x8c4ba476,
open, close, read, write, fstat, fsync, namefile, getdirentry,
reclaim, gettype, tryseek, truncate, create, lookup, ioctl,
}
}
}
impl FsOps { fn deref(&self) -> &Self::Target {
const fn from_rust_fs<T: vfs::FileSystem>() -> Self { &self.inode
extern fn sync(fs: &mut Fs) -> ErrorCode {
fs.fs.sync().unwrap();
ErrorCode::Ok
}
extern fn get_root(fs: &mut Fs) -> *mut INode {
fs.fs.root_inode();
unimplemented!();
}
extern fn unmount(fs: &mut Fs) -> ErrorCode {
unimplemented!();
} }
extern fn cleanup(fs: &mut Fs) {
unimplemented!();
}
FsOps { sync, get_root, unmount, cleanup }
} }
}
mod allocator { extern fn open(inode: &mut INode, flags: u32) -> ErrorCode {
use alloc::heap::{Alloc, AllocErr, Layout}; ErrorCode::Unimplemented
}
extern { extern fn close(inode: &mut INode) -> ErrorCode {
fn kmalloc(size: usize) -> *mut u8; ErrorCode::Unimplemented
fn kfree(ptr: *mut u8); }
extern fn read(inode: &mut INode, buf: &mut IoBuf) -> ErrorCode {
let len = inode.borrow().read_at(buf.offset as usize, buf.as_mut()).unwrap();
buf.skip(len);
ErrorCode::Ok
}
extern fn write(inode: &mut INode, buf: &mut IoBuf) -> ErrorCode {
let len = inode.borrow().write_at(buf.offset as usize, buf.as_ref()).unwrap();
buf.skip(len);
ErrorCode::Ok
}
extern fn fstat(inode: &mut INode, stat: &mut Stat) -> ErrorCode {
let info = inode.borrow().info().unwrap();
*stat = Stat::from(info);
ErrorCode::Ok
}
extern fn fsync(inode: &mut INode) -> ErrorCode {
inode.borrow_mut().sync().unwrap();
ErrorCode::Ok
}
extern fn namefile(inode: &mut INode, buf: &mut IoBuf) -> ErrorCode {
ErrorCode::Unimplemented
}
extern fn getdirentry(inode: &mut INode, buf: &mut IoBuf) -> ErrorCode {
ErrorCode::Unimplemented
}
extern fn reclaim(inode: &mut INode) -> ErrorCode {
ErrorCode::Unimplemented
}
extern fn gettype(inode: &mut INode, type_store: &mut u32) -> ErrorCode {
let info = inode.borrow().info().unwrap();
*type_store = info.type_ as u32;
ErrorCode::Ok
} }
extern fn tryseek(inode: &mut INode, pos: i32) -> ErrorCode {
ErrorCode::Unimplemented
}
extern fn truncate(inode: &mut INode, len: i32) -> ErrorCode {
ErrorCode::Unimplemented
}
extern fn create(inode: &mut INode, name: *const u8, excl: bool, inode_store: &mut &mut INode) -> ErrorCode {
ErrorCode::Unimplemented
}
extern fn lookup(inode: &mut INode, path: &mut u8, inode_store: &mut &mut INode) -> ErrorCode {
ErrorCode::Unimplemented
}
extern fn ioctl(inode: &mut INode, op: i32, data: &mut u8) -> ErrorCode {
ErrorCode::Unimplemented
}
INodeOps {
magic: 0x8c4ba476,
open, close, read, write, fstat, fsync, namefile, getdirentry,
reclaim, gettype, tryseek, truncate, create, lookup, ioctl,
}
};
pub struct UcoreAllocator; static FS_OPS: FsOps = {
impl Deref for Fs {
type Target = Rc<vfs::FileSystem>;
unsafe impl<'a> Alloc for &'a UcoreAllocator { fn deref(&self) -> &Self::Target {
unsafe fn alloc(&mut self, layout: Layout) -> Result<*mut u8, AllocErr> { &self.fs
const NULL: *mut u8 = 0 as *mut u8;
match kmalloc(layout.size()) {
NULL => Err(AllocErr::Exhausted { request: layout }),
ptr => Ok(ptr),
}
} }
unsafe fn dealloc(&mut self, ptr: *mut u8, layout: Layout) { }
kfree(ptr);
extern fn sync(fs: &mut Fs) -> ErrorCode {
cprintf!("fs.sync");
fs.sync().unwrap();
ErrorCode::Ok
}
extern fn get_root(fs: &mut Fs) -> *mut INode {
cprintf!("fs.getroot");
let inode = fs.root_inode();
INode::get_or_create(inode, fs)
}
extern fn unmount(fs: &mut Fs) -> ErrorCode {
unimplemented!();
}
extern fn cleanup(fs: &mut Fs) {
unimplemented!();
}
FsOps { sync, get_root, unmount, cleanup }
};
/// Allocator supported by ucore functions
pub struct UcoreAllocator;
unsafe impl<'a> Alloc for &'a UcoreAllocator {
unsafe fn alloc(&mut self, layout: Layout) -> Result<*mut u8, AllocErr> {
const NULL: *mut u8 = 0 as *mut u8;
match ucore::kmalloc(layout.size()) {
NULL => Err(AllocErr::Exhausted { request: layout }),
ptr => Ok(ptr),
} }
} }
unsafe fn dealloc(&mut self, ptr: *mut u8, layout: Layout) {
ucore::kfree(ptr);
}
} }

@ -139,3 +139,14 @@ fn create_then_lookup() {
sfs.sync().unwrap(); sfs.sync().unwrap();
} }
#[test]
fn rc_layout() {
// [usize, usize, T]
// ^ start ^ Rc::into_raw
let p = Rc::new([2u8; 5]);
let ptr = Rc::into_raw(p);
let start = unsafe{ (ptr as *const usize).offset(-2) };
let ns = unsafe{ &*(start as *const [usize; 2]) };
assert_eq!(ns, &[1usize, 1]);
}

@ -23,8 +23,8 @@ pub trait INode: Debug {
// fn reclaim(&mut self) -> Result<()>; // fn reclaim(&mut self) -> Result<()>;
// fn try_seek(&mut self, offset: u64) -> Result<()>; // fn try_seek(&mut self, offset: u64) -> Result<()>;
fn resize(&mut self, len: usize) -> Result<()>; fn resize(&mut self, len: usize) -> Result<()>;
fn create(&mut self, name: &'static str, type_: FileType) -> Result<Rc<RefCell<INode>>>; fn create(&mut self, name: &'static str, type_: FileType) -> Result<INodePtr>;
fn lookup(&self, path: &'static str) -> Result<Rc<RefCell<INode>>>; fn lookup(&self, path: &'static str) -> Result<INodePtr>;
fn list(&self) -> Result<Vec<String>>; fn list(&self) -> Result<Vec<String>>;
// fn io_ctrl(&mut self, op: u32, data: &[u8]) -> Result<()>; // fn io_ctrl(&mut self, op: u32, data: &[u8]) -> Result<()>;
} }
@ -47,7 +47,9 @@ pub type Result<T> = core::result::Result<T, ()>;
/// Abstract filesystem /// Abstract filesystem
pub trait FileSystem { pub trait FileSystem {
fn sync(&self) -> Result<()>; fn sync(&self) -> Result<()>;
fn root_inode(&self) -> Rc<RefCell<INode>>; fn root_inode(&self) -> INodePtr;
// fn unmount(&self) -> Result<()>; // fn unmount(&self) -> Result<()>;
// fn cleanup(&self); // fn cleanup(&self);
} }
pub type INodePtr = Rc<RefCell<INode>>;
Loading…
Cancel
Save