forked from xen-project/mini-os
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgntmap.c
288 lines (244 loc) · 7.51 KB
/
gntmap.c
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
* Manages grant mappings from other domains.
*
* Diego Ongaro <[email protected]>, July 2008
*
* Files of libxengnttab contain a gntmap, which is an array of
* (host address, grant handle) pairs. Grant handles come from a hypervisor map
* operation and are needed for the corresponding unmap.
*
* This is a rather naive implementation in terms of performance. If we start
* using it frequently, there's definitely some low-hanging fruit here.
*
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include <mini-os/os.h>
#include <mini-os/lib.h>
#include <mini-os/e820.h>
#include <mini-os/xmalloc.h>
#include <errno.h>
#include <xen/grant_table.h>
#include <inttypes.h>
#include <mini-os/gntmap.h>
//#define GNTMAP_DEBUG
#ifdef GNTMAP_DEBUG
#define DEBUG(_f, _a...) \
printk("MINI_OS(gntmap.c:%d): %s" _f "\n", __LINE__, __func__, ## _a)
#else
#define DEBUG(_f, _a...) ((void)0)
#endif
#define DEFAULT_MAX_GRANTS 128
struct gntmap_entry {
unsigned long host_addr;
grant_handle_t handle;
};
static inline int
gntmap_entry_used(struct gntmap *map, int idx)
{
return map->entries[idx].host_addr != 0;
}
static int gntmap_find_free_entry(struct gntmap *map)
{
int i;
for (i = 0; i < map->nentries; i++) {
if (!gntmap_entry_used(map, i))
return i;
}
DEBUG("(map=%p): all %d entries full",
map, map->nentries);
return -1;
}
static int gntmap_find_entry(struct gntmap *map, unsigned long addr)
{
int i;
for (i = 0; i < map->nentries; i++) {
if (map->entries[i].host_addr == addr)
return i;
}
return -1;
}
int
gntmap_set_max_grants(struct gntmap *map, int count)
{
DEBUG("(map=%p, count=%d)", map, count);
if (map->nentries != 0)
return -EBUSY;
map->entries = xmalloc_array(struct gntmap_entry, count);
if (map->entries == NULL)
return -ENOMEM;
#ifndef CONFIG_PARAVIRT
map->start_pfn = e820_get_reserved_pfns(count);
#endif
memset(map->entries, 0, sizeof(struct gntmap_entry) * count);
map->nentries = count;
return 0;
}
EXPORT_SYMBOL(gntmap_set_max_grants);
static int
_gntmap_unmap_grant_ref(struct gntmap *map, int idx)
{
struct gntmap_entry *entry = map->entries + idx;
struct gnttab_unmap_grant_ref op;
int rc;
#ifdef CONFIG_PARAVIRT
op.host_addr = (uint64_t) entry->host_addr;
#else
op.host_addr = (uint64_t)(map->start_pfn + idx) << PAGE_SHIFT;
#endif
op.dev_bus_addr = 0;
op.handle = entry->handle;
rc = HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, &op, 1);
if (rc != 0 || op.status != GNTST_okay) {
printk("GNTTABOP_unmap_grant_ref failed: "
"returned %d, status %" PRId16 "\n",
rc, op.status);
return rc != 0 ? rc : op.status;
}
entry->host_addr = 0;
return 0;
}
static int
_gntmap_map_grant_ref(struct gntmap *map, int idx,
unsigned long host_addr,
uint32_t domid,
uint32_t ref,
int writable)
{
struct gntmap_entry *entry = map->entries + idx;
struct gnttab_map_grant_ref op;
int rc;
#ifndef CONFIG_PARAVIRT
unsigned long pfn = map->start_pfn + idx;
#endif
op.ref = (grant_ref_t) ref;
op.dom = (domid_t) domid;
#ifdef CONFIG_PARAVIRT
op.host_addr = (uint64_t) host_addr;
#else
op.host_addr = (uint64_t)pfn << PAGE_SHIFT;
#endif
op.flags = GNTMAP_host_map;
if (!writable)
op.flags |= GNTMAP_readonly;
rc = HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, &op, 1);
if (rc != 0 || op.status != GNTST_okay) {
printk("GNTTABOP_map_grant_ref failed: "
"returned %d, status %" PRId16 "\n",
rc, op.status);
return rc != 0 ? rc : op.status;
}
#ifndef CONFIG_PARAVIRT
rc = do_map_frames(host_addr, &pfn, 1, 0, 0, DOMID_SELF, NULL,
writable ? L1_PROT : L1_PROT_RO);
if ( rc )
{
_gntmap_unmap_grant_ref(map, idx);
return rc;
}
#endif
entry->host_addr = host_addr;
entry->handle = op.handle;
return 0;
}
int
gntmap_munmap(struct gntmap *map, unsigned long start_address, int count)
{
int i, rc;
int idx;
DEBUG("(map=%p, start_address=%lx, count=%d)",
map, start_address, count);
#ifndef CONFIG_PARAVIRT
unmap_frames(start_address, count);
#endif
for (i = 0; i < count; i++) {
idx = gntmap_find_entry(map, start_address + PAGE_SIZE * i);
if (idx < 0) {
printk("gntmap: tried to munmap unknown page\n");
return -EINVAL;
}
rc = _gntmap_unmap_grant_ref(map, idx);
if (rc != 0)
return rc;
}
return 0;
}
EXPORT_SYMBOL(gntmap_munmap);
void*
gntmap_map_grant_refs(struct gntmap *map,
uint32_t count,
uint32_t *domids,
int domids_stride,
uint32_t *refs,
int writable)
{
unsigned long addr;
int idx;
int i;
DEBUG("(map=%p, count=%" PRIu32 ", "
"domids=%p [%" PRIu32 "...], domids_stride=%d, "
"refs=%p [%" PRIu32 "...], writable=%d)",
map, count,
domids, domids == NULL ? 0 : domids[0], domids_stride,
refs, refs == NULL ? 0 : refs[0], writable);
(void) gntmap_set_max_grants(map, DEFAULT_MAX_GRANTS);
addr = allocate_ondemand((unsigned long) count, 1);
if (addr == 0)
return NULL;
for (i = 0; i < count; i++) {
idx = gntmap_find_free_entry(map);
if (idx < 0 ||
_gntmap_map_grant_ref(map, idx,
addr + PAGE_SIZE * i,
domids[i * domids_stride],
refs[i],
writable) != 0) {
(void) gntmap_munmap(map, addr, i);
return NULL;
}
}
return (void*) addr;
}
EXPORT_SYMBOL(gntmap_map_grant_refs);
void
gntmap_init(struct gntmap *map)
{
DEBUG("(map=%p)", map);
map->nentries = 0;
map->entries = NULL;
}
EXPORT_SYMBOL(gntmap_init);
void
gntmap_fini(struct gntmap *map)
{
int i;
DEBUG("(map=%p)", map);
for (i = 0; i < map->nentries; i++) {
if (gntmap_entry_used(map, i))
(void) _gntmap_unmap_grant_ref(map, i);
}
#ifndef CONFIG_PARAVIRT
e820_put_reserved_pfns(map->start_pfn, map->nentries);
map->start_pfn = 0;
#endif
xfree(map->entries);
map->entries = NULL;
map->nentries = 0;
}
EXPORT_SYMBOL(gntmap_fini);