arm.c 23 KB

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