arm.c 25 KB

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