1 /******************************************************************************
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
34 #include <linux/module.h>
35 #include <linux/sched.h>
37 #include <linux/slab.h>
38 #include <linux/vmalloc.h>
39 #include <linux/uaccess.h>
43 #include <xen/interface/xen.h>
45 #include <xen/grant_table.h>
46 #include <xen/interface/memory.h>
47 #include <asm/xen/hypercall.h>
49 #include <asm/pgtable.h>
50 #include <asm/sync_bitops.h>
53 /* External tools reserve first few grant table entries. */
54 #define NR_RESERVED_ENTRIES 8
55 #define GNTTAB_LIST_END 0xffffffff
56 #define GREFS_PER_GRANT_FRAME (PAGE_SIZE / sizeof(struct grant_entry_v1))
58 static grant_ref_t **gnttab_list;
59 static unsigned int nr_grant_frames;
60 static unsigned int boot_max_nr_grant_frames;
61 static int gnttab_free_count;
62 static grant_ref_t gnttab_free_head;
63 static DEFINE_SPINLOCK(gnttab_list_lock);
64 unsigned long xen_hvm_resume_frames;
65 EXPORT_SYMBOL_GPL(xen_hvm_resume_frames);
68 struct grant_entry_v1 *v1;
72 /*This is a structure of function pointers for grant table*/
75 * Mapping a list of frames for storing grant entries. First input
76 * parameter is used to storing grant table address when grant table
77 * being setup, second parameter is the number of frames to map grant
78 * table. Returning GNTST_okay means success and negative value means
81 int (*map_frames)(unsigned long *, unsigned int);
83 * Release a list of frames which are mapped in map_frames for grant
86 void (*unmap_frames)(void);
88 * Introducing a valid entry into the grant table, granting the frame
89 * of this grant entry to domain for accessing, or transfering, or
90 * transitively accessing. First input parameter is reference of this
91 * introduced grant entry, second one is domid of granted domain, third
92 * one is the frame to be granted, and the last one is status of the
93 * grant entry to be updated.
95 void (*update_entry)(grant_ref_t, domid_t, unsigned long, unsigned);
97 * Stop granting a grant entry to domain for accessing. First input
98 * parameter is reference of a grant entry whose grant access will be
99 * stopped, second one is not in use now. If the grant entry is
100 * currently mapped for reading or writing, just return failure(==0)
101 * directly and don't tear down the grant access. Otherwise, stop grant
102 * access for this entry and return success(==1).
104 int (*end_foreign_access_ref)(grant_ref_t, int);
106 * Stop granting a grant entry to domain for transfer. If tranfer has
107 * not started, just reclaim the grant entry and return failure(==0).
108 * Otherwise, wait for the transfer to complete and then return the
111 unsigned long (*end_foreign_transfer_ref)(grant_ref_t);
113 * Query the status of a grant entry. Input parameter is reference of
114 * queried grant entry, return value is the status of queried entry.
115 * Detailed status(writing/reading) can be gotten from the return value
118 int (*query_foreign_access)(grant_ref_t);
121 static struct gnttab_ops *gnttab_interface;
123 static int grant_table_version;
125 static struct gnttab_free_callback *gnttab_free_callback_list;
127 static int gnttab_expand(unsigned int req_entries);
129 #define RPP (PAGE_SIZE / sizeof(grant_ref_t))
131 static inline grant_ref_t *__gnttab_entry(grant_ref_t entry)
133 return &gnttab_list[(entry) / RPP][(entry) % RPP];
135 /* This can be used as an l-value */
136 #define gnttab_entry(entry) (*__gnttab_entry(entry))
138 static int get_free_entries(unsigned count)
144 spin_lock_irqsave(&gnttab_list_lock, flags);
146 if ((gnttab_free_count < count) &&
147 ((rc = gnttab_expand(count - gnttab_free_count)) < 0)) {
148 spin_unlock_irqrestore(&gnttab_list_lock, flags);
152 ref = head = gnttab_free_head;
153 gnttab_free_count -= count;
155 head = gnttab_entry(head);
156 gnttab_free_head = gnttab_entry(head);
157 gnttab_entry(head) = GNTTAB_LIST_END;
159 spin_unlock_irqrestore(&gnttab_list_lock, flags);
164 static void do_free_callbacks(void)
166 struct gnttab_free_callback *callback, *next;
168 callback = gnttab_free_callback_list;
169 gnttab_free_callback_list = NULL;
171 while (callback != NULL) {
172 next = callback->next;
173 if (gnttab_free_count >= callback->count) {
174 callback->next = NULL;
175 callback->fn(callback->arg);
177 callback->next = gnttab_free_callback_list;
178 gnttab_free_callback_list = callback;
184 static inline void check_free_callbacks(void)
186 if (unlikely(gnttab_free_callback_list))
190 static void put_free_entry(grant_ref_t ref)
193 spin_lock_irqsave(&gnttab_list_lock, flags);
194 gnttab_entry(ref) = gnttab_free_head;
195 gnttab_free_head = ref;
197 check_free_callbacks();
198 spin_unlock_irqrestore(&gnttab_list_lock, flags);
202 * Introducing a valid entry into the grant table:
203 * 1. Write ent->domid.
204 * 2. Write ent->frame:
205 * GTF_permit_access: Frame to which access is permitted.
206 * GTF_accept_transfer: Pseudo-phys frame slot being filled by new
207 * frame, or zero if none.
208 * 3. Write memory barrier (WMB).
209 * 4. Write ent->flags, inc. valid type.
211 static void gnttab_update_entry_v1(grant_ref_t ref, domid_t domid,
212 unsigned long frame, unsigned flags)
214 gnttab_shared.v1[ref].domid = domid;
215 gnttab_shared.v1[ref].frame = frame;
217 gnttab_shared.v1[ref].flags = flags;
221 * Public grant-issuing interface functions
223 void gnttab_grant_foreign_access_ref(grant_ref_t ref, domid_t domid,
224 unsigned long frame, int readonly)
226 gnttab_interface->update_entry(ref, domid, frame,
227 GTF_permit_access | (readonly ? GTF_readonly : 0));
229 EXPORT_SYMBOL_GPL(gnttab_grant_foreign_access_ref);
231 int gnttab_grant_foreign_access(domid_t domid, unsigned long frame,
236 ref = get_free_entries(1);
237 if (unlikely(ref < 0))
240 gnttab_grant_foreign_access_ref(ref, domid, frame, readonly);
244 EXPORT_SYMBOL_GPL(gnttab_grant_foreign_access);
246 static int gnttab_query_foreign_access_v1(grant_ref_t ref)
248 return gnttab_shared.v1[ref].flags & (GTF_reading|GTF_writing);
251 int gnttab_query_foreign_access(grant_ref_t ref)
253 return gnttab_interface->query_foreign_access(ref);
255 EXPORT_SYMBOL_GPL(gnttab_query_foreign_access);
257 static int gnttab_end_foreign_access_ref_v1(grant_ref_t ref, int readonly)
262 pflags = &gnttab_shared.v1[ref].flags;
266 if (flags & (GTF_reading|GTF_writing)) {
267 printk(KERN_ALERT "WARNING: g.e. still in use!\n");
270 } while ((nflags = sync_cmpxchg(pflags, flags, 0)) != flags);
275 int gnttab_end_foreign_access_ref(grant_ref_t ref, int readonly)
277 return gnttab_interface->end_foreign_access_ref(ref, readonly);
279 EXPORT_SYMBOL_GPL(gnttab_end_foreign_access_ref);
281 void gnttab_end_foreign_access(grant_ref_t ref, int readonly,
284 if (gnttab_end_foreign_access_ref(ref, readonly)) {
289 /* XXX This needs to be fixed so that the ref and page are
290 placed on a list to be freed up later. */
292 "WARNING: leaking g.e. and page still in use!\n");
295 EXPORT_SYMBOL_GPL(gnttab_end_foreign_access);
297 int gnttab_grant_foreign_transfer(domid_t domid, unsigned long pfn)
301 ref = get_free_entries(1);
302 if (unlikely(ref < 0))
304 gnttab_grant_foreign_transfer_ref(ref, domid, pfn);
308 EXPORT_SYMBOL_GPL(gnttab_grant_foreign_transfer);
310 void gnttab_grant_foreign_transfer_ref(grant_ref_t ref, domid_t domid,
313 gnttab_interface->update_entry(ref, domid, pfn, GTF_accept_transfer);
315 EXPORT_SYMBOL_GPL(gnttab_grant_foreign_transfer_ref);
317 static unsigned long gnttab_end_foreign_transfer_ref_v1(grant_ref_t ref)
323 pflags = &gnttab_shared.v1[ref].flags;
326 * If a transfer is not even yet started, try to reclaim the grant
327 * reference and return failure (== 0).
329 while (!((flags = *pflags) & GTF_transfer_committed)) {
330 if (sync_cmpxchg(pflags, flags, 0) == flags)
335 /* If a transfer is in progress then wait until it is completed. */
336 while (!(flags & GTF_transfer_completed)) {
341 rmb(); /* Read the frame number /after/ reading completion status. */
342 frame = gnttab_shared.v1[ref].frame;
348 unsigned long gnttab_end_foreign_transfer_ref(grant_ref_t ref)
350 return gnttab_interface->end_foreign_transfer_ref(ref);
352 EXPORT_SYMBOL_GPL(gnttab_end_foreign_transfer_ref);
354 unsigned long gnttab_end_foreign_transfer(grant_ref_t ref)
356 unsigned long frame = gnttab_end_foreign_transfer_ref(ref);
360 EXPORT_SYMBOL_GPL(gnttab_end_foreign_transfer);
362 void gnttab_free_grant_reference(grant_ref_t ref)
366 EXPORT_SYMBOL_GPL(gnttab_free_grant_reference);
368 void gnttab_free_grant_references(grant_ref_t head)
373 if (head == GNTTAB_LIST_END)
375 spin_lock_irqsave(&gnttab_list_lock, flags);
377 while (gnttab_entry(ref) != GNTTAB_LIST_END) {
378 ref = gnttab_entry(ref);
381 gnttab_entry(ref) = gnttab_free_head;
382 gnttab_free_head = head;
383 gnttab_free_count += count;
384 check_free_callbacks();
385 spin_unlock_irqrestore(&gnttab_list_lock, flags);
387 EXPORT_SYMBOL_GPL(gnttab_free_grant_references);
389 int gnttab_alloc_grant_references(u16 count, grant_ref_t *head)
391 int h = get_free_entries(count);
400 EXPORT_SYMBOL_GPL(gnttab_alloc_grant_references);
402 int gnttab_empty_grant_references(const grant_ref_t *private_head)
404 return (*private_head == GNTTAB_LIST_END);
406 EXPORT_SYMBOL_GPL(gnttab_empty_grant_references);
408 int gnttab_claim_grant_reference(grant_ref_t *private_head)
410 grant_ref_t g = *private_head;
411 if (unlikely(g == GNTTAB_LIST_END))
413 *private_head = gnttab_entry(g);
416 EXPORT_SYMBOL_GPL(gnttab_claim_grant_reference);
418 void gnttab_release_grant_reference(grant_ref_t *private_head,
421 gnttab_entry(release) = *private_head;
422 *private_head = release;
424 EXPORT_SYMBOL_GPL(gnttab_release_grant_reference);
426 void gnttab_request_free_callback(struct gnttab_free_callback *callback,
427 void (*fn)(void *), void *arg, u16 count)
430 spin_lock_irqsave(&gnttab_list_lock, flags);
435 callback->count = count;
436 callback->next = gnttab_free_callback_list;
437 gnttab_free_callback_list = callback;
438 check_free_callbacks();
440 spin_unlock_irqrestore(&gnttab_list_lock, flags);
442 EXPORT_SYMBOL_GPL(gnttab_request_free_callback);
444 void gnttab_cancel_free_callback(struct gnttab_free_callback *callback)
446 struct gnttab_free_callback **pcb;
449 spin_lock_irqsave(&gnttab_list_lock, flags);
450 for (pcb = &gnttab_free_callback_list; *pcb; pcb = &(*pcb)->next) {
451 if (*pcb == callback) {
452 *pcb = callback->next;
456 spin_unlock_irqrestore(&gnttab_list_lock, flags);
458 EXPORT_SYMBOL_GPL(gnttab_cancel_free_callback);
460 static int grow_gnttab_list(unsigned int more_frames)
462 unsigned int new_nr_grant_frames, extra_entries, i;
463 unsigned int nr_glist_frames, new_nr_glist_frames;
465 new_nr_grant_frames = nr_grant_frames + more_frames;
466 extra_entries = more_frames * GREFS_PER_GRANT_FRAME;
468 nr_glist_frames = (nr_grant_frames * GREFS_PER_GRANT_FRAME + RPP - 1) / RPP;
469 new_nr_glist_frames =
470 (new_nr_grant_frames * GREFS_PER_GRANT_FRAME + RPP - 1) / RPP;
471 for (i = nr_glist_frames; i < new_nr_glist_frames; i++) {
472 gnttab_list[i] = (grant_ref_t *)__get_free_page(GFP_ATOMIC);
478 for (i = GREFS_PER_GRANT_FRAME * nr_grant_frames;
479 i < GREFS_PER_GRANT_FRAME * new_nr_grant_frames - 1; i++)
480 gnttab_entry(i) = i + 1;
482 gnttab_entry(i) = gnttab_free_head;
483 gnttab_free_head = GREFS_PER_GRANT_FRAME * nr_grant_frames;
484 gnttab_free_count += extra_entries;
486 nr_grant_frames = new_nr_grant_frames;
488 check_free_callbacks();
493 for ( ; i >= nr_glist_frames; i--)
494 free_page((unsigned long) gnttab_list[i]);
498 static unsigned int __max_nr_grant_frames(void)
500 struct gnttab_query_size query;
503 query.dom = DOMID_SELF;
505 rc = HYPERVISOR_grant_table_op(GNTTABOP_query_size, &query, 1);
506 if ((rc < 0) || (query.status != GNTST_okay))
507 return 4; /* Legacy max supported number of frames */
509 return query.max_nr_frames;
512 unsigned int gnttab_max_grant_frames(void)
514 unsigned int xen_max = __max_nr_grant_frames();
516 if (xen_max > boot_max_nr_grant_frames)
517 return boot_max_nr_grant_frames;
520 EXPORT_SYMBOL_GPL(gnttab_max_grant_frames);
522 int gnttab_map_refs(struct gnttab_map_grant_ref *map_ops,
523 struct gnttab_map_grant_ref *kmap_ops,
524 struct page **pages, unsigned int count)
530 ret = HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, map_ops, count);
534 if (xen_feature(XENFEAT_auto_translated_physmap))
537 for (i = 0; i < count; i++) {
538 /* Do not add to override if the map failed. */
539 if (map_ops[i].status)
542 if (map_ops[i].flags & GNTMAP_contains_pte) {
543 pte = (pte_t *) (mfn_to_virt(PFN_DOWN(map_ops[i].host_addr)) +
544 (map_ops[i].host_addr & ~PAGE_MASK));
547 /* If you really wanted to do this:
548 * mfn = PFN_DOWN(map_ops[i].dev_bus_addr);
550 * The reason we do not implement it is b/c on the
551 * unmap path (gnttab_unmap_refs) we have no means of
552 * checking whether the page is !GNTMAP_contains_pte.
554 * That is without some extra data-structure to carry
555 * the struct page, bool clear_pte, and list_head next
556 * tuples and deal with allocation/delallocation, etc.
558 * The users of this API set the GNTMAP_contains_pte
559 * flag so lets just return not supported until it
560 * becomes neccessary to implement.
564 ret = m2p_add_override(mfn, pages[i], &kmap_ops[i]);
571 EXPORT_SYMBOL_GPL(gnttab_map_refs);
573 int gnttab_unmap_refs(struct gnttab_unmap_grant_ref *unmap_ops,
574 struct page **pages, unsigned int count)
578 ret = HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, unmap_ops, count);
582 if (xen_feature(XENFEAT_auto_translated_physmap))
585 for (i = 0; i < count; i++) {
586 ret = m2p_remove_override(pages[i], true /* clear the PTE */);
593 EXPORT_SYMBOL_GPL(gnttab_unmap_refs);
595 static int gnttab_map_frames_v1(unsigned long *frames, unsigned int nr_gframes)
599 rc = arch_gnttab_map_shared(frames, nr_gframes,
600 gnttab_max_grant_frames(),
601 &gnttab_shared.addr);
607 static void gnttab_unmap_frames_v1(void)
609 arch_gnttab_unmap_shared(gnttab_shared.addr, nr_grant_frames);
612 static int gnttab_map(unsigned int start_idx, unsigned int end_idx)
614 struct gnttab_setup_table setup;
615 unsigned long *frames;
616 unsigned int nr_gframes = end_idx + 1;
619 if (xen_hvm_domain()) {
620 struct xen_add_to_physmap xatp;
621 unsigned int i = end_idx;
624 * Loop backwards, so that the first hypercall has the largest
625 * index, ensuring that the table will grow only once.
628 xatp.domid = DOMID_SELF;
630 xatp.space = XENMAPSPACE_grant_table;
631 xatp.gpfn = (xen_hvm_resume_frames >> PAGE_SHIFT) + i;
632 rc = HYPERVISOR_memory_op(XENMEM_add_to_physmap, &xatp);
635 "grant table add_to_physmap failed, err=%d\n", rc);
638 } while (i-- > start_idx);
643 frames = kmalloc(nr_gframes * sizeof(unsigned long), GFP_ATOMIC);
647 setup.dom = DOMID_SELF;
648 setup.nr_frames = nr_gframes;
649 set_xen_guest_handle(setup.frame_list, frames);
651 rc = HYPERVISOR_grant_table_op(GNTTABOP_setup_table, &setup, 1);
657 BUG_ON(rc || setup.status);
659 rc = gnttab_interface->map_frames(frames, nr_gframes);
666 static struct gnttab_ops gnttab_v1_ops = {
667 .map_frames = gnttab_map_frames_v1,
668 .unmap_frames = gnttab_unmap_frames_v1,
669 .update_entry = gnttab_update_entry_v1,
670 .end_foreign_access_ref = gnttab_end_foreign_access_ref_v1,
671 .end_foreign_transfer_ref = gnttab_end_foreign_transfer_ref_v1,
672 .query_foreign_access = gnttab_query_foreign_access_v1,
675 static void gnttab_request_version(void)
677 grant_table_version = 1;
678 gnttab_interface = &gnttab_v1_ops;
679 printk(KERN_INFO "Grant tables using version %d layout.\n",
680 grant_table_version);
683 int gnttab_resume(void)
685 unsigned int max_nr_gframes;
687 gnttab_request_version();
688 max_nr_gframes = gnttab_max_grant_frames();
689 if (max_nr_gframes < nr_grant_frames)
693 return gnttab_map(0, nr_grant_frames - 1);
695 if (gnttab_shared.addr == NULL) {
696 gnttab_shared.addr = ioremap(xen_hvm_resume_frames,
697 PAGE_SIZE * max_nr_gframes);
698 if (gnttab_shared.addr == NULL) {
700 "Failed to ioremap gnttab share frames!");
705 gnttab_map(0, nr_grant_frames - 1);
710 int gnttab_suspend(void)
712 gnttab_interface->unmap_frames();
716 static int gnttab_expand(unsigned int req_entries)
719 unsigned int cur, extra;
721 cur = nr_grant_frames;
722 extra = ((req_entries + (GREFS_PER_GRANT_FRAME-1)) /
723 GREFS_PER_GRANT_FRAME);
724 if (cur + extra > gnttab_max_grant_frames())
727 rc = gnttab_map(cur, cur + extra - 1);
729 rc = grow_gnttab_list(extra);
734 int gnttab_init(void)
737 unsigned int max_nr_glist_frames, nr_glist_frames;
738 unsigned int nr_init_grefs;
741 boot_max_nr_grant_frames = __max_nr_grant_frames();
743 /* Determine the maximum number of frames required for the
744 * grant reference free list on the current hypervisor.
746 max_nr_glist_frames = (boot_max_nr_grant_frames *
747 GREFS_PER_GRANT_FRAME / RPP);
749 gnttab_list = kmalloc(max_nr_glist_frames * sizeof(grant_ref_t *),
751 if (gnttab_list == NULL)
754 nr_glist_frames = (nr_grant_frames * GREFS_PER_GRANT_FRAME + RPP - 1) / RPP;
755 for (i = 0; i < nr_glist_frames; i++) {
756 gnttab_list[i] = (grant_ref_t *)__get_free_page(GFP_KERNEL);
757 if (gnttab_list[i] == NULL)
761 if (gnttab_resume() < 0)
764 nr_init_grefs = nr_grant_frames * GREFS_PER_GRANT_FRAME;
766 for (i = NR_RESERVED_ENTRIES; i < nr_init_grefs - 1; i++)
767 gnttab_entry(i) = i + 1;
769 gnttab_entry(nr_init_grefs - 1) = GNTTAB_LIST_END;
770 gnttab_free_count = nr_init_grefs - NR_RESERVED_ENTRIES;
771 gnttab_free_head = NR_RESERVED_ENTRIES;
773 printk("Grant table initialized\n");
777 for (i--; i >= 0; i--)
778 free_page((unsigned long)gnttab_list[i]);
782 EXPORT_SYMBOL_GPL(gnttab_init);
784 static int __devinit __gnttab_init(void)
786 /* Delay grant-table initialization in the PV on HVM case */
787 if (xen_hvm_domain())
790 if (!xen_pv_domain())
793 return gnttab_init();
796 core_initcall(__gnttab_init);