1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2020 Google LLC 4 * Author: Quentin Perret <qperret@google.com> 5 */ 6 7 #include <linux/kvm_host.h> 8 #include <asm/kvm_hyp.h> 9 #include <asm/kvm_mmu.h> 10 #include <asm/kvm_pgtable.h> 11 12 #include <nvhe/early_alloc.h> 13 #include <nvhe/fixed_config.h> 14 #include <nvhe/gfp.h> 15 #include <nvhe/memory.h> 16 #include <nvhe/mem_protect.h> 17 #include <nvhe/mm.h> 18 #include <nvhe/trap_handler.h> 19 20 struct hyp_pool hpool; 21 unsigned long hyp_nr_cpus; 22 23 #define hyp_percpu_size ((unsigned long)__per_cpu_end - \ 24 (unsigned long)__per_cpu_start) 25 26 static void *vmemmap_base; 27 static void *hyp_pgt_base; 28 static void *host_s2_pgt_base; 29 static struct kvm_pgtable_mm_ops pkvm_pgtable_mm_ops; 30 31 static int divide_memory_pool(void *virt, unsigned long size) 32 { 33 unsigned long vstart, vend, nr_pages; 34 35 hyp_early_alloc_init(virt, size); 36 37 hyp_vmemmap_range(__hyp_pa(virt), size, &vstart, &vend); 38 nr_pages = (vend - vstart) >> PAGE_SHIFT; 39 vmemmap_base = hyp_early_alloc_contig(nr_pages); 40 if (!vmemmap_base) 41 return -ENOMEM; 42 43 nr_pages = hyp_s1_pgtable_pages(); 44 hyp_pgt_base = hyp_early_alloc_contig(nr_pages); 45 if (!hyp_pgt_base) 46 return -ENOMEM; 47 48 nr_pages = host_s2_pgtable_pages(); 49 host_s2_pgt_base = hyp_early_alloc_contig(nr_pages); 50 if (!host_s2_pgt_base) 51 return -ENOMEM; 52 53 return 0; 54 } 55 56 static int recreate_hyp_mappings(phys_addr_t phys, unsigned long size, 57 unsigned long *per_cpu_base, 58 u32 hyp_va_bits) 59 { 60 void *start, *end, *virt = hyp_phys_to_virt(phys); 61 unsigned long pgt_size = hyp_s1_pgtable_pages() << PAGE_SHIFT; 62 enum kvm_pgtable_prot prot; 63 int ret, i; 64 65 /* Recreate the hyp page-table using the early page allocator */ 66 hyp_early_alloc_init(hyp_pgt_base, pgt_size); 67 ret = kvm_pgtable_hyp_init(&pkvm_pgtable, hyp_va_bits, 68 &hyp_early_alloc_mm_ops); 69 if (ret) 70 return ret; 71 72 ret = hyp_create_idmap(hyp_va_bits); 73 if (ret) 74 return ret; 75 76 ret = hyp_map_vectors(); 77 if (ret) 78 return ret; 79 80 ret = hyp_back_vmemmap(phys, size, hyp_virt_to_phys(vmemmap_base)); 81 if (ret) 82 return ret; 83 84 ret = pkvm_create_mappings(__hyp_text_start, __hyp_text_end, PAGE_HYP_EXEC); 85 if (ret) 86 return ret; 87 88 ret = pkvm_create_mappings(__hyp_rodata_start, __hyp_rodata_end, PAGE_HYP_RO); 89 if (ret) 90 return ret; 91 92 ret = pkvm_create_mappings(__hyp_bss_start, __hyp_bss_end, PAGE_HYP); 93 if (ret) 94 return ret; 95 96 ret = pkvm_create_mappings(virt, virt + size, PAGE_HYP); 97 if (ret) 98 return ret; 99 100 for (i = 0; i < hyp_nr_cpus; i++) { 101 start = (void *)kern_hyp_va(per_cpu_base[i]); 102 end = start + PAGE_ALIGN(hyp_percpu_size); 103 ret = pkvm_create_mappings(start, end, PAGE_HYP); 104 if (ret) 105 return ret; 106 107 end = (void *)per_cpu_ptr(&kvm_init_params, i)->stack_hyp_va; 108 start = end - PAGE_SIZE; 109 ret = pkvm_create_mappings(start, end, PAGE_HYP); 110 if (ret) 111 return ret; 112 } 113 114 /* 115 * Map the host's .bss and .rodata sections RO in the hypervisor, but 116 * transfer the ownership from the host to the hypervisor itself to 117 * make sure it can't be donated or shared with another entity. 118 * 119 * The ownership transition requires matching changes in the host 120 * stage-2. This will be done later (see finalize_host_mappings()) once 121 * the hyp_vmemmap is addressable. 122 */ 123 prot = pkvm_mkstate(PAGE_HYP_RO, PKVM_PAGE_SHARED_OWNED); 124 ret = pkvm_create_mappings(__start_rodata, __end_rodata, prot); 125 if (ret) 126 return ret; 127 128 ret = pkvm_create_mappings(__hyp_bss_end, __bss_stop, prot); 129 if (ret) 130 return ret; 131 132 return 0; 133 } 134 135 static void update_nvhe_init_params(void) 136 { 137 struct kvm_nvhe_init_params *params; 138 unsigned long i; 139 140 for (i = 0; i < hyp_nr_cpus; i++) { 141 params = per_cpu_ptr(&kvm_init_params, i); 142 params->pgd_pa = __hyp_pa(pkvm_pgtable.pgd); 143 dcache_clean_inval_poc((unsigned long)params, 144 (unsigned long)params + sizeof(*params)); 145 } 146 } 147 148 static void *hyp_zalloc_hyp_page(void *arg) 149 { 150 return hyp_alloc_pages(&hpool, 0); 151 } 152 153 static void hpool_get_page(void *addr) 154 { 155 hyp_get_page(&hpool, addr); 156 } 157 158 static void hpool_put_page(void *addr) 159 { 160 hyp_put_page(&hpool, addr); 161 } 162 163 static int finalize_host_mappings_walker(u64 addr, u64 end, u32 level, 164 kvm_pte_t *ptep, 165 enum kvm_pgtable_walk_flags flag, 166 void * const arg) 167 { 168 enum kvm_pgtable_prot prot; 169 enum pkvm_page_state state; 170 kvm_pte_t pte = *ptep; 171 phys_addr_t phys; 172 173 if (!kvm_pte_valid(pte)) 174 return 0; 175 176 if (level != (KVM_PGTABLE_MAX_LEVELS - 1)) 177 return -EINVAL; 178 179 phys = kvm_pte_to_phys(pte); 180 if (!addr_is_memory(phys)) 181 return -EINVAL; 182 183 /* 184 * Adjust the host stage-2 mappings to match the ownership attributes 185 * configured in the hypervisor stage-1. 186 */ 187 state = pkvm_getstate(kvm_pgtable_hyp_pte_prot(pte)); 188 switch (state) { 189 case PKVM_PAGE_OWNED: 190 return host_stage2_set_owner_locked(phys, PAGE_SIZE, pkvm_hyp_id); 191 case PKVM_PAGE_SHARED_OWNED: 192 prot = pkvm_mkstate(PKVM_HOST_MEM_PROT, PKVM_PAGE_SHARED_BORROWED); 193 break; 194 case PKVM_PAGE_SHARED_BORROWED: 195 prot = pkvm_mkstate(PKVM_HOST_MEM_PROT, PKVM_PAGE_SHARED_OWNED); 196 break; 197 default: 198 return -EINVAL; 199 } 200 201 return host_stage2_idmap_locked(phys, PAGE_SIZE, prot); 202 } 203 204 static int finalize_host_mappings(void) 205 { 206 struct kvm_pgtable_walker walker = { 207 .cb = finalize_host_mappings_walker, 208 .flags = KVM_PGTABLE_WALK_LEAF, 209 }; 210 int i, ret; 211 212 for (i = 0; i < hyp_memblock_nr; i++) { 213 struct memblock_region *reg = &hyp_memory[i]; 214 u64 start = (u64)hyp_phys_to_virt(reg->base); 215 216 ret = kvm_pgtable_walk(&pkvm_pgtable, start, reg->size, &walker); 217 if (ret) 218 return ret; 219 } 220 221 return 0; 222 } 223 224 void __noreturn __pkvm_init_finalise(void) 225 { 226 struct kvm_host_data *host_data = this_cpu_ptr(&kvm_host_data); 227 struct kvm_cpu_context *host_ctxt = &host_data->host_ctxt; 228 unsigned long nr_pages, reserved_pages, pfn; 229 int ret; 230 231 /* Now that the vmemmap is backed, install the full-fledged allocator */ 232 pfn = hyp_virt_to_pfn(hyp_pgt_base); 233 nr_pages = hyp_s1_pgtable_pages(); 234 reserved_pages = hyp_early_alloc_nr_used_pages(); 235 ret = hyp_pool_init(&hpool, pfn, nr_pages, reserved_pages); 236 if (ret) 237 goto out; 238 239 ret = kvm_host_prepare_stage2(host_s2_pgt_base); 240 if (ret) 241 goto out; 242 243 ret = finalize_host_mappings(); 244 if (ret) 245 goto out; 246 247 pkvm_pgtable_mm_ops = (struct kvm_pgtable_mm_ops) { 248 .zalloc_page = hyp_zalloc_hyp_page, 249 .phys_to_virt = hyp_phys_to_virt, 250 .virt_to_phys = hyp_virt_to_phys, 251 .get_page = hpool_get_page, 252 .put_page = hpool_put_page, 253 }; 254 pkvm_pgtable.mm_ops = &pkvm_pgtable_mm_ops; 255 256 out: 257 /* 258 * We tail-called to here from handle___pkvm_init() and will not return, 259 * so make sure to propagate the return value to the host. 260 */ 261 cpu_reg(host_ctxt, 1) = ret; 262 263 __host_enter(host_ctxt); 264 } 265 266 int __pkvm_init(phys_addr_t phys, unsigned long size, unsigned long nr_cpus, 267 unsigned long *per_cpu_base, u32 hyp_va_bits) 268 { 269 struct kvm_nvhe_init_params *params; 270 void *virt = hyp_phys_to_virt(phys); 271 void (*fn)(phys_addr_t params_pa, void *finalize_fn_va); 272 int ret; 273 274 BUG_ON(kvm_check_pvm_sysreg_table()); 275 276 if (!PAGE_ALIGNED(phys) || !PAGE_ALIGNED(size)) 277 return -EINVAL; 278 279 hyp_spin_lock_init(&pkvm_pgd_lock); 280 hyp_nr_cpus = nr_cpus; 281 282 ret = divide_memory_pool(virt, size); 283 if (ret) 284 return ret; 285 286 ret = recreate_hyp_mappings(phys, size, per_cpu_base, hyp_va_bits); 287 if (ret) 288 return ret; 289 290 update_nvhe_init_params(); 291 292 /* Jump in the idmap page to switch to the new page-tables */ 293 params = this_cpu_ptr(&kvm_init_params); 294 fn = (typeof(fn))__hyp_pa(__pkvm_init_switch_pgd); 295 fn(__hyp_pa(params), __pkvm_init_finalise); 296 297 unreachable(); 298 } 299