1
2
3
4
5
6
7
8
9
10
11
12#include <linux/module.h>
13#include <linux/errno.h>
14#include <linux/mm.h>
15
16#include <asm/xen/hypervisor.h>
17#include <xen/balloon.h>
18#include <xen/xen.h>
19#include <xen/xenbus.h>
20#include <xen/interface/io/ring.h>
21
22#include <xen/xen-front-pgdir-shbuf.h>
23
24#ifndef GRANT_INVALID_REF
25
26
27
28
29
30#define GRANT_INVALID_REF 0
31#endif
32
33
34
35
36
37
38
39struct xen_page_directory {
40 grant_ref_t gref_dir_next_page;
41 grant_ref_t gref[1];
42};
43
44
45
46
47
48
49
50struct xen_front_pgdir_shbuf_ops {
51
52
53
54
55
56 void (*calc_num_grefs)(struct xen_front_pgdir_shbuf *buf);
57
58
59 void (*fill_page_dir)(struct xen_front_pgdir_shbuf *buf);
60
61
62 int (*grant_refs_for_buffer)(struct xen_front_pgdir_shbuf *buf,
63 grant_ref_t *priv_gref_head, int gref_idx);
64
65
66 int (*map)(struct xen_front_pgdir_shbuf *buf);
67
68
69 int (*unmap)(struct xen_front_pgdir_shbuf *buf);
70};
71
72
73
74
75
76
77
78
79
80
81
82grant_ref_t
83xen_front_pgdir_shbuf_get_dir_start(struct xen_front_pgdir_shbuf *buf)
84{
85 if (!buf->grefs)
86 return GRANT_INVALID_REF;
87
88 return buf->grefs[0];
89}
90EXPORT_SYMBOL_GPL(xen_front_pgdir_shbuf_get_dir_start);
91
92
93
94
95
96
97
98
99
100
101
102
103int xen_front_pgdir_shbuf_map(struct xen_front_pgdir_shbuf *buf)
104{
105 if (buf->ops && buf->ops->map)
106 return buf->ops->map(buf);
107
108
109 return 0;
110}
111EXPORT_SYMBOL_GPL(xen_front_pgdir_shbuf_map);
112
113
114
115
116
117
118
119
120
121
122
123
124int xen_front_pgdir_shbuf_unmap(struct xen_front_pgdir_shbuf *buf)
125{
126 if (buf->ops && buf->ops->unmap)
127 return buf->ops->unmap(buf);
128
129
130 return 0;
131}
132EXPORT_SYMBOL_GPL(xen_front_pgdir_shbuf_unmap);
133
134
135
136
137
138
139void xen_front_pgdir_shbuf_free(struct xen_front_pgdir_shbuf *buf)
140{
141 if (buf->grefs) {
142 int i;
143
144 for (i = 0; i < buf->num_grefs; i++)
145 if (buf->grefs[i] != GRANT_INVALID_REF)
146 gnttab_end_foreign_access(buf->grefs[i],
147 0, 0UL);
148 }
149 kfree(buf->grefs);
150 kfree(buf->directory);
151}
152EXPORT_SYMBOL_GPL(xen_front_pgdir_shbuf_free);
153
154
155
156
157
158#define XEN_NUM_GREFS_PER_PAGE ((PAGE_SIZE - \
159 offsetof(struct xen_page_directory, \
160 gref)) / sizeof(grant_ref_t))
161
162
163
164
165
166
167static int get_num_pages_dir(struct xen_front_pgdir_shbuf *buf)
168{
169 return DIV_ROUND_UP(buf->num_pages, XEN_NUM_GREFS_PER_PAGE);
170}
171
172
173
174
175
176
177
178static void backend_calc_num_grefs(struct xen_front_pgdir_shbuf *buf)
179{
180
181 buf->num_grefs = get_num_pages_dir(buf);
182}
183
184
185
186
187
188
189
190static void guest_calc_num_grefs(struct xen_front_pgdir_shbuf *buf)
191{
192
193
194
195
196 buf->num_grefs = get_num_pages_dir(buf) + buf->num_pages;
197}
198
199#define xen_page_to_vaddr(page) \
200 ((uintptr_t)pfn_to_kaddr(page_to_xen_pfn(page)))
201
202
203
204
205
206
207
208
209static int backend_unmap(struct xen_front_pgdir_shbuf *buf)
210{
211 struct gnttab_unmap_grant_ref *unmap_ops;
212 int i, ret;
213
214 if (!buf->pages || !buf->backend_map_handles || !buf->grefs)
215 return 0;
216
217 unmap_ops = kcalloc(buf->num_pages, sizeof(*unmap_ops),
218 GFP_KERNEL);
219 if (!unmap_ops)
220 return -ENOMEM;
221
222 for (i = 0; i < buf->num_pages; i++) {
223 phys_addr_t addr;
224
225 addr = xen_page_to_vaddr(buf->pages[i]);
226 gnttab_set_unmap_op(&unmap_ops[i], addr, GNTMAP_host_map,
227 buf->backend_map_handles[i]);
228 }
229
230 ret = gnttab_unmap_refs(unmap_ops, NULL, buf->pages,
231 buf->num_pages);
232
233 for (i = 0; i < buf->num_pages; i++) {
234 if (unlikely(unmap_ops[i].status != GNTST_okay))
235 dev_err(&buf->xb_dev->dev,
236 "Failed to unmap page %d: %d\n",
237 i, unmap_ops[i].status);
238 }
239
240 if (ret)
241 dev_err(&buf->xb_dev->dev,
242 "Failed to unmap grant references, ret %d", ret);
243
244 kfree(unmap_ops);
245 kfree(buf->backend_map_handles);
246 buf->backend_map_handles = NULL;
247 return ret;
248}
249
250
251
252
253
254
255
256static int backend_map(struct xen_front_pgdir_shbuf *buf)
257{
258 struct gnttab_map_grant_ref *map_ops = NULL;
259 unsigned char *ptr;
260 int ret, cur_gref, cur_dir_page, cur_page, grefs_left;
261
262 map_ops = kcalloc(buf->num_pages, sizeof(*map_ops), GFP_KERNEL);
263 if (!map_ops)
264 return -ENOMEM;
265
266 buf->backend_map_handles = kcalloc(buf->num_pages,
267 sizeof(*buf->backend_map_handles),
268 GFP_KERNEL);
269 if (!buf->backend_map_handles) {
270 kfree(map_ops);
271 return -ENOMEM;
272 }
273
274
275
276
277
278
279 ptr = buf->directory;
280 grefs_left = buf->num_pages;
281 cur_page = 0;
282 for (cur_dir_page = 0; cur_dir_page < buf->num_grefs; cur_dir_page++) {
283 struct xen_page_directory *page_dir =
284 (struct xen_page_directory *)ptr;
285 int to_copy = XEN_NUM_GREFS_PER_PAGE;
286
287 if (to_copy > grefs_left)
288 to_copy = grefs_left;
289
290 for (cur_gref = 0; cur_gref < to_copy; cur_gref++) {
291 phys_addr_t addr;
292
293 addr = xen_page_to_vaddr(buf->pages[cur_page]);
294 gnttab_set_map_op(&map_ops[cur_page], addr,
295 GNTMAP_host_map,
296 page_dir->gref[cur_gref],
297 buf->xb_dev->otherend_id);
298 cur_page++;
299 }
300
301 grefs_left -= to_copy;
302 ptr += PAGE_SIZE;
303 }
304 ret = gnttab_map_refs(map_ops, NULL, buf->pages, buf->num_pages);
305
306
307 for (cur_page = 0; cur_page < buf->num_pages; cur_page++) {
308 if (likely(map_ops[cur_page].status == GNTST_okay)) {
309 buf->backend_map_handles[cur_page] =
310 map_ops[cur_page].handle;
311 } else {
312 buf->backend_map_handles[cur_page] =
313 INVALID_GRANT_HANDLE;
314 if (!ret)
315 ret = -ENXIO;
316 dev_err(&buf->xb_dev->dev,
317 "Failed to map page %d: %d\n",
318 cur_page, map_ops[cur_page].status);
319 }
320 }
321
322 if (ret) {
323 dev_err(&buf->xb_dev->dev,
324 "Failed to map grant references, ret %d", ret);
325 backend_unmap(buf);
326 }
327
328 kfree(map_ops);
329 return ret;
330}
331
332
333
334
335
336
337
338
339
340
341static void backend_fill_page_dir(struct xen_front_pgdir_shbuf *buf)
342{
343 struct xen_page_directory *page_dir;
344 unsigned char *ptr;
345 int i, num_pages_dir;
346
347 ptr = buf->directory;
348 num_pages_dir = get_num_pages_dir(buf);
349
350
351 for (i = 0; i < num_pages_dir - 1; i++) {
352 page_dir = (struct xen_page_directory *)ptr;
353
354 page_dir->gref_dir_next_page = buf->grefs[i + 1];
355 ptr += PAGE_SIZE;
356 }
357
358 page_dir = (struct xen_page_directory *)ptr;
359 page_dir->gref_dir_next_page = GRANT_INVALID_REF;
360}
361
362
363
364
365
366
367
368static void guest_fill_page_dir(struct xen_front_pgdir_shbuf *buf)
369{
370 unsigned char *ptr;
371 int cur_gref, grefs_left, to_copy, i, num_pages_dir;
372
373 ptr = buf->directory;
374 num_pages_dir = get_num_pages_dir(buf);
375
376
377
378
379
380 cur_gref = num_pages_dir;
381 grefs_left = buf->num_pages;
382 for (i = 0; i < num_pages_dir; i++) {
383 struct xen_page_directory *page_dir =
384 (struct xen_page_directory *)ptr;
385
386 if (grefs_left <= XEN_NUM_GREFS_PER_PAGE) {
387 to_copy = grefs_left;
388 page_dir->gref_dir_next_page = GRANT_INVALID_REF;
389 } else {
390 to_copy = XEN_NUM_GREFS_PER_PAGE;
391 page_dir->gref_dir_next_page = buf->grefs[i + 1];
392 }
393 memcpy(&page_dir->gref, &buf->grefs[cur_gref],
394 to_copy * sizeof(grant_ref_t));
395 ptr += PAGE_SIZE;
396 grefs_left -= to_copy;
397 cur_gref += to_copy;
398 }
399}
400
401
402
403
404
405
406
407
408
409
410static int guest_grant_refs_for_buffer(struct xen_front_pgdir_shbuf *buf,
411 grant_ref_t *priv_gref_head,
412 int gref_idx)
413{
414 int i, cur_ref, otherend_id;
415
416 otherend_id = buf->xb_dev->otherend_id;
417 for (i = 0; i < buf->num_pages; i++) {
418 cur_ref = gnttab_claim_grant_reference(priv_gref_head);
419 if (cur_ref < 0)
420 return cur_ref;
421
422 gnttab_grant_foreign_access_ref(cur_ref, otherend_id,
423 xen_page_to_gfn(buf->pages[i]),
424 0);
425 buf->grefs[gref_idx++] = cur_ref;
426 }
427 return 0;
428}
429
430
431
432
433
434
435
436
437
438
439static int grant_references(struct xen_front_pgdir_shbuf *buf)
440{
441 grant_ref_t priv_gref_head;
442 int ret, i, j, cur_ref;
443 int otherend_id, num_pages_dir;
444
445 ret = gnttab_alloc_grant_references(buf->num_grefs, &priv_gref_head);
446 if (ret < 0) {
447 dev_err(&buf->xb_dev->dev,
448 "Cannot allocate grant references\n");
449 return ret;
450 }
451
452 otherend_id = buf->xb_dev->otherend_id;
453 j = 0;
454 num_pages_dir = get_num_pages_dir(buf);
455 for (i = 0; i < num_pages_dir; i++) {
456 unsigned long frame;
457
458 cur_ref = gnttab_claim_grant_reference(&priv_gref_head);
459 if (cur_ref < 0)
460 return cur_ref;
461
462 frame = xen_page_to_gfn(virt_to_page(buf->directory +
463 PAGE_SIZE * i));
464 gnttab_grant_foreign_access_ref(cur_ref, otherend_id, frame, 0);
465 buf->grefs[j++] = cur_ref;
466 }
467
468 if (buf->ops->grant_refs_for_buffer) {
469 ret = buf->ops->grant_refs_for_buffer(buf, &priv_gref_head, j);
470 if (ret)
471 return ret;
472 }
473
474 gnttab_free_grant_references(priv_gref_head);
475 return 0;
476}
477
478
479
480
481
482
483
484static int alloc_storage(struct xen_front_pgdir_shbuf *buf)
485{
486 buf->grefs = kcalloc(buf->num_grefs, sizeof(*buf->grefs), GFP_KERNEL);
487 if (!buf->grefs)
488 return -ENOMEM;
489
490 buf->directory = kcalloc(get_num_pages_dir(buf), PAGE_SIZE, GFP_KERNEL);
491 if (!buf->directory)
492 return -ENOMEM;
493
494 return 0;
495}
496
497
498
499
500
501static const struct xen_front_pgdir_shbuf_ops backend_ops = {
502 .calc_num_grefs = backend_calc_num_grefs,
503 .fill_page_dir = backend_fill_page_dir,
504 .map = backend_map,
505 .unmap = backend_unmap
506};
507
508
509
510
511
512static const struct xen_front_pgdir_shbuf_ops local_ops = {
513 .calc_num_grefs = guest_calc_num_grefs,
514 .fill_page_dir = guest_fill_page_dir,
515 .grant_refs_for_buffer = guest_grant_refs_for_buffer,
516};
517
518
519
520
521
522
523
524int xen_front_pgdir_shbuf_alloc(struct xen_front_pgdir_shbuf_cfg *cfg)
525{
526 struct xen_front_pgdir_shbuf *buf = cfg->pgdir;
527 int ret;
528
529 if (cfg->be_alloc)
530 buf->ops = &backend_ops;
531 else
532 buf->ops = &local_ops;
533 buf->xb_dev = cfg->xb_dev;
534 buf->num_pages = cfg->num_pages;
535 buf->pages = cfg->pages;
536
537 buf->ops->calc_num_grefs(buf);
538
539 ret = alloc_storage(buf);
540 if (ret)
541 goto fail;
542
543 ret = grant_references(buf);
544 if (ret)
545 goto fail;
546
547 buf->ops->fill_page_dir(buf);
548
549 return 0;
550
551fail:
552 xen_front_pgdir_shbuf_free(buf);
553 return ret;
554}
555EXPORT_SYMBOL_GPL(xen_front_pgdir_shbuf_alloc);
556
557MODULE_DESCRIPTION("Xen frontend/backend page directory based "
558 "shared buffer handling");
559MODULE_AUTHOR("Oleksandr Andrushchenko");
560MODULE_LICENSE("GPL");
561