blob: 3b1786c4c9bc7776514ff796a38983404d6ed2ab [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.
Andrew Walbrane03395a2022-04-29 15:15:49 +000041pub fn main(arg0: u64, arg1: u64, arg2: u64, arg3: u64) {
Andrew Walbraneef98202022-04-27 16:23:06 +000042 println!("Hello world");
Andrew Walbrane03395a2022-04-29 15:15:49 +000043 println!("x0={:#010x}, x1={:#010x}, x2={:#010x}, x3={:#010x}", arg0, arg1, arg2, arg3);
Andrew Walbran6261cf42022-04-12 13:26:52 +000044 print_addresses();
Andrew Walbran5d4e1c92022-04-12 14:30:54 +000045 check_data();
Andrew Walbranf7b6dc82022-04-20 16:24:30 +000046
47 unsafe {
48 HEAP_ALLOCATOR.lock().init(&mut HEAP as *mut u8 as usize, HEAP.len());
49 }
50
51 check_alloc();
Andrew Walbran6261cf42022-04-12 13:26:52 +000052}
53
54fn 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 Walbran5d4e1c92022-04-12 14:30:54 +000096fn 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 Walbranf7b6dc82022-04-20 16:24:30 +0000101 println!("HEAP: {:#010x}", &HEAP as *const u8 as usize);
Andrew Walbran5d4e1c92022-04-12 14:30:54 +0000102 }
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 Walbranf7b6dc82022-04-20 16:24:30 +0000128fn 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 Walbran6261cf42022-04-12 13:26:52 +0000140extern "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 Walbraneef98202022-04-27 16:23:06 +0000154}