| 1 | /*- |
| 2 | * Copyright (c) 1991 Regents of the University of California. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * This code is derived from software contributed to Berkeley by |
| 6 | * the Systems Programming Group of the University of Utah Computer |
| 7 | * Science Department and William Jolitz of UUNET Technologies Inc. |
| 8 | * |
| 9 | * Redistribution and use in source and binary forms, with or without |
| 10 | * modification, are permitted provided that the following conditions |
| 11 | * are met: |
| 12 | * 1. Redistributions of source code must retain the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer. |
| 14 | * 2. Redistributions in binary form must reproduce the above copyright |
| 15 | * notice, this list of conditions and the following disclaimer in the |
| 16 | * documentation and/or other materials provided with the distribution. |
| 17 | * 3. Neither the name of the University nor the names of its contributors |
| 18 | * may be used to endorse or promote products derived from this software |
| 19 | * without specific prior written permission. |
| 20 | * |
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 31 | * SUCH DAMAGE. |
| 32 | */ |
| 33 | |
| 34 | #ifdef __arm__ |
| 35 | #include <arm/pmap.h> |
| 36 | #else /* !__arm__ */ |
| 37 | |
| 38 | #ifndef _MACHINE_PMAP_H_ |
| 39 | #define	_MACHINE_PMAP_H_ |
| 40 | |
| 41 | #include <machine/pte.h> |
| 42 | |
| 43 | #ifndef LOCORE |
| 44 | |
| 45 | #include <sys/queue.h> |
| 46 | #include <sys/_lock.h> |
| 47 | #include <sys/_mutex.h> |
| 48 | #include <sys/_pv_entry.h> |
| 49 | |
| 50 | #include <vm/_vm_radix.h> |
| 51 | |
| 52 | #ifdef _KERNEL |
| 53 | |
| 54 | #define	vtophys(va)	pmap_kextract((vm_offset_t)(va)) |
| 55 | |
| 56 | #endif |
| 57 | |
| 58 | #define	pmap_page_get_memattr(m)	((m)->md.pv_memattr) |
| 59 | #define	pmap_page_is_write_mapped(m)	(((m)->a.flags & PGA_WRITEABLE) != 0) |
| 60 | void pmap_page_set_memattr(vm_page_t m, vm_memattr_t ma); |
| 61 | |
| 62 | /* |
| 63 | * Pmap stuff |
| 64 | */ |
| 65 | |
| 66 | struct rangeset; |
| 67 | |
| 68 | struct md_page { |
| 69 | 	TAILQ_HEAD(,pv_entry)	pv_list; |
| 70 | 	int			pv_gen; |
| 71 | 	vm_memattr_t		pv_memattr; |
| 72 | 	uint8_t			pv_reserve[3]; |
| 73 | }; |
| 74 | |
| 75 | enum pmap_stage { |
| 76 | 	PM_INVALID, |
| 77 | 	PM_STAGE1, |
| 78 | 	PM_STAGE2, |
| 79 | }; |
| 80 | |
| 81 | struct pmap { |
| 82 | 	struct mtx		pm_mtx; |
| 83 | 	struct pmap_statistics	pm_stats;	/* pmap statistics */ |
| 84 | 	uint64_t		pm_ttbr; |
| 85 | 	vm_paddr_t		pm_l0_paddr; |
| 86 | 	pd_entry_t		*pm_l0; |
| 87 | 	TAILQ_HEAD(,pv_chunk)	pm_pvchunk;	/* list of mappings in pmap */ |
| 88 | 	struct vm_radix		pm_root;	/* spare page table pages */ |
| 89 | 	long			pm_cookie;	/* encodes the pmap's ASID */ |
| 90 | 	struct asid_set		*pm_asid_set;	/* The ASID/VMID set to use */ |
| 91 | 	enum pmap_stage		pm_stage; |
| 92 | 	int			pm_levels; |
| 93 | 	struct rangeset		*pm_bti; |
| 94 | 	uint64_t		pm_reserved[3]; |
| 95 | }; |
| 96 | typedef struct pmap *pmap_t; |
| 97 | |
| 98 | struct thread; |
| 99 | |
| 100 | #ifdef _KERNEL |
| 101 | extern struct pmap	kernel_pmap_store; |
| 102 | #define	kernel_pmap	(&kernel_pmap_store) |
| 103 | #define	pmap_kernel()	kernel_pmap |
| 104 | |
| 105 | extern bool		pmap_lpa_enabled; |
| 106 | |
| 107 | #define	PMAP_ASSERT_LOCKED(pmap) \ |
| 108 | 				mtx_assert(&(pmap)->pm_mtx, MA_OWNED) |
| 109 | #define	PMAP_LOCK(pmap)		mtx_lock(&(pmap)->pm_mtx) |
| 110 | #define	PMAP_LOCK_ASSERT(pmap, type) \ |
| 111 | 				mtx_assert(&(pmap)->pm_mtx, (type)) |
| 112 | #define	PMAP_LOCK_DESTROY(pmap)	mtx_destroy(&(pmap)->pm_mtx) |
| 113 | #define	PMAP_LOCK_INIT(pmap)	mtx_init(&(pmap)->pm_mtx, "pmap", \ |
| 114 | 				 NULL, MTX_DEF | MTX_DUPOK) |
| 115 | #define	PMAP_OWNED(pmap)	mtx_owned(&(pmap)->pm_mtx) |
| 116 | #define	PMAP_MTX(pmap)		(&(pmap)->pm_mtx) |
| 117 | #define	PMAP_TRYLOCK(pmap)	mtx_trylock(&(pmap)->pm_mtx) |
| 118 | #define	PMAP_UNLOCK(pmap)	mtx_unlock(&(pmap)->pm_mtx) |
| 119 | |
| 120 | #define	ASID_RESERVED_FOR_PID_0	0 |
| 121 | #define	ASID_RESERVED_FOR_EFI	1 |
| 122 | #define	ASID_FIRST_AVAILABLE	(ASID_RESERVED_FOR_EFI + 1) |
| 123 | #define	ASID_TO_OPERAND(asid)	({					\ |
| 124 | 	KASSERT((asid) != -1, ("invalid ASID"));			\ |
| 125 | 	(uint64_t)(asid) << TTBR_ASID_SHIFT;			\ |
| 126 | }) |
| 127 | |
| 128 | #define	PMAP_WANT_ACTIVE_CPUS_NAIVE |
| 129 | |
| 130 | extern vm_offset_t virtual_avail; |
| 131 | extern vm_offset_t virtual_end; |
| 132 | |
| 133 | extern pt_entry_t pmap_sh_attr; |
| 134 | |
| 135 | /* |
| 136 | * Macros to test if a mapping is mappable with an L1 Section mapping |
| 137 | * or an L2 Large Page mapping. |
| 138 | */ |
| 139 | #define	L1_MAPPABLE_P(va, pa, size)					\ |
| 140 | 	((((va) | (pa)) & L1_OFFSET) == 0 && (size) >= L1_SIZE) |
| 141 | |
| 142 | #define	pmap_vm_page_alloc_check(m) |
| 143 | |
| 144 | void	pmap_activate_vm(pmap_t); |
| 145 | void	pmap_bootstrap_dmap(vm_size_t); |
| 146 | void	pmap_bootstrap(void); |
| 147 | int	pmap_change_attr(vm_offset_t va, vm_size_t size, int mode); |
| 148 | int	pmap_change_prot(vm_offset_t va, vm_size_t size, vm_prot_t prot); |
| 149 | void	pmap_kenter(vm_offset_t sva, vm_size_t size, vm_paddr_t pa, int mode); |
| 150 | void	pmap_kenter_device(vm_offset_t, vm_size_t, vm_paddr_t); |
| 151 | bool	pmap_klookup(vm_offset_t va, vm_paddr_t *pa); |
| 152 | vm_paddr_t pmap_kextract(vm_offset_t va); |
| 153 | void	pmap_kremove(vm_offset_t); |
| 154 | void	pmap_kremove_device(vm_offset_t, vm_size_t); |
| 155 | void	*pmap_mapdev_attr(vm_paddr_t pa, vm_size_t size, vm_memattr_t ma); |
| 156 | bool	pmap_page_is_mapped(vm_page_t m); |
| 157 | int	pmap_pinit_stage(pmap_t, enum pmap_stage, int); |
| 158 | bool	pmap_ps_enabled(pmap_t pmap); |
| 159 | uint64_t pmap_to_ttbr0(pmap_t pmap); |
| 160 | void	pmap_disable_promotion(vm_offset_t sva, vm_size_t size); |
| 161 | void	pmap_map_delete(pmap_t, vm_offset_t, vm_offset_t); |
| 162 | |
| 163 | void	*pmap_mapdev(vm_paddr_t, vm_size_t); |
| 164 | void	*pmap_mapbios(vm_paddr_t, vm_size_t); |
| 165 | void	pmap_unmapdev(void *, vm_size_t); |
| 166 | void	pmap_unmapbios(void *, vm_size_t); |
| 167 | |
| 168 | bool	pmap_map_io_transient(vm_page_t *, vm_offset_t *, int, bool); |
| 169 | void	pmap_unmap_io_transient(vm_page_t *, vm_offset_t *, int, bool); |
| 170 | |
| 171 | bool	pmap_get_tables(pmap_t, vm_offset_t, pd_entry_t **, pd_entry_t **, |
| 172 | pd_entry_t **, pt_entry_t **); |
| 173 | |
| 174 | int	pmap_fault(pmap_t, uint64_t, uint64_t); |
| 175 | |
| 176 | struct pcb *pmap_switch(struct thread *); |
| 177 | |
| 178 | void	pmap_s1_invalidate_all_kernel(void); |
| 179 | |
| 180 | extern void (*pmap_clean_stage2_tlbi)(void); |
| 181 | extern void (*pmap_stage2_invalidate_range)(uint64_t, vm_offset_t, vm_offset_t, |
| 182 | bool); |
| 183 | extern void (*pmap_stage2_invalidate_all)(uint64_t); |
| 184 | |
| 185 | int pmap_vmspace_copy(pmap_t, pmap_t); |
| 186 | |
| 187 | int pmap_bti_set(pmap_t, vm_offset_t, vm_offset_t); |
| 188 | int pmap_bti_clear(pmap_t, vm_offset_t, vm_offset_t); |
| 189 | |
| 190 | #if defined(KASAN) || defined(KMSAN) |
| 191 | struct arm64_bootparams; |
| 192 | |
| 193 | void	pmap_bootstrap_san(void); |
| 194 | void	pmap_san_enter(vm_offset_t); |
| 195 | #endif |
| 196 | |
| 197 | #endif	/* _KERNEL */ |
| 198 | |
| 199 | #endif	/* !LOCORE */ |
| 200 | |
| 201 | #endif	/* !_MACHINE_PMAP_H_ */ |
| 202 | |
| 203 | #endif /* !__arm__ */ |