1 /******************************************************************************
2  *
3  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
4  * Use is subject to license terms.
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 
20 #include <unistd.h>
21 #include <fcntl.h>
22 #include <errno.h>
23 #include <sys/mman.h>
24 #include <sys/ioctl.h>
25 
26 #include "private.h"
27 
osdep_xenforeignmemory_open(xenforeignmemory_handle * fmem)28 int osdep_xenforeignmemory_open(xenforeignmemory_handle *fmem)
29 {
30     int flags, saved_errno;
31     int fd = open("/kern/xen/privcmd", O_RDWR);
32 
33     if ( fd == -1 )
34     {
35         PERROR("Could not obtain handle on privileged command interface");
36         return -1;
37     }
38 
39     /* Although we return the file handle as the 'xc handle' the API
40        does not specify / guarentee that this integer is in fact
41        a file handle. Thus we must take responsiblity to ensure
42        it doesn't propagate (ie leak) outside the process */
43     if ( (flags = fcntl(fd, F_GETFD)) < 0 )
44     {
45         PERROR("Could not get file handle flags");
46         goto error;
47     }
48     flags |= FD_CLOEXEC;
49     if ( fcntl(fd, F_SETFD, flags) < 0 )
50     {
51         PERROR("Could not set file handle flags");
52         goto error;
53     }
54 
55     fmem->fd = fd;
56     return 0;
57 
58  error:
59     saved_errno = errno;
60     close(fd);
61     errno = saved_errno;
62     return -1;
63 }
64 
osdep_xenforeignmemory_close(xenforeignmemory_handle * fmem)65 int osdep_xenforeignmemory_close(xenforeignmemory_handle *fmem)
66 {
67     int fd = fmem->fd;
68     return close(fd);
69 }
70 
osdep_xenforeignmemory_map(xenforeignmemory_handle * fmem,uint32_t dom,void * addr,int prot,int flags,size_t num,const xen_pfn_t arr[],int err[])71 void *osdep_xenforeignmemory_map(xenforeignmemory_handle *fmem,
72                                  uint32_t dom, void *addr,
73                                  int prot, int flags, size_t num,
74                                  const xen_pfn_t arr[/*num*/], int err[/*num*/])
75 
76 {
77     int fd = fmem->fd;
78     privcmd_mmapbatch_v2_t ioctlx;
79     addr = mmap(addr, num * XC_PAGE_SIZE, prot,
80                 flags | MAP_ANON | MAP_SHARED, -1, 0);
81     if ( addr == MAP_FAILED ) {
82         PERROR("osdep_xenforeignmemory_map: mmap failed");
83         return NULL;
84     }
85 
86     ioctlx.num=num;
87     ioctlx.dom=dom;
88     ioctlx.addr=(unsigned long)addr;
89     ioctlx.arr=arr;
90     ioctlx.err=err;
91 
92     if ( ioctl(fd, IOCTL_PRIVCMD_MMAPBATCH_V2, &ioctlx) < 0 )
93     {
94         int saved_errno = errno;
95         PERROR("osdep_xenforeignmemory_map: ioctl failed");
96         munmap(addr, num * XC_PAGE_SIZE);
97         errno = saved_errno;
98         return NULL;
99     }
100     return addr;
101 
102 }
103 
osdep_xenforeignmemory_unmap(xenforeignmemory_handle * fmem,void * addr,size_t num)104 int osdep_xenforeignmemory_unmap(xenforeignmemory_handle *fmem,
105                                  void *addr, size_t num)
106 {
107     return munmap(addr, num * XC_PAGE_SIZE);
108 }
109 
osdep_xenforeignmemory_restrict(xenforeignmemory_handle * fmem,domid_t domid)110 int osdep_xenforeignmemory_restrict(xenforeignmemory_handle *fmem,
111                                     domid_t domid)
112 {
113     errno = EOPNOTSUPP;
114     return -1;
115 }
116 
osdep_xenforeignmemory_unmap_resource(xenforeignmemory_handle * fmem,xenforeignmemory_resource_handle * fres)117 int osdep_xenforeignmemory_unmap_resource(
118     xenforeignmemory_handle *fmem, xenforeignmemory_resource_handle *fres)
119 {
120     return fres ? munmap(fres->addr, fres->nr_frames << XC_PAGE_SHIFT) : 0;
121 }
122 
osdep_xenforeignmemory_map_resource(xenforeignmemory_handle * fmem,xenforeignmemory_resource_handle * fres)123 int osdep_xenforeignmemory_map_resource(
124     xenforeignmemory_handle *fmem, xenforeignmemory_resource_handle *fres)
125 {
126     privcmd_mmap_resource_t mr = {
127         .dom = fres->domid,
128         .type = fres->type,
129         .id = fres->id,
130         .idx = fres->frame,
131         .num = fres->nr_frames,
132     };
133     int rc;
134 
135     if ( !fres->addr && !fres->nr_frames )
136         /* Request for resource size.  Skip mmap(). */
137         goto skip_mmap;
138 
139     fres->addr = mmap(fres->addr, fres->nr_frames << XC_PAGE_SHIFT,
140                       fres->prot, fres->flags | MAP_ANON | MAP_SHARED, -1, 0);
141     if ( fres->addr == MAP_FAILED )
142         return -1;
143 
144     mr.addr = (uintptr_t)fres->addr;
145 
146  skip_mmap:
147     rc = ioctl(fmem->fd, IOCTL_PRIVCMD_MMAP_RESOURCE, &mr);
148     if ( rc )
149     {
150         if ( errno == ENOSYS )
151             errno = EOPNOTSUPP;
152 
153         if ( fres->addr )
154         {
155             int saved_errno = errno;
156 
157             osdep_xenforeignmemory_unmap_resource(fmem, fres);
158             errno = saved_errno;
159         }
160 
161         return -1;
162     }
163 
164     /* If requesting size, copy back. */
165     if ( !fres->addr )
166         fres->nr_frames = mr.num;
167 
168     return 0;
169 }
170 
171 /*
172  * Local variables:
173  * mode: C
174  * c-file-style: "BSD"
175  * c-basic-offset: 4
176  * tab-width: 4
177  * indent-tabs-mode: nil
178  * End:
179  */
180