arm.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166
  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_IOEVENTFD:
  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. preempt_disable();
  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. preempt_enable();
  457. kvm_timer_sync_hwstate(vcpu);
  458. kvm_vgic_sync_hwstate(vcpu);
  459. continue;
  460. }
  461. /**************************************************************
  462. * Enter the guest
  463. */
  464. trace_kvm_entry(*vcpu_pc(vcpu));
  465. __kvm_guest_enter();
  466. vcpu->mode = IN_GUEST_MODE;
  467. ret = kvm_call_hyp(__kvm_vcpu_run, vcpu);
  468. vcpu->mode = OUTSIDE_GUEST_MODE;
  469. /*
  470. * Back from guest
  471. *************************************************************/
  472. /*
  473. * We may have taken a host interrupt in HYP mode (ie
  474. * while executing the guest). This interrupt is still
  475. * pending, as we haven't serviced it yet!
  476. *
  477. * We're now back in SVC mode, with interrupts
  478. * disabled. Enabling the interrupts now will have
  479. * the effect of taking the interrupt again, in SVC
  480. * mode this time.
  481. */
  482. local_irq_enable();
  483. /*
  484. * We do local_irq_enable() before calling kvm_guest_exit() so
  485. * that if a timer interrupt hits while running the guest we
  486. * account that tick as being spent in the guest. We enable
  487. * preemption after calling kvm_guest_exit() so that if we get
  488. * preempted we make sure ticks after that is not counted as
  489. * guest time.
  490. */
  491. kvm_guest_exit();
  492. trace_kvm_exit(kvm_vcpu_trap_get_class(vcpu), *vcpu_pc(vcpu));
  493. preempt_enable();
  494. kvm_timer_sync_hwstate(vcpu);
  495. kvm_vgic_sync_hwstate(vcpu);
  496. ret = handle_exit(vcpu, run, ret);
  497. }
  498. if (vcpu->sigset_active)
  499. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  500. return ret;
  501. }
  502. static int vcpu_interrupt_line(struct kvm_vcpu *vcpu, int number, bool level)
  503. {
  504. int bit_index;
  505. bool set;
  506. unsigned long *ptr;
  507. if (number == KVM_ARM_IRQ_CPU_IRQ)
  508. bit_index = __ffs(HCR_VI);
  509. else /* KVM_ARM_IRQ_CPU_FIQ */
  510. bit_index = __ffs(HCR_VF);
  511. ptr = (unsigned long *)&vcpu->arch.irq_lines;
  512. if (level)
  513. set = test_and_set_bit(bit_index, ptr);
  514. else
  515. set = test_and_clear_bit(bit_index, ptr);
  516. /*
  517. * If we didn't change anything, no need to wake up or kick other CPUs
  518. */
  519. if (set == level)
  520. return 0;
  521. /*
  522. * The vcpu irq_lines field was updated, wake up sleeping VCPUs and
  523. * trigger a world-switch round on the running physical CPU to set the
  524. * virtual IRQ/FIQ fields in the HCR appropriately.
  525. */
  526. kvm_vcpu_kick(vcpu);
  527. return 0;
  528. }
  529. int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level,
  530. bool line_status)
  531. {
  532. u32 irq = irq_level->irq;
  533. unsigned int irq_type, vcpu_idx, irq_num;
  534. int nrcpus = atomic_read(&kvm->online_vcpus);
  535. struct kvm_vcpu *vcpu = NULL;
  536. bool level = irq_level->level;
  537. irq_type = (irq >> KVM_ARM_IRQ_TYPE_SHIFT) & KVM_ARM_IRQ_TYPE_MASK;
  538. vcpu_idx = (irq >> KVM_ARM_IRQ_VCPU_SHIFT) & KVM_ARM_IRQ_VCPU_MASK;
  539. irq_num = (irq >> KVM_ARM_IRQ_NUM_SHIFT) & KVM_ARM_IRQ_NUM_MASK;
  540. trace_kvm_irq_line(irq_type, vcpu_idx, irq_num, irq_level->level);
  541. switch (irq_type) {
  542. case KVM_ARM_IRQ_TYPE_CPU:
  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 > KVM_ARM_IRQ_CPU_FIQ)
  551. return -EINVAL;
  552. return vcpu_interrupt_line(vcpu, irq_num, level);
  553. case KVM_ARM_IRQ_TYPE_PPI:
  554. if (!irqchip_in_kernel(kvm))
  555. return -ENXIO;
  556. if (vcpu_idx >= nrcpus)
  557. return -EINVAL;
  558. vcpu = kvm_get_vcpu(kvm, vcpu_idx);
  559. if (!vcpu)
  560. return -EINVAL;
  561. if (irq_num < VGIC_NR_SGIS || irq_num >= VGIC_NR_PRIVATE_IRQS)
  562. return -EINVAL;
  563. return kvm_vgic_inject_irq(kvm, vcpu->vcpu_id, irq_num, level);
  564. case KVM_ARM_IRQ_TYPE_SPI:
  565. if (!irqchip_in_kernel(kvm))
  566. return -ENXIO;
  567. if (irq_num < VGIC_NR_PRIVATE_IRQS)
  568. return -EINVAL;
  569. return kvm_vgic_inject_irq(kvm, 0, irq_num, level);
  570. }
  571. return -EINVAL;
  572. }
  573. static int kvm_vcpu_set_target(struct kvm_vcpu *vcpu,
  574. const struct kvm_vcpu_init *init)
  575. {
  576. unsigned int i;
  577. int phys_target = kvm_target_cpu();
  578. if (init->target != phys_target)
  579. return -EINVAL;
  580. /*
  581. * Secondary and subsequent calls to KVM_ARM_VCPU_INIT must
  582. * use the same target.
  583. */
  584. if (vcpu->arch.target != -1 && vcpu->arch.target != init->target)
  585. return -EINVAL;
  586. /* -ENOENT for unknown features, -EINVAL for invalid combinations. */
  587. for (i = 0; i < sizeof(init->features) * 8; i++) {
  588. bool set = (init->features[i / 32] & (1 << (i % 32)));
  589. if (set && i >= KVM_VCPU_MAX_FEATURES)
  590. return -ENOENT;
  591. /*
  592. * Secondary and subsequent calls to KVM_ARM_VCPU_INIT must
  593. * use the same feature set.
  594. */
  595. if (vcpu->arch.target != -1 && i < KVM_VCPU_MAX_FEATURES &&
  596. test_bit(i, vcpu->arch.features) != set)
  597. return -EINVAL;
  598. if (set)
  599. set_bit(i, vcpu->arch.features);
  600. }
  601. vcpu->arch.target = phys_target;
  602. /* Now we know what it is, we can reset it. */
  603. return kvm_reset_vcpu(vcpu);
  604. }
  605. static int kvm_arch_vcpu_ioctl_vcpu_init(struct kvm_vcpu *vcpu,
  606. struct kvm_vcpu_init *init)
  607. {
  608. int ret;
  609. ret = kvm_vcpu_set_target(vcpu, init);
  610. if (ret)
  611. return ret;
  612. /*
  613. * Ensure a rebooted VM will fault in RAM pages and detect if the
  614. * guest MMU is turned off and flush the caches as needed.
  615. */
  616. if (vcpu->arch.has_run_once)
  617. stage2_unmap_vm(vcpu->kvm);
  618. vcpu_reset_hcr(vcpu);
  619. /*
  620. * Handle the "start in power-off" case by marking the VCPU as paused.
  621. */
  622. if (test_bit(KVM_ARM_VCPU_POWER_OFF, vcpu->arch.features))
  623. vcpu->arch.pause = true;
  624. else
  625. vcpu->arch.pause = false;
  626. return 0;
  627. }
  628. long kvm_arch_vcpu_ioctl(struct file *filp,
  629. unsigned int ioctl, unsigned long arg)
  630. {
  631. struct kvm_vcpu *vcpu = filp->private_data;
  632. void __user *argp = (void __user *)arg;
  633. switch (ioctl) {
  634. case KVM_ARM_VCPU_INIT: {
  635. struct kvm_vcpu_init init;
  636. if (copy_from_user(&init, argp, sizeof(init)))
  637. return -EFAULT;
  638. return kvm_arch_vcpu_ioctl_vcpu_init(vcpu, &init);
  639. }
  640. case KVM_SET_ONE_REG:
  641. case KVM_GET_ONE_REG: {
  642. struct kvm_one_reg reg;
  643. if (unlikely(!kvm_vcpu_initialized(vcpu)))
  644. return -ENOEXEC;
  645. if (copy_from_user(&reg, argp, sizeof(reg)))
  646. return -EFAULT;
  647. if (ioctl == KVM_SET_ONE_REG)
  648. return kvm_arm_set_reg(vcpu, &reg);
  649. else
  650. return kvm_arm_get_reg(vcpu, &reg);
  651. }
  652. case KVM_GET_REG_LIST: {
  653. struct kvm_reg_list __user *user_list = argp;
  654. struct kvm_reg_list reg_list;
  655. unsigned n;
  656. if (unlikely(!kvm_vcpu_initialized(vcpu)))
  657. return -ENOEXEC;
  658. if (copy_from_user(&reg_list, user_list, sizeof(reg_list)))
  659. return -EFAULT;
  660. n = reg_list.n;
  661. reg_list.n = kvm_arm_num_regs(vcpu);
  662. if (copy_to_user(user_list, &reg_list, sizeof(reg_list)))
  663. return -EFAULT;
  664. if (n < reg_list.n)
  665. return -E2BIG;
  666. return kvm_arm_copy_reg_indices(vcpu, user_list->reg);
  667. }
  668. default:
  669. return -EINVAL;
  670. }
  671. }
  672. /**
  673. * kvm_vm_ioctl_get_dirty_log - get and clear the log of dirty pages in a slot
  674. * @kvm: kvm instance
  675. * @log: slot id and address to which we copy the log
  676. *
  677. * Steps 1-4 below provide general overview of dirty page logging. See
  678. * kvm_get_dirty_log_protect() function description for additional details.
  679. *
  680. * We call kvm_get_dirty_log_protect() to handle steps 1-3, upon return we
  681. * always flush the TLB (step 4) even if previous step failed and the dirty
  682. * bitmap may be corrupt. Regardless of previous outcome the KVM logging API
  683. * does not preclude user space subsequent dirty log read. Flushing TLB ensures
  684. * writes will be marked dirty for next log read.
  685. *
  686. * 1. Take a snapshot of the bit and clear it if needed.
  687. * 2. Write protect the corresponding page.
  688. * 3. Copy the snapshot to the userspace.
  689. * 4. Flush TLB's if needed.
  690. */
  691. int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
  692. {
  693. bool is_dirty = false;
  694. int r;
  695. mutex_lock(&kvm->slots_lock);
  696. r = kvm_get_dirty_log_protect(kvm, log, &is_dirty);
  697. if (is_dirty)
  698. kvm_flush_remote_tlbs(kvm);
  699. mutex_unlock(&kvm->slots_lock);
  700. return r;
  701. }
  702. static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm,
  703. struct kvm_arm_device_addr *dev_addr)
  704. {
  705. unsigned long dev_id, type;
  706. dev_id = (dev_addr->id & KVM_ARM_DEVICE_ID_MASK) >>
  707. KVM_ARM_DEVICE_ID_SHIFT;
  708. type = (dev_addr->id & KVM_ARM_DEVICE_TYPE_MASK) >>
  709. KVM_ARM_DEVICE_TYPE_SHIFT;
  710. switch (dev_id) {
  711. case KVM_ARM_DEVICE_VGIC_V2:
  712. return kvm_vgic_addr(kvm, type, &dev_addr->addr, true);
  713. default:
  714. return -ENODEV;
  715. }
  716. }
  717. long kvm_arch_vm_ioctl(struct file *filp,
  718. unsigned int ioctl, unsigned long arg)
  719. {
  720. struct kvm *kvm = filp->private_data;
  721. void __user *argp = (void __user *)arg;
  722. switch (ioctl) {
  723. case KVM_CREATE_IRQCHIP: {
  724. return kvm_vgic_create(kvm, KVM_DEV_TYPE_ARM_VGIC_V2);
  725. }
  726. case KVM_ARM_SET_DEVICE_ADDR: {
  727. struct kvm_arm_device_addr dev_addr;
  728. if (copy_from_user(&dev_addr, argp, sizeof(dev_addr)))
  729. return -EFAULT;
  730. return kvm_vm_ioctl_set_device_addr(kvm, &dev_addr);
  731. }
  732. case KVM_ARM_PREFERRED_TARGET: {
  733. int err;
  734. struct kvm_vcpu_init init;
  735. err = kvm_vcpu_preferred_target(&init);
  736. if (err)
  737. return err;
  738. if (copy_to_user(argp, &init, sizeof(init)))
  739. return -EFAULT;
  740. return 0;
  741. }
  742. default:
  743. return -EINVAL;
  744. }
  745. }
  746. static void cpu_init_hyp_mode(void *dummy)
  747. {
  748. phys_addr_t boot_pgd_ptr;
  749. phys_addr_t pgd_ptr;
  750. unsigned long hyp_stack_ptr;
  751. unsigned long stack_page;
  752. unsigned long vector_ptr;
  753. /* Switch from the HYP stub to our own HYP init vector */
  754. __hyp_set_vectors(kvm_get_idmap_vector());
  755. boot_pgd_ptr = kvm_mmu_get_boot_httbr();
  756. pgd_ptr = kvm_mmu_get_httbr();
  757. stack_page = __this_cpu_read(kvm_arm_hyp_stack_page);
  758. hyp_stack_ptr = stack_page + PAGE_SIZE;
  759. vector_ptr = (unsigned long)__kvm_hyp_vector;
  760. __cpu_init_hyp_mode(boot_pgd_ptr, pgd_ptr, hyp_stack_ptr, vector_ptr);
  761. }
  762. static int hyp_init_cpu_notify(struct notifier_block *self,
  763. unsigned long action, void *cpu)
  764. {
  765. switch (action) {
  766. case CPU_STARTING:
  767. case CPU_STARTING_FROZEN:
  768. if (__hyp_get_vectors() == hyp_default_vectors)
  769. cpu_init_hyp_mode(NULL);
  770. break;
  771. }
  772. return NOTIFY_OK;
  773. }
  774. static struct notifier_block hyp_init_cpu_nb = {
  775. .notifier_call = hyp_init_cpu_notify,
  776. };
  777. #ifdef CONFIG_CPU_PM
  778. static int hyp_init_cpu_pm_notifier(struct notifier_block *self,
  779. unsigned long cmd,
  780. void *v)
  781. {
  782. if (cmd == CPU_PM_EXIT &&
  783. __hyp_get_vectors() == hyp_default_vectors) {
  784. cpu_init_hyp_mode(NULL);
  785. return NOTIFY_OK;
  786. }
  787. return NOTIFY_DONE;
  788. }
  789. static struct notifier_block hyp_init_cpu_pm_nb = {
  790. .notifier_call = hyp_init_cpu_pm_notifier,
  791. };
  792. static void __init hyp_cpu_pm_init(void)
  793. {
  794. cpu_pm_register_notifier(&hyp_init_cpu_pm_nb);
  795. }
  796. #else
  797. static inline void hyp_cpu_pm_init(void)
  798. {
  799. }
  800. #endif
  801. /**
  802. * Inits Hyp-mode on all online CPUs
  803. */
  804. static int init_hyp_mode(void)
  805. {
  806. int cpu;
  807. int err = 0;
  808. /*
  809. * Allocate Hyp PGD and setup Hyp identity mapping
  810. */
  811. err = kvm_mmu_init();
  812. if (err)
  813. goto out_err;
  814. /*
  815. * It is probably enough to obtain the default on one
  816. * CPU. It's unlikely to be different on the others.
  817. */
  818. hyp_default_vectors = __hyp_get_vectors();
  819. /*
  820. * Allocate stack pages for Hypervisor-mode
  821. */
  822. for_each_possible_cpu(cpu) {
  823. unsigned long stack_page;
  824. stack_page = __get_free_page(GFP_KERNEL);
  825. if (!stack_page) {
  826. err = -ENOMEM;
  827. goto out_free_stack_pages;
  828. }
  829. per_cpu(kvm_arm_hyp_stack_page, cpu) = stack_page;
  830. }
  831. /*
  832. * Map the Hyp-code called directly from the host
  833. */
  834. err = create_hyp_mappings(__kvm_hyp_code_start, __kvm_hyp_code_end);
  835. if (err) {
  836. kvm_err("Cannot map world-switch code\n");
  837. goto out_free_mappings;
  838. }
  839. /*
  840. * Map the Hyp stack pages
  841. */
  842. for_each_possible_cpu(cpu) {
  843. char *stack_page = (char *)per_cpu(kvm_arm_hyp_stack_page, cpu);
  844. err = create_hyp_mappings(stack_page, stack_page + PAGE_SIZE);
  845. if (err) {
  846. kvm_err("Cannot map hyp stack\n");
  847. goto out_free_mappings;
  848. }
  849. }
  850. /*
  851. * Map the host CPU structures
  852. */
  853. kvm_host_cpu_state = alloc_percpu(kvm_cpu_context_t);
  854. if (!kvm_host_cpu_state) {
  855. err = -ENOMEM;
  856. kvm_err("Cannot allocate host CPU state\n");
  857. goto out_free_mappings;
  858. }
  859. for_each_possible_cpu(cpu) {
  860. kvm_cpu_context_t *cpu_ctxt;
  861. cpu_ctxt = per_cpu_ptr(kvm_host_cpu_state, cpu);
  862. err = create_hyp_mappings(cpu_ctxt, cpu_ctxt + 1);
  863. if (err) {
  864. kvm_err("Cannot map host CPU state: %d\n", err);
  865. goto out_free_context;
  866. }
  867. }
  868. /*
  869. * Execute the init code on each CPU.
  870. */
  871. on_each_cpu(cpu_init_hyp_mode, NULL, 1);
  872. /*
  873. * Init HYP view of VGIC
  874. */
  875. err = kvm_vgic_hyp_init();
  876. if (err)
  877. goto out_free_context;
  878. /*
  879. * Init HYP architected timer support
  880. */
  881. err = kvm_timer_hyp_init();
  882. if (err)
  883. goto out_free_mappings;
  884. #ifndef CONFIG_HOTPLUG_CPU
  885. free_boot_hyp_pgd();
  886. #endif
  887. kvm_perf_init();
  888. kvm_info("Hyp mode initialized successfully\n");
  889. return 0;
  890. out_free_context:
  891. free_percpu(kvm_host_cpu_state);
  892. out_free_mappings:
  893. free_hyp_pgds();
  894. out_free_stack_pages:
  895. for_each_possible_cpu(cpu)
  896. free_page(per_cpu(kvm_arm_hyp_stack_page, cpu));
  897. out_err:
  898. kvm_err("error initializing Hyp mode: %d\n", err);
  899. return err;
  900. }
  901. static void check_kvm_target_cpu(void *ret)
  902. {
  903. *(int *)ret = kvm_target_cpu();
  904. }
  905. struct kvm_vcpu *kvm_mpidr_to_vcpu(struct kvm *kvm, unsigned long mpidr)
  906. {
  907. struct kvm_vcpu *vcpu;
  908. int i;
  909. mpidr &= MPIDR_HWID_BITMASK;
  910. kvm_for_each_vcpu(i, vcpu, kvm) {
  911. if (mpidr == kvm_vcpu_get_mpidr_aff(vcpu))
  912. return vcpu;
  913. }
  914. return NULL;
  915. }
  916. /**
  917. * Initialize Hyp-mode and memory mappings on all CPUs.
  918. */
  919. int kvm_arch_init(void *opaque)
  920. {
  921. int err;
  922. int ret, cpu;
  923. if (!is_hyp_mode_available()) {
  924. kvm_err("HYP mode not available\n");
  925. return -ENODEV;
  926. }
  927. for_each_online_cpu(cpu) {
  928. smp_call_function_single(cpu, check_kvm_target_cpu, &ret, 1);
  929. if (ret < 0) {
  930. kvm_err("Error, CPU %d not supported!\n", cpu);
  931. return -ENODEV;
  932. }
  933. }
  934. cpu_notifier_register_begin();
  935. err = init_hyp_mode();
  936. if (err)
  937. goto out_err;
  938. err = __register_cpu_notifier(&hyp_init_cpu_nb);
  939. if (err) {
  940. kvm_err("Cannot register HYP init CPU notifier (%d)\n", err);
  941. goto out_err;
  942. }
  943. cpu_notifier_register_done();
  944. hyp_cpu_pm_init();
  945. kvm_coproc_table_init();
  946. return 0;
  947. out_err:
  948. cpu_notifier_register_done();
  949. return err;
  950. }
  951. /* NOP: Compiling as a module not supported */
  952. void kvm_arch_exit(void)
  953. {
  954. kvm_perf_teardown();
  955. }
  956. static int arm_init(void)
  957. {
  958. int rc = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
  959. return rc;
  960. }
  961. module_init(arm_init);