Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/sys/modules/examples Import a new example kernel module fops...
details: https://anonhg.NetBSD.org/src/rev/535bfef4b991
branches: trunk
changeset: 1008704:535bfef4b991
user: kamil <kamil%NetBSD.org@localhost>
date: Wed Apr 01 01:57:20 2020 +0000
description:
Import a new example kernel module fopsmapper
fopsmapper - basic implementation of mmap with fileops fo_mmap
Submitted by Aditya Vardhan Padala.
diffstat:
sys/modules/examples/Makefile | 3 +-
sys/modules/examples/README | 6 +-
sys/modules/examples/fopsmapper/Makefile | 6 +
sys/modules/examples/fopsmapper/cmd_mapper.c | 64 +++++++++
sys/modules/examples/fopsmapper/fopsmapper.c | 184 +++++++++++++++++++++++++++
5 files changed, 261 insertions(+), 2 deletions(-)
diffs (truncated from 306 to 300 lines):
diff -r d5f6ba4b063e -r 535bfef4b991 sys/modules/examples/Makefile
--- a/sys/modules/examples/Makefile Wed Apr 01 01:51:02 2020 +0000
+++ b/sys/modules/examples/Makefile Wed Apr 01 01:57:20 2020 +0000
@@ -1,8 +1,9 @@
-# $NetBSD: Makefile,v 1.8 2020/02/05 13:23:42 kamil Exp $
+# $NetBSD: Makefile,v 1.9 2020/04/01 01:57:20 kamil Exp $
.include <bsd.own.mk>
SUBDIR+= executor
+SUBDIR+= fopsmapper # Needs an additional helper program
SUBDIR+= hello
#SUBDIR+= luahello # Nothing to build here, only text files
SUBDIR+= luareadhappy # Needs an additional Lua script
diff -r d5f6ba4b063e -r 535bfef4b991 sys/modules/examples/README
--- a/sys/modules/examples/README Wed Apr 01 01:51:02 2020 +0000
+++ b/sys/modules/examples/README Wed Apr 01 01:57:20 2020 +0000
@@ -1,4 +1,4 @@
- $NetBSD: README,v 1.12 2020/02/07 19:22:21 pgoyette Exp $
+ $NetBSD: README,v 1.13 2020/04/01 01:57:20 kamil Exp $
Kernel Developer's Manual
@@ -7,6 +7,7 @@
This directory contains the following example modules:
* executor - basic implementation of callout and RUN_ONCE
+ * fopsmapper - basic implementation of mmap with fileops fo_mmap
* hello - the simplest `hello world' module
* luahello - the simplest `hello world' Lua module
* luareadhappy - demonstrates calling Lua code from C
@@ -74,5 +75,8 @@
The ping_block module first appeared in NetBSD 10.0 and was authored by
Nisarg Joshi.
+ The fopsmapper module first appeared in NetBSD 10.0 and was authored by
+ Aditya Vardhan Padala.
+
AUTHORS
This document was written by Kamil Rytarowski.
diff -r d5f6ba4b063e -r 535bfef4b991 sys/modules/examples/fopsmapper/Makefile
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/modules/examples/fopsmapper/Makefile Wed Apr 01 01:57:20 2020 +0000
@@ -0,0 +1,6 @@
+#S?= /usr/src/sys
+
+KMOD= fopsmapper
+SRCS= fopsmapper.c
+
+.include <bsd.kmodule.mk>
diff -r d5f6ba4b063e -r 535bfef4b991 sys/modules/examples/fopsmapper/cmd_mapper.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/modules/examples/fopsmapper/cmd_mapper.c Wed Apr 01 01:57:20 2020 +0000
@@ -0,0 +1,64 @@
+/* $NetBSD: cmd_mapper.c,v 1.1 2020/04/01 01:57:20 kamil Exp $ */
+
+/*-
+ * Copyright (c) 2020 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__RCSID("$NetBSD");
+
+#include <sys/mman.h>
+
+#include <err.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#define _PATH_DEV_MAPPER "/dev/fopsmapper"
+
+int main(int argc, char **argv)
+{
+ int devfd;
+ char *map = NULL;
+
+ if ((devfd = open(_PATH_DEV_MAPPER, O_RDONLY)) == -1)
+ err(EXIT_FAILURE, "Cannot open %s", _PATH_DEV_MAPPER);
+
+ map = (char *)(mmap(0, sysconf(_SC_PAGESIZE), PROT_READ, MAP_SHARED,
+ devfd, 0));
+ if (map == MAP_FAILED)
+ err(EXIT_FAILURE, "Mapping failed");
+
+ printf("Message from device: %s\n",map);
+
+ if (munmap(map, sysconf(_SC_PAGESIZE)) == -1)
+ err(EXIT_FAILURE, "Unmap failed");
+
+ if (close(devfd) == -1)
+ err(EXIT_FAILURE, "Cannot close %s", _PATH_DEV_MAPPER);
+
+ return EXIT_SUCCESS;
+}
diff -r d5f6ba4b063e -r 535bfef4b991 sys/modules/examples/fopsmapper/fopsmapper.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/modules/examples/fopsmapper/fopsmapper.c Wed Apr 01 01:57:20 2020 +0000
@@ -0,0 +1,184 @@
+/* $NetBSD: fopsmapper.c,v 1.1 2020/04/01 01:57:20 kamil Exp $ */
+
+/*-
+ * Copyright (c) 2020 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__KERNEL_RCSID(0, "$NetBSD: fopsmapper.c,v 1.1 2020/04/01 01:57:20 kamil Exp $");
+
+#include <sys/module.h>
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/kernel.h>
+
+#include <sys/conf.h>
+#include <sys/file.h>
+#include <sys/filedesc.h>
+#include <sys/kmem.h>
+#include <sys/mman.h>
+#include <sys/mutex.h>
+#include <uvm/uvm_extern.h>
+
+/*
+ * To use this module you need to:
+ *
+ * mknod /dev/fopsmapper c 351 0
+ *
+ */
+
+dev_type_open(fopsmapper_open);
+
+const struct cdevsw fopsmapper_cdevsw = {
+ .d_open = fopsmapper_open,
+ .d_close = noclose,
+ .d_read = noread,
+ .d_write = nowrite,
+ .d_ioctl = noioctl,
+ .d_stop = nostop,
+ .d_tty = notty,
+ .d_poll = nopoll,
+ .d_mmap = nommap,
+ .d_kqfilter = nokqfilter,
+ .d_discard = nodiscard,
+ .d_flag = D_OTHER
+};
+
+static int fopsmapper_mmap(file_t *, off_t *, size_t,
+ int, int *, int *,struct uvm_object **, int *);
+static int fopsmapper_close(file_t *);
+
+const struct fileops mapper_fileops = {
+ .fo_read = fbadop_read,
+ .fo_write = fbadop_write,
+ .fo_ioctl = fbadop_ioctl,
+ .fo_fcntl = fnullop_fcntl,
+ .fo_poll = fnullop_poll,
+ .fo_stat = fbadop_stat,
+ .fo_close = fopsmapper_close,
+ .fo_kqfilter = fnullop_kqfilter,
+ .fo_restart = fnullop_restart,
+ .fo_mmap = fopsmapper_mmap,
+};
+
+typedef struct fopsmapper_softc {
+ char *buf;
+ struct uvm_object *uobj;
+ size_t bufsize;
+} fops_t;
+
+int
+fopsmapper_open(dev_t dev, int flag, int mode, struct lwp *l)
+{
+ fops_t *fo;
+ struct file *fp;
+ int fd, error;
+
+ if ((error = fd_allocfile(&fp, &fd)) != 0)
+ return error;
+
+ fo = kmem_zalloc(sizeof(*fo), KM_SLEEP);
+
+ return fd_clone(fp, fd, flag, &mapper_fileops, fo);
+}
+
+int
+fopsmapper_mmap(file_t * fp, off_t * offp, size_t size, int prot,
+ int *flagsp, int *advicep, struct uvm_object **uobjp,
+ int *maxprotp)
+{
+ fops_t *fo;
+ int error;
+
+ if (prot & PROT_EXEC)
+ return EACCES;
+
+ if (size != PAGE_SIZE)
+ return EINVAL;
+
+ if ((fo = fp->f_data) == NULL)
+ return ENXIO;
+
+ fo->bufsize = size;
+ fo->uobj = uao_create(size, 0);
+
+ fo->buf = NULL;
+ /* Map the uvm object into kernel */
+ error = uvm_map(kernel_map, (vaddr_t *) &fo->buf, fo->bufsize,
+ fo->uobj, 0, 0,
+ UVM_MAPFLAG(UVM_PROT_RW, UVM_PROT_RW,
+ UVM_INH_SHARE,UVM_ADV_RANDOM, 0));
+
+ if (error) {
+ uao_detach(fo->uobj);
+ return error;
+ }
+ snprintf(fo->buf, 13, "Hey There!");
+
+ /* Get the reference of uobj */
+ uao_reference(fo->uobj);
+
+ *uobjp = fo->uobj;
+ *maxprotp = prot;
+ *advicep = UVM_ADV_RANDOM;
+
+ return 0;
+}
+
+int
+fopsmapper_close(file_t * fp)
+{
+ fops_t *fo;
+
+ fo = fp->f_data;
+ KASSERT(fo != NULL);
+
+ if (fo->buf != NULL)
+ uvm_deallocate(kernel_map, (vaddr_t)fo->buf, fo->bufsize);
+
+ kmem_free(fo, sizeof(*fo));
+
+ return 0;
+}
+
+MODULE(MODULE_CLASS_MISC, fopsmapper, NULL);
+
+static int
+fopsmapper_modcmd(modcmd_t cmd, void *arg __unused)
+{
+ int cmajor = 351, bmajor = -1;
+
+ switch (cmd) {
+ case MODULE_CMD_INIT:
+ if (devsw_attach("fopsmapper", NULL, &bmajor,
+ &fopsmapper_cdevsw, &cmajor))
+ return ENXIO;
+ return 0;
Home |
Main Index |
Thread Index |
Old Index