arm.c 11 KB

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