arm.c 32 KB

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