2 * Copyright (c) 2011 NetApp, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/malloc.h>
35 #include <sys/sglist.h>
37 #include <sys/rwlock.h>
40 #include <vm/vm_param.h>
42 #include <vm/vm_map.h>
43 #include <vm/vm_object.h>
44 #include <vm/vm_page.h>
45 #include <vm/vm_pager.h>
47 #include <machine/md_var.h>
59 vmm_mmio_alloc(struct vmspace *vmspace, vm_paddr_t gpa, size_t len,
66 sg = sglist_alloc(1, M_WAITOK);
67 error = sglist_append_phys(sg, hpa, len);
68 KASSERT(error == 0, ("error %d appending physaddr to sglist", error));
70 obj = vm_pager_allocate(OBJT_SG, sg, len, VM_PROT_RW, 0, NULL);
73 * VT-x ignores the MTRR settings when figuring out the
74 * memory type for translations obtained through EPT.
76 * Therefore we explicitly force the pages provided by
77 * this object to be mapped as uncacheable.
80 error = vm_object_set_memattr(obj, VM_MEMATTR_UNCACHEABLE);
81 VM_OBJECT_WUNLOCK(obj);
82 if (error != KERN_SUCCESS) {
83 panic("vmm_mmio_alloc: vm_object_set_memattr error %d",
86 error = vm_map_find(&vmspace->vm_map, obj, 0, &gpa, len, 0,
87 VMFS_NO_SPACE, VM_PROT_RW, VM_PROT_RW, 0);
88 if (error != KERN_SUCCESS) {
89 vm_object_deallocate(obj);
95 * Drop the reference on the sglist.
97 * If the scatter/gather object was successfully allocated then it
98 * has incremented the reference count on the sglist. Dropping the
99 * initial reference count ensures that the sglist will be freed
100 * when the object is deallocated.
102 * If the object could not be allocated then we end up freeing the
111 vmm_mmio_free(struct vmspace *vmspace, vm_paddr_t gpa, size_t len)
114 vm_map_remove(&vmspace->vm_map, gpa, gpa + len);
118 vmm_mem_alloc(struct vmspace *vmspace, vm_paddr_t gpa, size_t len)
124 panic("vmm_mem_alloc: invalid gpa %#lx", gpa);
126 if (len == 0 || (len & PAGE_MASK) != 0)
127 panic("vmm_mem_alloc: invalid allocation size %lu", len);
129 obj = vm_object_allocate(OBJT_DEFAULT, len >> PAGE_SHIFT);
131 error = vm_map_find(&vmspace->vm_map, obj, 0, &gpa, len, 0,
132 VMFS_NO_SPACE, VM_PROT_ALL, VM_PROT_ALL, 0);
133 if (error != KERN_SUCCESS) {
134 vm_object_deallocate(obj);
143 vmm_mem_free(struct vmspace *vmspace, vm_paddr_t gpa, size_t len)
146 vm_map_remove(&vmspace->vm_map, gpa, gpa + len);
150 vmm_mem_maxaddr(void)
153 return (ptoa(Maxmem));