arm.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  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/errno.h>
  19. #include <linux/err.h>
  20. #include <linux/kvm_host.h>
  21. #include <linux/module.h>
  22. #include <linux/vmalloc.h>
  23. #include <linux/fs.h>
  24. #include <linux/mman.h>
  25. #include <linux/sched.h>
  26. #include <linux/kvm.h>
  27. #include <trace/events/kvm.h>
  28. #define CREATE_TRACE_POINTS
  29. #include "trace.h"
  30. #include <asm/unified.h>
  31. #include <asm/uaccess.h>
  32. #include <asm/ptrace.h>
  33. #include <asm/mman.h>
  34. #include <asm/cputype.h>
  35. #include <asm/tlbflush.h>
  36. #include <asm/virt.h>
  37. #include <asm/kvm_arm.h>
  38. #include <asm/kvm_asm.h>
  39. #include <asm/kvm_mmu.h>
  40. #include <asm/kvm_emulate.h>
  41. #ifdef REQUIRES_VIRT
  42. __asm__(".arch_extension virt");
  43. #endif
  44. static DEFINE_PER_CPU(unsigned long, kvm_arm_hyp_stack_page);
  45. static struct vfp_hard_struct __percpu *kvm_host_vfp_state;
  46. static unsigned long hyp_default_vectors;
  47. /* The VMID used in the VTTBR */
  48. static atomic64_t kvm_vmid_gen = ATOMIC64_INIT(1);
  49. static u8 kvm_next_vmid;
  50. static DEFINE_SPINLOCK(kvm_vmid_lock);
  51. int kvm_arch_hardware_enable(void *garbage)
  52. {
  53. return 0;
  54. }
  55. int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
  56. {
  57. return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
  58. }
  59. void kvm_arch_hardware_disable(void *garbage)
  60. {
  61. }
  62. int kvm_arch_hardware_setup(void)
  63. {
  64. return 0;
  65. }
  66. void kvm_arch_hardware_unsetup(void)
  67. {
  68. }
  69. void kvm_arch_check_processor_compat(void *rtn)
  70. {
  71. *(int *)rtn = 0;
  72. }
  73. void kvm_arch_sync_events(struct kvm *kvm)
  74. {
  75. }
  76. /**
  77. * kvm_arch_init_vm - initializes a VM data structure
  78. * @kvm: pointer to the KVM struct
  79. */
  80. int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
  81. {
  82. int ret = 0;
  83. if (type)
  84. return -EINVAL;
  85. ret = kvm_alloc_stage2_pgd(kvm);
  86. if (ret)
  87. goto out_fail_alloc;
  88. ret = create_hyp_mappings(kvm, kvm + 1);
  89. if (ret)
  90. goto out_free_stage2_pgd;
  91. /* Mark the initial VMID generation invalid */
  92. kvm->arch.vmid_gen = 0;
  93. return ret;
  94. out_free_stage2_pgd:
  95. kvm_free_stage2_pgd(kvm);
  96. out_fail_alloc:
  97. return ret;
  98. }
  99. int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
  100. {
  101. return VM_FAULT_SIGBUS;
  102. }
  103. void kvm_arch_free_memslot(struct kvm_memory_slot *free,
  104. struct kvm_memory_slot *dont)
  105. {
  106. }
  107. int kvm_arch_create_memslot(struct kvm_memory_slot *slot, unsigned long npages)
  108. {
  109. return 0;
  110. }
  111. /**
  112. * kvm_arch_destroy_vm - destroy the VM data structure
  113. * @kvm: pointer to the KVM struct
  114. */
  115. void kvm_arch_destroy_vm(struct kvm *kvm)
  116. {
  117. int i;
  118. kvm_free_stage2_pgd(kvm);
  119. for (i = 0; i < KVM_MAX_VCPUS; ++i) {
  120. if (kvm->vcpus[i]) {
  121. kvm_arch_vcpu_free(kvm->vcpus[i]);
  122. kvm->vcpus[i] = NULL;
  123. }
  124. }
  125. }
  126. int kvm_dev_ioctl_check_extension(long ext)
  127. {
  128. int r;
  129. switch (ext) {
  130. case KVM_CAP_USER_MEMORY:
  131. case KVM_CAP_SYNC_MMU:
  132. case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
  133. case KVM_CAP_ONE_REG:
  134. r = 1;
  135. break;
  136. case KVM_CAP_COALESCED_MMIO:
  137. r = KVM_COALESCED_MMIO_PAGE_OFFSET;
  138. break;
  139. case KVM_CAP_NR_VCPUS:
  140. r = num_online_cpus();
  141. break;
  142. case KVM_CAP_MAX_VCPUS:
  143. r = KVM_MAX_VCPUS;
  144. break;
  145. default:
  146. r = 0;
  147. break;
  148. }
  149. return r;
  150. }
  151. long kvm_arch_dev_ioctl(struct file *filp,
  152. unsigned int ioctl, unsigned long arg)
  153. {
  154. return -EINVAL;
  155. }
  156. int kvm_arch_set_memory_region(struct kvm *kvm,
  157. struct kvm_userspace_memory_region *mem,
  158. struct kvm_memory_slot old,
  159. int user_alloc)
  160. {
  161. return 0;
  162. }
  163. int kvm_arch_prepare_memory_region(struct kvm *kvm,
  164. struct kvm_memory_slot *memslot,
  165. struct kvm_memory_slot old,
  166. struct kvm_userspace_memory_region *mem,
  167. int user_alloc)
  168. {
  169. return 0;
  170. }
  171. void kvm_arch_commit_memory_region(struct kvm *kvm,
  172. struct kvm_userspace_memory_region *mem,
  173. struct kvm_memory_slot old,
  174. int user_alloc)
  175. {
  176. }
  177. void kvm_arch_flush_shadow_all(struct kvm *kvm)
  178. {
  179. }
  180. void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
  181. struct kvm_memory_slot *slot)
  182. {
  183. }
  184. struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
  185. {
  186. int err;
  187. struct kvm_vcpu *vcpu;
  188. vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
  189. if (!vcpu) {
  190. err = -ENOMEM;
  191. goto out;
  192. }
  193. err = kvm_vcpu_init(vcpu, kvm, id);
  194. if (err)
  195. goto free_vcpu;
  196. err = create_hyp_mappings(vcpu, vcpu + 1);
  197. if (err)
  198. goto vcpu_uninit;
  199. return vcpu;
  200. vcpu_uninit:
  201. kvm_vcpu_uninit(vcpu);
  202. free_vcpu:
  203. kmem_cache_free(kvm_vcpu_cache, vcpu);
  204. out:
  205. return ERR_PTR(err);
  206. }
  207. int kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
  208. {
  209. return 0;
  210. }
  211. void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
  212. {
  213. kvm_mmu_free_memory_caches(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 __attribute_const__ kvm_target_cpu(void)
  225. {
  226. unsigned long implementor = read_cpuid_implementor();
  227. unsigned long part_number = read_cpuid_part_number();
  228. if (implementor != ARM_CPU_IMP_ARM)
  229. return -EINVAL;
  230. switch (part_number) {
  231. case ARM_CPU_PART_CORTEX_A15:
  232. return KVM_ARM_TARGET_CORTEX_A15;
  233. default:
  234. return -EINVAL;
  235. }
  236. }
  237. int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
  238. {
  239. /* Force users to call KVM_ARM_VCPU_INIT */
  240. vcpu->arch.target = -1;
  241. return 0;
  242. }
  243. void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
  244. {
  245. }
  246. void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  247. {
  248. vcpu->cpu = cpu;
  249. vcpu->arch.vfp_host = this_cpu_ptr(kvm_host_vfp_state);
  250. }
  251. void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
  252. {
  253. }
  254. int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
  255. struct kvm_guest_debug *dbg)
  256. {
  257. return -EINVAL;
  258. }
  259. int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
  260. struct kvm_mp_state *mp_state)
  261. {
  262. return -EINVAL;
  263. }
  264. int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
  265. struct kvm_mp_state *mp_state)
  266. {
  267. return -EINVAL;
  268. }
  269. int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
  270. {
  271. return 0;
  272. }
  273. /* Just ensure a guest exit from a particular CPU */
  274. static void exit_vm_noop(void *info)
  275. {
  276. }
  277. void force_vm_exit(const cpumask_t *mask)
  278. {
  279. smp_call_function_many(mask, exit_vm_noop, NULL, true);
  280. }
  281. /**
  282. * need_new_vmid_gen - check that the VMID is still valid
  283. * @kvm: The VM's VMID to checkt
  284. *
  285. * return true if there is a new generation of VMIDs being used
  286. *
  287. * The hardware supports only 256 values with the value zero reserved for the
  288. * host, so we check if an assigned value belongs to a previous generation,
  289. * which which requires us to assign a new value. If we're the first to use a
  290. * VMID for the new generation, we must flush necessary caches and TLBs on all
  291. * CPUs.
  292. */
  293. static bool need_new_vmid_gen(struct kvm *kvm)
  294. {
  295. return unlikely(kvm->arch.vmid_gen != atomic64_read(&kvm_vmid_gen));
  296. }
  297. /**
  298. * update_vttbr - Update the VTTBR with a valid VMID before the guest runs
  299. * @kvm The guest that we are about to run
  300. *
  301. * Called from kvm_arch_vcpu_ioctl_run before entering the guest to ensure the
  302. * VM has a valid VMID, otherwise assigns a new one and flushes corresponding
  303. * caches and TLBs.
  304. */
  305. static void update_vttbr(struct kvm *kvm)
  306. {
  307. phys_addr_t pgd_phys;
  308. u64 vmid;
  309. if (!need_new_vmid_gen(kvm))
  310. return;
  311. spin_lock(&kvm_vmid_lock);
  312. /*
  313. * We need to re-check the vmid_gen here to ensure that if another vcpu
  314. * already allocated a valid vmid for this vm, then this vcpu should
  315. * use the same vmid.
  316. */
  317. if (!need_new_vmid_gen(kvm)) {
  318. spin_unlock(&kvm_vmid_lock);
  319. return;
  320. }
  321. /* First user of a new VMID generation? */
  322. if (unlikely(kvm_next_vmid == 0)) {
  323. atomic64_inc(&kvm_vmid_gen);
  324. kvm_next_vmid = 1;
  325. /*
  326. * On SMP we know no other CPUs can use this CPU's or each
  327. * other's VMID after force_vm_exit returns since the
  328. * kvm_vmid_lock blocks them from reentry to the guest.
  329. */
  330. force_vm_exit(cpu_all_mask);
  331. /*
  332. * Now broadcast TLB + ICACHE invalidation over the inner
  333. * shareable domain to make sure all data structures are
  334. * clean.
  335. */
  336. kvm_call_hyp(__kvm_flush_vm_context);
  337. }
  338. kvm->arch.vmid_gen = atomic64_read(&kvm_vmid_gen);
  339. kvm->arch.vmid = kvm_next_vmid;
  340. kvm_next_vmid++;
  341. /* update vttbr to be used with the new vmid */
  342. pgd_phys = virt_to_phys(kvm->arch.pgd);
  343. vmid = ((u64)(kvm->arch.vmid) << VTTBR_VMID_SHIFT) & VTTBR_VMID_MASK;
  344. kvm->arch.vttbr = pgd_phys & VTTBR_BADDR_MASK;
  345. kvm->arch.vttbr |= vmid;
  346. spin_unlock(&kvm_vmid_lock);
  347. }
  348. /*
  349. * Return > 0 to return to guest, < 0 on error, 0 (and set exit_reason) on
  350. * proper exit to QEMU.
  351. */
  352. static int handle_exit(struct kvm_vcpu *vcpu, struct kvm_run *run,
  353. int exception_index)
  354. {
  355. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  356. return 0;
  357. }
  358. static int kvm_vcpu_first_run_init(struct kvm_vcpu *vcpu)
  359. {
  360. if (likely(vcpu->arch.has_run_once))
  361. return 0;
  362. vcpu->arch.has_run_once = true;
  363. return 0;
  364. }
  365. /**
  366. * kvm_arch_vcpu_ioctl_run - the main VCPU run function to execute guest code
  367. * @vcpu: The VCPU pointer
  368. * @run: The kvm_run structure pointer used for userspace state exchange
  369. *
  370. * This function is called through the VCPU_RUN ioctl called from user space. It
  371. * will execute VM code in a loop until the time slice for the process is used
  372. * or some emulation is needed from user space in which case the function will
  373. * return with return value 0 and with the kvm_run structure filled in with the
  374. * required data for the requested emulation.
  375. */
  376. int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
  377. {
  378. int ret;
  379. sigset_t sigsaved;
  380. /* Make sure they initialize the vcpu with KVM_ARM_VCPU_INIT */
  381. if (unlikely(vcpu->arch.target < 0))
  382. return -ENOEXEC;
  383. ret = kvm_vcpu_first_run_init(vcpu);
  384. if (ret)
  385. return ret;
  386. if (vcpu->sigset_active)
  387. sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
  388. ret = 1;
  389. run->exit_reason = KVM_EXIT_UNKNOWN;
  390. while (ret > 0) {
  391. /*
  392. * Check conditions before entering the guest
  393. */
  394. cond_resched();
  395. update_vttbr(vcpu->kvm);
  396. local_irq_disable();
  397. /*
  398. * Re-check atomic conditions
  399. */
  400. if (signal_pending(current)) {
  401. ret = -EINTR;
  402. run->exit_reason = KVM_EXIT_INTR;
  403. }
  404. if (ret <= 0 || need_new_vmid_gen(vcpu->kvm)) {
  405. local_irq_enable();
  406. continue;
  407. }
  408. /**************************************************************
  409. * Enter the guest
  410. */
  411. trace_kvm_entry(*vcpu_pc(vcpu));
  412. kvm_guest_enter();
  413. vcpu->mode = IN_GUEST_MODE;
  414. ret = kvm_call_hyp(__kvm_vcpu_run, vcpu);
  415. vcpu->mode = OUTSIDE_GUEST_MODE;
  416. kvm_guest_exit();
  417. trace_kvm_exit(*vcpu_pc(vcpu));
  418. /*
  419. * We may have taken a host interrupt in HYP mode (ie
  420. * while executing the guest). This interrupt is still
  421. * pending, as we haven't serviced it yet!
  422. *
  423. * We're now back in SVC mode, with interrupts
  424. * disabled. Enabling the interrupts now will have
  425. * the effect of taking the interrupt again, in SVC
  426. * mode this time.
  427. */
  428. local_irq_enable();
  429. /*
  430. * Back from guest
  431. *************************************************************/
  432. ret = handle_exit(vcpu, run, ret);
  433. }
  434. if (vcpu->sigset_active)
  435. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  436. return ret;
  437. }
  438. static int vcpu_interrupt_line(struct kvm_vcpu *vcpu, int number, bool level)
  439. {
  440. int bit_index;
  441. bool set;
  442. unsigned long *ptr;
  443. if (number == KVM_ARM_IRQ_CPU_IRQ)
  444. bit_index = __ffs(HCR_VI);
  445. else /* KVM_ARM_IRQ_CPU_FIQ */
  446. bit_index = __ffs(HCR_VF);
  447. ptr = (unsigned long *)&vcpu->arch.irq_lines;
  448. if (level)
  449. set = test_and_set_bit(bit_index, ptr);
  450. else
  451. set = test_and_clear_bit(bit_index, ptr);
  452. /*
  453. * If we didn't change anything, no need to wake up or kick other CPUs
  454. */
  455. if (set == level)
  456. return 0;
  457. /*
  458. * The vcpu irq_lines field was updated, wake up sleeping VCPUs and
  459. * trigger a world-switch round on the running physical CPU to set the
  460. * virtual IRQ/FIQ fields in the HCR appropriately.
  461. */
  462. kvm_vcpu_kick(vcpu);
  463. return 0;
  464. }
  465. int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level)
  466. {
  467. u32 irq = irq_level->irq;
  468. unsigned int irq_type, vcpu_idx, irq_num;
  469. int nrcpus = atomic_read(&kvm->online_vcpus);
  470. struct kvm_vcpu *vcpu = NULL;
  471. bool level = irq_level->level;
  472. irq_type = (irq >> KVM_ARM_IRQ_TYPE_SHIFT) & KVM_ARM_IRQ_TYPE_MASK;
  473. vcpu_idx = (irq >> KVM_ARM_IRQ_VCPU_SHIFT) & KVM_ARM_IRQ_VCPU_MASK;
  474. irq_num = (irq >> KVM_ARM_IRQ_NUM_SHIFT) & KVM_ARM_IRQ_NUM_MASK;
  475. trace_kvm_irq_line(irq_type, vcpu_idx, irq_num, irq_level->level);
  476. if (irq_type != KVM_ARM_IRQ_TYPE_CPU)
  477. return -EINVAL;
  478. if (vcpu_idx >= nrcpus)
  479. return -EINVAL;
  480. vcpu = kvm_get_vcpu(kvm, vcpu_idx);
  481. if (!vcpu)
  482. return -EINVAL;
  483. if (irq_num > KVM_ARM_IRQ_CPU_FIQ)
  484. return -EINVAL;
  485. return vcpu_interrupt_line(vcpu, irq_num, level);
  486. }
  487. long kvm_arch_vcpu_ioctl(struct file *filp,
  488. unsigned int ioctl, unsigned long arg)
  489. {
  490. struct kvm_vcpu *vcpu = filp->private_data;
  491. void __user *argp = (void __user *)arg;
  492. switch (ioctl) {
  493. case KVM_ARM_VCPU_INIT: {
  494. struct kvm_vcpu_init init;
  495. if (copy_from_user(&init, argp, sizeof(init)))
  496. return -EFAULT;
  497. return kvm_vcpu_set_target(vcpu, &init);
  498. }
  499. case KVM_SET_ONE_REG:
  500. case KVM_GET_ONE_REG: {
  501. struct kvm_one_reg reg;
  502. if (copy_from_user(&reg, argp, sizeof(reg)))
  503. return -EFAULT;
  504. if (ioctl == KVM_SET_ONE_REG)
  505. return kvm_arm_set_reg(vcpu, &reg);
  506. else
  507. return kvm_arm_get_reg(vcpu, &reg);
  508. }
  509. case KVM_GET_REG_LIST: {
  510. struct kvm_reg_list __user *user_list = argp;
  511. struct kvm_reg_list reg_list;
  512. unsigned n;
  513. if (copy_from_user(&reg_list, user_list, sizeof(reg_list)))
  514. return -EFAULT;
  515. n = reg_list.n;
  516. reg_list.n = kvm_arm_num_regs(vcpu);
  517. if (copy_to_user(user_list, &reg_list, sizeof(reg_list)))
  518. return -EFAULT;
  519. if (n < reg_list.n)
  520. return -E2BIG;
  521. return kvm_arm_copy_reg_indices(vcpu, user_list->reg);
  522. }
  523. default:
  524. return -EINVAL;
  525. }
  526. }
  527. int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
  528. {
  529. return -EINVAL;
  530. }
  531. long kvm_arch_vm_ioctl(struct file *filp,
  532. unsigned int ioctl, unsigned long arg)
  533. {
  534. return -EINVAL;
  535. }
  536. static void cpu_init_hyp_mode(void *vector)
  537. {
  538. unsigned long long pgd_ptr;
  539. unsigned long pgd_low, pgd_high;
  540. unsigned long hyp_stack_ptr;
  541. unsigned long stack_page;
  542. unsigned long vector_ptr;
  543. /* Switch from the HYP stub to our own HYP init vector */
  544. __hyp_set_vectors((unsigned long)vector);
  545. pgd_ptr = (unsigned long long)kvm_mmu_get_httbr();
  546. pgd_low = (pgd_ptr & ((1ULL << 32) - 1));
  547. pgd_high = (pgd_ptr >> 32ULL);
  548. stack_page = __get_cpu_var(kvm_arm_hyp_stack_page);
  549. hyp_stack_ptr = stack_page + PAGE_SIZE;
  550. vector_ptr = (unsigned long)__kvm_hyp_vector;
  551. /*
  552. * Call initialization code, and switch to the full blown
  553. * HYP code. The init code doesn't need to preserve these registers as
  554. * r1-r3 and r12 are already callee save according to the AAPCS.
  555. * Note that we slightly misuse the prototype by casing the pgd_low to
  556. * a void *.
  557. */
  558. kvm_call_hyp((void *)pgd_low, pgd_high, hyp_stack_ptr, vector_ptr);
  559. }
  560. /**
  561. * Inits Hyp-mode on all online CPUs
  562. */
  563. static int init_hyp_mode(void)
  564. {
  565. phys_addr_t init_phys_addr;
  566. int cpu;
  567. int err = 0;
  568. /*
  569. * Allocate Hyp PGD and setup Hyp identity mapping
  570. */
  571. err = kvm_mmu_init();
  572. if (err)
  573. goto out_err;
  574. /*
  575. * It is probably enough to obtain the default on one
  576. * CPU. It's unlikely to be different on the others.
  577. */
  578. hyp_default_vectors = __hyp_get_vectors();
  579. /*
  580. * Allocate stack pages for Hypervisor-mode
  581. */
  582. for_each_possible_cpu(cpu) {
  583. unsigned long stack_page;
  584. stack_page = __get_free_page(GFP_KERNEL);
  585. if (!stack_page) {
  586. err = -ENOMEM;
  587. goto out_free_stack_pages;
  588. }
  589. per_cpu(kvm_arm_hyp_stack_page, cpu) = stack_page;
  590. }
  591. /*
  592. * Execute the init code on each CPU.
  593. *
  594. * Note: The stack is not mapped yet, so don't do anything else than
  595. * initializing the hypervisor mode on each CPU using a local stack
  596. * space for temporary storage.
  597. */
  598. init_phys_addr = virt_to_phys(__kvm_hyp_init);
  599. for_each_online_cpu(cpu) {
  600. smp_call_function_single(cpu, cpu_init_hyp_mode,
  601. (void *)(long)init_phys_addr, 1);
  602. }
  603. /*
  604. * Unmap the identity mapping
  605. */
  606. kvm_clear_hyp_idmap();
  607. /*
  608. * Map the Hyp-code called directly from the host
  609. */
  610. err = create_hyp_mappings(__kvm_hyp_code_start, __kvm_hyp_code_end);
  611. if (err) {
  612. kvm_err("Cannot map world-switch code\n");
  613. goto out_free_mappings;
  614. }
  615. /*
  616. * Map the Hyp stack pages
  617. */
  618. for_each_possible_cpu(cpu) {
  619. char *stack_page = (char *)per_cpu(kvm_arm_hyp_stack_page, cpu);
  620. err = create_hyp_mappings(stack_page, stack_page + PAGE_SIZE);
  621. if (err) {
  622. kvm_err("Cannot map hyp stack\n");
  623. goto out_free_mappings;
  624. }
  625. }
  626. /*
  627. * Map the host VFP structures
  628. */
  629. kvm_host_vfp_state = alloc_percpu(struct vfp_hard_struct);
  630. if (!kvm_host_vfp_state) {
  631. err = -ENOMEM;
  632. kvm_err("Cannot allocate host VFP state\n");
  633. goto out_free_mappings;
  634. }
  635. for_each_possible_cpu(cpu) {
  636. struct vfp_hard_struct *vfp;
  637. vfp = per_cpu_ptr(kvm_host_vfp_state, cpu);
  638. err = create_hyp_mappings(vfp, vfp + 1);
  639. if (err) {
  640. kvm_err("Cannot map host VFP state: %d\n", err);
  641. goto out_free_vfp;
  642. }
  643. }
  644. kvm_info("Hyp mode initialized successfully\n");
  645. return 0;
  646. out_free_vfp:
  647. free_percpu(kvm_host_vfp_state);
  648. out_free_mappings:
  649. free_hyp_pmds();
  650. out_free_stack_pages:
  651. for_each_possible_cpu(cpu)
  652. free_page(per_cpu(kvm_arm_hyp_stack_page, cpu));
  653. out_err:
  654. kvm_err("error initializing Hyp mode: %d\n", err);
  655. return err;
  656. }
  657. /**
  658. * Initialize Hyp-mode and memory mappings on all CPUs.
  659. */
  660. int kvm_arch_init(void *opaque)
  661. {
  662. int err;
  663. if (!is_hyp_mode_available()) {
  664. kvm_err("HYP mode not available\n");
  665. return -ENODEV;
  666. }
  667. if (kvm_target_cpu() < 0) {
  668. kvm_err("Target CPU not supported!\n");
  669. return -ENODEV;
  670. }
  671. err = init_hyp_mode();
  672. if (err)
  673. goto out_err;
  674. return 0;
  675. out_err:
  676. return err;
  677. }
  678. /* NOP: Compiling as a module not supported */
  679. void kvm_arch_exit(void)
  680. {
  681. }
  682. static int arm_init(void)
  683. {
  684. int rc = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
  685. return rc;
  686. }
  687. module_init(arm_init);