arm.c 28 KB

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