1#ifndef _ASM_X86_XEN_PAGE_H 2#define _ASM_X86_XEN_PAGE_H 3 4#include <linux/kernel.h> 5#include <linux/types.h> 6#include <linux/spinlock.h> 7#include <linux/pfn.h> 8 9#include <asm/uaccess.h> 10#include <asm/page.h> 11#include <asm/pgtable.h> 12 13#include <xen/interface/xen.h> 14#include <xen/features.h> 15 16/* Xen machine address */ 17typedef struct xmaddr { 18 phys_addr_t maddr; 19} xmaddr_t; 20 21/* Xen pseudo-physical address */ 22typedef struct xpaddr { 23 phys_addr_t paddr; 24} xpaddr_t; 25 26#define XMADDR(x) ((xmaddr_t) { .maddr = (x) }) 27#define XPADDR(x) ((xpaddr_t) { .paddr = (x) }) 28 29/**** MACHINE <-> PHYSICAL CONVERSION MACROS ****/ 30#define INVALID_P2M_ENTRY (~0UL) 31#define FOREIGN_FRAME_BIT (1UL<<31) 32#define FOREIGN_FRAME(m) ((m) | FOREIGN_FRAME_BIT) 33 34/* Maximum amount of memory we can handle in a domain in pages */ 35#define MAX_DOMAIN_PAGES \ 36 ((unsigned long)((u64)CONFIG_XEN_MAX_DOMAIN_MEMORY * 1024 * 1024 * 1024 / PAGE_SIZE)) 37 38 39extern unsigned long get_phys_to_machine(unsigned long pfn); 40extern void set_phys_to_machine(unsigned long pfn, unsigned long mfn); 41 42static inline unsigned long pfn_to_mfn(unsigned long pfn) 43{ 44 if (xen_feature(XENFEAT_auto_translated_physmap)) 45 return pfn; 46 47 return get_phys_to_machine(pfn) & ~FOREIGN_FRAME_BIT; 48} 49 50static inline int phys_to_machine_mapping_valid(unsigned long pfn) 51{ 52 if (xen_feature(XENFEAT_auto_translated_physmap)) 53 return 1; 54 55 return get_phys_to_machine(pfn) != INVALID_P2M_ENTRY; 56} 57 58static inline unsigned long mfn_to_pfn(unsigned long mfn) 59{ 60 unsigned long pfn; 61 62 if (xen_feature(XENFEAT_auto_translated_physmap)) 63 return mfn; 64 65 66 pfn = 0; 67 /* 68 * The array access can fail (e.g., device space beyond end of RAM). 69 * In such cases it doesn't matter what we return (we return garbage), 70 * but we must handle the fault without crashing! 71 */ 72 __get_user(pfn, &machine_to_phys_mapping[mfn]); 73 74 return pfn; 75} 76 77static inline xmaddr_t phys_to_machine(xpaddr_t phys) 78{ 79 unsigned offset = phys.paddr & ~PAGE_MASK; 80 return XMADDR(PFN_PHYS(pfn_to_mfn(PFN_DOWN(phys.paddr))) | offset); 81} 82 83static inline xpaddr_t machine_to_phys(xmaddr_t machine) 84{ 85 unsigned offset = machine.maddr & ~PAGE_MASK; 86 return XPADDR(PFN_PHYS(mfn_to_pfn(PFN_DOWN(machine.maddr))) | offset); 87} 88 89/* 90 * We detect special mappings in one of two ways: 91 * 1. If the MFN is an I/O page then Xen will set the m2p entry 92 * to be outside our maximum possible pseudophys range. 93 * 2. If the MFN belongs to a different domain then we will certainly 94 * not have MFN in our p2m table. Conversely, if the page is ours, 95 * then we'll have p2m(m2p(MFN))==MFN. 96 * If we detect a special mapping then it doesn't have a 'struct page'. 97 * We force !pfn_valid() by returning an out-of-range pointer. 98 * 99 * NB. These checks require that, for any MFN that is not in our reservation, 100 * there is no PFN such that p2m(PFN) == MFN. Otherwise we can get confused if 101 * we are foreign-mapping the MFN, and the other domain as m2p(MFN) == PFN. 102 * Yikes! Various places must poke in INVALID_P2M_ENTRY for safety. 103 * 104 * NB2. When deliberately mapping foreign pages into the p2m table, you *must* 105 * use FOREIGN_FRAME(). This will cause pte_pfn() to choke on it, as we 106 * require. In all the cases we care about, the FOREIGN_FRAME bit is 107 * masked (e.g., pfn_to_mfn()) so behaviour there is correct. 108 */ 109static inline unsigned long mfn_to_local_pfn(unsigned long mfn) 110{ 111 unsigned long pfn = mfn_to_pfn(mfn); 112 if (get_phys_to_machine(pfn) != mfn) 113 return -1; /* force !pfn_valid() */ 114 return pfn; 115} 116 117/* VIRT <-> MACHINE conversion */ 118#define virt_to_machine(v) (phys_to_machine(XPADDR(__pa(v)))) 119#define virt_to_pfn(v) (PFN_DOWN(__pa(v))) 120#define virt_to_mfn(v) (pfn_to_mfn(virt_to_pfn(v))) 121#define mfn_to_virt(m) (__va(mfn_to_pfn(m) << PAGE_SHIFT)) 122 123static inline unsigned long pte_mfn(pte_t pte) 124{ 125 return (pte.pte & PTE_PFN_MASK) >> PAGE_SHIFT; 126} 127 128static inline pte_t mfn_pte(unsigned long page_nr, pgprot_t pgprot) 129{ 130 pte_t pte; 131 132 pte.pte = ((phys_addr_t)page_nr << PAGE_SHIFT) | 133 massage_pgprot(pgprot); 134 135 return pte; 136} 137 138static inline pteval_t pte_val_ma(pte_t pte) 139{ 140 return pte.pte; 141} 142 143static inline pte_t __pte_ma(pteval_t x) 144{ 145 return (pte_t) { .pte = x }; 146} 147 148#define pmd_val_ma(v) ((v).pmd) 149#ifdef __PAGETABLE_PUD_FOLDED 150#define pud_val_ma(v) ((v).pgd.pgd) 151#else 152#define pud_val_ma(v) ((v).pud) 153#endif 154#define __pmd_ma(x) ((pmd_t) { (x) } ) 155 156#define pgd_val_ma(x) ((x).pgd) 157 158 159xmaddr_t arbitrary_virt_to_machine(void *address); 160unsigned long arbitrary_virt_to_mfn(void *vaddr); 161void make_lowmem_page_readonly(void *vaddr); 162void make_lowmem_page_readwrite(void *vaddr); 163 164#endif /* _ASM_X86_XEN_PAGE_H */ 165