arm.c 33 KB

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