[34] | 1 | #ifndef _X86_64_PGALLOC_H |
---|
| 2 | #define _X86_64_PGALLOC_H |
---|
| 3 | |
---|
| 4 | #include <asm/fixmap.h> |
---|
| 5 | #include <asm/pda.h> |
---|
| 6 | #include <linux/threads.h> |
---|
| 7 | #include <linux/mm.h> |
---|
| 8 | #include <asm/io.h> /* for phys_to_virt and page_to_pseudophys */ |
---|
| 9 | |
---|
| 10 | #include <xen/features.h> |
---|
| 11 | void make_page_readonly(void *va, unsigned int feature); |
---|
| 12 | void make_page_writable(void *va, unsigned int feature); |
---|
| 13 | void make_pages_readonly(void *va, unsigned int nr, unsigned int feature); |
---|
| 14 | void make_pages_writable(void *va, unsigned int nr, unsigned int feature); |
---|
| 15 | |
---|
| 16 | #define __user_pgd(pgd) ((pgd) + PTRS_PER_PGD) |
---|
| 17 | |
---|
| 18 | static inline void pmd_populate_kernel(struct mm_struct *mm, pmd_t *pmd, pte_t *pte) |
---|
| 19 | { |
---|
| 20 | set_pmd(pmd, __pmd(_PAGE_TABLE | __pa(pte))); |
---|
| 21 | } |
---|
| 22 | |
---|
| 23 | static inline void pmd_populate(struct mm_struct *mm, pmd_t *pmd, struct page *pte) |
---|
| 24 | { |
---|
| 25 | if (unlikely((mm)->context.pinned)) { |
---|
| 26 | BUG_ON(HYPERVISOR_update_va_mapping( |
---|
| 27 | (unsigned long)__va(page_to_pfn(pte) << PAGE_SHIFT), |
---|
| 28 | pfn_pte(page_to_pfn(pte), PAGE_KERNEL_RO), 0)); |
---|
| 29 | set_pmd(pmd, __pmd(_PAGE_TABLE | (page_to_pfn(pte) << PAGE_SHIFT))); |
---|
| 30 | } else { |
---|
| 31 | *(pmd) = __pmd(_PAGE_TABLE | (page_to_pfn(pte) << PAGE_SHIFT)); |
---|
| 32 | } |
---|
| 33 | } |
---|
| 34 | |
---|
| 35 | static inline void pud_populate(struct mm_struct *mm, pud_t *pud, pmd_t *pmd) |
---|
| 36 | { |
---|
| 37 | if (unlikely((mm)->context.pinned)) { |
---|
| 38 | BUG_ON(HYPERVISOR_update_va_mapping( |
---|
| 39 | (unsigned long)pmd, |
---|
| 40 | pfn_pte(virt_to_phys(pmd)>>PAGE_SHIFT, |
---|
| 41 | PAGE_KERNEL_RO), 0)); |
---|
| 42 | set_pud(pud, __pud(_PAGE_TABLE | __pa(pmd))); |
---|
| 43 | } else { |
---|
| 44 | *(pud) = __pud(_PAGE_TABLE | __pa(pmd)); |
---|
| 45 | } |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | /* |
---|
| 49 | * We need to use the batch mode here, but pgd_pupulate() won't be |
---|
| 50 | * be called frequently. |
---|
| 51 | */ |
---|
| 52 | static inline void pgd_populate(struct mm_struct *mm, pgd_t *pgd, pud_t *pud) |
---|
| 53 | { |
---|
| 54 | if (unlikely((mm)->context.pinned)) { |
---|
| 55 | BUG_ON(HYPERVISOR_update_va_mapping( |
---|
| 56 | (unsigned long)pud, |
---|
| 57 | pfn_pte(virt_to_phys(pud)>>PAGE_SHIFT, |
---|
| 58 | PAGE_KERNEL_RO), 0)); |
---|
| 59 | set_pgd(pgd, __pgd(_PAGE_TABLE | __pa(pud))); |
---|
| 60 | set_pgd(__user_pgd(pgd), __pgd(_PAGE_TABLE | __pa(pud))); |
---|
| 61 | } else { |
---|
| 62 | *(pgd) = __pgd(_PAGE_TABLE | __pa(pud)); |
---|
| 63 | *(__user_pgd(pgd)) = *(pgd); |
---|
| 64 | } |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | extern struct page *pte_alloc_one(struct mm_struct *mm, unsigned long addr); |
---|
| 68 | extern void pte_free(struct page *pte); |
---|
| 69 | |
---|
| 70 | static inline pmd_t *pmd_alloc_one(struct mm_struct *mm, unsigned long addr) |
---|
| 71 | { |
---|
| 72 | struct page *pg; |
---|
| 73 | |
---|
| 74 | pg = pte_alloc_one(mm, addr); |
---|
| 75 | return pg ? page_address(pg) : NULL; |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | static inline void pmd_free(pmd_t *pmd) |
---|
| 79 | { |
---|
| 80 | BUG_ON((unsigned long)pmd & (PAGE_SIZE-1)); |
---|
| 81 | pte_free(virt_to_page(pmd)); |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | static inline pud_t *pud_alloc_one(struct mm_struct *mm, unsigned long addr) |
---|
| 85 | { |
---|
| 86 | struct page *pg; |
---|
| 87 | |
---|
| 88 | pg = pte_alloc_one(mm, addr); |
---|
| 89 | return pg ? page_address(pg) : NULL; |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | static inline void pud_free(pud_t *pud) |
---|
| 93 | { |
---|
| 94 | BUG_ON((unsigned long)pud & (PAGE_SIZE-1)); |
---|
| 95 | pte_free(virt_to_page(pud)); |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | static inline void pgd_list_add(pgd_t *pgd) |
---|
| 99 | { |
---|
| 100 | struct page *page = virt_to_page(pgd); |
---|
| 101 | |
---|
| 102 | spin_lock(&pgd_lock); |
---|
| 103 | page->index = (pgoff_t)pgd_list; |
---|
| 104 | if (pgd_list) |
---|
| 105 | pgd_list->private = (unsigned long)&page->index; |
---|
| 106 | pgd_list = page; |
---|
| 107 | page->private = (unsigned long)&pgd_list; |
---|
| 108 | spin_unlock(&pgd_lock); |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | static inline void pgd_list_del(pgd_t *pgd) |
---|
| 112 | { |
---|
| 113 | struct page *next, **pprev, *page = virt_to_page(pgd); |
---|
| 114 | |
---|
| 115 | spin_lock(&pgd_lock); |
---|
| 116 | next = (struct page *)page->index; |
---|
| 117 | pprev = (struct page **)page->private; |
---|
| 118 | *pprev = next; |
---|
| 119 | if (next) |
---|
| 120 | next->private = (unsigned long)pprev; |
---|
| 121 | spin_unlock(&pgd_lock); |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | static inline pgd_t *pgd_alloc(struct mm_struct *mm) |
---|
| 125 | { |
---|
| 126 | /* |
---|
| 127 | * We allocate two contiguous pages for kernel and user. |
---|
| 128 | */ |
---|
| 129 | unsigned boundary; |
---|
| 130 | pgd_t *pgd = (pgd_t *)__get_free_pages(GFP_KERNEL|__GFP_REPEAT, 1); |
---|
| 131 | if (!pgd) |
---|
| 132 | return NULL; |
---|
| 133 | pgd_list_add(pgd); |
---|
| 134 | /* |
---|
| 135 | * Copy kernel pointers in from init. |
---|
| 136 | * Could keep a freelist or slab cache of those because the kernel |
---|
| 137 | * part never changes. |
---|
| 138 | */ |
---|
| 139 | boundary = pgd_index(__PAGE_OFFSET); |
---|
| 140 | memset(pgd, 0, boundary * sizeof(pgd_t)); |
---|
| 141 | memcpy(pgd + boundary, |
---|
| 142 | init_level4_pgt + boundary, |
---|
| 143 | (PTRS_PER_PGD - boundary) * sizeof(pgd_t)); |
---|
| 144 | |
---|
| 145 | memset(__user_pgd(pgd), 0, PAGE_SIZE); /* clean up user pgd */ |
---|
| 146 | /* |
---|
| 147 | * Set level3_user_pgt for vsyscall area |
---|
| 148 | */ |
---|
| 149 | set_pgd(__user_pgd(pgd) + pgd_index(VSYSCALL_START), |
---|
| 150 | mk_kernel_pgd(__pa_symbol(level3_user_pgt))); |
---|
| 151 | return pgd; |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | static inline void pgd_free(pgd_t *pgd) |
---|
| 155 | { |
---|
| 156 | pte_t *ptep = virt_to_ptep(pgd); |
---|
| 157 | |
---|
| 158 | if (!pte_write(*ptep)) { |
---|
| 159 | xen_pgd_unpin(__pa(pgd)); |
---|
| 160 | BUG_ON(HYPERVISOR_update_va_mapping( |
---|
| 161 | (unsigned long)pgd, |
---|
| 162 | pfn_pte(virt_to_phys(pgd)>>PAGE_SHIFT, PAGE_KERNEL), |
---|
| 163 | 0)); |
---|
| 164 | } |
---|
| 165 | |
---|
| 166 | ptep = virt_to_ptep(__user_pgd(pgd)); |
---|
| 167 | |
---|
| 168 | if (!pte_write(*ptep)) { |
---|
| 169 | xen_pgd_unpin(__pa(__user_pgd(pgd))); |
---|
| 170 | BUG_ON(HYPERVISOR_update_va_mapping( |
---|
| 171 | (unsigned long)__user_pgd(pgd), |
---|
| 172 | pfn_pte(virt_to_phys(__user_pgd(pgd))>>PAGE_SHIFT, |
---|
| 173 | PAGE_KERNEL), |
---|
| 174 | 0)); |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | pgd_list_del(pgd); |
---|
| 178 | free_pages((unsigned long)pgd, 1); |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | static inline pte_t *pte_alloc_one_kernel(struct mm_struct *mm, unsigned long address) |
---|
| 182 | { |
---|
| 183 | pte_t *pte = (pte_t *)get_zeroed_page(GFP_KERNEL|__GFP_REPEAT); |
---|
| 184 | if (pte) |
---|
| 185 | make_page_readonly(pte, XENFEAT_writable_page_tables); |
---|
| 186 | |
---|
| 187 | return pte; |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | /* Should really implement gc for free page table pages. This could be |
---|
| 191 | done with a reference count in struct page. */ |
---|
| 192 | |
---|
| 193 | static inline void pte_free_kernel(pte_t *pte) |
---|
| 194 | { |
---|
| 195 | BUG_ON((unsigned long)pte & (PAGE_SIZE-1)); |
---|
| 196 | make_page_writable(pte, XENFEAT_writable_page_tables); |
---|
| 197 | free_page((unsigned long)pte); |
---|
| 198 | } |
---|
| 199 | |
---|
| 200 | #define __pte_free_tlb(tlb,pte) tlb_remove_page((tlb),(pte)) |
---|
| 201 | #define __pmd_free_tlb(tlb,x) tlb_remove_page((tlb),virt_to_page(x)) |
---|
| 202 | #define __pud_free_tlb(tlb,x) tlb_remove_page((tlb),virt_to_page(x)) |
---|
| 203 | |
---|
| 204 | #endif /* _X86_64_PGALLOC_H */ |
---|