1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 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 __FBSDID("$FreeBSD$"); 34 35 #include "opt_compat.h" 36 37 #include <sys/param.h> 38 #include <sys/systm.h> 39 #include <sys/conf.h> 40 #include <sys/uio.h> 41 #include <sys/kernel.h> 42 #include <sys/malloc.h> 43 #include <sys/module.h> 44 #include <sys/disk.h> 45 #include <sys/bus.h> 46 #include <sys/filio.h> 47 48 #include <machine/bus.h> 49 #include <machine/vmparam.h> 50 51 /* For use with destroy_dev(9). */ 52 static struct cdev *full_dev; 53 static struct cdev *null_dev; 54 static struct cdev *zero_dev; 55 56 static d_write_t full_write; 57 static d_write_t null_write; 58 static d_ioctl_t null_ioctl; 59 static d_ioctl_t zero_ioctl; 60 static d_read_t zero_read; 61 62 static struct cdevsw full_cdevsw = { 63 .d_version = D_VERSION, 64 .d_read = zero_read, 65 .d_write = full_write, 66 .d_ioctl = zero_ioctl, 67 .d_name = "full", 68 }; 69 70 static struct cdevsw null_cdevsw = { 71 .d_version = D_VERSION, 72 .d_read = (d_read_t *)nullop, 73 .d_write = null_write, 74 .d_ioctl = null_ioctl, 75 .d_name = "null", 76 }; 77 78 static struct cdevsw zero_cdevsw = { 79 .d_version = D_VERSION, 80 .d_read = zero_read, 81 .d_write = null_write, 82 .d_ioctl = zero_ioctl, 83 .d_name = "zero", 84 .d_flags = D_MMAP_ANON, 85 }; 86 87 88 89 /* ARGSUSED */ 90 static int 91 full_write(struct cdev *dev __unused, struct uio *uio __unused, int flags __unused) 92 { 93 94 return (ENOSPC); 95 } 96 97 /* ARGSUSED */ 98 static int 99 null_write(struct cdev *dev __unused, struct uio *uio, int flags __unused) 100 { 101 uio->uio_resid = 0; 102 103 return (0); 104 } 105 106 /* ARGSUSED */ 107 static int 108 null_ioctl(struct cdev *dev __unused, u_long cmd, caddr_t data __unused, 109 int flags __unused, struct thread *td) 110 { 111 int error; 112 error = 0; 113 114 switch (cmd) { 115 #ifdef COMPAT_FREEBSD11 116 case DIOCSKERNELDUMP_FREEBSD11: 117 #endif 118 case DIOCSKERNELDUMP: 119 error = set_dumper(NULL, NULL, td, 0, 0, NULL, 0, NULL); 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 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 155 /* ARGSUSED */ 156 static int 157 zero_read(struct cdev *dev __unused, struct uio *uio, int flags __unused) 158 { 159 void *zbuf; 160 ssize_t len; 161 int error = 0; 162 163 KASSERT(uio->uio_rw == UIO_READ, 164 ("Can't be in %s for write", __func__)); 165 zbuf = __DECONST(void *, zero_region); 166 while (uio->uio_resid > 0 && error == 0) { 167 len = uio->uio_resid; 168 if (len > ZERO_REGION_SIZE) 169 len = ZERO_REGION_SIZE; 170 error = uiomove(zbuf, len, uio); 171 } 172 173 return (error); 174 } 175 176 /* ARGSUSED */ 177 static int 178 null_modevent(module_t mod __unused, int type, void *data __unused) 179 { 180 switch(type) { 181 case MOD_LOAD: 182 if (bootverbose) 183 printf("null: <full device, null device, zero device>\n"); 184 full_dev = make_dev_credf(MAKEDEV_ETERNAL_KLD, &full_cdevsw, 0, 185 NULL, UID_ROOT, GID_WHEEL, 0666, "full"); 186 null_dev = make_dev_credf(MAKEDEV_ETERNAL_KLD, &null_cdevsw, 0, 187 NULL, UID_ROOT, GID_WHEEL, 0666, "null"); 188 zero_dev = make_dev_credf(MAKEDEV_ETERNAL_KLD, &zero_cdevsw, 0, 189 NULL, UID_ROOT, GID_WHEEL, 0666, "zero"); 190 break; 191 192 case MOD_UNLOAD: 193 destroy_dev(full_dev); 194 destroy_dev(null_dev); 195 destroy_dev(zero_dev); 196 break; 197 198 case MOD_SHUTDOWN: 199 break; 200 201 default: 202 return (EOPNOTSUPP); 203 } 204 205 return (0); 206 } 207 208 DEV_MODULE(null, null_modevent, NULL); 209 MODULE_VERSION(null, 1); 210