146018Swilliam /* 2*63359Sbostic * Copyright (c) 1991, 1993 3*63359Sbostic * The Regents of the University of California. All rights reserved. 446018Swilliam * 546018Swilliam * This code is derived from software contributed to Berkeley by 649614Sbostic * the Systems Programming Group of the University of Utah Computer 749614Sbostic * Science Department and William Jolitz of UUNET Technologies Inc. 846018Swilliam * 949614Sbostic * %sccs.include.redist.c% 1046018Swilliam * 11*63359Sbostic * @(#)pmap.h 8.1 (Berkeley) 06/11/93 1249614Sbostic */ 1349614Sbostic 1449614Sbostic /* 1547997Swilliam * Derived from hp300 version by Mike Hibler, this version by William 1647997Swilliam * Jolitz uses a recursive map [a pde points to the page directory] to 1747997Swilliam * map the page tables using the pagetables themselves. This is done to 1847997Swilliam * reduce the impact on kernel virtual memory for lots of sparse address 1947997Swilliam * space, and to reduce the cost of memory to each process. 2047997Swilliam * 2146018Swilliam * from hp300: @(#)pmap.h 7.2 (Berkeley) 12/16/90 2246018Swilliam */ 2346018Swilliam 2446018Swilliam #ifndef _PMAP_MACHINE_ 2546018Swilliam #define _PMAP_MACHINE_ 1 2646018Swilliam 2746018Swilliam /* 2846018Swilliam * 386 page table entry and page table directory 2946018Swilliam * W.Jolitz, 8/89 3046018Swilliam */ 3146018Swilliam 3246018Swilliam struct pde 3346018Swilliam { 3446018Swilliam unsigned int 3546018Swilliam pd_v:1, /* valid bit */ 3646018Swilliam pd_prot:2, /* access control */ 3746018Swilliam pd_mbz1:2, /* reserved, must be zero */ 3846018Swilliam pd_u:1, /* hardware maintained 'used' bit */ 3946018Swilliam :1, /* not used */ 4046018Swilliam pd_mbz2:2, /* reserved, must be zero */ 4146018Swilliam :3, /* reserved for software */ 4246018Swilliam pd_pfnum:20; /* physical page frame number of pte's*/ 4346018Swilliam }; 4446018Swilliam 4546018Swilliam #define PD_MASK 0xffc00000 /* page directory address bits */ 4647997Swilliam #define PT_MASK 0x003ff000 /* page table address bits */ 4747997Swilliam #define PD_SHIFT 22 /* page directory address shift */ 4847997Swilliam #define PG_SHIFT 12 /* page table address shift */ 4946018Swilliam 5046018Swilliam struct pte 5146018Swilliam { 5246018Swilliam unsigned int 5346018Swilliam pg_v:1, /* valid bit */ 5446018Swilliam pg_prot:2, /* access control */ 5546018Swilliam pg_mbz1:2, /* reserved, must be zero */ 5646018Swilliam pg_u:1, /* hardware maintained 'used' bit */ 5746018Swilliam pg_m:1, /* hardware maintained modified bit */ 5846018Swilliam pg_mbz2:2, /* reserved, must be zero */ 5946018Swilliam pg_w:1, /* software, wired down page */ 6046018Swilliam :1, /* software (unused) */ 6146018Swilliam pg_nc:1, /* 'uncacheable page' bit */ 6246018Swilliam pg_pfnum:20; /* physical page frame number */ 6346018Swilliam }; 6446018Swilliam 6546018Swilliam #define PG_V 0x00000001 6647997Swilliam #define PG_RO 0x00000000 6747997Swilliam #define PG_RW 0x00000002 6847997Swilliam #define PG_u 0x00000004 6946018Swilliam #define PG_PROT 0x00000006 /* all protection bits . */ 7046018Swilliam #define PG_W 0x00000200 7146018Swilliam #define PG_N 0x00000800 /* Non-cacheable */ 7246018Swilliam #define PG_M 0x00000040 7346018Swilliam #define PG_U 0x00000020 7446018Swilliam #define PG_FRAME 0xfffff000 7546018Swilliam 7646018Swilliam #define PG_NOACC 0 7746018Swilliam #define PG_KR 0x00000000 7846018Swilliam #define PG_KW 0x00000002 7946018Swilliam #define PG_URKR 0x00000004 8046018Swilliam #define PG_URKW 0x00000004 8146018Swilliam #define PG_UW 0x00000006 8246018Swilliam 8347997Swilliam /* Garbage for current bastardized pager that assumes a hp300 */ 8447997Swilliam #define PG_NV 0 8547997Swilliam #define PG_CI 0 8646018Swilliam /* 8746018Swilliam * Page Protection Exception bits 8846018Swilliam */ 8946018Swilliam 9046018Swilliam #define PGEX_P 0x01 /* Protection violation vs. not present */ 9146018Swilliam #define PGEX_W 0x02 /* during a Write cycle */ 9246018Swilliam #define PGEX_U 0x04 /* access from User mode (UPL) */ 9346018Swilliam 9446018Swilliam typedef struct pde pd_entry_t; /* page directory entry */ 9546018Swilliam typedef struct pte pt_entry_t; /* Mach page table entry */ 9646018Swilliam 9746018Swilliam /* 9846018Swilliam * One page directory, shared between 9946018Swilliam * kernel and user modes. 10046018Swilliam */ 10146018Swilliam #define I386_PAGE_SIZE NBPG 10246018Swilliam #define I386_PDR_SIZE NBPDR 10346018Swilliam 10446018Swilliam #define I386_KPDES 8 /* KPT page directory size */ 10546018Swilliam #define I386_UPDES NBPDR/sizeof(struct pde)-8 /* UPT page directory size */ 10646018Swilliam 10747997Swilliam #define UPTDI 0x3f6 /* ptd entry for u./kernel&user stack */ 10847997Swilliam #define PTDPTDI 0x3f7 /* ptd entry that points to ptd! */ 10947997Swilliam #define KPTDI_FIRST 0x3f8 /* start of kernel virtual pde's */ 11047997Swilliam #define KPTDI_LAST 0x3fA /* last of kernel virtual pde's */ 11146018Swilliam 11246018Swilliam /* 11347997Swilliam * Address of current and alternate address space page table maps 11447997Swilliam * and directories. 11547997Swilliam */ 11649701Swilliam #ifdef KERNEL 11749701Swilliam extern struct pte PTmap[], APTmap[], Upte; 11849701Swilliam extern struct pde PTD[], APTD[], PTDpde, APTDpde, Upde; 11949701Swilliam extern pt_entry_t *Sysmap; 12047997Swilliam 12149701Swilliam extern int IdlePTD; /* physical address of "Idle" state directory */ 12249701Swilliam #endif 12347997Swilliam 12447997Swilliam /* 12547997Swilliam * virtual address to page table entry and 12647997Swilliam * to physical address. Likewise for alternate address space. 12747998Swilliam * Note: these work recursively, thus vtopte of a pte will give 12847998Swilliam * the corresponding pde that in turn maps it. 12947997Swilliam */ 13047997Swilliam #define vtopte(va) (PTmap + i386_btop(va)) 13147997Swilliam #define kvtopte(va) vtopte(va) 13247997Swilliam #define ptetov(pt) (i386_ptob(pt - PTmap)) 13347997Swilliam #define vtophys(va) (i386_ptob(vtopte(va)->pg_pfnum) | ((int)(va) & PGOFSET)) 13447998Swilliam #define ispt(va) ((va) >= UPT_MIN_ADDRESS && (va) <= KPT_MAX_ADDRESS) 13547997Swilliam 13647997Swilliam #define avtopte(va) (APTmap + i386_btop(va)) 13747997Swilliam #define ptetoav(pt) (i386_ptob(pt - APTmap)) 13847997Swilliam #define avtophys(va) (i386_ptob(avtopte(va)->pg_pfnum) | ((int)(va) & PGOFSET)) 13947997Swilliam 14047997Swilliam /* 14147997Swilliam * macros to generate page directory/table indicies 14247997Swilliam */ 14347997Swilliam 14447997Swilliam #define pdei(va) (((va)&PD_MASK)>>PD_SHIFT) 14547997Swilliam #define ptei(va) (((va)&PT_MASK)>>PT_SHIFT) 14647997Swilliam 14747997Swilliam /* 14846018Swilliam * Pmap stuff 14946018Swilliam */ 15046018Swilliam 15146018Swilliam struct pmap { 15246018Swilliam pd_entry_t *pm_pdir; /* KVA of page directory */ 15346018Swilliam boolean_t pm_pdchanged; /* pdir changed */ 15446018Swilliam short pm_dref; /* page directory ref count */ 15546018Swilliam short pm_count; /* pmap reference count */ 15646018Swilliam simple_lock_data_t pm_lock; /* lock on pmap */ 15746018Swilliam struct pmap_statistics pm_stats; /* pmap statistics */ 15846018Swilliam long pm_ptpages; /* more stats: PT pages */ 15946018Swilliam }; 16046018Swilliam 16146018Swilliam typedef struct pmap *pmap_t; 16246018Swilliam 16349701Swilliam #ifdef KERNEL 16452601Smckusick extern struct pmap kernel_pmap_store; 16552601Smckusick #define kernel_pmap (&kernel_pmap_store) 16649701Swilliam #endif 16746018Swilliam 16846018Swilliam /* 16946018Swilliam * Macros for speed 17046018Swilliam */ 17146018Swilliam #define PMAP_ACTIVATE(pmapp, pcbp) \ 17249607Swilliam if ((pmapp) != NULL /*&& (pmapp)->pm_pdchanged */) { \ 17346018Swilliam (pcbp)->pcb_cr3 = \ 17447997Swilliam pmap_extract(kernel_pmap, (pmapp)->pm_pdir); \ 17549523Swilliam if ((pmapp) == &curproc->p_vmspace->vm_pmap) \ 17646018Swilliam load_cr3((pcbp)->pcb_cr3); \ 17746018Swilliam (pmapp)->pm_pdchanged = FALSE; \ 17846018Swilliam } 17947997Swilliam 18046018Swilliam #define PMAP_DEACTIVATE(pmapp, pcbp) 18146018Swilliam 18246018Swilliam /* 18346018Swilliam * For each vm_page_t, there is a list of all currently valid virtual 18446018Swilliam * mappings of that page. An entry is a pv_entry_t, the list is pv_table. 18546018Swilliam */ 18646018Swilliam typedef struct pv_entry { 18746018Swilliam struct pv_entry *pv_next; /* next pv_entry */ 18846018Swilliam pmap_t pv_pmap; /* pmap where mapping lies */ 18946018Swilliam vm_offset_t pv_va; /* virtual address for mapping */ 19046018Swilliam int pv_flags; /* flags */ 19146018Swilliam } *pv_entry_t; 19246018Swilliam 19346018Swilliam #define PV_ENTRY_NULL ((pv_entry_t) 0) 19446018Swilliam 19546018Swilliam #define PV_CI 0x01 /* all entries must be cache inhibited */ 19646018Swilliam #define PV_PTPAGE 0x02 /* entry maps a page table page */ 19746018Swilliam 19846018Swilliam #ifdef KERNEL 19946018Swilliam 20046018Swilliam pv_entry_t pv_table; /* array of entries, one per page */ 20146018Swilliam 20246018Swilliam #define pa_index(pa) atop(pa - vm_first_phys) 20346018Swilliam #define pa_to_pvh(pa) (&pv_table[pa_index(pa)]) 20446018Swilliam 20546018Swilliam #define pmap_resident_count(pmap) ((pmap)->pm_stats.resident_count) 20660998Shibler #define pmap_wired_count(pmap) ((pmap)->pm_stats.wired_count) 20746018Swilliam 20846018Swilliam #endif KERNEL 20946018Swilliam 21046018Swilliam #endif _PMAP_MACHINE_ 211