1 // Copyright © 2019 Intel Corporation 2 // 3 // SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause 4 // 5 // Copyright © 2020, Microsoft Corporation 6 // 7 // Copyright 2018-2019 CrowdStrike, Inc. 8 // 9 // 10 11 pub mod gic; 12 13 use crate::kvm::{KvmError, KvmResult}; 14 use kvm_bindings::{ 15 kvm_mp_state, kvm_one_reg, kvm_regs, KVM_REG_ARM64, KVM_REG_ARM64_SYSREG, 16 KVM_REG_ARM64_SYSREG_CRM_MASK, KVM_REG_ARM64_SYSREG_CRM_SHIFT, KVM_REG_ARM64_SYSREG_CRN_MASK, 17 KVM_REG_ARM64_SYSREG_CRN_SHIFT, KVM_REG_ARM64_SYSREG_OP0_MASK, KVM_REG_ARM64_SYSREG_OP0_SHIFT, 18 KVM_REG_ARM64_SYSREG_OP1_MASK, KVM_REG_ARM64_SYSREG_OP1_SHIFT, KVM_REG_ARM64_SYSREG_OP2_MASK, 19 KVM_REG_ARM64_SYSREG_OP2_SHIFT, KVM_REG_ARM_COPROC_MASK, KVM_REG_ARM_CORE, KVM_REG_SIZE_MASK, 20 KVM_REG_SIZE_U32, KVM_REG_SIZE_U64, 21 }; 22 pub use kvm_bindings::{ 23 kvm_one_reg as Register, kvm_regs as StandardRegisters, kvm_vcpu_init as VcpuInit, RegList, 24 }; 25 use serde::{Deserialize, Serialize}; 26 pub use {kvm_ioctls::Cap, kvm_ioctls::Kvm}; 27 28 // This macro gets the offset of a structure (i.e `str`) member (i.e `field`) without having 29 // an instance of that structure. 30 #[macro_export] 31 macro_rules! offset__of { 32 ($str:ty, $($field:ident)+) => ({ 33 let tmp: std::mem::MaybeUninit<$str> = std::mem::MaybeUninit::uninit(); 34 let base = tmp.as_ptr(); 35 36 // Avoid warnings when nesting `unsafe` blocks. 37 #[allow(unused_unsafe)] 38 // SAFETY: The pointer is valid and aligned, just not initialised. Using `addr_of` ensures 39 // that we don't actually read from `base` (which would be UB) nor create an intermediate 40 // reference. 41 let member = unsafe { core::ptr::addr_of!((*base).$($field)*) } as *const u8; 42 43 // Avoid warnings when nesting `unsafe` blocks. 44 #[allow(unused_unsafe)] 45 // SAFETY: The two pointers are within the same allocated object `tmp`. All requirements 46 // from offset_from are upheld. 47 unsafe { member.offset_from(base as *const u8) as usize } 48 }); 49 } 50 51 // Following are macros that help with getting the ID of a aarch64 core register. 52 // The core register are represented by the user_pt_regs structure. Look for it in 53 // arch/arm64/include/uapi/asm/ptrace.h. 54 55 // Get the ID of a core register 56 #[macro_export] 57 macro_rules! arm64_core_reg_id { 58 ($size: tt, $offset: tt) => { 59 // The core registers of an arm64 machine are represented 60 // in kernel by the `kvm_regs` structure. This structure is a 61 // mix of 32, 64 and 128 bit fields: 62 // struct kvm_regs { 63 // struct user_pt_regs regs; 64 // 65 // __u64 sp_el1; 66 // __u64 elr_el1; 67 // 68 // __u64 spsr[KVM_NR_SPSR]; 69 // 70 // struct user_fpsimd_state fp_regs; 71 // }; 72 // struct user_pt_regs { 73 // __u64 regs[31]; 74 // __u64 sp; 75 // __u64 pc; 76 // __u64 pstate; 77 // }; 78 // The id of a core register can be obtained like this: 79 // offset = id & ~(KVM_REG_ARCH_MASK | KVM_REG_SIZE_MASK | KVM_REG_ARM_CORE). Thus, 80 // id = KVM_REG_ARM64 | KVM_REG_SIZE_U64/KVM_REG_SIZE_U32/KVM_REG_SIZE_U128 | KVM_REG_ARM_CORE | offset 81 KVM_REG_ARM64 as u64 82 | u64::from(KVM_REG_ARM_CORE) 83 | $size 84 | (($offset / mem::size_of::<u32>()) as u64) 85 }; 86 } 87 88 // This macro computes the ID of a specific ARM64 system register similar to how 89 // the kernel C macro does. 90 // https://elixir.bootlin.com/linux/v4.20.17/source/arch/arm64/include/uapi/asm/kvm.h#L203 91 #[macro_export] 92 macro_rules! arm64_sys_reg { 93 ($name: tt, $op0: tt, $op1: tt, $crn: tt, $crm: tt, $op2: tt) => { 94 pub const $name: u64 = KVM_REG_ARM64 as u64 95 | KVM_REG_SIZE_U64 as u64 96 | KVM_REG_ARM64_SYSREG as u64 97 | ((($op0 as u64) << KVM_REG_ARM64_SYSREG_OP0_SHIFT) 98 & KVM_REG_ARM64_SYSREG_OP0_MASK as u64) 99 | ((($op1 as u64) << KVM_REG_ARM64_SYSREG_OP1_SHIFT) 100 & KVM_REG_ARM64_SYSREG_OP1_MASK as u64) 101 | ((($crn as u64) << KVM_REG_ARM64_SYSREG_CRN_SHIFT) 102 & KVM_REG_ARM64_SYSREG_CRN_MASK as u64) 103 | ((($crm as u64) << KVM_REG_ARM64_SYSREG_CRM_SHIFT) 104 & KVM_REG_ARM64_SYSREG_CRM_MASK as u64) 105 | ((($op2 as u64) << KVM_REG_ARM64_SYSREG_OP2_SHIFT) 106 & KVM_REG_ARM64_SYSREG_OP2_MASK as u64); 107 }; 108 } 109 110 // Constant imported from the Linux kernel: 111 // https://elixir.bootlin.com/linux/v4.20.17/source/arch/arm64/include/asm/sysreg.h#L135 112 arm64_sys_reg!(MPIDR_EL1, 3, 0, 0, 0, 5); 113 114 /// Specifies whether a particular register is a system register or not. 115 /// The kernel splits the registers on aarch64 in core registers and system registers. 116 /// So, below we get the system registers by checking that they are not core registers. 117 /// 118 /// # Arguments 119 /// 120 /// * `regid` - The index of the register we are checking. 121 pub fn is_system_register(regid: u64) -> bool { 122 if (regid & KVM_REG_ARM_COPROC_MASK as u64) == KVM_REG_ARM_CORE as u64 { 123 return false; 124 } 125 126 let size = regid & KVM_REG_SIZE_MASK; 127 128 assert!( 129 !(size != KVM_REG_SIZE_U32 && size != KVM_REG_SIZE_U64), 130 "Unexpected register size for system register {}", 131 size 132 ); 133 134 true 135 } 136 137 pub fn check_required_kvm_extensions(kvm: &Kvm) -> KvmResult<()> { 138 if !kvm.check_extension(Cap::SignalMsi) { 139 return Err(KvmError::CapabilityMissing(Cap::SignalMsi)); 140 } 141 if !kvm.check_extension(Cap::OneReg) { 142 return Err(KvmError::CapabilityMissing(Cap::OneReg)); 143 } 144 Ok(()) 145 } 146 147 #[derive(Clone, Default, Serialize, Deserialize)] 148 pub struct VcpuKvmState { 149 pub mp_state: kvm_mp_state, 150 pub core_regs: kvm_regs, 151 pub sys_regs: Vec<kvm_one_reg>, 152 // We will be using the mpidr for passing it to the VmState. 153 // The VmState will give this away for saving restoring the icc and redistributor 154 // registers. 155 pub mpidr: u64, 156 } 157