uvm_device.c revision 1.74 1 /* $NetBSD: uvm_device.c,v 1.74 2022/07/06 01:12:46 riastradh Exp $ */
2
3 /*
4 * Copyright (c) 1997 Charles D. Cranor and Washington University.
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 THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *
27 * from: Id: uvm_device.c,v 1.1.2.9 1998/02/06 05:11:47 chs Exp
28 */
29
30 /*
31 * uvm_device.c: the device pager.
32 */
33
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: uvm_device.c,v 1.74 2022/07/06 01:12:46 riastradh Exp $");
36
37 #include "opt_uvmhist.h"
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/conf.h>
42 #include <sys/proc.h>
43 #include <sys/kmem.h>
44
45 #include <uvm/uvm.h>
46 #include <uvm/uvm_device.h>
47 #include <uvm/uvm_pmap.h>
48
49 /*
50 * private global data structure
51 *
52 * we keep a list of active device objects in the system.
53 */
54
55 LIST_HEAD(udv_list_struct, uvm_device);
56 static struct udv_list_struct udv_list;
57 static kmutex_t udv_lock __cacheline_aligned;
58
59 /*
60 * functions
61 */
62
63 static void udv_init(void);
64 static void udv_reference(struct uvm_object *);
65 static void udv_detach(struct uvm_object *);
66 static int udv_fault(struct uvm_faultinfo *, vaddr_t,
67 struct vm_page **, int, int, vm_prot_t,
68 int);
69
70 /*
71 * master pager structure
72 */
73
74 const struct uvm_pagerops uvm_deviceops = {
75 .pgo_init = udv_init,
76 .pgo_reference = udv_reference,
77 .pgo_detach = udv_detach,
78 .pgo_fault = udv_fault,
79 };
80
81 /*
82 * the ops!
83 */
84
85 /*
86 * udv_init
87 *
88 * init pager private data structures.
89 */
90
91 static void
92 udv_init(void)
93 {
94 LIST_INIT(&udv_list);
95 mutex_init(&udv_lock, MUTEX_DEFAULT, IPL_NONE);
96 }
97
98 /*
99 * udv_attach
100 *
101 * get a VM object that is associated with a device. allocate a new
102 * one if needed.
103 *
104 * => caller must _not_ already be holding the lock on the uvm_object.
105 * => in fact, nothing should be locked so that we can sleep here.
106 */
107
108 struct uvm_object *
109 udv_attach(dev_t device, vm_prot_t accessprot,
110 voff_t off, /* used only for access check */
111 vsize_t size /* used only for access check */)
112 {
113 struct uvm_device *udv, *lcv;
114 const struct cdevsw *cdev;
115 dev_mmap_t *mapfn;
116
117 UVMHIST_FUNC(__func__);
118 UVMHIST_CALLARGS(maphist, "(device=%#jx)", device,0,0,0);
119
120 KASSERT(size > 0);
121
122 /*
123 * before we do anything, ensure this device supports mmap
124 */
125
126 cdev = cdevsw_lookup(device);
127 if (cdev == NULL) {
128 return (NULL);
129 }
130 mapfn = cdev->d_mmap;
131 if (mapfn == NULL || mapfn == nommap) {
132 return(NULL);
133 }
134
135 /*
136 * Negative offsets on the object are not allowed.
137 */
138
139 if ((cdev->d_flag & D_NEGOFFSAFE) == 0 &&
140 off != UVM_UNKNOWN_OFFSET && off < 0)
141 return(NULL);
142
143 /*
144 * Check that the specified range of the device allows the
145 * desired protection.
146 *
147 * XXX assumes VM_PROT_* == PROT_*
148 * XXX clobbers off and size, but nothing else here needs them.
149 */
150
151 while (size != 0) {
152 if (cdev_mmap(device, off, accessprot) == -1) {
153 return (NULL);
154 }
155 off += PAGE_SIZE; size -= PAGE_SIZE;
156 }
157
158 /*
159 * keep looping until we get it
160 */
161
162 for (;;) {
163
164 /*
165 * first, attempt to find it on the main list
166 */
167
168 mutex_enter(&udv_lock);
169 LIST_FOREACH(lcv, &udv_list, u_list) {
170 if (device == lcv->u_device)
171 break;
172 }
173
174 /*
175 * got it on main list. put a hold on it and unlock udv_lock.
176 */
177
178 if (lcv) {
179
180 /*
181 * if someone else has a hold on it, sleep and start
182 * over again.
183 */
184
185 if (lcv->u_flags & UVM_DEVICE_HOLD) {
186 lcv->u_flags |= UVM_DEVICE_WANTED;
187 UVM_UNLOCK_AND_WAIT(lcv, &udv_lock, false,
188 "udv_attach",0);
189 continue;
190 }
191
192 /* we are now holding it */
193 lcv->u_flags |= UVM_DEVICE_HOLD;
194 mutex_exit(&udv_lock);
195
196 /*
197 * bump reference count, unhold, return.
198 */
199
200 rw_enter(lcv->u_obj.vmobjlock, RW_WRITER);
201 lcv->u_obj.uo_refs++;
202 rw_exit(lcv->u_obj.vmobjlock);
203
204 mutex_enter(&udv_lock);
205 if (lcv->u_flags & UVM_DEVICE_WANTED)
206 wakeup(lcv);
207 lcv->u_flags &= ~(UVM_DEVICE_WANTED|UVM_DEVICE_HOLD);
208 mutex_exit(&udv_lock);
209 return(&lcv->u_obj);
210 }
211
212 /*
213 * Did not find it on main list. Need to allocate a new one.
214 */
215
216 mutex_exit(&udv_lock);
217
218 /* Note: both calls may allocate memory and sleep. */
219 udv = kmem_alloc(sizeof(*udv), KM_SLEEP);
220 uvm_obj_init(&udv->u_obj, &uvm_deviceops, true, 1);
221
222 mutex_enter(&udv_lock);
223
224 /*
225 * now we have to double check to make sure no one added it
226 * to the list while we were sleeping...
227 */
228
229 LIST_FOREACH(lcv, &udv_list, u_list) {
230 if (device == lcv->u_device)
231 break;
232 }
233
234 /*
235 * did we lose a race to someone else?
236 * free our memory and retry.
237 */
238
239 if (lcv) {
240 mutex_exit(&udv_lock);
241 uvm_obj_destroy(&udv->u_obj, true);
242 kmem_free(udv, sizeof(*udv));
243 continue;
244 }
245
246 /*
247 * we have it! init the data structures, add to list
248 * and return.
249 */
250
251 udv->u_flags = 0;
252 udv->u_device = device;
253 LIST_INSERT_HEAD(&udv_list, udv, u_list);
254 mutex_exit(&udv_lock);
255 return(&udv->u_obj);
256 }
257 /*NOTREACHED*/
258 }
259
260 /*
261 * udv_reference
262 *
263 * add a reference to a VM object. Note that the reference count must
264 * already be one (the passed in reference) so there is no chance of the
265 * udv being released or locked out here.
266 *
267 * => caller must call with object unlocked.
268 */
269
270 static void
271 udv_reference(struct uvm_object *uobj)
272 {
273 UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
274
275 rw_enter(uobj->vmobjlock, RW_WRITER);
276 uobj->uo_refs++;
277 UVMHIST_LOG(maphist, "<- done (uobj=%#jx, ref = %jd)",
278 (uintptr_t)uobj, uobj->uo_refs,0,0);
279 rw_exit(uobj->vmobjlock);
280 }
281
282 /*
283 * udv_detach
284 *
285 * remove a reference to a VM object.
286 *
287 * => caller must call with object unlocked and map locked.
288 */
289
290 static void
291 udv_detach(struct uvm_object *uobj)
292 {
293 struct uvm_device *udv = (struct uvm_device *)uobj;
294 UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
295
296 /*
297 * loop until done
298 */
299 again:
300 rw_enter(uobj->vmobjlock, RW_WRITER);
301 if (uobj->uo_refs > 1) {
302 uobj->uo_refs--;
303 rw_exit(uobj->vmobjlock);
304 UVMHIST_LOG(maphist," <- done, uobj=%#jx, ref=%jd",
305 (uintptr_t)uobj,uobj->uo_refs,0,0);
306 return;
307 }
308
309 /*
310 * is it being held? if so, wait until others are done.
311 */
312
313 mutex_enter(&udv_lock);
314 if (udv->u_flags & UVM_DEVICE_HOLD) {
315 udv->u_flags |= UVM_DEVICE_WANTED;
316 rw_exit(uobj->vmobjlock);
317 UVM_UNLOCK_AND_WAIT(udv, &udv_lock, false, "udv_detach",0);
318 goto again;
319 }
320
321 /*
322 * got it! nuke it now.
323 */
324
325 LIST_REMOVE(udv, u_list);
326 if (udv->u_flags & UVM_DEVICE_WANTED)
327 wakeup(udv);
328 mutex_exit(&udv_lock);
329 rw_exit(uobj->vmobjlock);
330
331 uvm_obj_destroy(uobj, true);
332 kmem_free(udv, sizeof(*udv));
333 UVMHIST_LOG(maphist," <- done, freed uobj=%#jx", (uintptr_t)uobj,
334 0, 0, 0);
335 }
336
337 /*
338 * udv_fault: non-standard fault routine for device "pages"
339 *
340 * => rather than having a "get" function, we have a fault routine
341 * since we don't return vm_pages we need full control over the
342 * pmap_enter map in
343 * => all the usual fault data structured are locked by the caller
344 * (i.e. maps(read), amap (if any), uobj)
345 * => on return, we unlock all fault data structures
346 * => flags: PGO_ALLPAGES: get all of the pages
347 * PGO_LOCKED: fault data structures are locked
348 * XXX: currently PGO_LOCKED is always required ... consider removing
349 * it as a flag
350 * => NOTE: vaddr is the VA of pps[0] in ufi->entry, _NOT_ pps[centeridx]
351 */
352
353 static int
354 udv_fault(struct uvm_faultinfo *ufi, vaddr_t vaddr, struct vm_page **pps,
355 int npages, int centeridx, vm_prot_t access_type,
356 int flags)
357 {
358 struct vm_map_entry *entry = ufi->entry;
359 struct uvm_object *uobj = entry->object.uvm_obj;
360 struct uvm_device *udv = (struct uvm_device *)uobj;
361 vaddr_t curr_va;
362 off_t curr_offset;
363 paddr_t paddr, mdpgno;
364 u_int mmapflags;
365 int lcv, retval;
366 dev_t device;
367 vm_prot_t mapprot;
368 UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
369 UVMHIST_LOG(maphist," flags=%#jx", flags,0,0,0);
370
371 /*
372 * we do not allow device mappings to be mapped copy-on-write
373 * so we kill any attempt to do so here.
374 */
375
376 if (UVM_ET_ISCOPYONWRITE(entry)) {
377 UVMHIST_LOG(maphist, "<- failed -- COW entry (etype=%#jx)",
378 entry->etype, 0,0,0);
379 uvmfault_unlockall(ufi, ufi->entry->aref.ar_amap, uobj);
380 return(EIO);
381 }
382
383 /*
384 * get device map function.
385 */
386
387 device = udv->u_device;
388 if (cdevsw_lookup(device) == NULL) {
389 /* XXX This should not happen */
390 uvmfault_unlockall(ufi, ufi->entry->aref.ar_amap, uobj);
391 return (EIO);
392 }
393
394 /*
395 * now we must determine the offset in udv to use and the VA to
396 * use for pmap_enter. note that we always use orig_map's pmap
397 * for pmap_enter (even if we have a submap). since virtual
398 * addresses in a submap must match the main map, this is ok.
399 */
400
401 /* udv offset = (offset from start of entry) + entry's offset */
402 curr_offset = entry->offset + (vaddr - entry->start);
403 /* pmap va = vaddr (virtual address of pps[0]) */
404 curr_va = vaddr;
405
406 /*
407 * loop over the page range entering in as needed
408 */
409
410 retval = 0;
411 for (lcv = 0 ; lcv < npages ; lcv++, curr_offset += PAGE_SIZE,
412 curr_va += PAGE_SIZE) {
413 if ((flags & PGO_ALLPAGES) == 0 && lcv != centeridx)
414 continue;
415
416 if (pps[lcv] == PGO_DONTCARE)
417 continue;
418
419 mdpgno = cdev_mmap(device, curr_offset, access_type);
420 if (mdpgno == -1) {
421 retval = EIO;
422 break;
423 }
424 paddr = pmap_phys_address(mdpgno);
425 mmapflags = pmap_mmap_flags(mdpgno);
426 mapprot = ufi->entry->protection;
427 UVMHIST_LOG(maphist,
428 " MAPPING: device: pm=%#jx, va=%#jx, pa=%#jx, at=%jd",
429 (uintptr_t)ufi->orig_map->pmap, curr_va, paddr, mapprot);
430 if (pmap_enter(ufi->orig_map->pmap, curr_va, paddr, mapprot,
431 PMAP_CANFAIL | mapprot | mmapflags) != 0) {
432 /*
433 * pmap_enter() didn't have the resource to
434 * enter this mapping. Unlock everything,
435 * wait for the pagedaemon to free up some
436 * pages, and then tell uvm_fault() to start
437 * the fault again.
438 *
439 * XXX Needs some rethinking for the PGO_ALLPAGES
440 * XXX case.
441 */
442 pmap_update(ufi->orig_map->pmap); /* sync what we have so far */
443 uvmfault_unlockall(ufi, ufi->entry->aref.ar_amap,
444 uobj);
445 return ENOMEM;
446 }
447 }
448
449 pmap_update(ufi->orig_map->pmap);
450 uvmfault_unlockall(ufi, ufi->entry->aref.ar_amap, uobj);
451 return (retval);
452 }
453