1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37#include <linux/sched.h>
38#include <linux/mm.h>
39#include <linux/vmalloc.h>
40
41#include <xen/interface/xen.h>
42#include <xen/page.h>
43#include <xen/grant_table.h>
44
45#include <asm/pgtable.h>
46
47static int map_pte_fn(pte_t *pte, struct page *pmd_page,
48 unsigned long addr, void *data)
49{
50 unsigned long **frames = (unsigned long **)data;
51
52 set_pte_at(&init_mm, addr, pte, mfn_pte((*frames)[0], PAGE_KERNEL));
53 (*frames)++;
54 return 0;
55}
56
57
58
59
60
61static int map_pte_fn_status(pte_t *pte, struct page *pmd_page,
62 unsigned long addr, void *data)
63{
64 uint64_t **frames = (uint64_t **)data;
65
66 set_pte_at(&init_mm, addr, pte, mfn_pte((*frames)[0], PAGE_KERNEL));
67 (*frames)++;
68 return 0;
69}
70
71static int unmap_pte_fn(pte_t *pte, struct page *pmd_page,
72 unsigned long addr, void *data)
73{
74
75 set_pte_at(&init_mm, addr, pte, __pte(0));
76 return 0;
77}
78
79int arch_gnttab_map_shared(unsigned long *frames, unsigned long nr_gframes,
80 unsigned long max_nr_gframes,
81 void **__shared)
82{
83 int rc;
84 void *shared = *__shared;
85
86 if (shared == NULL) {
87 struct vm_struct *area =
88 alloc_vm_area(PAGE_SIZE * max_nr_gframes, NULL);
89 BUG_ON(area == NULL);
90 shared = area->addr;
91 *__shared = shared;
92 }
93
94 rc = apply_to_page_range(&init_mm, (unsigned long)shared,
95 PAGE_SIZE * nr_gframes,
96 map_pte_fn, &frames);
97 return rc;
98}
99
100int arch_gnttab_map_status(uint64_t *frames, unsigned long nr_gframes,
101 unsigned long max_nr_gframes,
102 grant_status_t **__shared)
103{
104 int rc;
105 grant_status_t *shared = *__shared;
106
107 if (shared == NULL) {
108
109
110 struct vm_struct *area =
111 alloc_vm_area(PAGE_SIZE * max_nr_gframes, NULL);
112 BUG_ON(area == NULL);
113 shared = area->addr;
114 *__shared = shared;
115 }
116
117 rc = apply_to_page_range(&init_mm, (unsigned long)shared,
118 PAGE_SIZE * nr_gframes,
119 map_pte_fn_status, &frames);
120 return rc;
121}
122
123void arch_gnttab_unmap(void *shared, unsigned long nr_gframes)
124{
125 apply_to_page_range(&init_mm, (unsigned long)shared,
126 PAGE_SIZE * nr_gframes, unmap_pte_fn, NULL);
127}
128