guest.c 5.4 KB

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