1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
/*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation;
* version 2.1 of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; If not, see <http://www.gnu.org/licenses/>.
*
* Split out from xc_linus_osdep.c:
*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
*/
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <assert.h>
#include <stdio.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include "private.h"
#ifndef O_CLOEXEC
#define O_CLOEXEC 0
#endif
int osdep_xencall_open(xencall_handle *xcall)
{
int fd;
/*
* Prefer the newer interface.
*/
fd = open("/dev/xen/privcmd", O_RDWR|O_CLOEXEC);
if ( fd == -1 && ( errno == ENOENT || errno == ENXIO || errno == ENODEV ))
{
/* Fallback to /proc/xen/privcmd */
fd = open("/proc/xen/privcmd", O_RDWR|O_CLOEXEC);
}
if ( fd == -1 )
{
PERROR("Could not obtain handle on privileged command interface");
return -1;
}
xcall->fd = fd;
/*
* Try the same for the hypercall buffer device.
*/
fd = open("/dev/xen/hypercall", O_RDWR|O_CLOEXEC);
if ( fd == -1 && errno != ENOENT )
{
PERROR("Error on trying to open hypercall buffer device");
return -1;
}
xcall->buf_fd = fd;
return 0;
}
int osdep_xencall_close(xencall_handle *xcall)
{
if ( xcall->buf_fd >= 0 )
close(xcall->buf_fd);
if ( xcall->fd >= 0 )
close(xcall->fd);
return 0;
}
long osdep_hypercall(xencall_handle *xcall, privcmd_hypercall_t *hypercall)
{
return ioctl(xcall->fd, IOCTL_PRIVCMD_HYPERCALL, hypercall);
}
static void *alloc_pages_bufdev(xencall_handle *xcall, size_t npages)
{
void *p;
p = mmap(NULL, npages * PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED,
xcall->buf_fd, 0);
if ( p == MAP_FAILED )
{
PERROR("alloc_pages: mmap (,%zu*%lu,...) [bufdev] failed",
npages, (unsigned long)PAGE_SIZE);
p = NULL;
}
return p;
}
static void *alloc_pages_nobufdev(xencall_handle *xcall, size_t npages)
{
size_t size = npages * PAGE_SIZE;
void *p;
int rc, i, saved_errno;
/* Address returned by mmap is page aligned. */
p = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_LOCKED, -1, 0);
if ( p == MAP_FAILED )
{
PERROR("alloc_pages: mmap(,%zu,...) [nobufdev] failed", size);
return NULL;
}
/* Do not copy the VMA to child process on fork. Avoid the page being COW
on hypercall. */
rc = madvise(p, npages * PAGE_SIZE, MADV_DONTFORK);
if ( rc < 0 )
{
PERROR("alloc_pages: madvise (,%zu*%lu,) [nobufdev] failed",
npages, (unsigned long)PAGE_SIZE);
goto out;
}
/*
* Touch each page in turn to force them to be un-CoWed, in case a
* fork happened in another thread at an inopportune moment
* above. The madvise() will prevent any subsequent fork calls from
* causing the same problem.
*/
for ( i = 0; i < npages ; i++ )
{
char *c = (char *)p + (i*PAGE_SIZE);
*c = 0;
}
return p;
out:
saved_errno = errno;
(void)munmap(p, size);
errno = saved_errno;
return NULL;
}
void *osdep_alloc_pages(xencall_handle *xcall, size_t npages)
{
void *p;
if ( xcall->buf_fd >= 0 )
p = alloc_pages_bufdev(xcall, npages);
else
p = alloc_pages_nobufdev(xcall, npages);
return p;
}
void osdep_free_pages(xencall_handle *xcall, void *ptr, size_t npages)
{
int saved_errno = errno;
if ( xcall->buf_fd < 0 )
{
/* Recover the VMA flags. Maybe it's not necessary */
madvise(ptr, npages * PAGE_SIZE, MADV_DOFORK);
}
munmap(ptr, npages * PAGE_SIZE);
/* We MUST propagate the hypercall errno, not unmap call's. */
errno = saved_errno;
}
int xencall_buffers_never_fault(xencall_handle *xcall)
{
return xcall->buf_fd >= 0;
}
/*
* Local variables:
* mode: C
* c-file-style: "BSD"
* c-basic-offset: 4
* tab-width: 4
* indent-tabs-mode: nil
* End:
*/
|