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