arm.c 23 KB

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