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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
|
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <ion/ion.h>
#include <linux/ion.h>
size_t len = 1024*1024, align = 0;
int prot = PROT_READ | PROT_WRITE;
int map_flags = MAP_SHARED;
int alloc_flags = 0;
int heap_mask = 1;
int test = -1;
size_t stride;
int _ion_alloc_test(int *fd, struct ion_handle **handle)
{
int ret;
*fd = ion_open();
if (*fd < 0)
return *fd;
ret = ion_alloc(*fd, len, align, heap_mask, alloc_flags, handle);
if (ret)
printf("%s failed: %s\n", __func__, strerror(ret));
return ret;
}
void ion_alloc_test()
{
int fd, ret;
struct ion_handle *handle;
if(_ion_alloc_test(&fd, &handle))
return;
ret = ion_free(fd, handle);
if (ret) {
printf("%s failed: %s %p\n", __func__, strerror(ret), handle);
return;
}
ion_close(fd);
printf("ion alloc test: passed\n");
}
void ion_map_test()
{
int fd, map_fd, ret;
size_t i;
struct ion_handle *handle;
unsigned char *ptr;
if(_ion_alloc_test(&fd, &handle))
return;
ret = ion_map(fd, handle, len, prot, map_flags, 0, &ptr, &map_fd);
if (ret)
return;
for (i = 0; i < len; i++) {
ptr[i] = (unsigned char)i;
}
for (i = 0; i < len; i++)
if (ptr[i] != (unsigned char)i)
printf("%s failed wrote %zu read %d from mapped "
"memory\n", __func__, i, ptr[i]);
/* clean up properly */
ret = ion_free(fd, handle);
ion_close(fd);
munmap(ptr, len);
close(map_fd);
_ion_alloc_test(&fd, &handle);
close(fd);
#if 0
munmap(ptr, len);
close(map_fd);
ion_close(fd);
_ion_alloc_test(len, align, flags, &fd, &handle);
close(map_fd);
ret = ion_map(fd, handle, len, prot, flags, 0, &ptr, &map_fd);
/* don't clean up */
#endif
}
void ion_share_test()
{
struct ion_handle *handle;
int sd[2];
int num_fd = 1;
struct iovec count_vec = {
.iov_base = &num_fd,
.iov_len = sizeof num_fd,
};
char buf[CMSG_SPACE(sizeof(int))];
socketpair(AF_UNIX, SOCK_STREAM, 0, sd);
if (fork()) {
struct msghdr msg = {
.msg_control = buf,
.msg_controllen = sizeof buf,
.msg_iov = &count_vec,
.msg_iovlen = 1,
};
struct cmsghdr *cmsg;
int fd, share_fd, ret;
char *ptr;
/* parent */
if(_ion_alloc_test(&fd, &handle))
return;
ret = ion_share(fd, handle, &share_fd);
if (ret)
printf("share failed %s\n", strerror(errno));
ptr = mmap(NULL, len, prot, map_flags, share_fd, 0);
if (ptr == MAP_FAILED) {
return;
}
strcpy(ptr, "master");
cmsg = CMSG_FIRSTHDR(&msg);
cmsg->cmsg_level = SOL_SOCKET;
cmsg->cmsg_type = SCM_RIGHTS;
cmsg->cmsg_len = CMSG_LEN(sizeof(int));
*(int *)CMSG_DATA(cmsg) = share_fd;
/* send the fd */
printf("master? [%10s] should be [master]\n", ptr);
printf("master sending msg 1\n");
sendmsg(sd[0], &msg, 0);
if (recvmsg(sd[0], &msg, 0) < 0)
perror("master recv msg 2");
printf("master? [%10s] should be [child]\n", ptr);
/* send ping */
sendmsg(sd[0], &msg, 0);
printf("master->master? [%10s]\n", ptr);
if (recvmsg(sd[0], &msg, 0) < 0)
perror("master recv 1");
} else {
struct msghdr msg;
struct cmsghdr *cmsg;
char* ptr;
int fd, recv_fd;
char* child_buf[100];
/* child */
struct iovec count_vec = {
.iov_base = child_buf,
.iov_len = sizeof child_buf,
};
struct msghdr child_msg = {
.msg_control = buf,
.msg_controllen = sizeof buf,
.msg_iov = &count_vec,
.msg_iovlen = 1,
};
if (recvmsg(sd[1], &child_msg, 0) < 0)
perror("child recv msg 1");
cmsg = CMSG_FIRSTHDR(&child_msg);
if (cmsg == NULL) {
printf("no cmsg rcvd in child");
return;
}
recv_fd = *(int*)CMSG_DATA(cmsg);
if (recv_fd < 0) {
printf("could not get recv_fd from socket");
return;
}
printf("child %d\n", recv_fd);
fd = ion_open();
ptr = mmap(NULL, len, prot, map_flags, recv_fd, 0);
if (ptr == MAP_FAILED) {
return;
}
printf("child? [%10s] should be [master]\n", ptr);
strcpy(ptr, "child");
printf("child sending msg 2\n");
sendmsg(sd[1], &child_msg, 0);
}
}
int main(int argc, char* argv[]) {
int c;
enum tests {
ALLOC_TEST = 0, MAP_TEST, SHARE_TEST,
};
while (1) {
static struct option opts[] = {
{"alloc", no_argument, 0, 'a'},
{"alloc_flags", required_argument, 0, 'f'},
{"heap_mask", required_argument, 0, 'h'},
{"map", no_argument, 0, 'm'},
{"share", no_argument, 0, 's'},
{"len", required_argument, 0, 'l'},
{"align", required_argument, 0, 'g'},
{"map_flags", required_argument, 0, 'z'},
{"prot", required_argument, 0, 'p'},
};
int i = 0;
c = getopt_long(argc, argv, "af:h:l:mr:st", opts, &i);
if (c == -1)
break;
switch (c) {
case 'l':
len = atol(optarg);
break;
case 'g':
align = atol(optarg);
break;
case 'z':
map_flags = 0;
map_flags |= strstr(optarg, "PROT_EXEC") ?
PROT_EXEC : 0;
map_flags |= strstr(optarg, "PROT_READ") ?
PROT_READ: 0;
map_flags |= strstr(optarg, "PROT_WRITE") ?
PROT_WRITE: 0;
map_flags |= strstr(optarg, "PROT_NONE") ?
PROT_NONE: 0;
break;
case 'p':
prot = 0;
prot |= strstr(optarg, "MAP_PRIVATE") ?
MAP_PRIVATE : 0;
prot |= strstr(optarg, "MAP_SHARED") ?
MAP_PRIVATE : 0;
break;
case 'f':
alloc_flags = atol(optarg);
break;
case 'h':
heap_mask = atol(optarg);
break;
case 'a':
test = ALLOC_TEST;
break;
case 'm':
test = MAP_TEST;
break;
case 's':
test = SHARE_TEST;
break;
}
}
printf("test %d, len %zu, align %zu, map_flags %d, prot %d, heap_mask %d,"
" alloc_flags %d\n", test, len, align, map_flags, prot,
heap_mask, alloc_flags);
switch (test) {
case ALLOC_TEST:
ion_alloc_test();
break;
case MAP_TEST:
ion_map_test();
break;
case SHARE_TEST:
ion_share_test();
break;
default:
printf("must specify a test (alloc, map, share)\n");
}
return 0;
}
|