blob: 3c91f978373e79e7d71a2bc70a9303c659ab3add [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;
David Brazdilb41aa8f2022-07-05 12:41:00 +010032use log::{info, LevelFilter};
33use vmbase::{logger, main, println};
Andrew Walbraneef98202022-04-27 16:23:06 +000034
Andrew Walbran5d4e1c92022-04-12 14:30:54 +000035static INITIALISED_DATA: [u32; 4] = [1, 2, 3, 4];
36static mut ZEROED_DATA: [u32; 10] = [0; 10];
37static mut MUTABLE_DATA: [u32; 4] = [1, 2, 3, 4];
38
Andrew Walbran13564542022-04-20 16:29:45 +000039const ASID: usize = 1;
40const ROOT_LEVEL: usize = 1;
41
Andrew Walbranf7b6dc82022-04-20 16:24:30 +000042#[global_allocator]
43static HEAP_ALLOCATOR: LockedHeap<32> = LockedHeap::<32>::new();
44
45static mut HEAP: [u8; 65536] = [0; 65536];
46
Andrew Walbraneef98202022-04-27 16:23:06 +000047main!(main);
48
49/// Entry point for VM bootloader.
Andrew Walbrane03395a2022-04-29 15:15:49 +000050pub fn main(arg0: u64, arg1: u64, arg2: u64, arg3: u64) {
David Brazdilb41aa8f2022-07-05 12:41:00 +010051 logger::init(LevelFilter::Debug).unwrap();
52
Andrew Walbraneef98202022-04-27 16:23:06 +000053 println!("Hello world");
David Brazdilb41aa8f2022-07-05 12:41:00 +010054 info!("x0={:#018x}, x1={:#018x}, x2={:#018x}, x3={:#018x}", arg0, arg1, arg2, arg3);
Andrew Walbran6261cf42022-04-12 13:26:52 +000055 print_addresses();
Andrew Walbran153aad92022-06-28 15:51:13 +000056 assert_eq!(arg0, dtb_range().start.0 as u64);
Andrew Walbran5d4e1c92022-04-12 14:30:54 +000057 check_data();
Andrew Walbranf7b6dc82022-04-20 16:24:30 +000058
59 unsafe {
60 HEAP_ALLOCATOR.lock().init(&mut HEAP as *mut u8 as usize, HEAP.len());
61 }
62
63 check_alloc();
Andrew Walbran13564542022-04-20 16:29:45 +000064
65 let mut idmap = IdMap::new(ASID, ROOT_LEVEL);
66 idmap.map_range(&DEVICE_REGION, Attributes::DEVICE_NGNRE | Attributes::EXECUTE_NEVER).unwrap();
67 idmap
68 .map_range(
Andrew Walbran153aad92022-06-28 15:51:13 +000069 &text_range().into(),
Andrew Walbran13564542022-04-20 16:29:45 +000070 Attributes::NORMAL | Attributes::NON_GLOBAL | Attributes::READ_ONLY,
71 )
72 .unwrap();
73 idmap
74 .map_range(
Andrew Walbran153aad92022-06-28 15:51:13 +000075 &rodata_range().into(),
Andrew Walbran13564542022-04-20 16:29:45 +000076 Attributes::NORMAL
77 | Attributes::NON_GLOBAL
78 | Attributes::READ_ONLY
79 | Attributes::EXECUTE_NEVER,
80 )
81 .unwrap();
82 idmap
83 .map_range(
84 &writable_region(),
85 Attributes::NORMAL | Attributes::NON_GLOBAL | Attributes::EXECUTE_NEVER,
86 )
87 .unwrap();
88
David Brazdilb41aa8f2022-07-05 12:41:00 +010089 info!("Activating IdMap...");
90 info!("{:?}", idmap);
Andrew Walbran13564542022-04-20 16:29:45 +000091 idmap.activate();
David Brazdilb41aa8f2022-07-05 12:41:00 +010092 info!("Activated.");
Andrew Walbran13564542022-04-20 16:29:45 +000093
94 check_data();
95}
96
Andrew Walbran5d4e1c92022-04-12 14:30:54 +000097fn check_data() {
David Brazdilb41aa8f2022-07-05 12:41:00 +010098 info!("INITIALISED_DATA: {:#010x}", &INITIALISED_DATA as *const u32 as usize);
Andrew Walbran5d4e1c92022-04-12 14:30:54 +000099 unsafe {
David Brazdilb41aa8f2022-07-05 12:41:00 +0100100 info!("ZEROED_DATA: {:#010x}", &ZEROED_DATA as *const u32 as usize);
101 info!("MUTABLE_DATA: {:#010x}", &MUTABLE_DATA as *const u32 as usize);
102 info!("HEAP: {:#010x}", &HEAP as *const u8 as usize);
Andrew Walbran5d4e1c92022-04-12 14:30:54 +0000103 }
104
105 assert_eq!(INITIALISED_DATA[0], 1);
106 assert_eq!(INITIALISED_DATA[1], 2);
107 assert_eq!(INITIALISED_DATA[2], 3);
108 assert_eq!(INITIALISED_DATA[3], 4);
109
110 unsafe {
111 for element in ZEROED_DATA.iter() {
112 assert_eq!(*element, 0);
113 }
114 ZEROED_DATA[0] = 13;
115 assert_eq!(ZEROED_DATA[0], 13);
116 ZEROED_DATA[0] = 0;
117 assert_eq!(ZEROED_DATA[0], 0);
118
119 assert_eq!(MUTABLE_DATA[0], 1);
120 assert_eq!(MUTABLE_DATA[1], 2);
121 assert_eq!(MUTABLE_DATA[2], 3);
122 assert_eq!(MUTABLE_DATA[3], 4);
123 MUTABLE_DATA[0] += 41;
124 assert_eq!(MUTABLE_DATA[0], 42);
Andrew Walbran13564542022-04-20 16:29:45 +0000125 MUTABLE_DATA[0] -= 41;
126 assert_eq!(MUTABLE_DATA[0], 1);
Andrew Walbran5d4e1c92022-04-12 14:30:54 +0000127 }
David Brazdilb41aa8f2022-07-05 12:41:00 +0100128 info!("Data looks good");
Andrew Walbran5d4e1c92022-04-12 14:30:54 +0000129}
130
Andrew Walbranf7b6dc82022-04-20 16:24:30 +0000131fn check_alloc() {
David Brazdilb41aa8f2022-07-05 12:41:00 +0100132 info!("Allocating a Vec...");
Andrew Walbranf7b6dc82022-04-20 16:24:30 +0000133 let mut vector: Vec<u32> = vec![1, 2, 3, 4];
134 assert_eq!(vector[0], 1);
135 assert_eq!(vector[1], 2);
136 assert_eq!(vector[2], 3);
137 assert_eq!(vector[3], 4);
138 vector[2] = 42;
139 assert_eq!(vector[2], 42);
David Brazdilb41aa8f2022-07-05 12:41:00 +0100140 info!("Vec seems to work.");
Andrew Walbranf7b6dc82022-04-20 16:24:30 +0000141}