1 /******************************************************************************
2  *
3  * Copyright (c) 2007-2008, D G Murray <Derek.Murray@cl.cam.ac.uk>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation;
8  * version 2.1 of the License.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; If not, see <http://www.gnu.org/licenses/>.
17  *
18  * Split out from xc_gnttab.c
19  */
20 
21 #include <stdlib.h>
22 
23 #include "private.h"
24 
all_restrict_cb(Xentoolcore__Active_Handle * ah,domid_t domid)25 static int all_restrict_cb(Xentoolcore__Active_Handle *ah, domid_t domid) {
26     xengnttab_handle *xgt = CONTAINER_OF(ah, *xgt, tc_ah);
27     return xentoolcore__restrict_by_dup2_null(xgt->fd);
28 }
29 
xengnttab_open(xentoollog_logger * logger,unsigned open_flags)30 xengnttab_handle *xengnttab_open(xentoollog_logger *logger, unsigned open_flags)
31 {
32     xengnttab_handle *xgt = malloc(sizeof(*xgt));
33     int rc;
34 
35     if (!xgt) return NULL;
36 
37     xgt->fd = -1;
38     xgt->logger = logger;
39     xgt->logger_tofree  = NULL;
40 
41     xgt->tc_ah.restrict_callback = all_restrict_cb;
42     xentoolcore__register_active_handle(&xgt->tc_ah);
43 
44     if (!xgt->logger) {
45         xgt->logger = xgt->logger_tofree =
46             (xentoollog_logger*)
47             xtl_createlogger_stdiostream(stderr, XTL_PROGRESS, 0);
48         if (!xgt->logger) goto err;
49     }
50 
51     rc = osdep_gnttab_open(xgt);
52     if ( rc  < 0 ) goto err;
53 
54     return xgt;
55 
56 err:
57     xentoolcore__deregister_active_handle(&xgt->tc_ah);
58     osdep_gnttab_close(xgt);
59     xtl_logger_destroy(xgt->logger_tofree);
60     free(xgt);
61     return NULL;
62 }
63 
xengnttab_close(xengnttab_handle * xgt)64 int xengnttab_close(xengnttab_handle *xgt)
65 {
66     int rc;
67 
68     if ( !xgt )
69         return 0;
70 
71     xentoolcore__deregister_active_handle(&xgt->tc_ah);
72     rc = osdep_gnttab_close(xgt);
73     xtl_logger_destroy(xgt->logger_tofree);
74     free(xgt);
75     return rc;
76 }
77 
xengnttab_set_max_grants(xengnttab_handle * xgt,uint32_t count)78 int xengnttab_set_max_grants(xengnttab_handle *xgt, uint32_t count)
79 {
80     return osdep_gnttab_set_max_grants(xgt, count);
81 }
82 
xengnttab_map_grant_ref(xengnttab_handle * xgt,uint32_t domid,uint32_t ref,int prot)83 void *xengnttab_map_grant_ref(xengnttab_handle *xgt,
84                               uint32_t domid,
85                               uint32_t ref,
86                               int prot)
87 {
88     return osdep_gnttab_grant_map(xgt, 1, 0, prot, &domid, &ref, -1, -1);
89 }
90 
xengnttab_map_grant_refs(xengnttab_handle * xgt,uint32_t count,uint32_t * domids,uint32_t * refs,int prot)91 void *xengnttab_map_grant_refs(xengnttab_handle *xgt,
92                                uint32_t count,
93                                uint32_t *domids,
94                                uint32_t *refs,
95                                int prot)
96 {
97     return osdep_gnttab_grant_map(xgt, count, 0, prot, domids, refs, -1, -1);
98 }
99 
xengnttab_map_domain_grant_refs(xengnttab_handle * xgt,uint32_t count,uint32_t domid,uint32_t * refs,int prot)100 void *xengnttab_map_domain_grant_refs(xengnttab_handle *xgt,
101                                       uint32_t count,
102                                       uint32_t domid,
103                                       uint32_t *refs,
104                                       int prot)
105 {
106     return osdep_gnttab_grant_map(xgt, count, XENGNTTAB_GRANT_MAP_SINGLE_DOMAIN,
107                                   prot, &domid, refs, -1, -1);
108 }
109 
xengnttab_map_grant_ref_notify(xengnttab_handle * xgt,uint32_t domid,uint32_t ref,int prot,uint32_t notify_offset,evtchn_port_t notify_port)110 void *xengnttab_map_grant_ref_notify(xengnttab_handle *xgt,
111                                      uint32_t domid,
112                                      uint32_t ref,
113                                      int prot,
114                                      uint32_t notify_offset,
115                                      evtchn_port_t notify_port)
116 {
117     return osdep_gnttab_grant_map(xgt, 1, 0, prot,  &domid, &ref,
118                                   notify_offset, notify_port);
119 }
120 
xengnttab_unmap(xengnttab_handle * xgt,void * start_address,uint32_t count)121 int xengnttab_unmap(xengnttab_handle *xgt, void *start_address, uint32_t count)
122 {
123     return osdep_gnttab_unmap(xgt, start_address, count);
124 }
125 
xengnttab_grant_copy(xengnttab_handle * xgt,uint32_t count,xengnttab_grant_copy_segment_t * segs)126 int xengnttab_grant_copy(xengnttab_handle *xgt,
127                          uint32_t count,
128                          xengnttab_grant_copy_segment_t *segs)
129 {
130     return osdep_gnttab_grant_copy(xgt, count, segs);
131 }
132 /*
133  * Local variables:
134  * mode: C
135  * c-file-style: "BSD"
136  * c-basic-offset: 4
137  * tab-width: 4
138  * indent-tabs-mode: nil
139  * End:
140  */
141