arm.c 32 KB

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