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] |
| 19 | |
| 20 | mod exceptions; |
| 21 | |
| 22 | use vmbase::{main, println}; |
| 23 | |
Andrew Walbran | 5d4e1c9 | 2022-04-12 14:30:54 +0000 | [diff] [blame^] | 24 | static INITIALISED_DATA: [u32; 4] = [1, 2, 3, 4]; |
| 25 | static mut ZEROED_DATA: [u32; 10] = [0; 10]; |
| 26 | static mut MUTABLE_DATA: [u32; 4] = [1, 2, 3, 4]; |
| 27 | |
Andrew Walbran | eef9820 | 2022-04-27 16:23:06 +0000 | [diff] [blame] | 28 | main!(main); |
| 29 | |
| 30 | /// Entry point for VM bootloader. |
| 31 | pub fn main() { |
| 32 | println!("Hello world"); |
Andrew Walbran | 6261cf4 | 2022-04-12 13:26:52 +0000 | [diff] [blame] | 33 | print_addresses(); |
Andrew Walbran | 5d4e1c9 | 2022-04-12 14:30:54 +0000 | [diff] [blame^] | 34 | check_data(); |
Andrew Walbran | 6261cf4 | 2022-04-12 13:26:52 +0000 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | fn print_addresses() { |
| 38 | unsafe { |
| 39 | println!( |
| 40 | "dtb: {:#010x}-{:#010x} ({} bytes)", |
| 41 | &dtb_begin as *const u8 as usize, |
| 42 | &dtb_end as *const u8 as usize, |
| 43 | &dtb_end as *const u8 as usize - &dtb_begin as *const u8 as usize, |
| 44 | ); |
| 45 | println!( |
| 46 | "text: {:#010x}-{:#010x} ({} bytes)", |
| 47 | &text_begin as *const u8 as usize, |
| 48 | &text_end as *const u8 as usize, |
| 49 | &text_end as *const u8 as usize - &text_begin as *const u8 as usize, |
| 50 | ); |
| 51 | println!( |
| 52 | "rodata: {:#010x}-{:#010x} ({} bytes)", |
| 53 | &rodata_begin as *const u8 as usize, |
| 54 | &rodata_end as *const u8 as usize, |
| 55 | &rodata_end as *const u8 as usize - &rodata_begin as *const u8 as usize, |
| 56 | ); |
| 57 | println!( |
| 58 | "data: {:#010x}-{:#010x} ({} bytes, loaded at {:#010x})", |
| 59 | &data_begin as *const u8 as usize, |
| 60 | &data_end as *const u8 as usize, |
| 61 | &data_end as *const u8 as usize - &data_begin as *const u8 as usize, |
| 62 | &data_lma as *const u8 as usize, |
| 63 | ); |
| 64 | println!( |
| 65 | "bss: {:#010x}-{:#010x} ({} bytes)", |
| 66 | &bss_begin as *const u8 as usize, |
| 67 | &bss_end as *const u8 as usize, |
| 68 | &bss_end as *const u8 as usize - &bss_begin as *const u8 as usize, |
| 69 | ); |
| 70 | println!( |
| 71 | "boot_stack: {:#010x}-{:#010x} ({} bytes)", |
| 72 | &boot_stack_begin as *const u8 as usize, |
| 73 | &boot_stack_end as *const u8 as usize, |
| 74 | &boot_stack_end as *const u8 as usize - &boot_stack_begin as *const u8 as usize, |
| 75 | ); |
| 76 | } |
| 77 | } |
| 78 | |
Andrew Walbran | 5d4e1c9 | 2022-04-12 14:30:54 +0000 | [diff] [blame^] | 79 | fn check_data() { |
| 80 | println!("INITIALISED_DATA: {:#010x}", &INITIALISED_DATA as *const u32 as usize); |
| 81 | unsafe { |
| 82 | println!("ZEROED_DATA: {:#010x}", &ZEROED_DATA as *const u32 as usize); |
| 83 | println!("MUTABLE_DATA: {:#010x}", &MUTABLE_DATA as *const u32 as usize); |
| 84 | } |
| 85 | |
| 86 | assert_eq!(INITIALISED_DATA[0], 1); |
| 87 | assert_eq!(INITIALISED_DATA[1], 2); |
| 88 | assert_eq!(INITIALISED_DATA[2], 3); |
| 89 | assert_eq!(INITIALISED_DATA[3], 4); |
| 90 | |
| 91 | unsafe { |
| 92 | for element in ZEROED_DATA.iter() { |
| 93 | assert_eq!(*element, 0); |
| 94 | } |
| 95 | ZEROED_DATA[0] = 13; |
| 96 | assert_eq!(ZEROED_DATA[0], 13); |
| 97 | ZEROED_DATA[0] = 0; |
| 98 | assert_eq!(ZEROED_DATA[0], 0); |
| 99 | |
| 100 | assert_eq!(MUTABLE_DATA[0], 1); |
| 101 | assert_eq!(MUTABLE_DATA[1], 2); |
| 102 | assert_eq!(MUTABLE_DATA[2], 3); |
| 103 | assert_eq!(MUTABLE_DATA[3], 4); |
| 104 | MUTABLE_DATA[0] += 41; |
| 105 | assert_eq!(MUTABLE_DATA[0], 42); |
| 106 | } |
| 107 | println!("Data looks good"); |
| 108 | } |
| 109 | |
Andrew Walbran | 6261cf4 | 2022-04-12 13:26:52 +0000 | [diff] [blame] | 110 | extern "C" { |
| 111 | static dtb_begin: u8; |
| 112 | static dtb_end: u8; |
| 113 | static text_begin: u8; |
| 114 | static text_end: u8; |
| 115 | static rodata_begin: u8; |
| 116 | static rodata_end: u8; |
| 117 | static data_begin: u8; |
| 118 | static data_end: u8; |
| 119 | static data_lma: u8; |
| 120 | static bss_begin: u8; |
| 121 | static bss_end: u8; |
| 122 | static boot_stack_begin: u8; |
| 123 | static boot_stack_end: u8; |
Andrew Walbran | eef9820 | 2022-04-27 16:23:06 +0000 | [diff] [blame] | 124 | } |