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