blob: e3b9399d5cbd8bd16cb2c6eeb7c3d11221457af9 [file] [log] [blame]
Andrew Walbraneef98202022-04-27 16:23:06 +00001// 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 Walbranf7b6dc82022-04-20 16:24:30 +000019#![feature(default_alloc_error_handler)]
Andrew Walbraneef98202022-04-27 16:23:06 +000020
21mod exceptions;
22
Andrew Walbranf7b6dc82022-04-20 16:24:30 +000023extern crate alloc;
24
25use alloc::{vec, vec::Vec};
26use buddy_system_allocator::LockedHeap;
Andrew Walbraneef98202022-04-27 16:23:06 +000027use vmbase::{main, println};
28
Andrew Walbran5d4e1c92022-04-12 14:30:54 +000029static INITIALISED_DATA: [u32; 4] = [1, 2, 3, 4];
30static mut ZEROED_DATA: [u32; 10] = [0; 10];
31static mut MUTABLE_DATA: [u32; 4] = [1, 2, 3, 4];
32
Andrew Walbranf7b6dc82022-04-20 16:24:30 +000033#[global_allocator]
34static HEAP_ALLOCATOR: LockedHeap<32> = LockedHeap::<32>::new();
35
36static mut HEAP: [u8; 65536] = [0; 65536];
37
Andrew Walbraneef98202022-04-27 16:23:06 +000038main!(main);
39
40/// Entry point for VM bootloader.
41pub fn main() {
42 println!("Hello world");
Andrew Walbran6261cf42022-04-12 13:26:52 +000043 print_addresses();
Andrew Walbran5d4e1c92022-04-12 14:30:54 +000044 check_data();
Andrew Walbranf7b6dc82022-04-20 16:24:30 +000045
46 unsafe {
47 HEAP_ALLOCATOR.lock().init(&mut HEAP as *mut u8 as usize, HEAP.len());
48 }
49
50 check_alloc();
Andrew Walbran6261cf42022-04-12 13:26:52 +000051}
52
53fn print_addresses() {
54 unsafe {
55 println!(
56 "dtb: {:#010x}-{:#010x} ({} bytes)",
57 &dtb_begin as *const u8 as usize,
58 &dtb_end as *const u8 as usize,
59 &dtb_end as *const u8 as usize - &dtb_begin as *const u8 as usize,
60 );
61 println!(
62 "text: {:#010x}-{:#010x} ({} bytes)",
63 &text_begin as *const u8 as usize,
64 &text_end as *const u8 as usize,
65 &text_end as *const u8 as usize - &text_begin as *const u8 as usize,
66 );
67 println!(
68 "rodata: {:#010x}-{:#010x} ({} bytes)",
69 &rodata_begin as *const u8 as usize,
70 &rodata_end as *const u8 as usize,
71 &rodata_end as *const u8 as usize - &rodata_begin as *const u8 as usize,
72 );
73 println!(
74 "data: {:#010x}-{:#010x} ({} bytes, loaded at {:#010x})",
75 &data_begin as *const u8 as usize,
76 &data_end as *const u8 as usize,
77 &data_end as *const u8 as usize - &data_begin as *const u8 as usize,
78 &data_lma as *const u8 as usize,
79 );
80 println!(
81 "bss: {:#010x}-{:#010x} ({} bytes)",
82 &bss_begin as *const u8 as usize,
83 &bss_end as *const u8 as usize,
84 &bss_end as *const u8 as usize - &bss_begin as *const u8 as usize,
85 );
86 println!(
87 "boot_stack: {:#010x}-{:#010x} ({} bytes)",
88 &boot_stack_begin as *const u8 as usize,
89 &boot_stack_end as *const u8 as usize,
90 &boot_stack_end as *const u8 as usize - &boot_stack_begin as *const u8 as usize,
91 );
92 }
93}
94
Andrew Walbran5d4e1c92022-04-12 14:30:54 +000095fn check_data() {
96 println!("INITIALISED_DATA: {:#010x}", &INITIALISED_DATA as *const u32 as usize);
97 unsafe {
98 println!("ZEROED_DATA: {:#010x}", &ZEROED_DATA as *const u32 as usize);
99 println!("MUTABLE_DATA: {:#010x}", &MUTABLE_DATA as *const u32 as usize);
Andrew Walbranf7b6dc82022-04-20 16:24:30 +0000100 println!("HEAP: {:#010x}", &HEAP as *const u8 as usize);
Andrew Walbran5d4e1c92022-04-12 14:30:54 +0000101 }
102
103 assert_eq!(INITIALISED_DATA[0], 1);
104 assert_eq!(INITIALISED_DATA[1], 2);
105 assert_eq!(INITIALISED_DATA[2], 3);
106 assert_eq!(INITIALISED_DATA[3], 4);
107
108 unsafe {
109 for element in ZEROED_DATA.iter() {
110 assert_eq!(*element, 0);
111 }
112 ZEROED_DATA[0] = 13;
113 assert_eq!(ZEROED_DATA[0], 13);
114 ZEROED_DATA[0] = 0;
115 assert_eq!(ZEROED_DATA[0], 0);
116
117 assert_eq!(MUTABLE_DATA[0], 1);
118 assert_eq!(MUTABLE_DATA[1], 2);
119 assert_eq!(MUTABLE_DATA[2], 3);
120 assert_eq!(MUTABLE_DATA[3], 4);
121 MUTABLE_DATA[0] += 41;
122 assert_eq!(MUTABLE_DATA[0], 42);
123 }
124 println!("Data looks good");
125}
126
Andrew Walbranf7b6dc82022-04-20 16:24:30 +0000127fn check_alloc() {
128 println!("Allocating a Vec...");
129 let mut vector: Vec<u32> = vec![1, 2, 3, 4];
130 assert_eq!(vector[0], 1);
131 assert_eq!(vector[1], 2);
132 assert_eq!(vector[2], 3);
133 assert_eq!(vector[3], 4);
134 vector[2] = 42;
135 assert_eq!(vector[2], 42);
136 println!("Vec seems to work.");
137}
138
Andrew Walbran6261cf42022-04-12 13:26:52 +0000139extern "C" {
140 static dtb_begin: u8;
141 static dtb_end: u8;
142 static text_begin: u8;
143 static text_end: u8;
144 static rodata_begin: u8;
145 static rodata_end: u8;
146 static data_begin: u8;
147 static data_end: u8;
148 static data_lma: u8;
149 static bss_begin: u8;
150 static bss_end: u8;
151 static boot_stack_begin: u8;
152 static boot_stack_end: u8;
Andrew Walbraneef98202022-04-27 16:23:06 +0000153}