reset.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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_coproc.h>
  30. /*
  31. * ARMv8 Reset Values
  32. */
  33. static const struct kvm_regs default_regs_reset = {
  34. .regs.pstate = (PSR_MODE_EL1h | PSR_A_BIT | PSR_I_BIT |
  35. PSR_F_BIT | PSR_D_BIT),
  36. };
  37. static const struct kvm_regs default_regs_reset32 = {
  38. .regs.pstate = (COMPAT_PSR_MODE_SVC | COMPAT_PSR_A_BIT |
  39. COMPAT_PSR_I_BIT | COMPAT_PSR_F_BIT),
  40. };
  41. static const struct kvm_irq_level default_vtimer_irq = {
  42. .irq = 27,
  43. .level = 1,
  44. };
  45. static bool cpu_has_32bit_el1(void)
  46. {
  47. u64 pfr0;
  48. pfr0 = read_system_reg(SYS_ID_AA64PFR0_EL1);
  49. return !!(pfr0 & 0x20);
  50. }
  51. /**
  52. * kvm_arch_dev_ioctl_check_extension
  53. *
  54. * We currently assume that the number of HW registers is uniform
  55. * across all CPUs (see cpuinfo_sanity_check).
  56. */
  57. int kvm_arch_dev_ioctl_check_extension(long ext)
  58. {
  59. int r;
  60. switch (ext) {
  61. case KVM_CAP_ARM_EL1_32BIT:
  62. r = cpu_has_32bit_el1();
  63. break;
  64. case KVM_CAP_GUEST_DEBUG_HW_BPS:
  65. r = get_num_brps();
  66. break;
  67. case KVM_CAP_GUEST_DEBUG_HW_WPS:
  68. r = get_num_wrps();
  69. break;
  70. case KVM_CAP_SET_GUEST_DEBUG:
  71. r = 1;
  72. break;
  73. default:
  74. r = 0;
  75. }
  76. return r;
  77. }
  78. /**
  79. * kvm_reset_vcpu - sets core registers and sys_regs to reset value
  80. * @vcpu: The VCPU pointer
  81. *
  82. * This function finds the right table above and sets the registers on
  83. * the virtual CPU struct to their architectually defined reset
  84. * values.
  85. */
  86. int kvm_reset_vcpu(struct kvm_vcpu *vcpu)
  87. {
  88. const struct kvm_irq_level *cpu_vtimer_irq;
  89. const struct kvm_regs *cpu_reset;
  90. switch (vcpu->arch.target) {
  91. default:
  92. if (test_bit(KVM_ARM_VCPU_EL1_32BIT, vcpu->arch.features)) {
  93. if (!cpu_has_32bit_el1())
  94. return -EINVAL;
  95. cpu_reset = &default_regs_reset32;
  96. } else {
  97. cpu_reset = &default_regs_reset;
  98. }
  99. cpu_vtimer_irq = &default_vtimer_irq;
  100. break;
  101. }
  102. /* Reset core registers */
  103. memcpy(vcpu_gp_regs(vcpu), cpu_reset, sizeof(*cpu_reset));
  104. /* Reset system registers */
  105. kvm_reset_sys_regs(vcpu);
  106. /* Reset timer */
  107. return kvm_timer_vcpu_reset(vcpu, cpu_vtimer_irq);
  108. }