[PATCH v4] Add mmap

Vijay Kumar Banerjee vijaykumar9597 at gmail.com
Wed Jul 17 20:28:16 UTC 2019


---
 freebsd/sys/kern/kern_conf.c       |  8 +++----
 freebsd/sys/sys/conf.h             |  2 +-
 rtemsbsd/include/machine/vm.h      |  2 ++
 rtemsbsd/sys/fs/devfs/devfs_devs.c | 38 ++++++++++++++++++++++++++++++
 testsuite/cdev01/test_cdev.c       | 21 ++++++++++++++++-
 testsuite/cdev01/test_cdev01.h     |  3 ++-
 testsuite/cdev01/test_main.c       |  4 ++++
 7 files changed, 71 insertions(+), 7 deletions(-)

diff --git a/freebsd/sys/kern/kern_conf.c b/freebsd/sys/kern/kern_conf.c
index 92237d9d..560a450a 100644
--- a/freebsd/sys/kern/kern_conf.c
+++ b/freebsd/sys/kern/kern_conf.c
@@ -328,8 +328,8 @@ static struct cdevsw dead_cdevsw = {
 	.d_write =	dead_write,
 	.d_ioctl =	dead_ioctl,
 	.d_poll =	dead_poll,
-#ifndef __rtems__
 	.d_mmap =	dead_mmap,
+#ifndef __rtems__
 	.d_strategy =	dead_strategy,
 #endif /* __rtems__ */
 	.d_name =	"dead",
@@ -522,7 +522,6 @@ giant_kqfilter(struct cdev *dev, struct knote *kn)
 	return (retval);
 }
 
-#ifndef __rtems__
 static int
 giant_mmap(struct cdev *dev, vm_ooffset_t offset, vm_paddr_t *paddr, int nprot,
     vm_memattr_t *memattr)
@@ -541,6 +540,7 @@ giant_mmap(struct cdev *dev, vm_ooffset_t offset, vm_paddr_t *paddr, int nprot,
 	return (retval);
 }
 
+#ifndef __rtems__
 static int
 giant_mmap_single(struct cdev *dev, vm_ooffset_t *offset, vm_size_t size,
     vm_object_t *object, int nprot)
@@ -667,8 +667,8 @@ prep_cdevsw(struct cdevsw *devsw, int flags)
 		devsw->d_write = dead_write;
 		devsw->d_ioctl = dead_ioctl;
 		devsw->d_poll = dead_poll;
-#ifndef __rtems__
 		devsw->d_mmap = dead_mmap;
+#ifndef __rtems__
 		devsw->d_mmap_single = dead_mmap_single;
 		devsw->d_strategy = dead_strategy;
 		devsw->d_dump = dead_dump;
@@ -702,8 +702,8 @@ prep_cdevsw(struct cdevsw *devsw, int flags)
 	FIXUP(d_write,		no_write,	giant_write);
 	FIXUP(d_ioctl,		no_ioctl,	giant_ioctl);
 	FIXUP(d_poll,		no_poll,	giant_poll);
-#ifndef __rtems__
 	FIXUP(d_mmap,		no_mmap,	giant_mmap);
+#ifndef __rtems__
 	FIXUP(d_strategy,	no_strategy,	giant_strategy);
 #endif /* __rtems__ */
 	FIXUP(d_kqfilter,	no_kqfilter,	giant_kqfilter);
diff --git a/freebsd/sys/sys/conf.h b/freebsd/sys/sys/conf.h
index 4ace162f..c0a66442 100644
--- a/freebsd/sys/sys/conf.h
+++ b/freebsd/sys/sys/conf.h
@@ -209,8 +209,8 @@ struct cdevsw {
 	d_write_t		*d_write;
 	d_ioctl_t		*d_ioctl;
 	d_poll_t		*d_poll;
-#ifndef __rtems__
 	d_mmap_t		*d_mmap;
+#ifndef __rtems__
 	d_strategy_t		*d_strategy;
 	dumper_t		*d_dump;
 #endif /* __rtems__ */
diff --git a/rtemsbsd/include/machine/vm.h b/rtemsbsd/include/machine/vm.h
index e69de29b..351b7472 100644
--- a/rtemsbsd/include/machine/vm.h
+++ b/rtemsbsd/include/machine/vm.h
@@ -0,0 +1,2 @@
+#define VM_MEMATTR_DEFAULT 0
+#define VM_MEMATTR_UNCACHEABLE 1
diff --git a/rtemsbsd/sys/fs/devfs/devfs_devs.c b/rtemsbsd/sys/fs/devfs/devfs_devs.c
index 7c697b0a..7b09e530 100644
--- a/rtemsbsd/sys/fs/devfs/devfs_devs.c
+++ b/rtemsbsd/sys/fs/devfs/devfs_devs.c
@@ -387,6 +387,43 @@ devfs_imfs_kqfilter(rtems_libio_t *iop, struct knote *kn)
 	return error;
 }
 
