arm.c 23 KB

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