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