Skip to content

Commit

Permalink
Passes kernel config for AArch64 (#990)
Browse files Browse the repository at this point in the history
  • Loading branch information
ultimaweapon authored Sep 20, 2024
1 parent 5990701 commit 19eabaf
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 8 deletions.
7 changes: 3 additions & 4 deletions src/core/src/vmm/aarch64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,10 @@ pub fn setup_main_cpu(
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.
// Set PSTATE.
states.set_pstate(
Pstate::new()
.with_m(0b101)
.with_m(0b0101) // EL1 with SP_EL1 (EL1h).
.with_f(true)
.with_i(true)
.with_a(true)
Expand Down Expand Up @@ -85,6 +83,7 @@ pub fn setup_main_cpu(

// Set entry point, its argument and stack pointer.
states.set_x0(map.env_vaddr);
states.set_x1(map.conf_vaddr);
states.set_sp_el1(map.stack_vaddr + map.stack_len); // Top-down.
states.set_pc(map.kern_vaddr + entry);

Expand Down
27 changes: 25 additions & 2 deletions src/core/src/vmm/hv/aarch64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ pub struct CpuFeats {
}

/// Represents a value of `PSTATE`.
#[bitfield(u32)]
///
/// This has the same structure as `SPSR_EL1` when exception taken from AArch64 state.
#[bitfield(u64)]
pub struct Pstate {
#[bits(4)]
pub m: u8,
Expand All @@ -22,7 +24,28 @@ pub struct Pstate {
pub i: bool,
pub a: bool,
pub d: bool,
#[bits(22)]
#[bits(2)]
pub btype: u8,
pub ssbs: bool,
pub allint: bool,
#[bits(6)]
__: u8,
pub il: bool,
pub ss: bool,
pub pan: bool,
pub uao: bool,
pub dit: bool,
pub tco: bool,
#[bits(2)]
__: u8,
pub v: bool,
pub c: bool,
pub z: bool,
pub n: bool,
pub pm: bool,
pub ppend: bool,
pub exlock: bool,
#[bits(29)]
__: u32,
}

Expand Down
20 changes: 18 additions & 2 deletions src/core/src/vmm/hv/macos/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ impl<'a> Cpu for HfCpu<'a> {
sp_el1: State::None,
pc: State::None,
x0: State::None,
x1: State::None,
})
}

Expand Down Expand Up @@ -229,6 +230,8 @@ pub struct HfStates<'a, 'b> {
pc: State<u64>,
#[cfg(target_arch = "aarch64")]
x0: State<u64>,
#[cfg(target_arch = "aarch64")]
x1: State<u64>,
}

impl<'a, 'b> CpuStates for HfStates<'a, 'b> {
Expand Down Expand Up @@ -308,7 +311,7 @@ impl<'a, 'b> CpuStates for HfStates<'a, 'b> {

#[cfg(target_arch = "aarch64")]
fn set_pstate(&mut self, v: crate::vmm::hv::Pstate) {
self.pstate = State::Dirty(v.into_bits().into());
self.pstate = State::Dirty(v.into_bits());
}

#[cfg(target_arch = "aarch64")]
Expand Down Expand Up @@ -355,6 +358,11 @@ impl<'a, 'b> CpuStates for HfStates<'a, 'b> {
self.x0 = State::Dirty(v.try_into().unwrap());
}

#[cfg(target_arch = "aarch64")]
fn set_x1(&mut self, v: usize) {
self.x1 = State::Dirty(v.try_into().unwrap());
}

#[cfg(target_arch = "x86_64")]
fn commit(self) -> Result<(), Self::Err> {
if let State::Dirty(v) = self.rip {
Expand Down Expand Up @@ -394,7 +402,7 @@ impl<'a, 'b> CpuStates for HfStates<'a, 'b> {
fn commit(self) -> Result<(), Self::Err> {
use hv_sys::{
hv_reg_t_HV_REG_CPSR as HV_REG_CPSR, hv_reg_t_HV_REG_PC as HV_REG_PC,
hv_reg_t_HV_REG_X0 as HV_REG_X0,
hv_reg_t_HV_REG_X0 as HV_REG_X0, hv_reg_t_HV_REG_X1 as HV_REG_X1,
hv_sys_reg_t_HV_SYS_REG_MAIR_EL1 as HV_SYS_REG_MAIR_EL1,
hv_sys_reg_t_HV_SYS_REG_SCTLR_EL1 as HV_SYS_REG_SCTLR_EL1,
hv_sys_reg_t_HV_SYS_REG_SP_EL1 as HV_SYS_REG_SP_EL1,
Expand Down Expand Up @@ -454,6 +462,10 @@ impl<'a, 'b> CpuStates for HfStates<'a, 'b> {
set_reg(HV_REG_X0, v).map_err(StatesError::SetX0Failed)?;
}

if let State::Dirty(v) = self.x1 {
set_reg(HV_REG_X1, v).map_err(StatesError::SetX1Failed)?;
}

Ok(())
}
}
Expand Down Expand Up @@ -537,4 +549,8 @@ pub enum StatesError {
#[cfg(target_arch = "aarch64")]
#[error("couldn't set X0")]
SetX0Failed(NonZero<hv_sys::hv_return_t>),

#[cfg(target_arch = "aarch64")]
#[error("couldn't set X1")]
SetX1Failed(NonZero<hv_sys::hv_return_t>),
}
3 changes: 3 additions & 0 deletions src/core/src/vmm/hv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ pub trait CpuStates {
#[cfg(target_arch = "aarch64")]
fn set_x0(&mut self, v: usize);

#[cfg(target_arch = "aarch64")]
fn set_x1(&mut self, v: usize);

fn commit(self) -> Result<(), Self::Err>;
}

Expand Down

0 comments on commit 19eabaf

Please sign in to comment.