guest.c 7.1 KB

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