reset.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * Copyright (C) 2012,2013 - ARM Ltd
  3. * Author: Marc Zyngier <marc.zyngier@arm.com>
  4. *
  5. * Derived from arch/arm/kvm/reset.c
  6. * Copyright (C) 2012 - Virtual Open Systems and Columbia University
  7. * Author: Christoffer Dall <c.dall@virtualopensystems.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License, version 2, as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include <linux/errno.h>
  22. #include <linux/kvm_host.h>
  23. #include <linux/kvm.h>
  24. #include <linux/hw_breakpoint.h>
  25. #include <kvm/arm_arch_timer.h>
  26. #include <asm/cpufeature.h>
  27. #include <asm/cputype.h>
  28. #include <asm/ptrace.h>
  29. #include <asm/kvm_arm.h>
  30. #include <asm/kvm_asm.h>
  31. #include <asm/kvm_coproc.h>
  32. #include <asm/kvm_mmu.h>
  33. /* Maximum phys_shift supported for any VM on this host */
  34. static u32 kvm_ipa_limit;
  35. /*
  36. * ARMv8 Reset Values
  37. */
  38. static const struct kvm_regs default_regs_reset = {
  39. .regs.pstate = (PSR_MODE_EL1h | PSR_A_BIT | PSR_I_BIT |
  40. PSR_F_BIT | PSR_D_BIT),
  41. };
  42. static const struct kvm_regs default_regs_reset32 = {
  43. .regs.pstate = (PSR_AA32_MODE_SVC | PSR_AA32_A_BIT |
  44. PSR_AA32_I_BIT | PSR_AA32_F_BIT),
  45. };
  46. static bool cpu_has_32bit_el1(void)
  47. {
  48. u64 pfr0;
  49. pfr0 = read_sanitised_ftr_reg(SYS_ID_AA64PFR0_EL1);
  50. return !!(pfr0 & 0x20);
  51. }
  52. /**
  53. * kvm_arch_vm_ioctl_check_extension
  54. *
  55. * We currently assume that the number of HW registers is uniform
  56. * across all CPUs (see cpuinfo_sanity_check).
  57. */
  58. int kvm_arch_vm_ioctl_check_extension(struct kvm *kvm, long ext)
  59. {
  60. int r;
  61. switch (ext) {
  62. case KVM_CAP_ARM_EL1_32BIT:
  63. r = cpu_has_32bit_el1();
  64. break;
  65. case KVM_CAP_GUEST_DEBUG_HW_BPS:
  66. r = get_num_brps();
  67. break;
  68. case KVM_CAP_GUEST_DEBUG_HW_WPS:
  69. r = get_num_wrps();
  70. break;
  71. case KVM_CAP_ARM_PMU_V3:
  72. r = kvm_arm_support_pmu_v3();
  73. break;
  74. case KVM_CAP_ARM_INJECT_SERROR_ESR:
  75. r = cpus_have_const_cap(ARM64_HAS_RAS_EXTN);
  76. break;
  77. case KVM_CAP_SET_GUEST_DEBUG:
  78. case KVM_CAP_VCPU_ATTRIBUTES:
  79. r = 1;
  80. break;
  81. case KVM_CAP_ARM_VM_IPA_SIZE:
  82. r = kvm_ipa_limit;
  83. break;
  84. default:
  85. r = 0;
  86. }
  87. return r;
  88. }
  89. /**
  90. * kvm_reset_vcpu - sets core registers and sys_regs to reset value
  91. * @vcpu: The VCPU pointer
  92. *
  93. * This function finds the right table above and sets the registers on
  94. * the virtual CPU struct to their architecturally defined reset
  95. * values.
  96. */
  97. int kvm_reset_vcpu(struct kvm_vcpu *vcpu)
  98. {
  99. const struct kvm_regs *cpu_reset;
  100. switch (vcpu->arch.target) {
  101. default:
  102. if (test_bit(KVM_ARM_VCPU_EL1_32BIT, vcpu->arch.features)) {
  103. if (!cpu_has_32bit_el1())
  104. return -EINVAL;
  105. cpu_reset = &default_regs_reset32;
  106. } else {
  107. cpu_reset = &default_regs_reset;
  108. }
  109. break;
  110. }
  111. /* Reset core registers */
  112. memcpy(vcpu_gp_regs(vcpu), cpu_reset, sizeof(*cpu_reset));
  113. /* Reset system registers */
  114. kvm_reset_sys_regs(vcpu);
  115. /* Reset PMU */
  116. kvm_pmu_vcpu_reset(vcpu);
  117. /* Default workaround setup is enabled (if supported) */
  118. if (kvm_arm_have_ssbd() == KVM_SSBD_KERNEL)
  119. vcpu->arch.workaround_flags |= VCPU_WORKAROUND_2_FLAG;
  120. /* Reset timer */
  121. return kvm_timer_vcpu_reset(vcpu);
  122. }
  123. void kvm_set_ipa_limit(void)
  124. {
  125. unsigned int ipa_max, pa_max, va_max, parange;
  126. parange = read_sanitised_ftr_reg(SYS_ID_AA64MMFR0_EL1) & 0x7;
  127. pa_max = id_aa64mmfr0_parange_to_phys_shift(parange);
  128. /* Clamp the IPA limit to the PA size supported by the kernel */
  129. ipa_max = (pa_max > PHYS_MASK_SHIFT) ? PHYS_MASK_SHIFT : pa_max;
  130. /*
  131. * Since our stage2 table is dependent on the stage1 page table code,
  132. * we must always honor the following condition:
  133. *
  134. * Number of levels in Stage1 >= Number of levels in Stage2.
  135. *
  136. * So clamp the ipa limit further down to limit the number of levels.
  137. * Since we can concatenate upto 16 tables at entry level, we could
  138. * go upto 4bits above the maximum VA addressible with the current
  139. * number of levels.
  140. */
  141. va_max = PGDIR_SHIFT + PAGE_SHIFT - 3;
  142. va_max += 4;
  143. if (va_max < ipa_max)
  144. ipa_max = va_max;
  145. /*
  146. * If the final limit is lower than the real physical address
  147. * limit of the CPUs, report the reason.
  148. */
  149. if (ipa_max < pa_max)
  150. pr_info("kvm: Limiting the IPA size due to kernel %s Address limit\n",
  151. (va_max < pa_max) ? "Virtual" : "Physical");
  152. WARN(ipa_max < KVM_PHYS_SHIFT,
  153. "KVM IPA limit (%d bit) is smaller than default size\n", ipa_max);
  154. kvm_ipa_limit = ipa_max;
  155. kvm_info("IPA Size Limit: %dbits\n", kvm_ipa_limit);
  156. }
  157. /*
  158. * Configure the VTCR_EL2 for this VM. The VTCR value is common
  159. * across all the physical CPUs on the system. We use system wide
  160. * sanitised values to fill in different fields, except for Hardware
  161. * Management of Access Flags. HA Flag is set unconditionally on
  162. * all CPUs, as it is safe to run with or without the feature and
  163. * the bit is RES0 on CPUs that don't support it.
  164. */
  165. int kvm_arm_setup_stage2(struct kvm *kvm, unsigned long type)
  166. {
  167. u64 vtcr = VTCR_EL2_FLAGS;
  168. u32 parange, phys_shift;
  169. u8 lvls;
  170. if (type & ~KVM_VM_TYPE_ARM_IPA_SIZE_MASK)
  171. return -EINVAL;
  172. phys_shift = KVM_VM_TYPE_ARM_IPA_SIZE(type);
  173. if (phys_shift) {
  174. if (phys_shift > kvm_ipa_limit ||
  175. phys_shift < 32)
  176. return -EINVAL;
  177. } else {
  178. phys_shift = KVM_PHYS_SHIFT;
  179. }
  180. parange = read_sanitised_ftr_reg(SYS_ID_AA64MMFR0_EL1) & 7;
  181. if (parange > ID_AA64MMFR0_PARANGE_MAX)
  182. parange = ID_AA64MMFR0_PARANGE_MAX;
  183. vtcr |= parange << VTCR_EL2_PS_SHIFT;
  184. vtcr |= VTCR_EL2_T0SZ(phys_shift);
  185. /*
  186. * Use a minimum 2 level page table to prevent splitting
  187. * host PMD huge pages at stage2.
  188. */
  189. lvls = stage2_pgtable_levels(phys_shift);
  190. if (lvls < 2)
  191. lvls = 2;
  192. vtcr |= VTCR_EL2_LVLS_TO_SL0(lvls);
  193. /*
  194. * Enable the Hardware Access Flag management, unconditionally
  195. * on all CPUs. The features is RES0 on CPUs without the support
  196. * and must be ignored by the CPUs.
  197. */
  198. vtcr |= VTCR_EL2_HA;
  199. /* Set the vmid bits */
  200. vtcr |= (kvm_get_vmid_bits() == 16) ?
  201. VTCR_EL2_VS_16BIT :
  202. VTCR_EL2_VS_8BIT;
  203. kvm->arch.vtcr = vtcr;
  204. return 0;
  205. }