aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEric Anholt2008-04-23 16:52:30 -0500
committerEric Anholt2008-04-23 18:22:09 -0500
commit22877864c204139fe1c46899bedd237e38f0e849 (patch)
tree628aafef0a4c339896845141be02f8bf7c356936
parent8c741ed54e1be63528e79222b600f37506c6d6d2 (diff)
downloadexternal-libgbm-22877864c204139fe1c46899bedd237e38f0e849.tar.gz
external-libgbm-22877864c204139fe1c46899bedd237e38f0e849.tar.xz
external-libgbm-22877864c204139fe1c46899bedd237e38f0e849.zip
Add mmap ioctl to mmfs.
-rw-r--r--.gitignore1
-rw-r--r--linux-core/mmfs_drv.c43
-rw-r--r--tests/Makefile.am3
-rw-r--r--tests/mmfs_mmap.c129
4 files changed, 175 insertions, 1 deletions
diff --git a/.gitignore b/.gitignore
index 5a4bcb13..4d89a3cb 100644
--- a/.gitignore
+++ b/.gitignore
@@ -59,6 +59,7 @@ tests/getstats
59tests/getversion 59tests/getversion
60tests/lock 60tests/lock
61tests/mmfs_basic 61tests/mmfs_basic
62tests/mmfs_mmap
62tests/mmfs_readwrite 63tests/mmfs_readwrite
63tests/openclose 64tests/openclose
64tests/setversion 65tests/setversion
diff --git a/linux-core/mmfs_drv.c b/linux-core/mmfs_drv.c
index d973592d..88246297 100644
--- a/linux-core/mmfs_drv.c
+++ b/linux-core/mmfs_drv.c
@@ -32,6 +32,8 @@
32#include <linux/fs.h> 32#include <linux/fs.h>
33#include <linux/file.h> 33#include <linux/file.h>
34#include <linux/module.h> 34#include <linux/module.h>
35#include <linux/mman.h>
36#include <linux/pagemap.h>
35#include "mmfs.h" 37#include "mmfs.h"
36 38
37/** @file mmfs.c 39/** @file mmfs.c
@@ -245,6 +247,45 @@ mmfs_pread_ioctl(struct inode *inode, struct file *filp,
245} 247}
246 248
247/** 249/**
250 * Maps the contents of an object, returning the address it is mapped
251 * into.
252 *
253 * While the mapping holds a reference on the contents of the object, it doesn't
254 * imply a ref on the object itself.
255 */
256static int
257mmfs_mmap_ioctl(struct inode *inode, struct file *filp,
258 unsigned int cmd, unsigned long arg)
259{
260 struct mmfs_file *mmfs_filp = filp->private_data;
261 struct mmfs_mmap_args args;
262 struct mmfs_object *obj;
263 loff_t offset;
264
265 if (copy_from_user(&args, (void __user *)arg, sizeof(args)))
266 return -EFAULT;
267
268 obj = mmfs_object_lookup(mmfs_filp, args.handle);
269 if (obj == NULL)
270 return -EINVAL;
271
272 offset = args.offset;
273
274 down_write(&current->mm->mmap_sem);
275 args.addr = (void *)do_mmap(obj->filp, 0, args.size,
276 PROT_READ | PROT_WRITE, MAP_SHARED,
277 args.offset);
278 up_write(&current->mm->mmap_sem);
279
280 mmfs_object_unreference(obj);
281
282 if (copy_to_user((void __user *)arg, &args, sizeof(args)))
283 return -EFAULT;
284
285 return 0;
286}
287
288/**
248 * Writes data to the object referenced by handle. 289 * Writes data to the object referenced by handle.
249 * 290 *
250 * On error, the contents of the buffer that were to be modified are undefined. 291 * On error, the contents of the buffer that were to be modified are undefined.
@@ -297,6 +338,8 @@ mmfs_ioctl(struct inode *inode, struct file *filp,
297 return mmfs_pread_ioctl(inode, filp, cmd, arg); 338 return mmfs_pread_ioctl(inode, filp, cmd, arg);
298 case MMFS_IOCTL_PWRITE: 339 case MMFS_IOCTL_PWRITE:
299 return mmfs_pwrite_ioctl(inode, filp, cmd, arg); 340 return mmfs_pwrite_ioctl(inode, filp, cmd, arg);
341 case MMFS_IOCTL_MMAP:
342 return mmfs_mmap_ioctl(inode, filp, cmd, arg);
300 default: 343 default:
301 return -EINVAL; 344 return -EINVAL;
302 } 345 }
diff --git a/tests/Makefile.am b/tests/Makefile.am
index e2931013..67cb034d 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -24,7 +24,8 @@ TESTS = auth \
24 setversion \ 24 setversion \
25 updatedraw \ 25 updatedraw \
26 mmfs_basic \ 26 mmfs_basic \
27 mmfs_readwrite 27 mmfs_readwrite \
28 mmfs_mmap
28 29
29EXTRA_PROGRAMS = $(TESTS) 30EXTRA_PROGRAMS = $(TESTS)
30CLEANFILES = $(EXTRA_PROGRAMS) $(EXTRA_LTLIBRARIES) 31CLEANFILES = $(EXTRA_PROGRAMS) $(EXTRA_LTLIBRARIES)
diff --git a/tests/mmfs_mmap.c b/tests/mmfs_mmap.c
new file mode 100644
index 00000000..3cb6a853
--- /dev/null
+++ b/tests/mmfs_mmap.c
@@ -0,0 +1,129 @@
1/*
2 * Copyright © 2008 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 *
26 */
27
28#include <stdlib.h>
29#include <stdio.h>
30#include <string.h>
31#include <assert.h>
32#include <fcntl.h>
33#include <inttypes.h>
34#include <errno.h>
35#include <sys/stat.h>
36#include "mmfs.h"
37
38#define MMFS_BUFFER_SIZE 16384
39
40int do_read(int fd, int handle, void *buf, int offset, int size)
41{
42 struct mmfs_pread_args read;
43
44 /* Ensure that we don't have any convenient data in buf in case
45 * we fail.
46 */
47 memset(buf, 0xd0, size);
48
49 memset(&read, 0, sizeof(read));
50 read.handle = handle;
51 read.data = buf;
52 read.size = size;
53 read.offset = offset;
54
55 return ioctl(fd, MMFS_IOCTL_PREAD, &read);
56}
57
58int do_write(int fd, int handle, void *buf, int offset, int size)
59{
60 struct mmfs_pwrite_args write;
61
62 memset(&write, 0, sizeof(write));
63 write.handle = handle;
64 write.data = buf;
65 write.size = size;
66 write.offset = offset;
67
68 return ioctl(fd, MMFS_IOCTL_PWRITE, &write);
69}
70
71int main(int argc, char **argv)
72{
73 int fd;
74 struct mmfs_alloc_args alloc;
75 struct mmfs_mmap_args mmap;
76 struct mmfs_unreference_args unref;
77 uint8_t expected[MMFS_BUFFER_SIZE];
78 uint8_t buf[MMFS_BUFFER_SIZE];
79 int ret;
80 int handle;
81
82 fd = open_mmfs_device();
83
84 memset(&mmap, 0, sizeof(mmap));
85 mmap.handle = 0x10101010;
86 mmap.offset = 0;
87 mmap.size = 4096;
88 printf("Testing mmaping of bad object.\n");
89 ret = ioctl(fd, MMFS_IOCTL_MMAP, &mmap);
90 assert(ret == -1 && errno == EINVAL);
91
92 memset(&alloc, 0, sizeof(alloc));
93 alloc.size = MMFS_BUFFER_SIZE;
94 ret = ioctl(fd, MMFS_IOCTL_ALLOC, &alloc);
95 assert(ret == 0);
96 handle = alloc.handle;
97
98 printf("Testing mmaping of newly allocated object.\n");
99 mmap.handle = handle;
100 mmap.offset = 0;
101 mmap.size = MMFS_BUFFER_SIZE;
102 ret = ioctl(fd, MMFS_IOCTL_MMAP, &mmap);
103 assert(ret == 0);
104
105 printf("Testing contents of newly allocated object.\n");
106 memset(expected, 0, sizeof(expected));
107 assert(memcmp(mmap.addr, expected, sizeof(expected)) == 0);
108
109 printf("Testing coherency of writes and mmap reads.\n");
110 memset(buf, 0, sizeof(buf));
111 memset(buf + 1024, 0x01, 1024);
112 memset(expected + 1024, 0x01, 1024);
113 ret = do_write(fd, handle, buf, 0, MMFS_BUFFER_SIZE);
114 assert(ret == 0);
115 assert(memcmp(buf, mmap.addr, sizeof(buf)) == 0);
116
117 printf("Testing that mapping stays after unreference\n");
118 unref.handle = handle;
119 ret = ioctl(fd, MMFS_IOCTL_UNREFERENCE, &unref);
120 assert(ret == 0);
121 assert(memcmp(buf, mmap.addr, sizeof(buf)) == 0);
122
123 printf("Testing unmapping\n");
124 munmap(mmap.addr, MMFS_BUFFER_SIZE);
125
126 close(fd);
127
128 return 0;
129}