arm.c 27 KB

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