mirror of
https://github.com/panpaul/tiny_os
synced 2024-09-20 09:45:19 +08:00
feat: split lib/vspace:
1. `addr` move to `utils` 2. prepare to refactor `vspace` [skip_ci]
This commit is contained in:
parent
55147ad9ff
commit
a56c481970
19
Cargo.lock
generated
19
Cargo.lock
generated
@ -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"
|
||||
|
@ -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"
|
||||
|
@ -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 {
|
||||
|
@ -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]
|
||||
|
@ -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;
|
||||
|
@ -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
|
||||
|
||||
|
@ -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
|
||||
|
@ -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)]
|
||||
|
@ -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
|
||||
|
@ -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 {}
|
||||
|
@ -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 ...)
|
||||
|
@ -1,5 +1,5 @@
|
||||
use log::error;
|
||||
use vspace::addr::VirtAddr;
|
||||
use utils::addr::VirtAddr;
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct FrameWalker {
|
||||
|
@ -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"
|
||||
|
@ -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;
|
||||
|
@ -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 {
|
||||
|
@ -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,
|
||||
|
@ -1,11 +1,8 @@
|
||||
[package]
|
||||
name = "utils"
|
||||
version = "0.1.0"
|
||||
version = "0.1.1"
|
||||
edition = "2021"
|
||||
|
||||
[features]
|
||||
default = []
|
||||
legacy = []
|
||||
|
||||
[dependencies]
|
||||
vspace = { path = "../vspace" }
|
||||
|
@ -1,4 +1,4 @@
|
||||
use vspace::addr::VirtAddr;
|
||||
use crate::addr::VirtAddr;
|
||||
|
||||
extern "C" {
|
||||
pub type ExternSymbol;
|
||||
|
@ -1,6 +1,8 @@
|
||||
#![no_std]
|
||||
#![feature(extern_types)]
|
||||
#![feature(step_trait)]
|
||||
|
||||
pub mod addr;
|
||||
pub mod assert;
|
||||
pub mod atomic;
|
||||
pub mod bin;
|
||||
|
@ -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 }
|
@ -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;
|
@ -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;
|
||||
}
|
@ -1,5 +0,0 @@
|
||||
mod entry;
|
||||
mod table;
|
||||
|
||||
pub use entry::*;
|
||||
pub use table::*;
|
@ -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<Self> {
|
||||
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<Self> {
|
||||
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<T = ()> = Result<T, PageError>;
|
||||
|
||||
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<PhysAddr>;
|
||||
}
|
@ -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"
|
||||
|
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user