xref: /openbsd-src/sys/arch/amd64/include/pte.h (revision 4fb1029394b576c51b109300b01be3fdf22069ff)
1*4fb10293Sbluhm /*	$OpenBSD: pte.h,v 1.18 2024/07/09 19:11:06 bluhm Exp $	*/
2f5df1827Smickey /*	$NetBSD: pte.h,v 1.1 2003/04/26 18:39:47 fvdl Exp $	*/
3f5df1827Smickey 
4f5df1827Smickey /*
5f5df1827Smickey  * Copyright (c) 2001 Wasabi Systems, Inc.
6f5df1827Smickey  * All rights reserved.
7f5df1827Smickey  *
8f5df1827Smickey  * Written by Frank van der Linden for Wasabi Systems, Inc.
9f5df1827Smickey  *
10f5df1827Smickey  * Redistribution and use in source and binary forms, with or without
11f5df1827Smickey  * modification, are permitted provided that the following conditions
12f5df1827Smickey  * are met:
13f5df1827Smickey  * 1. Redistributions of source code must retain the above copyright
14f5df1827Smickey  *    notice, this list of conditions and the following disclaimer.
15f5df1827Smickey  * 2. Redistributions in binary form must reproduce the above copyright
16f5df1827Smickey  *    notice, this list of conditions and the following disclaimer in the
17f5df1827Smickey  *    documentation and/or other materials provided with the distribution.
18f5df1827Smickey  * 3. All advertising materials mentioning features or use of this software
19f5df1827Smickey  *    must display the following acknowledgement:
20f5df1827Smickey  *      This product includes software developed for the NetBSD Project by
21f5df1827Smickey  *      Wasabi Systems, Inc.
22f5df1827Smickey  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
23f5df1827Smickey  *    or promote products derived from this software without specific prior
24f5df1827Smickey  *    written permission.
25f5df1827Smickey  *
26f5df1827Smickey  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
27f5df1827Smickey  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28f5df1827Smickey  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29f5df1827Smickey  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
30f5df1827Smickey  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31f5df1827Smickey  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32f5df1827Smickey  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33f5df1827Smickey  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34f5df1827Smickey  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35f5df1827Smickey  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36f5df1827Smickey  * POSSIBILITY OF SUCH DAMAGE.
37f5df1827Smickey  */
38f5df1827Smickey 
392fa72412Spirofti #ifndef _MACHINE_PTE_H_
402fa72412Spirofti #define _MACHINE_PTE_H_
41f5df1827Smickey 
42f5df1827Smickey /*
43f5df1827Smickey  * amd64 MMU hardware structure:
44f5df1827Smickey  *
45f5df1827Smickey  * the (first generation) amd64 MMU is a 4-level MMU which maps 2^48 bytes
463ac73be5Stom  * of  virtual memory. The  pagesize we use is 4K (4096 [0x1000] bytes),
47f5df1827Smickey  * although 2M and 4M can be used as well. The indexes in the levels
48f5df1827Smickey  * are 9 bits wide (512 64bit entries per level), dividing the bits
49f5df1827Smickey  * 9-9-9-9-12.
50f5df1827Smickey  *
51f5df1827Smickey  * The top level table, called PML4, contains 512 64bit entries pointing
52f5df1827Smickey  * to 3rd level table. The 3rd level table is called the 'page directory
53f5df1827Smickey  * pointers directory' and has 512 entries pointing to page directories.
54f5df1827Smickey  * The 2nd level is the page directory, containing 512 pointers to
55f5df1827Smickey  * page table pages. Lastly, level 1 consists of pages containing 512
56f5df1827Smickey  * PTEs.
57f5df1827Smickey  *
58f5df1827Smickey  * Simply put, levels 4-1 all consist of pages containing 512
59f5df1827Smickey  * entries pointing to the next level. Level 0 is the actual PTEs
60f5df1827Smickey  * themselves.
61f5df1827Smickey  *
62f5df1827Smickey  * For a description on the other bits, which are i386 compatible,
63f5df1827Smickey  * see the i386 pte.h
64f5df1827Smickey  */
65f5df1827Smickey 
66f5df1827Smickey #if !defined(_LOCORE)
67f5df1827Smickey 
68f5df1827Smickey /*
69f5df1827Smickey  * here we define the data types for PDEs and PTEs
70f5df1827Smickey  */
71f5df1827Smickey 
72f5df1827Smickey typedef u_int64_t pd_entry_t;		/* PDE */
73f5df1827Smickey typedef u_int64_t pt_entry_t;		/* PTE */
74f5df1827Smickey 
75f5df1827Smickey #endif
76f5df1827Smickey 
77f5df1827Smickey /*
78f5df1827Smickey  * now we define various for playing with virtual addresses
79f5df1827Smickey  */
80f5df1827Smickey 
81f5df1827Smickey #define L1_SHIFT	12
82f5df1827Smickey #define	L2_SHIFT	21
83f5df1827Smickey #define	L3_SHIFT	30
84f5df1827Smickey #define	L4_SHIFT	39
85f5df1827Smickey #define	NBPD_L1		(1ULL << L1_SHIFT) /* # bytes mapped by L1 ent (4K) */
86f5df1827Smickey #define	NBPD_L2		(1ULL << L2_SHIFT) /* # bytes mapped by L2 ent (2MB) */
87f5df1827Smickey #define	NBPD_L3		(1ULL << L3_SHIFT) /* # bytes mapped by L3 ent (1G) */
88f5df1827Smickey #define	NBPD_L4		(1ULL << L4_SHIFT) /* # bytes mapped by L4 ent (512G) */
89f5df1827Smickey 
909fe10678Smiod #define L4_MASK		0x0000ff8000000000UL
919fe10678Smiod #define L3_MASK		0x0000007fc0000000UL
929fe10678Smiod #define L2_MASK		0x000000003fe00000UL
939fe10678Smiod #define L1_MASK		0x00000000001ff000UL
94f5df1827Smickey 
95f5df1827Smickey #define L4_FRAME	L4_MASK
96f5df1827Smickey #define L3_FRAME	(L4_FRAME|L3_MASK)
97f5df1827Smickey #define L2_FRAME	(L3_FRAME|L2_MASK)
98f5df1827Smickey #define L1_FRAME	(L2_FRAME|L1_MASK)
99f5df1827Smickey 
10022894a92Smlarkin #define PAGE_MASK_L2 	(NBPD_L2 - 1)
10122894a92Smlarkin 
102184e2458Sderaadt #define	x86_round_pdr(x) \
103184e2458Sderaadt 	((((unsigned long)(x)) + (NBPD_L2 - 1)) & ~(NBPD_L2 - 1))
104184e2458Sderaadt 
105f5df1827Smickey /*
106f5df1827Smickey  * PDE/PTE bits. These are no different from their i386 counterparts.
107f5df1827Smickey  */
108f5df1827Smickey 
1099fe10678Smiod #define	PG_V		0x0000000000000001UL	/* valid */
1109fe10678Smiod #define	PG_RO		0x0000000000000000UL	/* read-only */
1119fe10678Smiod #define	PG_RW		0x0000000000000002UL	/* read-write */
1129fe10678Smiod #define	PG_u		0x0000000000000004UL	/* user accessible */
1139fe10678Smiod #define	PG_PROT		0x0000000000000006UL
114453f8135Soga #define	PG_WT		0x0000000000000008UL	/* write through */
115453f8135Soga #define	PG_N		0x0000000000000010UL	/* non-cacheable */
1169fe10678Smiod #define	PG_U		0x0000000000000020UL	/* used */
1179fe10678Smiod #define	PG_M		0x0000000000000040UL	/* modified */
11807dfb2ccSoga #define	PG_PAT		0x0000000000000080UL	/* PAT bit. (on pte) */
11907dfb2ccSoga #define	PG_PS		0x0000000000000080UL	/* 2MB page size (on pde) */
1209fe10678Smiod #define	PG_G		0x0000000000000100UL	/* not flushed */
1219fe10678Smiod #define	PG_AVAIL1	0x0000000000000200UL
1229fe10678Smiod #define	PG_AVAIL2	0x0000000000000400UL
1239fe10678Smiod #define	PG_AVAIL3	0x0000000000000800UL
12407dfb2ccSoga #define	PG_PATLG	0x0000000000001000UL	/* PAT on large pages */
125cf8ee16dSderaadt #define	PG_PKMASK	0x7800000000000000UL	/* Protection Key Mask */
126cf8ee16dSderaadt #define	PG_XO		0x0800000000000000UL	/* key1 used for execute-only */
1279fe10678Smiod #define	PG_NX		0x8000000000000000UL	/* non-executable */
1281fef3300Smickey #define	PG_FRAME	0x000ffffffffff000UL
129f5df1827Smickey 
130da7f9b33Sguenther #define	PG_LGFRAME	0x000fffffffe00000UL	/* large (2M) page frame mask */
131f5df1827Smickey 
132cf8ee16dSderaadt #define PGK_VALUE	0xfffffffc		/* key0 is normal */
133cf8ee16dSderaadt 
134a8dd7b61Smlarkin /* EPT PTE bits */
135a8dd7b61Smlarkin #define EPT_R		(1ULL << 0)
136a8dd7b61Smlarkin #define EPT_W		(1ULL << 1)
137a8dd7b61Smlarkin #define EPT_X		(1ULL << 2)
138a8dd7b61Smlarkin #define EPT_WB		(6ULL << 3)
139a8dd7b61Smlarkin #define EPT_PS		(1ULL << 7)
140a8dd7b61Smlarkin 
14107dfb2ccSoga /* Cacheability bits when we are using PAT */
14207dfb2ccSoga #define	PG_WB		(0)		/* The default */
14307dfb2ccSoga #define	PG_WC		(PG_WT)		/* WT and CD is WC */
14407dfb2ccSoga #define	PG_UCMINUS	(PG_N)		/* UC but mtrr can override */
14507dfb2ccSoga #define	PG_UC		(PG_WT | PG_N)	/* hard UC */
14607dfb2ccSoga 
147f5df1827Smickey /*
148f5df1827Smickey  * short forms of protection codes
149f5df1827Smickey  */
150f5df1827Smickey 
1519fe10678Smiod #define	PG_KR		0x0000000000000000UL	/* kernel read-only */
1529fe10678Smiod #define	PG_KW		0x0000000000000002UL	/* kernel read-write */
153f5df1827Smickey 
154f5df1827Smickey /*
155f5df1827Smickey  * page protection exception bits
156f5df1827Smickey  */
157f5df1827Smickey 
158f5df1827Smickey #define PGEX_P		0x01	/* protection violation (vs. no mapping) */
159f5df1827Smickey #define PGEX_W		0x02	/* exception during a write cycle */
160f5df1827Smickey #define PGEX_U		0x04	/* exception while in user mode (upl) */
1617adef30aSderaadt #define PGEX_I		0x10	/* instruction fetch blocked by NX */
16243fcf7cdSjsg #define PGEX_PK		0x20	/* protection-key violation */
163f5df1827Smickey 
16436414dbbSmlarkin #ifdef _KERNEL
165e9e0c464Sderaadt extern pt_entry_t pg_xo;	/* XO pte bits using PKU key1 */
16636414dbbSmlarkin extern pt_entry_t pg_nx;	/* NX pte bit */
167*4fb10293Sbluhm extern pt_entry_t pg_crypt;	/* C pte bit */
168ca88a4fbSmlarkin extern pt_entry_t pg_g_kern;	/* PG_G if glbl mappings can be used in kern */
16936414dbbSmlarkin #endif /* _KERNEL */
17036414dbbSmlarkin 
1712fa72412Spirofti #endif /* _MACHINE_PTE_H_ */
172