Skip to content

Commit

Permalink
Use x86_64 and AtomicRefCell creates
Browse files Browse the repository at this point in the history
- The `spin` crate is unmaintained
  - Replace it with `atomic_refcell`
  - We don't need spin locks, just a checked refcount
- Use `x86_64` instead of `cpuio`
  - It has better abstractions
  - It has more functionality that we can user later
- Fixup existing port usage

Signed-off-by: Joe Richey <[email protected]>
  • Loading branch information
josephlr committed Feb 28, 2020
1 parent 71f2756 commit 743ce2d
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 94 deletions.
32 changes: 24 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ log-serial = []
log-panic = ["log-serial"]

[dependencies]
cpuio = "*"
spin = "0.5"
x86_64 = "0.9"
atomic_refcell = "0.1"
r-efi = "2.1.0"

2 changes: 1 addition & 1 deletion src/efi/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ impl<'a> BlockWrapper<'a> {
let last_block = unsafe { (*block).get_capacity() } - 1;

let size = core::mem::size_of::<BlockWrapper>();
let (_status, new_address) = super::ALLOCATOR.lock().allocate_pages(
let (_status, new_address) = super::ALLOCATOR.borrow_mut().allocate_pages(
AllocateType::AllocateAnyPages,
MemoryType::LoaderData,
((size + super::PAGE_SIZE as usize - 1) / super::PAGE_SIZE as usize) as u64,
Expand Down
4 changes: 2 additions & 2 deletions src/efi/console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub extern "win64" fn stdout_output_string(
message: *mut Char16,
) -> Status {
use core::fmt::Write;
let mut logger = crate::logger::LOGGER.lock();
let mut serial = crate::serial::SERIAL.borrow_mut();
let mut string_end = false;

loop {
Expand All @@ -67,7 +67,7 @@ pub extern "win64" fn stdout_output_string(
i += 1;
}
let s = unsafe { core::str::from_utf8_unchecked(&output) };
logger.write_str(s).unwrap();
serial.write_str(s).unwrap();
if string_end {
break;
}
Expand Down
4 changes: 2 additions & 2 deletions src/efi/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ pub extern "win64" fn open(
pub extern "win64" fn close(proto: *mut FileProtocol) -> Status {
let wrapper = container_of!(proto, FileWrapper, proto);
super::ALLOCATOR
.lock()
.borrow_mut()
.free_pages(&wrapper as *const _ as u64)
}

Expand Down Expand Up @@ -208,7 +208,7 @@ pub struct FileSystemWrapper<'a> {
impl<'a> FileSystemWrapper<'a> {
fn create_file(&self, root: bool) -> Option<*mut FileWrapper> {
let size = core::mem::size_of::<FileWrapper>();
let (status, new_address) = super::ALLOCATOR.lock().allocate_pages(
let (status, new_address) = super::ALLOCATOR.borrow_mut().allocate_pages(
AllocateType::AllocateAnyPages,
MemoryType::LoaderData,
((size + super::PAGE_SIZE as usize - 1) / super::PAGE_SIZE as usize) as u64,
Expand Down
26 changes: 13 additions & 13 deletions src/efi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

use core::ffi::c_void;

use atomic_refcell::AtomicRefCell;
use r_efi::{
efi::{
self, AllocateType, Boolean, CapsuleHeader, Char16, Event, EventNotify, Guid, Handle,
Expand All @@ -25,7 +26,6 @@ use r_efi::{
device_path::Protocol as DevicePathProtocol, loaded_image::Protocol as LoadedImageProtocol,
},
};
use spin::Mutex;

mod alloc;
mod block;
Expand All @@ -48,7 +48,7 @@ struct HandleWrapper {
handle_type: HandleType,
}

pub static ALLOCATOR: Mutex<Allocator> = Mutex::new(Allocator::new());
pub static ALLOCATOR: AtomicRefCell<Allocator> = AtomicRefCell::new(Allocator::new());

static mut BLOCK_WRAPPERS: block::BlockWrappers = block::BlockWrappers {
wrappers: [core::ptr::null_mut(); 16],
Expand Down Expand Up @@ -87,7 +87,7 @@ pub extern "win64" fn set_virtual_address_map(
core::slice::from_raw_parts_mut(descriptors as *mut alloc::MemoryDescriptor, count)
};

ALLOCATOR.lock().update_virtual_addresses(descriptors)
ALLOCATOR.borrow_mut().update_virtual_addresses(descriptors)
}

pub extern "win64" fn convert_pointer(_: usize, _: *mut *mut c_void) -> Status {
Expand Down Expand Up @@ -175,7 +175,7 @@ pub extern "win64" fn allocate_pages(
) -> Status {
let (status, new_address) =
ALLOCATOR
.lock()
.borrow_mut()
.allocate_pages(
allocate_type,
memory_type,
Expand All @@ -191,7 +191,7 @@ pub extern "win64" fn allocate_pages(
}

pub extern "win64" fn free_pages(address: PhysicalAddress, _: usize) -> Status {
ALLOCATOR.lock().free_pages(address)
ALLOCATOR.borrow_mut().free_pages(address)
}

pub extern "win64" fn get_memory_map(
Expand All @@ -201,7 +201,7 @@ pub extern "win64" fn get_memory_map(
descriptor_size: *mut usize,
descriptor_version: *mut u32,
) -> Status {
let count = ALLOCATOR.lock().get_descriptor_count();
let count = ALLOCATOR.borrow().get_descriptor_count();
let map_size = core::mem::size_of::<MemoryDescriptor>() * count;
if unsafe { *memory_map_size } < map_size {
unsafe {
Expand All @@ -212,13 +212,13 @@ pub extern "win64" fn get_memory_map(

let out =
unsafe { core::slice::from_raw_parts_mut(out as *mut alloc::MemoryDescriptor, count) };
let count = ALLOCATOR.lock().get_descriptors(out);
let count = ALLOCATOR.borrow().get_descriptors(out);
let map_size = core::mem::size_of::<MemoryDescriptor>() * count;
unsafe {
*memory_map_size = map_size;
*descriptor_version = efi::MEMORY_DESCRIPTOR_VERSION;
*descriptor_size = core::mem::size_of::<MemoryDescriptor>();
*key = ALLOCATOR.lock().get_map_key();
*key = ALLOCATOR.borrow().get_map_key();
}

Status::SUCCESS
Expand All @@ -229,7 +229,7 @@ pub extern "win64" fn allocate_pool(
size: usize,
address: *mut *mut c_void,
) -> Status {
let (status, new_address) = ALLOCATOR.lock().allocate_pages(
let (status, new_address) = ALLOCATOR.borrow_mut().allocate_pages(
AllocateType::AllocateAnyPages,
memory_type,
((size + PAGE_SIZE as usize - 1) / PAGE_SIZE as usize) as u64,
Expand All @@ -246,7 +246,7 @@ pub extern "win64" fn allocate_pool(
}

pub extern "win64" fn free_pool(ptr: *mut c_void) -> Status {
ALLOCATOR.lock().free_pages(ptr as u64)
ALLOCATOR.borrow_mut().free_pages(ptr as u64)
}

pub extern "win64" fn create_event(
Expand Down Expand Up @@ -597,7 +597,7 @@ fn populate_allocator(image_address: u64, image_size: u64) {

for entry in e820_table {
if entry.entry_type == E820_RAM {
ALLOCATOR.lock().add_initial_allocation(
ALLOCATOR.borrow_mut().add_initial_allocation(
MemoryType::ConventionalMemory,
entry.size / PAGE_SIZE,
entry.addr,
Expand All @@ -607,15 +607,15 @@ fn populate_allocator(image_address: u64, image_size: u64) {
}

// Add ourselves
ALLOCATOR.lock().allocate_pages(
ALLOCATOR.borrow_mut().allocate_pages(
AllocateType::AllocateAddress,
MemoryType::RuntimeServicesCode,
1024 * 1024 / PAGE_SIZE,
1024 * 1024,
);

// Add the loaded binary
ALLOCATOR.lock().allocate_pages(
ALLOCATOR.borrow_mut().allocate_pages(
AllocateType::AllocateAddress,
MemoryType::LoaderCode,
image_size / PAGE_SIZE,
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
use core::panic::PanicInfo;

#[macro_use]
mod logger;
mod serial;

#[macro_use]
mod common;
Expand Down
Loading

0 comments on commit 743ce2d

Please sign in to comment.