arm.c 26 KB

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