Lines Matching +full:vm +full:- +full:map
1 // SPDX-License-Identifier: GPL-2.0
18 static int modify_region(struct acrn_vm *vm, struct vm_memory_region_op *region) in modify_region() argument
25 return -ENOMEM; in modify_region()
27 regions->vmid = vm->vmid; in modify_region()
28 regions->regions_num = 1; in modify_region()
29 regions->regions_gpa = virt_to_phys(region); in modify_region()
34 "Failed to set memory region for VM[%u]!\n", vm->vmid); in modify_region()
41 * acrn_mm_region_add() - Set up the EPT mapping of a memory region.
42 * @vm: User VM.
43 * @user_gpa: A GPA of User VM.
44 * @service_gpa: A GPA of Service VM.
51 int acrn_mm_region_add(struct acrn_vm *vm, u64 user_gpa, u64 service_gpa, in acrn_mm_region_add() argument
59 return -ENOMEM; in acrn_mm_region_add()
61 region->type = ACRN_MEM_REGION_ADD; in acrn_mm_region_add()
62 region->user_vm_pa = user_gpa; in acrn_mm_region_add()
63 region->service_vm_pa = service_gpa; in acrn_mm_region_add()
64 region->size = size; in acrn_mm_region_add()
65 region->attr = ((mem_type & ACRN_MEM_TYPE_MASK) | in acrn_mm_region_add()
67 ret = modify_region(vm, region); in acrn_mm_region_add()
70 "%s: user-GPA[%pK] service-GPA[%pK] size[0x%llx].\n", in acrn_mm_region_add()
77 * acrn_mm_region_del() - Del the EPT mapping of a memory region.
78 * @vm: User VM.
79 * @user_gpa: A GPA of the User VM.
84 int acrn_mm_region_del(struct acrn_vm *vm, u64 user_gpa, u64 size) in acrn_mm_region_del() argument
91 return -ENOMEM; in acrn_mm_region_del()
93 region->type = ACRN_MEM_REGION_DEL; in acrn_mm_region_del()
94 region->user_vm_pa = user_gpa; in acrn_mm_region_del()
95 region->service_vm_pa = 0UL; in acrn_mm_region_del()
96 region->size = size; in acrn_mm_region_del()
97 region->attr = 0U; in acrn_mm_region_del()
99 ret = modify_region(vm, region); in acrn_mm_region_del()
101 dev_dbg(acrn_dev.this_device, "%s: user-GPA[%pK] size[0x%llx].\n", in acrn_mm_region_del()
107 int acrn_vm_memseg_map(struct acrn_vm *vm, struct acrn_vm_memmap *memmap) in acrn_vm_memseg_map() argument
111 if (memmap->type == ACRN_MEMMAP_RAM) in acrn_vm_memseg_map()
112 return acrn_vm_ram_map(vm, memmap); in acrn_vm_memseg_map()
114 if (memmap->type != ACRN_MEMMAP_MMIO) { in acrn_vm_memseg_map()
116 "Invalid memmap type: %u\n", memmap->type); in acrn_vm_memseg_map()
117 return -EINVAL; in acrn_vm_memseg_map()
120 ret = acrn_mm_region_add(vm, memmap->user_vm_pa, in acrn_vm_memseg_map()
121 memmap->service_vm_pa, memmap->len, in acrn_vm_memseg_map()
122 ACRN_MEM_TYPE_UC, memmap->attr); in acrn_vm_memseg_map()
125 "Add memory region failed, VM[%u]!\n", vm->vmid); in acrn_vm_memseg_map()
130 int acrn_vm_memseg_unmap(struct acrn_vm *vm, struct acrn_vm_memmap *memmap) in acrn_vm_memseg_unmap() argument
134 if (memmap->type != ACRN_MEMMAP_MMIO) { in acrn_vm_memseg_unmap()
136 "Invalid memmap type: %u\n", memmap->type); in acrn_vm_memseg_unmap()
137 return -EINVAL; in acrn_vm_memseg_unmap()
140 ret = acrn_mm_region_del(vm, memmap->user_vm_pa, memmap->len); in acrn_vm_memseg_unmap()
143 "Del memory region failed, VM[%u]!\n", vm->vmid); in acrn_vm_memseg_unmap()
149 * acrn_vm_ram_map() - Create a RAM EPT mapping of User VM.
150 * @vm: The User VM pointer
155 int acrn_vm_ram_map(struct acrn_vm *vm, struct acrn_vm_memmap *memmap) in acrn_vm_ram_map() argument
168 if (!vm || !memmap) in acrn_vm_ram_map()
169 return -EINVAL; in acrn_vm_ram_map()
171 mmap_read_lock(current->mm); in acrn_vm_ram_map()
172 vma = vma_lookup(current->mm, memmap->vma_base); in acrn_vm_ram_map()
173 if (vma && ((vma->vm_flags & VM_PFNMAP) != 0)) { in acrn_vm_ram_map()
174 if ((memmap->vma_base + memmap->len) > vma->vm_end) { in acrn_vm_ram_map()
175 mmap_read_unlock(current->mm); in acrn_vm_ram_map()
176 return -EINVAL; in acrn_vm_ram_map()
179 ret = follow_pfn(vma, memmap->vma_base, &pfn); in acrn_vm_ram_map()
180 mmap_read_unlock(current->mm); in acrn_vm_ram_map()
183 "Failed to lookup PFN at VMA:%pK.\n", (void *)memmap->vma_base); in acrn_vm_ram_map()
187 return acrn_mm_region_add(vm, memmap->user_vm_pa, in acrn_vm_ram_map()
188 PFN_PHYS(pfn), memmap->len, in acrn_vm_ram_map()
189 ACRN_MEM_TYPE_WB, memmap->attr); in acrn_vm_ram_map()
191 mmap_read_unlock(current->mm); in acrn_vm_ram_map()
193 /* Get the page number of the map region */ in acrn_vm_ram_map()
194 nr_pages = memmap->len >> PAGE_SHIFT; in acrn_vm_ram_map()
197 return -ENOMEM; in acrn_vm_ram_map()
199 /* Lock the pages of user memory map region */ in acrn_vm_ram_map()
200 pinned = pin_user_pages_fast(memmap->vma_base, in acrn_vm_ram_map()
207 ret = -EFAULT; in acrn_vm_ram_map()
211 /* Create a kernel map for the map region */ in acrn_vm_ram_map()
214 ret = -ENOMEM; in acrn_vm_ram_map()
218 /* Record Service VM va <-> User VM pa mapping */ in acrn_vm_ram_map()
219 mutex_lock(&vm->regions_mapping_lock); in acrn_vm_ram_map()
220 region_mapping = &vm->regions_mapping[vm->regions_mapping_count]; in acrn_vm_ram_map()
221 if (vm->regions_mapping_count < ACRN_MEM_MAPPING_MAX) { in acrn_vm_ram_map()
222 region_mapping->pages = pages; in acrn_vm_ram_map()
223 region_mapping->npages = nr_pages; in acrn_vm_ram_map()
224 region_mapping->size = memmap->len; in acrn_vm_ram_map()
225 region_mapping->service_vm_va = remap_vaddr; in acrn_vm_ram_map()
226 region_mapping->user_vm_pa = memmap->user_vm_pa; in acrn_vm_ram_map()
227 vm->regions_mapping_count++; in acrn_vm_ram_map()
231 ret = -ENOMEM; in acrn_vm_ram_map()
232 mutex_unlock(&vm->regions_mapping_lock); in acrn_vm_ram_map()
235 mutex_unlock(&vm->regions_mapping_lock); in acrn_vm_ram_map()
250 ret = -ENOMEM; in acrn_vm_ram_map()
255 vm_region = regions_info->regions_op; in acrn_vm_ram_map()
256 regions_info->vmid = vm->vmid; in acrn_vm_ram_map()
257 regions_info->regions_num = nr_regions; in acrn_vm_ram_map()
258 regions_info->regions_gpa = virt_to_phys(vm_region); in acrn_vm_ram_map()
259 user_vm_pa = memmap->user_vm_pa; in acrn_vm_ram_map()
268 vm_region->type = ACRN_MEM_REGION_ADD; in acrn_vm_ram_map()
269 vm_region->user_vm_pa = user_vm_pa; in acrn_vm_ram_map()
270 vm_region->service_vm_pa = page_to_phys(page); in acrn_vm_ram_map()
271 vm_region->size = region_size; in acrn_vm_ram_map()
272 vm_region->attr = (ACRN_MEM_TYPE_WB & ACRN_MEM_TYPE_MASK) | in acrn_vm_ram_map()
273 (memmap->attr & ACRN_MEM_ACCESS_RIGHT_MASK); in acrn_vm_ram_map()
284 "Failed to set regions, VM[%u]!\n", vm->vmid); in acrn_vm_ram_map()
290 "%s: VM[%u] service-GVA[%pK] user-GPA[%pK] size[0x%llx]\n", in acrn_vm_ram_map()
291 __func__, vm->vmid, in acrn_vm_ram_map()
292 remap_vaddr, (void *)memmap->user_vm_pa, memmap->len); in acrn_vm_ram_map()
298 mutex_lock(&vm->regions_mapping_lock); in acrn_vm_ram_map()
299 vm->regions_mapping_count--; in acrn_vm_ram_map()
300 mutex_unlock(&vm->regions_mapping_lock); in acrn_vm_ram_map()
312 * acrn_vm_all_ram_unmap() - Destroy a RAM EPT mapping of User VM.
313 * @vm: The User VM
315 void acrn_vm_all_ram_unmap(struct acrn_vm *vm) in acrn_vm_all_ram_unmap() argument
320 mutex_lock(&vm->regions_mapping_lock); in acrn_vm_all_ram_unmap()
321 for (i = 0; i < vm->regions_mapping_count; i++) { in acrn_vm_all_ram_unmap()
322 region_mapping = &vm->regions_mapping[i]; in acrn_vm_all_ram_unmap()
323 vunmap(region_mapping->service_vm_va); in acrn_vm_all_ram_unmap()
324 for (j = 0; j < region_mapping->npages; j++) in acrn_vm_all_ram_unmap()
325 unpin_user_page(region_mapping->pages[j]); in acrn_vm_all_ram_unmap()
326 vfree(region_mapping->pages); in acrn_vm_all_ram_unmap()
328 mutex_unlock(&vm->regions_mapping_lock); in acrn_vm_all_ram_unmap()