mapper.c revision 1.3
1/*	$NetBSD: mapper.c,v 1.3 2020/09/05 16:30:12 riastradh Exp $	*/
2
3/*-
4 * Copyright (c) 2019 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <sys/cdefs.h>
30__KERNEL_RCSID(0, "$NetBSD: mapper.c,v 1.3 2020/09/05 16:30:12 riastradh Exp $");
31
32#include <sys/param.h>
33#include <sys/conf.h>
34#include <sys/device.h>
35#include <sys/kernel.h>
36#include <sys/kmem.h>
37#include <sys/module.h>
38#include <sys/systm.h>
39
40/*
41 * Creating a device /dev/mapper for demonstration.
42 * To use this device you need to do:
43 * 	mknod /dev/mapper c 351 0
44 *
45 */
46
47dev_type_open(mapper_open);
48dev_type_close(mapper_close);
49dev_type_mmap(mapper_mmap);
50
51static struct cdevsw mapper_cdevsw = {
52	.d_open = mapper_open,
53	.d_close = mapper_close,
54	.d_read = noread,
55	.d_write = nowrite,
56	.d_ioctl = noioctl,
57	.d_stop = nostop,
58	.d_tty = notty,
59	.d_poll = nopoll,
60	.d_mmap = mapper_mmap,
61	.d_kqfilter = nokqfilter,
62	.d_discard = nodiscard,
63	.d_flag = D_OTHER
64};
65
66struct mapper_softc {
67	int refcnt;
68	char *buffer;
69};
70
71static struct mapper_softc sc;
72
73int
74mapper_open(dev_t self __unused, int flag __unused, int mode __unused,
75           struct lwp *l __unused)
76{
77	if (sc.refcnt > 0)
78		return EBUSY;
79	++sc.refcnt;
80	sc.buffer = kmem_zalloc(PAGE_SIZE, KM_SLEEP);
81	snprintf(sc.buffer, PAGE_SIZE, "Hey There!");
82	return 0;
83}
84
85int
86mapper_close(dev_t self __unused, int flag __unused, int mode __unused,
87            struct lwp *l __unused)
88{
89	kmem_free(sc.buffer, PAGE_SIZE);
90	--sc.refcnt;
91	return 0;
92}
93
94/*
95 * Here, pmap_extract() is used to extract the mapping from the specified
96 * physical map for the provided virtual address, where pmap_kernel() points
97 * to the kernel pmap.
98 */
99
100paddr_t
101mapper_mmap(dev_t dev, off_t off, int prot)
102{
103	paddr_t pa;
104
105	if (off & PAGE_MASK)
106		return (paddr_t)-1;
107	if (prot != VM_PROT_READ)
108		return (paddr_t)-1;
109	if (pmap_extract(pmap_kernel(), (vaddr_t)sc.buffer, &pa))
110		return (paddr_t)atop(pa);
111
112	return (paddr_t)-1;
113}
114
115MODULE(MODULE_CLASS_MISC, mapper, NULL);
116
117static int
118mapper_modcmd(modcmd_t cmd, void *arg __unused)
119{
120	/* The major should be verified and changed if needed to avoid
121	 * conflicts with other devices. */
122	int cmajor = 351, bmajor = -1;
123
124	switch (cmd) {
125	case MODULE_CMD_INIT:
126		if (devsw_attach("mapper", NULL, &bmajor, &mapper_cdevsw,
127					&cmajor))
128			return ENXIO;
129		return 0;
130	case MODULE_CMD_FINI:
131		if (sc.refcnt > 0)
132			return EBUSY;
133		devsw_detach(NULL, &mapper_cdevsw);
134		return 0;
135	default:
136		return ENOTTY;
137	}
138	return 0;
139}
140