os161-1.99
 All Data Structures
random.c
00001 /*
00002  * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
00003  *      The President and Fellows of Harvard College.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the University nor the names of its contributors
00014  *    may be used to endorse or promote products derived from this software
00015  *    without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00027  * SUCH DAMAGE.
00028  */
00029 
00030 #include <types.h>
00031 #include <kern/errno.h>
00032 #include <kern/fcntl.h>
00033 #include <lib.h>
00034 #include <uio.h>
00035 #include <vfs.h>
00036 #include <generic/random.h>
00037 #include "autoconf.h"
00038 
00039 /*
00040  * Machine-independent generic randomness device.
00041  *
00042  * Remembers something that's a random source, and provides random()
00043  * and randmax() to the rest of the kernel.
00044  *
00045  * The kernel config mechanism can be used to explicitly choose which
00046  * of the available random sources to use, if more than one is
00047  * available.
00048  */
00049 
00050 static struct random_softc *the_random = NULL;
00051 
00052 /*
00053  * VFS device functions.
00054  * open: allow reading only.
00055  */
00056 static
00057 int
00058 randopen(struct device *dev, int openflags)
00059 {
00060         (void)dev;
00061 
00062         if (openflags != O_RDONLY) {
00063                 return EIO;
00064         }
00065 
00066         return 0;
00067 }
00068 
00069 /*
00070  * VFS close function.
00071  */
00072 static
00073 int
00074 randclose(struct device *dev)
00075 {
00076         (void)dev;
00077         return 0;
00078 }
00079 
00080 /*
00081  * VFS I/O function. Hand off to implementation.
00082  */
00083 static
00084 int
00085 randio(struct device *dev, struct uio *uio)
00086 {
00087         struct random_softc *rs = dev->d_data;
00088 
00089         if (uio->uio_rw != UIO_READ) {
00090                 return EIO;
00091         }
00092 
00093         return rs->rs_read(rs->rs_devdata, uio);
00094 }
00095 
00096 /*
00097  * VFS ioctl function.
00098  */
00099 static
00100 int
00101 randioctl(struct device *dev, int op, userptr_t data)
00102 {
00103         /*
00104          * We don't support any ioctls.
00105          */
00106         (void)dev;
00107         (void)op;
00108         (void)data;
00109         return EIOCTL;
00110 }
00111 
00112 /*
00113  * Config function.
00114  */
00115 int
00116 config_random(struct random_softc *rs, int unit)
00117 {
00118         int result;
00119 
00120         /* We use only the first random device. */
00121         if (unit!=0) {
00122                 return ENODEV;
00123         }
00124 
00125         KASSERT(the_random==NULL);
00126         the_random = rs;
00127 
00128         rs->rs_dev.d_open = randopen;
00129         rs->rs_dev.d_close = randclose;
00130         rs->rs_dev.d_io = randio;
00131         rs->rs_dev.d_ioctl = randioctl;
00132         rs->rs_dev.d_blocks = 0;
00133         rs->rs_dev.d_blocksize = 1;
00134         rs->rs_dev.d_data = rs;
00135 
00136         /* Add the VFS device structure to the VFS device list. */
00137         result = vfs_adddev("random", &rs->rs_dev, 0);
00138         if (result) {
00139                 return result;
00140         }
00141 
00142         return 0;
00143 }
00144 
00145 
00146 /*
00147  * Random number functions exported to the rest of the kernel.
00148  */
00149 
00150 uint32_t
00151 random(void)
00152 {
00153         if (the_random==NULL) {
00154                 panic("No random device\n");
00155         }
00156         return the_random->rs_random(the_random->rs_devdata);
00157 }
00158 
00159 uint32_t
00160 randmax(void)
00161 {
00162         if (the_random==NULL) {
00163                 panic("No random device\n");
00164         }
00165         return the_random->rs_randmax(the_random->rs_devdata);
00166 }
 All Data Structures