format files

This commit is contained in:
Yuke Peng
2022-11-10 18:14:42 -08:00
parent 7be7775f97
commit 40cbd93ae8
23 changed files with 450 additions and 426 deletions

View File

@ -1,6 +1,9 @@
use proc_macro2::TokenStream; use proc_macro2::TokenStream;
use quote::quote; use quote::quote;
use syn::{parse_macro_input, Data, DataStruct, DeriveInput, Fields, DataEnum, punctuated::Punctuated, token::Comma, Field}; use syn::{
parse_macro_input, punctuated::Punctuated, token::Comma, Data, DataEnum, DataStruct,
DeriveInput, Field, Fields,
};
#[proc_macro_derive(Pod)] #[proc_macro_derive(Pod)]
pub fn derive_pod(input_token: proc_macro::TokenStream) -> proc_macro::TokenStream { pub fn derive_pod(input_token: proc_macro::TokenStream) -> proc_macro::TokenStream {

View File

@ -3,12 +3,12 @@
pub mod framebuffer; pub mod framebuffer;
mod io_port; mod io_port;
pub mod pci; pub mod pci;
pub mod serial;
mod pic; mod pic;
pub mod serial;
pub use pic::{TimerCallback, TIMER_FREQ};
pub(crate) use pic::{add_timeout_list,TICK};
pub use self::io_port::IoPort; pub use self::io_port::IoPort;
pub(crate) use pic::{add_timeout_list, TICK};
pub use pic::{TimerCallback, TIMER_FREQ};
pub(crate) fn init(framebuffer: &'static mut bootloader::boot_info::FrameBuffer) { pub(crate) fn init(framebuffer: &'static mut bootloader::boot_info::FrameBuffer) {
framebuffer::init(framebuffer); framebuffer::init(framebuffer);

View File

@ -136,7 +136,6 @@ impl TimerCallback {
pub fn is_enable(&self) -> bool { pub fn is_enable(&self) -> bool {
*self.enable *self.enable
} }
} }
impl PartialEq for TimerCallback { impl PartialEq for TimerCallback {
@ -167,14 +166,9 @@ pub fn add_timeout_list<F, T>(timeout: u64, data: T, callback: F) -> Arc<TimerCa
where where
F: Fn(&TimerCallback) + Send + Sync + 'static, F: Fn(&TimerCallback) + Send + Sync + 'static,
T: Any + Send + Sync, T: Any + Send + Sync,
{ {
unsafe { unsafe {
let timer_callback = TimerCallback::new( let timer_callback = TimerCallback::new(TICK + timeout, Arc::new(data), Box::new(callback));
TICK + timeout,
Arc::new(data),
Box::new(callback),
);
let arc = Arc::new(timer_callback); let arc = Arc::new(timer_callback);
TIMEOUT_LIST.get().push(arc.clone()); TIMEOUT_LIST.get().push(arc.clone());
arc arc

View File

@ -1,8 +1,11 @@
//! Timer. //! Timer.
use crate::{prelude::*, device::{TimerCallback, TICK,TIMER_FREQ}}; use crate::{
use spin::Mutex; device::{TimerCallback, TICK, TIMER_FREQ},
prelude::*,
};
use core::time::Duration; use core::time::Duration;
use spin::Mutex;
/// A timer invokes a callback function after a specified span of time elapsed. /// A timer invokes a callback function after a specified span of time elapsed.
/// ///
@ -41,7 +44,10 @@ impl Timer {
where where
F: Fn(Arc<Timer>) + Send + Sync + 'static, F: Fn(Arc<Timer>) + Send + Sync + 'static,
{ {
Ok(Arc::new(Self { function: Arc::new(f),inner:Mutex::new(TimerInner::default()) })) Ok(Arc::new(Self {
function: Arc::new(f),
inner: Mutex::new(TimerInner::default()),
}))
} }
/// Set a timeout value. /// Set a timeout value.
@ -53,17 +59,24 @@ impl Timer {
match &lock.timer_callback { match &lock.timer_callback {
Some(callback) => { Some(callback) => {
callback.disable(); callback.disable();
}, }
None => {}, None => {}
} }
let tick_count = timeout.as_secs() * TIMER_FREQ let tick_count = timeout.as_secs() * TIMER_FREQ
+ if timeout.subsec_nanos() !=0{(timeout.subsec_nanos() as u64 - 1)/NANOS_DIVIDE + 1} else {0}; + if timeout.subsec_nanos() != 0 {
(timeout.subsec_nanos() as u64 - 1) / NANOS_DIVIDE + 1
} else {
0
};
unsafe { unsafe {
lock.start_tick = TICK; lock.start_tick = TICK;
lock.timeout_tick = TICK + tick_count; lock.timeout_tick = TICK + tick_count;
} }
lock.timer_callback = Some(crate::device::add_timeout_list(tick_count, self.clone(), timer_callback)); lock.timer_callback = Some(crate::device::add_timeout_list(
tick_count,
self.clone(),
timer_callback,
));
} }
/// Returns the remaining timeout value. /// Returns the remaining timeout value.

View File

@ -4,9 +4,9 @@ use crate::process::Process;
use alloc::sync::Arc; use alloc::sync::Arc;
use alloc::vec::Vec; use alloc::vec::Vec;
use kxos_frame::info; use kxos_frame::info;
use kxos_frame_pod_derive::Pod;
use kxos_pci::PCIDevice; use kxos_pci::PCIDevice;
use kxos_virtio::PCIVirtioDevice; use kxos_virtio::PCIVirtioDevice;
use kxos_frame_pod_derive::Pod;
use lazy_static::lazy_static; use lazy_static::lazy_static;
use spin::mutex::Mutex; use spin::mutex::Mutex;

View File

@ -23,11 +23,11 @@ pub mod fs;
mod memory; mod memory;
pub mod prelude; pub mod prelude;
mod process; mod process;
pub mod rights;
pub mod syscall; pub mod syscall;
mod user_apps; mod user_apps;
mod util; mod util;
pub mod vm; pub mod vm;
pub mod rights;
#[macro_use] #[macro_use]
extern crate kxos_frame_pod_derive; extern crate kxos_frame_pod_derive;

View File

@ -1,5 +1,5 @@
use kxos_typeflags::type_flags;
use bitflags::bitflags; use bitflags::bitflags;
use kxos_typeflags::type_flags;
bitflags! { bitflags! {
/// Value-based access rights. /// Value-based access rights.
@ -54,10 +54,4 @@ type_flags! {
} }
/// The full set of access rights. /// The full set of access rights.
pub type Full = TRights![ pub type Full = TRights![Dup, Read, Write, Exec, Signal];
Dup,
Read,
Write,
Exec,
Signal
];

View File

@ -1,12 +1,15 @@
use core::ops::Range; use core::ops::Range;
use alloc::sync::Arc; use alloc::sync::Arc;
use kxos_frame::{vm::VmIo, Error};
use kxos_frame::prelude::Result; use kxos_frame::prelude::Result;
use kxos_frame::{vm::VmIo, Error};
use crate::{rights::Rights, vm::vmo::Vmo}; use crate::{rights::Rights, vm::vmo::Vmo};
use super::{Vmar, VmPerms, options::{VmarChildOptions, VmarMapOptions}, Vmar_}; use super::{
options::{VmarChildOptions, VmarMapOptions},
VmPerms, Vmar, Vmar_,
};
impl Vmar<Rights> { impl Vmar<Rights> {
/// Creates a root VMAR. /// Creates a root VMAR.
@ -52,7 +55,11 @@ impl Vmar<Rights> {
/// Memory permissions may be changed through the `protect` method, /// Memory permissions may be changed through the `protect` method,
/// which ensures that any updated memory permissions do not go beyond /// which ensures that any updated memory permissions do not go beyond
/// the access rights of the underlying VMOs. /// the access rights of the underlying VMOs.
pub fn new_map(&self, vmo: Vmo<Rights>, perms: VmPerms) -> Result<VmarMapOptions<Rights,Rights>> { pub fn new_map(
&self,
vmo: Vmo<Rights>,
perms: VmPerms,
) -> Result<VmarMapOptions<Rights, Rights>> {
let dup_self = self.dup()?; let dup_self = self.dup()?;
Ok(VmarMapOptions::new(dup_self, vmo, perms)) Ok(VmarMapOptions::new(dup_self, vmo, perms))
} }
@ -143,7 +150,6 @@ impl Vmar<Rights> {
Err(Error::AccessDenied) Err(Error::AccessDenied)
} }
} }
} }
impl VmIo for Vmar<Rights> { impl VmIo for Vmar<Rights> {

View File

@ -1,18 +1,18 @@
//! Virtual Memory Address Regions (VMARs). //! Virtual Memory Address Regions (VMARs).
mod static_cap;
mod dyn_cap; mod dyn_cap;
mod options; mod options;
mod static_cap;
use core::ops::Range;
use kxos_frame::vm::VmSpace;
use kxos_frame::vm::Vaddr;
use spin::Mutex;
use alloc::sync::Arc;
use kxos_frame::prelude::Result;
use kxos_frame::Error;
use crate::rights::Rights; use crate::rights::Rights;
use alloc::sync::Arc;
use bitflags::bitflags; use bitflags::bitflags;
use core::ops::Range;
use kxos_frame::prelude::Result;
use kxos_frame::vm::Vaddr;
use kxos_frame::vm::VmSpace;
use kxos_frame::Error;
use spin::Mutex;
/// Virtual Memory Address Regions (VMARs) are a type of capability that manages /// Virtual Memory Address Regions (VMARs) are a type of capability that manages
/// user address spaces. /// user address spaces.
@ -91,8 +91,6 @@ impl<R> Vmar<R> {
pub fn base(&self) -> Vaddr { pub fn base(&self) -> Vaddr {
self.0.base self.0.base
} }
} }
bitflags! { bitflags! {

View File

@ -1,11 +1,11 @@
//! Options for allocating child VMARs and creating mappings. //! Options for allocating child VMARs and creating mappings.
use kxos_frame::{config::PAGE_SIZE, vm::Vaddr};
use kxos_frame::prelude::Result; use kxos_frame::prelude::Result;
use kxos_frame::{config::PAGE_SIZE, vm::Vaddr};
use crate::vm::vmo::Vmo; use crate::vm::vmo::Vmo;
use super::{Vmar, VmPerms}; use super::{VmPerms, Vmar};
/// Options for allocating a child VMAR, which must not overlap with any /// Options for allocating a child VMAR, which must not overlap with any
/// existing mappings or child VMARs. /// existing mappings or child VMARs.

View File

@ -1,13 +1,16 @@
use core::ops::Range; use core::ops::Range;
use alloc::sync::Arc; use alloc::sync::Arc;
use kxos_frame::prelude::Result;
use kxos_frame::{vm::VmIo, Error}; use kxos_frame::{vm::VmIo, Error};
use kxos_rights_proc::require; use kxos_rights_proc::require;
use kxos_frame::prelude::Result;
use crate::{rights::*, vm::vmo::Vmo}; use crate::{rights::*, vm::vmo::Vmo};
use super::{Vmar, Vmar_, VmPerms, options::{VmarMapOptions, VmarChildOptions}}; use super::{
options::{VmarChildOptions, VmarMapOptions},
VmPerms, Vmar, Vmar_,
};
impl<R: TRights> Vmar<R> { impl<R: TRights> Vmar<R> {
/// Creates a root VMAR. /// Creates a root VMAR.
@ -156,7 +159,6 @@ impl<R: TRights> Vmar<R> {
Err(Error::AccessDenied) Err(Error::AccessDenied)
} }
} }
} }
impl<R: TRights> VmIo for Vmar<R> { impl<R: TRights> VmIo for Vmar<R> {

View File

@ -1,11 +1,14 @@
use core::ops::Range; use core::ops::Range;
use kxos_frame::{vm::VmIo, Error};
use kxos_frame::prelude::Result; use kxos_frame::prelude::Result;
use kxos_frame::{vm::VmIo, Error};
use crate::rights::{Rights, TRights}; use crate::rights::{Rights, TRights};
use super::{VmoChildOptions, options::{VmoSliceChild, VmoCowChild}, Vmo}; use super::{
options::{VmoCowChild, VmoSliceChild},
Vmo, VmoChildOptions,
};
impl Vmo<Rights> { impl Vmo<Rights> {
/// Creates a new slice VMO through a set of VMO child options. /// Creates a new slice VMO through a set of VMO child options.
@ -27,7 +30,10 @@ impl Vmo<Rights> {
/// ///
/// The new VMO child will be of the same capability flavor as the parent; /// The new VMO child will be of the same capability flavor as the parent;
/// so are the access rights. /// so are the access rights.
pub fn new_slice_child(&self, range: Range<usize>) -> Result<VmoChildOptions<Rights, VmoSliceChild>> { pub fn new_slice_child(
&self,
range: Range<usize>,
) -> Result<VmoChildOptions<Rights, VmoSliceChild>> {
let dup_self = self.dup()?; let dup_self = self.dup()?;
Ok(VmoChildOptions::new_slice_rights(dup_self, range)) Ok(VmoChildOptions::new_slice_rights(dup_self, range))
} }
@ -52,7 +58,10 @@ impl Vmo<Rights> {
/// The new VMO child will be of the same capability flavor as the parent. /// The new VMO child will be of the same capability flavor as the parent.
/// The child will be given the access rights of the parent /// The child will be given the access rights of the parent
/// plus the Write right. /// plus the Write right.
pub fn new_cow_child(&self, range: Range<usize>) -> Result<VmoChildOptions<Rights, VmoCowChild>> { pub fn new_cow_child(
&self,
range: Range<usize>,
) -> Result<VmoChildOptions<Rights, VmoCowChild>> {
let dup_self = self.dup()?; let dup_self = self.dup()?;
Ok(VmoChildOptions::new_cow(dup_self, range)) Ok(VmoChildOptions::new_cow(dup_self, range))
} }

View File

@ -2,22 +2,20 @@
use core::ops::Range; use core::ops::Range;
use kxos_frame::{prelude::Result, vm::Paddr, Error};
use crate::rights::Rights; use crate::rights::Rights;
use alloc::sync::Arc; use alloc::sync::Arc;
use bitflags::bitflags; use bitflags::bitflags;
use kxos_frame::{prelude::Result, vm::Paddr, Error};
mod static_cap;
mod dyn_cap; mod dyn_cap;
mod options; mod options;
mod pager; mod pager;
mod static_cap;
pub use options::{VmoOptions, VmoChildOptions}; pub use options::{VmoChildOptions, VmoOptions};
pub use pager::Pager; pub use pager::Pager;
use spin::Mutex; use spin::Mutex;
/// Virtual Memory Objects (VMOs) are a type of capability that represents a /// Virtual Memory Objects (VMOs) are a type of capability that represents a
/// range of memory pages. /// range of memory pages.
/// ///
@ -148,7 +146,6 @@ impl Vmo_ {
} }
} }
impl<R> Vmo<R> { impl<R> Vmo<R> {
/// Returns the size (in bytes) of a VMO. /// Returns the size (in bytes) of a VMO.
pub fn size(&self) -> usize { pub fn size(&self) -> usize {
@ -165,5 +162,4 @@ impl<R> Vmo<R> {
pub fn flags(&self) -> VmoFlags { pub fn flags(&self) -> VmoFlags {
self.0.flags() self.0.flags()
} }
} }

View File

@ -4,13 +4,13 @@ use core::marker::PhantomData;
use core::ops::Range; use core::ops::Range;
use alloc::sync::Arc; use alloc::sync::Arc;
use kxos_frame::vm::Paddr;
use kxos_frame::prelude::Result; use kxos_frame::prelude::Result;
use kxos_frame::vm::Paddr;
use kxos_rights_proc::require; use kxos_rights_proc::require;
use crate::rights::{Dup,TRights,Rights}; use crate::rights::{Dup, Rights, TRights};
use super::{Vmo, VmoFlags, Pager}; use super::{Pager, Vmo, VmoFlags};
/// Options for allocating a root VMO. /// Options for allocating a root VMO.
/// ///
@ -231,7 +231,9 @@ impl VmoChildOptions<Rights, VmoSliceChild> {
/// ///
/// The range of a child must be within that of the parent. /// The range of a child must be within that of the parent.
pub fn new_slice_rights(parent: Vmo<Rights>, range: Range<usize>) -> Self { pub fn new_slice_rights(parent: Vmo<Rights>, range: Range<usize>) -> Self {
parent.check_rights(Rights::DUP).expect("function new_slice_rights should called with rights Dup"); parent
.check_rights(Rights::DUP)
.expect("function new_slice_rights should called with rights Dup");
Self { Self {
flags: parent.flags() & Self::PARENT_FLAGS_MASK, flags: parent.flags() & Self::PARENT_FLAGS_MASK,
parent, parent,
@ -263,7 +265,8 @@ impl<R> VmoChildOptions<R, VmoCowChild> {
impl<R, C> VmoChildOptions<R, C> { impl<R, C> VmoChildOptions<R, C> {
/// Flags that a VMO child inherits from its parent. /// Flags that a VMO child inherits from its parent.
pub const PARENT_FLAGS_MASK: VmoFlags = VmoFlags::from_bits(VmoFlags::CONTIGUOUS.bits | VmoFlags::DMA.bits).unwrap(); pub const PARENT_FLAGS_MASK: VmoFlags =
VmoFlags::from_bits(VmoFlags::CONTIGUOUS.bits | VmoFlags::DMA.bits).unwrap();
/// Flags that a VMO child may differ from its parent. /// Flags that a VMO child may differ from its parent.
pub const CHILD_FLAGS_MASK: VmoFlags = VmoFlags::RESIZABLE; pub const CHILD_FLAGS_MASK: VmoFlags = VmoFlags::RESIZABLE;
@ -311,7 +314,6 @@ impl<R: TRights> VmoChildOptions<R, VmoCowChild> {
/// The child VMO is initially assigned all the parent's access rights /// The child VMO is initially assigned all the parent's access rights
/// plus the Write right. /// plus the Write right.
pub fn alloc<TRights>(mut self) -> Result<Vmo<TRights>> pub fn alloc<TRights>(mut self) -> Result<Vmo<TRights>>
where
// TODO: R1 must contain the Write right. To do so at the type level, // TODO: R1 must contain the Write right. To do so at the type level,
// we need to implement a type-level operator // we need to implement a type-level operator
// (say, `TRightsExtend(L, F)`) // (say, `TRightsExtend(L, F)`)

View File

@ -1,5 +1,5 @@
use kxos_frame::vm::VmFrame;
use kxos_frame::prelude::Result; use kxos_frame::prelude::Result;
use kxos_frame::vm::VmFrame;
/// Pagers provide frame to a VMO. /// Pagers provide frame to a VMO.
/// ///

View File

@ -1,12 +1,15 @@
use core::ops::Range; use core::ops::Range;
use kxos_frame::{vm::VmIo, Error};
use kxos_frame::prelude::Result; use kxos_frame::prelude::Result;
use kxos_frame::{vm::VmIo, Error};
use kxos_rights_proc::require; use kxos_rights_proc::require;
use crate::rights::*; use crate::rights::*;
use super::{Vmo, VmoChildOptions, options::{VmoSliceChild, VmoCowChild}}; use super::{
options::{VmoCowChild, VmoSliceChild},
Vmo, VmoChildOptions,
};
impl<R: TRights> Vmo<R> { impl<R: TRights> Vmo<R> {
/// Creates a new slice VMO through a set of VMO child options. /// Creates a new slice VMO through a set of VMO child options.
@ -29,7 +32,10 @@ impl<R: TRights> Vmo<R> {
/// The new VMO child will be of the same capability flavor as the parent; /// The new VMO child will be of the same capability flavor as the parent;
/// so are the access rights. /// so are the access rights.
#[require(R > Dup)] #[require(R > Dup)]
pub fn new_slice_child(&self, range: Range<usize>) -> Result<VmoChildOptions<R, VmoSliceChild>> { pub fn new_slice_child(
&self,
range: Range<usize>,
) -> Result<VmoChildOptions<R, VmoSliceChild>> {
let dup_self = self.dup()?; let dup_self = self.dup()?;
Ok(VmoChildOptions::new_slice(dup_self, range)) Ok(VmoChildOptions::new_slice(dup_self, range))
} }

View File

@ -1,8 +1,8 @@
use kxos_frame::Pod; use kxos_frame::Pod;
use kxos_frame_pod_derive::Pod;
use kxos_pci::capability::vendor::virtio::CapabilityVirtioData; use kxos_pci::capability::vendor::virtio::CapabilityVirtioData;
use kxos_pci::util::BAR; use kxos_pci::util::BAR;
use kxos_util::frame_ptr::InFramePtr; use kxos_util::frame_ptr::InFramePtr;
use kxos_frame_pod_derive::Pod;
pub const BLK_SIZE: usize = 512; pub const BLK_SIZE: usize = 512;

View File

@ -7,13 +7,12 @@ extern crate alloc;
use alloc::{sync::Arc, vec::Vec}; use alloc::{sync::Arc, vec::Vec};
use bitflags::bitflags; use bitflags::bitflags;
use kxos_frame::{info, offset_of, TrapFrame}; use kxos_frame::{info, offset_of, TrapFrame};
use kxos_frame_pod_derive::Pod;
use kxos_pci::util::{PCIDevice, BAR}; use kxos_pci::util::{PCIDevice, BAR};
use kxos_util::frame_ptr::InFramePtr; use kxos_util::frame_ptr::InFramePtr;
use kxos_frame_pod_derive::Pod;
use spin::{mutex::Mutex, MutexGuard}; use spin::{mutex::Mutex, MutexGuard};
use self::{block::VirtioBLKConfig, queue::VirtQueue}; use self::{block::VirtioBLKConfig, queue::VirtQueue};
use kxos_frame::Pod; use kxos_frame::Pod;

View File

@ -6,8 +6,8 @@ use bitflags::bitflags;
use core::sync::atomic::{fence, Ordering}; use core::sync::atomic::{fence, Ordering};
use kxos_frame::offset_of; use kxos_frame::offset_of;
use kxos_frame::Pod; use kxos_frame::Pod;
use kxos_util::frame_ptr::InFramePtr;
use kxos_frame_pod_derive::Pod; use kxos_frame_pod_derive::Pod;
use kxos_util::frame_ptr::InFramePtr;
#[derive(Debug)] #[derive(Debug)]
pub enum QueueError { pub enum QueueError {
InvalidArgs, InvalidArgs,

View File

@ -7,8 +7,8 @@ use bootloader::{entry_point, BootInfo};
use kxos_frame::timer::Timer; use kxos_frame::timer::Timer;
extern crate alloc; extern crate alloc;
use alloc::sync::Arc; use alloc::sync::Arc;
use core::time::Duration;
use core::panic::PanicInfo; use core::panic::PanicInfo;
use core::time::Duration;
use kxos_frame::println; use kxos_frame::println;
static mut TICK: usize = 0; static mut TICK: usize = 0;
@ -28,7 +28,9 @@ fn panic(info: &PanicInfo) -> ! {
#[test_case] #[test_case]
fn test_timer() { fn test_timer() {
println!("If you want to pass this test, you may need to enable the interrupt in kxos_frame/lib.rs"); println!(
"If you want to pass this test, you may need to enable the interrupt in kxos_frame/lib.rs"
);
println!("make sure the Timer irq number 32 handler won't panic"); println!("make sure the Timer irq number 32 handler won't panic");
unsafe { unsafe {
let timer = Timer::new(timer_callback).unwrap(); let timer = Timer::new(timer_callback).unwrap();