Skip to content

Commit

Permalink
Update rust, deps, fix exposed issues
Browse files Browse the repository at this point in the history
Update Rust and dependencies, and fix a number of exposed issues
that popped up as a result.  This code is now clippy-clean.

Signed-off-by: Dan Cross <[email protected]>
  • Loading branch information
Dan Cross authored and orangecms committed Jan 30, 2024
1 parent dbcba36 commit e176e4f
Show file tree
Hide file tree
Showing 20 changed files with 151 additions and 151 deletions.
159 changes: 76 additions & 83 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions aarch64/src/kalloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl FreeList {

pub unsafe fn free_pages(pages: &mut [Page4K]) {
static mut NODE: LockNode = LockNode::new();
let mut lock = FREE_LIST.lock(unsafe { &NODE });
let mut lock = FREE_LIST.lock(unsafe { &*ptr::addr_of!(NODE) });
let fl = &mut *lock;
for page in pages.iter_mut() {
fl.put(page);
Expand All @@ -48,7 +48,7 @@ pub unsafe fn free_pages(pages: &mut [Page4K]) {

pub fn alloc() -> Result<&'static mut Page4K, Error> {
static mut NODE: LockNode = LockNode::new();
let mut lock = FREE_LIST.lock(unsafe { &NODE });
let mut lock = FREE_LIST.lock(unsafe { &*ptr::addr_of!(NODE) });
let fl = &mut *lock;
fl.get()
}
10 changes: 5 additions & 5 deletions aarch64/src/kmem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl PhysAddr {
self.0
}

pub const fn to_virt(&self) -> usize {
pub const fn as_virt(&self) -> usize {
(self.0 as usize).wrapping_add(KZERO)
}

Expand All @@ -67,8 +67,8 @@ impl PhysAddr {
Self::from_virt(a.addr())
}

pub const fn to_ptr_mut<T>(&self) -> *mut T {
self.to_virt() as *mut T
pub const fn as_ptr_mut<T>(&self) -> *mut T {
self.as_virt() as *mut T
}

pub const fn round_up(&self, step: u64) -> PhysAddr {
Expand Down Expand Up @@ -102,11 +102,11 @@ impl Step for PhysAddr {
}

fn forward_checked(startpa: Self, count: usize) -> Option<Self> {
startpa.0.checked_add(count as u64).map(|x| PhysAddr(x))
startpa.0.checked_add(count as u64).map(PhysAddr)
}

fn backward_checked(startpa: Self, count: usize) -> Option<Self> {
startpa.0.checked_sub(count as u64).map(|x| PhysAddr(x))
startpa.0.checked_sub(count as u64).map(PhysAddr)
}
}

Expand Down
8 changes: 4 additions & 4 deletions aarch64/src/mailbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ static MAILBOX: Lock<Option<&'static mut Mailbox>> = Lock::new("mailbox", None);
/// can be made at a time. We have no heap at this point, so creating a mailbox
/// that can be initialised based off the devicetree is rather convoluted.
pub fn init(dt: &DeviceTree) {
static mut NODE: LockNode = LockNode::new();
let mut mailbox = MAILBOX.lock(unsafe { &NODE });
let node = LockNode::new();
let mut mailbox = MAILBOX.lock(&node);
*mailbox = Some({
static mut MAYBE_MAILBOX: MaybeUninit<Mailbox> = MaybeUninit::uninit();
unsafe {
Expand Down Expand Up @@ -124,8 +124,8 @@ where
let size = mem::size_of::<Message<T, U>>() as u32;
let req = Request::<Tag<T>> { size, code, tags: *tags };
let mut msg = MessageWithTags { request: req };
static mut NODE: LockNode = LockNode::new();
let mut mailbox = MAILBOX.lock(unsafe { &NODE });
let node = LockNode::new();
let mut mailbox = MAILBOX.lock(&node);
mailbox.as_deref_mut().unwrap().request(&mut msg);
let res = unsafe { msg.response };
res.tags.body
Expand Down
8 changes: 5 additions & 3 deletions aarch64/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![allow(clippy::upper_case_acronyms)]
#![cfg_attr(not(any(test, feature = "cargo-clippy")), no_std)]
#![allow(internal_features)]
#![cfg_attr(not(any(test)), no_std)]
#![cfg_attr(not(test), no_main)]
#![feature(alloc_error_handler)]
#![feature(asm_const)]
Expand All @@ -24,6 +25,7 @@ mod vm;
use crate::kmem::{PhysAddr, PhysRange};
use crate::vm::kernel_root;
use core::ffi::c_void;
use core::ptr;
use port::fdt::DeviceTree;
use port::println;
use vm::PageTable;
Expand Down Expand Up @@ -122,8 +124,8 @@ pub extern "C" fn main9(dtb_va: usize) {
kalloc::free_pages(kmem::early_pages());

let dtb_range = PhysRange::with_len(PhysAddr::from_virt(dtb_va).addr(), dt.size());
vm::init(&dt, &mut KPGTBL, dtb_range);
vm::switch(&KPGTBL);
vm::init(&dt, &mut *ptr::addr_of_mut!(KPGTBL), dtb_range);
vm::switch(&*ptr::addr_of!(KPGTBL));
}

print_binary_sections();
Expand Down
2 changes: 1 addition & 1 deletion aarch64/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use port::mem::VirtRange;
// - Add support for raspi4
#[panic_handler]
pub fn panic(info: &PanicInfo) -> ! {
let mmio = rpi_mmio().expect("mmio base detect failed").start().to_virt();
let mmio = rpi_mmio().expect("mmio base detect failed").start().as_virt();

let gpio_range = VirtRange::with_len(mmio + 0x200000, 0xb4);
let aux_range = VirtRange::with_len(mmio + 0x215000, 0x8);
Expand Down
4 changes: 3 additions & 1 deletion aarch64/src/trap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,7 @@ fn trap(frame: &mut TrapFrame) {
// Just print out the frame and loop for now
// TODO Make it a little prettier and more space efficient
println!("{:#x?}", frame);
loop {}
loop {
core::hint::spin_loop();
}
}
32 changes: 16 additions & 16 deletions aarch64/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ use port::fdt::DeviceTree;
#[cfg(not(test))]
use port::println;

pub const PAGE_SIZE_4K: usize = 4 * 1024;
pub const PAGE_SIZE_2M: usize = 2 * 1024 * 1024;
pub const PAGE_SIZE_1G: usize = 1 * 1024 * 1024 * 1024;
pub const PAGE_SIZE_4K: usize = 4 << 10;
pub const PAGE_SIZE_2M: usize = 2 << 20;
pub const PAGE_SIZE_1G: usize = 1 << 30;

#[allow(dead_code)]
#[derive(Debug, Clone, Copy, PartialEq)]
Expand Down Expand Up @@ -76,10 +76,10 @@ pub enum AccessPermission {
#[repr(u8)]
pub enum Shareable {
#[num_enum(default)]
NonShareable = 0, // Non-shareable (single core)
Unpredictable = 1, // Unpredicatable!
OuterShareable = 2, // Outer shareable (shared across CPUs, GPU)
InnerShareable = 3, // Inner shareable (shared across CPUs)
Non = 0, // Non-shareable (single core)
Unpredictable = 1, // Unpredicatable!
Outer = 2, // Outer shareable (shared across CPUs, GPU)
Inner = 3, // Inner shareable (shared across CPUs)
}

bitstruct! {
Expand Down Expand Up @@ -111,7 +111,7 @@ impl Entry {

fn rw_kernel_data() -> Self {
Entry(0)
.with_shareable(Shareable::InnerShareable)
.with_shareable(Shareable::Inner)
.with_accessed(true)
.with_uxn(true)
.with_pxn(true)
Expand All @@ -122,7 +122,7 @@ impl Entry {
fn ro_kernel_data() -> Self {
Entry(0)
.with_access_permission(AccessPermission::PrivRo)
.with_shareable(Shareable::InnerShareable)
.with_shareable(Shareable::Inner)
.with_accessed(true)
.with_uxn(true)
.with_pxn(true)
Expand All @@ -133,7 +133,7 @@ impl Entry {
fn ro_kernel_text() -> Self {
Entry(0)
.with_access_permission(AccessPermission::PrivRw)
.with_shareable(Shareable::InnerShareable)
.with_shareable(Shareable::Inner)
.with_accessed(true)
.with_uxn(true)
.with_pxn(false)
Expand All @@ -144,7 +144,7 @@ impl Entry {
fn ro_kernel_device() -> Self {
Entry(0)
.with_access_permission(AccessPermission::PrivRw)
.with_shareable(Shareable::InnerShareable)
.with_shareable(Shareable::Inner)
.with_accessed(true)
.with_uxn(true)
.with_pxn(true)
Expand All @@ -162,7 +162,7 @@ impl Entry {
}

fn virt_page_addr(self) -> usize {
self.phys_page_addr().to_virt()
self.phys_page_addr().as_virt()
}

fn table(self, level: Level) -> bool {
Expand Down Expand Up @@ -394,7 +394,7 @@ impl PageTable {
invalidate_all_tlb_entries();
}

return Ok(());
Ok(())
}

/// Map the physical range using the requested page size.
Expand All @@ -413,7 +413,7 @@ impl PageTable {
let mut startva = None;
let mut endva = 0;
for pa in range.step_by_rounded(page_size.size()) {
let va = pa.to_virt();
let va = pa.as_virt();
self.map_to(entry.with_phys_addr(pa), va, page_size)?;
startva.get_or_insert(va);
endva = va + page_size.size();
Expand Down Expand Up @@ -469,7 +469,7 @@ fn print_pte(indent: usize, i: usize, level: Level, pte: Entry) {
}
}

pub unsafe fn init(dt: &DeviceTree, kpage_table: &mut PageTable, dtb_range: PhysRange) {
pub unsafe fn init(_dt: &DeviceTree, kpage_table: &mut PageTable, dtb_range: PhysRange) {
// We use recursive page tables, but we have to be careful in the init call,
// since the kpage_table is not currently pointed to by ttbr1_el1. Any
// recursive addressing of (511, 511, 511, 511) always points to the
Expand Down Expand Up @@ -577,7 +577,7 @@ pub unsafe fn invalidate_all_tlb_entries() {

/// Return the root kernel page table
pub fn kernel_root() -> &'static mut PageTable {
unsafe { &mut *PhysAddr::new(ttbr1_el1()).to_ptr_mut::<PageTable>() }
unsafe { &mut *PhysAddr::new(ttbr1_el1()).as_ptr_mut::<PageTable>() }
}

#[cfg(test)]
Expand Down
4 changes: 2 additions & 2 deletions lib/riscv64-unknown-none-elf.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"arch": "riscv64",
"code-model": "medium",
"cpu": "generic-rv64",
"data-layout": "e-m:e-p:64:64-i64:64-i128:128-n64-S128",
"data-layout": "e-m:e-p:64:64-i64:64-i128:128-n32:64-S128",
"eh-frame-header": false,
"emit-debug-gdb-scripts": false,
"features": "+m,+a,+f,+d,+c",
Expand All @@ -20,4 +20,4 @@
"-nostdlib"
]
}
}
}
4 changes: 2 additions & 2 deletions lib/x86_64-unknown-none-elf.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"llvm-target": "x86_64-unknown-none-elf",
"data-layout": "e-m:e-i64:64-f80:128-n8:16:32:64-S128",
"data-layout": "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128",
"linker-flavor": "ld.lld",
"linker": "rust-lld",
"target-endian": "little",
Expand All @@ -20,4 +20,4 @@
"-nostdlib"
]
}
}
}
16 changes: 9 additions & 7 deletions port/src/dat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use bitflags::bitflags;
use core::ptr::NonNull;
use core::result::Result;

pub struct Error {}

pub struct Chan {
_offset: u64,
_devoffset: u64,
Expand Down Expand Up @@ -59,18 +61,18 @@ pub trait Dev {
fn shutdown();
fn attach(spec: &[u8]) -> Chan;
fn walk(&self, c: &Chan, nc: &mut Chan, name: &[&[u8]]) -> Walkqid;
fn stat(&self, c: &Chan, sb: &mut [u8]) -> Result<(), ()>;
fn stat(&self, c: &Chan, sb: &mut [u8]) -> Result<(), Error>;
fn open(&mut self, c: &Chan, mode: u32) -> Chan;
fn create(&mut self, c: &mut Chan, name: &[u8], mode: Mode, perms: u32);
fn close(&mut self, c: Chan);
fn read(&mut self, c: &mut Chan, buf: &mut [u8], offset: u64) -> Result<u64, ()>;
fn bread(&mut self, c: &mut Chan, bnum: u64, offset: u64) -> Result<Block, ()>;
fn write(&mut self, c: &mut Chan, buf: &[u8], offset: u64) -> Result<u64, ()>;
fn bwrite(&mut self, c: &mut Chan, block: &Block, offset: u64) -> Result<u64, ()>;
fn read(&mut self, c: &mut Chan, buf: &mut [u8], offset: u64) -> Result<u64, Error>;
fn bread(&mut self, c: &mut Chan, bnum: u64, offset: u64) -> Result<Block, Error>;
fn write(&mut self, c: &mut Chan, buf: &[u8], offset: u64) -> Result<u64, Error>;
fn bwrite(&mut self, c: &mut Chan, block: &Block, offset: u64) -> Result<u64, Error>;
fn remove(&mut self, c: &mut Chan);
fn wstat(&mut self, c: &mut Chan, sb: &[u8]) -> Result<(), ()>;
fn wstat(&mut self, c: &mut Chan, sb: &[u8]) -> Result<(), Error>;
fn power(&mut self, on: bool);
fn config(&mut self /* other args */) -> Result<(), ()>;
fn config(&mut self /* other args */) -> Result<(), Error>;
}

pub struct Block {
Expand Down
8 changes: 4 additions & 4 deletions port/src/devcons.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,17 @@ impl Console {
where
F: FnOnce() -> &'static mut dyn Uart,
{
static mut NODE: LockNode = LockNode::new();
let mut cons = CONS.lock(unsafe { &NODE });
let node = LockNode::new();
let mut cons = CONS.lock(&node);
*cons = Some(uart_fn());
Self
}

pub fn putstr(&mut self, s: &str) {
// XXX: Just for testing.

static mut NODE: LockNode = LockNode::new();
let mut uart_guard = CONS.lock(unsafe { &NODE });
let node = LockNode::new();
let mut uart_guard = CONS.lock(&node);
let uart = uart_guard.as_deref_mut().unwrap();
for b in s.bytes() {
putb(uart, b);
Expand Down
18 changes: 10 additions & 8 deletions port/src/fdt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ impl<'a> DeviceTree<'a> {
}

/// Given a pointer to the dtb as a usize, return a DeviceTree struct.
///
/// # Safety
/// The caller must ensure that `ptr` is a valid virtual address.
pub unsafe fn from_usize(ptr: usize) -> Result<Self> {
let u8ptr = ptr as *const mem::MaybeUninit<u8>;

Expand All @@ -79,8 +82,7 @@ impl<'a> DeviceTree<'a> {
let len = dtb_for_header.header.totalsize as usize;

// Extract the buffer for real
let dtb_buf: &[mem::MaybeUninit<u8>] =
unsafe { core::slice::from_raw_parts(u8ptr as *const MaybeUninit<u8>, len) };
let dtb_buf: &[mem::MaybeUninit<u8>] = unsafe { core::slice::from_raw_parts(u8ptr, len) };
FdtHeader::new(dtb_buf, false).map(|header| Self { data: dtb_buf, header })
}

Expand Down Expand Up @@ -158,7 +160,7 @@ impl<'a> DeviceTree<'a> {

pub fn property_value_as_u32(&self, prop: &Property) -> Option<u32> {
let value_end = prop.value_start + prop.value_len;
self.structs().get(prop.value_start..value_end).and_then(|bs| bytes_to_u32(bs))
self.structs().get(prop.value_start..value_end).and_then(bytes_to_u32)
}

pub fn property_value_as_u32_iter(&self, prop: &Property) -> impl Iterator<Item = u32> + '_ {
Expand All @@ -170,7 +172,7 @@ impl<'a> DeviceTree<'a> {
}
let (start, end) = (value_i, value_i + 4);
value_i = end;
return self.structs().get(start..end).and_then(|bs| bytes_to_u32(bs));
return self.structs().get(start..end).and_then(bytes_to_u32);
})
}

Expand All @@ -191,7 +193,7 @@ impl<'a> DeviceTree<'a> {
let bytes_fn = if num_cells == 1 { bytes_to_u32_as_u64 } else { bytes_to_u64 };
let start = value_i;
let end = value_i + (num_cells * 4);
self.structs().get(start..end).and_then(|bs| bytes_fn(bs))
self.structs().get(start..end).and_then(bytes_fn)
}

/// Return the reg values as u64 whether the size is 1 or 2 cells.
Expand Down Expand Up @@ -542,7 +544,7 @@ impl<'a> DeviceTree<'a> {
}

fn parse_token(structs: &[mem::MaybeUninit<u8>], i: usize) -> Option<FdtToken> {
let token = structs.get(i..).and_then(|bs| bytes_to_u32(bs));
let token = structs.get(i..).and_then(bytes_to_u32);

match token {
Some(0x1) => {
Expand All @@ -562,8 +564,8 @@ impl<'a> DeviceTree<'a> {
}
Some(0x2) => Some(FdtToken::EndNode(FdtTokenContext { start: i, total_len: 4 })),
Some(0x3) => {
let len = structs.get((i + 4)..).and_then(|bs| bytes_to_u32(bs)).unwrap_or(0);
let nameoff = structs.get((i + 8)..).and_then(|bs| bytes_to_u32(bs)).unwrap_or(0);
let len = structs.get((i + 4)..).and_then(bytes_to_u32).unwrap_or(0);
let nameoff = structs.get((i + 8)..).and_then(bytes_to_u32).unwrap_or(0);
Some(FdtToken::Prop(FdtPropContext {
start: i,
name_start: nameoff as usize,
Expand Down
2 changes: 1 addition & 1 deletion port/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#![allow(clippy::upper_case_acronyms)]
#![cfg_attr(not(any(test, feature = "cargo-clippy")), no_std)]
#![cfg_attr(not(any(test)), no_std)]
#![feature(maybe_uninit_slice)]
#![forbid(unsafe_op_in_unsafe_fn)]

Expand Down
2 changes: 1 addition & 1 deletion riscv64/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![feature(alloc_error_handler)]
#![feature(asm_const)]
#![feature(panic_info_message)]
#![cfg_attr(not(any(test, feature = "cargo-clippy")), no_std)]
#![cfg_attr(not(any(test)), no_std)]
#![cfg_attr(not(test), no_main)]
#![allow(clippy::upper_case_acronyms)]
#![forbid(unsafe_op_in_unsafe_fn)]
Expand Down
Loading

0 comments on commit e176e4f

Please sign in to comment.