arm.c 31 KB

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