guest.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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 <kvm/arm_psci.h>
  25. #include <asm/cputype.h>
  26. #include <asm/uaccess.h>
  27. #include <asm/kvm.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.ctxt.gp_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.ctxt.gp_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. + kvm_arm_get_fw_num_regs(vcpu)
  153. + NUM_TIMER_REGS;
  154. }
  155. /**
  156. * kvm_arm_copy_reg_indices - get indices of all registers.
  157. *
  158. * We do core registers right here, then we append coproc regs.
  159. */
  160. int kvm_arm_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *uindices)
  161. {
  162. unsigned int i;
  163. const u64 core_reg = KVM_REG_ARM | KVM_REG_SIZE_U32 | KVM_REG_ARM_CORE;
  164. int ret;
  165. for (i = 0; i < sizeof(struct kvm_regs)/sizeof(u32); i++) {
  166. if (put_user(core_reg | i, uindices))
  167. return -EFAULT;
  168. uindices++;
  169. }
  170. ret = kvm_arm_copy_fw_reg_indices(vcpu, uindices);
  171. if (ret)
  172. return ret;
  173. uindices += kvm_arm_get_fw_num_regs(vcpu);
  174. ret = copy_timer_indices(vcpu, uindices);
  175. if (ret)
  176. return ret;
  177. uindices += NUM_TIMER_REGS;
  178. return kvm_arm_copy_coproc_indices(vcpu, uindices);
  179. }
  180. int kvm_arm_get_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
  181. {
  182. /* We currently use nothing arch-specific in upper 32 bits */
  183. if ((reg->id & ~KVM_REG_SIZE_MASK) >> 32 != KVM_REG_ARM >> 32)
  184. return -EINVAL;
  185. /* Register group 16 means we want a core register. */
  186. if ((reg->id & KVM_REG_ARM_COPROC_MASK) == KVM_REG_ARM_CORE)
  187. return get_core_reg(vcpu, reg);
  188. if ((reg->id & KVM_REG_ARM_COPROC_MASK) == KVM_REG_ARM_FW)
  189. return kvm_arm_get_fw_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 ((reg->id & KVM_REG_ARM_COPROC_MASK) == KVM_REG_ARM_FW)
  203. return kvm_arm_set_fw_reg(vcpu, reg);
  204. if (is_timer_reg(reg->id))
  205. return set_timer_reg(vcpu, reg);
  206. return kvm_arm_coproc_set_reg(vcpu, reg);
  207. }
  208. int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
  209. struct kvm_sregs *sregs)
  210. {
  211. return -EINVAL;
  212. }
  213. int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
  214. struct kvm_sregs *sregs)
  215. {
  216. return -EINVAL;
  217. }
  218. int __attribute_const__ kvm_target_cpu(void)
  219. {
  220. switch (read_cpuid_part()) {
  221. case ARM_CPU_PART_CORTEX_A7:
  222. return KVM_ARM_TARGET_CORTEX_A7;
  223. case ARM_CPU_PART_CORTEX_A15:
  224. return KVM_ARM_TARGET_CORTEX_A15;
  225. default:
  226. return -EINVAL;
  227. }
  228. }
  229. int kvm_vcpu_preferred_target(struct kvm_vcpu_init *init)
  230. {
  231. int target = kvm_target_cpu();
  232. if (target < 0)
  233. return -ENODEV;
  234. memset(init, 0, sizeof(*init));
  235. /*
  236. * For now, we don't return any features.
  237. * In future, we might use features to return target
  238. * specific features available for the preferred
  239. * target type.
  240. */
  241. init->target = (__u32)target;
  242. return 0;
  243. }
  244. int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
  245. {
  246. return -EINVAL;
  247. }
  248. int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
  249. {
  250. return -EINVAL;
  251. }
  252. int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
  253. struct kvm_translation *tr)
  254. {
  255. return -EINVAL;
  256. }
  257. int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
  258. struct kvm_guest_debug *dbg)
  259. {
  260. return -EINVAL;
  261. }