#[cfg(all(target_arch = "aarch64", target_os = "macos"))]
use core::arch::{asm, naked_asm};
#[cfg(all(target_arch = "aarch64", target_os = "macos"))]
use std::ptr::{self, NonNull};
#[cfg(all(target_arch = "aarch64", target_os = "macos"))]
use crate::context::{FiberCore, fiber_root};
#[cfg(all(target_arch = "aarch64", target_os = "macos"))]
pub(crate) const FRAME_LEN: usize = 176;
#[cfg(all(target_arch = "aarch64", target_os = "macos"))]
const SLOT_X19: usize = 0;
#[cfg(all(target_arch = "aarch64", target_os = "macos"))]
const SLOT_X30: usize = 88;
#[cfg(all(target_arch = "aarch64", target_os = "macos"))]
const SLOT_FPCR: usize = 160;
#[cfg(all(target_arch = "aarch64", target_os = "macos"))]
#[unsafe(naked)]
pub(crate) unsafe extern "C" fn context_switch(
save_current: *mut usize,
restore: usize,
transfer: usize,
) -> usize {
naked_asm!(
"sub sp, sp, #176",
"stp x19, x20, [sp, #0]",
"stp x21, x22, [sp, #16]",
"stp x23, x24, [sp, #32]",
"stp x25, x26, [sp, #48]",
"stp x27, x28, [sp, #64]",
"stp x29, x30, [sp, #80]",
"stp d8, d9, [sp, #96]",
"stp d10, d11, [sp, #112]",
"stp d12, d13, [sp, #128]",
"stp d14, d15, [sp, #144]",
"mrs x3, fpcr",
"str x3, [sp, #160]",
"mov x4, sp",
"str x4, [x0]",
"mov sp, x1",
"ldr x4, [sp, #160]",
"cmp x3, x4",
"b.eq 2f",
"msr fpcr, x4",
"2:",
"ldp d14, d15, [sp, #144]",
"ldp d12, d13, [sp, #128]",
"ldp d10, d11, [sp, #112]",
"ldp d8, d9, [sp, #96]",
"ldp x29, x30, [sp, #80]",
"ldp x27, x28, [sp, #64]",
"ldp x25, x26, [sp, #48]",
"ldp x23, x24, [sp, #32]",
"ldp x21, x22, [sp, #16]",
"ldp x19, x20, [sp, #0]",
"add sp, sp, #176",
"mov x0, x2",
"ret",
)
}
#[cfg(all(target_arch = "aarch64", target_os = "macos"))]
#[unsafe(naked)]
pub(crate) unsafe extern "C" fn context_finish(restore: usize, transfer: usize) -> ! {
naked_asm!(
"mrs x3, fpcr",
"mov sp, x0",
"ldr x4, [sp, #160]",
"cmp x3, x4",
"b.eq 2f",
"msr fpcr, x4",
"2:",
"ldp d14, d15, [sp, #144]",
"ldp d12, d13, [sp, #128]",
"ldp d10, d11, [sp, #112]",
"ldp d8, d9, [sp, #96]",
"ldp x29, x30, [sp, #80]",
"ldp x27, x28, [sp, #64]",
"ldp x25, x26, [sp, #48]",
"ldp x23, x24, [sp, #32]",
"ldp x21, x22, [sp, #16]",
"ldp x19, x20, [sp, #0]",
"add sp, sp, #176",
"mov x0, x1",
"ret",
)
}
#[cfg(all(target_arch = "aarch64", target_os = "macos"))]
#[unsafe(naked)]
unsafe extern "C" fn trampoline() -> ! {
naked_asm!(
"mov x0, x19",
"mov x29, xzr",
"mov x30, xzr",
"b {root}",
root = sym fiber_root,
)
}
#[cfg(all(target_arch = "aarch64", target_os = "macos"))]
pub(crate) unsafe fn init_frame(frame_top: usize, core: NonNull<FiberCore>) -> usize {
let sp = frame_top - FRAME_LEN;
let frame = sp as *mut u8;
unsafe {
ptr::write_bytes(frame, 0, FRAME_LEN);
frame
.add(SLOT_X19)
.cast::<usize>()
.write(core.as_ptr() as usize);
frame
.add(SLOT_X30)
.cast::<usize>()
.write(trampoline as *const () as usize);
frame.add(SLOT_FPCR).cast::<u64>().write(fpcr());
}
sp
}
#[cfg(all(target_arch = "aarch64", target_os = "macos"))]
pub(crate) fn fpcr() -> u64 {
let value: u64;
unsafe {
asm!("mrs {}, fpcr", out(reg) value, options(nomem, nostack, preserves_flags));
}
value
}
#[cfg(test)]
#[path = "aarch64_macos_test.rs"]
mod aarch64_macos_test;