xref: /openbmc/linux/drivers/virt/acrn/mm.c (revision e873f36e)
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 
16 #include "acrn_drv.h"
17 
modify_region(struct acrn_vm * vm,struct vm_memory_region_op * region)18 static int modify_region(struct acrn_vm *vm, struct vm_memory_region_op *region)
19 {
20 	struct vm_memory_region_batch *regions;
21 	int ret;
22 
23 	regions = kzalloc(sizeof(*regions), GFP_KERNEL);
24 	if (!regions)
25 		return -ENOMEM;
26 
27 	regions->vmid = vm->vmid;
28 	regions->regions_num = 1;
29 	regions->regions_gpa = virt_to_phys(region);
30 
31 	ret = hcall_set_memory_regions(virt_to_phys(regions));
32 	if (ret < 0)
33 		dev_dbg(acrn_dev.this_device,
34 			"Failed to set memory region for VM[%u]!\n", vm->vmid);
35 
36 	kfree(regions);
37 	return ret;
38 }
39 
40 /**
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.
45  * @size:		Size of the region.
46  * @mem_type:		Combination of ACRN_MEM_TYPE_*.
47  * @mem_access_right:	Combination of ACRN_MEM_ACCESS_*.
48  *
49  * Return: 0 on success, <0 on error.
50  */
acrn_mm_region_add(struct acrn_vm * vm,u64 user_gpa,u64 service_gpa,u64 size,u32 mem_type,u32 mem_access_right)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)
53 {
54 	struct vm_memory_region_op *region;
55 	int ret = 0;
56 
57 	region = kzalloc(sizeof(*region), GFP_KERNEL);
58 	if (!region)
59 		return -ENOMEM;
60 
61 	region->type = ACRN_MEM_REGION_ADD;
62 	region->user_vm_pa = user_gpa;
63 	region->service_vm_pa = service_gpa;
64 	region->size = size;
65 	region->attr = ((mem_type & ACRN_MEM_TYPE_MASK) |
66 			(mem_access_right & ACRN_MEM_ACCESS_RIGHT_MASK));
67 	ret = modify_region(vm, region);
68 
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);
72 	kfree(region);
73 	return ret;
74 }
75 
76 /**
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.
80  * @size:	Size of the region.
81  *
82  * Return: 0 on success, <0 for error.
83  */
acrn_mm_region_del(struct acrn_vm * vm,u64 user_gpa,u64 size)84 int acrn_mm_region_del(struct acrn_vm *vm, u64 user_gpa, u64 size)
85 {
86 	struct vm_memory_region_op *region;
87 	int ret = 0;
88 
89 	region = kzalloc(sizeof(*region), GFP_KERNEL);
90 	if (!region)
91 		return -ENOMEM;
92 
93 	region->type = ACRN_MEM_REGION_DEL;
94 	region->user_vm_pa = user_gpa;
95 	region->service_vm_pa = 0UL;
96 	region->size = size;
97 	region->attr = 0U;
98 
99 	ret = modify_region(vm, region);
100 
101 	dev_dbg(acrn_dev.this_device, "%s: user-GPA[%pK] size[0x%llx].\n",
102 		__func__, (void *)user_gpa, size);
103 	kfree(region);
104 	return ret;
105 }
106 
acrn_vm_memseg_map(struct acrn_vm * vm,struct acrn_vm_memmap * memmap)107 int acrn_vm_memseg_map(struct acrn_vm *vm, struct acrn_vm_memmap *memmap)
108 {
109 	int ret;
110 
111 	if (memmap->type == ACRN_MEMMAP_RAM)
112 		return acrn_vm_ram_map(vm, memmap);
113 
114 	if (memmap->type != ACRN_MEMMAP_MMIO) {
115 		dev_dbg(acrn_dev.this_device,
116 			"Invalid memmap type: %u\n", memmap->type);
117 		return -EINVAL;
118 	}
119 
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);
123 	if (ret < 0)
124 		dev_dbg(acrn_dev.this_device,
125 			"Add memory region failed, VM[%u]!\n", vm->vmid);
126 
127 	return ret;
128 }
129 
acrn_vm_memseg_unmap(struct acrn_vm * vm,struct acrn_vm_memmap * memmap)130 int acrn_vm_memseg_unmap(struct acrn_vm *vm, struct acrn_vm_memmap *memmap)
131 {
132 	int ret;
133 
134 	if (memmap->type != ACRN_MEMMAP_MMIO) {
135 		dev_dbg(acrn_dev.this_device,
136 			"Invalid memmap type: %u\n", memmap->type);
137 		return -EINVAL;
138 	}
139 
140 	ret = acrn_mm_region_del(vm, memmap->user_vm_pa, memmap->len);
141 	if (ret < 0)
142 		dev_dbg(acrn_dev.this_device,
143 			"Del memory region failed, VM[%u]!\n", vm->vmid);
144 
145 	return ret;
146 }
147 
148 /**
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
152  *
153  * Return: 0 on success, <0 for error.
154  */
acrn_vm_ram_map(struct acrn_vm * vm,struct acrn_vm_memmap * memmap)155 int acrn_vm_ram_map(struct acrn_vm *vm, struct acrn_vm_memmap *memmap)
156 {
157 	struct vm_memory_region_batch *regions_info;
158 	int nr_pages, i, 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;
162 	void *remap_vaddr;
163 	int ret, pinned;
164 	u64 user_vm_pa;
165 	struct vm_area_struct *vma;
166 
167 	if (!vm || !memmap)
168 		return -EINVAL;
169 
170 	/* Get the page number of the map region */
171 	nr_pages = memmap->len >> PAGE_SHIFT;
172 	if (!nr_pages)
173 		return -EINVAL;
174 
175 	mmap_read_lock(current->mm);
176 	vma = vma_lookup(current->mm, memmap->vma_base);
177 	if (vma && ((vma->vm_flags & VM_PFNMAP) != 0)) {
178 		unsigned long start_pfn, cur_pfn;
179 		spinlock_t *ptl;
180 		bool writable;
181 		pte_t *ptep;
182 
183 		if ((memmap->vma_base + memmap->len) > vma->vm_end) {
184 			mmap_read_unlock(current->mm);
185 			return -EINVAL;
186 		}
187 
188 		for (i = 0; i < nr_pages; i++) {
189 			ret = follow_pte(vma->vm_mm,
190 					 memmap->vma_base + i * PAGE_SIZE,
191 					 &ptep, &ptl);
192 			if (ret)
193 				break;
194 
195 			cur_pfn = pte_pfn(ptep_get(ptep));
196 			if (i == 0)
197 				start_pfn = cur_pfn;
198 			writable = !!pte_write(ptep_get(ptep));
199 			pte_unmap_unlock(ptep, ptl);
200 
201 			/* Disallow write access if the PTE is not writable. */
202 			if (!writable &&
203 			    (memmap->attr & ACRN_MEM_ACCESS_WRITE)) {
204 				ret = -EFAULT;
205 				break;
206 			}
207 
208 			/* Disallow refcounted pages. */
209 			if (pfn_valid(cur_pfn) &&
210 			    !PageReserved(pfn_to_page(cur_pfn))) {
211 				ret = -EFAULT;
212 				break;
213 			}
214 
215 			/* Disallow non-contiguous ranges. */
216 			if (cur_pfn != start_pfn + i) {
217 				ret = -EINVAL;
218 				break;
219 			}
220 		}
221 		mmap_read_unlock(current->mm);
222 
223 		if (ret) {
224 			dev_dbg(acrn_dev.this_device,
225 				"Failed to lookup PFN at VMA:%pK.\n", (void *)memmap->vma_base);
226 			return ret;
227 		}
228 
229 		return acrn_mm_region_add(vm, memmap->user_vm_pa,
230 			 PFN_PHYS(start_pfn), memmap->len,
231 			 ACRN_MEM_TYPE_WB, memmap->attr);
232 	}
233 	mmap_read_unlock(current->mm);
234 
235 	pages = vzalloc(array_size(nr_pages, sizeof(*pages)));
236 	if (!pages)
237 		return -ENOMEM;
238 
239 	/* Lock the pages of user memory map region */
240 	pinned = pin_user_pages_fast(memmap->vma_base,
241 				     nr_pages, FOLL_WRITE | FOLL_LONGTERM,
242 				     pages);
243 	if (pinned < 0) {
244 		ret = pinned;
245 		goto free_pages;
246 	} else if (pinned != nr_pages) {
247 		ret = -EFAULT;
248 		goto put_pages;
249 	}
250 
251 	/* Create a kernel map for the map region */
252 	remap_vaddr = vmap(pages, nr_pages, VM_MAP, PAGE_KERNEL);
253 	if (!remap_vaddr) {
254 		ret = -ENOMEM;
255 		goto put_pages;
256 	}
257 
258 	/* Record Service VM va <-> User VM pa mapping */
259 	mutex_lock(&vm->regions_mapping_lock);
260 	region_mapping = &vm->regions_mapping[vm->regions_mapping_count];
261 	if (vm->regions_mapping_count < ACRN_MEM_MAPPING_MAX) {
262 		region_mapping->pages = pages;
263 		region_mapping->npages = nr_pages;
264 		region_mapping->size = memmap->len;
265 		region_mapping->service_vm_va = remap_vaddr;
266 		region_mapping->user_vm_pa = memmap->user_vm_pa;
267 		vm->regions_mapping_count++;
268 	} else {
269 		dev_warn(acrn_dev.this_device,
270 			"Run out of memory mapping slots!\n");
271 		ret = -ENOMEM;
272 		mutex_unlock(&vm->regions_mapping_lock);
273 		goto unmap_no_count;
274 	}
275 	mutex_unlock(&vm->regions_mapping_lock);
276 
277 	/* Calculate count of vm_memory_region_op */
278 	for (i = 0; i < nr_pages; i += 1 << order) {
279 		page = pages[i];
280 		VM_BUG_ON_PAGE(PageTail(page), page);
281 		order = compound_order(page);
282 		nr_regions++;
283 	}
284 
285 	/* Prepare the vm_memory_region_batch */
286 	regions_info = kzalloc(struct_size(regions_info, regions_op,
287 					   nr_regions), GFP_KERNEL);
288 	if (!regions_info) {
289 		ret = -ENOMEM;
290 		goto unmap_kernel_map;
291 	}
292 
293 	/* Fill each vm_memory_region_op */
294 	vm_region = regions_info->regions_op;
295 	regions_info->vmid = vm->vmid;
296 	regions_info->regions_num = nr_regions;
297 	regions_info->regions_gpa = virt_to_phys(vm_region);
298 	user_vm_pa = memmap->user_vm_pa;
299 	for (i = 0; i < nr_pages; i += 1 << order) {
300 		u32 region_size;
301 
302 		page = pages[i];
303 		VM_BUG_ON_PAGE(PageTail(page), page);
304 		order = compound_order(page);
305 		region_size = PAGE_SIZE << order;
306 		vm_region->type = ACRN_MEM_REGION_ADD;
307 		vm_region->user_vm_pa = user_vm_pa;
308 		vm_region->service_vm_pa = page_to_phys(page);
309 		vm_region->size = region_size;
310 		vm_region->attr = (ACRN_MEM_TYPE_WB & ACRN_MEM_TYPE_MASK) |
311 				  (memmap->attr & ACRN_MEM_ACCESS_RIGHT_MASK);
312 
313 		vm_region++;
314 		user_vm_pa += region_size;
315 	}
316 
317 	/* Inform the ACRN Hypervisor to set up EPT mappings */
318 	ret = hcall_set_memory_regions(virt_to_phys(regions_info));
319 	if (ret < 0) {
320 		dev_dbg(acrn_dev.this_device,
321 			"Failed to set regions, VM[%u]!\n", vm->vmid);
322 		goto unset_region;
323 	}
324 	kfree(regions_info);
325 
326 	dev_dbg(acrn_dev.this_device,
327 		"%s: VM[%u] service-GVA[%pK] user-GPA[%pK] size[0x%llx]\n",
328 		__func__, vm->vmid,
329 		remap_vaddr, (void *)memmap->user_vm_pa, memmap->len);
330 	return ret;
331 
332 unset_region:
333 	kfree(regions_info);
334 unmap_kernel_map:
335 	mutex_lock(&vm->regions_mapping_lock);
336 	vm->regions_mapping_count--;
337 	mutex_unlock(&vm->regions_mapping_lock);
338 unmap_no_count:
339 	vunmap(remap_vaddr);
340 put_pages:
341 	for (i = 0; i < pinned; i++)
342 		unpin_user_page(pages[i]);
343 free_pages:
344 	vfree(pages);
345 	return ret;
346 }
347 
348 /**
349  * acrn_vm_all_ram_unmap() - Destroy a RAM EPT mapping of User VM.
350  * @vm:	The User VM
351  */
acrn_vm_all_ram_unmap(struct acrn_vm * vm)352 void acrn_vm_all_ram_unmap(struct acrn_vm *vm)
353 {
354 	struct vm_memory_mapping *region_mapping;
355 	int i, j;
356 
357 	mutex_lock(&vm->regions_mapping_lock);
358 	for (i = 0; i < vm->regions_mapping_count; i++) {
359 		region_mapping = &vm->regions_mapping[i];
360 		vunmap(region_mapping->service_vm_va);
361 		for (j = 0; j < region_mapping->npages; j++)
362 			unpin_user_page(region_mapping->pages[j]);
363 		vfree(region_mapping->pages);
364 	}
365 	mutex_unlock(&vm->regions_mapping_lock);
366 }
367