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