arm.c 22 KB

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