arm.c 30 KB

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