arm.c 34 KB

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