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 <errno.h>
21 #include <fcntl.h>
22 #include <stdint.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26
27 #include <sys/mman.h>
28 #include <sys/ioctl.h>
29
30 #include "private.h"
31
32 #define PRIVCMD_DEV "/dev/xen/privcmd"
33
osdep_xenforeignmemory_open(xenforeignmemory_handle * fmem)34 int osdep_xenforeignmemory_open(xenforeignmemory_handle *fmem)
35 {
36 int fd = open(PRIVCMD_DEV, O_RDWR|O_CLOEXEC);
37
38 if ( fd == -1 )
39 {
40 PERROR("Could not obtain handle on privileged command interface "
41 PRIVCMD_DEV);
42 return -1;
43 }
44
45 fmem->fd = fd;
46 return 0;
47 }
48
osdep_xenforeignmemory_close(xenforeignmemory_handle * fmem)49 int osdep_xenforeignmemory_close(xenforeignmemory_handle *fmem)
50 {
51 int fd = fmem->fd;
52 if ( fd == -1 )
53 return 0;
54 return close(fd);
55 }
56
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[])57 void *osdep_xenforeignmemory_map(xenforeignmemory_handle *fmem,
58 uint32_t dom, void *addr,
59 int prot, int flags, size_t num,
60 const xen_pfn_t arr[/*num*/], int err[/*num*/])
61 {
62 int fd = fmem->fd;
63 privcmd_mmapbatch_t ioctlx;
64 int rc;
65
66 addr = mmap(addr, num << PAGE_SHIFT, prot, flags | MAP_SHARED, fd, 0);
67 if ( addr == MAP_FAILED )
68 {
69 PERROR("xc_map_foreign_bulk: mmap failed");
70 return NULL;
71 }
72
73 ioctlx.num = num;
74 ioctlx.dom = dom;
75 ioctlx.addr = (unsigned long)addr;
76 ioctlx.arr = arr;
77 ioctlx.err = err;
78
79 rc = ioctl(fd, IOCTL_PRIVCMD_MMAPBATCH, &ioctlx);
80 if ( rc < 0 )
81 {
82 int saved_errno = errno;
83 PERROR("xc_map_foreign_bulk: ioctl failed");
84 (void)munmap(addr, num << PAGE_SHIFT);
85 errno = saved_errno;
86 return NULL;
87 }
88
89 return addr;
90 }
91
osdep_xenforeignmemory_unmap(xenforeignmemory_handle * fmem,void * addr,size_t num)92 int osdep_xenforeignmemory_unmap(xenforeignmemory_handle *fmem,
93 void *addr, size_t num)
94 {
95 return munmap(addr, num << PAGE_SHIFT);
96 }
97
98 /*
99 * Local variables:
100 * mode: C
101 * c-file-style: "BSD"
102 * c-basic-offset: 4
103 * tab-width: 4
104 * indent-tabs-mode: nil
105 * End:
106 */
107