Skip to content
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
cc6e6c0
save
ZZJJWarth Jan 24, 2024
a533535
save
ZZJJWarth Jan 24, 2024
a65400a
save
ZZJJWarth Jan 25, 2024
dfa85f4
strange error
ZZJJWarth Jan 26, 2024
18d419e
一个优化的尝试
ZZJJWarth Feb 1, 2024
5eed550
dev stable 0.1.0
ZZJJWarth Feb 16, 2024
9cfd87d
save
ZZJJWarth Feb 17, 2024
6287525
完成了简单的blockcache支持,完成了代码注释
ZZJJWarth Feb 17, 2024
053edc3
complete merge
ZZJJWarth Feb 17, 2024
95361e5
save
ZZJJWarth Feb 17, 2024
6e6a6d7
save
ZZJJWarth Feb 17, 2024
b2a733c
save
ZZJJWarth Feb 17, 2024
c1eb2c7
1
fslongjin Feb 17, 2024
0934fb9
进行了修改
ZZJJWarth Feb 20, 2024
03d9c43
Merge github.com:ZZJJWarth/DragonOS into stable_0.1.1
ZZJJWarth Feb 20, 2024
af6a333
修改后的blockcache
ZZJJWarth Feb 20, 2024
7c80caf
Merge branch 'DragonOS-Community:master' into master
ZZJJWarth Feb 26, 2024
7a4a81c
Merge branch 'DragonOS-Community:master' into stable_0.1.1
ZZJJWarth Feb 26, 2024
8335673
Merge branch 'stable_0.1.1' of github.com:ZZJJWarth/DragonOS into sta…
ZZJJWarth Feb 20, 2024
022e65c
第二次修改
ZZJJWarth Feb 29, 2024
d173c95
Merge github.com:DragonOS-Community/DragonOS into stable_0.1.1
ZZJJWarth Feb 29, 2024
b26b85f
Merge branch 'DragonOS-Community:master' into master
ZZJJWarth Feb 29, 2024
4f909a8
Merge branch 'master' of github.com:ZZJJWarth/DragonOS into stable_0.1.1
ZZJJWarth Feb 29, 2024
5df3212
fmt code
ZZJJWarth Mar 1, 2024
9d64e0c
Merge branch 'DragonOS-Community:master' into master
ZZJJWarth Mar 1, 2024
5041858
Merge branch 'master' of github.com:ZZJJWarth/DragonOS into stable_0.1.1
ZZJJWarth Mar 1, 2024
5eadc37
merge
ZZJJWarth Mar 19, 2024
2fc092b
进行了必要的修改
ZZJJWarth Mar 19, 2024
f61989b
Merge branch 'DragonOS-Community:master' into stable_0.1.1
ZZJJWarth Mar 19, 2024
80f32c2
注释修改
ZZJJWarth Mar 19, 2024
49857d0
Merge branch 'stable_0.1.1' of github.com:ZZJJWarth/DragonOS into sta…
ZZJJWarth Mar 19, 2024
3fc6e16
删除无用注释
ZZJJWarth Mar 19, 2024
fff4031
Merge branch 'master' of https://github.com/DragonOS-Community/Dragon…
ZZJJWarth Mar 25, 2024
3c67798
fmt
ZZJJWarth Mar 25, 2024
d53e8dd
Merge branch 'master' of https://github.com/DragonOS-Community/Dragon…
ZZJJWarth Mar 25, 2024
628b6ec
修复了clippy error
ZZJJWarth Mar 25, 2024
342c56b
save
ZZJJWarth Mar 29, 2024
a908a07
save
ZZJJWarth Mar 29, 2024
4e1661d
Merge branch 'DragonOS-Community:master' into stable_0.1.1
ZZJJWarth Mar 31, 2024
127cee5
Merge branch 'stable_0.1.1' of github.com:ZZJJWarth/DragonOS into dev…
ZZJJWarth Mar 31, 2024
09a203c
完成了block_cache的上锁操作
ZZJJWarth Mar 31, 2024
8b1a8bc
Merge branch 'DragonOS-Community:master' into stable_0.1.1
ZZJJWarth Mar 31, 2024
dc4af16
Merge branch 'DragonOS-Community:master' into stable_0.1.1
ZZJJWarth Apr 2, 2024
f4b8f63
Merge branch 'stable_0.1.1' of github.com:ZZJJWarth/DragonOS into dev…
ZZJJWarth Apr 2, 2024
8df080b
Merge branch 'master' of https://github.com/DragonOS-Community/Dragon…
ZZJJWarth Apr 4, 2024
1a0c493
将部分usize修改为BlockId
ZZJJWarth Apr 4, 2024
f9cefd6
Merge branch 'master' of https://github.com/DragonOS-Community/Dragon…
ZZJJWarth Apr 6, 2024
16c6a5e
初步修改
ZZJJWarth Apr 6, 2024
7dd2ebc
完成了blockcache代码文件位置的迁移
ZZJJWarth Apr 6, 2024
f84a7bd
Merge branch 'DragonOS-Community:master' into stable_0.1.1
ZZJJWarth Apr 6, 2024
772f5ea
Merge branch 'master' of https://github.com/DragonOS-Community/Dragon…
ZZJJWarth Apr 7, 2024
375fc6a
Merge branch 'stable_0.1.1' of github.com:ZZJJWarth/DragonOS into dev…
ZZJJWarth Apr 7, 2024
739347c
删除了无关注释,规范了注释格式
ZZJJWarth Apr 7, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ cppcheck.xml
Cargo.lock
.cache
compile_commands.json
/logs/
/logs/
14 changes: 7 additions & 7 deletions kernel/src/arch/x86_64/mm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,13 @@ static XD_RESERVED: AtomicBool = AtomicBool::new(false);

