0001
0002 #define _GNU_SOURCE
0003 #define __EXPORTED_HEADERS__
0004
0005 #include <stdio.h>
0006 #include <stdlib.h>
0007 #include <unistd.h>
0008 #include <string.h>
0009 #include <errno.h>
0010 #include <fcntl.h>
0011 #include <malloc.h>
0012
0013 #include <sys/ioctl.h>
0014 #include <sys/syscall.h>
0015 #include <linux/memfd.h>
0016 #include <linux/udmabuf.h>
0017
0018 #define TEST_PREFIX "drivers/dma-buf/udmabuf"
0019 #define NUM_PAGES 4
0020
0021 static int memfd_create(const char *name, unsigned int flags)
0022 {
0023 return syscall(__NR_memfd_create, name, flags);
0024 }
0025
0026 int main(int argc, char *argv[])
0027 {
0028 struct udmabuf_create create;
0029 int devfd, memfd, buf, ret;
0030 off_t size;
0031 void *mem;
0032
0033 devfd = open("/dev/udmabuf", O_RDWR);
0034 if (devfd < 0) {
0035 printf("%s: [skip,no-udmabuf: Unable to access DMA buffer device file]\n",
0036 TEST_PREFIX);
0037 exit(77);
0038 }
0039
0040 memfd = memfd_create("udmabuf-test", MFD_ALLOW_SEALING);
0041 if (memfd < 0) {
0042 printf("%s: [skip,no-memfd]\n", TEST_PREFIX);
0043 exit(77);
0044 }
0045
0046 ret = fcntl(memfd, F_ADD_SEALS, F_SEAL_SHRINK);
0047 if (ret < 0) {
0048 printf("%s: [skip,fcntl-add-seals]\n", TEST_PREFIX);
0049 exit(77);
0050 }
0051
0052
0053 size = getpagesize() * NUM_PAGES;
0054 ret = ftruncate(memfd, size);
0055 if (ret == -1) {
0056 printf("%s: [FAIL,memfd-truncate]\n", TEST_PREFIX);
0057 exit(1);
0058 }
0059
0060 memset(&create, 0, sizeof(create));
0061
0062
0063 create.memfd = memfd;
0064 create.offset = getpagesize()/2;
0065 create.size = getpagesize();
0066 buf = ioctl(devfd, UDMABUF_CREATE, &create);
0067 if (buf >= 0) {
0068 printf("%s: [FAIL,test-1]\n", TEST_PREFIX);
0069 exit(1);
0070 }
0071
0072
0073 create.memfd = memfd;
0074 create.offset = 0;
0075 create.size = getpagesize()/2;
0076 buf = ioctl(devfd, UDMABUF_CREATE, &create);
0077 if (buf >= 0) {
0078 printf("%s: [FAIL,test-2]\n", TEST_PREFIX);
0079 exit(1);
0080 }
0081
0082
0083 create.memfd = 0;
0084 create.offset = 0;
0085 create.size = size;
0086 buf = ioctl(devfd, UDMABUF_CREATE, &create);
0087 if (buf >= 0) {
0088 printf("%s: [FAIL,test-3]\n", TEST_PREFIX);
0089 exit(1);
0090 }
0091
0092
0093 create.memfd = memfd;
0094 create.offset = 0;
0095 create.size = size;
0096 buf = ioctl(devfd, UDMABUF_CREATE, &create);
0097 if (buf < 0) {
0098 printf("%s: [FAIL,test-4]\n", TEST_PREFIX);
0099 exit(1);
0100 }
0101
0102 fprintf(stderr, "%s: ok\n", TEST_PREFIX);
0103 close(buf);
0104 close(memfd);
0105 close(devfd);
0106 return 0;
0107 }