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_DEVICE_CTRL:
  146. case KVM_CAP_USER_MEMORY:
  147. case KVM_CAP_SYNC_MMU:
  148. case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
  149. case KVM_CAP_ONE_REG:
  150. case KVM_CAP_ARM_PSCI:
  151. case KVM_CAP_ARM_PSCI_0_2:
  152. case KVM_CAP_READONLY_MEM:
  153. case KVM_CAP_MP_STATE:
  154. r = 1;
  155. break;
  156. case KVM_CAP_COALESCED_MMIO:
  157. r = KVM_COALESCED_MMIO_PAGE_OFFSET;
  158. break;
  159. case KVM_CAP_ARM_SET_DEVICE_ADDR:
  160. r = 1;
  161. break;
  162. case KVM_CAP_NR_VCPUS:
  163. r = num_online_cpus();
  164. break;
  165. case KVM_CAP_MAX_VCPUS:
  166. r = KVM_MAX_VCPUS;
  167. break;
  168. default:
  169. r = kvm_arch_dev_ioctl_check_extension(ext);
  170. break;
  171. }
  172. return r;
  173. }
  174. long kvm_arch_dev_ioctl(struct file *filp,
  175. unsigned int ioctl, unsigned long arg)
  176. {
  177. return -EINVAL;
  178. }
  179. struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
  180. {
  181. int err;
  182. struct kvm_vcpu *vcpu;
  183. if (irqchip_in_kernel(kvm) && vgic_initialized(kvm)) {
  184. err = -EBUSY;
  185. goto out;
  186. }
  187. if (id >= kvm->arch.max_vcpus) {
  188. err = -EINVAL;
  189. goto out;
  190. }
  191. vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
  192. if (!vcpu) {
  193. err = -ENOMEM;
  194. goto out;
  195. }
  196. err = kvm_vcpu_init(vcpu, kvm, id);
  197. if (err)
  198. goto free_vcpu;
  199. err = create_hyp_mappings(vcpu, vcpu + 1);
  200. if (err)
  201. goto vcpu_uninit;
  202. return vcpu;
  203. vcpu_uninit:
  204. kvm_vcpu_uninit(vcpu);
  205. free_vcpu:
  206. kmem_cache_free(kvm_vcpu_cache, vcpu);
  207. out:
  208. return ERR_PTR(err);
  209. }
  210. void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
  211. {
  212. }
  213. void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
  214. {
  215. kvm_mmu_free_memory_caches(vcpu);
  216. kvm_timer_vcpu_terminate(vcpu);
  217. kvm_vgic_vcpu_destroy(vcpu);
  218. kmem_cache_free(kvm_vcpu_cache, vcpu);
  219. }
  220. void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
  221. {
  222. kvm_arch_vcpu_free(vcpu);
  223. }
  224. int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
  225. {
  226. return kvm_timer_should_fire(vcpu);
  227. }
  228. int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
  229. {
  230. /* Force users to call KVM_ARM_VCPU_INIT */
  231. vcpu->arch.target = -1;
  232. bitmap_zero(vcpu->arch.features, KVM_VCPU_MAX_FEATURES);
  233. /* Set up the timer */
  234. kvm_timer_vcpu_init(vcpu);
  235. return 0;
  236. }
  237. void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  238. {
  239. vcpu->cpu = cpu;
  240. vcpu->arch.host_cpu_context = this_cpu_ptr(kvm_host_cpu_state);
  241. kvm_arm_set_running_vcpu(vcpu);
  242. }
  243. void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
  244. {
  245. /*
  246. * The arch-generic KVM code expects the cpu field of a vcpu to be -1
  247. * if the vcpu is no longer assigned to a cpu. This is used for the
  248. * optimized make_all_cpus_request path.
  249. */
  250. vcpu->cpu = -1;
  251. kvm_arm_set_running_vcpu(NULL);
  252. }
  253. int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
  254. struct kvm_guest_debug *dbg)
  255. {
  256. return -EINVAL;
  257. }
  258. int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
  259. struct kvm_mp_state *mp_state)
  260. {
  261. if (vcpu->arch.pause)
  262. mp_state->mp_state = KVM_MP_STATE_STOPPED;
  263. else
  264. mp_state->mp_state = KVM_MP_STATE_RUNNABLE;
  265. return 0;
  266. }
  267. int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
  268. struct kvm_mp_state *mp_state)
  269. {
  270. switch (mp_state->mp_state) {
  271. case KVM_MP_STATE_RUNNABLE:
  272. vcpu->arch.pause = false;
  273. break;
  274. case KVM_MP_STATE_STOPPED:
  275. vcpu->arch.pause = true;
  276. break;
  277. default:
  278. return -EINVAL;
  279. }
  280. return 0;
  281. }
  282. /**
  283. * kvm_arch_vcpu_runnable - determine if the vcpu can be scheduled
  284. * @v: The VCPU pointer
  285. *
  286. * If the guest CPU is not waiting for interrupts or an interrupt line is
  287. * asserted, the CPU is by definition runnable.
  288. */
  289. int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
  290. {
  291. return !!v->arch.irq_lines || kvm_vgic_vcpu_pending_irq(v);
  292. }
  293. /* Just ensure a guest exit from a particular CPU */
  294. static void exit_vm_noop(void *info)
  295. {
  296. }
  297. void force_vm_exit(const cpumask_t *mask)
  298. {
  299. smp_call_function_many(mask, exit_vm_noop, NULL, true);
  300. }
  301. /**
  302. * need_new_vmid_gen - check that the VMID is still valid
  303. * @kvm: The VM's VMID to checkt
  304. *
  305. * return true if there is a new generation of VMIDs being used
  306. *
  307. * The hardware supports only 256 values with the value zero reserved for the
  308. * host, so we check if an assigned value belongs to a previous generation,
  309. * which which requires us to assign a new value. If we're the first to use a
  310. * VMID for the new generation, we must flush necessary caches and TLBs on all
  311. * CPUs.
  312. */
  313. static bool need_new_vmid_gen(struct kvm *kvm)
  314. {
  315. return unlikely(kvm->arch.vmid_gen != atomic64_read(&kvm_vmid_gen));
  316. }
  317. /**
  318. * update_vttbr - Update the VTTBR with a valid VMID before the guest runs
  319. * @kvm The guest that we are about to run
  320. *
  321. * Called from kvm_arch_vcpu_ioctl_run before entering the guest to ensure the
  322. * VM has a valid VMID, otherwise assigns a new one and flushes corresponding
  323. * caches and TLBs.
  324. */
  325. static void update_vttbr(struct kvm *kvm)
  326. {
  327. phys_addr_t pgd_phys;
  328. u64 vmid;
  329. if (!need_new_vmid_gen(kvm))
  330. return;
  331. spin_lock(&kvm_vmid_lock);
  332. /*
  333. * We need to re-check the vmid_gen here to ensure that if another vcpu
  334. * already allocated a valid vmid for this vm, then this vcpu should
  335. * use the same vmid.
  336. */
  337. if (!need_new_vmid_gen(kvm)) {
  338. spin_unlock(&kvm_vmid_lock);
  339. return;
  340. }
  341. /* First user of a new VMID generation? */
  342. if (unlikely(kvm_next_vmid == 0)) {
  343. atomic64_inc(&kvm_vmid_gen);
  344. kvm_next_vmid = 1;
  345. /*
  346. * On SMP we know no other CPUs can use this CPU's or each
  347. * other's VMID after force_vm_exit returns since the
  348. * kvm_vmid_lock blocks them from reentry to the guest.
  349. */
  350. force_vm_exit(cpu_all_mask);
  351. /*
  352. * Now broadcast TLB + ICACHE invalidation over the inner
  353. * shareable domain to make sure all data structures are
  354. * clean.
  355. */
  356. kvm_call_hyp(__kvm_flush_vm_context);
  357. }
  358. kvm->arch.vmid_gen = atomic64_read(&kvm_vmid_gen);
  359. kvm->arch.vmid = kvm_next_vmid;
  360. kvm_next_vmid++;
  361. /* update vttbr to be used with the new vmid */
  362. pgd_phys = virt_to_phys(kvm_get_hwpgd(kvm));
  363. BUG_ON(pgd_phys & ~VTTBR_BADDR_MASK);
  364. vmid = ((u64)(kvm->arch.vmid) << VTTBR_VMID_SHIFT) & VTTBR_VMID_MASK;
  365. kvm->arch.vttbr = pgd_phys | vmid;
  366. spin_unlock(&kvm_vmid_lock);
  367. }
  368. static int kvm_vcpu_first_run_init(struct kvm_vcpu *vcpu)
  369. {
  370. struct kvm *kvm = vcpu->kvm;
  371. int ret;
  372. if (likely(vcpu->arch.has_run_once))
  373. return 0;
  374. vcpu->arch.has_run_once = true;
  375. /*
  376. * Map the VGIC hardware resources before running a vcpu the first
  377. * time on this VM.
  378. */
  379. if (unlikely(!vgic_ready(kvm))) {
  380. ret = kvm_vgic_map_resources(kvm);
  381. if (ret)
  382. return ret;
  383. }
  384. /*
  385. * Enable the arch timers only if we have an in-kernel VGIC
  386. * and it has been properly initialized, since we cannot handle
  387. * interrupts from the virtual timer with a userspace gic.
  388. */
  389. if (irqchip_in_kernel(kvm) && vgic_initialized(kvm))
  390. kvm_timer_enable(kvm);
  391. return 0;
  392. }
  393. bool kvm_arch_intc_initialized(struct kvm *kvm)
  394. {
  395. return vgic_initialized(kvm);
  396. }
  397. static void vcpu_pause(struct kvm_vcpu *vcpu)
  398. {
  399. wait_queue_head_t *wq = kvm_arch_vcpu_wq(vcpu);
  400. wait_event_interruptible(*wq, !vcpu->arch.pause);
  401. }
  402. static int kvm_vcpu_initialized(struct kvm_vcpu *vcpu)
  403. {
  404. return vcpu->arch.target >= 0;
  405. }
  406. /**
  407. * kvm_arch_vcpu_ioctl_run - the main VCPU run function to execute guest code
  408. * @vcpu: The VCPU pointer
  409. * @run: The kvm_run structure pointer used for userspace state exchange
  410. *
  411. * This function is called through the VCPU_RUN ioctl called from user space. It
  412. * will execute VM code in a loop until the time slice for the process is used
  413. * or some emulation is needed from user space in which case the function will
  414. * return with return value 0 and with the kvm_run structure filled in with the
  415. * required data for the requested emulation.
  416. */
  417. int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
  418. {
  419. int ret;
  420. sigset_t sigsaved;
  421. if (unlikely(!kvm_vcpu_initialized(vcpu)))
  422. return -ENOEXEC;
  423. ret = kvm_vcpu_first_run_init(vcpu);
  424. if (ret)
  425. return ret;
  426. if (run->exit_reason == KVM_EXIT_MMIO) {
  427. ret = kvm_handle_mmio_return(vcpu, vcpu->run);
  428. if (ret)
  429. return ret;
  430. }
  431. if (vcpu->sigset_active)
  432. sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
  433. ret = 1;
  434. run->exit_reason = KVM_EXIT_UNKNOWN;
  435. while (ret > 0) {
  436. /*
  437. * Check conditions before entering the guest
  438. */
  439. cond_resched();
  440. update_vttbr(vcpu->kvm);
  441. if (vcpu->arch.pause)
  442. vcpu_pause(vcpu);
  443. kvm_vgic_flush_hwstate(vcpu);
  444. kvm_timer_flush_hwstate(vcpu);
  445. local_irq_disable();
  446. /*
  447. * Re-check atomic conditions
  448. */
  449. if (signal_pending(current)) {
  450. ret = -EINTR;
  451. run->exit_reason = KVM_EXIT_INTR;
  452. }
  453. if (ret <= 0 || need_new_vmid_gen(vcpu->kvm)) {
  454. local_irq_enable();
  455. kvm_timer_sync_hwstate(vcpu);
  456. kvm_vgic_sync_hwstate(vcpu);
  457. continue;
  458. }
  459. /**************************************************************
  460. * Enter the guest
  461. */
  462. trace_kvm_entry(*vcpu_pc(vcpu));
  463. kvm_guest_enter();
  464. vcpu->mode = IN_GUEST_MODE;
  465. ret = kvm_call_hyp(__kvm_vcpu_run, vcpu);
  466. vcpu->mode = OUTSIDE_GUEST_MODE;
  467. kvm_guest_exit();
  468. trace_kvm_exit(kvm_vcpu_trap_get_class(vcpu), *vcpu_pc(vcpu));
  469. /*
  470. * We may have taken a host interrupt in HYP mode (ie
  471. * while executing the guest). This interrupt is still
  472. * pending, as we haven't serviced it yet!
  473. *
  474. * We're now back in SVC mode, with interrupts
  475. * disabled. Enabling the interrupts now will have
  476. * the effect of taking the interrupt again, in SVC
  477. * mode this time.
  478. */
  479. local_irq_enable();
  480. /*
  481. * Back from guest
  482. *************************************************************/
  483. kvm_timer_sync_hwstate(vcpu);
  484. kvm_vgic_sync_hwstate(vcpu);
  485. ret = handle_exit(vcpu, run, ret);
  486. }
  487. if (vcpu->sigset_active)
  488. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  489. return ret;
  490. }
  491. static int vcpu_interrupt_line(struct kvm_vcpu *vcpu, int number, bool level)
  492. {
  493. int bit_index;
  494. bool set;
  495. unsigned long *ptr;
  496. if (number == KVM_ARM_IRQ_CPU_IRQ)
  497. bit_index = __ffs(HCR_VI);
  498. else /* KVM_ARM_IRQ_CPU_FIQ */
  499. bit_index = __ffs(HCR_VF);
  500. ptr = (unsigned long *)&vcpu->arch.irq_lines;
  501. if (level)
  502. set = test_and_set_bit(bit_index, ptr);
  503. else
  504. set = test_and_clear_bit(bit_index, ptr);
  505. /*
  506. * If we didn't change anything, no need to wake up or kick other CPUs
  507. */
  508. if (set == level)
  509. return 0;
  510. /*
  511. * The vcpu irq_lines field was updated, wake up sleeping VCPUs and
  512. * trigger a world-switch round on the running physical CPU to set the
  513. * virtual IRQ/FIQ fields in the HCR appropriately.
  514. */
  515. kvm_vcpu_kick(vcpu);
  516. return 0;
  517. }
  518. int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level,
  519. bool line_status)
  520. {
  521. u32 irq = irq_level->irq;
  522. unsigned int irq_type, vcpu_idx, irq_num;
  523. int nrcpus = atomic_read(&kvm->online_vcpus);
  524. struct kvm_vcpu *vcpu = NULL;
  525. bool level = irq_level->level;
  526. irq_type = (irq >> KVM_ARM_IRQ_TYPE_SHIFT) & KVM_ARM_IRQ_TYPE_MASK;
  527. vcpu_idx = (irq >> KVM_ARM_IRQ_VCPU_SHIFT) & KVM_ARM_IRQ_VCPU_MASK;
  528. irq_num = (irq >> KVM_ARM_IRQ_NUM_SHIFT) & KVM_ARM_IRQ_NUM_MASK;
  529. trace_kvm_irq_line(irq_type, vcpu_idx, irq_num, irq_level->level);
  530. switch (irq_type) {
  531. case KVM_ARM_IRQ_TYPE_CPU:
  532. if (irqchip_in_kernel(kvm))
  533. return -ENXIO;
  534. if (vcpu_idx >= nrcpus)
  535. return -EINVAL;
  536. vcpu = kvm_get_vcpu(kvm, vcpu_idx);
  537. if (!vcpu)
  538. return -EINVAL;
  539. if (irq_num > KVM_ARM_IRQ_CPU_FIQ)
  540. return -EINVAL;
  541. return vcpu_interrupt_line(vcpu, irq_num, level);
  542. case KVM_ARM_IRQ_TYPE_PPI:
  543. if (!irqchip_in_kernel(kvm))
  544. return -ENXIO;
  545. if (vcpu_idx >= nrcpus)
  546. return -EINVAL;
  547. vcpu = kvm_get_vcpu(kvm, vcpu_idx);
  548. if (!vcpu)
  549. return -EINVAL;
  550. if (irq_num < VGIC_NR_SGIS || irq_num >= VGIC_NR_PRIVATE_IRQS)
  551. return -EINVAL;
  552. return kvm_vgic_inject_irq(kvm, vcpu->vcpu_id, irq_num, level);
  553. case KVM_ARM_IRQ_TYPE_SPI:
  554. if (!irqchip_in_kernel(kvm))
  555. return -ENXIO;
  556. if (irq_num < VGIC_NR_PRIVATE_IRQS ||
  557. irq_num > KVM_ARM_IRQ_GIC_MAX)
  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);