Skip to content

Commit

Permalink
Checks ID_AA64MMFR0_EL1 for at least 36 bits physical address (#967)
Browse files Browse the repository at this point in the history
  • Loading branch information
ultimaweapon authored Sep 5, 2024
1 parent 2dadceb commit cc1dc7e
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 18 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci-linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ jobs:
run: |
sudo apt-get update
sudo apt-get install -y flatpak flatpak-builder
- name: Update Rust
run: rustup update stable
- name: Add additional Rust targets
run: rustup target add x86_64-unknown-none
- name: Lint Rust sources
Expand Down
10 changes: 9 additions & 1 deletion src/core/src/vmm/aarch64.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// SPDX-License-Identifier: MIT OR Apache-2.0
use super::hv::{Cpu, CpuStates};
use super::hw::RamMap;
use super::MainCpuError;
Expand All @@ -8,7 +9,7 @@ pub fn setup_main_cpu(cpu: &mut impl Cpu, entry: usize, map: RamMap) -> Result<(
.states()
.map_err(|e| MainCpuError::GetCpuStatesFailed(Box::new(e)))?;
let mmfr0 = states
.get_id_aa64_mmfr0()
.get_id_aa64mmfr0()
.map_err(|e| MainCpuError::GetIdAa64mmfr0Failed(Box::new(e)))?;

match map.page_size.get() {
Expand All @@ -20,6 +21,13 @@ pub fn setup_main_cpu(cpu: &mut impl Cpu, entry: usize, map: RamMap) -> Result<(
_ => todo!(),
}

// Check if CPU support at least 36 bits physical address.
let pa_range = mmfr0 & 0xF;

if pa_range == 0 {
return Err(MainCpuError::PhysicalAddressTooSmall);
}

// Set PSTATE so the PE run in AArch64 mode. Not sure why we need M here since the document said
// it is ignore. See https://gist.github.com/imbushuo/51b09e61ecd7b7ac063853ad65cedf34 where
// M = 5 came from.
Expand Down
11 changes: 6 additions & 5 deletions src/core/src/vmm/hv/macos/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type hv_vcpu_t = hv_sys::hv_vcpu_t;
#[allow(non_camel_case_types)]
type hv_vcpu_t = hv_sys::hv_vcpuid_t;

#[cfg(target_arch = "x86_64")]
macro_rules! wrap_return {
($ret:expr) => {
match NonZero::new($ret) {
Expand Down Expand Up @@ -129,7 +130,7 @@ impl<'a> Cpu for HfCpu<'a> {
fn states(&mut self) -> Result<Self::States<'_>, Self::GetStatesErr> {
Ok(HfStates {
cpu: self,
id_aa64_mmfr0: State::None,
id_aa64mmfr0: State::None,
pstate: State::None,
sctlr_el1: State::None,
mair_el1: State::None,
Expand Down Expand Up @@ -190,7 +191,7 @@ impl<'a> Drop for HfCpu<'a> {
pub struct HfStates<'a, 'b> {
cpu: &'a mut HfCpu<'b>,
#[cfg(target_arch = "aarch64")]
id_aa64_mmfr0: State<u64>,
id_aa64mmfr0: State<u64>,
#[cfg(target_arch = "x86_64")]
rsp: State<usize>,
#[cfg(target_arch = "x86_64")]
Expand Down Expand Up @@ -237,16 +238,16 @@ impl<'a, 'b> CpuStates for HfStates<'a, 'b> {
type Err = StatesError;

#[cfg(target_arch = "aarch64")]
fn get_id_aa64_mmfr0(&mut self) -> Result<u64, Self::Err> {
fn get_id_aa64mmfr0(&mut self) -> Result<u64, Self::Err> {
use hv_sys::hv_sys_reg_t_HV_SYS_REG_ID_AA64MMFR0_EL1 as HV_SYS_REG_ID_AA64MMFR0_EL1;

let v = match self.id_aa64_mmfr0 {
let v = match self.id_aa64mmfr0 {
State::None => {
let v = self
.cpu
.read_sys(HV_SYS_REG_ID_AA64MMFR0_EL1)
.map_err(StatesError::ReadRegisterFailed)?;
self.id_aa64_mmfr0 = State::Clean(v);
self.id_aa64mmfr0 = State::Clean(v);
v
}
State::Clean(v) | State::Dirty(v) => v,
Expand Down
2 changes: 1 addition & 1 deletion src/core/src/vmm/hv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub trait CpuStates {
type Err: Error + Send + 'static;

#[cfg(target_arch = "aarch64")]
fn get_id_aa64_mmfr0(&mut self) -> Result<u64, Self::Err>;
fn get_id_aa64mmfr0(&mut self) -> Result<u64, Self::Err>;

#[cfg(target_arch = "x86_64")]
fn set_rdi(&mut self, v: usize);
Expand Down
4 changes: 4 additions & 0 deletions src/core/src/vmm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,10 @@ enum MainCpuError {
#[error("vCPU does not support {0:#x} page size")]
PageSizeNotSupported(NonZero<usize>),

#[cfg(target_arch = "aarch64")]
#[error("physical address supported by vCPU too small")]
PhysicalAddressTooSmall,

#[error("couldn't commit vCPU states")]
CommitCpuStatesFailed(#[source] Box<dyn Error + Send>),
}
13 changes: 2 additions & 11 deletions src/obkrnl/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use crate::config::set_boot_env;
use crate::context::Context;
use crate::malloc::KernelHeap;
use crate::proc::Thread;
use alloc::string::String;
use alloc::sync::Arc;
use core::arch::asm;
use core::mem::zeroed;
Expand Down Expand Up @@ -82,16 +81,8 @@ fn panic(i: &PanicInfo) -> ! {
None => ("unknown", 0),
};

// Get message.
let msg = if let Some(&v) = i.payload().downcast_ref::<&str>() {
v
} else if let Some(v) = i.payload().downcast_ref::<String>() {
v
} else {
"unknown panic payload"
};

crate::console::error(file, line, msg);
// Print the message.
crate::console::error(file, line, i.message());
crate::panic::panic();
}

Expand Down

0 comments on commit cc1dc7e

Please sign in to comment.