arm.c 32 KB

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