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>
41 #include <linux/hardirq.h>
44 #include <xen/interface/xen.h>
46 #include <xen/grant_table.h>
47 #include <xen/interface/memory.h>
48 #include <xen/hvc-console.h>
49 #include <asm/xen/hypercall.h>
51 #include <asm/pgtable.h>
52 #include <asm/sync_bitops.h>
54 /* External tools reserve first few grant table entries. */
55 #define NR_RESERVED_ENTRIES 8
56 #define GNTTAB_LIST_END 0xffffffff
57 #define GREFS_PER_GRANT_FRAME \
58 (grant_table_version == 1 ? \
59 (PAGE_SIZE / sizeof(struct grant_entry_v1)) : \
60 (PAGE_SIZE / sizeof(union grant_entry_v2)))
62 static grant_ref_t **gnttab_list;
63 static unsigned int nr_grant_frames;
64 static unsigned int boot_max_nr_grant_frames;
65 static int gnttab_free_count;
66 static grant_ref_t gnttab_free_head;
67 static DEFINE_SPINLOCK(gnttab_list_lock);
68 unsigned long xen_hvm_resume_frames;
69 EXPORT_SYMBOL_GPL(xen_hvm_resume_frames);
72 struct grant_entry_v1 *v1;
73 union grant_entry_v2 *v2;
77 /*This is a structure of function pointers for grant table*/
80 * Mapping a list of frames for storing grant entries. Frames parameter
81 * is used to store grant table address when grant table being setup,
82 * nr_gframes is the number of frames to map grant table. Returning
83 * GNTST_okay means success and negative value means failure.
85 int (*map_frames)(unsigned long *frames, unsigned int nr_gframes);
87 * Release a list of frames which are mapped in map_frames for grant
90 void (*unmap_frames)(void);
92 * Introducing a valid entry into the grant table, granting the frame of
93 * this grant entry to domain for accessing or transfering. Ref
94 * parameter is reference of this introduced grant entry, domid is id of
95 * granted domain, frame is the page frame to be granted, and flags is
96 * status of the grant entry to be updated.
98 void (*update_entry)(grant_ref_t ref, domid_t domid,
99 unsigned long frame, unsigned flags);
101 * Stop granting a grant entry to domain for accessing. Ref parameter is
102 * reference of a grant entry whose grant access will be stopped,
103 * readonly is not in use in this function. If the grant entry is
104 * currently mapped for reading or writing, just return failure(==0)
105 * directly and don't tear down the grant access. Otherwise, stop grant
106 * access for this entry and return success(==1).
108 int (*end_foreign_access_ref)(grant_ref_t ref, int readonly);
110 * Stop granting a grant entry to domain for transfer. Ref parameter is
111 * reference of a grant entry whose grant transfer will be stopped. If
112 * tranfer has not started, just reclaim the grant entry and return
113 * failure(==0). Otherwise, wait for the transfer to complete and then
116 unsigned long (*end_foreign_transfer_ref)(grant_ref_t ref);
118 * Query the status of a grant entry. Ref parameter is reference of
119 * queried grant entry, return value is the status of queried entry.
120 * Detailed status(writing/reading) can be gotten from the return value
123 int (*query_foreign_access)(grant_ref_t ref);
125 * Grant a domain to access a range of bytes within the page referred by
126 * an available grant entry. Ref parameter is reference of a grant entry
127 * which will be sub-page accessed, domid is id of grantee domain, frame
128 * is frame address of subpage grant, flags is grant type and flag
129 * information, page_off is offset of the range of bytes, and length is
130 * length of bytes to be accessed.
132 void (*update_subpage_entry)(grant_ref_t ref, domid_t domid,
133 unsigned long frame, int flags,
134 unsigned page_off, unsigned length);
136 * Redirect an available grant entry on domain A to another grant
137 * reference of domain B, then allow domain C to use grant reference
138 * of domain B transitively. Ref parameter is an available grant entry
139 * reference on domain A, domid is id of domain C which accesses grant
140 * entry transitively, flags is grant type and flag information,
141 * trans_domid is id of domain B whose grant entry is finally accessed
142 * transitively, trans_gref is grant entry transitive reference of
145 void (*update_trans_entry)(grant_ref_t ref, domid_t domid, int flags,
146 domid_t trans_domid, grant_ref_t trans_gref);
149 static struct gnttab_ops *gnttab_interface;
151 /*This reflects status of grant entries, so act as a global value*/
152 static grant_status_t *grstatus;
154 static int grant_table_version;
156 static struct gnttab_free_callback *gnttab_free_callback_list;
158 static int gnttab_expand(unsigned int req_entries);
160 #define RPP (PAGE_SIZE / sizeof(grant_ref_t))
161 #define SPP (PAGE_SIZE / sizeof(grant_status_t))
163 static inline grant_ref_t *__gnttab_entry(grant_ref_t entry)
165 return &gnttab_list[(entry) / RPP][(entry) % RPP];
167 /* This can be used as an l-value */
168 #define gnttab_entry(entry) (*__gnttab_entry(entry))
170 static int get_free_entries(unsigned count)
176 spin_lock_irqsave(&gnttab_list_lock, flags);
178 if ((gnttab_free_count < count) &&
179 ((rc = gnttab_expand(count - gnttab_free_count)) < 0)) {
180 spin_unlock_irqrestore(&gnttab_list_lock, flags);
184 ref = head = gnttab_free_head;
185 gnttab_free_count -= count;
187 head = gnttab_entry(head);
188 gnttab_free_head = gnttab_entry(head);
189 gnttab_entry(head) = GNTTAB_LIST_END;
191 spin_unlock_irqrestore(&gnttab_list_lock, flags);
196 static void do_free_callbacks(void)
198 struct gnttab_free_callback *callback, *next;
200 callback = gnttab_free_callback_list;
201 gnttab_free_callback_list = NULL;
203 while (callback != NULL) {
204 next = callback->next;
205 if (gnttab_free_count >= callback->count) {
206 callback->next = NULL;
207 callback->fn(callback->arg);
209 callback->next = gnttab_free_callback_list;
210 gnttab_free_callback_list = callback;
216 static inline void check_free_callbacks(void)
218 if (unlikely(gnttab_free_callback_list))
222 static void put_free_entry(grant_ref_t ref)
225 spin_lock_irqsave(&gnttab_list_lock, flags);
226 gnttab_entry(ref) = gnttab_free_head;
227 gnttab_free_head = ref;
229 check_free_callbacks();
230 spin_unlock_irqrestore(&gnttab_list_lock, flags);
234 * Following applies to gnttab_update_entry_v1 and gnttab_update_entry_v2.
235 * Introducing a valid entry into the grant table:
236 * 1. Write ent->domid.
237 * 2. Write ent->frame:
238 * GTF_permit_access: Frame to which access is permitted.
239 * GTF_accept_transfer: Pseudo-phys frame slot being filled by new
240 * frame, or zero if none.
241 * 3. Write memory barrier (WMB).
242 * 4. Write ent->flags, inc. valid type.
244 static void gnttab_update_entry_v1(grant_ref_t ref, domid_t domid,
245 unsigned long frame, unsigned flags)
247 gnttab_shared.v1[ref].domid = domid;
248 gnttab_shared.v1[ref].frame = frame;
250 gnttab_shared.v1[ref].flags = flags;
253 static void gnttab_update_entry_v2(grant_ref_t ref, domid_t domid,
254 unsigned long frame, unsigned flags)
256 gnttab_shared.v2[ref].hdr.domid = domid;
257 gnttab_shared.v2[ref].full_page.frame = frame;
259 gnttab_shared.v2[ref].hdr.flags = GTF_permit_access | flags;
263 * Public grant-issuing interface functions
265 void gnttab_grant_foreign_access_ref(grant_ref_t ref, domid_t domid,
266 unsigned long frame, int readonly)
268 gnttab_interface->update_entry(ref, domid, frame,
269 GTF_permit_access | (readonly ? GTF_readonly : 0));
271 EXPORT_SYMBOL_GPL(gnttab_grant_foreign_access_ref);
273 int gnttab_grant_foreign_access(domid_t domid, unsigned long frame,
278 ref = get_free_entries(1);
279 if (unlikely(ref < 0))
282 gnttab_grant_foreign_access_ref(ref, domid, frame, readonly);
286 EXPORT_SYMBOL_GPL(gnttab_grant_foreign_access);
288 void gnttab_update_subpage_entry_v2(grant_ref_t ref, domid_t domid,
289 unsigned long frame, int flags,
293 gnttab_shared.v2[ref].sub_page.frame = frame;
294 gnttab_shared.v2[ref].sub_page.page_off = page_off;
295 gnttab_shared.v2[ref].sub_page.length = length;
296 gnttab_shared.v2[ref].hdr.domid = domid;
298 gnttab_shared.v2[ref].hdr.flags =
299 GTF_permit_access | GTF_sub_page | flags;
302 int gnttab_grant_foreign_access_subpage_ref(grant_ref_t ref, domid_t domid,
303 unsigned long frame, int flags,
307 if (flags & (GTF_accept_transfer | GTF_reading |
308 GTF_writing | GTF_transitive))
311 if (gnttab_interface->update_subpage_entry == NULL)
314 gnttab_interface->update_subpage_entry(ref, domid, frame, flags,
319 EXPORT_SYMBOL_GPL(gnttab_grant_foreign_access_subpage_ref);
321 int gnttab_grant_foreign_access_subpage(domid_t domid, unsigned long frame,
322 int flags, unsigned page_off,
327 ref = get_free_entries(1);
328 if (unlikely(ref < 0))
331 rc = gnttab_grant_foreign_access_subpage_ref(ref, domid, frame, flags,
340 EXPORT_SYMBOL_GPL(gnttab_grant_foreign_access_subpage);
342 bool gnttab_subpage_grants_available(void)
344 return gnttab_interface->update_subpage_entry != NULL;
346 EXPORT_SYMBOL_GPL(gnttab_subpage_grants_available);
348 void gnttab_update_trans_entry_v2(grant_ref_t ref, domid_t domid,
349 int flags, domid_t trans_domid,
350 grant_ref_t trans_gref)
352 gnttab_shared.v2[ref].transitive.trans_domid = trans_domid;
353 gnttab_shared.v2[ref].transitive.gref = trans_gref;
354 gnttab_shared.v2[ref].hdr.domid = domid;
356 gnttab_shared.v2[ref].hdr.flags =
357 GTF_permit_access | GTF_transitive | flags;
360 int gnttab_grant_foreign_access_trans_ref(grant_ref_t ref, domid_t domid,
361 int flags, domid_t trans_domid,
362 grant_ref_t trans_gref)
364 if (flags & (GTF_accept_transfer | GTF_reading |
365 GTF_writing | GTF_sub_page))
368 if (gnttab_interface->update_trans_entry == NULL)
371 gnttab_interface->update_trans_entry(ref, domid, flags, trans_domid,
376 EXPORT_SYMBOL_GPL(gnttab_grant_foreign_access_trans_ref);
378 int gnttab_grant_foreign_access_trans(domid_t domid, int flags,
380 grant_ref_t trans_gref)
384 ref = get_free_entries(1);
385 if (unlikely(ref < 0))
388 rc = gnttab_grant_foreign_access_trans_ref(ref, domid, flags,
389 trans_domid, trans_gref);
397 EXPORT_SYMBOL_GPL(gnttab_grant_foreign_access_trans);
399 bool gnttab_trans_grants_available(void)
401 return gnttab_interface->update_trans_entry != NULL;
403 EXPORT_SYMBOL_GPL(gnttab_trans_grants_available);
405 static int gnttab_query_foreign_access_v1(grant_ref_t ref)
407 return gnttab_shared.v1[ref].flags & (GTF_reading|GTF_writing);
410 static int gnttab_query_foreign_access_v2(grant_ref_t ref)
412 return grstatus[ref] & (GTF_reading|GTF_writing);
415 int gnttab_query_foreign_access(grant_ref_t ref)
417 return gnttab_interface->query_foreign_access(ref);
419 EXPORT_SYMBOL_GPL(gnttab_query_foreign_access);
421 static int gnttab_end_foreign_access_ref_v1(grant_ref_t ref, int readonly)
426 pflags = &gnttab_shared.v1[ref].flags;
430 if (flags & (GTF_reading|GTF_writing))
432 } while ((nflags = sync_cmpxchg(pflags, flags, 0)) != flags);
437 static int gnttab_end_foreign_access_ref_v2(grant_ref_t ref, int readonly)
439 gnttab_shared.v2[ref].hdr.flags = 0;
441 if (grstatus[ref] & (GTF_reading|GTF_writing)) {
444 /* The read of grstatus needs to have acquire
445 semantics. On x86, reads already have
446 that, and we just need to protect against
447 compiler reorderings. On other
448 architectures we may need a full
460 static inline int _gnttab_end_foreign_access_ref(grant_ref_t ref, int readonly)
462 return gnttab_interface->end_foreign_access_ref(ref, readonly);
465 int gnttab_end_foreign_access_ref(grant_ref_t ref, int readonly)
467 if (_gnttab_end_foreign_access_ref(ref, readonly))
469 pr_warn("WARNING: g.e. %#x still in use!\n", ref);
472 EXPORT_SYMBOL_GPL(gnttab_end_foreign_access_ref);
474 struct deferred_entry {
475 struct list_head list;
481 static LIST_HEAD(deferred_list);
482 static void gnttab_handle_deferred(unsigned long);
483 static DEFINE_TIMER(deferred_timer, gnttab_handle_deferred, 0, 0);
485 static void gnttab_handle_deferred(unsigned long unused)
487 unsigned int nr = 10;
488 struct deferred_entry *first = NULL;
491 spin_lock_irqsave(&gnttab_list_lock, flags);
493 struct deferred_entry *entry
494 = list_first_entry(&deferred_list,
495 struct deferred_entry, list);
499 list_del(&entry->list);
500 spin_unlock_irqrestore(&gnttab_list_lock, flags);
501 if (_gnttab_end_foreign_access_ref(entry->ref, entry->ro)) {
502 put_free_entry(entry->ref);
504 pr_debug("freeing g.e. %#x (pfn %#lx)\n",
505 entry->ref, page_to_pfn(entry->page));
506 __free_page(entry->page);
508 pr_info("freeing g.e. %#x\n", entry->ref);
512 if (!--entry->warn_delay)
513 pr_info("g.e. %#x still pending\n",
518 spin_lock_irqsave(&gnttab_list_lock, flags);
520 list_add_tail(&entry->list, &deferred_list);
521 else if (list_empty(&deferred_list))
524 if (!list_empty(&deferred_list) && !timer_pending(&deferred_timer)) {
525 deferred_timer.expires = jiffies + HZ;
526 add_timer(&deferred_timer);
528 spin_unlock_irqrestore(&gnttab_list_lock, flags);
531 static void gnttab_add_deferred(grant_ref_t ref, bool readonly,
534 struct deferred_entry *entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
535 const char *what = KERN_WARNING "leaking";
541 entry->ro = readonly;
543 entry->warn_delay = 60;
544 spin_lock_irqsave(&gnttab_list_lock, flags);
545 list_add_tail(&entry->list, &deferred_list);
546 if (!timer_pending(&deferred_timer)) {
547 deferred_timer.expires = jiffies + HZ;
548 add_timer(&deferred_timer);
550 spin_unlock_irqrestore(&gnttab_list_lock, flags);
551 what = KERN_DEBUG "deferring";
553 printk("%s g.e. %#x (pfn %#lx)\n",
554 what, ref, page ? page_to_pfn(page) : -1);
557 void gnttab_end_foreign_access(grant_ref_t ref, int readonly,
560 if (gnttab_end_foreign_access_ref(ref, readonly)) {
565 gnttab_add_deferred(ref, readonly,
566 page ? virt_to_page(page) : NULL);
568 EXPORT_SYMBOL_GPL(gnttab_end_foreign_access);
570 int gnttab_grant_foreign_transfer(domid_t domid, unsigned long pfn)
574 ref = get_free_entries(1);
575 if (unlikely(ref < 0))
577 gnttab_grant_foreign_transfer_ref(ref, domid, pfn);
581 EXPORT_SYMBOL_GPL(gnttab_grant_foreign_transfer);
583 void gnttab_grant_foreign_transfer_ref(grant_ref_t ref, domid_t domid,
586 gnttab_interface->update_entry(ref, domid, pfn, GTF_accept_transfer);
588 EXPORT_SYMBOL_GPL(gnttab_grant_foreign_transfer_ref);
590 static unsigned long gnttab_end_foreign_transfer_ref_v1(grant_ref_t ref)
596 pflags = &gnttab_shared.v1[ref].flags;
599 * If a transfer is not even yet started, try to reclaim the grant
600 * reference and return failure (== 0).
602 while (!((flags = *pflags) & GTF_transfer_committed)) {
603 if (sync_cmpxchg(pflags, flags, 0) == flags)
608 /* If a transfer is in progress then wait until it is completed. */
609 while (!(flags & GTF_transfer_completed)) {
614 rmb(); /* Read the frame number /after/ reading completion status. */
615 frame = gnttab_shared.v1[ref].frame;
621 static unsigned long gnttab_end_foreign_transfer_ref_v2(grant_ref_t ref)
627 pflags = &gnttab_shared.v2[ref].hdr.flags;
630 * If a transfer is not even yet started, try to reclaim the grant
631 * reference and return failure (== 0).
633 while (!((flags = *pflags) & GTF_transfer_committed)) {
634 if (sync_cmpxchg(pflags, flags, 0) == flags)
639 /* If a transfer is in progress then wait until it is completed. */
640 while (!(flags & GTF_transfer_completed)) {
645 rmb(); /* Read the frame number /after/ reading completion status. */
646 frame = gnttab_shared.v2[ref].full_page.frame;
652 unsigned long gnttab_end_foreign_transfer_ref(grant_ref_t ref)
654 return gnttab_interface->end_foreign_transfer_ref(ref);
656 EXPORT_SYMBOL_GPL(gnttab_end_foreign_transfer_ref);
658 unsigned long gnttab_end_foreign_transfer(grant_ref_t ref)
660 unsigned long frame = gnttab_end_foreign_transfer_ref(ref);
664 EXPORT_SYMBOL_GPL(gnttab_end_foreign_transfer);
666 void gnttab_free_grant_reference(grant_ref_t ref)
670 EXPORT_SYMBOL_GPL(gnttab_free_grant_reference);
672 void gnttab_free_grant_references(grant_ref_t head)
677 if (head == GNTTAB_LIST_END)
679 spin_lock_irqsave(&gnttab_list_lock, flags);
681 while (gnttab_entry(ref) != GNTTAB_LIST_END) {
682 ref = gnttab_entry(ref);
685 gnttab_entry(ref) = gnttab_free_head;
686 gnttab_free_head = head;
687 gnttab_free_count += count;
688 check_free_callbacks();
689 spin_unlock_irqrestore(&gnttab_list_lock, flags);
691 EXPORT_SYMBOL_GPL(gnttab_free_grant_references);
693 int gnttab_alloc_grant_references(u16 count, grant_ref_t *head)
695 int h = get_free_entries(count);
704 EXPORT_SYMBOL_GPL(gnttab_alloc_grant_references);
706 int gnttab_empty_grant_references(const grant_ref_t *private_head)
708 return (*private_head == GNTTAB_LIST_END);
710 EXPORT_SYMBOL_GPL(gnttab_empty_grant_references);
712 int gnttab_claim_grant_reference(grant_ref_t *private_head)
714 grant_ref_t g = *private_head;
715 if (unlikely(g == GNTTAB_LIST_END))
717 *private_head = gnttab_entry(g);
720 EXPORT_SYMBOL_GPL(gnttab_claim_grant_reference);
722 void gnttab_release_grant_reference(grant_ref_t *private_head,
725 gnttab_entry(release) = *private_head;
726 *private_head = release;
728 EXPORT_SYMBOL_GPL(gnttab_release_grant_reference);
730 void gnttab_request_free_callback(struct gnttab_free_callback *callback,
731 void (*fn)(void *), void *arg, u16 count)
734 spin_lock_irqsave(&gnttab_list_lock, flags);
739 callback->count = count;
740 callback->next = gnttab_free_callback_list;
741 gnttab_free_callback_list = callback;
742 check_free_callbacks();
744 spin_unlock_irqrestore(&gnttab_list_lock, flags);
746 EXPORT_SYMBOL_GPL(gnttab_request_free_callback);
748 void gnttab_cancel_free_callback(struct gnttab_free_callback *callback)
750 struct gnttab_free_callback **pcb;
753 spin_lock_irqsave(&gnttab_list_lock, flags);
754 for (pcb = &gnttab_free_callback_list; *pcb; pcb = &(*pcb)->next) {
755 if (*pcb == callback) {
756 *pcb = callback->next;
760 spin_unlock_irqrestore(&gnttab_list_lock, flags);
762 EXPORT_SYMBOL_GPL(gnttab_cancel_free_callback);
764 static int grow_gnttab_list(unsigned int more_frames)
766 unsigned int new_nr_grant_frames, extra_entries, i;
767 unsigned int nr_glist_frames, new_nr_glist_frames;
769 new_nr_grant_frames = nr_grant_frames + more_frames;
770 extra_entries = more_frames * GREFS_PER_GRANT_FRAME;
772 nr_glist_frames = (nr_grant_frames * GREFS_PER_GRANT_FRAME + RPP - 1) / RPP;
773 new_nr_glist_frames =
774 (new_nr_grant_frames * GREFS_PER_GRANT_FRAME + RPP - 1) / RPP;
775 for (i = nr_glist_frames; i < new_nr_glist_frames; i++) {
776 gnttab_list[i] = (grant_ref_t *)__get_free_page(GFP_ATOMIC);
782 for (i = GREFS_PER_GRANT_FRAME * nr_grant_frames;
783 i < GREFS_PER_GRANT_FRAME * new_nr_grant_frames - 1; i++)
784 gnttab_entry(i) = i + 1;
786 gnttab_entry(i) = gnttab_free_head;
787 gnttab_free_head = GREFS_PER_GRANT_FRAME * nr_grant_frames;
788 gnttab_free_count += extra_entries;
790 nr_grant_frames = new_nr_grant_frames;
792 check_free_callbacks();
797 for ( ; i >= nr_glist_frames; i--)
798 free_page((unsigned long) gnttab_list[i]);
802 static unsigned int __max_nr_grant_frames(void)
804 struct gnttab_query_size query;
807 query.dom = DOMID_SELF;
809 rc = HYPERVISOR_grant_table_op(GNTTABOP_query_size, &query, 1);
810 if ((rc < 0) || (query.status != GNTST_okay))
811 return 4; /* Legacy max supported number of frames */
813 return query.max_nr_frames;
816 unsigned int gnttab_max_grant_frames(void)
818 unsigned int xen_max = __max_nr_grant_frames();
820 if (xen_max > boot_max_nr_grant_frames)
821 return boot_max_nr_grant_frames;
824 EXPORT_SYMBOL_GPL(gnttab_max_grant_frames);
826 int gnttab_map_refs(struct gnttab_map_grant_ref *map_ops,
827 struct gnttab_map_grant_ref *kmap_ops,
828 struct page **pages, unsigned int count)
835 ret = HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, map_ops, count);
839 if (xen_feature(XENFEAT_auto_translated_physmap))
842 if (!in_interrupt() && paravirt_get_lazy_mode() == PARAVIRT_LAZY_NONE) {
843 arch_enter_lazy_mmu_mode();
847 for (i = 0; i < count; i++) {
848 /* Do not add to override if the map failed. */
849 if (map_ops[i].status)
852 if (map_ops[i].flags & GNTMAP_contains_pte) {
853 pte = (pte_t *) (mfn_to_virt(PFN_DOWN(map_ops[i].host_addr)) +
854 (map_ops[i].host_addr & ~PAGE_MASK));
857 mfn = PFN_DOWN(map_ops[i].dev_bus_addr);
859 ret = m2p_add_override(mfn, pages[i], kmap_ops ?
860 &kmap_ops[i] : NULL);
866 arch_leave_lazy_mmu_mode();
870 EXPORT_SYMBOL_GPL(gnttab_map_refs);
872 int gnttab_unmap_refs(struct gnttab_unmap_grant_ref *unmap_ops,
873 struct gnttab_map_grant_ref *kmap_ops,
874 struct page **pages, unsigned int count)
879 ret = HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, unmap_ops, count);
883 if (xen_feature(XENFEAT_auto_translated_physmap))
886 if (!in_interrupt() && paravirt_get_lazy_mode() == PARAVIRT_LAZY_NONE) {
887 arch_enter_lazy_mmu_mode();
891 for (i = 0; i < count; i++) {
892 ret = m2p_remove_override(pages[i], kmap_ops ?
893 &kmap_ops[i] : NULL);
899 arch_leave_lazy_mmu_mode();
903 EXPORT_SYMBOL_GPL(gnttab_unmap_refs);
905 static unsigned nr_status_frames(unsigned nr_grant_frames)
907 return (nr_grant_frames * GREFS_PER_GRANT_FRAME + SPP - 1) / SPP;
910 static int gnttab_map_frames_v1(unsigned long *frames, unsigned int nr_gframes)
914 rc = arch_gnttab_map_shared(frames, nr_gframes,
915 gnttab_max_grant_frames(),
916 &gnttab_shared.addr);
922 static void gnttab_unmap_frames_v1(void)
924 arch_gnttab_unmap(gnttab_shared.addr, nr_grant_frames);
927 static int gnttab_map_frames_v2(unsigned long *frames, unsigned int nr_gframes)
930 unsigned int nr_sframes;
931 struct gnttab_get_status_frames getframes;
934 nr_sframes = nr_status_frames(nr_gframes);
936 /* No need for kzalloc as it is initialized in following hypercall
937 * GNTTABOP_get_status_frames.
939 sframes = kmalloc(nr_sframes * sizeof(uint64_t), GFP_ATOMIC);
943 getframes.dom = DOMID_SELF;
944 getframes.nr_frames = nr_sframes;
945 set_xen_guest_handle(getframes.frame_list, sframes);
947 rc = HYPERVISOR_grant_table_op(GNTTABOP_get_status_frames,
954 BUG_ON(rc || getframes.status);
956 rc = arch_gnttab_map_status(sframes, nr_sframes,
957 nr_status_frames(gnttab_max_grant_frames()),
962 rc = arch_gnttab_map_shared(frames, nr_gframes,
963 gnttab_max_grant_frames(),
964 &gnttab_shared.addr);
970 static void gnttab_unmap_frames_v2(void)
972 arch_gnttab_unmap(gnttab_shared.addr, nr_grant_frames);
973 arch_gnttab_unmap(grstatus, nr_status_frames(nr_grant_frames));
976 static int gnttab_map(unsigned int start_idx, unsigned int end_idx)
978 struct gnttab_setup_table setup;
979 unsigned long *frames;
980 unsigned int nr_gframes = end_idx + 1;
983 if (xen_hvm_domain()) {
984 struct xen_add_to_physmap xatp;
985 unsigned int i = end_idx;
988 * Loop backwards, so that the first hypercall has the largest
989 * index, ensuring that the table will grow only once.
992 xatp.domid = DOMID_SELF;
994 xatp.space = XENMAPSPACE_grant_table;
995 xatp.gpfn = (xen_hvm_resume_frames >> PAGE_SHIFT) + i;
996 rc = HYPERVISOR_memory_op(XENMEM_add_to_physmap, &xatp);
999 "grant table add_to_physmap failed, err=%d\n", rc);
1002 } while (i-- > start_idx);
1007 /* No need for kzalloc as it is initialized in following hypercall
1008 * GNTTABOP_setup_table.
1010 frames = kmalloc(nr_gframes * sizeof(unsigned long), GFP_ATOMIC);
1014 setup.dom = DOMID_SELF;
1015 setup.nr_frames = nr_gframes;
1016 set_xen_guest_handle(setup.frame_list, frames);
1018 rc = HYPERVISOR_grant_table_op(GNTTABOP_setup_table, &setup, 1);
1019 if (rc == -ENOSYS) {
1024 BUG_ON(rc || setup.status);
1026 rc = gnttab_interface->map_frames(frames, nr_gframes);
1033 static struct gnttab_ops gnttab_v1_ops = {
1034 .map_frames = gnttab_map_frames_v1,
1035 .unmap_frames = gnttab_unmap_frames_v1,
1036 .update_entry = gnttab_update_entry_v1,
1037 .end_foreign_access_ref = gnttab_end_foreign_access_ref_v1,
1038 .end_foreign_transfer_ref = gnttab_end_foreign_transfer_ref_v1,
1039 .query_foreign_access = gnttab_query_foreign_access_v1,
1042 static struct gnttab_ops gnttab_v2_ops = {
1043 .map_frames = gnttab_map_frames_v2,
1044 .unmap_frames = gnttab_unmap_frames_v2,
1045 .update_entry = gnttab_update_entry_v2,
1046 .end_foreign_access_ref = gnttab_end_foreign_access_ref_v2,
1047 .end_foreign_transfer_ref = gnttab_end_foreign_transfer_ref_v2,
1048 .query_foreign_access = gnttab_query_foreign_access_v2,
1049 .update_subpage_entry = gnttab_update_subpage_entry_v2,
1050 .update_trans_entry = gnttab_update_trans_entry_v2,
1053 static void gnttab_request_version(void)
1056 struct gnttab_set_version gsv;
1058 if (xen_hvm_domain())
1062 rc = HYPERVISOR_grant_table_op(GNTTABOP_set_version, &gsv, 1);
1063 if (rc == 0 && gsv.version == 2) {
1064 grant_table_version = 2;
1065 gnttab_interface = &gnttab_v2_ops;
1066 } else if (grant_table_version == 2) {
1068 * If we've already used version 2 features,
1069 * but then suddenly discover that they're not
1070 * available (e.g. migrating to an older
1071 * version of Xen), almost unbounded badness
1074 panic("we need grant tables version 2, but only version 1 is available");
1076 grant_table_version = 1;
1077 gnttab_interface = &gnttab_v1_ops;
1079 printk(KERN_INFO "Grant tables using version %d layout.\n",
1080 grant_table_version);
1083 int gnttab_resume(void)
1085 unsigned int max_nr_gframes;
1087 gnttab_request_version();
1088 max_nr_gframes = gnttab_max_grant_frames();
1089 if (max_nr_gframes < nr_grant_frames)
1092 if (xen_pv_domain())
1093 return gnttab_map(0, nr_grant_frames - 1);
1095 if (gnttab_shared.addr == NULL) {
1096 gnttab_shared.addr = ioremap(xen_hvm_resume_frames,
1097 PAGE_SIZE * max_nr_gframes);
1098 if (gnttab_shared.addr == NULL) {
1100 "Failed to ioremap gnttab share frames!");
1105 gnttab_map(0, nr_grant_frames - 1);
1110 int gnttab_suspend(void)
1112 gnttab_interface->unmap_frames();
1116 static int gnttab_expand(unsigned int req_entries)
1119 unsigned int cur, extra;
1121 cur = nr_grant_frames;
1122 extra = ((req_entries + (GREFS_PER_GRANT_FRAME-1)) /
1123 GREFS_PER_GRANT_FRAME);
1124 if (cur + extra > gnttab_max_grant_frames())
1127 rc = gnttab_map(cur, cur + extra - 1);
1129 rc = grow_gnttab_list(extra);
1134 int gnttab_init(void)
1137 unsigned int max_nr_glist_frames, nr_glist_frames;
1138 unsigned int nr_init_grefs;
1141 nr_grant_frames = 1;
1142 boot_max_nr_grant_frames = __max_nr_grant_frames();
1144 /* Determine the maximum number of frames required for the
1145 * grant reference free list on the current hypervisor.
1147 max_nr_glist_frames = (boot_max_nr_grant_frames *
1148 GREFS_PER_GRANT_FRAME / RPP);
1150 gnttab_list = kmalloc(max_nr_glist_frames * sizeof(grant_ref_t *),
1152 if (gnttab_list == NULL)
1155 nr_glist_frames = (nr_grant_frames * GREFS_PER_GRANT_FRAME + RPP - 1) / RPP;
1156 for (i = 0; i < nr_glist_frames; i++) {
1157 gnttab_list[i] = (grant_ref_t *)__get_free_page(GFP_KERNEL);
1158 if (gnttab_list[i] == NULL) {
1164 if (gnttab_resume() < 0) {
1169 nr_init_grefs = nr_grant_frames * GREFS_PER_GRANT_FRAME;
1171 for (i = NR_RESERVED_ENTRIES; i < nr_init_grefs - 1; i++)
1172 gnttab_entry(i) = i + 1;
1174 gnttab_entry(nr_init_grefs - 1) = GNTTAB_LIST_END;
1175 gnttab_free_count = nr_init_grefs - NR_RESERVED_ENTRIES;
1176 gnttab_free_head = NR_RESERVED_ENTRIES;
1178 printk("Grant table initialized\n");
1182 for (i--; i >= 0; i--)
1183 free_page((unsigned long)gnttab_list[i]);
1187 EXPORT_SYMBOL_GPL(gnttab_init);
1189 static int __devinit __gnttab_init(void)
1191 /* Delay grant-table initialization in the PV on HVM case */
1192 if (xen_hvm_domain())
1195 if (!xen_pv_domain())
1198 return gnttab_init();
1201 core_initcall(__gnttab_init);