pmap.h revision 1.15
11.15Sbouyer/*	$NetBSD: pmap.h,v 1.15 2007/11/22 16:16:45 bouyer 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.15Sbouyer#if defined(_KERNEL_OPT)
741.15Sbouyer#include "opt_xen.h"
751.15Sbouyer#endif
761.15Sbouyer
771.1Sfvdl#include <machine/pte.h>
781.1Sfvdl#include <machine/segments.h>
791.11Sad#include <machine/atomic.h>
801.12Sad#ifdef _KERNEL
811.12Sad#include <machine/cpufunc.h>
821.12Sad#endif
831.11Sad
841.1Sfvdl#include <uvm/uvm_object.h>
851.15Sbouyer#ifdef XEN
861.15Sbouyer#include <xen/xenfunc.h>
871.15Sbouyer#include <xen/xenpmap.h>
881.15Sbouyer#endif /* XEN */
891.1Sfvdl
901.1Sfvdl/*
911.1Sfvdl * The x86_64 pmap module closely resembles the i386 one. It uses
921.1Sfvdl * the same recursive entry scheme, and the same alternate area
931.1Sfvdl * trick for accessing non-current pmaps. See the i386 pmap.h
941.1Sfvdl * for a description. The obvious difference is that 3 extra
951.1Sfvdl * levels of page table need to be dealt with. The level 1 page
961.1Sfvdl * table pages are at:
971.1Sfvdl *
981.1Sfvdl * l1: 0x00007f8000000000 - 0x00007fffffffffff     (39 bits, needs PML4 entry)
991.1Sfvdl *
1001.1Sfvdl * The alternate space is at:
1011.1Sfvdl *
1021.1Sfvdl * l1: 0xffffff8000000000 - 0xffffffffffffffff     (39 bits, needs PML4 entry)
1031.1Sfvdl *
1041.1Sfvdl * The rest is kept as physical pages in 3 UVM objects, and is
1051.1Sfvdl * temporarily mapped for virtual access when needed.
1061.1Sfvdl *
1071.1Sfvdl * Note that address space is signed, so the layout for 48 bits is:
1081.1Sfvdl *
1091.1Sfvdl *  +---------------------------------+ 0xffffffffffffffff
1101.1Sfvdl *  |                                 |
1111.1Sfvdl *  |    alt.L1 table (PTE pages)     |
1121.1Sfvdl *  |                                 |
1131.1Sfvdl *  +---------------------------------+ 0xffffff8000000000
1141.1Sfvdl *  ~                                 ~
1151.1Sfvdl *  |                                 |
1161.1Sfvdl *  |         Kernel Space            |
1171.1Sfvdl *  |                                 |
1181.1Sfvdl *  |                                 |
1191.4Syamt *  +---------------------------------+ 0xffff800000000000 = 0x0000800000000000
1201.1Sfvdl *  |                                 |
1211.1Sfvdl *  |    alt.L1 table (PTE pages)     |
1221.1Sfvdl *  |                                 |
1231.1Sfvdl *  +---------------------------------+ 0x00007f8000000000
1241.1Sfvdl *  ~                                 ~
1251.1Sfvdl *  |                                 |
1261.1Sfvdl *  |         User Space              |
1271.1Sfvdl *  |                                 |
1281.1Sfvdl *  |                                 |
1291.1Sfvdl *  +---------------------------------+ 0x0000000000000000
1301.1Sfvdl *
1311.4Syamt * In other words, there is a 'VA hole' at 0x0000800000000000 -
1321.1Sfvdl * 0xffff800000000000 which will trap, just as on, for example,
1331.1Sfvdl * sparcv9.
1341.1Sfvdl *
1351.1Sfvdl * The unused space can be used if needed, but it adds a little more
1361.1Sfvdl * complexity to the calculations.
1371.1Sfvdl */
1381.1Sfvdl
1391.1Sfvdl/*
1401.1Sfvdl * The first generation of Hammer processors can use 48 bits of
1411.1Sfvdl * virtual memory, and 40 bits of physical memory. This will be
1421.1Sfvdl * more for later generations. These defines can be changed to
1431.1Sfvdl * variable names containing the # of bits, extracted from an
1441.1Sfvdl * extended cpuid instruction (variables are harder to use during
1451.1Sfvdl * bootstrap, though)
1461.1Sfvdl */
1471.1Sfvdl#define VIRT_BITS	48
1481.1Sfvdl#define PHYS_BITS	40
1491.1Sfvdl
1501.1Sfvdl/*
1511.1Sfvdl * Mask to get rid of the sign-extended part of addresses.
1521.1Sfvdl */
1531.1Sfvdl#define VA_SIGN_MASK		0xffff000000000000
1541.1Sfvdl#define VA_SIGN_NEG(va)		((va) | VA_SIGN_MASK)
1551.1Sfvdl/*
1561.1Sfvdl * XXXfvdl this one's not right.
1571.1Sfvdl */
1581.1Sfvdl#define VA_SIGN_POS(va)		((va) & ~VA_SIGN_MASK)
1591.1Sfvdl
1601.1Sfvdl#define L4_SLOT_PTE		255
1611.15Sbouyer#ifndef XEN
1621.1Sfvdl#define L4_SLOT_KERN		256
1631.15Sbouyer#else
1641.15Sbouyer/* Xen use slots 256-272, let's move farther */
1651.15Sbouyer#define L4_SLOT_KERN		320
1661.15Sbouyer#endif
1671.1Sfvdl#define L4_SLOT_KERNBASE	511
1681.1Sfvdl#define L4_SLOT_APTE		510
1691.1Sfvdl
1701.1Sfvdl#define PDIR_SLOT_KERN	L4_SLOT_KERN
1711.1Sfvdl#define PDIR_SLOT_PTE	L4_SLOT_PTE
1721.1Sfvdl#define PDIR_SLOT_APTE	L4_SLOT_APTE
1731.1Sfvdl
1741.1Sfvdl/*
1751.1Sfvdl * the following defines give the virtual addresses of various MMU
1761.1Sfvdl * data structures:
1771.1Sfvdl * PTE_BASE and APTE_BASE: the base VA of the linear PTE mappings
1781.1Sfvdl * PTD_BASE and APTD_BASE: the base VA of the recursive mapping of the PTD
1791.1Sfvdl * PDP_PDE and APDP_PDE: the VA of the PDE that points back to the PDP/APDP
1801.1Sfvdl *
1811.1Sfvdl */
1821.1Sfvdl
1831.1Sfvdl#define PTE_BASE  ((pt_entry_t *) (L4_SLOT_PTE * NBPD_L4))
1841.1Sfvdl#define APTE_BASE ((pt_entry_t *) (VA_SIGN_NEG((L4_SLOT_APTE * NBPD_L4))))
1851.1Sfvdl
1861.1Sfvdl#define L1_BASE		PTE_BASE
1871.1Sfvdl#define AL1_BASE	APTE_BASE
1881.1Sfvdl
1891.1Sfvdl#define L2_BASE ((pd_entry_t *)((char *)L1_BASE + L4_SLOT_PTE * NBPD_L3))
1901.1Sfvdl#define L3_BASE ((pd_entry_t *)((char *)L2_BASE + L4_SLOT_PTE * NBPD_L2))
1911.1Sfvdl#define L4_BASE ((pd_entry_t *)((char *)L3_BASE + L4_SLOT_PTE * NBPD_L1))
1921.1Sfvdl
1931.1Sfvdl#define AL2_BASE ((pd_entry_t *)((char *)AL1_BASE + L4_SLOT_PTE * NBPD_L3))
1941.1Sfvdl#define AL3_BASE ((pd_entry_t *)((char *)AL2_BASE + L4_SLOT_PTE * NBPD_L2))
1951.1Sfvdl#define AL4_BASE ((pd_entry_t *)((char *)AL3_BASE + L4_SLOT_PTE * NBPD_L1))
1961.1Sfvdl
1971.1Sfvdl#define PDP_PDE		(L4_BASE + PDIR_SLOT_PTE)
1981.1Sfvdl#define APDP_PDE	(L4_BASE + PDIR_SLOT_APTE)
1991.1Sfvdl
2001.1Sfvdl#define PDP_BASE	L4_BASE
2011.1Sfvdl#define APDP_BASE	AL4_BASE
2021.1Sfvdl
2031.1Sfvdl#define NKL4_MAX_ENTRIES	(unsigned long)1
2041.1Sfvdl#define NKL3_MAX_ENTRIES	(unsigned long)(NKL4_MAX_ENTRIES * 512)
2051.1Sfvdl#define NKL2_MAX_ENTRIES	(unsigned long)(NKL3_MAX_ENTRIES * 512)
2061.1Sfvdl#define NKL1_MAX_ENTRIES	(unsigned long)(NKL2_MAX_ENTRIES * 512)
2071.1Sfvdl
2081.1Sfvdl#define NKL4_KIMG_ENTRIES	1
2091.1Sfvdl#define NKL3_KIMG_ENTRIES	1
2101.1Sfvdl#define NKL2_KIMG_ENTRIES	8
2111.1Sfvdl
2121.1Sfvdl/*
2131.1Sfvdl * Since kva space is below the kernel in its entirety, we start off
2141.1Sfvdl * with zero entries on each level.
2151.1Sfvdl */
2161.1Sfvdl#define NKL4_START_ENTRIES	0
2171.1Sfvdl#define NKL3_START_ENTRIES	0
2181.1Sfvdl#define NKL2_START_ENTRIES	0
2191.1Sfvdl#define NKL1_START_ENTRIES	0	/* XXX */
2201.1Sfvdl
2211.1Sfvdl#define NTOPLEVEL_PDES		(PAGE_SIZE / (sizeof (pd_entry_t)))
2221.1Sfvdl
2231.1Sfvdl#define NPDPG			(PAGE_SIZE / sizeof (pd_entry_t))
2241.1Sfvdl
2251.1Sfvdl#define PTP_MASK_INITIALIZER	{ L1_FRAME, L2_FRAME, L3_FRAME, L4_FRAME }
2261.1Sfvdl#define PTP_SHIFT_INITIALIZER	{ L1_SHIFT, L2_SHIFT, L3_SHIFT, L4_SHIFT }
2271.1Sfvdl#define NKPTP_INITIALIZER	{ NKL1_START_ENTRIES, NKL2_START_ENTRIES, \
2281.1Sfvdl				  NKL3_START_ENTRIES, NKL4_START_ENTRIES }
2291.1Sfvdl#define NKPTPMAX_INITIALIZER	{ NKL1_MAX_ENTRIES, NKL2_MAX_ENTRIES, \
2301.1Sfvdl				  NKL3_MAX_ENTRIES, NKL4_MAX_ENTRIES }
2311.1Sfvdl#define NBPD_INITIALIZER	{ NBPD_L1, NBPD_L2, NBPD_L3, NBPD_L4 }
2321.1Sfvdl#define PDES_INITIALIZER	{ L2_BASE, L3_BASE, L4_BASE }
2331.1Sfvdl#define APDES_INITIALIZER	{ AL2_BASE, AL3_BASE, AL4_BASE }
2341.1Sfvdl
2351.1Sfvdl#define PTP_LEVELS	4
2361.1Sfvdl
2371.1Sfvdl/*
2381.1Sfvdl * PG_AVAIL usage: we make use of the ignored bits of the PTE
2391.1Sfvdl */
2401.1Sfvdl
2411.1Sfvdl#define PG_W		PG_AVAIL1	/* "wired" mapping */
2421.1Sfvdl#define PG_PVLIST	PG_AVAIL2	/* mapping has entry on pvlist */
2431.1Sfvdl/* PG_AVAIL3 not used */
2441.1Sfvdl
2451.14Syamt#define	PG_X		0		/* XXX dummy */
2461.14Syamt
2471.1Sfvdl/*
2481.1Sfvdl * Number of PTE's per cache line.  8 byte pte, 64-byte cache line
2491.1Sfvdl * Used to avoid false sharing of cache lines.
2501.1Sfvdl */
2511.1Sfvdl#define NPTECL		8
2521.1Sfvdl
2531.15Sbouyer#include <x86/pmap.h>
2541.15Sbouyer
2551.15Sbouyer#ifndef XEN
2561.15Sbouyer#define pmap_pa2pte(a)			(a)
2571.15Sbouyer#define pmap_pte2pa(a)			((a) & PG_FRAME)
2581.15Sbouyer#define pmap_pte_set(p, n)		do { *(p) = (n); } while (0)
2591.15Sbouyer#define pmap_pte_testset(p, n)		x86_atomic_testset_u64(p, n)
2601.11Sad#define pmap_pte_setbits(p, b)		x86_atomic_setbits_u64(p, b)
2611.1Sfvdl#define pmap_pte_clearbits(p, b)	x86_atomic_clearbits_u64(p, b)
2621.15Sbouyer#define pmap_pte_flush()		/* nothing */
2631.1Sfvdl#define pmap_cpu_has_pg_n()		(1)
2641.1Sfvdl#define pmap_cpu_has_invlpg		(1)
2651.15Sbouyer#else
2661.15Sbouyerstatic __inline pt_entry_t
2671.15Sbouyerpmap_pa2pte(paddr_t pa)
2681.15Sbouyer{
2691.15Sbouyer	return (pt_entry_t)xpmap_ptom_masked(pa);
2701.15Sbouyer}
2711.15Sbouyer
2721.15Sbouyerstatic __inline paddr_t
2731.15Sbouyerpmap_pte2pa(pt_entry_t pte)
2741.15Sbouyer{
2751.15Sbouyer	return xpmap_mtop_masked(pte & PG_FRAME);
2761.15Sbouyer}
2771.15Sbouyerstatic __inline void
2781.15Sbouyerpmap_pte_set(pt_entry_t *pte, pt_entry_t npte)
2791.15Sbouyer{
2801.15Sbouyer	int s = splvm();
2811.15Sbouyer	xpq_queue_pte_update((pt_entry_t *)xpmap_ptetomach(pte), npte);
2821.15Sbouyer	splx(s);
2831.15Sbouyer}
2841.15Sbouyer
2851.15Sbouyerstatic __inline pt_entry_t
2861.15Sbouyerpmap_pte_testset(volatile pt_entry_t *pte, pt_entry_t npte)
2871.15Sbouyer{
2881.15Sbouyer	int s = splvm();
2891.15Sbouyer	pt_entry_t opte = *pte;
2901.15Sbouyer	xpq_queue_pte_update((pt_entry_t *)xpmap_ptetomach(__UNVOLATILE(pte)),
2911.15Sbouyer	    npte);
2921.15Sbouyer	xpq_flush_queue();
2931.15Sbouyer	splx(s);
2941.15Sbouyer	return opte;
2951.15Sbouyer}
2961.15Sbouyer
2971.15Sbouyerstatic __inline void
2981.15Sbouyerpmap_pte_setbits(volatile pt_entry_t *pte, pt_entry_t bits)
2991.15Sbouyer{
3001.15Sbouyer	int s = splvm();
3011.15Sbouyer	xpq_queue_pte_update((pt_entry_t *)xpmap_ptetomach(__UNVOLATILE(pte)),
3021.15Sbouyer	    (*pte) | bits);
3031.15Sbouyer	xpq_flush_queue();
3041.15Sbouyer	splx(s);
3051.15Sbouyer}
3061.15Sbouyer
3071.15Sbouyerstatic __inline void
3081.15Sbouyerpmap_pte_clearbits(volatile pt_entry_t *pte, pt_entry_t bits)
3091.15Sbouyer{
3101.15Sbouyer	int s = splvm();
3111.15Sbouyer	xpq_queue_pte_update((pt_entry_t *)xpmap_ptetomach(__UNVOLATILE(pte)),
3121.15Sbouyer	    (*pte) & ~bits);
3131.15Sbouyer	xpq_flush_queue();
3141.15Sbouyer	splx(s);
3151.15Sbouyer}
3161.1Sfvdl
3171.15Sbouyer#define pmap_cpu_has_pg_n()		(1)
3181.15Sbouyer#define pmap_cpu_has_invlpg		(1)
3191.15Sbouyerstatic __inline void
3201.15Sbouyerpmap_pte_flush(void)
3211.15Sbouyer{
3221.15Sbouyer	int s = splvm();
3231.15Sbouyer	xpq_flush_queue();
3241.15Sbouyer	splx(s);
3251.15Sbouyer}
3261.15Sbouyer#endif
3271.11Sad
3281.14Syamtvoid pmap_prealloc_lowmem_ptps(void);
3291.14Syamtvoid pmap_changeprot_local(vaddr_t, vm_prot_t);
3301.11Sad
3311.1Sfvdl#endif	/* _AMD64_PMAP_H_ */
332