arm.c 38 KB

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