xref: /netbsd-src/sys/arch/amd64/include/pte.h (revision b2809c5e007e9832fa5c6bd6975b29b0152fb05d)
1*b2809c5eSriastradh /*	$NetBSD: pte.h,v 1.17 2022/08/21 09:12:43 riastradh Exp $	*/
281918bf8Sfvdl 
381918bf8Sfvdl /*
481918bf8Sfvdl  * Copyright (c) 2001 Wasabi Systems, Inc.
581918bf8Sfvdl  * All rights reserved.
681918bf8Sfvdl  *
781918bf8Sfvdl  * Written by Frank van der Linden for Wasabi Systems, Inc.
881918bf8Sfvdl  *
981918bf8Sfvdl  * Redistribution and use in source and binary forms, with or without
1081918bf8Sfvdl  * modification, are permitted provided that the following conditions
1181918bf8Sfvdl  * are met:
1281918bf8Sfvdl  * 1. Redistributions of source code must retain the above copyright
1381918bf8Sfvdl  *    notice, this list of conditions and the following disclaimer.
1481918bf8Sfvdl  * 2. Redistributions in binary form must reproduce the above copyright
1581918bf8Sfvdl  *    notice, this list of conditions and the following disclaimer in the
1681918bf8Sfvdl  *    documentation and/or other materials provided with the distribution.
1781918bf8Sfvdl  * 3. All advertising materials mentioning features or use of this software
1881918bf8Sfvdl  *    must display the following acknowledgement:
1981918bf8Sfvdl  *      This product includes software developed for the NetBSD Project by
2081918bf8Sfvdl  *      Wasabi Systems, Inc.
2181918bf8Sfvdl  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
2281918bf8Sfvdl  *    or promote products derived from this software without specific prior
2381918bf8Sfvdl  *    written permission.
2481918bf8Sfvdl  *
2581918bf8Sfvdl  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
2681918bf8Sfvdl  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2781918bf8Sfvdl  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2881918bf8Sfvdl  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
2981918bf8Sfvdl  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
3081918bf8Sfvdl  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
3181918bf8Sfvdl  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
3281918bf8Sfvdl  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
3381918bf8Sfvdl  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3481918bf8Sfvdl  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3581918bf8Sfvdl  * POSSIBILITY OF SUCH DAMAGE.
3681918bf8Sfvdl  */
3781918bf8Sfvdl 
3881918bf8Sfvdl #ifndef _AMD64_PTE_H_
3981918bf8Sfvdl #define _AMD64_PTE_H_
4081918bf8Sfvdl 
41744259c2Snjoly #ifdef __x86_64__
42744259c2Snjoly 
4381918bf8Sfvdl /*
4481918bf8Sfvdl  * amd64 MMU hardware structure:
4581918bf8Sfvdl  *
4681918bf8Sfvdl  * the (first generation) amd64 MMU is a 4-level MMU which maps 2^48 bytes
4745aabfbeSmbalmer  * of virtual memory. The pagesize we use is 4K (4096 [0x1000] bytes),
4881918bf8Sfvdl  * although 2M and 4M can be used as well. The indexes in the levels
4981918bf8Sfvdl  * are 9 bits wide (512 64bit entries per level), dividing the bits
5081918bf8Sfvdl  * 9-9-9-9-12.
5181918bf8Sfvdl  *
5281918bf8Sfvdl  * The top level table, called PML4, contains 512 64bit entries pointing
5381918bf8Sfvdl  * to 3rd level table. The 3rd level table is called the 'page directory
5481918bf8Sfvdl  * pointers directory' and has 512 entries pointing to page directories.
5581918bf8Sfvdl  * The 2nd level is the page directory, containing 512 pointers to
5681918bf8Sfvdl  * page table pages. Lastly, level 1 consists of pages containing 512
5781918bf8Sfvdl  * PTEs.
5881918bf8Sfvdl  *
5981918bf8Sfvdl  * Simply put, levels 4-1 all consist of pages containing 512
6081918bf8Sfvdl  * entries pointing to the next level. Level 0 is the actual PTEs
6181918bf8Sfvdl  * themselves.
6281918bf8Sfvdl  *
6381918bf8Sfvdl  * For a description on the other bits, which are i386 compatible,
6481918bf8Sfvdl  * see the i386 pte.h
6581918bf8Sfvdl  */
6681918bf8Sfvdl 
6781918bf8Sfvdl #if !defined(_LOCORE)
6881918bf8Sfvdl /*
695d4038a3Smaxv  * Here we define the data types for PDEs and PTEs.
7081918bf8Sfvdl  */
7128004f67Sriastradh #include <sys/stdint.h>
72a0fc30e9Scegger typedef uint64_t pd_entry_t;		/* PDE */
73a0fc30e9Scegger typedef uint64_t pt_entry_t;		/* PTE */
7481918bf8Sfvdl #endif
7581918bf8Sfvdl 
7681918bf8Sfvdl /*
77*b2809c5eSriastradh  * Mask to get rid of the sign-extended part of addresses.
78*b2809c5eSriastradh  */
79*b2809c5eSriastradh #define VA_SIGN_MASK		0xffff000000000000
80*b2809c5eSriastradh #define VA_SIGN_NEG(va)		((va) | VA_SIGN_MASK)
81*b2809c5eSriastradh /* XXXfvdl this one's not right. */
82*b2809c5eSriastradh #define VA_SIGN_POS(va)		((va) & ~VA_SIGN_MASK)
83*b2809c5eSriastradh 
84*b2809c5eSriastradh /*
855d4038a3Smaxv  * Now we define various constants for playing with virtual addresses.
8681918bf8Sfvdl  */
8781918bf8Sfvdl #define L1_SHIFT	12
8881918bf8Sfvdl #define L2_SHIFT	21
8981918bf8Sfvdl #define L3_SHIFT	30
9081918bf8Sfvdl #define L4_SHIFT	39
9142e5342aSjym #define NBPD_L1		(1UL << L1_SHIFT) /* # bytes mapped by L1 ent (4K) */
9242e5342aSjym #define NBPD_L2		(1UL << L2_SHIFT) /* # bytes mapped by L2 ent (2MB) */
9342e5342aSjym #define NBPD_L3		(1UL << L3_SHIFT) /* # bytes mapped by L3 ent (1G) */
9442e5342aSjym #define NBPD_L4		(1UL << L4_SHIFT) /* # bytes mapped by L4 ent (512G) */
9581918bf8Sfvdl 
9681918bf8Sfvdl #define L4_MASK		0x0000ff8000000000
9781918bf8Sfvdl #define L3_MASK		0x0000007fc0000000
9881918bf8Sfvdl #define L2_MASK		0x000000003fe00000
9981918bf8Sfvdl #define L1_MASK		0x00000000001ff000
10081918bf8Sfvdl 
10181918bf8Sfvdl #define L4_FRAME	L4_MASK
10281918bf8Sfvdl #define L3_FRAME	(L4_FRAME|L3_MASK)
10381918bf8Sfvdl #define L2_FRAME	(L3_FRAME|L2_MASK)
10481918bf8Sfvdl #define L1_FRAME	(L2_FRAME|L1_MASK)
10581918bf8Sfvdl 
10681918bf8Sfvdl /*
1071c8fe94aSmaxv  * x86 PTE/PDE bits.
1081c8fe94aSmaxv  */
1091c8fe94aSmaxv #define PTE_P		0x0000000000000001	/* Present */
1101c8fe94aSmaxv #define PTE_W		0x0000000000000002	/* Write */
1111c8fe94aSmaxv #define PTE_U		0x0000000000000004	/* User */
1121c8fe94aSmaxv #define PTE_PWT		0x0000000000000008	/* Write-Through */
1131c8fe94aSmaxv #define PTE_PCD		0x0000000000000010	/* Cache-Disable */
1141c8fe94aSmaxv #define PTE_A		0x0000000000000020	/* Accessed */
1151c8fe94aSmaxv #define PTE_D		0x0000000000000040	/* Dirty */
1161c8fe94aSmaxv #define PTE_PAT		0x0000000000000080	/* PAT on 4KB Pages */
1171c8fe94aSmaxv #define PTE_PS		0x0000000000000080	/* Large Page Size */
1181c8fe94aSmaxv #define PTE_G		0x0000000000000100	/* Global Translation */
1191c8fe94aSmaxv #define PTE_AVL1	0x0000000000000200	/* Ignored by Hardware */
1201c8fe94aSmaxv #define PTE_AVL2	0x0000000000000400	/* Ignored by Hardware */
1211c8fe94aSmaxv #define PTE_AVL3	0x0000000000000800	/* Ignored by Hardware */
1221c8fe94aSmaxv #define PTE_LGPAT	0x0000000000001000	/* PAT on Large Pages */
1231c8fe94aSmaxv #define PTE_NX		0x8000000000000000	/* No Execute */
1241c8fe94aSmaxv 
1251c8fe94aSmaxv #define PTE_4KFRAME	0x000ffffffffff000
1261c8fe94aSmaxv #define PTE_2MFRAME	0x000fffffffe00000
1271c8fe94aSmaxv #define PTE_1GFRAME	0x000fffffc0000000
1281c8fe94aSmaxv 
1291c8fe94aSmaxv #define PTE_FRAME	PTE_4KFRAME
1301c8fe94aSmaxv #define PTE_LGFRAME	PTE_2MFRAME
1311c8fe94aSmaxv 
132cbdb6aeeSriastradh #define	_MACHINE_PTE_H_X86
133a63798eaScegger #include <x86/pte.h>
134cbdb6aeeSriastradh #undef	_MACHINE_PTE_H_X86
13581918bf8Sfvdl 
136744259c2Snjoly #else   /*      !__x86_64__      */
137744259c2Snjoly 
138744259c2Snjoly #include <i386/pte.h>
139744259c2Snjoly 
140744259c2Snjoly #endif  /*      !__x86_64__      */
141744259c2Snjoly 
14281918bf8Sfvdl #endif /* _AMD64_PTE_H_ */
143