guest.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. return 0;
  38. }
  39. static u64 core_reg_offset_from_id(u64 id)
  40. {
  41. return id & ~(KVM_REG_ARCH_MASK | KVM_REG_SIZE_MASK | KVM_REG_ARM_CORE);
  42. }
  43. static int get_core_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
  44. {
  45. u32 __user *uaddr = (u32 __user *)(long)reg->addr;
  46. struct kvm_regs *regs = &vcpu->arch.regs;
  47. u64 off;
  48. if (KVM_REG_SIZE(reg->id) != 4)
  49. return -ENOENT;
  50. /* Our ID is an index into the kvm_regs struct. */
  51. off = core_reg_offset_from_id(reg->id);
  52. if (off >= sizeof(*regs) / KVM_REG_SIZE(reg->id))
  53. return -ENOENT;
  54. return put_user(((u32 *)regs)[off], uaddr);
  55. }
  56. static int set_core_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
  57. {
  58. u32 __user *uaddr = (u32 __user *)(long)reg->addr;
  59. struct kvm_regs *regs = &vcpu->arch.regs;
  60. u64 off, val;
  61. if (KVM_REG_SIZE(reg->id) != 4)
  62. return -ENOENT;
  63. /* Our ID is an index into the kvm_regs struct. */
  64. off = core_reg_offset_from_id(reg->id);
  65. if (off >= sizeof(*regs) / KVM_REG_SIZE(reg->id))
  66. return -ENOENT;
  67. if (get_user(val, uaddr) != 0)
  68. return -EFAULT;
  69. if (off == KVM_REG_ARM_CORE_REG(usr_regs.ARM_cpsr)) {
  70. unsigned long mode = val & MODE_MASK;
  71. switch (mode) {
  72. case USR_MODE:
  73. case FIQ_MODE:
  74. case IRQ_MODE:
  75. case SVC_MODE:
  76. case ABT_MODE:
  77. case UND_MODE:
  78. break;
  79. default:
  80. return -EINVAL;
  81. }
  82. }
  83. ((u32 *)regs)[off] = val;
  84. return 0;
  85. }
  86. int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
  87. {
  88. return -EINVAL;
  89. }
  90. int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
  91. {
  92. return -EINVAL;
  93. }
  94. #ifndef CONFIG_KVM_ARM_TIMER
  95. #define NUM_TIMER_REGS 0
  96. static int copy_timer_indices(struct kvm_vcpu *vcpu, u64 __user *uindices)
  97. {
  98. return 0;
  99. }
  100. static bool is_timer_reg(u64 index)
  101. {
  102. return false;
  103. }
  104. int kvm_arm_timer_set_reg(struct kvm_vcpu *vcpu, u64 regid, u64 value)
  105. {
  106. return 0;
  107. }
  108. u64 kvm_arm_timer_get_reg(struct kvm_vcpu *vcpu, u64 regid)
  109. {
  110. return 0;
  111. }
  112. #else
  113. #define NUM_TIMER_REGS 3
  114. static bool is_timer_reg(u64 index)
  115. {
  116. switch (index) {
  117. case KVM_REG_ARM_TIMER_CTL:
  118. case KVM_REG_ARM_TIMER_CNT:
  119. case KVM_REG_ARM_TIMER_CVAL:
  120. return true;
  121. }
  122. return false;
  123. }
  124. static int copy_timer_indices(struct kvm_vcpu *vcpu, u64 __user *uindices)
  125. {
  126. if (put_user(KVM_REG_ARM_TIMER_CTL, uindices))
  127. return -EFAULT;
  128. uindices++;
  129. if (put_user(KVM_REG_ARM_TIMER_CNT, uindices))
  130. return -EFAULT;
  131. uindices++;
  132. if (put_user(KVM_REG_ARM_TIMER_CVAL, uindices))
  133. return -EFAULT;
  134. return 0;
  135. }
  136. #endif
  137. static int set_timer_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
  138. {
  139. void __user *uaddr = (void __user *)(long)reg->addr;
  140. u64 val;
  141. int ret;
  142. ret = copy_from_user(&val, uaddr, KVM_REG_SIZE(reg->id));
  143. if (ret != 0)
  144. return ret;
  145. return kvm_arm_timer_set_reg(vcpu, reg->id, val);
  146. }
  147. static int get_timer_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
  148. {
  149. void __user *uaddr = (void __user *)(long)reg->addr;
  150. u64 val;
  151. val = kvm_arm_timer_get_reg(vcpu, reg->id);
  152. return copy_to_user(uaddr, &val, KVM_REG_SIZE(reg->id));
  153. }
  154. static unsigned long num_core_regs(void)
  155. {
  156. return sizeof(struct kvm_regs) / sizeof(u32);
  157. }
  158. /**
  159. * kvm_arm_num_regs - how many registers do we present via KVM_GET_ONE_REG
  160. *
  161. * This is for all registers.
  162. */
  163. unsigned long kvm_arm_num_regs(struct kvm_vcpu *vcpu)
  164. {
  165. return num_core_regs() + kvm_arm_num_coproc_regs(vcpu)
  166. + NUM_TIMER_REGS;
  167. }
  168. /**
  169. * kvm_arm_copy_reg_indices - get indices of all registers.
  170. *
  171. * We do core registers right here, then we apppend coproc regs.
  172. */
  173. int kvm_arm_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *uindices)
  174. {
  175. unsigned int i;
  176. const u64 core_reg = KVM_REG_ARM | KVM_REG_SIZE_U32 | KVM_REG_ARM_CORE;
  177. int ret;
  178. for (i = 0; i < sizeof(struct kvm_regs)/sizeof(u32); i++) {
  179. if (put_user(core_reg | i, uindices))
  180. return -EFAULT;
  181. uindices++;
  182. }
  183. ret = copy_timer_indices(vcpu, uindices);
  184. if (ret)
  185. return ret;
  186. uindices += NUM_TIMER_REGS;
  187. return kvm_arm_copy_coproc_indices(vcpu, uindices);
  188. }
  189. int kvm_arm_get_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
  190. {
  191. /* We currently use nothing arch-specific in upper 32 bits */
  192. if ((reg->id & ~KVM_REG_SIZE_MASK) >> 32 != KVM_REG_ARM >> 32)
  193. return -EINVAL;
  194. /* Register group 16 means we want a core register. */
  195. if ((reg->id & KVM_REG_ARM_COPROC_MASK) == KVM_REG_ARM_CORE)
  196. return get_core_reg(vcpu, reg);
  197. if (is_timer_reg(reg->id))
  198. return get_timer_reg(vcpu, reg);
  199. return kvm_arm_coproc_get_reg(vcpu, reg);
  200. }
  201. int kvm_arm_set_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
  202. {
  203. /* We currently use nothing arch-specific in upper 32 bits */
  204. if ((reg->id & ~KVM_REG_SIZE_MASK) >> 32 != KVM_REG_ARM >> 32)
  205. return -EINVAL;
  206. /* Register group 16 means we set a core register. */
  207. if ((reg->id & KVM_REG_ARM_COPROC_MASK) == KVM_REG_ARM_CORE)
  208. return set_core_reg(vcpu, reg);
  209. if (is_timer_reg(reg->id))
  210. return set_timer_reg(vcpu, reg);
  211. return kvm_arm_coproc_set_reg(vcpu, reg);
  212. }
  213. int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
  214. struct kvm_sregs *sregs)
  215. {
  216. return -EINVAL;
  217. }
  218. int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
  219. struct kvm_sregs *sregs)
  220. {
  221. return -EINVAL;
  222. }
  223. int __attribute_const__ kvm_target_cpu(void)
  224. {
  225. unsigned long implementor = read_cpuid_implementor();
  226. unsigned long part_number = read_cpuid_part_number();
  227. if (implementor != ARM_CPU_IMP_ARM)
  228. return -EINVAL;
  229. switch (part_number) {
  230. case ARM_CPU_PART_CORTEX_A7:
  231. return KVM_ARM_TARGET_CORTEX_A7;
  232. case ARM_CPU_PART_CORTEX_A15:
  233. return KVM_ARM_TARGET_CORTEX_A15;
  234. default:
  235. return -EINVAL;
  236. }
  237. }
  238. int kvm_vcpu_set_target(struct kvm_vcpu *vcpu,
  239. const struct kvm_vcpu_init *init)
  240. {
  241. unsigned int i;
  242. /* We can only cope with guest==host and only on A15/A7 (for now). */
  243. if (init->target != kvm_target_cpu())
  244. return -EINVAL;
  245. vcpu->arch.target = init->target;
  246. bitmap_zero(vcpu->arch.features, KVM_VCPU_MAX_FEATURES);
  247. /* -ENOENT for unknown features, -EINVAL for invalid combinations. */
  248. for (i = 0; i < sizeof(init->features) * 8; i++) {
  249. if (test_bit(i, (void *)init->features)) {
  250. if (i >= KVM_VCPU_MAX_FEATURES)
  251. return -ENOENT;
  252. set_bit(i, vcpu->arch.features);
  253. }
  254. }
  255. /* Now we know what it is, we can reset it. */
  256. return kvm_reset_vcpu(vcpu);
  257. }
  258. int kvm_vcpu_preferred_target(struct kvm_vcpu_init *init)
  259. {
  260. int target = kvm_target_cpu();
  261. if (target < 0)
  262. return -ENODEV;
  263. memset(init, 0, sizeof(*init));
  264. /*
  265. * For now, we don't return any features.
  266. * In future, we might use features to return target
  267. * specific features available for the preferred
  268. * target type.
  269. */
  270. init->target = (__u32)target;
  271. return 0;
  272. }
  273. int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
  274. {
  275. return -EINVAL;
  276. }
  277. int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
  278. {
  279. return -EINVAL;
  280. }
  281. int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
  282. struct kvm_translation *tr)
  283. {
  284. return -EINVAL;
  285. }