1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
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
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
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.
18 *
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.
29 *
30 */
31
32 #include <sys/cdefs.h>
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/conf.h>
36 #include <sys/uio.h>
37 #include <sys/kernel.h>
38 #include <sys/malloc.h>
39 #include <sys/module.h>
40 #include <sys/disk.h>
41 #include <sys/bus.h>
42 #include <sys/filio.h>
43
44 #include <machine/bus.h>
45 #include <machine/vmparam.h>
46
47 /* For use with destroy_dev(9). */
48 static struct cdev *full_dev;
49 static struct cdev *null_dev;
50 static struct cdev *zero_dev;
51
52 static d_write_t full_write;
53 static d_write_t null_write;
54 static d_ioctl_t null_ioctl;
55 static d_ioctl_t zero_ioctl;
56 static d_read_t zero_read;
57
58 static struct cdevsw full_cdevsw = {
59 .d_version = D_VERSION,
60 .d_read = zero_read,
61 .d_write = full_write,
62 .d_ioctl = zero_ioctl,
63 .d_name = "full",
64 };
65
66 static struct cdevsw null_cdevsw = {
67 .d_version = D_VERSION,
68 .d_read = (d_read_t *)nullop,
69 .d_write = null_write,
70 .d_ioctl = null_ioctl,
71 .d_name = "null",
72 };
73
74 static struct cdevsw zero_cdevsw = {
75 .d_version = D_VERSION,
76 .d_read = zero_read,
77 .d_write = null_write,
78 .d_ioctl = zero_ioctl,
79 .d_name = "zero",
80 .d_flags = D_MMAP_ANON,
81 };
82
83 /* ARGSUSED */
84 static int
full_write(struct cdev * dev __unused,struct uio * uio __unused,int flags __unused)85 full_write(struct cdev *dev __unused, struct uio *uio __unused, int flags __unused)
86 {
87
88 return (ENOSPC);
89 }
90
91 /* ARGSUSED */
92 static int
null_write(struct cdev * dev __unused,struct uio * uio,int flags __unused)93 null_write(struct cdev *dev __unused, struct uio *uio, int flags __unused)
94 {
95 uio->uio_resid = 0;
96
97 return (0);
98 }
99
100 /* ARGSUSED */
101 static int
null_ioctl(struct cdev * dev __unused,u_long cmd,caddr_t data __unused,int flags __unused,struct thread * td)102 null_ioctl(struct cdev *dev __unused, u_long cmd, caddr_t data __unused,
103 int flags __unused, struct thread *td)
104 {
105 struct diocskerneldump_arg kda;
106 int error;
107
108 error = 0;
109 switch (cmd) {
110 #ifdef COMPAT_FREEBSD12
111 case DIOCSKERNELDUMP_FREEBSD12:
112 if (cmd == DIOCSKERNELDUMP_FREEBSD12)
113 gone_in(14, "FreeBSD 12.x ABI compat");
114 /* FALLTHROUGH */
115 #endif
116 case DIOCSKERNELDUMP:
117 bzero(&kda, sizeof(kda));
118 kda.kda_index = KDA_REMOVE_ALL;
119 error = dumper_remove(NULL, &kda);
120 break;
121 case FIONBIO:
122 break;
123 case FIOASYNC:
124 if (*(int *)data != 0)
125 error = EINVAL;
126 break;
127 default:
128 error = ENOIOCTL;
129 }
130 return (error);
131 }
132
133 /* ARGSUSED */
134 static int
zero_ioctl(struct cdev * dev __unused,u_long cmd,caddr_t data __unused,int flags __unused,struct thread * td)135 zero_ioctl(struct cdev *dev __unused, u_long cmd, caddr_t data __unused,
136 int flags __unused, struct thread *td)
137 {
138 int error;
139 error = 0;
140
141 switch (cmd) {
142 case FIONBIO:
143 break;
144 case FIOASYNC:
145 if (*(int *)data != 0)
146 error = EINVAL;
147 break;
148 default:
149 error = ENOIOCTL;
150 }
151 return (error);
152 }
153
154 /* ARGSUSED */
155 static int
zero_read(struct cdev * dev __unused,struct uio * uio,int flags __unused)156 zero_read(struct cdev *dev __unused, struct uio *uio, int flags __unused)
157 {
158 void *zbuf;
159 ssize_t len;
160 int error = 0;
161
162 KASSERT(uio->uio_rw == UIO_READ,
163 ("Can't be in %s for write", __func__));
164 zbuf = __DECONST(void *, zero_region);
165 while (uio->uio_resid > 0 && error == 0) {
166 len = uio->uio_resid;
167 if (len > ZERO_REGION_SIZE)
168 len = ZERO_REGION_SIZE;
169 error = uiomove(zbuf, len, uio);
170 }
171
172 return (error);
173 }
174
175 /* ARGSUSED */
176 static int
null_modevent(module_t mod __unused,int type,void * data __unused)177 null_modevent(module_t mod __unused, int type, void *data __unused)
178 {
179 switch(type) {
180 case MOD_LOAD:
181 if (bootverbose)
182 printf("null: <full device, null device, zero device>\n");
183 full_dev = make_dev_credf(MAKEDEV_ETERNAL_KLD, &full_cdevsw, 0,
184 NULL, UID_ROOT, GID_WHEEL, 0666, "full");
185 null_dev = make_dev_credf(MAKEDEV_ETERNAL_KLD, &null_cdevsw, 0,
186 NULL, UID_ROOT, GID_WHEEL, 0666, "null");
187 zero_dev = make_dev_credf(MAKEDEV_ETERNAL_KLD, &zero_cdevsw, 0,
188 NULL, UID_ROOT, GID_WHEEL, 0666, "zero");
189 break;
190
191 case MOD_UNLOAD:
192 destroy_dev(full_dev);
193 destroy_dev(null_dev);
194 destroy_dev(zero_dev);
195 break;
196
197 case MOD_SHUTDOWN:
198 break;
199
200 default:
201 return (EOPNOTSUPP);
202 }
203
204 return (0);
205 }
206
207 DEV_MODULE(null, null_modevent, NULL);
208 MODULE_VERSION(null, 1);
209