impl MemoryManagementArch for X86_64MMArch {
/// 4K页
const PAGE_SHIFT: usize = 12;
const PAGE_SHIFT: usize = 12; //页的大小是4k

/// 每个页表项占8字节,总共有512个页表项
const PAGE_ENTRY_SHIFT: usize = 9;
/// 每个页表项占8字节,总共有512个页表项
const PAGE_ENTRY_SHIFT: usize = 9; //这里表示512的2对数

/// 四级页表(PML4T、PDPT、PDT、PT)
const PAGE_LEVELS: usize = 4;
const PAGE_LEVELS: usize = 4; //最大页表等级是4,但是实际上最高层的页表是3级,这四级页表分别是3,2,1,0

/// 页表项的有效位的index。在x86_64中,页表项的第[0, 47]位表示地址和flag,
/// 第[48, 51]位表示保留。因此,有效位的index为52。
Expand All @@ -89,13 +89,13 @@ impl MemoryManagementArch for X86_64MMArch {

const ENTRY_FLAG_DEFAULT_TABLE: usize = Self::ENTRY_FLAG_PRESENT;

const ENTRY_FLAG_PRESENT: usize = 1 << 0;
const ENTRY_FLAG_PRESENT: usize = 1 << 0; //00000000_00000000_00000000_00000001

const ENTRY_FLAG_READONLY: usize = 0;

const ENTRY_FLAG_READWRITE: usize = 1 << 1;
const ENTRY_FLAG_READWRITE: usize = 1 << 1; //00000000_00000000_00000000_00000010

const ENTRY_FLAG_USER: usize = 1 << 2;
const ENTRY_FLAG_USER: usize = 1 << 2; //00000000_00000000_00000000_00000100

const ENTRY_FLAG_WRITE_THROUGH: usize = 1 << 3;

Expand Down
66 changes: 60 additions & 6 deletions kernel/src/driver/base/block/block_device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use alloc::{sync::Arc, vec::Vec};
use core::any::Any;
use system_error::SystemError;

use super::disk_info::Partition;
use super::{cache::cached_block_device::BlockCache, disk_info::Partition};

/// 该文件定义了 Device 和 BlockDevice 的接口
/// Notice 设备错误码使用 Posix 规定的 int32_t 的错误码表示,而不是自己定义错误enum
Expand Down Expand Up @@ -239,6 +239,55 @@ pub trait BlockDevice: Device {
/// @brief 返回当前磁盘上的所有分区的Arc指针数组
fn partitions(&self) -> Vec<Arc<Partition>>;

/// @brief 是对read_at的覆盖。所有read_at函数均更改为了本函数
fn t_read(
&self,
lba_id_start: BlockId,
count: usize,
buf: &mut [u8],
) -> Result<usize, SystemError> {
self.cache_read(lba_id_start, count, buf)
}

/// @brief 是对write_at的覆盖。所有write_at函数均更改为了本函数
fn t_write(
&self,
lba_id_start: BlockId,
count: usize,
buf: &[u8],
) -> Result<usize, SystemError> {
self.cache_write(lba_id_start, count, buf)
}

/// @brief 其功能对外而言和read_at函数完全一致,但是加入blockcache的功能
fn cache_read(
&self,
lba_id_start: BlockId,
count: usize,
buf: &mut [u8],
) -> Result<usize, SystemError> {
let cache_response = BlockCache::read(lba_id_start, count, buf);
match cache_response {
Ok(_) => return Ok(count * 512),
Err(x) => {
let ans = self.read_at(lba_id_start, count, buf)?;
let _ = BlockCache::insert(x, buf);
return Ok(ans);
}
}
}

/// @brief 其功能对外而言和write_at函数完全一致,但是加入blockcache的功能
fn cache_write(
&self,
lba_id_start: BlockId,
count: usize,
buf: &[u8],
) -> Result<usize, SystemError> {
let _cache_response = BlockCache::test_write(lba_id_start, count, buf);
self.write_at(lba_id_start, count, buf)
}

fn write_at_bytes(&self, offset: usize, len: usize, buf: &[u8]) -> Result<usize, SystemError> {
// assert!(len <= buf.len());
if len > buf.len() {
Expand All @@ -256,7 +305,8 @@ pub trait BlockDevice: Device {
let full = multi && range.is_multi() || !multi && range.is_full();

if full {
self.write_at(range.lba_start, count, buf_slice)?;
self.t_write(range.lba_start, count, buf_slice)?;
// self.write_at(range.lba_start, count, buf_slice)?;
} else {
if self.blk_size_log2() > BLK_SIZE_LOG2_LIMIT {
return Err(SystemError::E2BIG);
Expand All @@ -265,10 +315,12 @@ pub trait BlockDevice: Device {
let mut temp = Vec::new();
temp.resize(1usize << self.blk_size_log2(), 0);
// 由于块设备每次读写都是整块的,在不完整写入之前,必须把不完整的地方补全
self.read_at(range.lba_start, 1, &mut temp[..])?;
self.t_read(range.lba_start, 1, &mut temp[..])?;
// self.read_at(range.lba_start, 1, &mut temp[..])?;
// 把数据从临时buffer复制到目标buffer
temp[range.begin..range.end].copy_from_slice(&buf_slice);
self.write_at(range.lba_start, 1, &temp[..])?;
self.t_write(range.lba_start, 1, &temp[..])?;
// self.write_at(range.lba_start, 1, &temp[..])?;
}
}
return Ok(len);
Expand Down Expand Up @@ -299,7 +351,8 @@ pub trait BlockDevice: Device {
// 读取整个block作为有效数据
if full {
// 调用 BlockDevice::read_at() 直接把引用传进去,不是把整个数组move进去
self.read_at(range.lba_start, count, buf_slice)?;
self.t_read(range.lba_start, count, buf_slice)?;
// self.read_at(range.lba_start, count, buf_slice)?;
} else {
// 判断块的长度不能超过最大值
if self.blk_size_log2() > BLK_SIZE_LOG2_LIMIT {
Expand All @@ -308,7 +361,8 @@ pub trait BlockDevice: Device {

let mut temp = Vec::new();
temp.resize(1usize << self.blk_size_log2(), 0);
self.read_at(range.lba_start, 1, &mut temp[..])?;
self.t_read(range.lba_start, 1, &mut temp[..])?;
// self.read_at(range.lba_start, 1, &mut temp[..])?;

// 把数据从临时buffer复制到目标buffer
buf_slice.copy_from_slice(&temp[range.begin..range.end]);
Expand Down
70 changes: 70 additions & 0 deletions kernel/src/driver/base/block/cache/cache_block.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
use core::cmp::Ordering;

use alloc::{boxed::Box, vec::Vec};

use super::cache_config::BLOCK_SIZE;
/// @brief 该枚举设计来是用于实现回写法的,但是目前并未使用
#[allow(dead_code)]
pub enum CacheBlockFlag {
Unused,
Unwrited,
Writed,
}
#[derive(Copy, Clone)]
pub struct CacheBlockAddr(usize);

impl CacheBlockAddr {
pub fn new(num: usize) -> Self {
Self(num)
}

pub fn data(&self) -> usize {
self.0
}
}
impl PartialEq<usize> for CacheBlockAddr {
fn eq(&self, other: &usize) -> bool {
self.0 == *other
}
}

impl PartialOrd<usize> for CacheBlockAddr {
fn partial_cmp(&self, other: &usize) -> Option<Ordering> {
Some(self.0.cmp(other))
}
}

/// @brief存储数据的最小单位
pub struct CacheBlock {
data: Box<[u8]>,
_flag: CacheBlockFlag,
lba_id: usize,
}

impl CacheBlock {
pub fn new(data: Box<[u8]>, flag: CacheBlockFlag, lba_id: usize) -> Self {
CacheBlock {
data,
_flag: flag,
lba_id,
}
}

pub fn from_data(lba_id: usize, data: Vec<u8>) -> Self {
let space_box = data.into_boxed_slice();
CacheBlock::new(space_box, CacheBlockFlag::Unwrited, lba_id)
}

pub fn _set_flag(&mut self, _flag: CacheBlockFlag) -> Option<()> {
todo!()
}
#[inline]
pub fn get_data(&self, buf: &mut [u8]) -> usize {
buf.copy_from_slice(&self.data);
return BLOCK_SIZE;
}

pub fn get_lba_id(&self) -> usize {
self.lba_id
}
}
6 changes: 6 additions & 0 deletions kernel/src/driver/base/block/cache/cache_config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pub const BLOCK_SIZE_LOG:usize=9;
///块大小这里固定为512
pub const BLOCK_SIZE: usize = 1<<BLOCK_SIZE_LOG;
///这里规定Cache的threshold大小,单位为:MB
pub const CACHE_THRESHOLD:usize=64;

103 changes: 103 additions & 0 deletions kernel/src/driver/base/block/cache/cache_iter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/// @brief 一个简单的结构体,是BlockIter的输出
///
/// #数据成员:
/// - 'iba_id' :表示单个块对应的iba_id
/// - '_data_start_addr' :表示该块在buf中的起始地址,目前并没有作用(例如:若该块是第2个块,那么该数据成员值为2*BLOCK_SIZE)
/// - '_block_size' :表示该块的大小
#[derive(Debug)]
pub struct BlockData {
iba_id: usize,
_data_start_addr: usize,
_block_size: usize,
}

impl BlockData {
pub fn new(iba_id: usize, data_start_addr: usize, block_size: usize) -> Self {
Self {
iba_id,
_data_start_addr: data_start_addr,
_block_size: block_size,
}
}
#[inline]
pub fn iba_id(&self) -> usize {
self.iba_id
}
#[inline]
pub fn _data_start_addr(&self) -> usize {
self._data_start_addr
}
#[inline]
pub fn _block_size(&self) -> usize {
self._block_size
}
}

/// @brief块迭代器,它获取需求(起始块,连续块的个数),并将连续的块输出为单一的块(如你需要读取lba_id为10~20的连续块,它就可以输出10,11...,20的BlockData)
///
/// #数据成员:
/// - 'iba_id_start' :表示起始块的iba_id
/// - 'count' :表示从起始块开始你需要读多少个块
/// - 'current' :表示当前遍历到第几个块了
/// - 'block_size' :规定块的大小

#[derive(Copy, Clone)]
pub struct BlockIter {
iba_id_start: usize,
count: usize,
current: usize,
block_size: usize,
}

impl BlockIter {
pub fn new(lba_id_start: usize, count: usize, block_size: usize) -> Self {
Self {
iba_id_start: lba_id_start,
count,
block_size,
current: 0,
}
}
}

impl Iterator for BlockIter {
type Item = BlockData;

fn next(&mut self) -> Option<Self::Item> {
if self.current < self.count {
let ans = BlockData::new(
self.iba_id_start + self.current,
self.current * self.block_size,
self.block_size,
);
self.current += 1;
Some(ans)
} else {
None
}
}
}

/// @brief 表示缺块信息的数据结构,往往在读取的时候发现缺块并产生FailData,在插入的时候使用FailData
///
/// #数据成员:
/// - 'lba_id' :表示缺块的lba_id
/// - 'index' :表示缺块在buf中的位置,用于在insert的时候定位缺块数据的位置
pub struct FailData {
lba_id: usize,
index: usize,
}

impl FailData {
pub fn new(lba_id: usize, index: usize) -> Self {
FailData { lba_id, index }
}
#[inline]
pub fn lba_id(&self) -> usize {
self.lba_id
}
///@brief 该函数返回的是缺块在buf中的位置,比如:index=1,那么我们就应该取buf\[512..1024\]
pub fn index(&self) -> usize {
self.index
}
}
Loading