arm.c 37 KB

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