arm.c 22 KB

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