arm.c 27 KB

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