guest.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /*
  2. * Copyright (C) 2012 - Virtual Open Systems and Columbia University
  3. * Author: Christoffer Dall <c.dall@virtualopensystems.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License, version 2, as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. */
  18. #include <linux/errno.h>
  19. #include <linux/err.h>
  20. #include <linux/kvm_host.h>
  21. #include <linux/module.h>
  22. #include <linux/vmalloc.h>
  23. #include <linux/fs.h>
  24. #include <asm/cputype.h>
  25. #include <asm/uaccess.h>
  26. #include <asm/kvm.h>
  27. #include <asm/kvm_asm.h>
  28. #include <asm/kvm_emulate.h>
  29. #include <asm/kvm_coproc.h>
  30. #define VM_STAT(x) { #x, offsetof(struct kvm, stat.x), KVM_STAT_VM }
  31. #define VCPU_STAT(x) { #x, offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU }
  32. struct kvm_stats_debugfs_item debugfs_entries[] = {
  33. { NULL }
  34. };
  35. int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
  36. {
  37. vcpu->arch.hcr = HCR_GUEST_MASK;
  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. u32 __user *uaddr = (u32 __user *)(long)reg->addr;
  47. struct kvm_regs *regs = &vcpu->arch.regs;
  48. u64 off;
  49. if (KVM_REG_SIZE(reg->id) != 4)
  50. return -ENOENT;
  51. /* Our ID is an index into the kvm_regs struct. */
  52. off = core_reg_offset_from_id(reg->id);
  53. if (off >= sizeof(*regs) / KVM_REG_SIZE(reg->id))
  54. return -ENOENT;
  55. return put_user(((u32 *)regs)[off], uaddr);
  56. }
  57. static int set_core_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
  58. {
  59. u32 __user *uaddr = (u32 __user *)(long)reg->addr;
  60. struct kvm_regs *regs = &vcpu->arch.regs;
  61. u64 off, val;
  62. if (KVM_REG_SIZE(reg->id) != 4)
  63. return -ENOENT;
  64. /* Our ID is an index into the kvm_regs struct. */
  65. off = core_reg_offset_from_id(reg->id);
  66. if (off >= sizeof(*regs) / KVM_REG_SIZE(reg->id))
  67. return -ENOENT;
  68. if (get_user(val, uaddr) != 0)
  69. return -EFAULT;
  70. if (off == KVM_REG_ARM_CORE_REG(usr_regs.ARM_cpsr)) {
  71. unsigned long mode = val & MODE_MASK;
  72. switch (mode) {
  73. case USR_MODE:
  74. case FIQ_MODE:
  75. case IRQ_MODE:
  76. case SVC_MODE:
  77. case ABT_MODE:
  78. case UND_MODE:
  79. break;
  80. default:
  81. return -EINVAL;
  82. }
  83. }
  84. ((u32 *)regs)[off] = val;
  85. return 0;
  86. }
  87. int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
  88. {
  89. return -EINVAL;
  90. }
  91. int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
  92. {
  93. return -EINVAL;
  94. }
  95. #ifndef CONFIG_KVM_ARM_TIMER
  96. #define NUM_TIMER_REGS 0
  97. static int copy_timer_indices(struct kvm_vcpu *vcpu, u64 __user *uindices)
  98. {
  99. return 0;
  100. }
  101. static bool is_timer_reg(u64 index)
  102. {
  103. return false;
  104. }
  105. int kvm_arm_timer_set_reg(struct kvm_vcpu *vcpu, u64 regid, u64 value)
  106. {
  107. return 0;
  108. }
  109. u64 kvm_arm_timer_get_reg(struct kvm_vcpu *vcpu, u64 regid)
  110. {
  111. return 0;
  112. }
  113. #else
  114. #define NUM_TIMER_REGS 3
  115. static bool is_timer_reg(u64 index)
  116. {
  117. switch (index) {
  118. case KVM_REG_ARM_TIMER_CTL:
  119. case KVM_REG_ARM_TIMER_CNT:
  120. case KVM_REG_ARM_TIMER_CVAL:
  121. return true;
  122. }
  123. return false;
  124. }
  125. static int copy_timer_indices(struct kvm_vcpu *vcpu, u64 __user *uindices)
  126. {
  127. if (put_user(KVM_REG_ARM_TIMER_CTL, uindices))
  128. return -EFAULT;
  129. uindices++;
  130. if (put_user(KVM_REG_ARM_TIMER_CNT, uindices))
  131. return -EFAULT;
  132. uindices++;
  133. if (put_user(KVM_REG_ARM_TIMER_CVAL, uindices))
  134. return -EFAULT;
  135. return 0;
  136. }
  137. #endif
  138. static int set_timer_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
  139. {
  140. void __user *uaddr = (void __user *)(long)reg->addr;
  141. u64 val;
  142. int ret;
  143. ret = copy_from_user(&val, uaddr, KVM_REG_SIZE(reg->id));
  144. if (ret != 0)
  145. return ret;
  146. return kvm_arm_timer_set_reg(vcpu, reg->id, val);
  147. }
  148. static int get_timer_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
  149. {
  150. void __user *uaddr = (void __user *)(long)reg->addr;
  151. u64 val;
  152. val = kvm_arm_timer_get_reg(vcpu, reg->id);
  153. return copy_to_user(uaddr, &val, KVM_REG_SIZE(reg->id));
  154. }
  155. static unsigned long num_core_regs(void)
  156. {
  157. return sizeof(struct kvm_regs) / sizeof(u32);
  158. }
  159. /**
  160. * kvm_arm_num_regs - how many registers do we present via KVM_GET_ONE_REG
  161. *
  162. * This is for all registers.
  163. */
  164. unsigned long kvm_arm_num_regs(struct kvm_vcpu *vcpu)
  165. {
  166. return num_core_regs() + kvm_arm_num_coproc_regs(vcpu)
  167. + NUM_TIMER_REGS;
  168. }
  169. /**
  170. * kvm_arm_copy_reg_indices - get indices of all registers.
  171. *
  172. * We do core registers right here, then we apppend coproc regs.
  173. */
  174. int kvm_arm_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *uindices)
  175. {
  176. unsigned int i;
  177. const u64 core_reg = KVM_REG_ARM | KVM_REG_SIZE_U32 | KVM_REG_ARM_CORE;
  178. int ret;
  179. for (i = 0; i < sizeof(struct kvm_regs)/sizeof(u32); i++) {
  180. if (put_user(core_reg | i, uindices))
  181. return -EFAULT;
  182. uindices++;
  183. }
  184. ret = copy_timer_indices(vcpu, uindices);
  185. if (ret)
  186. return ret;
  187. uindices += NUM_TIMER_REGS;
  188. return kvm_arm_copy_coproc_indices(vcpu, uindices);
  189. }
  190. int kvm_arm_get_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
  191. {
  192. /* We currently use nothing arch-specific in upper 32 bits */
  193. if ((reg->id & ~KVM_REG_SIZE_MASK) >> 32 != KVM_REG_ARM >> 32)
  194. return -EINVAL;
  195. /* Register group 16 means we want a core register. */
  196. if ((reg->id & KVM_REG_ARM_COPROC_MASK) == KVM_REG_ARM_CORE)
  197. return get_core_reg(vcpu, reg);
  198. if (is_timer_reg(reg->id))
  199. return get_timer_reg(vcpu, reg);
  200. return kvm_arm_coproc_get_reg(vcpu, reg);
  201. }
  202. int kvm_arm_set_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
  203. {
  204. /* We currently use nothing arch-specific in upper 32 bits */
  205. if ((reg->id & ~KVM_REG_SIZE_MASK) >> 32 != KVM_REG_ARM >> 32)
  206. return -EINVAL;
  207. /* Register group 16 means we set a core register. */
  208. if ((reg->id & KVM_REG_ARM_COPROC_MASK) == KVM_REG_ARM_CORE)
  209. return set_core_reg(vcpu, reg);
  210. if (is_timer_reg(reg->id))
  211. return set_timer_reg(vcpu, reg);
  212. return kvm_arm_coproc_set_reg(vcpu, reg);
  213. }
  214. int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
  215. struct kvm_sregs *sregs)
  216. {
  217. return -EINVAL;
  218. }
  219. int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
  220. struct kvm_sregs *sregs)
  221. {
  222. return -EINVAL;
  223. }
  224. int __attribute_const__ kvm_target_cpu(void)
  225. {
  226. unsigned long implementor = read_cpuid_implementor();
  227. unsigned long part_number = read_cpuid_part_number();
  228. if (implementor != ARM_CPU_IMP_ARM)
  229. return -EINVAL;
  230. switch (part_number) {
  231. case ARM_CPU_PART_CORTEX_A7:
  232. return KVM_ARM_TARGET_CORTEX_A7;
  233. case ARM_CPU_PART_CORTEX_A15:
  234. return KVM_ARM_TARGET_CORTEX_A15;
  235. default:
  236. return -EINVAL;
  237. }
  238. }
  239. int kvm_vcpu_set_target(struct kvm_vcpu *vcpu,
  240. const struct kvm_vcpu_init *init)
  241. {
  242. unsigned int i;
  243. /* We can only cope with guest==host and only on A15/A7 (for now). */
  244. if (init->target != kvm_target_cpu())
  245. return -EINVAL;
  246. vcpu->arch.target = init->target;
  247. bitmap_zero(vcpu->arch.features, KVM_VCPU_MAX_FEATURES);
  248. /* -ENOENT for unknown features, -EINVAL for invalid combinations. */
  249. for (i = 0; i < sizeof(init->features) * 8; i++) {
  250. if (test_bit(i, (void *)init->features)) {
  251. if (i >= KVM_VCPU_MAX_FEATURES)
  252. return -ENOENT;
  253. set_bit(i, vcpu->arch.features);
  254. }
  255. }
  256. /* Now we know what it is, we can reset it. */
  257. return kvm_reset_vcpu(vcpu);
  258. }
  259. int kvm_vcpu_preferred_target(struct kvm_vcpu_init *init)
  260. {
  261. int target = kvm_target_cpu();
  262. if (target < 0)
  263. return -ENODEV;
  264. memset(init, 0, sizeof(*init));
  265. /*
  266. * For now, we don't return any features.
  267. * In future, we might use features to return target
  268. * specific features available for the preferred
  269. * target type.
  270. */
  271. init->target = (__u32)target;
  272. return 0;
  273. }
  274. int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
  275. {
  276. return -EINVAL;
  277. }
  278. int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
  279. {
  280. return -EINVAL;
  281. }
  282. int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
  283. struct kvm_translation *tr)
  284. {
  285. return -EINVAL;
  286. }