Module Name:    src
Committed By:   kamil
Date:           Wed Apr  1 01:57:20 UTC 2020

Modified Files:
        src/sys/modules/examples: Makefile README
Added Files:
        src/sys/modules/examples/fopsmapper: Makefile cmd_mapper.c fopsmapper.c

Log Message:
Import a new example kernel module fopsmapper

fopsmapper - basic implementation of mmap with fileops fo_mmap

Submitted by Aditya Vardhan Padala.


To generate a diff of this commit:
cvs rdiff -u -r1.8 -r1.9 src/sys/modules/examples/Makefile
cvs rdiff -u -r1.12 -r1.13 src/sys/modules/examples/README
cvs rdiff -u -r0 -r1.1 src/sys/modules/examples/fopsmapper/Makefile \
    src/sys/modules/examples/fopsmapper/cmd_mapper.c \
    src/sys/modules/examples/fopsmapper/fopsmapper.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/modules/examples/Makefile
diff -u src/sys/modules/examples/Makefile:1.8 src/sys/modules/examples/Makefile:1.9
--- src/sys/modules/examples/Makefile:1.8	Wed Feb  5 13:23:42 2020
+++ src/sys/modules/examples/Makefile	Wed Apr  1 01:57:20 2020
@@ -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

Index: src/sys/modules/examples/README
diff -u src/sys/modules/examples/README:1.12 src/sys/modules/examples/README:1.13
--- src/sys/modules/examples/README:1.12	Fri Feb  7 19:22:21 2020
+++ src/sys/modules/examples/README	Wed Apr  1 01:57:20 2020
@@ -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 @@ DESCRIPTION
 
      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 @@ HISTORY
      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.

Added files:

Index: src/sys/modules/examples/fopsmapper/Makefile
diff -u /dev/null src/sys/modules/examples/fopsmapper/Makefile:1.1
--- /dev/null	Wed Apr  1 01:57:20 2020
+++ src/sys/modules/examples/fopsmapper/Makefile	Wed Apr  1 01:57:20 2020
@@ -0,0 +1,6 @@
+#S?=    /usr/src/sys
+
+KMOD= fopsmapper
+SRCS= fopsmapper.c
+
+.include <bsd.kmodule.mk>
Index: src/sys/modules/examples/fopsmapper/cmd_mapper.c
diff -u /dev/null src/sys/modules/examples/fopsmapper/cmd_mapper.c:1.1
--- /dev/null	Wed Apr  1 01:57:20 2020
+++ src/sys/modules/examples/fopsmapper/cmd_mapper.c	Wed Apr  1 01:57:20 2020
@@ -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;
+}
Index: src/sys/modules/examples/fopsmapper/fopsmapper.c
diff -u /dev/null src/sys/modules/examples/fopsmapper/fopsmapper.c:1.1
--- /dev/null	Wed Apr  1 01:57:20 2020
+++ src/sys/modules/examples/fopsmapper/fopsmapper.c	Wed Apr  1 01:57:20 2020
@@ -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;
+	case MODULE_CMD_FINI:
+		return EBUSY;
+	default:
+		return ENOTTY;
+	}
+}

Reply via email to