From 3279aab96139c86a0b26dfab8b72838e0125aea2 Mon Sep 17 00:00:00 2001 From: Astro Date: Mon, 11 Nov 2019 02:37:06 +0100 Subject: [PATCH] main: refactor into abort, panic, ram --- src/abort.rs | 13 +++++++++ src/main.rs | 65 +++++---------------------------------------- src/panic.rs | 9 +++++++ src/ram.rs | 42 +++++++++++++++++++++++++++++ src/zynq/ddr/mod.rs | 1 - 5 files changed, 71 insertions(+), 59 deletions(-) create mode 100644 src/abort.rs create mode 100644 src/panic.rs create mode 100644 src/ram.rs diff --git a/src/abort.rs b/src/abort.rs new file mode 100644 index 0000000..f177c5b --- /dev/null +++ b/src/abort.rs @@ -0,0 +1,13 @@ +use crate::println; + +#[no_mangle] +pub unsafe extern "C" fn PrefetchAbort() { + println!("PrefetchAbort"); + loop {} +} + +#[no_mangle] +pub unsafe extern "C" fn DataAbort() { + println!("DataAbort"); + loop {} +} diff --git a/src/main.rs b/src/main.rs index 1c60486..3511841 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,10 +10,7 @@ #![allow(dead_code)] extern crate alloc; -use alloc::{vec, vec::Vec, alloc::Layout}; -use core::alloc::GlobalAlloc; -use core::ptr::NonNull; -use core::cell::RefCell; +use alloc::{vec, vec::Vec}; use core::mem::transmute; use r0::zero_bss; use compiler_builtins as _; @@ -21,12 +18,14 @@ use smoltcp::wire::{EthernetAddress, IpAddress, IpCidr}; use smoltcp::iface::{NeighborCache, EthernetInterfaceBuilder}; use smoltcp::time::Instant; use smoltcp::socket::SocketSet; -use linked_list_allocator::Heap; mod regs; mod cortex_a9; -mod stdio; +mod abort; +mod panic; mod zynq; +mod stdio; +mod ram; use crate::regs::{RegisterR, RegisterW}; use crate::cortex_a9::{asm, regs::*, mmu}; @@ -97,11 +96,7 @@ fn main() { let mut ddr = zynq::ddr::DdrRam::new(); println!("DDR: {:?}", ddr.status()); ddr.memtest(); - - unsafe { - ALLOCATOR.0.borrow_mut() - .init(ddr.ptr::() as usize, ddr.size()); - } + ram::init_alloc(&mut ddr); let eth = zynq::eth::Eth::default(HWADDR.clone()); println!("Eth on"); @@ -181,52 +176,6 @@ fn main() { // None => println!("eth tx shortage"), // } } + } -#[panic_handler] -fn panic(info: &core::panic::PanicInfo) -> ! { - println!("\nPanic: {}", info); - - zynq::slcr::RegisterBlock::unlocked(|slcr| slcr.soft_reset()); - loop {} -} - -#[global_allocator] -static ALLOCATOR: HeapAlloc = HeapAlloc(RefCell::new(Heap::empty())); - -/// LockedHeap doesn't locking properly -struct HeapAlloc(RefCell); - -/// FIXME: unsound; lock properly -unsafe impl Sync for HeapAlloc {} - -unsafe impl GlobalAlloc for HeapAlloc { - unsafe fn alloc(&self, layout: Layout) -> *mut u8 { - self.0.borrow_mut() - .allocate_first_fit(layout) - .ok() - .map_or(0 as *mut u8, |allocation| allocation.as_ptr()) - } - - unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) { - self.0.borrow_mut() - .deallocate(NonNull::new_unchecked(ptr), layout) - } -} - -#[alloc_error_handler] -fn alloc_error(_: core::alloc::Layout) -> ! { - panic!("alloc_error") -} - -#[no_mangle] -pub unsafe extern "C" fn PrefetchAbort() { - println!("PrefetchAbort"); - loop {} -} - -#[no_mangle] -pub unsafe extern "C" fn DataAbort() { - println!("DataAbort"); - loop {} -} diff --git a/src/panic.rs b/src/panic.rs new file mode 100644 index 0000000..f637b59 --- /dev/null +++ b/src/panic.rs @@ -0,0 +1,9 @@ +use crate::{println, zynq}; + +#[panic_handler] +fn panic(info: &core::panic::PanicInfo) -> ! { + println!("\nPanic: {}", info); + + zynq::slcr::RegisterBlock::unlocked(|slcr| slcr.soft_reset()); + loop {} +} diff --git a/src/ram.rs b/src/ram.rs new file mode 100644 index 0000000..1d932e1 --- /dev/null +++ b/src/ram.rs @@ -0,0 +1,42 @@ +use core::cell::RefCell; +use core::alloc::GlobalAlloc; +use core::ptr::NonNull; +use alloc::alloc::Layout; +use linked_list_allocator::Heap; +use crate::zynq::ddr::DdrRam; + +#[global_allocator] +static ALLOCATOR: HeapAlloc = HeapAlloc(RefCell::new(Heap::empty())); + +/// LockedHeap doesn't locking properly +struct HeapAlloc(RefCell); + +/// FIXME: unsound; lock properly +unsafe impl Sync for HeapAlloc {} + +unsafe impl GlobalAlloc for HeapAlloc { + unsafe fn alloc(&self, layout: Layout) -> *mut u8 { + self.0.borrow_mut() + .allocate_first_fit(layout) + .ok() + .map_or(0 as *mut u8, |allocation| allocation.as_ptr()) + } + + unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) { + self.0.borrow_mut() + .deallocate(NonNull::new_unchecked(ptr), layout) + } +} + +pub fn init_alloc(ddr: &mut DdrRam) { + unsafe { + ALLOCATOR.0.borrow_mut() + .init(ddr.ptr::() as usize, ddr.size()); + } +} + + +#[alloc_error_handler] +fn alloc_error(_: core::alloc::Layout) -> ! { + panic!("alloc_error") +} diff --git a/src/zynq/ddr/mod.rs b/src/zynq/ddr/mod.rs index fc65528..654a3dc 100644 --- a/src/zynq/ddr/mod.rs +++ b/src/zynq/ddr/mod.rs @@ -186,7 +186,6 @@ impl DdrRam { self.regs.mode_sts_reg.read().operating_mode() } - // TODO: move into trait pub fn ptr(&mut self) -> *mut T { 0x0010_0000 as *mut _ }