guest.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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 COMPAT_PSR_MODE_USR:
  90. case COMPAT_PSR_MODE_FIQ:
  91. case COMPAT_PSR_MODE_IRQ:
  92. case COMPAT_PSR_MODE_SVC:
  93. case COMPAT_PSR_MODE_ABT:
  94. case COMPAT_PSR_MODE_UND:
  95. case PSR_MODE_EL0t:
  96. case PSR_MODE_EL1t:
  97. case PSR_MODE_EL1h:
  98. break;
  99. default:
  100. err = -EINVAL;
  101. goto out;
  102. }
  103. }
  104. memcpy((u32 *)regs + off, valp, KVM_REG_SIZE(reg->id));
  105. out:
  106. return err;
  107. }
  108. int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
  109. {
  110. return -EINVAL;
  111. }
  112. int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
  113. {
  114. return -EINVAL;
  115. }
  116. static unsigned long num_core_regs(void)
  117. {
  118. return sizeof(struct kvm_regs) / sizeof(__u32);
  119. }
  120. /**
  121. * ARM64 versions of the TIMER registers, always available on arm64
  122. */
  123. #define NUM_TIMER_REGS 3
  124. static bool is_timer_reg(u64 index)
  125. {
  126. switch (index) {
  127. case KVM_REG_ARM_TIMER_CTL:
  128. case KVM_REG_ARM_TIMER_CNT:
  129. case KVM_REG_ARM_TIMER_CVAL:
  130. return true;
  131. }
  132. return false;
  133. }
  134. static int copy_timer_indices(struct kvm_vcpu *vcpu, u64 __user *uindices)
  135. {
  136. if (put_user(KVM_REG_ARM_TIMER_CTL, uindices))
  137. return -EFAULT;
  138. uindices++;
  139. if (put_user(KVM_REG_ARM_TIMER_CNT, uindices))
  140. return -EFAULT;
  141. uindices++;
  142. if (put_user(KVM_REG_ARM_TIMER_CVAL, uindices))
  143. return -EFAULT;
  144. return 0;
  145. }
  146. static int set_timer_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
  147. {
  148. void __user *uaddr = (void __user *)(long)reg->addr;
  149. u64 val;
  150. int ret;
  151. ret = copy_from_user(&val, uaddr, KVM_REG_SIZE(reg->id));
  152. if (ret != 0)
  153. return -EFAULT;
  154. return kvm_arm_timer_set_reg(vcpu, reg->id, val);
  155. }
  156. static int get_timer_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
  157. {
  158. void __user *uaddr = (void __user *)(long)reg->addr;
  159. u64 val;
  160. val = kvm_arm_timer_get_reg(vcpu, reg->id);
  161. return copy_to_user(uaddr, &val, KVM_REG_SIZE(reg->id));
  162. }
  163. /**
  164. * kvm_arm_num_regs - how many registers do we present via KVM_GET_ONE_REG
  165. *
  166. * This is for all registers.
  167. */
  168. unsigned long kvm_arm_num_regs(struct kvm_vcpu *vcpu)
  169. {
  170. return num_core_regs() + kvm_arm_num_sys_reg_descs(vcpu)
  171. + NUM_TIMER_REGS;
  172. }
  173. /**
  174. * kvm_arm_copy_reg_indices - get indices of all registers.
  175. *
  176. * We do core registers right here, then we apppend system regs.
  177. */
  178. int kvm_arm_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *uindices)
  179. {
  180. unsigned int i;
  181. const u64 core_reg = KVM_REG_ARM64 | KVM_REG_SIZE_U64 | KVM_REG_ARM_CORE;
  182. int ret;
  183. for (i = 0; i < sizeof(struct kvm_regs) / sizeof(__u32); i++) {
  184. if (put_user(core_reg | i, uindices))
  185. return -EFAULT;
  186. uindices++;
  187. }
  188. ret = copy_timer_indices(vcpu, uindices);
  189. if (ret)
  190. return ret;
  191. uindices += NUM_TIMER_REGS;
  192. return kvm_arm_copy_sys_reg_indices(vcpu, uindices);
  193. }
  194. int kvm_arm_get_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
  195. {
  196. /* We currently use nothing arch-specific in upper 32 bits */
  197. if ((reg->id & ~KVM_REG_SIZE_MASK) >> 32 != KVM_REG_ARM64 >> 32)
  198. return -EINVAL;
  199. /* Register group 16 means we want a core register. */
  200. if ((reg->id & KVM_REG_ARM_COPROC_MASK) == KVM_REG_ARM_CORE)
  201. return get_core_reg(vcpu, reg);
  202. if (is_timer_reg(reg->id))
  203. return get_timer_reg(vcpu, reg);
  204. return kvm_arm_sys_reg_get_reg(vcpu, reg);
  205. }
  206. int kvm_arm_set_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
  207. {
  208. /* We currently use nothing arch-specific in upper 32 bits */
  209. if ((reg->id & ~KVM_REG_SIZE_MASK) >> 32 != KVM_REG_ARM64 >> 32)
  210. return -EINVAL;
  211. /* Register group 16 means we set a core register. */
  212. if ((reg->id & KVM_REG_ARM_COPROC_MASK) == KVM_REG_ARM_CORE)
  213. return set_core_reg(vcpu, reg);
  214. if (is_timer_reg(reg->id))
  215. return set_timer_reg(vcpu, reg);
  216. return kvm_arm_sys_reg_set_reg(vcpu, reg);
  217. }
  218. int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
  219. struct kvm_sregs *sregs)
  220. {
  221. return -EINVAL;
  222. }
  223. int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
  224. struct kvm_sregs *sregs)
  225. {
  226. return -EINVAL;
  227. }
  228. int __attribute_const__ kvm_target_cpu(void)
  229. {
  230. unsigned long implementor = read_cpuid_implementor();
  231. unsigned long part_number = read_cpuid_part_number();
  232. switch (implementor) {
  233. case ARM_CPU_IMP_ARM:
  234. switch (part_number) {
  235. case ARM_CPU_PART_AEM_V8:
  236. return KVM_ARM_TARGET_AEM_V8;
  237. case ARM_CPU_PART_FOUNDATION:
  238. return KVM_ARM_TARGET_FOUNDATION_V8;
  239. case ARM_CPU_PART_CORTEX_A53:
  240. return KVM_ARM_TARGET_CORTEX_A53;
  241. case ARM_CPU_PART_CORTEX_A57:
  242. return KVM_ARM_TARGET_CORTEX_A57;
  243. };
  244. break;
  245. case ARM_CPU_IMP_APM:
  246. switch (part_number) {
  247. case APM_CPU_PART_POTENZA:
  248. return KVM_ARM_TARGET_XGENE_POTENZA;
  249. };
  250. break;
  251. };
  252. return -EINVAL;
  253. }
  254. int kvm_vcpu_set_target(struct kvm_vcpu *vcpu,
  255. const struct kvm_vcpu_init *init)
  256. {
  257. unsigned int i;
  258. int phys_target = kvm_target_cpu();
  259. if (init->target != phys_target)
  260. return -EINVAL;
  261. vcpu->arch.target = phys_target;
  262. bitmap_zero(vcpu->arch.features, KVM_VCPU_MAX_FEATURES);
  263. /* -ENOENT for unknown features, -EINVAL for invalid combinations. */
  264. for (i = 0; i < sizeof(init->features) * 8; i++) {
  265. if (init->features[i / 32] & (1 << (i % 32))) {
  266. if (i >= KVM_VCPU_MAX_FEATURES)
  267. return -ENOENT;
  268. set_bit(i, vcpu->arch.features);
  269. }
  270. }
  271. /* Now we know what it is, we can reset it. */
  272. return kvm_reset_vcpu(vcpu);
  273. }
  274. int kvm_vcpu_preferred_target(struct kvm_vcpu_init *init)
  275. {
  276. int target = kvm_target_cpu();
  277. if (target < 0)
  278. return -ENODEV;
  279. memset(init, 0, sizeof(*init));
  280. /*
  281. * For now, we don't return any features.
  282. * In future, we might use features to return target
  283. * specific features available for the preferred
  284. * target type.
  285. */
  286. init->target = (__u32)target;
  287. return 0;
  288. }
  289. int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
  290. {
  291. return -EINVAL;
  292. }
  293. int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
  294. {
  295. return -EINVAL;
  296. }
  297. int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
  298. struct kvm_translation *tr)
  299. {
  300. return -EINVAL;
  301. }