ia64/xen-unstable

changeset 19689:4b2172c60a8c

libxc: Add a function to map a domain's grant table.

Signed-off-by: Jiang, Yunhong <yunhong.jiang@intel.com>
author Keir Fraser <keir.fraser@citrix.com>
date Mon Jun 01 14:13:20 2009 +0100 (2009-06-01)
parents bd39df93a29e
children 6d6ec6f966cf
files tools/libxc/xc_linux.c tools/libxc/xenctrl.h
line diff
     1.1 --- a/tools/libxc/xc_linux.c	Mon Jun 01 14:12:53 2009 +0100
     1.2 +++ b/tools/libxc/xc_linux.c	Mon Jun 01 14:13:20 2009 +0100
     1.3 @@ -562,6 +562,111 @@ int xc_gnttab_set_max_grants(int xcg_han
     1.4      return 0;
     1.5  }
     1.6  
     1.7 +int xc_gnttab_op(int xc_handle, int cmd,
     1.8 +                 void * op, int op_size, int count)
     1.9 +{
    1.10 +    int ret = 0;
    1.11 +    DECLARE_HYPERCALL;
    1.12 +
    1.13 +    hypercall.op     = __HYPERVISOR_grant_table_op;
    1.14 +    hypercall.arg[0] = cmd;
    1.15 +    hypercall.arg[1] = (unsigned long)op;
    1.16 +    hypercall.arg[2] = count;
    1.17 +
    1.18 +    if ( lock_pages(op, count* op_size) != 0 )
    1.19 +    {
    1.20 +        PERROR("Could not lock memory for Xen hypercall");
    1.21 +        goto out1;
    1.22 +    }
    1.23 +
    1.24 +    ret = do_xen_hypercall(xc_handle, &hypercall);
    1.25 +
    1.26 +    unlock_pages(op, count * op_size);
    1.27 +
    1.28 + out1:
    1.29 +    return ret;
    1.30 +}
    1.31 +
    1.32 +struct grant_entry *xc_gnttab_map_table(int xc_handle, int domid, int *gnt_num)
    1.33 +{
    1.34 +    int rc, i;
    1.35 +    struct gnttab_query_size query;
    1.36 +    struct gnttab_setup_table setup;
    1.37 +    unsigned long *frame_list = NULL;
    1.38 +    xen_pfn_t *pfn_list = NULL;
    1.39 +    struct grant_entry *gnt = NULL;
    1.40 +
    1.41 +    if (!gnt_num)
    1.42 +        return NULL;
    1.43 +
    1.44 +    query.dom = domid;
    1.45 +    rc = xc_gnttab_op(xc_handle, GNTTABOP_query_size,
    1.46 +                     &query, sizeof(query), 1);
    1.47 +
    1.48 +    if (rc || (query.status != GNTST_okay) )
    1.49 +    {
    1.50 +        ERROR("Could not query dom's grant size\n", domid);
    1.51 +        return NULL;
    1.52 +    }
    1.53 +
    1.54 +    *gnt_num = query.nr_frames *
    1.55 +            (PAGE_SIZE / sizeof(struct grant_entry) );
    1.56 +
    1.57 +    frame_list = malloc(query.nr_frames * sizeof(unsigned long));
    1.58 +    if (!frame_list || lock_pages(frame_list, query.nr_frames *
    1.59 +                                              sizeof(unsigned long)))
    1.60 +    {
    1.61 +        ERROR("Alloc/lock frame_list in xc_gnttab_map_table\n");
    1.62 +        if (frame_list)
    1.63 +            free(frame_list);
    1.64 +        return NULL;
    1.65 +    }
    1.66 +
    1.67 +    pfn_list = malloc(query.nr_frames * sizeof(xen_pfn_t));
    1.68 +
    1.69 +    if (!pfn_list)
    1.70 +    {
    1.71 +        ERROR("Could not lock pfn_list in xc_gnttab_map_table\n");
    1.72 +        goto err;
    1.73 +    }
    1.74 +
    1.75 +    setup.dom = domid;
    1.76 +    setup.nr_frames = query.nr_frames;
    1.77 +    set_xen_guest_handle(setup.frame_list, frame_list);
    1.78 +
    1.79 +    /* XXX Any race with other setup_table hypercall? */
    1.80 +    rc = xc_gnttab_op(xc_handle, GNTTABOP_setup_table,
    1.81 +                      &setup, sizeof(setup), 1);
    1.82 +
    1.83 +    if (rc ||( setup.status != GNTST_okay) )
    1.84 +    {
    1.85 +        ERROR("Could not get grant table frame list\n");
    1.86 +        goto err;
    1.87 +    }
    1.88 +
    1.89 +    for (i = 0; i < setup.nr_frames; i++)
    1.90 +        pfn_list[i] = frame_list[i];
    1.91 +
    1.92 +    gnt = xc_map_foreign_pages(xc_handle, domid, PROT_READ,
    1.93 +                               pfn_list, setup.nr_frames);
    1.94 +    if (!gnt)
    1.95 +    {
    1.96 +        ERROR("Could not map grant table\n");
    1.97 +        goto err;
    1.98 +    }
    1.99 +
   1.100 +err:
   1.101 +    if (frame_list)
   1.102 +    {
   1.103 +        unlock_pages(frame_list, query.nr_frames *  sizeof(unsigned long));
   1.104 +        free(frame_list);
   1.105 +    }
   1.106 +    if (pfn_list)
   1.107 +        free(pfn_list);
   1.108 +
   1.109 +    return gnt;
   1.110 +}
   1.111 +
   1.112  /*
   1.113   * Local variables:
   1.114   * mode: C
     2.1 --- a/tools/libxc/xenctrl.h	Mon Jun 01 14:12:53 2009 +0100
     2.2 +++ b/tools/libxc/xenctrl.h	Mon Jun 01 14:13:20 2009 +0100
     2.3 @@ -27,6 +27,7 @@
     2.4  #include <xen/event_channel.h>
     2.5  #include <xen/sched.h>
     2.6  #include <xen/memory.h>
     2.7 +#include <xen/grant_table.h>
     2.8  #include <xen/hvm/params.h>
     2.9  #include <xen/xsm/acm.h>
    2.10  #include <xen/xsm/acm_ops.h>
    2.11 @@ -937,6 +938,11 @@ int xc_gnttab_munmap(int xcg_handle,
    2.12  int xc_gnttab_set_max_grants(int xcg_handle,
    2.13  			     uint32_t count);
    2.14  
    2.15 +int xc_gnttab_op(int xc_handle, int cmd,
    2.16 +                 void * op, int op_size, int count);
    2.17 +
    2.18 +struct grant_entry *xc_gnttab_map_table(int xc_handle, int domid, int *gnt_num);
    2.19 +
    2.20  int xc_physdev_map_pirq(int xc_handle,
    2.21                          int domid,
    2.22                          int index,