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