arm.c 32 KB

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