arm.c 27 KB

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