blob: 6b110cfdab695a1d8909095f10706463aec807ee [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;
Andrew Walbran153aad92022-06-28 15:51:13 +000022mod layout;
Andrew Walbraneef98202022-04-27 16:23:06 +000023
Andrew Walbranf7b6dc82022-04-20 16:24:30 +000024extern crate alloc;
25
Andrew Walbran153aad92022-06-28 15:51:13 +000026use crate::layout::{
27 dtb_range, print_addresses, rodata_range, text_range, writable_region, DEVICE_REGION,
Andrew Walbran13564542022-04-20 16:29:45 +000028};
Andrew Walbran153aad92022-06-28 15:51:13 +000029use aarch64_paging::{idmap::IdMap, paging::Attributes};
Andrew Walbranf7b6dc82022-04-20 16:24:30 +000030use alloc::{vec, vec::Vec};
31use buddy_system_allocator::LockedHeap;
Andrew Walbraneef98202022-04-27 16:23:06 +000032use vmbase::{main, println};
33
Andrew Walbran5d4e1c92022-04-12 14:30:54 +000034static INITIALISED_DATA: [u32; 4] = [1, 2, 3, 4];
35static mut ZEROED_DATA: [u32; 10] = [0; 10];
36static mut MUTABLE_DATA: [u32; 4] = [1, 2, 3, 4];
37
Andrew Walbran13564542022-04-20 16:29:45 +000038const ASID: usize = 1;
39const ROOT_LEVEL: usize = 1;
40
Andrew Walbranf7b6dc82022-04-20 16:24:30 +000041#[global_allocator]
42static HEAP_ALLOCATOR: LockedHeap<32> = LockedHeap::<32>::new();
43
44static mut HEAP: [u8; 65536] = [0; 65536];
45
Andrew Walbraneef98202022-04-27 16:23:06 +000046main!(main);
47
48/// Entry point for VM bootloader.
Andrew Walbrane03395a2022-04-29 15:15:49 +000049pub fn main(arg0: u64, arg1: u64, arg2: u64, arg3: u64) {
Andrew Walbraneef98202022-04-27 16:23:06 +000050 println!("Hello world");
Andrew Walbranf17c6512022-06-28 15:52:23 +000051 println!("x0={:#018x}, x1={:#018x}, x2={:#018x}, x3={:#018x}", arg0, arg1, arg2, arg3);
Andrew Walbran6261cf42022-04-12 13:26:52 +000052 print_addresses();
Andrew Walbran153aad92022-06-28 15:51:13 +000053 assert_eq!(arg0, dtb_range().start.0 as u64);
Andrew Walbran5d4e1c92022-04-12 14:30:54 +000054 check_data();
Andrew Walbranf7b6dc82022-04-20 16:24:30 +000055
56 unsafe {
57 HEAP_ALLOCATOR.lock().init(&mut HEAP as *mut u8 as usize, HEAP.len());
58 }
59
60 check_alloc();
Andrew Walbran13564542022-04-20 16:29:45 +000061
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 Walbran153aad92022-06-28 15:51:13 +000066 &text_range().into(),
Andrew Walbran13564542022-04-20 16:29:45 +000067 Attributes::NORMAL | Attributes::NON_GLOBAL | Attributes::READ_ONLY,
68 )
69 .unwrap();
70 idmap
71 .map_range(
Andrew Walbran153aad92022-06-28 15:51:13 +000072 &rodata_range().into(),
Andrew Walbran13564542022-04-20 16:29:45 +000073 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 Walbran5d4e1c92022-04-12 14:30:54 +000094fn 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 Walbranf7b6dc82022-04-20 16:24:30 +000099 println!("HEAP: {:#010x}", &HEAP as *const u8 as usize);
Andrew Walbran5d4e1c92022-04-12 14:30:54 +0000100 }
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 Walbran13564542022-04-20 16:29:45 +0000122 MUTABLE_DATA[0] -= 41;
123 assert_eq!(MUTABLE_DATA[0], 1);
Andrew Walbran5d4e1c92022-04-12 14:30:54 +0000124 }
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}