Skip to content

Commit

Permalink
tool: apply rustfmt
Browse files Browse the repository at this point in the history
Signed-off-by: Nick Spinale <[email protected]>
  • Loading branch information
nspin authored and Ivan-Velickovic committed Jul 11, 2024
1 parent d20835b commit db6d2cf
Show file tree
Hide file tree
Showing 8 changed files with 1,818 additions and 699 deletions.
58 changes: 43 additions & 15 deletions tool/microkit/src/elf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
// SPDX-License-Identifier: BSD-2-Clause
//

use crate::util::bytes_to_struct;
use std::collections::HashMap;
use std::fs;
use std::path::Path;
use std::collections::HashMap;
use crate::util::bytes_to_struct;

#[repr(C, packed)]
struct ElfHeader32 {
Expand Down Expand Up @@ -142,7 +142,7 @@ impl ElfFile {
pub fn from_path(path: &Path) -> Result<ElfFile, String> {
let bytes = match fs::read(path) {
Ok(bytes) => bytes,
Err(err) => return Err(format!("Failed to read ELF '{}': {}", path.display(), err))
Err(err) => return Err(format!("Failed to read ELF '{}': {}", path.display(), err)),
};

let magic = &bytes[0..4];
Expand All @@ -158,12 +158,18 @@ impl ElfFile {
1 => {
hdr_size = std::mem::size_of::<ElfHeader32>();
word_size = 32;
},
}
2 => {
hdr_size = std::mem::size_of::<ElfHeader64>();
word_size = 64;
},
_ => return Err(format!("ELF '{}': invalid class '{}'", path.display(), class))
}
_ => {
return Err(format!(
"ELF '{}': invalid class '{}'",
path.display(),
class
))
}
};

// Now need to read the header into a struct
Expand All @@ -176,7 +182,10 @@ impl ElfFile {
assert!(hdr.ident_class == *class);

if hdr.ident_data != 1 {
return Err(format!("ELF '{}': incorrect endianness, only little endian architectures are supported", path.display()));
return Err(format!(
"ELF '{}': incorrect endianness, only little endian architectures are supported",
path.display()
));
}

let entry = hdr.entry;
Expand All @@ -201,7 +210,7 @@ impl ElfFile {
phys_addr: phent.paddr,
virt_addr: phent.vaddr,
loadable: phent.type_ == 1,
attrs: phent.flags
attrs: phent.flags,
};

segments.push(segment)
Expand All @@ -226,12 +235,18 @@ impl ElfFile {
}

if shstrtab_shent.is_none() {
return Err(format!("ELF '{}': unable to find string table section", path.display()));
return Err(format!(
"ELF '{}': unable to find string table section",
path.display()
));
}

assert!(symtab_shent.is_some());
if symtab_shent.is_none() {
return Err(format!("ELF '{}': unable to find symbol table section", path.display()));
return Err(format!(
"ELF '{}': unable to find symbol table section",
path.display()
));
}

// Reading the symbol table
Expand Down Expand Up @@ -274,13 +289,20 @@ impl ElfFile {
offset += symbol_size;
}

Ok(ElfFile { word_size, entry, segments, symbols })
Ok(ElfFile {
word_size,
entry,
segments,
symbols,
})
}

pub fn find_symbol(&self, variable_name: &str) -> Result<(u64, u64), String> {
if let Some((sym, duplicate)) = self.symbols.get(variable_name) {
if *duplicate {
Err(format!("Found multiple symbols with name '{variable_name}'"))
Err(format!(
"Found multiple symbols with name '{variable_name}'"
))
} else {
Ok((sym.value, sym.size))
}
Expand Down Expand Up @@ -320,10 +342,16 @@ impl ElfFile {
let end_idx = idx + null_byte_pos;
match std::str::from_utf8(&strtab[idx..end_idx]) {
Ok(string) => Ok(string),
Err(err) => Err(format!("Failed to convert strtab bytes to UTF-8 string: {}", err))
Err(err) => Err(format!(
"Failed to convert strtab bytes to UTF-8 string: {}",
err
)),
}
},
None => Err(format!("Could not find null byte in strtab from index {}", idx)),
}
None => Err(format!(
"Could not find null byte in strtab from index {}",
idx
)),
}
}
}
61 changes: 47 additions & 14 deletions tool/microkit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ pub mod sel4;
pub mod sysxml;
pub mod util;

