arm.c 23 KB

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