1 /*-
2 * Copyright (c) 1997 Berkeley Software Design, Inc. All rights reserved.
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 * 3. Berkeley Software Design Inc's name may not be used to endorse or
13 * promote products derived from this software without specific prior
14 * written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN INC ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN INC BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 * from BSDI: pmap.c,v 1.28.2.15 2000/04/27 03:10:31 cp Exp
29 */
30
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33
34 #include "opt_ddb.h"
35 #include "opt_pmap.h"
36
37 #include <sys/param.h>
38 #include <sys/queue.h>
39 #include <sys/ktr.h>
40 #include <sys/lock.h>
41 #include <sys/mutex.h>
42 #include <sys/proc.h>
43 #include <sys/smp.h>
44 #include <sys/sysctl.h>
45 #include <sys/systm.h>
46
47 #include <vm/vm.h>
48 #include <vm/vm_param.h>
49 #include <vm/vm_kern.h>
50 #include <vm/vm_page.h>
51 #include <vm/vm_map.h>
52 #include <vm/vm_object.h>
53 #include <vm/vm_extern.h>
54 #include <vm/vm_pageout.h>
55 #include <vm/vm_pager.h>
56
57 #include <machine/cpufunc.h>
58 #include <machine/frame.h>
59 #include <machine/trap.h>
60 #include <machine/pmap.h>
61 #include <machine/smp.h>
62 #include <machine/tlb.h>
63 #include <machine/tsb.h>
64 #include <machine/tte.h>
65
66 CTASSERT((1 << TTE_SHIFT) == sizeof(struct tte));
67 CTASSERT(TSB_BUCKET_MASK < (1 << 12));
68
69 PMAP_STATS_VAR(tsb_nrepl);
70 PMAP_STATS_VAR(tsb_nlookup_k);
71 PMAP_STATS_VAR(tsb_nlookup_u);
72 PMAP_STATS_VAR(tsb_nenter_k);
73 PMAP_STATS_VAR(tsb_nenter_k_oc);
74 PMAP_STATS_VAR(tsb_nenter_u);
75 PMAP_STATS_VAR(tsb_nenter_u_oc);
76 PMAP_STATS_VAR(tsb_nforeach);
77
78 struct tte *tsb_kernel;
79 vm_size_t tsb_kernel_mask;
80 vm_size_t tsb_kernel_size;
81 vm_paddr_t tsb_kernel_phys;
82 u_int tsb_kernel_ldd_phys;
83
84 struct tte *
85 tsb_tte_lookup(pmap_t pm, vm_offset_t va)
86 {
87 struct tte *bucket;
88 struct tte *tp;
89 u_long sz;
90 u_int i;
91
92 if (pm == kernel_pmap) {
93 PMAP_STATS_INC(tsb_nlookup_k);
94 tp = tsb_kvtotte(va);
95 if (tte_match(tp, va))
96 return (tp);
97 } else {
98 PMAP_LOCK_ASSERT(pm, MA_OWNED);
99 PMAP_STATS_INC(tsb_nlookup_u);
100 for (sz = TS_MIN; sz <= TS_MAX; sz++) {
101 bucket = tsb_vtobucket(pm, sz, va);
102 for (i = 0; i < TSB_BUCKET_SIZE; i++) {
103 tp = &bucket[i];
104 if (tte_match(tp, va))
105 return (tp);
106 }
107 }
108 }
109 return (NULL);
110 }
111
112 struct tte *
113 tsb_tte_enter(pmap_t pm, vm_page_t m, vm_offset_t va, u_long sz, u_long data)
114 {
115 struct tte *bucket;
116 struct tte *rtp;
117 struct tte *tp;
118 vm_offset_t ova;
119 int b0;
120 int i;
121
122 if (DCACHE_COLOR(VM_PAGE_TO_PHYS(m)) != DCACHE_COLOR(va)) {
123 CTR5(KTR_CT2,
124 "tsb_tte_enter: off colour va=%#lx pa=%#lx o=%p ot=%d pi=%#lx",
125 va, VM_PAGE_TO_PHYS(m), m->object,
126 m->object ? m->object->type : -1,
127 m->pindex);
128 if (pm == kernel_pmap)
129 PMAP_STATS_INC(tsb_nenter_k_oc);
130 else
131 PMAP_STATS_INC(tsb_nenter_u_oc);
132 }
133
134 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
135 PMAP_LOCK_ASSERT(pm, MA_OWNED);
136 if (pm == kernel_pmap) {
137 PMAP_STATS_INC(tsb_nenter_k);
138 tp = tsb_kvtotte(va);
139 KASSERT((tp->tte_data & TD_V) == 0,
140 ("tsb_tte_enter: replacing valid kernel mapping"));
141 goto enter;
142 }
143 PMAP_STATS_INC(tsb_nenter_u);
144
145 bucket = tsb_vtobucket(pm, sz, va);
146
147 tp = NULL;
148 rtp = NULL;
149 b0 = rd(tick) & (TSB_BUCKET_SIZE - 1);
150 i = b0;
151 do {
152 if ((bucket[i].tte_data & TD_V) == 0) {
153 tp = &bucket[i];
154 break;
155 }
156 if (tp == NULL) {
157 if ((bucket[i].tte_data & TD_REF) == 0)
158 tp = &bucket[i];
159 else if (rtp == NULL)
160 rtp = &bucket[i];
161 }
162 } while ((i = (i + 1) & (TSB_BUCKET_SIZE - 1)) != b0);
163
164 if (tp == NULL)
165 tp = rtp;
166 if ((tp->tte_data & TD_V) != 0) {
167 PMAP_STATS_INC(tsb_nrepl);
168 ova = TTE_GET_VA(tp);
169 pmap_remove_tte(pm, NULL, tp, ova);
170 tlb_page_demap(pm, ova);
171 }
172
173 enter:
174 if ((m->flags & PG_FICTITIOUS) == 0) {
175 data |= TD_CP;
176 if ((m->flags & PG_UNMANAGED) == 0) {
177 pm->pm_stats.resident_count++;
178 data |= TD_PV;
179 }
180 if (pmap_cache_enter(m, va) != 0)
181 data |= TD_CV;
182 TAILQ_INSERT_TAIL(&m->md.tte_list, tp, tte_link);
183 } else
184 data |= TD_FAKE | TD_E;
185
186 tp->tte_vpn = TV_VPN(va, sz);
187 tp->tte_data = data;
188
189 return (tp);
190 }
191
192 /*
193 * Traverse the tsb of a pmap, calling the callback function for any tte entry
194 * that has a virtual address between start and end. If this function returns 0,
195 * tsb_foreach() terminates.
196 * This is used by pmap_remove(), pmap_protect(), and pmap_copy() in the case
197 * that the number of pages in the range given to them reaches the
198 * dimensions of the tsb size as an optimization.
199 */
200 void
201 tsb_foreach(pmap_t pm1, pmap_t pm2, vm_offset_t start, vm_offset_t end,
202 tsb_callback_t *callback)
203 {
204 vm_offset_t va;
205 struct tte *tp;
206 struct tte *tsbp;
207 uintptr_t i;
208 uintptr_t n;
209
210 PMAP_STATS_INC(tsb_nforeach);
211 if (pm1 == kernel_pmap) {
212 tsbp = tsb_kernel;
213 n = tsb_kernel_size / sizeof(struct tte);
214 } else {
215 tsbp = pm1->pm_tsb;
216 n = TSB_SIZE;
217 }
218 for (i = 0; i < n; i++) {
219 tp = &tsbp[i];
220 if ((tp->tte_data & TD_V) != 0) {
221 va = TTE_GET_VA(tp);
222 if (va >= start && va < end) {
223 if (!callback(pm1, pm2, tp, va))
224 break;
225 }
226 }
227 }
228 }
Cache object: 30a4fddc966b91527a6d741f7ff0b728
|