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 /// 12 /// Export generically-named wrappers of kvm-bindings for Unix-based platforms 13 /// 14 use crate::kvm::{KvmError, KvmResult}; 15 use kvm_bindings::{ 16 kvm_mp_state, kvm_one_reg, kvm_regs, KVM_REG_ARM64, KVM_REG_ARM64_SYSREG, 17 KVM_REG_ARM64_SYSREG_CRM_MASK, KVM_REG_ARM64_SYSREG_CRM_SHIFT, KVM_REG_ARM64_SYSREG_CRN_MASK, 18 KVM_REG_ARM64_SYSREG_CRN_SHIFT, KVM_REG_ARM64_SYSREG_OP0_MASK, KVM_REG_ARM64_SYSREG_OP0_SHIFT, 19 KVM_REG_ARM64_SYSREG_OP1_MASK, KVM_REG_ARM64_SYSREG_OP1_SHIFT, KVM_REG_ARM64_SYSREG_OP2_MASK, 20 KVM_REG_ARM64_SYSREG_OP2_SHIFT, KVM_REG_ARM_COPROC_MASK, KVM_REG_ARM_CORE, KVM_REG_SIZE_MASK, 21 KVM_REG_SIZE_U32, KVM_REG_SIZE_U64, 22 }; 23 pub use kvm_bindings::{ 24 kvm_one_reg as Register, kvm_regs as StandardRegisters, kvm_vcpu_init as VcpuInit, RegList, 25 }; 26 use serde_derive::{Deserialize, Serialize}; 27 pub use {kvm_ioctls::Cap, kvm_ioctls::Kvm}; 28 29 // Following are macros that help with getting the ID of a aarch64 core register. 30 // The core register are represented by the user_pt_regs structure. Look for it in 31 // arch/arm64/include/uapi/asm/ptrace.h. 32 33 // This macro gets the offset of a structure (i.e `str`) member (i.e `field`) without having 34 // an instance of that structure. 35 #[macro_export] 36 macro_rules! offset__of { 37 ($str:ty, $($field:ident)+) => ({ 38 let tmp: std::mem::MaybeUninit<$str> = std::mem::MaybeUninit::uninit(); 39 let tmp = unsafe { tmp.assume_init() }; 40 let base = &tmp as *const _ as usize; 41 let member = &tmp.$($field)* as *const _ as usize; 42 43 member - base 44 }); 45 } 46 // Get the ID of a core register 47 #[macro_export] 48 macro_rules! arm64_core_reg_id { 49 ($size: tt, $offset: tt) => { 50 // The core registers of an arm64 machine are represented 51 // in kernel by the `kvm_regs` structure. This structure is a 52 // mix of 32, 64 and 128 bit fields: 53 // struct kvm_regs { 54 // struct user_pt_regs regs; 55 // 56 // __u64 sp_el1; 57 // __u64 elr_el1; 58 // 59 // __u64 spsr[KVM_NR_SPSR]; 60 // 61 // struct user_fpsimd_state fp_regs; 62 // }; 63 // struct user_pt_regs { 64 // __u64 regs[31]; 65 // __u64 sp; 66 // __u64 pc; 67 // __u64 pstate; 68 // }; 69 // The id of a core register can be obtained like this: 70 // offset = id & ~(KVM_REG_ARCH_MASK | KVM_REG_SIZE_MASK | KVM_REG_ARM_CORE). Thus, 71 // id = KVM_REG_ARM64 | KVM_REG_SIZE_U64/KVM_REG_SIZE_U32/KVM_REG_SIZE_U128 | KVM_REG_ARM_CORE | offset 72 KVM_REG_ARM64 as u64 73 | u64::from(KVM_REG_ARM_CORE) 74 | $size 75 | (($offset / mem::size_of::<u32>()) as u64) 76 }; 77 } 78 79 // This macro computes the ID of a specific ARM64 system register similar to how 80 // the kernel C macro does. 81 // https://elixir.bootlin.com/linux/v4.20.17/source/arch/arm64/include/uapi/asm/kvm.h#L203 82 #[macro_export] 83 macro_rules! arm64_sys_reg { 84 ($name: tt, $op0: tt, $op1: tt, $crn: tt, $crm: tt, $op2: tt) => { 85 pub const $name: u64 = KVM_REG_ARM64 as u64 86 | KVM_REG_SIZE_U64 as u64 87 | KVM_REG_ARM64_SYSREG as u64 88 | ((($op0 as u64) << KVM_REG_ARM64_SYSREG_OP0_SHIFT) 89 & KVM_REG_ARM64_SYSREG_OP0_MASK as u64) 90 | ((($op1 as u64) << KVM_REG_ARM64_SYSREG_OP1_SHIFT) 91 & KVM_REG_ARM64_SYSREG_OP1_MASK as u64) 92 | ((($crn as u64) << KVM_REG_ARM64_SYSREG_CRN_SHIFT) 93 & KVM_REG_ARM64_SYSREG_CRN_MASK as u64) 94 | ((($crm as u64) << KVM_REG_ARM64_SYSREG_CRM_SHIFT) 95 & KVM_REG_ARM64_SYSREG_CRM_MASK as u64) 96 | ((($op2 as u64) << KVM_REG_ARM64_SYSREG_OP2_SHIFT) 97 & KVM_REG_ARM64_SYSREG_OP2_MASK as u64); 98 }; 99 } 100 101 // Constant imported from the Linux kernel: 102 // https://elixir.bootlin.com/linux/v4.20.17/source/arch/arm64/include/asm/sysreg.h#L135 103 arm64_sys_reg!(MPIDR_EL1, 3, 0, 0, 0, 5); 104 105 /// Specifies whether a particular register is a system register or not. 106 /// The kernel splits the registers on aarch64 in core registers and system registers. 107 /// So, below we get the system registers by checking that they are not core registers. 108 /// 109 /// # Arguments 110 /// 111 /// * `regid` - The index of the register we are checking. 112 pub fn is_system_register(regid: u64) -> bool { 113 if (regid & KVM_REG_ARM_COPROC_MASK as u64) == KVM_REG_ARM_CORE as u64 { 114 return false; 115 } 116 117 let size = regid & KVM_REG_SIZE_MASK; 118 if size != KVM_REG_SIZE_U32 && size != KVM_REG_SIZE_U64 { 119 panic!("Unexpected register size for system register {}", size); 120 } 121 true 122 } 123 124 pub fn check_required_kvm_extensions(kvm: &Kvm) -> KvmResult<()> { 125 if !kvm.check_extension(Cap::SignalMsi) { 126 return Err(KvmError::CapabilityMissing(Cap::SignalMsi)); 127 } 128 if !kvm.check_extension(Cap::OneReg) { 129 return Err(KvmError::CapabilityMissing(Cap::OneReg)); 130 } 131 Ok(()) 132 } 133 134 #[derive(Clone, Default, Serialize, Deserialize)] 135 pub struct VcpuKvmState { 136 pub mp_state: kvm_mp_state, 137 pub core_regs: kvm_regs, 138 pub sys_regs: Vec<kvm_one_reg>, 139 // We will be using the mpidr for passing it to the VmState. 140 // The VmState will give this away for saving restoring the icc and redistributor 141 // registers. 142 pub mpidr: u64, 143 } 144