guest.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. return 0;
  39. }
  40. static u64 core_reg_offset_from_id(u64 id)
  41. {
  42. return id & ~(KVM_REG_ARCH_MASK | KVM_REG_SIZE_MASK | KVM_REG_ARM_CORE);
  43. }
  44. static int get_core_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
  45. {
  46. /*
  47. * Because the kvm_regs structure is a mix of 32, 64 and
  48. * 128bit fields, we index it as if it was a 32bit
  49. * array. Hence below, nr_regs is the number of entries, and
  50. * off the index in the "array".
  51. */
  52. __u32 __user *uaddr = (__u32 __user *)(unsigned long)reg->addr;
  53. struct kvm_regs *regs = vcpu_gp_regs(vcpu);
  54. int nr_regs = sizeof(*regs) / sizeof(__u32);
  55. u32 off;
  56. /* Our ID is an index into the kvm_regs struct. */
  57. off = core_reg_offset_from_id(reg->id);
  58. if (off >= nr_regs ||
  59. (off + (KVM_REG_SIZE(reg->id) / sizeof(__u32))) >= nr_regs)
  60. return -ENOENT;
  61. if (copy_to_user(uaddr, ((u32 *)regs) + off, KVM_REG_SIZE(reg->id)))
  62. return -EFAULT;
  63. return 0;
  64. }
  65. static int set_core_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
  66. {
  67. __u32 __user *uaddr = (__u32 __user *)(unsigned long)reg->addr;
  68. struct kvm_regs *regs = vcpu_gp_regs(vcpu);
  69. int nr_regs = sizeof(*regs) / sizeof(__u32);
  70. __uint128_t tmp;
  71. void *valp = &tmp;
  72. u64 off;
  73. int err = 0;
  74. /* Our ID is an index into the kvm_regs struct. */
  75. off = core_reg_offset_from_id(reg->id);
  76. if (off >= nr_regs ||
  77. (off + (KVM_REG_SIZE(reg->id) / sizeof(__u32))) >= nr_regs)
  78. return -ENOENT;
  79. if (KVM_REG_SIZE(reg->id) > sizeof(tmp))
  80. return -EINVAL;
  81. if (copy_from_user(valp, uaddr, KVM_REG_SIZE(reg->id))) {
  82. err = -EFAULT;
  83. goto out;
  84. }
  85. if (off == KVM_REG_ARM_CORE_REG(regs.pstate)) {
  86. u32 mode = (*(u32 *)valp) & COMPAT_PSR_MODE_MASK;
  87. switch (mode) {
  88. case COMPAT_PSR_MODE_USR:
  89. case COMPAT_PSR_MODE_FIQ:
  90. case COMPAT_PSR_MODE_IRQ:
  91. case COMPAT_PSR_MODE_SVC:
  92. case COMPAT_PSR_MODE_ABT:
  93. case COMPAT_PSR_MODE_UND:
  94. case PSR_MODE_EL0t:
  95. case PSR_MODE_EL1t:
  96. case PSR_MODE_EL1h:
  97. break;
  98. default:
  99. err = -EINVAL;
  100. goto out;
  101. }
  102. }
  103. memcpy((u32 *)regs + off, valp, KVM_REG_SIZE(reg->id));
  104. out:
  105. return err;
  106. }
  107. int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
  108. {
  109. return -EINVAL;
  110. }
  111. int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
  112. {
  113. return -EINVAL;
  114. }
  115. static unsigned long num_core_regs(void)
  116. {
  117. return sizeof(struct kvm_regs) / sizeof(__u32);
  118. }
  119. /**
  120. * ARM64 versions of the TIMER registers, always available on arm64
  121. */
  122. #define NUM_TIMER_REGS 3
  123. static bool is_timer_reg(u64 index)
  124. {
  125. switch (index) {
  126. case KVM_REG_ARM_TIMER_CTL:
  127. case KVM_REG_ARM_TIMER_CNT:
  128. case KVM_REG_ARM_TIMER_CVAL:
  129. return true;
  130. }
  131. return false;
  132. }
  133. static int copy_timer_indices(struct kvm_vcpu *vcpu, u64 __user *uindices)
  134. {
  135. if (put_user(KVM_REG_ARM_TIMER_CTL, uindices))
  136. return -EFAULT;
  137. uindices++;
  138. if (put_user(KVM_REG_ARM_TIMER_CNT, uindices))
  139. return -EFAULT;
  140. uindices++;
  141. if (put_user(KVM_REG_ARM_TIMER_CVAL, uindices))
  142. return -EFAULT;
  143. return 0;
  144. }
  145. static int set_timer_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
  146. {
  147. void __user *uaddr = (void __user *)(long)reg->addr;
  148. u64 val;
  149. int ret;
  150. ret = copy_from_user(&val, uaddr, KVM_REG_SIZE(reg->id));
  151. if (ret != 0)
  152. return -EFAULT;
  153. return kvm_arm_timer_set_reg(vcpu, reg->id, val);
  154. }
  155. static int get_timer_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
  156. {
  157. void __user *uaddr = (void __user *)(long)reg->addr;
  158. u64 val;
  159. val = kvm_arm_timer_get_reg(vcpu, reg->id);
  160. return copy_to_user(uaddr, &val, KVM_REG_SIZE(reg->id));
  161. }
  162. /**
  163. * kvm_arm_num_regs - how many registers do we present via KVM_GET_ONE_REG
  164. *
  165. * This is for all registers.
  166. */
  167. unsigned long kvm_arm_num_regs(struct kvm_vcpu *vcpu)
  168. {
  169. return num_core_regs() + kvm_arm_num_sys_reg_descs(vcpu)
  170. + NUM_TIMER_REGS;
  171. }
  172. /**
  173. * kvm_arm_copy_reg_indices - get indices of all registers.
  174. *
  175. * We do core registers right here, then we apppend system regs.
  176. */
  177. int kvm_arm_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *uindices)
  178. {
  179. unsigned int i;
  180. const u64 core_reg = KVM_REG_ARM64 | KVM_REG_SIZE_U64 | KVM_REG_ARM_CORE;
  181. int ret;
  182. for (i = 0; i < sizeof(struct kvm_regs) / sizeof(__u32); i++) {
  183. if (put_user(core_reg | i, uindices))
  184. return -EFAULT;
  185. uindices++;
  186. }
  187. ret = copy_timer_indices(vcpu, uindices);
  188. if (ret)
  189. return ret;
  190. uindices += NUM_TIMER_REGS;
  191. return kvm_arm_copy_sys_reg_indices(vcpu, uindices);
  192. }
  193. int kvm_arm_get_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
  194. {
  195. /* We currently use nothing arch-specific in upper 32 bits */
  196. if ((reg->id & ~KVM_REG_SIZE_MASK) >> 32 != KVM_REG_ARM64 >> 32)
  197. return -EINVAL;
  198. /* Register group 16 means we want a core register. */
  199. if ((reg->id & KVM_REG_ARM_COPROC_MASK) == KVM_REG_ARM_CORE)
  200. return get_core_reg(vcpu, reg);
  201. if (is_timer_reg(reg->id))
  202. return get_timer_reg(vcpu, reg);
  203. return kvm_arm_sys_reg_get_reg(vcpu, reg);
  204. }
  205. int kvm_arm_set_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
  206. {
  207. /* We currently use nothing arch-specific in upper 32 bits */
  208. if ((reg->id & ~KVM_REG_SIZE_MASK) >> 32 != KVM_REG_ARM64 >> 32)
  209. return -EINVAL;
  210. /* Register group 16 means we set a core register. */
  211. if ((reg->id & KVM_REG_ARM_COPROC_MASK) == KVM_REG_ARM_CORE)
  212. return set_core_reg(vcpu, reg);
  213. if (is_timer_reg(reg->id))
  214. return set_timer_reg(vcpu, reg);
  215. return kvm_arm_sys_reg_set_reg(vcpu, reg);
  216. }
  217. int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
  218. struct kvm_sregs *sregs)
  219. {
  220. return -EINVAL;
  221. }
  222. int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
  223. struct kvm_sregs *sregs)
  224. {
  225. return -EINVAL;
  226. }
  227. int __attribute_const__ kvm_target_cpu(void)
  228. {
  229. unsigned long implementor = read_cpuid_implementor();
  230. unsigned long part_number = read_cpuid_part_number();
  231. switch (implementor) {
  232. case ARM_CPU_IMP_ARM:
  233. switch (part_number) {
  234. case ARM_CPU_PART_AEM_V8:
  235. return KVM_ARM_TARGET_AEM_V8;
  236. case ARM_CPU_PART_FOUNDATION:
  237. return KVM_ARM_TARGET_FOUNDATION_V8;
  238. case ARM_CPU_PART_CORTEX_A53:
  239. return KVM_ARM_TARGET_CORTEX_A53;
  240. case ARM_CPU_PART_CORTEX_A57:
  241. return KVM_ARM_TARGET_CORTEX_A57;
  242. };
  243. break;
  244. case ARM_CPU_IMP_APM:
  245. switch (part_number) {
  246. case APM_CPU_PART_POTENZA:
  247. return KVM_ARM_TARGET_XGENE_POTENZA;
  248. };
  249. break;
  250. };
  251. return -EINVAL;
  252. }
  253. int kvm_vcpu_preferred_target(struct kvm_vcpu_init *init)
  254. {
  255. int target = kvm_target_cpu();
  256. if (target < 0)
  257. return -ENODEV;
  258. memset(init, 0, sizeof(*init));
  259. /*
  260. * For now, we don't return any features.
  261. * In future, we might use features to return target
  262. * specific features available for the preferred
  263. * target type.
  264. */
  265. init->target = (__u32)target;
  266. return 0;
  267. }
  268. int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
  269. {
  270. return -EINVAL;
  271. }
  272. int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
  273. {
  274. return -EINVAL;
  275. }
  276. int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
  277. struct kvm_translation *tr)
  278. {
  279. return -EINVAL;
  280. }