Skip to content

Commit

Permalink
Sets SP_EL1 for Mac M1 (#928)
Browse files Browse the repository at this point in the history
  • Loading branch information
ultimaweapon authored Aug 14, 2024
1 parent bee12a8 commit 9ac31cd
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/core/src/vmm/hv/linux/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ impl<'a> CpuStates for KvmStates<'a> {
}

#[cfg(target_arch = "aarch64")]
fn set_sp(&mut self, v: usize) {
fn set_sp_el1(&mut self, v: usize) {
todo!()
}

Expand Down
40 changes: 36 additions & 4 deletions src/core/src/vmm/hv/macos/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,10 @@ impl<'a> Cpu for HfCpu<'a> {

#[cfg(target_arch = "aarch64")]
fn states(&mut self) -> Result<Self::States<'_>, Self::GetStatesErr> {
todo!()
Ok(HfStates {
cpu: self,
sp_el1: State::None,
})
}

#[cfg(target_arch = "x86_64")]
Expand Down Expand Up @@ -176,17 +179,30 @@ impl<'a> Drop for HfCpu<'a> {
/// Implementation of [`Cpu::States`] for Hypervisor Framework.
pub struct HfStates<'a, 'b> {
cpu: &'a mut HfCpu<'b>,
#[cfg(target_arch = "x86_64")]
rsp: State<usize>,
#[cfg(target_arch = "x86_64")]
rip: State<usize>,
#[cfg(target_arch = "x86_64")]
cr0: State<usize>,
#[cfg(target_arch = "x86_64")]
cr3: State<usize>,
#[cfg(target_arch = "x86_64")]
cr4: State<usize>,
#[cfg(target_arch = "x86_64")]
cs: State<u64>,
#[cfg(target_arch = "x86_64")]
ds: State<usize>,
#[cfg(target_arch = "x86_64")]
es: State<usize>,
#[cfg(target_arch = "x86_64")]
fs: State<usize>,
#[cfg(target_arch = "x86_64")]
gs: State<usize>,
#[cfg(target_arch = "x86_64")]
ss: State<usize>,
#[cfg(target_arch = "aarch64")]
sp_el1: State<usize>,
}

impl<'a, 'b> CpuStates for HfStates<'a, 'b> {
Expand Down Expand Up @@ -260,8 +276,8 @@ impl<'a, 'b> CpuStates for HfStates<'a, 'b> {
}

#[cfg(target_arch = "aarch64")]
fn set_sp(&mut self, v: usize) {
todo!()
fn set_sp_el1(&mut self, v: usize) {
self.sp_el1 = State::Dirty(v);
}

#[cfg(target_arch = "aarch64")]
Expand Down Expand Up @@ -306,7 +322,19 @@ impl<'a, 'b> CpuStates for HfStates<'a, 'b> {

#[cfg(target_arch = "aarch64")]
fn commit(self) -> Result<(), Self::Err> {
todo!()
use hv_sys::{hv_sys_reg_t_HV_SYS_REG_SP_EL1, hv_vcpu_set_sys_reg};

let cpu = self.cpu.instance;

if let State::Dirty(v) = self.sp_el1 {
NonZero::new(unsafe {
hv_vcpu_set_sys_reg(cpu, hv_sys_reg_t_HV_SYS_REG_SP_EL1, v.try_into().unwrap())
})
.map(|v| Err::<(), _>(StatesError::SetSpEl1Failed(v)))
.transpose()?;
}

Ok(())
}
}

Expand Down Expand Up @@ -372,4 +400,8 @@ pub enum StatesError {
#[cfg(target_arch = "x86_64")]
#[error("couldn't set CR4")]
SetCr4Failed(NonZero<hv_sys::hv_return_t>),

#[cfg(target_arch = "aarch64")]
#[error("couldn't set SP_EL1")]
SetSpEl1Failed(NonZero<hv_sys::hv_return_t>),
}
4 changes: 3 additions & 1 deletion src/core/src/vmm/hv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ pub trait Hypervisor: Send + Sync {
}

/// Represents a core of the PS4 CPU.
///
/// On AArch64 this represent one Processing Element (PE).
pub trait Cpu {
type States<'a>: CpuStates + 'a
where
Expand Down Expand Up @@ -86,7 +88,7 @@ pub trait CpuStates {
fn set_ss(&mut self, p: bool);

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

#[cfg(target_arch = "aarch64")]
fn set_pc(&mut self, v: usize);
Expand Down
2 changes: 1 addition & 1 deletion src/core/src/vmm/hv/windows/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ impl<'a, 'b> CpuStates for WhpStates<'a, 'b> {
}

#[cfg(target_arch = "aarch64")]
fn set_sp(&mut self, v: usize) {
fn set_sp_el1(&mut self, v: usize) {
todo!()
}

Expand Down
11 changes: 10 additions & 1 deletion src/core/src/vmm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,16 @@ fn setup_main_cpu(cpu: &mut impl Cpu, entry: usize, map: RamMap) -> Result<(), M

#[cfg(target_arch = "aarch64")]
fn setup_main_cpu(cpu: &mut impl Cpu, entry: usize, map: RamMap) -> Result<(), MainCpuError> {
todo!()
let mut states = cpu
.states()
.map_err(|e| MainCpuError::GetCpuStatesFailed(Box::new(e)))?;

// Set stack pointer to the kernel.
states.set_sp_el1(map.stack_vaddr + map.stack_len); // Top-down.

states
.commit()
.map_err(|e| MainCpuError::CommitCpuStatesFailed(Box::new(e)))
}

#[cfg(target_arch = "x86_64")]
Expand Down

0 comments on commit 9ac31cd

Please sign in to comment.