1 /*
2  *
3  * Copyright 2007-2008 Samuel Thibault <samuel.thibault@eu.citrix.com>.
4  * All rights reserved.
5  * Use is subject to license terms.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation;
10  * version 2.1 of the License.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; If not, see <http://www.gnu.org/licenses/>.
19  *
20  * Splitfrom xc_minios.c
21  */
22 
23 #include <mini-os/types.h>
24 #include <mini-os/os.h>
25 #include <mini-os/lib.h>
26 
27 #include <mini-os/gntmap.h>
28 #include <sys/mman.h>
29 
30 #include <errno.h>
31 #include <unistd.h>
32 
33 #include "private.h"
34 
35 void minios_gnttab_close_fd(int fd);
36 
osdep_gnttab_open(xengnttab_handle * xgt)37 int osdep_gnttab_open(xengnttab_handle *xgt)
38 {
39     int fd = alloc_fd(FTYPE_GNTMAP);
40     if ( fd == -1 )
41         return -1;
42     gntmap_init(&files[fd].gntmap);
43     xgt->fd = fd;
44     return 0;
45 }
46 
osdep_gnttab_close(xengnttab_handle * xgt)47 int osdep_gnttab_close(xengnttab_handle *xgt)
48 {
49     if ( xgt->fd == -1 )
50         return 0;
51 
52     return close(xgt->fd);
53 }
54 
minios_gnttab_close_fd(int fd)55 void minios_gnttab_close_fd(int fd)
56 {
57     gntmap_fini(&files[fd].gntmap);
58     files[fd].type = FTYPE_NONE;
59 }
60 
osdep_gnttab_grant_map(xengnttab_handle * xgt,uint32_t count,int flags,int prot,uint32_t * domids,uint32_t * refs,uint32_t notify_offset,evtchn_port_t notify_port)61 void *osdep_gnttab_grant_map(xengnttab_handle *xgt,
62                              uint32_t count, int flags, int prot,
63                              uint32_t *domids, uint32_t *refs,
64                              uint32_t notify_offset,
65                              evtchn_port_t notify_port)
66 {
67     int fd = xgt->fd;
68     int stride = 1;
69     if (flags & XENGNTTAB_GRANT_MAP_SINGLE_DOMAIN)
70         stride = 0;
71     if (notify_offset != -1 || notify_port != -1) {
72         errno = ENOSYS;
73         return NULL;
74     }
75     return gntmap_map_grant_refs(&files[fd].gntmap,
76                                  count, domids, stride,
77                                  refs, prot & PROT_WRITE);
78 }
79 
osdep_gnttab_unmap(xengnttab_handle * xgt,void * start_address,uint32_t count)80 int osdep_gnttab_unmap(xengnttab_handle *xgt,
81                        void *start_address,
82                        uint32_t count)
83 {
84     int fd = xgt->fd;
85     int ret;
86     ret = gntmap_munmap(&files[fd].gntmap,
87                         (unsigned long) start_address,
88                         count);
89     if (ret < 0) {
90         errno = -ret;
91         return -1;
92     }
93     return ret;
94 }
95 
osdep_gnttab_set_max_grants(xengnttab_handle * xgt,uint32_t count)96 int osdep_gnttab_set_max_grants(xengnttab_handle *xgt, uint32_t count)
97 {
98     int fd = xgt->fd;
99     int ret;
100     ret = gntmap_set_max_grants(&files[fd].gntmap,
101                                 count);
102     if (ret < 0) {
103         errno = -ret;
104         return -1;
105     }
106     return ret;
107 }
108 
osdep_gnttab_grant_copy(xengnttab_handle * xgt,uint32_t count,xengnttab_grant_copy_segment_t * segs)109 int osdep_gnttab_grant_copy(xengnttab_handle *xgt,
110                             uint32_t count,
111                             xengnttab_grant_copy_segment_t *segs)
112 {
113     return -1;
114 }
115 
osdep_gnttab_dmabuf_exp_from_refs(xengnttab_handle * xgt,uint32_t domid,uint32_t flags,uint32_t count,const uint32_t * refs,uint32_t * fd)116 int osdep_gnttab_dmabuf_exp_from_refs(xengnttab_handle *xgt, uint32_t domid,
117                                       uint32_t flags, uint32_t count,
118                                       const uint32_t *refs, uint32_t *fd)
119 {
120     return -1;
121 }
122 
osdep_gnttab_dmabuf_exp_wait_released(xengnttab_handle * xgt,uint32_t fd,uint32_t wait_to_ms)123 int osdep_gnttab_dmabuf_exp_wait_released(xengnttab_handle *xgt,
124                                           uint32_t fd, uint32_t wait_to_ms)
125 {
126     return -1;
127 }
128 
osdep_gnttab_dmabuf_imp_to_refs(xengnttab_handle * xgt,uint32_t domid,uint32_t fd,uint32_t count,uint32_t * refs)129 int osdep_gnttab_dmabuf_imp_to_refs(xengnttab_handle *xgt, uint32_t domid,
130                                     uint32_t fd, uint32_t count,
131                                     uint32_t *refs)
132 {
133     return -1;
134 }
135 
osdep_gnttab_dmabuf_imp_release(xengnttab_handle * xgt,uint32_t fd)136 int osdep_gnttab_dmabuf_imp_release(xengnttab_handle *xgt, uint32_t fd)
137 {
138     return -1;
139 }
140 
141 /*
142  * Local variables:
143  * mode: C
144  * c-file-style: "BSD"
145  * c-basic-offset: 4
146  * tab-width: 4
147  * indent-tabs-mode: nil
148  * End:
149  */
150