1 /*- 2 * Copyright (c) 2000 Mark R. V. Murray & Jeroen C. van Gelderen 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer 10 * in this position and unchanged. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 * 26 */ 27 28 #include <sys/cdefs.h> 29 __FBSDID("$FreeBSD$"); 30 31 #include <sys/param.h> 32 #include <sys/systm.h> 33 #include <sys/conf.h> 34 #include <sys/uio.h> 35 #include <sys/kernel.h> 36 #include <sys/malloc.h> 37 #include <sys/module.h> 38 #include <sys/disk.h> 39 #include <sys/bus.h> 40 #include <machine/bus.h> 41 42 /* For use with destroy_dev(9). */ 43 static dev_t null_dev; 44 static dev_t zero_dev; 45 46 static d_write_t null_write; 47 static d_ioctl_t null_ioctl; 48 static d_read_t zero_read; 49 50 #define CDEV_MAJOR 2 51 #define NULL_MINOR 2 52 #define ZERO_MINOR 12 53 54 static struct cdevsw null_cdevsw = { 55 .d_version = D_VERSION, 56 .d_read = (d_read_t *)nullop, 57 .d_write = null_write, 58 .d_ioctl = null_ioctl, 59 .d_name = "null", 60 .d_maj = CDEV_MAJOR, 61 }; 62 63 static struct cdevsw zero_cdevsw = { 64 .d_version = D_VERSION, 65 .d_read = zero_read, 66 .d_write = null_write, 67 .d_name = "zero", 68 .d_maj = CDEV_MAJOR, 69 .d_flags = D_MMAP_ANON, 70 }; 71 72 static void *zbuf; 73 74 /* ARGSUSED */ 75 static int 76 null_write(dev_t dev __unused, struct uio *uio, int flags __unused) 77 { 78 uio->uio_resid = 0; 79 return 0; 80 } 81 82 /* ARGSUSED */ 83 static int 84 null_ioctl(dev_t dev __unused, u_long cmd, caddr_t data __unused, 85 int flags __unused, struct thread *td) 86 { 87 int error; 88 89 if (cmd != DIOCSKERNELDUMP) 90 return (ENOIOCTL); 91 error = suser(td); 92 if (error) 93 return (error); 94 return (set_dumper(NULL)); 95 } 96 97 /* ARGSUSED */ 98 static int 99 zero_read(dev_t dev __unused, struct uio *uio, int flags __unused) 100 { 101 int c; 102 int error = 0; 103 104 while (uio->uio_resid > 0 && error == 0) { 105 c = uio->uio_resid < PAGE_SIZE ? uio->uio_resid : PAGE_SIZE; 106 error = uiomove(zbuf, c, uio); 107 } 108 return error; 109 } 110 111 /* ARGSUSED */ 112 static int 113 null_modevent(module_t mod __unused, int type, void *data __unused) 114 { 115 switch(type) { 116 case MOD_LOAD: 117 if (bootverbose) 118 printf("null: <null device, zero device>\n"); 119 zbuf = (void *)malloc(PAGE_SIZE, M_TEMP, M_WAITOK | M_ZERO); 120 zero_dev = make_dev(&zero_cdevsw, ZERO_MINOR, UID_ROOT, 121 GID_WHEEL, 0666, "zero"); 122 null_dev = make_dev(&null_cdevsw, NULL_MINOR, UID_ROOT, 123 GID_WHEEL, 0666, "null"); 124 return 0; 125 126 case MOD_UNLOAD: 127 destroy_dev(null_dev); 128 destroy_dev(zero_dev); 129 free(zbuf, M_TEMP); 130 return 0; 131 132 case MOD_SHUTDOWN: 133 return 0; 134 135 default: 136 return EOPNOTSUPP; 137 } 138 } 139 140 DEV_MODULE(null, null_modevent, NULL); 141