Skip to content

Commit 381b041

Browse files
committed
Use x86_64 and AtomicRefCell creates
- 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 <joerichey@google.com>
1 parent 71f2756 commit 381b041

File tree

9 files changed

+122
-94
lines changed

9 files changed

+122
-94
lines changed

Cargo.lock

Lines changed: 24 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ log-serial = []
2424
log-panic = ["log-serial"]
2525

2626
[dependencies]
27-
cpuio = "*"
28-
spin = "0.5"
27+
x86_64 = "0.9"
28+
atomic_refcell = "0.1"
2929
r-efi = "2.1.0"
3030

src/efi/block.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ impl<'a> BlockWrapper<'a> {
186186
let last_block = unsafe { (*block).get_capacity() } - 1;
187187

188188
let size = core::mem::size_of::<BlockWrapper>();
189-
let (_status, new_address) = super::ALLOCATOR.lock().allocate_pages(
189+
let (_status, new_address) = super::ALLOCATOR.borrow_mut().allocate_pages(
190190
AllocateType::AllocateAnyPages,
191191
MemoryType::LoaderData,
192192
((size + super::PAGE_SIZE as usize - 1) / super::PAGE_SIZE as usize) as u64,

src/efi/console.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ pub extern "win64" fn stdout_output_string(
5252
message: *mut Char16,
5353
) -> Status {
5454
use core::fmt::Write;
55-
let mut logger = crate::logger::LOGGER.lock();
55+
let mut serial = crate::serial::SERIAL.borrow_mut();
5656
let mut string_end = false;
5757

5858
loop {
@@ -67,7 +67,7 @@ pub extern "win64" fn stdout_output_string(
6767
i += 1;
6868
}
6969
let s = unsafe { core::str::from_utf8_unchecked(&output) };
70-
logger.write_str(s).unwrap();
70+
serial.write_str(s).unwrap();
7171
if string_end {
7272
break;
7373
}

src/efi/file.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ pub extern "win64" fn open(
8484
pub extern "win64" fn close(proto: *mut FileProtocol) -> Status {
8585
let wrapper = container_of!(proto, FileWrapper, proto);
8686
super::ALLOCATOR
87-
.lock()
87+
.borrow_mut()
8888
.free_pages(&wrapper as *const _ as u64)
8989
}
9090

@@ -208,7 +208,7 @@ pub struct FileSystemWrapper<'a> {
208208
impl<'a> FileSystemWrapper<'a> {
209209
fn create_file(&self, root: bool) -> Option<*mut FileWrapper> {
210210
let size = core::mem::size_of::<FileWrapper>();
211-
let (status, new_address) = super::ALLOCATOR.lock().allocate_pages(
211+
let (status, new_address) = super::ALLOCATOR.borrow_mut().allocate_pages(
212212
AllocateType::AllocateAnyPages,
213213
MemoryType::LoaderData,
214214
((size + super::PAGE_SIZE as usize - 1) / super::PAGE_SIZE as usize) as u64,

src/efi/mod.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
use core::ffi::c_void;
1616

17+
use atomic_refcell::AtomicRefCell;
1718
use r_efi::{
1819
efi::{
1920
self, AllocateType, Boolean, CapsuleHeader, Char16, Event, EventNotify, Guid, Handle,
@@ -25,7 +26,6 @@ use r_efi::{
2526
device_path::Protocol as DevicePathProtocol, loaded_image::Protocol as LoadedImageProtocol,
2627
},
2728
};
28-
use spin::Mutex;
2929

3030
mod alloc;
3131
mod block;
@@ -48,7 +48,7 @@ struct HandleWrapper {
4848
handle_type: HandleType,
4949
}
5050

51-
pub static ALLOCATOR: Mutex<Allocator> = Mutex::new(Allocator::new());
51+
pub static ALLOCATOR: AtomicRefCell<Allocator> = AtomicRefCell::new(Allocator::new());
5252

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

90-
ALLOCATOR.lock().update_virtual_addresses(descriptors)
90+
ALLOCATOR.borrow_mut().update_virtual_addresses(descriptors)
9191
}
9292

9393
pub extern "win64" fn convert_pointer(_: usize, _: *mut *mut c_void) -> Status {
@@ -175,7 +175,7 @@ pub extern "win64" fn allocate_pages(
175175
) -> Status {
176176
let (status, new_address) =
177177
ALLOCATOR
178-
.lock()
178+
.borrow_mut()
179179
.allocate_pages(
180180
allocate_type,
181181
memory_type,
@@ -191,7 +191,7 @@ pub extern "win64" fn allocate_pages(
191191
}
192192

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

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

213213
let out =
214214
unsafe { core::slice::from_raw_parts_mut(out as *mut alloc::MemoryDescriptor, count) };
215-
let count = ALLOCATOR.lock().get_descriptors(out);
215+
let count = ALLOCATOR.borrow().get_descriptors(out);
216216
let map_size = core::mem::size_of::<MemoryDescriptor>() * count;
217217
unsafe {
218218
*memory_map_size = map_size;
219219
*descriptor_version = efi::MEMORY_DESCRIPTOR_VERSION;
220220
*descriptor_size = core::mem::size_of::<MemoryDescriptor>();
221-
*key = ALLOCATOR.lock().get_map_key();
221+
*key = ALLOCATOR.borrow().get_map_key();
222222
}
223223

224224
Status::SUCCESS
@@ -229,7 +229,7 @@ pub extern "win64" fn allocate_pool(
229229
size: usize,
230230
address: *mut *mut c_void,
231231
) -> Status {
232-
let (status, new_address) = ALLOCATOR.lock().allocate_pages(
232+
let (status, new_address) = ALLOCATOR.borrow_mut().allocate_pages(
233233
AllocateType::AllocateAnyPages,
234234
memory_type,
235235
((size + PAGE_SIZE as usize - 1) / PAGE_SIZE as usize) as u64,
@@ -246,7 +246,7 @@ pub extern "win64" fn allocate_pool(
246246
}
247247

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

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

598598
for entry in e820_table {
599599
if entry.entry_type == E820_RAM {
600-
ALLOCATOR.lock().add_initial_allocation(
600+
ALLOCATOR.borrow_mut().add_initial_allocation(
601601
MemoryType::ConventionalMemory,
602602
entry.size / PAGE_SIZE,
603603
entry.addr,
@@ -607,15 +607,15 @@ fn populate_allocator(image_address: u64, image_size: u64) {
607607
}
608608

609609
// Add ourselves
610-
ALLOCATOR.lock().allocate_pages(
610+
ALLOCATOR.borrow_mut().allocate_pages(
611611
AllocateType::AllocateAddress,
612612
MemoryType::RuntimeServicesCode,
613613
1024 * 1024 / PAGE_SIZE,
614614
1024 * 1024,
615615
);
616616

617617
// Add the loaded binary
618-
ALLOCATOR.lock().allocate_pages(
618+
ALLOCATOR.borrow_mut().allocate_pages(
619619
AllocateType::AllocateAddress,
620620
MemoryType::LoaderCode,
621621
image_size / PAGE_SIZE,

src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
use core::panic::PanicInfo;
2222

2323
#[macro_use]
24-
mod logger;
24+
mod serial;
2525

2626
#[macro_use]
2727
mod common;

0 commit comments

Comments
 (0)