1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * ACRN: Memory mapping management 4 * 5 * Copyright (C) 2020 Intel Corporation. All rights reserved. 6 * 7 * Authors: 8 * Fei Li <lei1.li@intel.com> 9 * Shuo Liu <shuo.a.liu@intel.com> 10 */ 11 12 #include <linux/io.h> 13 #include <linux/mm.h> 14 #include <linux/slab.h> 15 #include <linux/vmalloc.h> 16 17 #include "acrn_drv.h" 18 19 static int modify_region(struct acrn_vm *vm, struct vm_memory_region_op *region) 20 { 21 struct vm_memory_region_batch *regions; 22 int ret; 23 24 regions = kzalloc(sizeof(*regions), GFP_KERNEL); 25 if (!regions) 26 return -ENOMEM; 27 28 regions->vmid = vm->vmid; 29 regions->regions_num = 1; 30 regions->regions_gpa = virt_to_phys(region); 31 32 ret = hcall_set_memory_regions(virt_to_phys(regions)); 33 if (ret < 0) 34 dev_dbg(acrn_dev.this_device, 35 "Failed to set memory region for VM[%u]!\n", vm->vmid); 36 37 kfree(regions); 38 return ret; 39 } 40 41 /** 42 * acrn_mm_region_add() - Set up the EPT mapping of a memory region. 43 * @vm: User VM. 44 * @user_gpa: A GPA of User VM. 45 * @service_gpa: A GPA of Service VM. 46 * @size: Size of the region. 47 * @mem_type: Combination of ACRN_MEM_TYPE_*. 48 * @mem_access_right: Combination of ACRN_MEM_ACCESS_*. 49 * 50 * Return: 0 on success, <0 on error. 51 */ 52 int acrn_mm_region_add(struct acrn_vm *vm, u64 user_gpa, u64 service_gpa, 53 u64 size, u32 mem_type, u32 mem_access_right) 54 { 55 struct vm_memory_region_op *region; 56 int ret = 0; 57 58 region = kzalloc(sizeof(*region), GFP_KERNEL); 59 if (!region) 60 return -ENOMEM; 61 62 region->type = ACRN_MEM_REGION_ADD; 63 region->user_vm_pa = user_gpa; 64 region->service_vm_pa = service_gpa; 65 region->size = size; 66 region->attr = ((mem_type & ACRN_MEM_TYPE_MASK) | 67 (mem_access_right & ACRN_MEM_ACCESS_RIGHT_MASK)); 68 ret = modify_region(vm, region); 69 70 dev_dbg(acrn_dev.this_device, 71 "%s: user-GPA[%pK] service-GPA[%pK] size[0x%llx].\n", 72 __func__, (void *)user_gpa, (void *)service_gpa, size); 73 kfree(region); 74 return ret; 75 } 76 77 /** 78 * acrn_mm_region_del() - Del the EPT mapping of a memory region. 79 * @vm: User VM. 80 * @user_gpa: A GPA of the User VM. 81 * @size: Size of the region. 82 * 83 * Return: 0 on success, <0 for error. 84 */ 85 int acrn_mm_region_del(struct acrn_vm *vm, u64 user_gpa, u64 size) 86 { 87 struct vm_memory_region_op *region; 88 int ret = 0; 89 90 region = kzalloc(sizeof(*region), GFP_KERNEL); 91 if (!region) 92 return -ENOMEM; 93 94 region->type = ACRN_MEM_REGION_DEL; 95 region->user_vm_pa = user_gpa; 96 region->service_vm_pa = 0UL; 97 region->size = size; 98 region->attr = 0U; 99 100 ret = modify_region(vm, region); 101 102 dev_dbg(acrn_dev.this_device, "%s: user-GPA[%pK] size[0x%llx].\n", 103 __func__, (void *)user_gpa, size); 104 kfree(region); 105 return ret; 106 } 107 108 int acrn_vm_memseg_map(struct acrn_vm *vm, struct acrn_vm_memmap *memmap) 109 { 110 int ret; 111 112 if (memmap->type == ACRN_MEMMAP_RAM) 113 return acrn_vm_ram_map(vm, memmap); 114 115 if (memmap->type != ACRN_MEMMAP_MMIO) { 116 dev_dbg(acrn_dev.this_device, 117 "Invalid memmap type: %u\n", memmap->type); 118 return -EINVAL; 119 } 120 121 ret = acrn_mm_region_add(vm, memmap->user_vm_pa, 122 memmap->service_vm_pa, memmap->len, 123 ACRN_MEM_TYPE_UC, memmap->attr); 124 if (ret < 0) 125 dev_dbg(acrn_dev.this_device, 126 "Add memory region failed, VM[%u]!\n", vm->vmid); 127 128 return ret; 129 } 130 131 int acrn_vm_memseg_unmap(struct acrn_vm *vm, struct acrn_vm_memmap *memmap) 132 { 133 int ret; 134 135 if (memmap->type != ACRN_MEMMAP_MMIO) { 136 dev_dbg(acrn_dev.this_device, 137 "Invalid memmap type: %u\n", memmap->type); 138 return -EINVAL; 139 } 140 141 ret = acrn_mm_region_del(vm, memmap->user_vm_pa, memmap->len); 142 if (ret < 0) 143 dev_dbg(acrn_dev.this_device, 144 "Del memory region failed, VM[%u]!\n", vm->vmid); 145 146 return ret; 147 } 148 149 /** 150 * acrn_vm_ram_map() - Create a RAM EPT mapping of User VM. 151 * @vm: The User VM pointer 152 * @memmap: Info of the EPT mapping 153 * 154 * Return: 0 on success, <0 for error. 155 */ 156 int acrn_vm_ram_map(struct acrn_vm *vm, struct acrn_vm_memmap *memmap) 157 { 158 struct vm_memory_region_batch *regions_info; 159 int nr_pages, i = 0, order, nr_regions = 0; 160 struct vm_memory_mapping *region_mapping; 161 struct vm_memory_region_op *vm_region; 162 struct page **pages = NULL, *page; 163 void *remap_vaddr; 164 int ret, pinned; 165 u64 user_vm_pa; 166 unsigned long pfn; 167 struct vm_area_struct *vma; 168 169 if (!vm || !memmap) 170 return -EINVAL; 171 172 mmap_read_lock(current->mm); 173 vma = vma_lookup(current->mm, memmap->vma_base); 174 if (vma && ((vma->vm_flags & VM_PFNMAP) != 0)) { 175 if ((memmap->vma_base + memmap->len) > vma->vm_end) { 176 mmap_read_unlock(current->mm); 177 return -EINVAL; 178 } 179 180 ret = follow_pfn(vma, memmap->vma_base, &pfn); 181 mmap_read_unlock(current->mm); 182 if (ret < 0) { 183 dev_dbg(acrn_dev.this_device, 184 "Failed to lookup PFN at VMA:%pK.\n", (void *)memmap->vma_base); 185 return ret; 186 } 187 188 return acrn_mm_region_add(vm, memmap->user_vm_pa, 189 PFN_PHYS(pfn), memmap->len, 190 ACRN_MEM_TYPE_WB, memmap->attr); 191 } 192 mmap_read_unlock(current->mm); 193 194 /* Get the page number of the map region */ 195 nr_pages = memmap->len >> PAGE_SHIFT; 196 pages = vzalloc(array_size(nr_pages, sizeof(*pages))); 197 if (!pages) 198 return -ENOMEM; 199 200 /* Lock the pages of user memory map region */ 201 pinned = pin_user_pages_fast(memmap->vma_base, 202 nr_pages, FOLL_WRITE | FOLL_LONGTERM, 203 pages); 204 if (pinned < 0) { 205 ret = pinned; 206 goto free_pages; 207 } else if (pinned != nr_pages) { 208 ret = -EFAULT; 209 goto put_pages; 210 } 211 212 /* Create a kernel map for the map region */ 213 remap_vaddr = vmap(pages, nr_pages, VM_MAP, PAGE_KERNEL); 214 if (!remap_vaddr) { 215 ret = -ENOMEM; 216 goto put_pages; 217 } 218 219 /* Record Service VM va <-> User VM pa mapping */ 220 mutex_lock(&vm->regions_mapping_lock); 221 region_mapping = &vm->regions_mapping[vm->regions_mapping_count]; 222 if (vm->regions_mapping_count < ACRN_MEM_MAPPING_MAX) { 223 region_mapping->pages = pages; 224 region_mapping->npages = nr_pages; 225 region_mapping->size = memmap->len; 226 region_mapping->service_vm_va = remap_vaddr; 227 region_mapping->user_vm_pa = memmap->user_vm_pa; 228 vm->regions_mapping_count++; 229 } else { 230 dev_warn(acrn_dev.this_device, 231 "Run out of memory mapping slots!\n"); 232 ret = -ENOMEM; 233 mutex_unlock(&vm->regions_mapping_lock); 234 goto unmap_no_count; 235 } 236 mutex_unlock(&vm->regions_mapping_lock); 237 238 /* Calculate count of vm_memory_region_op */ 239 while (i < nr_pages) { 240 page = pages[i]; 241 VM_BUG_ON_PAGE(PageTail(page), page); 242 order = compound_order(page); 243 nr_regions++; 244 i += 1 << order; 245 } 246 247 /* Prepare the vm_memory_region_batch */ 248 regions_info = kzalloc(struct_size(regions_info, regions_op, 249 nr_regions), GFP_KERNEL); 250 if (!regions_info) { 251 ret = -ENOMEM; 252 goto unmap_kernel_map; 253 } 254 regions_info->regions_num = nr_regions; 255 256 /* Fill each vm_memory_region_op */ 257 vm_region = regions_info->regions_op; 258 regions_info->vmid = vm->vmid; 259 regions_info->regions_gpa = virt_to_phys(vm_region); 260 user_vm_pa = memmap->user_vm_pa; 261 i = 0; 262 while (i < nr_pages) { 263 u32 region_size; 264 265 page = pages[i]; 266 VM_BUG_ON_PAGE(PageTail(page), page); 267 order = compound_order(page); 268 region_size = PAGE_SIZE << order; 269 vm_region->type = ACRN_MEM_REGION_ADD; 270 vm_region->user_vm_pa = user_vm_pa; 271 vm_region->service_vm_pa = page_to_phys(page); 272 vm_region->size = region_size; 273 vm_region->attr = (ACRN_MEM_TYPE_WB & ACRN_MEM_TYPE_MASK) | 274 (memmap->attr & ACRN_MEM_ACCESS_RIGHT_MASK); 275 276 vm_region++; 277 user_vm_pa += region_size; 278 i += 1 << order; 279 } 280 281 /* Inform the ACRN Hypervisor to set up EPT mappings */ 282 ret = hcall_set_memory_regions(virt_to_phys(regions_info)); 283 if (ret < 0) { 284 dev_dbg(acrn_dev.this_device, 285 "Failed to set regions, VM[%u]!\n", vm->vmid); 286 goto unset_region; 287 } 288 kfree(regions_info); 289 290 dev_dbg(acrn_dev.this_device, 291 "%s: VM[%u] service-GVA[%pK] user-GPA[%pK] size[0x%llx]\n", 292 __func__, vm->vmid, 293 remap_vaddr, (void *)memmap->user_vm_pa, memmap->len); 294 return ret; 295 296 unset_region: 297 kfree(regions_info); 298 unmap_kernel_map: 299 mutex_lock(&vm->regions_mapping_lock); 300 vm->regions_mapping_count--; 301 mutex_unlock(&vm->regions_mapping_lock); 302 unmap_no_count: 303 vunmap(remap_vaddr); 304 put_pages: 305 for (i = 0; i < pinned; i++) 306 unpin_user_page(pages[i]); 307 free_pages: 308 vfree(pages); 309 return ret; 310 } 311 312 /** 313 * acrn_vm_all_ram_unmap() - Destroy a RAM EPT mapping of User VM. 314 * @vm: The User VM 315 */ 316 void acrn_vm_all_ram_unmap(struct acrn_vm *vm) 317 { 318 struct vm_memory_mapping *region_mapping; 319 int i, j; 320 321 mutex_lock(&vm->regions_mapping_lock); 322 for (i = 0; i < vm->regions_mapping_count; i++) { 323 region_mapping = &vm->regions_mapping[i]; 324 vunmap(region_mapping->service_vm_va); 325 for (j = 0; j < region_mapping->npages; j++) 326 unpin_user_page(region_mapping->pages[j]); 327 vfree(region_mapping->pages); 328 } 329 mutex_unlock(&vm->regions_mapping_lock); 330 } 331