blob: b2c46ed94bbd3836049846318b420e11bb566923 [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
Cao Jian510569c2019-08-16 14:01:44 +080042int _ionmem_alloc_test(void)
Dezhi Kong528d8cc2019-07-17 21:09:07 +080043{
Cao Jian510569c2019-08-16 14:01:44 +080044 int ret, ion_fd = -1;
Dezhi Kong528d8cc2019-07-17 21:09:07 +080045 IONMEM_AllocParams params;
46
Cao Jian510569c2019-08-16 14:01:44 +080047 ion_fd = ion_mem_init();
48 if (ion_fd < 0) {
49 printf("ion_mem_init failed\n");
50 return ion_fd;
51 }
52 ret = ion_mem_alloc(ion_fd, len, &params, alloc_flags);
Dezhi Kong528d8cc2019-07-17 21:09:07 +080053 if (ret)
54 printf("%s failed: %s\n", __func__, strerror(ret));
55 else
56 printf("%s: passed\n", __func__);
57 return ret;
58}
59
Dezhi Kong288546e2019-05-16 17:02:38 +080060int _ion_alloc_test(int *fd, ion_user_handle_t *handle)
61{
62 int ret;
Dezhi Kong528d8cc2019-07-17 21:09:07 +080063 int legacy_ion = 0;
Dezhi Kong288546e2019-05-16 17:02:38 +080064
65 *fd = ion_open();
66 if (*fd < 0)
67 return *fd;
68
Dezhi Kong528d8cc2019-07-17 21:09:07 +080069 legacy_ion = ion_is_legacy(*fd);
70 printf("legacy_ion=%d\n", legacy_ion);
71 if (legacy_ion)
72 ret = ion_alloc(*fd, len, align, heap_mask, alloc_flags, handle);
73 else
74 ret = ion_alloc_fd(*fd, len, align, heap_mask, alloc_flags, handle);
Dezhi Kong288546e2019-05-16 17:02:38 +080075
76 if (ret)
77 printf("%s failed: %s\n", __func__, strerror(ret));
78 return ret;
79}
80
81void ion_alloc_test()
82{
Dezhi Kong528d8cc2019-07-17 21:09:07 +080083 int fd, ret = 0;
Dezhi Kong288546e2019-05-16 17:02:38 +080084 ion_user_handle_t handle;
85
86 if (_ion_alloc_test(&fd, &handle))
87 return;
88
Dezhi Kong528d8cc2019-07-17 21:09:07 +080089 if (ion_is_legacy(fd))
90 ret = ion_free(fd, handle);
Dezhi Kong288546e2019-05-16 17:02:38 +080091 if (ret) {
92 printf("%s failed: %s %d\n", __func__, strerror(ret), handle);
93 return;
94 }
95 ion_close(fd);
96 printf("ion alloc test: passed\n");
97}
98
99void ion_map_test()
100{
101 int fd, map_fd, ret;
102 size_t i;
103 ion_user_handle_t handle;
104 unsigned char *ptr;
105
106 if (_ion_alloc_test(&fd, &handle))
107 return;
108
109 ret = ion_map(fd, handle, len, prot, map_flags, 0, &ptr, &map_fd);
110 if (ret)
111 return;
112
113 for (i = 0; i < len; i++) {
114 ptr[i] = (unsigned char)i;
115 }
116 for (i = 0; i < len; i++)
117 if (ptr[i] != (unsigned char)i)
118 printf("%s failed wrote %zu read %d from mapped "
119 "memory\n", __func__, i, ptr[i]);
120 /* clean up properly */
121 ret = ion_free(fd, handle);
122 ion_close(fd);
123 munmap(ptr, len);
124 close(map_fd);
125
126 _ion_alloc_test(&fd, &handle);
127 close(fd);
128
129#if 0
130 munmap(ptr, len);
131 close(map_fd);
132 ion_close(fd);
133
134 _ion_alloc_test(len, align, flags, &fd, &handle);
135 close(map_fd);
136 ret = ion_map(fd, handle, len, prot, flags, 0, &ptr, &map_fd);
137 /* don't clean up */
138#endif
139}
140
141void ion_share_test()
142
143{
144 ion_user_handle_t handle;
145 int sd[2];
146 int num_fd = 1;
147 struct iovec count_vec = {
148 .iov_base = &num_fd,
149 .iov_len = sizeof num_fd,
150 };
151 char buf[CMSG_SPACE(sizeof(int))];
152 socketpair(AF_UNIX, SOCK_STREAM, 0, sd);
153 if (fork()) {
154 struct msghdr msg = {
155 .msg_control = buf,
156 .msg_controllen = sizeof buf,
157 .msg_iov = &count_vec,
158 .msg_iovlen = 1,
159 };
160
161 struct cmsghdr *cmsg;
162 int fd, share_fd, ret;
163 char *ptr;
164 /* parent */
165 if (_ion_alloc_test(&fd, &handle))
166 return;
167 ret = ion_share(fd, handle, &share_fd);
168 if (ret)
169 printf("share failed %s\n", strerror(errno));
170 ptr = mmap(NULL, len, prot, map_flags, share_fd, 0);
171 if (ptr == MAP_FAILED) {
172 return;
173 }
174 strcpy(ptr, "master");
175 cmsg = CMSG_FIRSTHDR(&msg);
176 cmsg->cmsg_level = SOL_SOCKET;
177 cmsg->cmsg_type = SCM_RIGHTS;
178 cmsg->cmsg_len = CMSG_LEN(sizeof(int));
179 *(int *)CMSG_DATA(cmsg) = share_fd;
180 /* send the fd */
181 printf("master? [%10s] should be [master]\n", ptr);
182 printf("master sending msg 1\n");
183 sendmsg(sd[0], &msg, 0);
184 if (recvmsg(sd[0], &msg, 0) < 0)
185 perror("master recv msg 2");
186 printf("master? [%10s] should be [child]\n", ptr);
187
188 /* send ping */
189 sendmsg(sd[0], &msg, 0);
190 printf("master->master? [%10s]\n", ptr);
191 if (recvmsg(sd[0], &msg, 0) < 0)
192 perror("master recv 1");
193 close(fd);
194 _exit(0);
195 } else {
196 struct cmsghdr *cmsg;
197 char* ptr;
198 int fd, recv_fd;
199 char* child_buf[100];
200 /* child */
201 struct iovec count_vec = {
202 .iov_base = child_buf,
203 .iov_len = sizeof child_buf,
204 };
205
206 struct msghdr child_msg = {
207 .msg_control = buf,
208 .msg_controllen = sizeof buf,
209 .msg_iov = &count_vec,
210 .msg_iovlen = 1,
211 };
212
213 if (recvmsg(sd[1], &child_msg, 0) < 0)
214 perror("child recv msg 1");
215 cmsg = CMSG_FIRSTHDR(&child_msg);
216 if (cmsg == NULL) {
217 printf("no cmsg rcvd in child");
218 return;
219 }
220 recv_fd = *(int*)CMSG_DATA(cmsg);
221 if (recv_fd < 0) {
222 printf("could not get recv_fd from socket");
223 return;
224 }
225 printf("child %d\n", recv_fd);
226 fd = ion_open();
227 ptr = mmap(NULL, len, prot, map_flags, recv_fd, 0);
228 if (ptr == MAP_FAILED) {
229 return;
230 }
231 printf("child? [%10s] should be [master]\n", ptr);
232 strcpy(ptr, "child");
233 printf("child sending msg 2\n");
234 sendmsg(sd[1], &child_msg, 0);
235 close(fd);
236 }
237}
238
239int main(int argc, char* argv[]) {
240 int c;
241 enum tests {
Cao Jian510569c2019-08-16 14:01:44 +0800242 ALLOC_TEST = 0, MAP_TEST, SHARE_TEST, IONMEM_TEST,
Dezhi Kong288546e2019-05-16 17:02:38 +0800243 };
244
245 while (1) {
246 static struct option opts[] = {
Cao Jian510569c2019-08-16 14:01:44 +0800247 {"ion_mem_alloc", no_argument, 0, 'c'},
Dezhi Kong288546e2019-05-16 17:02:38 +0800248 {"alloc", no_argument, 0, 'a'},
249 {"alloc_flags", required_argument, 0, 'f'},
250 {"heap_mask", required_argument, 0, 'h'},
251 {"map", no_argument, 0, 'm'},
252 {"share", no_argument, 0, 's'},
253 {"len", required_argument, 0, 'l'},
254 {"align", required_argument, 0, 'g'},
255 {"map_flags", required_argument, 0, 'z'},
256 {"prot", required_argument, 0, 'p'},
257 };
258 int i = 0;
Dezhi Kong528d8cc2019-07-17 21:09:07 +0800259 c = getopt_long(argc, argv, "caf:h:l:mr:st", opts, &i);
Dezhi Kong288546e2019-05-16 17:02:38 +0800260 if (c == -1)
261 break;
262
263 switch (c) {
264 case 'l':
265 len = atol(optarg);
266 break;
267 case 'g':
268 align = atol(optarg);
269 break;
270 case 'z':
271 map_flags = 0;
272 map_flags |= strstr(optarg, "PROT_EXEC") ? PROT_EXEC : 0;
273 map_flags |= strstr(optarg, "PROT_READ") ? PROT_READ: 0;
274 map_flags |= strstr(optarg, "PROT_WRITE") ? PROT_WRITE: 0;
275 map_flags |= strstr(optarg, "PROT_NONE") ? PROT_NONE: 0;
276 break;
277 case 'p':
278 prot = 0;
279 prot |= strstr(optarg, "MAP_PRIVATE") ? MAP_PRIVATE : 0;
280 prot |= strstr(optarg, "MAP_SHARED") ? MAP_SHARED : 0;
281 break;
282 case 'f':
283 alloc_flags = atol(optarg);
284 break;
285 case 'h':
286 heap_mask = atol(optarg);
287 break;
288 case 'a':
289 test = ALLOC_TEST;
290 break;
291 case 'm':
292 test = MAP_TEST;
293 break;
294 case 's':
295 test = SHARE_TEST;
296 break;
Dezhi Kong528d8cc2019-07-17 21:09:07 +0800297 case 'c':
Cao Jian510569c2019-08-16 14:01:44 +0800298 test = IONMEM_TEST;
Dezhi Kong528d8cc2019-07-17 21:09:07 +0800299 break;
Dezhi Kong288546e2019-05-16 17:02:38 +0800300 }
301 }
302 printf("test %d, len %zu, align %zu, map_flags %d, prot %d, heap_mask %d,"
303 " alloc_flags %d\n", test, len, align, map_flags, prot,
304 heap_mask, alloc_flags);
305 switch (test) {
306 case ALLOC_TEST:
307 ion_alloc_test();
308 break;
309 case MAP_TEST:
310 ion_map_test();
311 break;
312 case SHARE_TEST:
313 ion_share_test();
314 break;
Cao Jian510569c2019-08-16 14:01:44 +0800315 case IONMEM_TEST:
316 _ionmem_alloc_test();
Dezhi Kong528d8cc2019-07-17 21:09:07 +0800317 break;
Dezhi Kong288546e2019-05-16 17:02:38 +0800318 default:
319 printf("must specify a test (alloc, map, share)\n");
320 }
321 return 0;
322}