pmap.h revision 1.14
11.14Syamt/*	$NetBSD: pmap.h,v 1.14 2007/10/18 15:28:34 yamt Exp $	*/
21.1Sfvdl
31.1Sfvdl/*
41.1Sfvdl *
51.1Sfvdl * Copyright (c) 1997 Charles D. Cranor and Washington University.
61.1Sfvdl * All rights reserved.
71.1Sfvdl *
81.1Sfvdl * Redistribution and use in source and binary forms, with or without
91.1Sfvdl * modification, are permitted provided that the following conditions
101.1Sfvdl * are met:
111.1Sfvdl * 1. Redistributions of source code must retain the above copyright
121.1Sfvdl *    notice, this list of conditions and the following disclaimer.
131.1Sfvdl * 2. Redistributions in binary form must reproduce the above copyright
141.1Sfvdl *    notice, this list of conditions and the following disclaimer in the
151.1Sfvdl *    documentation and/or other materials provided with the distribution.
161.1Sfvdl * 3. All advertising materials mentioning features or use of this software
171.1Sfvdl *    must display the following acknowledgment:
181.1Sfvdl *      This product includes software developed by Charles D. Cranor and
191.1Sfvdl *      Washington University.
201.1Sfvdl * 4. The name of the author may not be used to endorse or promote products
211.1Sfvdl *    derived from this software without specific prior written permission.
221.1Sfvdl *
231.1Sfvdl * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
241.1Sfvdl * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
251.1Sfvdl * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
261.1Sfvdl * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
271.1Sfvdl * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
281.1Sfvdl * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
291.1Sfvdl * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
301.1Sfvdl * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
311.1Sfvdl * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
321.1Sfvdl * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
331.1Sfvdl */
341.1Sfvdl
351.1Sfvdl/*
361.1Sfvdl * Copyright (c) 2001 Wasabi Systems, Inc.
371.1Sfvdl * All rights reserved.
381.1Sfvdl *
391.1Sfvdl * Written by Frank van der Linden for Wasabi Systems, Inc.
401.1Sfvdl *
411.1Sfvdl * Redistribution and use in source and binary forms, with or without
421.1Sfvdl * modification, are permitted provided that the following conditions
431.1Sfvdl * are met:
441.1Sfvdl * 1. Redistributions of source code must retain the above copyright
451.1Sfvdl *    notice, this list of conditions and the following disclaimer.
461.1Sfvdl * 2. Redistributions in binary form must reproduce the above copyright
471.1Sfvdl *    notice, this list of conditions and the following disclaimer in the
481.1Sfvdl *    documentation and/or other materials provided with the distribution.
491.1Sfvdl * 3. All advertising materials mentioning features or use of this software
501.1Sfvdl *    must display the following acknowledgement:
511.1Sfvdl *      This product includes software developed for the NetBSD Project by
521.1Sfvdl *      Wasabi Systems, Inc.
531.1Sfvdl * 4. The name of Wasabi Systems, Inc. may not be used to endorse
541.1Sfvdl *    or promote products derived from this software without specific prior
551.1Sfvdl *    written permission.
561.1Sfvdl *
571.1Sfvdl * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
581.1Sfvdl * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
591.1Sfvdl * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
601.1Sfvdl * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
611.1Sfvdl * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
621.1Sfvdl * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
631.1Sfvdl * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
641.1Sfvdl * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
651.1Sfvdl * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
661.1Sfvdl * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
671.1Sfvdl * POSSIBILITY OF SUCH DAMAGE.
681.1Sfvdl */
691.1Sfvdl
701.1Sfvdl#ifndef	_AMD64_PMAP_H_
711.1Sfvdl#define	_AMD64_PMAP_H_
721.1Sfvdl
731.1Sfvdl#include <machine/pte.h>
741.1Sfvdl#include <machine/segments.h>
751.11Sad#include <machine/atomic.h>
761.12Sad#ifdef _KERNEL
771.12Sad#include <machine/cpufunc.h>
781.12Sad#endif
791.11Sad
801.1Sfvdl#include <uvm/uvm_object.h>
811.1Sfvdl
821.1Sfvdl/*
831.1Sfvdl * The x86_64 pmap module closely resembles the i386 one. It uses
841.1Sfvdl * the same recursive entry scheme, and the same alternate area
851.1Sfvdl * trick for accessing non-current pmaps. See the i386 pmap.h
861.1Sfvdl * for a description. The obvious difference is that 3 extra
871.1Sfvdl * levels of page table need to be dealt with. The level 1 page
881.1Sfvdl * table pages are at:
891.1Sfvdl *
901.1Sfvdl * l1: 0x00007f8000000000 - 0x00007fffffffffff     (39 bits, needs PML4 entry)
911.1Sfvdl *
921.1Sfvdl * The alternate space is at:
931.1Sfvdl *
941.1Sfvdl * l1: 0xffffff8000000000 - 0xffffffffffffffff     (39 bits, needs PML4 entry)
951.1Sfvdl *
961.1Sfvdl * The rest is kept as physical pages in 3 UVM objects, and is
971.1Sfvdl * temporarily mapped for virtual access when needed.
981.1Sfvdl *
991.1Sfvdl * Note that address space is signed, so the layout for 48 bits is:
1001.1Sfvdl *
1011.1Sfvdl *  +---------------------------------+ 0xffffffffffffffff
1021.1Sfvdl *  |                                 |
1031.1Sfvdl *  |    alt.L1 table (PTE pages)     |
1041.1Sfvdl *  |                                 |
1051.1Sfvdl *  +---------------------------------+ 0xffffff8000000000
1061.1Sfvdl *  ~                                 ~
1071.1Sfvdl *  |                                 |
1081.1Sfvdl *  |         Kernel Space            |
1091.1Sfvdl *  |                                 |
1101.1Sfvdl *  |                                 |
1111.4Syamt *  +---------------------------------+ 0xffff800000000000 = 0x0000800000000000
1121.1Sfvdl *  |                                 |
1131.1Sfvdl *  |    alt.L1 table (PTE pages)     |
1141.1Sfvdl *  |                                 |
1151.1Sfvdl *  +---------------------------------+ 0x00007f8000000000
1161.1Sfvdl *  ~                                 ~
1171.1Sfvdl *  |                                 |
1181.1Sfvdl *  |         User Space              |
1191.1Sfvdl *  |                                 |
1201.1Sfvdl *  |                                 |
1211.1Sfvdl *  +---------------------------------+ 0x0000000000000000
1221.1Sfvdl *
1231.4Syamt * In other words, there is a 'VA hole' at 0x0000800000000000 -
1241.1Sfvdl * 0xffff800000000000 which will trap, just as on, for example,
1251.1Sfvdl * sparcv9.
1261.1Sfvdl *
1271.1Sfvdl * The unused space can be used if needed, but it adds a little more
1281.1Sfvdl * complexity to the calculations.
1291.1Sfvdl */
1301.1Sfvdl
1311.1Sfvdl/*
1321.1Sfvdl * The first generation of Hammer processors can use 48 bits of
1331.1Sfvdl * virtual memory, and 40 bits of physical memory. This will be
1341.1Sfvdl * more for later generations. These defines can be changed to
1351.1Sfvdl * variable names containing the # of bits, extracted from an
1361.1Sfvdl * extended cpuid instruction (variables are harder to use during
1371.1Sfvdl * bootstrap, though)
1381.1Sfvdl */
1391.1Sfvdl#define VIRT_BITS	48
1401.1Sfvdl#define PHYS_BITS	40
1411.1Sfvdl
1421.1Sfvdl/*
1431.1Sfvdl * Mask to get rid of the sign-extended part of addresses.
1441.1Sfvdl */
1451.1Sfvdl#define VA_SIGN_MASK		0xffff000000000000
1461.1Sfvdl#define VA_SIGN_NEG(va)		((va) | VA_SIGN_MASK)
1471.1Sfvdl/*
1481.1Sfvdl * XXXfvdl this one's not right.
1491.1Sfvdl */
1501.1Sfvdl#define VA_SIGN_POS(va)		((va) & ~VA_SIGN_MASK)
1511.1Sfvdl
1521.1Sfvdl#define L4_SLOT_PTE		255
1531.1Sfvdl#define L4_SLOT_KERN		256
1541.1Sfvdl#define L4_SLOT_KERNBASE	511
1551.1Sfvdl#define L4_SLOT_APTE		510
1561.1Sfvdl
1571.1Sfvdl#define PDIR_SLOT_KERN	L4_SLOT_KERN
1581.1Sfvdl#define PDIR_SLOT_PTE	L4_SLOT_PTE
1591.1Sfvdl#define PDIR_SLOT_APTE	L4_SLOT_APTE
1601.1Sfvdl
1611.1Sfvdl/*
1621.1Sfvdl * the following defines give the virtual addresses of various MMU
1631.1Sfvdl * data structures:
1641.1Sfvdl * PTE_BASE and APTE_BASE: the base VA of the linear PTE mappings
1651.1Sfvdl * PTD_BASE and APTD_BASE: the base VA of the recursive mapping of the PTD
1661.1Sfvdl * PDP_PDE and APDP_PDE: the VA of the PDE that points back to the PDP/APDP
1671.1Sfvdl *
1681.1Sfvdl */
1691.1Sfvdl
1701.1Sfvdl#define PTE_BASE  ((pt_entry_t *) (L4_SLOT_PTE * NBPD_L4))
1711.1Sfvdl#define APTE_BASE ((pt_entry_t *) (VA_SIGN_NEG((L4_SLOT_APTE * NBPD_L4))))
1721.1Sfvdl
1731.1Sfvdl#define L1_BASE		PTE_BASE
1741.1Sfvdl#define AL1_BASE	APTE_BASE
1751.1Sfvdl
1761.1Sfvdl#define L2_BASE ((pd_entry_t *)((char *)L1_BASE + L4_SLOT_PTE * NBPD_L3))
1771.1Sfvdl#define L3_BASE ((pd_entry_t *)((char *)L2_BASE + L4_SLOT_PTE * NBPD_L2))
1781.1Sfvdl#define L4_BASE ((pd_entry_t *)((char *)L3_BASE + L4_SLOT_PTE * NBPD_L1))
1791.1Sfvdl
1801.1Sfvdl#define AL2_BASE ((pd_entry_t *)((char *)AL1_BASE + L4_SLOT_PTE * NBPD_L3))
1811.1Sfvdl#define AL3_BASE ((pd_entry_t *)((char *)AL2_BASE + L4_SLOT_PTE * NBPD_L2))
1821.1Sfvdl#define AL4_BASE ((pd_entry_t *)((char *)AL3_BASE + L4_SLOT_PTE * NBPD_L1))
1831.1Sfvdl
1841.1Sfvdl#define PDP_PDE		(L4_BASE + PDIR_SLOT_PTE)
1851.1Sfvdl#define APDP_PDE	(L4_BASE + PDIR_SLOT_APTE)
1861.1Sfvdl
1871.1Sfvdl#define PDP_BASE	L4_BASE
1881.1Sfvdl#define APDP_BASE	AL4_BASE
1891.1Sfvdl
1901.1Sfvdl#define NKL4_MAX_ENTRIES	(unsigned long)1
1911.1Sfvdl#define NKL3_MAX_ENTRIES	(unsigned long)(NKL4_MAX_ENTRIES * 512)
1921.1Sfvdl#define NKL2_MAX_ENTRIES	(unsigned long)(NKL3_MAX_ENTRIES * 512)
1931.1Sfvdl#define NKL1_MAX_ENTRIES	(unsigned long)(NKL2_MAX_ENTRIES * 512)
1941.1Sfvdl
1951.1Sfvdl#define NKL4_KIMG_ENTRIES	1
1961.1Sfvdl#define NKL3_KIMG_ENTRIES	1
1971.1Sfvdl#define NKL2_KIMG_ENTRIES	8
1981.1Sfvdl
1991.1Sfvdl/*
2001.1Sfvdl * Since kva space is below the kernel in its entirety, we start off
2011.1Sfvdl * with zero entries on each level.
2021.1Sfvdl */
2031.1Sfvdl#define NKL4_START_ENTRIES	0
2041.1Sfvdl#define NKL3_START_ENTRIES	0
2051.1Sfvdl#define NKL2_START_ENTRIES	0
2061.1Sfvdl#define NKL1_START_ENTRIES	0	/* XXX */
2071.1Sfvdl
2081.1Sfvdl#define NTOPLEVEL_PDES		(PAGE_SIZE / (sizeof (pd_entry_t)))
2091.1Sfvdl
2101.1Sfvdl#define NPDPG			(PAGE_SIZE / sizeof (pd_entry_t))
2111.1Sfvdl
2121.1Sfvdl#define PTP_MASK_INITIALIZER	{ L1_FRAME, L2_FRAME, L3_FRAME, L4_FRAME }
2131.1Sfvdl#define PTP_SHIFT_INITIALIZER	{ L1_SHIFT, L2_SHIFT, L3_SHIFT, L4_SHIFT }
2141.1Sfvdl#define NKPTP_INITIALIZER	{ NKL1_START_ENTRIES, NKL2_START_ENTRIES, \
2151.1Sfvdl				  NKL3_START_ENTRIES, NKL4_START_ENTRIES }
2161.1Sfvdl#define NKPTPMAX_INITIALIZER	{ NKL1_MAX_ENTRIES, NKL2_MAX_ENTRIES, \
2171.1Sfvdl				  NKL3_MAX_ENTRIES, NKL4_MAX_ENTRIES }
2181.1Sfvdl#define NBPD_INITIALIZER	{ NBPD_L1, NBPD_L2, NBPD_L3, NBPD_L4 }
2191.1Sfvdl#define PDES_INITIALIZER	{ L2_BASE, L3_BASE, L4_BASE }
2201.1Sfvdl#define APDES_INITIALIZER	{ AL2_BASE, AL3_BASE, AL4_BASE }
2211.1Sfvdl
2221.1Sfvdl#define PTP_LEVELS	4
2231.1Sfvdl
2241.1Sfvdl/*
2251.1Sfvdl * PG_AVAIL usage: we make use of the ignored bits of the PTE
2261.1Sfvdl */
2271.1Sfvdl
2281.1Sfvdl#define PG_W		PG_AVAIL1	/* "wired" mapping */
2291.1Sfvdl#define PG_PVLIST	PG_AVAIL2	/* mapping has entry on pvlist */
2301.1Sfvdl/* PG_AVAIL3 not used */
2311.1Sfvdl
2321.14Syamt#define	PG_X		0		/* XXX dummy */
2331.14Syamt
2341.1Sfvdl/*
2351.1Sfvdl * Number of PTE's per cache line.  8 byte pte, 64-byte cache line
2361.1Sfvdl * Used to avoid false sharing of cache lines.
2371.1Sfvdl */
2381.1Sfvdl#define NPTECL		8
2391.1Sfvdl
2401.1Sfvdl#define pmap_pte_set(p, n)		x86_atomic_testset_u64(p, n)
2411.11Sad#define pmap_pte_setbits(p, b)		x86_atomic_setbits_u64(p, b)
2421.1Sfvdl#define pmap_pte_clearbits(p, b)	x86_atomic_clearbits_u64(p, b)
2431.1Sfvdl#define pmap_cpu_has_pg_n()		(1)
2441.1Sfvdl#define pmap_cpu_has_invlpg		(1)
2451.1Sfvdl
2461.14Syamt#include <x86/pmap.h>
2471.11Sad
2481.14Syamtvoid pmap_prealloc_lowmem_ptps(void);
2491.14Syamtvoid pmap_changeprot_local(vaddr_t, vm_prot_t);
2501.11Sad
2511.1Sfvdl#endif	/* _AMD64_PMAP_H_ */
252