1*0b8d836aSriastradh /* $NetBSD: mapper.c,v 1.4 2020/09/06 02:18:53 riastradh Exp $ */
21514387aSkamil
31514387aSkamil /*-
41514387aSkamil * Copyright (c) 2019 The NetBSD Foundation, Inc.
51514387aSkamil * All rights reserved.
61514387aSkamil *
71514387aSkamil * Redistribution and use in source and binary forms, with or without
81514387aSkamil * modification, are permitted provided that the following conditions
91514387aSkamil * are met:
101514387aSkamil * 1. Redistributions of source code must retain the above copyright
111514387aSkamil * notice, this list of conditions and the following disclaimer.
121514387aSkamil * 2. Redistributions in binary form must reproduce the above copyright
131514387aSkamil * notice, this list of conditions and the following disclaimer in the
141514387aSkamil * documentation and/or other materials provided with the distribution.
151514387aSkamil *
161514387aSkamil * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
171514387aSkamil * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
181514387aSkamil * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
191514387aSkamil * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
201514387aSkamil * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
211514387aSkamil * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
221514387aSkamil * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
231514387aSkamil * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
241514387aSkamil * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
251514387aSkamil * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
261514387aSkamil * POSSIBILITY OF SUCH DAMAGE.
271514387aSkamil */
281514387aSkamil
291514387aSkamil #include <sys/cdefs.h>
30*0b8d836aSriastradh __KERNEL_RCSID(0, "$NetBSD: mapper.c,v 1.4 2020/09/06 02:18:53 riastradh Exp $");
311514387aSkamil
321514387aSkamil #include <sys/param.h>
331514387aSkamil #include <sys/conf.h>
341514387aSkamil #include <sys/device.h>
351514387aSkamil #include <sys/kernel.h>
361514387aSkamil #include <sys/kmem.h>
371514387aSkamil #include <sys/module.h>
381514387aSkamil #include <sys/systm.h>
391514387aSkamil
40*0b8d836aSriastradh #include <uvm/uvm_extern.h>
41*0b8d836aSriastradh
421514387aSkamil /*
431514387aSkamil * Creating a device /dev/mapper for demonstration.
441514387aSkamil * To use this device you need to do:
4567a548a8Skamil * mknod /dev/mapper c 351 0
461514387aSkamil *
471514387aSkamil */
481514387aSkamil
491514387aSkamil dev_type_open(mapper_open);
501514387aSkamil dev_type_close(mapper_close);
511514387aSkamil dev_type_mmap(mapper_mmap);
521514387aSkamil
531514387aSkamil static struct cdevsw mapper_cdevsw = {
541514387aSkamil .d_open = mapper_open,
551514387aSkamil .d_close = mapper_close,
561514387aSkamil .d_read = noread,
571514387aSkamil .d_write = nowrite,
581514387aSkamil .d_ioctl = noioctl,
591514387aSkamil .d_stop = nostop,
601514387aSkamil .d_tty = notty,
611514387aSkamil .d_poll = nopoll,
621514387aSkamil .d_mmap = mapper_mmap,
631514387aSkamil .d_kqfilter = nokqfilter,
641514387aSkamil .d_discard = nodiscard,
651514387aSkamil .d_flag = D_OTHER
661514387aSkamil };
671514387aSkamil
681514387aSkamil struct mapper_softc {
691514387aSkamil int refcnt;
701514387aSkamil char *buffer;
711514387aSkamil };
721514387aSkamil
731514387aSkamil static struct mapper_softc sc;
741514387aSkamil
751514387aSkamil int
mapper_open(dev_t self __unused,int flag __unused,int mode __unused,struct lwp * l __unused)761514387aSkamil mapper_open(dev_t self __unused, int flag __unused, int mode __unused,
771514387aSkamil struct lwp *l __unused)
781514387aSkamil {
791514387aSkamil if (sc.refcnt > 0)
801514387aSkamil return EBUSY;
811514387aSkamil ++sc.refcnt;
821514387aSkamil sc.buffer = kmem_zalloc(PAGE_SIZE, KM_SLEEP);
831514387aSkamil snprintf(sc.buffer, PAGE_SIZE, "Hey There!");
841514387aSkamil return 0;
851514387aSkamil }
861514387aSkamil
871514387aSkamil int
mapper_close(dev_t self __unused,int flag __unused,int mode __unused,struct lwp * l __unused)881514387aSkamil mapper_close(dev_t self __unused, int flag __unused, int mode __unused,
891514387aSkamil struct lwp *l __unused)
901514387aSkamil {
911514387aSkamil kmem_free(sc.buffer, PAGE_SIZE);
921514387aSkamil --sc.refcnt;
931514387aSkamil return 0;
941514387aSkamil }
951514387aSkamil
961514387aSkamil /*
971514387aSkamil * Here, pmap_extract() is used to extract the mapping from the specified
981514387aSkamil * physical map for the provided virtual address, where pmap_kernel() points
991514387aSkamil * to the kernel pmap.
1001514387aSkamil */
1011514387aSkamil
1021514387aSkamil paddr_t
mapper_mmap(dev_t dev,off_t off,int prot)1031514387aSkamil mapper_mmap(dev_t dev, off_t off, int prot)
1041514387aSkamil {
1051514387aSkamil paddr_t pa;
1061514387aSkamil
1071514387aSkamil if (off & PAGE_MASK)
1081514387aSkamil return (paddr_t)-1;
1091514387aSkamil if (prot != VM_PROT_READ)
1101514387aSkamil return (paddr_t)-1;
1111514387aSkamil if (pmap_extract(pmap_kernel(), (vaddr_t)sc.buffer, &pa))
1121514387aSkamil return (paddr_t)atop(pa);
1131514387aSkamil
1141514387aSkamil return (paddr_t)-1;
1151514387aSkamil }
1161514387aSkamil
1171514387aSkamil MODULE(MODULE_CLASS_MISC, mapper, NULL);
1181514387aSkamil
1191514387aSkamil static int
mapper_modcmd(modcmd_t cmd,void * arg __unused)1201514387aSkamil mapper_modcmd(modcmd_t cmd, void *arg __unused)
1211514387aSkamil {
1221514387aSkamil /* The major should be verified and changed if needed to avoid
1231514387aSkamil * conflicts with other devices. */
12467a548a8Skamil int cmajor = 351, bmajor = -1;
1251514387aSkamil
1261514387aSkamil switch (cmd) {
1271514387aSkamil case MODULE_CMD_INIT:
1281514387aSkamil if (devsw_attach("mapper", NULL, &bmajor, &mapper_cdevsw,
1291514387aSkamil &cmajor))
1301514387aSkamil return ENXIO;
1311514387aSkamil return 0;
1321514387aSkamil case MODULE_CMD_FINI:
1331514387aSkamil if (sc.refcnt > 0)
1341514387aSkamil return EBUSY;
1351514387aSkamil devsw_detach(NULL, &mapper_cdevsw);
1361514387aSkamil return 0;
1371514387aSkamil default:
1381514387aSkamil return ENOTTY;
1391514387aSkamil }
1401514387aSkamil return 0;
1411514387aSkamil }
142