xref: /openbsd/sys/arch/riscv64/include/pmap.h (revision 6a951d55)
1 /*	$OpenBSD: pmap.h,v 1.13 2025/01/19 20:18:37 kettenis Exp $	*/
2 
3 /*
4  * Copyright (c) 2019-2020 Brian Bamsch <bbamsch@google.com>
5  * Copyright (c) 2008,2009,2014 Dale Rahn <drahn@dalerahn.com>
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 #ifndef	_MACHINE_PMAP_H_
20 #define	_MACHINE_PMAP_H_
21 
22 #ifndef _LOCORE
23 #include <sys/mutex.h>
24 #include <sys/queue.h>
25 #include <machine/pte.h>
26 #endif
27 
28 
29 /* V->P mapping data */
30 // XXX Only targeting compatibility with SV39
31 #define VP_IDX0_CNT	512
32 #define VP_IDX0_MASK	(VP_IDX0_CNT-1)
33 #define VP_IDX0_POS	39
34 #define VP_IDX1_CNT	512
35 #define VP_IDX1_MASK	(VP_IDX1_CNT-1)
36 #define VP_IDX1_POS	30
37 #define VP_IDX2_CNT	512
38 #define VP_IDX2_MASK	(VP_IDX2_CNT-1)
39 #define VP_IDX2_POS	21
40 #define VP_IDX3_CNT	512
41 #define VP_IDX3_MASK	(VP_IDX3_CNT-1)
42 #define VP_IDX3_POS	12
43 
44 /* cache flags */
45 // XXX These are duplicated from arm64 and may need some reworking
46 #define PMAP_CACHE_CI		(PMAP_MD0)		/* cache inhibit */
47 #define PMAP_CACHE_WB		(PMAP_MD1)		/* writeback */
48 #define PMAP_CACHE_DEV		(PMAP_MD2)		/* device mapping */
49 #define PMAP_CACHE_BITS		(PMAP_MD0|PMAP_MD1|PMAP_MD2)
50 
51 #define PTED_VA_MANAGED_M	(PMAP_MD3)
52 #define PTED_VA_WIRED_M		(PMAP_MD3 << 1)
53 #define PTED_VA_EXEC_M		(PMAP_MD3 << 2)
54 
55 #if defined(_KERNEL) && !defined(_LOCORE)
56 /*
57  * Pmap stuff
58  */
59 
60 typedef struct pmap *pmap_t;
61 
62 struct pmap {
63 	struct mutex pm_mtx;
64 	union {
65 		// XXX Sv48 not yet supported
66 		// XXX Consider inverting Lx
67 		struct pmapvp1 *l1;	/* virtual to physical table 3 lvl */
68 	} pm_vp;
69 	uint64_t pm_satp;
70 	int pm_privileged;
71 	int pm_refs;				/* ref count */
72 	struct pmap_statistics  pm_stats;	/* pmap statistics */
73 };
74 
75 #define PMAP_PA_MASK	~((paddr_t)PAGE_MASK) /* to remove the flags */
76 #define PMAP_NOCACHE	0x1 /* non-cacheable memory */
77 #define PMAP_DEVICE	0x2 /* device memory */
78 
79 #define PG_PMAP_MOD		PG_PMAP0
80 #define PG_PMAP_REF		PG_PMAP1
81 #define PG_PMAP_EXE		PG_PMAP2
82 
83 // [NCPUS]
84 extern paddr_t zero_page;
85 extern paddr_t copy_src_page;
86 extern paddr_t copy_dst_page;
87 
88 void pagezero(vaddr_t);
89 
90 extern struct pmap kernel_pmap_;
91 #define pmap_kernel()			(&kernel_pmap_)
92 #define	pmap_resident_count(pmap)	((pmap)->pm_stats.resident_count)
93 #define	pmap_wired_count(pmap)		((pmap)->pm_stats.wired_count)
94 
95 vaddr_t pmap_bootstrap(long kvo, paddr_t lpt1,
96 		vaddr_t kernelstart, vaddr_t kernelend,
97 		paddr_t memstart, paddr_t memend);
98 void pmap_kenter_cache(vaddr_t va, paddr_t pa, vm_prot_t prot, int cacheable);
99 void pmap_page_ro(pmap_t pm, vaddr_t va, vm_prot_t prot);
100 
101 paddr_t pmap_steal_avail(size_t size, int align, void **kva);
102 void pmap_avail_fixup(void);
103 void pmap_physload_avail(void);
104 
105 #define PMAP_GROWKERNEL
106 
107 #define	PHYS_TO_DMAP(pa)	((pa) - dmap_phys_base + DMAP_MIN_ADDRESS)
108 
109 struct pv_entry;
110 
111 /* investigate */
112 #define pmap_unuse_final(p)		do { /* nothing */ } while (0)
113 int	pmap_fault_fixup(pmap_t, vaddr_t, vm_prot_t);
114 void	pmap_postinit(void);
115 void	pmap_init_percpu(void);
116 
117 #define __HAVE_PMAP_POPULATE
118 
119 #endif /* _KERNEL && !_LOCORE */
120 
121 #ifndef _LOCORE
122 struct vm_page_md {
123 	struct mutex pv_mtx;
124 	LIST_HEAD(,pte_desc) pv_list;
125 };
126 
127 #define VM_MDPAGE_INIT(pg) do {			\
128 	mtx_init(&(pg)->mdpage.pv_mtx, IPL_VM);	\
129 	LIST_INIT(&((pg)->mdpage.pv_list));	\
130 } while (0)
131 #endif	/* _LOCORE */
132 
133 #endif	/* _MACHINE_PMAP_H_ */
134