reset.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2012,2013 - ARM Ltd
  4. * Author: Marc Zyngier <[email protected]>
  5. *
  6. * Derived from arch/arm/kvm/reset.c
  7. * Copyright (C) 2012 - Virtual Open Systems and Columbia University
  8. * Author: Christoffer Dall <[email protected]>
  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/virt.h>
  28. /* Maximum phys_shift supported for any VM on this host */
  29. static u32 kvm_ipa_limit;
  30. unsigned int kvm_sve_max_vl;
  31. unsigned int kvm_host_sve_max_vl;
  32. int kvm_arm_init_sve(void)
  33. {
  34. if (system_supports_sve()) {
  35. kvm_sve_max_vl = sve_max_virtualisable_vl();
  36. kvm_host_sve_max_vl = sve_max_vl();
  37. /*
  38. * The get_sve_reg()/set_sve_reg() ioctl interface will need
  39. * to be extended with multiple register slice support in
  40. * order to support vector lengths greater than
  41. * VL_ARCH_MAX:
  42. */
  43. if (WARN_ON(kvm_sve_max_vl > VL_ARCH_MAX))
  44. kvm_sve_max_vl = VL_ARCH_MAX;
  45. /*
  46. * Don't even try to make use of vector lengths that
  47. * aren't available on all CPUs, for now:
  48. */
  49. if (kvm_sve_max_vl < sve_max_vl())
  50. pr_warn("KVM: SVE vector length for guests limited to %u bytes\n",
  51. kvm_sve_max_vl);
  52. }
  53. return 0;
  54. }
  55. static int kvm_vcpu_enable_sve(struct kvm_vcpu *vcpu)
  56. {
  57. if (!system_supports_sve())
  58. return -EINVAL;
  59. vcpu->arch.sve_max_vl = kvm_sve_max_vl;
  60. /*
  61. * Userspace can still customize the vector lengths by writing
  62. * KVM_REG_ARM64_SVE_VLS. Allocation is deferred until
  63. * kvm_arm_vcpu_finalize(), which freezes the configuration.
  64. */
  65. vcpu_set_flag(vcpu, GUEST_HAS_SVE);
  66. return 0;
  67. }
  68. /*
  69. * Finalize vcpu's maximum SVE vector length, allocating
  70. * vcpu->arch.sve_state as necessary.
  71. */
  72. static int kvm_vcpu_finalize_sve(struct kvm_vcpu *vcpu)
  73. {
  74. void *buf;
  75. unsigned int vl;
  76. size_t reg_sz;
  77. int ret;
  78. vl = vcpu->arch.sve_max_vl;
  79. /*
  80. * Responsibility for these properties is shared between
  81. * kvm_arm_init_sve(), kvm_vcpu_enable_sve() and
  82. * set_sve_vls(). Double-check here just to be sure:
  83. */
  84. if (WARN_ON(!sve_vl_valid(vl) || vl > sve_max_virtualisable_vl() ||
  85. vl > VL_ARCH_MAX))
  86. return -EIO;
  87. reg_sz = vcpu_sve_state_size(vcpu);
  88. buf = kzalloc(reg_sz, GFP_KERNEL_ACCOUNT);
  89. if (!buf)
  90. return -ENOMEM;
  91. ret = kvm_share_hyp(buf, buf + reg_sz);
  92. if (ret) {
  93. kfree(buf);
  94. return ret;
  95. }
  96. vcpu->arch.sve_state = buf;
  97. vcpu_set_flag(vcpu, VCPU_SVE_FINALIZED);
  98. return 0;
  99. }
  100. int kvm_arm_vcpu_finalize(struct kvm_vcpu *vcpu, int feature)
  101. {
  102. switch (feature) {
  103. case KVM_ARM_VCPU_SVE:
  104. if (!vcpu_has_sve(vcpu))
  105. return -EINVAL;
  106. if (kvm_arm_vcpu_sve_finalized(vcpu))
  107. return -EPERM;
  108. return kvm_vcpu_finalize_sve(vcpu);
  109. }
  110. return -EINVAL;
  111. }
  112. bool kvm_arm_vcpu_is_finalized(struct kvm_vcpu *vcpu)
  113. {
  114. if (vcpu_has_sve(vcpu) && !kvm_arm_vcpu_sve_finalized(vcpu))
  115. return false;
  116. return true;
  117. }
  118. void kvm_arm_vcpu_destroy(struct kvm_vcpu *vcpu)
  119. {
  120. void *sve_state = vcpu->arch.sve_state;
  121. kvm_unshare_hyp(vcpu, vcpu + 1);
  122. if (sve_state)
  123. kvm_unshare_hyp(sve_state, sve_state + vcpu_sve_state_size(vcpu));
  124. kfree(sve_state);
  125. }
  126. static void kvm_vcpu_reset_sve(struct kvm_vcpu *vcpu)
  127. {
  128. if (vcpu_has_sve(vcpu))
  129. memset(vcpu->arch.sve_state, 0, vcpu_sve_state_size(vcpu));
  130. }
  131. /**
  132. * kvm_set_vm_width() - set the register width for the guest
  133. * @vcpu: Pointer to the vcpu being configured
  134. *
  135. * Set both KVM_ARCH_FLAG_EL1_32BIT and KVM_ARCH_FLAG_REG_WIDTH_CONFIGURED
  136. * in the VM flags based on the vcpu's requested register width, the HW
  137. * capabilities and other options (such as MTE).
  138. * When REG_WIDTH_CONFIGURED is already set, the vcpu settings must be
  139. * consistent with the value of the FLAG_EL1_32BIT bit in the flags.
  140. *
  141. * Return: 0 on success, negative error code on failure.
  142. */
  143. static int kvm_set_vm_width(struct kvm_vcpu *vcpu)
  144. {
  145. struct kvm *kvm = vcpu->kvm;
  146. bool is32bit;
  147. is32bit = vcpu_has_feature(vcpu, KVM_ARM_VCPU_EL1_32BIT);
  148. lockdep_assert_held(&kvm->arch.config_lock);
  149. if (test_bit(KVM_ARCH_FLAG_REG_WIDTH_CONFIGURED, &kvm->arch.flags)) {
  150. /*
  151. * The guest's register width is already configured.
  152. * Make sure that the vcpu is consistent with it.
  153. */
  154. if (is32bit == test_bit(KVM_ARCH_FLAG_EL1_32BIT, &kvm->arch.flags))
  155. return 0;
  156. return -EINVAL;
  157. }
  158. if (!cpus_have_const_cap(ARM64_HAS_32BIT_EL1) && is32bit)
  159. return -EINVAL;
  160. /* MTE is incompatible with AArch32 */
  161. if (kvm_has_mte(kvm) && is32bit)
  162. return -EINVAL;
  163. if (is32bit)
  164. set_bit(KVM_ARCH_FLAG_EL1_32BIT, &kvm->arch.flags);
  165. set_bit(KVM_ARCH_FLAG_REG_WIDTH_CONFIGURED, &kvm->arch.flags);
  166. return 0;
  167. }
  168. /**
  169. * kvm_reset_vcpu - sets core registers and sys_regs to reset value
  170. * @vcpu: The VCPU pointer
  171. *
  172. * This function sets the registers on the virtual CPU struct to their
  173. * architecturally defined reset values, except for registers whose reset is
  174. * deferred until kvm_arm_vcpu_finalize().
  175. *
  176. * Note: This function can be called from two paths: The KVM_ARM_VCPU_INIT
  177. * ioctl or as part of handling a request issued by another VCPU in the PSCI
  178. * handling code. In the first case, the VCPU will not be loaded, and in the
  179. * second case the VCPU will be loaded. Because this function operates purely
  180. * on the memory-backed values of system registers, we want to do a full put if
  181. * we were loaded (handling a request) and load the values back at the end of
  182. * the function. Otherwise we leave the state alone. In both cases, we
  183. * disable preemption around the vcpu reset as we would otherwise race with
  184. * preempt notifiers which also call put/load.
  185. */
  186. int kvm_reset_vcpu(struct kvm_vcpu *vcpu)
  187. {
  188. struct vcpu_reset_state reset_state;
  189. int ret;
  190. bool loaded;
  191. mutex_lock(&vcpu->kvm->arch.config_lock);
  192. ret = kvm_set_vm_width(vcpu);
  193. mutex_unlock(&vcpu->kvm->arch.config_lock);
  194. if (ret)
  195. return ret;
  196. spin_lock(&vcpu->arch.mp_state_lock);
  197. reset_state = vcpu->arch.reset_state;
  198. vcpu->arch.reset_state.reset = false;
  199. spin_unlock(&vcpu->arch.mp_state_lock);
  200. /* Reset PMU outside of the non-preemptible section */
  201. kvm_pmu_vcpu_reset(vcpu);
  202. preempt_disable();
  203. loaded = (vcpu->cpu != -1);
  204. if (loaded)
  205. kvm_arch_vcpu_put(vcpu);
  206. if (!kvm_arm_vcpu_sve_finalized(vcpu)) {
  207. if (test_bit(KVM_ARM_VCPU_SVE, vcpu->arch.features)) {
  208. ret = kvm_vcpu_enable_sve(vcpu);
  209. if (ret)
  210. goto out;
  211. }
  212. } else {
  213. kvm_vcpu_reset_sve(vcpu);
  214. }
  215. if (test_bit(KVM_ARM_VCPU_PTRAUTH_ADDRESS, vcpu->arch.features) ||
  216. test_bit(KVM_ARM_VCPU_PTRAUTH_GENERIC, vcpu->arch.features)) {
  217. if (kvm_vcpu_enable_ptrauth(vcpu)) {
  218. ret = -EINVAL;
  219. goto out;
  220. }
  221. }
  222. if (kvm_vcpu_has_pmu(vcpu) && !kvm_arm_support_pmu_v3()) {
  223. ret = -EINVAL;
  224. goto out;
  225. }
  226. /* Reset core registers */
  227. kvm_reset_vcpu_core(vcpu);
  228. /* Reset system registers */
  229. kvm_reset_sys_regs(vcpu);
  230. /*
  231. * Additional reset state handling that PSCI may have imposed on us.
  232. * Must be done after all the sys_reg reset.
  233. */
  234. if (reset_state.reset)
  235. kvm_reset_vcpu_psci(vcpu, &reset_state);
  236. /* Reset timer */
  237. ret = kvm_timer_vcpu_reset(vcpu);
  238. out:
  239. if (loaded)
  240. kvm_arch_vcpu_load(vcpu, smp_processor_id());
  241. preempt_enable();
  242. return ret;
  243. }
  244. u32 get_kvm_ipa_limit(void)
  245. {
  246. return kvm_ipa_limit;
  247. }
  248. int kvm_set_ipa_limit(void)
  249. {
  250. unsigned int parange;
  251. u64 mmfr0;
  252. mmfr0 = read_sanitised_ftr_reg(SYS_ID_AA64MMFR0_EL1);
  253. parange = cpuid_feature_extract_unsigned_field(mmfr0,
  254. ID_AA64MMFR0_EL1_PARANGE_SHIFT);
  255. /*
  256. * IPA size beyond 48 bits could not be supported
  257. * on either 4K or 16K page size. Hence let's cap
  258. * it to 48 bits, in case it's reported as larger
  259. * on the system.
  260. */
  261. if (PAGE_SIZE != SZ_64K)
  262. parange = min(parange, (unsigned int)ID_AA64MMFR0_EL1_PARANGE_48);
  263. /*
  264. * Check with ARMv8.5-GTG that our PAGE_SIZE is supported at
  265. * Stage-2. If not, things will stop very quickly.
  266. */
  267. switch (cpuid_feature_extract_unsigned_field(mmfr0, ID_AA64MMFR0_EL1_TGRAN_2_SHIFT)) {
  268. case ID_AA64MMFR0_EL1_TGRAN_2_SUPPORTED_NONE:
  269. kvm_err("PAGE_SIZE not supported at Stage-2, giving up\n");
  270. return -EINVAL;
  271. case ID_AA64MMFR0_EL1_TGRAN_2_SUPPORTED_DEFAULT:
  272. kvm_debug("PAGE_SIZE supported at Stage-2 (default)\n");
  273. break;
  274. case ID_AA64MMFR0_EL1_TGRAN_2_SUPPORTED_MIN ... ID_AA64MMFR0_EL1_TGRAN_2_SUPPORTED_MAX:
  275. kvm_debug("PAGE_SIZE supported at Stage-2 (advertised)\n");
  276. break;
  277. default:
  278. kvm_err("Unsupported value for TGRAN_2, giving up\n");
  279. return -EINVAL;
  280. }
  281. kvm_ipa_limit = id_aa64mmfr0_parange_to_phys_shift(parange);
  282. kvm_info("IPA Size Limit: %d bits%s\n", kvm_ipa_limit,
  283. ((kvm_ipa_limit < KVM_PHYS_SHIFT) ?
  284. " (Reduced IPA size, limited VM/VMM compatibility)" : ""));
  285. return 0;
  286. }