reset.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2012,2013 - ARM Ltd
  4. * Author: Marc Zyngier <marc.zyngier@arm.com>
  5. *
  6. * Derived from arch/arm/kvm/reset.c
  7. * Copyright (C) 2012 - Virtual Open Systems and Columbia University
  8. * Author: Christoffer Dall <c.dall@virtualopensystems.com>
  9. */
  10. #include <linux/errno.h>
  11. #include <linux/kernel.h>
  12. #include <linux/kvm_host.h>
  13. #include <linux/kvm.h>
  14. #include <linux/hw_breakpoint.h>
  15. #include <linux/slab.h>
  16. #include <linux/string.h>
  17. #include <linux/types.h>
  18. #include <kvm/arm_arch_timer.h>
  19. #include <asm/cpufeature.h>
  20. #include <asm/cputype.h>
  21. #include <asm/fpsimd.h>
  22. #include <asm/ptrace.h>
  23. #include <asm/kvm_arm.h>
  24. #include <asm/kvm_asm.h>
  25. #include <asm/kvm_emulate.h>
  26. #include <asm/kvm_mmu.h>
  27. #include <asm/kvm_nested.h>
  28. #include <asm/virt.h>
  29. /* Maximum phys_shift supported for any VM on this host */
  30. static u32 __ro_after_init kvm_ipa_limit;
  31. unsigned int __ro_after_init kvm_host_sve_max_vl;
  32. /*
  33. * ARMv8 Reset Values
  34. */
  35. #define VCPU_RESET_PSTATE_EL1 (PSR_MODE_EL1h | PSR_A_BIT | PSR_I_BIT | \
  36. PSR_F_BIT | PSR_D_BIT)
  37. #define VCPU_RESET_PSTATE_EL2 (PSR_MODE_EL2h | PSR_A_BIT | PSR_I_BIT | \
  38. PSR_F_BIT | PSR_D_BIT)
  39. #define VCPU_RESET_PSTATE_SVC (PSR_AA32_MODE_SVC | PSR_AA32_A_BIT | \
  40. PSR_AA32_I_BIT | PSR_AA32_F_BIT)
  41. unsigned int __ro_after_init kvm_sve_max_vl;
  42. int __init kvm_arm_init_sve(void)
  43. {
  44. if (system_supports_sve()) {
  45. kvm_sve_max_vl = sve_max_virtualisable_vl();
  46. kvm_host_sve_max_vl = sve_max_vl();
  47. kvm_nvhe_sym(kvm_host_sve_max_vl) = kvm_host_sve_max_vl;
  48. /*
  49. * The get_sve_reg()/set_sve_reg() ioctl interface will need
  50. * to be extended with multiple register slice support in
  51. * order to support vector lengths greater than
  52. * VL_ARCH_MAX:
  53. */
  54. if (WARN_ON(kvm_sve_max_vl > VL_ARCH_MAX))
  55. kvm_sve_max_vl = VL_ARCH_MAX;
  56. /*
  57. * Don't even try to make use of vector lengths that
  58. * aren't available on all CPUs, for now:
  59. */
  60. if (kvm_sve_max_vl < sve_max_vl())
  61. pr_warn("KVM: SVE vector length for guests limited to %u bytes\n",
  62. kvm_sve_max_vl);
  63. }
  64. return 0;
  65. }
  66. static void kvm_vcpu_enable_sve(struct kvm_vcpu *vcpu)
  67. {
  68. vcpu->arch.sve_max_vl = kvm_sve_max_vl;
  69. /*
  70. * Userspace can still customize the vector lengths by writing
  71. * KVM_REG_ARM64_SVE_VLS. Allocation is deferred until
  72. * kvm_arm_vcpu_finalize(), which freezes the configuration.
  73. */
  74. vcpu_set_flag(vcpu, GUEST_HAS_SVE);
  75. }
  76. /*
  77. * Finalize vcpu's maximum SVE vector length, allocating
  78. * vcpu->arch.sve_state as necessary.
  79. */
  80. static int kvm_vcpu_finalize_sve(struct kvm_vcpu *vcpu)
  81. {
  82. void *buf;
  83. unsigned int vl;
  84. size_t reg_sz;
  85. int ret;
  86. vl = vcpu->arch.sve_max_vl;
  87. /*
  88. * Responsibility for these properties is shared between
  89. * kvm_arm_init_sve(), kvm_vcpu_enable_sve() and
  90. * set_sve_vls(). Double-check here just to be sure:
  91. */
  92. if (WARN_ON(!sve_vl_valid(vl) || vl > sve_max_virtualisable_vl() ||
  93. vl > VL_ARCH_MAX))
  94. return -EIO;
  95. reg_sz = vcpu_sve_state_size(vcpu);
  96. buf = kzalloc(reg_sz, GFP_KERNEL_ACCOUNT);
  97. if (!buf)
  98. return -ENOMEM;
  99. ret = kvm_share_hyp(buf, buf + reg_sz);
  100. if (ret) {
  101. kfree(buf);
  102. return ret;
  103. }
  104. vcpu->arch.sve_state = buf;
  105. vcpu_set_flag(vcpu, VCPU_SVE_FINALIZED);
  106. return 0;
  107. }
  108. int kvm_arm_vcpu_finalize(struct kvm_vcpu *vcpu, int feature)
  109. {
  110. switch (feature) {
  111. case KVM_ARM_VCPU_SVE:
  112. if (!vcpu_has_sve(vcpu))
  113. return -EINVAL;
  114. if (kvm_arm_vcpu_sve_finalized(vcpu))
  115. return -EPERM;
  116. return kvm_vcpu_finalize_sve(vcpu);
  117. }
  118. return -EINVAL;
  119. }
  120. bool kvm_arm_vcpu_is_finalized(struct kvm_vcpu *vcpu)
  121. {
  122. if (vcpu_has_sve(vcpu) && !kvm_arm_vcpu_sve_finalized(vcpu))
  123. return false;
  124. return true;
  125. }
  126. void kvm_arm_vcpu_destroy(struct kvm_vcpu *vcpu)
  127. {
  128. void *sve_state = vcpu->arch.sve_state;
  129. kvm_unshare_hyp(vcpu, vcpu + 1);
  130. if (sve_state)
  131. kvm_unshare_hyp(sve_state, sve_state + vcpu_sve_state_size(vcpu));
  132. kfree(sve_state);
  133. kfree(vcpu->arch.ccsidr);
  134. }
  135. static void kvm_vcpu_reset_sve(struct kvm_vcpu *vcpu)
  136. {
  137. if (vcpu_has_sve(vcpu))
  138. memset(vcpu->arch.sve_state, 0, vcpu_sve_state_size(vcpu));
  139. }
  140. static void kvm_vcpu_enable_ptrauth(struct kvm_vcpu *vcpu)
  141. {
  142. vcpu_set_flag(vcpu, GUEST_HAS_PTRAUTH);
  143. }
  144. /**
  145. * kvm_reset_vcpu - sets core registers and sys_regs to reset value
  146. * @vcpu: The VCPU pointer
  147. *
  148. * This function sets the registers on the virtual CPU struct to their
  149. * architecturally defined reset values, except for registers whose reset is
  150. * deferred until kvm_arm_vcpu_finalize().
  151. *
  152. * Note: This function can be called from two paths: The KVM_ARM_VCPU_INIT
  153. * ioctl or as part of handling a request issued by another VCPU in the PSCI
  154. * handling code. In the first case, the VCPU will not be loaded, and in the
  155. * second case the VCPU will be loaded. Because this function operates purely
  156. * on the memory-backed values of system registers, we want to do a full put if
  157. * we were loaded (handling a request) and load the values back at the end of
  158. * the function. Otherwise we leave the state alone. In both cases, we
  159. * disable preemption around the vcpu reset as we would otherwise race with
  160. * preempt notifiers which also call put/load.
  161. */
  162. void kvm_reset_vcpu(struct kvm_vcpu *vcpu)
  163. {
  164. struct vcpu_reset_state reset_state;
  165. bool loaded;
  166. u32 pstate;
  167. spin_lock(&vcpu->arch.mp_state_lock);
  168. reset_state = vcpu->arch.reset_state;
  169. vcpu->arch.reset_state.reset = false;
  170. spin_unlock(&vcpu->arch.mp_state_lock);
  171. /* Reset PMU outside of the non-preemptible section */
  172. kvm_pmu_vcpu_reset(vcpu);
  173. preempt_disable();
  174. loaded = (vcpu->cpu != -1);
  175. if (loaded)
  176. kvm_arch_vcpu_put(vcpu);
  177. if (!kvm_arm_vcpu_sve_finalized(vcpu)) {
  178. if (vcpu_has_feature(vcpu, KVM_ARM_VCPU_SVE))
  179. kvm_vcpu_enable_sve(vcpu);
  180. } else {
  181. kvm_vcpu_reset_sve(vcpu);
  182. }
  183. if (vcpu_has_feature(vcpu, KVM_ARM_VCPU_PTRAUTH_ADDRESS) ||
  184. vcpu_has_feature(vcpu, KVM_ARM_VCPU_PTRAUTH_GENERIC))
  185. kvm_vcpu_enable_ptrauth(vcpu);
  186. if (vcpu_el1_is_32bit(vcpu))
  187. pstate = VCPU_RESET_PSTATE_SVC;
  188. else if (vcpu_has_nv(vcpu))
  189. pstate = VCPU_RESET_PSTATE_EL2;
  190. else
  191. pstate = VCPU_RESET_PSTATE_EL1;
  192. /* Reset core registers */
  193. memset(vcpu_gp_regs(vcpu), 0, sizeof(*vcpu_gp_regs(vcpu)));
  194. memset(&vcpu->arch.ctxt.fp_regs, 0, sizeof(vcpu->arch.ctxt.fp_regs));
  195. vcpu->arch.ctxt.spsr_abt = 0;
  196. vcpu->arch.ctxt.spsr_und = 0;
  197. vcpu->arch.ctxt.spsr_irq = 0;
  198. vcpu->arch.ctxt.spsr_fiq = 0;
  199. vcpu_gp_regs(vcpu)->pstate = pstate;
  200. /* Reset system registers */
  201. kvm_reset_sys_regs(vcpu);
  202. /*
  203. * Additional reset state handling that PSCI may have imposed on us.
  204. * Must be done after all the sys_reg reset.
  205. */
  206. if (reset_state.reset) {
  207. unsigned long target_pc = reset_state.pc;
  208. /* Gracefully handle Thumb2 entry point */
  209. if (vcpu_mode_is_32bit(vcpu) && (target_pc & 1)) {
  210. target_pc &= ~1UL;
  211. vcpu_set_thumb(vcpu);
  212. }
  213. /* Propagate caller endianness */
  214. if (reset_state.be)
  215. kvm_vcpu_set_be(vcpu);
  216. *vcpu_pc(vcpu) = target_pc;
  217. vcpu_set_reg(vcpu, 0, reset_state.r0);
  218. }
  219. /* Reset timer */
  220. kvm_timer_vcpu_reset(vcpu);
  221. if (loaded)
  222. kvm_arch_vcpu_load(vcpu, smp_processor_id());
  223. preempt_enable();
  224. }
  225. u32 kvm_get_pa_bits(struct kvm *kvm)
  226. {
  227. /* Fixed limit until we can configure ID_AA64MMFR0.PARange */
  228. return kvm_ipa_limit;
  229. }
  230. u32 get_kvm_ipa_limit(void)
  231. {
  232. return kvm_ipa_limit;
  233. }
  234. int __init kvm_set_ipa_limit(void)
  235. {
  236. unsigned int parange;
  237. u64 mmfr0;
  238. mmfr0 = read_sanitised_ftr_reg(SYS_ID_AA64MMFR0_EL1);
  239. parange = cpuid_feature_extract_unsigned_field(mmfr0,
  240. ID_AA64MMFR0_EL1_PARANGE_SHIFT);
  241. /*
  242. * IPA size beyond 48 bits for 4K and 16K page size is only supported
  243. * when LPA2 is available. So if we have LPA2, enable it, else cap to 48
  244. * bits, in case it's reported as larger on the system.
  245. */
  246. if (!kvm_lpa2_is_enabled() && PAGE_SIZE != SZ_64K)
  247. parange = min(parange, (unsigned int)ID_AA64MMFR0_EL1_PARANGE_48);
  248. /*
  249. * Check with ARMv8.5-GTG that our PAGE_SIZE is supported at
  250. * Stage-2. If not, things will stop very quickly.
  251. */
  252. switch (cpuid_feature_extract_unsigned_field(mmfr0, ID_AA64MMFR0_EL1_TGRAN_2_SHIFT)) {
  253. case ID_AA64MMFR0_EL1_TGRAN_2_SUPPORTED_NONE:
  254. kvm_err("PAGE_SIZE not supported at Stage-2, giving up\n");
  255. return -EINVAL;
  256. case ID_AA64MMFR0_EL1_TGRAN_2_SUPPORTED_DEFAULT:
  257. kvm_debug("PAGE_SIZE supported at Stage-2 (default)\n");
  258. break;
  259. case ID_AA64MMFR0_EL1_TGRAN_2_SUPPORTED_MIN ... ID_AA64MMFR0_EL1_TGRAN_2_SUPPORTED_MAX:
  260. kvm_debug("PAGE_SIZE supported at Stage-2 (advertised)\n");
  261. break;
  262. default:
  263. kvm_err("Unsupported value for TGRAN_2, giving up\n");
  264. return -EINVAL;
  265. }
  266. kvm_ipa_limit = id_aa64mmfr0_parange_to_phys_shift(parange);
  267. kvm_info("IPA Size Limit: %d bits%s\n", kvm_ipa_limit,
  268. ((kvm_ipa_limit < KVM_PHYS_SHIFT) ?
  269. " (Reduced IPA size, limited VM/VMM compatibility)" : ""));
  270. return 0;
  271. }