From b5b3a12bb0ba0405f6a8afedd8265511c273ba02 Mon Sep 17 00:00:00 2001 From: Yifan Wu Date: Sun, 28 Feb 2021 08:51:08 +0800 Subject: [PATCH 1/6] Fix k210 alignment issue when push cmdargs when sys_exec --- os/src/task/task.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/os/src/task/task.rs b/os/src/task/task.rs index 4edd5ce..ee5fc53 100644 --- a/os/src/task/task.rs +++ b/os/src/task/task.rs @@ -142,6 +142,8 @@ impl TaskControlBlock { } *translated_refmut(memory_set.token(), p as *mut u8) = 0; } + // make the user_sp aligned to 8B for k210 platform + user_sp -= user_sp % core::mem::size_of::(); // **** hold current PCB lock let mut inner = self.acquire_inner_lock(); From d1cde16ddb6e7011af864d54ecbc80fd5900cc0c Mon Sep 17 00:00:00 2001 From: Yifan Wu Date: Tue, 2 Mar 2021 21:10:32 +0800 Subject: [PATCH 2/6] Remove unused code. --- easy-fs/src/layout.rs | 27 --------------------------- 1 file changed, 27 deletions(-) diff --git a/easy-fs/src/layout.rs b/easy-fs/src/layout.rs index 7d14de4..2b6b62a 100644 --- a/easy-fs/src/layout.rs +++ b/easy-fs/src/layout.rs @@ -72,7 +72,6 @@ type IndirectBlock = [u32; BLOCK_SZ / 4]; type DataBlock = [u8; BLOCK_SZ]; #[repr(C)] -/// Only support level-1 indirect now, **indirect2** field is always 0. pub struct DiskInode { pub size: u32, pub direct: [u32; INODE_DIRECT_COUNT], @@ -235,32 +234,6 @@ impl DiskInode { } }); } - - /* - pub fn clear_size(&mut self, block_device: &Arc) -> Vec { - let mut v: Vec = Vec::new(); - let blocks = self.blocks() as usize; - self.size = 0; - for i in 0..blocks.min(INODE_DIRECT_COUNT) { - v.push(self.direct[i]); - self.direct[i] = 0; - } - if blocks > INODE_DIRECT_COUNT { - get_block_cache( - self.indirect1 as usize, - Arc::clone(block_device), - ) - .lock() - .modify(0, |indirect_block: &mut IndirectBlock| { - for i in 0..blocks - INODE_DIRECT_COUNT { - v.push(indirect_block[i]); - indirect_block[i] = 0; - } - }); - } - v - } - */ /// Clear size to zero and return blocks that should be deallocated. /// From 42b5b44ff8204c047cd0c25c7504bb79ec94ee3a Mon Sep 17 00:00:00 2001 From: Yifan Wu Date: Thu, 4 Mar 2021 03:27:46 +0800 Subject: [PATCH 3/6] Remove DirentBytes --- easy-fs/src/layout.rs | 27 ++++++++++++++++----------- easy-fs/src/vfs.rs | 14 ++++++-------- 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/easy-fs/src/layout.rs b/easy-fs/src/layout.rs index 2b6b62a..8ad3b84 100644 --- a/easy-fs/src/layout.rs +++ b/easy-fs/src/layout.rs @@ -407,10 +407,13 @@ pub struct DirEntry { pub const DIRENT_SZ: usize = 32; -//pub type DirentBlock = [DirEntry; BLOCK_SZ / DIRENT_SZ]; -pub type DirentBytes = [u8; DIRENT_SZ]; - impl DirEntry { + pub fn empty() -> Self { + Self { + name: [0u8; NAME_LENGTH_LIMIT + 1], + inode_number: 0, + } + } pub fn new(name: &str, inode_number: u32) -> Self { let mut bytes = [0u8; NAME_LENGTH_LIMIT + 1]; &mut bytes[..name.len()].copy_from_slice(name.as_bytes()); @@ -419,18 +422,20 @@ impl DirEntry { inode_number, } } - pub fn into_bytes(&self) -> &DirentBytes { + pub fn as_bytes(&self) -> &[u8] { unsafe { - &*(self as *const Self as usize as *const DirentBytes) + core::slice::from_raw_parts( + self as *const _ as usize as *const u8, + DIRENT_SZ, + ) } } - pub fn from_bytes(bytes: &DirentBytes) -> &Self { - unsafe { &*(bytes.as_ptr() as usize as *const Self) } - } - #[allow(unused)] - pub fn from_bytes_mut(bytes: &mut DirentBytes) -> &mut Self { + pub fn as_bytes_mut(&mut self) -> &mut [u8] { unsafe { - &mut *(bytes.as_mut_ptr() as usize as *mut Self) + core::slice::from_raw_parts_mut( + self as *mut _ as usize as *mut u8, + DIRENT_SZ, + ) } } pub fn name(&self) -> &str { diff --git a/easy-fs/src/vfs.rs b/easy-fs/src/vfs.rs index f10a8c8..3ed069f 100644 --- a/easy-fs/src/vfs.rs +++ b/easy-fs/src/vfs.rs @@ -3,7 +3,6 @@ use super::{ DiskInode, DiskInodeType, DirEntry, - DirentBytes, EasyFileSystem, DIRENT_SZ, get_block_cache, @@ -63,17 +62,16 @@ impl Inode { // assert it is a directory assert!(disk_inode.is_dir()); let file_count = (disk_inode.size as usize) / DIRENT_SZ; - let mut dirent_space: DirentBytes = Default::default(); + let mut dirent = DirEntry::empty(); for i in 0..file_count { assert_eq!( disk_inode.read_at( DIRENT_SZ * i, - &mut dirent_space, + dirent.as_bytes_mut(), &self.block_device, ), DIRENT_SZ, ); - let dirent = DirEntry::from_bytes(&dirent_space); if dirent.name() == name { return Some(dirent.inode_number() as u32); } @@ -144,7 +142,7 @@ impl Inode { let dirent = DirEntry::new(name, new_inode_id); root_inode.write_at( file_count * DIRENT_SZ, - dirent.into_bytes(), + dirent.as_bytes(), &self.block_device, ); }); @@ -164,16 +162,16 @@ impl Inode { let file_count = (disk_inode.size as usize) / DIRENT_SZ; let mut v: Vec = Vec::new(); for i in 0..file_count { - let mut dirent_bytes: DirentBytes = Default::default(); + let mut dirent = DirEntry::empty(); assert_eq!( disk_inode.read_at( i * DIRENT_SZ, - &mut dirent_bytes, + dirent.as_bytes_mut(), &self.block_device, ), DIRENT_SZ, ); - v.push(String::from(DirEntry::from_bytes(&dirent_bytes).name())); + v.push(String::from(dirent.name())); } v }) From 1b1361c6c0544d939b8d8e532744f2cc96dab79b Mon Sep 17 00:00:00 2001 From: Yifan Wu Date: Thu, 4 Mar 2021 08:43:17 +0800 Subject: [PATCH 4/6] Remove unused code. --- easy-fs/src/efs.rs | 16 ---------------- easy-fs/src/vfs.rs | 6 ------ 2 files changed, 22 deletions(-) diff --git a/easy-fs/src/efs.rs b/easy-fs/src/efs.rs index 9e4445d..6b3db0f 100644 --- a/easy-fs/src/efs.rs +++ b/easy-fs/src/efs.rs @@ -119,12 +119,6 @@ impl EasyFileSystem { ) } - /* - fn get_super_block(&self) -> Dirty { - Dirty::new(0, 0, self.block_device.clone()) - } - */ - pub fn get_disk_inode_pos(&self, inode_id: u32) -> (u32, usize) { let inode_size = core::mem::size_of::(); let inodes_per_block = (BLOCK_SZ / inode_size) as u32; @@ -136,16 +130,6 @@ impl EasyFileSystem { self.data_area_start_block + data_block_id } - /* - fn get_block(&self, block_id: u32) -> Dirty { - Dirty::new( - block_id as usize, - 0, - self.block_device.clone(), - ) - } - */ - pub fn alloc_inode(&mut self) -> u32 { self.inode_bitmap.alloc(&self.block_device).unwrap() as u32 } diff --git a/easy-fs/src/vfs.rs b/easy-fs/src/vfs.rs index 3ed069f..58f2be7 100644 --- a/easy-fs/src/vfs.rs +++ b/easy-fs/src/vfs.rs @@ -48,12 +48,6 @@ impl Inode { ).lock().modify(self.block_offset, f) } - /* - fn get_disk_inode(&self, fs: &mut MutexGuard) -> Dirty { - fs.get_disk_inode(self.inode_id) - } - */ - fn find_inode_id( &self, name: &str, From 88d0d2164f7b1f310ff4b50deb95b4343bce16d0 Mon Sep 17 00:00:00 2001 From: Yifan Wu Date: Fri, 5 Mar 2021 02:07:04 +0800 Subject: [PATCH 5/6] Fix qemu mmio range --- os/src/config.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/os/src/config.rs b/os/src/config.rs index 0633c4d..dce95ec 100644 --- a/os/src/config.rs +++ b/os/src/config.rs @@ -18,7 +18,7 @@ pub const CLOCK_FREQ: usize = 12500000; #[cfg(feature = "board_qemu")] pub const MMIO: &[(usize, usize)] = &[ - (0x10000000, 0x10000), + (0x10001000, 0x1000), ]; #[cfg(feature = "board_k210")] From 5c23e38321d86e6477665faf2a58c0bc93793f78 Mon Sep 17 00:00:00 2001 From: Yifan Wu Date: Sat, 6 Mar 2021 03:33:28 +0800 Subject: [PATCH 6/6] Do not fetch tools when running on qemu. --- os/Makefile | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/os/Makefile b/os/Makefile index 4d86c12..eb73dbd 100644 --- a/os/Makefile +++ b/os/Makefile @@ -71,10 +71,9 @@ disasm-vim: kernel @vim $(DISASM_TMP) @rm $(DISASM_TMP) -run: tools run-inner +run: run-inner -tools: - (which $(K210-BURNER)) || (cd .. && git clone https://github.com/sipeed/kflash.py.git && mv kflash.py tools) + run-inner: build ifeq ($(BOARD),qemu) @@ -86,6 +85,7 @@ ifeq ($(BOARD),qemu) -drive file=$(FS_IMG),if=none,format=raw,id=x0 \ -device virtio-blk-device,drive=x0,bus=virtio-mmio-bus.0 else + (which $(K210-BURNER)) || (cd .. && git clone https://github.com/sipeed/kflash.py.git && mv kflash.py tools) @cp $(BOOTLOADER) $(BOOTLOADER).copy @dd if=$(KERNEL_BIN) of=$(BOOTLOADER).copy bs=$(K210_BOOTLOADER_SIZE) seek=1 @mv $(BOOTLOADER).copy $(KERNEL_BIN) @@ -100,4 +100,4 @@ debug: build tmux split-window -h "riscv64-unknown-elf-gdb -ex 'file $(KERNEL_ELF)' -ex 'set arch riscv:rv64' -ex 'target remote localhost:1234'" && \ tmux -2 attach-session -d -.PHONY: build env kernel clean disasm disasm-vim run-inner tools +.PHONY: build env kernel clean disasm disasm-vim run-inner