1*551b33bfSkettenis /* $OpenBSD: pmap.h,v 1.54 2023/12/11 22:12:53 kettenis Exp $ */
2f58c7388Spefo
3f58c7388Spefo /*
4f58c7388Spefo * Copyright (c) 1987 Carnegie-Mellon University
5f58c7388Spefo * Copyright (c) 1992, 1993
6f58c7388Spefo * The Regents of the University of California. All rights reserved.
7f58c7388Spefo *
8f58c7388Spefo * This code is derived from software contributed to Berkeley by
9f58c7388Spefo * Ralph Campbell.
10f58c7388Spefo *
11f58c7388Spefo * Redistribution and use in source and binary forms, with or without
12f58c7388Spefo * modification, are permitted provided that the following conditions
13f58c7388Spefo * are met:
14f58c7388Spefo * 1. Redistributions of source code must retain the above copyright
15f58c7388Spefo * notice, this list of conditions and the following disclaimer.
16f58c7388Spefo * 2. Redistributions in binary form must reproduce the above copyright
17f58c7388Spefo * notice, this list of conditions and the following disclaimer in the
18f58c7388Spefo * documentation and/or other materials provided with the distribution.
1953aa784aSmiod * 3. Neither the name of the University nor the names of its contributors
20f58c7388Spefo * may be used to endorse or promote products derived from this software
21f58c7388Spefo * without specific prior written permission.
22f58c7388Spefo *
23f58c7388Spefo * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24f58c7388Spefo * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25f58c7388Spefo * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26f58c7388Spefo * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27f58c7388Spefo * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28f58c7388Spefo * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29f58c7388Spefo * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30f58c7388Spefo * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31f58c7388Spefo * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32f58c7388Spefo * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33f58c7388Spefo * SUCH DAMAGE.
34f58c7388Spefo *
35f58c7388Spefo * from: @(#)pmap.h 8.1 (Berkeley) 6/10/93
36f58c7388Spefo */
37f58c7388Spefo
382fa72412Spirofti #ifndef _MIPS64_PMAP_H_
392fa72412Spirofti #define _MIPS64_PMAP_H_
40f58c7388Spefo
413595613dSvisa #include <sys/mutex.h>
423595613dSvisa
43332203bdSderaadt #ifdef _KERNEL
44332203bdSderaadt
4522899553Smiod #include <machine/pte.h>
4622899553Smiod
47f58c7388Spefo /*
48f39ec411Svisa * The user address space is currently limited to 1TB (0x0 - 0x10000000000).
49f58c7388Spefo *
50f58c7388Spefo * The user address space is mapped using a two level structure where
5188a9c312Smiod * the virtual addresses bits are split in three groups:
52f39ec411Svisa * segment:directory:page:offset
5388a9c312Smiod * where:
5488a9c312Smiod * - offset are the in-page offsets (PAGE_SHIFT bits)
55f39ec411Svisa * - page are the third level page table index
56f39ec411Svisa * (PMAP_PGSHIFT - Log2(pt_entry_t) bits)
57f39ec411Svisa * - directory are the second level page table (directory) index
58f39ec411Svisa * (PMAP_PGSHIFT - Log2(void *) bits)
5988a9c312Smiod * - segment are the first level page table (segment) index
60f39ec411Svisa * (PMAP_PGSHIFT - Log2(void *) bits)
6188a9c312Smiod *
62f39ec411Svisa * This scheme allows Segment, directory and page tables have the same size
63f39ec411Svisa * (1 << PMAP_PGSHIFT bytes, regardless of the pt_entry_t size) to be able to
6488a9c312Smiod * share the same allocator.
65f58c7388Spefo *
66f58c7388Spefo * Note: The kernel doesn't use the same data structures as user programs.
67f58c7388Spefo * All the PTE entries are stored in a single array in Sysmap which is
68f58c7388Spefo * dynamically allocated at boot time.
69f58c7388Spefo */
70f58c7388Spefo
71f39ec411Svisa #if defined(MIPS_PTE64) && PAGE_SHIFT == 12
72f39ec411Svisa #error "Cannot use MIPS_PTE64 with 4KB pages."
73f39ec411Svisa #endif
74f39ec411Svisa
75eb44e751Smiod /*
76f39ec411Svisa * Size of page table structs (page tables, page directories,
77f39ec411Svisa * and segment table) used by this pmap.
78eb44e751Smiod */
79eb44e751Smiod
80f39ec411Svisa #define PMAP_PGSHIFT 12
81f39ec411Svisa #define PMAP_PGSIZE (1UL << PMAP_PGSHIFT)
82eb44e751Smiod
83f39ec411Svisa #define NPDEPG (PMAP_PGSIZE / sizeof(void *))
84f39ec411Svisa #define NPTEPG (PMAP_PGSIZE / sizeof(pt_entry_t))
8588a9c312Smiod
86eb44e751Smiod /*
87eb44e751Smiod * Segment sizes
88eb44e751Smiod */
89eb44e751Smiod
90f39ec411Svisa #define SEGSHIFT (PAGE_SHIFT+PMAP_PGSHIFT*2-PTE_LOG-3)
91f39ec411Svisa #define DIRSHIFT (PAGE_SHIFT+PMAP_PGSHIFT-PTE_LOG)
92eb44e751Smiod #define NBSEG (1UL << SEGSHIFT)
93f39ec411Svisa #define NBDIR (1UL << DIRSHIFT)
94eb44e751Smiod #define SEGOFSET (NBSEG - 1)
95f39ec411Svisa #define DIROFSET (NBDIR - 1)
96eb44e751Smiod
97f58c7388Spefo #define mips_trunc_seg(x) ((vaddr_t)(x) & ~SEGOFSET)
98f39ec411Svisa #define mips_trunc_dir(x) ((vaddr_t)(x) & ~DIROFSET)
99f58c7388Spefo #define mips_round_seg(x) (((vaddr_t)(x) + SEGOFSET) & ~SEGOFSET)
100f39ec411Svisa #define mips_round_dir(x) (((vaddr_t)(x) + DIROFSET) & ~DIROFSET)
101f58c7388Spefo #define pmap_segmap(m, v) ((m)->pm_segtab->seg_tab[((v) >> SEGSHIFT)])
102f58c7388Spefo
10388a9c312Smiod /* number of segments entries */
104f39ec411Svisa #define PMAP_SEGTABSIZE (PMAP_PGSIZE / sizeof(void *))
105f58c7388Spefo
106f58c7388Spefo struct segtab {
107f39ec411Svisa pt_entry_t **seg_tab[PMAP_SEGTABSIZE];
108f58c7388Spefo };
109f58c7388Spefo
110af4bb61eSsyuu struct pmap_asid_info {
111af4bb61eSsyuu u_int pma_asid; /* address space tag */
112af4bb61eSsyuu u_int pma_asidgen; /* TLB PID generation number */
113af4bb61eSsyuu };
114af4bb61eSsyuu
115f58c7388Spefo /*
116f58c7388Spefo * Machine dependent pmap structure.
117f58c7388Spefo */
118f58c7388Spefo typedef struct pmap {
1193595613dSvisa struct mutex pm_mtx; /* pmap lock */
120b96ff64eSvisa struct mutex pm_swmtx; /* pmap switch lock */
121f58c7388Spefo int pm_count; /* pmap reference count */
122f58c7388Spefo struct pmap_statistics pm_stats; /* pmap statistics */
123f58c7388Spefo struct segtab *pm_segtab; /* pointers to pages of PTEs */
124af4bb61eSsyuu struct pmap_asid_info pm_asid[1]; /* ASID information */
125f58c7388Spefo } *pmap_t;
126f58c7388Spefo
127af4bb61eSsyuu /*
128af4bb61eSsyuu * Compute the sizeof of a pmap structure. Subtract one because one
129af4bb61eSsyuu * ASID info structure is already included in the pmap structure itself.
130af4bb61eSsyuu */
131af4bb61eSsyuu #define PMAP_SIZEOF(x) \
132af4bb61eSsyuu (ALIGN(sizeof(struct pmap) + \
133af4bb61eSsyuu (sizeof(struct pmap_asid_info) * ((x) - 1))))
134af4bb61eSsyuu
13592684a9fSmiod /* machine-dependent pg_flags */
136c43131adSkrw #define PGF_UNCACHED PG_PMAP0 /* Page is explicitly uncached */
13792684a9fSmiod #define PGF_CACHED PG_PMAP1 /* Page is currently cached */
13892684a9fSmiod #define PGF_ATTR_MOD PG_PMAP2
13992684a9fSmiod #define PGF_ATTR_REF PG_PMAP3
140f27624b4Smiod #define PGF_PRESERVE (PGF_ATTR_MOD | PGF_ATTR_REF)
141a08eeb7eSpefo
1425c264cc0Smiod #define PMAP_NOCACHE PMAP_MD0
1435c264cc0Smiod
144af4bb61eSsyuu extern struct pmap *const kernel_pmap_ptr;
145f58c7388Spefo
146f58c7388Spefo #define pmap_resident_count(pmap) ((pmap)->pm_stats.resident_count)
147f58c7388Spefo #define pmap_wired_count(pmap) ((pmap)->pm_stats.wired_count)
148af4bb61eSsyuu #define pmap_kernel() (kernel_pmap_ptr)
149f58c7388Spefo
150d62ebcb2Sderaadt extern pt_entry_t pg_ri;
151d62ebcb2Sderaadt #define PMAP_CHECK_COPYIN (pg_ri == 0)
152d62ebcb2Sderaadt
153f58c7388Spefo #define PMAP_STEAL_MEMORY /* Enable 'stealing' during boot */
154f58c7388Spefo
15558e38c50Smiod #define PMAP_PREFER
15625b86d72Smiod extern vaddr_t pmap_prefer_mask;
157f8945191Sariane /* pmap prefer alignment */
158f8945191Sariane #define PMAP_PREFER_ALIGN() \
15925b86d72Smiod (pmap_prefer_mask ? pmap_prefer_mask + 1 : 0)
160f8945191Sariane /* pmap prefer offset in alignment */
16125b86d72Smiod #define PMAP_PREFER_OFFSET(of) ((of) & pmap_prefer_mask)
162f8945191Sariane
163f58c7388Spefo void pmap_bootstrap(void);
16416e66f46Svisa int pmap_copyinsn(pmap_t, vaddr_t, uint32_t *);
165875d9761Svisa int pmap_emulate_modify(pmap_t, vaddr_t);
1661c16ef4cSmiod void pmap_page_cache(vm_page_t, u_int);
167f58c7388Spefo
168*551b33bfSkettenis #define pmap_init_percpu() do { /* nothing */ } while (0)
169f26d16c8Smiod #define pmap_unuse_final(p) do { /* nothing yet */ } while (0)
170120b8d62Smiod #define pmap_remove_holes(vm) do { /* nothing */ } while (0)
171f58c7388Spefo
17210aec90fSmiod #define __HAVE_PMAP_DIRECT
17310aec90fSmiod vaddr_t pmap_map_direct(vm_page_t);
17410aec90fSmiod vm_page_t pmap_unmap_direct(vaddr_t);
17510aec90fSmiod
176f88a4ea9Smiod #define __HAVE_PMAP_COLLECT
177f88a4ea9Smiod
1789cb97e1eSpirofti /*
1799cb97e1eSpirofti * MD flags to pmap_enter:
1809cb97e1eSpirofti */
1819cb97e1eSpirofti
1829cb97e1eSpirofti #define PMAP_PA_MASK ~((paddr_t)PAGE_MASK)
1839cb97e1eSpirofti
18488a9c312Smiod /* Kernel virtual address to page table entry */
18588a9c312Smiod #define kvtopte(va) \
18688a9c312Smiod (Sysmap + (((vaddr_t)(va) - VM_MIN_KERNEL_ADDRESS) >> PAGE_SHIFT))
18788a9c312Smiod /* User virtual address to pte page entry */
18888a9c312Smiod #define uvtopte(va) (((va) >> PAGE_SHIFT) & (NPTEPG -1))
189f39ec411Svisa #define uvtopde(va) (((va) >> DIRSHIFT) & (NPDEPG - 1))
190f39ec411Svisa
191f39ec411Svisa static inline pt_entry_t *
pmap_pte_lookup(struct pmap * pmap,vaddr_t va)192f39ec411Svisa pmap_pte_lookup(struct pmap *pmap, vaddr_t va)
193f39ec411Svisa {
194f39ec411Svisa pt_entry_t **pde, *pte;
195f39ec411Svisa
196f39ec411Svisa if ((pde = pmap_segmap(pmap, va)) == NULL)
197f39ec411Svisa return NULL;
198f39ec411Svisa if ((pte = pde[uvtopde(va)]) == NULL)
199f39ec411Svisa return NULL;
200f39ec411Svisa return pte + uvtopte(va);
201f39ec411Svisa }
20288a9c312Smiod
20388a9c312Smiod extern pt_entry_t *Sysmap; /* kernel pte table */
20488a9c312Smiod extern u_int Sysmapsize; /* number of pte's in Sysmap */
20588a9c312Smiod
206f58c7388Spefo #endif /* _KERNEL */
207f58c7388Spefo
20862fe2d4bSmiod #if !defined(_LOCORE)
20962fe2d4bSmiod typedef struct pv_entry {
21062fe2d4bSmiod struct pv_entry *pv_next; /* next pv_entry */
21162fe2d4bSmiod struct pmap *pv_pmap; /* pmap where mapping lies */
21262fe2d4bSmiod vaddr_t pv_va; /* virtual address for mapping */
21362fe2d4bSmiod } *pv_entry_t;
21462fe2d4bSmiod
21562fe2d4bSmiod struct vm_page_md {
2163595613dSvisa struct mutex pv_mtx; /* pv list lock */
21762fe2d4bSmiod struct pv_entry pv_ent; /* pv list of this seg */
21862fe2d4bSmiod };
21962fe2d4bSmiod
22062fe2d4bSmiod #define VM_MDPAGE_INIT(pg) \
22162fe2d4bSmiod do { \
2223595613dSvisa mtx_init(&(pg)->mdpage.pv_mtx, IPL_VM); \
22362fe2d4bSmiod (pg)->mdpage.pv_ent.pv_next = NULL; \
22462fe2d4bSmiod (pg)->mdpage.pv_ent.pv_pmap = NULL; \
22562fe2d4bSmiod (pg)->mdpage.pv_ent.pv_va = 0; \
22662fe2d4bSmiod } while (0)
22762fe2d4bSmiod
22862fe2d4bSmiod #endif /* !_LOCORE */
22962fe2d4bSmiod
2302fa72412Spirofti #endif /* !_MIPS64_PMAP_H_ */
231