Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Xen mmu operations |
| 3 | * |
| 4 | * This file contains the various mmu fetch and update operations. |
| 5 | * The most important job they must perform is the mapping between the |
| 6 | * domain's pfn and the overall machine mfns. |
| 7 | * |
| 8 | * Xen allows guests to directly update the pagetable, in a controlled |
| 9 | * fashion. In other words, the guest modifies the same pagetable |
| 10 | * that the CPU actually uses, which eliminates the overhead of having |
| 11 | * a separate shadow pagetable. |
| 12 | * |
| 13 | * In order to allow this, it falls on the guest domain to map its |
| 14 | * notion of a "physical" pfn - which is just a domain-local linear |
| 15 | * address - into a real "machine address" which the CPU's MMU can |
| 16 | * use. |
| 17 | * |
| 18 | * A pgd_t/pmd_t/pte_t will typically contain an mfn, and so can be |
| 19 | * inserted directly into the pagetable. When creating a new |
| 20 | * pte/pmd/pgd, it converts the passed pfn into an mfn. Conversely, |
| 21 | * when reading the content back with __(pgd|pmd|pte)_val, it converts |
| 22 | * the mfn back into a pfn. |
| 23 | * |
| 24 | * The other constraint is that all pages which make up a pagetable |
| 25 | * must be mapped read-only in the guest. This prevents uncontrolled |
| 26 | * guest updates to the pagetable. Xen strictly enforces this, and |
| 27 | * will disallow any pagetable update which will end up mapping a |
| 28 | * pagetable page RW, and will disallow using any writable page as a |
| 29 | * pagetable. |
| 30 | * |
| 31 | * Naively, when loading %cr3 with the base of a new pagetable, Xen |
| 32 | * would need to validate the whole pagetable before going on. |
| 33 | * Naturally, this is quite slow. The solution is to "pin" a |
| 34 | * pagetable, which enforces all the constraints on the pagetable even |
| 35 | * when it is not actively in use. This menas that Xen can be assured |
| 36 | * that it is still valid when you do load it into %cr3, and doesn't |
| 37 | * need to revalidate it. |
| 38 | * |
| 39 | * Jeremy Fitzhardinge <jeremy@xensource.com>, XenSource Inc, 2007 |
| 40 | */ |
Jeremy Fitzhardinge | f120f13 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 41 | #include <linux/sched.h> |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 42 | #include <linux/highmem.h> |
Jeremy Fitzhardinge | 994025c | 2008-08-20 17:02:19 -0700 | [diff] [blame] | 43 | #include <linux/debugfs.h> |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 44 | #include <linux/bug.h> |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 45 | |
| 46 | #include <asm/pgtable.h> |
| 47 | #include <asm/tlbflush.h> |
Jeremy Fitzhardinge | 5deb30d | 2008-07-08 15:07:06 -0700 | [diff] [blame] | 48 | #include <asm/fixmap.h> |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 49 | #include <asm/mmu_context.h> |
Jeremy Fitzhardinge | 319f3ba | 2009-01-28 14:35:01 -0800 | [diff] [blame] | 50 | #include <asm/setup.h> |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 51 | #include <asm/paravirt.h> |
Jeremy Fitzhardinge | cbcd79c | 2008-07-08 15:06:27 -0700 | [diff] [blame] | 52 | #include <asm/linkage.h> |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 53 | |
| 54 | #include <asm/xen/hypercall.h> |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 55 | #include <asm/xen/hypervisor.h> |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 56 | |
| 57 | #include <xen/page.h> |
| 58 | #include <xen/interface/xen.h> |
Jeremy Fitzhardinge | 319f3ba | 2009-01-28 14:35:01 -0800 | [diff] [blame] | 59 | #include <xen/interface/version.h> |
| 60 | #include <xen/hvc-console.h> |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 61 | |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 62 | #include "multicalls.h" |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 63 | #include "mmu.h" |
Jeremy Fitzhardinge | 994025c | 2008-08-20 17:02:19 -0700 | [diff] [blame] | 64 | #include "debugfs.h" |
| 65 | |
| 66 | #define MMU_UPDATE_HISTO 30 |
| 67 | |
| 68 | #ifdef CONFIG_XEN_DEBUG_FS |
| 69 | |
| 70 | static struct { |
| 71 | u32 pgd_update; |
| 72 | u32 pgd_update_pinned; |
| 73 | u32 pgd_update_batched; |
| 74 | |
| 75 | u32 pud_update; |
| 76 | u32 pud_update_pinned; |
| 77 | u32 pud_update_batched; |
| 78 | |
| 79 | u32 pmd_update; |
| 80 | u32 pmd_update_pinned; |
| 81 | u32 pmd_update_batched; |
| 82 | |
| 83 | u32 pte_update; |
| 84 | u32 pte_update_pinned; |
| 85 | u32 pte_update_batched; |
| 86 | |
| 87 | u32 mmu_update; |
| 88 | u32 mmu_update_extended; |
| 89 | u32 mmu_update_histo[MMU_UPDATE_HISTO]; |
| 90 | |
| 91 | u32 prot_commit; |
| 92 | u32 prot_commit_batched; |
| 93 | |
| 94 | u32 set_pte_at; |
| 95 | u32 set_pte_at_batched; |
| 96 | u32 set_pte_at_pinned; |
| 97 | u32 set_pte_at_current; |
| 98 | u32 set_pte_at_kernel; |
| 99 | } mmu_stats; |
| 100 | |
| 101 | static u8 zero_stats; |
| 102 | |
| 103 | static inline void check_zero(void) |
| 104 | { |
| 105 | if (unlikely(zero_stats)) { |
| 106 | memset(&mmu_stats, 0, sizeof(mmu_stats)); |
| 107 | zero_stats = 0; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | #define ADD_STATS(elem, val) \ |
| 112 | do { check_zero(); mmu_stats.elem += (val); } while(0) |
| 113 | |
| 114 | #else /* !CONFIG_XEN_DEBUG_FS */ |
| 115 | |
| 116 | #define ADD_STATS(elem, val) do { (void)(val); } while(0) |
| 117 | |
| 118 | #endif /* CONFIG_XEN_DEBUG_FS */ |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 119 | |
Jeremy Fitzhardinge | 319f3ba | 2009-01-28 14:35:01 -0800 | [diff] [blame] | 120 | |
| 121 | /* |
| 122 | * Identity map, in addition to plain kernel map. This needs to be |
| 123 | * large enough to allocate page table pages to allocate the rest. |
| 124 | * Each page can map 2MB. |
| 125 | */ |
| 126 | static pte_t level1_ident_pgt[PTRS_PER_PTE * 4] __page_aligned_bss; |
| 127 | |
| 128 | #ifdef CONFIG_X86_64 |
| 129 | /* l3 pud for userspace vsyscall mapping */ |
| 130 | static pud_t level3_user_vsyscall[PTRS_PER_PUD] __page_aligned_bss; |
| 131 | #endif /* CONFIG_X86_64 */ |
| 132 | |
| 133 | /* |
| 134 | * Note about cr3 (pagetable base) values: |
| 135 | * |
| 136 | * xen_cr3 contains the current logical cr3 value; it contains the |
| 137 | * last set cr3. This may not be the current effective cr3, because |
| 138 | * its update may be being lazily deferred. However, a vcpu looking |
| 139 | * at its own cr3 can use this value knowing that it everything will |
| 140 | * be self-consistent. |
| 141 | * |
| 142 | * xen_current_cr3 contains the actual vcpu cr3; it is set once the |
| 143 | * hypercall to set the vcpu cr3 is complete (so it may be a little |
| 144 | * out of date, but it will never be set early). If one vcpu is |
| 145 | * looking at another vcpu's cr3 value, it should use this variable. |
| 146 | */ |
| 147 | DEFINE_PER_CPU(unsigned long, xen_cr3); /* cr3 stored as physaddr */ |
| 148 | DEFINE_PER_CPU(unsigned long, xen_current_cr3); /* actual vcpu cr3 */ |
| 149 | |
| 150 | |
Jeremy Fitzhardinge | d6182fb | 2008-07-08 15:07:13 -0700 | [diff] [blame] | 151 | /* |
| 152 | * Just beyond the highest usermode address. STACK_TOP_MAX has a |
| 153 | * redzone above it, so round it up to a PGD boundary. |
| 154 | */ |
| 155 | #define USER_LIMIT ((STACK_TOP_MAX + PGDIR_SIZE - 1) & PGDIR_MASK) |
| 156 | |
| 157 | |
Jeremy Fitzhardinge | d451bb7 | 2008-05-26 23:31:18 +0100 | [diff] [blame] | 158 | #define P2M_ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(unsigned long)) |
Jeremy Fitzhardinge | cf0923e | 2008-05-26 23:31:20 +0100 | [diff] [blame] | 159 | #define TOP_ENTRIES (MAX_DOMAIN_PAGES / P2M_ENTRIES_PER_PAGE) |
Jeremy Fitzhardinge | d451bb7 | 2008-05-26 23:31:18 +0100 | [diff] [blame] | 160 | |
Jeremy Fitzhardinge | cf0923e | 2008-05-26 23:31:20 +0100 | [diff] [blame] | 161 | /* Placeholder for holes in the address space */ |
Jeremy Fitzhardinge | cbcd79c | 2008-07-08 15:06:27 -0700 | [diff] [blame] | 162 | static unsigned long p2m_missing[P2M_ENTRIES_PER_PAGE] __page_aligned_data = |
Jeremy Fitzhardinge | cf0923e | 2008-05-26 23:31:20 +0100 | [diff] [blame] | 163 | { [ 0 ... P2M_ENTRIES_PER_PAGE-1 ] = ~0UL }; |
| 164 | |
| 165 | /* Array of pointers to pages containing p2m entries */ |
Jeremy Fitzhardinge | cbcd79c | 2008-07-08 15:06:27 -0700 | [diff] [blame] | 166 | static unsigned long *p2m_top[TOP_ENTRIES] __page_aligned_data = |
Jeremy Fitzhardinge | cf0923e | 2008-05-26 23:31:20 +0100 | [diff] [blame] | 167 | { [ 0 ... TOP_ENTRIES - 1] = &p2m_missing[0] }; |
Jeremy Fitzhardinge | d451bb7 | 2008-05-26 23:31:18 +0100 | [diff] [blame] | 168 | |
Jeremy Fitzhardinge | d5edbc1 | 2008-05-26 23:31:22 +0100 | [diff] [blame] | 169 | /* Arrays of p2m arrays expressed in mfns used for save/restore */ |
Jeremy Fitzhardinge | cbcd79c | 2008-07-08 15:06:27 -0700 | [diff] [blame] | 170 | static unsigned long p2m_top_mfn[TOP_ENTRIES] __page_aligned_bss; |
Jeremy Fitzhardinge | d5edbc1 | 2008-05-26 23:31:22 +0100 | [diff] [blame] | 171 | |
Jeremy Fitzhardinge | cbcd79c | 2008-07-08 15:06:27 -0700 | [diff] [blame] | 172 | static unsigned long p2m_top_mfn_list[TOP_ENTRIES / P2M_ENTRIES_PER_PAGE] |
| 173 | __page_aligned_bss; |
Jeremy Fitzhardinge | d5edbc1 | 2008-05-26 23:31:22 +0100 | [diff] [blame] | 174 | |
Jeremy Fitzhardinge | d451bb7 | 2008-05-26 23:31:18 +0100 | [diff] [blame] | 175 | static inline unsigned p2m_top_index(unsigned long pfn) |
| 176 | { |
Jeremy Fitzhardinge | 8006ec3 | 2008-05-26 23:31:19 +0100 | [diff] [blame] | 177 | BUG_ON(pfn >= MAX_DOMAIN_PAGES); |
Jeremy Fitzhardinge | d451bb7 | 2008-05-26 23:31:18 +0100 | [diff] [blame] | 178 | return pfn / P2M_ENTRIES_PER_PAGE; |
| 179 | } |
| 180 | |
| 181 | static inline unsigned p2m_index(unsigned long pfn) |
| 182 | { |
| 183 | return pfn % P2M_ENTRIES_PER_PAGE; |
| 184 | } |
| 185 | |
Jeremy Fitzhardinge | d5edbc1 | 2008-05-26 23:31:22 +0100 | [diff] [blame] | 186 | /* Build the parallel p2m_top_mfn structures */ |
| 187 | void xen_setup_mfn_list_list(void) |
| 188 | { |
| 189 | unsigned pfn, idx; |
| 190 | |
Tej | f63c2f2 | 2008-12-16 11:56:06 -0800 | [diff] [blame] | 191 | for (pfn = 0; pfn < MAX_DOMAIN_PAGES; pfn += P2M_ENTRIES_PER_PAGE) { |
Jeremy Fitzhardinge | d5edbc1 | 2008-05-26 23:31:22 +0100 | [diff] [blame] | 192 | unsigned topidx = p2m_top_index(pfn); |
| 193 | |
| 194 | p2m_top_mfn[topidx] = virt_to_mfn(p2m_top[topidx]); |
| 195 | } |
| 196 | |
Tej | f63c2f2 | 2008-12-16 11:56:06 -0800 | [diff] [blame] | 197 | for (idx = 0; idx < ARRAY_SIZE(p2m_top_mfn_list); idx++) { |
Jeremy Fitzhardinge | d5edbc1 | 2008-05-26 23:31:22 +0100 | [diff] [blame] | 198 | unsigned topidx = idx * P2M_ENTRIES_PER_PAGE; |
| 199 | p2m_top_mfn_list[idx] = virt_to_mfn(&p2m_top_mfn[topidx]); |
| 200 | } |
| 201 | |
| 202 | BUG_ON(HYPERVISOR_shared_info == &xen_dummy_shared_info); |
| 203 | |
| 204 | HYPERVISOR_shared_info->arch.pfn_to_mfn_frame_list_list = |
| 205 | virt_to_mfn(p2m_top_mfn_list); |
| 206 | HYPERVISOR_shared_info->arch.max_pfn = xen_start_info->nr_pages; |
| 207 | } |
| 208 | |
| 209 | /* Set up p2m_top to point to the domain-builder provided p2m pages */ |
Jeremy Fitzhardinge | d451bb7 | 2008-05-26 23:31:18 +0100 | [diff] [blame] | 210 | void __init xen_build_dynamic_phys_to_machine(void) |
| 211 | { |
Jeremy Fitzhardinge | d451bb7 | 2008-05-26 23:31:18 +0100 | [diff] [blame] | 212 | unsigned long *mfn_list = (unsigned long *)xen_start_info->mfn_list; |
Jeremy Fitzhardinge | 8006ec3 | 2008-05-26 23:31:19 +0100 | [diff] [blame] | 213 | unsigned long max_pfn = min(MAX_DOMAIN_PAGES, xen_start_info->nr_pages); |
Jeremy Fitzhardinge | d5edbc1 | 2008-05-26 23:31:22 +0100 | [diff] [blame] | 214 | unsigned pfn; |
Jeremy Fitzhardinge | d451bb7 | 2008-05-26 23:31:18 +0100 | [diff] [blame] | 215 | |
Tej | f63c2f2 | 2008-12-16 11:56:06 -0800 | [diff] [blame] | 216 | for (pfn = 0; pfn < max_pfn; pfn += P2M_ENTRIES_PER_PAGE) { |
Jeremy Fitzhardinge | d451bb7 | 2008-05-26 23:31:18 +0100 | [diff] [blame] | 217 | unsigned topidx = p2m_top_index(pfn); |
| 218 | |
| 219 | p2m_top[topidx] = &mfn_list[pfn]; |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | unsigned long get_phys_to_machine(unsigned long pfn) |
| 224 | { |
| 225 | unsigned topidx, idx; |
| 226 | |
Jeremy Fitzhardinge | 8006ec3 | 2008-05-26 23:31:19 +0100 | [diff] [blame] | 227 | if (unlikely(pfn >= MAX_DOMAIN_PAGES)) |
| 228 | return INVALID_P2M_ENTRY; |
| 229 | |
Jeremy Fitzhardinge | d451bb7 | 2008-05-26 23:31:18 +0100 | [diff] [blame] | 230 | topidx = p2m_top_index(pfn); |
Jeremy Fitzhardinge | d451bb7 | 2008-05-26 23:31:18 +0100 | [diff] [blame] | 231 | idx = p2m_index(pfn); |
| 232 | return p2m_top[topidx][idx]; |
| 233 | } |
Ingo Molnar | 15ce6005 | 2008-06-02 13:20:11 +0200 | [diff] [blame] | 234 | EXPORT_SYMBOL_GPL(get_phys_to_machine); |
Jeremy Fitzhardinge | d451bb7 | 2008-05-26 23:31:18 +0100 | [diff] [blame] | 235 | |
Jeremy Fitzhardinge | d5edbc1 | 2008-05-26 23:31:22 +0100 | [diff] [blame] | 236 | static void alloc_p2m(unsigned long **pp, unsigned long *mfnp) |
Jeremy Fitzhardinge | d451bb7 | 2008-05-26 23:31:18 +0100 | [diff] [blame] | 237 | { |
| 238 | unsigned long *p; |
| 239 | unsigned i; |
| 240 | |
| 241 | p = (void *)__get_free_page(GFP_KERNEL | __GFP_NOFAIL); |
| 242 | BUG_ON(p == NULL); |
| 243 | |
Tej | f63c2f2 | 2008-12-16 11:56:06 -0800 | [diff] [blame] | 244 | for (i = 0; i < P2M_ENTRIES_PER_PAGE; i++) |
Jeremy Fitzhardinge | d451bb7 | 2008-05-26 23:31:18 +0100 | [diff] [blame] | 245 | p[i] = INVALID_P2M_ENTRY; |
| 246 | |
Jeremy Fitzhardinge | cf0923e | 2008-05-26 23:31:20 +0100 | [diff] [blame] | 247 | if (cmpxchg(pp, p2m_missing, p) != p2m_missing) |
Jeremy Fitzhardinge | d451bb7 | 2008-05-26 23:31:18 +0100 | [diff] [blame] | 248 | free_page((unsigned long)p); |
Jeremy Fitzhardinge | d5edbc1 | 2008-05-26 23:31:22 +0100 | [diff] [blame] | 249 | else |
| 250 | *mfnp = virt_to_mfn(p); |
Jeremy Fitzhardinge | d451bb7 | 2008-05-26 23:31:18 +0100 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | void set_phys_to_machine(unsigned long pfn, unsigned long mfn) |
| 254 | { |
| 255 | unsigned topidx, idx; |
| 256 | |
| 257 | if (unlikely(xen_feature(XENFEAT_auto_translated_physmap))) { |
| 258 | BUG_ON(pfn != mfn && mfn != INVALID_P2M_ENTRY); |
| 259 | return; |
| 260 | } |
| 261 | |
Jeremy Fitzhardinge | 8006ec3 | 2008-05-26 23:31:19 +0100 | [diff] [blame] | 262 | if (unlikely(pfn >= MAX_DOMAIN_PAGES)) { |
| 263 | BUG_ON(mfn != INVALID_P2M_ENTRY); |
| 264 | return; |
| 265 | } |
| 266 | |
Jeremy Fitzhardinge | d451bb7 | 2008-05-26 23:31:18 +0100 | [diff] [blame] | 267 | topidx = p2m_top_index(pfn); |
Jeremy Fitzhardinge | cf0923e | 2008-05-26 23:31:20 +0100 | [diff] [blame] | 268 | if (p2m_top[topidx] == p2m_missing) { |
Jeremy Fitzhardinge | d451bb7 | 2008-05-26 23:31:18 +0100 | [diff] [blame] | 269 | /* no need to allocate a page to store an invalid entry */ |
| 270 | if (mfn == INVALID_P2M_ENTRY) |
| 271 | return; |
Jeremy Fitzhardinge | d5edbc1 | 2008-05-26 23:31:22 +0100 | [diff] [blame] | 272 | alloc_p2m(&p2m_top[topidx], &p2m_top_mfn[topidx]); |
Jeremy Fitzhardinge | d451bb7 | 2008-05-26 23:31:18 +0100 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | idx = p2m_index(pfn); |
| 276 | p2m_top[topidx][idx] = mfn; |
| 277 | } |
| 278 | |
Jeremy Fitzhardinge | 9976b39 | 2009-02-27 09:19:26 -0800 | [diff] [blame^] | 279 | unsigned long arbitrary_virt_to_mfn(void *vaddr) |
| 280 | { |
| 281 | xmaddr_t maddr = arbitrary_virt_to_machine(vaddr); |
| 282 | |
| 283 | return PFN_DOWN(maddr.maddr); |
| 284 | } |
| 285 | |
Jeremy Fitzhardinge | ce803e7 | 2008-07-08 15:06:55 -0700 | [diff] [blame] | 286 | xmaddr_t arbitrary_virt_to_machine(void *vaddr) |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 287 | { |
Jeremy Fitzhardinge | ce803e7 | 2008-07-08 15:06:55 -0700 | [diff] [blame] | 288 | unsigned long address = (unsigned long)vaddr; |
Harvey Harrison | da7bfc5 | 2008-02-09 23:24:08 +0100 | [diff] [blame] | 289 | unsigned int level; |
Chris Lalancette | 9f32d21 | 2008-10-23 17:40:25 -0700 | [diff] [blame] | 290 | pte_t *pte; |
| 291 | unsigned offset; |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 292 | |
Chris Lalancette | 9f32d21 | 2008-10-23 17:40:25 -0700 | [diff] [blame] | 293 | /* |
| 294 | * if the PFN is in the linear mapped vaddr range, we can just use |
| 295 | * the (quick) virt_to_machine() p2m lookup |
| 296 | */ |
| 297 | if (virt_addr_valid(vaddr)) |
| 298 | return virt_to_machine(vaddr); |
| 299 | |
| 300 | /* otherwise we have to do a (slower) full page-table walk */ |
| 301 | |
| 302 | pte = lookup_address(address, &level); |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 303 | BUG_ON(pte == NULL); |
Chris Lalancette | 9f32d21 | 2008-10-23 17:40:25 -0700 | [diff] [blame] | 304 | offset = address & ~PAGE_MASK; |
Jeremy Fitzhardinge | ebd879e | 2008-07-08 15:06:54 -0700 | [diff] [blame] | 305 | return XMADDR(((phys_addr_t)pte_mfn(*pte) << PAGE_SHIFT) + offset); |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | void make_lowmem_page_readonly(void *vaddr) |
| 309 | { |
| 310 | pte_t *pte, ptev; |
| 311 | unsigned long address = (unsigned long)vaddr; |
Harvey Harrison | da7bfc5 | 2008-02-09 23:24:08 +0100 | [diff] [blame] | 312 | unsigned int level; |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 313 | |
Ingo Molnar | f0646e4 | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 314 | pte = lookup_address(address, &level); |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 315 | BUG_ON(pte == NULL); |
| 316 | |
| 317 | ptev = pte_wrprotect(*pte); |
| 318 | |
| 319 | if (HYPERVISOR_update_va_mapping(address, ptev, 0)) |
| 320 | BUG(); |
| 321 | } |
| 322 | |
| 323 | void make_lowmem_page_readwrite(void *vaddr) |
| 324 | { |
| 325 | pte_t *pte, ptev; |
| 326 | unsigned long address = (unsigned long)vaddr; |
Harvey Harrison | da7bfc5 | 2008-02-09 23:24:08 +0100 | [diff] [blame] | 327 | unsigned int level; |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 328 | |
Ingo Molnar | f0646e4 | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 329 | pte = lookup_address(address, &level); |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 330 | BUG_ON(pte == NULL); |
| 331 | |
| 332 | ptev = pte_mkwrite(*pte); |
| 333 | |
| 334 | if (HYPERVISOR_update_va_mapping(address, ptev, 0)) |
| 335 | BUG(); |
| 336 | } |
| 337 | |
| 338 | |
Jeremy Fitzhardinge | 7708ad6 | 2008-08-19 13:34:22 -0700 | [diff] [blame] | 339 | static bool xen_page_pinned(void *ptr) |
Jeremy Fitzhardinge | e2426cf | 2008-05-31 01:24:27 +0100 | [diff] [blame] | 340 | { |
| 341 | struct page *page = virt_to_page(ptr); |
| 342 | |
| 343 | return PagePinned(page); |
| 344 | } |
| 345 | |
Jeremy Fitzhardinge | 7708ad6 | 2008-08-19 13:34:22 -0700 | [diff] [blame] | 346 | static void xen_extend_mmu_update(const struct mmu_update *update) |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 347 | { |
Jeremy Fitzhardinge | d66bf8f | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 348 | struct multicall_space mcs; |
| 349 | struct mmu_update *u; |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 350 | |
Jeremy Fitzhardinge | 400d349 | 2008-06-16 04:30:03 -0700 | [diff] [blame] | 351 | mcs = xen_mc_extend_args(__HYPERVISOR_mmu_update, sizeof(*u)); |
| 352 | |
Jeremy Fitzhardinge | 994025c | 2008-08-20 17:02:19 -0700 | [diff] [blame] | 353 | if (mcs.mc != NULL) { |
| 354 | ADD_STATS(mmu_update_extended, 1); |
| 355 | ADD_STATS(mmu_update_histo[mcs.mc->args[1]], -1); |
| 356 | |
Jeremy Fitzhardinge | 400d349 | 2008-06-16 04:30:03 -0700 | [diff] [blame] | 357 | mcs.mc->args[1]++; |
Jeremy Fitzhardinge | 994025c | 2008-08-20 17:02:19 -0700 | [diff] [blame] | 358 | |
| 359 | if (mcs.mc->args[1] < MMU_UPDATE_HISTO) |
| 360 | ADD_STATS(mmu_update_histo[mcs.mc->args[1]], 1); |
| 361 | else |
| 362 | ADD_STATS(mmu_update_histo[0], 1); |
| 363 | } else { |
| 364 | ADD_STATS(mmu_update, 1); |
Jeremy Fitzhardinge | 400d349 | 2008-06-16 04:30:03 -0700 | [diff] [blame] | 365 | mcs = __xen_mc_entry(sizeof(*u)); |
| 366 | MULTI_mmu_update(mcs.mc, mcs.args, 1, NULL, DOMID_SELF); |
Jeremy Fitzhardinge | 994025c | 2008-08-20 17:02:19 -0700 | [diff] [blame] | 367 | ADD_STATS(mmu_update_histo[1], 1); |
Jeremy Fitzhardinge | 400d349 | 2008-06-16 04:30:03 -0700 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | u = mcs.args; |
| 371 | *u = *update; |
| 372 | } |
| 373 | |
| 374 | void xen_set_pmd_hyper(pmd_t *ptr, pmd_t val) |
| 375 | { |
| 376 | struct mmu_update u; |
| 377 | |
Jeremy Fitzhardinge | d66bf8f | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 378 | preempt_disable(); |
| 379 | |
Jeremy Fitzhardinge | 400d349 | 2008-06-16 04:30:03 -0700 | [diff] [blame] | 380 | xen_mc_batch(); |
| 381 | |
Jeremy Fitzhardinge | ce803e7 | 2008-07-08 15:06:55 -0700 | [diff] [blame] | 382 | /* ptr may be ioremapped for 64-bit pagetable setup */ |
| 383 | u.ptr = arbitrary_virt_to_machine(ptr).maddr; |
Jeremy Fitzhardinge | 400d349 | 2008-06-16 04:30:03 -0700 | [diff] [blame] | 384 | u.val = pmd_val_ma(val); |
Jeremy Fitzhardinge | 7708ad6 | 2008-08-19 13:34:22 -0700 | [diff] [blame] | 385 | xen_extend_mmu_update(&u); |
Jeremy Fitzhardinge | d66bf8f | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 386 | |
Jeremy Fitzhardinge | 994025c | 2008-08-20 17:02:19 -0700 | [diff] [blame] | 387 | ADD_STATS(pmd_update_batched, paravirt_get_lazy_mode() == PARAVIRT_LAZY_MMU); |
| 388 | |
Jeremy Fitzhardinge | d66bf8f | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 389 | xen_mc_issue(PARAVIRT_LAZY_MMU); |
| 390 | |
| 391 | preempt_enable(); |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 392 | } |
| 393 | |
Jeremy Fitzhardinge | e2426cf | 2008-05-31 01:24:27 +0100 | [diff] [blame] | 394 | void xen_set_pmd(pmd_t *ptr, pmd_t val) |
| 395 | { |
Jeremy Fitzhardinge | 994025c | 2008-08-20 17:02:19 -0700 | [diff] [blame] | 396 | ADD_STATS(pmd_update, 1); |
| 397 | |
Jeremy Fitzhardinge | e2426cf | 2008-05-31 01:24:27 +0100 | [diff] [blame] | 398 | /* If page is not pinned, we can just update the entry |
| 399 | directly */ |
Jeremy Fitzhardinge | 7708ad6 | 2008-08-19 13:34:22 -0700 | [diff] [blame] | 400 | if (!xen_page_pinned(ptr)) { |
Jeremy Fitzhardinge | e2426cf | 2008-05-31 01:24:27 +0100 | [diff] [blame] | 401 | *ptr = val; |
| 402 | return; |
| 403 | } |
| 404 | |
Jeremy Fitzhardinge | 994025c | 2008-08-20 17:02:19 -0700 | [diff] [blame] | 405 | ADD_STATS(pmd_update_pinned, 1); |
| 406 | |
Jeremy Fitzhardinge | e2426cf | 2008-05-31 01:24:27 +0100 | [diff] [blame] | 407 | xen_set_pmd_hyper(ptr, val); |
| 408 | } |
| 409 | |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 410 | /* |
| 411 | * Associate a virtual page frame with a given physical page frame |
| 412 | * and protection flags for that frame. |
| 413 | */ |
| 414 | void set_pte_mfn(unsigned long vaddr, unsigned long mfn, pgprot_t flags) |
| 415 | { |
Jeremy Fitzhardinge | 836fe2f | 2008-07-08 15:06:58 -0700 | [diff] [blame] | 416 | set_pte_vaddr(vaddr, mfn_pte(mfn, flags)); |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | void xen_set_pte_at(struct mm_struct *mm, unsigned long addr, |
| 420 | pte_t *ptep, pte_t pteval) |
| 421 | { |
Jeremy Fitzhardinge | 2bd5003 | 2008-04-02 10:54:10 -0700 | [diff] [blame] | 422 | /* updates to init_mm may be done without lock */ |
| 423 | if (mm == &init_mm) |
| 424 | preempt_disable(); |
| 425 | |
Jeremy Fitzhardinge | 994025c | 2008-08-20 17:02:19 -0700 | [diff] [blame] | 426 | ADD_STATS(set_pte_at, 1); |
| 427 | // ADD_STATS(set_pte_at_pinned, xen_page_pinned(ptep)); |
| 428 | ADD_STATS(set_pte_at_current, mm == current->mm); |
| 429 | ADD_STATS(set_pte_at_kernel, mm == &init_mm); |
| 430 | |
Jeremy Fitzhardinge | d66bf8f | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 431 | if (mm == current->mm || mm == &init_mm) { |
Jeremy Fitzhardinge | 8965c1c | 2007-10-16 11:51:29 -0700 | [diff] [blame] | 432 | if (paravirt_get_lazy_mode() == PARAVIRT_LAZY_MMU) { |
Jeremy Fitzhardinge | d66bf8f | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 433 | struct multicall_space mcs; |
| 434 | mcs = xen_mc_entry(0); |
| 435 | |
| 436 | MULTI_update_va_mapping(mcs.mc, addr, pteval, 0); |
Jeremy Fitzhardinge | 994025c | 2008-08-20 17:02:19 -0700 | [diff] [blame] | 437 | ADD_STATS(set_pte_at_batched, 1); |
Jeremy Fitzhardinge | d66bf8f | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 438 | xen_mc_issue(PARAVIRT_LAZY_MMU); |
Jeremy Fitzhardinge | 2bd5003 | 2008-04-02 10:54:10 -0700 | [diff] [blame] | 439 | goto out; |
Jeremy Fitzhardinge | d66bf8f | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 440 | } else |
| 441 | if (HYPERVISOR_update_va_mapping(addr, pteval, 0) == 0) |
Jeremy Fitzhardinge | 2bd5003 | 2008-04-02 10:54:10 -0700 | [diff] [blame] | 442 | goto out; |
Jeremy Fitzhardinge | d66bf8f | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 443 | } |
| 444 | xen_set_pte(ptep, pteval); |
Jeremy Fitzhardinge | 2bd5003 | 2008-04-02 10:54:10 -0700 | [diff] [blame] | 445 | |
| 446 | out: |
| 447 | if (mm == &init_mm) |
| 448 | preempt_enable(); |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 449 | } |
| 450 | |
Tej | f63c2f2 | 2008-12-16 11:56:06 -0800 | [diff] [blame] | 451 | pte_t xen_ptep_modify_prot_start(struct mm_struct *mm, |
| 452 | unsigned long addr, pte_t *ptep) |
Jeremy Fitzhardinge | e57778a | 2008-06-16 04:30:02 -0700 | [diff] [blame] | 453 | { |
| 454 | /* Just return the pte as-is. We preserve the bits on commit */ |
| 455 | return *ptep; |
| 456 | } |
| 457 | |
| 458 | void xen_ptep_modify_prot_commit(struct mm_struct *mm, unsigned long addr, |
| 459 | pte_t *ptep, pte_t pte) |
| 460 | { |
Jeremy Fitzhardinge | 400d349 | 2008-06-16 04:30:03 -0700 | [diff] [blame] | 461 | struct mmu_update u; |
Jeremy Fitzhardinge | e57778a | 2008-06-16 04:30:02 -0700 | [diff] [blame] | 462 | |
Jeremy Fitzhardinge | 400d349 | 2008-06-16 04:30:03 -0700 | [diff] [blame] | 463 | xen_mc_batch(); |
| 464 | |
Chris Lalancette | 9f32d21 | 2008-10-23 17:40:25 -0700 | [diff] [blame] | 465 | u.ptr = arbitrary_virt_to_machine(ptep).maddr | MMU_PT_UPDATE_PRESERVE_AD; |
Jeremy Fitzhardinge | 400d349 | 2008-06-16 04:30:03 -0700 | [diff] [blame] | 466 | u.val = pte_val_ma(pte); |
Jeremy Fitzhardinge | 7708ad6 | 2008-08-19 13:34:22 -0700 | [diff] [blame] | 467 | xen_extend_mmu_update(&u); |
Jeremy Fitzhardinge | e57778a | 2008-06-16 04:30:02 -0700 | [diff] [blame] | 468 | |
Jeremy Fitzhardinge | 994025c | 2008-08-20 17:02:19 -0700 | [diff] [blame] | 469 | ADD_STATS(prot_commit, 1); |
| 470 | ADD_STATS(prot_commit_batched, paravirt_get_lazy_mode() == PARAVIRT_LAZY_MMU); |
| 471 | |
Jeremy Fitzhardinge | e57778a | 2008-06-16 04:30:02 -0700 | [diff] [blame] | 472 | xen_mc_issue(PARAVIRT_LAZY_MMU); |
| 473 | } |
| 474 | |
Jeremy Fitzhardinge | ebb9cfe | 2008-06-16 15:01:56 -0700 | [diff] [blame] | 475 | /* Assume pteval_t is equivalent to all the other *val_t types. */ |
| 476 | static pteval_t pte_mfn_to_pfn(pteval_t val) |
| 477 | { |
| 478 | if (val & _PAGE_PRESENT) { |
Jeremy Fitzhardinge | 59438c9 | 2008-07-21 22:59:42 -0700 | [diff] [blame] | 479 | unsigned long mfn = (val & PTE_PFN_MASK) >> PAGE_SHIFT; |
Jeremy Fitzhardinge | 77be1fa | 2008-07-21 22:59:56 -0700 | [diff] [blame] | 480 | pteval_t flags = val & PTE_FLAGS_MASK; |
Jeremy Fitzhardinge | d8355ac | 2008-07-03 22:10:18 -0700 | [diff] [blame] | 481 | val = ((pteval_t)mfn_to_pfn(mfn) << PAGE_SHIFT) | flags; |
Jeremy Fitzhardinge | ebb9cfe | 2008-06-16 15:01:56 -0700 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | return val; |
| 485 | } |
| 486 | |
| 487 | static pteval_t pte_pfn_to_mfn(pteval_t val) |
| 488 | { |
| 489 | if (val & _PAGE_PRESENT) { |
Jeremy Fitzhardinge | 59438c9 | 2008-07-21 22:59:42 -0700 | [diff] [blame] | 490 | unsigned long pfn = (val & PTE_PFN_MASK) >> PAGE_SHIFT; |
Jeremy Fitzhardinge | 77be1fa | 2008-07-21 22:59:56 -0700 | [diff] [blame] | 491 | pteval_t flags = val & PTE_FLAGS_MASK; |
Jeremy Fitzhardinge | d8355ac | 2008-07-03 22:10:18 -0700 | [diff] [blame] | 492 | val = ((pteval_t)pfn_to_mfn(pfn) << PAGE_SHIFT) | flags; |
Jeremy Fitzhardinge | ebb9cfe | 2008-06-16 15:01:56 -0700 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | return val; |
| 496 | } |
| 497 | |
Jeremy Fitzhardinge | 947a69c | 2008-03-17 16:37:09 -0700 | [diff] [blame] | 498 | pteval_t xen_pte_val(pte_t pte) |
| 499 | { |
Jeremy Fitzhardinge | ebb9cfe | 2008-06-16 15:01:56 -0700 | [diff] [blame] | 500 | return pte_mfn_to_pfn(pte.pte); |
Jeremy Fitzhardinge | 947a69c | 2008-03-17 16:37:09 -0700 | [diff] [blame] | 501 | } |
Jeremy Fitzhardinge | da5de7c | 2009-01-28 14:35:07 -0800 | [diff] [blame] | 502 | PV_CALLEE_SAVE_REGS_THUNK(xen_pte_val); |
Jeremy Fitzhardinge | 947a69c | 2008-03-17 16:37:09 -0700 | [diff] [blame] | 503 | |
| 504 | pgdval_t xen_pgd_val(pgd_t pgd) |
| 505 | { |
Jeremy Fitzhardinge | ebb9cfe | 2008-06-16 15:01:56 -0700 | [diff] [blame] | 506 | return pte_mfn_to_pfn(pgd.pgd); |
Jeremy Fitzhardinge | 947a69c | 2008-03-17 16:37:09 -0700 | [diff] [blame] | 507 | } |
Jeremy Fitzhardinge | da5de7c | 2009-01-28 14:35:07 -0800 | [diff] [blame] | 508 | PV_CALLEE_SAVE_REGS_THUNK(xen_pgd_val); |
Jeremy Fitzhardinge | 947a69c | 2008-03-17 16:37:09 -0700 | [diff] [blame] | 509 | |
| 510 | pte_t xen_make_pte(pteval_t pte) |
| 511 | { |
Jeremy Fitzhardinge | ebb9cfe | 2008-06-16 15:01:56 -0700 | [diff] [blame] | 512 | pte = pte_pfn_to_mfn(pte); |
| 513 | return native_make_pte(pte); |
Jeremy Fitzhardinge | 947a69c | 2008-03-17 16:37:09 -0700 | [diff] [blame] | 514 | } |
Jeremy Fitzhardinge | da5de7c | 2009-01-28 14:35:07 -0800 | [diff] [blame] | 515 | PV_CALLEE_SAVE_REGS_THUNK(xen_make_pte); |
Jeremy Fitzhardinge | 947a69c | 2008-03-17 16:37:09 -0700 | [diff] [blame] | 516 | |
| 517 | pgd_t xen_make_pgd(pgdval_t pgd) |
| 518 | { |
Jeremy Fitzhardinge | ebb9cfe | 2008-06-16 15:01:56 -0700 | [diff] [blame] | 519 | pgd = pte_pfn_to_mfn(pgd); |
| 520 | return native_make_pgd(pgd); |
Jeremy Fitzhardinge | 947a69c | 2008-03-17 16:37:09 -0700 | [diff] [blame] | 521 | } |
Jeremy Fitzhardinge | da5de7c | 2009-01-28 14:35:07 -0800 | [diff] [blame] | 522 | PV_CALLEE_SAVE_REGS_THUNK(xen_make_pgd); |
Jeremy Fitzhardinge | 947a69c | 2008-03-17 16:37:09 -0700 | [diff] [blame] | 523 | |
| 524 | pmdval_t xen_pmd_val(pmd_t pmd) |
| 525 | { |
Jeremy Fitzhardinge | ebb9cfe | 2008-06-16 15:01:56 -0700 | [diff] [blame] | 526 | return pte_mfn_to_pfn(pmd.pmd); |
Jeremy Fitzhardinge | 947a69c | 2008-03-17 16:37:09 -0700 | [diff] [blame] | 527 | } |
Jeremy Fitzhardinge | da5de7c | 2009-01-28 14:35:07 -0800 | [diff] [blame] | 528 | PV_CALLEE_SAVE_REGS_THUNK(xen_pmd_val); |
Jeremy Fitzhardinge | 2849914 | 2008-05-09 12:05:57 +0100 | [diff] [blame] | 529 | |
Jeremy Fitzhardinge | e2426cf | 2008-05-31 01:24:27 +0100 | [diff] [blame] | 530 | void xen_set_pud_hyper(pud_t *ptr, pud_t val) |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 531 | { |
Jeremy Fitzhardinge | 400d349 | 2008-06-16 04:30:03 -0700 | [diff] [blame] | 532 | struct mmu_update u; |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 533 | |
Jeremy Fitzhardinge | d66bf8f | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 534 | preempt_disable(); |
| 535 | |
Jeremy Fitzhardinge | 400d349 | 2008-06-16 04:30:03 -0700 | [diff] [blame] | 536 | xen_mc_batch(); |
| 537 | |
Jeremy Fitzhardinge | ce803e7 | 2008-07-08 15:06:55 -0700 | [diff] [blame] | 538 | /* ptr may be ioremapped for 64-bit pagetable setup */ |
| 539 | u.ptr = arbitrary_virt_to_machine(ptr).maddr; |
Jeremy Fitzhardinge | 400d349 | 2008-06-16 04:30:03 -0700 | [diff] [blame] | 540 | u.val = pud_val_ma(val); |
Jeremy Fitzhardinge | 7708ad6 | 2008-08-19 13:34:22 -0700 | [diff] [blame] | 541 | xen_extend_mmu_update(&u); |
Jeremy Fitzhardinge | d66bf8f | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 542 | |
Jeremy Fitzhardinge | 994025c | 2008-08-20 17:02:19 -0700 | [diff] [blame] | 543 | ADD_STATS(pud_update_batched, paravirt_get_lazy_mode() == PARAVIRT_LAZY_MMU); |
| 544 | |
Jeremy Fitzhardinge | d66bf8f | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 545 | xen_mc_issue(PARAVIRT_LAZY_MMU); |
| 546 | |
| 547 | preempt_enable(); |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 548 | } |
| 549 | |
Jeremy Fitzhardinge | e2426cf | 2008-05-31 01:24:27 +0100 | [diff] [blame] | 550 | void xen_set_pud(pud_t *ptr, pud_t val) |
| 551 | { |
Jeremy Fitzhardinge | 994025c | 2008-08-20 17:02:19 -0700 | [diff] [blame] | 552 | ADD_STATS(pud_update, 1); |
| 553 | |
Jeremy Fitzhardinge | e2426cf | 2008-05-31 01:24:27 +0100 | [diff] [blame] | 554 | /* If page is not pinned, we can just update the entry |
| 555 | directly */ |
Jeremy Fitzhardinge | 7708ad6 | 2008-08-19 13:34:22 -0700 | [diff] [blame] | 556 | if (!xen_page_pinned(ptr)) { |
Jeremy Fitzhardinge | e2426cf | 2008-05-31 01:24:27 +0100 | [diff] [blame] | 557 | *ptr = val; |
| 558 | return; |
| 559 | } |
| 560 | |
Jeremy Fitzhardinge | 994025c | 2008-08-20 17:02:19 -0700 | [diff] [blame] | 561 | ADD_STATS(pud_update_pinned, 1); |
| 562 | |
Jeremy Fitzhardinge | e2426cf | 2008-05-31 01:24:27 +0100 | [diff] [blame] | 563 | xen_set_pud_hyper(ptr, val); |
| 564 | } |
| 565 | |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 566 | void xen_set_pte(pte_t *ptep, pte_t pte) |
| 567 | { |
Jeremy Fitzhardinge | 994025c | 2008-08-20 17:02:19 -0700 | [diff] [blame] | 568 | ADD_STATS(pte_update, 1); |
| 569 | // ADD_STATS(pte_update_pinned, xen_page_pinned(ptep)); |
| 570 | ADD_STATS(pte_update_batched, paravirt_get_lazy_mode() == PARAVIRT_LAZY_MMU); |
| 571 | |
Jeremy Fitzhardinge | f6e5873 | 2008-07-08 15:06:38 -0700 | [diff] [blame] | 572 | #ifdef CONFIG_X86_PAE |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 573 | ptep->pte_high = pte.pte_high; |
| 574 | smp_wmb(); |
| 575 | ptep->pte_low = pte.pte_low; |
Jeremy Fitzhardinge | f6e5873 | 2008-07-08 15:06:38 -0700 | [diff] [blame] | 576 | #else |
| 577 | *ptep = pte; |
| 578 | #endif |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 579 | } |
| 580 | |
Jeremy Fitzhardinge | f6e5873 | 2008-07-08 15:06:38 -0700 | [diff] [blame] | 581 | #ifdef CONFIG_X86_PAE |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 582 | void xen_set_pte_atomic(pte_t *ptep, pte_t pte) |
| 583 | { |
Jeremy Fitzhardinge | f6e5873 | 2008-07-08 15:06:38 -0700 | [diff] [blame] | 584 | set_64bit((u64 *)ptep, native_pte_val(pte)); |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | void xen_pte_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep) |
| 588 | { |
| 589 | ptep->pte_low = 0; |
| 590 | smp_wmb(); /* make sure low gets written first */ |
| 591 | ptep->pte_high = 0; |
| 592 | } |
| 593 | |
| 594 | void xen_pmd_clear(pmd_t *pmdp) |
| 595 | { |
Jeremy Fitzhardinge | e2426cf | 2008-05-31 01:24:27 +0100 | [diff] [blame] | 596 | set_pmd(pmdp, __pmd(0)); |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 597 | } |
Jeremy Fitzhardinge | f6e5873 | 2008-07-08 15:06:38 -0700 | [diff] [blame] | 598 | #endif /* CONFIG_X86_PAE */ |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 599 | |
Jeremy Fitzhardinge | abf3303 | 2008-03-17 16:37:07 -0700 | [diff] [blame] | 600 | pmd_t xen_make_pmd(pmdval_t pmd) |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 601 | { |
Jeremy Fitzhardinge | ebb9cfe | 2008-06-16 15:01:56 -0700 | [diff] [blame] | 602 | pmd = pte_pfn_to_mfn(pmd); |
Jeremy Fitzhardinge | 947a69c | 2008-03-17 16:37:09 -0700 | [diff] [blame] | 603 | return native_make_pmd(pmd); |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 604 | } |
Jeremy Fitzhardinge | da5de7c | 2009-01-28 14:35:07 -0800 | [diff] [blame] | 605 | PV_CALLEE_SAVE_REGS_THUNK(xen_make_pmd); |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 606 | |
Jeremy Fitzhardinge | f6e5873 | 2008-07-08 15:06:38 -0700 | [diff] [blame] | 607 | #if PAGETABLE_LEVELS == 4 |
| 608 | pudval_t xen_pud_val(pud_t pud) |
| 609 | { |
| 610 | return pte_mfn_to_pfn(pud.pud); |
| 611 | } |
Jeremy Fitzhardinge | da5de7c | 2009-01-28 14:35:07 -0800 | [diff] [blame] | 612 | PV_CALLEE_SAVE_REGS_THUNK(xen_pud_val); |
Jeremy Fitzhardinge | f6e5873 | 2008-07-08 15:06:38 -0700 | [diff] [blame] | 613 | |
| 614 | pud_t xen_make_pud(pudval_t pud) |
| 615 | { |
| 616 | pud = pte_pfn_to_mfn(pud); |
| 617 | |
| 618 | return native_make_pud(pud); |
| 619 | } |
Jeremy Fitzhardinge | da5de7c | 2009-01-28 14:35:07 -0800 | [diff] [blame] | 620 | PV_CALLEE_SAVE_REGS_THUNK(xen_make_pud); |
Jeremy Fitzhardinge | f6e5873 | 2008-07-08 15:06:38 -0700 | [diff] [blame] | 621 | |
Jeremy Fitzhardinge | d6182fb | 2008-07-08 15:07:13 -0700 | [diff] [blame] | 622 | pgd_t *xen_get_user_pgd(pgd_t *pgd) |
| 623 | { |
| 624 | pgd_t *pgd_page = (pgd_t *)(((unsigned long)pgd) & PAGE_MASK); |
| 625 | unsigned offset = pgd - pgd_page; |
| 626 | pgd_t *user_ptr = NULL; |
| 627 | |
| 628 | if (offset < pgd_index(USER_LIMIT)) { |
| 629 | struct page *page = virt_to_page(pgd_page); |
| 630 | user_ptr = (pgd_t *)page->private; |
| 631 | if (user_ptr) |
| 632 | user_ptr += offset; |
| 633 | } |
| 634 | |
| 635 | return user_ptr; |
| 636 | } |
| 637 | |
| 638 | static void __xen_set_pgd_hyper(pgd_t *ptr, pgd_t val) |
Jeremy Fitzhardinge | f6e5873 | 2008-07-08 15:06:38 -0700 | [diff] [blame] | 639 | { |
| 640 | struct mmu_update u; |
| 641 | |
Jeremy Fitzhardinge | f6e5873 | 2008-07-08 15:06:38 -0700 | [diff] [blame] | 642 | u.ptr = virt_to_machine(ptr).maddr; |
| 643 | u.val = pgd_val_ma(val); |
Jeremy Fitzhardinge | 7708ad6 | 2008-08-19 13:34:22 -0700 | [diff] [blame] | 644 | xen_extend_mmu_update(&u); |
Jeremy Fitzhardinge | d6182fb | 2008-07-08 15:07:13 -0700 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | /* |
| 648 | * Raw hypercall-based set_pgd, intended for in early boot before |
| 649 | * there's a page structure. This implies: |
| 650 | * 1. The only existing pagetable is the kernel's |
| 651 | * 2. It is always pinned |
| 652 | * 3. It has no user pagetable attached to it |
| 653 | */ |
| 654 | void __init xen_set_pgd_hyper(pgd_t *ptr, pgd_t val) |
| 655 | { |
| 656 | preempt_disable(); |
| 657 | |
| 658 | xen_mc_batch(); |
| 659 | |
| 660 | __xen_set_pgd_hyper(ptr, val); |
Jeremy Fitzhardinge | f6e5873 | 2008-07-08 15:06:38 -0700 | [diff] [blame] | 661 | |
| 662 | xen_mc_issue(PARAVIRT_LAZY_MMU); |
| 663 | |
| 664 | preempt_enable(); |
| 665 | } |
| 666 | |
| 667 | void xen_set_pgd(pgd_t *ptr, pgd_t val) |
| 668 | { |
Jeremy Fitzhardinge | d6182fb | 2008-07-08 15:07:13 -0700 | [diff] [blame] | 669 | pgd_t *user_ptr = xen_get_user_pgd(ptr); |
| 670 | |
Jeremy Fitzhardinge | 994025c | 2008-08-20 17:02:19 -0700 | [diff] [blame] | 671 | ADD_STATS(pgd_update, 1); |
| 672 | |
Jeremy Fitzhardinge | f6e5873 | 2008-07-08 15:06:38 -0700 | [diff] [blame] | 673 | /* If page is not pinned, we can just update the entry |
| 674 | directly */ |
Jeremy Fitzhardinge | 7708ad6 | 2008-08-19 13:34:22 -0700 | [diff] [blame] | 675 | if (!xen_page_pinned(ptr)) { |
Jeremy Fitzhardinge | f6e5873 | 2008-07-08 15:06:38 -0700 | [diff] [blame] | 676 | *ptr = val; |
Jeremy Fitzhardinge | d6182fb | 2008-07-08 15:07:13 -0700 | [diff] [blame] | 677 | if (user_ptr) { |
Jeremy Fitzhardinge | 7708ad6 | 2008-08-19 13:34:22 -0700 | [diff] [blame] | 678 | WARN_ON(xen_page_pinned(user_ptr)); |
Jeremy Fitzhardinge | d6182fb | 2008-07-08 15:07:13 -0700 | [diff] [blame] | 679 | *user_ptr = val; |
| 680 | } |
Jeremy Fitzhardinge | f6e5873 | 2008-07-08 15:06:38 -0700 | [diff] [blame] | 681 | return; |
| 682 | } |
| 683 | |
Jeremy Fitzhardinge | 994025c | 2008-08-20 17:02:19 -0700 | [diff] [blame] | 684 | ADD_STATS(pgd_update_pinned, 1); |
| 685 | ADD_STATS(pgd_update_batched, paravirt_get_lazy_mode() == PARAVIRT_LAZY_MMU); |
| 686 | |
Jeremy Fitzhardinge | d6182fb | 2008-07-08 15:07:13 -0700 | [diff] [blame] | 687 | /* If it's pinned, then we can at least batch the kernel and |
| 688 | user updates together. */ |
| 689 | xen_mc_batch(); |
| 690 | |
| 691 | __xen_set_pgd_hyper(ptr, val); |
| 692 | if (user_ptr) |
| 693 | __xen_set_pgd_hyper(user_ptr, val); |
| 694 | |
| 695 | xen_mc_issue(PARAVIRT_LAZY_MMU); |
Jeremy Fitzhardinge | f6e5873 | 2008-07-08 15:06:38 -0700 | [diff] [blame] | 696 | } |
| 697 | #endif /* PAGETABLE_LEVELS == 4 */ |
| 698 | |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 699 | /* |
Jeremy Fitzhardinge | 5deb30d | 2008-07-08 15:07:06 -0700 | [diff] [blame] | 700 | * (Yet another) pagetable walker. This one is intended for pinning a |
| 701 | * pagetable. This means that it walks a pagetable and calls the |
| 702 | * callback function on each page it finds making up the page table, |
| 703 | * at every level. It walks the entire pagetable, but it only bothers |
| 704 | * pinning pte pages which are below limit. In the normal case this |
| 705 | * will be STACK_TOP_MAX, but at boot we need to pin up to |
| 706 | * FIXADDR_TOP. |
| 707 | * |
| 708 | * For 32-bit the important bit is that we don't pin beyond there, |
| 709 | * because then we start getting into Xen's ptes. |
| 710 | * |
| 711 | * For 64-bit, we must skip the Xen hole in the middle of the address |
| 712 | * space, just after the big x86-64 virtual hole. |
| 713 | */ |
Ian Campbell | 86bbc2c | 2008-11-21 10:21:33 +0000 | [diff] [blame] | 714 | static int __xen_pgd_walk(struct mm_struct *mm, pgd_t *pgd, |
| 715 | int (*func)(struct mm_struct *mm, struct page *, |
| 716 | enum pt_level), |
| 717 | unsigned long limit) |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 718 | { |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 719 | int flush = 0; |
Jeremy Fitzhardinge | 5deb30d | 2008-07-08 15:07:06 -0700 | [diff] [blame] | 720 | unsigned hole_low, hole_high; |
| 721 | unsigned pgdidx_limit, pudidx_limit, pmdidx_limit; |
| 722 | unsigned pgdidx, pudidx, pmdidx; |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 723 | |
Jeremy Fitzhardinge | 5deb30d | 2008-07-08 15:07:06 -0700 | [diff] [blame] | 724 | /* The limit is the last byte to be touched */ |
| 725 | limit--; |
| 726 | BUG_ON(limit >= FIXADDR_TOP); |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 727 | |
| 728 | if (xen_feature(XENFEAT_auto_translated_physmap)) |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 729 | return 0; |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 730 | |
Jeremy Fitzhardinge | 5deb30d | 2008-07-08 15:07:06 -0700 | [diff] [blame] | 731 | /* |
| 732 | * 64-bit has a great big hole in the middle of the address |
| 733 | * space, which contains the Xen mappings. On 32-bit these |
| 734 | * will end up making a zero-sized hole and so is a no-op. |
| 735 | */ |
Jeremy Fitzhardinge | d6182fb | 2008-07-08 15:07:13 -0700 | [diff] [blame] | 736 | hole_low = pgd_index(USER_LIMIT); |
Jeremy Fitzhardinge | 5deb30d | 2008-07-08 15:07:06 -0700 | [diff] [blame] | 737 | hole_high = pgd_index(PAGE_OFFSET); |
| 738 | |
| 739 | pgdidx_limit = pgd_index(limit); |
| 740 | #if PTRS_PER_PUD > 1 |
| 741 | pudidx_limit = pud_index(limit); |
| 742 | #else |
| 743 | pudidx_limit = 0; |
| 744 | #endif |
| 745 | #if PTRS_PER_PMD > 1 |
| 746 | pmdidx_limit = pmd_index(limit); |
| 747 | #else |
| 748 | pmdidx_limit = 0; |
| 749 | #endif |
| 750 | |
Jeremy Fitzhardinge | 5deb30d | 2008-07-08 15:07:06 -0700 | [diff] [blame] | 751 | for (pgdidx = 0; pgdidx <= pgdidx_limit; pgdidx++) { |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 752 | pud_t *pud; |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 753 | |
Jeremy Fitzhardinge | 5deb30d | 2008-07-08 15:07:06 -0700 | [diff] [blame] | 754 | if (pgdidx >= hole_low && pgdidx < hole_high) |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 755 | continue; |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 756 | |
Jeremy Fitzhardinge | 5deb30d | 2008-07-08 15:07:06 -0700 | [diff] [blame] | 757 | if (!pgd_val(pgd[pgdidx])) |
| 758 | continue; |
| 759 | |
| 760 | pud = pud_offset(&pgd[pgdidx], 0); |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 761 | |
| 762 | if (PTRS_PER_PUD > 1) /* not folded */ |
Jeremy Fitzhardinge | eefb47f | 2008-10-08 13:01:39 -0700 | [diff] [blame] | 763 | flush |= (*func)(mm, virt_to_page(pud), PT_PUD); |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 764 | |
Jeremy Fitzhardinge | 5deb30d | 2008-07-08 15:07:06 -0700 | [diff] [blame] | 765 | for (pudidx = 0; pudidx < PTRS_PER_PUD; pudidx++) { |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 766 | pmd_t *pmd; |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 767 | |
Jeremy Fitzhardinge | 5deb30d | 2008-07-08 15:07:06 -0700 | [diff] [blame] | 768 | if (pgdidx == pgdidx_limit && |
| 769 | pudidx > pudidx_limit) |
| 770 | goto out; |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 771 | |
Jeremy Fitzhardinge | 5deb30d | 2008-07-08 15:07:06 -0700 | [diff] [blame] | 772 | if (pud_none(pud[pudidx])) |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 773 | continue; |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 774 | |
Jeremy Fitzhardinge | 5deb30d | 2008-07-08 15:07:06 -0700 | [diff] [blame] | 775 | pmd = pmd_offset(&pud[pudidx], 0); |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 776 | |
| 777 | if (PTRS_PER_PMD > 1) /* not folded */ |
Jeremy Fitzhardinge | eefb47f | 2008-10-08 13:01:39 -0700 | [diff] [blame] | 778 | flush |= (*func)(mm, virt_to_page(pmd), PT_PMD); |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 779 | |
Jeremy Fitzhardinge | 5deb30d | 2008-07-08 15:07:06 -0700 | [diff] [blame] | 780 | for (pmdidx = 0; pmdidx < PTRS_PER_PMD; pmdidx++) { |
| 781 | struct page *pte; |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 782 | |
Jeremy Fitzhardinge | 5deb30d | 2008-07-08 15:07:06 -0700 | [diff] [blame] | 783 | if (pgdidx == pgdidx_limit && |
| 784 | pudidx == pudidx_limit && |
| 785 | pmdidx > pmdidx_limit) |
| 786 | goto out; |
| 787 | |
| 788 | if (pmd_none(pmd[pmdidx])) |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 789 | continue; |
| 790 | |
Jeremy Fitzhardinge | 5deb30d | 2008-07-08 15:07:06 -0700 | [diff] [blame] | 791 | pte = pmd_page(pmd[pmdidx]); |
Jeremy Fitzhardinge | eefb47f | 2008-10-08 13:01:39 -0700 | [diff] [blame] | 792 | flush |= (*func)(mm, pte, PT_PTE); |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 793 | } |
| 794 | } |
| 795 | } |
Jeremy Fitzhardinge | 11ad93e | 2008-08-19 13:32:51 -0700 | [diff] [blame] | 796 | |
Jeremy Fitzhardinge | 5deb30d | 2008-07-08 15:07:06 -0700 | [diff] [blame] | 797 | out: |
Jeremy Fitzhardinge | 11ad93e | 2008-08-19 13:32:51 -0700 | [diff] [blame] | 798 | /* Do the top level last, so that the callbacks can use it as |
| 799 | a cue to do final things like tlb flushes. */ |
Jeremy Fitzhardinge | eefb47f | 2008-10-08 13:01:39 -0700 | [diff] [blame] | 800 | flush |= (*func)(mm, virt_to_page(pgd), PT_PGD); |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 801 | |
| 802 | return flush; |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 803 | } |
| 804 | |
Ian Campbell | 86bbc2c | 2008-11-21 10:21:33 +0000 | [diff] [blame] | 805 | static int xen_pgd_walk(struct mm_struct *mm, |
| 806 | int (*func)(struct mm_struct *mm, struct page *, |
| 807 | enum pt_level), |
| 808 | unsigned long limit) |
| 809 | { |
| 810 | return __xen_pgd_walk(mm, mm->pgd, func, limit); |
| 811 | } |
| 812 | |
Jeremy Fitzhardinge | 7708ad6 | 2008-08-19 13:34:22 -0700 | [diff] [blame] | 813 | /* If we're using split pte locks, then take the page's lock and |
| 814 | return a pointer to it. Otherwise return NULL. */ |
Jeremy Fitzhardinge | eefb47f | 2008-10-08 13:01:39 -0700 | [diff] [blame] | 815 | static spinlock_t *xen_pte_lock(struct page *page, struct mm_struct *mm) |
Jeremy Fitzhardinge | 7426071 | 2007-10-16 11:51:30 -0700 | [diff] [blame] | 816 | { |
| 817 | spinlock_t *ptl = NULL; |
| 818 | |
Jeremy Fitzhardinge | f7d0b92 | 2008-09-09 15:43:22 -0700 | [diff] [blame] | 819 | #if USE_SPLIT_PTLOCKS |
Jeremy Fitzhardinge | 7426071 | 2007-10-16 11:51:30 -0700 | [diff] [blame] | 820 | ptl = __pte_lockptr(page); |
Jeremy Fitzhardinge | eefb47f | 2008-10-08 13:01:39 -0700 | [diff] [blame] | 821 | spin_lock_nest_lock(ptl, &mm->page_table_lock); |
Jeremy Fitzhardinge | 7426071 | 2007-10-16 11:51:30 -0700 | [diff] [blame] | 822 | #endif |
| 823 | |
| 824 | return ptl; |
| 825 | } |
| 826 | |
Jeremy Fitzhardinge | 7708ad6 | 2008-08-19 13:34:22 -0700 | [diff] [blame] | 827 | static void xen_pte_unlock(void *v) |
Jeremy Fitzhardinge | 7426071 | 2007-10-16 11:51:30 -0700 | [diff] [blame] | 828 | { |
| 829 | spinlock_t *ptl = v; |
| 830 | spin_unlock(ptl); |
| 831 | } |
| 832 | |
| 833 | static void xen_do_pin(unsigned level, unsigned long pfn) |
| 834 | { |
| 835 | struct mmuext_op *op; |
| 836 | struct multicall_space mcs; |
| 837 | |
| 838 | mcs = __xen_mc_entry(sizeof(*op)); |
| 839 | op = mcs.args; |
| 840 | op->cmd = level; |
| 841 | op->arg1.mfn = pfn_to_mfn(pfn); |
| 842 | MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF); |
| 843 | } |
| 844 | |
Jeremy Fitzhardinge | eefb47f | 2008-10-08 13:01:39 -0700 | [diff] [blame] | 845 | static int xen_pin_page(struct mm_struct *mm, struct page *page, |
| 846 | enum pt_level level) |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 847 | { |
Christoph Lameter | d60cd46 | 2008-04-28 02:12:51 -0700 | [diff] [blame] | 848 | unsigned pgfl = TestSetPagePinned(page); |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 849 | int flush; |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 850 | |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 851 | if (pgfl) |
| 852 | flush = 0; /* already pinned */ |
| 853 | else if (PageHighMem(page)) |
| 854 | /* kmaps need flushing if we found an unpinned |
| 855 | highpage */ |
| 856 | flush = 1; |
| 857 | else { |
| 858 | void *pt = lowmem_page_address(page); |
| 859 | unsigned long pfn = page_to_pfn(page); |
| 860 | struct multicall_space mcs = __xen_mc_entry(0); |
Jeremy Fitzhardinge | 7426071 | 2007-10-16 11:51:30 -0700 | [diff] [blame] | 861 | spinlock_t *ptl; |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 862 | |
| 863 | flush = 0; |
| 864 | |
Jeremy Fitzhardinge | 11ad93e | 2008-08-19 13:32:51 -0700 | [diff] [blame] | 865 | /* |
| 866 | * We need to hold the pagetable lock between the time |
| 867 | * we make the pagetable RO and when we actually pin |
| 868 | * it. If we don't, then other users may come in and |
| 869 | * attempt to update the pagetable by writing it, |
| 870 | * which will fail because the memory is RO but not |
| 871 | * pinned, so Xen won't do the trap'n'emulate. |
| 872 | * |
| 873 | * If we're using split pte locks, we can't hold the |
| 874 | * entire pagetable's worth of locks during the |
| 875 | * traverse, because we may wrap the preempt count (8 |
| 876 | * bits). The solution is to mark RO and pin each PTE |
| 877 | * page while holding the lock. This means the number |
| 878 | * of locks we end up holding is never more than a |
| 879 | * batch size (~32 entries, at present). |
| 880 | * |
| 881 | * If we're not using split pte locks, we needn't pin |
| 882 | * the PTE pages independently, because we're |
| 883 | * protected by the overall pagetable lock. |
| 884 | */ |
Jeremy Fitzhardinge | 7426071 | 2007-10-16 11:51:30 -0700 | [diff] [blame] | 885 | ptl = NULL; |
| 886 | if (level == PT_PTE) |
Jeremy Fitzhardinge | eefb47f | 2008-10-08 13:01:39 -0700 | [diff] [blame] | 887 | ptl = xen_pte_lock(page, mm); |
Jeremy Fitzhardinge | 7426071 | 2007-10-16 11:51:30 -0700 | [diff] [blame] | 888 | |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 889 | MULTI_update_va_mapping(mcs.mc, (unsigned long)pt, |
| 890 | pfn_pte(pfn, PAGE_KERNEL_RO), |
Jeremy Fitzhardinge | 7426071 | 2007-10-16 11:51:30 -0700 | [diff] [blame] | 891 | level == PT_PGD ? UVMF_TLB_FLUSH : 0); |
| 892 | |
Jeremy Fitzhardinge | 11ad93e | 2008-08-19 13:32:51 -0700 | [diff] [blame] | 893 | if (ptl) { |
Jeremy Fitzhardinge | 7426071 | 2007-10-16 11:51:30 -0700 | [diff] [blame] | 894 | xen_do_pin(MMUEXT_PIN_L1_TABLE, pfn); |
| 895 | |
Jeremy Fitzhardinge | 7426071 | 2007-10-16 11:51:30 -0700 | [diff] [blame] | 896 | /* Queue a deferred unlock for when this batch |
| 897 | is completed. */ |
Jeremy Fitzhardinge | 7708ad6 | 2008-08-19 13:34:22 -0700 | [diff] [blame] | 898 | xen_mc_callback(xen_pte_unlock, ptl); |
Jeremy Fitzhardinge | 7426071 | 2007-10-16 11:51:30 -0700 | [diff] [blame] | 899 | } |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 900 | } |
| 901 | |
| 902 | return flush; |
| 903 | } |
| 904 | |
| 905 | /* This is called just after a mm has been created, but it has not |
| 906 | been used yet. We need to make sure that its pagetable is all |
| 907 | read-only, and can be pinned. */ |
Jeremy Fitzhardinge | eefb47f | 2008-10-08 13:01:39 -0700 | [diff] [blame] | 908 | static void __xen_pgd_pin(struct mm_struct *mm, pgd_t *pgd) |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 909 | { |
Jeremy Fitzhardinge | d05fdf3 | 2008-10-28 19:23:06 +1100 | [diff] [blame] | 910 | vm_unmap_aliases(); |
| 911 | |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 912 | xen_mc_batch(); |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 913 | |
Ian Campbell | 86bbc2c | 2008-11-21 10:21:33 +0000 | [diff] [blame] | 914 | if (__xen_pgd_walk(mm, pgd, xen_pin_page, USER_LIMIT)) { |
Jeremy Fitzhardinge | d05fdf3 | 2008-10-28 19:23:06 +1100 | [diff] [blame] | 915 | /* re-enable interrupts for flushing */ |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 916 | xen_mc_issue(0); |
Jeremy Fitzhardinge | d05fdf3 | 2008-10-28 19:23:06 +1100 | [diff] [blame] | 917 | |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 918 | kmap_flush_unused(); |
Jeremy Fitzhardinge | d05fdf3 | 2008-10-28 19:23:06 +1100 | [diff] [blame] | 919 | |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 920 | xen_mc_batch(); |
| 921 | } |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 922 | |
Jeremy Fitzhardinge | d6182fb | 2008-07-08 15:07:13 -0700 | [diff] [blame] | 923 | #ifdef CONFIG_X86_64 |
| 924 | { |
| 925 | pgd_t *user_pgd = xen_get_user_pgd(pgd); |
| 926 | |
| 927 | xen_do_pin(MMUEXT_PIN_L4_TABLE, PFN_DOWN(__pa(pgd))); |
| 928 | |
| 929 | if (user_pgd) { |
Jeremy Fitzhardinge | eefb47f | 2008-10-08 13:01:39 -0700 | [diff] [blame] | 930 | xen_pin_page(mm, virt_to_page(user_pgd), PT_PGD); |
Tej | f63c2f2 | 2008-12-16 11:56:06 -0800 | [diff] [blame] | 931 | xen_do_pin(MMUEXT_PIN_L4_TABLE, |
| 932 | PFN_DOWN(__pa(user_pgd))); |
Jeremy Fitzhardinge | d6182fb | 2008-07-08 15:07:13 -0700 | [diff] [blame] | 933 | } |
| 934 | } |
| 935 | #else /* CONFIG_X86_32 */ |
Jeremy Fitzhardinge | 5deb30d | 2008-07-08 15:07:06 -0700 | [diff] [blame] | 936 | #ifdef CONFIG_X86_PAE |
| 937 | /* Need to make sure unshared kernel PMD is pinnable */ |
Jeremy Fitzhardinge | 47cb2ed | 2008-11-06 13:48:24 -0800 | [diff] [blame] | 938 | xen_pin_page(mm, pgd_page(pgd[pgd_index(TASK_SIZE)]), |
Jeremy Fitzhardinge | eefb47f | 2008-10-08 13:01:39 -0700 | [diff] [blame] | 939 | PT_PMD); |
Jeremy Fitzhardinge | 5deb30d | 2008-07-08 15:07:06 -0700 | [diff] [blame] | 940 | #endif |
Jeremy Fitzhardinge | 2849914 | 2008-05-09 12:05:57 +0100 | [diff] [blame] | 941 | xen_do_pin(MMUEXT_PIN_L3_TABLE, PFN_DOWN(__pa(pgd))); |
Jeremy Fitzhardinge | d6182fb | 2008-07-08 15:07:13 -0700 | [diff] [blame] | 942 | #endif /* CONFIG_X86_64 */ |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 943 | xen_mc_issue(0); |
| 944 | } |
| 945 | |
Jeremy Fitzhardinge | eefb47f | 2008-10-08 13:01:39 -0700 | [diff] [blame] | 946 | static void xen_pgd_pin(struct mm_struct *mm) |
| 947 | { |
| 948 | __xen_pgd_pin(mm, mm->pgd); |
| 949 | } |
| 950 | |
Jeremy Fitzhardinge | 0e91398 | 2008-05-26 23:31:27 +0100 | [diff] [blame] | 951 | /* |
| 952 | * On save, we need to pin all pagetables to make sure they get their |
| 953 | * mfns turned into pfns. Search the list for any unpinned pgds and pin |
| 954 | * them (unpinned pgds are not currently in use, probably because the |
| 955 | * process is under construction or destruction). |
Jeremy Fitzhardinge | eefb47f | 2008-10-08 13:01:39 -0700 | [diff] [blame] | 956 | * |
| 957 | * Expected to be called in stop_machine() ("equivalent to taking |
| 958 | * every spinlock in the system"), so the locking doesn't really |
| 959 | * matter all that much. |
Jeremy Fitzhardinge | 0e91398 | 2008-05-26 23:31:27 +0100 | [diff] [blame] | 960 | */ |
| 961 | void xen_mm_pin_all(void) |
| 962 | { |
| 963 | unsigned long flags; |
| 964 | struct page *page; |
| 965 | |
| 966 | spin_lock_irqsave(&pgd_lock, flags); |
| 967 | |
| 968 | list_for_each_entry(page, &pgd_list, lru) { |
| 969 | if (!PagePinned(page)) { |
Jeremy Fitzhardinge | eefb47f | 2008-10-08 13:01:39 -0700 | [diff] [blame] | 970 | __xen_pgd_pin(&init_mm, (pgd_t *)page_address(page)); |
Jeremy Fitzhardinge | 0e91398 | 2008-05-26 23:31:27 +0100 | [diff] [blame] | 971 | SetPageSavePinned(page); |
| 972 | } |
| 973 | } |
| 974 | |
| 975 | spin_unlock_irqrestore(&pgd_lock, flags); |
| 976 | } |
| 977 | |
Eduardo Habkost | c1f2f09 | 2008-07-08 15:06:24 -0700 | [diff] [blame] | 978 | /* |
| 979 | * The init_mm pagetable is really pinned as soon as its created, but |
| 980 | * that's before we have page structures to store the bits. So do all |
| 981 | * the book-keeping now. |
| 982 | */ |
Jeremy Fitzhardinge | eefb47f | 2008-10-08 13:01:39 -0700 | [diff] [blame] | 983 | static __init int xen_mark_pinned(struct mm_struct *mm, struct page *page, |
| 984 | enum pt_level level) |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 985 | { |
| 986 | SetPagePinned(page); |
| 987 | return 0; |
| 988 | } |
| 989 | |
| 990 | void __init xen_mark_init_mm_pinned(void) |
| 991 | { |
Jeremy Fitzhardinge | eefb47f | 2008-10-08 13:01:39 -0700 | [diff] [blame] | 992 | xen_pgd_walk(&init_mm, xen_mark_pinned, FIXADDR_TOP); |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 993 | } |
| 994 | |
Jeremy Fitzhardinge | eefb47f | 2008-10-08 13:01:39 -0700 | [diff] [blame] | 995 | static int xen_unpin_page(struct mm_struct *mm, struct page *page, |
| 996 | enum pt_level level) |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 997 | { |
Christoph Lameter | d60cd46 | 2008-04-28 02:12:51 -0700 | [diff] [blame] | 998 | unsigned pgfl = TestClearPagePinned(page); |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 999 | |
| 1000 | if (pgfl && !PageHighMem(page)) { |
| 1001 | void *pt = lowmem_page_address(page); |
| 1002 | unsigned long pfn = page_to_pfn(page); |
Jeremy Fitzhardinge | 7426071 | 2007-10-16 11:51:30 -0700 | [diff] [blame] | 1003 | spinlock_t *ptl = NULL; |
| 1004 | struct multicall_space mcs; |
| 1005 | |
Jeremy Fitzhardinge | 11ad93e | 2008-08-19 13:32:51 -0700 | [diff] [blame] | 1006 | /* |
| 1007 | * Do the converse to pin_page. If we're using split |
| 1008 | * pte locks, we must be holding the lock for while |
| 1009 | * the pte page is unpinned but still RO to prevent |
| 1010 | * concurrent updates from seeing it in this |
| 1011 | * partially-pinned state. |
| 1012 | */ |
Jeremy Fitzhardinge | 7426071 | 2007-10-16 11:51:30 -0700 | [diff] [blame] | 1013 | if (level == PT_PTE) { |
Jeremy Fitzhardinge | eefb47f | 2008-10-08 13:01:39 -0700 | [diff] [blame] | 1014 | ptl = xen_pte_lock(page, mm); |
Jeremy Fitzhardinge | 7426071 | 2007-10-16 11:51:30 -0700 | [diff] [blame] | 1015 | |
Jeremy Fitzhardinge | 11ad93e | 2008-08-19 13:32:51 -0700 | [diff] [blame] | 1016 | if (ptl) |
| 1017 | xen_do_pin(MMUEXT_UNPIN_TABLE, pfn); |
Jeremy Fitzhardinge | 7426071 | 2007-10-16 11:51:30 -0700 | [diff] [blame] | 1018 | } |
| 1019 | |
| 1020 | mcs = __xen_mc_entry(0); |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 1021 | |
| 1022 | MULTI_update_va_mapping(mcs.mc, (unsigned long)pt, |
| 1023 | pfn_pte(pfn, PAGE_KERNEL), |
Jeremy Fitzhardinge | 7426071 | 2007-10-16 11:51:30 -0700 | [diff] [blame] | 1024 | level == PT_PGD ? UVMF_TLB_FLUSH : 0); |
| 1025 | |
| 1026 | if (ptl) { |
| 1027 | /* unlock when batch completed */ |
Jeremy Fitzhardinge | 7708ad6 | 2008-08-19 13:34:22 -0700 | [diff] [blame] | 1028 | xen_mc_callback(xen_pte_unlock, ptl); |
Jeremy Fitzhardinge | 7426071 | 2007-10-16 11:51:30 -0700 | [diff] [blame] | 1029 | } |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 1030 | } |
| 1031 | |
| 1032 | return 0; /* never need to flush on unpin */ |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 1033 | } |
| 1034 | |
| 1035 | /* Release a pagetables pages back as normal RW */ |
Jeremy Fitzhardinge | eefb47f | 2008-10-08 13:01:39 -0700 | [diff] [blame] | 1036 | static void __xen_pgd_unpin(struct mm_struct *mm, pgd_t *pgd) |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 1037 | { |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 1038 | xen_mc_batch(); |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 1039 | |
Jeremy Fitzhardinge | 7426071 | 2007-10-16 11:51:30 -0700 | [diff] [blame] | 1040 | xen_do_pin(MMUEXT_UNPIN_TABLE, PFN_DOWN(__pa(pgd))); |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 1041 | |
Jeremy Fitzhardinge | d6182fb | 2008-07-08 15:07:13 -0700 | [diff] [blame] | 1042 | #ifdef CONFIG_X86_64 |
| 1043 | { |
| 1044 | pgd_t *user_pgd = xen_get_user_pgd(pgd); |
| 1045 | |
| 1046 | if (user_pgd) { |
Tej | f63c2f2 | 2008-12-16 11:56:06 -0800 | [diff] [blame] | 1047 | xen_do_pin(MMUEXT_UNPIN_TABLE, |
| 1048 | PFN_DOWN(__pa(user_pgd))); |
Jeremy Fitzhardinge | eefb47f | 2008-10-08 13:01:39 -0700 | [diff] [blame] | 1049 | xen_unpin_page(mm, virt_to_page(user_pgd), PT_PGD); |
Jeremy Fitzhardinge | d6182fb | 2008-07-08 15:07:13 -0700 | [diff] [blame] | 1050 | } |
| 1051 | } |
| 1052 | #endif |
| 1053 | |
Jeremy Fitzhardinge | 5deb30d | 2008-07-08 15:07:06 -0700 | [diff] [blame] | 1054 | #ifdef CONFIG_X86_PAE |
| 1055 | /* Need to make sure unshared kernel PMD is unpinned */ |
Jeremy Fitzhardinge | 47cb2ed | 2008-11-06 13:48:24 -0800 | [diff] [blame] | 1056 | xen_unpin_page(mm, pgd_page(pgd[pgd_index(TASK_SIZE)]), |
Jeremy Fitzhardinge | eefb47f | 2008-10-08 13:01:39 -0700 | [diff] [blame] | 1057 | PT_PMD); |
Jeremy Fitzhardinge | 5deb30d | 2008-07-08 15:07:06 -0700 | [diff] [blame] | 1058 | #endif |
Jeremy Fitzhardinge | d6182fb | 2008-07-08 15:07:13 -0700 | [diff] [blame] | 1059 | |
Ian Campbell | 86bbc2c | 2008-11-21 10:21:33 +0000 | [diff] [blame] | 1060 | __xen_pgd_walk(mm, pgd, xen_unpin_page, USER_LIMIT); |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 1061 | |
| 1062 | xen_mc_issue(0); |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 1063 | } |
| 1064 | |
Jeremy Fitzhardinge | eefb47f | 2008-10-08 13:01:39 -0700 | [diff] [blame] | 1065 | static void xen_pgd_unpin(struct mm_struct *mm) |
| 1066 | { |
| 1067 | __xen_pgd_unpin(mm, mm->pgd); |
| 1068 | } |
| 1069 | |
Jeremy Fitzhardinge | 0e91398 | 2008-05-26 23:31:27 +0100 | [diff] [blame] | 1070 | /* |
| 1071 | * On resume, undo any pinning done at save, so that the rest of the |
| 1072 | * kernel doesn't see any unexpected pinned pagetables. |
| 1073 | */ |
| 1074 | void xen_mm_unpin_all(void) |
| 1075 | { |
| 1076 | unsigned long flags; |
| 1077 | struct page *page; |
| 1078 | |
| 1079 | spin_lock_irqsave(&pgd_lock, flags); |
| 1080 | |
| 1081 | list_for_each_entry(page, &pgd_list, lru) { |
| 1082 | if (PageSavePinned(page)) { |
| 1083 | BUG_ON(!PagePinned(page)); |
Jeremy Fitzhardinge | eefb47f | 2008-10-08 13:01:39 -0700 | [diff] [blame] | 1084 | __xen_pgd_unpin(&init_mm, (pgd_t *)page_address(page)); |
Jeremy Fitzhardinge | 0e91398 | 2008-05-26 23:31:27 +0100 | [diff] [blame] | 1085 | ClearPageSavePinned(page); |
| 1086 | } |
| 1087 | } |
| 1088 | |
| 1089 | spin_unlock_irqrestore(&pgd_lock, flags); |
| 1090 | } |
| 1091 | |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 1092 | void xen_activate_mm(struct mm_struct *prev, struct mm_struct *next) |
| 1093 | { |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 1094 | spin_lock(&next->page_table_lock); |
Jeremy Fitzhardinge | eefb47f | 2008-10-08 13:01:39 -0700 | [diff] [blame] | 1095 | xen_pgd_pin(next); |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 1096 | spin_unlock(&next->page_table_lock); |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 1097 | } |
| 1098 | |
| 1099 | void xen_dup_mmap(struct mm_struct *oldmm, struct mm_struct *mm) |
| 1100 | { |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 1101 | spin_lock(&mm->page_table_lock); |
Jeremy Fitzhardinge | eefb47f | 2008-10-08 13:01:39 -0700 | [diff] [blame] | 1102 | xen_pgd_pin(mm); |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 1103 | spin_unlock(&mm->page_table_lock); |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 1104 | } |
| 1105 | |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1106 | |
| 1107 | #ifdef CONFIG_SMP |
| 1108 | /* Another cpu may still have their %cr3 pointing at the pagetable, so |
| 1109 | we need to repoint it somewhere else before we can unpin it. */ |
| 1110 | static void drop_other_mm_ref(void *info) |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 1111 | { |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1112 | struct mm_struct *mm = info; |
Jeremy Fitzhardinge | ce87b3d | 2008-07-08 15:06:40 -0700 | [diff] [blame] | 1113 | struct mm_struct *active_mm; |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 1114 | |
Brian Gerst | 9eb912d | 2009-01-19 00:38:57 +0900 | [diff] [blame] | 1115 | active_mm = percpu_read(cpu_tlbstate.active_mm); |
Jeremy Fitzhardinge | ce87b3d | 2008-07-08 15:06:40 -0700 | [diff] [blame] | 1116 | |
| 1117 | if (active_mm == mm) |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1118 | leave_mm(smp_processor_id()); |
Jeremy Fitzhardinge | 9f79991 | 2007-10-16 11:51:30 -0700 | [diff] [blame] | 1119 | |
| 1120 | /* If this cpu still has a stale cr3 reference, then make sure |
| 1121 | it has been flushed. */ |
Ingo Molnar | 6dbde35 | 2009-01-15 22:15:53 +0900 | [diff] [blame] | 1122 | if (percpu_read(xen_current_cr3) == __pa(mm->pgd)) { |
Jeremy Fitzhardinge | 9f79991 | 2007-10-16 11:51:30 -0700 | [diff] [blame] | 1123 | load_cr3(swapper_pg_dir); |
| 1124 | arch_flush_lazy_cpu_mode(); |
| 1125 | } |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1126 | } |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 1127 | |
Jeremy Fitzhardinge | 7708ad6 | 2008-08-19 13:34:22 -0700 | [diff] [blame] | 1128 | static void xen_drop_mm_ref(struct mm_struct *mm) |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1129 | { |
Mike Travis | e4d9820 | 2008-12-16 17:34:05 -0800 | [diff] [blame] | 1130 | cpumask_var_t mask; |
Jeremy Fitzhardinge | 9f79991 | 2007-10-16 11:51:30 -0700 | [diff] [blame] | 1131 | unsigned cpu; |
| 1132 | |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1133 | if (current->active_mm == mm) { |
| 1134 | if (current->mm == mm) |
| 1135 | load_cr3(swapper_pg_dir); |
| 1136 | else |
| 1137 | leave_mm(smp_processor_id()); |
Jeremy Fitzhardinge | 9f79991 | 2007-10-16 11:51:30 -0700 | [diff] [blame] | 1138 | arch_flush_lazy_cpu_mode(); |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 1139 | } |
| 1140 | |
Jeremy Fitzhardinge | 9f79991 | 2007-10-16 11:51:30 -0700 | [diff] [blame] | 1141 | /* Get the "official" set of cpus referring to our pagetable. */ |
Mike Travis | e4d9820 | 2008-12-16 17:34:05 -0800 | [diff] [blame] | 1142 | if (!alloc_cpumask_var(&mask, GFP_ATOMIC)) { |
| 1143 | for_each_online_cpu(cpu) { |
| 1144 | if (!cpumask_test_cpu(cpu, &mm->cpu_vm_mask) |
| 1145 | && per_cpu(xen_current_cr3, cpu) != __pa(mm->pgd)) |
| 1146 | continue; |
| 1147 | smp_call_function_single(cpu, drop_other_mm_ref, mm, 1); |
| 1148 | } |
| 1149 | return; |
| 1150 | } |
| 1151 | cpumask_copy(mask, &mm->cpu_vm_mask); |
Jeremy Fitzhardinge | 9f79991 | 2007-10-16 11:51:30 -0700 | [diff] [blame] | 1152 | |
| 1153 | /* It's possible that a vcpu may have a stale reference to our |
| 1154 | cr3, because its in lazy mode, and it hasn't yet flushed |
| 1155 | its set of pending hypercalls yet. In this case, we can |
| 1156 | look at its actual current cr3 value, and force it to flush |
| 1157 | if needed. */ |
| 1158 | for_each_online_cpu(cpu) { |
| 1159 | if (per_cpu(xen_current_cr3, cpu) == __pa(mm->pgd)) |
Mike Travis | e4d9820 | 2008-12-16 17:34:05 -0800 | [diff] [blame] | 1160 | cpumask_set_cpu(cpu, mask); |
Jeremy Fitzhardinge | 9f79991 | 2007-10-16 11:51:30 -0700 | [diff] [blame] | 1161 | } |
| 1162 | |
Mike Travis | e4d9820 | 2008-12-16 17:34:05 -0800 | [diff] [blame] | 1163 | if (!cpumask_empty(mask)) |
| 1164 | smp_call_function_many(mask, drop_other_mm_ref, mm, 1); |
| 1165 | free_cpumask_var(mask); |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1166 | } |
| 1167 | #else |
Jeremy Fitzhardinge | 7708ad6 | 2008-08-19 13:34:22 -0700 | [diff] [blame] | 1168 | static void xen_drop_mm_ref(struct mm_struct *mm) |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1169 | { |
| 1170 | if (current->active_mm == mm) |
| 1171 | load_cr3(swapper_pg_dir); |
| 1172 | } |
| 1173 | #endif |
| 1174 | |
| 1175 | /* |
| 1176 | * While a process runs, Xen pins its pagetables, which means that the |
| 1177 | * hypervisor forces it to be read-only, and it controls all updates |
| 1178 | * to it. This means that all pagetable updates have to go via the |
| 1179 | * hypervisor, which is moderately expensive. |
| 1180 | * |
| 1181 | * Since we're pulling the pagetable down, we switch to use init_mm, |
| 1182 | * unpin old process pagetable and mark it all read-write, which |
| 1183 | * allows further operations on it to be simple memory accesses. |
| 1184 | * |
| 1185 | * The only subtle point is that another CPU may be still using the |
| 1186 | * pagetable because of lazy tlb flushing. This means we need need to |
| 1187 | * switch all CPUs off this pagetable before we can unpin it. |
| 1188 | */ |
| 1189 | void xen_exit_mmap(struct mm_struct *mm) |
| 1190 | { |
| 1191 | get_cpu(); /* make sure we don't move around */ |
Jeremy Fitzhardinge | 7708ad6 | 2008-08-19 13:34:22 -0700 | [diff] [blame] | 1192 | xen_drop_mm_ref(mm); |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1193 | put_cpu(); |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 1194 | |
Jeremy Fitzhardinge | f120f13 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1195 | spin_lock(&mm->page_table_lock); |
Jeremy Fitzhardinge | df912ea | 2007-09-25 11:50:00 -0700 | [diff] [blame] | 1196 | |
| 1197 | /* pgd may not be pinned in the error exit path of execve */ |
Jeremy Fitzhardinge | 7708ad6 | 2008-08-19 13:34:22 -0700 | [diff] [blame] | 1198 | if (xen_page_pinned(mm->pgd)) |
Jeremy Fitzhardinge | eefb47f | 2008-10-08 13:01:39 -0700 | [diff] [blame] | 1199 | xen_pgd_unpin(mm); |
Jeremy Fitzhardinge | 7426071 | 2007-10-16 11:51:30 -0700 | [diff] [blame] | 1200 | |
Jeremy Fitzhardinge | f120f13 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1201 | spin_unlock(&mm->page_table_lock); |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 1202 | } |
Jeremy Fitzhardinge | 994025c | 2008-08-20 17:02:19 -0700 | [diff] [blame] | 1203 | |
Jeremy Fitzhardinge | 319f3ba | 2009-01-28 14:35:01 -0800 | [diff] [blame] | 1204 | static __init void xen_pagetable_setup_start(pgd_t *base) |
| 1205 | { |
| 1206 | } |
| 1207 | |
| 1208 | static __init void xen_pagetable_setup_done(pgd_t *base) |
| 1209 | { |
| 1210 | xen_setup_shared_info(); |
| 1211 | } |
| 1212 | |
| 1213 | static void xen_write_cr2(unsigned long cr2) |
| 1214 | { |
| 1215 | percpu_read(xen_vcpu)->arch.cr2 = cr2; |
| 1216 | } |
| 1217 | |
| 1218 | static unsigned long xen_read_cr2(void) |
| 1219 | { |
| 1220 | return percpu_read(xen_vcpu)->arch.cr2; |
| 1221 | } |
| 1222 | |
| 1223 | unsigned long xen_read_cr2_direct(void) |
| 1224 | { |
| 1225 | return percpu_read(xen_vcpu_info.arch.cr2); |
| 1226 | } |
| 1227 | |
| 1228 | static void xen_flush_tlb(void) |
| 1229 | { |
| 1230 | struct mmuext_op *op; |
| 1231 | struct multicall_space mcs; |
| 1232 | |
| 1233 | preempt_disable(); |
| 1234 | |
| 1235 | mcs = xen_mc_entry(sizeof(*op)); |
| 1236 | |
| 1237 | op = mcs.args; |
| 1238 | op->cmd = MMUEXT_TLB_FLUSH_LOCAL; |
| 1239 | MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF); |
| 1240 | |
| 1241 | xen_mc_issue(PARAVIRT_LAZY_MMU); |
| 1242 | |
| 1243 | preempt_enable(); |
| 1244 | } |
| 1245 | |
| 1246 | static void xen_flush_tlb_single(unsigned long addr) |
| 1247 | { |
| 1248 | struct mmuext_op *op; |
| 1249 | struct multicall_space mcs; |
| 1250 | |
| 1251 | preempt_disable(); |
| 1252 | |
| 1253 | mcs = xen_mc_entry(sizeof(*op)); |
| 1254 | op = mcs.args; |
| 1255 | op->cmd = MMUEXT_INVLPG_LOCAL; |
| 1256 | op->arg1.linear_addr = addr & PAGE_MASK; |
| 1257 | MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF); |
| 1258 | |
| 1259 | xen_mc_issue(PARAVIRT_LAZY_MMU); |
| 1260 | |
| 1261 | preempt_enable(); |
| 1262 | } |
| 1263 | |
| 1264 | static void xen_flush_tlb_others(const struct cpumask *cpus, |
| 1265 | struct mm_struct *mm, unsigned long va) |
| 1266 | { |
| 1267 | struct { |
| 1268 | struct mmuext_op op; |
| 1269 | DECLARE_BITMAP(mask, NR_CPUS); |
| 1270 | } *args; |
| 1271 | struct multicall_space mcs; |
| 1272 | |
| 1273 | BUG_ON(cpumask_empty(cpus)); |
| 1274 | BUG_ON(!mm); |
| 1275 | |
| 1276 | mcs = xen_mc_entry(sizeof(*args)); |
| 1277 | args = mcs.args; |
| 1278 | args->op.arg2.vcpumask = to_cpumask(args->mask); |
| 1279 | |
| 1280 | /* Remove us, and any offline CPUS. */ |
| 1281 | cpumask_and(to_cpumask(args->mask), cpus, cpu_online_mask); |
| 1282 | cpumask_clear_cpu(smp_processor_id(), to_cpumask(args->mask)); |
Jeremy Fitzhardinge | 319f3ba | 2009-01-28 14:35:01 -0800 | [diff] [blame] | 1283 | |
| 1284 | if (va == TLB_FLUSH_ALL) { |
| 1285 | args->op.cmd = MMUEXT_TLB_FLUSH_MULTI; |
| 1286 | } else { |
| 1287 | args->op.cmd = MMUEXT_INVLPG_MULTI; |
| 1288 | args->op.arg1.linear_addr = va; |
| 1289 | } |
| 1290 | |
| 1291 | MULTI_mmuext_op(mcs.mc, &args->op, 1, NULL, DOMID_SELF); |
| 1292 | |
Jeremy Fitzhardinge | 319f3ba | 2009-01-28 14:35:01 -0800 | [diff] [blame] | 1293 | xen_mc_issue(PARAVIRT_LAZY_MMU); |
| 1294 | } |
| 1295 | |
| 1296 | static unsigned long xen_read_cr3(void) |
| 1297 | { |
| 1298 | return percpu_read(xen_cr3); |
| 1299 | } |
| 1300 | |
| 1301 | static void set_current_cr3(void *v) |
| 1302 | { |
| 1303 | percpu_write(xen_current_cr3, (unsigned long)v); |
| 1304 | } |
| 1305 | |
| 1306 | static void __xen_write_cr3(bool kernel, unsigned long cr3) |
| 1307 | { |
| 1308 | struct mmuext_op *op; |
| 1309 | struct multicall_space mcs; |
| 1310 | unsigned long mfn; |
| 1311 | |
| 1312 | if (cr3) |
| 1313 | mfn = pfn_to_mfn(PFN_DOWN(cr3)); |
| 1314 | else |
| 1315 | mfn = 0; |
| 1316 | |
| 1317 | WARN_ON(mfn == 0 && kernel); |
| 1318 | |
| 1319 | mcs = __xen_mc_entry(sizeof(*op)); |
| 1320 | |
| 1321 | op = mcs.args; |
| 1322 | op->cmd = kernel ? MMUEXT_NEW_BASEPTR : MMUEXT_NEW_USER_BASEPTR; |
| 1323 | op->arg1.mfn = mfn; |
| 1324 | |
| 1325 | MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF); |
| 1326 | |
| 1327 | if (kernel) { |
| 1328 | percpu_write(xen_cr3, cr3); |
| 1329 | |
| 1330 | /* Update xen_current_cr3 once the batch has actually |
| 1331 | been submitted. */ |
| 1332 | xen_mc_callback(set_current_cr3, (void *)cr3); |
| 1333 | } |
| 1334 | } |
| 1335 | |
| 1336 | static void xen_write_cr3(unsigned long cr3) |
| 1337 | { |
| 1338 | BUG_ON(preemptible()); |
| 1339 | |
| 1340 | xen_mc_batch(); /* disables interrupts */ |
| 1341 | |
| 1342 | /* Update while interrupts are disabled, so its atomic with |
| 1343 | respect to ipis */ |
| 1344 | percpu_write(xen_cr3, cr3); |
| 1345 | |
| 1346 | __xen_write_cr3(true, cr3); |
| 1347 | |
| 1348 | #ifdef CONFIG_X86_64 |
| 1349 | { |
| 1350 | pgd_t *user_pgd = xen_get_user_pgd(__va(cr3)); |
| 1351 | if (user_pgd) |
| 1352 | __xen_write_cr3(false, __pa(user_pgd)); |
| 1353 | else |
| 1354 | __xen_write_cr3(false, 0); |
| 1355 | } |
| 1356 | #endif |
| 1357 | |
| 1358 | xen_mc_issue(PARAVIRT_LAZY_CPU); /* interrupts restored */ |
| 1359 | } |
| 1360 | |
| 1361 | static int xen_pgd_alloc(struct mm_struct *mm) |
| 1362 | { |
| 1363 | pgd_t *pgd = mm->pgd; |
| 1364 | int ret = 0; |
| 1365 | |
| 1366 | BUG_ON(PagePinned(virt_to_page(pgd))); |
| 1367 | |
| 1368 | #ifdef CONFIG_X86_64 |
| 1369 | { |
| 1370 | struct page *page = virt_to_page(pgd); |
| 1371 | pgd_t *user_pgd; |
| 1372 | |
| 1373 | BUG_ON(page->private != 0); |
| 1374 | |
| 1375 | ret = -ENOMEM; |
| 1376 | |
| 1377 | user_pgd = (pgd_t *)__get_free_page(GFP_KERNEL | __GFP_ZERO); |
| 1378 | page->private = (unsigned long)user_pgd; |
| 1379 | |
| 1380 | if (user_pgd != NULL) { |
| 1381 | user_pgd[pgd_index(VSYSCALL_START)] = |
| 1382 | __pgd(__pa(level3_user_vsyscall) | _PAGE_TABLE); |
| 1383 | ret = 0; |
| 1384 | } |
| 1385 | |
| 1386 | BUG_ON(PagePinned(virt_to_page(xen_get_user_pgd(pgd)))); |
| 1387 | } |
| 1388 | #endif |
| 1389 | |
| 1390 | return ret; |
| 1391 | } |
| 1392 | |
| 1393 | static void xen_pgd_free(struct mm_struct *mm, pgd_t *pgd) |
| 1394 | { |
| 1395 | #ifdef CONFIG_X86_64 |
| 1396 | pgd_t *user_pgd = xen_get_user_pgd(pgd); |
| 1397 | |
| 1398 | if (user_pgd) |
| 1399 | free_page((unsigned long)user_pgd); |
| 1400 | #endif |
| 1401 | } |
| 1402 | |
Jeremy Fitzhardinge | 1f4f931 | 2009-02-02 13:58:06 -0800 | [diff] [blame] | 1403 | #ifdef CONFIG_HIGHPTE |
| 1404 | static void *xen_kmap_atomic_pte(struct page *page, enum km_type type) |
| 1405 | { |
| 1406 | pgprot_t prot = PAGE_KERNEL; |
| 1407 | |
| 1408 | if (PagePinned(page)) |
| 1409 | prot = PAGE_KERNEL_RO; |
| 1410 | |
| 1411 | if (0 && PageHighMem(page)) |
| 1412 | printk("mapping highpte %lx type %d prot %s\n", |
| 1413 | page_to_pfn(page), type, |
| 1414 | (unsigned long)pgprot_val(prot) & _PAGE_RW ? "WRITE" : "READ"); |
| 1415 | |
| 1416 | return kmap_atomic_prot(page, type, prot); |
| 1417 | } |
| 1418 | #endif |
| 1419 | |
| 1420 | #ifdef CONFIG_X86_32 |
| 1421 | static __init pte_t mask_rw_pte(pte_t *ptep, pte_t pte) |
| 1422 | { |
| 1423 | /* If there's an existing pte, then don't allow _PAGE_RW to be set */ |
| 1424 | if (pte_val_ma(*ptep) & _PAGE_PRESENT) |
| 1425 | pte = __pte_ma(((pte_val_ma(*ptep) & _PAGE_RW) | ~_PAGE_RW) & |
| 1426 | pte_val_ma(pte)); |
| 1427 | |
| 1428 | return pte; |
| 1429 | } |
| 1430 | |
| 1431 | /* Init-time set_pte while constructing initial pagetables, which |
| 1432 | doesn't allow RO pagetable pages to be remapped RW */ |
| 1433 | static __init void xen_set_pte_init(pte_t *ptep, pte_t pte) |
| 1434 | { |
| 1435 | pte = mask_rw_pte(ptep, pte); |
| 1436 | |
| 1437 | xen_set_pte(ptep, pte); |
| 1438 | } |
| 1439 | #endif |
Jeremy Fitzhardinge | 319f3ba | 2009-01-28 14:35:01 -0800 | [diff] [blame] | 1440 | |
| 1441 | /* Early in boot, while setting up the initial pagetable, assume |
| 1442 | everything is pinned. */ |
| 1443 | static __init void xen_alloc_pte_init(struct mm_struct *mm, unsigned long pfn) |
| 1444 | { |
| 1445 | #ifdef CONFIG_FLATMEM |
| 1446 | BUG_ON(mem_map); /* should only be used early */ |
| 1447 | #endif |
| 1448 | make_lowmem_page_readonly(__va(PFN_PHYS(pfn))); |
| 1449 | } |
| 1450 | |
| 1451 | /* Early release_pte assumes that all pts are pinned, since there's |
| 1452 | only init_mm and anything attached to that is pinned. */ |
| 1453 | static void xen_release_pte_init(unsigned long pfn) |
| 1454 | { |
| 1455 | make_lowmem_page_readwrite(__va(PFN_PHYS(pfn))); |
| 1456 | } |
| 1457 | |
| 1458 | static void pin_pagetable_pfn(unsigned cmd, unsigned long pfn) |
| 1459 | { |
| 1460 | struct mmuext_op op; |
| 1461 | op.cmd = cmd; |
| 1462 | op.arg1.mfn = pfn_to_mfn(pfn); |
| 1463 | if (HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF)) |
| 1464 | BUG(); |
| 1465 | } |
| 1466 | |
| 1467 | /* This needs to make sure the new pte page is pinned iff its being |
| 1468 | attached to a pinned pagetable. */ |
| 1469 | static void xen_alloc_ptpage(struct mm_struct *mm, unsigned long pfn, unsigned level) |
| 1470 | { |
| 1471 | struct page *page = pfn_to_page(pfn); |
| 1472 | |
| 1473 | if (PagePinned(virt_to_page(mm->pgd))) { |
| 1474 | SetPagePinned(page); |
| 1475 | |
| 1476 | vm_unmap_aliases(); |
| 1477 | if (!PageHighMem(page)) { |
| 1478 | make_lowmem_page_readonly(__va(PFN_PHYS((unsigned long)pfn))); |
| 1479 | if (level == PT_PTE && USE_SPLIT_PTLOCKS) |
| 1480 | pin_pagetable_pfn(MMUEXT_PIN_L1_TABLE, pfn); |
| 1481 | } else { |
| 1482 | /* make sure there are no stray mappings of |
| 1483 | this page */ |
| 1484 | kmap_flush_unused(); |
| 1485 | } |
| 1486 | } |
| 1487 | } |
| 1488 | |
| 1489 | static void xen_alloc_pte(struct mm_struct *mm, unsigned long pfn) |
| 1490 | { |
| 1491 | xen_alloc_ptpage(mm, pfn, PT_PTE); |
| 1492 | } |
| 1493 | |
| 1494 | static void xen_alloc_pmd(struct mm_struct *mm, unsigned long pfn) |
| 1495 | { |
| 1496 | xen_alloc_ptpage(mm, pfn, PT_PMD); |
| 1497 | } |
| 1498 | |
| 1499 | /* This should never happen until we're OK to use struct page */ |
| 1500 | static void xen_release_ptpage(unsigned long pfn, unsigned level) |
| 1501 | { |
| 1502 | struct page *page = pfn_to_page(pfn); |
| 1503 | |
| 1504 | if (PagePinned(page)) { |
| 1505 | if (!PageHighMem(page)) { |
| 1506 | if (level == PT_PTE && USE_SPLIT_PTLOCKS) |
| 1507 | pin_pagetable_pfn(MMUEXT_UNPIN_TABLE, pfn); |
| 1508 | make_lowmem_page_readwrite(__va(PFN_PHYS(pfn))); |
| 1509 | } |
| 1510 | ClearPagePinned(page); |
| 1511 | } |
| 1512 | } |
| 1513 | |
| 1514 | static void xen_release_pte(unsigned long pfn) |
| 1515 | { |
| 1516 | xen_release_ptpage(pfn, PT_PTE); |
| 1517 | } |
| 1518 | |
| 1519 | static void xen_release_pmd(unsigned long pfn) |
| 1520 | { |
| 1521 | xen_release_ptpage(pfn, PT_PMD); |
| 1522 | } |
| 1523 | |
| 1524 | #if PAGETABLE_LEVELS == 4 |
| 1525 | static void xen_alloc_pud(struct mm_struct *mm, unsigned long pfn) |
| 1526 | { |
| 1527 | xen_alloc_ptpage(mm, pfn, PT_PUD); |
| 1528 | } |
| 1529 | |
| 1530 | static void xen_release_pud(unsigned long pfn) |
| 1531 | { |
| 1532 | xen_release_ptpage(pfn, PT_PUD); |
| 1533 | } |
| 1534 | #endif |
| 1535 | |
| 1536 | void __init xen_reserve_top(void) |
| 1537 | { |
| 1538 | #ifdef CONFIG_X86_32 |
| 1539 | unsigned long top = HYPERVISOR_VIRT_START; |
| 1540 | struct xen_platform_parameters pp; |
| 1541 | |
| 1542 | if (HYPERVISOR_xen_version(XENVER_platform_parameters, &pp) == 0) |
| 1543 | top = pp.virt_start; |
| 1544 | |
| 1545 | reserve_top_address(-top); |
| 1546 | #endif /* CONFIG_X86_32 */ |
| 1547 | } |
| 1548 | |
| 1549 | /* |
| 1550 | * Like __va(), but returns address in the kernel mapping (which is |
| 1551 | * all we have until the physical memory mapping has been set up. |
| 1552 | */ |
| 1553 | static void *__ka(phys_addr_t paddr) |
| 1554 | { |
| 1555 | #ifdef CONFIG_X86_64 |
| 1556 | return (void *)(paddr + __START_KERNEL_map); |
| 1557 | #else |
| 1558 | return __va(paddr); |
| 1559 | #endif |
| 1560 | } |
| 1561 | |
| 1562 | /* Convert a machine address to physical address */ |
| 1563 | static unsigned long m2p(phys_addr_t maddr) |
| 1564 | { |
| 1565 | phys_addr_t paddr; |
| 1566 | |
| 1567 | maddr &= PTE_PFN_MASK; |
| 1568 | paddr = mfn_to_pfn(maddr >> PAGE_SHIFT) << PAGE_SHIFT; |
| 1569 | |
| 1570 | return paddr; |
| 1571 | } |
| 1572 | |
| 1573 | /* Convert a machine address to kernel virtual */ |
| 1574 | static void *m2v(phys_addr_t maddr) |
| 1575 | { |
| 1576 | return __ka(m2p(maddr)); |
| 1577 | } |
| 1578 | |
| 1579 | static void set_page_prot(void *addr, pgprot_t prot) |
| 1580 | { |
| 1581 | unsigned long pfn = __pa(addr) >> PAGE_SHIFT; |
| 1582 | pte_t pte = pfn_pte(pfn, prot); |
| 1583 | |
| 1584 | if (HYPERVISOR_update_va_mapping((unsigned long)addr, pte, 0)) |
| 1585 | BUG(); |
| 1586 | } |
| 1587 | |
| 1588 | static __init void xen_map_identity_early(pmd_t *pmd, unsigned long max_pfn) |
| 1589 | { |
| 1590 | unsigned pmdidx, pteidx; |
| 1591 | unsigned ident_pte; |
| 1592 | unsigned long pfn; |
| 1593 | |
| 1594 | ident_pte = 0; |
| 1595 | pfn = 0; |
| 1596 | for (pmdidx = 0; pmdidx < PTRS_PER_PMD && pfn < max_pfn; pmdidx++) { |
| 1597 | pte_t *pte_page; |
| 1598 | |
| 1599 | /* Reuse or allocate a page of ptes */ |
| 1600 | if (pmd_present(pmd[pmdidx])) |
| 1601 | pte_page = m2v(pmd[pmdidx].pmd); |
| 1602 | else { |
| 1603 | /* Check for free pte pages */ |
| 1604 | if (ident_pte == ARRAY_SIZE(level1_ident_pgt)) |
| 1605 | break; |
| 1606 | |
| 1607 | pte_page = &level1_ident_pgt[ident_pte]; |
| 1608 | ident_pte += PTRS_PER_PTE; |
| 1609 | |
| 1610 | pmd[pmdidx] = __pmd(__pa(pte_page) | _PAGE_TABLE); |
| 1611 | } |
| 1612 | |
| 1613 | /* Install mappings */ |
| 1614 | for (pteidx = 0; pteidx < PTRS_PER_PTE; pteidx++, pfn++) { |
| 1615 | pte_t pte; |
| 1616 | |
| 1617 | if (pfn > max_pfn_mapped) |
| 1618 | max_pfn_mapped = pfn; |
| 1619 | |
| 1620 | if (!pte_none(pte_page[pteidx])) |
| 1621 | continue; |
| 1622 | |
| 1623 | pte = pfn_pte(pfn, PAGE_KERNEL_EXEC); |
| 1624 | pte_page[pteidx] = pte; |
| 1625 | } |
| 1626 | } |
| 1627 | |
| 1628 | for (pteidx = 0; pteidx < ident_pte; pteidx += PTRS_PER_PTE) |
| 1629 | set_page_prot(&level1_ident_pgt[pteidx], PAGE_KERNEL_RO); |
| 1630 | |
| 1631 | set_page_prot(pmd, PAGE_KERNEL_RO); |
| 1632 | } |
| 1633 | |
| 1634 | #ifdef CONFIG_X86_64 |
| 1635 | static void convert_pfn_mfn(void *v) |
| 1636 | { |
| 1637 | pte_t *pte = v; |
| 1638 | int i; |
| 1639 | |
| 1640 | /* All levels are converted the same way, so just treat them |
| 1641 | as ptes. */ |
| 1642 | for (i = 0; i < PTRS_PER_PTE; i++) |
| 1643 | pte[i] = xen_make_pte(pte[i].pte); |
| 1644 | } |
| 1645 | |
| 1646 | /* |
| 1647 | * Set up the inital kernel pagetable. |
| 1648 | * |
| 1649 | * We can construct this by grafting the Xen provided pagetable into |
| 1650 | * head_64.S's preconstructed pagetables. We copy the Xen L2's into |
| 1651 | * level2_ident_pgt, level2_kernel_pgt and level2_fixmap_pgt. This |
| 1652 | * means that only the kernel has a physical mapping to start with - |
| 1653 | * but that's enough to get __va working. We need to fill in the rest |
| 1654 | * of the physical mapping once some sort of allocator has been set |
| 1655 | * up. |
| 1656 | */ |
| 1657 | __init pgd_t *xen_setup_kernel_pagetable(pgd_t *pgd, |
| 1658 | unsigned long max_pfn) |
| 1659 | { |
| 1660 | pud_t *l3; |
| 1661 | pmd_t *l2; |
| 1662 | |
| 1663 | /* Zap identity mapping */ |
| 1664 | init_level4_pgt[0] = __pgd(0); |
| 1665 | |
| 1666 | /* Pre-constructed entries are in pfn, so convert to mfn */ |
| 1667 | convert_pfn_mfn(init_level4_pgt); |
| 1668 | convert_pfn_mfn(level3_ident_pgt); |
| 1669 | convert_pfn_mfn(level3_kernel_pgt); |
| 1670 | |
| 1671 | l3 = m2v(pgd[pgd_index(__START_KERNEL_map)].pgd); |
| 1672 | l2 = m2v(l3[pud_index(__START_KERNEL_map)].pud); |
| 1673 | |
| 1674 | memcpy(level2_ident_pgt, l2, sizeof(pmd_t) * PTRS_PER_PMD); |
| 1675 | memcpy(level2_kernel_pgt, l2, sizeof(pmd_t) * PTRS_PER_PMD); |
| 1676 | |
| 1677 | l3 = m2v(pgd[pgd_index(__START_KERNEL_map + PMD_SIZE)].pgd); |
| 1678 | l2 = m2v(l3[pud_index(__START_KERNEL_map + PMD_SIZE)].pud); |
| 1679 | memcpy(level2_fixmap_pgt, l2, sizeof(pmd_t) * PTRS_PER_PMD); |
| 1680 | |
| 1681 | /* Set up identity map */ |
| 1682 | xen_map_identity_early(level2_ident_pgt, max_pfn); |
| 1683 | |
| 1684 | /* Make pagetable pieces RO */ |
| 1685 | set_page_prot(init_level4_pgt, PAGE_KERNEL_RO); |
| 1686 | set_page_prot(level3_ident_pgt, PAGE_KERNEL_RO); |
| 1687 | set_page_prot(level3_kernel_pgt, PAGE_KERNEL_RO); |
| 1688 | set_page_prot(level3_user_vsyscall, PAGE_KERNEL_RO); |
| 1689 | set_page_prot(level2_kernel_pgt, PAGE_KERNEL_RO); |
| 1690 | set_page_prot(level2_fixmap_pgt, PAGE_KERNEL_RO); |
| 1691 | |
| 1692 | /* Pin down new L4 */ |
| 1693 | pin_pagetable_pfn(MMUEXT_PIN_L4_TABLE, |
| 1694 | PFN_DOWN(__pa_symbol(init_level4_pgt))); |
| 1695 | |
| 1696 | /* Unpin Xen-provided one */ |
| 1697 | pin_pagetable_pfn(MMUEXT_UNPIN_TABLE, PFN_DOWN(__pa(pgd))); |
| 1698 | |
| 1699 | /* Switch over */ |
| 1700 | pgd = init_level4_pgt; |
| 1701 | |
| 1702 | /* |
| 1703 | * At this stage there can be no user pgd, and no page |
| 1704 | * structure to attach it to, so make sure we just set kernel |
| 1705 | * pgd. |
| 1706 | */ |
| 1707 | xen_mc_batch(); |
| 1708 | __xen_write_cr3(true, __pa(pgd)); |
| 1709 | xen_mc_issue(PARAVIRT_LAZY_CPU); |
| 1710 | |
| 1711 | reserve_early(__pa(xen_start_info->pt_base), |
| 1712 | __pa(xen_start_info->pt_base + |
| 1713 | xen_start_info->nr_pt_frames * PAGE_SIZE), |
| 1714 | "XEN PAGETABLES"); |
| 1715 | |
| 1716 | return pgd; |
| 1717 | } |
| 1718 | #else /* !CONFIG_X86_64 */ |
| 1719 | static pmd_t level2_kernel_pgt[PTRS_PER_PMD] __page_aligned_bss; |
| 1720 | |
| 1721 | __init pgd_t *xen_setup_kernel_pagetable(pgd_t *pgd, |
| 1722 | unsigned long max_pfn) |
| 1723 | { |
| 1724 | pmd_t *kernel_pmd; |
| 1725 | |
| 1726 | init_pg_tables_start = __pa(pgd); |
| 1727 | init_pg_tables_end = __pa(pgd) + xen_start_info->nr_pt_frames*PAGE_SIZE; |
| 1728 | max_pfn_mapped = PFN_DOWN(init_pg_tables_end + 512*1024); |
| 1729 | |
| 1730 | kernel_pmd = m2v(pgd[KERNEL_PGD_BOUNDARY].pgd); |
| 1731 | memcpy(level2_kernel_pgt, kernel_pmd, sizeof(pmd_t) * PTRS_PER_PMD); |
| 1732 | |
| 1733 | xen_map_identity_early(level2_kernel_pgt, max_pfn); |
| 1734 | |
| 1735 | memcpy(swapper_pg_dir, pgd, sizeof(pgd_t) * PTRS_PER_PGD); |
| 1736 | set_pgd(&swapper_pg_dir[KERNEL_PGD_BOUNDARY], |
| 1737 | __pgd(__pa(level2_kernel_pgt) | _PAGE_PRESENT)); |
| 1738 | |
| 1739 | set_page_prot(level2_kernel_pgt, PAGE_KERNEL_RO); |
| 1740 | set_page_prot(swapper_pg_dir, PAGE_KERNEL_RO); |
| 1741 | set_page_prot(empty_zero_page, PAGE_KERNEL_RO); |
| 1742 | |
| 1743 | pin_pagetable_pfn(MMUEXT_UNPIN_TABLE, PFN_DOWN(__pa(pgd))); |
| 1744 | |
| 1745 | xen_write_cr3(__pa(swapper_pg_dir)); |
| 1746 | |
| 1747 | pin_pagetable_pfn(MMUEXT_PIN_L3_TABLE, PFN_DOWN(__pa(swapper_pg_dir))); |
| 1748 | |
| 1749 | return swapper_pg_dir; |
| 1750 | } |
| 1751 | #endif /* CONFIG_X86_64 */ |
| 1752 | |
| 1753 | static void xen_set_fixmap(unsigned idx, unsigned long phys, pgprot_t prot) |
| 1754 | { |
| 1755 | pte_t pte; |
| 1756 | |
| 1757 | phys >>= PAGE_SHIFT; |
| 1758 | |
| 1759 | switch (idx) { |
| 1760 | case FIX_BTMAP_END ... FIX_BTMAP_BEGIN: |
| 1761 | #ifdef CONFIG_X86_F00F_BUG |
| 1762 | case FIX_F00F_IDT: |
| 1763 | #endif |
| 1764 | #ifdef CONFIG_X86_32 |
| 1765 | case FIX_WP_TEST: |
| 1766 | case FIX_VDSO: |
| 1767 | # ifdef CONFIG_HIGHMEM |
| 1768 | case FIX_KMAP_BEGIN ... FIX_KMAP_END: |
| 1769 | # endif |
| 1770 | #else |
| 1771 | case VSYSCALL_LAST_PAGE ... VSYSCALL_FIRST_PAGE: |
| 1772 | #endif |
| 1773 | #ifdef CONFIG_X86_LOCAL_APIC |
| 1774 | case FIX_APIC_BASE: /* maps dummy local APIC */ |
| 1775 | #endif |
| 1776 | pte = pfn_pte(phys, prot); |
| 1777 | break; |
| 1778 | |
| 1779 | default: |
| 1780 | pte = mfn_pte(phys, prot); |
| 1781 | break; |
| 1782 | } |
| 1783 | |
| 1784 | __native_set_fixmap(idx, pte); |
| 1785 | |
| 1786 | #ifdef CONFIG_X86_64 |
| 1787 | /* Replicate changes to map the vsyscall page into the user |
| 1788 | pagetable vsyscall mapping. */ |
| 1789 | if (idx >= VSYSCALL_LAST_PAGE && idx <= VSYSCALL_FIRST_PAGE) { |
| 1790 | unsigned long vaddr = __fix_to_virt(idx); |
| 1791 | set_pte_vaddr_pud(level3_user_vsyscall, vaddr, pte); |
| 1792 | } |
| 1793 | #endif |
| 1794 | } |
| 1795 | |
| 1796 | __init void xen_post_allocator_init(void) |
| 1797 | { |
| 1798 | pv_mmu_ops.set_pte = xen_set_pte; |
| 1799 | pv_mmu_ops.set_pmd = xen_set_pmd; |
| 1800 | pv_mmu_ops.set_pud = xen_set_pud; |
| 1801 | #if PAGETABLE_LEVELS == 4 |
| 1802 | pv_mmu_ops.set_pgd = xen_set_pgd; |
| 1803 | #endif |
| 1804 | |
| 1805 | /* This will work as long as patching hasn't happened yet |
| 1806 | (which it hasn't) */ |
| 1807 | pv_mmu_ops.alloc_pte = xen_alloc_pte; |
| 1808 | pv_mmu_ops.alloc_pmd = xen_alloc_pmd; |
| 1809 | pv_mmu_ops.release_pte = xen_release_pte; |
| 1810 | pv_mmu_ops.release_pmd = xen_release_pmd; |
| 1811 | #if PAGETABLE_LEVELS == 4 |
| 1812 | pv_mmu_ops.alloc_pud = xen_alloc_pud; |
| 1813 | pv_mmu_ops.release_pud = xen_release_pud; |
| 1814 | #endif |
| 1815 | |
| 1816 | #ifdef CONFIG_X86_64 |
| 1817 | SetPagePinned(virt_to_page(level3_user_vsyscall)); |
| 1818 | #endif |
| 1819 | xen_mark_init_mm_pinned(); |
| 1820 | } |
| 1821 | |
| 1822 | |
| 1823 | const struct pv_mmu_ops xen_mmu_ops __initdata = { |
| 1824 | .pagetable_setup_start = xen_pagetable_setup_start, |
| 1825 | .pagetable_setup_done = xen_pagetable_setup_done, |
| 1826 | |
| 1827 | .read_cr2 = xen_read_cr2, |
| 1828 | .write_cr2 = xen_write_cr2, |
| 1829 | |
| 1830 | .read_cr3 = xen_read_cr3, |
| 1831 | .write_cr3 = xen_write_cr3, |
| 1832 | |
| 1833 | .flush_tlb_user = xen_flush_tlb, |
| 1834 | .flush_tlb_kernel = xen_flush_tlb, |
| 1835 | .flush_tlb_single = xen_flush_tlb_single, |
| 1836 | .flush_tlb_others = xen_flush_tlb_others, |
| 1837 | |
| 1838 | .pte_update = paravirt_nop, |
| 1839 | .pte_update_defer = paravirt_nop, |
| 1840 | |
| 1841 | .pgd_alloc = xen_pgd_alloc, |
| 1842 | .pgd_free = xen_pgd_free, |
| 1843 | |
| 1844 | .alloc_pte = xen_alloc_pte_init, |
| 1845 | .release_pte = xen_release_pte_init, |
| 1846 | .alloc_pmd = xen_alloc_pte_init, |
| 1847 | .alloc_pmd_clone = paravirt_nop, |
| 1848 | .release_pmd = xen_release_pte_init, |
| 1849 | |
| 1850 | #ifdef CONFIG_HIGHPTE |
| 1851 | .kmap_atomic_pte = xen_kmap_atomic_pte, |
| 1852 | #endif |
| 1853 | |
| 1854 | #ifdef CONFIG_X86_64 |
| 1855 | .set_pte = xen_set_pte, |
| 1856 | #else |
| 1857 | .set_pte = xen_set_pte_init, |
| 1858 | #endif |
| 1859 | .set_pte_at = xen_set_pte_at, |
| 1860 | .set_pmd = xen_set_pmd_hyper, |
| 1861 | |
| 1862 | .ptep_modify_prot_start = __ptep_modify_prot_start, |
| 1863 | .ptep_modify_prot_commit = __ptep_modify_prot_commit, |
| 1864 | |
Jeremy Fitzhardinge | da5de7c | 2009-01-28 14:35:07 -0800 | [diff] [blame] | 1865 | .pte_val = PV_CALLEE_SAVE(xen_pte_val), |
| 1866 | .pgd_val = PV_CALLEE_SAVE(xen_pgd_val), |
Jeremy Fitzhardinge | 319f3ba | 2009-01-28 14:35:01 -0800 | [diff] [blame] | 1867 | |
Jeremy Fitzhardinge | da5de7c | 2009-01-28 14:35:07 -0800 | [diff] [blame] | 1868 | .make_pte = PV_CALLEE_SAVE(xen_make_pte), |
| 1869 | .make_pgd = PV_CALLEE_SAVE(xen_make_pgd), |
Jeremy Fitzhardinge | 319f3ba | 2009-01-28 14:35:01 -0800 | [diff] [blame] | 1870 | |
| 1871 | #ifdef CONFIG_X86_PAE |
| 1872 | .set_pte_atomic = xen_set_pte_atomic, |
| 1873 | .set_pte_present = xen_set_pte_at, |
| 1874 | .pte_clear = xen_pte_clear, |
| 1875 | .pmd_clear = xen_pmd_clear, |
| 1876 | #endif /* CONFIG_X86_PAE */ |
| 1877 | .set_pud = xen_set_pud_hyper, |
| 1878 | |
Jeremy Fitzhardinge | da5de7c | 2009-01-28 14:35:07 -0800 | [diff] [blame] | 1879 | .make_pmd = PV_CALLEE_SAVE(xen_make_pmd), |
| 1880 | .pmd_val = PV_CALLEE_SAVE(xen_pmd_val), |
Jeremy Fitzhardinge | 319f3ba | 2009-01-28 14:35:01 -0800 | [diff] [blame] | 1881 | |
| 1882 | #if PAGETABLE_LEVELS == 4 |
Jeremy Fitzhardinge | da5de7c | 2009-01-28 14:35:07 -0800 | [diff] [blame] | 1883 | .pud_val = PV_CALLEE_SAVE(xen_pud_val), |
| 1884 | .make_pud = PV_CALLEE_SAVE(xen_make_pud), |
Jeremy Fitzhardinge | 319f3ba | 2009-01-28 14:35:01 -0800 | [diff] [blame] | 1885 | .set_pgd = xen_set_pgd_hyper, |
| 1886 | |
| 1887 | .alloc_pud = xen_alloc_pte_init, |
| 1888 | .release_pud = xen_release_pte_init, |
| 1889 | #endif /* PAGETABLE_LEVELS == 4 */ |
| 1890 | |
| 1891 | .activate_mm = xen_activate_mm, |
| 1892 | .dup_mmap = xen_dup_mmap, |
| 1893 | .exit_mmap = xen_exit_mmap, |
| 1894 | |
| 1895 | .lazy_mode = { |
| 1896 | .enter = paravirt_enter_lazy_mmu, |
| 1897 | .leave = xen_leave_lazy, |
| 1898 | }, |
| 1899 | |
| 1900 | .set_fixmap = xen_set_fixmap, |
| 1901 | }; |
| 1902 | |
| 1903 | |
Jeremy Fitzhardinge | 994025c | 2008-08-20 17:02:19 -0700 | [diff] [blame] | 1904 | #ifdef CONFIG_XEN_DEBUG_FS |
| 1905 | |
| 1906 | static struct dentry *d_mmu_debug; |
| 1907 | |
| 1908 | static int __init xen_mmu_debugfs(void) |
| 1909 | { |
| 1910 | struct dentry *d_xen = xen_init_debugfs(); |
| 1911 | |
| 1912 | if (d_xen == NULL) |
| 1913 | return -ENOMEM; |
| 1914 | |
| 1915 | d_mmu_debug = debugfs_create_dir("mmu", d_xen); |
| 1916 | |
| 1917 | debugfs_create_u8("zero_stats", 0644, d_mmu_debug, &zero_stats); |
| 1918 | |
| 1919 | debugfs_create_u32("pgd_update", 0444, d_mmu_debug, &mmu_stats.pgd_update); |
| 1920 | debugfs_create_u32("pgd_update_pinned", 0444, d_mmu_debug, |
| 1921 | &mmu_stats.pgd_update_pinned); |
| 1922 | debugfs_create_u32("pgd_update_batched", 0444, d_mmu_debug, |
| 1923 | &mmu_stats.pgd_update_pinned); |
| 1924 | |
| 1925 | debugfs_create_u32("pud_update", 0444, d_mmu_debug, &mmu_stats.pud_update); |
| 1926 | debugfs_create_u32("pud_update_pinned", 0444, d_mmu_debug, |
| 1927 | &mmu_stats.pud_update_pinned); |
| 1928 | debugfs_create_u32("pud_update_batched", 0444, d_mmu_debug, |
| 1929 | &mmu_stats.pud_update_pinned); |
| 1930 | |
| 1931 | debugfs_create_u32("pmd_update", 0444, d_mmu_debug, &mmu_stats.pmd_update); |
| 1932 | debugfs_create_u32("pmd_update_pinned", 0444, d_mmu_debug, |
| 1933 | &mmu_stats.pmd_update_pinned); |
| 1934 | debugfs_create_u32("pmd_update_batched", 0444, d_mmu_debug, |
| 1935 | &mmu_stats.pmd_update_pinned); |
| 1936 | |
| 1937 | debugfs_create_u32("pte_update", 0444, d_mmu_debug, &mmu_stats.pte_update); |
| 1938 | // debugfs_create_u32("pte_update_pinned", 0444, d_mmu_debug, |
| 1939 | // &mmu_stats.pte_update_pinned); |
| 1940 | debugfs_create_u32("pte_update_batched", 0444, d_mmu_debug, |
| 1941 | &mmu_stats.pte_update_pinned); |
| 1942 | |
| 1943 | debugfs_create_u32("mmu_update", 0444, d_mmu_debug, &mmu_stats.mmu_update); |
| 1944 | debugfs_create_u32("mmu_update_extended", 0444, d_mmu_debug, |
| 1945 | &mmu_stats.mmu_update_extended); |
| 1946 | xen_debugfs_create_u32_array("mmu_update_histo", 0444, d_mmu_debug, |
| 1947 | mmu_stats.mmu_update_histo, 20); |
| 1948 | |
| 1949 | debugfs_create_u32("set_pte_at", 0444, d_mmu_debug, &mmu_stats.set_pte_at); |
| 1950 | debugfs_create_u32("set_pte_at_batched", 0444, d_mmu_debug, |
| 1951 | &mmu_stats.set_pte_at_batched); |
| 1952 | debugfs_create_u32("set_pte_at_current", 0444, d_mmu_debug, |
| 1953 | &mmu_stats.set_pte_at_current); |
| 1954 | debugfs_create_u32("set_pte_at_kernel", 0444, d_mmu_debug, |
| 1955 | &mmu_stats.set_pte_at_kernel); |
| 1956 | |
| 1957 | debugfs_create_u32("prot_commit", 0444, d_mmu_debug, &mmu_stats.prot_commit); |
| 1958 | debugfs_create_u32("prot_commit_batched", 0444, d_mmu_debug, |
| 1959 | &mmu_stats.prot_commit_batched); |
| 1960 | |
| 1961 | return 0; |
| 1962 | } |
| 1963 | fs_initcall(xen_mmu_debugfs); |
| 1964 | |
| 1965 | #endif /* CONFIG_XEN_DEBUG_FS */ |