Colin Cross | 735ead6 | 2013-12-18 15:09:04 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
Rebecca Schultz Zavin | afd9123 | 2012-02-22 16:36:55 -0800 | [diff] [blame] | 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> |
| 31 | #include <linux/ion.h> |
Rebecca Schultz Zavin | afd9123 | 2012-02-22 16:36:55 -0800 | [diff] [blame] | 32 | |
| 33 | size_t len = 1024*1024, align = 0; |
| 34 | int prot = PROT_READ | PROT_WRITE; |
| 35 | int map_flags = MAP_SHARED; |
| 36 | int alloc_flags = 0; |
Rebecca Schultz Zavin | a50fd55 | 2012-06-11 15:12:37 -0700 | [diff] [blame] | 37 | int heap_mask = 1; |
Rebecca Schultz Zavin | afd9123 | 2012-02-22 16:36:55 -0800 | [diff] [blame] | 38 | int test = -1; |
Rebecca Schultz Zavin | afd9123 | 2012-02-22 16:36:55 -0800 | [diff] [blame] | 39 | size_t stride; |
| 40 | |
Rom Lemarchand | 969eac8 | 2013-10-21 15:19:56 -0700 | [diff] [blame] | 41 | int _ion_alloc_test(int *fd, ion_user_handle_t *handle) |
Rebecca Schultz Zavin | afd9123 | 2012-02-22 16:36:55 -0800 | [diff] [blame] | 42 | { |
Colin Cross | 92d7ca6 | 2013-11-08 19:04:18 -0800 | [diff] [blame] | 43 | int ret; |
Rebecca Schultz Zavin | afd9123 | 2012-02-22 16:36:55 -0800 | [diff] [blame] | 44 | |
Colin Cross | 92d7ca6 | 2013-11-08 19:04:18 -0800 | [diff] [blame] | 45 | *fd = ion_open(); |
| 46 | if (*fd < 0) |
| 47 | return *fd; |
Rebecca Schultz Zavin | afd9123 | 2012-02-22 16:36:55 -0800 | [diff] [blame] | 48 | |
Colin Cross | 92d7ca6 | 2013-11-08 19:04:18 -0800 | [diff] [blame] | 49 | ret = ion_alloc(*fd, len, align, heap_mask, alloc_flags, handle); |
Rebecca Schultz Zavin | afd9123 | 2012-02-22 16:36:55 -0800 | [diff] [blame] | 50 | |
Colin Cross | 92d7ca6 | 2013-11-08 19:04:18 -0800 | [diff] [blame] | 51 | if (ret) |
| 52 | printf("%s failed: %s\n", __func__, strerror(ret)); |
| 53 | return ret; |
Rebecca Schultz Zavin | afd9123 | 2012-02-22 16:36:55 -0800 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | void ion_alloc_test() |
| 57 | { |
Colin Cross | 92d7ca6 | 2013-11-08 19:04:18 -0800 | [diff] [blame] | 58 | int fd, ret; |
| 59 | ion_user_handle_t handle; |
Rebecca Schultz Zavin | afd9123 | 2012-02-22 16:36:55 -0800 | [diff] [blame] | 60 | |
Colin Cross | 92d7ca6 | 2013-11-08 19:04:18 -0800 | [diff] [blame] | 61 | if(_ion_alloc_test(&fd, &handle)) |
| 62 | return; |
Rebecca Schultz Zavin | afd9123 | 2012-02-22 16:36:55 -0800 | [diff] [blame] | 63 | |
Colin Cross | 92d7ca6 | 2013-11-08 19:04:18 -0800 | [diff] [blame] | 64 | ret = ion_free(fd, handle); |
| 65 | if (ret) { |
Elliott Hughes | ccecf14 | 2014-01-16 10:53:11 -0800 | [diff] [blame] | 66 | printf("%s failed: %s %d\n", __func__, strerror(ret), handle); |
Colin Cross | 92d7ca6 | 2013-11-08 19:04:18 -0800 | [diff] [blame] | 67 | return; |
| 68 | } |
| 69 | ion_close(fd); |
| 70 | printf("ion alloc test: passed\n"); |
Rebecca Schultz Zavin | afd9123 | 2012-02-22 16:36:55 -0800 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | void ion_map_test() |
| 74 | { |
Colin Cross | 92d7ca6 | 2013-11-08 19:04:18 -0800 | [diff] [blame] | 75 | int fd, map_fd, ret; |
| 76 | size_t i; |
| 77 | ion_user_handle_t handle; |
| 78 | unsigned char *ptr; |
Rebecca Schultz Zavin | afd9123 | 2012-02-22 16:36:55 -0800 | [diff] [blame] | 79 | |
Colin Cross | 92d7ca6 | 2013-11-08 19:04:18 -0800 | [diff] [blame] | 80 | if(_ion_alloc_test(&fd, &handle)) |
| 81 | return; |
Rebecca Schultz Zavin | afd9123 | 2012-02-22 16:36:55 -0800 | [diff] [blame] | 82 | |
Colin Cross | 92d7ca6 | 2013-11-08 19:04:18 -0800 | [diff] [blame] | 83 | ret = ion_map(fd, handle, len, prot, map_flags, 0, &ptr, &map_fd); |
| 84 | if (ret) |
| 85 | return; |
Rebecca Schultz Zavin | afd9123 | 2012-02-22 16:36:55 -0800 | [diff] [blame] | 86 | |
Colin Cross | 92d7ca6 | 2013-11-08 19:04:18 -0800 | [diff] [blame] | 87 | for (i = 0; i < len; i++) { |
| 88 | ptr[i] = (unsigned char)i; |
| 89 | } |
| 90 | for (i = 0; i < len; i++) |
| 91 | if (ptr[i] != (unsigned char)i) |
| 92 | printf("%s failed wrote %zu read %d from mapped " |
| 93 | "memory\n", __func__, i, ptr[i]); |
| 94 | /* clean up properly */ |
| 95 | ret = ion_free(fd, handle); |
| 96 | ion_close(fd); |
| 97 | munmap(ptr, len); |
| 98 | close(map_fd); |
Rebecca Schultz Zavin | afd9123 | 2012-02-22 16:36:55 -0800 | [diff] [blame] | 99 | |
Colin Cross | 92d7ca6 | 2013-11-08 19:04:18 -0800 | [diff] [blame] | 100 | _ion_alloc_test(&fd, &handle); |
| 101 | close(fd); |
Rebecca Schultz Zavin | afd9123 | 2012-02-22 16:36:55 -0800 | [diff] [blame] | 102 | |
| 103 | #if 0 |
Colin Cross | 92d7ca6 | 2013-11-08 19:04:18 -0800 | [diff] [blame] | 104 | munmap(ptr, len); |
| 105 | close(map_fd); |
| 106 | ion_close(fd); |
Rebecca Schultz Zavin | afd9123 | 2012-02-22 16:36:55 -0800 | [diff] [blame] | 107 | |
Colin Cross | 92d7ca6 | 2013-11-08 19:04:18 -0800 | [diff] [blame] | 108 | _ion_alloc_test(len, align, flags, &fd, &handle); |
| 109 | close(map_fd); |
| 110 | ret = ion_map(fd, handle, len, prot, flags, 0, &ptr, &map_fd); |
| 111 | /* don't clean up */ |
Rebecca Schultz Zavin | afd9123 | 2012-02-22 16:36:55 -0800 | [diff] [blame] | 112 | #endif |
| 113 | } |
| 114 | |
| 115 | void ion_share_test() |
| 116 | |
| 117 | { |
Colin Cross | 92d7ca6 | 2013-11-08 19:04:18 -0800 | [diff] [blame] | 118 | ion_user_handle_t handle; |
| 119 | int sd[2]; |
| 120 | int num_fd = 1; |
| 121 | struct iovec count_vec = { |
| 122 | .iov_base = &num_fd, |
| 123 | .iov_len = sizeof num_fd, |
| 124 | }; |
| 125 | char buf[CMSG_SPACE(sizeof(int))]; |
| 126 | socketpair(AF_UNIX, SOCK_STREAM, 0, sd); |
| 127 | if (fork()) { |
| 128 | struct msghdr msg = { |
| 129 | .msg_control = buf, |
| 130 | .msg_controllen = sizeof buf, |
| 131 | .msg_iov = &count_vec, |
| 132 | .msg_iovlen = 1, |
| 133 | }; |
Rebecca Schultz Zavin | afd9123 | 2012-02-22 16:36:55 -0800 | [diff] [blame] | 134 | |
Colin Cross | 92d7ca6 | 2013-11-08 19:04:18 -0800 | [diff] [blame] | 135 | struct cmsghdr *cmsg; |
| 136 | int fd, share_fd, ret; |
| 137 | char *ptr; |
| 138 | /* parent */ |
| 139 | if(_ion_alloc_test(&fd, &handle)) |
| 140 | return; |
| 141 | ret = ion_share(fd, handle, &share_fd); |
| 142 | if (ret) |
| 143 | printf("share failed %s\n", strerror(errno)); |
| 144 | ptr = mmap(NULL, len, prot, map_flags, share_fd, 0); |
| 145 | if (ptr == MAP_FAILED) { |
| 146 | return; |
| 147 | } |
| 148 | strcpy(ptr, "master"); |
| 149 | cmsg = CMSG_FIRSTHDR(&msg); |
| 150 | cmsg->cmsg_level = SOL_SOCKET; |
| 151 | cmsg->cmsg_type = SCM_RIGHTS; |
| 152 | cmsg->cmsg_len = CMSG_LEN(sizeof(int)); |
| 153 | *(int *)CMSG_DATA(cmsg) = share_fd; |
| 154 | /* send the fd */ |
| 155 | printf("master? [%10s] should be [master]\n", ptr); |
| 156 | printf("master sending msg 1\n"); |
| 157 | sendmsg(sd[0], &msg, 0); |
| 158 | if (recvmsg(sd[0], &msg, 0) < 0) |
| 159 | perror("master recv msg 2"); |
| 160 | printf("master? [%10s] should be [child]\n", ptr); |
Rebecca Schultz Zavin | afd9123 | 2012-02-22 16:36:55 -0800 | [diff] [blame] | 161 | |
Colin Cross | 92d7ca6 | 2013-11-08 19:04:18 -0800 | [diff] [blame] | 162 | /* send ping */ |
| 163 | sendmsg(sd[0], &msg, 0); |
| 164 | printf("master->master? [%10s]\n", ptr); |
| 165 | if (recvmsg(sd[0], &msg, 0) < 0) |
| 166 | perror("master recv 1"); |
Andreas Gampe | f0a54c1 | 2014-11-24 19:31:40 -0800 | [diff] [blame] | 167 | close(fd); |
| 168 | _exit(0); |
Colin Cross | 92d7ca6 | 2013-11-08 19:04:18 -0800 | [diff] [blame] | 169 | } else { |
Colin Cross | 92d7ca6 | 2013-11-08 19:04:18 -0800 | [diff] [blame] | 170 | struct cmsghdr *cmsg; |
| 171 | char* ptr; |
| 172 | int fd, recv_fd; |
| 173 | char* child_buf[100]; |
| 174 | /* child */ |
| 175 | struct iovec count_vec = { |
| 176 | .iov_base = child_buf, |
| 177 | .iov_len = sizeof child_buf, |
| 178 | }; |
Rebecca Schultz Zavin | afd9123 | 2012-02-22 16:36:55 -0800 | [diff] [blame] | 179 | |
Colin Cross | 92d7ca6 | 2013-11-08 19:04:18 -0800 | [diff] [blame] | 180 | struct msghdr child_msg = { |
| 181 | .msg_control = buf, |
| 182 | .msg_controllen = sizeof buf, |
| 183 | .msg_iov = &count_vec, |
| 184 | .msg_iovlen = 1, |
| 185 | }; |
Rebecca Schultz Zavin | afd9123 | 2012-02-22 16:36:55 -0800 | [diff] [blame] | 186 | |
Colin Cross | 92d7ca6 | 2013-11-08 19:04:18 -0800 | [diff] [blame] | 187 | if (recvmsg(sd[1], &child_msg, 0) < 0) |
| 188 | perror("child recv msg 1"); |
| 189 | cmsg = CMSG_FIRSTHDR(&child_msg); |
| 190 | if (cmsg == NULL) { |
| 191 | printf("no cmsg rcvd in child"); |
| 192 | return; |
| 193 | } |
| 194 | recv_fd = *(int*)CMSG_DATA(cmsg); |
| 195 | if (recv_fd < 0) { |
| 196 | printf("could not get recv_fd from socket"); |
| 197 | return; |
| 198 | } |
| 199 | printf("child %d\n", recv_fd); |
| 200 | fd = ion_open(); |
| 201 | ptr = mmap(NULL, len, prot, map_flags, recv_fd, 0); |
| 202 | if (ptr == MAP_FAILED) { |
| 203 | return; |
| 204 | } |
| 205 | printf("child? [%10s] should be [master]\n", ptr); |
| 206 | strcpy(ptr, "child"); |
| 207 | printf("child sending msg 2\n"); |
| 208 | sendmsg(sd[1], &child_msg, 0); |
Andreas Gampe | f0a54c1 | 2014-11-24 19:31:40 -0800 | [diff] [blame] | 209 | close(fd); |
Colin Cross | 92d7ca6 | 2013-11-08 19:04:18 -0800 | [diff] [blame] | 210 | } |
Rebecca Schultz Zavin | afd9123 | 2012-02-22 16:36:55 -0800 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | int main(int argc, char* argv[]) { |
Colin Cross | 92d7ca6 | 2013-11-08 19:04:18 -0800 | [diff] [blame] | 214 | int c; |
| 215 | enum tests { |
| 216 | ALLOC_TEST = 0, MAP_TEST, SHARE_TEST, |
| 217 | }; |
Rebecca Schultz Zavin | afd9123 | 2012-02-22 16:36:55 -0800 | [diff] [blame] | 218 | |
Colin Cross | 92d7ca6 | 2013-11-08 19:04:18 -0800 | [diff] [blame] | 219 | while (1) { |
| 220 | static struct option opts[] = { |
| 221 | {"alloc", no_argument, 0, 'a'}, |
| 222 | {"alloc_flags", required_argument, 0, 'f'}, |
| 223 | {"heap_mask", required_argument, 0, 'h'}, |
| 224 | {"map", no_argument, 0, 'm'}, |
| 225 | {"share", no_argument, 0, 's'}, |
| 226 | {"len", required_argument, 0, 'l'}, |
| 227 | {"align", required_argument, 0, 'g'}, |
| 228 | {"map_flags", required_argument, 0, 'z'}, |
| 229 | {"prot", required_argument, 0, 'p'}, |
| 230 | }; |
| 231 | int i = 0; |
| 232 | c = getopt_long(argc, argv, "af:h:l:mr:st", opts, &i); |
| 233 | if (c == -1) |
| 234 | break; |
Rebecca Schultz Zavin | afd9123 | 2012-02-22 16:36:55 -0800 | [diff] [blame] | 235 | |
Colin Cross | 92d7ca6 | 2013-11-08 19:04:18 -0800 | [diff] [blame] | 236 | switch (c) { |
| 237 | case 'l': |
| 238 | len = atol(optarg); |
| 239 | break; |
| 240 | case 'g': |
| 241 | align = atol(optarg); |
| 242 | break; |
| 243 | case 'z': |
| 244 | map_flags = 0; |
| 245 | map_flags |= strstr(optarg, "PROT_EXEC") ? PROT_EXEC : 0; |
| 246 | map_flags |= strstr(optarg, "PROT_READ") ? PROT_READ: 0; |
| 247 | map_flags |= strstr(optarg, "PROT_WRITE") ? PROT_WRITE: 0; |
| 248 | map_flags |= strstr(optarg, "PROT_NONE") ? PROT_NONE: 0; |
| 249 | break; |
| 250 | case 'p': |
| 251 | prot = 0; |
| 252 | prot |= strstr(optarg, "MAP_PRIVATE") ? MAP_PRIVATE : 0; |
Steven Moreland | b81e185 | 2018-02-14 13:11:11 -0800 | [diff] [blame^] | 253 | prot |= strstr(optarg, "MAP_SHARED") ? MAP_SHARED : 0; |
Colin Cross | 92d7ca6 | 2013-11-08 19:04:18 -0800 | [diff] [blame] | 254 | break; |
| 255 | case 'f': |
| 256 | alloc_flags = atol(optarg); |
| 257 | break; |
| 258 | case 'h': |
| 259 | heap_mask = atol(optarg); |
| 260 | break; |
| 261 | case 'a': |
| 262 | test = ALLOC_TEST; |
| 263 | break; |
| 264 | case 'm': |
| 265 | test = MAP_TEST; |
| 266 | break; |
| 267 | case 's': |
| 268 | test = SHARE_TEST; |
| 269 | break; |
| 270 | } |
| 271 | } |
| 272 | printf("test %d, len %zu, align %zu, map_flags %d, prot %d, heap_mask %d," |
| 273 | " alloc_flags %d\n", test, len, align, map_flags, prot, |
| 274 | heap_mask, alloc_flags); |
| 275 | switch (test) { |
| 276 | case ALLOC_TEST: |
| 277 | ion_alloc_test(); |
| 278 | break; |
| 279 | case MAP_TEST: |
| 280 | ion_map_test(); |
| 281 | break; |
| 282 | case SHARE_TEST: |
| 283 | ion_share_test(); |
| 284 | break; |
| 285 | default: |
| 286 | printf("must specify a test (alloc, map, share)\n"); |
| 287 | } |
| 288 | return 0; |
Rebecca Schultz Zavin | afd9123 | 2012-02-22 16:36:55 -0800 | [diff] [blame] | 289 | } |