2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2000 Mark R. V. Murray & Jeroen C. van Gelderen
5 * Copyright (c) 2001-2004 Mark R. V. Murray
6 * Copyright (c) 2014 Eitan Adler
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer
14 * in this position and unchanged.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/malloc.h>
41 #include <sys/module.h>
44 #include <sys/filio.h>
46 #include <machine/bus.h>
47 #include <machine/vmparam.h>
49 /* For use with destroy_dev(9). */
50 static struct cdev *full_dev;
51 static struct cdev *null_dev;
52 static struct cdev *zero_dev;
54 static d_write_t full_write;
55 static d_write_t null_write;
56 static d_ioctl_t null_ioctl;
57 static d_ioctl_t zero_ioctl;
58 static d_read_t zero_read;
60 static struct cdevsw full_cdevsw = {
61 .d_version = D_VERSION,
63 .d_write = full_write,
64 .d_ioctl = zero_ioctl,
68 static struct cdevsw null_cdevsw = {
69 .d_version = D_VERSION,
70 .d_read = (d_read_t *)nullop,
71 .d_write = null_write,
72 .d_ioctl = null_ioctl,
76 static struct cdevsw zero_cdevsw = {
77 .d_version = D_VERSION,
79 .d_write = null_write,
80 .d_ioctl = zero_ioctl,
82 .d_flags = D_MMAP_ANON,
89 full_write(struct cdev *dev __unused, struct uio *uio __unused, int flags __unused)
97 null_write(struct cdev *dev __unused, struct uio *uio, int flags __unused)
106 null_ioctl(struct cdev *dev __unused, u_long cmd, caddr_t data __unused,
107 int flags __unused, struct thread *td)
109 struct diocskerneldump_arg kda;
114 #ifdef COMPAT_FREEBSD11
115 case DIOCSKERNELDUMP_FREEBSD11:
116 gone_in(13, "FreeBSD 11.x ABI compat");
119 #ifdef COMPAT_FREEBSD12
120 case DIOCSKERNELDUMP_FREEBSD12:
121 if (cmd == DIOCSKERNELDUMP_FREEBSD12)
122 gone_in(14, "FreeBSD 12.x ABI compat");
125 case DIOCSKERNELDUMP:
126 bzero(&kda, sizeof(kda));
127 kda.kda_index = KDA_REMOVE_ALL;
128 error = dumper_remove(NULL, &kda);
133 if (*(int *)data != 0)
144 zero_ioctl(struct cdev *dev __unused, u_long cmd, caddr_t data __unused,
145 int flags __unused, struct thread *td)
154 if (*(int *)data != 0)
166 zero_read(struct cdev *dev __unused, struct uio *uio, int flags __unused)
172 KASSERT(uio->uio_rw == UIO_READ,
173 ("Can't be in %s for write", __func__));
174 zbuf = __DECONST(void *, zero_region);
175 while (uio->uio_resid > 0 && error == 0) {
176 len = uio->uio_resid;
177 if (len > ZERO_REGION_SIZE)
178 len = ZERO_REGION_SIZE;
179 error = uiomove(zbuf, len, uio);
187 null_modevent(module_t mod __unused, int type, void *data __unused)
192 printf("null: <full device, null device, zero device>\n");
193 full_dev = make_dev_credf(MAKEDEV_ETERNAL_KLD, &full_cdevsw, 0,
194 NULL, UID_ROOT, GID_WHEEL, 0666, "full");
195 null_dev = make_dev_credf(MAKEDEV_ETERNAL_KLD, &null_cdevsw, 0,
196 NULL, UID_ROOT, GID_WHEEL, 0666, "null");
197 zero_dev = make_dev_credf(MAKEDEV_ETERNAL_KLD, &zero_cdevsw, 0,
198 NULL, UID_ROOT, GID_WHEEL, 0666, "zero");
202 destroy_dev(full_dev);
203 destroy_dev(null_dev);
204 destroy_dev(zero_dev);
217 DEV_MODULE(null, null_modevent, NULL);
218 MODULE_VERSION(null, 1);