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 #define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt
36 #include <linux/module.h>
37 #include <linux/sched.h>
39 #include <linux/slab.h>
40 #include <linux/vmalloc.h>
41 #include <linux/uaccess.h>
43 #include <linux/delay.h>
44 #include <linux/hardirq.h>
47 #include <xen/interface/xen.h>
49 #include <xen/grant_table.h>
50 #include <xen/interface/memory.h>
51 #include <xen/hvc-console.h>
52 #include <xen/swiotlb-xen.h>
53 #include <asm/xen/hypercall.h>
54 #include <asm/xen/interface.h>
56 #include <asm/pgtable.h>
57 #include <asm/sync_bitops.h>
59 /* External tools reserve first few grant table entries. */
60 #define NR_RESERVED_ENTRIES 8
61 #define GNTTAB_LIST_END 0xffffffff
63 static grant_ref_t
**gnttab_list
;
64 static unsigned int 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 struct grant_frames xen_auto_xlat_grant_frames
;
71 struct grant_entry_v1
*v1
;
75 /*This is a structure of function pointers for grant table*/
78 * Mapping a list of frames for storing grant entries. Frames parameter
79 * is used to store grant table address when grant table being setup,
80 * nr_gframes is the number of frames to map grant table. Returning
81 * GNTST_okay means success and negative value means failure.
83 int (*map_frames
)(xen_pfn_t
*frames
, unsigned int nr_gframes
);
85 * Release a list of frames which are mapped in map_frames for grant
88 void (*unmap_frames
)(void);
90 * Introducing a valid entry into the grant table, granting the frame of
91 * this grant entry to domain for accessing or transfering. Ref
92 * parameter is reference of this introduced grant entry, domid is id of
93 * granted domain, frame is the page frame to be granted, and flags is
94 * status of the grant entry to be updated.
96 void (*update_entry
)(grant_ref_t ref
, domid_t domid
,
97 unsigned long frame
, unsigned flags
);
99 * Stop granting a grant entry to domain for accessing. Ref parameter is
100 * reference of a grant entry whose grant access will be stopped,
101 * readonly is not in use in this function. If the grant entry is
102 * currently mapped for reading or writing, just return failure(==0)
103 * directly and don't tear down the grant access. Otherwise, stop grant
104 * access for this entry and return success(==1).
106 int (*end_foreign_access_ref
)(grant_ref_t ref
, int readonly
);
108 * Stop granting a grant entry to domain for transfer. Ref parameter is
109 * reference of a grant entry whose grant transfer will be stopped. If
110 * tranfer has not started, just reclaim the grant entry and return
111 * failure(==0). Otherwise, wait for the transfer to complete and then
114 unsigned long (*end_foreign_transfer_ref
)(grant_ref_t ref
);
116 * Query the status of a grant entry. Ref parameter is reference of
117 * queried grant entry, return value is the status of queried entry.
118 * Detailed status(writing/reading) can be gotten from the return value
121 int (*query_foreign_access
)(grant_ref_t ref
);
124 static struct gnttab_ops
*gnttab_interface
;
126 static int grant_table_version
;
127 static int grefs_per_grant_frame
;
129 static struct gnttab_free_callback
*gnttab_free_callback_list
;
131 static int gnttab_expand(unsigned int req_entries
);
133 #define RPP (PAGE_SIZE / sizeof(grant_ref_t))
134 #define SPP (PAGE_SIZE / sizeof(grant_status_t))
136 static inline grant_ref_t
*__gnttab_entry(grant_ref_t entry
)
138 return &gnttab_list
[(entry
) / RPP
][(entry
) % RPP
];
140 /* This can be used as an l-value */
141 #define gnttab_entry(entry) (*__gnttab_entry(entry))
143 static int get_free_entries(unsigned count
)
149 spin_lock_irqsave(&gnttab_list_lock
, flags
);
151 if ((gnttab_free_count
< count
) &&
152 ((rc
= gnttab_expand(count
- gnttab_free_count
)) < 0)) {
153 spin_unlock_irqrestore(&gnttab_list_lock
, flags
);
157 ref
= head
= gnttab_free_head
;
158 gnttab_free_count
-= count
;
160 head
= gnttab_entry(head
);
161 gnttab_free_head
= gnttab_entry(head
);
162 gnttab_entry(head
) = GNTTAB_LIST_END
;
164 spin_unlock_irqrestore(&gnttab_list_lock
, flags
);
169 static void do_free_callbacks(void)
171 struct gnttab_free_callback
*callback
, *next
;
173 callback
= gnttab_free_callback_list
;
174 gnttab_free_callback_list
= NULL
;
176 while (callback
!= NULL
) {
177 next
= callback
->next
;
178 if (gnttab_free_count
>= callback
->count
) {
179 callback
->next
= NULL
;
180 callback
->fn(callback
->arg
);
182 callback
->next
= gnttab_free_callback_list
;
183 gnttab_free_callback_list
= callback
;
189 static inline void check_free_callbacks(void)
191 if (unlikely(gnttab_free_callback_list
))
195 static void put_free_entry(grant_ref_t ref
)
198 spin_lock_irqsave(&gnttab_list_lock
, flags
);
199 gnttab_entry(ref
) = gnttab_free_head
;
200 gnttab_free_head
= ref
;
202 check_free_callbacks();
203 spin_unlock_irqrestore(&gnttab_list_lock
, flags
);
207 * Following applies to gnttab_update_entry_v1.
208 * Introducing a valid entry into the grant table:
209 * 1. Write ent->domid.
210 * 2. Write ent->frame:
211 * GTF_permit_access: Frame to which access is permitted.
212 * GTF_accept_transfer: Pseudo-phys frame slot being filled by new
213 * frame, or zero if none.
214 * 3. Write memory barrier (WMB).
215 * 4. Write ent->flags, inc. valid type.
217 static void gnttab_update_entry_v1(grant_ref_t ref
, domid_t domid
,
218 unsigned long frame
, unsigned flags
)
220 gnttab_shared
.v1
[ref
].domid
= domid
;
221 gnttab_shared
.v1
[ref
].frame
= frame
;
223 gnttab_shared
.v1
[ref
].flags
= flags
;
227 * Public grant-issuing interface functions
229 void gnttab_grant_foreign_access_ref(grant_ref_t ref
, domid_t domid
,
230 unsigned long frame
, int readonly
)
232 gnttab_interface
->update_entry(ref
, domid
, frame
,
233 GTF_permit_access
| (readonly
? GTF_readonly
: 0));
235 EXPORT_SYMBOL_GPL(gnttab_grant_foreign_access_ref
);
237 int gnttab_grant_foreign_access(domid_t domid
, unsigned long frame
,
242 ref
= get_free_entries(1);
243 if (unlikely(ref
< 0))
246 gnttab_grant_foreign_access_ref(ref
, domid
, frame
, readonly
);
250 EXPORT_SYMBOL_GPL(gnttab_grant_foreign_access
);
252 static int gnttab_query_foreign_access_v1(grant_ref_t ref
)
254 return gnttab_shared
.v1
[ref
].flags
& (GTF_reading
|GTF_writing
);
257 int gnttab_query_foreign_access(grant_ref_t ref
)
259 return gnttab_interface
->query_foreign_access(ref
);
261 EXPORT_SYMBOL_GPL(gnttab_query_foreign_access
);
263 static int gnttab_end_foreign_access_ref_v1(grant_ref_t ref
, int readonly
)
268 pflags
= &gnttab_shared
.v1
[ref
].flags
;
272 if (flags
& (GTF_reading
|GTF_writing
))
274 } while ((nflags
= sync_cmpxchg(pflags
, flags
, 0)) != flags
);
279 static inline int _gnttab_end_foreign_access_ref(grant_ref_t ref
, int readonly
)
281 return gnttab_interface
->end_foreign_access_ref(ref
, readonly
);
284 int gnttab_end_foreign_access_ref(grant_ref_t ref
, int readonly
)
286 if (_gnttab_end_foreign_access_ref(ref
, readonly
))
288 pr_warn("WARNING: g.e. %#x still in use!\n", ref
);
291 EXPORT_SYMBOL_GPL(gnttab_end_foreign_access_ref
);
293 struct deferred_entry
{
294 struct list_head list
;
300 static LIST_HEAD(deferred_list
);
301 static void gnttab_handle_deferred(unsigned long);
302 static DEFINE_TIMER(deferred_timer
, gnttab_handle_deferred
, 0, 0);
304 static void gnttab_handle_deferred(unsigned long unused
)
306 unsigned int nr
= 10;
307 struct deferred_entry
*first
= NULL
;
310 spin_lock_irqsave(&gnttab_list_lock
, flags
);
312 struct deferred_entry
*entry
313 = list_first_entry(&deferred_list
,
314 struct deferred_entry
, list
);
318 list_del(&entry
->list
);
319 spin_unlock_irqrestore(&gnttab_list_lock
, flags
);
320 if (_gnttab_end_foreign_access_ref(entry
->ref
, entry
->ro
)) {
321 put_free_entry(entry
->ref
);
323 pr_debug("freeing g.e. %#x (pfn %#lx)\n",
324 entry
->ref
, page_to_pfn(entry
->page
));
325 __free_page(entry
->page
);
327 pr_info("freeing g.e. %#x\n", entry
->ref
);
331 if (!--entry
->warn_delay
)
332 pr_info("g.e. %#x still pending\n", entry
->ref
);
336 spin_lock_irqsave(&gnttab_list_lock
, flags
);
338 list_add_tail(&entry
->list
, &deferred_list
);
339 else if (list_empty(&deferred_list
))
342 if (!list_empty(&deferred_list
) && !timer_pending(&deferred_timer
)) {
343 deferred_timer
.expires
= jiffies
+ HZ
;
344 add_timer(&deferred_timer
);
346 spin_unlock_irqrestore(&gnttab_list_lock
, flags
);
349 static void gnttab_add_deferred(grant_ref_t ref
, bool readonly
,
352 struct deferred_entry
*entry
= kmalloc(sizeof(*entry
), GFP_ATOMIC
);
353 const char *what
= KERN_WARNING
"leaking";
359 entry
->ro
= readonly
;
361 entry
->warn_delay
= 60;
362 spin_lock_irqsave(&gnttab_list_lock
, flags
);
363 list_add_tail(&entry
->list
, &deferred_list
);
364 if (!timer_pending(&deferred_timer
)) {
365 deferred_timer
.expires
= jiffies
+ HZ
;
366 add_timer(&deferred_timer
);
368 spin_unlock_irqrestore(&gnttab_list_lock
, flags
);
369 what
= KERN_DEBUG
"deferring";
371 printk("%s g.e. %#x (pfn %#lx)\n",
372 what
, ref
, page
? page_to_pfn(page
) : -1);
375 void gnttab_end_foreign_access(grant_ref_t ref
, int readonly
,
378 if (gnttab_end_foreign_access_ref(ref
, readonly
)) {
383 gnttab_add_deferred(ref
, readonly
,
384 page
? virt_to_page(page
) : NULL
);
386 EXPORT_SYMBOL_GPL(gnttab_end_foreign_access
);
388 int gnttab_grant_foreign_transfer(domid_t domid
, unsigned long pfn
)
392 ref
= get_free_entries(1);
393 if (unlikely(ref
< 0))
395 gnttab_grant_foreign_transfer_ref(ref
, domid
, pfn
);
399 EXPORT_SYMBOL_GPL(gnttab_grant_foreign_transfer
);
401 void gnttab_grant_foreign_transfer_ref(grant_ref_t ref
, domid_t domid
,
404 gnttab_interface
->update_entry(ref
, domid
, pfn
, GTF_accept_transfer
);
406 EXPORT_SYMBOL_GPL(gnttab_grant_foreign_transfer_ref
);
408 static unsigned long gnttab_end_foreign_transfer_ref_v1(grant_ref_t ref
)
414 pflags
= &gnttab_shared
.v1
[ref
].flags
;
417 * If a transfer is not even yet started, try to reclaim the grant
418 * reference and return failure (== 0).
420 while (!((flags
= *pflags
) & GTF_transfer_committed
)) {
421 if (sync_cmpxchg(pflags
, flags
, 0) == flags
)
426 /* If a transfer is in progress then wait until it is completed. */
427 while (!(flags
& GTF_transfer_completed
)) {
432 rmb(); /* Read the frame number /after/ reading completion status. */
433 frame
= gnttab_shared
.v1
[ref
].frame
;
439 unsigned long gnttab_end_foreign_transfer_ref(grant_ref_t ref
)
441 return gnttab_interface
->end_foreign_transfer_ref(ref
);
443 EXPORT_SYMBOL_GPL(gnttab_end_foreign_transfer_ref
);
445 unsigned long gnttab_end_foreign_transfer(grant_ref_t ref
)
447 unsigned long frame
= gnttab_end_foreign_transfer_ref(ref
);
451 EXPORT_SYMBOL_GPL(gnttab_end_foreign_transfer
);
453 void gnttab_free_grant_reference(grant_ref_t ref
)
457 EXPORT_SYMBOL_GPL(gnttab_free_grant_reference
);
459 void gnttab_free_grant_references(grant_ref_t head
)
464 if (head
== GNTTAB_LIST_END
)
466 spin_lock_irqsave(&gnttab_list_lock
, flags
);
468 while (gnttab_entry(ref
) != GNTTAB_LIST_END
) {
469 ref
= gnttab_entry(ref
);
472 gnttab_entry(ref
) = gnttab_free_head
;
473 gnttab_free_head
= head
;
474 gnttab_free_count
+= count
;
475 check_free_callbacks();
476 spin_unlock_irqrestore(&gnttab_list_lock
, flags
);
478 EXPORT_SYMBOL_GPL(gnttab_free_grant_references
);
480 int gnttab_alloc_grant_references(u16 count
, grant_ref_t
*head
)
482 int h
= get_free_entries(count
);
491 EXPORT_SYMBOL_GPL(gnttab_alloc_grant_references
);
493 int gnttab_empty_grant_references(const grant_ref_t
*private_head
)
495 return (*private_head
== GNTTAB_LIST_END
);
497 EXPORT_SYMBOL_GPL(gnttab_empty_grant_references
);
499 int gnttab_claim_grant_reference(grant_ref_t
*private_head
)
501 grant_ref_t g
= *private_head
;
502 if (unlikely(g
== GNTTAB_LIST_END
))
504 *private_head
= gnttab_entry(g
);
507 EXPORT_SYMBOL_GPL(gnttab_claim_grant_reference
);
509 void gnttab_release_grant_reference(grant_ref_t
*private_head
,
512 gnttab_entry(release
) = *private_head
;
513 *private_head
= release
;
515 EXPORT_SYMBOL_GPL(gnttab_release_grant_reference
);
517 void gnttab_request_free_callback(struct gnttab_free_callback
*callback
,
518 void (*fn
)(void *), void *arg
, u16 count
)
521 struct gnttab_free_callback
*cb
;
523 spin_lock_irqsave(&gnttab_list_lock
, flags
);
525 /* Check if the callback is already on the list */
526 cb
= gnttab_free_callback_list
;
535 callback
->count
= count
;
536 callback
->next
= gnttab_free_callback_list
;
537 gnttab_free_callback_list
= callback
;
538 check_free_callbacks();
540 spin_unlock_irqrestore(&gnttab_list_lock
, flags
);
542 EXPORT_SYMBOL_GPL(gnttab_request_free_callback
);
544 void gnttab_cancel_free_callback(struct gnttab_free_callback
*callback
)
546 struct gnttab_free_callback
**pcb
;
549 spin_lock_irqsave(&gnttab_list_lock
, flags
);
550 for (pcb
= &gnttab_free_callback_list
; *pcb
; pcb
= &(*pcb
)->next
) {
551 if (*pcb
== callback
) {
552 *pcb
= callback
->next
;
556 spin_unlock_irqrestore(&gnttab_list_lock
, flags
);
558 EXPORT_SYMBOL_GPL(gnttab_cancel_free_callback
);
560 static int grow_gnttab_list(unsigned int more_frames
)
562 unsigned int new_nr_grant_frames
, extra_entries
, i
;
563 unsigned int nr_glist_frames
, new_nr_glist_frames
;
565 BUG_ON(grefs_per_grant_frame
== 0);
567 new_nr_grant_frames
= nr_grant_frames
+ more_frames
;
568 extra_entries
= more_frames
* grefs_per_grant_frame
;
570 nr_glist_frames
= (nr_grant_frames
* grefs_per_grant_frame
+ RPP
- 1) / RPP
;
571 new_nr_glist_frames
=
572 (new_nr_grant_frames
* grefs_per_grant_frame
+ RPP
- 1) / RPP
;
573 for (i
= nr_glist_frames
; i
< new_nr_glist_frames
; i
++) {
574 gnttab_list
[i
] = (grant_ref_t
*)__get_free_page(GFP_ATOMIC
);
580 for (i
= grefs_per_grant_frame
* nr_grant_frames
;
581 i
< grefs_per_grant_frame
* new_nr_grant_frames
- 1; i
++)
582 gnttab_entry(i
) = i
+ 1;
584 gnttab_entry(i
) = gnttab_free_head
;
585 gnttab_free_head
= grefs_per_grant_frame
* nr_grant_frames
;
586 gnttab_free_count
+= extra_entries
;
588 nr_grant_frames
= new_nr_grant_frames
;
590 check_free_callbacks();
595 while (i
-- > nr_glist_frames
)
596 free_page((unsigned long) gnttab_list
[i
]);
600 static unsigned int __max_nr_grant_frames(void)
602 struct gnttab_query_size query
;
605 query
.dom
= DOMID_SELF
;
607 rc
= HYPERVISOR_grant_table_op(GNTTABOP_query_size
, &query
, 1);
608 if ((rc
< 0) || (query
.status
!= GNTST_okay
))
609 return 4; /* Legacy max supported number of frames */
611 return query
.max_nr_frames
;
614 unsigned int gnttab_max_grant_frames(void)
616 unsigned int xen_max
= __max_nr_grant_frames();
617 static unsigned int boot_max_nr_grant_frames
;
619 /* First time, initialize it properly. */
620 if (!boot_max_nr_grant_frames
)
621 boot_max_nr_grant_frames
= __max_nr_grant_frames();
623 if (xen_max
> boot_max_nr_grant_frames
)
624 return boot_max_nr_grant_frames
;
627 EXPORT_SYMBOL_GPL(gnttab_max_grant_frames
);
629 int gnttab_setup_auto_xlat_frames(phys_addr_t addr
)
632 unsigned int max_nr_gframes
= __max_nr_grant_frames();
636 if (xen_auto_xlat_grant_frames
.count
)
639 vaddr
= xen_remap(addr
, PAGE_SIZE
* max_nr_gframes
);
641 pr_warn("Failed to ioremap gnttab share frames (addr=%pa)!\n",
645 pfn
= kcalloc(max_nr_gframes
, sizeof(pfn
[0]), GFP_KERNEL
);
650 for (i
= 0; i
< max_nr_gframes
; i
++)
651 pfn
[i
] = PFN_DOWN(addr
) + i
;
653 xen_auto_xlat_grant_frames
.vaddr
= vaddr
;
654 xen_auto_xlat_grant_frames
.pfn
= pfn
;
655 xen_auto_xlat_grant_frames
.count
= max_nr_gframes
;
659 EXPORT_SYMBOL_GPL(gnttab_setup_auto_xlat_frames
);
661 void gnttab_free_auto_xlat_frames(void)
663 if (!xen_auto_xlat_grant_frames
.count
)
665 kfree(xen_auto_xlat_grant_frames
.pfn
);
666 xen_unmap(xen_auto_xlat_grant_frames
.vaddr
);
668 xen_auto_xlat_grant_frames
.pfn
= NULL
;
669 xen_auto_xlat_grant_frames
.count
= 0;
670 xen_auto_xlat_grant_frames
.vaddr
= NULL
;
672 EXPORT_SYMBOL_GPL(gnttab_free_auto_xlat_frames
);
674 /* Handling of paged out grant targets (GNTST_eagain) */
675 #define MAX_DELAY 256
677 gnttab_retry_eagain_gop(unsigned int cmd
, void *gop
, int16_t *status
,
683 BUG_ON(HYPERVISOR_grant_table_op(cmd
, gop
, 1));
684 if (*status
== GNTST_eagain
)
686 } while ((*status
== GNTST_eagain
) && (delay
< MAX_DELAY
));
688 if (delay
>= MAX_DELAY
) {
689 pr_err("%s: %s eagain grant\n", func
, current
->comm
);
690 *status
= GNTST_bad_page
;
694 void gnttab_batch_map(struct gnttab_map_grant_ref
*batch
, unsigned count
)
696 struct gnttab_map_grant_ref
*op
;
698 if (HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref
, batch
, count
))
700 for (op
= batch
; op
< batch
+ count
; op
++)
701 if (op
->status
== GNTST_eagain
)
702 gnttab_retry_eagain_gop(GNTTABOP_map_grant_ref
, op
,
703 &op
->status
, __func__
);
705 EXPORT_SYMBOL_GPL(gnttab_batch_map
);
707 void gnttab_batch_copy(struct gnttab_copy
*batch
, unsigned count
)
709 struct gnttab_copy
*op
;
711 if (HYPERVISOR_grant_table_op(GNTTABOP_copy
, batch
, count
))
713 for (op
= batch
; op
< batch
+ count
; op
++)
714 if (op
->status
== GNTST_eagain
)
715 gnttab_retry_eagain_gop(GNTTABOP_copy
, op
,
716 &op
->status
, __func__
);
718 EXPORT_SYMBOL_GPL(gnttab_batch_copy
);
720 int gnttab_map_refs(struct gnttab_map_grant_ref
*map_ops
,
721 struct gnttab_map_grant_ref
*kmap_ops
,
722 struct page
**pages
, unsigned int count
)
726 ret
= HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref
, map_ops
, count
);
730 /* Retry eagain maps */
731 for (i
= 0; i
< count
; i
++)
732 if (map_ops
[i
].status
== GNTST_eagain
)
733 gnttab_retry_eagain_gop(GNTTABOP_map_grant_ref
, map_ops
+ i
,
734 &map_ops
[i
].status
, __func__
);
736 return set_foreign_p2m_mapping(map_ops
, kmap_ops
, pages
, count
);
738 EXPORT_SYMBOL_GPL(gnttab_map_refs
);
740 int gnttab_unmap_refs(struct gnttab_unmap_grant_ref
*unmap_ops
,
741 struct gnttab_map_grant_ref
*kmap_ops
,
742 struct page
**pages
, unsigned int count
)
746 ret
= HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref
, unmap_ops
, count
);
750 return clear_foreign_p2m_mapping(unmap_ops
, kmap_ops
, pages
, count
);
752 EXPORT_SYMBOL_GPL(gnttab_unmap_refs
);
754 static int gnttab_map_frames_v1(xen_pfn_t
*frames
, unsigned int nr_gframes
)
758 rc
= arch_gnttab_map_shared(frames
, nr_gframes
,
759 gnttab_max_grant_frames(),
760 &gnttab_shared
.addr
);
766 static void gnttab_unmap_frames_v1(void)
768 arch_gnttab_unmap(gnttab_shared
.addr
, nr_grant_frames
);
771 static int gnttab_map(unsigned int start_idx
, unsigned int end_idx
)
773 struct gnttab_setup_table setup
;
775 unsigned int nr_gframes
= end_idx
+ 1;
778 if (xen_feature(XENFEAT_auto_translated_physmap
)) {
779 struct xen_add_to_physmap xatp
;
780 unsigned int i
= end_idx
;
782 BUG_ON(xen_auto_xlat_grant_frames
.count
< nr_gframes
);
784 * Loop backwards, so that the first hypercall has the largest
785 * index, ensuring that the table will grow only once.
788 xatp
.domid
= DOMID_SELF
;
790 xatp
.space
= XENMAPSPACE_grant_table
;
791 xatp
.gpfn
= xen_auto_xlat_grant_frames
.pfn
[i
];
792 rc
= HYPERVISOR_memory_op(XENMEM_add_to_physmap
, &xatp
);
794 pr_warn("grant table add_to_physmap failed, err=%d\n",
798 } while (i
-- > start_idx
);
803 /* No need for kzalloc as it is initialized in following hypercall
804 * GNTTABOP_setup_table.
806 frames
= kmalloc(nr_gframes
* sizeof(unsigned long), GFP_ATOMIC
);
810 setup
.dom
= DOMID_SELF
;
811 setup
.nr_frames
= nr_gframes
;
812 set_xen_guest_handle(setup
.frame_list
, frames
);
814 rc
= HYPERVISOR_grant_table_op(GNTTABOP_setup_table
, &setup
, 1);
820 BUG_ON(rc
|| setup
.status
);
822 rc
= gnttab_interface
->map_frames(frames
, nr_gframes
);
829 static struct gnttab_ops gnttab_v1_ops
= {
830 .map_frames
= gnttab_map_frames_v1
,
831 .unmap_frames
= gnttab_unmap_frames_v1
,
832 .update_entry
= gnttab_update_entry_v1
,
833 .end_foreign_access_ref
= gnttab_end_foreign_access_ref_v1
,
834 .end_foreign_transfer_ref
= gnttab_end_foreign_transfer_ref_v1
,
835 .query_foreign_access
= gnttab_query_foreign_access_v1
,
838 static void gnttab_request_version(void)
840 /* Only version 1 is used, which will always be available. */
841 grant_table_version
= 1;
842 grefs_per_grant_frame
= PAGE_SIZE
/ sizeof(struct grant_entry_v1
);
843 gnttab_interface
= &gnttab_v1_ops
;
845 pr_info("Grant tables using version %d layout\n", grant_table_version
);
848 static int gnttab_setup(void)
850 unsigned int max_nr_gframes
;
852 max_nr_gframes
= gnttab_max_grant_frames();
853 if (max_nr_gframes
< nr_grant_frames
)
856 if (xen_feature(XENFEAT_auto_translated_physmap
) && gnttab_shared
.addr
== NULL
) {
857 gnttab_shared
.addr
= xen_auto_xlat_grant_frames
.vaddr
;
858 if (gnttab_shared
.addr
== NULL
) {
859 pr_warn("gnttab share frames (addr=0x%08lx) is not mapped!\n",
860 (unsigned long)xen_auto_xlat_grant_frames
.vaddr
);
864 return gnttab_map(0, nr_grant_frames
- 1);
867 int gnttab_resume(void)
869 gnttab_request_version();
870 return gnttab_setup();
873 int gnttab_suspend(void)
875 if (!xen_feature(XENFEAT_auto_translated_physmap
))
876 gnttab_interface
->unmap_frames();
880 static int gnttab_expand(unsigned int req_entries
)
883 unsigned int cur
, extra
;
885 BUG_ON(grefs_per_grant_frame
== 0);
886 cur
= nr_grant_frames
;
887 extra
= ((req_entries
+ (grefs_per_grant_frame
-1)) /
888 grefs_per_grant_frame
);
889 if (cur
+ extra
> gnttab_max_grant_frames())
892 rc
= gnttab_map(cur
, cur
+ extra
- 1);
894 rc
= grow_gnttab_list(extra
);
899 int gnttab_init(void)
902 unsigned long max_nr_grant_frames
;
903 unsigned int max_nr_glist_frames
, nr_glist_frames
;
904 unsigned int nr_init_grefs
;
907 gnttab_request_version();
908 max_nr_grant_frames
= gnttab_max_grant_frames();
911 /* Determine the maximum number of frames required for the
912 * grant reference free list on the current hypervisor.
914 BUG_ON(grefs_per_grant_frame
== 0);
915 max_nr_glist_frames
= (max_nr_grant_frames
*
916 grefs_per_grant_frame
/ RPP
);
918 gnttab_list
= kmalloc(max_nr_glist_frames
* sizeof(grant_ref_t
*),
920 if (gnttab_list
== NULL
)
923 nr_glist_frames
= (nr_grant_frames
* grefs_per_grant_frame
+ RPP
- 1) / RPP
;
924 for (i
= 0; i
< nr_glist_frames
; i
++) {
925 gnttab_list
[i
] = (grant_ref_t
*)__get_free_page(GFP_KERNEL
);
926 if (gnttab_list
[i
] == NULL
) {
932 ret
= arch_gnttab_init(max_nr_grant_frames
);
936 if (gnttab_setup() < 0) {
941 nr_init_grefs
= nr_grant_frames
* grefs_per_grant_frame
;
943 for (i
= NR_RESERVED_ENTRIES
; i
< nr_init_grefs
- 1; i
++)
944 gnttab_entry(i
) = i
+ 1;
946 gnttab_entry(nr_init_grefs
- 1) = GNTTAB_LIST_END
;
947 gnttab_free_count
= nr_init_grefs
- NR_RESERVED_ENTRIES
;
948 gnttab_free_head
= NR_RESERVED_ENTRIES
;
950 printk("Grant table initialized\n");
954 for (i
--; i
>= 0; i
--)
955 free_page((unsigned long)gnttab_list
[i
]);
959 EXPORT_SYMBOL_GPL(gnttab_init
);
961 static int __gnttab_init(void)
963 /* Delay grant-table initialization in the PV on HVM case */
964 if (xen_hvm_domain())
967 if (!xen_pv_domain())
970 return gnttab_init();
972 /* Starts after core_initcall so that xen_pvh_gnttab_setup can be called
973 * beforehand to initialize xen_auto_xlat_grant_frames. */
974 core_initcall_sync(__gnttab_init
);