arm.c 24 KB

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