1 /*- 2 * Copyright (c) 2000 Mark R. V. Murray & Jeroen C. van Gelderen 3 * Copyright (c) 2001-2004 Mark R. V. Murray 4 * Copyright (c) 2014 Eitan Adler 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer 12 * in this position and unchanged. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * 28 */ 29 30 #include <sys/cdefs.h> 31 __FBSDID("$FreeBSD$"); 32 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 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 int error; 108 error = 0; 109 110 switch (cmd) { 111 case DIOCSKERNELDUMP: 112 error = set_dumper(NULL, NULL, td); 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 148 /* ARGSUSED */ 149 static int 150 zero_read(struct cdev *dev __unused, struct uio *uio, int flags __unused) 151 { 152 void *zbuf; 153 ssize_t len; 154 int error = 0; 155 156 KASSERT(uio->uio_rw == UIO_READ, 157 ("Can't be in %s for write", __func__)); 158 zbuf = __DECONST(void *, zero_region); 159 while (uio->uio_resid > 0 && error == 0) { 160 len = uio->uio_resid; 161 if (len > ZERO_REGION_SIZE) 162 len = ZERO_REGION_SIZE; 163 error = uiomove(zbuf, len, uio); 164 } 165 166 return (error); 167 } 168 169 /* ARGSUSED */ 170 static int 171 null_modevent(module_t mod __unused, int type, void *data __unused) 172 { 173 switch(type) { 174 case MOD_LOAD: 175 if (bootverbose) 176 printf("null: <full device, null device, zero device>\n"); 177 full_dev = make_dev_credf(MAKEDEV_ETERNAL_KLD, &full_cdevsw, 0, 178 NULL, UID_ROOT, GID_WHEEL, 0666, "full"); 179 null_dev = make_dev_credf(MAKEDEV_ETERNAL_KLD, &null_cdevsw, 0, 180 NULL, UID_ROOT, GID_WHEEL, 0666, "null"); 181 zero_dev = make_dev_credf(MAKEDEV_ETERNAL_KLD, &zero_cdevsw, 0, 182 NULL, UID_ROOT, GID_WHEEL, 0666, "zero"); 183 break; 184 185 case MOD_UNLOAD: 186 destroy_dev(full_dev); 187 destroy_dev(null_dev); 188 destroy_dev(zero_dev); 189 break; 190 191 case MOD_SHUTDOWN: 192 break; 193 194 default: 195 return (EOPNOTSUPP); 196 } 197 198 return (0); 199 } 200 201 DEV_MODULE(null, null_modevent, NULL); 202 MODULE_VERSION(null, 1); 203