pmap.h revision 1.22 1 /* $NetBSD: pmap.h,v 1.22 2022/10/26 07:35:20 skrll Exp $ */
2
3 /*
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Ralph Campbell.
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. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * @(#)pmap.h 8.1 (Berkeley) 6/10/93
35 */
36
37 /*
38 * Copyright (c) 1987 Carnegie-Mellon University
39 *
40 * This code is derived from software contributed to Berkeley by
41 * Ralph Campbell.
42 *
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. All advertising materials mentioning features or use of this software
52 * must display the following acknowledgement:
53 * This product includes software developed by the University of
54 * California, Berkeley and its contributors.
55 * 4. Neither the name of the University nor the names of its contributors
56 * may be used to endorse or promote products derived from this software
57 * without specific prior written permission.
58 *
59 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69 * SUCH DAMAGE.
70 *
71 * @(#)pmap.h 8.1 (Berkeley) 6/10/93
72 */
73
74 #ifndef _UVM_PMAP_PMAP_H_
75 #define _UVM_PMAP_PMAP_H_
76
77 #include <sys/rwlock.h>
78 #include <uvm/uvm_object.h>
79 #include <uvm/uvm_stat.h>
80
81 #ifdef UVMHIST
82 UVMHIST_DECL(pmapexechist);
83 UVMHIST_DECL(pmaphist);
84 UVMHIST_DECL(pmapxtabhist);
85 #endif
86
87 /*
88 * Alternate mapping hooks for pool pages. Avoids thrashing the TLB.
89 */
90 struct vm_page *pmap_md_alloc_poolpage(int);
91
92 #if !defined(KASAN)
93 vaddr_t pmap_map_poolpage(paddr_t);
94 paddr_t pmap_unmap_poolpage(vaddr_t);
95 #define PMAP_ALLOC_POOLPAGE(flags) pmap_md_alloc_poolpage(flags)
96 #define PMAP_MAP_POOLPAGE(pa) pmap_map_poolpage(pa)
97 #define PMAP_UNMAP_POOLPAGE(va) pmap_unmap_poolpage(va)
98
99 #if defined(_LP64)
100 #define PMAP_DIRECT
101 static __inline int
102 pmap_direct_process(paddr_t pa, voff_t pgoff, size_t len,
103 int (*process)(void *, size_t, void *), void *arg)
104 {
105 vaddr_t va = pmap_md_direct_map_paddr(pa);
106
107 return process((void *)(va + pgoff), len, arg);
108 }
109 #endif
110 #endif
111
112 #define PMAP_MAP_PDETABPAGE(pa) pmap_md_map_poolpage(pa, PAGE_SIZE)
113 #define PMAP_MAP_SEGTABPAGE(pa) pmap_md_map_poolpage(pa, PAGE_SIZE)
114 #define PMAP_MAP_PTEPAGE(pa) pmap_md_map_poolpage(pa, PAGE_SIZE)
115
116 /*
117 * The user address space is mapped using a two level structure where
118 * virtual address bits 31..22 are used to index into a segment table which
119 * points to a page worth of PTEs (4096 page can hold 1024 PTEs).
120 * Bits 21..12 are then used to index a PTE which describes a page within
121 * a segment.
122 */
123
124 #define pmap_trunc_seg(x) ((vaddr_t)(x) & ~SEGOFSET)
125 #define pmap_round_seg(x) (((vaddr_t)(x) + SEGOFSET) & ~SEGOFSET)
126
127 /*
128 * Each ptpage maps a "segment" worth of address space. That is
129 * NPTEPG * PAGE_SIZE.
130 */
131
132 typedef struct {
133 pt_entry_t ppg_ptes[NPTEPG];
134 } pmap_ptpage_t;
135
136 #if defined(PMAP_HWPAGEWALKER)
137 typedef union pmap_pdetab {
138 pd_entry_t pde_pde[PMAP_PDETABSIZE];
139 union pmap_pdetab * pde_next;
140 } pmap_pdetab_t;
141 #endif
142 #if !defined(PMAP_HWPAGEWALKER) || !defined(PMAP_MAP_PDETABPAGE)
143 typedef union pmap_segtab {
144 #ifdef _LP64
145 union pmap_segtab * seg_seg[PMAP_SEGTABSIZE];
146 #endif
147 pmap_ptpage_t * seg_ppg[PMAP_SEGTABSIZE];
148 #ifdef PMAP_HWPAGEWALKER
149 pd_entry_t seg_pde[PMAP_PDETABSIZE];
150 #endif
151 union pmap_segtab * seg_next;
152 } pmap_segtab_t;
153 #endif
154
155
156 #ifdef _KERNEL
157 struct pmap;
158 typedef bool (*pte_callback_t)(struct pmap *, vaddr_t, vaddr_t,
159 pt_entry_t *, uintptr_t);
160
161 /*
162 * Common part of the bootstraping the system enough to run with
163 * virtual memory.
164 */
165 void pmap_bootstrap_common(void);
166
167 pt_entry_t *pmap_pte_lookup(struct pmap *, vaddr_t);
168 pt_entry_t *pmap_pte_reserve(struct pmap *, vaddr_t, int);
169 void pmap_pte_process(struct pmap *, vaddr_t, vaddr_t, pte_callback_t,
170 uintptr_t);
171 void pmap_segtab_activate(struct pmap *, struct lwp *);
172 void pmap_segtab_deactivate(struct pmap *);
173 void pmap_segtab_init(struct pmap *);
174 void pmap_segtab_destroy(struct pmap *, pte_callback_t, uintptr_t);
175 #ifdef PMAP_HWPAGEWALKER
176 pd_entry_t *pmap_pde_lookup(struct pmap *, vaddr_t, paddr_t *);
177 bool pmap_pdetab_fixup(struct pmap *, vaddr_t);
178 #endif
179 extern kmutex_t pmap_segtab_lock;
180 #endif /* _KERNEL */
181
182 #ifdef MULTIPROCESSOR
183 #include <sys/kcpuset.h>
184 #endif
185 #include <uvm/pmap/pmap_tlb.h>
186
187 /*
188 * Machine dependent pmap structure.
189 */
190 struct pmap {
191 struct uvm_object pm_uobject;
192 #define pm_count pm_uobject.uo_refs /* pmap reference count */
193 #define pm_pvp_list pm_uobject.memq
194
195 krwlock_t pm_obj_lock; /* lock for pm_uobject */
196 #define pm_lock pm_uobject.vmobjlock
197
198 struct pglist pm_ppg_list;
199 #if defined(PMAP_HWPAGEWALKER)
200 struct pglist pm_pdetab_list;
201 #endif
202 #if !defined(PMAP_HWPAGEWALKER) || !defined(PMAP_MAP_PDETABPAGE)
203 struct pglist pm_segtab_list;
204 #endif
205 #ifdef MULTIPROCESSOR
206 kcpuset_t *pm_active; /* pmap was active on ... */
207 kcpuset_t *pm_onproc; /* pmap is active on ... */
208 volatile u_int pm_shootdown_pending;
209 #endif
210 #if defined(PMAP_HWPAGEWALKER)
211 pmap_pdetab_t * pm_pdetab; /* pointer to HW PDEs */
212 #endif
213 #if !defined(PMAP_HWPAGEWALKER) || !defined(PMAP_MAP_PDETABPAGE)
214 pmap_segtab_t * pm_segtab; /* pointers to pages of PTEs; or */
215 /* virtual shadow of HW PDEs */
216 #endif
217 u_int pm_flags;
218 #define PMAP_DEFERRED_ACTIVATE __BIT(0)
219 struct pmap_statistics pm_stats; /* pmap statistics */
220 vaddr_t pm_minaddr;
221 vaddr_t pm_maxaddr;
222 #ifdef __HAVE_PMAP_MD
223 struct pmap_md pm_md;
224 #endif
225 struct pmap_asid_info pm_pai[1];
226 };
227
228 static inline void
229 pmap_lock(struct pmap *pm)
230 {
231
232 rw_enter(pm->pm_lock, RW_WRITER);
233 }
234
235 static inline void
236 pmap_unlock(struct pmap *pm)
237 {
238
239 rw_exit(pm->pm_lock);
240 }
241
242 #ifdef _KERNEL
243 struct pmap_kernel {
244 struct pmap kernel_pmap;
245 #if defined(MULTIPROCESSOR) && PMAP_TLB_MAX > 1
246 struct pmap_asid_info kernel_pai[PMAP_TLB_MAX-1];
247 #endif
248 };
249
250 struct pmap_limits {
251 paddr_t avail_start;
252 paddr_t avail_end;
253 vaddr_t virtual_start;
254 vaddr_t virtual_end;
255 };
256
257 /*
258 * Initialize the kernel pmap.
259 */
260 #ifdef MULTIPROCESSOR
261 #define PMAP_SIZE offsetof(struct pmap, pm_pai[PMAP_TLB_MAX])
262 #else
263 #define PMAP_SIZE sizeof(struct pmap)
264 #endif
265
266 /*
267 * The pools from which pmap structures and sub-structures are allocated.
268 */
269 extern struct pool pmap_pmap_pool;
270 extern struct pool pmap_pv_pool;
271 extern struct pool_allocator pmap_pv_page_allocator;
272
273 extern struct pmap_kernel kernel_pmap_store;
274 extern struct pmap_limits pmap_limits;
275
276 extern u_int pmap_page_colormask;
277
278 /*
279 * The current top of kernel VM
280 */
281 extern vaddr_t pmap_curmaxkvaddr;
282
283 #if defined(PMAP_HWPAGEWALKER)
284 extern pmap_pdetab_t pmap_kern_pdetab;
285 #else
286 extern pmap_segtab_t pmap_kern_segtab;
287 #endif
288
289 #define pmap_wired_count(pmap) ((pmap)->pm_stats.wired_count)
290 #define pmap_resident_count(pmap) ((pmap)->pm_stats.resident_count)
291
292 bool pmap_remove_all(pmap_t);
293 void pmap_set_modified(paddr_t);
294 bool pmap_page_clear_attributes(struct vm_page_md *, u_int);
295 void pmap_page_set_attributes(struct vm_page_md *, u_int);
296 void pmap_pvlist_lock_init(size_t);
297 #ifdef PMAP_VIRTUAL_CACHE_ALIASES
298 void pmap_page_cache(struct vm_page_md *, bool);
299 #endif
300
301 #if defined(__HAVE_PMAP_PV_TRACK) && !defined(PMAP_PV_TRACK_ONLY_STUBS)
302 void pmap_pv_protect(paddr_t, vm_prot_t);
303 #endif
304
305 #define PMAP_WB 0
306 #define PMAP_WBINV 1
307 #define PMAP_INV 2
308
309 kmutex_t *pmap_pvlist_lock_addr(struct vm_page_md *);
310
311 #define PMAP_STEAL_MEMORY /* enable pmap_steal_memory() */
312 #define PMAP_GROWKERNEL /* enable pmap_growkernel() */
313
314 #define PMAP_COUNT(name) (pmap_evcnt_##name.ev_count++ + 0)
315 #define PMAP_COUNTER(name, desc) \
316 struct evcnt pmap_evcnt_##name = \
317 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap", desc); \
318 EVCNT_ATTACH_STATIC(pmap_evcnt_##name)
319
320
321 static inline pt_entry_t *
322 kvtopte(vaddr_t va)
323 {
324
325 return pmap_pte_lookup(pmap_kernel(), va);
326 }
327
328 /* for ddb */
329 void pmap_db_pmap_print(struct pmap *, void (*)(const char *, ...) __printflike(1, 2));
330 void pmap_db_mdpg_print(struct vm_page *, void (*)(const char *, ...) __printflike(1, 2));
331
332 #if defined(EFI_RUNTIME)
333 struct pmap *
334 pmap_efirt(void);
335
336 #define pmap_activate_efirt() pmap_md_activate_efirt()
337 #define pmap_deactivate_efirt() pmap_md_deactivate_efirt()
338
339 #endif
340
341 #endif /* _KERNEL */
342 #endif /* _UVM_PMAP_PMAP_H_ */
343