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; |
Andrew Walbran | 153aad9 | 2022-06-28 15:51:13 +0000 | [diff] [blame] | 22 | mod layout; |
Andrew Walbran | eef9820 | 2022-04-27 16:23:06 +0000 | [diff] [blame] | 23 | |
Andrew Walbran | f7b6dc8 | 2022-04-20 16:24:30 +0000 | [diff] [blame] | 24 | extern crate alloc; |
| 25 | |
Andrew Walbran | 153aad9 | 2022-06-28 15:51:13 +0000 | [diff] [blame] | 26 | use crate::layout::{ |
| 27 | dtb_range, print_addresses, rodata_range, text_range, writable_region, DEVICE_REGION, |
Andrew Walbran | 1356454 | 2022-04-20 16:29:45 +0000 | [diff] [blame] | 28 | }; |
Andrew Walbran | 153aad9 | 2022-06-28 15:51:13 +0000 | [diff] [blame] | 29 | use aarch64_paging::{idmap::IdMap, paging::Attributes}; |
Andrew Walbran | f7b6dc8 | 2022-04-20 16:24:30 +0000 | [diff] [blame] | 30 | use alloc::{vec, vec::Vec}; |
| 31 | use buddy_system_allocator::LockedHeap; |
Andrew Walbran | eef9820 | 2022-04-27 16:23:06 +0000 | [diff] [blame] | 32 | use vmbase::{main, println}; |
| 33 | |
Andrew Walbran | 5d4e1c9 | 2022-04-12 14:30:54 +0000 | [diff] [blame] | 34 | static INITIALISED_DATA: [u32; 4] = [1, 2, 3, 4]; |
| 35 | static mut ZEROED_DATA: [u32; 10] = [0; 10]; |
| 36 | static mut MUTABLE_DATA: [u32; 4] = [1, 2, 3, 4]; |
| 37 | |
Andrew Walbran | 1356454 | 2022-04-20 16:29:45 +0000 | [diff] [blame] | 38 | const ASID: usize = 1; |
| 39 | const ROOT_LEVEL: usize = 1; |
| 40 | |
Andrew Walbran | f7b6dc8 | 2022-04-20 16:24:30 +0000 | [diff] [blame] | 41 | #[global_allocator] |
| 42 | static HEAP_ALLOCATOR: LockedHeap<32> = LockedHeap::<32>::new(); |
| 43 | |
| 44 | static mut HEAP: [u8; 65536] = [0; 65536]; |
| 45 | |
Andrew Walbran | eef9820 | 2022-04-27 16:23:06 +0000 | [diff] [blame] | 46 | main!(main); |
| 47 | |
| 48 | /// Entry point for VM bootloader. |
Andrew Walbran | e03395a | 2022-04-29 15:15:49 +0000 | [diff] [blame] | 49 | pub fn main(arg0: u64, arg1: u64, arg2: u64, arg3: u64) { |
Andrew Walbran | eef9820 | 2022-04-27 16:23:06 +0000 | [diff] [blame] | 50 | println!("Hello world"); |
Andrew Walbran | f17c651 | 2022-06-28 15:52:23 +0000 | [diff] [blame] | 51 | println!("x0={:#018x}, x1={:#018x}, x2={:#018x}, x3={:#018x}", arg0, arg1, arg2, arg3); |
Andrew Walbran | 6261cf4 | 2022-04-12 13:26:52 +0000 | [diff] [blame] | 52 | print_addresses(); |
Andrew Walbran | 153aad9 | 2022-06-28 15:51:13 +0000 | [diff] [blame] | 53 | assert_eq!(arg0, dtb_range().start.0 as u64); |
Andrew Walbran | 5d4e1c9 | 2022-04-12 14:30:54 +0000 | [diff] [blame] | 54 | check_data(); |
Andrew Walbran | f7b6dc8 | 2022-04-20 16:24:30 +0000 | [diff] [blame] | 55 | |
| 56 | unsafe { |
| 57 | HEAP_ALLOCATOR.lock().init(&mut HEAP as *mut u8 as usize, HEAP.len()); |
| 58 | } |
| 59 | |
| 60 | check_alloc(); |
Andrew Walbran | 1356454 | 2022-04-20 16:29:45 +0000 | [diff] [blame] | 61 | |
| 62 | let mut idmap = IdMap::new(ASID, ROOT_LEVEL); |
| 63 | idmap.map_range(&DEVICE_REGION, Attributes::DEVICE_NGNRE | Attributes::EXECUTE_NEVER).unwrap(); |
| 64 | idmap |
| 65 | .map_range( |
Andrew Walbran | 153aad9 | 2022-06-28 15:51:13 +0000 | [diff] [blame] | 66 | &text_range().into(), |
Andrew Walbran | 1356454 | 2022-04-20 16:29:45 +0000 | [diff] [blame] | 67 | Attributes::NORMAL | Attributes::NON_GLOBAL | Attributes::READ_ONLY, |
| 68 | ) |
| 69 | .unwrap(); |
| 70 | idmap |
| 71 | .map_range( |
Andrew Walbran | 153aad9 | 2022-06-28 15:51:13 +0000 | [diff] [blame] | 72 | &rodata_range().into(), |
Andrew Walbran | 1356454 | 2022-04-20 16:29:45 +0000 | [diff] [blame] | 73 | Attributes::NORMAL |
| 74 | | Attributes::NON_GLOBAL |
| 75 | | Attributes::READ_ONLY |
| 76 | | Attributes::EXECUTE_NEVER, |
| 77 | ) |
| 78 | .unwrap(); |
| 79 | idmap |
| 80 | .map_range( |
| 81 | &writable_region(), |
| 82 | Attributes::NORMAL | Attributes::NON_GLOBAL | Attributes::EXECUTE_NEVER, |
| 83 | ) |
| 84 | .unwrap(); |
| 85 | |
| 86 | println!("Activating IdMap..."); |
| 87 | println!("{:?}", idmap); |
| 88 | idmap.activate(); |
| 89 | println!("Activated."); |
| 90 | |
| 91 | check_data(); |
| 92 | } |
| 93 | |
Andrew Walbran | 5d4e1c9 | 2022-04-12 14:30:54 +0000 | [diff] [blame] | 94 | fn check_data() { |
| 95 | println!("INITIALISED_DATA: {:#010x}", &INITIALISED_DATA as *const u32 as usize); |
| 96 | unsafe { |
| 97 | println!("ZEROED_DATA: {:#010x}", &ZEROED_DATA as *const u32 as usize); |
| 98 | println!("MUTABLE_DATA: {:#010x}", &MUTABLE_DATA as *const u32 as usize); |
Andrew Walbran | f7b6dc8 | 2022-04-20 16:24:30 +0000 | [diff] [blame] | 99 | println!("HEAP: {:#010x}", &HEAP as *const u8 as usize); |
Andrew Walbran | 5d4e1c9 | 2022-04-12 14:30:54 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | assert_eq!(INITIALISED_DATA[0], 1); |
| 103 | assert_eq!(INITIALISED_DATA[1], 2); |
| 104 | assert_eq!(INITIALISED_DATA[2], 3); |
| 105 | assert_eq!(INITIALISED_DATA[3], 4); |
| 106 | |
| 107 | unsafe { |
| 108 | for element in ZEROED_DATA.iter() { |
| 109 | assert_eq!(*element, 0); |
| 110 | } |
| 111 | ZEROED_DATA[0] = 13; |
| 112 | assert_eq!(ZEROED_DATA[0], 13); |
| 113 | ZEROED_DATA[0] = 0; |
| 114 | assert_eq!(ZEROED_DATA[0], 0); |
| 115 | |
| 116 | assert_eq!(MUTABLE_DATA[0], 1); |
| 117 | assert_eq!(MUTABLE_DATA[1], 2); |
| 118 | assert_eq!(MUTABLE_DATA[2], 3); |
| 119 | assert_eq!(MUTABLE_DATA[3], 4); |
| 120 | MUTABLE_DATA[0] += 41; |
| 121 | assert_eq!(MUTABLE_DATA[0], 42); |
Andrew Walbran | 1356454 | 2022-04-20 16:29:45 +0000 | [diff] [blame] | 122 | MUTABLE_DATA[0] -= 41; |
| 123 | assert_eq!(MUTABLE_DATA[0], 1); |
Andrew Walbran | 5d4e1c9 | 2022-04-12 14:30:54 +0000 | [diff] [blame] | 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 | } |