arm.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064
  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 bool vgic_present;
  57. static void kvm_arm_set_running_vcpu(struct kvm_vcpu *vcpu)
  58. {
  59. BUG_ON(preemptible());
  60. __this_cpu_write(kvm_arm_running_vcpu, vcpu);
  61. }
  62. /**
  63. * kvm_arm_get_running_vcpu - get the vcpu running on the current CPU.
  64. * Must be called from non-preemptible context
  65. */
  66. struct kvm_vcpu *kvm_arm_get_running_vcpu(void)
  67. {
  68. BUG_ON(preemptible());
  69. return __this_cpu_read(kvm_arm_running_vcpu);
  70. }
  71. /**
  72. * kvm_arm_get_running_vcpus - get the per-CPU array of currently running vcpus.
  73. */
  74. struct kvm_vcpu __percpu **kvm_get_running_vcpus(void)
  75. {
  76. return &kvm_arm_running_vcpu;
  77. }
  78. int kvm_arch_hardware_enable(void *garbage)
  79. {
  80. return 0;
  81. }
  82. int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
  83. {
  84. return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
  85. }
  86. void kvm_arch_hardware_disable(void *garbage)
  87. {
  88. }
  89. int kvm_arch_hardware_setup(void)
  90. {
  91. return 0;
  92. }
  93. void kvm_arch_hardware_unsetup(void)
  94. {
  95. }
  96. void kvm_arch_check_processor_compat(void *rtn)
  97. {
  98. *(int *)rtn = 0;
  99. }
  100. void kvm_arch_sync_events(struct kvm *kvm)
  101. {
  102. }
  103. /**
  104. * kvm_arch_init_vm - initializes a VM data structure
  105. * @kvm: pointer to the KVM struct
  106. */
  107. int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
  108. {
  109. int ret = 0;
  110. if (type)
  111. return -EINVAL;
  112. ret = kvm_alloc_stage2_pgd(kvm);
  113. if (ret)
  114. goto out_fail_alloc;
  115. ret = create_hyp_mappings(kvm, kvm + 1);
  116. if (ret)
  117. goto out_free_stage2_pgd;
  118. /* Mark the initial VMID generation invalid */
  119. kvm->arch.vmid_gen = 0;
  120. return ret;
  121. out_free_stage2_pgd:
  122. kvm_free_stage2_pgd(kvm);
  123. out_fail_alloc:
  124. return ret;
  125. }
  126. int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
  127. {
  128. return VM_FAULT_SIGBUS;
  129. }
  130. void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free,
  131. struct kvm_memory_slot *dont)
  132. {
  133. }
  134. int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
  135. unsigned long npages)
  136. {
  137. return 0;
  138. }
  139. /**
  140. * kvm_arch_destroy_vm - destroy the VM data structure
  141. * @kvm: pointer to the KVM struct
  142. */
  143. void kvm_arch_destroy_vm(struct kvm *kvm)
  144. {
  145. int i;
  146. kvm_free_stage2_pgd(kvm);
  147. for (i = 0; i < KVM_MAX_VCPUS; ++i) {
  148. if (kvm->vcpus[i]) {
  149. kvm_arch_vcpu_free(kvm->vcpus[i]);
  150. kvm->vcpus[i] = NULL;
  151. }
  152. }
  153. }
  154. int kvm_dev_ioctl_check_extension(long ext)
  155. {
  156. int r;
  157. switch (ext) {
  158. case KVM_CAP_IRQCHIP:
  159. r = vgic_present;
  160. break;
  161. case KVM_CAP_USER_MEMORY:
  162. case KVM_CAP_SYNC_MMU:
  163. case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
  164. case KVM_CAP_ONE_REG:
  165. case KVM_CAP_ARM_PSCI:
  166. r = 1;
  167. break;
  168. case KVM_CAP_COALESCED_MMIO:
  169. r = KVM_COALESCED_MMIO_PAGE_OFFSET;
  170. break;
  171. case KVM_CAP_ARM_SET_DEVICE_ADDR:
  172. r = 1;
  173. break;
  174. case KVM_CAP_NR_VCPUS:
  175. r = num_online_cpus();
  176. break;
  177. case KVM_CAP_MAX_VCPUS:
  178. r = KVM_MAX_VCPUS;
  179. break;
  180. default:
  181. r = kvm_arch_dev_ioctl_check_extension(ext);
  182. break;
  183. }
  184. return r;
  185. }
  186. long kvm_arch_dev_ioctl(struct file *filp,
  187. unsigned int ioctl, unsigned long arg)
  188. {
  189. return -EINVAL;
  190. }
  191. void kvm_arch_memslots_updated(struct kvm *kvm)
  192. {
  193. }
  194. int kvm_arch_prepare_memory_region(struct kvm *kvm,
  195. struct kvm_memory_slot *memslot,
  196. struct kvm_userspace_memory_region *mem,
  197. enum kvm_mr_change change)
  198. {
  199. return 0;
  200. }
  201. void kvm_arch_commit_memory_region(struct kvm *kvm,
  202. struct kvm_userspace_memory_region *mem,
  203. const struct kvm_memory_slot *old,
  204. enum kvm_mr_change change)
  205. {
  206. }
  207. void kvm_arch_flush_shadow_all(struct kvm *kvm)
  208. {
  209. }
  210. void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
  211. struct kvm_memory_slot *slot)
  212. {
  213. }
  214. struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
  215. {
  216. int err;
  217. struct kvm_vcpu *vcpu;
  218. vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
  219. if (!vcpu) {
  220. err = -ENOMEM;
  221. goto out;
  222. }
  223. err = kvm_vcpu_init(vcpu, kvm, id);
  224. if (err)
  225. goto free_vcpu;
  226. err = create_hyp_mappings(vcpu, vcpu + 1);
  227. if (err)
  228. goto vcpu_uninit;
  229. return vcpu;
  230. vcpu_uninit:
  231. kvm_vcpu_uninit(vcpu);
  232. free_vcpu:
  233. kmem_cache_free(kvm_vcpu_cache, vcpu);
  234. out:
  235. return ERR_PTR(err);
  236. }
  237. int kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
  238. {
  239. return 0;
  240. }
  241. void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
  242. {
  243. kvm_mmu_free_memory_caches(vcpu);
  244. kvm_timer_vcpu_terminate(vcpu);
  245. kmem_cache_free(kvm_vcpu_cache, vcpu);
  246. }
  247. void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
  248. {
  249. kvm_arch_vcpu_free(vcpu);
  250. }
  251. int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
  252. {
  253. return 0;
  254. }
  255. int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
  256. {
  257. int ret;
  258. /* Force users to call KVM_ARM_VCPU_INIT */
  259. vcpu->arch.target = -1;
  260. /* Set up VGIC */
  261. ret = kvm_vgic_vcpu_init(vcpu);
  262. if (ret)
  263. return ret;
  264. /* Set up the timer */
  265. kvm_timer_vcpu_init(vcpu);
  266. return 0;
  267. }
  268. void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
  269. {
  270. }
  271. void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  272. {
  273. vcpu->cpu = cpu;
  274. vcpu->arch.host_cpu_context = this_cpu_ptr(kvm_host_cpu_state);
  275. /*
  276. * Check whether this vcpu requires the cache to be flushed on
  277. * this physical CPU. This is a consequence of doing dcache
  278. * operations by set/way on this vcpu. We do it here to be in
  279. * a non-preemptible section.
  280. */
  281. if (cpumask_test_and_clear_cpu(cpu, &vcpu->arch.require_dcache_flush))
  282. flush_cache_all(); /* We'd really want v7_flush_dcache_all() */
  283. kvm_arm_set_running_vcpu(vcpu);
  284. }
  285. void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
  286. {
  287. kvm_arm_set_running_vcpu(NULL);
  288. }
  289. int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
  290. struct kvm_guest_debug *dbg)
  291. {
  292. return -EINVAL;
  293. }
  294. int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
  295. struct kvm_mp_state *mp_state)
  296. {
  297. return -EINVAL;
  298. }
  299. int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
  300. struct kvm_mp_state *mp_state)
  301. {
  302. return -EINVAL;
  303. }
  304. /**
  305. * kvm_arch_vcpu_runnable - determine if the vcpu can be scheduled
  306. * @v: The VCPU pointer
  307. *
  308. * If the guest CPU is not waiting for interrupts or an interrupt line is
  309. * asserted, the CPU is by definition runnable.
  310. */
  311. int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
  312. {
  313. return !!v->arch.irq_lines || kvm_vgic_vcpu_pending_irq(v);
  314. }
  315. /* Just ensure a guest exit from a particular CPU */
  316. static void exit_vm_noop(void *info)
  317. {
  318. }
  319. void force_vm_exit(const cpumask_t *mask)
  320. {
  321. smp_call_function_many(mask, exit_vm_noop, NULL, true);
  322. }
  323. /**
  324. * need_new_vmid_gen - check that the VMID is still valid
  325. * @kvm: The VM's VMID to checkt
  326. *
  327. * return true if there is a new generation of VMIDs being used
  328. *
  329. * The hardware supports only 256 values with the value zero reserved for the
  330. * host, so we check if an assigned value belongs to a previous generation,
  331. * which which requires us to assign a new value. If we're the first to use a
  332. * VMID for the new generation, we must flush necessary caches and TLBs on all
  333. * CPUs.
  334. */
  335. static bool need_new_vmid_gen(struct kvm *kvm)
  336. {
  337. return unlikely(kvm->arch.vmid_gen != atomic64_read(&kvm_vmid_gen));
  338. }
  339. /**
  340. * update_vttbr - Update the VTTBR with a valid VMID before the guest runs
  341. * @kvm The guest that we are about to run
  342. *
  343. * Called from kvm_arch_vcpu_ioctl_run before entering the guest to ensure the
  344. * VM has a valid VMID, otherwise assigns a new one and flushes corresponding
  345. * caches and TLBs.
  346. */
  347. static void update_vttbr(struct kvm *kvm)
  348. {
  349. phys_addr_t pgd_phys;
  350. u64 vmid;
  351. if (!need_new_vmid_gen(kvm))
  352. return;
  353. spin_lock(&kvm_vmid_lock);
  354. /*
  355. * We need to re-check the vmid_gen here to ensure that if another vcpu
  356. * already allocated a valid vmid for this vm, then this vcpu should
  357. * use the same vmid.
  358. */
  359. if (!need_new_vmid_gen(kvm)) {
  360. spin_unlock(&kvm_vmid_lock);
  361. return;
  362. }
  363. /* First user of a new VMID generation? */
  364. if (unlikely(kvm_next_vmid == 0)) {
  365. atomic64_inc(&kvm_vmid_gen);
  366. kvm_next_vmid = 1;
  367. /*
  368. * On SMP we know no other CPUs can use this CPU's or each
  369. * other's VMID after force_vm_exit returns since the
  370. * kvm_vmid_lock blocks them from reentry to the guest.
  371. */
  372. force_vm_exit(cpu_all_mask);
  373. /*
  374. * Now broadcast TLB + ICACHE invalidation over the inner
  375. * shareable domain to make sure all data structures are
  376. * clean.
  377. */
  378. kvm_call_hyp(__kvm_flush_vm_context);
  379. }
  380. kvm->arch.vmid_gen = atomic64_read(&kvm_vmid_gen);
  381. kvm->arch.vmid = kvm_next_vmid;
  382. kvm_next_vmid++;
  383. /* update vttbr to be used with the new vmid */
  384. pgd_phys = virt_to_phys(kvm->arch.pgd);
  385. vmid = ((u64)(kvm->arch.vmid) << VTTBR_VMID_SHIFT) & VTTBR_VMID_MASK;
  386. kvm->arch.vttbr = pgd_phys & VTTBR_BADDR_MASK;
  387. kvm->arch.vttbr |= vmid;
  388. spin_unlock(&kvm_vmid_lock);
  389. }
  390. static int kvm_vcpu_first_run_init(struct kvm_vcpu *vcpu)
  391. {
  392. if (likely(vcpu->arch.has_run_once))
  393. return 0;
  394. vcpu->arch.has_run_once = true;
  395. /*
  396. * Initialize the VGIC before running a vcpu the first time on
  397. * this VM.
  398. */
  399. if (irqchip_in_kernel(vcpu->kvm) &&
  400. unlikely(!vgic_initialized(vcpu->kvm))) {
  401. int ret = kvm_vgic_init(vcpu->kvm);
  402. if (ret)
  403. return ret;
  404. }
  405. /*
  406. * Handle the "start in power-off" case by calling into the
  407. * PSCI code.
  408. */
  409. if (test_and_clear_bit(KVM_ARM_VCPU_POWER_OFF, vcpu->arch.features)) {
  410. *vcpu_reg(vcpu, 0) = KVM_PSCI_FN_CPU_OFF;
  411. kvm_psci_call(vcpu);
  412. }
  413. return 0;
  414. }
  415. static void vcpu_pause(struct kvm_vcpu *vcpu)
  416. {
  417. wait_queue_head_t *wq = kvm_arch_vcpu_wq(vcpu);
  418. wait_event_interruptible(*wq, !vcpu->arch.pause);
  419. }
  420. static int kvm_vcpu_initialized(struct kvm_vcpu *vcpu)
  421. {
  422. return vcpu->arch.target >= 0;
  423. }
  424. /**
  425. * kvm_arch_vcpu_ioctl_run - the main VCPU run function to execute guest code
  426. * @vcpu: The VCPU pointer
  427. * @run: The kvm_run structure pointer used for userspace state exchange
  428. *
  429. * This function is called through the VCPU_RUN ioctl called from user space. It
  430. * will execute VM code in a loop until the time slice for the process is used
  431. * or some emulation is needed from user space in which case the function will
  432. * return with return value 0 and with the kvm_run structure filled in with the
  433. * required data for the requested emulation.
  434. */
  435. int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
  436. {
  437. int ret;
  438. sigset_t sigsaved;
  439. if (unlikely(!kvm_vcpu_initialized(vcpu)))
  440. return -ENOEXEC;
  441. ret = kvm_vcpu_first_run_init(vcpu);
  442. if (ret)
  443. return ret;
  444. if (run->exit_reason == KVM_EXIT_MMIO) {
  445. ret = kvm_handle_mmio_return(vcpu, vcpu->run);
  446. if (ret)
  447. return ret;
  448. }
  449. if (vcpu->sigset_active)
  450. sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
  451. ret = 1;
  452. run->exit_reason = KVM_EXIT_UNKNOWN;
  453. while (ret > 0) {
  454. /*
  455. * Check conditions before entering the guest
  456. */
  457. cond_resched();
  458. update_vttbr(vcpu->kvm);
  459. if (vcpu->arch.pause)
  460. vcpu_pause(vcpu);
  461. kvm_vgic_flush_hwstate(vcpu);
  462. kvm_timer_flush_hwstate(vcpu);
  463. local_irq_disable();
  464. /*
  465. * Re-check atomic conditions
  466. */
  467. if (signal_pending(current)) {
  468. ret = -EINTR;
  469. run->exit_reason = KVM_EXIT_INTR;
  470. }
  471. if (ret <= 0 || need_new_vmid_gen(vcpu->kvm)) {
  472. local_irq_enable();
  473. kvm_timer_sync_hwstate(vcpu);
  474. kvm_vgic_sync_hwstate(vcpu);
  475. continue;
  476. }
  477. /**************************************************************
  478. * Enter the guest
  479. */
  480. trace_kvm_entry(*vcpu_pc(vcpu));
  481. kvm_guest_enter();
  482. vcpu->mode = IN_GUEST_MODE;
  483. ret = kvm_call_hyp(__kvm_vcpu_run, vcpu);
  484. vcpu->mode = OUTSIDE_GUEST_MODE;
  485. vcpu->arch.last_pcpu = smp_processor_id();
  486. kvm_guest_exit();
  487. trace_kvm_exit(*vcpu_pc(vcpu));
  488. /*
  489. * We may have taken a host interrupt in HYP mode (ie
  490. * while executing the guest). This interrupt is still
  491. * pending, as we haven't serviced it yet!
  492. *
  493. * We're now back in SVC mode, with interrupts
  494. * disabled. Enabling the interrupts now will have
  495. * the effect of taking the interrupt again, in SVC
  496. * mode this time.
  497. */
  498. local_irq_enable();
  499. /*
  500. * Back from guest
  501. *************************************************************/
  502. kvm_timer_sync_hwstate(vcpu);
  503. kvm_vgic_sync_hwstate(vcpu);
  504. ret = handle_exit(vcpu, run, ret);
  505. }
  506. if (vcpu->sigset_active)
  507. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  508. return ret;
  509. }
  510. static int vcpu_interrupt_line(struct kvm_vcpu *vcpu, int number, bool level)
  511. {
  512. int bit_index;
  513. bool set;
  514. unsigned long *ptr;
  515. if (number == KVM_ARM_IRQ_CPU_IRQ)
  516. bit_index = __ffs(HCR_VI);
  517. else /* KVM_ARM_IRQ_CPU_FIQ */
  518. bit_index = __ffs(HCR_VF);
  519. ptr = (unsigned long *)&vcpu->arch.irq_lines;
  520. if (level)
  521. set = test_and_set_bit(bit_index, ptr);
  522. else
  523. set = test_and_clear_bit(bit_index, ptr);
  524. /*
  525. * If we didn't change anything, no need to wake up or kick other CPUs
  526. */
  527. if (set == level)
  528. return 0;
  529. /*
  530. * The vcpu irq_lines field was updated, wake up sleeping VCPUs and
  531. * trigger a world-switch round on the running physical CPU to set the
  532. * virtual IRQ/FIQ fields in the HCR appropriately.
  533. */
  534. kvm_vcpu_kick(vcpu);
  535. return 0;
  536. }
  537. int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level,
  538. bool line_status)
  539. {
  540. u32 irq = irq_level->irq;
  541. unsigned int irq_type, vcpu_idx, irq_num;
  542. int nrcpus = atomic_read(&kvm->online_vcpus);
  543. struct kvm_vcpu *vcpu = NULL;
  544. bool level = irq_level->level;
  545. irq_type = (irq >> KVM_ARM_IRQ_TYPE_SHIFT) & KVM_ARM_IRQ_TYPE_MASK;
  546. vcpu_idx = (irq >> KVM_ARM_IRQ_VCPU_SHIFT) & KVM_ARM_IRQ_VCPU_MASK;
  547. irq_num = (irq >> KVM_ARM_IRQ_NUM_SHIFT) & KVM_ARM_IRQ_NUM_MASK;
  548. trace_kvm_irq_line(irq_type, vcpu_idx, irq_num, irq_level->level);
  549. switch (irq_type) {
  550. case KVM_ARM_IRQ_TYPE_CPU:
  551. if (irqchip_in_kernel(kvm))
  552. return -ENXIO;
  553. if (vcpu_idx >= nrcpus)
  554. return -EINVAL;
  555. vcpu = kvm_get_vcpu(kvm, vcpu_idx);
  556. if (!vcpu)
  557. return -EINVAL;
  558. if (irq_num > KVM_ARM_IRQ_CPU_FIQ)
  559. return -EINVAL;
  560. return vcpu_interrupt_line(vcpu, irq_num, level);
  561. case KVM_ARM_IRQ_TYPE_PPI:
  562. if (!irqchip_in_kernel(kvm))
  563. return -ENXIO;
  564. if (vcpu_idx >= nrcpus)
  565. return -EINVAL;
  566. vcpu = kvm_get_vcpu(kvm, vcpu_idx);
  567. if (!vcpu)
  568. return -EINVAL;
  569. if (irq_num < VGIC_NR_SGIS || irq_num >= VGIC_NR_PRIVATE_IRQS)
  570. return -EINVAL;
  571. return kvm_vgic_inject_irq(kvm, vcpu->vcpu_id, irq_num, level);
  572. case KVM_ARM_IRQ_TYPE_SPI:
  573. if (!irqchip_in_kernel(kvm))
  574. return -ENXIO;
  575. if (irq_num < VGIC_NR_PRIVATE_IRQS ||
  576. irq_num > KVM_ARM_IRQ_GIC_MAX)
  577. return -EINVAL;
  578. return kvm_vgic_inject_irq(kvm, 0, irq_num, level);
  579. }
  580. return -EINVAL;
  581. }
  582. long kvm_arch_vcpu_ioctl(struct file *filp,
  583. unsigned int ioctl, unsigned long arg)
  584. {
  585. struct kvm_vcpu *vcpu = filp->private_data;
  586. void __user *argp = (void __user *)arg;
  587. switch (ioctl) {
  588. case KVM_ARM_VCPU_INIT: {
  589. struct kvm_vcpu_init init;
  590. if (copy_from_user(&init, argp, sizeof(init)))
  591. return -EFAULT;
  592. return kvm_vcpu_set_target(vcpu, &init);
  593. }
  594. case KVM_SET_ONE_REG:
  595. case KVM_GET_ONE_REG: {
  596. struct kvm_one_reg reg;
  597. if (unlikely(!kvm_vcpu_initialized(vcpu)))
  598. return -ENOEXEC;
  599. if (copy_from_user(&reg, argp, sizeof(reg)))
  600. return -EFAULT;
  601. if (ioctl == KVM_SET_ONE_REG)
  602. return kvm_arm_set_reg(vcpu, &reg);
  603. else
  604. return kvm_arm_get_reg(vcpu, &reg);
  605. }
  606. case KVM_GET_REG_LIST: {
  607. struct kvm_reg_list __user *user_list = argp;
  608. struct kvm_reg_list reg_list;
  609. unsigned n;
  610. if (unlikely(!kvm_vcpu_initialized(vcpu)))
  611. return -ENOEXEC;
  612. if (copy_from_user(&reg_list, user_list, sizeof(reg_list)))
  613. return -EFAULT;
  614. n = reg_list.n;
  615. reg_list.n = kvm_arm_num_regs(vcpu);
  616. if (copy_to_user(user_list, &reg_list, sizeof(reg_list)))
  617. return -EFAULT;
  618. if (n < reg_list.n)
  619. return -E2BIG;
  620. return kvm_arm_copy_reg_indices(vcpu, user_list->reg);
  621. }
  622. default:
  623. return -EINVAL;
  624. }
  625. }
  626. int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
  627. {
  628. return -EINVAL;
  629. }
  630. static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm,
  631. struct kvm_arm_device_addr *dev_addr)
  632. {
  633. unsigned long dev_id, type;
  634. dev_id = (dev_addr->id & KVM_ARM_DEVICE_ID_MASK) >>
  635. KVM_ARM_DEVICE_ID_SHIFT;
  636. type = (dev_addr->id & KVM_ARM_DEVICE_TYPE_MASK) >>
  637. KVM_ARM_DEVICE_TYPE_SHIFT;
  638. switch (dev_id) {
  639. case KVM_ARM_DEVICE_VGIC_V2:
  640. if (!vgic_present)
  641. return -ENXIO;
  642. return kvm_vgic_set_addr(kvm, type, dev_addr->addr);
  643. default:
  644. return -ENODEV;
  645. }
  646. }
  647. long kvm_arch_vm_ioctl(struct file *filp,
  648. unsigned int ioctl, unsigned long arg)
  649. {
  650. struct kvm *kvm = filp->private_data;
  651. void __user *argp = (void __user *)arg;
  652. switch (ioctl) {
  653. case KVM_CREATE_IRQCHIP: {
  654. if (vgic_present)
  655. return kvm_vgic_create(kvm);
  656. else
  657. return -ENXIO;
  658. }
  659. case KVM_ARM_SET_DEVICE_ADDR: {
  660. struct kvm_arm_device_addr dev_addr;
  661. if (copy_from_user(&dev_addr, argp, sizeof(dev_addr)))
  662. return -EFAULT;
  663. return kvm_vm_ioctl_set_device_addr(kvm, &dev_addr);
  664. }
  665. case KVM_ARM_PREFERRED_TARGET: {
  666. int err;
  667. struct kvm_vcpu_init init;
  668. err = kvm_vcpu_preferred_target(&init);
  669. if (err)
  670. return err;
  671. if (copy_to_user(argp, &init, sizeof(init)))
  672. return -EFAULT;
  673. return 0;
  674. }
  675. default:
  676. return -EINVAL;
  677. }
  678. }
  679. static void cpu_init_hyp_mode(void *dummy)
  680. {
  681. phys_addr_t boot_pgd_ptr;
  682. phys_addr_t pgd_ptr;
  683. unsigned long hyp_stack_ptr;
  684. unsigned long stack_page;
  685. unsigned long vector_ptr;
  686. /* Switch from the HYP stub to our own HYP init vector */
  687. __hyp_set_vectors(kvm_get_idmap_vector());
  688. boot_pgd_ptr = kvm_mmu_get_boot_httbr();
  689. pgd_ptr = kvm_mmu_get_httbr();
  690. stack_page = __this_cpu_read(kvm_arm_hyp_stack_page);
  691. hyp_stack_ptr = stack_page + PAGE_SIZE;
  692. vector_ptr = (unsigned long)__kvm_hyp_vector;
  693. __cpu_init_hyp_mode(boot_pgd_ptr, pgd_ptr, hyp_stack_ptr, vector_ptr);
  694. }
  695. static int hyp_init_cpu_notify(struct notifier_block *self,
  696. unsigned long action, void *cpu)
  697. {
  698. switch (action) {
  699. case CPU_STARTING:
  700. case CPU_STARTING_FROZEN:
  701. cpu_init_hyp_mode(NULL);
  702. break;
  703. }
  704. return NOTIFY_OK;
  705. }
  706. static struct notifier_block hyp_init_cpu_nb = {
  707. .notifier_call = hyp_init_cpu_notify,
  708. };
  709. #ifdef CONFIG_CPU_PM
  710. static int hyp_init_cpu_pm_notifier(struct notifier_block *self,
  711. unsigned long cmd,
  712. void *v)
  713. {
  714. if (cmd == CPU_PM_EXIT) {
  715. cpu_init_hyp_mode(NULL);
  716. return NOTIFY_OK;
  717. }
  718. return NOTIFY_DONE;
  719. }
  720. static struct notifier_block hyp_init_cpu_pm_nb = {
  721. .notifier_call = hyp_init_cpu_pm_notifier,
  722. };
  723. static void __init hyp_cpu_pm_init(void)
  724. {
  725. cpu_pm_register_notifier(&hyp_init_cpu_pm_nb);
  726. }
  727. #else
  728. static inline void hyp_cpu_pm_init(void)
  729. {
  730. }
  731. #endif
  732. /**
  733. * Inits Hyp-mode on all online CPUs
  734. */
  735. static int init_hyp_mode(void)
  736. {
  737. int cpu;
  738. int err = 0;
  739. /*
  740. * Allocate Hyp PGD and setup Hyp identity mapping
  741. */
  742. err = kvm_mmu_init();
  743. if (err)
  744. goto out_err;
  745. /*
  746. * It is probably enough to obtain the default on one
  747. * CPU. It's unlikely to be different on the others.
  748. */
  749. hyp_default_vectors = __hyp_get_vectors();
  750. /*
  751. * Allocate stack pages for Hypervisor-mode
  752. */
  753. for_each_possible_cpu(cpu) {
  754. unsigned long stack_page;
  755. stack_page = __get_free_page(GFP_KERNEL);
  756. if (!stack_page) {
  757. err = -ENOMEM;
  758. goto out_free_stack_pages;
  759. }
  760. per_cpu(kvm_arm_hyp_stack_page, cpu) = stack_page;
  761. }
  762. /*
  763. * Map the Hyp-code called directly from the host
  764. */
  765. err = create_hyp_mappings(__kvm_hyp_code_start, __kvm_hyp_code_end);
  766. if (err) {
  767. kvm_err("Cannot map world-switch code\n");
  768. goto out_free_mappings;
  769. }
  770. /*
  771. * Map the Hyp stack pages
  772. */
  773. for_each_possible_cpu(cpu) {
  774. char *stack_page = (char *)per_cpu(kvm_arm_hyp_stack_page, cpu);
  775. err = create_hyp_mappings(stack_page, stack_page + PAGE_SIZE);
  776. if (err) {
  777. kvm_err("Cannot map hyp stack\n");
  778. goto out_free_mappings;
  779. }
  780. }
  781. /*
  782. * Map the host CPU structures
  783. */
  784. kvm_host_cpu_state = alloc_percpu(kvm_cpu_context_t);
  785. if (!kvm_host_cpu_state) {
  786. err = -ENOMEM;
  787. kvm_err("Cannot allocate host CPU state\n");
  788. goto out_free_mappings;
  789. }
  790. for_each_possible_cpu(cpu) {
  791. kvm_cpu_context_t *cpu_ctxt;
  792. cpu_ctxt = per_cpu_ptr(kvm_host_cpu_state, cpu);
  793. err = create_hyp_mappings(cpu_ctxt, cpu_ctxt + 1);
  794. if (err) {
  795. kvm_err("Cannot map host CPU state: %d\n", err);
  796. goto out_free_context;
  797. }
  798. }
  799. /*
  800. * Execute the init code on each CPU.
  801. */
  802. on_each_cpu(cpu_init_hyp_mode, NULL, 1);
  803. /*
  804. * Init HYP view of VGIC
  805. */
  806. err = kvm_vgic_hyp_init();
  807. if (err)
  808. goto out_free_context;
  809. #ifdef CONFIG_KVM_ARM_VGIC
  810. vgic_present = true;
  811. #endif
  812. /*
  813. * Init HYP architected timer support
  814. */
  815. err = kvm_timer_hyp_init();
  816. if (err)
  817. goto out_free_mappings;
  818. #ifndef CONFIG_HOTPLUG_CPU
  819. free_boot_hyp_pgd();
  820. #endif
  821. kvm_perf_init();
  822. kvm_info("Hyp mode initialized successfully\n");
  823. return 0;
  824. out_free_context:
  825. free_percpu(kvm_host_cpu_state);
  826. out_free_mappings:
  827. free_hyp_pgds();
  828. out_free_stack_pages:
  829. for_each_possible_cpu(cpu)
  830. free_page(per_cpu(kvm_arm_hyp_stack_page, cpu));
  831. out_err:
  832. kvm_err("error initializing Hyp mode: %d\n", err);
  833. return err;
  834. }
  835. static void check_kvm_target_cpu(void *ret)
  836. {
  837. *(int *)ret = kvm_target_cpu();
  838. }
  839. /**
  840. * Initialize Hyp-mode and memory mappings on all CPUs.
  841. */
  842. int kvm_arch_init(void *opaque)
  843. {
  844. int err;
  845. int ret, cpu;
  846. if (!is_hyp_mode_available()) {
  847. kvm_err("HYP mode not available\n");
  848. return -ENODEV;
  849. }
  850. for_each_online_cpu(cpu) {
  851. smp_call_function_single(cpu, check_kvm_target_cpu, &ret, 1);
  852. if (ret < 0) {
  853. kvm_err("Error, CPU %d not supported!\n", cpu);
  854. return -ENODEV;
  855. }
  856. }
  857. err = init_hyp_mode();
  858. if (err)
  859. goto out_err;
  860. err = register_cpu_notifier(&hyp_init_cpu_nb);
  861. if (err) {
  862. kvm_err("Cannot register HYP init CPU notifier (%d)\n", err);
  863. goto out_err;
  864. }
  865. hyp_cpu_pm_init();
  866. kvm_coproc_table_init();
  867. return 0;
  868. out_err:
  869. return err;
  870. }
  871. /* NOP: Compiling as a module not supported */
  872. void kvm_arch_exit(void)
  873. {
  874. kvm_perf_teardown();
  875. }
  876. static int arm_init(void)
  877. {
  878. int rc = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
  879. return rc;
  880. }
  881. module_init(arm_init);