arm.c 33 KB

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