kvm_pkvm.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2020 - Google LLC
  4. * Author: Quentin Perret <qperret@google.com>
  5. */
  6. #ifndef __ARM64_KVM_PKVM_H__
  7. #define __ARM64_KVM_PKVM_H__
  8. #include <linux/arm_ffa.h>
  9. #include <linux/memblock.h>
  10. #include <linux/scatterlist.h>
  11. #include <asm/kvm_host.h>
  12. #include <asm/kvm_pgtable.h>
  13. /* Maximum number of VMs that can co-exist under pKVM. */
  14. #define KVM_MAX_PVMS 255
  15. #define HYP_MEMBLOCK_REGIONS 128
  16. int pkvm_init_host_vm(struct kvm *kvm);
  17. int pkvm_create_hyp_vm(struct kvm *kvm);
  18. bool pkvm_hyp_vm_is_created(struct kvm *kvm);
  19. void pkvm_destroy_hyp_vm(struct kvm *kvm);
  20. int pkvm_create_hyp_vcpu(struct kvm_vcpu *vcpu);
  21. /*
  22. * Check whether the specific capability is allowed in pKVM.
  23. *
  24. * Certain features are allowed only for non-protected VMs in pKVM, which is why
  25. * this takes the VM (kvm) as a parameter.
  26. */
  27. static inline bool kvm_pkvm_ext_allowed(struct kvm *kvm, long ext)
  28. {
  29. switch (ext) {
  30. case KVM_CAP_IRQCHIP:
  31. case KVM_CAP_ARM_PSCI:
  32. case KVM_CAP_ARM_PSCI_0_2:
  33. case KVM_CAP_NR_VCPUS:
  34. case KVM_CAP_MAX_VCPUS:
  35. case KVM_CAP_MAX_VCPU_ID:
  36. case KVM_CAP_MSI_DEVID:
  37. case KVM_CAP_ARM_VM_IPA_SIZE:
  38. case KVM_CAP_ARM_PMU_V3:
  39. case KVM_CAP_ARM_SVE:
  40. case KVM_CAP_ARM_PTRAUTH_ADDRESS:
  41. case KVM_CAP_ARM_PTRAUTH_GENERIC:
  42. return true;
  43. case KVM_CAP_ARM_MTE:
  44. return false;
  45. default:
  46. return !kvm || !kvm_vm_is_protected(kvm);
  47. }
  48. }
  49. /*
  50. * Check whether the KVM VM IOCTL is allowed in pKVM.
  51. *
  52. * Certain features are allowed only for non-protected VMs in pKVM, which is why
  53. * this takes the VM (kvm) as a parameter.
  54. */
  55. static inline bool kvm_pkvm_ioctl_allowed(struct kvm *kvm, unsigned int ioctl)
  56. {
  57. long ext;
  58. int r;
  59. r = kvm_get_cap_for_kvm_ioctl(ioctl, &ext);
  60. if (WARN_ON_ONCE(r < 0))
  61. return false;
  62. return kvm_pkvm_ext_allowed(kvm, ext);
  63. }
  64. extern struct memblock_region kvm_nvhe_sym(hyp_memory)[];
  65. extern unsigned int kvm_nvhe_sym(hyp_memblock_nr);
  66. static inline unsigned long
  67. hyp_vmemmap_memblock_size(struct memblock_region *reg, size_t vmemmap_entry_size)
  68. {
  69. unsigned long nr_pages = reg->size >> PAGE_SHIFT;
  70. unsigned long start, end;
  71. start = (reg->base >> PAGE_SHIFT) * vmemmap_entry_size;
  72. end = start + nr_pages * vmemmap_entry_size;
  73. start = ALIGN_DOWN(start, PAGE_SIZE);
  74. end = ALIGN(end, PAGE_SIZE);
  75. return end - start;
  76. }
  77. static inline unsigned long hyp_vmemmap_pages(size_t vmemmap_entry_size)
  78. {
  79. unsigned long res = 0, i;
  80. for (i = 0; i < kvm_nvhe_sym(hyp_memblock_nr); i++) {
  81. res += hyp_vmemmap_memblock_size(&kvm_nvhe_sym(hyp_memory)[i],
  82. vmemmap_entry_size);
  83. }
  84. return res >> PAGE_SHIFT;
  85. }
  86. static inline unsigned long hyp_vm_table_pages(void)
  87. {
  88. return PAGE_ALIGN(KVM_MAX_PVMS * sizeof(void *)) >> PAGE_SHIFT;
  89. }
  90. static inline unsigned long __hyp_pgtable_max_pages(unsigned long nr_pages)
  91. {
  92. unsigned long total = 0;
  93. int i;
  94. /* Provision the worst case scenario */
  95. for (i = KVM_PGTABLE_FIRST_LEVEL; i <= KVM_PGTABLE_LAST_LEVEL; i++) {
  96. nr_pages = DIV_ROUND_UP(nr_pages, PTRS_PER_PTE);
  97. total += nr_pages;
  98. }
  99. return total;
  100. }
  101. static inline unsigned long __hyp_pgtable_total_pages(void)
  102. {
  103. unsigned long res = 0, i;
  104. /* Cover all of memory with page-granularity */
  105. for (i = 0; i < kvm_nvhe_sym(hyp_memblock_nr); i++) {
  106. struct memblock_region *reg = &kvm_nvhe_sym(hyp_memory)[i];
  107. res += __hyp_pgtable_max_pages(reg->size >> PAGE_SHIFT);
  108. }
  109. return res;
  110. }
  111. static inline unsigned long hyp_s1_pgtable_pages(void)
  112. {
  113. unsigned long res;
  114. res = __hyp_pgtable_total_pages();
  115. /* Allow 1 GiB for private mappings */
  116. res += __hyp_pgtable_max_pages(SZ_1G >> PAGE_SHIFT);
  117. return res;
  118. }
  119. static inline unsigned long host_s2_pgtable_pages(void)
  120. {
  121. unsigned long res;
  122. /*
  123. * Include an extra 16 pages to safely upper-bound the worst case of
  124. * concatenated pgds.
  125. */
  126. res = __hyp_pgtable_total_pages() + 16;
  127. /* Allow 1 GiB for MMIO mappings */
  128. res += __hyp_pgtable_max_pages(SZ_1G >> PAGE_SHIFT);
  129. return res;
  130. }
  131. #ifdef CONFIG_NVHE_EL2_DEBUG
  132. static inline unsigned long pkvm_selftest_pages(void) { return 32; }
  133. #else
  134. static inline unsigned long pkvm_selftest_pages(void) { return 0; }
  135. #endif
  136. #define KVM_FFA_MBOX_NR_PAGES 1
  137. static inline unsigned long hyp_ffa_proxy_pages(void)
  138. {
  139. size_t desc_max;
  140. /*
  141. * The hypervisor FFA proxy needs enough memory to buffer a fragmented
  142. * descriptor returned from EL3 in response to a RETRIEVE_REQ call.
  143. */
  144. desc_max = sizeof(struct ffa_mem_region) +
  145. sizeof(struct ffa_mem_region_attributes) +
  146. sizeof(struct ffa_composite_mem_region) +
  147. SG_MAX_SEGMENTS * sizeof(struct ffa_mem_region_addr_range);
  148. /* Plus a page each for the hypervisor's RX and TX mailboxes. */
  149. return (2 * KVM_FFA_MBOX_NR_PAGES) + DIV_ROUND_UP(desc_max, PAGE_SIZE);
  150. }
  151. static inline size_t pkvm_host_sve_state_size(void)
  152. {
  153. if (!system_supports_sve())
  154. return 0;
  155. return size_add(sizeof(struct cpu_sve_state),
  156. SVE_SIG_REGS_SIZE(sve_vq_from_vl(kvm_host_sve_max_vl)));
  157. }
  158. struct pkvm_mapping {
  159. struct rb_node node;
  160. u64 gfn;
  161. u64 pfn;
  162. u64 nr_pages;
  163. u64 __subtree_last; /* Internal member for interval tree */
  164. };
  165. int pkvm_pgtable_stage2_init(struct kvm_pgtable *pgt, struct kvm_s2_mmu *mmu,
  166. struct kvm_pgtable_mm_ops *mm_ops);
  167. void pkvm_pgtable_stage2_destroy_range(struct kvm_pgtable *pgt,
  168. u64 addr, u64 size);
  169. void pkvm_pgtable_stage2_destroy_pgd(struct kvm_pgtable *pgt);
  170. int pkvm_pgtable_stage2_map(struct kvm_pgtable *pgt, u64 addr, u64 size, u64 phys,
  171. enum kvm_pgtable_prot prot, void *mc,
  172. enum kvm_pgtable_walk_flags flags);
  173. int pkvm_pgtable_stage2_unmap(struct kvm_pgtable *pgt, u64 addr, u64 size);
  174. int pkvm_pgtable_stage2_wrprotect(struct kvm_pgtable *pgt, u64 addr, u64 size);
  175. int pkvm_pgtable_stage2_flush(struct kvm_pgtable *pgt, u64 addr, u64 size);
  176. bool pkvm_pgtable_stage2_test_clear_young(struct kvm_pgtable *pgt, u64 addr, u64 size, bool mkold);
  177. int pkvm_pgtable_stage2_relax_perms(struct kvm_pgtable *pgt, u64 addr, enum kvm_pgtable_prot prot,
  178. enum kvm_pgtable_walk_flags flags);
  179. void pkvm_pgtable_stage2_mkyoung(struct kvm_pgtable *pgt, u64 addr,
  180. enum kvm_pgtable_walk_flags flags);
  181. int pkvm_pgtable_stage2_split(struct kvm_pgtable *pgt, u64 addr, u64 size,
  182. struct kvm_mmu_memory_cache *mc);
  183. void pkvm_pgtable_stage2_free_unlinked(struct kvm_pgtable_mm_ops *mm_ops, void *pgtable, s8 level);
  184. kvm_pte_t *pkvm_pgtable_stage2_create_unlinked(struct kvm_pgtable *pgt, u64 phys, s8 level,
  185. enum kvm_pgtable_prot prot, void *mc,
  186. bool force_pte);
  187. #endif /* __ARM64_KVM_PKVM_H__ */