guest.c 7.2 KB

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