null.c revision 291215
1/*-
2 * Copyright (c) 2000 Mark R. V. Murray & Jeroen C. van Gelderen
3 * Copyright (c) 2001-2004 Mark R. V. Murray
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer
11 *    in this position and unchanged.
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 AUTHORS ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *
27 */
28
29#include <sys/cdefs.h>
30__FBSDID("$FreeBSD: stable/10/sys/dev/null/null.c 291215 2015-11-23 18:00:55Z smh $");
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). */
47static struct cdev *null_dev;
48static struct cdev *zero_dev;
49
50static d_write_t null_write;
51static d_ioctl_t null_ioctl;
52static d_ioctl_t zero_ioctl;
53static d_read_t zero_read;
54
55static struct cdevsw null_cdevsw = {
56	.d_version =	D_VERSION,
57	.d_read =	(d_read_t *)nullop,
58	.d_write =	null_write,
59	.d_ioctl =	null_ioctl,
60	.d_name =	"null",
61};
62
63static struct cdevsw zero_cdevsw = {
64	.d_version =	D_VERSION,
65	.d_read =	zero_read,
66	.d_write =	null_write,
67	.d_ioctl =	zero_ioctl,
68	.d_name =	"zero",
69	.d_flags =	D_MMAP_ANON,
70};
71
72/* ARGSUSED */
73static int
74null_write(struct cdev *dev __unused, struct uio *uio, int flags __unused)
75{
76	uio->uio_resid = 0;
77
78	return (0);
79}
80
81/* ARGSUSED */
82static int
83null_ioctl(struct cdev *dev __unused, u_long cmd, caddr_t data __unused,
84    int flags __unused, struct thread *td)
85{
86	int error;
87	error = 0;
88
89	switch (cmd) {
90	case DIOCSKERNELDUMP:
91		error = set_dumper(NULL, NULL, td);
92		break;
93	case FIONBIO:
94		break;
95	case FIOASYNC:
96		if (*(int *)data != 0)
97			error = EINVAL;
98		break;
99	default:
100		error = ENOIOCTL;
101	}
102	return (error);
103}
104
105/* ARGSUSED */
106static int
107zero_ioctl(struct cdev *dev __unused, u_long cmd, caddr_t data __unused,
108	   int flags __unused, struct thread *td)
109{
110	int error;
111	error = 0;
112
113	switch (cmd) {
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
127/* ARGSUSED */
128static int
129zero_read(struct cdev *dev __unused, struct uio *uio, int flags __unused)
130{
131	void *zbuf;
132	ssize_t len;
133	int error = 0;
134
135	KASSERT(uio->uio_rw == UIO_READ,
136	    ("Can't be in %s for write", __func__));
137	zbuf = __DECONST(void *, zero_region);
138	while (uio->uio_resid > 0 && error == 0) {
139		len = uio->uio_resid;
140		if (len > ZERO_REGION_SIZE)
141			len = ZERO_REGION_SIZE;
142		error = uiomove(zbuf, len, uio);
143	}
144
145	return (error);
146}
147
148/* ARGSUSED */
149static int
150null_modevent(module_t mod __unused, int type, void *data __unused)
151{
152	switch(type) {
153	case MOD_LOAD:
154		if (bootverbose)
155			printf("null: <null device, zero device>\n");
156		null_dev = make_dev_credf(MAKEDEV_ETERNAL_KLD, &null_cdevsw, 0,
157		    NULL, UID_ROOT, GID_WHEEL, 0666, "null");
158		zero_dev = make_dev_credf(MAKEDEV_ETERNAL_KLD, &zero_cdevsw, 0,
159		    NULL, UID_ROOT, GID_WHEEL, 0666, "zero");
160		break;
161
162	case MOD_UNLOAD:
163		destroy_dev(null_dev);
164		destroy_dev(zero_dev);
165		break;
166
167	case MOD_SHUTDOWN:
168		break;
169
170	default:
171		return (EOPNOTSUPP);
172	}
173
174	return (0);
175}
176
177DEV_MODULE(null, null_modevent, NULL);
178MODULE_VERSION(null, 1);
179