Skip to content

Commit

Permalink
Sets TTBR1_EL1 for Mac M1 (#942)
Browse files Browse the repository at this point in the history
  • Loading branch information
ultimaweapon authored Aug 22, 2024
1 parent 9753c23 commit c909802
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 9 deletions.
33 changes: 28 additions & 5 deletions src/core/src/vmm/hv/macos/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ impl<'a> Cpu for HfCpu<'a> {
Ok(HfStates {
cpu: self,
tcr_el1: State::None,
ttbr1_el1: State::None,
sp_el1: State::None,
})
}
Expand Down Expand Up @@ -206,6 +207,8 @@ pub struct HfStates<'a, 'b> {
#[cfg(target_arch = "aarch64")]
tcr_el1: State<u64>,
#[cfg(target_arch = "aarch64")]
ttbr1_el1: State<u64>,
#[cfg(target_arch = "aarch64")]
sp_el1: State<u64>,
}

Expand Down Expand Up @@ -280,14 +283,24 @@ impl<'a, 'b> CpuStates for HfStates<'a, 'b> {
}

#[cfg(target_arch = "aarch64")]
fn set_tcr_el1(&mut self, t0sz: u8, t1sz: u8) {
fn set_tcr_el1(&mut self, ips: u8, a1: bool, t0sz: u8, t1sz: u8) {
let ips: u64 = ips.into();
let a1: u64 = a1.into();
let t0sz: u64 = t0sz.into();
let t1sz: u64 = t1sz.into();

assert_eq!(ips & 0b11111000, 0);
assert_eq!(t0sz & 0b11000000, 0);
assert_eq!(t1sz & 0b11000000, 0);

self.tcr_el1 = State::Dirty(t1sz << 16 | t0sz);
self.tcr_el1 = State::Dirty(ips << 32 | a1 << 22 | t1sz << 16 | t0sz);
}

#[cfg(target_arch = "aarch64")]
fn set_ttbr1_el1(&mut self, baddr: usize) {
assert_eq!(baddr & 0xFFFF000000000001, 0);

self.ttbr1_el1 = State::Dirty(baddr.try_into().unwrap());
}

#[cfg(target_arch = "aarch64")]
Expand Down Expand Up @@ -338,7 +351,9 @@ impl<'a, 'b> CpuStates for HfStates<'a, 'b> {
#[cfg(target_arch = "aarch64")]
fn commit(self) -> Result<(), Self::Err> {
use hv_sys::{
hv_sys_reg_t_HV_SYS_REG_SP_EL1, hv_sys_reg_t_HV_SYS_REG_TCR_EL1, hv_vcpu_set_sys_reg,
hv_sys_reg_t_HV_SYS_REG_SP_EL1 as HV_SYS_REG_SP_EL1,
hv_sys_reg_t_HV_SYS_REG_TCR_EL1 as HV_SYS_REG_TCR_EL1,
hv_sys_reg_t_HV_SYS_REG_TTBR1_EL1 as HV_SYS_REG_TTBR1_EL1, hv_vcpu_set_sys_reg,
};

// Set system registers.
Expand All @@ -349,11 +364,15 @@ impl<'a, 'b> CpuStates for HfStates<'a, 'b> {
};

if let State::Dirty(v) = self.tcr_el1 {
set_sys(hv_sys_reg_t_HV_SYS_REG_TCR_EL1, v).map_err(StatesError::SetTcrEl1Failed)?;
set_sys(HV_SYS_REG_TCR_EL1, v).map_err(StatesError::SetTcrEl1Failed)?;
}

if let State::Dirty(v) = self.ttbr1_el1 {
set_sys(HV_SYS_REG_TTBR1_EL1, v).map_err(StatesError::SetTtbr1El1Failed)?;
}

if let State::Dirty(v) = self.sp_el1 {
set_sys(hv_sys_reg_t_HV_SYS_REG_SP_EL1, v).map_err(StatesError::SetSpEl1Failed)?;
set_sys(HV_SYS_REG_SP_EL1, v).map_err(StatesError::SetSpEl1Failed)?;
}

Ok(())
Expand Down Expand Up @@ -443,6 +462,10 @@ pub enum StatesError {
#[error("couldn't set TCR_EL1")]
SetTcrEl1Failed(NonZero<hv_sys::hv_return_t>),

#[cfg(target_arch = "aarch64")]
#[error("couldn't set TTBR1_EL1")]
SetTtbr1El1Failed(NonZero<hv_sys::hv_return_t>),

#[cfg(target_arch = "aarch64")]
#[error("couldn't set SP_EL1")]
SetSpEl1Failed(NonZero<hv_sys::hv_return_t>),
Expand Down
10 changes: 8 additions & 2 deletions src/core/src/vmm/hv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,15 @@ pub trait CpuStates {
fn set_ss(&mut self, p: bool);

/// # Panics
/// If `t0sz` or `t1sz` larger than 6 bits.
/// - If `ips` greater than 7.
/// - If `t0sz` or `t1sz` larger than 6 bits.
#[cfg(target_arch = "aarch64")]
fn set_tcr_el1(&mut self, t0sz: u8, t1sz: u8);
fn set_tcr_el1(&mut self, ips: u8, a1: bool, t0sz: u8, t1sz: u8);

/// # Panics
/// If `baddr` has non-zero on bit 0 or 48:64.
#[cfg(target_arch = "aarch64")]
fn set_ttbr1_el1(&mut self, baddr: usize);

#[cfg(target_arch = "aarch64")]
fn set_sp_el1(&mut self, v: usize);
Expand Down
9 changes: 7 additions & 2 deletions src/core/src/vmm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -553,8 +553,13 @@ fn setup_main_cpu(cpu: &mut impl Cpu, entry: usize, map: RamMap) -> Result<(), M
.states()
.map_err(|e| MainCpuError::GetCpuStatesFailed(Box::new(e)))?;

// Uses 48-bit virtual addresses (64 - 16 = 48) for both kernel space and user space.
states.set_tcr_el1(16, 16);
// Uses 48-bit Intermediate Physical Address (ips = 0b101) and 48-bit virtual addresses
// (64 - 16 = 48) for both kernel space and user space. Use ASID from user space (TTBR0_EL1 AKA
// lower VA range).
states.set_tcr_el1(0b101, false, 16, 16);

// Set page table.
states.set_ttbr1_el1(map.page_table);

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

0 comments on commit c909802

Please sign in to comment.