arm.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169
  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/errno.h>
  19. #include <linux/err.h>
  20. #include <linux/kvm_host.h>
  21. #include <linux/module.h>
  22. #include <linux/vmalloc.h>
  23. #include <linux/fs.h>
  24. #include <linux/mman.h>
  25. #include <linux/sched.h>
  26. #include <linux/kvm.h>
  27. #include <trace/events/kvm.h>
  28. #define CREATE_TRACE_POINTS
  29. #include "trace.h"
  30. #include <asm/unified.h>
  31. #include <asm/uaccess.h>
  32. #include <asm/ptrace.h>
  33. #include <asm/mman.h>
  34. #include <asm/cputype.h>
  35. #include <asm/tlbflush.h>
  36. #include <asm/cacheflush.h>
  37. #include <asm/virt.h>
  38. #include <asm/kvm_arm.h>
  39. #include <asm/kvm_asm.h>
  40. #include <asm/kvm_mmu.h>
  41. #include <asm/kvm_emulate.h>
  42. #include <asm/kvm_coproc.h>
  43. #include <asm/kvm_psci.h>
  44. #include <asm/opcodes.h>
  45. #ifdef REQUIRES_VIRT
  46. __asm__(".arch_extension virt");
  47. #endif
  48. static DEFINE_PER_CPU(unsigned long, kvm_arm_hyp_stack_page);
  49. static struct vfp_hard_struct __percpu *kvm_host_vfp_state;
  50. static unsigned long hyp_default_vectors;
  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 u8 kvm_next_vmid;
  56. static DEFINE_SPINLOCK(kvm_vmid_lock);
  57. static bool vgic_present;
  58. static void kvm_arm_set_running_vcpu(struct kvm_vcpu *vcpu)
  59. {
  60. BUG_ON(preemptible());
  61. __get_cpu_var(kvm_arm_running_vcpu) = vcpu;
  62. }
  63. /**
  64. * kvm_arm_get_running_vcpu - get the vcpu running on the current CPU.
  65. * Must be called from non-preemptible context
  66. */
  67. struct kvm_vcpu *kvm_arm_get_running_vcpu(void)
  68. {
  69. BUG_ON(preemptible());
  70. return __get_cpu_var(kvm_arm_running_vcpu);
  71. }
  72. /**
  73. * kvm_arm_get_running_vcpus - get the per-CPU array of currently running vcpus.
  74. */
  75. struct kvm_vcpu __percpu **kvm_get_running_vcpus(void)
  76. {
  77. return &kvm_arm_running_vcpu;
  78. }
  79. int kvm_arch_hardware_enable(void *garbage)
  80. {
  81. return 0;
  82. }
  83. int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
  84. {
  85. return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
  86. }
  87. void kvm_arch_hardware_disable(void *garbage)
  88. {
  89. }
  90. int kvm_arch_hardware_setup(void)
  91. {
  92. return 0;
  93. }
  94. void kvm_arch_hardware_unsetup(void)
  95. {
  96. }
  97. void kvm_arch_check_processor_compat(void *rtn)
  98. {
  99. *(int *)rtn = 0;
  100. }
  101. void kvm_arch_sync_events(struct kvm *kvm)
  102. {
  103. }
  104. /**
  105. * kvm_arch_init_vm - initializes a VM data structure
  106. * @kvm: pointer to the KVM struct
  107. */
  108. int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
  109. {
  110. int ret = 0;
  111. if (type)
  112. return -EINVAL;
  113. ret = kvm_alloc_stage2_pgd(kvm);
  114. if (ret)
  115. goto out_fail_alloc;
  116. ret = create_hyp_mappings(kvm, kvm + 1);
  117. if (ret)
  118. goto out_free_stage2_pgd;
  119. /* Mark the initial VMID generation invalid */
  120. kvm->arch.vmid_gen = 0;
  121. return ret;
  122. out_free_stage2_pgd:
  123. kvm_free_stage2_pgd(kvm);
  124. out_fail_alloc:
  125. return ret;
  126. }
  127. int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
  128. {
  129. return VM_FAULT_SIGBUS;
  130. }
  131. void kvm_arch_free_memslot(struct kvm_memory_slot *free,
  132. struct kvm_memory_slot *dont)
  133. {
  134. }
  135. int kvm_arch_create_memslot(struct kvm_memory_slot *slot, unsigned long npages)
  136. {
  137. return 0;
  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. kvm_free_stage2_pgd(kvm);
  147. for (i = 0; i < KVM_MAX_VCPUS; ++i) {
  148. if (kvm->vcpus[i]) {
  149. kvm_arch_vcpu_free(kvm->vcpus[i]);
  150. kvm->vcpus[i] = NULL;
  151. }
  152. }
  153. }
  154. int kvm_dev_ioctl_check_extension(long ext)
  155. {
  156. int r;
  157. switch (ext) {
  158. case KVM_CAP_IRQCHIP:
  159. r = vgic_present;
  160. break;
  161. case KVM_CAP_USER_MEMORY:
  162. case KVM_CAP_SYNC_MMU:
  163. case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
  164. case KVM_CAP_ONE_REG:
  165. case KVM_CAP_ARM_PSCI:
  166. r = 1;
  167. break;
  168. case KVM_CAP_COALESCED_MMIO:
  169. r = KVM_COALESCED_MMIO_PAGE_OFFSET;
  170. break;
  171. case KVM_CAP_ARM_SET_DEVICE_ADDR:
  172. r = 1;
  173. case KVM_CAP_NR_VCPUS:
  174. r = num_online_cpus();
  175. break;
  176. case KVM_CAP_MAX_VCPUS:
  177. r = KVM_MAX_VCPUS;
  178. break;
  179. default:
  180. r = 0;
  181. break;
  182. }
  183. return r;
  184. }
  185. long kvm_arch_dev_ioctl(struct file *filp,
  186. unsigned int ioctl, unsigned long arg)
  187. {
  188. return -EINVAL;
  189. }
  190. int kvm_arch_set_memory_region(struct kvm *kvm,
  191. struct kvm_userspace_memory_region *mem,
  192. struct kvm_memory_slot old,
  193. int user_alloc)
  194. {
  195. return 0;
  196. }
  197. int kvm_arch_prepare_memory_region(struct kvm *kvm,
  198. struct kvm_memory_slot *memslot,
  199. struct kvm_memory_slot old,
  200. struct kvm_userspace_memory_region *mem,
  201. bool user_alloc)
  202. {
  203. return 0;
  204. }
  205. void kvm_arch_commit_memory_region(struct kvm *kvm,
  206. struct kvm_userspace_memory_region *mem,
  207. struct kvm_memory_slot old,
  208. bool user_alloc)
  209. {
  210. }
  211. void kvm_arch_flush_shadow_all(struct kvm *kvm)
  212. {
  213. }
  214. void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
  215. struct kvm_memory_slot *slot)
  216. {
  217. }
  218. struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
  219. {
  220. int err;
  221. struct kvm_vcpu *vcpu;
  222. vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
  223. if (!vcpu) {
  224. err = -ENOMEM;
  225. goto out;
  226. }
  227. err = kvm_vcpu_init(vcpu, kvm, id);
  228. if (err)
  229. goto free_vcpu;
  230. err = create_hyp_mappings(vcpu, vcpu + 1);
  231. if (err)
  232. goto vcpu_uninit;
  233. return vcpu;
  234. vcpu_uninit:
  235. kvm_vcpu_uninit(vcpu);
  236. free_vcpu:
  237. kmem_cache_free(kvm_vcpu_cache, vcpu);
  238. out:
  239. return ERR_PTR(err);
  240. }
  241. int kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
  242. {
  243. return 0;
  244. }
  245. void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
  246. {
  247. kvm_mmu_free_memory_caches(vcpu);
  248. kvm_timer_vcpu_terminate(vcpu);
  249. kmem_cache_free(kvm_vcpu_cache, vcpu);
  250. }
  251. void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
  252. {
  253. kvm_arch_vcpu_free(vcpu);
  254. }
  255. int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
  256. {
  257. return 0;
  258. }
  259. int __attribute_const__ kvm_target_cpu(void)
  260. {
  261. unsigned long implementor = read_cpuid_implementor();
  262. unsigned long part_number = read_cpuid_part_number();
  263. if (implementor != ARM_CPU_IMP_ARM)
  264. return -EINVAL;
  265. switch (part_number) {
  266. case ARM_CPU_PART_CORTEX_A15:
  267. return KVM_ARM_TARGET_CORTEX_A15;
  268. default:
  269. return -EINVAL;
  270. }
  271. }
  272. int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
  273. {
  274. int ret;
  275. /* Force users to call KVM_ARM_VCPU_INIT */
  276. vcpu->arch.target = -1;
  277. /* Set up VGIC */
  278. ret = kvm_vgic_vcpu_init(vcpu);
  279. if (ret)
  280. return ret;
  281. /* Set up the timer */
  282. kvm_timer_vcpu_init(vcpu);
  283. return 0;
  284. }
  285. void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
  286. {
  287. }
  288. void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  289. {
  290. vcpu->cpu = cpu;
  291. vcpu->arch.vfp_host = this_cpu_ptr(kvm_host_vfp_state);
  292. /*
  293. * Check whether this vcpu requires the cache to be flushed on
  294. * this physical CPU. This is a consequence of doing dcache
  295. * operations by set/way on this vcpu. We do it here to be in
  296. * a non-preemptible section.
  297. */
  298. if (cpumask_test_and_clear_cpu(cpu, &vcpu->arch.require_dcache_flush))
  299. flush_cache_all(); /* We'd really want v7_flush_dcache_all() */
  300. kvm_arm_set_running_vcpu(vcpu);
  301. }
  302. void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
  303. {
  304. kvm_arm_set_running_vcpu(NULL);
  305. }
  306. int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
  307. struct kvm_guest_debug *dbg)
  308. {
  309. return -EINVAL;
  310. }
  311. int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
  312. struct kvm_mp_state *mp_state)
  313. {
  314. return -EINVAL;
  315. }
  316. int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
  317. struct kvm_mp_state *mp_state)
  318. {
  319. return -EINVAL;
  320. }
  321. /**
  322. * kvm_arch_vcpu_runnable - determine if the vcpu can be scheduled
  323. * @v: The VCPU pointer
  324. *
  325. * If the guest CPU is not waiting for interrupts or an interrupt line is
  326. * asserted, the CPU is by definition runnable.
  327. */
  328. int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
  329. {
  330. return !!v->arch.irq_lines || kvm_vgic_vcpu_pending_irq(v);
  331. }
  332. /* Just ensure a guest exit from a particular CPU */
  333. static void exit_vm_noop(void *info)
  334. {
  335. }
  336. void force_vm_exit(const cpumask_t *mask)
  337. {
  338. smp_call_function_many(mask, exit_vm_noop, NULL, true);
  339. }
  340. /**
  341. * need_new_vmid_gen - check that the VMID is still valid
  342. * @kvm: The VM's VMID to checkt
  343. *
  344. * return true if there is a new generation of VMIDs being used
  345. *
  346. * The hardware supports only 256 values with the value zero reserved for the
  347. * host, so we check if an assigned value belongs to a previous generation,
  348. * which which requires us to assign a new value. If we're the first to use a
  349. * VMID for the new generation, we must flush necessary caches and TLBs on all
  350. * CPUs.
  351. */
  352. static bool need_new_vmid_gen(struct kvm *kvm)
  353. {
  354. return unlikely(kvm->arch.vmid_gen != atomic64_read(&kvm_vmid_gen));
  355. }
  356. /**
  357. * update_vttbr - Update the VTTBR with a valid VMID before the guest runs
  358. * @kvm The guest that we are about to run
  359. *
  360. * Called from kvm_arch_vcpu_ioctl_run before entering the guest to ensure the
  361. * VM has a valid VMID, otherwise assigns a new one and flushes corresponding
  362. * caches and TLBs.
  363. */
  364. static void update_vttbr(struct kvm *kvm)
  365. {
  366. phys_addr_t pgd_phys;
  367. u64 vmid;
  368. if (!need_new_vmid_gen(kvm))
  369. return;
  370. spin_lock(&kvm_vmid_lock);
  371. /*
  372. * We need to re-check the vmid_gen here to ensure that if another vcpu
  373. * already allocated a valid vmid for this vm, then this vcpu should
  374. * use the same vmid.
  375. */
  376. if (!need_new_vmid_gen(kvm)) {
  377. spin_unlock(&kvm_vmid_lock);
  378. return;
  379. }
  380. /* First user of a new VMID generation? */
  381. if (unlikely(kvm_next_vmid == 0)) {
  382. atomic64_inc(&kvm_vmid_gen);
  383. kvm_next_vmid = 1;
  384. /*
  385. * On SMP we know no other CPUs can use this CPU's or each
  386. * other's VMID after force_vm_exit returns since the
  387. * kvm_vmid_lock blocks them from reentry to the guest.
  388. */
  389. force_vm_exit(cpu_all_mask);
  390. /*
  391. * Now broadcast TLB + ICACHE invalidation over the inner
  392. * shareable domain to make sure all data structures are
  393. * clean.
  394. */
  395. kvm_call_hyp(__kvm_flush_vm_context);
  396. }
  397. kvm->arch.vmid_gen = atomic64_read(&kvm_vmid_gen);
  398. kvm->arch.vmid = kvm_next_vmid;
  399. kvm_next_vmid++;
  400. /* update vttbr to be used with the new vmid */
  401. pgd_phys = virt_to_phys(kvm->arch.pgd);
  402. vmid = ((u64)(kvm->arch.vmid) << VTTBR_VMID_SHIFT) & VTTBR_VMID_MASK;
  403. kvm->arch.vttbr = pgd_phys & VTTBR_BADDR_MASK;
  404. kvm->arch.vttbr |= vmid;
  405. spin_unlock(&kvm_vmid_lock);
  406. }
  407. static int handle_svc_hyp(struct kvm_vcpu *vcpu, struct kvm_run *run)
  408. {
  409. /* SVC called from Hyp mode should never get here */
  410. kvm_debug("SVC called from Hyp mode shouldn't go here\n");
  411. BUG();
  412. return -EINVAL; /* Squash warning */
  413. }
  414. static int handle_hvc(struct kvm_vcpu *vcpu, struct kvm_run *run)
  415. {
  416. trace_kvm_hvc(*vcpu_pc(vcpu), *vcpu_reg(vcpu, 0),
  417. vcpu->arch.hsr & HSR_HVC_IMM_MASK);
  418. if (kvm_psci_call(vcpu))
  419. return 1;
  420. kvm_inject_undefined(vcpu);
  421. return 1;
  422. }
  423. static int handle_smc(struct kvm_vcpu *vcpu, struct kvm_run *run)
  424. {
  425. if (kvm_psci_call(vcpu))
  426. return 1;
  427. kvm_inject_undefined(vcpu);
  428. return 1;
  429. }
  430. static int handle_pabt_hyp(struct kvm_vcpu *vcpu, struct kvm_run *run)
  431. {
  432. /* The hypervisor should never cause aborts */
  433. kvm_err("Prefetch Abort taken from Hyp mode at %#08x (HSR: %#08x)\n",
  434. vcpu->arch.hxfar, vcpu->arch.hsr);
  435. return -EFAULT;
  436. }
  437. static int handle_dabt_hyp(struct kvm_vcpu *vcpu, struct kvm_run *run)
  438. {
  439. /* This is either an error in the ws. code or an external abort */
  440. kvm_err("Data Abort taken from Hyp mode at %#08x (HSR: %#08x)\n",
  441. vcpu->arch.hxfar, vcpu->arch.hsr);
  442. return -EFAULT;
  443. }
  444. typedef int (*exit_handle_fn)(struct kvm_vcpu *, struct kvm_run *);
  445. static exit_handle_fn arm_exit_handlers[] = {
  446. [HSR_EC_WFI] = kvm_handle_wfi,
  447. [HSR_EC_CP15_32] = kvm_handle_cp15_32,
  448. [HSR_EC_CP15_64] = kvm_handle_cp15_64,
  449. [HSR_EC_CP14_MR] = kvm_handle_cp14_access,
  450. [HSR_EC_CP14_LS] = kvm_handle_cp14_load_store,
  451. [HSR_EC_CP14_64] = kvm_handle_cp14_access,
  452. [HSR_EC_CP_0_13] = kvm_handle_cp_0_13_access,
  453. [HSR_EC_CP10_ID] = kvm_handle_cp10_id,
  454. [HSR_EC_SVC_HYP] = handle_svc_hyp,
  455. [HSR_EC_HVC] = handle_hvc,
  456. [HSR_EC_SMC] = handle_smc,
  457. [HSR_EC_IABT] = kvm_handle_guest_abort,
  458. [HSR_EC_IABT_HYP] = handle_pabt_hyp,
  459. [HSR_EC_DABT] = kvm_handle_guest_abort,
  460. [HSR_EC_DABT_HYP] = handle_dabt_hyp,
  461. };
  462. /*
  463. * A conditional instruction is allowed to trap, even though it
  464. * wouldn't be executed. So let's re-implement the hardware, in
  465. * software!
  466. */
  467. static bool kvm_condition_valid(struct kvm_vcpu *vcpu)
  468. {
  469. unsigned long cpsr, cond, insn;
  470. /*
  471. * Exception Code 0 can only happen if we set HCR.TGE to 1, to
  472. * catch undefined instructions, and then we won't get past
  473. * the arm_exit_handlers test anyway.
  474. */
  475. BUG_ON(((vcpu->arch.hsr & HSR_EC) >> HSR_EC_SHIFT) == 0);
  476. /* Top two bits non-zero? Unconditional. */
  477. if (vcpu->arch.hsr >> 30)
  478. return true;
  479. cpsr = *vcpu_cpsr(vcpu);
  480. /* Is condition field valid? */
  481. if ((vcpu->arch.hsr & HSR_CV) >> HSR_CV_SHIFT)
  482. cond = (vcpu->arch.hsr & HSR_COND) >> HSR_COND_SHIFT;
  483. else {
  484. /* This can happen in Thumb mode: examine IT state. */
  485. unsigned long it;
  486. it = ((cpsr >> 8) & 0xFC) | ((cpsr >> 25) & 0x3);
  487. /* it == 0 => unconditional. */
  488. if (it == 0)
  489. return true;
  490. /* The cond for this insn works out as the top 4 bits. */
  491. cond = (it >> 4);
  492. }
  493. /* Shift makes it look like an ARM-mode instruction */
  494. insn = cond << 28;
  495. return arm_check_condition(insn, cpsr) != ARM_OPCODE_CONDTEST_FAIL;
  496. }
  497. /*
  498. * Return > 0 to return to guest, < 0 on error, 0 (and set exit_reason) on
  499. * proper exit to QEMU.
  500. */
  501. static int handle_exit(struct kvm_vcpu *vcpu, struct kvm_run *run,
  502. int exception_index)
  503. {
  504. unsigned long hsr_ec;
  505. switch (exception_index) {
  506. case ARM_EXCEPTION_IRQ:
  507. return 1;
  508. case ARM_EXCEPTION_UNDEFINED:
  509. kvm_err("Undefined exception in Hyp mode at: %#08x\n",
  510. vcpu->arch.hyp_pc);
  511. BUG();
  512. panic("KVM: Hypervisor undefined exception!\n");
  513. case ARM_EXCEPTION_DATA_ABORT:
  514. case ARM_EXCEPTION_PREF_ABORT:
  515. case ARM_EXCEPTION_HVC:
  516. hsr_ec = (vcpu->arch.hsr & HSR_EC) >> HSR_EC_SHIFT;
  517. if (hsr_ec >= ARRAY_SIZE(arm_exit_handlers)
  518. || !arm_exit_handlers[hsr_ec]) {
  519. kvm_err("Unkown exception class: %#08lx, "
  520. "hsr: %#08x\n", hsr_ec,
  521. (unsigned int)vcpu->arch.hsr);
  522. BUG();
  523. }
  524. /*
  525. * See ARM ARM B1.14.1: "Hyp traps on instructions
  526. * that fail their condition code check"
  527. */
  528. if (!kvm_condition_valid(vcpu)) {
  529. bool is_wide = vcpu->arch.hsr & HSR_IL;
  530. kvm_skip_instr(vcpu, is_wide);
  531. return 1;
  532. }
  533. return arm_exit_handlers[hsr_ec](vcpu, run);
  534. default:
  535. kvm_pr_unimpl("Unsupported exception type: %d",
  536. exception_index);
  537. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  538. return 0;
  539. }
  540. }
  541. static int kvm_vcpu_first_run_init(struct kvm_vcpu *vcpu)
  542. {
  543. if (likely(vcpu->arch.has_run_once))
  544. return 0;
  545. vcpu->arch.has_run_once = true;
  546. /*
  547. * Initialize the VGIC before running a vcpu the first time on
  548. * this VM.
  549. */
  550. if (irqchip_in_kernel(vcpu->kvm) &&
  551. unlikely(!vgic_initialized(vcpu->kvm))) {
  552. int ret = kvm_vgic_init(vcpu->kvm);
  553. if (ret)
  554. return ret;
  555. }
  556. /*
  557. * Handle the "start in power-off" case by calling into the
  558. * PSCI code.
  559. */
  560. if (test_and_clear_bit(KVM_ARM_VCPU_POWER_OFF, vcpu->arch.features)) {
  561. *vcpu_reg(vcpu, 0) = KVM_PSCI_FN_CPU_OFF;
  562. kvm_psci_call(vcpu);
  563. }
  564. return 0;
  565. }
  566. static void vcpu_pause(struct kvm_vcpu *vcpu)
  567. {
  568. wait_queue_head_t *wq = kvm_arch_vcpu_wq(vcpu);
  569. wait_event_interruptible(*wq, !vcpu->arch.pause);
  570. }
  571. /**
  572. * kvm_arch_vcpu_ioctl_run - the main VCPU run function to execute guest code
  573. * @vcpu: The VCPU pointer
  574. * @run: The kvm_run structure pointer used for userspace state exchange
  575. *
  576. * This function is called through the VCPU_RUN ioctl called from user space. It
  577. * will execute VM code in a loop until the time slice for the process is used
  578. * or some emulation is needed from user space in which case the function will
  579. * return with return value 0 and with the kvm_run structure filled in with the
  580. * required data for the requested emulation.
  581. */
  582. int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
  583. {
  584. int ret;
  585. sigset_t sigsaved;
  586. /* Make sure they initialize the vcpu with KVM_ARM_VCPU_INIT */
  587. if (unlikely(vcpu->arch.target < 0))
  588. return -ENOEXEC;
  589. ret = kvm_vcpu_first_run_init(vcpu);
  590. if (ret)
  591. return ret;
  592. if (run->exit_reason == KVM_EXIT_MMIO) {
  593. ret = kvm_handle_mmio_return(vcpu, vcpu->run);
  594. if (ret)
  595. return ret;
  596. }
  597. if (vcpu->sigset_active)
  598. sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
  599. ret = 1;
  600. run->exit_reason = KVM_EXIT_UNKNOWN;
  601. while (ret > 0) {
  602. /*
  603. * Check conditions before entering the guest
  604. */
  605. cond_resched();
  606. update_vttbr(vcpu->kvm);
  607. if (vcpu->arch.pause)
  608. vcpu_pause(vcpu);
  609. kvm_vgic_flush_hwstate(vcpu);
  610. kvm_timer_flush_hwstate(vcpu);
  611. local_irq_disable();
  612. /*
  613. * Re-check atomic conditions
  614. */
  615. if (signal_pending(current)) {
  616. ret = -EINTR;
  617. run->exit_reason = KVM_EXIT_INTR;
  618. }
  619. if (ret <= 0 || need_new_vmid_gen(vcpu->kvm)) {
  620. local_irq_enable();
  621. kvm_timer_sync_hwstate(vcpu);
  622. kvm_vgic_sync_hwstate(vcpu);
  623. continue;
  624. }
  625. /**************************************************************
  626. * Enter the guest
  627. */
  628. trace_kvm_entry(*vcpu_pc(vcpu));
  629. kvm_guest_enter();
  630. vcpu->mode = IN_GUEST_MODE;
  631. ret = kvm_call_hyp(__kvm_vcpu_run, vcpu);
  632. vcpu->mode = OUTSIDE_GUEST_MODE;
  633. vcpu->arch.last_pcpu = smp_processor_id();
  634. kvm_guest_exit();
  635. trace_kvm_exit(*vcpu_pc(vcpu));
  636. /*
  637. * We may have taken a host interrupt in HYP mode (ie
  638. * while executing the guest). This interrupt is still
  639. * pending, as we haven't serviced it yet!
  640. *
  641. * We're now back in SVC mode, with interrupts
  642. * disabled. Enabling the interrupts now will have
  643. * the effect of taking the interrupt again, in SVC
  644. * mode this time.
  645. */
  646. local_irq_enable();
  647. /*
  648. * Back from guest
  649. *************************************************************/
  650. kvm_timer_sync_hwstate(vcpu);
  651. kvm_vgic_sync_hwstate(vcpu);
  652. ret = handle_exit(vcpu, run, ret);
  653. }
  654. if (vcpu->sigset_active)
  655. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  656. return ret;
  657. }
  658. static int vcpu_interrupt_line(struct kvm_vcpu *vcpu, int number, bool level)
  659. {
  660. int bit_index;
  661. bool set;
  662. unsigned long *ptr;
  663. if (number == KVM_ARM_IRQ_CPU_IRQ)
  664. bit_index = __ffs(HCR_VI);
  665. else /* KVM_ARM_IRQ_CPU_FIQ */
  666. bit_index = __ffs(HCR_VF);
  667. ptr = (unsigned long *)&vcpu->arch.irq_lines;
  668. if (level)
  669. set = test_and_set_bit(bit_index, ptr);
  670. else
  671. set = test_and_clear_bit(bit_index, ptr);
  672. /*
  673. * If we didn't change anything, no need to wake up or kick other CPUs
  674. */
  675. if (set == level)
  676. return 0;
  677. /*
  678. * The vcpu irq_lines field was updated, wake up sleeping VCPUs and
  679. * trigger a world-switch round on the running physical CPU to set the
  680. * virtual IRQ/FIQ fields in the HCR appropriately.
  681. */
  682. kvm_vcpu_kick(vcpu);
  683. return 0;
  684. }
  685. int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level)
  686. {
  687. u32 irq = irq_level->irq;
  688. unsigned int irq_type, vcpu_idx, irq_num;
  689. int nrcpus = atomic_read(&kvm->online_vcpus);
  690. struct kvm_vcpu *vcpu = NULL;
  691. bool level = irq_level->level;
  692. irq_type = (irq >> KVM_ARM_IRQ_TYPE_SHIFT) & KVM_ARM_IRQ_TYPE_MASK;
  693. vcpu_idx = (irq >> KVM_ARM_IRQ_VCPU_SHIFT) & KVM_ARM_IRQ_VCPU_MASK;
  694. irq_num = (irq >> KVM_ARM_IRQ_NUM_SHIFT) & KVM_ARM_IRQ_NUM_MASK;
  695. trace_kvm_irq_line(irq_type, vcpu_idx, irq_num, irq_level->level);
  696. switch (irq_type) {
  697. case KVM_ARM_IRQ_TYPE_CPU:
  698. if (irqchip_in_kernel(kvm))
  699. return -ENXIO;
  700. if (vcpu_idx >= nrcpus)
  701. return -EINVAL;
  702. vcpu = kvm_get_vcpu(kvm, vcpu_idx);
  703. if (!vcpu)
  704. return -EINVAL;
  705. if (irq_num > KVM_ARM_IRQ_CPU_FIQ)
  706. return -EINVAL;
  707. return vcpu_interrupt_line(vcpu, irq_num, level);
  708. case KVM_ARM_IRQ_TYPE_PPI:
  709. if (!irqchip_in_kernel(kvm))
  710. return -ENXIO;
  711. if (vcpu_idx >= nrcpus)
  712. return -EINVAL;
  713. vcpu = kvm_get_vcpu(kvm, vcpu_idx);
  714. if (!vcpu)
  715. return -EINVAL;
  716. if (irq_num < VGIC_NR_SGIS || irq_num >= VGIC_NR_PRIVATE_IRQS)
  717. return -EINVAL;
  718. return kvm_vgic_inject_irq(kvm, vcpu->vcpu_id, irq_num, level);
  719. case KVM_ARM_IRQ_TYPE_SPI:
  720. if (!irqchip_in_kernel(kvm))
  721. return -ENXIO;
  722. if (irq_num < VGIC_NR_PRIVATE_IRQS ||
  723. irq_num > KVM_ARM_IRQ_GIC_MAX)
  724. return -EINVAL;
  725. return kvm_vgic_inject_irq(kvm, 0, irq_num, level);
  726. }
  727. return -EINVAL;
  728. }
  729. long kvm_arch_vcpu_ioctl(struct file *filp,
  730. unsigned int ioctl, unsigned long arg)
  731. {
  732. struct kvm_vcpu *vcpu = filp->private_data;
  733. void __user *argp = (void __user *)arg;
  734. switch (ioctl) {
  735. case KVM_ARM_VCPU_INIT: {
  736. struct kvm_vcpu_init init;
  737. if (copy_from_user(&init, argp, sizeof(init)))
  738. return -EFAULT;
  739. return kvm_vcpu_set_target(vcpu, &init);
  740. }
  741. case KVM_SET_ONE_REG:
  742. case KVM_GET_ONE_REG: {
  743. struct kvm_one_reg reg;
  744. if (copy_from_user(&reg, argp, sizeof(reg)))
  745. return -EFAULT;
  746. if (ioctl == KVM_SET_ONE_REG)
  747. return kvm_arm_set_reg(vcpu, &reg);
  748. else
  749. return kvm_arm_get_reg(vcpu, &reg);
  750. }
  751. case KVM_GET_REG_LIST: {
  752. struct kvm_reg_list __user *user_list = argp;
  753. struct kvm_reg_list reg_list;
  754. unsigned n;
  755. if (copy_from_user(&reg_list, user_list, sizeof(reg_list)))
  756. return -EFAULT;
  757. n = reg_list.n;
  758. reg_list.n = kvm_arm_num_regs(vcpu);
  759. if (copy_to_user(user_list, &reg_list, sizeof(reg_list)))
  760. return -EFAULT;
  761. if (n < reg_list.n)
  762. return -E2BIG;
  763. return kvm_arm_copy_reg_indices(vcpu, user_list->reg);
  764. }
  765. default:
  766. return -EINVAL;
  767. }
  768. }
  769. int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
  770. {
  771. return -EINVAL;
  772. }
  773. static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm,
  774. struct kvm_arm_device_addr *dev_addr)
  775. {
  776. unsigned long dev_id, type;
  777. dev_id = (dev_addr->id & KVM_ARM_DEVICE_ID_MASK) >>
  778. KVM_ARM_DEVICE_ID_SHIFT;
  779. type = (dev_addr->id & KVM_ARM_DEVICE_TYPE_MASK) >>
  780. KVM_ARM_DEVICE_TYPE_SHIFT;
  781. switch (dev_id) {
  782. case KVM_ARM_DEVICE_VGIC_V2:
  783. if (!vgic_present)
  784. return -ENXIO;
  785. return kvm_vgic_set_addr(kvm, type, dev_addr->addr);
  786. default:
  787. return -ENODEV;
  788. }
  789. }
  790. long kvm_arch_vm_ioctl(struct file *filp,
  791. unsigned int ioctl, unsigned long arg)
  792. {
  793. struct kvm *kvm = filp->private_data;
  794. void __user *argp = (void __user *)arg;
  795. switch (ioctl) {
  796. case KVM_CREATE_IRQCHIP: {
  797. if (vgic_present)
  798. return kvm_vgic_create(kvm);
  799. else
  800. return -ENXIO;
  801. }
  802. case KVM_ARM_SET_DEVICE_ADDR: {
  803. struct kvm_arm_device_addr dev_addr;
  804. if (copy_from_user(&dev_addr, argp, sizeof(dev_addr)))
  805. return -EFAULT;
  806. return kvm_vm_ioctl_set_device_addr(kvm, &dev_addr);
  807. }
  808. default:
  809. return -EINVAL;
  810. }
  811. }
  812. static void cpu_init_hyp_mode(void *vector)
  813. {
  814. unsigned long long pgd_ptr;
  815. unsigned long pgd_low, pgd_high;
  816. unsigned long hyp_stack_ptr;
  817. unsigned long stack_page;
  818. unsigned long vector_ptr;
  819. /* Switch from the HYP stub to our own HYP init vector */
  820. __hyp_set_vectors((unsigned long)vector);
  821. pgd_ptr = (unsigned long long)kvm_mmu_get_httbr();
  822. pgd_low = (pgd_ptr & ((1ULL << 32) - 1));
  823. pgd_high = (pgd_ptr >> 32ULL);
  824. stack_page = __get_cpu_var(kvm_arm_hyp_stack_page);
  825. hyp_stack_ptr = stack_page + PAGE_SIZE;
  826. vector_ptr = (unsigned long)__kvm_hyp_vector;
  827. /*
  828. * Call initialization code, and switch to the full blown
  829. * HYP code. The init code doesn't need to preserve these registers as
  830. * r1-r3 and r12 are already callee save according to the AAPCS.
  831. * Note that we slightly misuse the prototype by casing the pgd_low to
  832. * a void *.
  833. */
  834. kvm_call_hyp((void *)pgd_low, pgd_high, hyp_stack_ptr, vector_ptr);
  835. }
  836. /**
  837. * Inits Hyp-mode on all online CPUs
  838. */
  839. static int init_hyp_mode(void)
  840. {
  841. phys_addr_t init_phys_addr;
  842. int cpu;
  843. int err = 0;
  844. /*
  845. * Allocate Hyp PGD and setup Hyp identity mapping
  846. */
  847. err = kvm_mmu_init();
  848. if (err)
  849. goto out_err;
  850. /*
  851. * It is probably enough to obtain the default on one
  852. * CPU. It's unlikely to be different on the others.
  853. */
  854. hyp_default_vectors = __hyp_get_vectors();
  855. /*
  856. * Allocate stack pages for Hypervisor-mode
  857. */
  858. for_each_possible_cpu(cpu) {
  859. unsigned long stack_page;
  860. stack_page = __get_free_page(GFP_KERNEL);
  861. if (!stack_page) {
  862. err = -ENOMEM;
  863. goto out_free_stack_pages;
  864. }
  865. per_cpu(kvm_arm_hyp_stack_page, cpu) = stack_page;
  866. }
  867. /*
  868. * Execute the init code on each CPU.
  869. *
  870. * Note: The stack is not mapped yet, so don't do anything else than
  871. * initializing the hypervisor mode on each CPU using a local stack
  872. * space for temporary storage.
  873. */
  874. init_phys_addr = virt_to_phys(__kvm_hyp_init);
  875. for_each_online_cpu(cpu) {
  876. smp_call_function_single(cpu, cpu_init_hyp_mode,
  877. (void *)(long)init_phys_addr, 1);
  878. }
  879. /*
  880. * Unmap the identity mapping
  881. */
  882. kvm_clear_hyp_idmap();
  883. /*
  884. * Map the Hyp-code called directly from the host
  885. */
  886. err = create_hyp_mappings(__kvm_hyp_code_start, __kvm_hyp_code_end);
  887. if (err) {
  888. kvm_err("Cannot map world-switch code\n");
  889. goto out_free_mappings;
  890. }
  891. /*
  892. * Map the Hyp stack pages
  893. */
  894. for_each_possible_cpu(cpu) {
  895. char *stack_page = (char *)per_cpu(kvm_arm_hyp_stack_page, cpu);
  896. err = create_hyp_mappings(stack_page, stack_page + PAGE_SIZE);
  897. if (err) {
  898. kvm_err("Cannot map hyp stack\n");
  899. goto out_free_mappings;
  900. }
  901. }
  902. /*
  903. * Map the host VFP structures
  904. */
  905. kvm_host_vfp_state = alloc_percpu(struct vfp_hard_struct);
  906. if (!kvm_host_vfp_state) {
  907. err = -ENOMEM;
  908. kvm_err("Cannot allocate host VFP state\n");
  909. goto out_free_mappings;
  910. }
  911. for_each_possible_cpu(cpu) {
  912. struct vfp_hard_struct *vfp;
  913. vfp = per_cpu_ptr(kvm_host_vfp_state, cpu);
  914. err = create_hyp_mappings(vfp, vfp + 1);
  915. if (err) {
  916. kvm_err("Cannot map host VFP state: %d\n", err);
  917. goto out_free_vfp;
  918. }
  919. }
  920. /*
  921. * Init HYP view of VGIC
  922. */
  923. err = kvm_vgic_hyp_init();
  924. if (err)
  925. goto out_free_vfp;
  926. #ifdef CONFIG_KVM_ARM_VGIC
  927. vgic_present = true;
  928. #endif
  929. /*
  930. * Init HYP architected timer support
  931. */
  932. err = kvm_timer_hyp_init();
  933. if (err)
  934. goto out_free_mappings;
  935. kvm_info("Hyp mode initialized successfully\n");
  936. return 0;
  937. out_free_vfp:
  938. free_percpu(kvm_host_vfp_state);
  939. out_free_mappings:
  940. free_hyp_pmds();
  941. out_free_stack_pages:
  942. for_each_possible_cpu(cpu)
  943. free_page(per_cpu(kvm_arm_hyp_stack_page, cpu));
  944. out_err:
  945. kvm_err("error initializing Hyp mode: %d\n", err);
  946. return err;
  947. }
  948. /**
  949. * Initialize Hyp-mode and memory mappings on all CPUs.
  950. */
  951. int kvm_arch_init(void *opaque)
  952. {
  953. int err;
  954. if (!is_hyp_mode_available()) {
  955. kvm_err("HYP mode not available\n");
  956. return -ENODEV;
  957. }
  958. if (kvm_target_cpu() < 0) {
  959. kvm_err("Target CPU not supported!\n");
  960. return -ENODEV;
  961. }
  962. err = init_hyp_mode();
  963. if (err)
  964. goto out_err;
  965. kvm_coproc_table_init();
  966. return 0;
  967. out_err:
  968. return err;
  969. }
  970. /* NOP: Compiling as a module not supported */
  971. void kvm_arch_exit(void)
  972. {
  973. }
  974. static int arm_init(void)
  975. {
  976. int rc = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
  977. return rc;
  978. }
  979. module_init(arm_init);