arm.c 22 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034
  1. /*
  2. * Copyright (C) 2012 - Virtual Open Systems and Columbia University
  3. * Author: Christoffer Dall <c.dall@virtualopensystems.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License, version 2, as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. */
  18. #include <linux/cpu.h>
  19. #include <linux/errno.h>
  20. #include <linux/err.h>
  21. #include <linux/kvm_host.h>
  22. #include <linux/module.h>
  23. #include <linux/vmalloc.h>
  24. #include <linux/fs.h>
  25. #include <linux/mman.h>
  26. #include <linux/sched.h>
  27. #include <linux/kvm.h>
  28. #include <trace/events/kvm.h>
  29. #define CREATE_TRACE_POINTS
  30. #include "trace.h"
  31. #include <asm/uaccess.h>
  32. #include <asm/ptrace.h>
  33. #include <asm/mman.h>
  34. #include <asm/tlbflush.h>
  35. #include <asm/cacheflush.h>
  36. #include <asm/virt.h>
  37. #include <asm/kvm_arm.h>
  38. #include <asm/kvm_asm.h>
  39. #include <asm/kvm_mmu.h>
  40. #include <asm/kvm_emulate.h>
  41. #include <asm/kvm_coproc.h>
  42. #include <asm/kvm_psci.h>
  43. #ifdef REQUIRES_VIRT
  44. __asm__(".arch_extension virt");
  45. #endif
  46. static DEFINE_PER_CPU(unsigned long, kvm_arm_hyp_stack_page);
  47. static kvm_cpu_context_t __percpu *kvm_host_cpu_state;
  48. static unsigned long hyp_default_vectors;
  49. /* Per-CPU variable containing the currently running vcpu. */
  50. static DEFINE_PER_CPU(struct kvm_vcpu *, kvm_arm_running_vcpu);
  51. /* The VMID used in the VTTBR */
  52. static atomic64_t kvm_vmid_gen = ATOMIC64_INIT(1);
  53. static u8 kvm_next_vmid;
  54. static DEFINE_SPINLOCK(kvm_vmid_lock);
  55. static bool vgic_present;
  56. static void kvm_arm_set_running_vcpu(struct kvm_vcpu *vcpu)
  57. {
  58. BUG_ON(preemptible());
  59. __this_cpu_write(kvm_arm_running_vcpu, vcpu);
  60. }
  61. /**
  62. * kvm_arm_get_running_vcpu - get the vcpu running on the current CPU.
  63. * Must be called from non-preemptible context
  64. */
  65. struct kvm_vcpu *kvm_arm_get_running_vcpu(void)
  66. {
  67. BUG_ON(preemptible());
  68. return __this_cpu_read(kvm_arm_running_vcpu);
  69. }
  70. /**
  71. * kvm_arm_get_running_vcpus - get the per-CPU array of currently running vcpus.
  72. */
  73. struct kvm_vcpu __percpu **kvm_get_running_vcpus(void)
  74. {
  75. return &kvm_arm_running_vcpu;
  76. }
  77. int kvm_arch_hardware_enable(void *garbage)
  78. {
  79. return 0;
  80. }
  81. int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
  82. {
  83. return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
  84. }
  85. void kvm_arch_hardware_disable(void *garbage)
  86. {
  87. }
  88. int kvm_arch_hardware_setup(void)
  89. {
  90. return 0;
  91. }
  92. void kvm_arch_hardware_unsetup(void)
  93. {
  94. }
  95. void kvm_arch_check_processor_compat(void *rtn)
  96. {
  97. *(int *)rtn = 0;
  98. }
  99. void kvm_arch_sync_events(struct kvm *kvm)
  100. {
  101. }
  102. /**
  103. * kvm_arch_init_vm - initializes a VM data structure
  104. * @kvm: pointer to the KVM struct
  105. */
  106. int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
  107. {
  108. int ret = 0;
  109. if (type)
  110. return -EINVAL;
  111. ret = kvm_alloc_stage2_pgd(kvm);
  112. if (ret)
  113. goto out_fail_alloc;
  114. ret = create_hyp_mappings(kvm, kvm + 1);
  115. if (ret)
  116. goto out_free_stage2_pgd;
  117. /* Mark the initial VMID generation invalid */
  118. kvm->arch.vmid_gen = 0;
  119. return ret;
  120. out_free_stage2_pgd:
  121. kvm_free_stage2_pgd(kvm);
  122. out_fail_alloc:
  123. return ret;
  124. }
  125. int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
  126. {
  127. return VM_FAULT_SIGBUS;
  128. }
  129. void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free,
  130. struct kvm_memory_slot *dont)
  131. {
  132. }
  133. int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
  134. unsigned long npages)
  135. {
  136. return 0;
  137. }
  138. /**
  139. * kvm_arch_destroy_vm - destroy the VM data structure
  140. * @kvm: pointer to the KVM struct
  141. */
  142. void kvm_arch_destroy_vm(struct kvm *kvm)
  143. {
  144. int i;
  145. kvm_free_stage2_pgd(kvm);
  146. for (i = 0; i < KVM_MAX_VCPUS; ++i) {
  147. if (kvm->vcpus[i]) {
  148. kvm_arch_vcpu_free(kvm->vcpus[i]);
  149. kvm->vcpus[i] = NULL;
  150. }
  151. }
  152. }
  153. int kvm_dev_ioctl_check_extension(long ext)
  154. {
  155. int r;
  156. switch (ext) {
  157. case KVM_CAP_IRQCHIP:
  158. r = vgic_present;
  159. break;
  160. case KVM_CAP_USER_MEMORY:
  161. case KVM_CAP_SYNC_MMU:
  162. case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
  163. case KVM_CAP_ONE_REG:
  164. case KVM_CAP_ARM_PSCI:
  165. r = 1;
  166. break;
  167. case KVM_CAP_COALESCED_MMIO:
  168. r = KVM_COALESCED_MMIO_PAGE_OFFSET;
  169. break;
  170. case KVM_CAP_ARM_SET_DEVICE_ADDR:
  171. r = 1;
  172. break;
  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 = kvm_arch_dev_ioctl_check_extension(ext);
  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. void kvm_arch_memslots_updated(struct kvm *kvm)
  191. {
  192. }
  193. int kvm_arch_prepare_memory_region(struct kvm *kvm,
  194. struct kvm_memory_slot *memslot,
  195. struct kvm_userspace_memory_region *mem,
  196. enum kvm_mr_change change)
  197. {
  198. return 0;
  199. }
  200. void kvm_arch_commit_memory_region(struct kvm *kvm,
  201. struct kvm_userspace_memory_region *mem,
  202. const struct kvm_memory_slot *old,
  203. enum kvm_mr_change change)
  204. {
  205. }
  206. void kvm_arch_flush_shadow_all(struct kvm *kvm)
  207. {
  208. }
  209. void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
  210. struct kvm_memory_slot *slot)
  211. {
  212. }
  213. struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
  214. {
  215. int err;
  216. struct kvm_vcpu *vcpu;
  217. vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
  218. if (!vcpu) {
  219. err = -ENOMEM;
  220. goto out;
  221. }
  222. err = kvm_vcpu_init(vcpu, kvm, id);
  223. if (err)
  224. goto free_vcpu;
  225. err = create_hyp_mappings(vcpu, vcpu + 1);
  226. if (err)
  227. goto vcpu_uninit;
  228. return vcpu;
  229. vcpu_uninit:
  230. kvm_vcpu_uninit(vcpu);
  231. free_vcpu:
  232. kmem_cache_free(kvm_vcpu_cache, vcpu);
  233. out:
  234. return ERR_PTR(err);
  235. }
  236. int kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
  237. {
  238. return 0;
  239. }
  240. void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
  241. {
  242. kvm_mmu_free_memory_caches(vcpu);
  243. kvm_timer_vcpu_terminate(vcpu);
  244. kmem_cache_free(kvm_vcpu_cache, vcpu);
  245. }
  246. void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
  247. {
  248. kvm_arch_vcpu_free(vcpu);
  249. }
  250. int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
  251. {
  252. return 0;
  253. }
  254. int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
  255. {
  256. int ret;
  257. /* Force users to call KVM_ARM_VCPU_INIT */
  258. vcpu->arch.target = -1;
  259. /* Set up VGIC */
  260. ret = kvm_vgic_vcpu_init(vcpu);
  261. if (ret)
  262. return ret;
  263. /* Set up the timer */
  264. kvm_timer_vcpu_init(vcpu);
  265. return 0;
  266. }
  267. void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
  268. {
  269. }
  270. void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  271. {
  272. vcpu->cpu = cpu;
  273. vcpu->arch.host_cpu_context = this_cpu_ptr(kvm_host_cpu_state);
  274. /*
  275. * Check whether this vcpu requires the cache to be flushed on
  276. * this physical CPU. This is a consequence of doing dcache
  277. * operations by set/way on this vcpu. We do it here to be in
  278. * a non-preemptible section.
  279. */
  280. if (cpumask_test_and_clear_cpu(cpu, &vcpu->arch.require_dcache_flush))
  281. flush_cache_all(); /* We'd really want v7_flush_dcache_all() */
  282. kvm_arm_set_running_vcpu(vcpu);
  283. }
  284. void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
  285. {
  286. kvm_arm_set_running_vcpu(NULL);
  287. }
  288. int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
  289. struct kvm_guest_debug *dbg)
  290. {
  291. return -EINVAL;
  292. }
  293. int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
  294. struct kvm_mp_state *mp_state)
  295. {
  296. return -EINVAL;
  297. }
  298. int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
  299. struct kvm_mp_state *mp_state)
  300. {
  301. return -EINVAL;
  302. }
  303. /**
  304. * kvm_arch_vcpu_runnable - determine if the vcpu can be scheduled
  305. * @v: The VCPU pointer
  306. *
  307. * If the guest CPU is not waiting for interrupts or an interrupt line is
  308. * asserted, the CPU is by definition runnable.
  309. */
  310. int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
  311. {
  312. return !!v->arch.irq_lines || kvm_vgic_vcpu_pending_irq(v);
  313. }
  314. /* Just ensure a guest exit from a particular CPU */
  315. static void exit_vm_noop(void *info)
  316. {
  317. }
  318. void force_vm_exit(const cpumask_t *mask)
  319. {
  320. smp_call_function_many(mask, exit_vm_noop, NULL, true);
  321. }
  322. /**
  323. * need_new_vmid_gen - check that the VMID is still valid
  324. * @kvm: The VM's VMID to checkt
  325. *
  326. * return true if there is a new generation of VMIDs being used
  327. *
  328. * The hardware supports only 256 values with the value zero reserved for the
  329. * host, so we check if an assigned value belongs to a previous generation,
  330. * which which requires us to assign a new value. If we're the first to use a
  331. * VMID for the new generation, we must flush necessary caches and TLBs on all
  332. * CPUs.
  333. */
  334. static bool need_new_vmid_gen(struct kvm *kvm)
  335. {
  336. return unlikely(kvm->arch.vmid_gen != atomic64_read(&kvm_vmid_gen));
  337. }
  338. /**
  339. * update_vttbr - Update the VTTBR with a valid VMID before the guest runs
  340. * @kvm The guest that we are about to run
  341. *
  342. * Called from kvm_arch_vcpu_ioctl_run before entering the guest to ensure the
  343. * VM has a valid VMID, otherwise assigns a new one and flushes corresponding
  344. * caches and TLBs.
  345. */
  346. static void update_vttbr(struct kvm *kvm)
  347. {
  348. phys_addr_t pgd_phys;
  349. u64 vmid;
  350. if (!need_new_vmid_gen(kvm))
  351. return;
  352. spin_lock(&kvm_vmid_lock);
  353. /*
  354. * We need to re-check the vmid_gen here to ensure that if another vcpu
  355. * already allocated a valid vmid for this vm, then this vcpu should
  356. * use the same vmid.
  357. */
  358. if (!need_new_vmid_gen(kvm)) {
  359. spin_unlock(&kvm_vmid_lock);
  360. return;
  361. }
  362. /* First user of a new VMID generation? */
  363. if (unlikely(kvm_next_vmid == 0)) {
  364. atomic64_inc(&kvm_vmid_gen);
  365. kvm_next_vmid = 1;
  366. /*
  367. * On SMP we know no other CPUs can use this CPU's or each
  368. * other's VMID after force_vm_exit returns since the
  369. * kvm_vmid_lock blocks them from reentry to the guest.
  370. */
  371. force_vm_exit(cpu_all_mask);
  372. /*
  373. * Now broadcast TLB + ICACHE invalidation over the inner
  374. * shareable domain to make sure all data structures are
  375. * clean.
  376. */
  377. kvm_call_hyp(__kvm_flush_vm_context);
  378. }
  379. kvm->arch.vmid_gen = atomic64_read(&kvm_vmid_gen);
  380. kvm->arch.vmid = kvm_next_vmid;
  381. kvm_next_vmid++;
  382. /* update vttbr to be used with the new vmid */
  383. pgd_phys = virt_to_phys(kvm->arch.pgd);
  384. vmid = ((u64)(kvm->arch.vmid) << VTTBR_VMID_SHIFT) & VTTBR_VMID_MASK;
  385. kvm->arch.vttbr = pgd_phys & VTTBR_BADDR_MASK;
  386. kvm->arch.vttbr |= vmid;
  387. spin_unlock(&kvm_vmid_lock);
  388. }
  389. static int kvm_vcpu_first_run_init(struct kvm_vcpu *vcpu)
  390. {
  391. if (likely(vcpu->arch.has_run_once))
  392. return 0;
  393. vcpu->arch.has_run_once = true;
  394. /*
  395. * Initialize the VGIC before running a vcpu the first time on
  396. * this VM.
  397. */
  398. if (irqchip_in_kernel(vcpu->kvm) &&
  399. unlikely(!vgic_initialized(vcpu->kvm))) {
  400. int ret = kvm_vgic_init(vcpu->kvm);
  401. if (ret)
  402. return ret;
  403. }
  404. /*
  405. * Handle the "start in power-off" case by calling into the
  406. * PSCI code.
  407. */
  408. if (test_and_clear_bit(KVM_ARM_VCPU_POWER_OFF, vcpu->arch.features)) {
  409. *vcpu_reg(vcpu, 0) = KVM_PSCI_FN_CPU_OFF;
  410. kvm_psci_call(vcpu);
  411. }
  412. return 0;
  413. }
  414. static void vcpu_pause(struct kvm_vcpu *vcpu)
  415. {
  416. wait_queue_head_t *wq = kvm_arch_vcpu_wq(vcpu);
  417. wait_event_interruptible(*wq, !vcpu->arch.pause);
  418. }
  419. static int kvm_vcpu_initialized(struct kvm_vcpu *vcpu)
  420. {
  421. return vcpu->arch.target >= 0;
  422. }
  423. /**
  424. * kvm_arch_vcpu_ioctl_run - the main VCPU run function to execute guest code
  425. * @vcpu: The VCPU pointer
  426. * @run: The kvm_run structure pointer used for userspace state exchange
  427. *
  428. * This function is called through the VCPU_RUN ioctl called from user space. It
  429. * will execute VM code in a loop until the time slice for the process is used
  430. * or some emulation is needed from user space in which case the function will
  431. * return with return value 0 and with the kvm_run structure filled in with the
  432. * required data for the requested emulation.
  433. */
  434. int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
  435. {
  436. int ret;
  437. sigset_t sigsaved;
  438. if (unlikely(!kvm_vcpu_initialized(vcpu)))
  439. return -ENOEXEC;
  440. ret = kvm_vcpu_first_run_init(vcpu);
  441. if (ret)
  442. return ret;
  443. if (run->exit_reason == KVM_EXIT_MMIO) {
  444. ret = kvm_handle_mmio_return(vcpu, vcpu->run);
  445. if (ret)
  446. return ret;
  447. }
  448. if (vcpu->sigset_active)
  449. sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
  450. ret = 1;
  451. run->exit_reason = KVM_EXIT_UNKNOWN;
  452. while (ret > 0) {
  453. /*
  454. * Check conditions before entering the guest
  455. */
  456. cond_resched();
  457. update_vttbr(vcpu->kvm);
  458. if (vcpu->arch.pause)
  459. vcpu_pause(vcpu);
  460. kvm_vgic_flush_hwstate(vcpu);
  461. kvm_timer_flush_hwstate(vcpu);
  462. local_irq_disable();
  463. /*
  464. * Re-check atomic conditions
  465. */
  466. if (signal_pending(current)) {
  467. ret = -EINTR;
  468. run->exit_reason = KVM_EXIT_INTR;
  469. }
  470. if (ret <= 0 || need_new_vmid_gen(vcpu->kvm)) {
  471. local_irq_enable();
  472. kvm_timer_sync_hwstate(vcpu);
  473. kvm_vgic_sync_hwstate(vcpu);
  474. continue;
  475. }
  476. /**************************************************************
  477. * Enter the guest
  478. */
  479. trace_kvm_entry(*vcpu_pc(vcpu));
  480. kvm_guest_enter();
  481. vcpu->mode = IN_GUEST_MODE;
  482. ret = kvm_call_hyp(__kvm_vcpu_run, vcpu);
  483. vcpu->mode = OUTSIDE_GUEST_MODE;
  484. vcpu->arch.last_pcpu = smp_processor_id();
  485. kvm_guest_exit();
  486. trace_kvm_exit(*vcpu_pc(vcpu));
  487. /*
  488. * We may have taken a host interrupt in HYP mode (ie
  489. * while executing the guest). This interrupt is still
  490. * pending, as we haven't serviced it yet!
  491. *
  492. * We're now back in SVC mode, with interrupts
  493. * disabled. Enabling the interrupts now will have
  494. * the effect of taking the interrupt again, in SVC
  495. * mode this time.
  496. */
  497. local_irq_enable();
  498. /*
  499. * Back from guest
  500. *************************************************************/
  501. kvm_timer_sync_hwstate(vcpu);
  502. kvm_vgic_sync_hwstate(vcpu);
  503. ret = handle_exit(vcpu, run, ret);
  504. }
  505. if (vcpu->sigset_active)
  506. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  507. return ret;
  508. }
  509. static int vcpu_interrupt_line(struct kvm_vcpu *vcpu, int number, bool level)
  510. {
  511. int bit_index;
  512. bool set;
  513. unsigned long *ptr;
  514. if (number == KVM_ARM_IRQ_CPU_IRQ)
  515. bit_index = __ffs(HCR_VI);
  516. else /* KVM_ARM_IRQ_CPU_FIQ */
  517. bit_index = __ffs(HCR_VF);
  518. ptr = (unsigned long *)&vcpu->arch.irq_lines;
  519. if (level)
  520. set = test_and_set_bit(bit_index, ptr);
  521. else
  522. set = test_and_clear_bit(bit_index, ptr);
  523. /*
  524. * If we didn't change anything, no need to wake up or kick other CPUs
  525. */
  526. if (set == level)
  527. return 0;
  528. /*
  529. * The vcpu irq_lines field was updated, wake up sleeping VCPUs and
  530. * trigger a world-switch round on the running physical CPU to set the
  531. * virtual IRQ/FIQ fields in the HCR appropriately.
  532. */
  533. kvm_vcpu_kick(vcpu);
  534. return 0;
  535. }
  536. int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level,
  537. bool line_status)
  538. {
  539. u32 irq = irq_level->irq;
  540. unsigned int irq_type, vcpu_idx, irq_num;
  541. int nrcpus = atomic_read(&kvm->online_vcpus);
  542. struct kvm_vcpu *vcpu = NULL;
  543. bool level = irq_level->level;
  544. irq_type = (irq >> KVM_ARM_IRQ_TYPE_SHIFT) & KVM_ARM_IRQ_TYPE_MASK;
  545. vcpu_idx = (irq >> KVM_ARM_IRQ_VCPU_SHIFT) & KVM_ARM_IRQ_VCPU_MASK;
  546. irq_num = (irq >> KVM_ARM_IRQ_NUM_SHIFT) & KVM_ARM_IRQ_NUM_MASK;
  547. trace_kvm_irq_line(irq_type, vcpu_idx, irq_num, irq_level->level);
  548. switch (irq_type) {
  549. case KVM_ARM_IRQ_TYPE_CPU:
  550. if (irqchip_in_kernel(kvm))
  551. return -ENXIO;
  552. if (vcpu_idx >= nrcpus)
  553. return -EINVAL;
  554. vcpu = kvm_get_vcpu(kvm, vcpu_idx);
  555. if (!vcpu)
  556. return -EINVAL;
  557. if (irq_num > KVM_ARM_IRQ_CPU_FIQ)
  558. return -EINVAL;
  559. return vcpu_interrupt_line(vcpu, irq_num, level);
  560. case KVM_ARM_IRQ_TYPE_PPI:
  561. if (!irqchip_in_kernel(kvm))
  562. return -ENXIO;
  563. if (vcpu_idx >= nrcpus)
  564. return -EINVAL;
  565. vcpu = kvm_get_vcpu(kvm, vcpu_idx);
  566. if (!vcpu)
  567. return -EINVAL;
  568. if (irq_num < VGIC_NR_SGIS || irq_num >= VGIC_NR_PRIVATE_IRQS)
  569. return -EINVAL;
  570. return kvm_vgic_inject_irq(kvm, vcpu->vcpu_id, irq_num, level);
  571. case KVM_ARM_IRQ_TYPE_SPI:
  572. if (!irqchip_in_kernel(kvm))
  573. return -ENXIO;
  574. if (irq_num < VGIC_NR_PRIVATE_IRQS ||
  575. irq_num > KVM_ARM_IRQ_GIC_MAX)
  576. return -EINVAL;
  577. return kvm_vgic_inject_irq(kvm, 0, irq_num, level);
  578. }
  579. return -EINVAL;
  580. }
  581. long kvm_arch_vcpu_ioctl(struct file *filp,
  582. unsigned int ioctl, unsigned long arg)
  583. {
  584. struct kvm_vcpu *vcpu = filp->private_data;
  585. void __user *argp = (void __user *)arg;
  586. switch (ioctl) {
  587. case KVM_ARM_VCPU_INIT: {
  588. struct kvm_vcpu_init init;
  589. if (copy_from_user(&init, argp, sizeof(init)))
  590. return -EFAULT;
  591. return kvm_vcpu_set_target(vcpu, &init);
  592. }
  593. case KVM_SET_ONE_REG:
  594. case KVM_GET_ONE_REG: {
  595. struct kvm_one_reg reg;
  596. if (unlikely(!kvm_vcpu_initialized(vcpu)))
  597. return -ENOEXEC;
  598. if (copy_from_user(&reg, argp, sizeof(reg)))
  599. return -EFAULT;
  600. if (ioctl == KVM_SET_ONE_REG)
  601. return kvm_arm_set_reg(vcpu, &reg);
  602. else
  603. return kvm_arm_get_reg(vcpu, &reg);
  604. }
  605. case KVM_GET_REG_LIST: {
  606. struct kvm_reg_list __user *user_list = argp;
  607. struct kvm_reg_list reg_list;
  608. unsigned n;
  609. if (unlikely(!kvm_vcpu_initialized(vcpu)))
  610. return -ENOEXEC;
  611. if (copy_from_user(&reg_list, user_list, sizeof(reg_list)))
  612. return -EFAULT;
  613. n = reg_list.n;
  614. reg_list.n = kvm_arm_num_regs(vcpu);
  615. if (copy_to_user(user_list, &reg_list, sizeof(reg_list)))
  616. return -EFAULT;
  617. if (n < reg_list.n)
  618. return -E2BIG;
  619. return kvm_arm_copy_reg_indices(vcpu, user_list->reg);
  620. }
  621. default:
  622. return -EINVAL;
  623. }
  624. }
  625. int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
  626. {
  627. return -EINVAL;
  628. }
  629. static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm,
  630. struct kvm_arm_device_addr *dev_addr)
  631. {
  632. unsigned long dev_id, type;
  633. dev_id = (dev_addr->id & KVM_ARM_DEVICE_ID_MASK) >>
  634. KVM_ARM_DEVICE_ID_SHIFT;
  635. type = (dev_addr->id & KVM_ARM_DEVICE_TYPE_MASK) >>
  636. KVM_ARM_DEVICE_TYPE_SHIFT;
  637. switch (dev_id) {
  638. case KVM_ARM_DEVICE_VGIC_V2:
  639. if (!vgic_present)
  640. return -ENXIO;
  641. return kvm_vgic_set_addr(kvm, type, dev_addr->addr);
  642. default:
  643. return -ENODEV;
  644. }
  645. }
  646. long kvm_arch_vm_ioctl(struct file *filp,
  647. unsigned int ioctl, unsigned long arg)
  648. {
  649. struct kvm *kvm = filp->private_data;
  650. void __user *argp = (void __user *)arg;
  651. switch (ioctl) {
  652. case KVM_CREATE_IRQCHIP: {
  653. if (vgic_present)
  654. return kvm_vgic_create(kvm);
  655. else
  656. return -ENXIO;
  657. }
  658. case KVM_ARM_SET_DEVICE_ADDR: {
  659. struct kvm_arm_device_addr dev_addr;
  660. if (copy_from_user(&dev_addr, argp, sizeof(dev_addr)))
  661. return -EFAULT;
  662. return kvm_vm_ioctl_set_device_addr(kvm, &dev_addr);
  663. }
  664. case KVM_ARM_PREFERRED_TARGET: {
  665. int err;
  666. struct kvm_vcpu_init init;
  667. err = kvm_vcpu_preferred_target(&init);
  668. if (err)
  669. return err;
  670. if (copy_to_user(argp, &init, sizeof(init)))
  671. return -EFAULT;
  672. return 0;
  673. }
  674. default:
  675. return -EINVAL;
  676. }
  677. }
  678. static void cpu_init_hyp_mode(void *dummy)
  679. {
  680. phys_addr_t boot_pgd_ptr;
  681. phys_addr_t pgd_ptr;
  682. unsigned long hyp_stack_ptr;
  683. unsigned long stack_page;
  684. unsigned long vector_ptr;
  685. /* Switch from the HYP stub to our own HYP init vector */
  686. __hyp_set_vectors(kvm_get_idmap_vector());
  687. boot_pgd_ptr = kvm_mmu_get_boot_httbr();
  688. pgd_ptr = kvm_mmu_get_httbr();
  689. stack_page = __this_cpu_read(kvm_arm_hyp_stack_page);
  690. hyp_stack_ptr = stack_page + PAGE_SIZE;
  691. vector_ptr = (unsigned long)__kvm_hyp_vector;
  692. __cpu_init_hyp_mode(boot_pgd_ptr, pgd_ptr, hyp_stack_ptr, vector_ptr);
  693. }
  694. static int hyp_init_cpu_notify(struct notifier_block *self,
  695. unsigned long action, void *cpu)
  696. {
  697. switch (action) {
  698. case CPU_STARTING:
  699. case CPU_STARTING_FROZEN:
  700. cpu_init_hyp_mode(NULL);
  701. break;
  702. }
  703. return NOTIFY_OK;
  704. }
  705. static struct notifier_block hyp_init_cpu_nb = {
  706. .notifier_call = hyp_init_cpu_notify,
  707. };
  708. /**
  709. * Inits Hyp-mode on all online CPUs
  710. */
  711. static int init_hyp_mode(void)
  712. {
  713. int cpu;
  714. int err = 0;
  715. /*
  716. * Allocate Hyp PGD and setup Hyp identity mapping
  717. */
  718. err = kvm_mmu_init();
  719. if (err)
  720. goto out_err;
  721. /*
  722. * It is probably enough to obtain the default on one
  723. * CPU. It's unlikely to be different on the others.
  724. */
  725. hyp_default_vectors = __hyp_get_vectors();
  726. /*
  727. * Allocate stack pages for Hypervisor-mode
  728. */
  729. for_each_possible_cpu(cpu) {
  730. unsigned long stack_page;
  731. stack_page = __get_free_page(GFP_KERNEL);
  732. if (!stack_page) {
  733. err = -ENOMEM;
  734. goto out_free_stack_pages;
  735. }
  736. per_cpu(kvm_arm_hyp_stack_page, cpu) = stack_page;
  737. }
  738. /*
  739. * Map the Hyp-code called directly from the host
  740. */
  741. err = create_hyp_mappings(__kvm_hyp_code_start, __kvm_hyp_code_end);
  742. if (err) {
  743. kvm_err("Cannot map world-switch code\n");
  744. goto out_free_mappings;
  745. }
  746. /*
  747. * Map the Hyp stack pages
  748. */
  749. for_each_possible_cpu(cpu) {
  750. char *stack_page = (char *)per_cpu(kvm_arm_hyp_stack_page, cpu);
  751. err = create_hyp_mappings(stack_page, stack_page + PAGE_SIZE);
  752. if (err) {
  753. kvm_err("Cannot map hyp stack\n");
  754. goto out_free_mappings;
  755. }
  756. }
  757. /*
  758. * Map the host CPU structures
  759. */
  760. kvm_host_cpu_state = alloc_percpu(kvm_cpu_context_t);
  761. if (!kvm_host_cpu_state) {
  762. err = -ENOMEM;
  763. kvm_err("Cannot allocate host CPU state\n");
  764. goto out_free_mappings;
  765. }
  766. for_each_possible_cpu(cpu) {
  767. kvm_cpu_context_t *cpu_ctxt;
  768. cpu_ctxt = per_cpu_ptr(kvm_host_cpu_state, cpu);
  769. err = create_hyp_mappings(cpu_ctxt, cpu_ctxt + 1);
  770. if (err) {
  771. kvm_err("Cannot map host CPU state: %d\n", err);
  772. goto out_free_context;
  773. }
  774. }
  775. /*
  776. * Execute the init code on each CPU.
  777. */
  778. on_each_cpu(cpu_init_hyp_mode, NULL, 1);
  779. /*
  780. * Init HYP view of VGIC
  781. */
  782. err = kvm_vgic_hyp_init();
  783. if (err)
  784. goto out_free_context;
  785. #ifdef CONFIG_KVM_ARM_VGIC
  786. vgic_present = true;
  787. #endif
  788. /*
  789. * Init HYP architected timer support
  790. */
  791. err = kvm_timer_hyp_init();
  792. if (err)
  793. goto out_free_mappings;
  794. #ifndef CONFIG_HOTPLUG_CPU
  795. free_boot_hyp_pgd();
  796. #endif
  797. kvm_perf_init();
  798. kvm_info("Hyp mode initialized successfully\n");
  799. return 0;
  800. out_free_context:
  801. free_percpu(kvm_host_cpu_state);
  802. out_free_mappings:
  803. free_hyp_pgds();
  804. out_free_stack_pages:
  805. for_each_possible_cpu(cpu)
  806. free_page(per_cpu(kvm_arm_hyp_stack_page, cpu));
  807. out_err:
  808. kvm_err("error initializing Hyp mode: %d\n", err);
  809. return err;
  810. }
  811. static void check_kvm_target_cpu(void *ret)
  812. {
  813. *(int *)ret = kvm_target_cpu();
  814. }
  815. /**
  816. * Initialize Hyp-mode and memory mappings on all CPUs.
  817. */
  818. int kvm_arch_init(void *opaque)
  819. {
  820. int err;
  821. int ret, cpu;
  822. if (!is_hyp_mode_available()) {
  823. kvm_err("HYP mode not available\n");
  824. return -ENODEV;
  825. }
  826. for_each_online_cpu(cpu) {
  827. smp_call_function_single(cpu, check_kvm_target_cpu, &ret, 1);
  828. if (ret < 0) {
  829. kvm_err("Error, CPU %d not supported!\n", cpu);
  830. return -ENODEV;
  831. }
  832. }
  833. err = init_hyp_mode();
  834. if (err)
  835. goto out_err;
  836. err = register_cpu_notifier(&hyp_init_cpu_nb);
  837. if (err) {
  838. kvm_err("Cannot register HYP init CPU notifier (%d)\n", err);
  839. goto out_err;
  840. }
  841. kvm_coproc_table_init();
  842. return 0;
  843. out_err:
  844. return err;
  845. }
  846. /* NOP: Compiling as a module not supported */
  847. void kvm_arch_exit(void)
  848. {
  849. kvm_perf_teardown();
  850. }
  851. static int arm_init(void)
  852. {
  853. int rc = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
  854. return rc;
  855. }
  856. module_init(arm_init);