1 /******************************************************************************
4 * Two sets of functionality:
5 * 1. Granting foreign access to our memory reservation.
6 * 2. Accessing others' memory reservations via grant references.
7 * (i.e., mechanisms for both sender and recipient of grant references)
9 * Copyright (c) 2005, Christopher Clark
10 * Copyright (c) 2004, K A Fraser
13 #include <sys/cdefs.h>
14 __FBSDID("$FreeBSD$");
16 #include "opt_global.h"
19 #include <sys/param.h>
20 #include <sys/systm.h>
23 #include <sys/module.h>
24 #include <sys/kernel.h>
26 #include <sys/malloc.h>
29 #include <machine/xen/hypervisor.h>
30 #include <machine/xen/synch_bitops.h>
31 #include <xen/gnttab.h>
33 #define cmpxchg(a, b, c) atomic_cmpset_int((volatile u_int *)(a),(b),(c))
37 if ( !(_p) ) { printk("Assertion '%s': line %d, file %s\n", \
38 #_p , __LINE__, __FILE__); *(int*)0=0; }
40 #define ASSERT(_p) ((void)0)
43 #define WPRINTK(fmt, args...) \
44 printk("xen_grant: " fmt, ##args)
46 /* External tools reserve first few grant table entries. */
47 #define NR_RESERVED_ENTRIES 8
48 #define GNTTAB_LIST_END 0xffffffff
49 #define GREFS_PER_GRANT_FRAME (PAGE_SIZE / sizeof(grant_entry_t))
51 static grant_ref_t **gnttab_list;
52 static unsigned int nr_grant_frames;
53 static unsigned int boot_max_nr_grant_frames;
54 static int gnttab_free_count;
55 static grant_ref_t gnttab_free_head;
56 static struct mtx gnttab_list_lock;
58 static grant_entry_t *shared;
60 static struct gnttab_free_callback *gnttab_free_callback_list = NULL;
62 static int gnttab_expand(unsigned int req_entries);
64 #define RPP (PAGE_SIZE / sizeof(grant_ref_t))
65 #define gnttab_entry(entry) (gnttab_list[(entry) / RPP][(entry) % RPP])
68 get_free_entries(int count)
73 mtx_lock(&gnttab_list_lock);
74 if ((gnttab_free_count < count) &&
75 ((rc = gnttab_expand(count - gnttab_free_count)) < 0)) {
76 mtx_unlock(&gnttab_list_lock);
79 ref = head = gnttab_free_head;
80 gnttab_free_count -= count;
82 head = gnttab_entry(head);
83 gnttab_free_head = gnttab_entry(head);
84 gnttab_entry(head) = GNTTAB_LIST_END;
85 mtx_unlock(&gnttab_list_lock);
89 #define get_free_entry() get_free_entries(1)
92 do_free_callbacks(void)
94 struct gnttab_free_callback *callback, *next;
96 callback = gnttab_free_callback_list;
97 gnttab_free_callback_list = NULL;
99 while (callback != NULL) {
100 next = callback->next;
101 if (gnttab_free_count >= callback->count) {
102 callback->next = NULL;
103 callback->fn(callback->arg);
105 callback->next = gnttab_free_callback_list;
106 gnttab_free_callback_list = callback;
113 check_free_callbacks(void)
115 if (unlikely(gnttab_free_callback_list != NULL))
120 put_free_entry(grant_ref_t ref)
123 mtx_lock(&gnttab_list_lock);
124 gnttab_entry(ref) = gnttab_free_head;
125 gnttab_free_head = ref;
127 check_free_callbacks();
128 mtx_unlock(&gnttab_list_lock);
132 * Public grant-issuing interface functions
136 gnttab_grant_foreign_access(domid_t domid, unsigned long frame, int readonly)
140 if (unlikely((ref = get_free_entry()) == -1))
143 shared[ref].frame = frame;
144 shared[ref].domid = domid;
146 shared[ref].flags = GTF_permit_access | (readonly ? GTF_readonly : 0);
152 gnttab_grant_foreign_access_ref(grant_ref_t ref, domid_t domid,
153 unsigned long frame, int readonly)
155 shared[ref].frame = frame;
156 shared[ref].domid = domid;
158 shared[ref].flags = GTF_permit_access | (readonly ? GTF_readonly : 0);
162 gnttab_query_foreign_access(grant_ref_t ref)
166 nflags = shared[ref].flags;
168 return (nflags & (GTF_reading|GTF_writing));
172 gnttab_end_foreign_access_ref(grant_ref_t ref)
174 uint16_t flags, nflags;
176 nflags = shared[ref].flags;
178 if ( (flags = nflags) & (GTF_reading|GTF_writing) ) {
179 printf("WARNING: g.e. still in use!\n");
182 } while ((nflags = synch_cmpxchg(&shared[ref].flags, flags, 0)) !=
189 gnttab_end_foreign_access(grant_ref_t ref, void *page)
191 if (gnttab_end_foreign_access_ref(ref)) {
194 free(page, M_DEVBUF);
198 /* XXX This needs to be fixed so that the ref and page are
199 placed on a list to be freed up later. */
200 printf("WARNING: leaking g.e. and page still in use!\n");
205 gnttab_grant_foreign_transfer(domid_t domid, unsigned long pfn)
209 if (unlikely((ref = get_free_entry()) == -1))
212 gnttab_grant_foreign_transfer_ref(ref, domid, pfn);
218 gnttab_grant_foreign_transfer_ref(grant_ref_t ref, domid_t domid,
221 shared[ref].frame = pfn;
222 shared[ref].domid = domid;
224 shared[ref].flags = GTF_accept_transfer;
228 gnttab_end_foreign_transfer_ref(grant_ref_t ref)
234 * If a transfer is not even yet started, try to reclaim the grant
235 * reference and return failure (== 0).
237 while (!((flags = shared[ref].flags) & GTF_transfer_committed)) {
238 if ( synch_cmpxchg(&shared[ref].flags, flags, 0) == flags )
243 /* If a transfer is in progress then wait until it is completed. */
244 while (!(flags & GTF_transfer_completed)) {
245 flags = shared[ref].flags;
249 /* Read the frame number /after/ reading completion status. */
251 frame = shared[ref].frame;
252 PANIC_IF(frame == 0);
258 gnttab_end_foreign_transfer(grant_ref_t ref)
260 unsigned long frame = gnttab_end_foreign_transfer_ref(ref);
267 gnttab_free_grant_reference(grant_ref_t ref)
274 gnttab_free_grant_references(grant_ref_t head)
279 if (head == GNTTAB_LIST_END)
282 mtx_lock(&gnttab_list_lock);
284 while (gnttab_entry(ref) != GNTTAB_LIST_END) {
285 ref = gnttab_entry(ref);
288 gnttab_entry(ref) = gnttab_free_head;
289 gnttab_free_head = head;
290 gnttab_free_count += count;
291 check_free_callbacks();
292 mtx_unlock(&gnttab_list_lock);
296 gnttab_alloc_grant_references(uint16_t count, grant_ref_t *head)
298 int h = get_free_entries(count);
309 gnttab_empty_grant_references(const grant_ref_t *private_head)
311 return (*private_head == GNTTAB_LIST_END);
315 gnttab_claim_grant_reference(grant_ref_t *private_head)
317 grant_ref_t g = *private_head;
319 if (unlikely(g == GNTTAB_LIST_END))
321 *private_head = gnttab_entry(g);
327 gnttab_release_grant_reference(grant_ref_t *private_head, grant_ref_t release)
329 gnttab_entry(release) = *private_head;
330 *private_head = release;
334 gnttab_request_free_callback(struct gnttab_free_callback *callback,
335 void (*fn)(void *), void *arg, uint16_t count)
338 mtx_lock(&gnttab_list_lock);
343 callback->count = count;
344 callback->next = gnttab_free_callback_list;
345 gnttab_free_callback_list = callback;
346 check_free_callbacks();
348 mtx_unlock(&gnttab_list_lock);
353 gnttab_cancel_free_callback(struct gnttab_free_callback *callback)
355 struct gnttab_free_callback **pcb;
357 mtx_lock(&gnttab_list_lock);
358 for (pcb = &gnttab_free_callback_list; *pcb; pcb = &(*pcb)->next) {
359 if (*pcb == callback) {
360 *pcb = callback->next;
364 mtx_unlock(&gnttab_list_lock);
369 grow_gnttab_list(unsigned int more_frames)
371 unsigned int new_nr_grant_frames, extra_entries, i;
373 new_nr_grant_frames = nr_grant_frames + more_frames;
374 extra_entries = more_frames * GREFS_PER_GRANT_FRAME;
376 for (i = nr_grant_frames; i < new_nr_grant_frames; i++)
378 gnttab_list[i] = (grant_ref_t *)malloc(PAGE_SIZE, M_DEVBUF, M_NOWAIT);
384 for (i = GREFS_PER_GRANT_FRAME * nr_grant_frames;
385 i < GREFS_PER_GRANT_FRAME * new_nr_grant_frames - 1; i++)
386 gnttab_entry(i) = i + 1;
388 gnttab_entry(i) = gnttab_free_head;
389 gnttab_free_head = GREFS_PER_GRANT_FRAME * nr_grant_frames;
390 gnttab_free_count += extra_entries;
392 nr_grant_frames = new_nr_grant_frames;
394 check_free_callbacks();
399 for ( ; i >= nr_grant_frames; i--)
400 free(gnttab_list[i], M_DEVBUF);
405 __max_nr_grant_frames(void)
407 struct gnttab_query_size query;
410 query.dom = DOMID_SELF;
412 rc = HYPERVISOR_grant_table_op(GNTTABOP_query_size, &query, 1);
413 if ((rc < 0) || (query.status != GNTST_okay))
414 return (4); /* Legacy max supported number of frames */
416 return (query.max_nr_frames);
420 unsigned int max_nr_grant_frames(void)
422 unsigned int xen_max = __max_nr_grant_frames();
424 if (xen_max > boot_max_nr_grant_frames)
425 return (boot_max_nr_grant_frames);
431 * XXX needed for backend support
435 map_pte_fn(pte_t *pte, struct page *pmd_page,
436 unsigned long addr, void *data)
438 unsigned long **frames = (unsigned long **)data;
440 set_pte_at(&init_mm, addr, pte, pfn_pte_ma((*frames)[0], PAGE_KERNEL));
446 unmap_pte_fn(pte_t *pte, struct page *pmd_page,
447 unsigned long addr, void *data)
450 set_pte_at(&init_mm, addr, pte, __pte(0));
456 gnttab_map(unsigned int start_idx, unsigned int end_idx)
458 struct gnttab_setup_table setup;
461 unsigned int nr_gframes = end_idx + 1;
464 frames = malloc(nr_gframes * sizeof(unsigned long), M_DEVBUF, M_NOWAIT);
468 setup.dom = DOMID_SELF;
469 setup.nr_frames = nr_gframes;
470 set_xen_guest_handle(setup.frame_list, frames);
472 rc = HYPERVISOR_grant_table_op(GNTTABOP_setup_table, &setup, 1);
474 free(frames, M_DEVBUF);
477 PANIC_IF(rc || setup.status);
479 if (shared == NULL) {
482 area = kmem_alloc_nofault(kernel_map,
483 PAGE_SIZE * max_nr_grant_frames());
485 shared = (grant_entry_t *)area;
487 for (i = 0; i < nr_gframes; i++)
488 PT_SET_MA(((caddr_t)shared) + i*PAGE_SIZE,
489 ((vm_paddr_t)frames[i]) << PAGE_SHIFT | PG_RW | PG_V);
491 free(frames, M_DEVBUF);
499 if (max_nr_grant_frames() < nr_grant_frames)
501 return gnttab_map(0, nr_grant_frames - 1);
509 pages = (PAGE_SIZE*nr_grant_frames) >> PAGE_SHIFT;
511 for (i = 0; i < pages; i++)
512 PT_SET_MA(shared + (i*PAGE_SIZE), (vm_paddr_t)0);
518 gnttab_expand(unsigned int req_entries)
521 unsigned int cur, extra;
523 cur = nr_grant_frames;
524 extra = ((req_entries + (GREFS_PER_GRANT_FRAME-1)) /
525 GREFS_PER_GRANT_FRAME);
526 if (cur + extra > max_nr_grant_frames())
529 if ((rc = gnttab_map(cur, cur + extra - 1)) == 0)
530 rc = grow_gnttab_list(extra);
536 gnttab_init(void *unused)
539 unsigned int max_nr_glist_frames;
540 unsigned int nr_init_grefs;
542 if (!is_running_on_xen())
546 boot_max_nr_grant_frames = __max_nr_grant_frames();
548 /* Determine the maximum number of frames required for the
549 * grant reference free list on the current hypervisor.
551 max_nr_glist_frames = (boot_max_nr_grant_frames *
552 GREFS_PER_GRANT_FRAME /
553 (PAGE_SIZE / sizeof(grant_ref_t)));
555 gnttab_list = malloc(max_nr_glist_frames * sizeof(grant_ref_t *),
558 if (gnttab_list == NULL)
561 for (i = 0; i < nr_grant_frames; i++) {
562 gnttab_list[i] = (grant_ref_t *)malloc(PAGE_SIZE, M_DEVBUF, M_NOWAIT);
563 if (gnttab_list[i] == NULL)
567 if (gnttab_resume() < 0)
570 nr_init_grefs = nr_grant_frames * GREFS_PER_GRANT_FRAME;
572 for (i = NR_RESERVED_ENTRIES; i < nr_init_grefs - 1; i++)
573 gnttab_entry(i) = i + 1;
575 gnttab_entry(nr_init_grefs - 1) = GNTTAB_LIST_END;
576 gnttab_free_count = nr_init_grefs - NR_RESERVED_ENTRIES;
577 gnttab_free_head = NR_RESERVED_ENTRIES;
579 printk("Grant table initialized\n");
583 for (i--; i >= 0; i--)
584 free(gnttab_list[i], M_DEVBUF);
585 free(gnttab_list, M_DEVBUF);
590 MTX_SYSINIT(gnttab, &gnttab_list_lock, "GNTTAB LOCK", MTX_DEF);
591 SYSINIT(gnttab, SI_SUB_PSEUDO, SI_ORDER_FIRST, gnttab_init, NULL);