arm.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015
  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 <linux/mman.h>
  25. #include <linux/sched.h>
  26. #include <linux/kvm.h>
  27. #include <trace/events/kvm.h>
  28. #define CREATE_TRACE_POINTS
  29. #include "trace.h"
  30. #include <asm/unified.h>
  31. #include <asm/uaccess.h>
  32. #include <asm/ptrace.h>
  33. #include <asm/mman.h>
  34. #include <asm/cputype.h>
  35. #include <asm/tlbflush.h>
  36. #include <asm/cacheflush.h>
  37. #include <asm/virt.h>
  38. #include <asm/kvm_arm.h>
  39. #include <asm/kvm_asm.h>
  40. #include <asm/kvm_mmu.h>
  41. #include <asm/kvm_emulate.h>
  42. #include <asm/kvm_coproc.h>
  43. #include <asm/kvm_psci.h>
  44. #include <asm/opcodes.h>
  45. #ifdef REQUIRES_VIRT
  46. __asm__(".arch_extension virt");
  47. #endif
  48. static DEFINE_PER_CPU(unsigned long, kvm_arm_hyp_stack_page);
  49. static struct vfp_hard_struct __percpu *kvm_host_vfp_state;
  50. static unsigned long hyp_default_vectors;
  51. /* The VMID used in the VTTBR */
  52. static atomic64_t kvm_vmid_gen = ATOMIC64_INIT(1);
  53. static u8 kvm_next_vmid;
  54. static DEFINE_SPINLOCK(kvm_vmid_lock);
  55. int kvm_arch_hardware_enable(void *garbage)
  56. {
  57. return 0;
  58. }
  59. int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
  60. {
  61. return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
  62. }
  63. void kvm_arch_hardware_disable(void *garbage)
  64. {
  65. }
  66. int kvm_arch_hardware_setup(void)
  67. {
  68. return 0;
  69. }
  70. void kvm_arch_hardware_unsetup(void)
  71. {
  72. }
  73. void kvm_arch_check_processor_compat(void *rtn)
  74. {
  75. *(int *)rtn = 0;
  76. }
  77. void kvm_arch_sync_events(struct kvm *kvm)
  78. {
  79. }
  80. /**
  81. * kvm_arch_init_vm - initializes a VM data structure
  82. * @kvm: pointer to the KVM struct
  83. */
  84. int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
  85. {
  86. int ret = 0;
  87. if (type)
  88. return -EINVAL;
  89. ret = kvm_alloc_stage2_pgd(kvm);
  90. if (ret)
  91. goto out_fail_alloc;
  92. ret = create_hyp_mappings(kvm, kvm + 1);
  93. if (ret)
  94. goto out_free_stage2_pgd;
  95. /* Mark the initial VMID generation invalid */
  96. kvm->arch.vmid_gen = 0;
  97. return ret;
  98. out_free_stage2_pgd:
  99. kvm_free_stage2_pgd(kvm);
  100. out_fail_alloc:
  101. return ret;
  102. }
  103. int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
  104. {
  105. return VM_FAULT_SIGBUS;
  106. }
  107. void kvm_arch_free_memslot(struct kvm_memory_slot *free,
  108. struct kvm_memory_slot *dont)
  109. {
  110. }
  111. int kvm_arch_create_memslot(struct kvm_memory_slot *slot, unsigned long npages)
  112. {
  113. return 0;
  114. }
  115. /**
  116. * kvm_arch_destroy_vm - destroy the VM data structure
  117. * @kvm: pointer to the KVM struct
  118. */
  119. void kvm_arch_destroy_vm(struct kvm *kvm)
  120. {
  121. int i;
  122. kvm_free_stage2_pgd(kvm);
  123. for (i = 0; i < KVM_MAX_VCPUS; ++i) {
  124. if (kvm->vcpus[i]) {
  125. kvm_arch_vcpu_free(kvm->vcpus[i]);
  126. kvm->vcpus[i] = NULL;
  127. }
  128. }
  129. }
  130. int kvm_dev_ioctl_check_extension(long ext)
  131. {
  132. int r;
  133. switch (ext) {
  134. case KVM_CAP_USER_MEMORY:
  135. case KVM_CAP_SYNC_MMU:
  136. case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
  137. case KVM_CAP_ONE_REG:
  138. case KVM_CAP_ARM_PSCI:
  139. r = 1;
  140. break;
  141. case KVM_CAP_COALESCED_MMIO:
  142. r = KVM_COALESCED_MMIO_PAGE_OFFSET;
  143. break;
  144. case KVM_CAP_NR_VCPUS:
  145. r = num_online_cpus();
  146. break;
  147. case KVM_CAP_MAX_VCPUS:
  148. r = KVM_MAX_VCPUS;
  149. break;
  150. default:
  151. r = 0;
  152. break;
  153. }
  154. return r;
  155. }
  156. long kvm_arch_dev_ioctl(struct file *filp,
  157. unsigned int ioctl, unsigned long arg)
  158. {
  159. return -EINVAL;
  160. }
  161. int kvm_arch_set_memory_region(struct kvm *kvm,
  162. struct kvm_userspace_memory_region *mem,
  163. struct kvm_memory_slot old,
  164. int user_alloc)
  165. {
  166. return 0;
  167. }
  168. int kvm_arch_prepare_memory_region(struct kvm *kvm,
  169. struct kvm_memory_slot *memslot,
  170. struct kvm_memory_slot old,
  171. struct kvm_userspace_memory_region *mem,
  172. int user_alloc)
  173. {
  174. return 0;
  175. }
  176. void kvm_arch_commit_memory_region(struct kvm *kvm,
  177. struct kvm_userspace_memory_region *mem,
  178. struct kvm_memory_slot old,
  179. int user_alloc)
  180. {
  181. }
  182. void kvm_arch_flush_shadow_all(struct kvm *kvm)
  183. {
  184. }
  185. void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
  186. struct kvm_memory_slot *slot)
  187. {
  188. }
  189. struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
  190. {
  191. int err;
  192. struct kvm_vcpu *vcpu;
  193. vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
  194. if (!vcpu) {
  195. err = -ENOMEM;
  196. goto out;
  197. }
  198. err = kvm_vcpu_init(vcpu, kvm, id);
  199. if (err)
  200. goto free_vcpu;
  201. err = create_hyp_mappings(vcpu, vcpu + 1);
  202. if (err)
  203. goto vcpu_uninit;
  204. return vcpu;
  205. vcpu_uninit:
  206. kvm_vcpu_uninit(vcpu);
  207. free_vcpu:
  208. kmem_cache_free(kvm_vcpu_cache, vcpu);
  209. out:
  210. return ERR_PTR(err);
  211. }
  212. int kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
  213. {
  214. return 0;
  215. }
  216. void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
  217. {
  218. kvm_mmu_free_memory_caches(vcpu);
  219. kmem_cache_free(kvm_vcpu_cache, vcpu);
  220. }
  221. void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
  222. {
  223. kvm_arch_vcpu_free(vcpu);
  224. }
  225. int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
  226. {
  227. return 0;
  228. }
  229. int __attribute_const__ kvm_target_cpu(void)
  230. {
  231. unsigned long implementor = read_cpuid_implementor();
  232. unsigned long part_number = read_cpuid_part_number();
  233. if (implementor != ARM_CPU_IMP_ARM)
  234. return -EINVAL;
  235. switch (part_number) {
  236. case ARM_CPU_PART_CORTEX_A15:
  237. return KVM_ARM_TARGET_CORTEX_A15;
  238. default:
  239. return -EINVAL;
  240. }
  241. }
  242. int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
  243. {
  244. /* Force users to call KVM_ARM_VCPU_INIT */
  245. vcpu->arch.target = -1;
  246. return 0;
  247. }
  248. void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
  249. {
  250. }
  251. void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  252. {
  253. vcpu->cpu = cpu;
  254. vcpu->arch.vfp_host = this_cpu_ptr(kvm_host_vfp_state);
  255. /*
  256. * Check whether this vcpu requires the cache to be flushed on
  257. * this physical CPU. This is a consequence of doing dcache
  258. * operations by set/way on this vcpu. We do it here to be in
  259. * a non-preemptible section.
  260. */
  261. if (cpumask_test_and_clear_cpu(cpu, &vcpu->arch.require_dcache_flush))
  262. flush_cache_all(); /* We'd really want v7_flush_dcache_all() */
  263. }
  264. void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
  265. {
  266. }
  267. int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
  268. struct kvm_guest_debug *dbg)
  269. {
  270. return -EINVAL;
  271. }
  272. int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
  273. struct kvm_mp_state *mp_state)
  274. {
  275. return -EINVAL;
  276. }
  277. int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
  278. struct kvm_mp_state *mp_state)
  279. {
  280. return -EINVAL;
  281. }
  282. /**
  283. * kvm_arch_vcpu_runnable - determine if the vcpu can be scheduled
  284. * @v: The VCPU pointer
  285. *
  286. * If the guest CPU is not waiting for interrupts or an interrupt line is
  287. * asserted, the CPU is by definition runnable.
  288. */
  289. int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
  290. {
  291. return !!v->arch.irq_lines;
  292. }
  293. /* Just ensure a guest exit from a particular CPU */
  294. static void exit_vm_noop(void *info)
  295. {
  296. }
  297. void force_vm_exit(const cpumask_t *mask)
  298. {
  299. smp_call_function_many(mask, exit_vm_noop, NULL, true);
  300. }
  301. /**
  302. * need_new_vmid_gen - check that the VMID is still valid
  303. * @kvm: The VM's VMID to checkt
  304. *
  305. * return true if there is a new generation of VMIDs being used
  306. *
  307. * The hardware supports only 256 values with the value zero reserved for the
  308. * host, so we check if an assigned value belongs to a previous generation,
  309. * which which requires us to assign a new value. If we're the first to use a
  310. * VMID for the new generation, we must flush necessary caches and TLBs on all
  311. * CPUs.
  312. */
  313. static bool need_new_vmid_gen(struct kvm *kvm)
  314. {
  315. return unlikely(kvm->arch.vmid_gen != atomic64_read(&kvm_vmid_gen));
  316. }
  317. /**
  318. * update_vttbr - Update the VTTBR with a valid VMID before the guest runs
  319. * @kvm The guest that we are about to run
  320. *
  321. * Called from kvm_arch_vcpu_ioctl_run before entering the guest to ensure the
  322. * VM has a valid VMID, otherwise assigns a new one and flushes corresponding
  323. * caches and TLBs.
  324. */
  325. static void update_vttbr(struct kvm *kvm)
  326. {
  327. phys_addr_t pgd_phys;
  328. u64 vmid;
  329. if (!need_new_vmid_gen(kvm))
  330. return;
  331. spin_lock(&kvm_vmid_lock);
  332. /*
  333. * We need to re-check the vmid_gen here to ensure that if another vcpu
  334. * already allocated a valid vmid for this vm, then this vcpu should
  335. * use the same vmid.
  336. */
  337. if (!need_new_vmid_gen(kvm)) {
  338. spin_unlock(&kvm_vmid_lock);
  339. return;
  340. }
  341. /* First user of a new VMID generation? */
  342. if (unlikely(kvm_next_vmid == 0)) {
  343. atomic64_inc(&kvm_vmid_gen);
  344. kvm_next_vmid = 1;
  345. /*
  346. * On SMP we know no other CPUs can use this CPU's or each
  347. * other's VMID after force_vm_exit returns since the
  348. * kvm_vmid_lock blocks them from reentry to the guest.
  349. */
  350. force_vm_exit(cpu_all_mask);
  351. /*
  352. * Now broadcast TLB + ICACHE invalidation over the inner
  353. * shareable domain to make sure all data structures are
  354. * clean.
  355. */
  356. kvm_call_hyp(__kvm_flush_vm_context);
  357. }
  358. kvm->arch.vmid_gen = atomic64_read(&kvm_vmid_gen);
  359. kvm->arch.vmid = kvm_next_vmid;
  360. kvm_next_vmid++;
  361. /* update vttbr to be used with the new vmid */
  362. pgd_phys = virt_to_phys(kvm->arch.pgd);
  363. vmid = ((u64)(kvm->arch.vmid) << VTTBR_VMID_SHIFT) & VTTBR_VMID_MASK;
  364. kvm->arch.vttbr = pgd_phys & VTTBR_BADDR_MASK;
  365. kvm->arch.vttbr |= vmid;
  366. spin_unlock(&kvm_vmid_lock);
  367. }
  368. static int handle_svc_hyp(struct kvm_vcpu *vcpu, struct kvm_run *run)
  369. {
  370. /* SVC called from Hyp mode should never get here */
  371. kvm_debug("SVC called from Hyp mode shouldn't go here\n");
  372. BUG();
  373. return -EINVAL; /* Squash warning */
  374. }
  375. static int handle_hvc(struct kvm_vcpu *vcpu, struct kvm_run *run)
  376. {
  377. trace_kvm_hvc(*vcpu_pc(vcpu), *vcpu_reg(vcpu, 0),
  378. vcpu->arch.hsr & HSR_HVC_IMM_MASK);
  379. if (kvm_psci_call(vcpu))
  380. return 1;
  381. kvm_inject_undefined(vcpu);
  382. return 1;
  383. }
  384. static int handle_smc(struct kvm_vcpu *vcpu, struct kvm_run *run)
  385. {
  386. if (kvm_psci_call(vcpu))
  387. return 1;
  388. kvm_inject_undefined(vcpu);
  389. return 1;
  390. }
  391. static int handle_pabt_hyp(struct kvm_vcpu *vcpu, struct kvm_run *run)
  392. {
  393. /* The hypervisor should never cause aborts */
  394. kvm_err("Prefetch Abort taken from Hyp mode at %#08x (HSR: %#08x)\n",
  395. vcpu->arch.hxfar, vcpu->arch.hsr);
  396. return -EFAULT;
  397. }
  398. static int handle_dabt_hyp(struct kvm_vcpu *vcpu, struct kvm_run *run)
  399. {
  400. /* This is either an error in the ws. code or an external abort */
  401. kvm_err("Data Abort taken from Hyp mode at %#08x (HSR: %#08x)\n",
  402. vcpu->arch.hxfar, vcpu->arch.hsr);
  403. return -EFAULT;
  404. }
  405. typedef int (*exit_handle_fn)(struct kvm_vcpu *, struct kvm_run *);
  406. static exit_handle_fn arm_exit_handlers[] = {
  407. [HSR_EC_WFI] = kvm_handle_wfi,
  408. [HSR_EC_CP15_32] = kvm_handle_cp15_32,
  409. [HSR_EC_CP15_64] = kvm_handle_cp15_64,
  410. [HSR_EC_CP14_MR] = kvm_handle_cp14_access,
  411. [HSR_EC_CP14_LS] = kvm_handle_cp14_load_store,
  412. [HSR_EC_CP14_64] = kvm_handle_cp14_access,
  413. [HSR_EC_CP_0_13] = kvm_handle_cp_0_13_access,
  414. [HSR_EC_CP10_ID] = kvm_handle_cp10_id,
  415. [HSR_EC_SVC_HYP] = handle_svc_hyp,
  416. [HSR_EC_HVC] = handle_hvc,
  417. [HSR_EC_SMC] = handle_smc,
  418. [HSR_EC_IABT] = kvm_handle_guest_abort,
  419. [HSR_EC_IABT_HYP] = handle_pabt_hyp,
  420. [HSR_EC_DABT] = kvm_handle_guest_abort,
  421. [HSR_EC_DABT_HYP] = handle_dabt_hyp,
  422. };
  423. /*
  424. * A conditional instruction is allowed to trap, even though it
  425. * wouldn't be executed. So let's re-implement the hardware, in
  426. * software!
  427. */
  428. static bool kvm_condition_valid(struct kvm_vcpu *vcpu)
  429. {
  430. unsigned long cpsr, cond, insn;
  431. /*
  432. * Exception Code 0 can only happen if we set HCR.TGE to 1, to
  433. * catch undefined instructions, and then we won't get past
  434. * the arm_exit_handlers test anyway.
  435. */
  436. BUG_ON(((vcpu->arch.hsr & HSR_EC) >> HSR_EC_SHIFT) == 0);
  437. /* Top two bits non-zero? Unconditional. */
  438. if (vcpu->arch.hsr >> 30)
  439. return true;
  440. cpsr = *vcpu_cpsr(vcpu);
  441. /* Is condition field valid? */
  442. if ((vcpu->arch.hsr & HSR_CV) >> HSR_CV_SHIFT)
  443. cond = (vcpu->arch.hsr & HSR_COND) >> HSR_COND_SHIFT;
  444. else {
  445. /* This can happen in Thumb mode: examine IT state. */
  446. unsigned long it;
  447. it = ((cpsr >> 8) & 0xFC) | ((cpsr >> 25) & 0x3);
  448. /* it == 0 => unconditional. */
  449. if (it == 0)
  450. return true;
  451. /* The cond for this insn works out as the top 4 bits. */
  452. cond = (it >> 4);
  453. }
  454. /* Shift makes it look like an ARM-mode instruction */
  455. insn = cond << 28;
  456. return arm_check_condition(insn, cpsr) != ARM_OPCODE_CONDTEST_FAIL;
  457. }
  458. /*
  459. * Return > 0 to return to guest, < 0 on error, 0 (and set exit_reason) on
  460. * proper exit to QEMU.
  461. */
  462. static int handle_exit(struct kvm_vcpu *vcpu, struct kvm_run *run,
  463. int exception_index)
  464. {
  465. unsigned long hsr_ec;
  466. switch (exception_index) {
  467. case ARM_EXCEPTION_IRQ:
  468. return 1;
  469. case ARM_EXCEPTION_UNDEFINED:
  470. kvm_err("Undefined exception in Hyp mode at: %#08x\n",
  471. vcpu->arch.hyp_pc);
  472. BUG();
  473. panic("KVM: Hypervisor undefined exception!\n");
  474. case ARM_EXCEPTION_DATA_ABORT:
  475. case ARM_EXCEPTION_PREF_ABORT:
  476. case ARM_EXCEPTION_HVC:
  477. hsr_ec = (vcpu->arch.hsr & HSR_EC) >> HSR_EC_SHIFT;
  478. if (hsr_ec >= ARRAY_SIZE(arm_exit_handlers)
  479. || !arm_exit_handlers[hsr_ec]) {
  480. kvm_err("Unkown exception class: %#08lx, "
  481. "hsr: %#08x\n", hsr_ec,
  482. (unsigned int)vcpu->arch.hsr);
  483. BUG();
  484. }
  485. /*
  486. * See ARM ARM B1.14.1: "Hyp traps on instructions
  487. * that fail their condition code check"
  488. */
  489. if (!kvm_condition_valid(vcpu)) {
  490. bool is_wide = vcpu->arch.hsr & HSR_IL;
  491. kvm_skip_instr(vcpu, is_wide);
  492. return 1;
  493. }
  494. return arm_exit_handlers[hsr_ec](vcpu, run);
  495. default:
  496. kvm_pr_unimpl("Unsupported exception type: %d",
  497. exception_index);
  498. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  499. return 0;
  500. }
  501. }
  502. static int kvm_vcpu_first_run_init(struct kvm_vcpu *vcpu)
  503. {
  504. if (likely(vcpu->arch.has_run_once))
  505. return 0;
  506. vcpu->arch.has_run_once = true;
  507. /*
  508. * Handle the "start in power-off" case by calling into the
  509. * PSCI code.
  510. */
  511. if (test_and_clear_bit(KVM_ARM_VCPU_POWER_OFF, vcpu->arch.features)) {
  512. *vcpu_reg(vcpu, 0) = KVM_PSCI_FN_CPU_OFF;
  513. kvm_psci_call(vcpu);
  514. }
  515. return 0;
  516. }
  517. static void vcpu_pause(struct kvm_vcpu *vcpu)
  518. {
  519. wait_queue_head_t *wq = kvm_arch_vcpu_wq(vcpu);
  520. wait_event_interruptible(*wq, !vcpu->arch.pause);
  521. }
  522. /**
  523. * kvm_arch_vcpu_ioctl_run - the main VCPU run function to execute guest code
  524. * @vcpu: The VCPU pointer
  525. * @run: The kvm_run structure pointer used for userspace state exchange
  526. *
  527. * This function is called through the VCPU_RUN ioctl called from user space. It
  528. * will execute VM code in a loop until the time slice for the process is used
  529. * or some emulation is needed from user space in which case the function will
  530. * return with return value 0 and with the kvm_run structure filled in with the
  531. * required data for the requested emulation.
  532. */
  533. int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
  534. {
  535. int ret;
  536. sigset_t sigsaved;
  537. /* Make sure they initialize the vcpu with KVM_ARM_VCPU_INIT */
  538. if (unlikely(vcpu->arch.target < 0))
  539. return -ENOEXEC;
  540. ret = kvm_vcpu_first_run_init(vcpu);
  541. if (ret)
  542. return ret;
  543. if (run->exit_reason == KVM_EXIT_MMIO) {
  544. ret = kvm_handle_mmio_return(vcpu, vcpu->run);
  545. if (ret)
  546. return ret;
  547. }
  548. if (vcpu->sigset_active)
  549. sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
  550. ret = 1;
  551. run->exit_reason = KVM_EXIT_UNKNOWN;
  552. while (ret > 0) {
  553. /*
  554. * Check conditions before entering the guest
  555. */
  556. cond_resched();
  557. update_vttbr(vcpu->kvm);
  558. if (vcpu->arch.pause)
  559. vcpu_pause(vcpu);
  560. local_irq_disable();
  561. /*
  562. * Re-check atomic conditions
  563. */
  564. if (signal_pending(current)) {
  565. ret = -EINTR;
  566. run->exit_reason = KVM_EXIT_INTR;
  567. }
  568. if (ret <= 0 || need_new_vmid_gen(vcpu->kvm)) {
  569. local_irq_enable();
  570. continue;
  571. }
  572. /**************************************************************
  573. * Enter the guest
  574. */
  575. trace_kvm_entry(*vcpu_pc(vcpu));
  576. kvm_guest_enter();
  577. vcpu->mode = IN_GUEST_MODE;
  578. ret = kvm_call_hyp(__kvm_vcpu_run, vcpu);
  579. vcpu->mode = OUTSIDE_GUEST_MODE;
  580. vcpu->arch.last_pcpu = smp_processor_id();
  581. kvm_guest_exit();
  582. trace_kvm_exit(*vcpu_pc(vcpu));
  583. /*
  584. * We may have taken a host interrupt in HYP mode (ie
  585. * while executing the guest). This interrupt is still
  586. * pending, as we haven't serviced it yet!
  587. *
  588. * We're now back in SVC mode, with interrupts
  589. * disabled. Enabling the interrupts now will have
  590. * the effect of taking the interrupt again, in SVC
  591. * mode this time.
  592. */
  593. local_irq_enable();
  594. /*
  595. * Back from guest
  596. *************************************************************/
  597. ret = handle_exit(vcpu, run, ret);
  598. }
  599. if (vcpu->sigset_active)
  600. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  601. return ret;
  602. }
  603. static int vcpu_interrupt_line(struct kvm_vcpu *vcpu, int number, bool level)
  604. {
  605. int bit_index;
  606. bool set;
  607. unsigned long *ptr;
  608. if (number == KVM_ARM_IRQ_CPU_IRQ)
  609. bit_index = __ffs(HCR_VI);
  610. else /* KVM_ARM_IRQ_CPU_FIQ */
  611. bit_index = __ffs(HCR_VF);
  612. ptr = (unsigned long *)&vcpu->arch.irq_lines;
  613. if (level)
  614. set = test_and_set_bit(bit_index, ptr);
  615. else
  616. set = test_and_clear_bit(bit_index, ptr);
  617. /*
  618. * If we didn't change anything, no need to wake up or kick other CPUs
  619. */
  620. if (set == level)
  621. return 0;
  622. /*
  623. * The vcpu irq_lines field was updated, wake up sleeping VCPUs and
  624. * trigger a world-switch round on the running physical CPU to set the
  625. * virtual IRQ/FIQ fields in the HCR appropriately.
  626. */
  627. kvm_vcpu_kick(vcpu);
  628. return 0;
  629. }
  630. int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level)
  631. {
  632. u32 irq = irq_level->irq;
  633. unsigned int irq_type, vcpu_idx, irq_num;
  634. int nrcpus = atomic_read(&kvm->online_vcpus);
  635. struct kvm_vcpu *vcpu = NULL;
  636. bool level = irq_level->level;
  637. irq_type = (irq >> KVM_ARM_IRQ_TYPE_SHIFT) & KVM_ARM_IRQ_TYPE_MASK;
  638. vcpu_idx = (irq >> KVM_ARM_IRQ_VCPU_SHIFT) & KVM_ARM_IRQ_VCPU_MASK;
  639. irq_num = (irq >> KVM_ARM_IRQ_NUM_SHIFT) & KVM_ARM_IRQ_NUM_MASK;
  640. trace_kvm_irq_line(irq_type, vcpu_idx, irq_num, irq_level->level);
  641. if (irq_type != KVM_ARM_IRQ_TYPE_CPU)
  642. return -EINVAL;
  643. if (vcpu_idx >= nrcpus)
  644. return -EINVAL;
  645. vcpu = kvm_get_vcpu(kvm, vcpu_idx);
  646. if (!vcpu)
  647. return -EINVAL;
  648. if (irq_num > KVM_ARM_IRQ_CPU_FIQ)
  649. return -EINVAL;
  650. return vcpu_interrupt_line(vcpu, irq_num, level);
  651. }
  652. long kvm_arch_vcpu_ioctl(struct file *filp,
  653. unsigned int ioctl, unsigned long arg)
  654. {
  655. struct kvm_vcpu *vcpu = filp->private_data;
  656. void __user *argp = (void __user *)arg;
  657. switch (ioctl) {
  658. case KVM_ARM_VCPU_INIT: {
  659. struct kvm_vcpu_init init;
  660. if (copy_from_user(&init, argp, sizeof(init)))
  661. return -EFAULT;
  662. return kvm_vcpu_set_target(vcpu, &init);
  663. }
  664. case KVM_SET_ONE_REG:
  665. case KVM_GET_ONE_REG: {
  666. struct kvm_one_reg reg;
  667. if (copy_from_user(&reg, argp, sizeof(reg)))
  668. return -EFAULT;
  669. if (ioctl == KVM_SET_ONE_REG)
  670. return kvm_arm_set_reg(vcpu, &reg);
  671. else
  672. return kvm_arm_get_reg(vcpu, &reg);
  673. }
  674. case KVM_GET_REG_LIST: {
  675. struct kvm_reg_list __user *user_list = argp;
  676. struct kvm_reg_list reg_list;
  677. unsigned n;
  678. if (copy_from_user(&reg_list, user_list, sizeof(reg_list)))
  679. return -EFAULT;
  680. n = reg_list.n;
  681. reg_list.n = kvm_arm_num_regs(vcpu);
  682. if (copy_to_user(user_list, &reg_list, sizeof(reg_list)))
  683. return -EFAULT;
  684. if (n < reg_list.n)
  685. return -E2BIG;
  686. return kvm_arm_copy_reg_indices(vcpu, user_list->reg);
  687. }
  688. default:
  689. return -EINVAL;
  690. }
  691. }
  692. int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
  693. {
  694. return -EINVAL;
  695. }
  696. long kvm_arch_vm_ioctl(struct file *filp,
  697. unsigned int ioctl, unsigned long arg)
  698. {
  699. return -EINVAL;
  700. }
  701. static void cpu_init_hyp_mode(void *vector)
  702. {
  703. unsigned long long pgd_ptr;
  704. unsigned long pgd_low, pgd_high;
  705. unsigned long hyp_stack_ptr;
  706. unsigned long stack_page;
  707. unsigned long vector_ptr;
  708. /* Switch from the HYP stub to our own HYP init vector */
  709. __hyp_set_vectors((unsigned long)vector);
  710. pgd_ptr = (unsigned long long)kvm_mmu_get_httbr();
  711. pgd_low = (pgd_ptr & ((1ULL << 32) - 1));
  712. pgd_high = (pgd_ptr >> 32ULL);
  713. stack_page = __get_cpu_var(kvm_arm_hyp_stack_page);
  714. hyp_stack_ptr = stack_page + PAGE_SIZE;
  715. vector_ptr = (unsigned long)__kvm_hyp_vector;
  716. /*
  717. * Call initialization code, and switch to the full blown
  718. * HYP code. The init code doesn't need to preserve these registers as
  719. * r1-r3 and r12 are already callee save according to the AAPCS.
  720. * Note that we slightly misuse the prototype by casing the pgd_low to
  721. * a void *.
  722. */
  723. kvm_call_hyp((void *)pgd_low, pgd_high, hyp_stack_ptr, vector_ptr);
  724. }
  725. /**
  726. * Inits Hyp-mode on all online CPUs
  727. */
  728. static int init_hyp_mode(void)
  729. {
  730. phys_addr_t init_phys_addr;
  731. int cpu;
  732. int err = 0;
  733. /*
  734. * Allocate Hyp PGD and setup Hyp identity mapping
  735. */
  736. err = kvm_mmu_init();
  737. if (err)
  738. goto out_err;
  739. /*
  740. * It is probably enough to obtain the default on one
  741. * CPU. It's unlikely to be different on the others.
  742. */
  743. hyp_default_vectors = __hyp_get_vectors();
  744. /*
  745. * Allocate stack pages for Hypervisor-mode
  746. */
  747. for_each_possible_cpu(cpu) {
  748. unsigned long stack_page;
  749. stack_page = __get_free_page(GFP_KERNEL);
  750. if (!stack_page) {
  751. err = -ENOMEM;
  752. goto out_free_stack_pages;
  753. }
  754. per_cpu(kvm_arm_hyp_stack_page, cpu) = stack_page;
  755. }
  756. /*
  757. * Execute the init code on each CPU.
  758. *
  759. * Note: The stack is not mapped yet, so don't do anything else than
  760. * initializing the hypervisor mode on each CPU using a local stack
  761. * space for temporary storage.
  762. */
  763. init_phys_addr = virt_to_phys(__kvm_hyp_init);
  764. for_each_online_cpu(cpu) {
  765. smp_call_function_single(cpu, cpu_init_hyp_mode,
  766. (void *)(long)init_phys_addr, 1);
  767. }
  768. /*
  769. * Unmap the identity mapping
  770. */
  771. kvm_clear_hyp_idmap();
  772. /*
  773. * Map the Hyp-code called directly from the host
  774. */
  775. err = create_hyp_mappings(__kvm_hyp_code_start, __kvm_hyp_code_end);
  776. if (err) {
  777. kvm_err("Cannot map world-switch code\n");
  778. goto out_free_mappings;
  779. }
  780. /*
  781. * Map the Hyp stack pages
  782. */
  783. for_each_possible_cpu(cpu) {
  784. char *stack_page = (char *)per_cpu(kvm_arm_hyp_stack_page, cpu);
  785. err = create_hyp_mappings(stack_page, stack_page + PAGE_SIZE);
  786. if (err) {
  787. kvm_err("Cannot map hyp stack\n");
  788. goto out_free_mappings;
  789. }
  790. }
  791. /*
  792. * Map the host VFP structures
  793. */
  794. kvm_host_vfp_state = alloc_percpu(struct vfp_hard_struct);
  795. if (!kvm_host_vfp_state) {
  796. err = -ENOMEM;
  797. kvm_err("Cannot allocate host VFP state\n");
  798. goto out_free_mappings;
  799. }
  800. for_each_possible_cpu(cpu) {
  801. struct vfp_hard_struct *vfp;
  802. vfp = per_cpu_ptr(kvm_host_vfp_state, cpu);
  803. err = create_hyp_mappings(vfp, vfp + 1);
  804. if (err) {
  805. kvm_err("Cannot map host VFP state: %d\n", err);
  806. goto out_free_vfp;
  807. }
  808. }
  809. kvm_info("Hyp mode initialized successfully\n");
  810. return 0;
  811. out_free_vfp:
  812. free_percpu(kvm_host_vfp_state);
  813. out_free_mappings:
  814. free_hyp_pmds();
  815. out_free_stack_pages:
  816. for_each_possible_cpu(cpu)
  817. free_page(per_cpu(kvm_arm_hyp_stack_page, cpu));
  818. out_err:
  819. kvm_err("error initializing Hyp mode: %d\n", err);
  820. return err;
  821. }
  822. /**
  823. * Initialize Hyp-mode and memory mappings on all CPUs.
  824. */
  825. int kvm_arch_init(void *opaque)
  826. {
  827. int err;
  828. if (!is_hyp_mode_available()) {
  829. kvm_err("HYP mode not available\n");
  830. return -ENODEV;
  831. }
  832. if (kvm_target_cpu() < 0) {
  833. kvm_err("Target CPU not supported!\n");
  834. return -ENODEV;
  835. }
  836. err = init_hyp_mode();
  837. if (err)
  838. goto out_err;
  839. kvm_coproc_table_init();
  840. return 0;
  841. out_err:
  842. return err;
  843. }
  844. /* NOP: Compiling as a module not supported */
  845. void kvm_arch_exit(void)
  846. {
  847. }
  848. static int arm_init(void)
  849. {
  850. int rc = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
  851. return rc;
  852. }
  853. module_init(arm_init);