arm.c 31 KB

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