arm.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561
  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_pm.h>
  19. #include <linux/errno.h>
  20. #include <linux/err.h>
  21. #include <linux/kvm_host.h>
  22. #include <linux/list.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 <linux/kvm_irqfd.h>
  30. #include <linux/irqbypass.h>
  31. #include <trace/events/kvm.h>
  32. #include <kvm/arm_pmu.h>
  33. #define CREATE_TRACE_POINTS
  34. #include "trace.h"
  35. #include <linux/uaccess.h>
  36. #include <asm/ptrace.h>
  37. #include <asm/mman.h>
  38. #include <asm/tlbflush.h>
  39. #include <asm/cacheflush.h>
  40. #include <asm/virt.h>
  41. #include <asm/kvm_arm.h>
  42. #include <asm/kvm_asm.h>
  43. #include <asm/kvm_mmu.h>
  44. #include <asm/kvm_emulate.h>
  45. #include <asm/kvm_coproc.h>
  46. #include <asm/kvm_psci.h>
  47. #include <asm/sections.h>
  48. #ifdef REQUIRES_VIRT
  49. __asm__(".arch_extension virt");
  50. #endif
  51. static DEFINE_PER_CPU(unsigned long, kvm_arm_hyp_stack_page);
  52. static kvm_cpu_context_t __percpu *kvm_host_cpu_state;
  53. /* Per-CPU variable containing the currently running vcpu. */
  54. static DEFINE_PER_CPU(struct kvm_vcpu *, kvm_arm_running_vcpu);
  55. /* The VMID used in the VTTBR */
  56. static atomic64_t kvm_vmid_gen = ATOMIC64_INIT(1);
  57. static u32 kvm_next_vmid;
  58. static unsigned int kvm_vmid_bits __read_mostly;
  59. static DEFINE_SPINLOCK(kvm_vmid_lock);
  60. static bool vgic_present;
  61. static DEFINE_PER_CPU(unsigned char, kvm_arm_hardware_enabled);
  62. static void kvm_arm_set_running_vcpu(struct kvm_vcpu *vcpu)
  63. {
  64. __this_cpu_write(kvm_arm_running_vcpu, vcpu);
  65. }
  66. /**
  67. * kvm_arm_get_running_vcpu - get the vcpu running on the current CPU.
  68. * Must be called from non-preemptible context
  69. */
  70. struct kvm_vcpu *kvm_arm_get_running_vcpu(void)
  71. {
  72. return __this_cpu_read(kvm_arm_running_vcpu);
  73. }
  74. /**
  75. * kvm_arm_get_running_vcpus - get the per-CPU array of currently running vcpus.
  76. */
  77. struct kvm_vcpu * __percpu *kvm_get_running_vcpus(void)
  78. {
  79. return &kvm_arm_running_vcpu;
  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, cpu;
  100. if (type)
  101. return -EINVAL;
  102. kvm->arch.last_vcpu_ran = alloc_percpu(typeof(*kvm->arch.last_vcpu_ran));
  103. if (!kvm->arch.last_vcpu_ran)
  104. return -ENOMEM;
  105. for_each_possible_cpu(cpu)
  106. *per_cpu_ptr(kvm->arch.last_vcpu_ran, cpu) = -1;
  107. ret = kvm_alloc_stage2_pgd(kvm);
  108. if (ret)
  109. goto out_fail_alloc;
  110. ret = create_hyp_mappings(kvm, kvm + 1, PAGE_HYP);
  111. if (ret)
  112. goto out_free_stage2_pgd;
  113. kvm_vgic_early_init(kvm);
  114. /* Mark the initial VMID generation invalid */
  115. kvm->arch.vmid_gen = 0;
  116. /* The maximum number of VCPUs is limited by the host's GIC model */
  117. kvm->arch.max_vcpus = vgic_present ?
  118. kvm_vgic_get_max_vcpus() : KVM_MAX_VCPUS;
  119. return ret;
  120. out_free_stage2_pgd:
  121. kvm_free_stage2_pgd(kvm);
  122. out_fail_alloc:
  123. free_percpu(kvm->arch.last_vcpu_ran);
  124. kvm->arch.last_vcpu_ran = NULL;
  125. return ret;
  126. }
  127. bool kvm_arch_has_vcpu_debugfs(void)
  128. {
  129. return false;
  130. }
  131. int kvm_arch_create_vcpu_debugfs(struct kvm_vcpu *vcpu)
  132. {
  133. return 0;
  134. }
  135. int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
  136. {
  137. return VM_FAULT_SIGBUS;
  138. }
  139. /**
  140. * kvm_arch_destroy_vm - destroy the VM data structure
  141. * @kvm: pointer to the KVM struct
  142. */
  143. void kvm_arch_destroy_vm(struct kvm *kvm)
  144. {
  145. int i;
  146. kvm_vgic_destroy(kvm);
  147. free_percpu(kvm->arch.last_vcpu_ran);
  148. kvm->arch.last_vcpu_ran = NULL;
  149. for (i = 0; i < KVM_MAX_VCPUS; ++i) {
  150. if (kvm->vcpus[i]) {
  151. kvm_arch_vcpu_free(kvm->vcpus[i]);
  152. kvm->vcpus[i] = NULL;
  153. }
  154. }
  155. atomic_set(&kvm->online_vcpus, 0);
  156. }
  157. int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
  158. {
  159. int r;
  160. switch (ext) {
  161. case KVM_CAP_IRQCHIP:
  162. r = vgic_present;
  163. break;
  164. case KVM_CAP_IOEVENTFD:
  165. case KVM_CAP_DEVICE_CTRL:
  166. case KVM_CAP_USER_MEMORY:
  167. case KVM_CAP_SYNC_MMU:
  168. case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
  169. case KVM_CAP_ONE_REG:
  170. case KVM_CAP_ARM_PSCI:
  171. case KVM_CAP_ARM_PSCI_0_2:
  172. case KVM_CAP_READONLY_MEM:
  173. case KVM_CAP_MP_STATE:
  174. case KVM_CAP_IMMEDIATE_EXIT:
  175. r = 1;
  176. break;
  177. case KVM_CAP_ARM_SET_DEVICE_ADDR:
  178. r = 1;
  179. break;
  180. case KVM_CAP_NR_VCPUS:
  181. r = num_online_cpus();
  182. break;
  183. case KVM_CAP_MAX_VCPUS:
  184. r = KVM_MAX_VCPUS;
  185. break;
  186. case KVM_CAP_NR_MEMSLOTS:
  187. r = KVM_USER_MEM_SLOTS;
  188. break;
  189. case KVM_CAP_MSI_DEVID:
  190. if (!kvm)
  191. r = -EINVAL;
  192. else
  193. r = kvm->arch.vgic.msis_require_devid;
  194. break;
  195. case KVM_CAP_ARM_USER_IRQ:
  196. /*
  197. * 1: EL1_VTIMER, EL1_PTIMER, and PMU.
  198. * (bump this number if adding more devices)
  199. */
  200. r = 1;
  201. break;
  202. default:
  203. r = kvm_arch_dev_ioctl_check_extension(kvm, ext);
  204. break;
  205. }
  206. return r;
  207. }
  208. long kvm_arch_dev_ioctl(struct file *filp,
  209. unsigned int ioctl, unsigned long arg)
  210. {
  211. return -EINVAL;
  212. }
  213. struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
  214. {
  215. int err;
  216. struct kvm_vcpu *vcpu;
  217. if (irqchip_in_kernel(kvm) && vgic_initialized(kvm)) {
  218. err = -EBUSY;
  219. goto out;
  220. }
  221. if (id >= kvm->arch.max_vcpus) {
  222. err = -EINVAL;
  223. goto out;
  224. }
  225. vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
  226. if (!vcpu) {
  227. err = -ENOMEM;
  228. goto out;
  229. }
  230. err = kvm_vcpu_init(vcpu, kvm, id);
  231. if (err)
  232. goto free_vcpu;
  233. err = create_hyp_mappings(vcpu, vcpu + 1, PAGE_HYP);
  234. if (err)
  235. goto vcpu_uninit;
  236. return vcpu;
  237. vcpu_uninit:
  238. kvm_vcpu_uninit(vcpu);
  239. free_vcpu:
  240. kmem_cache_free(kvm_vcpu_cache, vcpu);
  241. out:
  242. return ERR_PTR(err);
  243. }
  244. void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
  245. {
  246. kvm_vgic_vcpu_early_init(vcpu);
  247. }
  248. void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
  249. {
  250. kvm_mmu_free_memory_caches(vcpu);
  251. kvm_timer_vcpu_terminate(vcpu);
  252. kvm_pmu_vcpu_destroy(vcpu);
  253. kvm_vcpu_uninit(vcpu);
  254. kmem_cache_free(kvm_vcpu_cache, vcpu);
  255. }
  256. void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
  257. {
  258. kvm_arch_vcpu_free(vcpu);
  259. }
  260. int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
  261. {
  262. return kvm_timer_is_pending(vcpu);
  263. }
  264. void kvm_arch_vcpu_blocking(struct kvm_vcpu *vcpu)
  265. {
  266. kvm_timer_schedule(vcpu);
  267. kvm_vgic_v4_enable_doorbell(vcpu);
  268. }
  269. void kvm_arch_vcpu_unblocking(struct kvm_vcpu *vcpu)
  270. {
  271. kvm_timer_unschedule(vcpu);
  272. kvm_vgic_v4_disable_doorbell(vcpu);
  273. }
  274. int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
  275. {
  276. /* Force users to call KVM_ARM_VCPU_INIT */
  277. vcpu->arch.target = -1;
  278. bitmap_zero(vcpu->arch.features, KVM_VCPU_MAX_FEATURES);
  279. /* Set up the timer */
  280. kvm_timer_vcpu_init(vcpu);
  281. kvm_arm_reset_debug_ptr(vcpu);
  282. return kvm_vgic_vcpu_init(vcpu);
  283. }
  284. void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  285. {
  286. int *last_ran;
  287. last_ran = this_cpu_ptr(vcpu->kvm->arch.last_vcpu_ran);
  288. /*
  289. * We might get preempted before the vCPU actually runs, but
  290. * over-invalidation doesn't affect correctness.
  291. */
  292. if (*last_ran != vcpu->vcpu_id) {
  293. kvm_call_hyp(__kvm_tlb_flush_local_vmid, vcpu);
  294. *last_ran = vcpu->vcpu_id;
  295. }
  296. vcpu->cpu = cpu;
  297. vcpu->arch.host_cpu_context = this_cpu_ptr(kvm_host_cpu_state);
  298. kvm_arm_set_running_vcpu(vcpu);
  299. kvm_vgic_load(vcpu);
  300. kvm_timer_vcpu_load(vcpu);
  301. }
  302. void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
  303. {
  304. kvm_timer_vcpu_put(vcpu);
  305. kvm_vgic_put(vcpu);
  306. vcpu->cpu = -1;
  307. kvm_arm_set_running_vcpu(NULL);
  308. }
  309. static void vcpu_power_off(struct kvm_vcpu *vcpu)
  310. {
  311. vcpu->arch.power_off = true;
  312. kvm_make_request(KVM_REQ_SLEEP, vcpu);
  313. kvm_vcpu_kick(vcpu);
  314. }
  315. int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
  316. struct kvm_mp_state *mp_state)
  317. {
  318. if (vcpu->arch.power_off)
  319. mp_state->mp_state = KVM_MP_STATE_STOPPED;
  320. else
  321. mp_state->mp_state = KVM_MP_STATE_RUNNABLE;
  322. return 0;
  323. }
  324. int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
  325. struct kvm_mp_state *mp_state)
  326. {
  327. switch (mp_state->mp_state) {
  328. case KVM_MP_STATE_RUNNABLE:
  329. vcpu->arch.power_off = false;
  330. break;
  331. case KVM_MP_STATE_STOPPED:
  332. vcpu_power_off(vcpu);
  333. break;
  334. default:
  335. return -EINVAL;
  336. }
  337. return 0;
  338. }
  339. /**
  340. * kvm_arch_vcpu_runnable - determine if the vcpu can be scheduled
  341. * @v: The VCPU pointer
  342. *
  343. * If the guest CPU is not waiting for interrupts or an interrupt line is
  344. * asserted, the CPU is by definition runnable.
  345. */
  346. int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
  347. {
  348. return ((!!v->arch.irq_lines || kvm_vgic_vcpu_pending_irq(v))
  349. && !v->arch.power_off && !v->arch.pause);
  350. }
  351. bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu)
  352. {
  353. return vcpu_mode_priv(vcpu);
  354. }
  355. /* Just ensure a guest exit from a particular CPU */
  356. static void exit_vm_noop(void *info)
  357. {
  358. }
  359. void force_vm_exit(const cpumask_t *mask)
  360. {
  361. preempt_disable();
  362. smp_call_function_many(mask, exit_vm_noop, NULL, true);
  363. preempt_enable();
  364. }
  365. /**
  366. * need_new_vmid_gen - check that the VMID is still valid
  367. * @kvm: The VM's VMID to check
  368. *
  369. * return true if there is a new generation of VMIDs being used
  370. *
  371. * The hardware supports only 256 values with the value zero reserved for the
  372. * host, so we check if an assigned value belongs to a previous generation,
  373. * which which requires us to assign a new value. If we're the first to use a
  374. * VMID for the new generation, we must flush necessary caches and TLBs on all
  375. * CPUs.
  376. */
  377. static bool need_new_vmid_gen(struct kvm *kvm)
  378. {
  379. return unlikely(kvm->arch.vmid_gen != atomic64_read(&kvm_vmid_gen));
  380. }
  381. /**
  382. * update_vttbr - Update the VTTBR with a valid VMID before the guest runs
  383. * @kvm The guest that we are about to run
  384. *
  385. * Called from kvm_arch_vcpu_ioctl_run before entering the guest to ensure the
  386. * VM has a valid VMID, otherwise assigns a new one and flushes corresponding
  387. * caches and TLBs.
  388. */
  389. static void update_vttbr(struct kvm *kvm)
  390. {
  391. phys_addr_t pgd_phys;
  392. u64 vmid;
  393. if (!need_new_vmid_gen(kvm))
  394. return;
  395. spin_lock(&kvm_vmid_lock);
  396. /*
  397. * We need to re-check the vmid_gen here to ensure that if another vcpu
  398. * already allocated a valid vmid for this vm, then this vcpu should
  399. * use the same vmid.
  400. */
  401. if (!need_new_vmid_gen(kvm)) {
  402. spin_unlock(&kvm_vmid_lock);
  403. return;
  404. }
  405. /* First user of a new VMID generation? */
  406. if (unlikely(kvm_next_vmid == 0)) {
  407. atomic64_inc(&kvm_vmid_gen);
  408. kvm_next_vmid = 1;
  409. /*
  410. * On SMP we know no other CPUs can use this CPU's or each
  411. * other's VMID after force_vm_exit returns since the
  412. * kvm_vmid_lock blocks them from reentry to the guest.
  413. */
  414. force_vm_exit(cpu_all_mask);
  415. /*
  416. * Now broadcast TLB + ICACHE invalidation over the inner
  417. * shareable domain to make sure all data structures are
  418. * clean.
  419. */
  420. kvm_call_hyp(__kvm_flush_vm_context);
  421. }
  422. kvm->arch.vmid_gen = atomic64_read(&kvm_vmid_gen);
  423. kvm->arch.vmid = kvm_next_vmid;
  424. kvm_next_vmid++;
  425. kvm_next_vmid &= (1 << kvm_vmid_bits) - 1;
  426. /* update vttbr to be used with the new vmid */
  427. pgd_phys = virt_to_phys(kvm->arch.pgd);
  428. BUG_ON(pgd_phys & ~VTTBR_BADDR_MASK);
  429. vmid = ((u64)(kvm->arch.vmid) << VTTBR_VMID_SHIFT) & VTTBR_VMID_MASK(kvm_vmid_bits);
  430. kvm->arch.vttbr = pgd_phys | vmid;
  431. spin_unlock(&kvm_vmid_lock);
  432. }
  433. static int kvm_vcpu_first_run_init(struct kvm_vcpu *vcpu)
  434. {
  435. struct kvm *kvm = vcpu->kvm;
  436. int ret = 0;
  437. if (likely(vcpu->arch.has_run_once))
  438. return 0;
  439. vcpu->arch.has_run_once = true;
  440. /*
  441. * Map the VGIC hardware resources before running a vcpu the first
  442. * time on this VM.
  443. */
  444. if (unlikely(irqchip_in_kernel(kvm) && !vgic_ready(kvm))) {
  445. ret = kvm_vgic_map_resources(kvm);
  446. if (ret)
  447. return ret;
  448. }
  449. ret = kvm_timer_enable(vcpu);
  450. if (ret)
  451. return ret;
  452. ret = kvm_arm_pmu_v3_enable(vcpu);
  453. return ret;
  454. }
  455. bool kvm_arch_intc_initialized(struct kvm *kvm)
  456. {
  457. return vgic_initialized(kvm);
  458. }
  459. void kvm_arm_halt_guest(struct kvm *kvm)
  460. {
  461. int i;
  462. struct kvm_vcpu *vcpu;
  463. kvm_for_each_vcpu(i, vcpu, kvm)
  464. vcpu->arch.pause = true;
  465. kvm_make_all_cpus_request(kvm, KVM_REQ_SLEEP);
  466. }
  467. void kvm_arm_resume_guest(struct kvm *kvm)
  468. {
  469. int i;
  470. struct kvm_vcpu *vcpu;
  471. kvm_for_each_vcpu(i, vcpu, kvm) {
  472. vcpu->arch.pause = false;
  473. swake_up(kvm_arch_vcpu_wq(vcpu));
  474. }
  475. }
  476. static void vcpu_req_sleep(struct kvm_vcpu *vcpu)
  477. {
  478. struct swait_queue_head *wq = kvm_arch_vcpu_wq(vcpu);
  479. swait_event_interruptible(*wq, ((!vcpu->arch.power_off) &&
  480. (!vcpu->arch.pause)));
  481. if (vcpu->arch.power_off || vcpu->arch.pause) {
  482. /* Awaken to handle a signal, request we sleep again later. */
  483. kvm_make_request(KVM_REQ_SLEEP, vcpu);
  484. }
  485. }
  486. static int kvm_vcpu_initialized(struct kvm_vcpu *vcpu)
  487. {
  488. return vcpu->arch.target >= 0;
  489. }
  490. static void check_vcpu_requests(struct kvm_vcpu *vcpu)
  491. {
  492. if (kvm_request_pending(vcpu)) {
  493. if (kvm_check_request(KVM_REQ_SLEEP, vcpu))
  494. vcpu_req_sleep(vcpu);
  495. /*
  496. * Clear IRQ_PENDING requests that were made to guarantee
  497. * that a VCPU sees new virtual interrupts.
  498. */
  499. kvm_check_request(KVM_REQ_IRQ_PENDING, vcpu);
  500. }
  501. }
  502. /**
  503. * kvm_arch_vcpu_ioctl_run - the main VCPU run function to execute guest code
  504. * @vcpu: The VCPU pointer
  505. * @run: The kvm_run structure pointer used for userspace state exchange
  506. *
  507. * This function is called through the VCPU_RUN ioctl called from user space. It
  508. * will execute VM code in a loop until the time slice for the process is used
  509. * or some emulation is needed from user space in which case the function will
  510. * return with return value 0 and with the kvm_run structure filled in with the
  511. * required data for the requested emulation.
  512. */
  513. int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
  514. {
  515. int ret;
  516. if (unlikely(!kvm_vcpu_initialized(vcpu)))
  517. return -ENOEXEC;
  518. ret = kvm_vcpu_first_run_init(vcpu);
  519. if (ret)
  520. return ret;
  521. if (run->exit_reason == KVM_EXIT_MMIO) {
  522. ret = kvm_handle_mmio_return(vcpu, vcpu->run);
  523. if (ret)
  524. return ret;
  525. if (kvm_arm_handle_step_debug(vcpu, vcpu->run))
  526. return 0;
  527. }
  528. if (run->immediate_exit)
  529. return -EINTR;
  530. kvm_sigset_activate(vcpu);
  531. ret = 1;
  532. run->exit_reason = KVM_EXIT_UNKNOWN;
  533. while (ret > 0) {
  534. /*
  535. * Check conditions before entering the guest
  536. */
  537. cond_resched();
  538. update_vttbr(vcpu->kvm);
  539. check_vcpu_requests(vcpu);
  540. /*
  541. * Preparing the interrupts to be injected also
  542. * involves poking the GIC, which must be done in a
  543. * non-preemptible context.
  544. */
  545. preempt_disable();
  546. /* Flush FP/SIMD state that can't survive guest entry/exit */
  547. kvm_fpsimd_flush_cpu_state();
  548. kvm_pmu_flush_hwstate(vcpu);
  549. local_irq_disable();
  550. kvm_vgic_flush_hwstate(vcpu);
  551. /*
  552. * If we have a singal pending, or need to notify a userspace
  553. * irqchip about timer or PMU level changes, then we exit (and
  554. * update the timer level state in kvm_timer_update_run
  555. * below).
  556. */
  557. if (signal_pending(current) ||
  558. kvm_timer_should_notify_user(vcpu) ||
  559. kvm_pmu_should_notify_user(vcpu)) {
  560. ret = -EINTR;
  561. run->exit_reason = KVM_EXIT_INTR;
  562. }
  563. /*
  564. * Ensure we set mode to IN_GUEST_MODE after we disable
  565. * interrupts and before the final VCPU requests check.
  566. * See the comment in kvm_vcpu_exiting_guest_mode() and
  567. * Documentation/virtual/kvm/vcpu-requests.rst
  568. */
  569. smp_store_mb(vcpu->mode, IN_GUEST_MODE);
  570. if (ret <= 0 || need_new_vmid_gen(vcpu->kvm) ||
  571. kvm_request_pending(vcpu)) {
  572. vcpu->mode = OUTSIDE_GUEST_MODE;
  573. kvm_pmu_sync_hwstate(vcpu);
  574. kvm_timer_sync_hwstate(vcpu);
  575. kvm_vgic_sync_hwstate(vcpu);
  576. local_irq_enable();
  577. preempt_enable();
  578. continue;
  579. }
  580. kvm_arm_setup_debug(vcpu);
  581. /**************************************************************
  582. * Enter the guest
  583. */
  584. trace_kvm_entry(*vcpu_pc(vcpu));
  585. guest_enter_irqoff();
  586. ret = kvm_call_hyp(__kvm_vcpu_run, vcpu);
  587. vcpu->mode = OUTSIDE_GUEST_MODE;
  588. vcpu->stat.exits++;
  589. /*
  590. * Back from guest
  591. *************************************************************/
  592. kvm_arm_clear_debug(vcpu);
  593. /*
  594. * We must sync the PMU state before the vgic state so
  595. * that the vgic can properly sample the updated state of the
  596. * interrupt line.
  597. */
  598. kvm_pmu_sync_hwstate(vcpu);
  599. /*
  600. * Sync the vgic state before syncing the timer state because
  601. * the timer code needs to know if the virtual timer
  602. * interrupts are active.
  603. */
  604. kvm_vgic_sync_hwstate(vcpu);
  605. /*
  606. * Sync the timer hardware state before enabling interrupts as
  607. * we don't want vtimer interrupts to race with syncing the
  608. * timer virtual interrupt state.
  609. */
  610. kvm_timer_sync_hwstate(vcpu);
  611. /*
  612. * We may have taken a host interrupt in HYP mode (ie
  613. * while executing the guest). This interrupt is still
  614. * pending, as we haven't serviced it yet!
  615. *
  616. * We're now back in SVC mode, with interrupts
  617. * disabled. Enabling the interrupts now will have
  618. * the effect of taking the interrupt again, in SVC
  619. * mode this time.
  620. */
  621. local_irq_enable();
  622. /*
  623. * We do local_irq_enable() before calling guest_exit() so
  624. * that if a timer interrupt hits while running the guest we
  625. * account that tick as being spent in the guest. We enable
  626. * preemption after calling guest_exit() so that if we get
  627. * preempted we make sure ticks after that is not counted as
  628. * guest time.
  629. */
  630. guest_exit();
  631. trace_kvm_exit(ret, kvm_vcpu_trap_get_class(vcpu), *vcpu_pc(vcpu));
  632. preempt_enable();
  633. ret = handle_exit(vcpu, run, ret);
  634. }
  635. /* Tell userspace about in-kernel device output levels */
  636. if (unlikely(!irqchip_in_kernel(vcpu->kvm))) {
  637. kvm_timer_update_run(vcpu);
  638. kvm_pmu_update_run(vcpu);
  639. }
  640. kvm_sigset_deactivate(vcpu);
  641. return ret;
  642. }
  643. static int vcpu_interrupt_line(struct kvm_vcpu *vcpu, int number, bool level)
  644. {
  645. int bit_index;
  646. bool set;
  647. unsigned long *ptr;
  648. if (number == KVM_ARM_IRQ_CPU_IRQ)
  649. bit_index = __ffs(HCR_VI);
  650. else /* KVM_ARM_IRQ_CPU_FIQ */
  651. bit_index = __ffs(HCR_VF);
  652. ptr = (unsigned long *)&vcpu->arch.irq_lines;
  653. if (level)
  654. set = test_and_set_bit(bit_index, ptr);
  655. else
  656. set = test_and_clear_bit(bit_index, ptr);
  657. /*
  658. * If we didn't change anything, no need to wake up or kick other CPUs
  659. */
  660. if (set == level)
  661. return 0;
  662. /*
  663. * The vcpu irq_lines field was updated, wake up sleeping VCPUs and
  664. * trigger a world-switch round on the running physical CPU to set the
  665. * virtual IRQ/FIQ fields in the HCR appropriately.
  666. */
  667. kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu);
  668. kvm_vcpu_kick(vcpu);
  669. return 0;
  670. }
  671. int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level,
  672. bool line_status)
  673. {
  674. u32 irq = irq_level->irq;
  675. unsigned int irq_type, vcpu_idx, irq_num;
  676. int nrcpus = atomic_read(&kvm->online_vcpus);
  677. struct kvm_vcpu *vcpu = NULL;
  678. bool level = irq_level->level;
  679. irq_type = (irq >> KVM_ARM_IRQ_TYPE_SHIFT) & KVM_ARM_IRQ_TYPE_MASK;
  680. vcpu_idx = (irq >> KVM_ARM_IRQ_VCPU_SHIFT) & KVM_ARM_IRQ_VCPU_MASK;
  681. irq_num = (irq >> KVM_ARM_IRQ_NUM_SHIFT) & KVM_ARM_IRQ_NUM_MASK;
  682. trace_kvm_irq_line(irq_type, vcpu_idx, irq_num, irq_level->level);
  683. switch (irq_type) {
  684. case KVM_ARM_IRQ_TYPE_CPU:
  685. if (irqchip_in_kernel(kvm))
  686. return -ENXIO;
  687. if (vcpu_idx >= nrcpus)
  688. return -EINVAL;
  689. vcpu = kvm_get_vcpu(kvm, vcpu_idx);
  690. if (!vcpu)
  691. return -EINVAL;
  692. if (irq_num > KVM_ARM_IRQ_CPU_FIQ)
  693. return -EINVAL;
  694. return vcpu_interrupt_line(vcpu, irq_num, level);
  695. case KVM_ARM_IRQ_TYPE_PPI:
  696. if (!irqchip_in_kernel(kvm))
  697. return -ENXIO;
  698. if (vcpu_idx >= nrcpus)
  699. return -EINVAL;
  700. vcpu = kvm_get_vcpu(kvm, vcpu_idx);
  701. if (!vcpu)
  702. return -EINVAL;
  703. if (irq_num < VGIC_NR_SGIS || irq_num >= VGIC_NR_PRIVATE_IRQS)
  704. return -EINVAL;
  705. return kvm_vgic_inject_irq(kvm, vcpu->vcpu_id, irq_num, level, NULL);
  706. case KVM_ARM_IRQ_TYPE_SPI:
  707. if (!irqchip_in_kernel(kvm))
  708. return -ENXIO;
  709. if (irq_num < VGIC_NR_PRIVATE_IRQS)
  710. return -EINVAL;
  711. return kvm_vgic_inject_irq(kvm, 0, irq_num, level, NULL);
  712. }
  713. return -EINVAL;
  714. }
  715. static int kvm_vcpu_set_target(struct kvm_vcpu *vcpu,
  716. const struct kvm_vcpu_init *init)
  717. {
  718. unsigned int i;
  719. int phys_target = kvm_target_cpu();
  720. if (init->target != phys_target)
  721. return -EINVAL;
  722. /*
  723. * Secondary and subsequent calls to KVM_ARM_VCPU_INIT must
  724. * use the same target.
  725. */
  726. if (vcpu->arch.target != -1 && vcpu->arch.target != init->target)
  727. return -EINVAL;
  728. /* -ENOENT for unknown features, -EINVAL for invalid combinations. */
  729. for (i = 0; i < sizeof(init->features) * 8; i++) {
  730. bool set = (init->features[i / 32] & (1 << (i % 32)));
  731. if (set && i >= KVM_VCPU_MAX_FEATURES)
  732. return -ENOENT;
  733. /*
  734. * Secondary and subsequent calls to KVM_ARM_VCPU_INIT must
  735. * use the same feature set.
  736. */
  737. if (vcpu->arch.target != -1 && i < KVM_VCPU_MAX_FEATURES &&
  738. test_bit(i, vcpu->arch.features) != set)
  739. return -EINVAL;
  740. if (set)
  741. set_bit(i, vcpu->arch.features);
  742. }
  743. vcpu->arch.target = phys_target;
  744. /* Now we know what it is, we can reset it. */
  745. return kvm_reset_vcpu(vcpu);
  746. }
  747. static int kvm_arch_vcpu_ioctl_vcpu_init(struct kvm_vcpu *vcpu,
  748. struct kvm_vcpu_init *init)
  749. {
  750. int ret;
  751. ret = kvm_vcpu_set_target(vcpu, init);
  752. if (ret)
  753. return ret;
  754. /*
  755. * Ensure a rebooted VM will fault in RAM pages and detect if the
  756. * guest MMU is turned off and flush the caches as needed.
  757. */
  758. if (vcpu->arch.has_run_once)
  759. stage2_unmap_vm(vcpu->kvm);
  760. vcpu_reset_hcr(vcpu);
  761. /*
  762. * Handle the "start in power-off" case.
  763. */
  764. if (test_bit(KVM_ARM_VCPU_POWER_OFF, vcpu->arch.features))
  765. vcpu_power_off(vcpu);
  766. else
  767. vcpu->arch.power_off = false;
  768. return 0;
  769. }
  770. static int kvm_arm_vcpu_set_attr(struct kvm_vcpu *vcpu,
  771. struct kvm_device_attr *attr)
  772. {
  773. int ret = -ENXIO;
  774. switch (attr->group) {
  775. default:
  776. ret = kvm_arm_vcpu_arch_set_attr(vcpu, attr);
  777. break;
  778. }
  779. return ret;
  780. }
  781. static int kvm_arm_vcpu_get_attr(struct kvm_vcpu *vcpu,
  782. struct kvm_device_attr *attr)
  783. {
  784. int ret = -ENXIO;
  785. switch (attr->group) {
  786. default:
  787. ret = kvm_arm_vcpu_arch_get_attr(vcpu, attr);
  788. break;
  789. }
  790. return ret;
  791. }
  792. static int kvm_arm_vcpu_has_attr(struct kvm_vcpu *vcpu,
  793. struct kvm_device_attr *attr)
  794. {
  795. int ret = -ENXIO;
  796. switch (attr->group) {
  797. default:
  798. ret = kvm_arm_vcpu_arch_has_attr(vcpu, attr);
  799. break;
  800. }
  801. return ret;
  802. }
  803. long kvm_arch_vcpu_ioctl(struct file *filp,
  804. unsigned int ioctl, unsigned long arg)
  805. {
  806. struct kvm_vcpu *vcpu = filp->private_data;
  807. void __user *argp = (void __user *)arg;
  808. struct kvm_device_attr attr;
  809. switch (ioctl) {
  810. case KVM_ARM_VCPU_INIT: {
  811. struct kvm_vcpu_init init;
  812. if (copy_from_user(&init, argp, sizeof(init)))
  813. return -EFAULT;
  814. return kvm_arch_vcpu_ioctl_vcpu_init(vcpu, &init);
  815. }
  816. case KVM_SET_ONE_REG:
  817. case KVM_GET_ONE_REG: {
  818. struct kvm_one_reg reg;
  819. if (unlikely(!kvm_vcpu_initialized(vcpu)))
  820. return -ENOEXEC;
  821. if (copy_from_user(&reg, argp, sizeof(reg)))
  822. return -EFAULT;
  823. if (ioctl == KVM_SET_ONE_REG)
  824. return kvm_arm_set_reg(vcpu, &reg);
  825. else
  826. return kvm_arm_get_reg(vcpu, &reg);
  827. }
  828. case KVM_GET_REG_LIST: {
  829. struct kvm_reg_list __user *user_list = argp;
  830. struct kvm_reg_list reg_list;
  831. unsigned n;
  832. if (unlikely(!kvm_vcpu_initialized(vcpu)))
  833. return -ENOEXEC;
  834. if (copy_from_user(&reg_list, user_list, sizeof(reg_list)))
  835. return -EFAULT;
  836. n = reg_list.n;
  837. reg_list.n = kvm_arm_num_regs(vcpu);
  838. if (copy_to_user(user_list, &reg_list, sizeof(reg_list)))
  839. return -EFAULT;
  840. if (n < reg_list.n)
  841. return -E2BIG;
  842. return kvm_arm_copy_reg_indices(vcpu, user_list->reg);
  843. }
  844. case KVM_SET_DEVICE_ATTR: {
  845. if (copy_from_user(&attr, argp, sizeof(attr)))
  846. return -EFAULT;
  847. return kvm_arm_vcpu_set_attr(vcpu, &attr);
  848. }
  849. case KVM_GET_DEVICE_ATTR: {
  850. if (copy_from_user(&attr, argp, sizeof(attr)))
  851. return -EFAULT;
  852. return kvm_arm_vcpu_get_attr(vcpu, &attr);
  853. }
  854. case KVM_HAS_DEVICE_ATTR: {
  855. if (copy_from_user(&attr, argp, sizeof(attr)))
  856. return -EFAULT;
  857. return kvm_arm_vcpu_has_attr(vcpu, &attr);
  858. }
  859. default:
  860. return -EINVAL;
  861. }
  862. }
  863. /**
  864. * kvm_vm_ioctl_get_dirty_log - get and clear the log of dirty pages in a slot
  865. * @kvm: kvm instance
  866. * @log: slot id and address to which we copy the log
  867. *
  868. * Steps 1-4 below provide general overview of dirty page logging. See
  869. * kvm_get_dirty_log_protect() function description for additional details.
  870. *
  871. * We call kvm_get_dirty_log_protect() to handle steps 1-3, upon return we
  872. * always flush the TLB (step 4) even if previous step failed and the dirty
  873. * bitmap may be corrupt. Regardless of previous outcome the KVM logging API
  874. * does not preclude user space subsequent dirty log read. Flushing TLB ensures
  875. * writes will be marked dirty for next log read.
  876. *
  877. * 1. Take a snapshot of the bit and clear it if needed.
  878. * 2. Write protect the corresponding page.
  879. * 3. Copy the snapshot to the userspace.
  880. * 4. Flush TLB's if needed.
  881. */
  882. int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
  883. {
  884. bool is_dirty = false;
  885. int r;
  886. mutex_lock(&kvm->slots_lock);
  887. r = kvm_get_dirty_log_protect(kvm, log, &is_dirty);
  888. if (is_dirty)
  889. kvm_flush_remote_tlbs(kvm);
  890. mutex_unlock(&kvm->slots_lock);
  891. return r;
  892. }
  893. static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm,
  894. struct kvm_arm_device_addr *dev_addr)
  895. {
  896. unsigned long dev_id, type;
  897. dev_id = (dev_addr->id & KVM_ARM_DEVICE_ID_MASK) >>
  898. KVM_ARM_DEVICE_ID_SHIFT;
  899. type = (dev_addr->id & KVM_ARM_DEVICE_TYPE_MASK) >>
  900. KVM_ARM_DEVICE_TYPE_SHIFT;
  901. switch (dev_id) {
  902. case KVM_ARM_DEVICE_VGIC_V2:
  903. if (!vgic_present)
  904. return -ENXIO;
  905. return kvm_vgic_addr(kvm, type, &dev_addr->addr, true);
  906. default:
  907. return -ENODEV;
  908. }
  909. }
  910. long kvm_arch_vm_ioctl(struct file *filp,
  911. unsigned int ioctl, unsigned long arg)
  912. {
  913. struct kvm *kvm = filp->private_data;
  914. void __user *argp = (void __user *)arg;
  915. switch (ioctl) {
  916. case KVM_CREATE_IRQCHIP: {
  917. int ret;
  918. if (!vgic_present)
  919. return -ENXIO;
  920. mutex_lock(&kvm->lock);
  921. ret = kvm_vgic_create(kvm, KVM_DEV_TYPE_ARM_VGIC_V2);
  922. mutex_unlock(&kvm->lock);
  923. return ret;
  924. }
  925. case KVM_ARM_SET_DEVICE_ADDR: {
  926. struct kvm_arm_device_addr dev_addr;
  927. if (copy_from_user(&dev_addr, argp, sizeof(dev_addr)))
  928. return -EFAULT;
  929. return kvm_vm_ioctl_set_device_addr(kvm, &dev_addr);
  930. }
  931. case KVM_ARM_PREFERRED_TARGET: {
  932. int err;
  933. struct kvm_vcpu_init init;
  934. err = kvm_vcpu_preferred_target(&init);
  935. if (err)
  936. return err;
  937. if (copy_to_user(argp, &init, sizeof(init)))
  938. return -EFAULT;
  939. return 0;
  940. }
  941. default:
  942. return -EINVAL;
  943. }
  944. }
  945. static void cpu_init_hyp_mode(void *dummy)
  946. {
  947. phys_addr_t pgd_ptr;
  948. unsigned long hyp_stack_ptr;
  949. unsigned long stack_page;
  950. unsigned long vector_ptr;
  951. /* Switch from the HYP stub to our own HYP init vector */
  952. __hyp_set_vectors(kvm_get_idmap_vector());
  953. pgd_ptr = kvm_mmu_get_httbr();
  954. stack_page = __this_cpu_read(kvm_arm_hyp_stack_page);
  955. hyp_stack_ptr = stack_page + PAGE_SIZE;
  956. vector_ptr = (unsigned long)kvm_ksym_ref(__kvm_hyp_vector);
  957. __cpu_init_hyp_mode(pgd_ptr, hyp_stack_ptr, vector_ptr);
  958. __cpu_init_stage2();
  959. kvm_arm_init_debug();
  960. }
  961. static void cpu_hyp_reset(void)
  962. {
  963. if (!is_kernel_in_hyp_mode())
  964. __hyp_reset_vectors();
  965. }
  966. static void cpu_hyp_reinit(void)
  967. {
  968. cpu_hyp_reset();
  969. if (is_kernel_in_hyp_mode()) {
  970. /*
  971. * __cpu_init_stage2() is safe to call even if the PM
  972. * event was cancelled before the CPU was reset.
  973. */
  974. __cpu_init_stage2();
  975. kvm_timer_init_vhe();
  976. } else {
  977. cpu_init_hyp_mode(NULL);
  978. }
  979. if (vgic_present)
  980. kvm_vgic_init_cpu_hardware();
  981. }
  982. static void _kvm_arch_hardware_enable(void *discard)
  983. {
  984. if (!__this_cpu_read(kvm_arm_hardware_enabled)) {
  985. cpu_hyp_reinit();
  986. __this_cpu_write(kvm_arm_hardware_enabled, 1);
  987. }
  988. }
  989. int kvm_arch_hardware_enable(void)
  990. {
  991. _kvm_arch_hardware_enable(NULL);
  992. return 0;
  993. }
  994. static void _kvm_arch_hardware_disable(void *discard)
  995. {
  996. if (__this_cpu_read(kvm_arm_hardware_enabled)) {
  997. cpu_hyp_reset();
  998. __this_cpu_write(kvm_arm_hardware_enabled, 0);
  999. }
  1000. }
  1001. void kvm_arch_hardware_disable(void)
  1002. {
  1003. _kvm_arch_hardware_disable(NULL);
  1004. }
  1005. #ifdef CONFIG_CPU_PM
  1006. static int hyp_init_cpu_pm_notifier(struct notifier_block *self,
  1007. unsigned long cmd,
  1008. void *v)
  1009. {
  1010. /*
  1011. * kvm_arm_hardware_enabled is left with its old value over
  1012. * PM_ENTER->PM_EXIT. It is used to indicate PM_EXIT should
  1013. * re-enable hyp.
  1014. */
  1015. switch (cmd) {
  1016. case CPU_PM_ENTER:
  1017. if (__this_cpu_read(kvm_arm_hardware_enabled))
  1018. /*
  1019. * don't update kvm_arm_hardware_enabled here
  1020. * so that the hardware will be re-enabled
  1021. * when we resume. See below.
  1022. */
  1023. cpu_hyp_reset();
  1024. return NOTIFY_OK;
  1025. case CPU_PM_EXIT:
  1026. if (__this_cpu_read(kvm_arm_hardware_enabled))
  1027. /* The hardware was enabled before suspend. */
  1028. cpu_hyp_reinit();
  1029. return NOTIFY_OK;
  1030. default:
  1031. return NOTIFY_DONE;
  1032. }
  1033. }
  1034. static struct notifier_block hyp_init_cpu_pm_nb = {
  1035. .notifier_call = hyp_init_cpu_pm_notifier,
  1036. };
  1037. static void __init hyp_cpu_pm_init(void)
  1038. {
  1039. cpu_pm_register_notifier(&hyp_init_cpu_pm_nb);
  1040. }
  1041. static void __init hyp_cpu_pm_exit(void)
  1042. {
  1043. cpu_pm_unregister_notifier(&hyp_init_cpu_pm_nb);
  1044. }
  1045. #else
  1046. static inline void hyp_cpu_pm_init(void)
  1047. {
  1048. }
  1049. static inline void hyp_cpu_pm_exit(void)
  1050. {
  1051. }
  1052. #endif
  1053. static void teardown_common_resources(void)
  1054. {
  1055. free_percpu(kvm_host_cpu_state);
  1056. }
  1057. static int init_common_resources(void)
  1058. {
  1059. kvm_host_cpu_state = alloc_percpu(kvm_cpu_context_t);
  1060. if (!kvm_host_cpu_state) {
  1061. kvm_err("Cannot allocate host CPU state\n");
  1062. return -ENOMEM;
  1063. }
  1064. /* set size of VMID supported by CPU */
  1065. kvm_vmid_bits = kvm_get_vmid_bits();
  1066. kvm_info("%d-bit VMID\n", kvm_vmid_bits);
  1067. return 0;
  1068. }
  1069. static int init_subsystems(void)
  1070. {
  1071. int err = 0;
  1072. /*
  1073. * Enable hardware so that subsystem initialisation can access EL2.
  1074. */
  1075. on_each_cpu(_kvm_arch_hardware_enable, NULL, 1);
  1076. /*
  1077. * Register CPU lower-power notifier
  1078. */
  1079. hyp_cpu_pm_init();
  1080. /*
  1081. * Init HYP view of VGIC
  1082. */
  1083. err = kvm_vgic_hyp_init();
  1084. switch (err) {
  1085. case 0:
  1086. vgic_present = true;
  1087. break;
  1088. case -ENODEV:
  1089. case -ENXIO:
  1090. vgic_present = false;
  1091. err = 0;
  1092. break;
  1093. default:
  1094. goto out;
  1095. }
  1096. /*
  1097. * Init HYP architected timer support
  1098. */
  1099. err = kvm_timer_hyp_init();
  1100. if (err)
  1101. goto out;
  1102. kvm_perf_init();
  1103. kvm_coproc_table_init();
  1104. out:
  1105. on_each_cpu(_kvm_arch_hardware_disable, NULL, 1);
  1106. return err;
  1107. }
  1108. static void teardown_hyp_mode(void)
  1109. {
  1110. int cpu;
  1111. free_hyp_pgds();
  1112. for_each_possible_cpu(cpu)
  1113. free_page(per_cpu(kvm_arm_hyp_stack_page, cpu));
  1114. hyp_cpu_pm_exit();
  1115. }
  1116. /**
  1117. * Inits Hyp-mode on all online CPUs
  1118. */
  1119. static int init_hyp_mode(void)
  1120. {
  1121. int cpu;
  1122. int err = 0;
  1123. /*
  1124. * Allocate Hyp PGD and setup Hyp identity mapping
  1125. */
  1126. err = kvm_mmu_init();
  1127. if (err)
  1128. goto out_err;
  1129. /*
  1130. * Allocate stack pages for Hypervisor-mode
  1131. */
  1132. for_each_possible_cpu(cpu) {
  1133. unsigned long stack_page;
  1134. stack_page = __get_free_page(GFP_KERNEL);
  1135. if (!stack_page) {
  1136. err = -ENOMEM;
  1137. goto out_err;
  1138. }
  1139. per_cpu(kvm_arm_hyp_stack_page, cpu) = stack_page;
  1140. }
  1141. /*
  1142. * Map the Hyp-code called directly from the host
  1143. */
  1144. err = create_hyp_mappings(kvm_ksym_ref(__hyp_text_start),
  1145. kvm_ksym_ref(__hyp_text_end), PAGE_HYP_EXEC);
  1146. if (err) {
  1147. kvm_err("Cannot map world-switch code\n");
  1148. goto out_err;
  1149. }
  1150. err = create_hyp_mappings(kvm_ksym_ref(__start_rodata),
  1151. kvm_ksym_ref(__end_rodata), PAGE_HYP_RO);
  1152. if (err) {
  1153. kvm_err("Cannot map rodata section\n");
  1154. goto out_err;
  1155. }
  1156. err = create_hyp_mappings(kvm_ksym_ref(__bss_start),
  1157. kvm_ksym_ref(__bss_stop), PAGE_HYP_RO);
  1158. if (err) {
  1159. kvm_err("Cannot map bss section\n");
  1160. goto out_err;
  1161. }
  1162. /*
  1163. * Map the Hyp stack pages
  1164. */
  1165. for_each_possible_cpu(cpu) {
  1166. char *stack_page = (char *)per_cpu(kvm_arm_hyp_stack_page, cpu);
  1167. err = create_hyp_mappings(stack_page, stack_page + PAGE_SIZE,
  1168. PAGE_HYP);
  1169. if (err) {
  1170. kvm_err("Cannot map hyp stack\n");
  1171. goto out_err;
  1172. }
  1173. }
  1174. for_each_possible_cpu(cpu) {
  1175. kvm_cpu_context_t *cpu_ctxt;
  1176. cpu_ctxt = per_cpu_ptr(kvm_host_cpu_state, cpu);
  1177. err = create_hyp_mappings(cpu_ctxt, cpu_ctxt + 1, PAGE_HYP);
  1178. if (err) {
  1179. kvm_err("Cannot map host CPU state: %d\n", err);
  1180. goto out_err;
  1181. }
  1182. }
  1183. return 0;
  1184. out_err:
  1185. teardown_hyp_mode();
  1186. kvm_err("error initializing Hyp mode: %d\n", err);
  1187. return err;
  1188. }
  1189. static void check_kvm_target_cpu(void *ret)
  1190. {
  1191. *(int *)ret = kvm_target_cpu();
  1192. }
  1193. struct kvm_vcpu *kvm_mpidr_to_vcpu(struct kvm *kvm, unsigned long mpidr)
  1194. {
  1195. struct kvm_vcpu *vcpu;
  1196. int i;
  1197. mpidr &= MPIDR_HWID_BITMASK;
  1198. kvm_for_each_vcpu(i, vcpu, kvm) {
  1199. if (mpidr == kvm_vcpu_get_mpidr_aff(vcpu))
  1200. return vcpu;
  1201. }
  1202. return NULL;
  1203. }
  1204. bool kvm_arch_has_irq_bypass(void)
  1205. {
  1206. return true;
  1207. }
  1208. int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons,
  1209. struct irq_bypass_producer *prod)
  1210. {
  1211. struct kvm_kernel_irqfd *irqfd =
  1212. container_of(cons, struct kvm_kernel_irqfd, consumer);
  1213. return kvm_vgic_v4_set_forwarding(irqfd->kvm, prod->irq,
  1214. &irqfd->irq_entry);
  1215. }
  1216. void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons,
  1217. struct irq_bypass_producer *prod)
  1218. {
  1219. struct kvm_kernel_irqfd *irqfd =
  1220. container_of(cons, struct kvm_kernel_irqfd, consumer);
  1221. kvm_vgic_v4_unset_forwarding(irqfd->kvm, prod->irq,
  1222. &irqfd->irq_entry);
  1223. }
  1224. void kvm_arch_irq_bypass_stop(struct irq_bypass_consumer *cons)
  1225. {
  1226. struct kvm_kernel_irqfd *irqfd =
  1227. container_of(cons, struct kvm_kernel_irqfd, consumer);
  1228. kvm_arm_halt_guest(irqfd->kvm);
  1229. }
  1230. void kvm_arch_irq_bypass_start(struct irq_bypass_consumer *cons)
  1231. {
  1232. struct kvm_kernel_irqfd *irqfd =
  1233. container_of(cons, struct kvm_kernel_irqfd, consumer);
  1234. kvm_arm_resume_guest(irqfd->kvm);
  1235. }
  1236. /**
  1237. * Initialize Hyp-mode and memory mappings on all CPUs.
  1238. */
  1239. int kvm_arch_init(void *opaque)
  1240. {
  1241. int err;
  1242. int ret, cpu;
  1243. bool in_hyp_mode;
  1244. if (!is_hyp_mode_available()) {
  1245. kvm_info("HYP mode not available\n");
  1246. return -ENODEV;
  1247. }
  1248. for_each_online_cpu(cpu) {
  1249. smp_call_function_single(cpu, check_kvm_target_cpu, &ret, 1);
  1250. if (ret < 0) {
  1251. kvm_err("Error, CPU %d not supported!\n", cpu);
  1252. return -ENODEV;
  1253. }
  1254. }
  1255. err = init_common_resources();
  1256. if (err)
  1257. return err;
  1258. in_hyp_mode = is_kernel_in_hyp_mode();
  1259. if (!in_hyp_mode) {
  1260. err = init_hyp_mode();
  1261. if (err)
  1262. goto out_err;
  1263. }
  1264. err = init_subsystems();
  1265. if (err)
  1266. goto out_hyp;
  1267. if (in_hyp_mode)
  1268. kvm_info("VHE mode initialized successfully\n");
  1269. else
  1270. kvm_info("Hyp mode initialized successfully\n");
  1271. return 0;
  1272. out_hyp:
  1273. if (!in_hyp_mode)
  1274. teardown_hyp_mode();
  1275. out_err:
  1276. teardown_common_resources();
  1277. return err;
  1278. }
  1279. /* NOP: Compiling as a module not supported */
  1280. void kvm_arch_exit(void)
  1281. {
  1282. kvm_perf_teardown();
  1283. }
  1284. static int arm_init(void)
  1285. {
  1286. int rc = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
  1287. return rc;
  1288. }
  1289. module_init(arm_init);