Remove spin::Mutex in frame_allocator and memory_set.

This commit is contained in:
Yifan Wu 2021-09-08 00:26:22 +08:00
parent 32b85b1d0c
commit 37346c8d0f
5 changed files with 15 additions and 15 deletions

View file

@ -10,7 +10,6 @@ edition = "2018"
riscv = { git = "https://github.com/rcore-os/riscv", features = ["inline-asm"] } riscv = { git = "https://github.com/rcore-os/riscv", features = ["inline-asm"] }
lazy_static = { version = "1.4.0", features = ["spin_no_std"] } lazy_static = { version = "1.4.0", features = ["spin_no_std"] }
buddy_system_allocator = "0.6" buddy_system_allocator = "0.6"
spin = "0.7.0"
bitflags = "1.2.1" bitflags = "1.2.1"
xmas-elf = "0.7.0" xmas-elf = "0.7.0"

View file

@ -1,6 +1,6 @@
use super::{PhysAddr, PhysPageNum}; use super::{PhysAddr, PhysPageNum};
use alloc::vec::Vec; use alloc::vec::Vec;
use spin::Mutex; use crate::sync::UPSafeCell;
use crate::config::MEMORY_END; use crate::config::MEMORY_END;
use lazy_static::*; use lazy_static::*;
use core::fmt::{self, Debug, Formatter}; use core::fmt::{self, Debug, Formatter};
@ -87,8 +87,9 @@ impl FrameAllocator for StackFrameAllocator {
type FrameAllocatorImpl = StackFrameAllocator; type FrameAllocatorImpl = StackFrameAllocator;
lazy_static! { lazy_static! {
pub static ref FRAME_ALLOCATOR: Mutex<FrameAllocatorImpl> = pub static ref FRAME_ALLOCATOR: UPSafeCell<FrameAllocatorImpl> = unsafe {
Mutex::new(FrameAllocatorImpl::new()); UPSafeCell::new(FrameAllocatorImpl::new())
};
} }
pub fn init_frame_allocator() { pub fn init_frame_allocator() {
@ -96,20 +97,20 @@ pub fn init_frame_allocator() {
fn ekernel(); fn ekernel();
} }
FRAME_ALLOCATOR FRAME_ALLOCATOR
.lock() .exclusive_access()
.init(PhysAddr::from(ekernel as usize).ceil(), PhysAddr::from(MEMORY_END).floor()); .init(PhysAddr::from(ekernel as usize).ceil(), PhysAddr::from(MEMORY_END).floor());
} }
pub fn frame_alloc() -> Option<FrameTracker> { pub fn frame_alloc() -> Option<FrameTracker> {
FRAME_ALLOCATOR FRAME_ALLOCATOR
.lock() .exclusive_access()
.alloc() .alloc()
.map(|ppn| FrameTracker::new(ppn)) .map(|ppn| FrameTracker::new(ppn))
} }
fn frame_dealloc(ppn: PhysPageNum) { fn frame_dealloc(ppn: PhysPageNum) {
FRAME_ALLOCATOR FRAME_ALLOCATOR
.lock() .exclusive_access()
.dealloc(ppn); .dealloc(ppn);
} }

View file

@ -7,7 +7,7 @@ use alloc::vec::Vec;
use riscv::register::satp; use riscv::register::satp;
use alloc::sync::Arc; use alloc::sync::Arc;
use lazy_static::*; use lazy_static::*;
use spin::Mutex; use crate::sync::UPSafeCell;
use crate::config::{ use crate::config::{
MEMORY_END, MEMORY_END,
PAGE_SIZE, PAGE_SIZE,
@ -30,9 +30,9 @@ extern "C" {
} }
lazy_static! { lazy_static! {
pub static ref KERNEL_SPACE: Arc<Mutex<MemorySet>> = Arc::new(Mutex::new( pub static ref KERNEL_SPACE: Arc<UPSafeCell<MemorySet>> = Arc::new(unsafe {
MemorySet::new_kernel() UPSafeCell::new(MemorySet::new_kernel()
)); )});
} }
pub struct MemorySet { pub struct MemorySet {
@ -290,7 +290,7 @@ bitflags! {
#[allow(unused)] #[allow(unused)]
pub fn remap_test() { 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_text: VirtAddr = ((stext as usize + etext as usize) / 2).into();
let mid_rodata: VirtAddr = ((srodata as usize + erodata 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(); let mid_data: VirtAddr = ((sdata as usize + edata as usize) / 2).into();

View file

@ -15,5 +15,5 @@ pub use memory_set::remap_test;
pub fn init() { pub fn init() {
heap_allocator::init_heap(); heap_allocator::init_heap();
frame_allocator::init_frame_allocator(); frame_allocator::init_frame_allocator();
KERNEL_SPACE.lock().activate(); KERNEL_SPACE.exclusive_access().activate();
} }

View file

@ -29,7 +29,7 @@ impl TaskControlBlock {
// map a kernel-stack in kernel space // map a kernel-stack in kernel space
let (kernel_stack_bottom, kernel_stack_top) = kernel_stack_position(app_id); let (kernel_stack_bottom, kernel_stack_top) = kernel_stack_position(app_id);
KERNEL_SPACE KERNEL_SPACE
.lock() .exclusive_access()
.insert_framed_area( .insert_framed_area(
kernel_stack_bottom.into(), kernel_stack_bottom.into(),
kernel_stack_top.into(), kernel_stack_top.into(),
@ -47,7 +47,7 @@ impl TaskControlBlock {
*trap_cx = TrapContext::app_init_context( *trap_cx = TrapContext::app_init_context(
entry_point, entry_point,
user_sp, user_sp,
KERNEL_SPACE.lock().token(), KERNEL_SPACE.exclusive_access().token(),
kernel_stack_top, kernel_stack_top,
trap_handler as usize, trap_handler as usize,
); );