arm.c 31 KB

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