+static int
+devfs_imfs_mmap(rtems_libio_t *iop, void **addr, size_t len, int prot,
+    off_t off)
+{
+	struct cdev *cdev = devfs_imfs_get_context_by_iop(iop);
+	struct file *fp = rtems_bsd_iop_to_fp(iop);
+	struct thread *td = rtems_bsd_get_curthread_or_null();
+	struct file *fpop;
+	struct cdevsw *dsw;
+	int error, ref;
+
+	if (td != 0) {
+		if (cdev == NULL) {
+			error = ENXIO;
+			goto err;
+		}
+		if (cdev->si_flags & SI_ALIAS) {
+			cdev = cdev->si_parent;
+		}
+		dsw = dev_refthread(cdev, &ref);
+		if (dsw == NULL) {
+			error = ENXIO;
+			goto err;
+		}
+		fpop = td->td_fpop;
+		curthread->td_fpop = fp;
+		error = dsw->d_mmap( cdev, off, (vm_paddr_t *) addr, prot, VM_MEMATTR_DEFAULT);
+		td->td_fpop = fpop;
+		dev_relthread(cdev, ref);
+	} else {
+		error = ENOMEM;
+	}
+
+err:
+	return rtems_bsd_error_to_status_and_errno(error);
+}
+
 static const rtems_filesystem_file_handlers_r devfs_imfs_handlers = {
 	.open_h = devfs_imfs_open,
 	.close_h = devfs_imfs_close,
@@ -403,6 +440,7 @@ static const rtems_filesystem_file_handlers_r devfs_imfs_handlers = {
 	.kqfilter_h = devfs_imfs_kqfilter,
 	.readv_h = devfs_imfs_readv,
 	.writev_h = devfs_imfs_writev,
+	.mmap_h = devfs_imfs_mmap,
 };
 
 static const IMFS_node_control devfs_imfs_control = IMFS_GENERIC_INITIALIZER(
diff --git a/testsuite/cdev01/test_cdev.c b/testsuite/cdev01/test_cdev.c
index 19fb1a5e..267c0e20 100644
--- a/testsuite/cdev01/test_cdev.c
+++ b/testsuite/cdev01/test_cdev.c
@@ -30,8 +30,10 @@
  */
 
 #include <machine/rtems-bsd-kernel-space.h>
+#include <machine/vm.h>
 #include <sys/types.h>
 #include <sys/conf.h>
+#include <sys/mman.h>
 
 #include <rtems/seterr.h>
 
@@ -46,6 +48,7 @@ static	d_write_t	testwrite;
 static	d_ioctl_t	testioctl;
 static	d_poll_t	testpoll;
 static	d_kqfilter_t	testkqfilter;
+static	d_mmap_t	testmmap;
 
 static struct cdevsw test_cdevsw = {
 	.d_version =	D_VERSION,
@@ -59,6 +62,7 @@ static struct cdevsw test_cdevsw = {
 	.d_ioctl =	testioctl,
 	.d_poll =	testpoll,
 	.d_kqfilter =	testkqfilter,
+	.d_mmap =	testmmap,
 };
 
 static	int
@@ -77,7 +81,7 @@ testclose(struct cdev *dev, int fflag, int devtype, struct thread *td)
 {
 	test_state *state = dev->si_drv1;
 
-	assert(*state == TEST_KQFILTER);
+	assert(*state == TEST_MMAP);
 	*state = TEST_CLOSED;
 
 	return 0;
@@ -148,6 +152,21 @@ testkqfilter(struct cdev *dev, struct knote *kn)
 	return TEST_KQ_ERRNO;
 }
 
+static int
+testmmap(struct cdev *dev, vm_ooffset_t offset, vm_paddr_t *paddr,
+         int nprot, vm_memattr_t *memattr)
+{
+	test_state *state = dev->si_drv1;
+
+	assert(memattr == VM_MEMATTR_DEFAULT);
+	assert(paddr != NULL);
+	assert(nprot != PROT_NONE);
+	assert(*state == TEST_KQFILTER);
+	*state = TEST_MMAP;
+
+	return 0;
+}
+
 void
 test_make_dev(test_state *state, const char *name)
 {
diff --git a/testsuite/cdev01/test_cdev01.h b/testsuite/cdev01/test_cdev01.h
index a83001b5..58866a0c 100644
--- a/testsuite/cdev01/test_cdev01.h
+++ b/testsuite/cdev01/test_cdev01.h
@@ -42,7 +42,8 @@ typedef enum {
 	TEST_WRITEV,
 	TEST_POLL,
 	TEST_KQFILTER,
-	TEST_CLOSED
+	TEST_MMAP,
+	TEST_CLOSED,
 } test_state;
 
 void test_make_dev(test_state *state, const char *name);
diff --git a/testsuite/cdev01/test_main.c b/testsuite/cdev01/test_main.c
index cbff9133..23aefa86 100644
--- a/testsuite/cdev01/test_main.c
+++ b/testsuite/cdev01/test_main.c
@@ -35,6 +35,7 @@
 #include <sys/time.h>
 #include <sys/uio.h>
 #include <sys/ioctl.h>
+#include <sys/mman.h>
 
 #include <assert.h>
 #include <errno.h>
@@ -125,6 +126,9 @@ static void test_cdev(const char *path)
 	assert(rv == -1);
 	assert(errno == TEST_KQ_ERRNO);
 
+	rv = mmap(NULL, 1, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
+	assert(rv == 0);
+
 	rv = close(fd);
 	assert(rv == 0);
 
-- 
2.20.1



More information about the devel mailing list