xref: /openbsd/sys/arch/alpha/include/pmap.h (revision 551b33bf)
1 /* $OpenBSD: pmap.h,v 1.46 2023/12/11 22:12:52 kettenis Exp $ */
2 /* $NetBSD: pmap.h,v 1.37 2000/11/19 03:16:35 thorpej Exp $ */
3 
4 /*-
5  * Copyright (c) 1998, 1999, 2000 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
10  * NASA Ames Research Center and by Chris G. Demetriou.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 /*
35  * Copyright (c) 1987 Carnegie-Mellon University
36  * Copyright (c) 1991, 1993
37  *	The Regents of the University of California.  All rights reserved.
38  *
39  * This code is derived from software contributed to Berkeley by
40  * the Systems Programming Group of the University of Utah Computer
41  * Science Department.
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. Neither the name of the University nor the names of its contributors
52  *    may be used to endorse or promote products derived from this software
53  *    without specific prior written permission.
54  *
55  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
56  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
59  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65  * SUCH DAMAGE.
66  *
67  *	@(#)pmap.h	8.1 (Berkeley) 6/10/93
68  */
69 
70 #ifndef	_PMAP_MACHINE_
71 #define	_PMAP_MACHINE_
72 
73 #include <sys/mutex.h>
74 #include <machine/pte.h>
75 
76 #ifdef _KERNEL
77 
78 #include <sys/queue.h>
79 
80 /*
81  * Machine-dependent virtual memory state.
82  *
83  * If we ever support processor numbers higher than 63, we'll have to
84  * rethink the CPU mask.
85  *
86  * Note pm_asn and pm_asngen are arrays allocated in pmap_create().
87  * Their size is based on the PCS count from the HWRPB, and indexed
88  * by processor ID (from `whami').
89  *
90  * The kernel pmap is a special case; it gets statically-allocated
91  * arrays which hold enough for ALPHA_MAXPROCS.
92  */
93 struct pmap_asn_info {
94 	unsigned int		pma_asn;	/* address space number */
95 	unsigned long		pma_asngen;	/* ASN generation number */
96 };
97 
98 struct pmap {
99 	TAILQ_ENTRY(pmap)	pm_list;	/* list of all pmaps */
100 	pt_entry_t		*pm_lev1map;	/* level 1 map */
101 	int			pm_count;	/* pmap reference count */
102 	struct mutex		pm_mtx;		/* lock on pmap */
103 	struct pmap_statistics	pm_stats;	/* pmap statistics */
104 	unsigned long		pm_cpus;	/* mask of CPUs using pmap */
105 	unsigned long		pm_needisync;	/* mask of CPUs needing isync */
106 	struct pmap_asn_info	pm_asni[1];	/* ASN information */
107 			/*	variable length		*/
108 };
109 typedef struct pmap	*pmap_t;
110 
111 /*
112  * Compute the sizeof of a pmap structure.  Subtract one because one
113  * ASN info structure is already included in the pmap structure itself.
114  */
115 #define PMAP_SIZEOF(x)							\
116 	(ALIGN(sizeof(struct pmap) +					\
117 	       (sizeof(struct pmap_asn_info) * ((x) - 1))))
118 
119 #define	PMAP_ASN_RESERVED	0	/* reserved for Lev1map users */
120 
121 extern struct pmap	kernel_pmap_store[];
122 
123 #define pmap_kernel()	kernel_pmap_store
124 
125 /*
126  * For each vm_page_t, there is a list of all currently valid virtual
127  * mappings of that page.  An entry is a pv_entry_t, the list is pv_table.
128  */
129 typedef struct pv_entry {
130 	struct pv_entry *pv_next;	/* next pv_entry on list */
131 	struct pmap	*pv_pmap;	/* pmap where mapping lies */
132 	vaddr_t		pv_va;		/* virtual address for mapping */
133 	pt_entry_t	*pv_pte;	/* PTE that maps the VA */
134 } *pv_entry_t;
135 
136 /* pg_flags extra flags */
137 #define	PG_PMAP_MOD		PG_PMAP0	/* modified */
138 #define	PG_PMAP_REF		PG_PMAP1	/* referenced */
139 
140 #if defined(MULTIPROCESSOR)
141 void	pmap_tlb_shootdown(pmap_t, vaddr_t, pt_entry_t, u_long *);
142 void	pmap_tlb_shootnow(u_long);
143 void	pmap_do_tlb_shootdown(struct cpu_info *, struct trapframe *);
144 #define	PMAP_TLB_SHOOTDOWN_CPUSET_DECL		u_long shootset = 0;
145 #define	PMAP_TLB_SHOOTDOWN(pm, va, pte)					\
146 	pmap_tlb_shootdown((pm), (va), (pte), &shootset)
147 #define	PMAP_TLB_SHOOTNOW()						\
148 	pmap_tlb_shootnow(shootset)
149 #else
150 #define	PMAP_TLB_SHOOTDOWN_CPUSET_DECL		/* nothing */
151 #define	PMAP_TLB_SHOOTDOWN(pm, va, pte)		/* nothing */
152 #define	PMAP_TLB_SHOOTNOW()			/* nothing */
153 #endif /* MULTIPROCESSOR */
154 
155 #define	pmap_resident_count(pmap)	((pmap)->pm_stats.resident_count)
156 #define	pmap_wired_count(pmap)		((pmap)->pm_stats.wired_count)
157 
158 #define pmap_update(pmap)		/* nothing (yet) */
159 
160 #define pmap_proc_iflush(p, va, len)	/* nothing */
161 #define pmap_init_percpu()		do { /* nothing */ } while (0)
162 #define pmap_unuse_final(p)		/* nothing */
163 #define	pmap_remove_holes(vm)		do { /* nothing */ } while (0)
164 
165 extern	pt_entry_t *VPT;		/* Virtual Page Table */
166 
167 #define PMAP_CHECK_COPYIN	1
168 
169 #define	PMAP_STEAL_MEMORY		/* enable pmap_steal_memory() */
170 #define PMAP_GROWKERNEL			/* enable pmap_growkernel() */
171 
172 /*
173  * Alternate mapping hooks for pool pages.  Avoids thrashing the TLB.
174  */
175 #define	pmap_map_direct(pg)	ALPHA_PHYS_TO_K0SEG(VM_PAGE_TO_PHYS(pg))
176 #define	pmap_unmap_direct(va)	PHYS_TO_VM_PAGE(ALPHA_K0SEG_TO_PHYS((va)))
177 #define	__HAVE_PMAP_DIRECT
178 
179 paddr_t vtophys(vaddr_t);
180 
181 #define	__HAVE_PMAP_COLLECT
182 
183 /* Machine-specific functions. */
184 void	pmap_bootstrap(paddr_t ptaddr, u_int maxasn, u_long ncpuids);
185 int	pmap_emulate_reference(struct proc *p, vaddr_t v, int user, int type);
186 
187 #define	pmap_pte_pa(pte)	(PG_PFNUM(*(pte)) << PAGE_SHIFT)
188 #define	pmap_pte_prot(pte)	(*(pte) & PG_PROT)
189 #define	pmap_pte_w(pte)		(*(pte) & PG_WIRED)
190 #define	pmap_pte_v(pte)		(*(pte) & PG_V)
191 #define	pmap_pte_pv(pte)	(*(pte) & PG_PVLIST)
192 #define	pmap_pte_asm(pte)	(*(pte) & PG_ASM)
193 #define	pmap_pte_exec(pte)	(*(pte) & PG_EXEC)
194 
195 #define	pmap_pte_set_w(pte, v)						\
196 do {									\
197 	if (v)								\
198 		*(pte) |= PG_WIRED;					\
199 	else								\
200 		*(pte) &= ~PG_WIRED;					\
201 } while (0)
202 
203 #define	pmap_pte_w_chg(pte, nw)	((nw) ^ pmap_pte_w(pte))
204 
205 #define	pmap_pte_set_prot(pte, np)					\
206 do {									\
207 	*(pte) &= ~PG_PROT;						\
208 	*(pte) |= (np);							\
209 } while (0)
210 
211 #define	pmap_pte_prot_chg(pte, np) ((np) ^ pmap_pte_prot(pte))
212 
213 static __inline pt_entry_t *pmap_l2pte(pmap_t, vaddr_t, pt_entry_t *);
214 static __inline pt_entry_t *pmap_l3pte(pmap_t, vaddr_t, pt_entry_t *);
215 
216 #define	pmap_l1pte(pmap, v)						\
217 	(&(pmap)->pm_lev1map[l1pte_index((vaddr_t)(v))])
218 
219 static __inline pt_entry_t *
pmap_l2pte(pmap,v,l1pte)220 pmap_l2pte(pmap, v, l1pte)
221 	pmap_t pmap;
222 	vaddr_t v;
223 	pt_entry_t *l1pte;
224 {
225 	pt_entry_t *lev2map;
226 
227 	if (l1pte == NULL) {
228 		l1pte = pmap_l1pte(pmap, v);
229 		if (pmap_pte_v(l1pte) == 0)
230 			return (NULL);
231 	}
232 
233 	lev2map = (pt_entry_t *)ALPHA_PHYS_TO_K0SEG(pmap_pte_pa(l1pte));
234 	return (&lev2map[l2pte_index(v)]);
235 }
236 
237 static __inline pt_entry_t *
pmap_l3pte(pmap,v,l2pte)238 pmap_l3pte(pmap, v, l2pte)
239 	pmap_t pmap;
240 	vaddr_t v;
241 	pt_entry_t *l2pte;
242 {
243 	pt_entry_t *l1pte, *lev2map, *lev3map;
244 
245 	if (l2pte == NULL) {
246 		l1pte = pmap_l1pte(pmap, v);
247 		if (pmap_pte_v(l1pte) == 0)
248 			return (NULL);
249 
250 		lev2map = (pt_entry_t *)ALPHA_PHYS_TO_K0SEG(pmap_pte_pa(l1pte));
251 		l2pte = &lev2map[l2pte_index(v)];
252 		if (pmap_pte_v(l2pte) == 0)
253 			return (NULL);
254 	}
255 
256 	lev3map = (pt_entry_t *)ALPHA_PHYS_TO_K0SEG(pmap_pte_pa(l2pte));
257 	return (&lev3map[l3pte_index(v)]);
258 }
259 
260 /*
261  * Macro for processing deferred I-stream synchronization.
262  *
263  * The pmap module may defer syncing the user I-stream until the
264  * return to userspace, since the IMB PALcode op can be quite
265  * expensive.  Since user instructions won't be executed until the
266  * return to userspace, this can be deferred until just before userret().
267  */
268 #define	PMAP_USERRET(pmap)						\
269 do {									\
270 	u_long cpu_mask = (1UL << cpu_number());			\
271 									\
272 	if ((pmap)->pm_needisync & cpu_mask) {				\
273 		atomic_clearbits_ulong(&(pmap)->pm_needisync,		\
274 		    cpu_mask);						\
275 		alpha_pal_imb();					\
276 	}								\
277 } while (0)
278 
279 #endif /* _KERNEL */
280 
281 /*
282  * pmap-specific data stored in the vm_page structure.
283  */
284 struct vm_page_md {
285 	struct mutex pvh_mtx;
286 	struct pv_entry *pvh_list;	/* pv entry list */
287 };
288 
289 #define	VM_MDPAGE_INIT(pg)						\
290 do {									\
291 	mtx_init(&(pg)->mdpage.pvh_mtx, IPL_VM);			\
292 	(pg)->mdpage.pvh_list = NULL;					\
293 } while (0)
294 
295 #endif /* _PMAP_MACHINE_ */
296