arm.c 24 KB

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