blob: a769abb37c2ca974eafc0d723b39b9f4de40a149 [file] [log] [blame]
David Brazdil66fc1202022-07-04 21:48:45 +01001// 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//! Project Rialto main source file.
16
17#![no_main]
18#![no_std]
19#![feature(default_alloc_error_handler)]
20
21mod exceptions;
22
23extern crate alloc;
24extern crate log;
25
26use aarch64_paging::{
27 idmap::IdMap,
28 paging::{Attributes, MemoryRegion},
29 AddressRangeError,
30};
31use buddy_system_allocator::LockedHeap;
32use log::{debug, info};
33use vmbase::main;
34
35const SZ_1K: usize = 1024;
36const SZ_64K: usize = 64 * SZ_1K;
37const SZ_1M: usize = 1024 * SZ_1K;
38const SZ_1G: usize = 1024 * SZ_1M;
39
40// Root level is given by the value of TCR_EL1.TG0 and TCR_EL1.T0SZ, set in
41// entry.S. For 4KB granule and 39-bit VA, the root level is 1.
42const PT_ROOT_LEVEL: usize = 1;
43const PT_ASID: usize = 1;
44
45#[global_allocator]
46static HEAP_ALLOCATOR: LockedHeap<32> = LockedHeap::<32>::new();
47
48static mut HEAP: [u8; SZ_64K] = [0; SZ_64K];
49
50unsafe fn kimg_ptr(sym: &u8) -> *const u8 {
51 sym as *const u8
52}
53
54unsafe fn kimg_addr(sym: &u8) -> usize {
55 kimg_ptr(sym) as usize
56}
57
58unsafe fn kimg_region(begin: &u8, end: &u8) -> MemoryRegion {
59 MemoryRegion::new(kimg_addr(begin), kimg_addr(end))
60}
61
62fn init_heap() {
63 // SAFETY: Allocator set to otherwise unused, static memory.
64 unsafe {
65 HEAP_ALLOCATOR.lock().init(&mut HEAP as *mut u8 as usize, HEAP.len());
66 }
67 info!("Initialized heap.");
68}
69
70fn init_kernel_pgt(pgt: &mut IdMap) -> Result<(), AddressRangeError> {
71 // The first 1 GiB of address space is used by crosvm for MMIO.
72 let reg_dev = MemoryRegion::new(0, SZ_1G);
73 // SAFETY: Taking addresses of kernel image sections to set up page table
74 // mappings. Not taking ownerhip of the memory.
75 let reg_text = unsafe { kimg_region(&text_begin, &text_end) };
76 let reg_rodata = unsafe { kimg_region(&rodata_begin, &rodata_end) };
77 let reg_data = unsafe { kimg_region(&data_begin, &boot_stack_end) };
78
79 debug!("Preparing kernel page tables.");
80 debug!(" dev: {}-{}", reg_dev.start(), reg_dev.end());
81 debug!(" text: {}-{}", reg_text.start(), reg_text.end());
82 debug!(" rodata: {}-{}", reg_rodata.start(), reg_rodata.end());
83 debug!(" data: {}-{}", reg_data.start(), reg_data.end());
84
85 let prot_dev = Attributes::DEVICE_NGNRE | Attributes::EXECUTE_NEVER;
86 let prot_rx = Attributes::NORMAL | Attributes::NON_GLOBAL | Attributes::READ_ONLY;
87 let prot_ro = Attributes::NORMAL
88 | Attributes::NON_GLOBAL
89 | Attributes::READ_ONLY
90 | Attributes::EXECUTE_NEVER;
91 let prot_rw = Attributes::NORMAL | Attributes::NON_GLOBAL | Attributes::EXECUTE_NEVER;
92
93 pgt.map_range(&reg_dev, prot_dev)?;
94 pgt.map_range(&reg_text, prot_rx)?;
95 pgt.map_range(&reg_rodata, prot_ro)?;
96 pgt.map_range(&reg_data, prot_rw)?;
97
98 info!("Finished preparing kernel page table.");
99 Ok(())
100}
101
102fn activate_kernel_pgt(pgt: &mut IdMap) {
103 pgt.activate();
104 info!("Activated kernel page table.");
105}
106
107/// Entry point for Rialto.
108pub fn main(_a0: u64, _a1: u64, _a2: u64, _a3: u64) {
109 vmbase::logger::init(log::LevelFilter::Debug).unwrap();
110
111 info!("Welcome to Rialto!");
112 init_heap();
113
114 let mut pgt = IdMap::new(PT_ASID, PT_ROOT_LEVEL);
115 init_kernel_pgt(&mut pgt).unwrap();
116 activate_kernel_pgt(&mut pgt);
117}
118
119extern "C" {
120 static text_begin: u8;
121 static text_end: u8;
122 static rodata_begin: u8;
123 static rodata_end: u8;
124 static data_begin: u8;
125 static boot_stack_end: u8;
126}
127
128main!(main);