blob: 2ba2b06456b6a6a9047faba36a3e90f07b325787 [file] [log] [blame]
Dezhi Kong288546e2019-05-16 17:02:38 +08001/*
2 * Copyright 2013 Google, Inc
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <errno.h>
18#include <fcntl.h>
19#include <getopt.h>
20#include <string.h>
21#include <stdlib.h>
22#include <stdio.h>
23#include <sys/mman.h>
24#include <sys/ioctl.h>
25#include <sys/socket.h>
26#include <sys/stat.h>
27#include <sys/types.h>
28#include <unistd.h>
29
30#include <ion/ion.h>
Dezhi Kong528d8cc2019-07-17 21:09:07 +080031#include <ion/IONmem.h>
Dezhi Kong288546e2019-05-16 17:02:38 +080032#include <linux/ion.h>
33
34size_t len = 1024*1024, align = 0;
35int prot = PROT_READ | PROT_WRITE;
36int map_flags = MAP_SHARED;
37int alloc_flags = 0;
38int heap_mask = 1;
39int test = -1;
40size_t stride;
41
Dezhi Kong528d8cc2019-07-17 21:09:07 +080042int _CMEM_alloc_test(void)
43{
44 int ret;
45 IONMEM_AllocParams params;
46
47 CMEM_init();
48 ret = CMEM_alloc(len, &params, alloc_flags);
49 if (ret)
50 printf("%s failed: %s\n", __func__, strerror(ret));
51 else
52 printf("%s: passed\n", __func__);
53 return ret;
54}
55
Dezhi Kong288546e2019-05-16 17:02:38 +080056int _ion_alloc_test(int *fd, ion_user_handle_t *handle)
57{
58 int ret;
Dezhi Kong528d8cc2019-07-17 21:09:07 +080059 int legacy_ion = 0;
Dezhi Kong288546e2019-05-16 17:02:38 +080060
61 *fd = ion_open();
62 if (*fd < 0)
63 return *fd;
64
Dezhi Kong528d8cc2019-07-17 21:09:07 +080065 legacy_ion = ion_is_legacy(*fd);
66 printf("legacy_ion=%d\n", legacy_ion);
67 if (legacy_ion)
68 ret = ion_alloc(*fd, len, align, heap_mask, alloc_flags, handle);
69 else
70 ret = ion_alloc_fd(*fd, len, align, heap_mask, alloc_flags, handle);
Dezhi Kong288546e2019-05-16 17:02:38 +080071
72 if (ret)
73 printf("%s failed: %s\n", __func__, strerror(ret));
74 return ret;
75}
76
77void ion_alloc_test()
78{
Dezhi Kong528d8cc2019-07-17 21:09:07 +080079 int fd, ret = 0;
Dezhi Kong288546e2019-05-16 17:02:38 +080080 ion_user_handle_t handle;
81
82 if (_ion_alloc_test(&fd, &handle))
83 return;
84
Dezhi Kong528d8cc2019-07-17 21:09:07 +080085 if (ion_is_legacy(fd))
86 ret = ion_free(fd, handle);
Dezhi Kong288546e2019-05-16 17:02:38 +080087 if (ret) {
88 printf("%s failed: %s %d\n", __func__, strerror(ret), handle);
89 return;
90 }
91 ion_close(fd);
92 printf("ion alloc test: passed\n");
93}
94
95void ion_map_test()
96{
97 int fd, map_fd, ret;
98 size_t i;
99 ion_user_handle_t handle;
100 unsigned char *ptr;
101
102 if (_ion_alloc_test(&fd, &handle))
103 return;
104
105 ret = ion_map(fd, handle, len, prot, map_flags, 0, &ptr, &map_fd);
106 if (ret)
107 return;
108
109 for (i = 0; i < len; i++) {
110 ptr[i] = (unsigned char)i;
111 }
112 for (i = 0; i < len; i++)
113 if (ptr[i] != (unsigned char)i)
114 printf("%s failed wrote %zu read %d from mapped "
115 "memory\n", __func__, i, ptr[i]);
116 /* clean up properly */
117 ret = ion_free(fd, handle);
118 ion_close(fd);
119 munmap(ptr, len);
120 close(map_fd);
121
122 _ion_alloc_test(&fd, &handle);
123 close(fd);
124
125#if 0
126 munmap(ptr, len);
127 close(map_fd);
128 ion_close(fd);
129
130 _ion_alloc_test(len, align, flags, &fd, &handle);
131 close(map_fd);
132 ret = ion_map(fd, handle, len, prot, flags, 0, &ptr, &map_fd);
133 /* don't clean up */
134#endif
135}
136
137void ion_share_test()
138
139{
140 ion_user_handle_t handle;
141 int sd[2];
142 int num_fd = 1;
143 struct iovec count_vec = {
144 .iov_base = &num_fd,
145 .iov_len = sizeof num_fd,
146 };
147 char buf[CMSG_SPACE(sizeof(int))];
148 socketpair(AF_UNIX, SOCK_STREAM, 0, sd);
149 if (fork()) {
150 struct msghdr msg = {
151 .msg_control = buf,
152 .msg_controllen = sizeof buf,
153 .msg_iov = &count_vec,
154 .msg_iovlen = 1,
155 };
156
157 struct cmsghdr *cmsg;
158 int fd, share_fd, ret;
159 char *ptr;
160 /* parent */
161 if (_ion_alloc_test(&fd, &handle))
162 return;
163 ret = ion_share(fd, handle, &share_fd);
164 if (ret)
165 printf("share failed %s\n", strerror(errno));
166 ptr = mmap(NULL, len, prot, map_flags, share_fd, 0);
167 if (ptr == MAP_FAILED) {
168 return;
169 }
170 strcpy(ptr, "master");
171 cmsg = CMSG_FIRSTHDR(&msg);
172 cmsg->cmsg_level = SOL_SOCKET;
173 cmsg->cmsg_type = SCM_RIGHTS;
174 cmsg->cmsg_len = CMSG_LEN(sizeof(int));
175 *(int *)CMSG_DATA(cmsg) = share_fd;
176 /* send the fd */
177 printf("master? [%10s] should be [master]\n", ptr);
178 printf("master sending msg 1\n");
179 sendmsg(sd[0], &msg, 0);
180 if (recvmsg(sd[0], &msg, 0) < 0)
181 perror("master recv msg 2");
182 printf("master? [%10s] should be [child]\n", ptr);
183
184 /* send ping */
185 sendmsg(sd[0], &msg, 0);
186 printf("master->master? [%10s]\n", ptr);
187 if (recvmsg(sd[0], &msg, 0) < 0)
188 perror("master recv 1");
189 close(fd);
190 _exit(0);
191 } else {
192 struct cmsghdr *cmsg;
193 char* ptr;
194 int fd, recv_fd;
195 char* child_buf[100];
196 /* child */
197 struct iovec count_vec = {
198 .iov_base = child_buf,
199 .iov_len = sizeof child_buf,
200 };
201
202 struct msghdr child_msg = {
203 .msg_control = buf,
204 .msg_controllen = sizeof buf,
205 .msg_iov = &count_vec,
206 .msg_iovlen = 1,
207 };
208
209 if (recvmsg(sd[1], &child_msg, 0) < 0)
210 perror("child recv msg 1");
211 cmsg = CMSG_FIRSTHDR(&child_msg);
212 if (cmsg == NULL) {
213 printf("no cmsg rcvd in child");
214 return;
215 }
216 recv_fd = *(int*)CMSG_DATA(cmsg);
217 if (recv_fd < 0) {
218 printf("could not get recv_fd from socket");
219 return;
220 }
221 printf("child %d\n", recv_fd);
222 fd = ion_open();
223 ptr = mmap(NULL, len, prot, map_flags, recv_fd, 0);
224 if (ptr == MAP_FAILED) {
225 return;
226 }
227 printf("child? [%10s] should be [master]\n", ptr);
228 strcpy(ptr, "child");
229 printf("child sending msg 2\n");
230 sendmsg(sd[1], &child_msg, 0);
231 close(fd);
232 }
233}
234
235int main(int argc, char* argv[]) {
236 int c;
237 enum tests {
Dezhi Kong528d8cc2019-07-17 21:09:07 +0800238 ALLOC_TEST = 0, MAP_TEST, SHARE_TEST, CMEM_TEST,
Dezhi Kong288546e2019-05-16 17:02:38 +0800239 };
240
241 while (1) {
242 static struct option opts[] = {
Dezhi Kong528d8cc2019-07-17 21:09:07 +0800243 {"CMEM_alloc", no_argument, 0, 'c'},
Dezhi Kong288546e2019-05-16 17:02:38 +0800244 {"alloc", no_argument, 0, 'a'},
245 {"alloc_flags", required_argument, 0, 'f'},
246 {"heap_mask", required_argument, 0, 'h'},
247 {"map", no_argument, 0, 'm'},
248 {"share", no_argument, 0, 's'},
249 {"len", required_argument, 0, 'l'},
250 {"align", required_argument, 0, 'g'},
251 {"map_flags", required_argument, 0, 'z'},
252 {"prot", required_argument, 0, 'p'},
253 };
254 int i = 0;
Dezhi Kong528d8cc2019-07-17 21:09:07 +0800255 c = getopt_long(argc, argv, "caf:h:l:mr:st", opts, &i);
Dezhi Kong288546e2019-05-16 17:02:38 +0800256 if (c == -1)
257 break;
258
259 switch (c) {
260 case 'l':
261 len = atol(optarg);
262 break;
263 case 'g':
264 align = atol(optarg);
265 break;
266 case 'z':
267 map_flags = 0;
268 map_flags |= strstr(optarg, "PROT_EXEC") ? PROT_EXEC : 0;
269 map_flags |= strstr(optarg, "PROT_READ") ? PROT_READ: 0;
270 map_flags |= strstr(optarg, "PROT_WRITE") ? PROT_WRITE: 0;
271 map_flags |= strstr(optarg, "PROT_NONE") ? PROT_NONE: 0;
272 break;
273 case 'p':
274 prot = 0;
275 prot |= strstr(optarg, "MAP_PRIVATE") ? MAP_PRIVATE : 0;
276 prot |= strstr(optarg, "MAP_SHARED") ? MAP_SHARED : 0;
277 break;
278 case 'f':
279 alloc_flags = atol(optarg);
280 break;
281 case 'h':
282 heap_mask = atol(optarg);
283 break;
284 case 'a':
285 test = ALLOC_TEST;
286 break;
287 case 'm':
288 test = MAP_TEST;
289 break;
290 case 's':
291 test = SHARE_TEST;
292 break;
Dezhi Kong528d8cc2019-07-17 21:09:07 +0800293 case 'c':
294 test = CMEM_TEST;
295 break;
Dezhi Kong288546e2019-05-16 17:02:38 +0800296 }
297 }
298 printf("test %d, len %zu, align %zu, map_flags %d, prot %d, heap_mask %d,"
299 " alloc_flags %d\n", test, len, align, map_flags, prot,
300 heap_mask, alloc_flags);
301 switch (test) {
302 case ALLOC_TEST:
303 ion_alloc_test();
304 break;
305 case MAP_TEST:
306 ion_map_test();
307 break;
308 case SHARE_TEST:
309 ion_share_test();
310 break;
Dezhi Kong528d8cc2019-07-17 21:09:07 +0800311 case CMEM_TEST:
312 _CMEM_alloc_test();
313 break;
Dezhi Kong288546e2019-05-16 17:02:38 +0800314 default:
315 printf("must specify a test (alloc, map, share)\n");
316 }
317 return 0;
318}