arm.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005
  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. /**
  416. * kvm_arch_vcpu_ioctl_run - the main VCPU run function to execute guest code
  417. * @vcpu: The VCPU pointer
  418. * @run: The kvm_run structure pointer used for userspace state exchange
  419. *
  420. * This function is called through the VCPU_RUN ioctl called from user space. It
  421. * will execute VM code in a loop until the time slice for the process is used
  422. * or some emulation is needed from user space in which case the function will
  423. * return with return value 0 and with the kvm_run structure filled in with the
  424. * required data for the requested emulation.
  425. */
  426. int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
  427. {
  428. int ret;
  429. sigset_t sigsaved;
  430. /* Make sure they initialize the vcpu with KVM_ARM_VCPU_INIT */
  431. if (unlikely(vcpu->arch.target < 0))
  432. return -ENOEXEC;
  433. ret = kvm_vcpu_first_run_init(vcpu);
  434. if (ret)
  435. return ret;
  436. if (run->exit_reason == KVM_EXIT_MMIO) {
  437. ret = kvm_handle_mmio_return(vcpu, vcpu->run);
  438. if (ret)
  439. return ret;
  440. }
  441. if (vcpu->sigset_active)
  442. sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
  443. ret = 1;
  444. run->exit_reason = KVM_EXIT_UNKNOWN;
  445. while (ret > 0) {
  446. /*
  447. * Check conditions before entering the guest
  448. */
  449. cond_resched();
  450. update_vttbr(vcpu->kvm);
  451. if (vcpu->arch.pause)
  452. vcpu_pause(vcpu);
  453. kvm_vgic_flush_hwstate(vcpu);
  454. kvm_timer_flush_hwstate(vcpu);
  455. local_irq_disable();
  456. /*
  457. * Re-check atomic conditions
  458. */
  459. if (signal_pending(current)) {
  460. ret = -EINTR;
  461. run->exit_reason = KVM_EXIT_INTR;
  462. }
  463. if (ret <= 0 || need_new_vmid_gen(vcpu->kvm)) {
  464. local_irq_enable();
  465. kvm_timer_sync_hwstate(vcpu);
  466. kvm_vgic_sync_hwstate(vcpu);
  467. continue;
  468. }
  469. /**************************************************************
  470. * Enter the guest
  471. */
  472. trace_kvm_entry(*vcpu_pc(vcpu));
  473. kvm_guest_enter();
  474. vcpu->mode = IN_GUEST_MODE;
  475. ret = kvm_call_hyp(__kvm_vcpu_run, vcpu);
  476. vcpu->mode = OUTSIDE_GUEST_MODE;
  477. vcpu->arch.last_pcpu = smp_processor_id();
  478. kvm_guest_exit();
  479. trace_kvm_exit(*vcpu_pc(vcpu));
  480. /*
  481. * We may have taken a host interrupt in HYP mode (ie
  482. * while executing the guest). This interrupt is still
  483. * pending, as we haven't serviced it yet!
  484. *
  485. * We're now back in SVC mode, with interrupts
  486. * disabled. Enabling the interrupts now will have
  487. * the effect of taking the interrupt again, in SVC
  488. * mode this time.
  489. */
  490. local_irq_enable();
  491. /*
  492. * Back from guest
  493. *************************************************************/
  494. kvm_timer_sync_hwstate(vcpu);
  495. kvm_vgic_sync_hwstate(vcpu);
  496. ret = handle_exit(vcpu, run, ret);
  497. }
  498. if (vcpu->sigset_active)
  499. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  500. return ret;
  501. }
  502. static int vcpu_interrupt_line(struct kvm_vcpu *vcpu, int number, bool level)
  503. {
  504. int bit_index;
  505. bool set;
  506. unsigned long *ptr;
  507. if (number == KVM_ARM_IRQ_CPU_IRQ)
  508. bit_index = __ffs(HCR_VI);
  509. else /* KVM_ARM_IRQ_CPU_FIQ */
  510. bit_index = __ffs(HCR_VF);
  511. ptr = (unsigned long *)&vcpu->arch.irq_lines;
  512. if (level)
  513. set = test_and_set_bit(bit_index, ptr);
  514. else
  515. set = test_and_clear_bit(bit_index, ptr);
  516. /*
  517. * If we didn't change anything, no need to wake up or kick other CPUs
  518. */
  519. if (set == level)
  520. return 0;
  521. /*
  522. * The vcpu irq_lines field was updated, wake up sleeping VCPUs and
  523. * trigger a world-switch round on the running physical CPU to set the
  524. * virtual IRQ/FIQ fields in the HCR appropriately.
  525. */
  526. kvm_vcpu_kick(vcpu);
  527. return 0;
  528. }
  529. int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level,
  530. bool line_status)
  531. {
  532. u32 irq = irq_level->irq;
  533. unsigned int irq_type, vcpu_idx, irq_num;
  534. int nrcpus = atomic_read(&kvm->online_vcpus);
  535. struct kvm_vcpu *vcpu = NULL;
  536. bool level = irq_level->level;
  537. irq_type = (irq >> KVM_ARM_IRQ_TYPE_SHIFT) & KVM_ARM_IRQ_TYPE_MASK;
  538. vcpu_idx = (irq >> KVM_ARM_IRQ_VCPU_SHIFT) & KVM_ARM_IRQ_VCPU_MASK;
  539. irq_num = (irq >> KVM_ARM_IRQ_NUM_SHIFT) & KVM_ARM_IRQ_NUM_MASK;
  540. trace_kvm_irq_line(irq_type, vcpu_idx, irq_num, irq_level->level);
  541. switch (irq_type) {
  542. case KVM_ARM_IRQ_TYPE_CPU:
  543. if (irqchip_in_kernel(kvm))
  544. return -ENXIO;
  545. if (vcpu_idx >= nrcpus)
  546. return -EINVAL;
  547. vcpu = kvm_get_vcpu(kvm, vcpu_idx);
  548. if (!vcpu)
  549. return -EINVAL;
  550. if (irq_num > KVM_ARM_IRQ_CPU_FIQ)
  551. return -EINVAL;
  552. return vcpu_interrupt_line(vcpu, irq_num, level);
  553. case KVM_ARM_IRQ_TYPE_PPI:
  554. if (!irqchip_in_kernel(kvm))
  555. return -ENXIO;
  556. if (vcpu_idx >= nrcpus)
  557. return -EINVAL;
  558. vcpu = kvm_get_vcpu(kvm, vcpu_idx);
  559. if (!vcpu)
  560. return -EINVAL;
  561. if (irq_num < VGIC_NR_SGIS || irq_num >= VGIC_NR_PRIVATE_IRQS)
  562. return -EINVAL;
  563. return kvm_vgic_inject_irq(kvm, vcpu->vcpu_id, irq_num, level);
  564. case KVM_ARM_IRQ_TYPE_SPI:
  565. if (!irqchip_in_kernel(kvm))
  566. return -ENXIO;
  567. if (irq_num < VGIC_NR_PRIVATE_IRQS ||
  568. irq_num > KVM_ARM_IRQ_GIC_MAX)
  569. return -EINVAL;
  570. return kvm_vgic_inject_irq(kvm, 0, irq_num, level);
  571. }
  572. return -EINVAL;
  573. }
  574. long kvm_arch_vcpu_ioctl(struct file *filp,
  575. unsigned int ioctl, unsigned long arg)
  576. {
  577. struct kvm_vcpu *vcpu = filp->private_data;
  578. void __user *argp = (void __user *)arg;
  579. switch (ioctl) {
  580. case KVM_ARM_VCPU_INIT: {
  581. struct kvm_vcpu_init init;
  582. if (copy_from_user(&init, argp, sizeof(init)))
  583. return -EFAULT;
  584. return kvm_vcpu_set_target(vcpu, &init);
  585. }
  586. case KVM_SET_ONE_REG:
  587. case KVM_GET_ONE_REG: {
  588. struct kvm_one_reg reg;
  589. if (copy_from_user(&reg, argp, sizeof(reg)))
  590. return -EFAULT;
  591. if (ioctl == KVM_SET_ONE_REG)
  592. return kvm_arm_set_reg(vcpu, &reg);
  593. else
  594. return kvm_arm_get_reg(vcpu, &reg);
  595. }
  596. case KVM_GET_REG_LIST: {
  597. struct kvm_reg_list __user *user_list = argp;
  598. struct kvm_reg_list reg_list;
  599. unsigned n;
  600. if (copy_from_user(&reg_list, user_list, sizeof(reg_list)))
  601. return -EFAULT;
  602. n = reg_list.n;
  603. reg_list.n = kvm_arm_num_regs(vcpu);
  604. if (copy_to_user(user_list, &reg_list, sizeof(reg_list)))
  605. return -EFAULT;
  606. if (n < reg_list.n)
  607. return -E2BIG;
  608. return kvm_arm_copy_reg_indices(vcpu, user_list->reg);
  609. }
  610. default:
  611. return -EINVAL;
  612. }
  613. }
  614. int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
  615. {
  616. return -EINVAL;
  617. }
  618. static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm,
  619. struct kvm_arm_device_addr *dev_addr)
  620. {
  621. unsigned long dev_id, type;
  622. dev_id = (dev_addr->id & KVM_ARM_DEVICE_ID_MASK) >>
  623. KVM_ARM_DEVICE_ID_SHIFT;
  624. type = (dev_addr->id & KVM_ARM_DEVICE_TYPE_MASK) >>
  625. KVM_ARM_DEVICE_TYPE_SHIFT;
  626. switch (dev_id) {
  627. case KVM_ARM_DEVICE_VGIC_V2:
  628. if (!vgic_present)
  629. return -ENXIO;
  630. return kvm_vgic_set_addr(kvm, type, dev_addr->addr);
  631. default:
  632. return -ENODEV;
  633. }
  634. }
  635. long kvm_arch_vm_ioctl(struct file *filp,
  636. unsigned int ioctl, unsigned long arg)
  637. {
  638. struct kvm *kvm = filp->private_data;
  639. void __user *argp = (void __user *)arg;
  640. switch (ioctl) {
  641. case KVM_CREATE_IRQCHIP: {
  642. if (vgic_present)
  643. return kvm_vgic_create(kvm);
  644. else
  645. return -ENXIO;
  646. }
  647. case KVM_ARM_SET_DEVICE_ADDR: {
  648. struct kvm_arm_device_addr dev_addr;
  649. if (copy_from_user(&dev_addr, argp, sizeof(dev_addr)))
  650. return -EFAULT;
  651. return kvm_vm_ioctl_set_device_addr(kvm, &dev_addr);
  652. }
  653. default:
  654. return -EINVAL;
  655. }
  656. }
  657. static void cpu_init_hyp_mode(void *dummy)
  658. {
  659. phys_addr_t boot_pgd_ptr;
  660. phys_addr_t pgd_ptr;
  661. unsigned long hyp_stack_ptr;
  662. unsigned long stack_page;
  663. unsigned long vector_ptr;
  664. /* Switch from the HYP stub to our own HYP init vector */
  665. __hyp_set_vectors(kvm_get_idmap_vector());
  666. boot_pgd_ptr = kvm_mmu_get_boot_httbr();
  667. pgd_ptr = kvm_mmu_get_httbr();
  668. stack_page = __get_cpu_var(kvm_arm_hyp_stack_page);
  669. hyp_stack_ptr = stack_page + PAGE_SIZE;
  670. vector_ptr = (unsigned long)__kvm_hyp_vector;
  671. __cpu_init_hyp_mode(boot_pgd_ptr, pgd_ptr, hyp_stack_ptr, vector_ptr);
  672. }
  673. static int hyp_init_cpu_notify(struct notifier_block *self,
  674. unsigned long action, void *cpu)
  675. {
  676. switch (action) {
  677. case CPU_STARTING:
  678. case CPU_STARTING_FROZEN:
  679. cpu_init_hyp_mode(NULL);
  680. break;
  681. }
  682. return NOTIFY_OK;
  683. }
  684. static struct notifier_block hyp_init_cpu_nb = {
  685. .notifier_call = hyp_init_cpu_notify,
  686. };
  687. /**
  688. * Inits Hyp-mode on all online CPUs
  689. */
  690. static int init_hyp_mode(void)
  691. {
  692. int cpu;
  693. int err = 0;
  694. /*
  695. * Allocate Hyp PGD and setup Hyp identity mapping
  696. */
  697. err = kvm_mmu_init();
  698. if (err)
  699. goto out_err;
  700. /*
  701. * It is probably enough to obtain the default on one
  702. * CPU. It's unlikely to be different on the others.
  703. */
  704. hyp_default_vectors = __hyp_get_vectors();
  705. /*
  706. * Allocate stack pages for Hypervisor-mode
  707. */
  708. for_each_possible_cpu(cpu) {
  709. unsigned long stack_page;
  710. stack_page = __get_free_page(GFP_KERNEL);
  711. if (!stack_page) {
  712. err = -ENOMEM;
  713. goto out_free_stack_pages;
  714. }
  715. per_cpu(kvm_arm_hyp_stack_page, cpu) = stack_page;
  716. }
  717. /*
  718. * Map the Hyp-code called directly from the host
  719. */
  720. err = create_hyp_mappings(__kvm_hyp_code_start, __kvm_hyp_code_end);
  721. if (err) {
  722. kvm_err("Cannot map world-switch code\n");
  723. goto out_free_mappings;
  724. }
  725. /*
  726. * Map the Hyp stack pages
  727. */
  728. for_each_possible_cpu(cpu) {
  729. char *stack_page = (char *)per_cpu(kvm_arm_hyp_stack_page, cpu);
  730. err = create_hyp_mappings(stack_page, stack_page + PAGE_SIZE);
  731. if (err) {
  732. kvm_err("Cannot map hyp stack\n");
  733. goto out_free_mappings;
  734. }
  735. }
  736. /*
  737. * Map the host CPU structures
  738. */
  739. kvm_host_cpu_state = alloc_percpu(kvm_cpu_context_t);
  740. if (!kvm_host_cpu_state) {
  741. err = -ENOMEM;
  742. kvm_err("Cannot allocate host CPU state\n");
  743. goto out_free_mappings;
  744. }
  745. for_each_possible_cpu(cpu) {
  746. kvm_cpu_context_t *cpu_ctxt;
  747. cpu_ctxt = per_cpu_ptr(kvm_host_cpu_state, cpu);
  748. err = create_hyp_mappings(cpu_ctxt, cpu_ctxt + 1);
  749. if (err) {
  750. kvm_err("Cannot map host CPU state: %d\n", err);
  751. goto out_free_context;
  752. }
  753. }
  754. /*
  755. * Execute the init code on each CPU.
  756. */
  757. on_each_cpu(cpu_init_hyp_mode, NULL, 1);
  758. /*
  759. * Init HYP view of VGIC
  760. */
  761. err = kvm_vgic_hyp_init();
  762. if (err)
  763. goto out_free_context;
  764. #ifdef CONFIG_KVM_ARM_VGIC
  765. vgic_present = true;
  766. #endif
  767. /*
  768. * Init HYP architected timer support
  769. */
  770. err = kvm_timer_hyp_init();
  771. if (err)
  772. goto out_free_mappings;
  773. #ifndef CONFIG_HOTPLUG_CPU
  774. free_boot_hyp_pgd();
  775. #endif
  776. kvm_perf_init();
  777. kvm_info("Hyp mode initialized successfully\n");
  778. return 0;
  779. out_free_context:
  780. free_percpu(kvm_host_cpu_state);
  781. out_free_mappings:
  782. free_hyp_pgds();
  783. out_free_stack_pages:
  784. for_each_possible_cpu(cpu)
  785. free_page(per_cpu(kvm_arm_hyp_stack_page, cpu));
  786. out_err:
  787. kvm_err("error initializing Hyp mode: %d\n", err);
  788. return err;
  789. }
  790. static void check_kvm_target_cpu(void *ret)
  791. {
  792. *(int *)ret = kvm_target_cpu();
  793. }
  794. /**
  795. * Initialize Hyp-mode and memory mappings on all CPUs.
  796. */
  797. int kvm_arch_init(void *opaque)
  798. {
  799. int err;
  800. int ret, cpu;
  801. if (!is_hyp_mode_available()) {
  802. kvm_err("HYP mode not available\n");
  803. return -ENODEV;
  804. }
  805. for_each_online_cpu(cpu) {
  806. smp_call_function_single(cpu, check_kvm_target_cpu, &ret, 1);
  807. if (ret < 0) {
  808. kvm_err("Error, CPU %d not supported!\n", cpu);
  809. return -ENODEV;
  810. }
  811. }
  812. err = init_hyp_mode();
  813. if (err)
  814. goto out_err;
  815. err = register_cpu_notifier(&hyp_init_cpu_nb);
  816. if (err) {
  817. kvm_err("Cannot register HYP init CPU notifier (%d)\n", err);
  818. goto out_err;
  819. }
  820. kvm_coproc_table_init();
  821. return 0;
  822. out_err:
  823. return err;
  824. }
  825. /* NOP: Compiling as a module not supported */
  826. void kvm_arch_exit(void)
  827. {
  828. kvm_perf_teardown();
  829. }
  830. static int arm_init(void)
  831. {
  832. int rc = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
  833. return rc;
  834. }
  835. module_init(arm_init);