guest.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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/uaccess.h>
  25. #include <asm/kvm.h>
  26. #include <asm/kvm_asm.h>
  27. #include <asm/kvm_emulate.h>
  28. #include <asm/kvm_coproc.h>
  29. #define VM_STAT(x) { #x, offsetof(struct kvm, stat.x), KVM_STAT_VM }
  30. #define VCPU_STAT(x) { #x, offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU }
  31. struct kvm_stats_debugfs_item debugfs_entries[] = {
  32. { NULL }
  33. };
  34. int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
  35. {
  36. return 0;
  37. }
  38. static u64 core_reg_offset_from_id(u64 id)
  39. {
  40. return id & ~(KVM_REG_ARCH_MASK | KVM_REG_SIZE_MASK | KVM_REG_ARM_CORE);
  41. }
  42. static int get_core_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
  43. {
  44. u32 __user *uaddr = (u32 __user *)(long)reg->addr;
  45. struct kvm_regs *regs = &vcpu->arch.regs;
  46. u64 off;
  47. if (KVM_REG_SIZE(reg->id) != 4)
  48. return -ENOENT;
  49. /* Our ID is an index into the kvm_regs struct. */
  50. off = core_reg_offset_from_id(reg->id);
  51. if (off >= sizeof(*regs) / KVM_REG_SIZE(reg->id))
  52. return -ENOENT;
  53. return put_user(((u32 *)regs)[off], uaddr);
  54. }
  55. static int set_core_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
  56. {
  57. u32 __user *uaddr = (u32 __user *)(long)reg->addr;
  58. struct kvm_regs *regs = &vcpu->arch.regs;
  59. u64 off, val;
  60. if (KVM_REG_SIZE(reg->id) != 4)
  61. return -ENOENT;
  62. /* Our ID is an index into the kvm_regs struct. */
  63. off = core_reg_offset_from_id(reg->id);
  64. if (off >= sizeof(*regs) / KVM_REG_SIZE(reg->id))
  65. return -ENOENT;
  66. if (get_user(val, uaddr) != 0)
  67. return -EFAULT;
  68. if (off == KVM_REG_ARM_CORE_REG(usr_regs.ARM_cpsr)) {
  69. unsigned long mode = val & MODE_MASK;
  70. switch (mode) {
  71. case USR_MODE:
  72. case FIQ_MODE:
  73. case IRQ_MODE:
  74. case SVC_MODE:
  75. case ABT_MODE:
  76. case UND_MODE:
  77. break;
  78. default:
  79. return -EINVAL;
  80. }
  81. }
  82. ((u32 *)regs)[off] = val;
  83. return 0;
  84. }
  85. int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
  86. {
  87. return -EINVAL;
  88. }
  89. int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
  90. {
  91. return -EINVAL;
  92. }
  93. static unsigned long num_core_regs(void)
  94. {
  95. return sizeof(struct kvm_regs) / sizeof(u32);
  96. }
  97. /**
  98. * kvm_arm_num_regs - how many registers do we present via KVM_GET_ONE_REG
  99. *
  100. * This is for all registers.
  101. */
  102. unsigned long kvm_arm_num_regs(struct kvm_vcpu *vcpu)
  103. {
  104. return num_core_regs() + kvm_arm_num_coproc_regs(vcpu);
  105. }
  106. /**
  107. * kvm_arm_copy_reg_indices - get indices of all registers.
  108. *
  109. * We do core registers right here, then we apppend coproc regs.
  110. */
  111. int kvm_arm_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *uindices)
  112. {
  113. unsigned int i;
  114. const u64 core_reg = KVM_REG_ARM | KVM_REG_SIZE_U32 | KVM_REG_ARM_CORE;
  115. for (i = 0; i < sizeof(struct kvm_regs)/sizeof(u32); i++) {
  116. if (put_user(core_reg | i, uindices))
  117. return -EFAULT;
  118. uindices++;
  119. }
  120. return kvm_arm_copy_coproc_indices(vcpu, uindices);
  121. }
  122. int kvm_arm_get_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
  123. {
  124. /* We currently use nothing arch-specific in upper 32 bits */
  125. if ((reg->id & ~KVM_REG_SIZE_MASK) >> 32 != KVM_REG_ARM >> 32)
  126. return -EINVAL;
  127. /* Register group 16 means we want a core register. */
  128. if ((reg->id & KVM_REG_ARM_COPROC_MASK) == KVM_REG_ARM_CORE)
  129. return get_core_reg(vcpu, reg);
  130. return kvm_arm_coproc_get_reg(vcpu, reg);
  131. }
  132. int kvm_arm_set_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
  133. {
  134. /* We currently use nothing arch-specific in upper 32 bits */
  135. if ((reg->id & ~KVM_REG_SIZE_MASK) >> 32 != KVM_REG_ARM >> 32)
  136. return -EINVAL;
  137. /* Register group 16 means we set a core register. */
  138. if ((reg->id & KVM_REG_ARM_COPROC_MASK) == KVM_REG_ARM_CORE)
  139. return set_core_reg(vcpu, reg);
  140. return kvm_arm_coproc_set_reg(vcpu, reg);
  141. }
  142. int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
  143. struct kvm_sregs *sregs)
  144. {
  145. return -EINVAL;
  146. }
  147. int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
  148. struct kvm_sregs *sregs)
  149. {
  150. return -EINVAL;
  151. }
  152. int kvm_vcpu_set_target(struct kvm_vcpu *vcpu,
  153. const struct kvm_vcpu_init *init)
  154. {
  155. unsigned int i;
  156. /* We can only do a cortex A15 for now. */
  157. if (init->target != kvm_target_cpu())
  158. return -EINVAL;
  159. vcpu->arch.target = init->target;
  160. bitmap_zero(vcpu->arch.features, KVM_VCPU_MAX_FEATURES);
  161. /* -ENOENT for unknown features, -EINVAL for invalid combinations. */
  162. for (i = 0; i < sizeof(init->features) * 8; i++) {
  163. if (test_bit(i, (void *)init->features)) {
  164. if (i >= KVM_VCPU_MAX_FEATURES)
  165. return -ENOENT;
  166. set_bit(i, vcpu->arch.features);
  167. }
  168. }
  169. /* Now we know what it is, we can reset it. */
  170. return kvm_reset_vcpu(vcpu);
  171. }
  172. int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
  173. {
  174. return -EINVAL;
  175. }
  176. int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
  177. {
  178. return -EINVAL;
  179. }
  180. int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
  181. struct kvm_translation *tr)
  182. {
  183. return -EINVAL;
  184. }