kvm_guest.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // SPDX-License-Identifier: GPL-2.0
  2. #define pr_fmt(fmt) "smccc: KVM: " fmt
  3. #include <linux/arm-smccc.h>
  4. #include <linux/bitmap.h>
  5. #include <linux/cache.h>
  6. #include <linux/kernel.h>
  7. #include <linux/string.h>
  8. #include <asm/hypervisor.h>
  9. void __weak kvm_arm_init_hyp_services(void) {}
  10. static DECLARE_BITMAP(__kvm_arm_hyp_services, ARM_SMCCC_KVM_NUM_FUNCS) __ro_after_init = { };
  11. void __init kvm_init_hyp_services(void)
  12. {
  13. struct arm_smccc_res res;
  14. u32 val[4];
  15. if (arm_smccc_1_1_get_conduit() != SMCCC_CONDUIT_HVC)
  16. return;
  17. arm_smccc_1_1_invoke(ARM_SMCCC_VENDOR_HYP_CALL_UID_FUNC_ID, &res);
  18. if (res.a0 != ARM_SMCCC_VENDOR_HYP_UID_KVM_REG_0 ||
  19. res.a1 != ARM_SMCCC_VENDOR_HYP_UID_KVM_REG_1 ||
  20. res.a2 != ARM_SMCCC_VENDOR_HYP_UID_KVM_REG_2 ||
  21. res.a3 != ARM_SMCCC_VENDOR_HYP_UID_KVM_REG_3)
  22. return;
  23. memset(&res, 0, sizeof(res));
  24. arm_smccc_1_1_invoke(ARM_SMCCC_VENDOR_HYP_KVM_FEATURES_FUNC_ID, &res);
  25. val[0] = lower_32_bits(res.a0);
  26. val[1] = lower_32_bits(res.a1);
  27. val[2] = lower_32_bits(res.a2);
  28. val[3] = lower_32_bits(res.a3);
  29. bitmap_from_arr32(__kvm_arm_hyp_services, val, ARM_SMCCC_KVM_NUM_FUNCS);
  30. pr_info("hypervisor services detected (0x%08lx 0x%08lx 0x%08lx 0x%08lx)\n",
  31. res.a3, res.a2, res.a1, res.a0);
  32. kvm_arm_init_hyp_services();
  33. }
  34. bool kvm_arm_hyp_service_available(u32 func_id)
  35. {
  36. if (func_id >= ARM_SMCCC_KVM_NUM_FUNCS)
  37. return false;
  38. return test_bit(func_id, __kvm_arm_hyp_services);
  39. }
  40. EXPORT_SYMBOL_GPL(kvm_arm_hyp_service_available);