diff --git a/os/src/drivers/block/mod.rs b/os/src/drivers/block/mod.rs index 345ba284..fbd5de9d 100644 --- a/os/src/drivers/block/mod.rs +++ b/os/src/drivers/block/mod.rs @@ -10,7 +10,10 @@ use crate::fs::easy_fs::BlockDevice; use lazy_static::*; lazy_static! { - pub static ref BLOCK_DEVICE: Arc = Arc::new(BlockDeviceImpl::new()); + pub static ref BLOCK_DEVICE: Arc = { + kprintln!("[KERN] drivers::block::lazy_static!BLOCK_DEVICE begin"); + Arc::new(BlockDeviceImpl::new()) + }; } #[allow(unused)] diff --git a/os/src/drivers/block/virtio_blk.rs b/os/src/drivers/block/virtio_blk.rs index a30e3bf8..7cd0eb87 100644 --- a/os/src/drivers/block/virtio_blk.rs +++ b/os/src/drivers/block/virtio_blk.rs @@ -20,7 +20,10 @@ pub struct VirtIOBlock { } lazy_static! { - static ref QUEUE_FRAMES: UPIntrFreeCell> = unsafe { UPIntrFreeCell::new(Vec::new()) }; + static ref QUEUE_FRAMES: UPIntrFreeCell> = { + kprintln!("[KERN] drivers::block::virtio_blk::lazy_static!QUEUE_FRAMES begin"); + unsafe { UPIntrFreeCell::new(Vec::new()) } + }; } impl BlockDevice for VirtIOBlock { diff --git a/os/src/drivers/chardev/mod.rs b/os/src/drivers/chardev/mod.rs index 28665769..f12679a3 100644 --- a/os/src/drivers/chardev/mod.rs +++ b/os/src/drivers/chardev/mod.rs @@ -13,5 +13,8 @@ pub trait CharDevice { } lazy_static! { - pub static ref UART: Arc = Arc::new(CharDeviceImpl::new()); + pub static ref UART: Arc = { + kprintln!("[KERN] drivers::chardev::lazy_static!UART begin"); + Arc::new(CharDeviceImpl::new()) + }; } diff --git a/os/src/fs/easy_fs/block_cache.rs b/os/src/fs/easy_fs/block_cache.rs index 19dbac59..ac7ab5f2 100644 --- a/os/src/fs/easy_fs/block_cache.rs +++ b/os/src/fs/easy_fs/block_cache.rs @@ -121,8 +121,10 @@ impl BlockCacheManager { } lazy_static! { - pub static ref BLOCK_CACHE_MANAGER: Mutex = - Mutex::new(BlockCacheManager::new()); + pub static ref BLOCK_CACHE_MANAGER: Mutex = { + kprintln!("[KERN EASYFS] block_cache::lazy_static!BLOCK_CACHE_MANAGER begin"); + Mutex::new(BlockCacheManager::new()) + }; } pub fn get_block_cache( diff --git a/os/src/fs/easy_fs/vfs.rs b/os/src/fs/easy_fs/vfs.rs index 0b713688..1f689408 100644 --- a/os/src/fs/easy_fs/vfs.rs +++ b/os/src/fs/easy_fs/vfs.rs @@ -32,7 +32,7 @@ impl Inode { } fn read_disk_inode(&self, f: impl FnOnce(&DiskInode) -> V) -> V { - //kprintln!("[KERN EASYFS] vfs::Inode::read_disk_inode() begin"); + kprintln!("[KERN EASYFS] vfs::Inode::read_disk_inode() begin"); get_block_cache(self.block_id, Arc::clone(&self.block_device)) .lock() .read(self.block_offset, f) @@ -166,7 +166,7 @@ impl Inode { } pub fn read_at(&self, offset: usize, buf: &mut [u8]) -> usize { - //kprintln!("[KERN EASYFS] vfs::Inode::read_at() begin"); + kprintln!("[KERN EASYFS] vfs::Inode::read_at() begin"); let _fs = self.fs.lock(); self.read_disk_inode(|disk_inode| disk_inode.read_at(offset, buf, &self.block_device)) } diff --git a/os/src/fs/inode.rs b/os/src/fs/inode.rs index eaee2f01..a3601084 100644 --- a/os/src/fs/inode.rs +++ b/os/src/fs/inode.rs @@ -21,6 +21,7 @@ pub struct OSInodeInner { impl OSInode { pub fn new(readable: bool, writable: bool, inode: Arc) -> Self { + kprintln!("[KERN] fs::inode::OSInode::new() begin"); Self { readable, writable, @@ -28,6 +29,7 @@ impl OSInode { } } pub fn read_all(&self) -> Vec { + kprintln!("[KERN] fs::inode::OSInode::read_all() begin"); let mut inner = self.inner.exclusive_access(); let mut buffer = [0u8; 512]; let mut v: Vec = Vec::new(); @@ -45,6 +47,7 @@ impl OSInode { lazy_static! { pub static ref ROOT_INODE: Arc = { + kprintln!("[KERN] fs::inode::lazy_static!ROOT_INODE begin"); let efs = EasyFileSystem::open(BLOCK_DEVICE.clone()); Arc::new(EasyFileSystem::root_inode(&efs)) }; @@ -114,7 +117,7 @@ impl File for OSInode { self.writable } fn read(&self, mut buf: UserBuffer) -> usize { - kprintln!("[KERN] fs::inode::read() begin"); + kprintln!("[KERN] fs::inode::OSInode::read() begin"); let mut inner = self.inner.exclusive_access(); let mut total_read_size = 0usize; for slice in buf.buffers.iter_mut() { @@ -125,11 +128,11 @@ impl File for OSInode { inner.offset += read_size; total_read_size += read_size; } - kprintln!("[KERN] fs::inode::read() end"); + kprintln!("[KERN] fs::inode::OSInode::read() end"); total_read_size } fn write(&self, buf: UserBuffer) -> usize { - kprintln!("[KERN] fs::inode::write() begin"); + kprintln!("[KERN] fs::inode::OSInode::write() begin"); let mut inner = self.inner.exclusive_access(); let mut total_write_size = 0usize; for slice in buf.buffers.iter() { @@ -138,7 +141,7 @@ impl File for OSInode { inner.offset += write_size; total_write_size += write_size; } - kprintln!("[KERN] fs::inode::write() end"); + kprintln!("[KERN] fs::inode::OSInode::write() end"); total_write_size } } diff --git a/os/src/main.rs b/os/src/main.rs index d64efc2a..e0c60441 100644 --- a/os/src/main.rs +++ b/os/src/main.rs @@ -50,7 +50,10 @@ use lazy_static::*; use sync::UPIntrFreeCell; lazy_static! { - pub static ref DEV_NON_BLOCKING_ACCESS: UPIntrFreeCell = unsafe { UPIntrFreeCell::new(false) }; + pub static ref DEV_NON_BLOCKING_ACCESS: UPIntrFreeCell = { + kprintln!("[KERN] main::lazy_static!DEV_NON_BLOCKING_ACCESS begin"); + unsafe { UPIntrFreeCell::new(false) } + }; } #[no_mangle] diff --git a/os/src/mm/frame_allocator.rs b/os/src/mm/frame_allocator.rs index 215fd2f1..86b22d39 100644 --- a/os/src/mm/frame_allocator.rs +++ b/os/src/mm/frame_allocator.rs @@ -83,8 +83,10 @@ impl FrameAllocator for StackFrameAllocator { type FrameAllocatorImpl = StackFrameAllocator; lazy_static! { - pub static ref FRAME_ALLOCATOR: UPIntrFreeCell = - unsafe { UPIntrFreeCell::new(FrameAllocatorImpl::new()) }; + pub static ref FRAME_ALLOCATOR: UPIntrFreeCell ={ + kprintln!("[KERN] mm::frame_allocator::lazy_static!FRAME_ALLOCATOR begin"); + unsafe { UPIntrFreeCell::new(FrameAllocatorImpl::new()) } + }; } pub fn init_frame_allocator() { diff --git a/os/src/mm/memory_set.rs b/os/src/mm/memory_set.rs index 99726b01..11914c18 100644 --- a/os/src/mm/memory_set.rs +++ b/os/src/mm/memory_set.rs @@ -25,8 +25,10 @@ extern "C" { } lazy_static! { - pub static ref KERNEL_SPACE: Arc> = - Arc::new(unsafe { UPIntrFreeCell::new(MemorySet::new_kernel()) }); + pub static ref KERNEL_SPACE: Arc> ={ + kprintln!("[KERN] mm::memory_set::lazy_static!KERNEL_SPACE begin"); + Arc::new(unsafe { UPIntrFreeCell::new(MemorySet::new_kernel()) }) + }; } pub fn kernel_token() -> usize { diff --git a/os/src/sync/up.rs b/os/src/sync/up.rs index 55c8d40a..58260956 100644 --- a/os/src/sync/up.rs +++ b/os/src/sync/up.rs @@ -57,6 +57,7 @@ pub struct IntrMaskingInfo { lazy_static! { static ref INTR_MASKING_INFO: UPSafeCellRaw = unsafe { + kprintln!("[KERN] sync::up::lazy_static!INTR_MASKING_INFO begin"); UPSafeCellRaw::new(IntrMaskingInfo::new()) }; } diff --git a/os/src/task/id.rs b/os/src/task/id.rs index e336c500..cfb50367 100644 --- a/os/src/task/id.rs +++ b/os/src/task/id.rs @@ -40,10 +40,17 @@ impl RecycleAllocator { } lazy_static! { - static ref PID_ALLOCATOR: UPIntrFreeCell = - unsafe { UPIntrFreeCell::new(RecycleAllocator::new()) }; - static ref KSTACK_ALLOCATOR: UPIntrFreeCell = - unsafe { UPIntrFreeCell::new(RecycleAllocator::new()) }; + static ref PID_ALLOCATOR: UPIntrFreeCell = { + kprintln!("[KERN] task::id::lazy_static!PID_ALLOCATOR begin"); + unsafe { UPIntrFreeCell::new(RecycleAllocator::new()) } + }; +} + +lazy_static! { + static ref KSTACK_ALLOCATOR: UPIntrFreeCell ={ + kprintln!("[KERN] task::id::lazy_static!KSTACK_ALLOCATOR begin"); + unsafe { UPIntrFreeCell::new(RecycleAllocator::new()) } + }; } pub struct PidHandle(pub usize); @@ -60,14 +67,17 @@ impl Drop for PidHandle { /// Return (bottom, top) of a kernel stack in kernel space. pub fn kernel_stack_position(kstack_id: usize) -> (usize, usize) { + kprintln!("[KERN] task::id::kernel_stack_position() begin"); let top = TRAMPOLINE - kstack_id * (KERNEL_STACK_SIZE + PAGE_SIZE); let bottom = top - KERNEL_STACK_SIZE; + kprintln!("[KERN] task::id::kernel_stack_position() end"); (bottom, top) } pub struct KernelStack(pub usize); pub fn kstack_alloc() -> KernelStack { + kprintln!("[KERN] task::id::kstack_alloc() begin"); let kstack_id = KSTACK_ALLOCATOR.exclusive_access().alloc(); let (kstack_bottom, kstack_top) = kernel_stack_position(kstack_id); KERNEL_SPACE.exclusive_access().insert_framed_area( @@ -75,6 +85,7 @@ pub fn kstack_alloc() -> KernelStack { kstack_top.into(), MapPermission::R | MapPermission::W, ); + kprintln!("[KERN] task::id::kstack_alloc() end"); KernelStack(kstack_id) } @@ -127,6 +138,7 @@ impl TaskUserRes { ustack_base: usize, alloc_user_res: bool, ) -> Self { + kprintln!("[KERN] task::id::TaskUserRes::new() begin"); let tid = process.inner_exclusive_access().alloc_tid(); let task_user_res = Self { tid, @@ -136,13 +148,16 @@ impl TaskUserRes { if alloc_user_res { task_user_res.alloc_user_res(); } + kprintln!("[KERN] task::id::TaskUserRes::new() end"); task_user_res } pub fn alloc_user_res(&self) { + kprintln!("[KERN] task::id::TaskUserRes::alloc_user_res() begin"); let process = self.process.upgrade().unwrap(); let mut process_inner = process.inner_exclusive_access(); // alloc user stack + kprintln!("[KERN] task::id::TaskUserRes::alloc_user_res(): alloc user stack"); let ustack_bottom = ustack_bottom_from_tid(self.ustack_base, self.tid); let ustack_top = ustack_bottom + USER_STACK_SIZE; process_inner.memory_set.insert_framed_area( @@ -151,6 +166,7 @@ impl TaskUserRes { MapPermission::R | MapPermission::W | MapPermission::U, ); // alloc trap_cx + kprintln!("[KERN] task::id::TaskUserRes::alloc_user_res(): alloc trap_cx"); let trap_cx_bottom = trap_cx_bottom_from_tid(self.tid); let trap_cx_top = trap_cx_bottom + PAGE_SIZE; process_inner.memory_set.insert_framed_area( @@ -158,22 +174,29 @@ impl TaskUserRes { trap_cx_top.into(), MapPermission::R | MapPermission::W, ); + kprintln!("[KERN] task::id::TaskUserRes::alloc_user_res() end"); } fn dealloc_user_res(&self) { + kprintln!("[KERN] task::id::TaskUserRes::dealloc_user_res() begin"); // dealloc tid + kprintln!("[KERN] task::id::TaskUserRes::dealloc_user_res(): dealloc tid"); let process = self.process.upgrade().unwrap(); let mut process_inner = process.inner_exclusive_access(); // dealloc ustack manually + kprintln!("[KERN] task::id::TaskUserRes::dealloc_user_res(): dealloc ustack manually"); let ustack_bottom_va: VirtAddr = ustack_bottom_from_tid(self.ustack_base, self.tid).into(); process_inner .memory_set .remove_area_with_start_vpn(ustack_bottom_va.into()); // dealloc trap_cx manually + kprintln!("[KERN] task::id::TaskUserRes::dealloc_user_res(): dealloc trap_cx manually"); let trap_cx_bottom_va: VirtAddr = trap_cx_bottom_from_tid(self.tid).into(); process_inner .memory_set .remove_area_with_start_vpn(trap_cx_bottom_va.into()); + + kprintln!("[KERN] task::id::TaskUserRes::dealloc_user_res() end"); } #[allow(unused)] diff --git a/os/src/task/manager.rs b/os/src/task/manager.rs index 52f2fb8f..354f1bc0 100644 --- a/os/src/task/manager.rs +++ b/os/src/task/manager.rs @@ -24,10 +24,17 @@ impl TaskManager { } lazy_static! { - pub static ref TASK_MANAGER: UPIntrFreeCell = - unsafe { UPIntrFreeCell::new(TaskManager::new()) }; - pub static ref PID2PCB: UPIntrFreeCell>> = - unsafe { UPIntrFreeCell::new(BTreeMap::new()) }; + pub static ref TASK_MANAGER: UPIntrFreeCell ={ + kprintln!("[KERN] task::manager::lazy_static!TASK_MANAGER begin"); + unsafe { UPIntrFreeCell::new(TaskManager::new()) } + }; +} + +lazy_static! { + pub static ref PID2PCB: UPIntrFreeCell>> ={ + kprintln!("[KERN] task::manager::lazy_static!PID2PCB begin"); + unsafe { UPIntrFreeCell::new(BTreeMap::new()) } + }; } pub fn add_task(task: Arc) { diff --git a/os/src/task/process.rs b/os/src/task/process.rs index 0fe841fd..c881c59a 100644 --- a/os/src/task/process.rs +++ b/os/src/task/process.rs @@ -74,9 +74,12 @@ impl ProcessControlBlock { pub fn new(elf_data: &[u8]) -> Arc { // memory_set with elf program headers/trampoline/trap context/user stack kprintln!("[KERN] task::process::new() begin"); + kprintln!("[KERN] task::process::new(): build MemorySet, set user_stack_base, set entry_point"); let (memory_set, ustack_base, entry_point) = MemorySet::from_elf(elf_data); // allocate a pid + kprintln!("[KERN] task::process::new(): allocate a pid"); let pid_handle = pid_alloc(); + kprintln!("[KERN] task::process::new(): new ProcessControlBlockInner"); let process = Arc::new(Self { pid: pid_handle, inner: unsafe { @@ -104,12 +107,15 @@ impl ProcessControlBlock { }, }); // create a main thread, we should allocate ustack and trap_cx here + kprintln!("[KERN] task::process::new(): create a main thread begin"); + kprintln!("[KERN] task::process::new(): create a main thread: new TCB(alloc kstack, utack & trap_cx...) "); let task = Arc::new(TaskControlBlock::new( Arc::clone(&process), ustack_base, true, )); // prepare trap_cx of main thread + kprintln!("[KERN] task::process::new(): create a main thread: set trap_cx(entry_point, ustack_top, k_satp, k_sp, trap_handler) "); let task_inner = task.inner_exclusive_access(); let trap_cx = task_inner.get_trap_cx(); let ustack_top = task_inner.res.as_ref().unwrap().ustack_top(); @@ -122,12 +128,15 @@ impl ProcessControlBlock { kstack_top, trap_handler as usize, ); + kprintln!("[KERN] task::process::new(): create a main thread end"); // add main thread to the process + kprintln!("[KERN] task::process::new(): add main thread to the process"); let mut process_inner = process.inner_exclusive_access(); process_inner.tasks.push(Some(Arc::clone(&task))); drop(process_inner); insert_into_pid2process(process.getpid(), Arc::clone(&process)); // add main thread to scheduler + kprintln!("[KERN] task::process::new(): add_task(task): add main thread to tscheduler"); add_task(task); kprintln!("[KERN] task::process::new() end"); process diff --git a/os/src/task/processor.rs b/os/src/task/processor.rs index e9a88497..60c6a10e 100644 --- a/os/src/task/processor.rs +++ b/os/src/task/processor.rs @@ -30,7 +30,10 @@ impl Processor { } lazy_static! { - pub static ref PROCESSOR: UPIntrFreeCell = unsafe { UPIntrFreeCell::new(Processor::new()) }; + pub static ref PROCESSOR: UPIntrFreeCell = { + kprintln!("[KERN] task::processor::lazy_static!PROCESSOR begin"); + unsafe { UPIntrFreeCell::new(Processor::new()) } + }; } pub fn run_tasks() { diff --git a/os/src/timer.rs b/os/src/timer.rs index ea70a90f..1702a2ff 100644 --- a/os/src/timer.rs +++ b/os/src/timer.rs @@ -52,8 +52,10 @@ impl Ord for TimerCondVar { } lazy_static! { - static ref TIMERS: UPIntrFreeCell> = - unsafe { UPIntrFreeCell::new(BinaryHeap::::new()) }; + static ref TIMERS: UPIntrFreeCell> ={ + kprintln!("[KERN] timer::lazy_static!TIMERS begin"); + unsafe { UPIntrFreeCell::new(BinaryHeap::::new()) } + }; } pub fn add_timer(expire_ms: usize, task: Arc) {