guest.c 7.0 KB

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