arm.c 37 KB

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