arm.c 26 KB

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