This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / drivers / xen / util.c
1 #include <linux/config.h>
2 #include <linux/mm.h>
3 #include <linux/module.h>
4 #include <linux/slab.h>
5 #include <linux/vmalloc.h>
6 #include <asm/uaccess.h>
7 #include <xen/driver_util.h>
8
9 static int f(pte_t *pte, struct page *pmd_page, unsigned long addr, void *data)
10 {
11         /* apply_to_page_range() does all the hard work. */
12         return 0;
13 }
14
15 struct vm_struct *alloc_vm_area(unsigned long size)
16 {
17         struct vm_struct *area;
18
19         area = get_vm_area(size, VM_IOREMAP);
20         if (area == NULL)
21                 return NULL;
22
23         /*
24          * This ensures that page tables are constructed for this region
25          * of kernel virtual address space and mapped into init_mm.
26          */
27         if (apply_to_page_range(&init_mm, (unsigned long)area->addr,
28                                 area->size, f, NULL)) {
29                 free_vm_area(area);
30                 return NULL;
31         }
32
33         return area;
34 }
35 EXPORT_SYMBOL_GPL(alloc_vm_area);
36
37 void free_vm_area(struct vm_struct *area)
38 {
39         struct vm_struct *ret;
40         ret = remove_vm_area(area->addr);
41         BUG_ON(ret != area);
42         kfree(area);
43 }
44 EXPORT_SYMBOL_GPL(free_vm_area);
45
46 void lock_vm_area(struct vm_struct *area)
47 {
48         unsigned long i;
49         char c;
50
51         /*
52          * Prevent context switch to a lazy mm that doesn't have this area
53          * mapped into its page tables.
54          */
55         preempt_disable();
56
57         /*
58          * Ensure that the page tables are mapped into the current mm. The
59          * page-fault path will copy the page directory pointers from init_mm.
60          */
61         for (i = 0; i < area->size; i += PAGE_SIZE)
62                 (void)__get_user(c, (char __user *)area->addr + i);
63 }
64 EXPORT_SYMBOL_GPL(lock_vm_area);
65
66 void unlock_vm_area(struct vm_struct *area)
67 {
68         preempt_enable();
69 }
70 EXPORT_SYMBOL_GPL(unlock_vm_area);