arm.c 32 KB

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