arm.c 27 KB

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