arm.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984
  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/uaccess.h>
  31. #include <asm/ptrace.h>
  32. #include <asm/mman.h>
  33. #include <asm/tlbflush.h>
  34. #include <asm/cacheflush.h>
  35. #include <asm/virt.h>
  36. #include <asm/kvm_arm.h>
  37. #include <asm/kvm_asm.h>
  38. #include <asm/kvm_mmu.h>
  39. #include <asm/kvm_emulate.h>
  40. #include <asm/kvm_coproc.h>
  41. #include <asm/kvm_psci.h>
  42. #ifdef REQUIRES_VIRT
  43. __asm__(".arch_extension virt");
  44. #endif
  45. static DEFINE_PER_CPU(unsigned long, kvm_arm_hyp_stack_page);
  46. static kvm_kernel_vfp_t __percpu *kvm_host_vfp_state;
  47. static unsigned long hyp_default_vectors;
  48. /* Per-CPU variable containing the currently running vcpu. */
  49. static DEFINE_PER_CPU(struct kvm_vcpu *, kvm_arm_running_vcpu);
  50. /* The VMID used in the VTTBR */
  51. static atomic64_t kvm_vmid_gen = ATOMIC64_INIT(1);
  52. static u8 kvm_next_vmid;
  53. static DEFINE_SPINLOCK(kvm_vmid_lock);
  54. static bool vgic_present;
  55. static void kvm_arm_set_running_vcpu(struct kvm_vcpu *vcpu)
  56. {
  57. BUG_ON(preemptible());
  58. __get_cpu_var(kvm_arm_running_vcpu) = vcpu;
  59. }
  60. /**
  61. * kvm_arm_get_running_vcpu - get the vcpu running on the current CPU.
  62. * Must be called from non-preemptible context
  63. */
  64. struct kvm_vcpu *kvm_arm_get_running_vcpu(void)
  65. {
  66. BUG_ON(preemptible());
  67. return __get_cpu_var(kvm_arm_running_vcpu);
  68. }
  69. /**
  70. * kvm_arm_get_running_vcpus - get the per-CPU array of currently running vcpus.
  71. */
  72. struct kvm_vcpu __percpu **kvm_get_running_vcpus(void)
  73. {
  74. return &kvm_arm_running_vcpu;
  75. }
  76. int kvm_arch_hardware_enable(void *garbage)
  77. {
  78. return 0;
  79. }
  80. int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
  81. {
  82. return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
  83. }
  84. void kvm_arch_hardware_disable(void *garbage)
  85. {
  86. }
  87. int kvm_arch_hardware_setup(void)
  88. {
  89. return 0;
  90. }
  91. void kvm_arch_hardware_unsetup(void)
  92. {
  93. }
  94. void kvm_arch_check_processor_compat(void *rtn)
  95. {
  96. *(int *)rtn = 0;
  97. }
  98. void kvm_arch_sync_events(struct kvm *kvm)
  99. {
  100. }
  101. /**
  102. * kvm_arch_init_vm - initializes a VM data structure
  103. * @kvm: pointer to the KVM struct
  104. */
  105. int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
  106. {
  107. int ret = 0;
  108. if (type)
  109. return -EINVAL;
  110. ret = kvm_alloc_stage2_pgd(kvm);
  111. if (ret)
  112. goto out_fail_alloc;
  113. ret = create_hyp_mappings(kvm, kvm + 1);
  114. if (ret)
  115. goto out_free_stage2_pgd;
  116. /* Mark the initial VMID generation invalid */
  117. kvm->arch.vmid_gen = 0;
  118. return ret;
  119. out_free_stage2_pgd:
  120. kvm_free_stage2_pgd(kvm);
  121. out_fail_alloc:
  122. return ret;
  123. }
  124. int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
  125. {
  126. return VM_FAULT_SIGBUS;
  127. }
  128. void kvm_arch_free_memslot(struct kvm_memory_slot *free,
  129. struct kvm_memory_slot *dont)
  130. {
  131. }
  132. int kvm_arch_create_memslot(struct kvm_memory_slot *slot, unsigned long npages)
  133. {
  134. return 0;
  135. }
  136. /**
  137. * kvm_arch_destroy_vm - destroy the VM data structure
  138. * @kvm: pointer to the KVM struct
  139. */
  140. void kvm_arch_destroy_vm(struct kvm *kvm)
  141. {
  142. int i;
  143. kvm_free_stage2_pgd(kvm);
  144. for (i = 0; i < KVM_MAX_VCPUS; ++i) {
  145. if (kvm->vcpus[i]) {
  146. kvm_arch_vcpu_free(kvm->vcpus[i]);
  147. kvm->vcpus[i] = NULL;
  148. }
  149. }
  150. }
  151. int kvm_dev_ioctl_check_extension(long ext)
  152. {
  153. int r;
  154. switch (ext) {
  155. case KVM_CAP_IRQCHIP:
  156. r = vgic_present;
  157. break;
  158. case KVM_CAP_USER_MEMORY:
  159. case KVM_CAP_SYNC_MMU:
  160. case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
  161. case KVM_CAP_ONE_REG:
  162. case KVM_CAP_ARM_PSCI:
  163. r = 1;
  164. break;
  165. case KVM_CAP_COALESCED_MMIO:
  166. r = KVM_COALESCED_MMIO_PAGE_OFFSET;
  167. break;
  168. case KVM_CAP_ARM_SET_DEVICE_ADDR:
  169. r = 1;
  170. break;
  171. case KVM_CAP_NR_VCPUS:
  172. r = num_online_cpus();
  173. break;
  174. case KVM_CAP_MAX_VCPUS:
  175. r = KVM_MAX_VCPUS;
  176. break;
  177. default:
  178. r = 0;
  179. break;
  180. }
  181. return r;
  182. }
  183. long kvm_arch_dev_ioctl(struct file *filp,
  184. unsigned int ioctl, unsigned long arg)
  185. {
  186. return -EINVAL;
  187. }
  188. int kvm_arch_set_memory_region(struct kvm *kvm,
  189. struct kvm_userspace_memory_region *mem,
  190. struct kvm_memory_slot old,
  191. int user_alloc)
  192. {
  193. return 0;
  194. }
  195. int kvm_arch_prepare_memory_region(struct kvm *kvm,
  196. struct kvm_memory_slot *memslot,
  197. struct kvm_memory_slot old,
  198. struct kvm_userspace_memory_region *mem,
  199. bool user_alloc)
  200. {
  201. return 0;
  202. }
  203. void kvm_arch_commit_memory_region(struct kvm *kvm,
  204. struct kvm_userspace_memory_region *mem,
  205. struct kvm_memory_slot old,
  206. bool user_alloc)
  207. {
  208. }
  209. void kvm_arch_flush_shadow_all(struct kvm *kvm)
  210. {
  211. }
  212. void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
  213. struct kvm_memory_slot *slot)
  214. {
  215. }
  216. struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
  217. {
  218. int err;
  219. struct kvm_vcpu *vcpu;
  220. vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
  221. if (!vcpu) {
  222. err = -ENOMEM;
  223. goto out;
  224. }
  225. err = kvm_vcpu_init(vcpu, kvm, id);
  226. if (err)
  227. goto free_vcpu;
  228. err = create_hyp_mappings(vcpu, vcpu + 1);
  229. if (err)
  230. goto vcpu_uninit;
  231. return vcpu;
  232. vcpu_uninit:
  233. kvm_vcpu_uninit(vcpu);
  234. free_vcpu:
  235. kmem_cache_free(kvm_vcpu_cache, vcpu);
  236. out:
  237. return ERR_PTR(err);
  238. }
  239. int kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
  240. {
  241. return 0;
  242. }
  243. void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
  244. {
  245. kvm_mmu_free_memory_caches(vcpu);
  246. kvm_timer_vcpu_terminate(vcpu);
  247. kmem_cache_free(kvm_vcpu_cache, vcpu);
  248. }
  249. void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
  250. {
  251. kvm_arch_vcpu_free(vcpu);
  252. }
  253. int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
  254. {
  255. return 0;
  256. }
  257. int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
  258. {
  259. int ret;
  260. /* Force users to call KVM_ARM_VCPU_INIT */
  261. vcpu->arch.target = -1;
  262. /* Set up VGIC */
  263. ret = kvm_vgic_vcpu_init(vcpu);
  264. if (ret)
  265. return ret;
  266. /* Set up the timer */
  267. kvm_timer_vcpu_init(vcpu);
  268. return 0;
  269. }
  270. void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
  271. {
  272. }
  273. void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  274. {
  275. vcpu->cpu = cpu;
  276. vcpu->arch.vfp_host = this_cpu_ptr(kvm_host_vfp_state);
  277. /*
  278. * Check whether this vcpu requires the cache to be flushed on
  279. * this physical CPU. This is a consequence of doing dcache
  280. * operations by set/way on this vcpu. We do it here to be in
  281. * a non-preemptible section.
  282. */
  283. if (cpumask_test_and_clear_cpu(cpu, &vcpu->arch.require_dcache_flush))
  284. flush_cache_all(); /* We'd really want v7_flush_dcache_all() */
  285. kvm_arm_set_running_vcpu(vcpu);
  286. }
  287. void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
  288. {
  289. kvm_arm_set_running_vcpu(NULL);
  290. }
  291. int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
  292. struct kvm_guest_debug *dbg)
  293. {
  294. return -EINVAL;
  295. }
  296. int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
  297. struct kvm_mp_state *mp_state)
  298. {
  299. return -EINVAL;
  300. }
  301. int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
  302. struct kvm_mp_state *mp_state)
  303. {
  304. return -EINVAL;
  305. }
  306. /**
  307. * kvm_arch_vcpu_runnable - determine if the vcpu can be scheduled
  308. * @v: The VCPU pointer
  309. *
  310. * If the guest CPU is not waiting for interrupts or an interrupt line is
  311. * asserted, the CPU is by definition runnable.
  312. */
  313. int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
  314. {
  315. return !!v->arch.irq_lines || kvm_vgic_vcpu_pending_irq(v);
  316. }
  317. /* Just ensure a guest exit from a particular CPU */
  318. static void exit_vm_noop(void *info)
  319. {
  320. }
  321. void force_vm_exit(const cpumask_t *mask)
  322. {
  323. smp_call_function_many(mask, exit_vm_noop, NULL, true);
  324. }
  325. /**
  326. * need_new_vmid_gen - check that the VMID is still valid
  327. * @kvm: The VM's VMID to checkt
  328. *
  329. * return true if there is a new generation of VMIDs being used
  330. *
  331. * The hardware supports only 256 values with the value zero reserved for the
  332. * host, so we check if an assigned value belongs to a previous generation,
  333. * which which requires us to assign a new value. If we're the first to use a
  334. * VMID for the new generation, we must flush necessary caches and TLBs on all
  335. * CPUs.
  336. */
  337. static bool need_new_vmid_gen(struct kvm *kvm)
  338. {
  339. return unlikely(kvm->arch.vmid_gen != atomic64_read(&kvm_vmid_gen));
  340. }
  341. /**
  342. * update_vttbr - Update the VTTBR with a valid VMID before the guest runs
  343. * @kvm The guest that we are about to run
  344. *
  345. * Called from kvm_arch_vcpu_ioctl_run before entering the guest to ensure the
  346. * VM has a valid VMID, otherwise assigns a new one and flushes corresponding
  347. * caches and TLBs.
  348. */
  349. static void update_vttbr(struct kvm *kvm)
  350. {
  351. phys_addr_t pgd_phys;
  352. u64 vmid;
  353. if (!need_new_vmid_gen(kvm))
  354. return;
  355. spin_lock(&kvm_vmid_lock);
  356. /*
  357. * We need to re-check the vmid_gen here to ensure that if another vcpu
  358. * already allocated a valid vmid for this vm, then this vcpu should
  359. * use the same vmid.
  360. */
  361. if (!need_new_vmid_gen(kvm)) {
  362. spin_unlock(&kvm_vmid_lock);
  363. return;
  364. }
  365. /* First user of a new VMID generation? */
  366. if (unlikely(kvm_next_vmid == 0)) {
  367. atomic64_inc(&kvm_vmid_gen);
  368. kvm_next_vmid = 1;
  369. /*
  370. * On SMP we know no other CPUs can use this CPU's or each
  371. * other's VMID after force_vm_exit returns since the
  372. * kvm_vmid_lock blocks them from reentry to the guest.
  373. */
  374. force_vm_exit(cpu_all_mask);
  375. /*
  376. * Now broadcast TLB + ICACHE invalidation over the inner
  377. * shareable domain to make sure all data structures are
  378. * clean.
  379. */
  380. kvm_call_hyp(__kvm_flush_vm_context);
  381. }
  382. kvm->arch.vmid_gen = atomic64_read(&kvm_vmid_gen);
  383. kvm->arch.vmid = kvm_next_vmid;
  384. kvm_next_vmid++;
  385. /* update vttbr to be used with the new vmid */
  386. pgd_phys = virt_to_phys(kvm->arch.pgd);
  387. vmid = ((u64)(kvm->arch.vmid) << VTTBR_VMID_SHIFT) & VTTBR_VMID_MASK;
  388. kvm->arch.vttbr = pgd_phys & VTTBR_BADDR_MASK;
  389. kvm->arch.vttbr |= vmid;
  390. spin_unlock(&kvm_vmid_lock);
  391. }
  392. static int kvm_vcpu_first_run_init(struct kvm_vcpu *vcpu)
  393. {
  394. if (likely(vcpu->arch.has_run_once))
  395. return 0;
  396. vcpu->arch.has_run_once = true;
  397. /*
  398. * Initialize the VGIC before running a vcpu the first time on
  399. * this VM.
  400. */
  401. if (irqchip_in_kernel(vcpu->kvm) &&
  402. unlikely(!vgic_initialized(vcpu->kvm))) {
  403. int ret = kvm_vgic_init(vcpu->kvm);
  404. if (ret)
  405. return ret;
  406. }
  407. /*
  408. * Handle the "start in power-off" case by calling into the
  409. * PSCI code.
  410. */
  411. if (test_and_clear_bit(KVM_ARM_VCPU_POWER_OFF, vcpu->arch.features)) {
  412. *vcpu_reg(vcpu, 0) = KVM_PSCI_FN_CPU_OFF;
  413. kvm_psci_call(vcpu);
  414. }
  415. return 0;
  416. }
  417. static void vcpu_pause(struct kvm_vcpu *vcpu)
  418. {
  419. wait_queue_head_t *wq = kvm_arch_vcpu_wq(vcpu);
  420. wait_event_interruptible(*wq, !vcpu->arch.pause);
  421. }
  422. /**
  423. * kvm_arch_vcpu_ioctl_run - the main VCPU run function to execute guest code
  424. * @vcpu: The VCPU pointer
  425. * @run: The kvm_run structure pointer used for userspace state exchange
  426. *
  427. * This function is called through the VCPU_RUN ioctl called from user space. It
  428. * will execute VM code in a loop until the time slice for the process is used
  429. * or some emulation is needed from user space in which case the function will
  430. * return with return value 0 and with the kvm_run structure filled in with the
  431. * required data for the requested emulation.
  432. */
  433. int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
  434. {
  435. int ret;
  436. sigset_t sigsaved;
  437. /* Make sure they initialize the vcpu with KVM_ARM_VCPU_INIT */
  438. if (unlikely(vcpu->arch.target < 0))
  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. {
  538. u32 irq = irq_level->irq;
  539. unsigned int irq_type, vcpu_idx, irq_num;
  540. int nrcpus = atomic_read(&kvm->online_vcpus);
  541. struct kvm_vcpu *vcpu = NULL;
  542. bool level = irq_level->level;
  543. irq_type = (irq >> KVM_ARM_IRQ_TYPE_SHIFT) & KVM_ARM_IRQ_TYPE_MASK;
  544. vcpu_idx = (irq >> KVM_ARM_IRQ_VCPU_SHIFT) & KVM_ARM_IRQ_VCPU_MASK;
  545. irq_num = (irq >> KVM_ARM_IRQ_NUM_SHIFT) & KVM_ARM_IRQ_NUM_MASK;
  546. trace_kvm_irq_line(irq_type, vcpu_idx, irq_num, irq_level->level);
  547. switch (irq_type) {
  548. case KVM_ARM_IRQ_TYPE_CPU:
  549. if (irqchip_in_kernel(kvm))
  550. return -ENXIO;
  551. if (vcpu_idx >= nrcpus)
  552. return -EINVAL;
  553. vcpu = kvm_get_vcpu(kvm, vcpu_idx);
  554. if (!vcpu)
  555. return -EINVAL;
  556. if (irq_num > KVM_ARM_IRQ_CPU_FIQ)
  557. return -EINVAL;
  558. return vcpu_interrupt_line(vcpu, irq_num, level);
  559. case KVM_ARM_IRQ_TYPE_PPI:
  560. if (!irqchip_in_kernel(kvm))
  561. return -ENXIO;
  562. if (vcpu_idx >= nrcpus)
  563. return -EINVAL;
  564. vcpu = kvm_get_vcpu(kvm, vcpu_idx);
  565. if (!vcpu)
  566. return -EINVAL;
  567. if (irq_num < VGIC_NR_SGIS || irq_num >= VGIC_NR_PRIVATE_IRQS)
  568. return -EINVAL;
  569. return kvm_vgic_inject_irq(kvm, vcpu->vcpu_id, irq_num, level);
  570. case KVM_ARM_IRQ_TYPE_SPI:
  571. if (!irqchip_in_kernel(kvm))
  572. return -ENXIO;
  573. if (irq_num < VGIC_NR_PRIVATE_IRQS ||
  574. irq_num > KVM_ARM_IRQ_GIC_MAX)
  575. return -EINVAL;
  576. return kvm_vgic_inject_irq(kvm, 0, irq_num, level);
  577. }
  578. return -EINVAL;
  579. }
  580. long kvm_arch_vcpu_ioctl(struct file *filp,
  581. unsigned int ioctl, unsigned long arg)
  582. {
  583. struct kvm_vcpu *vcpu = filp->private_data;
  584. void __user *argp = (void __user *)arg;
  585. switch (ioctl) {
  586. case KVM_ARM_VCPU_INIT: {
  587. struct kvm_vcpu_init init;
  588. if (copy_from_user(&init, argp, sizeof(init)))
  589. return -EFAULT;
  590. return kvm_vcpu_set_target(vcpu, &init);
  591. }
  592. case KVM_SET_ONE_REG:
  593. case KVM_GET_ONE_REG: {
  594. struct kvm_one_reg reg;
  595. if (copy_from_user(&reg, argp, sizeof(reg)))
  596. return -EFAULT;
  597. if (ioctl == KVM_SET_ONE_REG)
  598. return kvm_arm_set_reg(vcpu, &reg);
  599. else
  600. return kvm_arm_get_reg(vcpu, &reg);
  601. }
  602. case KVM_GET_REG_LIST: {
  603. struct kvm_reg_list __user *user_list = argp;
  604. struct kvm_reg_list reg_list;
  605. unsigned n;
  606. if (copy_from_user(&reg_list, user_list, sizeof(reg_list)))
  607. return -EFAULT;
  608. n = reg_list.n;
  609. reg_list.n = kvm_arm_num_regs(vcpu);
  610. if (copy_to_user(user_list, &reg_list, sizeof(reg_list)))
  611. return -EFAULT;
  612. if (n < reg_list.n)
  613. return -E2BIG;
  614. return kvm_arm_copy_reg_indices(vcpu, user_list->reg);
  615. }
  616. default:
  617. return -EINVAL;
  618. }
  619. }
  620. int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
  621. {
  622. return -EINVAL;
  623. }
  624. static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm,
  625. struct kvm_arm_device_addr *dev_addr)
  626. {
  627. unsigned long dev_id, type;
  628. dev_id = (dev_addr->id & KVM_ARM_DEVICE_ID_MASK) >>
  629. KVM_ARM_DEVICE_ID_SHIFT;
  630. type = (dev_addr->id & KVM_ARM_DEVICE_TYPE_MASK) >>
  631. KVM_ARM_DEVICE_TYPE_SHIFT;
  632. switch (dev_id) {
  633. case KVM_ARM_DEVICE_VGIC_V2:
  634. if (!vgic_present)
  635. return -ENXIO;
  636. return kvm_vgic_set_addr(kvm, type, dev_addr->addr);
  637. default:
  638. return -ENODEV;
  639. }
  640. }
  641. long kvm_arch_vm_ioctl(struct file *filp,
  642. unsigned int ioctl, unsigned long arg)
  643. {
  644. struct kvm *kvm = filp->private_data;
  645. void __user *argp = (void __user *)arg;
  646. switch (ioctl) {
  647. case KVM_CREATE_IRQCHIP: {
  648. if (vgic_present)
  649. return kvm_vgic_create(kvm);
  650. else
  651. return -ENXIO;
  652. }
  653. case KVM_ARM_SET_DEVICE_ADDR: {
  654. struct kvm_arm_device_addr dev_addr;
  655. if (copy_from_user(&dev_addr, argp, sizeof(dev_addr)))
  656. return -EFAULT;
  657. return kvm_vm_ioctl_set_device_addr(kvm, &dev_addr);
  658. }
  659. default:
  660. return -EINVAL;
  661. }
  662. }
  663. static void cpu_init_hyp_mode(void *vector)
  664. {
  665. unsigned long long pgd_ptr;
  666. unsigned long hyp_stack_ptr;
  667. unsigned long stack_page;
  668. unsigned long vector_ptr;
  669. /* Switch from the HYP stub to our own HYP init vector */
  670. __hyp_set_vectors((unsigned long)vector);
  671. pgd_ptr = (unsigned long long)kvm_mmu_get_httbr();
  672. stack_page = __get_cpu_var(kvm_arm_hyp_stack_page);
  673. hyp_stack_ptr = stack_page + PAGE_SIZE;
  674. vector_ptr = (unsigned long)__kvm_hyp_vector;
  675. __cpu_init_hyp_mode(pgd_ptr, hyp_stack_ptr, vector_ptr);
  676. }
  677. /**
  678. * Inits Hyp-mode on all online CPUs
  679. */
  680. static int init_hyp_mode(void)
  681. {
  682. phys_addr_t init_phys_addr;
  683. int cpu;
  684. int err = 0;
  685. /*
  686. * Allocate Hyp PGD and setup Hyp identity mapping
  687. */
  688. err = kvm_mmu_init();
  689. if (err)
  690. goto out_err;
  691. /*
  692. * It is probably enough to obtain the default on one
  693. * CPU. It's unlikely to be different on the others.
  694. */
  695. hyp_default_vectors = __hyp_get_vectors();
  696. /*
  697. * Allocate stack pages for Hypervisor-mode
  698. */
  699. for_each_possible_cpu(cpu) {
  700. unsigned long stack_page;
  701. stack_page = __get_free_page(GFP_KERNEL);
  702. if (!stack_page) {
  703. err = -ENOMEM;
  704. goto out_free_stack_pages;
  705. }
  706. per_cpu(kvm_arm_hyp_stack_page, cpu) = stack_page;
  707. }
  708. /*
  709. * Execute the init code on each CPU.
  710. *
  711. * Note: The stack is not mapped yet, so don't do anything else than
  712. * initializing the hypervisor mode on each CPU using a local stack
  713. * space for temporary storage.
  714. */
  715. init_phys_addr = virt_to_phys(__kvm_hyp_init);
  716. for_each_online_cpu(cpu) {
  717. smp_call_function_single(cpu, cpu_init_hyp_mode,
  718. (void *)(long)init_phys_addr, 1);
  719. }
  720. /*
  721. * Unmap the identity mapping
  722. */
  723. kvm_clear_hyp_idmap();
  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 VFP structures
  745. */
  746. kvm_host_vfp_state = alloc_percpu(kvm_kernel_vfp_t);
  747. if (!kvm_host_vfp_state) {
  748. err = -ENOMEM;
  749. kvm_err("Cannot allocate host VFP state\n");
  750. goto out_free_mappings;
  751. }
  752. for_each_possible_cpu(cpu) {
  753. kvm_kernel_vfp_t *vfp;
  754. vfp = per_cpu_ptr(kvm_host_vfp_state, cpu);
  755. err = create_hyp_mappings(vfp, vfp + 1);
  756. if (err) {
  757. kvm_err("Cannot map host VFP state: %d\n", err);
  758. goto out_free_vfp;
  759. }
  760. }
  761. /*
  762. * Init HYP view of VGIC
  763. */
  764. err = kvm_vgic_hyp_init();
  765. if (err)
  766. goto out_free_vfp;
  767. #ifdef CONFIG_KVM_ARM_VGIC
  768. vgic_present = true;
  769. #endif
  770. /*
  771. * Init HYP architected timer support
  772. */
  773. err = kvm_timer_hyp_init();
  774. if (err)
  775. goto out_free_mappings;
  776. kvm_info("Hyp mode initialized successfully\n");
  777. return 0;
  778. out_free_vfp:
  779. free_percpu(kvm_host_vfp_state);
  780. out_free_mappings:
  781. free_hyp_pmds();
  782. out_free_stack_pages:
  783. for_each_possible_cpu(cpu)
  784. free_page(per_cpu(kvm_arm_hyp_stack_page, cpu));
  785. out_err:
  786. kvm_err("error initializing Hyp mode: %d\n", err);
  787. return err;
  788. }
  789. /**
  790. * Initialize Hyp-mode and memory mappings on all CPUs.
  791. */
  792. int kvm_arch_init(void *opaque)
  793. {
  794. int err;
  795. if (!is_hyp_mode_available()) {
  796. kvm_err("HYP mode not available\n");
  797. return -ENODEV;
  798. }
  799. if (kvm_target_cpu() < 0) {
  800. kvm_err("Target CPU not supported!\n");
  801. return -ENODEV;
  802. }
  803. err = init_hyp_mode();
  804. if (err)
  805. goto out_err;
  806. kvm_coproc_table_init();
  807. return 0;
  808. out_err:
  809. return err;
  810. }
  811. /* NOP: Compiling as a module not supported */
  812. void kvm_arch_exit(void)
  813. {
  814. }
  815. static int arm_init(void)
  816. {
  817. int rc = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
  818. return rc;
  819. }
  820. module_init(arm_init);