arm.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730
  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. kvm_arm_vcpu_init_debug(vcpu);
  485. if (likely(irqchip_in_kernel(kvm))) {
  486. /*
  487. * Map the VGIC hardware resources before running a vcpu the
  488. * first time on this VM.
  489. */
  490. if (unlikely(!vgic_ready(kvm))) {
  491. ret = kvm_vgic_map_resources(kvm);
  492. if (ret)
  493. return ret;
  494. }
  495. } else {
  496. /*
  497. * Tell the rest of the code that there are userspace irqchip
  498. * VMs in the wild.
  499. */
  500. static_branch_inc(&userspace_irqchip_in_use);
  501. }
  502. ret = kvm_timer_enable(vcpu);
  503. if (ret)
  504. return ret;
  505. ret = kvm_arm_pmu_v3_enable(vcpu);
  506. return ret;
  507. }
  508. bool kvm_arch_intc_initialized(struct kvm *kvm)
  509. {
  510. return vgic_initialized(kvm);
  511. }
  512. void kvm_arm_halt_guest(struct kvm *kvm)
  513. {
  514. int i;
  515. struct kvm_vcpu *vcpu;
  516. kvm_for_each_vcpu(i, vcpu, kvm)
  517. vcpu->arch.pause = true;
  518. kvm_make_all_cpus_request(kvm, KVM_REQ_SLEEP);
  519. }
  520. void kvm_arm_resume_guest(struct kvm *kvm)
  521. {
  522. int i;
  523. struct kvm_vcpu *vcpu;
  524. kvm_for_each_vcpu(i, vcpu, kvm) {
  525. vcpu->arch.pause = false;
  526. swake_up_one(kvm_arch_vcpu_wq(vcpu));
  527. }
  528. }
  529. static void vcpu_req_sleep(struct kvm_vcpu *vcpu)
  530. {
  531. struct swait_queue_head *wq = kvm_arch_vcpu_wq(vcpu);
  532. swait_event_interruptible_exclusive(*wq, ((!vcpu->arch.power_off) &&
  533. (!vcpu->arch.pause)));
  534. if (vcpu->arch.power_off || vcpu->arch.pause) {
  535. /* Awaken to handle a signal, request we sleep again later. */
  536. kvm_make_request(KVM_REQ_SLEEP, vcpu);
  537. }
  538. /*
  539. * Make sure we will observe a potential reset request if we've
  540. * observed a change to the power state. Pairs with the smp_wmb() in
  541. * kvm_psci_vcpu_on().
  542. */
  543. smp_rmb();
  544. }
  545. static int kvm_vcpu_initialized(struct kvm_vcpu *vcpu)
  546. {
  547. return vcpu->arch.target >= 0;
  548. }
  549. static void check_vcpu_requests(struct kvm_vcpu *vcpu)
  550. {
  551. if (kvm_request_pending(vcpu)) {
  552. if (kvm_check_request(KVM_REQ_SLEEP, vcpu))
  553. vcpu_req_sleep(vcpu);
  554. if (kvm_check_request(KVM_REQ_VCPU_RESET, vcpu))
  555. kvm_reset_vcpu(vcpu);
  556. /*
  557. * Clear IRQ_PENDING requests that were made to guarantee
  558. * that a VCPU sees new virtual interrupts.
  559. */
  560. kvm_check_request(KVM_REQ_IRQ_PENDING, vcpu);
  561. }
  562. }
  563. /**
  564. * kvm_arch_vcpu_ioctl_run - the main VCPU run function to execute guest code
  565. * @vcpu: The VCPU pointer
  566. * @run: The kvm_run structure pointer used for userspace state exchange
  567. *
  568. * This function is called through the VCPU_RUN ioctl called from user space. It
  569. * will execute VM code in a loop until the time slice for the process is used
  570. * or some emulation is needed from user space in which case the function will
  571. * return with return value 0 and with the kvm_run structure filled in with the
  572. * required data for the requested emulation.
  573. */
  574. int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
  575. {
  576. int ret;
  577. if (unlikely(!kvm_vcpu_initialized(vcpu)))
  578. return -ENOEXEC;
  579. ret = kvm_vcpu_first_run_init(vcpu);
  580. if (ret)
  581. return ret;
  582. if (run->exit_reason == KVM_EXIT_MMIO) {
  583. ret = kvm_handle_mmio_return(vcpu, vcpu->run);
  584. if (ret)
  585. return ret;
  586. if (kvm_arm_handle_step_debug(vcpu, vcpu->run))
  587. return 0;
  588. }
  589. if (run->immediate_exit)
  590. return -EINTR;
  591. vcpu_load(vcpu);
  592. kvm_sigset_activate(vcpu);
  593. ret = 1;
  594. run->exit_reason = KVM_EXIT_UNKNOWN;
  595. while (ret > 0) {
  596. /*
  597. * Check conditions before entering the guest
  598. */
  599. cond_resched();
  600. update_vttbr(vcpu->kvm);
  601. check_vcpu_requests(vcpu);
  602. /*
  603. * Preparing the interrupts to be injected also
  604. * involves poking the GIC, which must be done in a
  605. * non-preemptible context.
  606. */
  607. preempt_disable();
  608. kvm_pmu_flush_hwstate(vcpu);
  609. local_irq_disable();
  610. kvm_vgic_flush_hwstate(vcpu);
  611. /*
  612. * Exit if we have a signal pending so that we can deliver the
  613. * signal to user space.
  614. */
  615. if (signal_pending(current)) {
  616. ret = -EINTR;
  617. run->exit_reason = KVM_EXIT_INTR;
  618. }
  619. /*
  620. * If we're using a userspace irqchip, then check if we need
  621. * to tell a userspace irqchip about timer or PMU level
  622. * changes and if so, exit to userspace (the actual level
  623. * state gets updated in kvm_timer_update_run and
  624. * kvm_pmu_update_run below).
  625. */
  626. if (static_branch_unlikely(&userspace_irqchip_in_use)) {
  627. if (kvm_timer_should_notify_user(vcpu) ||
  628. kvm_pmu_should_notify_user(vcpu)) {
  629. ret = -EINTR;
  630. run->exit_reason = KVM_EXIT_INTR;
  631. }
  632. }
  633. /*
  634. * Ensure we set mode to IN_GUEST_MODE after we disable
  635. * interrupts and before the final VCPU requests check.
  636. * See the comment in kvm_vcpu_exiting_guest_mode() and
  637. * Documentation/virtual/kvm/vcpu-requests.rst
  638. */
  639. smp_store_mb(vcpu->mode, IN_GUEST_MODE);
  640. if (ret <= 0 || need_new_vmid_gen(vcpu->kvm) ||
  641. kvm_request_pending(vcpu)) {
  642. vcpu->mode = OUTSIDE_GUEST_MODE;
  643. isb(); /* Ensure work in x_flush_hwstate is committed */
  644. kvm_pmu_sync_hwstate(vcpu);
  645. if (static_branch_unlikely(&userspace_irqchip_in_use))
  646. kvm_timer_sync_hwstate(vcpu);
  647. kvm_vgic_sync_hwstate(vcpu);
  648. local_irq_enable();
  649. preempt_enable();
  650. continue;
  651. }
  652. kvm_arm_setup_debug(vcpu);
  653. /**************************************************************
  654. * Enter the guest
  655. */
  656. trace_kvm_entry(*vcpu_pc(vcpu));
  657. guest_enter_irqoff();
  658. if (has_vhe()) {
  659. kvm_arm_vhe_guest_enter();
  660. ret = kvm_vcpu_run_vhe(vcpu);
  661. kvm_arm_vhe_guest_exit();
  662. } else {
  663. ret = kvm_call_hyp(__kvm_vcpu_run_nvhe, vcpu);
  664. }
  665. vcpu->mode = OUTSIDE_GUEST_MODE;
  666. vcpu->stat.exits++;
  667. /*
  668. * Back from guest
  669. *************************************************************/
  670. kvm_arm_clear_debug(vcpu);
  671. /*
  672. * We must sync the PMU state before the vgic state so
  673. * that the vgic can properly sample the updated state of the
  674. * interrupt line.
  675. */
  676. kvm_pmu_sync_hwstate(vcpu);
  677. /*
  678. * Sync the vgic state before syncing the timer state because
  679. * the timer code needs to know if the virtual timer
  680. * interrupts are active.
  681. */
  682. kvm_vgic_sync_hwstate(vcpu);
  683. /*
  684. * Sync the timer hardware state before enabling interrupts as
  685. * we don't want vtimer interrupts to race with syncing the
  686. * timer virtual interrupt state.
  687. */
  688. if (static_branch_unlikely(&userspace_irqchip_in_use))
  689. kvm_timer_sync_hwstate(vcpu);
  690. kvm_arch_vcpu_ctxsync_fp(vcpu);
  691. /*
  692. * We may have taken a host interrupt in HYP mode (ie
  693. * while executing the guest). This interrupt is still
  694. * pending, as we haven't serviced it yet!
  695. *
  696. * We're now back in SVC mode, with interrupts
  697. * disabled. Enabling the interrupts now will have
  698. * the effect of taking the interrupt again, in SVC
  699. * mode this time.
  700. */
  701. local_irq_enable();
  702. /*
  703. * We do local_irq_enable() before calling guest_exit() so
  704. * that if a timer interrupt hits while running the guest we
  705. * account that tick as being spent in the guest. We enable
  706. * preemption after calling guest_exit() so that if we get
  707. * preempted we make sure ticks after that is not counted as
  708. * guest time.
  709. */
  710. guest_exit();
  711. trace_kvm_exit(ret, kvm_vcpu_trap_get_class(vcpu), *vcpu_pc(vcpu));
  712. /* Exit types that need handling before we can be preempted */
  713. handle_exit_early(vcpu, run, ret);
  714. preempt_enable();
  715. ret = handle_exit(vcpu, run, ret);
  716. }
  717. /* Tell userspace about in-kernel device output levels */
  718. if (unlikely(!irqchip_in_kernel(vcpu->kvm))) {
  719. kvm_timer_update_run(vcpu);
  720. kvm_pmu_update_run(vcpu);
  721. }
  722. kvm_sigset_deactivate(vcpu);
  723. vcpu_put(vcpu);
  724. return ret;
  725. }
  726. static int vcpu_interrupt_line(struct kvm_vcpu *vcpu, int number, bool level)
  727. {
  728. int bit_index;
  729. bool set;
  730. unsigned long *hcr;
  731. if (number == KVM_ARM_IRQ_CPU_IRQ)
  732. bit_index = __ffs(HCR_VI);
  733. else /* KVM_ARM_IRQ_CPU_FIQ */
  734. bit_index = __ffs(HCR_VF);
  735. hcr = vcpu_hcr(vcpu);
  736. if (level)
  737. set = test_and_set_bit(bit_index, hcr);
  738. else
  739. set = test_and_clear_bit(bit_index, hcr);
  740. /*
  741. * If we didn't change anything, no need to wake up or kick other CPUs
  742. */
  743. if (set == level)
  744. return 0;
  745. /*
  746. * The vcpu irq_lines field was updated, wake up sleeping VCPUs and
  747. * trigger a world-switch round on the running physical CPU to set the
  748. * virtual IRQ/FIQ fields in the HCR appropriately.
  749. */
  750. kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu);
  751. kvm_vcpu_kick(vcpu);
  752. return 0;
  753. }
  754. int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level,
  755. bool line_status)
  756. {
  757. u32 irq = irq_level->irq;
  758. unsigned int irq_type, vcpu_idx, irq_num;
  759. int nrcpus = atomic_read(&kvm->online_vcpus);
  760. struct kvm_vcpu *vcpu = NULL;
  761. bool level = irq_level->level;
  762. irq_type = (irq >> KVM_ARM_IRQ_TYPE_SHIFT) & KVM_ARM_IRQ_TYPE_MASK;
  763. vcpu_idx = (irq >> KVM_ARM_IRQ_VCPU_SHIFT) & KVM_ARM_IRQ_VCPU_MASK;
  764. irq_num = (irq >> KVM_ARM_IRQ_NUM_SHIFT) & KVM_ARM_IRQ_NUM_MASK;
  765. trace_kvm_irq_line(irq_type, vcpu_idx, irq_num, irq_level->level);
  766. switch (irq_type) {
  767. case KVM_ARM_IRQ_TYPE_CPU:
  768. if (irqchip_in_kernel(kvm))
  769. return -ENXIO;
  770. if (vcpu_idx >= nrcpus)
  771. return -EINVAL;
  772. vcpu = kvm_get_vcpu(kvm, vcpu_idx);
  773. if (!vcpu)
  774. return -EINVAL;
  775. if (irq_num > KVM_ARM_IRQ_CPU_FIQ)
  776. return -EINVAL;
  777. return vcpu_interrupt_line(vcpu, irq_num, level);
  778. case KVM_ARM_IRQ_TYPE_PPI:
  779. if (!irqchip_in_kernel(kvm))
  780. return -ENXIO;
  781. if (vcpu_idx >= nrcpus)
  782. return -EINVAL;
  783. vcpu = kvm_get_vcpu(kvm, vcpu_idx);
  784. if (!vcpu)
  785. return -EINVAL;
  786. if (irq_num < VGIC_NR_SGIS || irq_num >= VGIC_NR_PRIVATE_IRQS)
  787. return -EINVAL;
  788. return kvm_vgic_inject_irq(kvm, vcpu->vcpu_id, irq_num, level, NULL);
  789. case KVM_ARM_IRQ_TYPE_SPI:
  790. if (!irqchip_in_kernel(kvm))
  791. return -ENXIO;
  792. if (irq_num < VGIC_NR_PRIVATE_IRQS)
  793. return -EINVAL;
  794. return kvm_vgic_inject_irq(kvm, 0, irq_num, level, NULL);
  795. }
  796. return -EINVAL;
  797. }
  798. static int kvm_vcpu_set_target(struct kvm_vcpu *vcpu,
  799. const struct kvm_vcpu_init *init)
  800. {
  801. unsigned int i, ret;
  802. int phys_target = kvm_target_cpu();
  803. if (init->target != phys_target)
  804. return -EINVAL;
  805. /*
  806. * Secondary and subsequent calls to KVM_ARM_VCPU_INIT must
  807. * use the same target.
  808. */
  809. if (vcpu->arch.target != -1 && vcpu->arch.target != init->target)
  810. return -EINVAL;
  811. /* -ENOENT for unknown features, -EINVAL for invalid combinations. */
  812. for (i = 0; i < sizeof(init->features) * 8; i++) {
  813. bool set = (init->features[i / 32] & (1 << (i % 32)));
  814. if (set && i >= KVM_VCPU_MAX_FEATURES)
  815. return -ENOENT;
  816. /*
  817. * Secondary and subsequent calls to KVM_ARM_VCPU_INIT must
  818. * use the same feature set.
  819. */
  820. if (vcpu->arch.target != -1 && i < KVM_VCPU_MAX_FEATURES &&
  821. test_bit(i, vcpu->arch.features) != set)
  822. return -EINVAL;
  823. if (set)
  824. set_bit(i, vcpu->arch.features);
  825. }
  826. vcpu->arch.target = phys_target;
  827. /* Now we know what it is, we can reset it. */
  828. ret = kvm_reset_vcpu(vcpu);
  829. if (ret) {
  830. vcpu->arch.target = -1;
  831. bitmap_zero(vcpu->arch.features, KVM_VCPU_MAX_FEATURES);
  832. }
  833. return ret;
  834. }
  835. static int kvm_arch_vcpu_ioctl_vcpu_init(struct kvm_vcpu *vcpu,
  836. struct kvm_vcpu_init *init)
  837. {
  838. int ret;
  839. ret = kvm_vcpu_set_target(vcpu, init);
  840. if (ret)
  841. return ret;
  842. /*
  843. * Ensure a rebooted VM will fault in RAM pages and detect if the
  844. * guest MMU is turned off and flush the caches as needed.
  845. */
  846. if (vcpu->arch.has_run_once)
  847. stage2_unmap_vm(vcpu->kvm);
  848. vcpu_reset_hcr(vcpu);
  849. /*
  850. * Handle the "start in power-off" case.
  851. */
  852. if (test_bit(KVM_ARM_VCPU_POWER_OFF, vcpu->arch.features))
  853. vcpu_power_off(vcpu);
  854. else
  855. vcpu->arch.power_off = false;
  856. return 0;
  857. }
  858. static int kvm_arm_vcpu_set_attr(struct kvm_vcpu *vcpu,
  859. struct kvm_device_attr *attr)
  860. {
  861. int ret = -ENXIO;
  862. switch (attr->group) {
  863. default:
  864. ret = kvm_arm_vcpu_arch_set_attr(vcpu, attr);
  865. break;
  866. }
  867. return ret;
  868. }
  869. static int kvm_arm_vcpu_get_attr(struct kvm_vcpu *vcpu,
  870. struct kvm_device_attr *attr)
  871. {
  872. int ret = -ENXIO;
  873. switch (attr->group) {
  874. default:
  875. ret = kvm_arm_vcpu_arch_get_attr(vcpu, attr);
  876. break;
  877. }
  878. return ret;
  879. }
  880. static int kvm_arm_vcpu_has_attr(struct kvm_vcpu *vcpu,
  881. struct kvm_device_attr *attr)
  882. {
  883. int ret = -ENXIO;
  884. switch (attr->group) {
  885. default:
  886. ret = kvm_arm_vcpu_arch_has_attr(vcpu, attr);
  887. break;
  888. }
  889. return ret;
  890. }
  891. static int kvm_arm_vcpu_get_events(struct kvm_vcpu *vcpu,
  892. struct kvm_vcpu_events *events)
  893. {
  894. memset(events, 0, sizeof(*events));
  895. return __kvm_arm_vcpu_get_events(vcpu, events);
  896. }
  897. static int kvm_arm_vcpu_set_events(struct kvm_vcpu *vcpu,
  898. struct kvm_vcpu_events *events)
  899. {
  900. int i;
  901. /* check whether the reserved field is zero */
  902. for (i = 0; i < ARRAY_SIZE(events->reserved); i++)
  903. if (events->reserved[i])
  904. return -EINVAL;
  905. /* check whether the pad field is zero */
  906. for (i = 0; i < ARRAY_SIZE(events->exception.pad); i++)
  907. if (events->exception.pad[i])
  908. return -EINVAL;
  909. return __kvm_arm_vcpu_set_events(vcpu, events);
  910. }
  911. long kvm_arch_vcpu_ioctl(struct file *filp,
  912. unsigned int ioctl, unsigned long arg)
  913. {
  914. struct kvm_vcpu *vcpu = filp->private_data;
  915. void __user *argp = (void __user *)arg;
  916. struct kvm_device_attr attr;
  917. long r;
  918. switch (ioctl) {
  919. case KVM_ARM_VCPU_INIT: {
  920. struct kvm_vcpu_init init;
  921. r = -EFAULT;
  922. if (copy_from_user(&init, argp, sizeof(init)))
  923. break;
  924. r = kvm_arch_vcpu_ioctl_vcpu_init(vcpu, &init);
  925. break;
  926. }
  927. case KVM_SET_ONE_REG:
  928. case KVM_GET_ONE_REG: {
  929. struct kvm_one_reg reg;
  930. r = -ENOEXEC;
  931. if (unlikely(!kvm_vcpu_initialized(vcpu)))
  932. break;
  933. r = -EFAULT;
  934. if (copy_from_user(&reg, argp, sizeof(reg)))
  935. break;
  936. if (ioctl == KVM_SET_ONE_REG)
  937. r = kvm_arm_set_reg(vcpu, &reg);
  938. else
  939. r = kvm_arm_get_reg(vcpu, &reg);
  940. break;
  941. }
  942. case KVM_GET_REG_LIST: {
  943. struct kvm_reg_list __user *user_list = argp;
  944. struct kvm_reg_list reg_list;
  945. unsigned n;
  946. r = -ENOEXEC;
  947. if (unlikely(!kvm_vcpu_initialized(vcpu)))
  948. break;
  949. r = -EFAULT;
  950. if (copy_from_user(&reg_list, user_list, sizeof(reg_list)))
  951. break;
  952. n = reg_list.n;
  953. reg_list.n = kvm_arm_num_regs(vcpu);
  954. if (copy_to_user(user_list, &reg_list, sizeof(reg_list)))
  955. break;
  956. r = -E2BIG;
  957. if (n < reg_list.n)
  958. break;
  959. r = kvm_arm_copy_reg_indices(vcpu, user_list->reg);
  960. break;
  961. }
  962. case KVM_SET_DEVICE_ATTR: {
  963. r = -EFAULT;
  964. if (copy_from_user(&attr, argp, sizeof(attr)))
  965. break;
  966. r = kvm_arm_vcpu_set_attr(vcpu, &attr);
  967. break;
  968. }
  969. case KVM_GET_DEVICE_ATTR: {
  970. r = -EFAULT;
  971. if (copy_from_user(&attr, argp, sizeof(attr)))
  972. break;
  973. r = kvm_arm_vcpu_get_attr(vcpu, &attr);
  974. break;
  975. }
  976. case KVM_HAS_DEVICE_ATTR: {
  977. r = -EFAULT;
  978. if (copy_from_user(&attr, argp, sizeof(attr)))
  979. break;
  980. r = kvm_arm_vcpu_has_attr(vcpu, &attr);
  981. break;
  982. }
  983. case KVM_GET_VCPU_EVENTS: {
  984. struct kvm_vcpu_events events;
  985. if (kvm_arm_vcpu_get_events(vcpu, &events))
  986. return -EINVAL;
  987. if (copy_to_user(argp, &events, sizeof(events)))
  988. return -EFAULT;
  989. return 0;
  990. }
  991. case KVM_SET_VCPU_EVENTS: {
  992. struct kvm_vcpu_events events;
  993. if (copy_from_user(&events, argp, sizeof(events)))
  994. return -EFAULT;
  995. return kvm_arm_vcpu_set_events(vcpu, &events);
  996. }
  997. default:
  998. r = -EINVAL;
  999. }
  1000. return r;
  1001. }
  1002. /**
  1003. * kvm_vm_ioctl_get_dirty_log - get and clear the log of dirty pages in a slot
  1004. * @kvm: kvm instance
  1005. * @log: slot id and address to which we copy the log
  1006. *
  1007. * Steps 1-4 below provide general overview of dirty page logging. See
  1008. * kvm_get_dirty_log_protect() function description for additional details.
  1009. *
  1010. * We call kvm_get_dirty_log_protect() to handle steps 1-3, upon return we
  1011. * always flush the TLB (step 4) even if previous step failed and the dirty
  1012. * bitmap may be corrupt. Regardless of previous outcome the KVM logging API
  1013. * does not preclude user space subsequent dirty log read. Flushing TLB ensures
  1014. * writes will be marked dirty for next log read.
  1015. *
  1016. * 1. Take a snapshot of the bit and clear it if needed.
  1017. * 2. Write protect the corresponding page.
  1018. * 3. Copy the snapshot to the userspace.
  1019. * 4. Flush TLB's if needed.
  1020. */
  1021. int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
  1022. {
  1023. bool is_dirty = false;
  1024. int r;
  1025. mutex_lock(&kvm->slots_lock);
  1026. r = kvm_get_dirty_log_protect(kvm, log, &is_dirty);
  1027. if (is_dirty)
  1028. kvm_flush_remote_tlbs(kvm);
  1029. mutex_unlock(&kvm->slots_lock);
  1030. return r;
  1031. }
  1032. static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm,
  1033. struct kvm_arm_device_addr *dev_addr)
  1034. {
  1035. unsigned long dev_id, type;
  1036. dev_id = (dev_addr->id & KVM_ARM_DEVICE_ID_MASK) >>
  1037. KVM_ARM_DEVICE_ID_SHIFT;
  1038. type = (dev_addr->id & KVM_ARM_DEVICE_TYPE_MASK) >>
  1039. KVM_ARM_DEVICE_TYPE_SHIFT;
  1040. switch (dev_id) {
  1041. case KVM_ARM_DEVICE_VGIC_V2:
  1042. if (!vgic_present)
  1043. return -ENXIO;
  1044. return kvm_vgic_addr(kvm, type, &dev_addr->addr, true);
  1045. default:
  1046. return -ENODEV;
  1047. }
  1048. }
  1049. long kvm_arch_vm_ioctl(struct file *filp,
  1050. unsigned int ioctl, unsigned long arg)
  1051. {
  1052. struct kvm *kvm = filp->private_data;
  1053. void __user *argp = (void __user *)arg;
  1054. switch (ioctl) {
  1055. case KVM_CREATE_IRQCHIP: {
  1056. int ret;
  1057. if (!vgic_present)
  1058. return -ENXIO;
  1059. mutex_lock(&kvm->lock);
  1060. ret = kvm_vgic_create(kvm, KVM_DEV_TYPE_ARM_VGIC_V2);
  1061. mutex_unlock(&kvm->lock);
  1062. return ret;
  1063. }
  1064. case KVM_ARM_SET_DEVICE_ADDR: {
  1065. struct kvm_arm_device_addr dev_addr;
  1066. if (copy_from_user(&dev_addr, argp, sizeof(dev_addr)))
  1067. return -EFAULT;
  1068. return kvm_vm_ioctl_set_device_addr(kvm, &dev_addr);
  1069. }
  1070. case KVM_ARM_PREFERRED_TARGET: {
  1071. int err;
  1072. struct kvm_vcpu_init init;
  1073. err = kvm_vcpu_preferred_target(&init);
  1074. if (err)
  1075. return err;
  1076. if (copy_to_user(argp, &init, sizeof(init)))
  1077. return -EFAULT;
  1078. return 0;
  1079. }
  1080. default:
  1081. return -EINVAL;
  1082. }
  1083. }
  1084. static void cpu_init_hyp_mode(void *dummy)
  1085. {
  1086. phys_addr_t pgd_ptr;
  1087. unsigned long hyp_stack_ptr;
  1088. unsigned long stack_page;
  1089. unsigned long vector_ptr;
  1090. /* Switch from the HYP stub to our own HYP init vector */
  1091. __hyp_set_vectors(kvm_get_idmap_vector());
  1092. pgd_ptr = kvm_mmu_get_httbr();
  1093. stack_page = __this_cpu_read(kvm_arm_hyp_stack_page);
  1094. hyp_stack_ptr = stack_page + PAGE_SIZE;
  1095. vector_ptr = (unsigned long)kvm_get_hyp_vector();
  1096. __cpu_init_hyp_mode(pgd_ptr, hyp_stack_ptr, vector_ptr);
  1097. __cpu_init_stage2();
  1098. }
  1099. static void cpu_hyp_reset(void)
  1100. {
  1101. if (!is_kernel_in_hyp_mode())
  1102. __hyp_reset_vectors();
  1103. }
  1104. static void cpu_hyp_reinit(void)
  1105. {
  1106. cpu_hyp_reset();
  1107. if (is_kernel_in_hyp_mode()) {
  1108. /*
  1109. * __cpu_init_stage2() is safe to call even if the PM
  1110. * event was cancelled before the CPU was reset.
  1111. */
  1112. __cpu_init_stage2();
  1113. kvm_timer_init_vhe();
  1114. } else {
  1115. cpu_init_hyp_mode(NULL);
  1116. }
  1117. kvm_arm_init_debug();
  1118. if (vgic_present)
  1119. kvm_vgic_init_cpu_hardware();
  1120. }
  1121. static void _kvm_arch_hardware_enable(void *discard)
  1122. {
  1123. if (!__this_cpu_read(kvm_arm_hardware_enabled)) {
  1124. cpu_hyp_reinit();
  1125. __this_cpu_write(kvm_arm_hardware_enabled, 1);
  1126. }
  1127. }
  1128. int kvm_arch_hardware_enable(void)
  1129. {
  1130. _kvm_arch_hardware_enable(NULL);
  1131. return 0;
  1132. }
  1133. static void _kvm_arch_hardware_disable(void *discard)
  1134. {
  1135. if (__this_cpu_read(kvm_arm_hardware_enabled)) {
  1136. cpu_hyp_reset();
  1137. __this_cpu_write(kvm_arm_hardware_enabled, 0);
  1138. }
  1139. }
  1140. void kvm_arch_hardware_disable(void)
  1141. {
  1142. _kvm_arch_hardware_disable(NULL);
  1143. }
  1144. #ifdef CONFIG_CPU_PM
  1145. static int hyp_init_cpu_pm_notifier(struct notifier_block *self,
  1146. unsigned long cmd,
  1147. void *v)
  1148. {
  1149. /*
  1150. * kvm_arm_hardware_enabled is left with its old value over
  1151. * PM_ENTER->PM_EXIT. It is used to indicate PM_EXIT should
  1152. * re-enable hyp.
  1153. */
  1154. switch (cmd) {
  1155. case CPU_PM_ENTER:
  1156. if (__this_cpu_read(kvm_arm_hardware_enabled))
  1157. /*
  1158. * don't update kvm_arm_hardware_enabled here
  1159. * so that the hardware will be re-enabled
  1160. * when we resume. See below.
  1161. */
  1162. cpu_hyp_reset();
  1163. return NOTIFY_OK;
  1164. case CPU_PM_ENTER_FAILED:
  1165. case CPU_PM_EXIT:
  1166. if (__this_cpu_read(kvm_arm_hardware_enabled))
  1167. /* The hardware was enabled before suspend. */
  1168. cpu_hyp_reinit();
  1169. return NOTIFY_OK;
  1170. default:
  1171. return NOTIFY_DONE;
  1172. }
  1173. }
  1174. static struct notifier_block hyp_init_cpu_pm_nb = {
  1175. .notifier_call = hyp_init_cpu_pm_notifier,
  1176. };
  1177. static void __init hyp_cpu_pm_init(void)
  1178. {
  1179. cpu_pm_register_notifier(&hyp_init_cpu_pm_nb);
  1180. }
  1181. static void __init hyp_cpu_pm_exit(void)
  1182. {
  1183. cpu_pm_unregister_notifier(&hyp_init_cpu_pm_nb);
  1184. }
  1185. #else
  1186. static inline void hyp_cpu_pm_init(void)
  1187. {
  1188. }
  1189. static inline void hyp_cpu_pm_exit(void)
  1190. {
  1191. }
  1192. #endif
  1193. static int init_common_resources(void)
  1194. {
  1195. /* set size of VMID supported by CPU */
  1196. kvm_vmid_bits = kvm_get_vmid_bits();
  1197. kvm_info("%d-bit VMID\n", kvm_vmid_bits);
  1198. return 0;
  1199. }
  1200. static int init_subsystems(void)
  1201. {
  1202. int err = 0;
  1203. /*
  1204. * Enable hardware so that subsystem initialisation can access EL2.
  1205. */
  1206. on_each_cpu(_kvm_arch_hardware_enable, NULL, 1);
  1207. /*
  1208. * Register CPU lower-power notifier
  1209. */
  1210. hyp_cpu_pm_init();
  1211. /*
  1212. * Init HYP view of VGIC
  1213. */
  1214. err = kvm_vgic_hyp_init();
  1215. switch (err) {
  1216. case 0:
  1217. vgic_present = true;
  1218. break;
  1219. case -ENODEV:
  1220. case -ENXIO:
  1221. vgic_present = false;
  1222. err = 0;
  1223. break;
  1224. default:
  1225. goto out;
  1226. }
  1227. /*
  1228. * Init HYP architected timer support
  1229. */
  1230. err = kvm_timer_hyp_init(vgic_present);
  1231. if (err)
  1232. goto out;
  1233. kvm_perf_init();
  1234. kvm_coproc_table_init();
  1235. out:
  1236. on_each_cpu(_kvm_arch_hardware_disable, NULL, 1);
  1237. return err;
  1238. }
  1239. static void teardown_hyp_mode(void)
  1240. {
  1241. int cpu;
  1242. free_hyp_pgds();
  1243. for_each_possible_cpu(cpu)
  1244. free_page(per_cpu(kvm_arm_hyp_stack_page, cpu));
  1245. hyp_cpu_pm_exit();
  1246. }
  1247. /**
  1248. * Inits Hyp-mode on all online CPUs
  1249. */
  1250. static int init_hyp_mode(void)
  1251. {
  1252. int cpu;
  1253. int err = 0;
  1254. /*
  1255. * Allocate Hyp PGD and setup Hyp identity mapping
  1256. */
  1257. err = kvm_mmu_init();
  1258. if (err)
  1259. goto out_err;
  1260. /*
  1261. * Allocate stack pages for Hypervisor-mode
  1262. */
  1263. for_each_possible_cpu(cpu) {
  1264. unsigned long stack_page;
  1265. stack_page = __get_free_page(GFP_KERNEL);
  1266. if (!stack_page) {
  1267. err = -ENOMEM;
  1268. goto out_err;
  1269. }
  1270. per_cpu(kvm_arm_hyp_stack_page, cpu) = stack_page;
  1271. }
  1272. /*
  1273. * Map the Hyp-code called directly from the host
  1274. */
  1275. err = create_hyp_mappings(kvm_ksym_ref(__hyp_text_start),
  1276. kvm_ksym_ref(__hyp_text_end), PAGE_HYP_EXEC);
  1277. if (err) {
  1278. kvm_err("Cannot map world-switch code\n");
  1279. goto out_err;
  1280. }
  1281. err = create_hyp_mappings(kvm_ksym_ref(__start_rodata),
  1282. kvm_ksym_ref(__end_rodata), PAGE_HYP_RO);
  1283. if (err) {
  1284. kvm_err("Cannot map rodata section\n");
  1285. goto out_err;
  1286. }
  1287. err = create_hyp_mappings(kvm_ksym_ref(__bss_start),
  1288. kvm_ksym_ref(__bss_stop), PAGE_HYP_RO);
  1289. if (err) {
  1290. kvm_err("Cannot map bss section\n");
  1291. goto out_err;
  1292. }
  1293. err = kvm_map_vectors();
  1294. if (err) {
  1295. kvm_err("Cannot map vectors\n");
  1296. goto out_err;
  1297. }
  1298. /*
  1299. * Map the Hyp stack pages
  1300. */
  1301. for_each_possible_cpu(cpu) {
  1302. char *stack_page = (char *)per_cpu(kvm_arm_hyp_stack_page, cpu);
  1303. err = create_hyp_mappings(stack_page, stack_page + PAGE_SIZE,
  1304. PAGE_HYP);
  1305. if (err) {
  1306. kvm_err("Cannot map hyp stack\n");
  1307. goto out_err;
  1308. }
  1309. }
  1310. for_each_possible_cpu(cpu) {
  1311. kvm_cpu_context_t *cpu_ctxt;
  1312. cpu_ctxt = per_cpu_ptr(&kvm_host_cpu_state, cpu);
  1313. err = create_hyp_mappings(cpu_ctxt, cpu_ctxt + 1, PAGE_HYP);
  1314. if (err) {
  1315. kvm_err("Cannot map host CPU state: %d\n", err);
  1316. goto out_err;
  1317. }
  1318. }
  1319. err = hyp_map_aux_data();
  1320. if (err)
  1321. kvm_err("Cannot map host auxilary data: %d\n", err);
  1322. return 0;
  1323. out_err:
  1324. teardown_hyp_mode();
  1325. kvm_err("error initializing Hyp mode: %d\n", err);
  1326. return err;
  1327. }
  1328. static void check_kvm_target_cpu(void *ret)
  1329. {
  1330. *(int *)ret = kvm_target_cpu();
  1331. }
  1332. struct kvm_vcpu *kvm_mpidr_to_vcpu(struct kvm *kvm, unsigned long mpidr)
  1333. {
  1334. struct kvm_vcpu *vcpu;
  1335. int i;
  1336. mpidr &= MPIDR_HWID_BITMASK;
  1337. kvm_for_each_vcpu(i, vcpu, kvm) {
  1338. if (mpidr == kvm_vcpu_get_mpidr_aff(vcpu))
  1339. return vcpu;
  1340. }
  1341. return NULL;
  1342. }
  1343. bool kvm_arch_has_irq_bypass(void)
  1344. {
  1345. return true;
  1346. }
  1347. int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons,
  1348. struct irq_bypass_producer *prod)
  1349. {
  1350. struct kvm_kernel_irqfd *irqfd =
  1351. container_of(cons, struct kvm_kernel_irqfd, consumer);
  1352. return kvm_vgic_v4_set_forwarding(irqfd->kvm, prod->irq,
  1353. &irqfd->irq_entry);
  1354. }
  1355. void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons,
  1356. struct irq_bypass_producer *prod)
  1357. {
  1358. struct kvm_kernel_irqfd *irqfd =
  1359. container_of(cons, struct kvm_kernel_irqfd, consumer);
  1360. kvm_vgic_v4_unset_forwarding(irqfd->kvm, prod->irq,
  1361. &irqfd->irq_entry);
  1362. }
  1363. void kvm_arch_irq_bypass_stop(struct irq_bypass_consumer *cons)
  1364. {
  1365. struct kvm_kernel_irqfd *irqfd =
  1366. container_of(cons, struct kvm_kernel_irqfd, consumer);
  1367. kvm_arm_halt_guest(irqfd->kvm);
  1368. }
  1369. void kvm_arch_irq_bypass_start(struct irq_bypass_consumer *cons)
  1370. {
  1371. struct kvm_kernel_irqfd *irqfd =
  1372. container_of(cons, struct kvm_kernel_irqfd, consumer);
  1373. kvm_arm_resume_guest(irqfd->kvm);
  1374. }
  1375. /**
  1376. * Initialize Hyp-mode and memory mappings on all CPUs.
  1377. */
  1378. int kvm_arch_init(void *opaque)
  1379. {
  1380. int err;
  1381. int ret, cpu;
  1382. bool in_hyp_mode;
  1383. if (!is_hyp_mode_available()) {
  1384. kvm_info("HYP mode not available\n");
  1385. return -ENODEV;
  1386. }
  1387. if (!kvm_arch_check_sve_has_vhe()) {
  1388. kvm_pr_unimpl("SVE system without VHE unsupported. Broken cpu?");
  1389. return -ENODEV;
  1390. }
  1391. for_each_online_cpu(cpu) {
  1392. smp_call_function_single(cpu, check_kvm_target_cpu, &ret, 1);
  1393. if (ret < 0) {
  1394. kvm_err("Error, CPU %d not supported!\n", cpu);
  1395. return -ENODEV;
  1396. }
  1397. }
  1398. err = init_common_resources();
  1399. if (err)
  1400. return err;
  1401. in_hyp_mode = is_kernel_in_hyp_mode();
  1402. if (!in_hyp_mode) {
  1403. err = init_hyp_mode();
  1404. if (err)
  1405. goto out_err;
  1406. }
  1407. err = init_subsystems();
  1408. if (err)
  1409. goto out_hyp;
  1410. if (in_hyp_mode)
  1411. kvm_info("VHE mode initialized successfully\n");
  1412. else
  1413. kvm_info("Hyp mode initialized successfully\n");
  1414. return 0;
  1415. out_hyp:
  1416. if (!in_hyp_mode)
  1417. teardown_hyp_mode();
  1418. out_err:
  1419. return err;
  1420. }
  1421. /* NOP: Compiling as a module not supported */
  1422. void kvm_arch_exit(void)
  1423. {
  1424. kvm_perf_teardown();
  1425. }
  1426. static int arm_init(void)
  1427. {
  1428. int rc = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
  1429. return rc;
  1430. }
  1431. module_init(arm_init);