arm.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089
  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/cpu.h>
  19. #include <linux/cpu_pm.h>
  20. #include <linux/errno.h>
  21. #include <linux/err.h>
  22. #include <linux/kvm_host.h>
  23. #include <linux/module.h>
  24. #include <linux/vmalloc.h>
  25. #include <linux/fs.h>
  26. #include <linux/mman.h>
  27. #include <linux/sched.h>
  28. #include <linux/kvm.h>
  29. #include <trace/events/kvm.h>
  30. #define CREATE_TRACE_POINTS
  31. #include "trace.h"
  32. #include <asm/uaccess.h>
  33. #include <asm/ptrace.h>
  34. #include <asm/mman.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. #ifdef REQUIRES_VIRT
  45. __asm__(".arch_extension virt");
  46. #endif
  47. static DEFINE_PER_CPU(unsigned long, kvm_arm_hyp_stack_page);
  48. static kvm_cpu_context_t __percpu *kvm_host_cpu_state;
  49. static unsigned long hyp_default_vectors;
  50. /* Per-CPU variable containing the currently running vcpu. */
  51. static DEFINE_PER_CPU(struct kvm_vcpu *, kvm_arm_running_vcpu);
  52. /* The VMID used in the VTTBR */
  53. static atomic64_t kvm_vmid_gen = ATOMIC64_INIT(1);
  54. static u8 kvm_next_vmid;
  55. static DEFINE_SPINLOCK(kvm_vmid_lock);
  56. static bool vgic_present;
  57. static void kvm_arm_set_running_vcpu(struct kvm_vcpu *vcpu)
  58. {
  59. BUG_ON(preemptible());
  60. __this_cpu_write(kvm_arm_running_vcpu, vcpu);
  61. }
  62. /**
  63. * kvm_arm_get_running_vcpu - get the vcpu running on the current CPU.
  64. * Must be called from non-preemptible context
  65. */
  66. struct kvm_vcpu *kvm_arm_get_running_vcpu(void)
  67. {
  68. BUG_ON(preemptible());
  69. return __this_cpu_read(kvm_arm_running_vcpu);
  70. }
  71. /**
  72. * kvm_arm_get_running_vcpus - get the per-CPU array of currently running vcpus.
  73. */
  74. struct kvm_vcpu __percpu **kvm_get_running_vcpus(void)
  75. {
  76. return &kvm_arm_running_vcpu;
  77. }
  78. int kvm_arch_hardware_enable(void *garbage)
  79. {
  80. return 0;
  81. }
  82. int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
  83. {
  84. return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
  85. }
  86. void kvm_arch_hardware_disable(void *garbage)
  87. {
  88. }
  89. int kvm_arch_hardware_setup(void)
  90. {
  91. return 0;
  92. }
  93. void kvm_arch_hardware_unsetup(void)
  94. {
  95. }
  96. void kvm_arch_check_processor_compat(void *rtn)
  97. {
  98. *(int *)rtn = 0;
  99. }
  100. void kvm_arch_sync_events(struct kvm *kvm)
  101. {
  102. }
  103. /**
  104. * kvm_arch_init_vm - initializes a VM data structure
  105. * @kvm: pointer to the KVM struct
  106. */
  107. int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
  108. {
  109. int ret = 0;
  110. if (type)
  111. return -EINVAL;
  112. ret = kvm_alloc_stage2_pgd(kvm);
  113. if (ret)
  114. goto out_fail_alloc;
  115. ret = create_hyp_mappings(kvm, kvm + 1);
  116. if (ret)
  117. goto out_free_stage2_pgd;
  118. kvm_timer_init(kvm);
  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 *kvm, struct kvm_memory_slot *free,
  132. struct kvm_memory_slot *dont)
  133. {
  134. }
  135. int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
  136. unsigned long npages)
  137. {
  138. return 0;
  139. }
  140. /**
  141. * kvm_arch_destroy_vm - destroy the VM data structure
  142. * @kvm: pointer to the KVM struct
  143. */
  144. void kvm_arch_destroy_vm(struct kvm *kvm)
  145. {
  146. int i;
  147. kvm_free_stage2_pgd(kvm);
  148. for (i = 0; i < KVM_MAX_VCPUS; ++i) {
  149. if (kvm->vcpus[i]) {
  150. kvm_arch_vcpu_free(kvm->vcpus[i]);
  151. kvm->vcpus[i] = NULL;
  152. }
  153. }
  154. }
  155. int kvm_dev_ioctl_check_extension(long ext)
  156. {
  157. int r;
  158. switch (ext) {
  159. case KVM_CAP_IRQCHIP:
  160. r = vgic_present;
  161. break;
  162. case KVM_CAP_DEVICE_CTRL:
  163. case KVM_CAP_USER_MEMORY:
  164. case KVM_CAP_SYNC_MMU:
  165. case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
  166. case KVM_CAP_ONE_REG:
  167. case KVM_CAP_ARM_PSCI:
  168. r = 1;
  169. break;
  170. case KVM_CAP_COALESCED_MMIO:
  171. r = KVM_COALESCED_MMIO_PAGE_OFFSET;
  172. break;
  173. case KVM_CAP_ARM_SET_DEVICE_ADDR:
  174. r = 1;
  175. break;
  176. case KVM_CAP_NR_VCPUS:
  177. r = num_online_cpus();
  178. break;
  179. case KVM_CAP_MAX_VCPUS:
  180. r = KVM_MAX_VCPUS;
  181. break;
  182. default:
  183. r = kvm_arch_dev_ioctl_check_extension(ext);
  184. break;
  185. }
  186. return r;
  187. }
  188. long kvm_arch_dev_ioctl(struct file *filp,
  189. unsigned int ioctl, unsigned long arg)
  190. {
  191. return -EINVAL;
  192. }
  193. void kvm_arch_memslots_updated(struct kvm *kvm)
  194. {
  195. }
  196. int kvm_arch_prepare_memory_region(struct kvm *kvm,
  197. struct kvm_memory_slot *memslot,
  198. struct kvm_userspace_memory_region *mem,
  199. enum kvm_mr_change change)
  200. {
  201. return 0;
  202. }
  203. void kvm_arch_commit_memory_region(struct kvm *kvm,
  204. struct kvm_userspace_memory_region *mem,
  205. const struct kvm_memory_slot *old,
  206. enum kvm_mr_change change)
  207. {
  208. }
  209. void kvm_arch_flush_shadow_all(struct kvm *kvm)
  210. {
  211. }
  212. void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
  213. struct kvm_memory_slot *slot)
  214. {
  215. }
  216. struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
  217. {
  218. int err;
  219. struct kvm_vcpu *vcpu;
  220. vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
  221. if (!vcpu) {
  222. err = -ENOMEM;
  223. goto out;
  224. }
  225. err = kvm_vcpu_init(vcpu, kvm, id);
  226. if (err)
  227. goto free_vcpu;
  228. err = create_hyp_mappings(vcpu, vcpu + 1);
  229. if (err)
  230. goto vcpu_uninit;
  231. return vcpu;
  232. vcpu_uninit:
  233. kvm_vcpu_uninit(vcpu);
  234. free_vcpu:
  235. kmem_cache_free(kvm_vcpu_cache, vcpu);
  236. out:
  237. return ERR_PTR(err);
  238. }
  239. int kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
  240. {
  241. return 0;
  242. }
  243. void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
  244. {
  245. kvm_mmu_free_memory_caches(vcpu);
  246. kvm_timer_vcpu_terminate(vcpu);
  247. kmem_cache_free(kvm_vcpu_cache, vcpu);
  248. }
  249. void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
  250. {
  251. kvm_arch_vcpu_free(vcpu);
  252. }
  253. int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
  254. {
  255. return 0;
  256. }
  257. int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
  258. {
  259. int ret;
  260. /* Force users to call KVM_ARM_VCPU_INIT */
  261. vcpu->arch.target = -1;
  262. /* Set up VGIC */
  263. ret = kvm_vgic_vcpu_init(vcpu);
  264. if (ret)
  265. return ret;
  266. /* Set up the timer */
  267. kvm_timer_vcpu_init(vcpu);
  268. return 0;
  269. }
  270. void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
  271. {
  272. }
  273. void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  274. {
  275. vcpu->cpu = cpu;
  276. vcpu->arch.host_cpu_context = this_cpu_ptr(kvm_host_cpu_state);
  277. /*
  278. * Check whether this vcpu requires the cache to be flushed on
  279. * this physical CPU. This is a consequence of doing dcache
  280. * operations by set/way on this vcpu. We do it here to be in
  281. * a non-preemptible section.
  282. */
  283. if (cpumask_test_and_clear_cpu(cpu, &vcpu->arch.require_dcache_flush))
  284. flush_cache_all(); /* We'd really want v7_flush_dcache_all() */
  285. kvm_arm_set_running_vcpu(vcpu);
  286. }
  287. void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
  288. {
  289. /*
  290. * The arch-generic KVM code expects the cpu field of a vcpu to be -1
  291. * if the vcpu is no longer assigned to a cpu. This is used for the
  292. * optimized make_all_cpus_request path.
  293. */
  294. vcpu->cpu = -1;
  295. kvm_arm_set_running_vcpu(NULL);
  296. }
  297. int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
  298. struct kvm_guest_debug *dbg)
  299. {
  300. return -EINVAL;
  301. }
  302. int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
  303. struct kvm_mp_state *mp_state)
  304. {
  305. return -EINVAL;
  306. }
  307. int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
  308. struct kvm_mp_state *mp_state)
  309. {
  310. return -EINVAL;
  311. }
  312. /**
  313. * kvm_arch_vcpu_runnable - determine if the vcpu can be scheduled
  314. * @v: The VCPU pointer
  315. *
  316. * If the guest CPU is not waiting for interrupts or an interrupt line is
  317. * asserted, the CPU is by definition runnable.
  318. */
  319. int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
  320. {
  321. return !!v->arch.irq_lines || kvm_vgic_vcpu_pending_irq(v);
  322. }
  323. /* Just ensure a guest exit from a particular CPU */
  324. static void exit_vm_noop(void *info)
  325. {
  326. }
  327. void force_vm_exit(const cpumask_t *mask)
  328. {
  329. smp_call_function_many(mask, exit_vm_noop, NULL, true);
  330. }
  331. /**
  332. * need_new_vmid_gen - check that the VMID is still valid
  333. * @kvm: The VM's VMID to checkt
  334. *
  335. * return true if there is a new generation of VMIDs being used
  336. *
  337. * The hardware supports only 256 values with the value zero reserved for the
  338. * host, so we check if an assigned value belongs to a previous generation,
  339. * which which requires us to assign a new value. If we're the first to use a
  340. * VMID for the new generation, we must flush necessary caches and TLBs on all
  341. * CPUs.
  342. */
  343. static bool need_new_vmid_gen(struct kvm *kvm)
  344. {
  345. return unlikely(kvm->arch.vmid_gen != atomic64_read(&kvm_vmid_gen));
  346. }
  347. /**
  348. * update_vttbr - Update the VTTBR with a valid VMID before the guest runs
  349. * @kvm The guest that we are about to run
  350. *
  351. * Called from kvm_arch_vcpu_ioctl_run before entering the guest to ensure the
  352. * VM has a valid VMID, otherwise assigns a new one and flushes corresponding
  353. * caches and TLBs.
  354. */
  355. static void update_vttbr(struct kvm *kvm)
  356. {
  357. phys_addr_t pgd_phys;
  358. u64 vmid;
  359. if (!need_new_vmid_gen(kvm))
  360. return;
  361. spin_lock(&kvm_vmid_lock);
  362. /*
  363. * We need to re-check the vmid_gen here to ensure that if another vcpu
  364. * already allocated a valid vmid for this vm, then this vcpu should
  365. * use the same vmid.
  366. */
  367. if (!need_new_vmid_gen(kvm)) {
  368. spin_unlock(&kvm_vmid_lock);
  369. return;
  370. }
  371. /* First user of a new VMID generation? */
  372. if (unlikely(kvm_next_vmid == 0)) {
  373. atomic64_inc(&kvm_vmid_gen);
  374. kvm_next_vmid = 1;
  375. /*
  376. * On SMP we know no other CPUs can use this CPU's or each
  377. * other's VMID after force_vm_exit returns since the
  378. * kvm_vmid_lock blocks them from reentry to the guest.
  379. */
  380. force_vm_exit(cpu_all_mask);
  381. /*
  382. * Now broadcast TLB + ICACHE invalidation over the inner
  383. * shareable domain to make sure all data structures are
  384. * clean.
  385. */
  386. kvm_call_hyp(__kvm_flush_vm_context);
  387. }
  388. kvm->arch.vmid_gen = atomic64_read(&kvm_vmid_gen);
  389. kvm->arch.vmid = kvm_next_vmid;
  390. kvm_next_vmid++;
  391. /* update vttbr to be used with the new vmid */
  392. pgd_phys = virt_to_phys(kvm->arch.pgd);
  393. vmid = ((u64)(kvm->arch.vmid) << VTTBR_VMID_SHIFT) & VTTBR_VMID_MASK;
  394. kvm->arch.vttbr = pgd_phys & VTTBR_BADDR_MASK;
  395. kvm->arch.vttbr |= vmid;
  396. spin_unlock(&kvm_vmid_lock);
  397. }
  398. static int kvm_vcpu_first_run_init(struct kvm_vcpu *vcpu)
  399. {
  400. int ret;
  401. if (likely(vcpu->arch.has_run_once))
  402. return 0;
  403. vcpu->arch.has_run_once = true;
  404. /*
  405. * Initialize the VGIC before running a vcpu the first time on
  406. * this VM.
  407. */
  408. if (unlikely(!vgic_initialized(vcpu->kvm))) {
  409. ret = kvm_vgic_init(vcpu->kvm);
  410. if (ret)
  411. return ret;
  412. }
  413. return 0;
  414. }
  415. static void vcpu_pause(struct kvm_vcpu *vcpu)
  416. {
  417. wait_queue_head_t *wq = kvm_arch_vcpu_wq(vcpu);
  418. wait_event_interruptible(*wq, !vcpu->arch.pause);
  419. }
  420. static int kvm_vcpu_initialized(struct kvm_vcpu *vcpu)
  421. {
  422. return vcpu->arch.target >= 0;
  423. }
  424. /**
  425. * kvm_arch_vcpu_ioctl_run - the main VCPU run function to execute guest code
  426. * @vcpu: The VCPU pointer
  427. * @run: The kvm_run structure pointer used for userspace state exchange
  428. *
  429. * This function is called through the VCPU_RUN ioctl called from user space. It
  430. * will execute VM code in a loop until the time slice for the process is used
  431. * or some emulation is needed from user space in which case the function will
  432. * return with return value 0 and with the kvm_run structure filled in with the
  433. * required data for the requested emulation.
  434. */
  435. int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
  436. {
  437. int ret;
  438. sigset_t sigsaved;
  439. if (unlikely(!kvm_vcpu_initialized(vcpu)))
  440. return -ENOEXEC;
  441. ret = kvm_vcpu_first_run_init(vcpu);
  442. if (ret)
  443. return ret;
  444. if (run->exit_reason == KVM_EXIT_MMIO) {
  445. ret = kvm_handle_mmio_return(vcpu, vcpu->run);
  446. if (ret)
  447. return ret;
  448. }
  449. if (vcpu->sigset_active)
  450. sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
  451. ret = 1;
  452. run->exit_reason = KVM_EXIT_UNKNOWN;
  453. while (ret > 0) {
  454. /*
  455. * Check conditions before entering the guest
  456. */
  457. cond_resched();
  458. update_vttbr(vcpu->kvm);
  459. if (vcpu->arch.pause)
  460. vcpu_pause(vcpu);
  461. kvm_vgic_flush_hwstate(vcpu);
  462. kvm_timer_flush_hwstate(vcpu);
  463. local_irq_disable();
  464. /*
  465. * Re-check atomic conditions
  466. */
  467. if (signal_pending(current)) {
  468. ret = -EINTR;
  469. run->exit_reason = KVM_EXIT_INTR;
  470. }
  471. if (ret <= 0 || need_new_vmid_gen(vcpu->kvm)) {
  472. local_irq_enable();
  473. kvm_timer_sync_hwstate(vcpu);
  474. kvm_vgic_sync_hwstate(vcpu);
  475. continue;
  476. }
  477. /**************************************************************
  478. * Enter the guest
  479. */
  480. trace_kvm_entry(*vcpu_pc(vcpu));
  481. kvm_guest_enter();
  482. vcpu->mode = IN_GUEST_MODE;
  483. ret = kvm_call_hyp(__kvm_vcpu_run, vcpu);
  484. vcpu->mode = OUTSIDE_GUEST_MODE;
  485. vcpu->arch.last_pcpu = smp_processor_id();
  486. kvm_guest_exit();
  487. trace_kvm_exit(*vcpu_pc(vcpu));
  488. /*
  489. * We may have taken a host interrupt in HYP mode (ie
  490. * while executing the guest). This interrupt is still
  491. * pending, as we haven't serviced it yet!
  492. *
  493. * We're now back in SVC mode, with interrupts
  494. * disabled. Enabling the interrupts now will have
  495. * the effect of taking the interrupt again, in SVC
  496. * mode this time.
  497. */
  498. local_irq_enable();
  499. /*
  500. * Back from guest
  501. *************************************************************/
  502. kvm_timer_sync_hwstate(vcpu);
  503. kvm_vgic_sync_hwstate(vcpu);
  504. ret = handle_exit(vcpu, run, ret);
  505. }
  506. if (vcpu->sigset_active)
  507. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  508. return ret;
  509. }
  510. static int vcpu_interrupt_line(struct kvm_vcpu *vcpu, int number, bool level)
  511. {
  512. int bit_index;
  513. bool set;
  514. unsigned long *ptr;
  515. if (number == KVM_ARM_IRQ_CPU_IRQ)
  516. bit_index = __ffs(HCR_VI);
  517. else /* KVM_ARM_IRQ_CPU_FIQ */
  518. bit_index = __ffs(HCR_VF);
  519. ptr = (unsigned long *)&vcpu->arch.irq_lines;
  520. if (level)
  521. set = test_and_set_bit(bit_index, ptr);
  522. else
  523. set = test_and_clear_bit(bit_index, ptr);
  524. /*
  525. * If we didn't change anything, no need to wake up or kick other CPUs
  526. */
  527. if (set == level)
  528. return 0;
  529. /*
  530. * The vcpu irq_lines field was updated, wake up sleeping VCPUs and
  531. * trigger a world-switch round on the running physical CPU to set the
  532. * virtual IRQ/FIQ fields in the HCR appropriately.
  533. */
  534. kvm_vcpu_kick(vcpu);
  535. return 0;
  536. }
  537. int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level,
  538. bool line_status)
  539. {
  540. u32 irq = irq_level->irq;
  541. unsigned int irq_type, vcpu_idx, irq_num;
  542. int nrcpus = atomic_read(&kvm->online_vcpus);
  543. struct kvm_vcpu *vcpu = NULL;
  544. bool level = irq_level->level;
  545. irq_type = (irq >> KVM_ARM_IRQ_TYPE_SHIFT) & KVM_ARM_IRQ_TYPE_MASK;
  546. vcpu_idx = (irq >> KVM_ARM_IRQ_VCPU_SHIFT) & KVM_ARM_IRQ_VCPU_MASK;
  547. irq_num = (irq >> KVM_ARM_IRQ_NUM_SHIFT) & KVM_ARM_IRQ_NUM_MASK;
  548. trace_kvm_irq_line(irq_type, vcpu_idx, irq_num, irq_level->level);
  549. switch (irq_type) {
  550. case KVM_ARM_IRQ_TYPE_CPU:
  551. if (irqchip_in_kernel(kvm))
  552. return -ENXIO;
  553. if (vcpu_idx >= nrcpus)
  554. return -EINVAL;
  555. vcpu = kvm_get_vcpu(kvm, vcpu_idx);
  556. if (!vcpu)
  557. return -EINVAL;
  558. if (irq_num > KVM_ARM_IRQ_CPU_FIQ)
  559. return -EINVAL;
  560. return vcpu_interrupt_line(vcpu, irq_num, level);
  561. case KVM_ARM_IRQ_TYPE_PPI:
  562. if (!irqchip_in_kernel(kvm))
  563. return -ENXIO;
  564. if (vcpu_idx >= nrcpus)
  565. return -EINVAL;
  566. vcpu = kvm_get_vcpu(kvm, vcpu_idx);
  567. if (!vcpu)
  568. return -EINVAL;
  569. if (irq_num < VGIC_NR_SGIS || irq_num >= VGIC_NR_PRIVATE_IRQS)
  570. return -EINVAL;
  571. return kvm_vgic_inject_irq(kvm, vcpu->vcpu_id, irq_num, level);
  572. case KVM_ARM_IRQ_TYPE_SPI:
  573. if (!irqchip_in_kernel(kvm))
  574. return -ENXIO;
  575. if (irq_num < VGIC_NR_PRIVATE_IRQS ||
  576. irq_num > KVM_ARM_IRQ_GIC_MAX)
  577. return -EINVAL;
  578. return kvm_vgic_inject_irq(kvm, 0, irq_num, level);
  579. }
  580. return -EINVAL;
  581. }
  582. static int kvm_arch_vcpu_ioctl_vcpu_init(struct kvm_vcpu *vcpu,
  583. struct kvm_vcpu_init *init)
  584. {
  585. int ret;
  586. ret = kvm_vcpu_set_target(vcpu, init);
  587. if (ret)
  588. return ret;
  589. /*
  590. * Handle the "start in power-off" case by marking the VCPU as paused.
  591. */
  592. if (__test_and_clear_bit(KVM_ARM_VCPU_POWER_OFF, vcpu->arch.features))
  593. vcpu->arch.pause = true;
  594. return 0;
  595. }
  596. long kvm_arch_vcpu_ioctl(struct file *filp,
  597. unsigned int ioctl, unsigned long arg)
  598. {
  599. struct kvm_vcpu *vcpu = filp->private_data;
  600. void __user *argp = (void __user *)arg;
  601. switch (ioctl) {
  602. case KVM_ARM_VCPU_INIT: {
  603. struct kvm_vcpu_init init;
  604. if (copy_from_user(&init, argp, sizeof(init)))
  605. return -EFAULT;
  606. return kvm_arch_vcpu_ioctl_vcpu_init(vcpu, &init);
  607. }
  608. case KVM_SET_ONE_REG:
  609. case KVM_GET_ONE_REG: {
  610. struct kvm_one_reg reg;
  611. if (unlikely(!kvm_vcpu_initialized(vcpu)))
  612. return -ENOEXEC;
  613. if (copy_from_user(&reg, argp, sizeof(reg)))
  614. return -EFAULT;
  615. if (ioctl == KVM_SET_ONE_REG)
  616. return kvm_arm_set_reg(vcpu, &reg);
  617. else
  618. return kvm_arm_get_reg(vcpu, &reg);
  619. }
  620. case KVM_GET_REG_LIST: {
  621. struct kvm_reg_list __user *user_list = argp;
  622. struct kvm_reg_list reg_list;
  623. unsigned n;
  624. if (unlikely(!kvm_vcpu_initialized(vcpu)))
  625. return -ENOEXEC;
  626. if (copy_from_user(&reg_list, user_list, sizeof(reg_list)))
  627. return -EFAULT;
  628. n = reg_list.n;
  629. reg_list.n = kvm_arm_num_regs(vcpu);
  630. if (copy_to_user(user_list, &reg_list, sizeof(reg_list)))
  631. return -EFAULT;
  632. if (n < reg_list.n)
  633. return -E2BIG;
  634. return kvm_arm_copy_reg_indices(vcpu, user_list->reg);
  635. }
  636. default:
  637. return -EINVAL;
  638. }
  639. }
  640. int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
  641. {
  642. return -EINVAL;
  643. }
  644. static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm,
  645. struct kvm_arm_device_addr *dev_addr)
  646. {
  647. unsigned long dev_id, type;
  648. dev_id = (dev_addr->id & KVM_ARM_DEVICE_ID_MASK) >>
  649. KVM_ARM_DEVICE_ID_SHIFT;
  650. type = (dev_addr->id & KVM_ARM_DEVICE_TYPE_MASK) >>
  651. KVM_ARM_DEVICE_TYPE_SHIFT;
  652. switch (dev_id) {
  653. case KVM_ARM_DEVICE_VGIC_V2:
  654. if (!vgic_present)
  655. return -ENXIO;
  656. return kvm_vgic_addr(kvm, type, &dev_addr->addr, true);
  657. default:
  658. return -ENODEV;
  659. }
  660. }
  661. long kvm_arch_vm_ioctl(struct file *filp,
  662. unsigned int ioctl, unsigned long arg)
  663. {
  664. struct kvm *kvm = filp->private_data;
  665. void __user *argp = (void __user *)arg;
  666. switch (ioctl) {
  667. case KVM_CREATE_IRQCHIP: {
  668. if (vgic_present)
  669. return kvm_vgic_create(kvm);
  670. else
  671. return -ENXIO;
  672. }
  673. case KVM_ARM_SET_DEVICE_ADDR: {
  674. struct kvm_arm_device_addr dev_addr;
  675. if (copy_from_user(&dev_addr, argp, sizeof(dev_addr)))
  676. return -EFAULT;
  677. return kvm_vm_ioctl_set_device_addr(kvm, &dev_addr);
  678. }
  679. case KVM_ARM_PREFERRED_TARGET: {
  680. int err;
  681. struct kvm_vcpu_init init;
  682. err = kvm_vcpu_preferred_target(&init);
  683. if (err)
  684. return err;
  685. if (copy_to_user(argp, &init, sizeof(init)))
  686. return -EFAULT;
  687. return 0;
  688. }
  689. default:
  690. return -EINVAL;
  691. }
  692. }
  693. static void cpu_init_hyp_mode(void *dummy)
  694. {
  695. phys_addr_t boot_pgd_ptr;
  696. phys_addr_t pgd_ptr;
  697. unsigned long hyp_stack_ptr;
  698. unsigned long stack_page;
  699. unsigned long vector_ptr;
  700. /* Switch from the HYP stub to our own HYP init vector */
  701. __hyp_set_vectors(kvm_get_idmap_vector());
  702. boot_pgd_ptr = kvm_mmu_get_boot_httbr();
  703. pgd_ptr = kvm_mmu_get_httbr();
  704. stack_page = __this_cpu_read(kvm_arm_hyp_stack_page);
  705. hyp_stack_ptr = stack_page + PAGE_SIZE;
  706. vector_ptr = (unsigned long)__kvm_hyp_vector;
  707. __cpu_init_hyp_mode(boot_pgd_ptr, pgd_ptr, hyp_stack_ptr, vector_ptr);
  708. }
  709. static int hyp_init_cpu_notify(struct notifier_block *self,
  710. unsigned long action, void *cpu)
  711. {
  712. switch (action) {
  713. case CPU_STARTING:
  714. case CPU_STARTING_FROZEN:
  715. cpu_init_hyp_mode(NULL);
  716. break;
  717. }
  718. return NOTIFY_OK;
  719. }
  720. static struct notifier_block hyp_init_cpu_nb = {
  721. .notifier_call = hyp_init_cpu_notify,
  722. };
  723. #ifdef CONFIG_CPU_PM
  724. static int hyp_init_cpu_pm_notifier(struct notifier_block *self,
  725. unsigned long cmd,
  726. void *v)
  727. {
  728. if (cmd == CPU_PM_EXIT &&
  729. __hyp_get_vectors() == hyp_default_vectors) {
  730. cpu_init_hyp_mode(NULL);
  731. return NOTIFY_OK;
  732. }
  733. return NOTIFY_DONE;
  734. }
  735. static struct notifier_block hyp_init_cpu_pm_nb = {
  736. .notifier_call = hyp_init_cpu_pm_notifier,
  737. };
  738. static void __init hyp_cpu_pm_init(void)
  739. {
  740. cpu_pm_register_notifier(&hyp_init_cpu_pm_nb);
  741. }
  742. #else
  743. static inline void hyp_cpu_pm_init(void)
  744. {
  745. }
  746. #endif
  747. /**
  748. * Inits Hyp-mode on all online CPUs
  749. */
  750. static int init_hyp_mode(void)
  751. {
  752. int cpu;
  753. int err = 0;
  754. /*
  755. * Allocate Hyp PGD and setup Hyp identity mapping
  756. */
  757. err = kvm_mmu_init();
  758. if (err)
  759. goto out_err;
  760. /*
  761. * It is probably enough to obtain the default on one
  762. * CPU. It's unlikely to be different on the others.
  763. */
  764. hyp_default_vectors = __hyp_get_vectors();
  765. /*
  766. * Allocate stack pages for Hypervisor-mode
  767. */
  768. for_each_possible_cpu(cpu) {
  769. unsigned long stack_page;
  770. stack_page = __get_free_page(GFP_KERNEL);
  771. if (!stack_page) {
  772. err = -ENOMEM;
  773. goto out_free_stack_pages;
  774. }
  775. per_cpu(kvm_arm_hyp_stack_page, cpu) = stack_page;
  776. }
  777. /*
  778. * Map the Hyp-code called directly from the host
  779. */
  780. err = create_hyp_mappings(__kvm_hyp_code_start, __kvm_hyp_code_end);
  781. if (err) {
  782. kvm_err("Cannot map world-switch code\n");
  783. goto out_free_mappings;
  784. }
  785. /*
  786. * Map the Hyp stack pages
  787. */
  788. for_each_possible_cpu(cpu) {
  789. char *stack_page = (char *)per_cpu(kvm_arm_hyp_stack_page, cpu);
  790. err = create_hyp_mappings(stack_page, stack_page + PAGE_SIZE);
  791. if (err) {
  792. kvm_err("Cannot map hyp stack\n");
  793. goto out_free_mappings;
  794. }
  795. }
  796. /*
  797. * Map the host CPU structures
  798. */
  799. kvm_host_cpu_state = alloc_percpu(kvm_cpu_context_t);
  800. if (!kvm_host_cpu_state) {
  801. err = -ENOMEM;
  802. kvm_err("Cannot allocate host CPU state\n");
  803. goto out_free_mappings;
  804. }
  805. for_each_possible_cpu(cpu) {
  806. kvm_cpu_context_t *cpu_ctxt;
  807. cpu_ctxt = per_cpu_ptr(kvm_host_cpu_state, cpu);
  808. err = create_hyp_mappings(cpu_ctxt, cpu_ctxt + 1);
  809. if (err) {
  810. kvm_err("Cannot map host CPU state: %d\n", err);
  811. goto out_free_context;
  812. }
  813. }
  814. /*
  815. * Execute the init code on each CPU.
  816. */
  817. on_each_cpu(cpu_init_hyp_mode, NULL, 1);
  818. /*
  819. * Init HYP view of VGIC
  820. */
  821. err = kvm_vgic_hyp_init();
  822. if (err)
  823. goto out_free_context;
  824. #ifdef CONFIG_KVM_ARM_VGIC
  825. vgic_present = true;
  826. #endif
  827. /*
  828. * Init HYP architected timer support
  829. */
  830. err = kvm_timer_hyp_init();
  831. if (err)
  832. goto out_free_mappings;
  833. #ifndef CONFIG_HOTPLUG_CPU
  834. free_boot_hyp_pgd();
  835. #endif
  836. kvm_perf_init();
  837. kvm_info("Hyp mode initialized successfully\n");
  838. return 0;
  839. out_free_context:
  840. free_percpu(kvm_host_cpu_state);
  841. out_free_mappings:
  842. free_hyp_pgds();
  843. out_free_stack_pages:
  844. for_each_possible_cpu(cpu)
  845. free_page(per_cpu(kvm_arm_hyp_stack_page, cpu));
  846. out_err:
  847. kvm_err("error initializing Hyp mode: %d\n", err);
  848. return err;
  849. }
  850. static void check_kvm_target_cpu(void *ret)
  851. {
  852. *(int *)ret = kvm_target_cpu();
  853. }
  854. /**
  855. * Initialize Hyp-mode and memory mappings on all CPUs.
  856. */
  857. int kvm_arch_init(void *opaque)
  858. {
  859. int err;
  860. int ret, cpu;
  861. if (!is_hyp_mode_available()) {
  862. kvm_err("HYP mode not available\n");
  863. return -ENODEV;
  864. }
  865. for_each_online_cpu(cpu) {
  866. smp_call_function_single(cpu, check_kvm_target_cpu, &ret, 1);
  867. if (ret < 0) {
  868. kvm_err("Error, CPU %d not supported!\n", cpu);
  869. return -ENODEV;
  870. }
  871. }
  872. cpu_notifier_register_begin();
  873. err = init_hyp_mode();
  874. if (err)
  875. goto out_err;
  876. err = __register_cpu_notifier(&hyp_init_cpu_nb);
  877. if (err) {
  878. kvm_err("Cannot register HYP init CPU notifier (%d)\n", err);
  879. goto out_err;
  880. }
  881. cpu_notifier_register_done();
  882. hyp_cpu_pm_init();
  883. kvm_coproc_table_init();
  884. return 0;
  885. out_err:
  886. cpu_notifier_register_done();
  887. return err;
  888. }
  889. /* NOP: Compiling as a module not supported */
  890. void kvm_arch_exit(void)
  891. {
  892. kvm_perf_teardown();
  893. }
  894. static int arm_init(void)
  895. {
  896. int rc = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
  897. return rc;
  898. }
  899. module_init(arm_init);