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 | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 43 | #include <linux/bug.h> |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 44 | |
| 45 | #include <asm/pgtable.h> |
| 46 | #include <asm/tlbflush.h> |
| 47 | #include <asm/mmu_context.h> |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 48 | #include <asm/paravirt.h> |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 49 | |
| 50 | #include <asm/xen/hypercall.h> |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 51 | #include <asm/xen/hypervisor.h> |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 52 | |
| 53 | #include <xen/page.h> |
| 54 | #include <xen/interface/xen.h> |
| 55 | |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 56 | #include "multicalls.h" |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 57 | #include "mmu.h" |
| 58 | |
Jeremy Fitzhardinge | d451bb7 | 2008-05-26 23:31:18 +0100 | [diff] [blame] | 59 | #define P2M_ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(unsigned long)) |
Jeremy Fitzhardinge | cf0923e | 2008-05-26 23:31:20 +0100 | [diff] [blame] | 60 | #define TOP_ENTRIES (MAX_DOMAIN_PAGES / P2M_ENTRIES_PER_PAGE) |
Jeremy Fitzhardinge | d451bb7 | 2008-05-26 23:31:18 +0100 | [diff] [blame] | 61 | |
Jeremy Fitzhardinge | cf0923e | 2008-05-26 23:31:20 +0100 | [diff] [blame] | 62 | /* Placeholder for holes in the address space */ |
| 63 | static unsigned long p2m_missing[P2M_ENTRIES_PER_PAGE] |
| 64 | __attribute__((section(".data.page_aligned"))) = |
| 65 | { [ 0 ... P2M_ENTRIES_PER_PAGE-1 ] = ~0UL }; |
| 66 | |
| 67 | /* Array of pointers to pages containing p2m entries */ |
| 68 | static unsigned long *p2m_top[TOP_ENTRIES] |
| 69 | __attribute__((section(".data.page_aligned"))) = |
| 70 | { [ 0 ... TOP_ENTRIES - 1] = &p2m_missing[0] }; |
Jeremy Fitzhardinge | d451bb7 | 2008-05-26 23:31:18 +0100 | [diff] [blame] | 71 | |
Jeremy Fitzhardinge | d5edbc1 | 2008-05-26 23:31:22 +0100 | [diff] [blame] | 72 | /* Arrays of p2m arrays expressed in mfns used for save/restore */ |
| 73 | static unsigned long p2m_top_mfn[TOP_ENTRIES] |
| 74 | __attribute__((section(".bss.page_aligned"))); |
| 75 | |
Ingo Molnar | b20aecc | 2008-05-28 14:24:38 +0200 | [diff] [blame] | 76 | static unsigned long p2m_top_mfn_list[ |
| 77 | PAGE_ALIGN(TOP_ENTRIES / P2M_ENTRIES_PER_PAGE)] |
Jeremy Fitzhardinge | d5edbc1 | 2008-05-26 23:31:22 +0100 | [diff] [blame] | 78 | __attribute__((section(".bss.page_aligned"))); |
| 79 | |
Jeremy Fitzhardinge | d451bb7 | 2008-05-26 23:31:18 +0100 | [diff] [blame] | 80 | static inline unsigned p2m_top_index(unsigned long pfn) |
| 81 | { |
Jeremy Fitzhardinge | 8006ec3 | 2008-05-26 23:31:19 +0100 | [diff] [blame] | 82 | BUG_ON(pfn >= MAX_DOMAIN_PAGES); |
Jeremy Fitzhardinge | d451bb7 | 2008-05-26 23:31:18 +0100 | [diff] [blame] | 83 | return pfn / P2M_ENTRIES_PER_PAGE; |
| 84 | } |
| 85 | |
| 86 | static inline unsigned p2m_index(unsigned long pfn) |
| 87 | { |
| 88 | return pfn % P2M_ENTRIES_PER_PAGE; |
| 89 | } |
| 90 | |
Jeremy Fitzhardinge | d5edbc1 | 2008-05-26 23:31:22 +0100 | [diff] [blame] | 91 | /* Build the parallel p2m_top_mfn structures */ |
| 92 | void xen_setup_mfn_list_list(void) |
| 93 | { |
| 94 | unsigned pfn, idx; |
| 95 | |
| 96 | for(pfn = 0; pfn < MAX_DOMAIN_PAGES; pfn += P2M_ENTRIES_PER_PAGE) { |
| 97 | unsigned topidx = p2m_top_index(pfn); |
| 98 | |
| 99 | p2m_top_mfn[topidx] = virt_to_mfn(p2m_top[topidx]); |
| 100 | } |
| 101 | |
| 102 | for(idx = 0; idx < ARRAY_SIZE(p2m_top_mfn_list); idx++) { |
| 103 | unsigned topidx = idx * P2M_ENTRIES_PER_PAGE; |
| 104 | p2m_top_mfn_list[idx] = virt_to_mfn(&p2m_top_mfn[topidx]); |
| 105 | } |
| 106 | |
| 107 | BUG_ON(HYPERVISOR_shared_info == &xen_dummy_shared_info); |
| 108 | |
| 109 | HYPERVISOR_shared_info->arch.pfn_to_mfn_frame_list_list = |
| 110 | virt_to_mfn(p2m_top_mfn_list); |
| 111 | HYPERVISOR_shared_info->arch.max_pfn = xen_start_info->nr_pages; |
| 112 | } |
| 113 | |
| 114 | /* 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] | 115 | void __init xen_build_dynamic_phys_to_machine(void) |
| 116 | { |
Jeremy Fitzhardinge | d451bb7 | 2008-05-26 23:31:18 +0100 | [diff] [blame] | 117 | unsigned long *mfn_list = (unsigned long *)xen_start_info->mfn_list; |
Jeremy Fitzhardinge | 8006ec3 | 2008-05-26 23:31:19 +0100 | [diff] [blame] | 118 | 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] | 119 | unsigned pfn; |
Jeremy Fitzhardinge | d451bb7 | 2008-05-26 23:31:18 +0100 | [diff] [blame] | 120 | |
Jeremy Fitzhardinge | 8006ec3 | 2008-05-26 23:31:19 +0100 | [diff] [blame] | 121 | for(pfn = 0; pfn < max_pfn; pfn += P2M_ENTRIES_PER_PAGE) { |
Jeremy Fitzhardinge | d451bb7 | 2008-05-26 23:31:18 +0100 | [diff] [blame] | 122 | unsigned topidx = p2m_top_index(pfn); |
| 123 | |
| 124 | p2m_top[topidx] = &mfn_list[pfn]; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | unsigned long get_phys_to_machine(unsigned long pfn) |
| 129 | { |
| 130 | unsigned topidx, idx; |
| 131 | |
Jeremy Fitzhardinge | 8006ec3 | 2008-05-26 23:31:19 +0100 | [diff] [blame] | 132 | if (unlikely(pfn >= MAX_DOMAIN_PAGES)) |
| 133 | return INVALID_P2M_ENTRY; |
| 134 | |
Jeremy Fitzhardinge | d451bb7 | 2008-05-26 23:31:18 +0100 | [diff] [blame] | 135 | topidx = p2m_top_index(pfn); |
Jeremy Fitzhardinge | d451bb7 | 2008-05-26 23:31:18 +0100 | [diff] [blame] | 136 | idx = p2m_index(pfn); |
| 137 | return p2m_top[topidx][idx]; |
| 138 | } |
Ingo Molnar | 15ce6005 | 2008-06-02 13:20:11 +0200 | [diff] [blame^] | 139 | EXPORT_SYMBOL_GPL(get_phys_to_machine); |
Jeremy Fitzhardinge | d451bb7 | 2008-05-26 23:31:18 +0100 | [diff] [blame] | 140 | |
Jeremy Fitzhardinge | d5edbc1 | 2008-05-26 23:31:22 +0100 | [diff] [blame] | 141 | static void alloc_p2m(unsigned long **pp, unsigned long *mfnp) |
Jeremy Fitzhardinge | d451bb7 | 2008-05-26 23:31:18 +0100 | [diff] [blame] | 142 | { |
| 143 | unsigned long *p; |
| 144 | unsigned i; |
| 145 | |
| 146 | p = (void *)__get_free_page(GFP_KERNEL | __GFP_NOFAIL); |
| 147 | BUG_ON(p == NULL); |
| 148 | |
| 149 | for(i = 0; i < P2M_ENTRIES_PER_PAGE; i++) |
| 150 | p[i] = INVALID_P2M_ENTRY; |
| 151 | |
Jeremy Fitzhardinge | cf0923e | 2008-05-26 23:31:20 +0100 | [diff] [blame] | 152 | if (cmpxchg(pp, p2m_missing, p) != p2m_missing) |
Jeremy Fitzhardinge | d451bb7 | 2008-05-26 23:31:18 +0100 | [diff] [blame] | 153 | free_page((unsigned long)p); |
Jeremy Fitzhardinge | d5edbc1 | 2008-05-26 23:31:22 +0100 | [diff] [blame] | 154 | else |
| 155 | *mfnp = virt_to_mfn(p); |
Jeremy Fitzhardinge | d451bb7 | 2008-05-26 23:31:18 +0100 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | void set_phys_to_machine(unsigned long pfn, unsigned long mfn) |
| 159 | { |
| 160 | unsigned topidx, idx; |
| 161 | |
| 162 | if (unlikely(xen_feature(XENFEAT_auto_translated_physmap))) { |
| 163 | BUG_ON(pfn != mfn && mfn != INVALID_P2M_ENTRY); |
| 164 | return; |
| 165 | } |
| 166 | |
Jeremy Fitzhardinge | 8006ec3 | 2008-05-26 23:31:19 +0100 | [diff] [blame] | 167 | if (unlikely(pfn >= MAX_DOMAIN_PAGES)) { |
| 168 | BUG_ON(mfn != INVALID_P2M_ENTRY); |
| 169 | return; |
| 170 | } |
| 171 | |
Jeremy Fitzhardinge | d451bb7 | 2008-05-26 23:31:18 +0100 | [diff] [blame] | 172 | topidx = p2m_top_index(pfn); |
Jeremy Fitzhardinge | cf0923e | 2008-05-26 23:31:20 +0100 | [diff] [blame] | 173 | if (p2m_top[topidx] == p2m_missing) { |
Jeremy Fitzhardinge | d451bb7 | 2008-05-26 23:31:18 +0100 | [diff] [blame] | 174 | /* no need to allocate a page to store an invalid entry */ |
| 175 | if (mfn == INVALID_P2M_ENTRY) |
| 176 | return; |
Jeremy Fitzhardinge | d5edbc1 | 2008-05-26 23:31:22 +0100 | [diff] [blame] | 177 | alloc_p2m(&p2m_top[topidx], &p2m_top_mfn[topidx]); |
Jeremy Fitzhardinge | d451bb7 | 2008-05-26 23:31:18 +0100 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | idx = p2m_index(pfn); |
| 181 | p2m_top[topidx][idx] = mfn; |
| 182 | } |
| 183 | |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 184 | xmaddr_t arbitrary_virt_to_machine(unsigned long address) |
| 185 | { |
Harvey Harrison | da7bfc5 | 2008-02-09 23:24:08 +0100 | [diff] [blame] | 186 | unsigned int level; |
Ingo Molnar | f0646e4 | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 187 | pte_t *pte = lookup_address(address, &level); |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 188 | unsigned offset = address & PAGE_MASK; |
| 189 | |
| 190 | BUG_ON(pte == NULL); |
| 191 | |
| 192 | return XMADDR((pte_mfn(*pte) << PAGE_SHIFT) + offset); |
| 193 | } |
| 194 | |
| 195 | void make_lowmem_page_readonly(void *vaddr) |
| 196 | { |
| 197 | pte_t *pte, ptev; |
| 198 | unsigned long address = (unsigned long)vaddr; |
Harvey Harrison | da7bfc5 | 2008-02-09 23:24:08 +0100 | [diff] [blame] | 199 | unsigned int level; |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 200 | |
Ingo Molnar | f0646e4 | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 201 | pte = lookup_address(address, &level); |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 202 | BUG_ON(pte == NULL); |
| 203 | |
| 204 | ptev = pte_wrprotect(*pte); |
| 205 | |
| 206 | if (HYPERVISOR_update_va_mapping(address, ptev, 0)) |
| 207 | BUG(); |
| 208 | } |
| 209 | |
| 210 | void make_lowmem_page_readwrite(void *vaddr) |
| 211 | { |
| 212 | pte_t *pte, ptev; |
| 213 | unsigned long address = (unsigned long)vaddr; |
Harvey Harrison | da7bfc5 | 2008-02-09 23:24:08 +0100 | [diff] [blame] | 214 | unsigned int level; |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 215 | |
Ingo Molnar | f0646e4 | 2008-01-30 13:33:43 +0100 | [diff] [blame] | 216 | pte = lookup_address(address, &level); |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 217 | BUG_ON(pte == NULL); |
| 218 | |
| 219 | ptev = pte_mkwrite(*pte); |
| 220 | |
| 221 | if (HYPERVISOR_update_va_mapping(address, ptev, 0)) |
| 222 | BUG(); |
| 223 | } |
| 224 | |
| 225 | |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 226 | void xen_set_pmd(pmd_t *ptr, pmd_t val) |
| 227 | { |
Jeremy Fitzhardinge | d66bf8f | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 228 | struct multicall_space mcs; |
| 229 | struct mmu_update *u; |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 230 | |
Jeremy Fitzhardinge | d66bf8f | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 231 | preempt_disable(); |
| 232 | |
| 233 | mcs = xen_mc_entry(sizeof(*u)); |
| 234 | u = mcs.args; |
| 235 | u->ptr = virt_to_machine(ptr).maddr; |
| 236 | u->val = pmd_val_ma(val); |
| 237 | MULTI_mmu_update(mcs.mc, u, 1, NULL, DOMID_SELF); |
| 238 | |
| 239 | xen_mc_issue(PARAVIRT_LAZY_MMU); |
| 240 | |
| 241 | preempt_enable(); |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 242 | } |
| 243 | |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 244 | /* |
| 245 | * Associate a virtual page frame with a given physical page frame |
| 246 | * and protection flags for that frame. |
| 247 | */ |
| 248 | void set_pte_mfn(unsigned long vaddr, unsigned long mfn, pgprot_t flags) |
| 249 | { |
| 250 | pgd_t *pgd; |
| 251 | pud_t *pud; |
| 252 | pmd_t *pmd; |
| 253 | pte_t *pte; |
| 254 | |
| 255 | pgd = swapper_pg_dir + pgd_index(vaddr); |
| 256 | if (pgd_none(*pgd)) { |
| 257 | BUG(); |
| 258 | return; |
| 259 | } |
| 260 | pud = pud_offset(pgd, vaddr); |
| 261 | if (pud_none(*pud)) { |
| 262 | BUG(); |
| 263 | return; |
| 264 | } |
| 265 | pmd = pmd_offset(pud, vaddr); |
| 266 | if (pmd_none(*pmd)) { |
| 267 | BUG(); |
| 268 | return; |
| 269 | } |
| 270 | pte = pte_offset_kernel(pmd, vaddr); |
| 271 | /* <mfn,flags> stored as-is, to permit clearing entries */ |
| 272 | xen_set_pte(pte, mfn_pte(mfn, flags)); |
| 273 | |
| 274 | /* |
| 275 | * It's enough to flush this one mapping. |
| 276 | * (PGE mappings get flushed as well) |
| 277 | */ |
| 278 | __flush_tlb_one(vaddr); |
| 279 | } |
| 280 | |
| 281 | void xen_set_pte_at(struct mm_struct *mm, unsigned long addr, |
| 282 | pte_t *ptep, pte_t pteval) |
| 283 | { |
Jeremy Fitzhardinge | 2bd5003 | 2008-04-02 10:54:10 -0700 | [diff] [blame] | 284 | /* updates to init_mm may be done without lock */ |
| 285 | if (mm == &init_mm) |
| 286 | preempt_disable(); |
| 287 | |
Jeremy Fitzhardinge | d66bf8f | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 288 | if (mm == current->mm || mm == &init_mm) { |
Jeremy Fitzhardinge | 8965c1c | 2007-10-16 11:51:29 -0700 | [diff] [blame] | 289 | if (paravirt_get_lazy_mode() == PARAVIRT_LAZY_MMU) { |
Jeremy Fitzhardinge | d66bf8f | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 290 | struct multicall_space mcs; |
| 291 | mcs = xen_mc_entry(0); |
| 292 | |
| 293 | MULTI_update_va_mapping(mcs.mc, addr, pteval, 0); |
| 294 | xen_mc_issue(PARAVIRT_LAZY_MMU); |
Jeremy Fitzhardinge | 2bd5003 | 2008-04-02 10:54:10 -0700 | [diff] [blame] | 295 | goto out; |
Jeremy Fitzhardinge | d66bf8f | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 296 | } else |
| 297 | if (HYPERVISOR_update_va_mapping(addr, pteval, 0) == 0) |
Jeremy Fitzhardinge | 2bd5003 | 2008-04-02 10:54:10 -0700 | [diff] [blame] | 298 | goto out; |
Jeremy Fitzhardinge | d66bf8f | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 299 | } |
| 300 | xen_set_pte(ptep, pteval); |
Jeremy Fitzhardinge | 2bd5003 | 2008-04-02 10:54:10 -0700 | [diff] [blame] | 301 | |
| 302 | out: |
| 303 | if (mm == &init_mm) |
| 304 | preempt_enable(); |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 305 | } |
| 306 | |
Jeremy Fitzhardinge | 947a69c | 2008-03-17 16:37:09 -0700 | [diff] [blame] | 307 | pteval_t xen_pte_val(pte_t pte) |
| 308 | { |
| 309 | pteval_t ret = pte.pte; |
| 310 | |
| 311 | if (ret & _PAGE_PRESENT) |
| 312 | ret = machine_to_phys(XMADDR(ret)).paddr | _PAGE_PRESENT; |
| 313 | |
| 314 | return ret; |
| 315 | } |
| 316 | |
| 317 | pgdval_t xen_pgd_val(pgd_t pgd) |
| 318 | { |
| 319 | pgdval_t ret = pgd.pgd; |
| 320 | if (ret & _PAGE_PRESENT) |
| 321 | ret = machine_to_phys(XMADDR(ret)).paddr | _PAGE_PRESENT; |
| 322 | return ret; |
| 323 | } |
| 324 | |
| 325 | pte_t xen_make_pte(pteval_t pte) |
| 326 | { |
| 327 | if (pte & _PAGE_PRESENT) { |
| 328 | pte = phys_to_machine(XPADDR(pte)).maddr; |
| 329 | pte &= ~(_PAGE_PCD | _PAGE_PWT); |
| 330 | } |
| 331 | |
| 332 | return (pte_t){ .pte = pte }; |
| 333 | } |
| 334 | |
| 335 | pgd_t xen_make_pgd(pgdval_t pgd) |
| 336 | { |
| 337 | if (pgd & _PAGE_PRESENT) |
| 338 | pgd = phys_to_machine(XPADDR(pgd)).maddr; |
| 339 | |
| 340 | return (pgd_t){ pgd }; |
| 341 | } |
| 342 | |
| 343 | pmdval_t xen_pmd_val(pmd_t pmd) |
| 344 | { |
| 345 | pmdval_t ret = native_pmd_val(pmd); |
| 346 | if (ret & _PAGE_PRESENT) |
| 347 | ret = machine_to_phys(XMADDR(ret)).paddr | _PAGE_PRESENT; |
| 348 | return ret; |
| 349 | } |
Jeremy Fitzhardinge | 3843fc2 | 2008-05-09 12:05:57 +0100 | [diff] [blame] | 350 | |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 351 | void xen_set_pud(pud_t *ptr, pud_t val) |
| 352 | { |
Jeremy Fitzhardinge | d66bf8f | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 353 | struct multicall_space mcs; |
| 354 | struct mmu_update *u; |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 355 | |
Jeremy Fitzhardinge | d66bf8f | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 356 | preempt_disable(); |
| 357 | |
| 358 | mcs = xen_mc_entry(sizeof(*u)); |
| 359 | u = mcs.args; |
| 360 | u->ptr = virt_to_machine(ptr).maddr; |
| 361 | u->val = pud_val_ma(val); |
| 362 | MULTI_mmu_update(mcs.mc, u, 1, NULL, DOMID_SELF); |
| 363 | |
| 364 | xen_mc_issue(PARAVIRT_LAZY_MMU); |
| 365 | |
| 366 | preempt_enable(); |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | void xen_set_pte(pte_t *ptep, pte_t pte) |
| 370 | { |
| 371 | ptep->pte_high = pte.pte_high; |
| 372 | smp_wmb(); |
| 373 | ptep->pte_low = pte.pte_low; |
| 374 | } |
| 375 | |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 376 | void xen_set_pte_atomic(pte_t *ptep, pte_t pte) |
| 377 | { |
| 378 | set_64bit((u64 *)ptep, pte_val_ma(pte)); |
| 379 | } |
| 380 | |
| 381 | void xen_pte_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep) |
| 382 | { |
| 383 | ptep->pte_low = 0; |
| 384 | smp_wmb(); /* make sure low gets written first */ |
| 385 | ptep->pte_high = 0; |
| 386 | } |
| 387 | |
| 388 | void xen_pmd_clear(pmd_t *pmdp) |
| 389 | { |
| 390 | xen_set_pmd(pmdp, __pmd(0)); |
| 391 | } |
| 392 | |
Jeremy Fitzhardinge | abf3303 | 2008-03-17 16:37:07 -0700 | [diff] [blame] | 393 | pmd_t xen_make_pmd(pmdval_t pmd) |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 394 | { |
Jeremy Fitzhardinge | 430442e | 2008-03-17 16:37:08 -0700 | [diff] [blame] | 395 | if (pmd & _PAGE_PRESENT) |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 396 | pmd = phys_to_machine(XPADDR(pmd)).maddr; |
| 397 | |
Jeremy Fitzhardinge | 947a69c | 2008-03-17 16:37:09 -0700 | [diff] [blame] | 398 | return native_make_pmd(pmd); |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 399 | } |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 400 | |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 401 | /* |
| 402 | (Yet another) pagetable walker. This one is intended for pinning a |
| 403 | pagetable. This means that it walks a pagetable and calls the |
| 404 | callback function on each page it finds making up the page table, |
| 405 | at every level. It walks the entire pagetable, but it only bothers |
| 406 | pinning pte pages which are below pte_limit. In the normal case |
| 407 | this will be TASK_SIZE, but at boot we need to pin up to |
| 408 | FIXADDR_TOP. But the important bit is that we don't pin beyond |
| 409 | there, because then we start getting into Xen's ptes. |
| 410 | */ |
Jeremy Fitzhardinge | 7426071 | 2007-10-16 11:51:30 -0700 | [diff] [blame] | 411 | static int pgd_walk(pgd_t *pgd_base, int (*func)(struct page *, enum pt_level), |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 412 | unsigned long limit) |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 413 | { |
| 414 | pgd_t *pgd = pgd_base; |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 415 | int flush = 0; |
| 416 | unsigned long addr = 0; |
| 417 | unsigned long pgd_next; |
| 418 | |
| 419 | BUG_ON(limit > FIXADDR_TOP); |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 420 | |
| 421 | if (xen_feature(XENFEAT_auto_translated_physmap)) |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 422 | return 0; |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 423 | |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 424 | for (; addr != FIXADDR_TOP; pgd++, addr = pgd_next) { |
| 425 | pud_t *pud; |
| 426 | unsigned long pud_limit, pud_next; |
| 427 | |
| 428 | pgd_next = pud_limit = pgd_addr_end(addr, FIXADDR_TOP); |
| 429 | |
| 430 | if (!pgd_val(*pgd)) |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 431 | continue; |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 432 | |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 433 | pud = pud_offset(pgd, 0); |
| 434 | |
| 435 | if (PTRS_PER_PUD > 1) /* not folded */ |
Jeremy Fitzhardinge | 7426071 | 2007-10-16 11:51:30 -0700 | [diff] [blame] | 436 | flush |= (*func)(virt_to_page(pud), PT_PUD); |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 437 | |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 438 | for (; addr != pud_limit; pud++, addr = pud_next) { |
| 439 | pmd_t *pmd; |
| 440 | unsigned long pmd_limit; |
| 441 | |
| 442 | pud_next = pud_addr_end(addr, pud_limit); |
| 443 | |
| 444 | if (pud_next < limit) |
| 445 | pmd_limit = pud_next; |
| 446 | else |
| 447 | pmd_limit = limit; |
| 448 | |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 449 | if (pud_none(*pud)) |
| 450 | continue; |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 451 | |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 452 | pmd = pmd_offset(pud, 0); |
| 453 | |
| 454 | if (PTRS_PER_PMD > 1) /* not folded */ |
Jeremy Fitzhardinge | 7426071 | 2007-10-16 11:51:30 -0700 | [diff] [blame] | 455 | flush |= (*func)(virt_to_page(pmd), PT_PMD); |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 456 | |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 457 | for (; addr != pmd_limit; pmd++) { |
| 458 | addr += (PAGE_SIZE * PTRS_PER_PTE); |
| 459 | if ((pmd_limit-1) < (addr-1)) { |
| 460 | addr = pmd_limit; |
| 461 | break; |
| 462 | } |
| 463 | |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 464 | if (pmd_none(*pmd)) |
| 465 | continue; |
| 466 | |
Jeremy Fitzhardinge | 7426071 | 2007-10-16 11:51:30 -0700 | [diff] [blame] | 467 | flush |= (*func)(pmd_page(*pmd), PT_PTE); |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 468 | } |
| 469 | } |
| 470 | } |
| 471 | |
Jeremy Fitzhardinge | 7426071 | 2007-10-16 11:51:30 -0700 | [diff] [blame] | 472 | flush |= (*func)(virt_to_page(pgd_base), PT_PGD); |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 473 | |
| 474 | return flush; |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 475 | } |
| 476 | |
Jeremy Fitzhardinge | 7426071 | 2007-10-16 11:51:30 -0700 | [diff] [blame] | 477 | static spinlock_t *lock_pte(struct page *page) |
| 478 | { |
| 479 | spinlock_t *ptl = NULL; |
| 480 | |
| 481 | #if NR_CPUS >= CONFIG_SPLIT_PTLOCK_CPUS |
| 482 | ptl = __pte_lockptr(page); |
| 483 | spin_lock(ptl); |
| 484 | #endif |
| 485 | |
| 486 | return ptl; |
| 487 | } |
| 488 | |
| 489 | static void do_unlock(void *v) |
| 490 | { |
| 491 | spinlock_t *ptl = v; |
| 492 | spin_unlock(ptl); |
| 493 | } |
| 494 | |
| 495 | static void xen_do_pin(unsigned level, unsigned long pfn) |
| 496 | { |
| 497 | struct mmuext_op *op; |
| 498 | struct multicall_space mcs; |
| 499 | |
| 500 | mcs = __xen_mc_entry(sizeof(*op)); |
| 501 | op = mcs.args; |
| 502 | op->cmd = level; |
| 503 | op->arg1.mfn = pfn_to_mfn(pfn); |
| 504 | MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF); |
| 505 | } |
| 506 | |
| 507 | static int pin_page(struct page *page, enum pt_level level) |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 508 | { |
Christoph Lameter | d60cd46 | 2008-04-28 02:12:51 -0700 | [diff] [blame] | 509 | unsigned pgfl = TestSetPagePinned(page); |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 510 | int flush; |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 511 | |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 512 | if (pgfl) |
| 513 | flush = 0; /* already pinned */ |
| 514 | else if (PageHighMem(page)) |
| 515 | /* kmaps need flushing if we found an unpinned |
| 516 | highpage */ |
| 517 | flush = 1; |
| 518 | else { |
| 519 | void *pt = lowmem_page_address(page); |
| 520 | unsigned long pfn = page_to_pfn(page); |
| 521 | struct multicall_space mcs = __xen_mc_entry(0); |
Jeremy Fitzhardinge | 7426071 | 2007-10-16 11:51:30 -0700 | [diff] [blame] | 522 | spinlock_t *ptl; |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 523 | |
| 524 | flush = 0; |
| 525 | |
Jeremy Fitzhardinge | 7426071 | 2007-10-16 11:51:30 -0700 | [diff] [blame] | 526 | ptl = NULL; |
| 527 | if (level == PT_PTE) |
| 528 | ptl = lock_pte(page); |
| 529 | |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 530 | MULTI_update_va_mapping(mcs.mc, (unsigned long)pt, |
| 531 | pfn_pte(pfn, PAGE_KERNEL_RO), |
Jeremy Fitzhardinge | 7426071 | 2007-10-16 11:51:30 -0700 | [diff] [blame] | 532 | level == PT_PGD ? UVMF_TLB_FLUSH : 0); |
| 533 | |
| 534 | if (level == PT_PTE) |
| 535 | xen_do_pin(MMUEXT_PIN_L1_TABLE, pfn); |
| 536 | |
| 537 | if (ptl) { |
| 538 | /* Queue a deferred unlock for when this batch |
| 539 | is completed. */ |
| 540 | xen_mc_callback(do_unlock, ptl); |
| 541 | } |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | return flush; |
| 545 | } |
| 546 | |
| 547 | /* This is called just after a mm has been created, but it has not |
| 548 | been used yet. We need to make sure that its pagetable is all |
| 549 | read-only, and can be pinned. */ |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 550 | void xen_pgd_pin(pgd_t *pgd) |
| 551 | { |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 552 | xen_mc_batch(); |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 553 | |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 554 | if (pgd_walk(pgd, pin_page, TASK_SIZE)) { |
| 555 | /* re-enable interrupts for kmap_flush_unused */ |
| 556 | xen_mc_issue(0); |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 557 | kmap_flush_unused(); |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 558 | xen_mc_batch(); |
| 559 | } |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 560 | |
Jeremy Fitzhardinge | 3843fc2 | 2008-05-09 12:05:57 +0100 | [diff] [blame] | 561 | xen_do_pin(MMUEXT_PIN_L3_TABLE, PFN_DOWN(__pa(pgd))); |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 562 | xen_mc_issue(0); |
| 563 | } |
| 564 | |
Jeremy Fitzhardinge | 0e91398 | 2008-05-26 23:31:27 +0100 | [diff] [blame] | 565 | /* |
| 566 | * On save, we need to pin all pagetables to make sure they get their |
| 567 | * mfns turned into pfns. Search the list for any unpinned pgds and pin |
| 568 | * them (unpinned pgds are not currently in use, probably because the |
| 569 | * process is under construction or destruction). |
| 570 | */ |
| 571 | void xen_mm_pin_all(void) |
| 572 | { |
| 573 | unsigned long flags; |
| 574 | struct page *page; |
| 575 | |
| 576 | spin_lock_irqsave(&pgd_lock, flags); |
| 577 | |
| 578 | list_for_each_entry(page, &pgd_list, lru) { |
| 579 | if (!PagePinned(page)) { |
| 580 | xen_pgd_pin((pgd_t *)page_address(page)); |
| 581 | SetPageSavePinned(page); |
| 582 | } |
| 583 | } |
| 584 | |
| 585 | spin_unlock_irqrestore(&pgd_lock, flags); |
| 586 | } |
| 587 | |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 588 | /* The init_mm pagetable is really pinned as soon as its created, but |
| 589 | that's before we have page structures to store the bits. So do all |
| 590 | the book-keeping now. */ |
Jeremy Fitzhardinge | 7426071 | 2007-10-16 11:51:30 -0700 | [diff] [blame] | 591 | static __init int mark_pinned(struct page *page, enum pt_level level) |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 592 | { |
| 593 | SetPagePinned(page); |
| 594 | return 0; |
| 595 | } |
| 596 | |
| 597 | void __init xen_mark_init_mm_pinned(void) |
| 598 | { |
| 599 | pgd_walk(init_mm.pgd, mark_pinned, FIXADDR_TOP); |
| 600 | } |
| 601 | |
Jeremy Fitzhardinge | 7426071 | 2007-10-16 11:51:30 -0700 | [diff] [blame] | 602 | static int unpin_page(struct page *page, enum pt_level level) |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 603 | { |
Christoph Lameter | d60cd46 | 2008-04-28 02:12:51 -0700 | [diff] [blame] | 604 | unsigned pgfl = TestClearPagePinned(page); |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 605 | |
| 606 | if (pgfl && !PageHighMem(page)) { |
| 607 | void *pt = lowmem_page_address(page); |
| 608 | unsigned long pfn = page_to_pfn(page); |
Jeremy Fitzhardinge | 7426071 | 2007-10-16 11:51:30 -0700 | [diff] [blame] | 609 | spinlock_t *ptl = NULL; |
| 610 | struct multicall_space mcs; |
| 611 | |
| 612 | if (level == PT_PTE) { |
| 613 | ptl = lock_pte(page); |
| 614 | |
| 615 | xen_do_pin(MMUEXT_UNPIN_TABLE, pfn); |
| 616 | } |
| 617 | |
| 618 | mcs = __xen_mc_entry(0); |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 619 | |
| 620 | MULTI_update_va_mapping(mcs.mc, (unsigned long)pt, |
| 621 | pfn_pte(pfn, PAGE_KERNEL), |
Jeremy Fitzhardinge | 7426071 | 2007-10-16 11:51:30 -0700 | [diff] [blame] | 622 | level == PT_PGD ? UVMF_TLB_FLUSH : 0); |
| 623 | |
| 624 | if (ptl) { |
| 625 | /* unlock when batch completed */ |
| 626 | xen_mc_callback(do_unlock, ptl); |
| 627 | } |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | return 0; /* never need to flush on unpin */ |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | /* Release a pagetables pages back as normal RW */ |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 634 | static void xen_pgd_unpin(pgd_t *pgd) |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 635 | { |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 636 | xen_mc_batch(); |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 637 | |
Jeremy Fitzhardinge | 7426071 | 2007-10-16 11:51:30 -0700 | [diff] [blame] | 638 | xen_do_pin(MMUEXT_UNPIN_TABLE, PFN_DOWN(__pa(pgd))); |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 639 | |
| 640 | pgd_walk(pgd, unpin_page, TASK_SIZE); |
| 641 | |
| 642 | xen_mc_issue(0); |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 643 | } |
| 644 | |
Jeremy Fitzhardinge | 0e91398 | 2008-05-26 23:31:27 +0100 | [diff] [blame] | 645 | /* |
| 646 | * On resume, undo any pinning done at save, so that the rest of the |
| 647 | * kernel doesn't see any unexpected pinned pagetables. |
| 648 | */ |
| 649 | void xen_mm_unpin_all(void) |
| 650 | { |
| 651 | unsigned long flags; |
| 652 | struct page *page; |
| 653 | |
| 654 | spin_lock_irqsave(&pgd_lock, flags); |
| 655 | |
| 656 | list_for_each_entry(page, &pgd_list, lru) { |
| 657 | if (PageSavePinned(page)) { |
| 658 | BUG_ON(!PagePinned(page)); |
| 659 | printk("unpinning pinned %p\n", page_address(page)); |
| 660 | xen_pgd_unpin((pgd_t *)page_address(page)); |
| 661 | ClearPageSavePinned(page); |
| 662 | } |
| 663 | } |
| 664 | |
| 665 | spin_unlock_irqrestore(&pgd_lock, flags); |
| 666 | } |
| 667 | |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 668 | void xen_activate_mm(struct mm_struct *prev, struct mm_struct *next) |
| 669 | { |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 670 | spin_lock(&next->page_table_lock); |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 671 | xen_pgd_pin(next->pgd); |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 672 | spin_unlock(&next->page_table_lock); |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | void xen_dup_mmap(struct mm_struct *oldmm, struct mm_struct *mm) |
| 676 | { |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 677 | spin_lock(&mm->page_table_lock); |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 678 | xen_pgd_pin(mm->pgd); |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 679 | spin_unlock(&mm->page_table_lock); |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 680 | } |
| 681 | |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 682 | |
| 683 | #ifdef CONFIG_SMP |
| 684 | /* Another cpu may still have their %cr3 pointing at the pagetable, so |
| 685 | we need to repoint it somewhere else before we can unpin it. */ |
| 686 | static void drop_other_mm_ref(void *info) |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 687 | { |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 688 | struct mm_struct *mm = info; |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 689 | |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 690 | if (__get_cpu_var(cpu_tlbstate).active_mm == mm) |
| 691 | leave_mm(smp_processor_id()); |
Jeremy Fitzhardinge | 9f79991 | 2007-10-16 11:51:30 -0700 | [diff] [blame] | 692 | |
| 693 | /* If this cpu still has a stale cr3 reference, then make sure |
| 694 | it has been flushed. */ |
| 695 | if (x86_read_percpu(xen_current_cr3) == __pa(mm->pgd)) { |
| 696 | load_cr3(swapper_pg_dir); |
| 697 | arch_flush_lazy_cpu_mode(); |
| 698 | } |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 699 | } |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 700 | |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 701 | static void drop_mm_ref(struct mm_struct *mm) |
| 702 | { |
Jeremy Fitzhardinge | 9f79991 | 2007-10-16 11:51:30 -0700 | [diff] [blame] | 703 | cpumask_t mask; |
| 704 | unsigned cpu; |
| 705 | |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 706 | if (current->active_mm == mm) { |
| 707 | if (current->mm == mm) |
| 708 | load_cr3(swapper_pg_dir); |
| 709 | else |
| 710 | leave_mm(smp_processor_id()); |
Jeremy Fitzhardinge | 9f79991 | 2007-10-16 11:51:30 -0700 | [diff] [blame] | 711 | arch_flush_lazy_cpu_mode(); |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 712 | } |
| 713 | |
Jeremy Fitzhardinge | 9f79991 | 2007-10-16 11:51:30 -0700 | [diff] [blame] | 714 | /* Get the "official" set of cpus referring to our pagetable. */ |
| 715 | mask = mm->cpu_vm_mask; |
| 716 | |
| 717 | /* It's possible that a vcpu may have a stale reference to our |
| 718 | cr3, because its in lazy mode, and it hasn't yet flushed |
| 719 | its set of pending hypercalls yet. In this case, we can |
| 720 | look at its actual current cr3 value, and force it to flush |
| 721 | if needed. */ |
| 722 | for_each_online_cpu(cpu) { |
| 723 | if (per_cpu(xen_current_cr3, cpu) == __pa(mm->pgd)) |
| 724 | cpu_set(cpu, mask); |
| 725 | } |
| 726 | |
| 727 | if (!cpus_empty(mask)) |
| 728 | xen_smp_call_function_mask(mask, drop_other_mm_ref, mm, 1); |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 729 | } |
| 730 | #else |
| 731 | static void drop_mm_ref(struct mm_struct *mm) |
| 732 | { |
| 733 | if (current->active_mm == mm) |
| 734 | load_cr3(swapper_pg_dir); |
| 735 | } |
| 736 | #endif |
| 737 | |
| 738 | /* |
| 739 | * While a process runs, Xen pins its pagetables, which means that the |
| 740 | * hypervisor forces it to be read-only, and it controls all updates |
| 741 | * to it. This means that all pagetable updates have to go via the |
| 742 | * hypervisor, which is moderately expensive. |
| 743 | * |
| 744 | * Since we're pulling the pagetable down, we switch to use init_mm, |
| 745 | * unpin old process pagetable and mark it all read-write, which |
| 746 | * allows further operations on it to be simple memory accesses. |
| 747 | * |
| 748 | * The only subtle point is that another CPU may be still using the |
| 749 | * pagetable because of lazy tlb flushing. This means we need need to |
| 750 | * switch all CPUs off this pagetable before we can unpin it. |
| 751 | */ |
| 752 | void xen_exit_mmap(struct mm_struct *mm) |
| 753 | { |
| 754 | get_cpu(); /* make sure we don't move around */ |
| 755 | drop_mm_ref(mm); |
| 756 | put_cpu(); |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 757 | |
Jeremy Fitzhardinge | f120f13 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 758 | spin_lock(&mm->page_table_lock); |
Jeremy Fitzhardinge | df912ea | 2007-09-25 11:50:00 -0700 | [diff] [blame] | 759 | |
| 760 | /* pgd may not be pinned in the error exit path of execve */ |
| 761 | if (PagePinned(virt_to_page(mm->pgd))) |
| 762 | xen_pgd_unpin(mm->pgd); |
Jeremy Fitzhardinge | 7426071 | 2007-10-16 11:51:30 -0700 | [diff] [blame] | 763 | |
Jeremy Fitzhardinge | f120f13 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 764 | spin_unlock(&mm->page_table_lock); |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 765 | } |