xref: /freebsd/sys/amd64/vmm/vmm_mem_machdep.c (revision df21a004be237a1dccd03c7b47254625eea62fa9)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2011 NetApp, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/malloc.h>
32 #include <sys/sglist.h>
33 #include <sys/lock.h>
34 #include <sys/rwlock.h>
35 
36 #include <vm/vm.h>
37 #include <vm/vm_param.h>
38 #include <vm/pmap.h>
39 #include <vm/vm_extern.h>
40 #include <vm/vm_map.h>
41 #include <vm/vm_object.h>
42 #include <vm/vm_page.h>
43 #include <vm/vm_pager.h>
44 
45 #include <machine/md_var.h>
46 
47 #include "vmm_mem.h"
48 
49 int
50 vmm_mmio_alloc(struct vmspace *vmspace, vm_paddr_t gpa, size_t len,
51     vm_paddr_t hpa)
52 {
53 	struct sglist *sg;
54 	vm_object_t obj;
55 	int error;
56 
57 	if (gpa + len < gpa || hpa + len < hpa || (gpa & PAGE_MASK) != 0 ||
58 	    (hpa & PAGE_MASK) != 0 || (len & PAGE_MASK) != 0)
59 		return (EINVAL);
60 
61 	sg = sglist_alloc(1, M_WAITOK);
62 	error = sglist_append_phys(sg, hpa, len);
63 	KASSERT(error == 0, ("error %d appending physaddr to sglist", error));
64 
65 	obj = vm_pager_allocate(OBJT_SG, sg, len, VM_PROT_RW, 0, NULL);
66 	if (obj == NULL)
67 		return (ENOMEM);
68 
69 	/*
70 	 * VT-x ignores the MTRR settings when figuring out the memory type for
71 	 * translations obtained through EPT.
72 	 *
73 	 * Therefore we explicitly force the pages provided by this object to be
74 	 * mapped as uncacheable.
75 	 */
76 	VM_OBJECT_WLOCK(obj);
77 	error = vm_object_set_memattr(obj, VM_MEMATTR_UNCACHEABLE);
78 	VM_OBJECT_WUNLOCK(obj);
79 	if (error != KERN_SUCCESS)
80 		panic("vmm_mmio_alloc: vm_object_set_memattr error %d", error);
81 
82 	vm_map_lock(&vmspace->vm_map);
83 	error = vm_map_insert(&vmspace->vm_map, obj, 0, gpa, gpa + len,
84 	    VM_PROT_RW, VM_PROT_RW, 0);
85 	vm_map_unlock(&vmspace->vm_map);
86 	if (error != KERN_SUCCESS) {
87 		error = vm_mmap_to_errno(error);
88 		vm_object_deallocate(obj);
89 	} else {
90 		error = 0;
91 	}
92 
93 	/*
94 	 * Drop the reference on the sglist.
95 	 *
96 	 * If the scatter/gather object was successfully allocated then it
97 	 * has incremented the reference count on the sglist. Dropping the
98 	 * initial reference count ensures that the sglist will be freed
99 	 * when the object is deallocated.
100 	 *
101 	 * If the object could not be allocated then we end up freeing the
102 	 * sglist.
103 	 */
104 	sglist_free(sg);
105 
106 	return (error);
107 }
108 
109 void
110 vmm_mmio_free(struct vmspace *vmspace, vm_paddr_t gpa, size_t len)
111 {
112 
113 	vm_map_remove(&vmspace->vm_map, gpa, gpa + len);
114 }
115 
116 vm_paddr_t
117 vmm_mem_maxaddr(void)
118 {
119 
120 	return (ptoa(Maxmem));
121 }
122