arm.c 34 KB

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