mapper.c revision 1.4
1/*	$NetBSD: mapper.c,v 1.4 2020/09/06 02:18:53 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.4 2020/09/06 02:18:53 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#include <uvm/uvm_extern.h>
41
42/*
43 * Creating a device /dev/mapper for demonstration.
44 * To use this device you need to do:
45 * 	mknod /dev/mapper c 351 0
46 *
47 */
48
49dev_type_open(mapper_open);
50dev_type_close(mapper_close);
51dev_type_mmap(mapper_mmap);
52
53static struct cdevsw mapper_cdevsw = {
54	.d_open = mapper_open,
55	.d_close = mapper_close,
56	.d_read = noread,
57	.d_write = nowrite,
58	.d_ioctl = noioctl,
59	.d_stop = nostop,
60	.d_tty = notty,
61	.d_poll = nopoll,
62	.d_mmap = mapper_mmap,
63	.d_kqfilter = nokqfilter,
64	.d_discard = nodiscard,
65	.d_flag = D_OTHER
66};
67
68struct mapper_softc {
69	int refcnt;
70	char *buffer;
71};
72
73static struct mapper_softc sc;
74
75int
76mapper_open(dev_t self __unused, int flag __unused, int mode __unused,
77           struct lwp *l __unused)
78{
79	if (sc.refcnt > 0)
80		return EBUSY;
81	++sc.refcnt;
82	sc.buffer = kmem_zalloc(PAGE_SIZE, KM_SLEEP);
83	snprintf(sc.buffer, PAGE_SIZE, "Hey There!");
84	return 0;
85}
86
87int
88mapper_close(dev_t self __unused, int flag __unused, int mode __unused,
89            struct lwp *l __unused)
90{
91	kmem_free(sc.buffer, PAGE_SIZE);
92	--sc.refcnt;
93	return 0;
94}
95
96/*
97 * Here, pmap_extract() is used to extract the mapping from the specified
98 * physical map for the provided virtual address, where pmap_kernel() points
99 * to the kernel pmap.
100 */
101
102paddr_t
103mapper_mmap(dev_t dev, off_t off, int prot)
104{
105	paddr_t pa;
106
107	if (off & PAGE_MASK)
108		return (paddr_t)-1;
109	if (prot != VM_PROT_READ)
110		return (paddr_t)-1;
111	if (pmap_extract(pmap_kernel(), (vaddr_t)sc.buffer, &pa))
112		return (paddr_t)atop(pa);
113
114	return (paddr_t)-1;
115}
116
117MODULE(MODULE_CLASS_MISC, mapper, NULL);
118
119static int
120mapper_modcmd(modcmd_t cmd, void *arg __unused)
121{
122	/* The major should be verified and changed if needed to avoid
123	 * conflicts with other devices. */
124	int cmajor = 351, bmajor = -1;
125
126	switch (cmd) {
127	case MODULE_CMD_INIT:
128		if (devsw_attach("mapper", NULL, &bmajor, &mapper_cdevsw,
129					&cmajor))
130			return ENXIO;
131		return 0;
132	case MODULE_CMD_FINI:
133		if (sc.refcnt > 0)
134			return EBUSY;
135		devsw_detach(NULL, &mapper_cdevsw);
136		return 0;
137	default:
138		return ENOTTY;
139	}
140	return 0;
141}
142