From 928a6f0d8c28dea5acbb23d1bbe77ab73087b552 Mon Sep 17 00:00:00 2001 From: Yifan Wu Date: Wed, 8 Sep 2021 00:26:22 +0800 Subject: [PATCH] Remove spin::Mutex in frame_allocator and memory_set. --- os/Cargo.toml | 1 - os/src/mm/frame_allocator.rs | 13 +++++++------ os/src/mm/memory_set.rs | 10 +++++----- os/src/mm/mod.rs | 2 +- os/src/task/task.rs | 4 ++-- 5 files changed, 15 insertions(+), 15 deletions(-) diff --git a/os/Cargo.toml b/os/Cargo.toml index 588df758..f8cbacdc 100644 --- a/os/Cargo.toml +++ b/os/Cargo.toml @@ -10,7 +10,6 @@ edition = "2018" riscv = { git = "https://github.com/rcore-os/riscv", features = ["inline-asm"] } lazy_static = { version = "1.4.0", features = ["spin_no_std"] } buddy_system_allocator = "0.6" -spin = "0.7.0" bitflags = "1.2.1" xmas-elf = "0.7.0" diff --git a/os/src/mm/frame_allocator.rs b/os/src/mm/frame_allocator.rs index c17385f1..34152355 100644 --- a/os/src/mm/frame_allocator.rs +++ b/os/src/mm/frame_allocator.rs @@ -1,6 +1,6 @@ use super::{PhysAddr, PhysPageNum}; use alloc::vec::Vec; -use spin::Mutex; +use crate::sync::UPSafeCell; use crate::config::MEMORY_END; use lazy_static::*; use core::fmt::{self, Debug, Formatter}; @@ -87,8 +87,9 @@ impl FrameAllocator for StackFrameAllocator { type FrameAllocatorImpl = StackFrameAllocator; lazy_static! { - pub static ref FRAME_ALLOCATOR: Mutex = - Mutex::new(FrameAllocatorImpl::new()); + pub static ref FRAME_ALLOCATOR: UPSafeCell = unsafe { + UPSafeCell::new(FrameAllocatorImpl::new()) + }; } pub fn init_frame_allocator() { @@ -96,20 +97,20 @@ pub fn init_frame_allocator() { fn ekernel(); } FRAME_ALLOCATOR - .lock() + .exclusive_access() .init(PhysAddr::from(ekernel as usize).ceil(), PhysAddr::from(MEMORY_END).floor()); } pub fn frame_alloc() -> Option { FRAME_ALLOCATOR - .lock() + .exclusive_access() .alloc() .map(|ppn| FrameTracker::new(ppn)) } fn frame_dealloc(ppn: PhysPageNum) { FRAME_ALLOCATOR - .lock() + .exclusive_access() .dealloc(ppn); } diff --git a/os/src/mm/memory_set.rs b/os/src/mm/memory_set.rs index f2311509..ad3986a2 100644 --- a/os/src/mm/memory_set.rs +++ b/os/src/mm/memory_set.rs @@ -7,7 +7,7 @@ use alloc::vec::Vec; use riscv::register::satp; use alloc::sync::Arc; use lazy_static::*; -use spin::Mutex; +use crate::sync::UPSafeCell; use crate::config::{ MEMORY_END, PAGE_SIZE, @@ -30,9 +30,9 @@ extern "C" { } lazy_static! { - pub static ref KERNEL_SPACE: Arc> = Arc::new(Mutex::new( - MemorySet::new_kernel() - )); + pub static ref KERNEL_SPACE: Arc> = Arc::new(unsafe { + UPSafeCell::new(MemorySet::new_kernel() + )}); } pub struct MemorySet { @@ -290,7 +290,7 @@ bitflags! { #[allow(unused)] pub fn remap_test() { - let mut kernel_space = KERNEL_SPACE.lock(); + let mut kernel_space = KERNEL_SPACE.exclusive_access(); let mid_text: VirtAddr = ((stext as usize + etext as usize) / 2).into(); let mid_rodata: VirtAddr = ((srodata as usize + erodata as usize) / 2).into(); let mid_data: VirtAddr = ((sdata as usize + edata as usize) / 2).into(); diff --git a/os/src/mm/mod.rs b/os/src/mm/mod.rs index 04af2e12..bb04aea9 100644 --- a/os/src/mm/mod.rs +++ b/os/src/mm/mod.rs @@ -15,5 +15,5 @@ pub use memory_set::remap_test; pub fn init() { heap_allocator::init_heap(); frame_allocator::init_frame_allocator(); - KERNEL_SPACE.lock().activate(); + KERNEL_SPACE.exclusive_access().activate(); } diff --git a/os/src/task/task.rs b/os/src/task/task.rs index b08d82a9..47368db6 100644 --- a/os/src/task/task.rs +++ b/os/src/task/task.rs @@ -29,7 +29,7 @@ impl TaskControlBlock { // map a kernel-stack in kernel space let (kernel_stack_bottom, kernel_stack_top) = kernel_stack_position(app_id); KERNEL_SPACE - .lock() + .exclusive_access() .insert_framed_area( kernel_stack_bottom.into(), kernel_stack_top.into(), @@ -47,7 +47,7 @@ impl TaskControlBlock { *trap_cx = TrapContext::app_init_context( entry_point, user_sp, - KERNEL_SPACE.lock().token(), + KERNEL_SPACE.exclusive_access().token(), kernel_stack_top, trap_handler as usize, );