From a56c4819709634be1ca3bfcc594d633a3afa986f Mon Sep 17 00:00:00 2001 From: Paul Pan Date: Tue, 7 May 2024 23:50:52 +0800 Subject: [PATCH] feat: split lib/vspace: 1. `addr` move to `utils` 2. prepare to refactor `vspace` [skip_ci] --- Cargo.lock | 19 +------- kernel/Cargo.toml | 1 - kernel/src/arch/riscv/backtrace.rs | 2 +- kernel/src/arch/riscv/entry.rs | 2 +- kernel/src/arch/riscv/layout.rs | 2 +- kernel/src/drivers/serial/sifive.rs | 2 +- kernel/src/drivers/serial/uart16550.rs | 2 +- kernel/src/objects/cap.rs | 2 +- kernel/src/objects/cnode.rs | 2 +- kernel/src/objects/null.rs | 2 +- kernel/src/objects/untyped.rs | 2 +- kernel/src/plat/backtrace.rs | 2 +- lib/allocator/Cargo.toml | 3 +- lib/allocator/src/bitmap.rs | 2 +- lib/allocator/src/block.rs | 2 +- lib/allocator/src/freelist.rs | 2 +- lib/utils/Cargo.toml | 5 +-- lib/{vspace => utils}/src/addr.rs | 0 lib/utils/src/extern_addr.rs | 2 +- lib/utils/src/lib.rs | 2 + lib/vspace/Cargo.toml | 13 ------ lib/vspace/src/lib.rs | 9 ---- lib/vspace/src/paging/entry.rs | 28 ------------ lib/vspace/src/paging/mod.rs | 5 --- lib/vspace/src/paging/table.rs | 60 -------------------------- uapi/Cargo.toml | 1 - uapi/src/vspace.rs | 2 - 27 files changed, 20 insertions(+), 156 deletions(-) rename lib/{vspace => utils}/src/addr.rs (100%) delete mode 100644 lib/vspace/Cargo.toml delete mode 100644 lib/vspace/src/lib.rs delete mode 100644 lib/vspace/src/paging/entry.rs delete mode 100644 lib/vspace/src/paging/mod.rs delete mode 100644 lib/vspace/src/paging/table.rs diff --git a/Cargo.lock b/Cargo.lock index b753c21..986e98c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -10,12 +10,11 @@ checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" [[package]] name = "allocator" -version = "0.1.0" +version = "0.1.1" dependencies = [ "spin", "static_assertions", "utils", - "vspace", ] [[package]] @@ -110,7 +109,6 @@ dependencies = [ "uapi", "uart_16550", "utils", - "vspace", ] [[package]] @@ -254,7 +252,6 @@ version = "0.1.0" dependencies = [ "num-derive", "num-traits", - "vspace", ] [[package]] @@ -276,19 +273,7 @@ checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" [[package]] name = "utils" -version = "0.1.0" -dependencies = [ - "vspace", -] - -[[package]] -name = "vspace" -version = "0.1.0" -dependencies = [ - "bitflags 2.5.0", - "num-derive", - "num-traits", -] +version = "0.1.1" [[package]] name = "x86" diff --git a/kernel/Cargo.toml b/kernel/Cargo.toml index 467fbaa..2ad6c86 100644 --- a/kernel/Cargo.toml +++ b/kernel/Cargo.toml @@ -23,7 +23,6 @@ riscv = [] uapi = { path = "../uapi" } allocator = { path = "../lib/allocator" } utils = { path = "../lib/utils", default-features = false } -vspace = { path = "../lib/vspace", default-features = false } bitflags = "2.5" cfg-if = "1.0" diff --git a/kernel/src/arch/riscv/backtrace.rs b/kernel/src/arch/riscv/backtrace.rs index d659b94..8f70104 100644 --- a/kernel/src/arch/riscv/backtrace.rs +++ b/kernel/src/arch/riscv/backtrace.rs @@ -1,6 +1,6 @@ use super::layout::{BSS_END, BSS_START, TEXT_END, TEXT_START}; use crate::plat::backtrace::FrameWalker; -use vspace::addr::{AddressOps, VirtAddr}; +use utils::addr::{AddressOps, VirtAddr}; impl FrameWalker { fn read_fp() -> usize { diff --git a/kernel/src/arch/riscv/entry.rs b/kernel/src/arch/riscv/entry.rs index 4206bdd..6ea261d 100644 --- a/kernel/src/arch/riscv/entry.rs +++ b/kernel/src/arch/riscv/entry.rs @@ -3,8 +3,8 @@ use crate::arch::layout::{mmap_phys_to_virt, zero_bss}; use crate::arch::vspace::{setup_kernel_paging, setup_memory}; use crate::entry::{rust_main, HART_ID}; use crate::plat::console::mute_console; +use utils::addr::AddressOps; use utils::atomic::AtomicConstPtr; -use vspace::addr::AddressOps; #[naked] #[no_mangle] diff --git a/kernel/src/arch/riscv/layout.rs b/kernel/src/arch/riscv/layout.rs index f63292f..f8b09f0 100644 --- a/kernel/src/arch/riscv/layout.rs +++ b/kernel/src/arch/riscv/layout.rs @@ -1,7 +1,7 @@ use core::alloc::Layout; +use utils::addr::{AddressOps, PhysAddr, VirtAddr}; use utils::extern_addr::ExternSymbol; use utils::size::KIB; -use vspace::addr::{AddressOps, PhysAddr, VirtAddr}; extern "C" { pub static KERNEL_START: ExternSymbol; diff --git a/kernel/src/drivers/serial/sifive.rs b/kernel/src/drivers/serial/sifive.rs index 603ba54..cbeb280 100644 --- a/kernel/src/drivers/serial/sifive.rs +++ b/kernel/src/drivers/serial/sifive.rs @@ -4,7 +4,7 @@ use crate::drivers::Driver; use crate::plat::console::ConsoleDevice; use core::sync::atomic::{AtomicPtr, Ordering}; use fdt::node::FdtNode; -use vspace::addr::PhysAddr; +use utils::addr::PhysAddr; // https://static.dev.sifive.com/FU540-C000-v1.0.pdf diff --git a/kernel/src/drivers/serial/uart16550.rs b/kernel/src/drivers/serial/uart16550.rs index ab87164..2fe22a7 100644 --- a/kernel/src/drivers/serial/uart16550.rs +++ b/kernel/src/drivers/serial/uart16550.rs @@ -4,7 +4,7 @@ use crate::drivers::Driver; use crate::plat::console::ConsoleDevice; use fdt::node::FdtNode; use uart_16550::MmioSerialPort; -use vspace::addr::PhysAddr; +use utils::addr::PhysAddr; pub struct Uart16550 where Uart16550: SerialDriver diff --git a/kernel/src/objects/cap.rs b/kernel/src/objects/cap.rs index 53610f4..51e5db4 100644 --- a/kernel/src/objects/cap.rs +++ b/kernel/src/objects/cap.rs @@ -1,11 +1,11 @@ use crate::objects::null::NullCap; use core::cell::Cell; use uapi::cap::ObjectType; +use utils::addr::PhysAddr; use utils::{ linked_list::{Link, LinkHelper}, LinkHelperImpl, }; -use vspace::addr::PhysAddr; /// RawCap is the specific implementation of capability which stores in CNode #[derive(Copy, Clone, Default, PartialEq, Eq)] diff --git a/kernel/src/objects/cnode.rs b/kernel/src/objects/cnode.rs index 11fea75..0826e5e 100644 --- a/kernel/src/objects/cnode.rs +++ b/kernel/src/objects/cnode.rs @@ -4,8 +4,8 @@ use super::{ }; use crate::arch::layout::mmap_phys_to_virt; use uapi::{cap::ObjectType, error::CapFault}; +use utils::addr::{AddressOps, PhysAddr}; use utils::MASK; -use vspace::addr::{AddressOps, PhysAddr}; /// CNodeObject is a array of Capabilities (`RawCap`) /// The size of the array is stored in CNodeCap diff --git a/kernel/src/objects/null.rs b/kernel/src/objects/null.rs index ac81401..42b8cf7 100644 --- a/kernel/src/objects/null.rs +++ b/kernel/src/objects/null.rs @@ -1,7 +1,7 @@ use super::cap::RawCap; use super::{Cap, KernelObject}; use uapi::cap::ObjectType; -use vspace::addr::PhysAddr; +use utils::addr::PhysAddr; /// NullObject is used as empty (capability) slot pub struct NullObject {} diff --git a/kernel/src/objects/untyped.rs b/kernel/src/objects/untyped.rs index c7c25a2..a6c30dd 100644 --- a/kernel/src/objects/untyped.rs +++ b/kernel/src/objects/untyped.rs @@ -4,9 +4,9 @@ use super::null::NullCap; use super::{Cap, KernelObject}; use uapi::cap::ObjectType; use uapi::error::{SysError, SysResult}; +use utils::addr::{align_up, PhysAddr}; use utils::then::Then; use utils::MASK; -use vspace::addr::{align_up, PhysAddr}; /// UntypedObject is used as raw memory (associated with PhysAddr) /// It can be further retyped to other objects (TCB, CNode ...) diff --git a/kernel/src/plat/backtrace.rs b/kernel/src/plat/backtrace.rs index cf458fd..fde7ced 100644 --- a/kernel/src/plat/backtrace.rs +++ b/kernel/src/plat/backtrace.rs @@ -1,5 +1,5 @@ use log::error; -use vspace::addr::VirtAddr; +use utils::addr::VirtAddr; #[derive(Clone, Copy)] pub struct FrameWalker { diff --git a/lib/allocator/Cargo.toml b/lib/allocator/Cargo.toml index d5cb774..8b756a5 100644 --- a/lib/allocator/Cargo.toml +++ b/lib/allocator/Cargo.toml @@ -1,10 +1,9 @@ [package] name = "allocator" -version = "0.1.0" +version = "0.1.1" edition = "2021" [dependencies] -vspace = { path = "../vspace" } utils = { path = "../utils" } spin = "0.9" diff --git a/lib/allocator/src/bitmap.rs b/lib/allocator/src/bitmap.rs index 931d469..d706d55 100644 --- a/lib/allocator/src/bitmap.rs +++ b/lib/allocator/src/bitmap.rs @@ -1,7 +1,7 @@ use core::alloc::{GlobalAlloc, Layout}; use core::ptr::null_mut; use spin::Mutex; -use vspace::addr::{AddressOps, PhysAddr}; +use utils::addr::{AddressOps, PhysAddr}; pub trait BitmapCfg: Copy + Clone { const CAPACITY: usize; diff --git a/lib/allocator/src/block.rs b/lib/allocator/src/block.rs index 8eacfda..b2aba4d 100644 --- a/lib/allocator/src/block.rs +++ b/lib/allocator/src/block.rs @@ -1,6 +1,6 @@ use core::alloc::Layout; use core::cmp::min; -use vspace::addr::{AddressOps, PhysAddr}; +use utils::addr::{AddressOps, PhysAddr}; #[derive(Copy, Clone, Debug)] struct Block { diff --git a/lib/allocator/src/freelist.rs b/lib/allocator/src/freelist.rs index 304ae2d..0fdfe41 100644 --- a/lib/allocator/src/freelist.rs +++ b/lib/allocator/src/freelist.rs @@ -3,8 +3,8 @@ use core::alloc::{GlobalAlloc, Layout}; use core::fmt::Debug; use spin::Mutex; +use utils::addr::{AddressOps, PhysAddr}; use utils::then::Then; -use vspace::addr::{AddressOps, PhysAddr}; struct ListNode { size: usize, diff --git a/lib/utils/Cargo.toml b/lib/utils/Cargo.toml index a70827d..7874f83 100644 --- a/lib/utils/Cargo.toml +++ b/lib/utils/Cargo.toml @@ -1,11 +1,8 @@ [package] name = "utils" -version = "0.1.0" +version = "0.1.1" edition = "2021" [features] default = [] legacy = [] - -[dependencies] -vspace = { path = "../vspace" } diff --git a/lib/vspace/src/addr.rs b/lib/utils/src/addr.rs similarity index 100% rename from lib/vspace/src/addr.rs rename to lib/utils/src/addr.rs diff --git a/lib/utils/src/extern_addr.rs b/lib/utils/src/extern_addr.rs index af97664..7fc1ce4 100644 --- a/lib/utils/src/extern_addr.rs +++ b/lib/utils/src/extern_addr.rs @@ -1,4 +1,4 @@ -use vspace::addr::VirtAddr; +use crate::addr::VirtAddr; extern "C" { pub type ExternSymbol; diff --git a/lib/utils/src/lib.rs b/lib/utils/src/lib.rs index a84826c..51c08a8 100644 --- a/lib/utils/src/lib.rs +++ b/lib/utils/src/lib.rs @@ -1,6 +1,8 @@ #![no_std] #![feature(extern_types)] +#![feature(step_trait)] +pub mod addr; pub mod assert; pub mod atomic; pub mod bin; diff --git a/lib/vspace/Cargo.toml b/lib/vspace/Cargo.toml deleted file mode 100644 index 2000d3f..0000000 --- a/lib/vspace/Cargo.toml +++ /dev/null @@ -1,13 +0,0 @@ -[package] -name = "vspace" -version = "0.1.0" -edition = "2021" - -[features] -default = [] -legacy = [] - -[dependencies] -bitflags = "2.4" -num-derive = "0.4" -num-traits = { version = "0.2", default-features = false } diff --git a/lib/vspace/src/lib.rs b/lib/vspace/src/lib.rs deleted file mode 100644 index b9a2f96..0000000 --- a/lib/vspace/src/lib.rs +++ /dev/null @@ -1,9 +0,0 @@ -#![cfg_attr(not(test), no_std)] -#![feature(const_mut_refs)] -#![feature(step_trait)] - -#[macro_use] -extern crate num_derive; - -pub mod addr; -pub mod paging; diff --git a/lib/vspace/src/paging/entry.rs b/lib/vspace/src/paging/entry.rs deleted file mode 100644 index ad49d5a..0000000 --- a/lib/vspace/src/paging/entry.rs +++ /dev/null @@ -1,28 +0,0 @@ -use crate::addr::PhysAddr; -use bitflags::bitflags; -use core::fmt::Debug; - -bitflags! { - #[derive(Debug, Copy, Clone)] - pub struct MapAttr: usize { - const PAGE_TABLE = 1 << 0; - const READABLE = 1 << 1; - const WRITABLE = 1 << 2; - const EXECUTABLE = 1 << 3; - const USER_ACCESSIBLE = 1 << 4; - } -} - -pub trait EntryOps: Clone + Copy + Debug { - fn new_page(phys_addr: PhysAddr, attr: MapAttr) -> Self; - fn new_table(phys_addr: PhysAddr) -> Self; - - fn addr(&self) -> PhysAddr; - fn attr(&self) -> MapAttr; - - fn set_addr(&mut self, addr: PhysAddr); - fn set_attr(&mut self, attr: MapAttr); - - fn is_valid(&self) -> bool; - fn is_leaf(&self) -> bool; -} diff --git a/lib/vspace/src/paging/mod.rs b/lib/vspace/src/paging/mod.rs deleted file mode 100644 index 7379868..0000000 --- a/lib/vspace/src/paging/mod.rs +++ /dev/null @@ -1,5 +0,0 @@ -mod entry; -mod table; - -pub use entry::*; -pub use table::*; diff --git a/lib/vspace/src/paging/table.rs b/lib/vspace/src/paging/table.rs deleted file mode 100644 index a19e5bb..0000000 --- a/lib/vspace/src/paging/table.rs +++ /dev/null @@ -1,60 +0,0 @@ -use super::{EntryOps, MapAttr}; -use crate::addr::{PhysAddr, VirtAddr}; -use core::fmt::Debug; - -#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, FromPrimitive, ToPrimitive)] -pub enum TableLevel { - Level0 = 0, // KiloPage - Level1 = 1, // MegaPage - Level2 = 2, // GigaPage - Level3 = 3, // TeraPage - Level4 = 4, // PetaPage -} - -impl TableLevel { - pub fn next(&self) -> Option { - match self { - Self::Level0 => None, - Self::Level1 => Some(Self::Level0), - Self::Level2 => Some(Self::Level1), - Self::Level3 => Some(Self::Level2), - Self::Level4 => Some(Self::Level3), - } - } - - pub fn previous(&self) -> Option { - match self { - Self::Level0 => Some(Self::Level1), - Self::Level1 => Some(Self::Level2), - Self::Level2 => Some(Self::Level3), - Self::Level3 => Some(Self::Level4), - Self::Level4 => None, - } - } -} - -#[derive(Debug)] -pub enum PageError { - AlreadyMapped(TableLevel), - MissingEntry(TableLevel), -} - -pub type PageResult = Result; - -pub trait TableOps: Debug { - type Entry: EntryOps; - const MAX_PAGE_SIZE: TableLevel; - const TABLE_SIZE: usize; - - /// # Safety - /// `location` must be a page-aligned virtual address and will not be dropped. - unsafe fn new(location: VirtAddr) -> &'static mut Self; - - fn map(&mut self, from: VirtAddr, to: PhysAddr, attr: MapAttr, level: TableLevel) -> PageResult; - fn unmap(&mut self, vaddr: VirtAddr) -> PageResult; - - fn lookup(&mut self, vaddr: VirtAddr) -> Option<&Self::Entry>; - fn lookup_mut(&mut self, vaddr: VirtAddr) -> Option<&mut Self::Entry>; - - fn translate(&mut self, vaddr: VirtAddr) -> Option; -} diff --git a/uapi/Cargo.toml b/uapi/Cargo.toml index e1cae26..4e9f8fb 100644 --- a/uapi/Cargo.toml +++ b/uapi/Cargo.toml @@ -4,6 +4,5 @@ version = "0.1.0" edition = "2021" [dependencies] -vspace = { path = "../lib/vspace" } num-traits = { version = "0.2", default-features = false } num-derive = "0.4" diff --git a/uapi/src/vspace.rs b/uapi/src/vspace.rs index 5cca885..1b74af9 100644 --- a/uapi/src/vspace.rs +++ b/uapi/src/vspace.rs @@ -1,5 +1,3 @@ -pub use vspace::paging::MapAttr; - // TODO: Only support leaf page for now, no huge page support! pub const FRAME_SIZE: usize = 4096;