guest.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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_emulate.h>
  28. #include <asm/kvm_coproc.h>
  29. #define VM_STAT(x) { #x, offsetof(struct kvm, stat.x), KVM_STAT_VM }
  30. #define VCPU_STAT(x) { #x, offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU }
  31. struct kvm_stats_debugfs_item debugfs_entries[] = {
  32. VCPU_STAT(hvc_exit_stat),
  33. VCPU_STAT(wfe_exit_stat),
  34. VCPU_STAT(wfi_exit_stat),
  35. VCPU_STAT(mmio_exit_user),
  36. VCPU_STAT(mmio_exit_kernel),
  37. VCPU_STAT(exits),
  38. { NULL }
  39. };
  40. int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
  41. {
  42. return 0;
  43. }
  44. static u64 core_reg_offset_from_id(u64 id)
  45. {
  46. return id & ~(KVM_REG_ARCH_MASK | KVM_REG_SIZE_MASK | KVM_REG_ARM_CORE);
  47. }
  48. static int get_core_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
  49. {
  50. u32 __user *uaddr = (u32 __user *)(long)reg->addr;
  51. struct kvm_regs *regs = &vcpu->arch.ctxt.gp_regs;
  52. u64 off;
  53. if (KVM_REG_SIZE(reg->id) != 4)
  54. return -ENOENT;
  55. /* Our ID is an index into the kvm_regs struct. */
  56. off = core_reg_offset_from_id(reg->id);
  57. if (off >= sizeof(*regs) / KVM_REG_SIZE(reg->id))
  58. return -ENOENT;
  59. return put_user(((u32 *)regs)[off], uaddr);
  60. }
  61. static int set_core_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
  62. {
  63. u32 __user *uaddr = (u32 __user *)(long)reg->addr;
  64. struct kvm_regs *regs = &vcpu->arch.ctxt.gp_regs;
  65. u64 off, val;
  66. if (KVM_REG_SIZE(reg->id) != 4)
  67. return -ENOENT;
  68. /* Our ID is an index into the kvm_regs struct. */
  69. off = core_reg_offset_from_id(reg->id);
  70. if (off >= sizeof(*regs) / KVM_REG_SIZE(reg->id))
  71. return -ENOENT;
  72. if (get_user(val, uaddr) != 0)
  73. return -EFAULT;
  74. if (off == KVM_REG_ARM_CORE_REG(usr_regs.ARM_cpsr)) {
  75. unsigned long mode = val & MODE_MASK;
  76. switch (mode) {
  77. case USR_MODE:
  78. case FIQ_MODE:
  79. case IRQ_MODE:
  80. case SVC_MODE:
  81. case ABT_MODE:
  82. case UND_MODE:
  83. break;
  84. default:
  85. return -EINVAL;
  86. }
  87. }
  88. ((u32 *)regs)[off] = val;
  89. return 0;
  90. }
  91. int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
  92. {
  93. return -EINVAL;
  94. }
  95. int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
  96. {
  97. return -EINVAL;
  98. }
  99. #define NUM_TIMER_REGS 3
  100. static bool is_timer_reg(u64 index)
  101. {
  102. switch (index) {
  103. case KVM_REG_ARM_TIMER_CTL:
  104. case KVM_REG_ARM_TIMER_CNT:
  105. case KVM_REG_ARM_TIMER_CVAL:
  106. return true;
  107. }
  108. return false;
  109. }
  110. static int copy_timer_indices(struct kvm_vcpu *vcpu, u64 __user *uindices)
  111. {
  112. if (put_user(KVM_REG_ARM_TIMER_CTL, uindices))
  113. return -EFAULT;
  114. uindices++;
  115. if (put_user(KVM_REG_ARM_TIMER_CNT, uindices))
  116. return -EFAULT;
  117. uindices++;
  118. if (put_user(KVM_REG_ARM_TIMER_CVAL, uindices))
  119. return -EFAULT;
  120. return 0;
  121. }
  122. static int set_timer_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
  123. {
  124. void __user *uaddr = (void __user *)(long)reg->addr;
  125. u64 val;
  126. int ret;
  127. ret = copy_from_user(&val, uaddr, KVM_REG_SIZE(reg->id));
  128. if (ret != 0)
  129. return -EFAULT;
  130. return kvm_arm_timer_set_reg(vcpu, reg->id, val);
  131. }
  132. static int get_timer_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
  133. {
  134. void __user *uaddr = (void __user *)(long)reg->addr;
  135. u64 val;
  136. val = kvm_arm_timer_get_reg(vcpu, reg->id);
  137. return copy_to_user(uaddr, &val, KVM_REG_SIZE(reg->id)) ? -EFAULT : 0;
  138. }
  139. static unsigned long num_core_regs(void)
  140. {
  141. return sizeof(struct kvm_regs) / sizeof(u32);
  142. }
  143. /**
  144. * kvm_arm_num_regs - how many registers do we present via KVM_GET_ONE_REG
  145. *
  146. * This is for all registers.
  147. */
  148. unsigned long kvm_arm_num_regs(struct kvm_vcpu *vcpu)
  149. {
  150. return num_core_regs() + kvm_arm_num_coproc_regs(vcpu)
  151. + NUM_TIMER_REGS;
  152. }
  153. /**
  154. * kvm_arm_copy_reg_indices - get indices of all registers.
  155. *
  156. * We do core registers right here, then we append coproc regs.
  157. */
  158. int kvm_arm_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *uindices)
  159. {
  160. unsigned int i;
  161. const u64 core_reg = KVM_REG_ARM | KVM_REG_SIZE_U32 | KVM_REG_ARM_CORE;
  162. int ret;
  163. for (i = 0; i < sizeof(struct kvm_regs)/sizeof(u32); i++) {
  164. if (put_user(core_reg | i, uindices))
  165. return -EFAULT;
  166. uindices++;
  167. }
  168. ret = copy_timer_indices(vcpu, uindices);
  169. if (ret)
  170. return ret;
  171. uindices += NUM_TIMER_REGS;
  172. return kvm_arm_copy_coproc_indices(vcpu, uindices);
  173. }
  174. int kvm_arm_get_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
  175. {
  176. /* We currently use nothing arch-specific in upper 32 bits */
  177. if ((reg->id & ~KVM_REG_SIZE_MASK) >> 32 != KVM_REG_ARM >> 32)
  178. return -EINVAL;
  179. /* Register group 16 means we want a core register. */
  180. if ((reg->id & KVM_REG_ARM_COPROC_MASK) == KVM_REG_ARM_CORE)
  181. return get_core_reg(vcpu, reg);
  182. if (is_timer_reg(reg->id))
  183. return get_timer_reg(vcpu, reg);
  184. return kvm_arm_coproc_get_reg(vcpu, reg);
  185. }
  186. int kvm_arm_set_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
  187. {
  188. /* We currently use nothing arch-specific in upper 32 bits */
  189. if ((reg->id & ~KVM_REG_SIZE_MASK) >> 32 != KVM_REG_ARM >> 32)
  190. return -EINVAL;
  191. /* Register group 16 means we set a core register. */
  192. if ((reg->id & KVM_REG_ARM_COPROC_MASK) == KVM_REG_ARM_CORE)
  193. return set_core_reg(vcpu, reg);
  194. if (is_timer_reg(reg->id))
  195. return set_timer_reg(vcpu, reg);
  196. return kvm_arm_coproc_set_reg(vcpu, reg);
  197. }
  198. int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
  199. struct kvm_sregs *sregs)
  200. {
  201. return -EINVAL;
  202. }
  203. int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
  204. struct kvm_sregs *sregs)
  205. {
  206. return -EINVAL;
  207. }
  208. int __attribute_const__ kvm_target_cpu(void)
  209. {
  210. switch (read_cpuid_part()) {
  211. case ARM_CPU_PART_CORTEX_A7:
  212. return KVM_ARM_TARGET_CORTEX_A7;
  213. case ARM_CPU_PART_CORTEX_A15:
  214. return KVM_ARM_TARGET_CORTEX_A15;
  215. default:
  216. return -EINVAL;
  217. }
  218. }
  219. int kvm_vcpu_preferred_target(struct kvm_vcpu_init *init)
  220. {
  221. int target = kvm_target_cpu();
  222. if (target < 0)
  223. return -ENODEV;
  224. memset(init, 0, sizeof(*init));
  225. /*
  226. * For now, we don't return any features.
  227. * In future, we might use features to return target
  228. * specific features available for the preferred
  229. * target type.
  230. */
  231. init->target = (__u32)target;
  232. return 0;
  233. }
  234. int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
  235. {
  236. return -EINVAL;
  237. }
  238. int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
  239. {
  240. return -EINVAL;
  241. }
  242. int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
  243. struct kvm_translation *tr)
  244. {
  245. return -EINVAL;
  246. }
  247. int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
  248. struct kvm_guest_debug *dbg)
  249. {
  250. return -EINVAL;
  251. }