Browse Source

Add heap allocator to aarch64 example.

Andrew Walbran 1 year ago
parent
commit
52d8243fc2
2 changed files with 16 additions and 1 deletions
  1. 2 1
      examples/aarch64/Cargo.toml
  2. 14 0
      examples/aarch64/src/main.rs

+ 2 - 1
examples/aarch64/Cargo.toml

@@ -5,12 +5,13 @@ authors = ["Andrew Walbran <qwandor@google.com>"]
 edition = "2021"
 edition = "2021"
 
 
 [dependencies]
 [dependencies]
+buddy_system_allocator = "0.9.0"
 fdt = "0.1.5"
 fdt = "0.1.5"
 lazy_static = { version = "1.4.0", features = ["spin_no_std"] }
 lazy_static = { version = "1.4.0", features = ["spin_no_std"] }
 log = "0.4.17"
 log = "0.4.17"
 smccc = "0.1.1"
 smccc = "0.1.1"
 spin = "0.9.8"
 spin = "0.9.8"
-virtio-drivers = { path = "../..", default-features = false }
+virtio-drivers = { path = "../.." }
 
 
 [build-dependencies]
 [build-dependencies]
 cc = "1.0.73"
 cc = "1.0.73"

+ 14 - 0
examples/aarch64/src/main.rs

@@ -13,6 +13,7 @@ mod uart8250;
 #[cfg(platform = "crosvm")]
 #[cfg(platform = "crosvm")]
 use uart8250 as uart;
 use uart8250 as uart;
 
 
+use buddy_system_allocator::LockedHeap;
 use core::{
 use core::{
     mem::size_of,
     mem::size_of,
     panic::PanicInfo,
     panic::PanicInfo,
@@ -47,6 +48,11 @@ pub const UART_BASE_ADDRESS: usize = 0x900_0000;
 #[cfg(platform = "crosvm")]
 #[cfg(platform = "crosvm")]
 pub const UART_BASE_ADDRESS: usize = 0x3f8;
 pub const UART_BASE_ADDRESS: usize = 0x3f8;
 
 
+#[global_allocator]
+static HEAP_ALLOCATOR: LockedHeap<32> = LockedHeap::new();
+
+static mut HEAP: [u8; 65536] = [0; 65536];
+
 #[no_mangle]
 #[no_mangle]
 extern "C" fn main(x0: u64, x1: u64, x2: u64, x3: u64) {
 extern "C" fn main(x0: u64, x1: u64, x2: u64, x3: u64) {
     logger::init(LevelFilter::Debug).unwrap();
     logger::init(LevelFilter::Debug).unwrap();
@@ -56,6 +62,14 @@ extern "C" fn main(x0: u64, x1: u64, x2: u64, x3: u64) {
         x0, x1, x2, x3
         x0, x1, x2, x3
     );
     );
 
 
+    // Safe because `HEAP` is only used here and `entry` is only called once.
+    unsafe {
+        // Give the allocator some memory to allocate.
+        HEAP_ALLOCATOR
+            .lock()
+            .init(HEAP.as_mut_ptr() as usize, HEAP.len());
+    }
+
     info!("Loading FDT from {:#018x}", x0);
     info!("Loading FDT from {:#018x}", x0);
     // Safe because the pointer is a valid pointer to unaliased memory.
     // Safe because the pointer is a valid pointer to unaliased memory.
     let fdt = unsafe { Fdt::from_ptr(x0 as *const u8).unwrap() };
     let fdt = unsafe { Fdt::from_ptr(x0 as *const u8).unwrap() };