arm.c 36 KB

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