xref: /freebsd/sys/vm/sg_pager.c (revision b0b1dbdd)
1 /*-
2  * Copyright (c) 2009 Hudson River Trading LLC
3  * Written by: John H. Baldwin <jhb@FreeBSD.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 /*
32  * This pager manages OBJT_SG objects.  These objects are backed by
33  * a scatter/gather list of physical address ranges.
34  */
35 
36 #include <sys/param.h>
37 #include <sys/lock.h>
38 #include <sys/mutex.h>
39 #include <sys/rwlock.h>
40 #include <sys/sglist.h>
41 #include <vm/vm.h>
42 #include <vm/vm_param.h>
43 #include <vm/vm_object.h>
44 #include <vm/vm_page.h>
45 #include <vm/vm_pager.h>
46 #include <vm/vm_phys.h>
47 #include <vm/uma.h>
48 
49 static vm_object_t sg_pager_alloc(void *, vm_ooffset_t, vm_prot_t,
50     vm_ooffset_t, struct ucred *);
51 static void sg_pager_dealloc(vm_object_t);
52 static int sg_pager_getpages(vm_object_t, vm_page_t *, int, int *, int *);
53 static void sg_pager_putpages(vm_object_t, vm_page_t *, int,
54 		boolean_t, int *);
55 static boolean_t sg_pager_haspage(vm_object_t, vm_pindex_t, int *,
56 		int *);
57 
58 struct pagerops sgpagerops = {
59 	.pgo_alloc =	sg_pager_alloc,
60 	.pgo_dealloc =	sg_pager_dealloc,
61 	.pgo_getpages =	sg_pager_getpages,
62 	.pgo_putpages =	sg_pager_putpages,
63 	.pgo_haspage =	sg_pager_haspage,
64 };
65 
66 static vm_object_t
67 sg_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
68     vm_ooffset_t foff, struct ucred *cred)
69 {
70 	struct sglist *sg;
71 	vm_object_t object;
72 	vm_pindex_t npages, pindex;
73 	int i;
74 
75 	/*
76 	 * Offset should be page aligned.
77 	 */
78 	if (foff & PAGE_MASK)
79 		return (NULL);
80 
81 	/*
82 	 * The scatter/gather list must only include page-aligned
83 	 * ranges.
84 	 */
85 	npages = 0;
86 	sg = handle;
87 	for (i = 0; i < sg->sg_nseg; i++) {
88 		if ((sg->sg_segs[i].ss_paddr % PAGE_SIZE) != 0 ||
89 		    (sg->sg_segs[i].ss_len % PAGE_SIZE) != 0)
90 			return (NULL);
91 		npages += sg->sg_segs[i].ss_len / PAGE_SIZE;
92 	}
93 
94 	/*
95 	 * The scatter/gather list has a fixed size.  Refuse requests
96 	 * to map beyond that.
97 	 */
98 	size = round_page(size);
99 	pindex = UOFF_TO_IDX(foff) + UOFF_TO_IDX(size);
100 	if (pindex > npages || pindex < UOFF_TO_IDX(foff) ||
101 	    pindex < UOFF_TO_IDX(size))
102 		return (NULL);
103 
104 	/*
105 	 * Allocate a new object and associate it with the
106 	 * scatter/gather list.  It is ok for our purposes to have
107 	 * multiple VM objects associated with the same scatter/gather
108 	 * list because scatter/gather lists are static.  This is also
109 	 * simpler than ensuring a unique object per scatter/gather
110 	 * list.
111 	 */
112 	object = vm_object_allocate(OBJT_SG, npages);
113 	object->handle = sglist_hold(sg);
114 	TAILQ_INIT(&object->un_pager.sgp.sgp_pglist);
115 	return (object);
116 }
117 
118 static void
119 sg_pager_dealloc(vm_object_t object)
120 {
121 	struct sglist *sg;
122 	vm_page_t m;
123 
124 	/*
125 	 * Free up our fake pages.
126 	 */
127 	while ((m = TAILQ_FIRST(&object->un_pager.sgp.sgp_pglist)) != 0) {
128 		TAILQ_REMOVE(&object->un_pager.sgp.sgp_pglist, m, plinks.q);
129 		vm_page_putfake(m);
130 	}
131 
132 	sg = object->handle;
133 	sglist_free(sg);
134 	object->handle = NULL;
135 	object->type = OBJT_DEAD;
136 }
137 
138 static int
139 sg_pager_getpages(vm_object_t object, vm_page_t *m, int count, int *rbehind,
140     int *rahead)
141 {
142 	struct sglist *sg;
143 	vm_page_t m_paddr, page;
144 	vm_pindex_t offset;
145 	vm_paddr_t paddr;
146 	vm_memattr_t memattr;
147 	size_t space;
148 	int i;
149 
150 	/* Since our haspage reports zero after/before, the count is 1. */
151 	KASSERT(count == 1, ("%s: count %d", __func__, count));
152 	VM_OBJECT_ASSERT_WLOCKED(object);
153 	sg = object->handle;
154 	memattr = object->memattr;
155 	VM_OBJECT_WUNLOCK(object);
156 	offset = m[0]->pindex;
157 
158 	/*
159 	 * Lookup the physical address of the requested page.  An initial
160 	 * value of '1' instead of '0' is used so we can assert that the
161 	 * page is found since '0' can be a valid page-aligned physical
162 	 * address.
163 	 */
164 	space = 0;
165 	paddr = 1;
166 	for (i = 0; i < sg->sg_nseg; i++) {
167 		if (space + sg->sg_segs[i].ss_len <= (offset * PAGE_SIZE)) {
168 			space += sg->sg_segs[i].ss_len;
169 			continue;
170 		}
171 		paddr = sg->sg_segs[i].ss_paddr + offset * PAGE_SIZE - space;
172 		break;
173 	}
174 	KASSERT(paddr != 1, ("invalid SG page index"));
175 
176 	/* If "paddr" is a real page, perform a sanity check on "memattr". */
177 	if ((m_paddr = vm_phys_paddr_to_vm_page(paddr)) != NULL &&
178 	    pmap_page_get_memattr(m_paddr) != memattr) {
179 		memattr = pmap_page_get_memattr(m_paddr);
180 		printf(
181 	    "WARNING: A device driver has set \"memattr\" inconsistently.\n");
182 	}
183 
184 	/* Return a fake page for the requested page. */
185 	KASSERT(!(m[0]->flags & PG_FICTITIOUS),
186 	    ("backing page for SG is fake"));
187 
188 	/* Construct a new fake page. */
189 	page = vm_page_getfake(paddr, memattr);
190 	VM_OBJECT_WLOCK(object);
191 	TAILQ_INSERT_TAIL(&object->un_pager.sgp.sgp_pglist, page, plinks.q);
192 	vm_page_replace_checked(page, object, offset, m[0]);
193 	vm_page_lock(m[0]);
194 	vm_page_free(m[0]);
195 	vm_page_unlock(m[0]);
196 	m[0] = page;
197 	page->valid = VM_PAGE_BITS_ALL;
198 
199 	if (rbehind)
200 		*rbehind = 0;
201 	if (rahead)
202 		*rahead = 0;
203 
204 	return (VM_PAGER_OK);
205 }
206 
207 static void
208 sg_pager_putpages(vm_object_t object, vm_page_t *m, int count,
209     boolean_t sync, int *rtvals)
210 {
211 
212 	panic("sg_pager_putpage called");
213 }
214 
215 static boolean_t
216 sg_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before,
217     int *after)
218 {
219 
220 	if (before != NULL)
221 		*before = 0;
222 	if (after != NULL)
223 		*after = 0;
224 	return (TRUE);
225 }
226