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 <malloc.h>
32 #include <unistd.h>
33 
34 #include "private.h"
35 
gnttab_close_fd(struct file * file)36 static int gnttab_close_fd(struct file *file)
37 {
38     gntmap_fini(file->dev);
39     free(file->dev);
40 
41     return 0;
42 }
43 
44 static const struct file_ops gnttab_ops = {
45     .name = "gnttab",
46     .close = gnttab_close_fd,
47 };
48 
49 static unsigned int ftype_gnttab;
50 
51 __attribute__((constructor))
gnttab_initialize(void)52 static void gnttab_initialize(void)
53 {
54     ftype_gnttab = alloc_file_type(&gnttab_ops);
55 }
56 
osdep_gnttab_open(xengnttab_handle * xgt)57 int osdep_gnttab_open(xengnttab_handle *xgt)
58 {
59     int fd;
60     struct file *file;
61     struct gntmap *gntmap;
62 
63     gntmap = malloc(sizeof(*gntmap));
64     if ( !gntmap )
65         return -1;
66 
67     fd = alloc_fd(ftype_gnttab);
68     file = get_file_from_fd(fd);
69 
70     if ( !file )
71     {
72         free(gntmap);
73         return -1;
74     }
75 
76     file->dev = gntmap;
77     gntmap_init(gntmap);
78     xgt->fd = fd;
79     return 0;
80 }
81 
osdep_gnttab_close(xengnttab_handle * xgt)82 int osdep_gnttab_close(xengnttab_handle *xgt)
83 {
84     if ( xgt->fd == -1 )
85         return 0;
86 
87     return close(xgt->fd);
88 }
89 
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)90 void *osdep_gnttab_grant_map(xengnttab_handle *xgt,
91                              uint32_t count, int flags, int prot,
92                              uint32_t *domids, uint32_t *refs,
93                              uint32_t notify_offset,
94                              evtchn_port_t notify_port)
95 {
96     struct file *file = get_file_from_fd(xgt->fd);
97     int stride = 1;
98 
99     if (flags & XENGNTTAB_GRANT_MAP_SINGLE_DOMAIN)
100         stride = 0;
101     if (notify_offset != -1 || notify_port != -1) {
102         errno = ENOSYS;
103         return NULL;
104     }
105     return gntmap_map_grant_refs(file->dev, count, domids, stride,
106                                  refs, prot & PROT_WRITE);
107 }
108 
osdep_gnttab_unmap(xengnttab_handle * xgt,void * start_address,uint32_t count)109 int osdep_gnttab_unmap(xengnttab_handle *xgt,
110                        void *start_address,
111                        uint32_t count)
112 {
113     struct file *file = get_file_from_fd(xgt->fd);
114     int ret;
115 
116     ret = gntmap_munmap(file->dev, (unsigned long) start_address, count);
117     if (ret < 0) {
118         errno = -ret;
119         return -1;
120     }
121     return ret;
122 }
123 
osdep_gnttab_set_max_grants(xengnttab_handle * xgt,uint32_t count)124 int osdep_gnttab_set_max_grants(xengnttab_handle *xgt, uint32_t count)
125 {
126     struct file *file = get_file_from_fd(xgt->fd);
127     int ret;
128 
129     ret = gntmap_set_max_grants(file->dev, count);
130     if (ret < 0) {
131         errno = -ret;
132         return -1;
133     }
134     return ret;
135 }
136 
osdep_gnttab_grant_copy(xengnttab_handle * xgt,uint32_t count,xengnttab_grant_copy_segment_t * segs)137 int osdep_gnttab_grant_copy(xengnttab_handle *xgt,
138                             uint32_t count,
139                             xengnttab_grant_copy_segment_t *segs)
140 {
141     return -1;
142 }
143 
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)144 int osdep_gnttab_dmabuf_exp_from_refs(xengnttab_handle *xgt, uint32_t domid,
145                                       uint32_t flags, uint32_t count,
146                                       const uint32_t *refs, uint32_t *fd)
147 {
148     return -1;
149 }
150 
osdep_gnttab_dmabuf_exp_wait_released(xengnttab_handle * xgt,uint32_t fd,uint32_t wait_to_ms)151 int osdep_gnttab_dmabuf_exp_wait_released(xengnttab_handle *xgt,
152                                           uint32_t fd, uint32_t wait_to_ms)
153 {
154     return -1;
155 }
156 
osdep_gnttab_dmabuf_imp_to_refs(xengnttab_handle * xgt,uint32_t domid,uint32_t fd,uint32_t count,uint32_t * refs)157 int osdep_gnttab_dmabuf_imp_to_refs(xengnttab_handle *xgt, uint32_t domid,
158                                     uint32_t fd, uint32_t count,
159                                     uint32_t *refs)
160 {
161     return -1;
162 }
163 
osdep_gnttab_dmabuf_imp_release(xengnttab_handle * xgt,uint32_t fd)164 int osdep_gnttab_dmabuf_imp_release(xengnttab_handle *xgt, uint32_t fd)
165 {
166     return -1;
167 }
168 
169 /*
170  * Local variables:
171  * mode: C
172  * c-file-style: "BSD"
173  * c-basic-offset: 4
174  * tab-width: 4
175  * indent-tabs-mode: nil
176  * End:
177  */
178