root/kern/include/lib.h

/* [<][>][^][v][top][bottom][index][help] */

INCLUDED FROM


   1 /*
   2  * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
   3  *      The President and Fellows of Harvard College.
   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  * 2. Redistributions in binary form must reproduce the above copyright
  11  *    notice, this list of conditions and the following disclaimer in the
  12  *    documentation and/or other materials provided with the distribution.
  13  * 3. Neither the name of the University nor the names of its contributors
  14  *    may be used to endorse or promote products derived from this software
  15  *    without specific prior written permission.
  16  *
  17  * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
  18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
  21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27  * SUCH DAMAGE.
  28  */
  29 
  30 #ifndef _LIB_H_
  31 #define _LIB_H_
  32 
  33 /*
  34  * Miscellaneous standard C functions for the kernel, and other widely used
  35  * kernel functions.
  36  *
  37  * Note: setjmp and longjmp are in <setjmp.h>.
  38  */
  39 
  40 
  41 #include <cdefs.h>
  42 
  43 /*
  44  * Assert macros.
  45  *
  46  * KASSERT and DEBUGASSERT are the same, except that they can be
  47  * toggled independently. DEBUGASSERT is used in places where making
  48  * checks is likely to be expensive and relatively unlikely to be
  49  * helpful.
  50  *
  51  * Note that there's also a COMPILE_ASSERT for compile-time checks;
  52  * it's in <cdefs.h>.
  53  *
  54  * Regular assertions (KASSERT) are disabled by the kernel config
  55  * option "noasserts". DEBUGASSERT could be controlled by kernel
  56  * config also, but since it's likely to be wanted only rarely during
  57  * testing there doesn't seem much point; one can just edit this file
  58  * temporarily instead.
  59  */
  60 #include "opt-noasserts.h"
  61 
  62 #if OPT_NOASSERTS
  63 #define KASSERT(expr) ((void)(expr))
  64 #else
  65 #define KASSERT(expr) \
  66         ((expr) ? (void)0 : badassert(#expr, __FILE__, __LINE__, __func__))
  67 #endif
  68 
  69 #if 1 /* no debug asserts */
  70 #define DEBUGASSERT(expr) ((void)(expr))
  71 #else
  72 #define DEBUGASSERT(expr) \
  73         ((expr) ? (void)0 : badassert(#expr, __FILE__, __LINE__, __func__))
  74 #endif
  75 
  76 /*
  77  * Bit flags for DEBUG()
  78  */
  79 #define DB_LOCORE      0x0001
  80 #define DB_SYSCALL     0x0002
  81 #define DB_INTERRUPT   0x0004
  82 #define DB_DEVICE      0x0008
  83 #define DB_THREADS     0x0010
  84 #define DB_VM          0x0020
  85 #define DB_EXEC        0x0040
  86 #define DB_VFS         0x0080
  87 #define DB_SFS         0x0100
  88 #define DB_NET         0x0200
  89 #define DB_NETFS       0x0400
  90 #define DB_KMALLOC     0x0800
  91 #define DB_SYNCPROB    0x1000
  92 
  93 extern uint32_t dbflags;
  94 
  95 /*
  96  * DEBUG() is for conditionally printing debug messages to the console.
  97  *
  98  * The idea is that you put lots of lines of the form
  99  *
 100  *      DEBUG(DB_VM, "VM free pages: %u\n", free_pages);
 101  *
 102  * throughout the kernel; then you can toggle whether these messages
 103  * are printed or not at runtime by setting the value of dbflags with
 104  * the debugger.
 105  *
 106  * Unfortunately, as of this writing, there are only a very few such
 107  * messages actually present in the system yet. Feel free to add more.
 108  *
 109  * DEBUG is a varargs macro. These were added to the language in C99.
 110  */
 111 #define DEBUG(d, ...) ((dbflags & (d)) ? kprintf(__VA_ARGS__) : 0)
 112 
 113 /*
 114  * Random number generator, using the random device.
 115  *
 116  * random() returns a number between 0 and randmax() inclusive.
 117  */
 118 #define RANDOM_MAX (randmax())
 119 uint32_t randmax(void);
 120 uint32_t random(void);
 121 
 122 /*
 123  * Kernel heap memory allocation. Like malloc/free.
 124  * If out of memory, kmalloc returns NULL.
 125  */
 126 void *kmalloc(size_t size);
 127 void kfree(void *ptr);
 128 void kheap_printstats(void);
 129 
 130 /*
 131  * C string functions. 
 132  *
 133  * kstrdup is like strdup, but calls kmalloc instead of malloc.
 134  * If out of memory, it returns NULL.
 135  */
 136 size_t strlen(const char *str);
 137 int strcmp(const char *str1, const char *str2);
 138 char *strcpy(char *dest, const char *src);
 139 char *strcat(char *dest, const char *src);
 140 char *kstrdup(const char *str);
 141 char *strchr(const char *searched, int searchfor);
 142 char *strrchr(const char *searched, int searchfor);
 143 char *strtok_r(char *buf, const char *seps, char **context);
 144 
 145 void *memcpy(void *dest, const void *src, size_t len);
 146 void *memmove(void *dest, const void *src, size_t len);
 147 void bzero(void *ptr, size_t len);
 148 int atoi(const char *str);
 149 
 150 int snprintf(char *buf, size_t maxlen, const char *fmt, ...) __PF(3,4);
 151 
 152 const char *strerror(int errcode);
 153 
 154 /*
 155  * Low-level console access.
 156  *
 157  * putch_prepare and putch_complete should be called around a series
 158  * of putch() calls, if printing in polling mode is a possibility.
 159  * kprintf does this.
 160  */
 161 void putch(int ch);
 162 void putch_prepare(void);
 163 void putch_complete(void);
 164 int getch(void);
 165 void beep(void);
 166 
 167 /*
 168  * Higher-level console output.
 169  *
 170  * kprintf is like printf, only in the kernel.
 171  * panic prepends the string "panic: " to the message printed, and then
 172  * resets the system.
 173  * badassert calls panic in a way suitable for an assertion failure.
 174  * kgets is like gets, only with a buffer size argument.
 175  *
 176  * kprintf_bootstrap sets up a lock for kprintf and should be called
 177  * during boot once malloc is available and before any additional
 178  * threads are created.
 179  */
 180 int kprintf(const char *format, ...) __PF(1,2);
 181 void panic(const char *format, ...) __PF(1,2);
 182 void badassert(const char *expr, const char *file, int line, const char *func);
 183 
 184 void kgets(char *buf, size_t maxbuflen);
 185 
 186 void kprintf_bootstrap(void);
 187 
 188 /*
 189  * Other miscellaneous stuff
 190  */
 191 
 192 #define DIVROUNDUP(a,b) (((a)+(b)-1)/(b))
 193 #define ROUNDUP(a,b)    (DIVROUNDUP(a,b)*b)
 194 
 195 
 196 #endif /* _LIB_H_ */

/* [<][>][^][v][top][bottom][index][help] */