guest.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. #else
  106. #define NUM_TIMER_REGS 3
  107. static bool is_timer_reg(u64 index)
  108. {
  109. switch (index) {
  110. case KVM_REG_ARM_TIMER_CTL:
  111. case KVM_REG_ARM_TIMER_CNT:
  112. case KVM_REG_ARM_TIMER_CVAL:
  113. return true;
  114. }
  115. return false;
  116. }
  117. static int copy_timer_indices(struct kvm_vcpu *vcpu, u64 __user *uindices)
  118. {
  119. if (put_user(KVM_REG_ARM_TIMER_CTL, uindices))
  120. return -EFAULT;
  121. uindices++;
  122. if (put_user(KVM_REG_ARM_TIMER_CNT, uindices))
  123. return -EFAULT;
  124. uindices++;
  125. if (put_user(KVM_REG_ARM_TIMER_CVAL, uindices))
  126. return -EFAULT;
  127. return 0;
  128. }
  129. #endif
  130. static int set_timer_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
  131. {
  132. void __user *uaddr = (void __user *)(long)reg->addr;
  133. u64 val;
  134. int ret;
  135. ret = copy_from_user(&val, uaddr, KVM_REG_SIZE(reg->id));
  136. if (ret != 0)
  137. return -EFAULT;
  138. return kvm_arm_timer_set_reg(vcpu, reg->id, val);
  139. }
  140. static int get_timer_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
  141. {
  142. void __user *uaddr = (void __user *)(long)reg->addr;
  143. u64 val;
  144. val = kvm_arm_timer_get_reg(vcpu, reg->id);
  145. return copy_to_user(uaddr, &val, KVM_REG_SIZE(reg->id));
  146. }
  147. static unsigned long num_core_regs(void)
  148. {
  149. return sizeof(struct kvm_regs) / sizeof(u32);
  150. }
  151. /**
  152. * kvm_arm_num_regs - how many registers do we present via KVM_GET_ONE_REG
  153. *
  154. * This is for all registers.
  155. */
  156. unsigned long kvm_arm_num_regs(struct kvm_vcpu *vcpu)
  157. {
  158. return num_core_regs() + kvm_arm_num_coproc_regs(vcpu)
  159. + NUM_TIMER_REGS;
  160. }
  161. /**
  162. * kvm_arm_copy_reg_indices - get indices of all registers.
  163. *
  164. * We do core registers right here, then we apppend coproc regs.
  165. */
  166. int kvm_arm_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *uindices)
  167. {
  168. unsigned int i;
  169. const u64 core_reg = KVM_REG_ARM | KVM_REG_SIZE_U32 | KVM_REG_ARM_CORE;
  170. int ret;
  171. for (i = 0; i < sizeof(struct kvm_regs)/sizeof(u32); i++) {
  172. if (put_user(core_reg | i, uindices))
  173. return -EFAULT;
  174. uindices++;
  175. }
  176. ret = copy_timer_indices(vcpu, uindices);
  177. if (ret)
  178. return ret;
  179. uindices += NUM_TIMER_REGS;
  180. return kvm_arm_copy_coproc_indices(vcpu, uindices);
  181. }
  182. int kvm_arm_get_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
  183. {
  184. /* We currently use nothing arch-specific in upper 32 bits */
  185. if ((reg->id & ~KVM_REG_SIZE_MASK) >> 32 != KVM_REG_ARM >> 32)
  186. return -EINVAL;
  187. /* Register group 16 means we want a core register. */
  188. if ((reg->id & KVM_REG_ARM_COPROC_MASK) == KVM_REG_ARM_CORE)
  189. return get_core_reg(vcpu, reg);
  190. if (is_timer_reg(reg->id))
  191. return get_timer_reg(vcpu, reg);
  192. return kvm_arm_coproc_get_reg(vcpu, reg);
  193. }
  194. int kvm_arm_set_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_ARM >> 32)
  198. return -EINVAL;
  199. /* Register group 16 means we set a core register. */
  200. if ((reg->id & KVM_REG_ARM_COPROC_MASK) == KVM_REG_ARM_CORE)
  201. return set_core_reg(vcpu, reg);
  202. if (is_timer_reg(reg->id))
  203. return set_timer_reg(vcpu, reg);
  204. return kvm_arm_coproc_set_reg(vcpu, reg);
  205. }
  206. int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
  207. struct kvm_sregs *sregs)
  208. {
  209. return -EINVAL;
  210. }
  211. int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
  212. struct kvm_sregs *sregs)
  213. {
  214. return -EINVAL;
  215. }
  216. int __attribute_const__ kvm_target_cpu(void)
  217. {
  218. switch (read_cpuid_part()) {
  219. case ARM_CPU_PART_CORTEX_A7:
  220. return KVM_ARM_TARGET_CORTEX_A7;
  221. case ARM_CPU_PART_CORTEX_A15:
  222. return KVM_ARM_TARGET_CORTEX_A15;
  223. default:
  224. return -EINVAL;
  225. }
  226. }
  227. int kvm_vcpu_set_target(struct kvm_vcpu *vcpu,
  228. const struct kvm_vcpu_init *init)
  229. {
  230. unsigned int i;
  231. /* We can only cope with guest==host and only on A15/A7 (for now). */
  232. if (init->target != kvm_target_cpu())
  233. return -EINVAL;
  234. vcpu->arch.target = init->target;
  235. bitmap_zero(vcpu->arch.features, KVM_VCPU_MAX_FEATURES);
  236. /* -ENOENT for unknown features, -EINVAL for invalid combinations. */
  237. for (i = 0; i < sizeof(init->features) * 8; i++) {
  238. if (test_bit(i, (void *)init->features)) {
  239. if (i >= KVM_VCPU_MAX_FEATURES)
  240. return -ENOENT;
  241. set_bit(i, vcpu->arch.features);
  242. }
  243. }
  244. /* Now we know what it is, we can reset it. */
  245. return kvm_reset_vcpu(vcpu);
  246. }
  247. int kvm_vcpu_preferred_target(struct kvm_vcpu_init *init)
  248. {
  249. int target = kvm_target_cpu();
  250. if (target < 0)
  251. return -ENODEV;
  252. memset(init, 0, sizeof(*init));
  253. /*
  254. * For now, we don't return any features.
  255. * In future, we might use features to return target
  256. * specific features available for the preferred
  257. * target type.
  258. */
  259. init->target = (__u32)target;
  260. return 0;
  261. }
  262. int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
  263. {
  264. return -EINVAL;
  265. }
  266. int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
  267. {
  268. return -EINVAL;
  269. }
  270. int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
  271. struct kvm_translation *tr)
  272. {
  273. return -EINVAL;
  274. }