MemoryManager: clippy findings

Change-Id: I9a82a9cd7628c7a770752baccf382a7c4e7b625c
GitOrigin-RevId: 7cf81bf93076305d733ea658fae08e58c3e6f7f1
This commit is contained in:
Sam Leffler
2022-05-11 23:18:26 +00:00
parent 06d636f27f
commit e2ec09e001
3 changed files with 99 additions and 110 deletions

View File

@@ -2,6 +2,7 @@
// Code here binds the camkes component to the rust code. // Code here binds the camkes component to the rust code.
#![no_std] #![no_std]
#![allow(clippy::missing_safety_doc)]
use core::ops::Range; use core::ops::Range;
use core::slice; use core::slice;
@@ -41,7 +42,7 @@ extern "C" {
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn pre_init() { pub unsafe extern "C" fn pre_init() {
static KATA_LOGGER: KataLogger = KataLogger; static KATA_LOGGER: KataLogger = KataLogger;
log::set_logger(&KATA_LOGGER).unwrap(); log::set_logger(&KATA_LOGGER).unwrap();
// NB: set to max; the LoggerInterface will filter // NB: set to max; the LoggerInterface will filter
@@ -49,19 +50,16 @@ pub extern "C" fn pre_init() {
// TODO(sleffler): temp until we integrate with seL4 // TODO(sleffler): temp until we integrate with seL4
static mut HEAP_MEMORY: [u8; 8 * 1024] = [0; 8 * 1024]; static mut HEAP_MEMORY: [u8; 8 * 1024] = [0; 8 * 1024];
unsafe {
allocator::ALLOCATOR.init(HEAP_MEMORY.as_mut_ptr() as usize, HEAP_MEMORY.len()); allocator::ALLOCATOR.init(HEAP_MEMORY.as_mut_ptr() as usize, HEAP_MEMORY.len());
trace!( trace!(
"setup heap: start_addr {:p} size {}", "setup heap: start_addr {:p} size {}",
HEAP_MEMORY.as_ptr(), HEAP_MEMORY.as_ptr(),
HEAP_MEMORY.len() HEAP_MEMORY.len()
); );
}
extern "C" { extern "C" {
fn sel4runtime_bootinfo() -> *const seL4_BootInfo; fn sel4runtime_bootinfo() -> *const seL4_BootInfo;
} }
unsafe {
// The MemoryManager component is labeled to receive BootInfo); use // The MemoryManager component is labeled to receive BootInfo); use
// it to complete initialization of the MemoryManager interface. // it to complete initialization of the MemoryManager interface.
let bootinfo = &*sel4runtime_bootinfo(); let bootinfo = &*sel4runtime_bootinfo();
@@ -86,19 +84,15 @@ pub extern "C" fn pre_init() {
trace!("setup cspace slots: first slot {} free {}", trace!("setup cspace slots: first slot {} free {}",
KATA_CSPACE_SLOTS.base_slot(), KATA_CSPACE_SLOTS.base_slot(),
KATA_CSPACE_SLOTS.free_slots()); KATA_CSPACE_SLOTS.free_slots());
}
unsafe {
// Delete the CAmkES-setup CNode; we're going to reuse the // Delete the CAmkES-setup CNode; we're going to reuse the
// well-known slot once it is empty (see below). // well-known slot once it is empty (see memory__init below).
seL4_CNode_Delete(SELF_CNODE, MEMORY_RECV_CNODE, seL4_WordBits as u8) seL4_CNode_Delete(SELF_CNODE, MEMORY_RECV_CNODE, seL4_WordBits as u8)
.expect("recv_node"); .expect("recv_node");
}
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn memory__init() { pub unsafe extern "C" fn memory__init() {
unsafe {
// Point the receive path to the well-known slot that was emptied. // Point the receive path to the well-known slot that was emptied.
// This will be used to receive CNode's from clients for alloc & // This will be used to receive CNode's from clients for alloc &
// free requests. // free requests.
@@ -108,7 +102,6 @@ pub extern "C" fn memory__init() {
// the correct ipc buffer). // the correct ipc buffer).
seL4_SetCapReceivePath(SELF_CNODE, MEMORY_RECV_CNODE, seL4_WordBits); seL4_SetCapReceivePath(SELF_CNODE, MEMORY_RECV_CNODE, seL4_WordBits);
trace!("Cap receive path {}:{}:{}", SELF_CNODE, MEMORY_RECV_CNODE, seL4_WordBits); trace!("Cap receive path {}:{}:{}", SELF_CNODE, MEMORY_RECV_CNODE, seL4_WordBits);
}
} }
// MemoryInterface glue stubs. // MemoryInterface glue stubs.
@@ -124,11 +117,10 @@ fn clear_path(&(root, index, depth): &(seL4_CPtr, seL4_CPtr, seL4_Word)) {
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn memory_alloc( pub unsafe extern "C" fn memory_alloc(
c_raw_data_len: u32, c_raw_data_len: u32,
c_raw_data: *const u8, c_raw_data: *const u8,
) -> MemoryManagerError { ) -> MemoryManagerError {
unsafe {
let recv_path = seL4_GetCapReceivePath(); let recv_path = seL4_GetCapReceivePath();
// NB: make sure noone clobbers the setup done in memory__init // NB: make sure noone clobbers the setup done in memory__init
assert_eq!(recv_path, (SELF_CNODE, MEMORY_RECV_CNODE, seL4_WordBits)); assert_eq!(recv_path, (SELF_CNODE, MEMORY_RECV_CNODE, seL4_WordBits));
@@ -146,15 +138,13 @@ pub extern "C" fn memory_alloc(
// NB: must clear ReceivePath for next request // NB: must clear ReceivePath for next request
clear_path(&recv_path); clear_path(&recv_path);
ret_status ret_status
}
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn memory_free( pub unsafe extern "C" fn memory_free(
c_raw_data_len: u32, c_raw_data_len: u32,
c_raw_data: *const u8, c_raw_data: *const u8,
) -> MemoryManagerError { ) -> MemoryManagerError {
unsafe {
let recv_path = seL4_GetCapReceivePath(); let recv_path = seL4_GetCapReceivePath();
// NB: make sure noone clobbers the setup done in memory__init // NB: make sure noone clobbers the setup done in memory__init
assert_eq!(recv_path, (SELF_CNODE, MEMORY_RECV_CNODE, seL4_WordBits)); assert_eq!(recv_path, (SELF_CNODE, MEMORY_RECV_CNODE, seL4_WordBits));
@@ -172,14 +162,12 @@ pub extern "C" fn memory_free(
// NB: must clear ReceivePath for next request // NB: must clear ReceivePath for next request
clear_path(&recv_path); clear_path(&recv_path);
ret_status ret_status
}
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn memory_stats( pub unsafe extern "C" fn memory_stats(
c_raw_resp_data: *mut RawMemoryStatsData, c_raw_resp_data: *mut RawMemoryStatsData,
) -> MemoryManagerError { ) -> MemoryManagerError {
unsafe {
// TODO(sleffler): verify no cap was received // TODO(sleffler): verify no cap was received
match KATA_MEMORY.stats() { match KATA_MEMORY.stats() {
Ok(stats) => { Ok(stats) => {
@@ -190,5 +178,4 @@ pub extern "C" fn memory_stats(
} }
Err(e) => e.into(), Err(e) => e.into(),
} }
}
} }

View File

@@ -10,7 +10,6 @@ use core::fmt;
use kata_os_common::slot_allocator; use kata_os_common::slot_allocator;
use kata_os_common::sel4_sys; use kata_os_common::sel4_sys;
use log::trace; use log::trace;
use postcard;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use sel4_sys::seL4_CNode_Move; use sel4_sys::seL4_CNode_Move;
@@ -150,6 +149,9 @@ impl ObjDescBundle {
ObjDescBundle { cnode, depth, objs } ObjDescBundle { cnode, depth, objs }
} }
// Returns whether there are any object descriptors.
pub fn is_empty(&self) -> bool { self.objs.len() == 0 }
// Returns the number of object descriptors. // Returns the number of object descriptors.
pub fn len(&self) -> usize { self.objs.len() } pub fn len(&self) -> usize { self.objs.len() }
@@ -239,7 +241,7 @@ impl ObjDescBundle {
} }
unsafe { KATA_CSPACE_SLOTS.free(od.cptr, count) }; unsafe { KATA_CSPACE_SLOTS.free(od.cptr, count) };
od.cptr = dest_slot; od.cptr = dest_slot;
dest_slot = dest_slot + count; dest_slot += count;
} }
self.cnode = dest_cnode; self.cnode = dest_cnode;
self.depth = dest_depth; self.depth = dest_depth;
@@ -347,7 +349,7 @@ impl From<MemoryError> for MemoryManagerError {
impl From<Result<(), MemoryError>> for MemoryManagerError { impl From<Result<(), MemoryError>> for MemoryManagerError {
fn from(result: Result<(), MemoryError>) -> MemoryManagerError { fn from(result: Result<(), MemoryError>) -> MemoryManagerError {
result.map_or_else( result.map_or_else(
|e| MemoryManagerError::from(e), MemoryManagerError::from,
|_v| MemoryManagerError::MmeSuccess, |_v| MemoryManagerError::MmeSuccess,
) )
} }
@@ -400,7 +402,7 @@ pub fn kata_object_alloc_in_toplevel(
let mut request = ObjDescBundle::new( let mut request = ObjDescBundle::new(
unsafe { MEMORY_RECV_CNODE }, unsafe { MEMORY_RECV_CNODE },
unsafe { MEMORY_RECV_CNODE_DEPTH }, unsafe { MEMORY_RECV_CNODE_DEPTH },
objs.clone(), objs,
); );
kata_object_alloc(&request)?; kata_object_alloc(&request)?;
match request.move_objects_to_toplevel() { match request.move_objects_to_toplevel() {
@@ -424,7 +426,7 @@ pub fn kata_object_alloc_in_cnode(
fn next_log2(value: usize) -> usize { fn next_log2(value: usize) -> usize {
let mut size_bits = 0; let mut size_bits = 0;
while value > (1 << size_bits) { while value > (1 << size_bits) {
size_bits = size_bits + 1; size_bits += 1;
} }
size_bits size_bits
} }
@@ -437,7 +439,7 @@ pub fn kata_object_alloc_in_cnode(
// Now construct the request for |objs| with |cnode| as the container. // Now construct the request for |objs| with |cnode| as the container.
let request = ObjDescBundle::new( let request = ObjDescBundle::new(
cnode.objs[0].cptr, cnode_depth as u8, objs.clone(), cnode.objs[0].cptr, cnode_depth as u8, objs,
); );
match kata_object_alloc(&request) { match kata_object_alloc(&request) {
Err(e) => { Err(e) => {
@@ -642,7 +644,7 @@ pub fn kata_object_free_in_cnode(
/*cptr=*/ request.cnode /*cptr=*/ request.cnode
)], )],
); );
kata_object_free(&request)?; kata_object_free(request)?;
// No way to recover if this fails.. // No way to recover if this fails..
kata_object_free_toplevel(&cnode_obj) kata_object_free_toplevel(&cnode_obj)
} }
@@ -666,7 +668,7 @@ pub fn kata_memory_stats() -> Result<MemoryManagerStats, MemoryManagerError> {
fn memory_stats(c_data: *mut RawMemoryStatsData) -> MemoryManagerError; fn memory_stats(c_data: *mut RawMemoryStatsData) -> MemoryManagerError;
} }
let raw_data = &mut [0u8; RAW_MEMORY_STATS_DATA_SIZE]; let raw_data = &mut [0u8; RAW_MEMORY_STATS_DATA_SIZE];
match unsafe { memory_stats(raw_data as *mut _) }.into() { match unsafe { memory_stats(raw_data as *mut _) } {
MemoryManagerError::MmeSuccess => { MemoryManagerError::MmeSuccess => {
let stats = postcard::from_bytes::<MemoryManagerStats>(raw_data) let stats = postcard::from_bytes::<MemoryManagerStats>(raw_data)
.map_err(|_| MemoryManagerError::MmeDeserializeFailed)?; .map_err(|_| MemoryManagerError::MmeDeserializeFailed)?;

View File

@@ -47,7 +47,7 @@ impl UntypedSlab {
size_bits: ut.size_bits(), size_bits: ut.size_bits(),
_base_paddr: ut.paddr, _base_paddr: ut.paddr,
_last_paddr: ut.paddr + (1 << ut.size_bits()), _last_paddr: ut.paddr + (1 << ut.size_bits()),
cptr: cptr, cptr,
_next_paddr: ut.paddr, _next_paddr: ut.paddr,
} }
} }
@@ -89,7 +89,7 @@ impl MemoryManager {
// Creates a new MemoryManager instance. The allocator is seeded // Creates a new MemoryManager instance. The allocator is seeded
// from the untyped memory descriptors. // from the untyped memory descriptors.
pub fn new(slots: Range<seL4_CPtr>, untypeds: &[seL4_UntypedDesc]) -> Self { pub fn new(slots: Range<seL4_CPtr>, untypeds: &[seL4_UntypedDesc]) -> Self {
assert!(untypeds.len() > 0); assert!(!untypeds.is_empty());
assert_eq!(slots.end - slots.start, untypeds.len()); assert_eq!(slots.end - slots.start, untypeds.len());
let mut m = MemoryManager { let mut m = MemoryManager {
untypeds: SmallVec::new(), untypeds: SmallVec::new(),