From 1f728686ff0bae06ebe66be5656ce54c3fa4cac7 Mon Sep 17 00:00:00 2001 From: Astro Date: Thu, 31 Oct 2019 01:41:10 +0100 Subject: [PATCH] rm ram, add linked_list_allocator on ddr --- Cargo.lock | 16 ++++++++++++ Cargo.toml | 1 + src/main.rs | 15 ++++++++++- src/ram.rs | 75 ----------------------------------------------------- 4 files changed, 31 insertions(+), 76 deletions(-) delete mode 100644 src/ram.rs diff --git a/Cargo.lock b/Cargo.lock index e9cfc196..d458b0dc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -20,6 +20,14 @@ name = "compiler_builtins" version = "0.1.19" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "linked_list_allocator" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "spin 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "managed" version = "0.7.1" @@ -40,6 +48,11 @@ dependencies = [ "managed 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "spin" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "vcell" version = "0.1.0" @@ -59,6 +72,7 @@ version = "0.0.0" dependencies = [ "bit_field 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)", "compiler_builtins 0.1.19 (registry+https://github.com/rust-lang/crates.io-index)", + "linked_list_allocator 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)", "r0 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "smoltcp 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", "volatile-register 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -69,8 +83,10 @@ dependencies = [ "checksum bitflags 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3d155346769a6855b86399e9bc3814ab343cd3d62c7e985113d46a0ec3c281fd" "checksum byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a7c3dd8985a7111efc5c80b44e23ecdd8c007de8ade3b96595387e812b957cf5" "checksum compiler_builtins 0.1.19 (registry+https://github.com/rust-lang/crates.io-index)" = "4e32b9fc11fdb3aefbd0a4761a8d3a2b7419608b759fa14a26525df4ea5deaba" +"checksum linked_list_allocator 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)" = "47314ec1d29aa869ee7cb5a5be57be9b1055c56567d59c3fb6689926743e0bea" "checksum managed 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "fdcec5e97041c7f0f1c5b7d93f12e57293c831c646f4cc7a5db59460c7ea8de6" "checksum r0 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e2a38df5b15c8d5c7e8654189744d8e396bddc18ad48041a500ce52d6948941f" "checksum smoltcp 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fef582369edb298c6c41319a544ca9c4e83622f226055ccfcb35974fbb55ed34" +"checksum spin 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d" "checksum vcell 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "45c297f0afb6928cd08ab1ff9d95e99392595ea25ae1b5ecf822ff8764e57a0d" "checksum volatile-register 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "0d67cb4616d99b940db1d6bd28844ff97108b498a6ca850e5b6191a532063286" diff --git a/Cargo.toml b/Cargo.toml index 5bae93ba..c5b98f92 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,3 +23,4 @@ volatile-register = "0.2" bit_field = "0.10" compiler_builtins = { version = "~0.1", default-features = false, features = ["mem", "no-lang-items"]} smoltcp = { version = "0.5", default-features = false, features = ["proto-ipv4", "socket-tcp"] } +linked_list_allocator = "0.6" diff --git a/src/main.rs b/src/main.rs index c98314e8..0ca695f7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,7 @@ #![feature(naked_functions)] #![feature(compiler_builtins_lib)] #![feature(never_type)] +#![feature(alloc_error_handler)] // TODO: disallow unused/dead_code when code moves into a lib crate #![allow(dead_code)] @@ -15,12 +16,12 @@ use smoltcp::wire::{EthernetAddress, IpAddress, IpCidr}; use smoltcp::iface::{NeighborCache, EthernetInterfaceBuilder, EthernetInterface}; use smoltcp::time::Instant; use smoltcp::socket::SocketSet; +use linked_list_allocator::LockedHeap; mod regs; mod cortex_a9; mod stdio; mod zynq; -mod ram; use crate::regs::{RegisterR, RegisterW}; use crate::cortex_a9::{asm, regs::*, mmu}; @@ -83,6 +84,9 @@ fn l1_cache_init() { dciall(); } +#[global_allocator] +static ALLOCATOR: LockedHeap = LockedHeap::empty(); + const HWADDR: [u8; 6] = [0, 0x23, 0xde, 0xea, 0xbe, 0xef]; fn main() { @@ -92,6 +96,10 @@ fn main() { println!("DDR: {:?}", ddr.status()); ddr.memtest(); + unsafe { + ALLOCATOR.lock().init(ddr.ptr::() as usize, ddr.size()); + } + let eth = zynq::eth::Eth::default(HWADDR.clone()); println!("Eth on"); @@ -176,6 +184,11 @@ fn panic(info: &core::panic::PanicInfo) -> ! { loop {} } +#[alloc_error_handler] +fn alloc_error(_: core::alloc::Layout) -> ! { + panic!("alloc_error") +} + #[no_mangle] pub unsafe extern "C" fn PrefetchAbort() { println!("PrefetchAbort"); diff --git a/src/ram.rs b/src/ram.rs deleted file mode 100644 index 767392b1..00000000 --- a/src/ram.rs +++ /dev/null @@ -1,75 +0,0 @@ -use core::mem::{align_of, size_of}; - -pub trait RAM { - #[inline(always)] - fn addr(&self) -> usize; - #[inline(always)] - fn size(&self) -> usize; - - #[inline(always)] - fn into_memory_region(&self) -> MemoryRegion { - MemoryRegion { - addr: self.addr(), - size: self.size(), - } - } - -} - -pub struct MemoryRegion { - addr: usize, - size: usize, -} - -impl MemoryRegion { - #[inline(always)] - fn split(&mut self, offset: usize) -> MemoryRegion { - if offset > self.size { - panic!("StaticAllocator: unable to split {}/{} bytes", offset, self.size); - } - - let result = MemoryRegion { - addr: self.addr, - size: offset, - }; - self.addr += offset; - self.size -= offset; - result - } - - unsafe fn clear(self) -> *mut T { - let result = self.addr as *mut T; - for b in core::slice::from_raw_parts_mut::(result as *mut u8, self.size) { - *b = 0; - } - result - } - - pub fn align(&mut self, alignment: usize) { - let mask = alignment - 1; - let addr = (self.addr | mask) + 1; - self.size -= addr - self.addr; - self.addr = addr; - } -} - -pub struct StaticAllocator { - free: MemoryRegion, -} - -impl StaticAllocator { - pub fn new(r: R) -> Self { - StaticAllocator { - free: r.into_memory_region(), - } - } - - pub fn alloc<'a: 'b, 'b, T: Sized>(&'a mut self) -> &'b mut T { - self.free.align(align_of::()); - - let size = size_of::(); - let region = self.free.split(size); - - unsafe { &mut *region.clear() } - } -}