guest.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. static unsigned long num_core_regs(void)
  95. {
  96. return sizeof(struct kvm_regs) / sizeof(u32);
  97. }
  98. /**
  99. * kvm_arm_num_regs - how many registers do we present via KVM_GET_ONE_REG
  100. *
  101. * This is for all registers.
  102. */
  103. unsigned long kvm_arm_num_regs(struct kvm_vcpu *vcpu)
  104. {
  105. return num_core_regs() + kvm_arm_num_coproc_regs(vcpu);
  106. }
  107. /**
  108. * kvm_arm_copy_reg_indices - get indices of all registers.
  109. *
  110. * We do core registers right here, then we apppend coproc regs.
  111. */
  112. int kvm_arm_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *uindices)
  113. {
  114. unsigned int i;
  115. const u64 core_reg = KVM_REG_ARM | KVM_REG_SIZE_U32 | KVM_REG_ARM_CORE;
  116. for (i = 0; i < sizeof(struct kvm_regs)/sizeof(u32); i++) {
  117. if (put_user(core_reg | i, uindices))
  118. return -EFAULT;
  119. uindices++;
  120. }
  121. return kvm_arm_copy_coproc_indices(vcpu, uindices);
  122. }
  123. int kvm_arm_get_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
  124. {
  125. /* We currently use nothing arch-specific in upper 32 bits */
  126. if ((reg->id & ~KVM_REG_SIZE_MASK) >> 32 != KVM_REG_ARM >> 32)
  127. return -EINVAL;
  128. /* Register group 16 means we want a core register. */
  129. if ((reg->id & KVM_REG_ARM_COPROC_MASK) == KVM_REG_ARM_CORE)
  130. return get_core_reg(vcpu, reg);
  131. return kvm_arm_coproc_get_reg(vcpu, reg);
  132. }
  133. int kvm_arm_set_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
  134. {
  135. /* We currently use nothing arch-specific in upper 32 bits */
  136. if ((reg->id & ~KVM_REG_SIZE_MASK) >> 32 != KVM_REG_ARM >> 32)
  137. return -EINVAL;
  138. /* Register group 16 means we set a core register. */
  139. if ((reg->id & KVM_REG_ARM_COPROC_MASK) == KVM_REG_ARM_CORE)
  140. return set_core_reg(vcpu, reg);
  141. return kvm_arm_coproc_set_reg(vcpu, reg);
  142. }
  143. int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
  144. struct kvm_sregs *sregs)
  145. {
  146. return -EINVAL;
  147. }
  148. int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
  149. struct kvm_sregs *sregs)
  150. {
  151. return -EINVAL;
  152. }
  153. int __attribute_const__ kvm_target_cpu(void)
  154. {
  155. unsigned long implementor = read_cpuid_implementor();
  156. unsigned long part_number = read_cpuid_part_number();
  157. if (implementor != ARM_CPU_IMP_ARM)
  158. return -EINVAL;
  159. switch (part_number) {
  160. case ARM_CPU_PART_CORTEX_A15:
  161. return KVM_ARM_TARGET_CORTEX_A15;
  162. default:
  163. return -EINVAL;
  164. }
  165. }
  166. int kvm_vcpu_set_target(struct kvm_vcpu *vcpu,
  167. const struct kvm_vcpu_init *init)
  168. {
  169. unsigned int i;
  170. /* We can only do a cortex A15 for now. */
  171. if (init->target != kvm_target_cpu())
  172. return -EINVAL;
  173. vcpu->arch.target = init->target;
  174. bitmap_zero(vcpu->arch.features, KVM_VCPU_MAX_FEATURES);
  175. /* -ENOENT for unknown features, -EINVAL for invalid combinations. */
  176. for (i = 0; i < sizeof(init->features) * 8; i++) {
  177. if (test_bit(i, (void *)init->features)) {
  178. if (i >= KVM_VCPU_MAX_FEATURES)
  179. return -ENOENT;
  180. set_bit(i, vcpu->arch.features);
  181. }
  182. }
  183. /* Now we know what it is, we can reset it. */
  184. return kvm_reset_vcpu(vcpu);
  185. }
  186. int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
  187. {
  188. return -EINVAL;
  189. }
  190. int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
  191. {
  192. return -EINVAL;
  193. }
  194. int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
  195. struct kvm_translation *tr)
  196. {
  197. return -EINVAL;
  198. }