Committer: Michael Beasley <mike@snafu.setup>
[mikesnafu-overlay.git] / drivers / xen / grant-table.c
blobd85dc6d41c2aee930f79f08cb31ebecb406e948c
1 /******************************************************************************
2 * grant_table.c
4 * Granting foreign access to our memory reservation.
6 * Copyright (c) 2005-2006, Christopher Clark
7 * Copyright (c) 2004-2005, K A Fraser
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License version 2
11 * as published by the Free Software Foundation; or, when distributed
12 * separately from the Linux kernel or incorporated into other
13 * software packages, subject to the following license:
15 * Permission is hereby granted, free of charge, to any person obtaining a copy
16 * of this source file (the "Software"), to deal in the Software without
17 * restriction, including without limitation the rights to use, copy, modify,
18 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
19 * and to permit persons to whom the Software is furnished to do so, subject to
20 * the following conditions:
22 * The above copyright notice and this permission notice shall be included in
23 * all copies or substantial portions of the Software.
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
30 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
31 * IN THE SOFTWARE.
34 #include <linux/module.h>
35 #include <linux/sched.h>
36 #include <linux/mm.h>
37 #include <linux/vmalloc.h>
38 #include <linux/uaccess.h>
40 #include <xen/interface/xen.h>
41 #include <xen/page.h>
42 #include <xen/grant_table.h>
44 #include <asm/pgtable.h>
45 #include <asm/sync_bitops.h>
48 /* External tools reserve first few grant table entries. */
49 #define NR_RESERVED_ENTRIES 8
50 #define GNTTAB_LIST_END 0xffffffff
51 #define GREFS_PER_GRANT_FRAME (PAGE_SIZE / sizeof(struct grant_entry))
53 static grant_ref_t **gnttab_list;
54 static unsigned int nr_grant_frames;
55 static unsigned int boot_max_nr_grant_frames;
56 static int gnttab_free_count;
57 static grant_ref_t gnttab_free_head;
58 static DEFINE_SPINLOCK(gnttab_list_lock);
60 static struct grant_entry *shared;
62 static struct gnttab_free_callback *gnttab_free_callback_list;
64 static int gnttab_expand(unsigned int req_entries);
66 #define RPP (PAGE_SIZE / sizeof(grant_ref_t))
68 static inline grant_ref_t *__gnttab_entry(grant_ref_t entry)
70 return &gnttab_list[(entry) / RPP][(entry) % RPP];
72 /* This can be used as an l-value */
73 #define gnttab_entry(entry) (*__gnttab_entry(entry))
75 static int get_free_entries(unsigned count)
77 unsigned long flags;
78 int ref, rc;
79 grant_ref_t head;
81 spin_lock_irqsave(&gnttab_list_lock, flags);
83 if ((gnttab_free_count < count) &&
84 ((rc = gnttab_expand(count - gnttab_free_count)) < 0)) {
85 spin_unlock_irqrestore(&gnttab_list_lock, flags);
86 return rc;
89 ref = head = gnttab_free_head;
90 gnttab_free_count -= count;
91 while (count-- > 1)
92 head = gnttab_entry(head);
93 gnttab_free_head = gnttab_entry(head);
94 gnttab_entry(head) = GNTTAB_LIST_END;
96 spin_unlock_irqrestore(&gnttab_list_lock, flags);
98 return ref;
101 static void do_free_callbacks(void)
103 struct gnttab_free_callback *callback, *next;
105 callback = gnttab_free_callback_list;
106 gnttab_free_callback_list = NULL;
108 while (callback != NULL) {
109 next = callback->next;
110 if (gnttab_free_count >= callback->count) {
111 callback->next = NULL;
112 callback->fn(callback->arg);
113 } else {
114 callback->next = gnttab_free_callback_list;
115 gnttab_free_callback_list = callback;
117 callback = next;
121 static inline void check_free_callbacks(void)
123 if (unlikely(gnttab_free_callback_list))
124 do_free_callbacks();
127 static void put_free_entry(grant_ref_t ref)
129 unsigned long flags;
130 spin_lock_irqsave(&gnttab_list_lock, flags);
131 gnttab_entry(ref) = gnttab_free_head;
132 gnttab_free_head = ref;
133 gnttab_free_count++;
134 check_free_callbacks();
135 spin_unlock_irqrestore(&gnttab_list_lock, flags);
138 static void update_grant_entry(grant_ref_t ref, domid_t domid,
139 unsigned long frame, unsigned flags)
142 * Introducing a valid entry into the grant table:
143 * 1. Write ent->domid.
144 * 2. Write ent->frame:
145 * GTF_permit_access: Frame to which access is permitted.
146 * GTF_accept_transfer: Pseudo-phys frame slot being filled by new
147 * frame, or zero if none.
148 * 3. Write memory barrier (WMB).
149 * 4. Write ent->flags, inc. valid type.
151 shared[ref].frame = frame;
152 shared[ref].domid = domid;
153 wmb();
154 shared[ref].flags = flags;
158 * Public grant-issuing interface functions
160 void gnttab_grant_foreign_access_ref(grant_ref_t ref, domid_t domid,
161 unsigned long frame, int readonly)
163 update_grant_entry(ref, domid, frame,
164 GTF_permit_access | (readonly ? GTF_readonly : 0));
166 EXPORT_SYMBOL_GPL(gnttab_grant_foreign_access_ref);
168 int gnttab_grant_foreign_access(domid_t domid, unsigned long frame,
169 int readonly)
171 int ref;
173 ref = get_free_entries(1);
174 if (unlikely(ref < 0))
175 return -ENOSPC;
177 gnttab_grant_foreign_access_ref(ref, domid, frame, readonly);
179 return ref;
181 EXPORT_SYMBOL_GPL(gnttab_grant_foreign_access);
183 int gnttab_query_foreign_access(grant_ref_t ref)
185 u16 nflags;
187 nflags = shared[ref].flags;
189 return (nflags & (GTF_reading|GTF_writing));
191 EXPORT_SYMBOL_GPL(gnttab_query_foreign_access);
193 int gnttab_end_foreign_access_ref(grant_ref_t ref, int readonly)
195 u16 flags, nflags;
197 nflags = shared[ref].flags;
198 do {
199 flags = nflags;
200 if (flags & (GTF_reading|GTF_writing)) {
201 printk(KERN_ALERT "WARNING: g.e. still in use!\n");
202 return 0;
204 } while ((nflags = sync_cmpxchg(&shared[ref].flags, flags, 0)) != flags);
206 return 1;
208 EXPORT_SYMBOL_GPL(gnttab_end_foreign_access_ref);
210 void gnttab_end_foreign_access(grant_ref_t ref, int readonly,
211 unsigned long page)
213 if (gnttab_end_foreign_access_ref(ref, readonly)) {
214 put_free_entry(ref);
215 if (page != 0)
216 free_page(page);
217 } else {
218 /* XXX This needs to be fixed so that the ref and page are
219 placed on a list to be freed up later. */
220 printk(KERN_WARNING
221 "WARNING: leaking g.e. and page still in use!\n");
224 EXPORT_SYMBOL_GPL(gnttab_end_foreign_access);
226 int gnttab_grant_foreign_transfer(domid_t domid, unsigned long pfn)
228 int ref;
230 ref = get_free_entries(1);
231 if (unlikely(ref < 0))
232 return -ENOSPC;
233 gnttab_grant_foreign_transfer_ref(ref, domid, pfn);
235 return ref;
237 EXPORT_SYMBOL_GPL(gnttab_grant_foreign_transfer);
239 void gnttab_grant_foreign_transfer_ref(grant_ref_t ref, domid_t domid,
240 unsigned long pfn)
242 update_grant_entry(ref, domid, pfn, GTF_accept_transfer);
244 EXPORT_SYMBOL_GPL(gnttab_grant_foreign_transfer_ref);
246 unsigned long gnttab_end_foreign_transfer_ref(grant_ref_t ref)
248 unsigned long frame;
249 u16 flags;
252 * If a transfer is not even yet started, try to reclaim the grant
253 * reference and return failure (== 0).
255 while (!((flags = shared[ref].flags) & GTF_transfer_committed)) {
256 if (sync_cmpxchg(&shared[ref].flags, flags, 0) == flags)
257 return 0;
258 cpu_relax();
261 /* If a transfer is in progress then wait until it is completed. */
262 while (!(flags & GTF_transfer_completed)) {
263 flags = shared[ref].flags;
264 cpu_relax();
267 rmb(); /* Read the frame number /after/ reading completion status. */
268 frame = shared[ref].frame;
269 BUG_ON(frame == 0);
271 return frame;
273 EXPORT_SYMBOL_GPL(gnttab_end_foreign_transfer_ref);
275 unsigned long gnttab_end_foreign_transfer(grant_ref_t ref)
277 unsigned long frame = gnttab_end_foreign_transfer_ref(ref);
278 put_free_entry(ref);
279 return frame;
281 EXPORT_SYMBOL_GPL(gnttab_end_foreign_transfer);
283 void gnttab_free_grant_reference(grant_ref_t ref)
285 put_free_entry(ref);
287 EXPORT_SYMBOL_GPL(gnttab_free_grant_reference);
289 void gnttab_free_grant_references(grant_ref_t head)
291 grant_ref_t ref;
292 unsigned long flags;
293 int count = 1;
294 if (head == GNTTAB_LIST_END)
295 return;
296 spin_lock_irqsave(&gnttab_list_lock, flags);
297 ref = head;
298 while (gnttab_entry(ref) != GNTTAB_LIST_END) {
299 ref = gnttab_entry(ref);
300 count++;
302 gnttab_entry(ref) = gnttab_free_head;
303 gnttab_free_head = head;
304 gnttab_free_count += count;
305 check_free_callbacks();
306 spin_unlock_irqrestore(&gnttab_list_lock, flags);
308 EXPORT_SYMBOL_GPL(gnttab_free_grant_references);
310 int gnttab_alloc_grant_references(u16 count, grant_ref_t *head)
312 int h = get_free_entries(count);
314 if (h < 0)
315 return -ENOSPC;
317 *head = h;
319 return 0;
321 EXPORT_SYMBOL_GPL(gnttab_alloc_grant_references);
323 int gnttab_empty_grant_references(const grant_ref_t *private_head)
325 return (*private_head == GNTTAB_LIST_END);
327 EXPORT_SYMBOL_GPL(gnttab_empty_grant_references);
329 int gnttab_claim_grant_reference(grant_ref_t *private_head)
331 grant_ref_t g = *private_head;
332 if (unlikely(g == GNTTAB_LIST_END))
333 return -ENOSPC;
334 *private_head = gnttab_entry(g);
335 return g;
337 EXPORT_SYMBOL_GPL(gnttab_claim_grant_reference);
339 void gnttab_release_grant_reference(grant_ref_t *private_head,
340 grant_ref_t release)
342 gnttab_entry(release) = *private_head;
343 *private_head = release;
345 EXPORT_SYMBOL_GPL(gnttab_release_grant_reference);
347 void gnttab_request_free_callback(struct gnttab_free_callback *callback,
348 void (*fn)(void *), void *arg, u16 count)
350 unsigned long flags;
351 spin_lock_irqsave(&gnttab_list_lock, flags);
352 if (callback->next)
353 goto out;
354 callback->fn = fn;
355 callback->arg = arg;
356 callback->count = count;
357 callback->next = gnttab_free_callback_list;
358 gnttab_free_callback_list = callback;
359 check_free_callbacks();
360 out:
361 spin_unlock_irqrestore(&gnttab_list_lock, flags);
363 EXPORT_SYMBOL_GPL(gnttab_request_free_callback);
365 void gnttab_cancel_free_callback(struct gnttab_free_callback *callback)
367 struct gnttab_free_callback **pcb;
368 unsigned long flags;
370 spin_lock_irqsave(&gnttab_list_lock, flags);
371 for (pcb = &gnttab_free_callback_list; *pcb; pcb = &(*pcb)->next) {
372 if (*pcb == callback) {
373 *pcb = callback->next;
374 break;
377 spin_unlock_irqrestore(&gnttab_list_lock, flags);
379 EXPORT_SYMBOL_GPL(gnttab_cancel_free_callback);
381 static int grow_gnttab_list(unsigned int more_frames)
383 unsigned int new_nr_grant_frames, extra_entries, i;
384 unsigned int nr_glist_frames, new_nr_glist_frames;
386 new_nr_grant_frames = nr_grant_frames + more_frames;
387 extra_entries = more_frames * GREFS_PER_GRANT_FRAME;
389 nr_glist_frames = (nr_grant_frames * GREFS_PER_GRANT_FRAME + RPP - 1) / RPP;
390 new_nr_glist_frames =
391 (new_nr_grant_frames * GREFS_PER_GRANT_FRAME + RPP - 1) / RPP;
392 for (i = nr_glist_frames; i < new_nr_glist_frames; i++) {
393 gnttab_list[i] = (grant_ref_t *)__get_free_page(GFP_ATOMIC);
394 if (!gnttab_list[i])
395 goto grow_nomem;
399 for (i = GREFS_PER_GRANT_FRAME * nr_grant_frames;
400 i < GREFS_PER_GRANT_FRAME * new_nr_grant_frames - 1; i++)
401 gnttab_entry(i) = i + 1;
403 gnttab_entry(i) = gnttab_free_head;
404 gnttab_free_head = GREFS_PER_GRANT_FRAME * nr_grant_frames;
405 gnttab_free_count += extra_entries;
407 nr_grant_frames = new_nr_grant_frames;
409 check_free_callbacks();
411 return 0;
413 grow_nomem:
414 for ( ; i >= nr_glist_frames; i--)
415 free_page((unsigned long) gnttab_list[i]);
416 return -ENOMEM;
419 static unsigned int __max_nr_grant_frames(void)
421 struct gnttab_query_size query;
422 int rc;
424 query.dom = DOMID_SELF;
426 rc = HYPERVISOR_grant_table_op(GNTTABOP_query_size, &query, 1);
427 if ((rc < 0) || (query.status != GNTST_okay))
428 return 4; /* Legacy max supported number of frames */
430 return query.max_nr_frames;
433 static inline unsigned int max_nr_grant_frames(void)
435 unsigned int xen_max = __max_nr_grant_frames();
437 if (xen_max > boot_max_nr_grant_frames)
438 return boot_max_nr_grant_frames;
439 return xen_max;
442 static int map_pte_fn(pte_t *pte, struct page *pmd_page,
443 unsigned long addr, void *data)
445 unsigned long **frames = (unsigned long **)data;
447 set_pte_at(&init_mm, addr, pte, mfn_pte((*frames)[0], PAGE_KERNEL));
448 (*frames)++;
449 return 0;
452 static int unmap_pte_fn(pte_t *pte, struct page *pmd_page,
453 unsigned long addr, void *data)
456 set_pte_at(&init_mm, addr, pte, __pte(0));
457 return 0;
460 static int gnttab_map(unsigned int start_idx, unsigned int end_idx)
462 struct gnttab_setup_table setup;
463 unsigned long *frames;
464 unsigned int nr_gframes = end_idx + 1;
465 int rc;
467 frames = kmalloc(nr_gframes * sizeof(unsigned long), GFP_ATOMIC);
468 if (!frames)
469 return -ENOMEM;
471 setup.dom = DOMID_SELF;
472 setup.nr_frames = nr_gframes;
473 setup.frame_list = frames;
475 rc = HYPERVISOR_grant_table_op(GNTTABOP_setup_table, &setup, 1);
476 if (rc == -ENOSYS) {
477 kfree(frames);
478 return -ENOSYS;
481 BUG_ON(rc || setup.status);
483 if (shared == NULL) {
484 struct vm_struct *area;
485 area = alloc_vm_area(PAGE_SIZE * max_nr_grant_frames());
486 BUG_ON(area == NULL);
487 shared = area->addr;
489 rc = apply_to_page_range(&init_mm, (unsigned long)shared,
490 PAGE_SIZE * nr_gframes,
491 map_pte_fn, &frames);
492 BUG_ON(rc);
493 frames -= nr_gframes; /* adjust after map_pte_fn() */
495 kfree(frames);
497 return 0;
500 static int gnttab_resume(void)
502 if (max_nr_grant_frames() < nr_grant_frames)
503 return -ENOSYS;
504 return gnttab_map(0, nr_grant_frames - 1);
507 static int gnttab_suspend(void)
509 apply_to_page_range(&init_mm, (unsigned long)shared,
510 PAGE_SIZE * nr_grant_frames,
511 unmap_pte_fn, NULL);
513 return 0;
516 static int gnttab_expand(unsigned int req_entries)
518 int rc;
519 unsigned int cur, extra;
521 cur = nr_grant_frames;
522 extra = ((req_entries + (GREFS_PER_GRANT_FRAME-1)) /
523 GREFS_PER_GRANT_FRAME);
524 if (cur + extra > max_nr_grant_frames())
525 return -ENOSPC;
527 rc = gnttab_map(cur, cur + extra - 1);
528 if (rc == 0)
529 rc = grow_gnttab_list(extra);
531 return rc;
534 static int __devinit gnttab_init(void)
536 int i;
537 unsigned int max_nr_glist_frames, nr_glist_frames;
538 unsigned int nr_init_grefs;
540 if (!is_running_on_xen())
541 return -ENODEV;
543 nr_grant_frames = 1;
544 boot_max_nr_grant_frames = __max_nr_grant_frames();
546 /* Determine the maximum number of frames required for the
547 * grant reference free list on the current hypervisor.
549 max_nr_glist_frames = (boot_max_nr_grant_frames *
550 GREFS_PER_GRANT_FRAME / RPP);
552 gnttab_list = kmalloc(max_nr_glist_frames * sizeof(grant_ref_t *),
553 GFP_KERNEL);
554 if (gnttab_list == NULL)
555 return -ENOMEM;
557 nr_glist_frames = (nr_grant_frames * GREFS_PER_GRANT_FRAME + RPP - 1) / RPP;
558 for (i = 0; i < nr_glist_frames; i++) {
559 gnttab_list[i] = (grant_ref_t *)__get_free_page(GFP_KERNEL);
560 if (gnttab_list[i] == NULL)
561 goto ini_nomem;
564 if (gnttab_resume() < 0)
565 return -ENODEV;
567 nr_init_grefs = nr_grant_frames * GREFS_PER_GRANT_FRAME;
569 for (i = NR_RESERVED_ENTRIES; i < nr_init_grefs - 1; i++)
570 gnttab_entry(i) = i + 1;
572 gnttab_entry(nr_init_grefs - 1) = GNTTAB_LIST_END;
573 gnttab_free_count = nr_init_grefs - NR_RESERVED_ENTRIES;
574 gnttab_free_head = NR_RESERVED_ENTRIES;
576 printk("Grant table initialized\n");
577 return 0;
579 ini_nomem:
580 for (i--; i >= 0; i--)
581 free_page((unsigned long)gnttab_list[i]);
582 kfree(gnttab_list);
583 return -ENOMEM;
586 core_initcall(gnttab_init);