1/*	$NetBSD: pmap.h,v 1.134 2022/08/20 23:49:31 riastradh Exp $	*/
  2
  3/*
  4 * Copyright (c) 1997 Charles D. Cranor and Washington University.
  5 * All rights reserved.
  6 *
  7 * Redistribution and use in source and binary forms, with or without
  8 * modification, are permitted provided that the following conditions
  9 * are met:
 10 * 1. Redistributions of source code must retain the above copyright
 11 *    notice, this list of conditions and the following disclaimer.
 12 * 2. Redistributions in binary form must reproduce the above copyright
 13 *    notice, this list of conditions and the following disclaimer in the
 14 *    documentation and/or other materials provided with the distribution.
 15 *
 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 26 */
 27
 28/*
 29 * Copyright (c) 2001 Wasabi Systems, Inc.
 30 * All rights reserved.
 31 *
 32 * Written by Frank van der Linden for Wasabi Systems, Inc.
 33 *
 34 * Redistribution and use in source and binary forms, with or without
 35 * modification, are permitted provided that the following conditions
 36 * are met:
 37 * 1. Redistributions of source code must retain the above copyright
 38 *    notice, this list of conditions and the following disclaimer.
 39 * 2. Redistributions in binary form must reproduce the above copyright
 40 *    notice, this list of conditions and the following disclaimer in the
 41 *    documentation and/or other materials provided with the distribution.
 42 * 3. All advertising materials mentioning features or use of this software
 43 *    must display the following acknowledgement:
 44 *      This product includes software developed for the NetBSD Project by
 45 *      Wasabi Systems, Inc.
 46 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
 47 *    or promote products derived from this software without specific prior
 48 *    written permission.
 49 *
 50 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
 51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 52 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 53 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
 54 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 55 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 56 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 57 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 58 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 59 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 60 * POSSIBILITY OF SUCH DAMAGE.
 61 */
 62
 63/*
 64 * pmap.h: see pmap.c for the history of this pmap module.
 65 */
 66
 67#ifndef _X86_PMAP_H_
 68#define	_X86_PMAP_H_
 69
 70#if defined(_KERNEL)
 71#include <x86/pmap_pv.h>
 72#include <uvm/pmap/pmap_pvt.h>
 73
 74/*
 75 * MD flags that we use for pmap_enter and pmap_kenter_pa:
 76 */
 77
 78/*
 79 * macros
 80 */
 81
 82#define pmap_clear_modify(pg)		pmap_clear_attrs(pg, PP_ATTRS_D)
 83#define pmap_clear_reference(pg)	pmap_clear_attrs(pg, PP_ATTRS_A)
 84#define pmap_copy(DP,SP,D,L,S)		__USE(L)
 85#define pmap_is_modified(pg)		pmap_test_attrs(pg, PP_ATTRS_D)
 86#define pmap_is_referenced(pg)		pmap_test_attrs(pg, PP_ATTRS_A)
 87#define pmap_move(DP,SP,D,L,S)
 88#define pmap_phys_address(ppn)		(x86_ptob(ppn) & ~X86_MMAP_FLAG_MASK)
 89#define pmap_mmap_flags(ppn)		x86_mmap_flags(ppn)
 90
 91#if defined(__x86_64__) || defined(PAE)
 92#define X86_MMAP_FLAG_SHIFT	(64 - PGSHIFT)
 93#else
 94#define X86_MMAP_FLAG_SHIFT	(32 - PGSHIFT)
 95#endif
 96
 97#define X86_MMAP_FLAG_MASK	0xf
 98#define X86_MMAP_FLAG_PREFETCH	0x1
 99
