1/*-
2 * Copyright (c) 2000 Peter Wemm
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23 * SUCH DAMAGE.
24 */
25
26#include <sys/cdefs.h>
27__FBSDID("$FreeBSD: stable/10/sys/vm/phys_pager.c 310110 2016-12-15 10:47:35Z kib $");
28
29#include <sys/param.h>
30#include <sys/systm.h>
31#include <sys/conf.h>
32#include <sys/kernel.h>
33#include <sys/lock.h>
34#include <sys/proc.h>
35#include <sys/mutex.h>
36#include <sys/mman.h>
37#include <sys/rwlock.h>
38#include <sys/sysctl.h>
39
40#include <vm/vm.h>
41#include <vm/vm_param.h>
42#include <vm/vm_object.h>
43#include <vm/vm_page.h>
44#include <vm/vm_pager.h>
45
46/* list of phys pager objects */
47static struct pagerlst phys_pager_object_list;
48/* protect access to phys_pager_object_list */
49static struct mtx phys_pager_mtx;
50
51static void
52phys_pager_init(void)
53{
54
55	TAILQ_INIT(&phys_pager_object_list);
56	mtx_init(&phys_pager_mtx, "phys_pager list", NULL, MTX_DEF);
57}
58
59static vm_object_t
60phys_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
61    vm_ooffset_t foff, struct ucred *cred)
62{
63	vm_object_t object, object1;
64	vm_pindex_t pindex;
65
66	/*
67	 * Offset should be page aligned.
68	 */
69	if (foff & PAGE_MASK)
70		return (NULL);
71
72	pindex = OFF_TO_IDX(foff + PAGE_MASK + size);
73
74	if (handle != NULL) {
75		mtx_lock(&phys_pager_mtx);
76		/*
77		 * Look up pager, creating as necessary.
78		 */
79		object1 = NULL;
80		object = vm_pager_object_lookup(&phys_pager_object_list, handle);
81		if (object == NULL) {
82			/*
83			 * Allocate object and associate it with the pager.
84			 */
85			mtx_unlock(&phys_pager_mtx);
86			object1 = vm_object_allocate(OBJT_PHYS, pindex);
87			mtx_lock(&phys_pager_mtx);
88			object = vm_pager_object_lookup(&phys_pager_object_list,
89			    handle);
90			if (object != NULL) {
91				/*
92				 * We raced with other thread while
93				 * allocating object.
94				 */
95				if (pindex > object->size)
96					object->size = pindex;
97			} else {
98				object = object1;
99				object1 = NULL;
100				object->handle = handle;
101				TAILQ_INSERT_TAIL(&phys_pager_object_list,
102				    object, pager_object_list);
103			}
104		} else {
105			if (pindex > object->size)
106				object->size = pindex;
107		}
108		mtx_unlock(&phys_pager_mtx);
109		vm_object_deallocate(object1);
110	} else {
111		object = vm_object_allocate(OBJT_PHYS, pindex);
112	}
113
114	return (object);
115}
116
117static void
118phys_pager_dealloc(vm_object_t object)
119{
120
121	if (object->handle != NULL) {
122		VM_OBJECT_WUNLOCK(object);
123		mtx_lock(&phys_pager_mtx);
124		TAILQ_REMOVE(&phys_pager_object_list, object, pager_object_list);
125		mtx_unlock(&phys_pager_mtx);
126		VM_OBJECT_WLOCK(object);
127	}
128	object->handle = NULL;
129	object->type = OBJT_DEAD;
130}
131
132/*
133 * Fill as many pages as vm_fault has allocated for us.
134 */
135static int
136phys_pager_getpages(vm_object_t object, vm_page_t *m, int count, int reqpage)
137{
138	int i;
139
140	VM_OBJECT_ASSERT_WLOCKED(object);
141	for (i = 0; i < count; i++) {
142		if (m[i]->valid == 0) {
143			if ((m[i]->flags & PG_ZERO) == 0)
144				pmap_zero_page(m[i]);
145			m[i]->valid = VM_PAGE_BITS_ALL;
146		}
147		KASSERT(m[i]->valid == VM_PAGE_BITS_ALL,
148		    ("phys_pager_getpages: partially valid page %p", m[i]));
149		KASSERT(m[i]->dirty == 0,
150		    ("phys_pager_getpages: dirty page %p", m[i]));
151		/* The requested page must remain busy, the others not. */
152		if (i == reqpage) {
153			vm_page_lock(m[i]);
154			vm_page_flash(m[i]);
155			vm_page_unlock(m[i]);
156		} else
157			vm_page_xunbusy(m[i]);
158	}
159	return (VM_PAGER_OK);
160}
161
162static void
163phys_pager_putpages(vm_object_t object, vm_page_t *m, int count, boolean_t sync,
164    int *rtvals)
165{
166
167	panic("phys_pager_putpage called");
168}
169
170/*
171 * Implement a pretty aggressive clustered getpages strategy.  Hint that
172 * everything in an entire 4MB window should be prefaulted at once.
173 *
174 * XXX 4MB (1024 slots per page table page) is convenient for x86,
175 * but may not be for other arches.
176 */
177#ifndef PHYSCLUSTER
178#define PHYSCLUSTER 1024
179#endif
180static boolean_t
181phys_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before,
182    int *after)
183{
184	vm_pindex_t base, end;
185
186	base = pindex & (~(PHYSCLUSTER - 1));
187	end = base + (PHYSCLUSTER - 1);
188	if (before != NULL)
189		*before = pindex - base;
190	if (after != NULL)
191		*after = end - pindex;
192	return (TRUE);
193}
194
195struct pagerops physpagerops = {
196	.pgo_init =	phys_pager_init,
197	.pgo_alloc =	phys_pager_alloc,
198	.pgo_dealloc = 	phys_pager_dealloc,
199	.pgo_getpages =	phys_pager_getpages,
200	.pgo_putpages =	phys_pager_putpages,
201	.pgo_haspage =	phys_pager_haspage,
202};
203