reset.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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/cputype.h>
  27. #include <asm/ptrace.h>
  28. #include <asm/kvm_arm.h>
  29. #include <asm/kvm_asm.h>
  30. #include <asm/kvm_coproc.h>
  31. #include <asm/kvm_emulate.h>
  32. #include <asm/kvm_mmu.h>
  33. /*
  34. * ARMv8 Reset Values
  35. */
  36. static const struct kvm_regs default_regs_reset = {
  37. .regs.pstate = (PSR_MODE_EL1h | PSR_A_BIT | PSR_I_BIT |
  38. PSR_F_BIT | PSR_D_BIT),
  39. };
  40. static const struct kvm_regs default_regs_reset32 = {
  41. .regs.pstate = (PSR_AA32_MODE_SVC | PSR_AA32_A_BIT |
  42. PSR_AA32_I_BIT | PSR_AA32_F_BIT),
  43. };
  44. static bool cpu_has_32bit_el1(void)
  45. {
  46. u64 pfr0;
  47. pfr0 = read_sanitised_ftr_reg(SYS_ID_AA64PFR0_EL1);
  48. return !!(pfr0 & 0x20);
  49. }
  50. /**
  51. * kvm_arch_dev_ioctl_check_extension
  52. *
  53. * We currently assume that the number of HW registers is uniform
  54. * across all CPUs (see cpuinfo_sanity_check).
  55. */
  56. int kvm_arch_dev_ioctl_check_extension(struct kvm *kvm, long ext)
  57. {
  58. int r;
  59. switch (ext) {
  60. case KVM_CAP_ARM_EL1_32BIT:
  61. r = cpu_has_32bit_el1();
  62. break;
  63. case KVM_CAP_GUEST_DEBUG_HW_BPS:
  64. r = get_num_brps();
  65. break;
  66. case KVM_CAP_GUEST_DEBUG_HW_WPS:
  67. r = get_num_wrps();
  68. break;
  69. case KVM_CAP_ARM_PMU_V3:
  70. r = kvm_arm_support_pmu_v3();
  71. break;
  72. case KVM_CAP_ARM_INJECT_SERROR_ESR:
  73. r = cpus_have_const_cap(ARM64_HAS_RAS_EXTN);
  74. break;
  75. case KVM_CAP_SET_GUEST_DEBUG:
  76. case KVM_CAP_VCPU_ATTRIBUTES:
  77. case KVM_CAP_VCPU_EVENTS:
  78. r = 1;
  79. break;
  80. default:
  81. r = 0;
  82. }
  83. return r;
  84. }
  85. /**
  86. * kvm_reset_vcpu - sets core registers and sys_regs to reset value
  87. * @vcpu: The VCPU pointer
  88. *
  89. * This function finds the right table above and sets the registers on
  90. * the virtual CPU struct to their architecturally defined reset
  91. * values.
  92. *
  93. * Note: This function can be called from two paths: The KVM_ARM_VCPU_INIT
  94. * ioctl or as part of handling a request issued by another VCPU in the PSCI
  95. * handling code. In the first case, the VCPU will not be loaded, and in the
  96. * second case the VCPU will be loaded. Because this function operates purely
  97. * on the memory-backed valus of system registers, we want to do a full put if
  98. * we were loaded (handling a request) and load the values back at the end of
  99. * the function. Otherwise we leave the state alone. In both cases, we
  100. * disable preemption around the vcpu reset as we would otherwise race with
  101. * preempt notifiers which also call put/load.
  102. */
  103. int kvm_reset_vcpu(struct kvm_vcpu *vcpu)
  104. {
  105. const struct kvm_regs *cpu_reset;
  106. int ret = -EINVAL;
  107. bool loaded;
  108. /* Reset PMU outside of the non-preemptible section */
  109. kvm_pmu_vcpu_reset(vcpu);
  110. preempt_disable();
  111. loaded = (vcpu->cpu != -1);
  112. if (loaded)
  113. kvm_arch_vcpu_put(vcpu);
  114. switch (vcpu->arch.target) {
  115. default:
  116. if (test_bit(KVM_ARM_VCPU_EL1_32BIT, vcpu->arch.features)) {
  117. if (!cpu_has_32bit_el1())
  118. goto out;
  119. cpu_reset = &default_regs_reset32;
  120. } else {
  121. cpu_reset = &default_regs_reset;
  122. }
  123. break;
  124. }
  125. /* Reset core registers */
  126. memcpy(vcpu_gp_regs(vcpu), cpu_reset, sizeof(*cpu_reset));
  127. /* Reset system registers */
  128. kvm_reset_sys_regs(vcpu);
  129. /*
  130. * Additional reset state handling that PSCI may have imposed on us.
  131. * Must be done after all the sys_reg reset.
  132. */
  133. if (vcpu->arch.reset_state.reset) {
  134. unsigned long target_pc = vcpu->arch.reset_state.pc;
  135. /* Gracefully handle Thumb2 entry point */
  136. if (vcpu_mode_is_32bit(vcpu) && (target_pc & 1)) {
  137. target_pc &= ~1UL;
  138. vcpu_set_thumb(vcpu);
  139. }
  140. /* Propagate caller endianness */
  141. if (vcpu->arch.reset_state.be)
  142. kvm_vcpu_set_be(vcpu);
  143. *vcpu_pc(vcpu) = target_pc;
  144. vcpu_set_reg(vcpu, 0, vcpu->arch.reset_state.r0);
  145. vcpu->arch.reset_state.reset = false;
  146. }
  147. /* Default workaround setup is enabled (if supported) */
  148. if (kvm_arm_have_ssbd() == KVM_SSBD_KERNEL)
  149. vcpu->arch.workaround_flags |= VCPU_WORKAROUND_2_FLAG;
  150. /* Reset timer */
  151. ret = kvm_timer_vcpu_reset(vcpu);
  152. out:
  153. if (loaded)
  154. kvm_arch_vcpu_load(vcpu, smp_processor_id());
  155. preempt_enable();
  156. return ret;
  157. }