arm.c 31 KB

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