Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (C) 2020 Google LLC
0004  * Author: Quentin Perret <qperret@google.com>
0005  */
0006 
0007 #include <linux/kvm_host.h>
0008 #include <asm/kvm_hyp.h>
0009 #include <asm/kvm_mmu.h>
0010 #include <asm/kvm_pgtable.h>
0011 #include <asm/kvm_pkvm.h>
0012 
0013 #include <nvhe/early_alloc.h>
0014 #include <nvhe/fixed_config.h>
0015 #include <nvhe/gfp.h>
0016 #include <nvhe/memory.h>
0017 #include <nvhe/mem_protect.h>
0018 #include <nvhe/mm.h>
0019 #include <nvhe/trap_handler.h>
0020 
0021 unsigned long hyp_nr_cpus;
0022 
0023 #define hyp_percpu_size ((unsigned long)__per_cpu_end - \
0024              (unsigned long)__per_cpu_start)
0025 
0026 static void *vmemmap_base;
0027 static void *hyp_pgt_base;
0028 static void *host_s2_pgt_base;
0029 static struct kvm_pgtable_mm_ops pkvm_pgtable_mm_ops;
0030 static struct hyp_pool hpool;
0031 
0032 static int divide_memory_pool(void *virt, unsigned long size)
0033 {
0034     unsigned long vstart, vend, nr_pages;
0035 
0036     hyp_early_alloc_init(virt, size);
0037 
0038     hyp_vmemmap_range(__hyp_pa(virt), size, &vstart, &vend);
0039     nr_pages = (vend - vstart) >> PAGE_SHIFT;
0040     vmemmap_base = hyp_early_alloc_contig(nr_pages);
0041     if (!vmemmap_base)
0042         return -ENOMEM;
0043 
0044     nr_pages = hyp_s1_pgtable_pages();
0045     hyp_pgt_base = hyp_early_alloc_contig(nr_pages);
0046     if (!hyp_pgt_base)
0047         return -ENOMEM;
0048 
0049     nr_pages = host_s2_pgtable_pages();
0050     host_s2_pgt_base = hyp_early_alloc_contig(nr_pages);
0051     if (!host_s2_pgt_base)
0052         return -ENOMEM;
0053 
0054     return 0;
0055 }
0056 
0057 static int recreate_hyp_mappings(phys_addr_t phys, unsigned long size,
0058                  unsigned long *per_cpu_base,
0059                  u32 hyp_va_bits)
0060 {
0061     void *start, *end, *virt = hyp_phys_to_virt(phys);
0062     unsigned long pgt_size = hyp_s1_pgtable_pages() << PAGE_SHIFT;
0063     enum kvm_pgtable_prot prot;
0064     int ret, i;
0065 
0066     /* Recreate the hyp page-table using the early page allocator */
0067     hyp_early_alloc_init(hyp_pgt_base, pgt_size);
0068     ret = kvm_pgtable_hyp_init(&pkvm_pgtable, hyp_va_bits,
0069                    &hyp_early_alloc_mm_ops);
0070     if (ret)
0071         return ret;
0072 
0073     ret = hyp_create_idmap(hyp_va_bits);
0074     if (ret)
0075         return ret;
0076 
0077     ret = hyp_map_vectors();
0078     if (ret)
0079         return ret;
0080 
0081     ret = hyp_back_vmemmap(phys, size, hyp_virt_to_phys(vmemmap_base));
0082     if (ret)
0083         return ret;
0084 
0085     ret = pkvm_create_mappings(__hyp_text_start, __hyp_text_end, PAGE_HYP_EXEC);
0086     if (ret)
0087         return ret;
0088 
0089     ret = pkvm_create_mappings(__hyp_rodata_start, __hyp_rodata_end, PAGE_HYP_RO);
0090     if (ret)
0091         return ret;
0092 
0093     ret = pkvm_create_mappings(__hyp_bss_start, __hyp_bss_end, PAGE_HYP);
0094     if (ret)
0095         return ret;
0096 
0097     ret = pkvm_create_mappings(virt, virt + size, PAGE_HYP);
0098     if (ret)
0099         return ret;
0100 
0101     for (i = 0; i < hyp_nr_cpus; i++) {
0102         struct kvm_nvhe_init_params *params = per_cpu_ptr(&kvm_init_params, i);
0103         unsigned long hyp_addr;
0104 
0105         start = (void *)kern_hyp_va(per_cpu_base[i]);
0106         end = start + PAGE_ALIGN(hyp_percpu_size);
0107         ret = pkvm_create_mappings(start, end, PAGE_HYP);
0108         if (ret)
0109             return ret;
0110 
0111         /*
0112          * Allocate a contiguous HYP private VA range for the stack
0113          * and guard page. The allocation is also aligned based on
0114          * the order of its size.
0115          */
0116         ret = pkvm_alloc_private_va_range(PAGE_SIZE * 2, &hyp_addr);
0117         if (ret)
0118             return ret;
0119 
0120         /*
0121          * Since the stack grows downwards, map the stack to the page
0122          * at the higher address and leave the lower guard page
0123          * unbacked.
0124          *
0125          * Any valid stack address now has the PAGE_SHIFT bit as 1
0126          * and addresses corresponding to the guard page have the
0127          * PAGE_SHIFT bit as 0 - this is used for overflow detection.
0128          */
0129         hyp_spin_lock(&pkvm_pgd_lock);
0130         ret = kvm_pgtable_hyp_map(&pkvm_pgtable, hyp_addr + PAGE_SIZE,
0131                     PAGE_SIZE, params->stack_pa, PAGE_HYP);
0132         hyp_spin_unlock(&pkvm_pgd_lock);
0133         if (ret)
0134             return ret;
0135 
0136         /* Update stack_hyp_va to end of the stack's private VA range */
0137         params->stack_hyp_va = hyp_addr + (2 * PAGE_SIZE);
0138     }
0139 
0140     /*
0141      * Map the host's .bss and .rodata sections RO in the hypervisor, but
0142      * transfer the ownership from the host to the hypervisor itself to
0143      * make sure it can't be donated or shared with another entity.
0144      *
0145      * The ownership transition requires matching changes in the host
0146      * stage-2. This will be done later (see finalize_host_mappings()) once
0147      * the hyp_vmemmap is addressable.
0148      */
0149     prot = pkvm_mkstate(PAGE_HYP_RO, PKVM_PAGE_SHARED_OWNED);
0150     ret = pkvm_create_mappings(__start_rodata, __end_rodata, prot);
0151     if (ret)
0152         return ret;
0153 
0154     ret = pkvm_create_mappings(__hyp_bss_end, __bss_stop, prot);
0155     if (ret)
0156         return ret;
0157 
0158     return 0;
0159 }
0160 
0161 static void update_nvhe_init_params(void)
0162 {
0163     struct kvm_nvhe_init_params *params;
0164     unsigned long i;
0165 
0166     for (i = 0; i < hyp_nr_cpus; i++) {
0167         params = per_cpu_ptr(&kvm_init_params, i);
0168         params->pgd_pa = __hyp_pa(pkvm_pgtable.pgd);
0169         dcache_clean_inval_poc((unsigned long)params,
0170                     (unsigned long)params + sizeof(*params));
0171     }
0172 }
0173 
0174 static void *hyp_zalloc_hyp_page(void *arg)
0175 {
0176     return hyp_alloc_pages(&hpool, 0);
0177 }
0178 
0179 static void hpool_get_page(void *addr)
0180 {
0181     hyp_get_page(&hpool, addr);
0182 }
0183 
0184 static void hpool_put_page(void *addr)
0185 {
0186     hyp_put_page(&hpool, addr);
0187 }
0188 
0189 static int finalize_host_mappings_walker(u64 addr, u64 end, u32 level,
0190                      kvm_pte_t *ptep,
0191                      enum kvm_pgtable_walk_flags flag,
0192                      void * const arg)
0193 {
0194     struct kvm_pgtable_mm_ops *mm_ops = arg;
0195     enum kvm_pgtable_prot prot;
0196     enum pkvm_page_state state;
0197     kvm_pte_t pte = *ptep;
0198     phys_addr_t phys;
0199 
0200     if (!kvm_pte_valid(pte))
0201         return 0;
0202 
0203     /*
0204      * Fix-up the refcount for the page-table pages as the early allocator
0205      * was unable to access the hyp_vmemmap and so the buddy allocator has
0206      * initialised the refcount to '1'.
0207      */
0208     mm_ops->get_page(ptep);
0209     if (flag != KVM_PGTABLE_WALK_LEAF)
0210         return 0;
0211 
0212     if (level != (KVM_PGTABLE_MAX_LEVELS - 1))
0213         return -EINVAL;
0214 
0215     phys = kvm_pte_to_phys(pte);
0216     if (!addr_is_memory(phys))
0217         return -EINVAL;
0218 
0219     /*
0220      * Adjust the host stage-2 mappings to match the ownership attributes
0221      * configured in the hypervisor stage-1.
0222      */
0223     state = pkvm_getstate(kvm_pgtable_hyp_pte_prot(pte));
0224     switch (state) {
0225     case PKVM_PAGE_OWNED:
0226         return host_stage2_set_owner_locked(phys, PAGE_SIZE, pkvm_hyp_id);
0227     case PKVM_PAGE_SHARED_OWNED:
0228         prot = pkvm_mkstate(PKVM_HOST_MEM_PROT, PKVM_PAGE_SHARED_BORROWED);
0229         break;
0230     case PKVM_PAGE_SHARED_BORROWED:
0231         prot = pkvm_mkstate(PKVM_HOST_MEM_PROT, PKVM_PAGE_SHARED_OWNED);
0232         break;
0233     default:
0234         return -EINVAL;
0235     }
0236 
0237     return host_stage2_idmap_locked(phys, PAGE_SIZE, prot);
0238 }
0239 
0240 static int finalize_host_mappings(void)
0241 {
0242     struct kvm_pgtable_walker walker = {
0243         .cb = finalize_host_mappings_walker,
0244         .flags  = KVM_PGTABLE_WALK_LEAF | KVM_PGTABLE_WALK_TABLE_POST,
0245         .arg    = pkvm_pgtable.mm_ops,
0246     };
0247     int i, ret;
0248 
0249     for (i = 0; i < hyp_memblock_nr; i++) {
0250         struct memblock_region *reg = &hyp_memory[i];
0251         u64 start = (u64)hyp_phys_to_virt(reg->base);
0252 
0253         ret = kvm_pgtable_walk(&pkvm_pgtable, start, reg->size, &walker);
0254         if (ret)
0255             return ret;
0256     }
0257 
0258     return 0;
0259 }
0260 
0261 void __noreturn __pkvm_init_finalise(void)
0262 {
0263     struct kvm_host_data *host_data = this_cpu_ptr(&kvm_host_data);
0264     struct kvm_cpu_context *host_ctxt = &host_data->host_ctxt;
0265     unsigned long nr_pages, reserved_pages, pfn;
0266     int ret;
0267 
0268     /* Now that the vmemmap is backed, install the full-fledged allocator */
0269     pfn = hyp_virt_to_pfn(hyp_pgt_base);
0270     nr_pages = hyp_s1_pgtable_pages();
0271     reserved_pages = hyp_early_alloc_nr_used_pages();
0272     ret = hyp_pool_init(&hpool, pfn, nr_pages, reserved_pages);
0273     if (ret)
0274         goto out;
0275 
0276     ret = kvm_host_prepare_stage2(host_s2_pgt_base);
0277     if (ret)
0278         goto out;
0279 
0280     pkvm_pgtable_mm_ops = (struct kvm_pgtable_mm_ops) {
0281         .zalloc_page = hyp_zalloc_hyp_page,
0282         .phys_to_virt = hyp_phys_to_virt,
0283         .virt_to_phys = hyp_virt_to_phys,
0284         .get_page = hpool_get_page,
0285         .put_page = hpool_put_page,
0286         .page_count = hyp_page_count,
0287     };
0288     pkvm_pgtable.mm_ops = &pkvm_pgtable_mm_ops;
0289 
0290     ret = finalize_host_mappings();
0291     if (ret)
0292         goto out;
0293 
0294 out:
0295     /*
0296      * We tail-called to here from handle___pkvm_init() and will not return,
0297      * so make sure to propagate the return value to the host.
0298      */
0299     cpu_reg(host_ctxt, 1) = ret;
0300 
0301     __host_enter(host_ctxt);
0302 }
0303 
0304 int __pkvm_init(phys_addr_t phys, unsigned long size, unsigned long nr_cpus,
0305         unsigned long *per_cpu_base, u32 hyp_va_bits)
0306 {
0307     struct kvm_nvhe_init_params *params;
0308     void *virt = hyp_phys_to_virt(phys);
0309     void (*fn)(phys_addr_t params_pa, void *finalize_fn_va);
0310     int ret;
0311 
0312     BUG_ON(kvm_check_pvm_sysreg_table());
0313 
0314     if (!PAGE_ALIGNED(phys) || !PAGE_ALIGNED(size))
0315         return -EINVAL;
0316 
0317     hyp_spin_lock_init(&pkvm_pgd_lock);
0318     hyp_nr_cpus = nr_cpus;
0319 
0320     ret = divide_memory_pool(virt, size);
0321     if (ret)
0322         return ret;
0323 
0324     ret = recreate_hyp_mappings(phys, size, per_cpu_base, hyp_va_bits);
0325     if (ret)
0326         return ret;
0327 
0328     update_nvhe_init_params();
0329 
0330     /* Jump in the idmap page to switch to the new page-tables */
0331     params = this_cpu_ptr(&kvm_init_params);
0332     fn = (typeof(fn))__hyp_pa(__pkvm_init_switch_pgd);
0333     fn(__hyp_pa(params), __pkvm_init_finalise);
0334 
0335     unreachable();
0336 }