arm.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  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. #ifdef REQUIRES_VIRT
  41. __asm__(".arch_extension virt");
  42. #endif
  43. static DEFINE_PER_CPU(unsigned long, kvm_arm_hyp_stack_page);
  44. static struct vfp_hard_struct __percpu *kvm_host_vfp_state;
  45. static unsigned long hyp_default_vectors;
  46. int kvm_arch_hardware_enable(void *garbage)
  47. {
  48. return 0;
  49. }
  50. int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
  51. {
  52. return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
  53. }
  54. void kvm_arch_hardware_disable(void *garbage)
  55. {
  56. }
  57. int kvm_arch_hardware_setup(void)
  58. {
  59. return 0;
  60. }
  61. void kvm_arch_hardware_unsetup(void)
  62. {
  63. }
  64. void kvm_arch_check_processor_compat(void *rtn)
  65. {
  66. *(int *)rtn = 0;
  67. }
  68. void kvm_arch_sync_events(struct kvm *kvm)
  69. {
  70. }
  71. /**
  72. * kvm_arch_init_vm - initializes a VM data structure
  73. * @kvm: pointer to the KVM struct
  74. */
  75. int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
  76. {
  77. int ret = 0;
  78. if (type)
  79. return -EINVAL;
  80. ret = kvm_alloc_stage2_pgd(kvm);
  81. if (ret)
  82. goto out_fail_alloc;
  83. ret = create_hyp_mappings(kvm, kvm + 1);
  84. if (ret)
  85. goto out_free_stage2_pgd;
  86. /* Mark the initial VMID generation invalid */
  87. kvm->arch.vmid_gen = 0;
  88. return ret;
  89. out_free_stage2_pgd:
  90. kvm_free_stage2_pgd(kvm);
  91. out_fail_alloc:
  92. return ret;
  93. }
  94. int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
  95. {
  96. return VM_FAULT_SIGBUS;
  97. }
  98. void kvm_arch_free_memslot(struct kvm_memory_slot *free,
  99. struct kvm_memory_slot *dont)
  100. {
  101. }
  102. int kvm_arch_create_memslot(struct kvm_memory_slot *slot, unsigned long npages)
  103. {
  104. return 0;
  105. }
  106. /**
  107. * kvm_arch_destroy_vm - destroy the VM data structure
  108. * @kvm: pointer to the KVM struct
  109. */
  110. void kvm_arch_destroy_vm(struct kvm *kvm)
  111. {
  112. int i;
  113. kvm_free_stage2_pgd(kvm);
  114. for (i = 0; i < KVM_MAX_VCPUS; ++i) {
  115. if (kvm->vcpus[i]) {
  116. kvm_arch_vcpu_free(kvm->vcpus[i]);
  117. kvm->vcpus[i] = NULL;
  118. }
  119. }
  120. }
  121. int kvm_dev_ioctl_check_extension(long ext)
  122. {
  123. int r;
  124. switch (ext) {
  125. case KVM_CAP_USER_MEMORY:
  126. case KVM_CAP_SYNC_MMU:
  127. case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
  128. case KVM_CAP_ONE_REG:
  129. r = 1;
  130. break;
  131. case KVM_CAP_COALESCED_MMIO:
  132. r = KVM_COALESCED_MMIO_PAGE_OFFSET;
  133. break;
  134. case KVM_CAP_NR_VCPUS:
  135. r = num_online_cpus();
  136. break;
  137. case KVM_CAP_MAX_VCPUS:
  138. r = KVM_MAX_VCPUS;
  139. break;
  140. default:
  141. r = 0;
  142. break;
  143. }
  144. return r;
  145. }
  146. long kvm_arch_dev_ioctl(struct file *filp,
  147. unsigned int ioctl, unsigned long arg)
  148. {
  149. return -EINVAL;
  150. }
  151. int kvm_arch_set_memory_region(struct kvm *kvm,
  152. struct kvm_userspace_memory_region *mem,
  153. struct kvm_memory_slot old,
  154. int user_alloc)
  155. {
  156. return 0;
  157. }
  158. int kvm_arch_prepare_memory_region(struct kvm *kvm,
  159. struct kvm_memory_slot *memslot,
  160. struct kvm_memory_slot old,
  161. struct kvm_userspace_memory_region *mem,
  162. int user_alloc)
  163. {
  164. return 0;
  165. }
  166. void kvm_arch_commit_memory_region(struct kvm *kvm,
  167. struct kvm_userspace_memory_region *mem,
  168. struct kvm_memory_slot old,
  169. int user_alloc)
  170. {
  171. }
  172. void kvm_arch_flush_shadow_all(struct kvm *kvm)
  173. {
  174. }
  175. void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
  176. struct kvm_memory_slot *slot)
  177. {
  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. vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
  184. if (!vcpu) {
  185. err = -ENOMEM;
  186. goto out;
  187. }
  188. err = kvm_vcpu_init(vcpu, kvm, id);
  189. if (err)
  190. goto free_vcpu;
  191. err = create_hyp_mappings(vcpu, vcpu + 1);
  192. if (err)
  193. goto vcpu_uninit;
  194. return vcpu;
  195. vcpu_uninit:
  196. kvm_vcpu_uninit(vcpu);
  197. free_vcpu:
  198. kmem_cache_free(kvm_vcpu_cache, vcpu);
  199. out:
  200. return ERR_PTR(err);
  201. }
  202. int kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
  203. {
  204. return 0;
  205. }
  206. void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
  207. {
  208. kvm_mmu_free_memory_caches(vcpu);
  209. kmem_cache_free(kvm_vcpu_cache, vcpu);
  210. }
  211. void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
  212. {
  213. kvm_arch_vcpu_free(vcpu);
  214. }
  215. int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
  216. {
  217. return 0;
  218. }
  219. int __attribute_const__ kvm_target_cpu(void)
  220. {
  221. unsigned long implementor = read_cpuid_implementor();
  222. unsigned long part_number = read_cpuid_part_number();
  223. if (implementor != ARM_CPU_IMP_ARM)
  224. return -EINVAL;
  225. switch (part_number) {
  226. case ARM_CPU_PART_CORTEX_A15:
  227. return KVM_ARM_TARGET_CORTEX_A15;
  228. default:
  229. return -EINVAL;
  230. }
  231. }
  232. int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
  233. {
  234. return 0;
  235. }
  236. void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
  237. {
  238. }
  239. void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  240. {
  241. vcpu->cpu = cpu;
  242. }
  243. void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
  244. {
  245. }
  246. int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
  247. struct kvm_guest_debug *dbg)
  248. {
  249. return -EINVAL;
  250. }
  251. int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
  252. struct kvm_mp_state *mp_state)
  253. {
  254. return -EINVAL;
  255. }
  256. int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
  257. struct kvm_mp_state *mp_state)
  258. {
  259. return -EINVAL;
  260. }
  261. int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
  262. {
  263. return 0;
  264. }
  265. int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
  266. {
  267. return -EINVAL;
  268. }
  269. static int vcpu_interrupt_line(struct kvm_vcpu *vcpu, int number, bool level)
  270. {
  271. int bit_index;
  272. bool set;
  273. unsigned long *ptr;
  274. if (number == KVM_ARM_IRQ_CPU_IRQ)
  275. bit_index = __ffs(HCR_VI);
  276. else /* KVM_ARM_IRQ_CPU_FIQ */
  277. bit_index = __ffs(HCR_VF);
  278. ptr = (unsigned long *)&vcpu->arch.irq_lines;
  279. if (level)
  280. set = test_and_set_bit(bit_index, ptr);
  281. else
  282. set = test_and_clear_bit(bit_index, ptr);
  283. /*
  284. * If we didn't change anything, no need to wake up or kick other CPUs
  285. */
  286. if (set == level)
  287. return 0;
  288. /*
  289. * The vcpu irq_lines field was updated, wake up sleeping VCPUs and
  290. * trigger a world-switch round on the running physical CPU to set the
  291. * virtual IRQ/FIQ fields in the HCR appropriately.
  292. */
  293. kvm_vcpu_kick(vcpu);
  294. return 0;
  295. }
  296. int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level)
  297. {
  298. u32 irq = irq_level->irq;
  299. unsigned int irq_type, vcpu_idx, irq_num;
  300. int nrcpus = atomic_read(&kvm->online_vcpus);
  301. struct kvm_vcpu *vcpu = NULL;
  302. bool level = irq_level->level;
  303. irq_type = (irq >> KVM_ARM_IRQ_TYPE_SHIFT) & KVM_ARM_IRQ_TYPE_MASK;
  304. vcpu_idx = (irq >> KVM_ARM_IRQ_VCPU_SHIFT) & KVM_ARM_IRQ_VCPU_MASK;
  305. irq_num = (irq >> KVM_ARM_IRQ_NUM_SHIFT) & KVM_ARM_IRQ_NUM_MASK;
  306. trace_kvm_irq_line(irq_type, vcpu_idx, irq_num, irq_level->level);
  307. if (irq_type != KVM_ARM_IRQ_TYPE_CPU)
  308. return -EINVAL;
  309. if (vcpu_idx >= nrcpus)
  310. return -EINVAL;
  311. vcpu = kvm_get_vcpu(kvm, vcpu_idx);
  312. if (!vcpu)
  313. return -EINVAL;
  314. if (irq_num > KVM_ARM_IRQ_CPU_FIQ)
  315. return -EINVAL;
  316. return vcpu_interrupt_line(vcpu, irq_num, level);
  317. }
  318. long kvm_arch_vcpu_ioctl(struct file *filp,
  319. unsigned int ioctl, unsigned long arg)
  320. {
  321. struct kvm_vcpu *vcpu = filp->private_data;
  322. void __user *argp = (void __user *)arg;
  323. switch (ioctl) {
  324. case KVM_ARM_VCPU_INIT: {
  325. struct kvm_vcpu_init init;
  326. if (copy_from_user(&init, argp, sizeof(init)))
  327. return -EFAULT;
  328. return kvm_vcpu_set_target(vcpu, &init);
  329. }
  330. case KVM_SET_ONE_REG:
  331. case KVM_GET_ONE_REG: {
  332. struct kvm_one_reg reg;
  333. if (copy_from_user(&reg, argp, sizeof(reg)))
  334. return -EFAULT;
  335. if (ioctl == KVM_SET_ONE_REG)
  336. return kvm_arm_set_reg(vcpu, &reg);
  337. else
  338. return kvm_arm_get_reg(vcpu, &reg);
  339. }
  340. case KVM_GET_REG_LIST: {
  341. struct kvm_reg_list __user *user_list = argp;
  342. struct kvm_reg_list reg_list;
  343. unsigned n;
  344. if (copy_from_user(&reg_list, user_list, sizeof(reg_list)))
  345. return -EFAULT;
  346. n = reg_list.n;
  347. reg_list.n = kvm_arm_num_regs(vcpu);
  348. if (copy_to_user(user_list, &reg_list, sizeof(reg_list)))
  349. return -EFAULT;
  350. if (n < reg_list.n)
  351. return -E2BIG;
  352. return kvm_arm_copy_reg_indices(vcpu, user_list->reg);
  353. }
  354. default:
  355. return -EINVAL;
  356. }
  357. }
  358. int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
  359. {
  360. return -EINVAL;
  361. }
  362. long kvm_arch_vm_ioctl(struct file *filp,
  363. unsigned int ioctl, unsigned long arg)
  364. {
  365. return -EINVAL;
  366. }
  367. static void cpu_init_hyp_mode(void *vector)
  368. {
  369. unsigned long long pgd_ptr;
  370. unsigned long pgd_low, pgd_high;
  371. unsigned long hyp_stack_ptr;
  372. unsigned long stack_page;
  373. unsigned long vector_ptr;
  374. /* Switch from the HYP stub to our own HYP init vector */
  375. __hyp_set_vectors((unsigned long)vector);
  376. pgd_ptr = (unsigned long long)kvm_mmu_get_httbr();
  377. pgd_low = (pgd_ptr & ((1ULL << 32) - 1));
  378. pgd_high = (pgd_ptr >> 32ULL);
  379. stack_page = __get_cpu_var(kvm_arm_hyp_stack_page);
  380. hyp_stack_ptr = stack_page + PAGE_SIZE;
  381. vector_ptr = (unsigned long)__kvm_hyp_vector;
  382. /*
  383. * Call initialization code, and switch to the full blown
  384. * HYP code. The init code doesn't need to preserve these registers as
  385. * r1-r3 and r12 are already callee save according to the AAPCS.
  386. * Note that we slightly misuse the prototype by casing the pgd_low to
  387. * a void *.
  388. */
  389. kvm_call_hyp((void *)pgd_low, pgd_high, hyp_stack_ptr, vector_ptr);
  390. }
  391. /**
  392. * Inits Hyp-mode on all online CPUs
  393. */
  394. static int init_hyp_mode(void)
  395. {
  396. phys_addr_t init_phys_addr;
  397. int cpu;
  398. int err = 0;
  399. /*
  400. * Allocate Hyp PGD and setup Hyp identity mapping
  401. */
  402. err = kvm_mmu_init();
  403. if (err)
  404. goto out_err;
  405. /*
  406. * It is probably enough to obtain the default on one
  407. * CPU. It's unlikely to be different on the others.
  408. */
  409. hyp_default_vectors = __hyp_get_vectors();
  410. /*
  411. * Allocate stack pages for Hypervisor-mode
  412. */
  413. for_each_possible_cpu(cpu) {
  414. unsigned long stack_page;
  415. stack_page = __get_free_page(GFP_KERNEL);
  416. if (!stack_page) {
  417. err = -ENOMEM;
  418. goto out_free_stack_pages;
  419. }
  420. per_cpu(kvm_arm_hyp_stack_page, cpu) = stack_page;
  421. }
  422. /*
  423. * Execute the init code on each CPU.
  424. *
  425. * Note: The stack is not mapped yet, so don't do anything else than
  426. * initializing the hypervisor mode on each CPU using a local stack
  427. * space for temporary storage.
  428. */
  429. init_phys_addr = virt_to_phys(__kvm_hyp_init);
  430. for_each_online_cpu(cpu) {
  431. smp_call_function_single(cpu, cpu_init_hyp_mode,
  432. (void *)(long)init_phys_addr, 1);
  433. }
  434. /*
  435. * Unmap the identity mapping
  436. */
  437. kvm_clear_hyp_idmap();
  438. /*
  439. * Map the Hyp-code called directly from the host
  440. */
  441. err = create_hyp_mappings(__kvm_hyp_code_start, __kvm_hyp_code_end);
  442. if (err) {
  443. kvm_err("Cannot map world-switch code\n");
  444. goto out_free_mappings;
  445. }
  446. /*
  447. * Map the Hyp stack pages
  448. */
  449. for_each_possible_cpu(cpu) {
  450. char *stack_page = (char *)per_cpu(kvm_arm_hyp_stack_page, cpu);
  451. err = create_hyp_mappings(stack_page, stack_page + PAGE_SIZE);
  452. if (err) {
  453. kvm_err("Cannot map hyp stack\n");
  454. goto out_free_mappings;
  455. }
  456. }
  457. /*
  458. * Map the host VFP structures
  459. */
  460. kvm_host_vfp_state = alloc_percpu(struct vfp_hard_struct);
  461. if (!kvm_host_vfp_state) {
  462. err = -ENOMEM;
  463. kvm_err("Cannot allocate host VFP state\n");
  464. goto out_free_mappings;
  465. }
  466. for_each_possible_cpu(cpu) {
  467. struct vfp_hard_struct *vfp;
  468. vfp = per_cpu_ptr(kvm_host_vfp_state, cpu);
  469. err = create_hyp_mappings(vfp, vfp + 1);
  470. if (err) {
  471. kvm_err("Cannot map host VFP state: %d\n", err);
  472. goto out_free_vfp;
  473. }
  474. }
  475. kvm_info("Hyp mode initialized successfully\n");
  476. return 0;
  477. out_free_vfp:
  478. free_percpu(kvm_host_vfp_state);
  479. out_free_mappings:
  480. free_hyp_pmds();
  481. out_free_stack_pages:
  482. for_each_possible_cpu(cpu)
  483. free_page(per_cpu(kvm_arm_hyp_stack_page, cpu));
  484. out_err:
  485. kvm_err("error initializing Hyp mode: %d\n", err);
  486. return err;
  487. }
  488. /**
  489. * Initialize Hyp-mode and memory mappings on all CPUs.
  490. */
  491. int kvm_arch_init(void *opaque)
  492. {
  493. int err;
  494. if (!is_hyp_mode_available()) {
  495. kvm_err("HYP mode not available\n");
  496. return -ENODEV;
  497. }
  498. if (kvm_target_cpu() < 0) {
  499. kvm_err("Target CPU not supported!\n");
  500. return -ENODEV;
  501. }
  502. err = init_hyp_mode();
  503. if (err)
  504. goto out_err;
  505. return 0;
  506. out_err:
  507. return err;
  508. }
  509. /* NOP: Compiling as a module not supported */
  510. void kvm_arch_exit(void)
  511. {
  512. }
  513. static int arm_init(void)
  514. {
  515. int rc = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
  516. return rc;
  517. }
  518. module_init(arm_init);