arm.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180
  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 void kvm_arm_set_running_vcpu(struct kvm_vcpu *vcpu)
  57. {
  58. BUG_ON(preemptible());
  59. __this_cpu_write(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 __this_cpu_read(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)
  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. int kvm_arch_hardware_setup(void)
  86. {
  87. return 0;
  88. }
  89. void kvm_arch_check_processor_compat(void *rtn)
  90. {
  91. *(int *)rtn = 0;
  92. }
  93. /**
  94. * kvm_arch_init_vm - initializes a VM data structure
  95. * @kvm: pointer to the KVM struct
  96. */
  97. int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
  98. {
  99. int ret = 0;
  100. if (type)
  101. return -EINVAL;
  102. ret = kvm_alloc_stage2_pgd(kvm);
  103. if (ret)
  104. goto out_fail_alloc;
  105. ret = create_hyp_mappings(kvm, kvm + 1);
  106. if (ret)
  107. goto out_free_stage2_pgd;
  108. kvm_vgic_early_init(kvm);
  109. kvm_timer_init(kvm);
  110. /* Mark the initial VMID generation invalid */
  111. kvm->arch.vmid_gen = 0;
  112. /* The maximum number of VCPUs is limited by the host's GIC model */
  113. kvm->arch.max_vcpus = kvm_vgic_get_max_vcpus();
  114. return ret;
  115. out_free_stage2_pgd:
  116. kvm_free_stage2_pgd(kvm);
  117. out_fail_alloc:
  118. return ret;
  119. }
  120. int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
  121. {
  122. return VM_FAULT_SIGBUS;
  123. }
  124. /**
  125. * kvm_arch_destroy_vm - destroy the VM data structure
  126. * @kvm: pointer to the KVM struct
  127. */
  128. void kvm_arch_destroy_vm(struct kvm *kvm)
  129. {
  130. int i;
  131. kvm_free_stage2_pgd(kvm);
  132. for (i = 0; i < KVM_MAX_VCPUS; ++i) {
  133. if (kvm->vcpus[i]) {
  134. kvm_arch_vcpu_free(kvm->vcpus[i]);
  135. kvm->vcpus[i] = NULL;
  136. }
  137. }
  138. kvm_vgic_destroy(kvm);
  139. }
  140. int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
  141. {
  142. int r;
  143. switch (ext) {
  144. case KVM_CAP_IRQCHIP:
  145. case KVM_CAP_IOEVENTFD:
  146. case KVM_CAP_DEVICE_CTRL:
  147. case KVM_CAP_USER_MEMORY:
  148. case KVM_CAP_SYNC_MMU:
  149. case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
  150. case KVM_CAP_ONE_REG:
  151. case KVM_CAP_ARM_PSCI:
  152. case KVM_CAP_ARM_PSCI_0_2:
  153. case KVM_CAP_READONLY_MEM:
  154. case KVM_CAP_MP_STATE:
  155. r = 1;
  156. break;
  157. case KVM_CAP_COALESCED_MMIO:
  158. r = KVM_COALESCED_MMIO_PAGE_OFFSET;
  159. break;
  160. case KVM_CAP_ARM_SET_DEVICE_ADDR:
  161. r = 1;
  162. break;
  163. case KVM_CAP_NR_VCPUS:
  164. r = num_online_cpus();
  165. break;
  166. case KVM_CAP_MAX_VCPUS:
  167. r = KVM_MAX_VCPUS;
  168. break;
  169. default:
  170. r = kvm_arch_dev_ioctl_check_extension(ext);
  171. break;
  172. }
  173. return r;
  174. }
  175. long kvm_arch_dev_ioctl(struct file *filp,
  176. unsigned int ioctl, unsigned long arg)
  177. {
  178. return -EINVAL;
  179. }
  180. struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
  181. {
  182. int err;
  183. struct kvm_vcpu *vcpu;
  184. if (irqchip_in_kernel(kvm) && vgic_initialized(kvm)) {
  185. err = -EBUSY;
  186. goto out;
  187. }
  188. if (id >= kvm->arch.max_vcpus) {
  189. err = -EINVAL;
  190. goto out;
  191. }
  192. vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
  193. if (!vcpu) {
  194. err = -ENOMEM;
  195. goto out;
  196. }
  197. err = kvm_vcpu_init(vcpu, kvm, id);
  198. if (err)
  199. goto free_vcpu;
  200. err = create_hyp_mappings(vcpu, vcpu + 1);
  201. if (err)
  202. goto vcpu_uninit;
  203. return vcpu;
  204. vcpu_uninit:
  205. kvm_vcpu_uninit(vcpu);
  206. free_vcpu:
  207. kmem_cache_free(kvm_vcpu_cache, vcpu);
  208. out:
  209. return ERR_PTR(err);
  210. }
  211. void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
  212. {
  213. kvm_vgic_vcpu_early_init(vcpu);
  214. }
  215. void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
  216. {
  217. kvm_mmu_free_memory_caches(vcpu);
  218. kvm_timer_vcpu_terminate(vcpu);
  219. kvm_vgic_vcpu_destroy(vcpu);
  220. kmem_cache_free(kvm_vcpu_cache, vcpu);
  221. }
  222. void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
  223. {
  224. kvm_arch_vcpu_free(vcpu);
  225. }
  226. int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
  227. {
  228. return kvm_timer_should_fire(vcpu);
  229. }
  230. int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
  231. {
  232. /* Force users to call KVM_ARM_VCPU_INIT */
  233. vcpu->arch.target = -1;
  234. bitmap_zero(vcpu->arch.features, KVM_VCPU_MAX_FEATURES);
  235. /* Set up the timer */
  236. kvm_timer_vcpu_init(vcpu);
  237. kvm_arm_reset_debug_ptr(vcpu);
  238. return 0;
  239. }
  240. void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  241. {
  242. vcpu->cpu = cpu;
  243. vcpu->arch.host_cpu_context = this_cpu_ptr(kvm_host_cpu_state);
  244. kvm_arm_set_running_vcpu(vcpu);
  245. }
  246. void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
  247. {
  248. /*
  249. * The arch-generic KVM code expects the cpu field of a vcpu to be -1
  250. * if the vcpu is no longer assigned to a cpu. This is used for the
  251. * optimized make_all_cpus_request path.
  252. */
  253. vcpu->cpu = -1;
  254. kvm_arm_set_running_vcpu(NULL);
  255. }
  256. int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
  257. struct kvm_mp_state *mp_state)
  258. {
  259. if (vcpu->arch.pause)
  260. mp_state->mp_state = KVM_MP_STATE_STOPPED;
  261. else
  262. mp_state->mp_state = KVM_MP_STATE_RUNNABLE;
  263. return 0;
  264. }
  265. int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
  266. struct kvm_mp_state *mp_state)
  267. {
  268. switch (mp_state->mp_state) {
  269. case KVM_MP_STATE_RUNNABLE:
  270. vcpu->arch.pause = false;
  271. break;
  272. case KVM_MP_STATE_STOPPED:
  273. vcpu->arch.pause = true;
  274. break;
  275. default:
  276. return -EINVAL;
  277. }
  278. return 0;
  279. }
  280. /**
  281. * kvm_arch_vcpu_runnable - determine if the vcpu can be scheduled
  282. * @v: The VCPU pointer
  283. *
  284. * If the guest CPU is not waiting for interrupts or an interrupt line is
  285. * asserted, the CPU is by definition runnable.
  286. */
  287. int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
  288. {
  289. return !!v->arch.irq_lines || kvm_vgic_vcpu_pending_irq(v);
  290. }
  291. /* Just ensure a guest exit from a particular CPU */
  292. static void exit_vm_noop(void *info)
  293. {
  294. }
  295. void force_vm_exit(const cpumask_t *mask)
  296. {
  297. smp_call_function_many(mask, exit_vm_noop, NULL, true);
  298. }
  299. /**
  300. * need_new_vmid_gen - check that the VMID is still valid
  301. * @kvm: The VM's VMID to checkt
  302. *
  303. * return true if there is a new generation of VMIDs being used
  304. *
  305. * The hardware supports only 256 values with the value zero reserved for the
  306. * host, so we check if an assigned value belongs to a previous generation,
  307. * which which requires us to assign a new value. If we're the first to use a
  308. * VMID for the new generation, we must flush necessary caches and TLBs on all
  309. * CPUs.
  310. */
  311. static bool need_new_vmid_gen(struct kvm *kvm)
  312. {
  313. return unlikely(kvm->arch.vmid_gen != atomic64_read(&kvm_vmid_gen));
  314. }
  315. /**
  316. * update_vttbr - Update the VTTBR with a valid VMID before the guest runs
  317. * @kvm The guest that we are about to run
  318. *
  319. * Called from kvm_arch_vcpu_ioctl_run before entering the guest to ensure the
  320. * VM has a valid VMID, otherwise assigns a new one and flushes corresponding
  321. * caches and TLBs.
  322. */
  323. static void update_vttbr(struct kvm *kvm)
  324. {
  325. phys_addr_t pgd_phys;
  326. u64 vmid;
  327. if (!need_new_vmid_gen(kvm))
  328. return;
  329. spin_lock(&kvm_vmid_lock);
  330. /*
  331. * We need to re-check the vmid_gen here to ensure that if another vcpu
  332. * already allocated a valid vmid for this vm, then this vcpu should
  333. * use the same vmid.
  334. */
  335. if (!need_new_vmid_gen(kvm)) {
  336. spin_unlock(&kvm_vmid_lock);
  337. return;
  338. }
  339. /* First user of a new VMID generation? */
  340. if (unlikely(kvm_next_vmid == 0)) {
  341. atomic64_inc(&kvm_vmid_gen);
  342. kvm_next_vmid = 1;
  343. /*
  344. * On SMP we know no other CPUs can use this CPU's or each
  345. * other's VMID after force_vm_exit returns since the
  346. * kvm_vmid_lock blocks them from reentry to the guest.
  347. */
  348. force_vm_exit(cpu_all_mask);
  349. /*
  350. * Now broadcast TLB + ICACHE invalidation over the inner
  351. * shareable domain to make sure all data structures are
  352. * clean.
  353. */
  354. kvm_call_hyp(__kvm_flush_vm_context);
  355. }
  356. kvm->arch.vmid_gen = atomic64_read(&kvm_vmid_gen);
  357. kvm->arch.vmid = kvm_next_vmid;
  358. kvm_next_vmid++;
  359. /* update vttbr to be used with the new vmid */
  360. pgd_phys = virt_to_phys(kvm_get_hwpgd(kvm));
  361. BUG_ON(pgd_phys & ~VTTBR_BADDR_MASK);
  362. vmid = ((u64)(kvm->arch.vmid) << VTTBR_VMID_SHIFT) & VTTBR_VMID_MASK;
  363. kvm->arch.vttbr = pgd_phys | vmid;
  364. spin_unlock(&kvm_vmid_lock);
  365. }
  366. static int kvm_vcpu_first_run_init(struct kvm_vcpu *vcpu)
  367. {
  368. struct kvm *kvm = vcpu->kvm;
  369. int ret;
  370. if (likely(vcpu->arch.has_run_once))
  371. return 0;
  372. vcpu->arch.has_run_once = true;
  373. /*
  374. * Map the VGIC hardware resources before running a vcpu the first
  375. * time on this VM.
  376. */
  377. if (unlikely(irqchip_in_kernel(kvm) && !vgic_ready(kvm))) {
  378. ret = kvm_vgic_map_resources(kvm);
  379. if (ret)
  380. return ret;
  381. }
  382. /*
  383. * Enable the arch timers only if we have an in-kernel VGIC
  384. * and it has been properly initialized, since we cannot handle
  385. * interrupts from the virtual timer with a userspace gic.
  386. */
  387. if (irqchip_in_kernel(kvm) && vgic_initialized(kvm))
  388. kvm_timer_enable(kvm);
  389. return 0;
  390. }
  391. bool kvm_arch_intc_initialized(struct kvm *kvm)
  392. {
  393. return vgic_initialized(kvm);
  394. }
  395. static void vcpu_pause(struct kvm_vcpu *vcpu)
  396. {
  397. wait_queue_head_t *wq = kvm_arch_vcpu_wq(vcpu);
  398. wait_event_interruptible(*wq, !vcpu->arch.pause);
  399. }
  400. static int kvm_vcpu_initialized(struct kvm_vcpu *vcpu)
  401. {
  402. return vcpu->arch.target >= 0;
  403. }
  404. /**
  405. * kvm_arch_vcpu_ioctl_run - the main VCPU run function to execute guest code
  406. * @vcpu: The VCPU pointer
  407. * @run: The kvm_run structure pointer used for userspace state exchange
  408. *
  409. * This function is called through the VCPU_RUN ioctl called from user space. It
  410. * will execute VM code in a loop until the time slice for the process is used
  411. * or some emulation is needed from user space in which case the function will
  412. * return with return value 0 and with the kvm_run structure filled in with the
  413. * required data for the requested emulation.
  414. */
  415. int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
  416. {
  417. int ret;
  418. sigset_t sigsaved;
  419. if (unlikely(!kvm_vcpu_initialized(vcpu)))
  420. return -ENOEXEC;
  421. ret = kvm_vcpu_first_run_init(vcpu);
  422. if (ret)
  423. return ret;
  424. if (run->exit_reason == KVM_EXIT_MMIO) {
  425. ret = kvm_handle_mmio_return(vcpu, vcpu->run);
  426. if (ret)
  427. return ret;
  428. }
  429. if (vcpu->sigset_active)
  430. sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
  431. ret = 1;
  432. run->exit_reason = KVM_EXIT_UNKNOWN;
  433. while (ret > 0) {
  434. /*
  435. * Check conditions before entering the guest
  436. */
  437. cond_resched();
  438. update_vttbr(vcpu->kvm);
  439. if (vcpu->arch.pause)
  440. vcpu_pause(vcpu);
  441. /*
  442. * Disarming the background timer must be done in a
  443. * preemptible context, as this call may sleep.
  444. */
  445. kvm_timer_flush_hwstate(vcpu);
  446. /*
  447. * Preparing the interrupts to be injected also
  448. * involves poking the GIC, which must be done in a
  449. * non-preemptible context.
  450. */
  451. preempt_disable();
  452. kvm_vgic_flush_hwstate(vcpu);
  453. local_irq_disable();
  454. /*
  455. * Re-check atomic conditions
  456. */
  457. if (signal_pending(current)) {
  458. ret = -EINTR;
  459. run->exit_reason = KVM_EXIT_INTR;
  460. }
  461. if (ret <= 0 || need_new_vmid_gen(vcpu->kvm)) {
  462. local_irq_enable();
  463. kvm_vgic_sync_hwstate(vcpu);
  464. preempt_enable();
  465. kvm_timer_sync_hwstate(vcpu);
  466. continue;
  467. }
  468. kvm_arm_setup_debug(vcpu);
  469. /**************************************************************
  470. * Enter the guest
  471. */
  472. trace_kvm_entry(*vcpu_pc(vcpu));
  473. __kvm_guest_enter();
  474. vcpu->mode = IN_GUEST_MODE;
  475. ret = kvm_call_hyp(__kvm_vcpu_run, vcpu);
  476. vcpu->mode = OUTSIDE_GUEST_MODE;
  477. /*
  478. * Back from guest
  479. *************************************************************/
  480. kvm_arm_clear_debug(vcpu);
  481. /*
  482. * We may have taken a host interrupt in HYP mode (ie
  483. * while executing the guest). This interrupt is still
  484. * pending, as we haven't serviced it yet!
  485. *
  486. * We're now back in SVC mode, with interrupts
  487. * disabled. Enabling the interrupts now will have
  488. * the effect of taking the interrupt again, in SVC
  489. * mode this time.
  490. */
  491. local_irq_enable();
  492. /*
  493. * We do local_irq_enable() before calling kvm_guest_exit() so
  494. * that if a timer interrupt hits while running the guest we
  495. * account that tick as being spent in the guest. We enable
  496. * preemption after calling kvm_guest_exit() so that if we get
  497. * preempted we make sure ticks after that is not counted as
  498. * guest time.
  499. */
  500. kvm_guest_exit();
  501. trace_kvm_exit(kvm_vcpu_trap_get_class(vcpu), *vcpu_pc(vcpu));
  502. kvm_vgic_sync_hwstate(vcpu);
  503. preempt_enable();
  504. kvm_timer_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. return -EINVAL;
  578. return kvm_vgic_inject_irq(kvm, 0, irq_num, level);
  579. }
  580. return -EINVAL;
  581. }
  582. static int kvm_vcpu_set_target(struct kvm_vcpu *vcpu,
  583. const struct kvm_vcpu_init *init)
  584. {
  585. unsigned int i;
  586. int phys_target = kvm_target_cpu();
  587. if (init->target != phys_target)
  588. return -EINVAL;
  589. /*
  590. * Secondary and subsequent calls to KVM_ARM_VCPU_INIT must
  591. * use the same target.
  592. */
  593. if (vcpu->arch.target != -1 && vcpu->arch.target != init->target)
  594. return -EINVAL;
  595. /* -ENOENT for unknown features, -EINVAL for invalid combinations. */
  596. for (i = 0; i < sizeof(init->features) * 8; i++) {
  597. bool set = (init->features[i / 32] & (1 << (i % 32)));
  598. if (set && i >= KVM_VCPU_MAX_FEATURES)
  599. return -ENOENT;
  600. /*
  601. * Secondary and subsequent calls to KVM_ARM_VCPU_INIT must
  602. * use the same feature set.
  603. */
  604. if (vcpu->arch.target != -1 && i < KVM_VCPU_MAX_FEATURES &&
  605. test_bit(i, vcpu->arch.features) != set)
  606. return -EINVAL;
  607. if (set)
  608. set_bit(i, vcpu->arch.features);
  609. }
  610. vcpu->arch.target = phys_target;
  611. /* Now we know what it is, we can reset it. */
  612. return kvm_reset_vcpu(vcpu);
  613. }
  614. static int kvm_arch_vcpu_ioctl_vcpu_init(struct kvm_vcpu *vcpu,
  615. struct kvm_vcpu_init *init)
  616. {
  617. int ret;
  618. ret = kvm_vcpu_set_target(vcpu, init);
  619. if (ret)
  620. return ret;
  621. /*
  622. * Ensure a rebooted VM will fault in RAM pages and detect if the
  623. * guest MMU is turned off and flush the caches as needed.
  624. */
  625. if (vcpu->arch.has_run_once)
  626. stage2_unmap_vm(vcpu->kvm);
  627. vcpu_reset_hcr(vcpu);
  628. /*
  629. * Handle the "start in power-off" case by marking the VCPU as paused.
  630. */
  631. if (test_bit(KVM_ARM_VCPU_POWER_OFF, vcpu->arch.features))
  632. vcpu->arch.pause = true;
  633. else
  634. vcpu->arch.pause = false;
  635. return 0;
  636. }
  637. long kvm_arch_vcpu_ioctl(struct file *filp,
  638. unsigned int ioctl, unsigned long arg)
  639. {
  640. struct kvm_vcpu *vcpu = filp->private_data;
  641. void __user *argp = (void __user *)arg;
  642. switch (ioctl) {
  643. case KVM_ARM_VCPU_INIT: {
  644. struct kvm_vcpu_init init;
  645. if (copy_from_user(&init, argp, sizeof(init)))
  646. return -EFAULT;
  647. return kvm_arch_vcpu_ioctl_vcpu_init(vcpu, &init);
  648. }
  649. case KVM_SET_ONE_REG:
  650. case KVM_GET_ONE_REG: {
  651. struct kvm_one_reg reg;
  652. if (unlikely(!kvm_vcpu_initialized(vcpu)))
  653. return -ENOEXEC;
  654. if (copy_from_user(&reg, argp, sizeof(reg)))
  655. return -EFAULT;
  656. if (ioctl == KVM_SET_ONE_REG)
  657. return kvm_arm_set_reg(vcpu, &reg);
  658. else
  659. return kvm_arm_get_reg(vcpu, &reg);
  660. }
  661. case KVM_GET_REG_LIST: {
  662. struct kvm_reg_list __user *user_list = argp;
  663. struct kvm_reg_list reg_list;
  664. unsigned n;
  665. if (unlikely(!kvm_vcpu_initialized(vcpu)))
  666. return -ENOEXEC;
  667. if (copy_from_user(&reg_list, user_list, sizeof(reg_list)))
  668. return -EFAULT;
  669. n = reg_list.n;
  670. reg_list.n = kvm_arm_num_regs(vcpu);
  671. if (copy_to_user(user_list, &reg_list, sizeof(reg_list)))
  672. return -EFAULT;
  673. if (n < reg_list.n)
  674. return -E2BIG;
  675. return kvm_arm_copy_reg_indices(vcpu, user_list->reg);
  676. }
  677. default:
  678. return -EINVAL;
  679. }
  680. }
  681. /**
  682. * kvm_vm_ioctl_get_dirty_log - get and clear the log of dirty pages in a slot
  683. * @kvm: kvm instance
  684. * @log: slot id and address to which we copy the log
  685. *
  686. * Steps 1-4 below provide general overview of dirty page logging. See
  687. * kvm_get_dirty_log_protect() function description for additional details.
  688. *
  689. * We call kvm_get_dirty_log_protect() to handle steps 1-3, upon return we
  690. * always flush the TLB (step 4) even if previous step failed and the dirty
  691. * bitmap may be corrupt. Regardless of previous outcome the KVM logging API
  692. * does not preclude user space subsequent dirty log read. Flushing TLB ensures
  693. * writes will be marked dirty for next log read.
  694. *
  695. * 1. Take a snapshot of the bit and clear it if needed.
  696. * 2. Write protect the corresponding page.
  697. * 3. Copy the snapshot to the userspace.
  698. * 4. Flush TLB's if needed.
  699. */
  700. int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
  701. {
  702. bool is_dirty = false;
  703. int r;
  704. mutex_lock(&kvm->slots_lock);
  705. r = kvm_get_dirty_log_protect(kvm, log, &is_dirty);
  706. if (is_dirty)
  707. kvm_flush_remote_tlbs(kvm);
  708. mutex_unlock(&kvm->slots_lock);
  709. return r;
  710. }
  711. static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm,
  712. struct kvm_arm_device_addr *dev_addr)
  713. {
  714. unsigned long dev_id, type;
  715. dev_id = (dev_addr->id & KVM_ARM_DEVICE_ID_MASK) >>
  716. KVM_ARM_DEVICE_ID_SHIFT;
  717. type = (dev_addr->id & KVM_ARM_DEVICE_TYPE_MASK) >>
  718. KVM_ARM_DEVICE_TYPE_SHIFT;
  719. switch (dev_id) {
  720. case KVM_ARM_DEVICE_VGIC_V2:
  721. return kvm_vgic_addr(kvm, type, &dev_addr->addr, true);
  722. default:
  723. return -ENODEV;
  724. }
  725. }
  726. long kvm_arch_vm_ioctl(struct file *filp,
  727. unsigned int ioctl, unsigned long arg)
  728. {
  729. struct kvm *kvm = filp->private_data;
  730. void __user *argp = (void __user *)arg;
  731. switch (ioctl) {
  732. case KVM_CREATE_IRQCHIP: {
  733. return kvm_vgic_create(kvm, KVM_DEV_TYPE_ARM_VGIC_V2);
  734. }
  735. case KVM_ARM_SET_DEVICE_ADDR: {
  736. struct kvm_arm_device_addr dev_addr;
  737. if (copy_from_user(&dev_addr, argp, sizeof(dev_addr)))
  738. return -EFAULT;
  739. return kvm_vm_ioctl_set_device_addr(kvm, &dev_addr);
  740. }
  741. case KVM_ARM_PREFERRED_TARGET: {
  742. int err;
  743. struct kvm_vcpu_init init;
  744. err = kvm_vcpu_preferred_target(&init);
  745. if (err)
  746. return err;
  747. if (copy_to_user(argp, &init, sizeof(init)))
  748. return -EFAULT;
  749. return 0;
  750. }
  751. default:
  752. return -EINVAL;
  753. }
  754. }
  755. static void cpu_init_hyp_mode(void *dummy)
  756. {
  757. phys_addr_t boot_pgd_ptr;
  758. phys_addr_t pgd_ptr;
  759. unsigned long hyp_stack_ptr;
  760. unsigned long stack_page;
  761. unsigned long vector_ptr;
  762. /* Switch from the HYP stub to our own HYP init vector */
  763. __hyp_set_vectors(kvm_get_idmap_vector());
  764. boot_pgd_ptr = kvm_mmu_get_boot_httbr();
  765. pgd_ptr = kvm_mmu_get_httbr();
  766. stack_page = __this_cpu_read(kvm_arm_hyp_stack_page);
  767. hyp_stack_ptr = stack_page + PAGE_SIZE;
  768. vector_ptr = (unsigned long)__kvm_hyp_vector;
  769. __cpu_init_hyp_mode(boot_pgd_ptr, pgd_ptr, hyp_stack_ptr, vector_ptr);
  770. kvm_arm_init_debug();
  771. }
  772. static int hyp_init_cpu_notify(struct notifier_block *self,
  773. unsigned long action, void *cpu)
  774. {
  775. switch (action) {
  776. case CPU_STARTING:
  777. case CPU_STARTING_FROZEN:
  778. if (__hyp_get_vectors() == hyp_default_vectors)
  779. cpu_init_hyp_mode(NULL);
  780. break;
  781. }
  782. return NOTIFY_OK;
  783. }
  784. static struct notifier_block hyp_init_cpu_nb = {
  785. .notifier_call = hyp_init_cpu_notify,
  786. };
  787. #ifdef CONFIG_CPU_PM
  788. static int hyp_init_cpu_pm_notifier(struct notifier_block *self,
  789. unsigned long cmd,
  790. void *v)
  791. {
  792. if (cmd == CPU_PM_EXIT &&
  793. __hyp_get_vectors() == hyp_default_vectors) {
  794. cpu_init_hyp_mode(NULL);
  795. return NOTIFY_OK;
  796. }
  797. return NOTIFY_DONE;
  798. }
  799. static struct notifier_block hyp_init_cpu_pm_nb = {
  800. .notifier_call = hyp_init_cpu_pm_notifier,
  801. };
  802. static void __init hyp_cpu_pm_init(void)
  803. {
  804. cpu_pm_register_notifier(&hyp_init_cpu_pm_nb);
  805. }
  806. #else
  807. static inline void hyp_cpu_pm_init(void)
  808. {
  809. }
  810. #endif
  811. /**
  812. * Inits Hyp-mode on all online CPUs
  813. */
  814. static int init_hyp_mode(void)
  815. {
  816. int cpu;
  817. int err = 0;
  818. /*
  819. * Allocate Hyp PGD and setup Hyp identity mapping
  820. */
  821. err = kvm_mmu_init();
  822. if (err)
  823. goto out_err;
  824. /*
  825. * It is probably enough to obtain the default on one
  826. * CPU. It's unlikely to be different on the others.
  827. */
  828. hyp_default_vectors = __hyp_get_vectors();
  829. /*
  830. * Allocate stack pages for Hypervisor-mode
  831. */
  832. for_each_possible_cpu(cpu) {
  833. unsigned long stack_page;
  834. stack_page = __get_free_page(GFP_KERNEL);
  835. if (!stack_page) {
  836. err = -ENOMEM;
  837. goto out_free_stack_pages;
  838. }
  839. per_cpu(kvm_arm_hyp_stack_page, cpu) = stack_page;
  840. }
  841. /*
  842. * Map the Hyp-code called directly from the host
  843. */
  844. err = create_hyp_mappings(__kvm_hyp_code_start, __kvm_hyp_code_end);
  845. if (err) {
  846. kvm_err("Cannot map world-switch code\n");
  847. goto out_free_mappings;
  848. }
  849. /*
  850. * Map the Hyp stack pages
  851. */
  852. for_each_possible_cpu(cpu) {
  853. char *stack_page = (char *)per_cpu(kvm_arm_hyp_stack_page, cpu);
  854. err = create_hyp_mappings(stack_page, stack_page + PAGE_SIZE);
  855. if (err) {
  856. kvm_err("Cannot map hyp stack\n");
  857. goto out_free_mappings;
  858. }
  859. }
  860. /*
  861. * Map the host CPU structures
  862. */
  863. kvm_host_cpu_state = alloc_percpu(kvm_cpu_context_t);
  864. if (!kvm_host_cpu_state) {
  865. err = -ENOMEM;
  866. kvm_err("Cannot allocate host CPU state\n");
  867. goto out_free_mappings;
  868. }
  869. for_each_possible_cpu(cpu) {
  870. kvm_cpu_context_t *cpu_ctxt;
  871. cpu_ctxt = per_cpu_ptr(kvm_host_cpu_state, cpu);
  872. err = create_hyp_mappings(cpu_ctxt, cpu_ctxt + 1);
  873. if (err) {
  874. kvm_err("Cannot map host CPU state: %d\n", err);
  875. goto out_free_context;
  876. }
  877. }
  878. /*
  879. * Execute the init code on each CPU.
  880. */
  881. on_each_cpu(cpu_init_hyp_mode, NULL, 1);
  882. /*
  883. * Init HYP view of VGIC
  884. */
  885. err = kvm_vgic_hyp_init();
  886. if (err)
  887. goto out_free_context;
  888. /*
  889. * Init HYP architected timer support
  890. */
  891. err = kvm_timer_hyp_init();
  892. if (err)
  893. goto out_free_mappings;
  894. #ifndef CONFIG_HOTPLUG_CPU
  895. free_boot_hyp_pgd();
  896. #endif
  897. kvm_perf_init();
  898. kvm_info("Hyp mode initialized successfully\n");
  899. return 0;
  900. out_free_context:
  901. free_percpu(kvm_host_cpu_state);
  902. out_free_mappings:
  903. free_hyp_pgds();
  904. out_free_stack_pages:
  905. for_each_possible_cpu(cpu)
  906. free_page(per_cpu(kvm_arm_hyp_stack_page, cpu));
  907. out_err:
  908. kvm_err("error initializing Hyp mode: %d\n", err);
  909. return err;
  910. }
  911. static void check_kvm_target_cpu(void *ret)
  912. {
  913. *(int *)ret = kvm_target_cpu();
  914. }
  915. struct kvm_vcpu *kvm_mpidr_to_vcpu(struct kvm *kvm, unsigned long mpidr)
  916. {
  917. struct kvm_vcpu *vcpu;
  918. int i;
  919. mpidr &= MPIDR_HWID_BITMASK;
  920. kvm_for_each_vcpu(i, vcpu, kvm) {
  921. if (mpidr == kvm_vcpu_get_mpidr_aff(vcpu))
  922. return vcpu;
  923. }
  924. return NULL;
  925. }
  926. /**
  927. * Initialize Hyp-mode and memory mappings on all CPUs.
  928. */
  929. int kvm_arch_init(void *opaque)
  930. {
  931. int err;
  932. int ret, cpu;
  933. if (!is_hyp_mode_available()) {
  934. kvm_err("HYP mode not available\n");
  935. return -ENODEV;
  936. }
  937. for_each_online_cpu(cpu) {
  938. smp_call_function_single(cpu, check_kvm_target_cpu, &ret, 1);
  939. if (ret < 0) {
  940. kvm_err("Error, CPU %d not supported!\n", cpu);
  941. return -ENODEV;
  942. }
  943. }
  944. cpu_notifier_register_begin();
  945. err = init_hyp_mode();
  946. if (err)
  947. goto out_err;
  948. err = __register_cpu_notifier(&hyp_init_cpu_nb);
  949. if (err) {
  950. kvm_err("Cannot register HYP init CPU notifier (%d)\n", err);
  951. goto out_err;
  952. }
  953. cpu_notifier_register_done();
  954. hyp_cpu_pm_init();
  955. kvm_coproc_table_init();
  956. return 0;
  957. out_err:
  958. cpu_notifier_register_done();
  959. return err;
  960. }
  961. /* NOP: Compiling as a module not supported */
  962. void kvm_arch_exit(void)
  963. {
  964. kvm_perf_teardown();
  965. }
  966. static int arm_init(void)
  967. {
  968. int rc = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
  969. return rc;
  970. }
  971. module_init(arm_init);