use std::fmt;
use std::cmp::min;
use sel4::BootInfo;
use std::cmp::min;
use std::fmt;

// Note that this value is used in the monitor so should also be changed there
// if this was to change.
Expand All @@ -31,7 +31,11 @@ pub struct UntypedObject {

impl UntypedObject {
pub fn new(cap: u64, region: MemoryRegion, is_device: bool) -> UntypedObject {
UntypedObject { cap, region, is_device }
UntypedObject {
cap,
region,
is_device,
}
}

pub fn base(&self) -> u64 {
Expand Down Expand Up @@ -59,7 +63,12 @@ pub struct Region {

impl Region {
pub fn new(name: String, addr: u64, size: u64, segment_idx: usize) -> Region {
Region { name, addr, size, segment_idx }
Region {
name,
addr,
size,
segment_idx,
}
}

pub fn data<'a>(&self, elf: &'a elf::ElfFile) -> &'a Vec<u8> {
Expand All @@ -69,7 +78,11 @@ impl Region {

impl fmt::Display for Region {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "<Region name={} addr=0x{:x} size={}>", self.name, self.addr, self.size)
write!(
f,
"<Region name={} addr=0x{:x} size={}>",
self.name, self.addr, self.size
)
}
}

Expand All @@ -84,7 +97,11 @@ pub struct MemoryRegion {

impl fmt::Display for MemoryRegion {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "MemoryRegion(base=0x{:x}, end=0x{:x})", self.base, self.end)
write!(
f,
"MemoryRegion(base=0x{:x}, end=0x{:x})",
self.base, self.end
)
}
}

Expand Down Expand Up @@ -150,7 +167,8 @@ impl DisjointMemoryRegion {
}
// FIXME: Should extend here if adjacent rather than
// inserting now
self.regions.insert(insert_idx, MemoryRegion::new(base, end));
self.regions
.insert(insert_idx, MemoryRegion::new(base, end));
self.check();
}

Expand Down Expand Up @@ -182,7 +200,8 @@ impl DisjointMemoryRegion {
} else {
// Splitting
self.regions[idx] = MemoryRegion::new(region.base, base);
self.regions.insert(idx + 1, MemoryRegion::new(end, region.end));
self.regions
.insert(idx + 1, MemoryRegion::new(end, region.end));
}

self.check();
Expand Down Expand Up @@ -216,8 +235,8 @@ impl DisjointMemoryRegion {
Some(region) => {
self.remove_region(region.base, region.base + size);
region.base
},
None => panic!("Unable to allocate {} bytes", size)
}
None => panic!("Unable to allocate {} bytes", size),
}
}
}
Expand All @@ -235,8 +254,16 @@ pub struct UntypedAllocator {
}

impl UntypedAllocator {
pub fn new(untyped_object: UntypedObject, allocation_point: u64, allocations: Vec<KernelAllocation>) -> UntypedAllocator {
UntypedAllocator { untyped_object, allocation_point, allocations }
pub fn new(
untyped_object: UntypedObject,
allocation_point: u64,
allocations: Vec<KernelAllocation>,
) -> UntypedAllocator {
UntypedAllocator {
untyped_object,
allocation_point,
allocations,
}
}

pub fn base(&self) -> u64 {
Expand Down Expand Up @@ -284,7 +311,10 @@ impl ObjectAllocator {
untyped.push(UntypedAllocator::new(*ut, 0, vec![]));
}

ObjectAllocator { allocation_idx: 0, untyped }
ObjectAllocator {
allocation_idx: 0,
untyped,
}
}

pub fn alloc(&mut self, size: u64) -> KernelAllocation {
Expand All @@ -300,7 +330,10 @@ impl ObjectAllocator {
if start + (count * size) <= ut.end() {
ut.allocation_point = (start - ut.base()) + (count * size);
self.allocation_idx += 1;
let allocation = KernelAllocation { untyped_cap_address: ut.untyped_object.cap, phys_addr: start };
let allocation = KernelAllocation {
untyped_cap_address: ut.untyped_object.cap,
phys_addr: start,
};
ut.allocations.push(allocation);
return allocation;
}
Expand Down
Loading

0 comments on commit db6d2cf

Please sign in to comment.