2 #include <asm/xen/page.h>
3 #include <asm/xen/hypercall.h>
4 #include <xen/interface/memory.h>
6 #include "multicalls.h"
10 * Protects atomic reservation decrease/increase against concurrent increases.
11 * Also protects non-atomic updates of current_pages and balloon lists.
13 DEFINE_SPINLOCK(xen_reservation_lock);
15 unsigned long arbitrary_virt_to_mfn(void *vaddr)
17 xmaddr_t maddr = arbitrary_virt_to_machine(vaddr);
19 return PFN_DOWN(maddr.maddr);
22 xmaddr_t arbitrary_virt_to_machine(void *vaddr)
24 unsigned long address = (unsigned long)vaddr;
30 * if the PFN is in the linear mapped vaddr range, we can just use
31 * the (quick) virt_to_machine() p2m lookup
33 if (virt_addr_valid(vaddr))
34 return virt_to_machine(vaddr);
36 /* otherwise we have to do a (slower) full page-table walk */
38 pte = lookup_address(address, &level);
40 offset = address & ~PAGE_MASK;
41 return XMADDR(((phys_addr_t)pte_mfn(*pte) << PAGE_SHIFT) + offset);
43 EXPORT_SYMBOL_GPL(arbitrary_virt_to_machine);
45 static noinline void xen_flush_tlb_all(void)
48 struct multicall_space mcs;
52 mcs = xen_mc_entry(sizeof(*op));
55 op->cmd = MMUEXT_TLB_FLUSH_ALL;
56 MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
58 xen_mc_issue(PARAVIRT_LAZY_MMU);
63 #define REMAP_BATCH_SIZE 16
69 struct mmu_update *mmu_update;
72 static int remap_area_mfn_pte_fn(pte_t *ptep, pgtable_t token,
73 unsigned long addr, void *data)
75 struct remap_data *rmd = data;
76 pte_t pte = pte_mkspecial(mfn_pte(*rmd->mfn, rmd->prot));
78 /* If we have a contiguous range, just update the mfn itself,
79 else update pointer to be "next mfn". */
85 rmd->mmu_update->ptr = virt_to_machine(ptep).maddr | MMU_NORMAL_PT_UPDATE;
86 rmd->mmu_update->val = pte_val_ma(pte);
92 static int do_remap_gfn(struct vm_area_struct *vma,
94 xen_pfn_t *gfn, int nr,
95 int *err_ptr, pgprot_t prot,
100 struct remap_data rmd;
101 struct mmu_update mmu_update[REMAP_BATCH_SIZE];
105 BUG_ON(!((vma->vm_flags & (VM_PFNMAP | VM_IO)) == (VM_PFNMAP | VM_IO)));
109 /* We use the err_ptr to indicate if there we are doing a contiguous
110 * mapping or a discontigious mapping. */
111 rmd.contiguous = !err_ptr;
116 int batch = min(REMAP_BATCH_SIZE, nr);
117 int batch_left = batch;
118 range = (unsigned long)batch << PAGE_SHIFT;
120 rmd.mmu_update = mmu_update;
121 err = apply_to_page_range(vma->vm_mm, addr, range,
122 remap_area_mfn_pte_fn, &rmd);
126 /* We record the error for each page that gives an error, but
127 * continue mapping until the whole set is done */
131 err = HYPERVISOR_mmu_update(&mmu_update[index],
132 batch_left, &done, domid);
135 * @err_ptr may be the same buffer as @gfn, so
136 * only clear it after each chunk of @gfn is
140 for (i = index; i < index + done; i++)
147 done++; /* Skip failed frame. */
152 } while (batch_left);
164 return err < 0 ? err : mapped;
167 int xen_remap_domain_gfn_range(struct vm_area_struct *vma,
169 xen_pfn_t gfn, int nr,
170 pgprot_t prot, unsigned domid,
173 return do_remap_gfn(vma, addr, &gfn, nr, NULL, prot, domid, pages);
175 EXPORT_SYMBOL_GPL(xen_remap_domain_gfn_range);
177 int xen_remap_domain_gfn_array(struct vm_area_struct *vma,
179 xen_pfn_t *gfn, int nr,
180 int *err_ptr, pgprot_t prot,
181 unsigned domid, struct page **pages)
183 /* We BUG_ON because it's a programmer error to pass a NULL err_ptr,
184 * and the consequences later is quite hard to detect what the actual
185 * cause of "wrong memory was mapped in".
187 BUG_ON(err_ptr == NULL);
188 return do_remap_gfn(vma, addr, gfn, nr, err_ptr, prot, domid, pages);
190 EXPORT_SYMBOL_GPL(xen_remap_domain_gfn_array);
192 /* Returns: 0 success */
193 int xen_unmap_domain_gfn_range(struct vm_area_struct *vma,
194 int numpgs, struct page **pages)
196 if (!pages || !xen_feature(XENFEAT_auto_translated_physmap))
201 EXPORT_SYMBOL_GPL(xen_unmap_domain_gfn_range);