|
|
1.1 root 1: /*
2: * Copyright (c) 1990 University of Utah.
3: * Copyright (c) 1991 The Regents of the University of California.
4: * All rights reserved.
5: *
6: * This code is derived from software contributed to Berkeley by
7: * the Systems Programming Group of the University of Utah Computer
8: * Science Department.
9: *
10: * Redistribution and use in source and binary forms, with or without
11: * modification, are permitted provided that the following conditions
12: * are met:
13: * 1. Redistributions of source code must retain the above copyright
14: * notice, this list of conditions and the following disclaimer.
15: * 2. Redistributions in binary form must reproduce the above copyright
16: * notice, this list of conditions and the following disclaimer in the
17: * documentation and/or other materials provided with the distribution.
18: * 3. All advertising materials mentioning features or use of this software
19: * must display the following acknowledgement:
20: * This product includes software developed by the University of
21: * California, Berkeley and its contributors.
22: * 4. Neither the name of the University nor the names of its contributors
23: * may be used to endorse or promote products derived from this software
24: * without specific prior written permission.
25: *
26: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36: * SUCH DAMAGE.
37: *
1.1.1.3 ! root 38: * from: @(#)device_pager.c 7.2 (Berkeley) 4/20/91
! 39: * device_pager.c,v 1.3 1993/07/02 10:26:49 cgd Exp
1.1 root 40: */
41:
42: /*
43: * Page to/from special files.
44: */
45:
46: #include "devpager.h"
47: #if NDEVPAGER > 0
48:
49: #include "param.h"
50: #include "conf.h"
51: #include "mman.h"
52: #include "malloc.h"
53:
54: #include "vm.h"
55: #include "vm_page.h"
56: #include "vm_kern.h"
57: #include "device_pager.h"
58:
59: queue_head_t dev_pager_list; /* list of managed devices */
60:
61: #ifdef DEBUG
62: int dpagerdebug = 0;
63: #define DDB_FOLLOW 0x01
64: #define DDB_INIT 0x02
65: #define DDB_ALLOC 0x04
66: #define DDB_FAIL 0x08
67: #endif
68:
69: void
70: dev_pager_init()
71: {
72: #ifdef DEBUG
73: if (dpagerdebug & DDB_FOLLOW)
74: printf("dev_pager_init()\n");
75: #endif
76: queue_init(&dev_pager_list);
77: }
78:
79: vm_pager_t
80: dev_pager_alloc(handle, size, prot)
81: caddr_t handle;
82: vm_size_t size;
83: vm_prot_t prot;
84: {
85: dev_t dev;
86: vm_pager_t pager;
87: int (*mapfunc)(), nprot;
88: register vm_object_t object;
89: register vm_page_t page;
90: register dev_pager_t devp;
91: register int npages, off;
92: extern int nullop(), enodev();
93:
94:
95: #ifdef DEBUG
96: if (dpagerdebug & DDB_FOLLOW)
97: printf("dev_pager_alloc(%x, %x, %x)\n", handle, size, prot);
98: #endif
99: /*
100: * Pageout to device, should never happen.
101: */
102: if (handle == NULL)
103: panic("dev_pager_alloc called");
104:
105: /*
106: * Look it up, creating as necessary
107: */
108: pager = vm_pager_lookup(&dev_pager_list, handle);
109: if (pager == NULL) {
110: /*
111: * Validation. Make sure this device can be mapped
112: * and that range to map is acceptible to device.
113: */
114: dev = (dev_t)handle;
115: mapfunc = cdevsw[major(dev)].d_mmap;
116: if (!mapfunc || mapfunc == enodev || mapfunc == nullop)
117: return(NULL);
118: nprot = 0;
119: if (prot & VM_PROT_READ)
120: nprot |= PROT_READ;
121: if (prot & VM_PROT_WRITE)
122: nprot |= PROT_WRITE;
123: if (prot & VM_PROT_EXECUTE)
124: nprot |= PROT_EXEC;
125: npages = atop(round_page(size));
126: for (off = 0; npages--; off += PAGE_SIZE)
127: if ((*mapfunc)(dev, off, nprot) == -1)
128: return(NULL);
129: /*
130: * Allocate and initialize pager structs
131: */
132: pager = (vm_pager_t)malloc(sizeof *pager, M_VMPAGER, M_WAITOK);
133: if (pager == NULL)
134: return(NULL);
135: devp = (dev_pager_t)malloc(sizeof *devp, M_VMPGDATA, M_WAITOK);
136: if (devp == NULL) {
137: free((caddr_t)pager, M_VMPAGER);
138: return(NULL);
139: }
140: devp->devp_dev = dev;
141: devp->devp_npages = atop(round_page(size));
142: pager->pg_handle = handle;
143: pager->pg_ops = &devicepagerops;
144: pager->pg_type = PG_DEVICE;
145: pager->pg_data = (caddr_t)devp;
146: /*
147: * Allocate object and vm_page structures to describe memory
148: */
149: npages = devp->devp_npages;
150: object = devp->devp_object = vm_object_allocate(ptoa(npages));
151: vm_object_enter(object, pager);
152: vm_object_setpager(object, pager, (vm_offset_t)0, FALSE);
153: devp->devp_pages = (vm_page_t)
154: kmem_alloc(kernel_map, npages*sizeof(struct vm_page));
155: off = 0;
156: for (page = devp->devp_pages;
157: page < &devp->devp_pages[npages]; page++) {
158: vm_object_lock(object);
159: vm_page_init(page, object, off);
160: page->phys_addr =
161: pmap_phys_address((*mapfunc)(dev, off, nprot));
162: page->wire_count = 1;
163: page->fictitious = TRUE;
164: PAGE_WAKEUP(page);
165: vm_object_unlock(object);
166: off += PAGE_SIZE;
167: }
168: /*
169: * Finally, put it on the managed list so other can find it.
170: */
1.1.1.2 root 171: queue_enter(&dev_pager_list, pager, vm_pager_t, pg_list);
1.1 root 172: #ifdef DEBUG
173: if (dpagerdebug & DDB_ALLOC)
174: printf("dev_pager_alloc: pages %d@%x\n",
175: devp->devp_npages, devp->devp_pages);
176: #endif
177: } else {
178: /*
179: * vm_object_lookup() gains a reference and also
180: * removes the object from the cache.
181: */
182: devp = (dev_pager_t)pager->pg_data;
183: if (vm_object_lookup(pager) != devp->devp_object)
184: panic("dev_pager_setup: bad object");
185: }
186: #ifdef DEBUG
187: if (dpagerdebug & DDB_ALLOC) {
188: printf("dev_pager_alloc: pager %x devp %x object %x\n",
189: pager, devp, object);
190: vm_object_print(object, FALSE);
191: }
192: #endif
193: return(pager);
194:
195: }
196:
197: void
198: dev_pager_dealloc(pager)
199: vm_pager_t pager;
200: {
201: dev_pager_t devp = (dev_pager_t)pager->pg_data;
202: register vm_object_t object;
203:
204: #ifdef DEBUG
205: if (dpagerdebug & DDB_FOLLOW)
206: printf("dev_pager_dealloc(%x)\n", pager);
207: #endif
1.1.1.2 root 208: queue_remove(&dev_pager_list, pager, vm_pager_t, pg_list);
1.1 root 209: object = devp->devp_object;
210: #ifdef DEBUG
211: if (dpagerdebug & DDB_ALLOC)
212: printf("dev_pager_dealloc: devp %x object %x pages %d@%x\n",
213: devp, object, devp->devp_npages, devp->devp_pages);
214: #endif
215: while (!queue_empty(&object->memq))
216: vm_page_remove((vm_page_t)queue_first(&object->memq));
217: kmem_free(kernel_map, devp->devp_pages,
218: devp->devp_npages * sizeof(struct vm_page));
219: free((caddr_t)devp, M_VMPGDATA);
1.1.1.2 root 220: free((caddr_t)pager, M_VMPAGER);
1.1 root 221: }
222:
223: dev_pager_getpage(pager, m, sync)
224: vm_pager_t pager;
225: vm_page_t m;
226: boolean_t sync;
227: {
228: #ifdef DEBUG
229: if (dpagerdebug & DDB_FOLLOW)
230: printf("dev_pager_getpage(%x, %x)\n", pager, m);
231: #endif
232: return(VM_PAGER_BAD);
233: }
234:
235: dev_pager_putpage(pager, m, sync)
236: vm_pager_t pager;
237: vm_page_t m;
238: boolean_t sync;
239: {
240: #ifdef DEBUG
241: if (dpagerdebug & DDB_FOLLOW)
242: printf("dev_pager_putpage(%x, %x)\n", pager, m);
243: #endif
244: if (pager == NULL)
245: return;
246: panic("dev_pager_putpage called");
247: }
248:
249: boolean_t
250: dev_pager_haspage(pager, offset)
251: vm_pager_t pager;
252: vm_offset_t offset;
253: {
254: #ifdef DEBUG
255: if (dpagerdebug & DDB_FOLLOW)
256: printf("dev_pager_haspage(%x, %x)\n", pager, offset);
257: #endif
258: return(TRUE);
259: }
260: #endif
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.