guest.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. * Copyright (C) 2012,2013 - ARM Ltd
  3. * Author: Marc Zyngier <marc.zyngier@arm.com>
  4. *
  5. * Derived from arch/arm/kvm/guest.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/err.h>
  23. #include <linux/kvm_host.h>
  24. #include <linux/module.h>
  25. #include <linux/vmalloc.h>
  26. #include <linux/fs.h>
  27. #include <asm/cputype.h>
  28. #include <asm/uaccess.h>
  29. #include <asm/kvm.h>
  30. #include <asm/kvm_asm.h>
  31. #include <asm/kvm_emulate.h>
  32. #include <asm/kvm_coproc.h>
  33. struct kvm_stats_debugfs_item debugfs_entries[] = {
  34. { NULL }
  35. };
  36. int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
  37. {
  38. vcpu->arch.hcr_el2 = HCR_GUEST_FLAGS;
  39. return 0;
  40. }
  41. static u64 core_reg_offset_from_id(u64 id)
  42. {
  43. return id & ~(KVM_REG_ARCH_MASK | KVM_REG_SIZE_MASK | KVM_REG_ARM_CORE);
  44. }
  45. static int get_core_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
  46. {
  47. /*
  48. * Because the kvm_regs structure is a mix of 32, 64 and
  49. * 128bit fields, we index it as if it was a 32bit
  50. * array. Hence below, nr_regs is the number of entries, and
  51. * off the index in the "array".
  52. */
  53. __u32 __user *uaddr = (__u32 __user *)(unsigned long)reg->addr;
  54. struct kvm_regs *regs = vcpu_gp_regs(vcpu);
  55. int nr_regs = sizeof(*regs) / sizeof(__u32);
  56. u32 off;
  57. /* Our ID is an index into the kvm_regs struct. */
  58. off = core_reg_offset_from_id(reg->id);
  59. if (off >= nr_regs ||
  60. (off + (KVM_REG_SIZE(reg->id) / sizeof(__u32))) >= nr_regs)
  61. return -ENOENT;
  62. if (copy_to_user(uaddr, ((u32 *)regs) + off, KVM_REG_SIZE(reg->id)))
  63. return -EFAULT;
  64. return 0;
  65. }
  66. static int set_core_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
  67. {
  68. __u32 __user *uaddr = (__u32 __user *)(unsigned long)reg->addr;
  69. struct kvm_regs *regs = vcpu_gp_regs(vcpu);
  70. int nr_regs = sizeof(*regs) / sizeof(__u32);
  71. __uint128_t tmp;
  72. void *valp = &tmp;
  73. u64 off;
  74. int err = 0;
  75. /* Our ID is an index into the kvm_regs struct. */
  76. off = core_reg_offset_from_id(reg->id);
  77. if (off >= nr_regs ||
  78. (off + (KVM_REG_SIZE(reg->id) / sizeof(__u32))) >= nr_regs)
  79. return -ENOENT;
  80. if (KVM_REG_SIZE(reg->id) > sizeof(tmp))
  81. return -EINVAL;
  82. if (copy_from_user(valp, uaddr, KVM_REG_SIZE(reg->id))) {
  83. err = -EFAULT;
  84. goto out;
  85. }
  86. if (off == KVM_REG_ARM_CORE_REG(regs.pstate)) {
  87. u32 mode = (*(u32 *)valp) & COMPAT_PSR_MODE_MASK;
  88. switch (mode) {
  89. case PSR_MODE_EL0t:
  90. case PSR_MODE_EL1t:
  91. case PSR_MODE_EL1h:
  92. break;
  93. default:
  94. err = -EINVAL;
  95. goto out;
  96. }
  97. }
  98. memcpy((u32 *)regs + off, valp, KVM_REG_SIZE(reg->id));
  99. out:
  100. return err;
  101. }
  102. int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
  103. {
  104. return -EINVAL;
  105. }
  106. int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
  107. {
  108. return -EINVAL;
  109. }
  110. static unsigned long num_core_regs(void)
  111. {
  112. return sizeof(struct kvm_regs) / sizeof(__u32);
  113. }
  114. /**
  115. * kvm_arm_num_regs - how many registers do we present via KVM_GET_ONE_REG
  116. *
  117. * This is for all registers.
  118. */
  119. unsigned long kvm_arm_num_regs(struct kvm_vcpu *vcpu)
  120. {
  121. return num_core_regs() + kvm_arm_num_sys_reg_descs(vcpu);
  122. }
  123. /**
  124. * kvm_arm_copy_reg_indices - get indices of all registers.
  125. *
  126. * We do core registers right here, then we apppend system regs.
  127. */
  128. int kvm_arm_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *uindices)
  129. {
  130. unsigned int i;
  131. const u64 core_reg = KVM_REG_ARM64 | KVM_REG_SIZE_U64 | KVM_REG_ARM_CORE;
  132. for (i = 0; i < sizeof(struct kvm_regs) / sizeof(__u32); i++) {
  133. if (put_user(core_reg | i, uindices))
  134. return -EFAULT;
  135. uindices++;
  136. }
  137. return kvm_arm_copy_sys_reg_indices(vcpu, uindices);
  138. }
  139. int kvm_arm_get_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
  140. {
  141. /* We currently use nothing arch-specific in upper 32 bits */
  142. if ((reg->id & ~KVM_REG_SIZE_MASK) >> 32 != KVM_REG_ARM64 >> 32)
  143. return -EINVAL;
  144. /* Register group 16 means we want a core register. */
  145. if ((reg->id & KVM_REG_ARM_COPROC_MASK) == KVM_REG_ARM_CORE)
  146. return get_core_reg(vcpu, reg);
  147. return kvm_arm_sys_reg_get_reg(vcpu, reg);
  148. }
  149. int kvm_arm_set_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
  150. {
  151. /* We currently use nothing arch-specific in upper 32 bits */
  152. if ((reg->id & ~KVM_REG_SIZE_MASK) >> 32 != KVM_REG_ARM64 >> 32)
  153. return -EINVAL;
  154. /* Register group 16 means we set a core register. */
  155. if ((reg->id & KVM_REG_ARM_COPROC_MASK) == KVM_REG_ARM_CORE)
  156. return set_core_reg(vcpu, reg);
  157. return kvm_arm_sys_reg_set_reg(vcpu, reg);
  158. }
  159. int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
  160. struct kvm_sregs *sregs)
  161. {
  162. return -EINVAL;
  163. }
  164. int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
  165. struct kvm_sregs *sregs)
  166. {
  167. return -EINVAL;
  168. }
  169. int __attribute_const__ kvm_target_cpu(void)
  170. {
  171. unsigned long implementor = read_cpuid_implementor();
  172. unsigned long part_number = read_cpuid_part_number();
  173. if (implementor != ARM_CPU_IMP_ARM)
  174. return -EINVAL;
  175. switch (part_number) {
  176. case ARM_CPU_PART_AEM_V8:
  177. return KVM_ARM_TARGET_AEM_V8;
  178. case ARM_CPU_PART_FOUNDATION:
  179. return KVM_ARM_TARGET_FOUNDATION_V8;
  180. case ARM_CPU_PART_CORTEX_A57:
  181. /* Currently handled by the generic backend */
  182. return KVM_ARM_TARGET_CORTEX_A57;
  183. default:
  184. return -EINVAL;
  185. }
  186. }
  187. int kvm_vcpu_set_target(struct kvm_vcpu *vcpu,
  188. const struct kvm_vcpu_init *init)
  189. {
  190. unsigned int i;
  191. int phys_target = kvm_target_cpu();
  192. if (init->target != phys_target)
  193. return -EINVAL;
  194. vcpu->arch.target = phys_target;
  195. bitmap_zero(vcpu->arch.features, KVM_VCPU_MAX_FEATURES);
  196. /* -ENOENT for unknown features, -EINVAL for invalid combinations. */
  197. for (i = 0; i < sizeof(init->features) * 8; i++) {
  198. if (init->features[i / 32] & (1 << (i % 32))) {
  199. if (i >= KVM_VCPU_MAX_FEATURES)
  200. return -ENOENT;
  201. set_bit(i, vcpu->arch.features);
  202. }
  203. }
  204. /* Now we know what it is, we can reset it. */
  205. return kvm_reset_vcpu(vcpu);
  206. }
  207. int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
  208. {
  209. return -EINVAL;
  210. }
  211. int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
  212. {
  213. return -EINVAL;
  214. }
  215. int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
  216. struct kvm_translation *tr)
  217. {
  218. return -EINVAL;
  219. }