Home | History | Annotate | Line # | Download | only in include
pmap_private.h revision 1.1
      1  1.1  riastrad /*	$NetBSD: pmap_private.h,v 1.1 2022/08/20 23:48:50 riastradh Exp $	*/
      2  1.1  riastrad 
      3  1.1  riastrad /*
      4  1.1  riastrad  * Copyright (c) 1997 Charles D. Cranor and Washington University.
      5  1.1  riastrad  * All rights reserved.
      6  1.1  riastrad  *
      7  1.1  riastrad  * Redistribution and use in source and binary forms, with or without
      8  1.1  riastrad  * modification, are permitted provided that the following conditions
      9  1.1  riastrad  * are met:
     10  1.1  riastrad  * 1. Redistributions of source code must retain the above copyright
     11  1.1  riastrad  *    notice, this list of conditions and the following disclaimer.
     12  1.1  riastrad  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  riastrad  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  riastrad  *    documentation and/or other materials provided with the distribution.
     15  1.1  riastrad  *
     16  1.1  riastrad  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  1.1  riastrad  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  1.1  riastrad  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  1.1  riastrad  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  1.1  riastrad  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21  1.1  riastrad  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22  1.1  riastrad  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23  1.1  riastrad  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24  1.1  riastrad  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25  1.1  riastrad  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  1.1  riastrad  */
     27  1.1  riastrad 
     28  1.1  riastrad /*
     29  1.1  riastrad  * Copyright (c) 2001 Wasabi Systems, Inc.
     30  1.1  riastrad  * All rights reserved.
     31  1.1  riastrad  *
     32  1.1  riastrad  * Written by Frank van der Linden for Wasabi Systems, Inc.
     33  1.1  riastrad  *
     34  1.1  riastrad  * Redistribution and use in source and binary forms, with or without
     35  1.1  riastrad  * modification, are permitted provided that the following conditions
     36  1.1  riastrad  * are met:
     37  1.1  riastrad  * 1. Redistributions of source code must retain the above copyright
     38  1.1  riastrad  *    notice, this list of conditions and the following disclaimer.
     39  1.1  riastrad  * 2. Redistributions in binary form must reproduce the above copyright
     40  1.1  riastrad  *    notice, this list of conditions and the following disclaimer in the
     41  1.1  riastrad  *    documentation and/or other materials provided with the distribution.
     42  1.1  riastrad  * 3. All advertising materials mentioning features or use of this software
     43  1.1  riastrad  *    must display the following acknowledgement:
     44  1.1  riastrad  *      This product includes software developed for the NetBSD Project by
     45  1.1  riastrad  *      Wasabi Systems, Inc.
     46  1.1  riastrad  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     47  1.1  riastrad  *    or promote products derived from this software without specific prior
     48  1.1  riastrad  *    written permission.
     49  1.1  riastrad  *
     50  1.1  riastrad  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     51  1.1  riastrad  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     52  1.1  riastrad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     53  1.1  riastrad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     54  1.1  riastrad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     55  1.1  riastrad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     56  1.1  riastrad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     57  1.1  riastrad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     58  1.1  riastrad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     59  1.1  riastrad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     60  1.1  riastrad  * POSSIBILITY OF SUCH DAMAGE.
     61  1.1  riastrad  */
     62  1.1  riastrad 
     63  1.1  riastrad #ifndef	_X86_PMAP_PRIVATE_H_
     64  1.1  riastrad #define	_X86_PMAP_PRIVATE_H_
     65  1.1  riastrad 
     66  1.1  riastrad #ifndef	_MACHINE_PMAP_PRIVATE_H_X86
     67  1.1  riastrad #error Include machine/pmap_private.h, not x86/pmap_private.h.
     68  1.1  riastrad #endif
     69  1.1  riastrad 
     70  1.1  riastrad #ifdef _KERNEL_OPT
     71  1.1  riastrad #include "opt_svs.h"
     72  1.1  riastrad #endif
     73  1.1  riastrad 
     74  1.1  riastrad #include <sys/param.h>
     75  1.1  riastrad #include <sys/types.h>
     76  1.1  riastrad 
     77  1.1  riastrad #include <sys/mutex.h>
     78  1.1  riastrad #include <sys/pool.h>
     79  1.1  riastrad #include <sys/queue.h>
     80  1.1  riastrad 
     81  1.1  riastrad #include <machine/pte.h>
     82  1.1  riastrad #include <machine/vmparam.h>
     83  1.1  riastrad 
     84  1.1  riastrad #include <uvm/uvm_pmap.h>
     85  1.1  riastrad 
     86  1.1  riastrad struct pmap;
     87  1.1  riastrad 
     88  1.1  riastrad #define SLAREA_USER	0
     89  1.1  riastrad #define SLAREA_PTE	1
     90  1.1  riastrad #define SLAREA_MAIN	2
     91  1.1  riastrad #define SLAREA_PCPU	3
     92  1.1  riastrad #define SLAREA_DMAP	4
     93  1.1  riastrad #define SLAREA_HYPV	5
     94  1.1  riastrad #define SLAREA_ASAN	6
     95  1.1  riastrad #define SLAREA_MSAN	7
     96  1.1  riastrad #define SLAREA_KERN	8
     97  1.1  riastrad #define SLSPACE_NAREAS	9
     98  1.1  riastrad 
     99  1.1  riastrad struct slotspace {
    100  1.1  riastrad 	struct {
    101  1.1  riastrad 		size_t sslot; /* start slot */
    102  1.1  riastrad 		size_t nslot; /* # of slots */
    103  1.1  riastrad 		bool active;  /* area is active */
    104  1.1  riastrad 	} area[SLSPACE_NAREAS];
    105  1.1  riastrad };
    106  1.1  riastrad 
    107  1.1  riastrad extern struct slotspace slotspace;
    108  1.1  riastrad 
    109  1.1  riastrad #include <x86/gdt.h>
    110  1.1  riastrad 
    111  1.1  riastrad struct pcpu_entry {
    112  1.1  riastrad 	uint8_t gdt[MAXGDTSIZ];
    113  1.1  riastrad 	uint8_t ldt[MAX_USERLDT_SIZE];
    114  1.1  riastrad 	uint8_t idt[PAGE_SIZE];
    115  1.1  riastrad 	uint8_t tss[PAGE_SIZE];
    116  1.1  riastrad 	uint8_t ist0[PAGE_SIZE];
    117  1.1  riastrad 	uint8_t ist1[PAGE_SIZE];
    118  1.1  riastrad 	uint8_t ist2[PAGE_SIZE];
    119  1.1  riastrad 	uint8_t ist3[PAGE_SIZE];
    120  1.1  riastrad 	uint8_t rsp0[2 * PAGE_SIZE];
    121  1.1  riastrad } __packed;
    122  1.1  riastrad 
    123  1.1  riastrad struct pcpu_area {
    124  1.1  riastrad #ifdef SVS
    125  1.1  riastrad 	uint8_t utls[PAGE_SIZE];
    126  1.1  riastrad #endif
    127  1.1  riastrad 	uint8_t ldt[PAGE_SIZE];
    128  1.1  riastrad 	struct pcpu_entry ent[MAXCPUS];
    129  1.1  riastrad } __packed;
    130  1.1  riastrad 
    131  1.1  riastrad extern struct pcpu_area *pcpuarea;
    132  1.1  riastrad 
    133  1.1  riastrad #define PMAP_PCID_KERN	0
    134  1.1  riastrad #define PMAP_PCID_USER	1
    135  1.1  riastrad 
    136  1.1  riastrad /*
    137  1.1  riastrad  * pmap data structures: see pmap.c for details of locking.
    138  1.1  riastrad  */
    139  1.1  riastrad 
    140  1.1  riastrad /*
    141  1.1  riastrad  * we maintain a list of all non-kernel pmaps
    142  1.1  riastrad  */
    143  1.1  riastrad 
    144  1.1  riastrad LIST_HEAD(pmap_head, pmap); /* struct pmap_head: head of a pmap list */
    145  1.1  riastrad 
    146  1.1  riastrad /*
    147  1.1  riastrad  * linked list of all non-kernel pmaps
    148  1.1  riastrad  */
    149  1.1  riastrad extern struct pmap_head pmaps;
    150  1.1  riastrad extern kmutex_t pmaps_lock;    /* protects pmaps */
    151  1.1  riastrad 
    152  1.1  riastrad /*
    153  1.1  riastrad  * pool_cache(9) that pmaps are allocated from
    154  1.1  riastrad  */
    155  1.1  riastrad extern struct pool_cache pmap_cache;
    156  1.1  riastrad 
    157  1.1  riastrad /* macro to access pm_pdirpa slots */
    158  1.1  riastrad #ifdef PAE
    159  1.1  riastrad #define pmap_pdirpa(pmap, index) \
    160  1.1  riastrad 	((pmap)->pm_pdirpa[l2tol3(index)] + l2tol2(index) * sizeof(pd_entry_t))
    161  1.1  riastrad #else
    162  1.1  riastrad #define pmap_pdirpa(pmap, index) \
    163  1.1  riastrad 	((pmap)->pm_pdirpa[0] + (index) * sizeof(pd_entry_t))
    164  1.1  riastrad #endif
    165  1.1  riastrad 
    166  1.1  riastrad /*
    167  1.1  riastrad  * global kernel variables
    168  1.1  riastrad  */
    169  1.1  riastrad 
    170  1.1  riastrad /*
    171  1.1  riastrad  * PDPpaddr is the physical address of the kernel's PDP.
    172  1.1  riastrad  * - i386 non-PAE and amd64: PDPpaddr corresponds directly to the %cr3
    173  1.1  riastrad  * value associated to the kernel process, proc0.
    174  1.1  riastrad  * - i386 PAE: it still represents the PA of the kernel's PDP (L2). Due to
    175  1.1  riastrad  * the L3 PD, it cannot be considered as the equivalent of a %cr3 any more.
    176  1.1  riastrad  * - Xen: it corresponds to the PFN of the kernel's PDP.
    177  1.1  riastrad  */
    178  1.1  riastrad extern u_long PDPpaddr;
    179  1.1  riastrad 
    180  1.1  riastrad extern pd_entry_t pmap_pg_g;			/* do we support PTE_G? */
    181  1.1  riastrad extern pd_entry_t pmap_pg_nx;			/* do we support PTE_NX? */
    182  1.1  riastrad extern int pmap_largepages;
    183  1.1  riastrad extern long nkptp[PTP_LEVELS];
    184  1.1  riastrad 
    185  1.1  riastrad #define pmap_valid_entry(E) 		((E) & PTE_P) /* is PDE or PTE valid? */
    186  1.1  riastrad 
    187  1.1  riastrad void		pmap_map_ptes(struct pmap *, struct pmap **, pd_entry_t **,
    188  1.1  riastrad 		    pd_entry_t * const **);
    189  1.1  riastrad void		pmap_unmap_ptes(struct pmap *, struct pmap *);
    190  1.1  riastrad 
    191  1.1  riastrad bool		pmap_pdes_valid(vaddr_t, pd_entry_t * const *, pd_entry_t *,
    192  1.1  riastrad 		    int *lastlvl);
    193  1.1  riastrad 
    194  1.1  riastrad bool		pmap_is_curpmap(struct pmap *);
    195  1.1  riastrad 
    196  1.1  riastrad void		pmap_ept_transform(struct pmap *);
    197  1.1  riastrad 
    198  1.1  riastrad #ifndef __HAVE_DIRECT_MAP
    199  1.1  riastrad void		pmap_vpage_cpu_init(struct cpu_info *);
    200  1.1  riastrad #endif
    201  1.1  riastrad vaddr_t		slotspace_rand(int, size_t, size_t, size_t, vaddr_t);
    202  1.1  riastrad 
    203  1.1  riastrad vaddr_t reserve_dumppages(vaddr_t); /* XXX: not a pmap fn */
    204  1.1  riastrad 
    205  1.1  riastrad typedef enum tlbwhy {
    206  1.1  riastrad 	TLBSHOOT_REMOVE_ALL,
    207  1.1  riastrad 	TLBSHOOT_KENTER,
    208  1.1  riastrad 	TLBSHOOT_KREMOVE,
    209  1.1  riastrad 	TLBSHOOT_FREE_PTP,
    210  1.1  riastrad 	TLBSHOOT_REMOVE_PTE,
    211  1.1  riastrad 	TLBSHOOT_SYNC_PV,
    212  1.1  riastrad 	TLBSHOOT_WRITE_PROTECT,
    213  1.1  riastrad 	TLBSHOOT_ENTER,
    214  1.1  riastrad 	TLBSHOOT_NVMM,
    215  1.1  riastrad 	TLBSHOOT_BUS_DMA,
    216  1.1  riastrad 	TLBSHOOT_BUS_SPACE,
    217  1.1  riastrad 	TLBSHOOT__MAX,
    218  1.1  riastrad } tlbwhy_t;
    219  1.1  riastrad 
    220  1.1  riastrad void		pmap_tlb_init(void);
    221  1.1  riastrad void		pmap_tlb_cpu_init(struct cpu_info *);
    222  1.1  riastrad void		pmap_tlb_shootdown(pmap_t, vaddr_t, pt_entry_t, tlbwhy_t);
    223  1.1  riastrad void		pmap_tlb_shootnow(void);
    224  1.1  riastrad void		pmap_tlb_intr(void);
    225  1.1  riastrad 
    226  1.1  riastrad /*
    227  1.1  riastrad  * inline functions
    228  1.1  riastrad  */
    229  1.1  riastrad 
    230  1.1  riastrad /*
    231  1.1  riastrad  * pmap_update_pg: flush one page from the TLB (or flush the whole thing
    232  1.1  riastrad  *	if hardware doesn't support one-page flushing)
    233  1.1  riastrad  */
    234  1.1  riastrad 
    235  1.1  riastrad __inline static void __unused
    236  1.1  riastrad pmap_update_pg(vaddr_t va)
    237  1.1  riastrad {
    238  1.1  riastrad 	invlpg(va);
    239  1.1  riastrad }
    240  1.1  riastrad 
    241  1.1  riastrad /*
    242  1.1  riastrad  * various address inlines
    243  1.1  riastrad  *
    244  1.1  riastrad  *  vtopte: return a pointer to the PTE mapping a VA, works only for
    245  1.1  riastrad  *  user and PT addresses
    246  1.1  riastrad  *
    247  1.1  riastrad  *  kvtopte: return a pointer to the PTE mapping a kernel VA
    248  1.1  riastrad  */
    249  1.1  riastrad 
    250  1.1  riastrad #include <lib/libkern/libkern.h>
    251  1.1  riastrad 
    252  1.1  riastrad static __inline pt_entry_t * __unused
    253  1.1  riastrad vtopte(vaddr_t va)
    254  1.1  riastrad {
    255  1.1  riastrad 
    256  1.1  riastrad 	KASSERT(va < VM_MIN_KERNEL_ADDRESS);
    257  1.1  riastrad 
    258  1.1  riastrad 	return (PTE_BASE + pl1_i(va));
    259  1.1  riastrad }
    260  1.1  riastrad 
    261  1.1  riastrad static __inline pt_entry_t * __unused
    262  1.1  riastrad kvtopte(vaddr_t va)
    263  1.1  riastrad {
    264  1.1  riastrad 	pd_entry_t *pde;
    265  1.1  riastrad 
    266  1.1  riastrad 	KASSERT(va >= VM_MIN_KERNEL_ADDRESS);
    267  1.1  riastrad 
    268  1.1  riastrad 	pde = L2_BASE + pl2_i(va);
    269  1.1  riastrad 	if (*pde & PTE_PS)
    270  1.1  riastrad 		return ((pt_entry_t *)pde);
    271  1.1  riastrad 
    272  1.1  riastrad 	return (PTE_BASE + pl1_i(va));
    273  1.1  riastrad }
    274  1.1  riastrad 
    275  1.1  riastrad #ifdef XENPV
    276  1.1  riastrad #include <sys/bitops.h>
    277  1.1  riastrad 
    278  1.1  riastrad #define XPTE_MASK	L1_FRAME
    279  1.1  riastrad /* Selects the index of a PTE in (A)PTE_BASE */
    280  1.1  riastrad #define XPTE_SHIFT	(L1_SHIFT - ilog2(sizeof(pt_entry_t)))
    281  1.1  riastrad 
    282  1.1  riastrad /* PTE access inline functions */
    283  1.1  riastrad 
    284  1.1  riastrad /*
    285  1.1  riastrad  * Get the machine address of the pointed pte
    286  1.1  riastrad  * We use hardware MMU to get value so works only for levels 1-3
    287  1.1  riastrad  */
    288  1.1  riastrad 
    289  1.1  riastrad static __inline paddr_t
    290  1.1  riastrad xpmap_ptetomach(pt_entry_t *pte)
    291  1.1  riastrad {
    292  1.1  riastrad 	pt_entry_t *up_pte;
    293  1.1  riastrad 	vaddr_t va = (vaddr_t) pte;
    294  1.1  riastrad 
    295  1.1  riastrad 	va = ((va & XPTE_MASK) >> XPTE_SHIFT) | (vaddr_t) PTE_BASE;
    296  1.1  riastrad 	up_pte = (pt_entry_t *) va;
    297  1.1  riastrad 
    298  1.1  riastrad 	return (paddr_t) (((*up_pte) & PTE_FRAME) + (((vaddr_t) pte) & (~PTE_FRAME & ~VA_SIGN_MASK)));
    299  1.1  riastrad }
    300  1.1  riastrad 
    301  1.1  riastrad /* Xen helpers to change bits of a pte */
    302  1.1  riastrad #define XPMAP_UPDATE_DIRECT	1	/* Update direct map entry flags too */
    303  1.1  riastrad 
    304  1.1  riastrad paddr_t	vtomach(vaddr_t);
    305  1.1  riastrad #define vtomfn(va) (vtomach(va) >> PAGE_SHIFT)
    306  1.1  riastrad #endif	/* XENPV */
    307  1.1  riastrad 
    308  1.1  riastrad #ifdef __HAVE_PCPU_AREA
    309  1.1  riastrad extern struct pcpu_area *pcpuarea;
    310  1.1  riastrad #define PDIR_SLOT_PCPU		510
    311  1.1  riastrad #define PMAP_PCPU_BASE		(VA_SIGN_NEG((PDIR_SLOT_PCPU * NBPD_L4)))
    312  1.1  riastrad #endif
    313  1.1  riastrad 
    314  1.1  riastrad void	svs_quad_copy(void *, void *, long);
    315  1.1  riastrad 
    316  1.1  riastrad #endif	/* _X86_PMAP_PRIVATE_H_ */
    317