xref: /freebsd/sys/arm64/include/pmap.h (revision 9d3dc1e5e0b0c59e1cc473953a8c509779685c2a)
1412042e2SAndrew Turner /*-
2412042e2SAndrew Turner  * Copyright (c) 1991 Regents of the University of California.
3412042e2SAndrew Turner  * All rights reserved.
4412042e2SAndrew Turner  *
5412042e2SAndrew Turner  * This code is derived from software contributed to Berkeley by
6412042e2SAndrew Turner  * the Systems Programming Group of the University of Utah Computer
7412042e2SAndrew Turner  * Science Department and William Jolitz of UUNET Technologies Inc.
8412042e2SAndrew Turner  *
9412042e2SAndrew Turner  * Redistribution and use in source and binary forms, with or without
10412042e2SAndrew Turner  * modification, are permitted provided that the following conditions
11412042e2SAndrew Turner  * are met:
12412042e2SAndrew Turner  * 1. Redistributions of source code must retain the above copyright
13412042e2SAndrew Turner  *    notice, this list of conditions and the following disclaimer.
14412042e2SAndrew Turner  * 2. Redistributions in binary form must reproduce the above copyright
15412042e2SAndrew Turner  *    notice, this list of conditions and the following disclaimer in the
16412042e2SAndrew Turner  *    documentation and/or other materials provided with the distribution.
17f72c920cSEd Maste  * 3. Neither the name of the University nor the names of its contributors
18412042e2SAndrew Turner  *    may be used to endorse or promote products derived from this software
19412042e2SAndrew Turner  *    without specific prior written permission.
20412042e2SAndrew Turner  *
21412042e2SAndrew Turner  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22412042e2SAndrew Turner  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23412042e2SAndrew Turner  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24412042e2SAndrew Turner  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25412042e2SAndrew Turner  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26412042e2SAndrew Turner  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27412042e2SAndrew Turner  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28412042e2SAndrew Turner  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29412042e2SAndrew Turner  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30412042e2SAndrew Turner  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31412042e2SAndrew Turner  * SUCH DAMAGE.
32412042e2SAndrew Turner  */
33412042e2SAndrew Turner 
34d5d97bedSMike Karels #ifdef __arm__
35d5d97bedSMike Karels #include <arm/pmap.h>
36d5d97bedSMike Karels #else /* !__arm__ */
37d5d97bedSMike Karels 
38412042e2SAndrew Turner #ifndef _MACHINE_PMAP_H_
39412042e2SAndrew Turner #define	_MACHINE_PMAP_H_
40412042e2SAndrew Turner 
41412042e2SAndrew Turner #include <machine/pte.h>
42412042e2SAndrew Turner 
43412042e2SAndrew Turner #ifndef LOCORE
44412042e2SAndrew Turner 
45412042e2SAndrew Turner #include <sys/queue.h>
46412042e2SAndrew Turner #include <sys/_lock.h>
47412042e2SAndrew Turner #include <sys/_mutex.h>
484d90a5afSJohn Baldwin #include <sys/_pv_entry.h>
49412042e2SAndrew Turner 
503969d2f9SAndrew Turner #include <vm/_vm_radix.h>
513969d2f9SAndrew Turner 
52412042e2SAndrew Turner #ifdef _KERNEL
53412042e2SAndrew Turner 
54412042e2SAndrew Turner #define	vtophys(va)	pmap_kextract((vm_offset_t)(va))
55412042e2SAndrew Turner 
56412042e2SAndrew Turner #endif
57412042e2SAndrew Turner 
58412042e2SAndrew Turner #define	pmap_page_get_memattr(m)	((m)->md.pv_memattr)
595cff1f4dSMark Johnston #define	pmap_page_is_write_mapped(m)	(((m)->a.flags & PGA_WRITEABLE) != 0)
60412042e2SAndrew Turner void pmap_page_set_memattr(vm_page_t m, vm_memattr_t ma);
61412042e2SAndrew Turner 
62412042e2SAndrew Turner /*
63412042e2SAndrew Turner  * Pmap stuff
64412042e2SAndrew Turner  */
65412042e2SAndrew Turner 
66d3eae160SAndrew Turner struct rangeset;
67d3eae160SAndrew Turner 
68412042e2SAndrew Turner struct md_page {
69412042e2SAndrew Turner 	TAILQ_HEAD(,pv_entry)	pv_list;
70412042e2SAndrew Turner 	int			pv_gen;
71412042e2SAndrew Turner 	vm_memattr_t		pv_memattr;
72412042e2SAndrew Turner };
73412042e2SAndrew Turner 
7423982ffdSAndrew Turner enum pmap_stage {
7523982ffdSAndrew Turner 	PM_INVALID,
7623982ffdSAndrew Turner 	PM_STAGE1,
7723982ffdSAndrew Turner 	PM_STAGE2,
7823982ffdSAndrew Turner };
79412042e2SAndrew Turner 
80412042e2SAndrew Turner struct pmap {
81412042e2SAndrew Turner 	struct mtx		pm_mtx;
8250e3ab6bSAlan Cox 	struct pmap_statistics	pm_stats;	/* pmap statistics */
839815c092SAndrew Turner 	uint64_t		pm_ttbr;
8450e3ab6bSAlan Cox 	vm_paddr_t		pm_l0_paddr;
85f2f21fafSAndrew Turner 	pd_entry_t		*pm_l0;
86412042e2SAndrew Turner 	TAILQ_HEAD(,pv_chunk)	pm_pvchunk;	/* list of mappings in pmap */
873969d2f9SAndrew Turner 	struct vm_radix		pm_root;	/* spare page table pages */
8850e3ab6bSAlan Cox 	long			pm_cookie;	/* encodes the pmap's ASID */
89249ecb63SAndrew Turner 	struct asid_set		*pm_asid_set;	/* The ASID/VMID set to use */
9023982ffdSAndrew Turner 	enum pmap_stage		pm_stage;
919815c092SAndrew Turner 	int			pm_levels;
92d3eae160SAndrew Turner 	struct rangeset		*pm_bti;
93d3eae160SAndrew Turner 	uint64_t		pm_reserved[3];
94412042e2SAndrew Turner };
951f15b0e6SMark Johnston typedef struct pmap *pmap_t;
96412042e2SAndrew Turner 
9745a91d45SAndrew Turner struct thread;
9845a91d45SAndrew Turner 
99412042e2SAndrew Turner #ifdef _KERNEL
100412042e2SAndrew Turner extern struct pmap	kernel_pmap_store;
101412042e2SAndrew Turner #define	kernel_pmap	(&kernel_pmap_store)
102412042e2SAndrew Turner #define	pmap_kernel()	kernel_pmap
103412042e2SAndrew Turner 
104412042e2SAndrew Turner #define	PMAP_ASSERT_LOCKED(pmap) \
105412042e2SAndrew Turner 				mtx_assert(&(pmap)->pm_mtx, MA_OWNED)
106412042e2SAndrew Turner #define	PMAP_LOCK(pmap)		mtx_lock(&(pmap)->pm_mtx)
107412042e2SAndrew Turner #define	PMAP_LOCK_ASSERT(pmap, type) \
108412042e2SAndrew Turner 				mtx_assert(&(pmap)->pm_mtx, (type))
109412042e2SAndrew Turner #define	PMAP_LOCK_DESTROY(pmap)	mtx_destroy(&(pmap)->pm_mtx)
110412042e2SAndrew Turner #define	PMAP_LOCK_INIT(pmap)	mtx_init(&(pmap)->pm_mtx, "pmap", \
111412042e2SAndrew Turner 				    NULL, MTX_DEF | MTX_DUPOK)
112412042e2SAndrew Turner #define	PMAP_OWNED(pmap)	mtx_owned(&(pmap)->pm_mtx)
113412042e2SAndrew Turner #define	PMAP_MTX(pmap)		(&(pmap)->pm_mtx)
114412042e2SAndrew Turner #define	PMAP_TRYLOCK(pmap)	mtx_trylock(&(pmap)->pm_mtx)
115412042e2SAndrew Turner #define	PMAP_UNLOCK(pmap)	mtx_unlock(&(pmap)->pm_mtx)
116412042e2SAndrew Turner 
11750e3ab6bSAlan Cox #define	ASID_RESERVED_FOR_PID_0	0
11850e3ab6bSAlan Cox #define	ASID_RESERVED_FOR_EFI	1
11950e3ab6bSAlan Cox #define	ASID_FIRST_AVAILABLE	(ASID_RESERVED_FOR_EFI + 1)
12050e3ab6bSAlan Cox #define	ASID_TO_OPERAND(asid)	({					\
12150e3ab6bSAlan Cox 	KASSERT((asid) != -1, ("invalid ASID"));			\
122459e0b92SAndrew Turner 	(uint64_t)(asid) << TTBR_ASID_SHIFT;			\
12350e3ab6bSAlan Cox })
12450e3ab6bSAlan Cox 
1258882b785SKonstantin Belousov #define	PMAP_WANT_ACTIVE_CPUS_NAIVE
1268882b785SKonstantin Belousov 
127412042e2SAndrew Turner extern vm_offset_t virtual_avail;
128412042e2SAndrew Turner extern vm_offset_t virtual_end;
129412042e2SAndrew Turner 
130*d52c3190SAndrew Turner extern pt_entry_t pmap_sh_attr;
131*d52c3190SAndrew Turner 
132412042e2SAndrew Turner /*
133412042e2SAndrew Turner  * Macros to test if a mapping is mappable with an L1 Section mapping
134412042e2SAndrew Turner  * or an L2 Large Page mapping.
135412042e2SAndrew Turner  */
136412042e2SAndrew Turner #define	L1_MAPPABLE_P(va, pa, size)					\
137412042e2SAndrew Turner 	((((va) | (pa)) & L1_OFFSET) == 0 && (size) >= L1_SIZE)
138412042e2SAndrew Turner 
139041b7317SKonstantin Belousov #define	pmap_vm_page_alloc_check(m)
140041b7317SKonstantin Belousov 
1412cb0e95fSAndrew Turner void	pmap_activate_vm(pmap_t);
142ba313626SAndrew Turner void	pmap_bootstrap(vm_size_t);
1430760b4c2SEd Maste int	pmap_change_attr(vm_offset_t va, vm_size_t size, int mode);
144a85ce4adSAndrew Turner int	pmap_change_prot(vm_offset_t va, vm_size_t size, vm_prot_t prot);
14578442297SEmmanuel Vadot void	pmap_kenter(vm_offset_t sva, vm_size_t size, vm_paddr_t pa, int mode);
146e5acd89cSAndrew Turner void	pmap_kenter_device(vm_offset_t, vm_size_t, vm_paddr_t);
147f64329bcSAndrew Turner bool	pmap_klookup(vm_offset_t va, vm_paddr_t *pa);
148412042e2SAndrew Turner vm_paddr_t pmap_kextract(vm_offset_t va);
149412042e2SAndrew Turner void	pmap_kremove(vm_offset_t);
150e5acd89cSAndrew Turner void	pmap_kremove_device(vm_offset_t, vm_size_t);
151ea8f128cSJohn Baldwin void	*pmap_mapdev_attr(vm_paddr_t pa, vm_size_t size, vm_memattr_t ma);
152d4586dd3SMark Johnston bool	pmap_page_is_mapped(vm_page_t m);
1539815c092SAndrew Turner int	pmap_pinit_stage(pmap_t, enum pmap_stage, int);
154398a929fSMark Johnston bool	pmap_ps_enabled(pmap_t pmap);
15550e3ab6bSAlan Cox uint64_t pmap_to_ttbr0(pmap_t pmap);
156e3917bb2SAndrew Turner void	pmap_disable_promotion(vm_offset_t sva, vm_size_t size);
157020edaeaSAndrew Turner void	pmap_map_delete(pmap_t, vm_offset_t, vm_offset_t);
158412042e2SAndrew Turner 
159ea8f128cSJohn Baldwin void	*pmap_mapdev(vm_paddr_t, vm_size_t);
16085ffc586SAndrew Turner void	*pmap_mapbios(vm_paddr_t, vm_size_t);
1617ae99f80SJohn Baldwin void	pmap_unmapdev(void *, vm_size_t);
1627ae99f80SJohn Baldwin void	pmap_unmapbios(void *, vm_size_t);
163412042e2SAndrew Turner 
1644961faaaSJohn Baldwin bool	pmap_map_io_transient(vm_page_t *, vm_offset_t *, int, bool);
1654961faaaSJohn Baldwin void	pmap_unmap_io_transient(vm_page_t *, vm_offset_t *, int, bool);
166412042e2SAndrew Turner 
1679a0de24bSAndrew Turner bool	pmap_get_tables(pmap_t, vm_offset_t, pd_entry_t **, pd_entry_t **,
168f2f21fafSAndrew Turner     pd_entry_t **, pt_entry_t **);
1699a0de24bSAndrew Turner 
170510a3f1bSAndrew Turner int	pmap_fault(pmap_t, uint64_t, uint64_t);
171510a3f1bSAndrew Turner 
17285b7c566SAndrew Turner struct pcb *pmap_switch(struct thread *);
17345a91d45SAndrew Turner 
1742cb0e95fSAndrew Turner extern void (*pmap_clean_stage2_tlbi)(void);
1756419b48fSAndrew Turner extern void (*pmap_stage2_invalidate_range)(uint64_t, vm_offset_t, vm_offset_t,
1766419b48fSAndrew Turner     bool);
1776419b48fSAndrew Turner extern void (*pmap_stage2_invalidate_all)(uint64_t);
1782cb0e95fSAndrew Turner 
179d3eae160SAndrew Turner int pmap_vmspace_copy(pmap_t, pmap_t);
180e7a9df16SKonstantin Belousov 
181d3eae160SAndrew Turner int pmap_bti_set(pmap_t, vm_offset_t, vm_offset_t);
182d3eae160SAndrew Turner int pmap_bti_clear(pmap_t, vm_offset_t, vm_offset_t);
183e7a9df16SKonstantin Belousov 
18489c52f9dSKyle Evans #if defined(KASAN) || defined(KMSAN)
18589c52f9dSKyle Evans struct arm64_bootparams;
18689c52f9dSKyle Evans 
1875fae5358SAndrew Turner void	pmap_bootstrap_san(void);
18889c52f9dSKyle Evans void	pmap_san_enter(vm_offset_t);
18989c52f9dSKyle Evans #endif
19089c52f9dSKyle Evans 
191412042e2SAndrew Turner #endif	/* _KERNEL */
192412042e2SAndrew Turner 
193412042e2SAndrew Turner #endif	/* !LOCORE */
194412042e2SAndrew Turner 
195412042e2SAndrew Turner #endif	/* !_MACHINE_PMAP_H_ */
196d5d97bedSMike Karels 
197d5d97bedSMike Karels #endif /* !__arm__ */
198