guest.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /*
  2. * Copyright (C) 2012,2013 - ARM Ltd
  3. * Author: Marc Zyngier <marc.zyngier@arm.com>
  4. *
  5. * Derived from arch/arm/kvm/guest.c:
  6. * Copyright (C) 2012 - Virtual Open Systems and Columbia University
  7. * Author: Christoffer Dall <c.dall@virtualopensystems.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include <linux/errno.h>
  22. #include <linux/err.h>
  23. #include <linux/kvm_host.h>
  24. #include <linux/module.h>
  25. #include <linux/vmalloc.h>
  26. #include <linux/fs.h>
  27. #include <asm/cputype.h>
  28. #include <asm/uaccess.h>
  29. #include <asm/kvm.h>
  30. #include <asm/kvm_asm.h>
  31. #include <asm/kvm_emulate.h>
  32. #include <asm/kvm_coproc.h>
  33. #include "trace.h"
  34. struct kvm_stats_debugfs_item debugfs_entries[] = {
  35. { NULL }
  36. };
  37. int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
  38. {
  39. return 0;
  40. }
  41. static u64 core_reg_offset_from_id(u64 id)
  42. {
  43. return id & ~(KVM_REG_ARCH_MASK | KVM_REG_SIZE_MASK | KVM_REG_ARM_CORE);
  44. }
  45. static int validate_core_offset(const struct kvm_one_reg *reg)
  46. {
  47. u64 off = core_reg_offset_from_id(reg->id);
  48. int size;
  49. switch (off) {
  50. case KVM_REG_ARM_CORE_REG(regs.regs[0]) ...
  51. KVM_REG_ARM_CORE_REG(regs.regs[30]):
  52. case KVM_REG_ARM_CORE_REG(regs.sp):
  53. case KVM_REG_ARM_CORE_REG(regs.pc):
  54. case KVM_REG_ARM_CORE_REG(regs.pstate):
  55. case KVM_REG_ARM_CORE_REG(sp_el1):
  56. case KVM_REG_ARM_CORE_REG(elr_el1):
  57. case KVM_REG_ARM_CORE_REG(spsr[0]) ...
  58. KVM_REG_ARM_CORE_REG(spsr[KVM_NR_SPSR - 1]):
  59. size = sizeof(__u64);
  60. break;
  61. case KVM_REG_ARM_CORE_REG(fp_regs.vregs[0]) ...
  62. KVM_REG_ARM_CORE_REG(fp_regs.vregs[31]):
  63. size = sizeof(__uint128_t);
  64. break;
  65. case KVM_REG_ARM_CORE_REG(fp_regs.fpsr):
  66. case KVM_REG_ARM_CORE_REG(fp_regs.fpcr):
  67. size = sizeof(__u32);
  68. break;
  69. default:
  70. return -EINVAL;
  71. }
  72. if (KVM_REG_SIZE(reg->id) == size &&
  73. IS_ALIGNED(off, size / sizeof(__u32)))
  74. return 0;
  75. return -EINVAL;
  76. }
  77. static int get_core_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
  78. {
  79. /*
  80. * Because the kvm_regs structure is a mix of 32, 64 and
  81. * 128bit fields, we index it as if it was a 32bit
  82. * array. Hence below, nr_regs is the number of entries, and
  83. * off the index in the "array".
  84. */
  85. __u32 __user *uaddr = (__u32 __user *)(unsigned long)reg->addr;
  86. struct kvm_regs *regs = vcpu_gp_regs(vcpu);
  87. int nr_regs = sizeof(*regs) / sizeof(__u32);
  88. u32 off;
  89. /* Our ID is an index into the kvm_regs struct. */
  90. off = core_reg_offset_from_id(reg->id);
  91. if (off >= nr_regs ||
  92. (off + (KVM_REG_SIZE(reg->id) / sizeof(__u32))) >= nr_regs)
  93. return -ENOENT;
  94. if (validate_core_offset(reg))
  95. return -EINVAL;
  96. if (copy_to_user(uaddr, ((u32 *)regs) + off, KVM_REG_SIZE(reg->id)))
  97. return -EFAULT;
  98. return 0;
  99. }
  100. static int set_core_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
  101. {
  102. __u32 __user *uaddr = (__u32 __user *)(unsigned long)reg->addr;
  103. struct kvm_regs *regs = vcpu_gp_regs(vcpu);
  104. int nr_regs = sizeof(*regs) / sizeof(__u32);
  105. __uint128_t tmp;
  106. void *valp = &tmp;
  107. u64 off;
  108. int err = 0;
  109. /* Our ID is an index into the kvm_regs struct. */
  110. off = core_reg_offset_from_id(reg->id);
  111. if (off >= nr_regs ||
  112. (off + (KVM_REG_SIZE(reg->id) / sizeof(__u32))) >= nr_regs)
  113. return -ENOENT;
  114. if (validate_core_offset(reg))
  115. return -EINVAL;
  116. if (KVM_REG_SIZE(reg->id) > sizeof(tmp))
  117. return -EINVAL;
  118. if (copy_from_user(valp, uaddr, KVM_REG_SIZE(reg->id))) {
  119. err = -EFAULT;
  120. goto out;
  121. }
  122. if (off == KVM_REG_ARM_CORE_REG(regs.pstate)) {
  123. u64 mode = (*(u64 *)valp) & COMPAT_PSR_MODE_MASK;
  124. switch (mode) {
  125. case COMPAT_PSR_MODE_USR:
  126. if (!system_supports_32bit_el0())
  127. return -EINVAL;
  128. break;
  129. case COMPAT_PSR_MODE_FIQ:
  130. case COMPAT_PSR_MODE_IRQ:
  131. case COMPAT_PSR_MODE_SVC:
  132. case COMPAT_PSR_MODE_ABT:
  133. case COMPAT_PSR_MODE_UND:
  134. if (!vcpu_el1_is_32bit(vcpu))
  135. return -EINVAL;
  136. break;
  137. case PSR_MODE_EL0t:
  138. case PSR_MODE_EL1t:
  139. case PSR_MODE_EL1h:
  140. if (vcpu_el1_is_32bit(vcpu))
  141. return -EINVAL;
  142. break;
  143. default:
  144. err = -EINVAL;
  145. goto out;
  146. }
  147. }
  148. memcpy((u32 *)regs + off, valp, KVM_REG_SIZE(reg->id));
  149. out:
  150. return err;
  151. }
  152. int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
  153. {
  154. return -EINVAL;
  155. }
  156. int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
  157. {
  158. return -EINVAL;
  159. }
  160. static unsigned long num_core_regs(void)
  161. {
  162. return sizeof(struct kvm_regs) / sizeof(__u32);
  163. }
  164. /**
  165. * ARM64 versions of the TIMER registers, always available on arm64
  166. */
  167. #define NUM_TIMER_REGS 3
  168. static bool is_timer_reg(u64 index)
  169. {
  170. switch (index) {
  171. case KVM_REG_ARM_TIMER_CTL:
  172. case KVM_REG_ARM_TIMER_CNT:
  173. case KVM_REG_ARM_TIMER_CVAL:
  174. return true;
  175. }
  176. return false;
  177. }
  178. static int copy_timer_indices(struct kvm_vcpu *vcpu, u64 __user *uindices)
  179. {
  180. if (put_user(KVM_REG_ARM_TIMER_CTL, uindices))
  181. return -EFAULT;
  182. uindices++;
  183. if (put_user(KVM_REG_ARM_TIMER_CNT, uindices))
  184. return -EFAULT;
  185. uindices++;
  186. if (put_user(KVM_REG_ARM_TIMER_CVAL, uindices))
  187. return -EFAULT;
  188. return 0;
  189. }
  190. static int set_timer_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
  191. {
  192. void __user *uaddr = (void __user *)(long)reg->addr;
  193. u64 val;
  194. int ret;
  195. ret = copy_from_user(&val, uaddr, KVM_REG_SIZE(reg->id));
  196. if (ret != 0)
  197. return -EFAULT;
  198. return kvm_arm_timer_set_reg(vcpu, reg->id, val);
  199. }
  200. static int get_timer_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
  201. {
  202. void __user *uaddr = (void __user *)(long)reg->addr;
  203. u64 val;
  204. val = kvm_arm_timer_get_reg(vcpu, reg->id);
  205. return copy_to_user(uaddr, &val, KVM_REG_SIZE(reg->id)) ? -EFAULT : 0;
  206. }
  207. /**
  208. * kvm_arm_num_regs - how many registers do we present via KVM_GET_ONE_REG
  209. *
  210. * This is for all registers.
  211. */
  212. unsigned long kvm_arm_num_regs(struct kvm_vcpu *vcpu)
  213. {
  214. return num_core_regs() + kvm_arm_num_sys_reg_descs(vcpu)
  215. + NUM_TIMER_REGS;
  216. }
  217. /**
  218. * kvm_arm_copy_reg_indices - get indices of all registers.
  219. *
  220. * We do core registers right here, then we apppend system regs.
  221. */
  222. int kvm_arm_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *uindices)
  223. {
  224. unsigned int i;
  225. const u64 core_reg = KVM_REG_ARM64 | KVM_REG_SIZE_U64 | KVM_REG_ARM_CORE;
  226. int ret;
  227. for (i = 0; i < sizeof(struct kvm_regs) / sizeof(__u32); i++) {
  228. if (put_user(core_reg | i, uindices))
  229. return -EFAULT;
  230. uindices++;
  231. }
  232. ret = copy_timer_indices(vcpu, uindices);
  233. if (ret)
  234. return ret;
  235. uindices += NUM_TIMER_REGS;
  236. return kvm_arm_copy_sys_reg_indices(vcpu, uindices);
  237. }
  238. int kvm_arm_get_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
  239. {
  240. /* We currently use nothing arch-specific in upper 32 bits */
  241. if ((reg->id & ~KVM_REG_SIZE_MASK) >> 32 != KVM_REG_ARM64 >> 32)
  242. return -EINVAL;
  243. /* Register group 16 means we want a core register. */
  244. if ((reg->id & KVM_REG_ARM_COPROC_MASK) == KVM_REG_ARM_CORE)
  245. return get_core_reg(vcpu, reg);
  246. if (is_timer_reg(reg->id))
  247. return get_timer_reg(vcpu, reg);
  248. return kvm_arm_sys_reg_get_reg(vcpu, reg);
  249. }
  250. int kvm_arm_set_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
  251. {
  252. /* We currently use nothing arch-specific in upper 32 bits */
  253. if ((reg->id & ~KVM_REG_SIZE_MASK) >> 32 != KVM_REG_ARM64 >> 32)
  254. return -EINVAL;
  255. /* Register group 16 means we set a core register. */
  256. if ((reg->id & KVM_REG_ARM_COPROC_MASK) == KVM_REG_ARM_CORE)
  257. return set_core_reg(vcpu, reg);
  258. if (is_timer_reg(reg->id))
  259. return set_timer_reg(vcpu, reg);
  260. return kvm_arm_sys_reg_set_reg(vcpu, reg);
  261. }
  262. int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
  263. struct kvm_sregs *sregs)
  264. {
  265. return -EINVAL;
  266. }
  267. int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
  268. struct kvm_sregs *sregs)
  269. {
  270. return -EINVAL;
  271. }
  272. int __attribute_const__ kvm_target_cpu(void)
  273. {
  274. unsigned long implementor = read_cpuid_implementor();
  275. unsigned long part_number = read_cpuid_part_number();
  276. switch (implementor) {
  277. case ARM_CPU_IMP_ARM:
  278. switch (part_number) {
  279. case ARM_CPU_PART_AEM_V8:
  280. return KVM_ARM_TARGET_AEM_V8;
  281. case ARM_CPU_PART_FOUNDATION:
  282. return KVM_ARM_TARGET_FOUNDATION_V8;
  283. case ARM_CPU_PART_CORTEX_A53:
  284. return KVM_ARM_TARGET_CORTEX_A53;
  285. case ARM_CPU_PART_CORTEX_A57:
  286. return KVM_ARM_TARGET_CORTEX_A57;
  287. };
  288. break;
  289. case ARM_CPU_IMP_APM:
  290. switch (part_number) {
  291. case APM_CPU_PART_POTENZA:
  292. return KVM_ARM_TARGET_XGENE_POTENZA;
  293. };
  294. break;
  295. };
  296. /* Return a default generic target */
  297. return KVM_ARM_TARGET_GENERIC_V8;
  298. }
  299. int kvm_vcpu_preferred_target(struct kvm_vcpu_init *init)
  300. {
  301. int target = kvm_target_cpu();
  302. if (target < 0)
  303. return -ENODEV;
  304. memset(init, 0, sizeof(*init));
  305. /*
  306. * For now, we don't return any features.
  307. * In future, we might use features to return target
  308. * specific features available for the preferred
  309. * target type.
  310. */
  311. init->target = (__u32)target;
  312. return 0;
  313. }
  314. int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
  315. {
  316. return -EINVAL;
  317. }
  318. int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
  319. {
  320. return -EINVAL;
  321. }
  322. int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
  323. struct kvm_translation *tr)
  324. {
  325. return -EINVAL;
  326. }
  327. #define KVM_GUESTDBG_VALID_MASK (KVM_GUESTDBG_ENABLE | \
  328. KVM_GUESTDBG_USE_SW_BP | \
  329. KVM_GUESTDBG_USE_HW | \
  330. KVM_GUESTDBG_SINGLESTEP)
  331. /**
  332. * kvm_arch_vcpu_ioctl_set_guest_debug - set up guest debugging
  333. * @kvm: pointer to the KVM struct
  334. * @kvm_guest_debug: the ioctl data buffer
  335. *
  336. * This sets up and enables the VM for guest debugging. Userspace
  337. * passes in a control flag to enable different debug types and
  338. * potentially other architecture specific information in the rest of
  339. * the structure.
  340. */
  341. int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
  342. struct kvm_guest_debug *dbg)
  343. {
  344. trace_kvm_set_guest_debug(vcpu, dbg->control);
  345. if (dbg->control & ~KVM_GUESTDBG_VALID_MASK)
  346. return -EINVAL;
  347. if (dbg->control & KVM_GUESTDBG_ENABLE) {
  348. vcpu->guest_debug = dbg->control;
  349. /* Hardware assisted Break and Watch points */
  350. if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW) {
  351. vcpu->arch.external_debug_state = dbg->arch;
  352. }
  353. } else {
  354. /* If not enabled clear all flags */
  355. vcpu->guest_debug = 0;
  356. }
  357. return 0;
  358. }