Converted vm_page bit fields to flags to allow for some optimizations
[unix-history] / sys / vm / vm_pager.c
CommitLineData
15637ed4
RG
1/*
2 * Copyright (c) 1991 Regents of the University of California.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * The Mach Operating System project at Carnegie-Mellon University.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
1284e777 36 * from: @(#)vm_pager.c 7.4 (Berkeley) 5/7/91
fd76afd7 37 * $Id: vm_pager.c,v 1.7 1993/12/19 23:24:17 wollman Exp $
1284e777
RG
38 */
39
40/*
15637ed4
RG
41 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
42 * All rights reserved.
43 *
44 * Authors: Avadis Tevanian, Jr., Michael Wayne Young
45 *
46 * Permission to use, copy, modify and distribute this software and
47 * its documentation is hereby granted, provided that both the copyright
48 * notice and this permission notice appear in all copies of the
49 * software, derivative works or modified versions, and any portions
50 * thereof, and that both notices appear in supporting documentation.
51 *
52 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
53 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
54 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
55 *
56 * Carnegie Mellon requests users of this software to return to
57 *
58 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
59 * School of Computer Science
60 * Carnegie Mellon University
61 * Pittsburgh PA 15213-3890
62 *
63 * any improvements or extensions that they make and grant Carnegie the
64 * rights to redistribute these changes.
15637ed4
RG
65 */
66
67/*
68 * Paging space routine stubs. Emulates a matchmaker-like interface
69 * for builtin pagers.
70 */
71
72#include "param.h"
fde1aeb2 73#include "systm.h"
15637ed4
RG
74#include "malloc.h"
75
76#include "vm.h"
77#include "vm_page.h"
78#include "vm_kern.h"
79
15637ed4 80extern struct pagerops swappagerops;
15637ed4 81extern struct pagerops vnodepagerops;
15637ed4 82extern struct pagerops devicepagerops;
15637ed4
RG
83
84struct pagerops *pagertab[] = {
85 &swappagerops, /* PG_SWAP */
86 &vnodepagerops, /* PG_VNODE */
87 &devicepagerops, /* PG_DEV */
88};
89int npagers = sizeof (pagertab) / sizeof (pagertab[0]);
90
91struct pagerops *dfltpagerops = NULL; /* default pager */
92
93/*
94 * Kernel address space for mapping pages.
95 * Used by pagers where KVAs are needed for IO.
96 */
97#define PAGER_MAP_SIZE (256 * PAGE_SIZE)
98vm_map_t pager_map;
99vm_offset_t pager_sva, pager_eva;
100
101void
102vm_pager_init()
103{
104 struct pagerops **pgops;
105
106 /*
107 * Allocate a kernel submap for tracking get/put page mappings
108 */
109 pager_map = kmem_suballoc(kernel_map, &pager_sva, &pager_eva,
110 PAGER_MAP_SIZE, FALSE);
111 /*
112 * Initialize known pagers
9cd75be8 113 * If pgops is a null pointer skip over it.
15637ed4
RG
114 */
115 for (pgops = pagertab; pgops < &pagertab[npagers]; pgops++)
9cd75be8 116 if (*pgops) (*(*pgops)->pgo_init)();
15637ed4
RG
117 if (dfltpagerops == NULL)
118 panic("no default pager");
119}
120
121/*
122 * Allocate an instance of a pager of the given type.
123 */
124vm_pager_t
0a8fbd8d 125vm_pager_allocate(type, handle, size, prot, off)
15637ed4
RG
126 int type;
127 caddr_t handle;
128 vm_size_t size;
129 vm_prot_t prot;
0a8fbd8d 130 int off;
15637ed4
RG
131{
132 vm_pager_t pager;
133 struct pagerops *ops;
134
135 ops = (type == PG_DFLT) ? dfltpagerops : pagertab[type];
0a8fbd8d 136 return((*ops->pgo_alloc)(handle, size, prot, off));
15637ed4
RG
137}
138
139void
140vm_pager_deallocate(pager)
141 vm_pager_t pager;
142{
143 if (pager == NULL)
144 panic("vm_pager_deallocate: null pager");
145
146 VM_PAGER_DEALLOC(pager);
147}
148
4c45483e 149int
15637ed4
RG
150vm_pager_get(pager, m, sync)
151 vm_pager_t pager;
152 vm_page_t m;
153 boolean_t sync;
154{
155 extern boolean_t vm_page_zero_fill();
156
157 if (pager == NULL)
158 return(vm_page_zero_fill(m) ? VM_PAGER_OK : VM_PAGER_FAIL);
159 return(VM_PAGER_GET(pager, m, sync));
160}
161
4c45483e 162int
15637ed4
RG
163vm_pager_put(pager, m, sync)
164 vm_pager_t pager;
165 vm_page_t m;
166 boolean_t sync;
167{
168 if (pager == NULL)
169 panic("vm_pager_put: null pager");
170 return(VM_PAGER_PUT(pager, m, sync));
171}
172
173boolean_t
174vm_pager_has_page(pager, offset)
175 vm_pager_t pager;
176 vm_offset_t offset;
177{
178 if (pager == NULL)
179 panic("vm_pager_has_page");
180 return(VM_PAGER_HASPAGE(pager, offset));
181}
182
183/*
184 * Called by pageout daemon before going back to sleep.
185 * Gives pagers a chance to clean up any completed async pageing operations.
186 */
187void
188vm_pager_sync()
189{
190 struct pagerops **pgops;
191
192 for (pgops = pagertab; pgops < &pagertab[npagers]; pgops++)
193 (*(*pgops)->pgo_putpage)(NULL, NULL, FALSE);
194}
195
196vm_offset_t
197vm_pager_map_page(m)
198 vm_page_t m;
199{
200 vm_offset_t kva;
201
202#ifdef DEBUG
fd76afd7 203 if (!(m->flags & PG_BUSY) || (m->flags & PG_ACTIVE))
15637ed4 204 panic("vm_pager_map_page: page active or not busy");
fd76afd7 205 if (m->flags & PG_PAGEROWNED)
15637ed4
RG
206 printf("vm_pager_map_page: page %x already in pager\n", m);
207#endif
208 kva = kmem_alloc_wait(pager_map, PAGE_SIZE);
209#ifdef DEBUG
fd76afd7 210 m->flags |= PG_PAGEROWNED;
15637ed4
RG
211#endif
212 pmap_enter(vm_map_pmap(pager_map), kva, VM_PAGE_TO_PHYS(m),
213 VM_PROT_DEFAULT, TRUE);
214 return(kva);
215}
216
217void
218vm_pager_unmap_page(kva)
219 vm_offset_t kva;
220{
221#ifdef DEBUG
222 vm_page_t m;
223
224 m = PHYS_TO_VM_PAGE(pmap_extract(vm_map_pmap(pager_map), kva));
225#endif
226 kmem_free_wakeup(pager_map, kva, PAGE_SIZE);
227#ifdef DEBUG
fd76afd7
DG
228 if (m->flags & PG_PAGEROWNED)
229 m->flags &= ~PG_PAGEROWNED;
15637ed4
RG
230 else
231 printf("vm_pager_unmap_page: page %x(%x/%x) not owned\n",
232 m, kva, VM_PAGE_TO_PHYS(m));
233#endif
234}
235
236vm_pager_t
237vm_pager_lookup(list, handle)
238 register queue_head_t *list;
239 caddr_t handle;
240{
241 register vm_pager_t pager;
242
243 pager = (vm_pager_t) queue_first(list);
244 while (!queue_end(list, (queue_entry_t)pager)) {
245 if (pager->pg_handle == handle)
246 return(pager);
247 pager = (vm_pager_t) queue_next(&pager->pg_list);
248 }
249 return(NULL);
250}
251
252/*
253 * This routine gains a reference to the object.
254 * Explicit deallocation is necessary.
255 */
4c45483e 256int
15637ed4
RG
257pager_cache(object, should_cache)
258 vm_object_t object;
259 boolean_t should_cache;
260{
261 if (object == NULL)
262 return(KERN_INVALID_ARGUMENT);
263
264 vm_object_cache_lock();
265 vm_object_lock(object);
266 object->can_persist = should_cache;
267 vm_object_unlock(object);
268 vm_object_cache_unlock();
269
270 vm_object_deallocate(object);
271
272 return(KERN_SUCCESS);
273}