Andrew Walbran | eef9820 | 2022-04-27 16:23:06 +0000 | [diff] [blame] | 1 | // Copyright 2022, The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | //! VM bootloader example. |
| 16 | |
| 17 | #![no_main] |
| 18 | #![no_std] |
Andrew Walbran | f7b6dc8 | 2022-04-20 16:24:30 +0000 | [diff] [blame] | 19 | #![feature(default_alloc_error_handler)] |
Andrew Walbran | eef9820 | 2022-04-27 16:23:06 +0000 | [diff] [blame] | 20 | |
| 21 | mod exceptions; |
| 22 | |
Andrew Walbran | f7b6dc8 | 2022-04-20 16:24:30 +0000 | [diff] [blame] | 23 | extern crate alloc; |
| 24 | |
| 25 | use alloc::{vec, vec::Vec}; |
| 26 | use buddy_system_allocator::LockedHeap; |
Andrew Walbran | eef9820 | 2022-04-27 16:23:06 +0000 | [diff] [blame] | 27 | use vmbase::{main, println}; |
| 28 | |
Andrew Walbran | 5d4e1c9 | 2022-04-12 14:30:54 +0000 | [diff] [blame] | 29 | static INITIALISED_DATA: [u32; 4] = [1, 2, 3, 4]; |
| 30 | static mut ZEROED_DATA: [u32; 10] = [0; 10]; |
| 31 | static mut MUTABLE_DATA: [u32; 4] = [1, 2, 3, 4]; |
| 32 | |
Andrew Walbran | f7b6dc8 | 2022-04-20 16:24:30 +0000 | [diff] [blame] | 33 | #[global_allocator] |
| 34 | static HEAP_ALLOCATOR: LockedHeap<32> = LockedHeap::<32>::new(); |
| 35 | |
| 36 | static mut HEAP: [u8; 65536] = [0; 65536]; |
| 37 | |
Andrew Walbran | eef9820 | 2022-04-27 16:23:06 +0000 | [diff] [blame] | 38 | main!(main); |
| 39 | |
| 40 | /// Entry point for VM bootloader. |
Andrew Walbran | e03395a | 2022-04-29 15:15:49 +0000 | [diff] [blame^] | 41 | pub fn main(arg0: u64, arg1: u64, arg2: u64, arg3: u64) { |
Andrew Walbran | eef9820 | 2022-04-27 16:23:06 +0000 | [diff] [blame] | 42 | println!("Hello world"); |
Andrew Walbran | e03395a | 2022-04-29 15:15:49 +0000 | [diff] [blame^] | 43 | println!("x0={:#010x}, x1={:#010x}, x2={:#010x}, x3={:#010x}", arg0, arg1, arg2, arg3); |
Andrew Walbran | 6261cf4 | 2022-04-12 13:26:52 +0000 | [diff] [blame] | 44 | print_addresses(); |
Andrew Walbran | 5d4e1c9 | 2022-04-12 14:30:54 +0000 | [diff] [blame] | 45 | check_data(); |
Andrew Walbran | f7b6dc8 | 2022-04-20 16:24:30 +0000 | [diff] [blame] | 46 | |
| 47 | unsafe { |
| 48 | HEAP_ALLOCATOR.lock().init(&mut HEAP as *mut u8 as usize, HEAP.len()); |
| 49 | } |
| 50 | |
| 51 | check_alloc(); |
Andrew Walbran | 6261cf4 | 2022-04-12 13:26:52 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | fn print_addresses() { |
| 55 | unsafe { |
| 56 | println!( |
| 57 | "dtb: {:#010x}-{:#010x} ({} bytes)", |
| 58 | &dtb_begin as *const u8 as usize, |
| 59 | &dtb_end as *const u8 as usize, |
| 60 | &dtb_end as *const u8 as usize - &dtb_begin as *const u8 as usize, |
| 61 | ); |
| 62 | println!( |
| 63 | "text: {:#010x}-{:#010x} ({} bytes)", |
| 64 | &text_begin as *const u8 as usize, |
| 65 | &text_end as *const u8 as usize, |
| 66 | &text_end as *const u8 as usize - &text_begin as *const u8 as usize, |
| 67 | ); |
| 68 | println!( |
| 69 | "rodata: {:#010x}-{:#010x} ({} bytes)", |
| 70 | &rodata_begin as *const u8 as usize, |
| 71 | &rodata_end as *const u8 as usize, |
| 72 | &rodata_end as *const u8 as usize - &rodata_begin as *const u8 as usize, |
| 73 | ); |
| 74 | println!( |
| 75 | "data: {:#010x}-{:#010x} ({} bytes, loaded at {:#010x})", |
| 76 | &data_begin as *const u8 as usize, |
| 77 | &data_end as *const u8 as usize, |
| 78 | &data_end as *const u8 as usize - &data_begin as *const u8 as usize, |
| 79 | &data_lma as *const u8 as usize, |
| 80 | ); |
| 81 | println!( |
| 82 | "bss: {:#010x}-{:#010x} ({} bytes)", |
| 83 | &bss_begin as *const u8 as usize, |
| 84 | &bss_end as *const u8 as usize, |
| 85 | &bss_end as *const u8 as usize - &bss_begin as *const u8 as usize, |
| 86 | ); |
| 87 | println!( |
| 88 | "boot_stack: {:#010x}-{:#010x} ({} bytes)", |
| 89 | &boot_stack_begin as *const u8 as usize, |
| 90 | &boot_stack_end as *const u8 as usize, |
| 91 | &boot_stack_end as *const u8 as usize - &boot_stack_begin as *const u8 as usize, |
| 92 | ); |
| 93 | } |
| 94 | } |
| 95 | |
Andrew Walbran | 5d4e1c9 | 2022-04-12 14:30:54 +0000 | [diff] [blame] | 96 | fn check_data() { |
| 97 | println!("INITIALISED_DATA: {:#010x}", &INITIALISED_DATA as *const u32 as usize); |
| 98 | unsafe { |
| 99 | println!("ZEROED_DATA: {:#010x}", &ZEROED_DATA as *const u32 as usize); |
| 100 | println!("MUTABLE_DATA: {:#010x}", &MUTABLE_DATA as *const u32 as usize); |
Andrew Walbran | f7b6dc8 | 2022-04-20 16:24:30 +0000 | [diff] [blame] | 101 | println!("HEAP: {:#010x}", &HEAP as *const u8 as usize); |
Andrew Walbran | 5d4e1c9 | 2022-04-12 14:30:54 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | assert_eq!(INITIALISED_DATA[0], 1); |
| 105 | assert_eq!(INITIALISED_DATA[1], 2); |
| 106 | assert_eq!(INITIALISED_DATA[2], 3); |
| 107 | assert_eq!(INITIALISED_DATA[3], 4); |
| 108 | |
| 109 | unsafe { |
| 110 | for element in ZEROED_DATA.iter() { |
| 111 | assert_eq!(*element, 0); |
| 112 | } |
| 113 | ZEROED_DATA[0] = 13; |
| 114 | assert_eq!(ZEROED_DATA[0], 13); |
| 115 | ZEROED_DATA[0] = 0; |
| 116 | assert_eq!(ZEROED_DATA[0], 0); |
| 117 | |
| 118 | assert_eq!(MUTABLE_DATA[0], 1); |
| 119 | assert_eq!(MUTABLE_DATA[1], 2); |
| 120 | assert_eq!(MUTABLE_DATA[2], 3); |
| 121 | assert_eq!(MUTABLE_DATA[3], 4); |
| 122 | MUTABLE_DATA[0] += 41; |
| 123 | assert_eq!(MUTABLE_DATA[0], 42); |
| 124 | } |
| 125 | println!("Data looks good"); |
| 126 | } |
| 127 | |
Andrew Walbran | f7b6dc8 | 2022-04-20 16:24:30 +0000 | [diff] [blame] | 128 | fn check_alloc() { |
| 129 | println!("Allocating a Vec..."); |
| 130 | let mut vector: Vec<u32> = vec![1, 2, 3, 4]; |
| 131 | assert_eq!(vector[0], 1); |
| 132 | assert_eq!(vector[1], 2); |
| 133 | assert_eq!(vector[2], 3); |
| 134 | assert_eq!(vector[3], 4); |
| 135 | vector[2] = 42; |
| 136 | assert_eq!(vector[2], 42); |
| 137 | println!("Vec seems to work."); |
| 138 | } |
| 139 | |
Andrew Walbran | 6261cf4 | 2022-04-12 13:26:52 +0000 | [diff] [blame] | 140 | extern "C" { |
| 141 | static dtb_begin: u8; |
| 142 | static dtb_end: u8; |
| 143 | static text_begin: u8; |
| 144 | static text_end: u8; |
| 145 | static rodata_begin: u8; |
| 146 | static rodata_end: u8; |
| 147 | static data_begin: u8; |
| 148 | static data_end: u8; |
| 149 | static data_lma: u8; |
| 150 | static bss_begin: u8; |
| 151 | static bss_end: u8; |
| 152 | static boot_stack_begin: u8; |
| 153 | static boot_stack_end: u8; |
Andrew Walbran | eef9820 | 2022-04-27 16:23:06 +0000 | [diff] [blame] | 154 | } |