1 // SPDX-License-Identifier: GPL-2.0
3 * ACRN: Memory mapping management
5 * Copyright (C) 2020 Intel Corporation. All rights reserved.
8 * Fei Li <lei1.li@intel.com>
9 * Shuo Liu <shuo.a.liu@intel.com>
14 #include <linux/slab.h>
18 static int modify_region(struct acrn_vm *vm, struct vm_memory_region_op *region)
20 struct vm_memory_region_batch *regions;
23 regions = kzalloc(sizeof(*regions), GFP_KERNEL);
27 regions->vmid = vm->vmid;
28 regions->regions_num = 1;
29 regions->regions_gpa = virt_to_phys(region);
31 ret = hcall_set_memory_regions(virt_to_phys(regions));
33 dev_dbg(acrn_dev.this_device,
34 "Failed to set memory region for VM[%u]!\n", vm->vmid);
41 * acrn_mm_region_add() - Set up the EPT mapping of a memory region.
43 * @user_gpa: A GPA of User VM.
44 * @service_gpa: A GPA of Service VM.
45 * @size: Size of the region.
46 * @mem_type: Combination of ACRN_MEM_TYPE_*.
47 * @mem_access_right: Combination of ACRN_MEM_ACCESS_*.
49 * Return: 0 on success, <0 on error.
51 int acrn_mm_region_add(struct acrn_vm *vm, u64 user_gpa, u64 service_gpa,
52 u64 size, u32 mem_type, u32 mem_access_right)
54 struct vm_memory_region_op *region;
57 region = kzalloc(sizeof(*region), GFP_KERNEL);
61 region->type = ACRN_MEM_REGION_ADD;
62 region->user_vm_pa = user_gpa;
63 region->service_vm_pa = service_gpa;
65 region->attr = ((mem_type & ACRN_MEM_TYPE_MASK) |
66 (mem_access_right & ACRN_MEM_ACCESS_RIGHT_MASK));
67 ret = modify_region(vm, region);
69 dev_dbg(acrn_dev.this_device,
70 "%s: user-GPA[%pK] service-GPA[%pK] size[0x%llx].\n",
71 __func__, (void *)user_gpa, (void *)service_gpa, size);
77 * acrn_mm_region_del() - Del the EPT mapping of a memory region.
79 * @user_gpa: A GPA of the User VM.
80 * @size: Size of the region.
82 * Return: 0 on success, <0 for error.
84 int acrn_mm_region_del(struct acrn_vm *vm, u64 user_gpa, u64 size)
86 struct vm_memory_region_op *region;
89 region = kzalloc(sizeof(*region), GFP_KERNEL);
93 region->type = ACRN_MEM_REGION_DEL;
94 region->user_vm_pa = user_gpa;
95 region->service_vm_pa = 0UL;
99 ret = modify_region(vm, region);
101 dev_dbg(acrn_dev.this_device, "%s: user-GPA[%pK] size[0x%llx].\n",
102 __func__, (void *)user_gpa, size);
107 int acrn_vm_memseg_map(struct acrn_vm *vm, struct acrn_vm_memmap *memmap)
111 if (memmap->type == ACRN_MEMMAP_RAM)
112 return acrn_vm_ram_map(vm, memmap);
114 if (memmap->type != ACRN_MEMMAP_MMIO) {
115 dev_dbg(acrn_dev.this_device,
116 "Invalid memmap type: %u\n", memmap->type);
120 ret = acrn_mm_region_add(vm, memmap->user_vm_pa,
121 memmap->service_vm_pa, memmap->len,
122 ACRN_MEM_TYPE_UC, memmap->attr);
124 dev_dbg(acrn_dev.this_device,
125 "Add memory region failed, VM[%u]!\n", vm->vmid);
130 int acrn_vm_memseg_unmap(struct acrn_vm *vm, struct acrn_vm_memmap *memmap)
134 if (memmap->type != ACRN_MEMMAP_MMIO) {
135 dev_dbg(acrn_dev.this_device,
136 "Invalid memmap type: %u\n", memmap->type);
140 ret = acrn_mm_region_del(vm, memmap->user_vm_pa, memmap->len);
142 dev_dbg(acrn_dev.this_device,
143 "Del memory region failed, VM[%u]!\n", vm->vmid);
149 * acrn_vm_ram_map() - Create a RAM EPT mapping of User VM.
150 * @vm: The User VM pointer
151 * @memmap: Info of the EPT mapping
153 * Return: 0 on success, <0 for error.
155 int acrn_vm_ram_map(struct acrn_vm *vm, struct acrn_vm_memmap *memmap)
157 struct vm_memory_region_batch *regions_info;
158 int nr_pages, i = 0, order, nr_regions = 0;
159 struct vm_memory_mapping *region_mapping;
160 struct vm_memory_region_op *vm_region;
161 struct page **pages = NULL, *page;
166 struct vm_area_struct *vma;
171 mmap_read_lock(current->mm);
172 vma = vma_lookup(current->mm, memmap->vma_base);
173 if (vma && ((vma->vm_flags & VM_PFNMAP) != 0)) {
174 if ((memmap->vma_base + memmap->len) > vma->vm_end) {
175 mmap_read_unlock(current->mm);
179 ret = follow_pfn(vma, memmap->vma_base, &pfn);
180 mmap_read_unlock(current->mm);
182 dev_dbg(acrn_dev.this_device,
183 "Failed to lookup PFN at VMA:%pK.\n", (void *)memmap->vma_base);
187 return acrn_mm_region_add(vm, memmap->user_vm_pa,
188 PFN_PHYS(pfn), memmap->len,
189 ACRN_MEM_TYPE_WB, memmap->attr);
191 mmap_read_unlock(current->mm);
193 /* Get the page number of the map region */
194 nr_pages = memmap->len >> PAGE_SHIFT;
195 pages = vzalloc(array_size(nr_pages, sizeof(*pages)));
199 /* Lock the pages of user memory map region */
200 pinned = pin_user_pages_fast(memmap->vma_base,
201 nr_pages, FOLL_WRITE | FOLL_LONGTERM,
206 } else if (pinned != nr_pages) {
211 /* Create a kernel map for the map region */
212 remap_vaddr = vmap(pages, nr_pages, VM_MAP, PAGE_KERNEL);
218 /* Record Service VM va <-> User VM pa mapping */
219 mutex_lock(&vm->regions_mapping_lock);
220 region_mapping = &vm->regions_mapping[vm->regions_mapping_count];
221 if (vm->regions_mapping_count < ACRN_MEM_MAPPING_MAX) {
222 region_mapping->pages = pages;
223 region_mapping->npages = nr_pages;
224 region_mapping->size = memmap->len;
225 region_mapping->service_vm_va = remap_vaddr;
226 region_mapping->user_vm_pa = memmap->user_vm_pa;
227 vm->regions_mapping_count++;
229 dev_warn(acrn_dev.this_device,
230 "Run out of memory mapping slots!\n");
232 mutex_unlock(&vm->regions_mapping_lock);
235 mutex_unlock(&vm->regions_mapping_lock);
237 /* Calculate count of vm_memory_region_op */
238 while (i < nr_pages) {
240 VM_BUG_ON_PAGE(PageTail(page), page);
241 order = compound_order(page);
246 /* Prepare the vm_memory_region_batch */
247 regions_info = kzalloc(struct_size(regions_info, regions_op,
248 nr_regions), GFP_KERNEL);
251 goto unmap_kernel_map;
254 /* Fill each vm_memory_region_op */
255 vm_region = regions_info->regions_op;
256 regions_info->vmid = vm->vmid;
257 regions_info->regions_num = nr_regions;
258 regions_info->regions_gpa = virt_to_phys(vm_region);
259 user_vm_pa = memmap->user_vm_pa;
261 while (i < nr_pages) {
265 VM_BUG_ON_PAGE(PageTail(page), page);
266 order = compound_order(page);
267 region_size = PAGE_SIZE << order;
268 vm_region->type = ACRN_MEM_REGION_ADD;
269 vm_region->user_vm_pa = user_vm_pa;
270 vm_region->service_vm_pa = page_to_phys(page);
271 vm_region->size = region_size;
272 vm_region->attr = (ACRN_MEM_TYPE_WB & ACRN_MEM_TYPE_MASK) |
273 (memmap->attr & ACRN_MEM_ACCESS_RIGHT_MASK);
276 user_vm_pa += region_size;
280 /* Inform the ACRN Hypervisor to set up EPT mappings */
281 ret = hcall_set_memory_regions(virt_to_phys(regions_info));
283 dev_dbg(acrn_dev.this_device,
284 "Failed to set regions, VM[%u]!\n", vm->vmid);
289 dev_dbg(acrn_dev.this_device,
290 "%s: VM[%u] service-GVA[%pK] user-GPA[%pK] size[0x%llx]\n",
292 remap_vaddr, (void *)memmap->user_vm_pa, memmap->len);
298 mutex_lock(&vm->regions_mapping_lock);
299 vm->regions_mapping_count--;
300 mutex_unlock(&vm->regions_mapping_lock);
304 for (i = 0; i < pinned; i++)
305 unpin_user_page(pages[i]);
312 * acrn_vm_all_ram_unmap() - Destroy a RAM EPT mapping of User VM.
315 void acrn_vm_all_ram_unmap(struct acrn_vm *vm)
317 struct vm_memory_mapping *region_mapping;
320 mutex_lock(&vm->regions_mapping_lock);
321 for (i = 0; i < vm->regions_mapping_count; i++) {
322 region_mapping = &vm->regions_mapping[i];
323 vunmap(region_mapping->service_vm_va);
324 for (j = 0; j < region_mapping->npages; j++)
325 unpin_user_page(region_mapping->pages[j]);
326 vfree(region_mapping->pages);
328 mutex_unlock(&vm->regions_mapping_lock);