Add a new example kernel module: mapper

Basic implementation of mmap.

Submitted by Akul Pillai.
This commit is contained in:
kamil 2019-01-17 20:47:42 +00:00
parent 76718fd7da
commit 1514387ab8
5 changed files with 229 additions and 2 deletions

View File

@ -1,4 +1,4 @@
# $NetBSD: Makefile,v 1.6 2018/05/29 16:53:56 kamil Exp $
# $NetBSD: Makefile,v 1.7 2019/01/17 20:47:42 kamil Exp $
.include <bsd.own.mk>
@ -6,6 +6,7 @@ SUBDIR+= executor
SUBDIR+= hello
#SUBDIR+= luahello # Nothing to build here, only text files
SUBDIR+= luareadhappy # Needs an additional Lua script
SUBDIR+= mapper # Needs an additional helper program
SUBDIR+= panic_string # Crashes the system
SUBDIR+= ping # Needs an additional helper program
SUBDIR+= properties

View File

@ -1,4 +1,4 @@
$NetBSD: README,v 1.8 2018/05/29 16:53:56 kamil Exp $
$NetBSD: README,v 1.9 2019/01/17 20:47:42 kamil Exp $
Kernel Developer's Manual
@ -10,6 +10,7 @@ DESCRIPTION
* hello - the simplest `hello world' module
* luahello - the simplest `hello world' Lua module
* luareadhappy - demonstrates calling Lua code from C
* mapper - basic implementation of mmap
* panic_string - shows how panic is being called through a device
* ping - basic ioctl(9)
* properties - handle incoming properties during the module load
@ -66,5 +67,8 @@ HISTORY
The panic_string module first appeared in NetBSD 9.0 and was authored by
Harry Pantazis.
The mapper module first appeared in NetBSD 9.0 and was authored by
Akul Pillai.
AUTHORS
This document was written by Kamil Rytarowski.

View File

@ -0,0 +1,17 @@
# $NetBSD: Makefile,v 1.1 2019/01/17 20:47:42 kamil Exp $
.include "../Makefile.inc"
#S?= /usr/src/sys
KMOD= mapper
SRCS= mapper.c
.include <bsd.kmodule.mk>
# To make use of this module, you'll need to separately build the
# cmd_mapper program, with a Makefile similar to
#
# MKMAN= NO
# PROG= cmd_mapper
# .include <bsd.prog.mk>

View File

@ -0,0 +1,64 @@
/* $NetBSD: cmd_mapper.c,v 1.1 2019/01/17 20:47:42 kamil Exp $ */
/*-
* Copyright (c) 2019 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: cmd_mapper.c,v 1.1 2019/01/17 20:47:42 kamil Exp $");
#include <sys/mman.h>
#include <err.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define _PATH_DEV_MAPPER "/dev/mapper"
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;
}

View File

@ -0,0 +1,141 @@
/* $NetBSD: mapper.c,v 1.1 2019/01/17 20:47:42 kamil Exp $ */
/*-
* Copyright (c) 2019 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: mapper.c,v 1.1 2019/01/17 20:47:42 kamil Exp $");
#include <sys/param.h>
#include <sys/conf.h>
#include <sys/device.h>
#include <sys/kernel.h>
#include <sys/kmem.h>
#include <sys/module.h>
#include <sys/systm.h>
#include <uvm/uvm.h>
/*
* Creating a device /dev/mapper for demonstration.
* To use this device you need to do:
* mknod /dev/mapper c 210 0
*
*/
dev_type_open(mapper_open);
dev_type_close(mapper_close);
dev_type_mmap(mapper_mmap);
static struct cdevsw mapper_cdevsw = {
.d_open = mapper_open,
.d_close = mapper_close,
.d_read = noread,
.d_write = nowrite,
.d_ioctl = noioctl,
.d_stop = nostop,
.d_tty = notty,
.d_poll = nopoll,
.d_mmap = mapper_mmap,
.d_kqfilter = nokqfilter,
.d_discard = nodiscard,
.d_flag = D_OTHER
};
struct mapper_softc {
int refcnt;
char *buffer;
};
static struct mapper_softc sc;
int
mapper_open(dev_t self __unused, int flag __unused, int mode __unused,
struct lwp *l __unused)
{
if (sc.refcnt > 0)
return EBUSY;
++sc.refcnt;
sc.buffer = kmem_zalloc(PAGE_SIZE, KM_SLEEP);
snprintf(sc.buffer, PAGE_SIZE, "Hey There!");
return 0;
}
int
mapper_close(dev_t self __unused, int flag __unused, int mode __unused,
struct lwp *l __unused)
{
kmem_free(sc.buffer, PAGE_SIZE);
--sc.refcnt;
return 0;
}
/*
* Here, pmap_extract() is used to extract the mapping from the specified
* physical map for the provided virtual address, where pmap_kernel() points
* to the kernel pmap.
*/
paddr_t
mapper_mmap(dev_t dev, off_t off, int prot)
{
paddr_t pa;
if (off & PAGE_MASK)
return (paddr_t)-1;
if (prot != VM_PROT_READ)
return (paddr_t)-1;
if (pmap_extract(pmap_kernel(), (vaddr_t)sc.buffer, &pa))
return (paddr_t)atop(pa);
return (paddr_t)-1;
}
MODULE(MODULE_CLASS_MISC, mapper, NULL);
static int
mapper_modcmd(modcmd_t cmd, void *arg __unused)
{
/* The major should be verified and changed if needed to avoid
* conflicts with other devices. */
int cmajor = 210, bmajor = -1;
switch (cmd) {
case MODULE_CMD_INIT:
if (devsw_attach("mapper", NULL, &bmajor, &mapper_cdevsw,
&cmajor))
return ENXIO;
return 0;
case MODULE_CMD_FINI:
if (sc.refcnt > 0)
return EBUSY;
devsw_detach(NULL, &mapper_cdevsw);
return 0;
default:
return ENOTTY;
}
return 0;
}