use core::arch::asm;
use aarch64_cpu::{asm::barrier, registers::*};
use memory_addr::{PhysAddr, VirtAddr};
#[inline]
pub fn enable_irqs() {
unsafe { asm!("msr daifclr, #2") };
}
#[inline]
pub fn disable_irqs() {
unsafe { asm!("msr daifset, #2") };
}
#[inline]
pub fn irqs_enabled() -> bool {
!DAIF.matches_all(DAIF::I::Masked)
}
#[inline]
pub fn wait_for_irqs() {
aarch64_cpu::asm::wfi();
}
#[inline]
pub fn halt() {
disable_irqs();
aarch64_cpu::asm::wfi(); }
#[inline]
pub fn read_kernel_page_table() -> PhysAddr {
#[cfg(not(feature = "arm-el2"))]
let root = TTBR1_EL1.get();
#[cfg(feature = "arm-el2")]
let root = TTBR0_EL2.get();
pa!(root as usize)
}
#[inline]
pub fn read_user_page_table() -> PhysAddr {
let root = TTBR0_EL1.get();
pa!(root as usize)
}
#[inline]
pub unsafe fn write_kernel_page_table(root_paddr: PhysAddr) {
#[cfg(not(feature = "arm-el2"))]
{
TTBR1_EL1.set(root_paddr.as_usize() as _);
}
#[cfg(feature = "arm-el2")]
{
TTBR0_EL2.set(root_paddr.as_usize() as _);
}
}
#[inline]
pub unsafe fn write_user_page_table(root_paddr: PhysAddr) {
TTBR0_EL1.set(root_paddr.as_usize() as _);
}
#[inline]
pub fn flush_tlb(vaddr: Option<VirtAddr>) {
if let Some(vaddr) = vaddr {
const VA_MASK: usize = (1 << 44) - 1; let operand = (vaddr.as_usize() >> 12) & VA_MASK;
#[cfg(not(feature = "arm-el2"))]
unsafe {
asm!("tlbi vaae1is, {}; dsb sy; isb", in(reg) operand)
}
#[cfg(feature = "arm-el2")]
unsafe {
asm!("tlbi vae2is, {}; dsb sy; isb", in(reg) operand)
}
} else {
#[cfg(not(feature = "arm-el2"))]
unsafe {
asm!("tlbi vmalle1; dsb sy; isb")
}
#[cfg(feature = "arm-el2")]
unsafe {
asm!("tlbi alle2; dsb sy; isb")
}
}
}
#[inline]
pub fn flush_icache_all() {
unsafe { asm!("ic iallu; dsb sy; isb") };
}
#[inline]
pub fn flush_dcache_line(vaddr: VirtAddr) {
unsafe { asm!("dc ivac, {0:x}; dsb sy; isb", in(reg) vaddr.as_usize()) };
}
#[inline]
pub unsafe fn write_exception_vector_base(vbar: usize) {
#[cfg(not(feature = "arm-el2"))]
VBAR_EL1.set(vbar as _);
#[cfg(feature = "arm-el2")]
VBAR_EL2.set(vbar as _);
}
#[inline]
pub fn read_thread_pointer() -> usize {
TPIDR_EL0.get() as usize
}
#[inline]
pub unsafe fn write_thread_pointer(tpidr_el0: usize) {
TPIDR_EL0.set(tpidr_el0 as _)
}
#[inline]
pub fn enable_fp() {
CPACR_EL1.write(CPACR_EL1::FPEN::TrapNothing);
barrier::isb(barrier::SY);
}
#[inline]
pub fn timer_frequency() -> u32 {
CNTFRQ_EL0.get() as u32
}
#[inline]
pub fn phys_timer_counter() -> u64 {
CNTPCT_EL0.get()
}
#[inline]
pub fn phys_timer_enable(enabled: bool) {
CNTP_CTL_EL0.modify(CNTP_CTL_EL0::ENABLE.val(enabled as u64));
}
#[inline]
pub fn phys_timer_set_countdown(ticks: u32) {
CNTP_TVAL_EL0.set(ticks as u64);
}
#[inline]
pub fn virt_timer_counter() -> u64 {
CNTVCT_EL0.get()
}
#[inline]
pub fn virt_timer_enable(enabled: bool) {
CNTV_CTL_EL0.modify(CNTV_CTL_EL0::ENABLE.val(enabled as u64));
}
#[inline]
pub fn virt_timer_set_countdown(ticks: u32) {
CNTV_TVAL_EL0.set(ticks as u64);
}