100/*
101 * prototypes
102 */
103
104void		pmap_activate(struct lwp *);
105void		pmap_bootstrap(vaddr_t);
106bool		pmap_clear_attrs(struct vm_page *, unsigned);
107bool		pmap_pv_clear_attrs(paddr_t, unsigned);
108void		pmap_deactivate(struct lwp *);
109void		pmap_page_remove(struct vm_page *);
110void		pmap_pv_remove(paddr_t);
111void		pmap_remove(struct pmap *, vaddr_t, vaddr_t);
112bool		pmap_test_attrs(struct vm_page *, unsigned);
113void		pmap_write_protect(struct pmap *, vaddr_t, vaddr_t, vm_prot_t);
114void		pmap_load(void);
115paddr_t		pmap_init_tmp_pgtbl(paddr_t);
116bool		pmap_remove_all(struct pmap *);
117void		pmap_ldt_cleanup(struct lwp *);
118void		pmap_ldt_sync(struct pmap *);
119void		pmap_kremove_local(vaddr_t, vsize_t);
120
121#define	__HAVE_PMAP_PV_TRACK	1
122void		pmap_pv_init(void);
123void		pmap_pv_track(paddr_t, psize_t);
124void		pmap_pv_untrack(paddr_t, psize_t);
125
126u_int		x86_mmap_flags(paddr_t);
127
128#define PMAP_GROWKERNEL		/* turn on pmap_growkernel interface */
129#define PMAP_FORK		/* turn on pmap_fork interface */
130
131/*
132 * inline functions
133 */
134
135/*
136 * pmap_page_protect: change the protection of all recorded mappings
137 *	of a managed page
138 *
139 * => this function is a frontend for pmap_page_remove/pmap_clear_attrs
140 * => we only have to worry about making the page more protected.
141 *	unprotecting a page is done on-demand at fault time.
142 */
143
144__inline static void __unused
145pmap_page_protect(struct vm_page *pg, vm_prot_t prot)
146{
147	if ((prot & VM_PROT_WRITE) == 0) {
148		if (prot & (VM_PROT_READ|VM_PROT_EXECUTE)) {
149			(void)pmap_clear_attrs(pg, PP_ATTRS_W);
150		} else {
151			pmap_page_remove(pg);
152		}
153	}
154}
155
156/*
157 * pmap_pv_protect: change the protection of all recorded mappings
158 *	of an unmanaged page
159 */
160
161__inline static void __unused
162pmap_pv_protect(paddr_t pa, vm_prot_t prot)
163{
164	if ((prot & VM_PROT_WRITE) == 0) {
165		if (prot & (VM_PROT_READ|VM_PROT_EXECUTE)) {
166			(void)pmap_pv_clear_attrs(pa, PP_ATTRS_W);
167		} else {
168			pmap_pv_remove(pa);
169		}
170	}
171}
172
173/*
174 * pmap_protect: change the protection of pages in a pmap
175 *
176 * => this function is a frontend for pmap_remove/pmap_write_protect
177 * => we only have to worry about making the page more protected.
178 *	unprotecting a page is done on-demand at fault time.
179 */
180
181__inline static void __unused
182pmap_protect(struct pmap *pmap, vaddr_t sva, vaddr_t eva, vm_prot_t prot)
183{
184	if ((prot & VM_PROT_WRITE) == 0) {
185		if (prot & (VM_PROT_READ|VM_PROT_EXECUTE)) {
186			pmap_write_protect(pmap, sva, eva, prot);
187		} else {
188			pmap_remove(pmap, sva, eva);
189		}
190	}
191}
192
193paddr_t vtophys(vaddr_t);
194vaddr_t	pmap_map(vaddr_t, paddr_t, paddr_t, vm_prot_t);
195void	pmap_cpu_init_late(struct cpu_info *);
196
197/* pmap functions with machine addresses */
198void	pmap_kenter_ma(vaddr_t, paddr_t, vm_prot_t, u_int);
199int	pmap_enter_ma(struct pmap *, vaddr_t, paddr_t, paddr_t,
200	    vm_prot_t, u_int, int);
201bool	pmap_extract_ma(pmap_t, vaddr_t, paddr_t *);
202
203paddr_t pmap_get_physpage(void);
204
205/*
206 * Hooks for the pool allocator.
207 */
208#define	POOL_VTOPHYS(va)	vtophys((vaddr_t) (va))
209
210#ifdef __HAVE_DIRECT_MAP
211
212extern vaddr_t pmap_direct_base;
213extern vaddr_t pmap_direct_end;
214
215#define PMAP_DIRECT_BASE	pmap_direct_base
216#define PMAP_DIRECT_END		pmap_direct_end
217
218#define PMAP_DIRECT_MAP(pa)	((vaddr_t)PMAP_DIRECT_BASE + (pa))
219#define PMAP_DIRECT_UNMAP(va)	((paddr_t)(va) - PMAP_DIRECT_BASE)
220
221/*
222 * Alternate mapping hooks for pool pages.
223 */
224#define PMAP_MAP_POOLPAGE(pa)	PMAP_DIRECT_MAP((pa))
225#define PMAP_UNMAP_POOLPAGE(va)	PMAP_DIRECT_UNMAP((va))
226
227#endif /* __HAVE_DIRECT_MAP */
228
229#define	__HAVE_VM_PAGE_MD
230#define	VM_MDPAGE_INIT(pg) \
231	memset(&(pg)->mdpage, 0, sizeof((pg)->mdpage)); \
232	PMAP_PAGE_INIT(&(pg)->mdpage.mp_pp)
233
234struct vm_page_md {
235	struct pmap_page mp_pp;
236};
237
238#endif /* _KERNEL */
239
240#endif /* _X86_PMAP_H_ */