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 <sys/param.h>
17 #include <sys/systm.h>
20 #include <sys/module.h>
21 #include <sys/kernel.h>
23 #include <sys/malloc.h>
25 #include <sys/limits.h>
27 #include <machine/resource.h>
28 #include <machine/cpu.h>
30 #include <xen/xen-os.h>
31 #include <xen/hypervisor.h>
32 #include <machine/xen/synch_bitops.h>
34 #include <xen/hypervisor.h>
35 #include <xen/gnttab.h>
38 #include <vm/vm_kern.h>
39 #include <vm/vm_extern.h>
42 /* External tools reserve first few grant table entries. */
43 #define NR_RESERVED_ENTRIES 8
44 #define GREFS_PER_GRANT_FRAME (PAGE_SIZE / sizeof(grant_entry_t))
46 static grant_ref_t **gnttab_list;
47 static unsigned int nr_grant_frames;
48 static unsigned int boot_max_nr_grant_frames;
49 static int gnttab_free_count;
50 static grant_ref_t gnttab_free_head;
51 static struct mtx gnttab_list_lock;
54 * Resource representing allocated physical address space
55 * for the grant table metainfo
57 static struct resource *gnttab_pseudo_phys_res;
59 /* Resource id for allocated physical address space. */
60 static int gnttab_pseudo_phys_res_id;
62 static grant_entry_t *shared;
64 static struct gnttab_free_callback *gnttab_free_callback_list = NULL;
66 static int gnttab_expand(unsigned int req_entries);
68 #define RPP (PAGE_SIZE / sizeof(grant_ref_t))
69 #define gnttab_entry(entry) (gnttab_list[(entry) / RPP][(entry) % RPP])
72 get_free_entries(int count, int *entries)
77 mtx_lock(&gnttab_list_lock);
78 if ((gnttab_free_count < count) &&
79 ((error = gnttab_expand(count - gnttab_free_count)) != 0)) {
80 mtx_unlock(&gnttab_list_lock);
83 ref = head = gnttab_free_head;
84 gnttab_free_count -= count;
86 head = gnttab_entry(head);
87 gnttab_free_head = gnttab_entry(head);
88 gnttab_entry(head) = GNTTAB_LIST_END;
89 mtx_unlock(&gnttab_list_lock);
96 do_free_callbacks(void)
98 struct gnttab_free_callback *callback, *next;
100 callback = gnttab_free_callback_list;
101 gnttab_free_callback_list = NULL;
103 while (callback != NULL) {
104 next = callback->next;
105 if (gnttab_free_count >= callback->count) {
106 callback->next = NULL;
107 callback->fn(callback->arg);
109 callback->next = gnttab_free_callback_list;
110 gnttab_free_callback_list = callback;
117 check_free_callbacks(void)
119 if (__predict_false(gnttab_free_callback_list != NULL))
124 put_free_entry(grant_ref_t ref)
127 mtx_lock(&gnttab_list_lock);
128 gnttab_entry(ref) = gnttab_free_head;
129 gnttab_free_head = ref;
131 check_free_callbacks();
132 mtx_unlock(&gnttab_list_lock);
136 * Public grant-issuing interface functions
140 gnttab_grant_foreign_access(domid_t domid, unsigned long frame, int readonly,
145 error = get_free_entries(1, &ref);
147 if (__predict_false(error))
150 shared[ref].frame = frame;
151 shared[ref].domid = domid;
153 shared[ref].flags = GTF_permit_access | (readonly ? GTF_readonly : 0);
162 gnttab_grant_foreign_access_ref(grant_ref_t ref, domid_t domid,
163 unsigned long frame, int readonly)
166 shared[ref].frame = frame;
167 shared[ref].domid = domid;
169 shared[ref].flags = GTF_permit_access | (readonly ? GTF_readonly : 0);
173 gnttab_query_foreign_access(grant_ref_t ref)
177 nflags = shared[ref].flags;
179 return (nflags & (GTF_reading|GTF_writing));
183 gnttab_end_foreign_access_ref(grant_ref_t ref)
185 uint16_t flags, nflags;
187 nflags = shared[ref].flags;
189 if ( (flags = nflags) & (GTF_reading|GTF_writing) ) {
190 printf("%s: WARNING: g.e. still in use!\n", __func__);
193 } while ((nflags = synch_cmpxchg(&shared[ref].flags, flags, 0)) !=
200 gnttab_end_foreign_access(grant_ref_t ref, void *page)
202 if (gnttab_end_foreign_access_ref(ref)) {
205 free(page, M_DEVBUF);
209 /* XXX This needs to be fixed so that the ref and page are
210 placed on a list to be freed up later. */
211 printf("%s: WARNING: leaking g.e. and page still in use!\n",
217 gnttab_end_foreign_access_references(u_int count, grant_ref_t *refs)
219 grant_ref_t *last_ref;
223 head = GNTTAB_LIST_END;
225 last_ref = refs + count;
226 while (refs != last_ref) {
228 if (gnttab_end_foreign_access_ref(*refs)) {
229 gnttab_entry(*refs) = head;
233 * XXX This needs to be fixed so that the ref
234 * is placed on a list to be freed up later.
236 printf("%s: WARNING: leaking g.e. still in use!\n",
244 mtx_lock(&gnttab_list_lock);
245 gnttab_free_count += count;
246 gnttab_entry(tail) = gnttab_free_head;
247 gnttab_free_head = head;
248 check_free_callbacks();
249 mtx_unlock(&gnttab_list_lock);
254 gnttab_grant_foreign_transfer(domid_t domid, unsigned long pfn,
259 error = get_free_entries(1, &ref);
260 if (__predict_false(error))
263 gnttab_grant_foreign_transfer_ref(ref, domid, pfn);
270 gnttab_grant_foreign_transfer_ref(grant_ref_t ref, domid_t domid,
273 shared[ref].frame = pfn;
274 shared[ref].domid = domid;
276 shared[ref].flags = GTF_accept_transfer;
280 gnttab_end_foreign_transfer_ref(grant_ref_t ref)
286 * If a transfer is not even yet started, try to reclaim the grant
287 * reference and return failure (== 0).
289 while (!((flags = shared[ref].flags) & GTF_transfer_committed)) {
290 if ( synch_cmpxchg(&shared[ref].flags, flags, 0) == flags )
295 /* If a transfer is in progress then wait until it is completed. */
296 while (!(flags & GTF_transfer_completed)) {
297 flags = shared[ref].flags;
301 /* Read the frame number /after/ reading completion status. */
303 frame = shared[ref].frame;
304 KASSERT(frame != 0, ("grant table inconsistent"));
310 gnttab_end_foreign_transfer(grant_ref_t ref)
312 unsigned long frame = gnttab_end_foreign_transfer_ref(ref);
319 gnttab_free_grant_reference(grant_ref_t ref)
326 gnttab_free_grant_references(grant_ref_t head)
331 if (head == GNTTAB_LIST_END)
335 while (gnttab_entry(ref) != GNTTAB_LIST_END) {
336 ref = gnttab_entry(ref);
339 mtx_lock(&gnttab_list_lock);
340 gnttab_entry(ref) = gnttab_free_head;
341 gnttab_free_head = head;
342 gnttab_free_count += count;
343 check_free_callbacks();
344 mtx_unlock(&gnttab_list_lock);
348 gnttab_alloc_grant_references(uint16_t count, grant_ref_t *head)
352 error = get_free_entries(count, &ref);
353 if (__predict_false(error))
361 gnttab_empty_grant_references(const grant_ref_t *private_head)
364 return (*private_head == GNTTAB_LIST_END);
368 gnttab_claim_grant_reference(grant_ref_t *private_head)
370 grant_ref_t g = *private_head;
372 if (__predict_false(g == GNTTAB_LIST_END))
374 *private_head = gnttab_entry(g);
379 gnttab_release_grant_reference(grant_ref_t *private_head, grant_ref_t release)
382 gnttab_entry(release) = *private_head;
383 *private_head = release;
387 gnttab_request_free_callback(struct gnttab_free_callback *callback,
388 void (*fn)(void *), void *arg, uint16_t count)
391 mtx_lock(&gnttab_list_lock);
396 callback->count = count;
397 callback->next = gnttab_free_callback_list;
398 gnttab_free_callback_list = callback;
399 check_free_callbacks();
401 mtx_unlock(&gnttab_list_lock);
406 gnttab_cancel_free_callback(struct gnttab_free_callback *callback)
408 struct gnttab_free_callback **pcb;
410 mtx_lock(&gnttab_list_lock);
411 for (pcb = &gnttab_free_callback_list; *pcb; pcb = &(*pcb)->next) {
412 if (*pcb == callback) {
413 *pcb = callback->next;
417 mtx_unlock(&gnttab_list_lock);
422 grow_gnttab_list(unsigned int more_frames)
424 unsigned int new_nr_grant_frames, extra_entries, i;
426 new_nr_grant_frames = nr_grant_frames + more_frames;
427 extra_entries = more_frames * GREFS_PER_GRANT_FRAME;
429 for (i = nr_grant_frames; i < new_nr_grant_frames; i++)
431 gnttab_list[i] = (grant_ref_t *)
432 malloc(PAGE_SIZE, M_DEVBUF, M_NOWAIT);
438 for (i = GREFS_PER_GRANT_FRAME * nr_grant_frames;
439 i < GREFS_PER_GRANT_FRAME * new_nr_grant_frames - 1; i++)
440 gnttab_entry(i) = i + 1;
442 gnttab_entry(i) = gnttab_free_head;
443 gnttab_free_head = GREFS_PER_GRANT_FRAME * nr_grant_frames;
444 gnttab_free_count += extra_entries;
446 nr_grant_frames = new_nr_grant_frames;
448 check_free_callbacks();
453 for ( ; i >= nr_grant_frames; i--)
454 free(gnttab_list[i], M_DEVBUF);
459 __max_nr_grant_frames(void)
461 struct gnttab_query_size query;
464 query.dom = DOMID_SELF;
466 rc = HYPERVISOR_grant_table_op(GNTTABOP_query_size, &query, 1);
467 if ((rc < 0) || (query.status != GNTST_okay))
468 return (4); /* Legacy max supported number of frames */
470 return (query.max_nr_frames);
474 unsigned int max_nr_grant_frames(void)
476 unsigned int xen_max = __max_nr_grant_frames();
478 if (xen_max > boot_max_nr_grant_frames)
479 return (boot_max_nr_grant_frames);
485 * XXX needed for backend support
489 map_pte_fn(pte_t *pte, struct page *pmd_page,
490 unsigned long addr, void *data)
492 unsigned long **frames = (unsigned long **)data;
494 set_pte_at(&init_mm, addr, pte, pfn_pte_ma((*frames)[0], PAGE_KERNEL));
500 unmap_pte_fn(pte_t *pte, struct page *pmd_page,
501 unsigned long addr, void *data)
504 set_pte_at(&init_mm, addr, pte, __pte(0));
509 static vm_paddr_t resume_frames;
512 gnttab_map(unsigned int start_idx, unsigned int end_idx)
514 struct xen_add_to_physmap xatp;
515 unsigned int i = end_idx;
518 * Loop backwards, so that the first hypercall has the largest index,
519 * ensuring that the table will grow only once.
522 xatp.domid = DOMID_SELF;
524 xatp.space = XENMAPSPACE_grant_table;
525 xatp.gpfn = (resume_frames >> PAGE_SHIFT) + i;
526 if (HYPERVISOR_memory_op(XENMEM_add_to_physmap, &xatp))
527 panic("HYPERVISOR_memory_op failed to map gnttab");
528 } while (i-- > start_idx);
530 if (shared == NULL) {
533 area = kva_alloc(PAGE_SIZE * max_nr_grant_frames());
534 KASSERT(area, ("can't allocate VM space for grant table"));
535 shared = (grant_entry_t *)area;
538 for (i = start_idx; i <= end_idx; i++) {
539 pmap_kenter((vm_offset_t) shared + i * PAGE_SIZE,
540 resume_frames + i * PAGE_SIZE);
547 gnttab_resume(device_t dev)
549 unsigned int max_nr_gframes, nr_gframes;
551 nr_gframes = nr_grant_frames;
552 max_nr_gframes = max_nr_grant_frames();
553 if (max_nr_gframes < nr_gframes)
556 if (!resume_frames) {
558 ("No resume frames and no device provided"));
560 gnttab_pseudo_phys_res = xenmem_alloc(dev,
561 &gnttab_pseudo_phys_res_id, PAGE_SIZE * max_nr_gframes);
562 if (gnttab_pseudo_phys_res == NULL)
563 panic("Unable to reserve physical memory for gnttab");
564 resume_frames = rman_get_start(gnttab_pseudo_phys_res);
567 return (gnttab_map(0, nr_gframes - 1));
571 gnttab_expand(unsigned int req_entries)
574 unsigned int cur, extra;
576 cur = nr_grant_frames;
577 extra = howmany(req_entries, GREFS_PER_GRANT_FRAME);
578 if (cur + extra > max_nr_grant_frames())
581 error = gnttab_map(cur, cur + extra - 1);
583 error = grow_gnttab_list(extra);
588 MTX_SYSINIT(gnttab, &gnttab_list_lock, "GNTTAB LOCK", MTX_DEF | MTX_RECURSE);
590 /*------------------ Private Device Attachment Functions --------------------*/
592 * \brief Identify instances of this device type in the system.
594 * \param driver The driver performing this identify action.
595 * \param parent The NewBus parent device for any devices this method adds.
598 granttable_identify(driver_t *driver __unused, device_t parent)
601 KASSERT(xen_domain(),
602 ("Trying to attach grant-table device on non Xen domain"));
604 * A single device instance for our driver is always present
605 * in a system operating under Xen.
607 if (BUS_ADD_CHILD(parent, 0, driver->name, 0) == NULL)
608 panic("unable to attach Xen Grant-table device");
612 * \brief Probe for the existence of the Xen Grant-table device
614 * \param dev NewBus device_t for this instance.
616 * \return Always returns 0 indicating success.
619 granttable_probe(device_t dev)
622 device_set_desc(dev, "Xen Grant-table Device");
623 return (BUS_PROBE_NOWILDCARD);
627 * \brief Attach the Xen Grant-table device.
629 * \param dev NewBus device_t for this instance.
631 * \return On success, 0. Otherwise an errno value indicating the
635 granttable_attach(device_t dev)
638 unsigned int max_nr_glist_frames;
639 unsigned int nr_init_grefs;
642 boot_max_nr_grant_frames = __max_nr_grant_frames();
644 /* Determine the maximum number of frames required for the
645 * grant reference free list on the current hypervisor.
647 max_nr_glist_frames = (boot_max_nr_grant_frames *
648 GREFS_PER_GRANT_FRAME /
649 (PAGE_SIZE / sizeof(grant_ref_t)));
651 gnttab_list = malloc(max_nr_glist_frames * sizeof(grant_ref_t *),
654 if (gnttab_list == NULL)
657 for (i = 0; i < nr_grant_frames; i++) {
658 gnttab_list[i] = (grant_ref_t *)
659 malloc(PAGE_SIZE, M_DEVBUF, M_NOWAIT);
660 if (gnttab_list[i] == NULL)
664 if (gnttab_resume(dev))
667 nr_init_grefs = nr_grant_frames * GREFS_PER_GRANT_FRAME;
669 for (i = NR_RESERVED_ENTRIES; i < nr_init_grefs - 1; i++)
670 gnttab_entry(i) = i + 1;
672 gnttab_entry(nr_init_grefs - 1) = GNTTAB_LIST_END;
673 gnttab_free_count = nr_init_grefs - NR_RESERVED_ENTRIES;
674 gnttab_free_head = NR_RESERVED_ENTRIES;
677 printf("Grant table initialized\n");
682 for (i--; i >= 0; i--)
683 free(gnttab_list[i], M_DEVBUF);
684 free(gnttab_list, M_DEVBUF);
688 /*-------------------- Private Device Attachment Data -----------------------*/
689 static device_method_t granttable_methods[] = {
690 /* Device interface */
691 DEVMETHOD(device_identify, granttable_identify),
692 DEVMETHOD(device_probe, granttable_probe),
693 DEVMETHOD(device_attach, granttable_attach),
698 DEFINE_CLASS_0(granttable, granttable_driver, granttable_methods, 0);
699 devclass_t granttable_devclass;
701 DRIVER_MODULE_ORDERED(granttable, xenpv, granttable_driver, granttable_devclass,
702 NULL, NULL, SI_ORDER_FIRST);