/*1* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 20092* The President and Fellows of Harvard College.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12* 3. Neither the name of the University nor the names of its contributors13* may be used to endorse or promote products derived from this software14* without specific prior written permission.15*16* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND17* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE18* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE19* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE20* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL21* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS22* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)23* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT24* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY25* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF26* SUCH DAMAGE.27*/2829#ifndef _LIB_H_30#define _LIB_H_3132/*33* Miscellaneous standard C functions for the kernel, and other widely used34* kernel functions.35*36* Note: setjmp and longjmp are in <setjmp.h>.37*/383940#include <cdefs.h>4142/*43* Assert macros.44*45* KASSERT and DEBUGASSERT are the same, except that they can be46* toggled independently. DEBUGASSERT is used in places where making47* checks is likely to be expensive and relatively unlikely to be48* helpful.49*50* Note that there's also a COMPILE_ASSERT for compile-time checks;51* it's in <cdefs.h>.52*53* Regular assertions (KASSERT) are disabled by the kernel config54* option "noasserts". DEBUGASSERT could be controlled by kernel55* config also, but since it's likely to be wanted only rarely during56* testing there doesn't seem much point; one can just edit this file57* temporarily instead.58*/59#include "opt-noasserts.h"6061#if OPT_NOASSERTS62#define KASSERT(expr) ((void)(expr))63#else64#define KASSERT(expr) \65((expr) ? (void)0 : badassert(#expr, __FILE__, __LINE__, __func__))66#endif6768#if 1 /* no debug asserts */69#define DEBUGASSERT(expr) ((void)(expr))70#else71#define DEBUGASSERT(expr) \72((expr) ? (void)0 : badassert(#expr, __FILE__, __LINE__, __func__))73#endif7475/*76* Bit flags for DEBUG()77*/78#define DB_LOCORE 0x00179#define DB_SYSCALL 0x00280#define DB_INTERRUPT 0x00481#define DB_DEVICE 0x00882#define DB_THREADS 0x01083#define DB_VM 0x02084#define DB_EXEC 0x04085#define DB_VFS 0x08086#define DB_SFS 0x10087#define DB_NET 0x20088#define DB_NETFS 0x40089#define DB_KMALLOC 0x8009091extern uint32_t dbflags;9293/*94* DEBUG() is for conditionally printing debug messages to the console.95*96* The idea is that you put lots of lines of the form97*98* DEBUG(DB_VM, "VM free pages: %u\n", free_pages);99*100* throughout the kernel; then you can toggle whether these messages101* are printed or not at runtime by setting the value of dbflags with102* the debugger.103*104* Unfortunately, as of this writing, there are only a very few such105* messages actually present in the system yet. Feel free to add more.106*107* DEBUG is a varargs macro. These were added to the language in C99.108*/109#define DEBUG(d, ...) ((dbflags & (d)) ? kprintf(__VA_ARGS__) : 0)110111/*112* Random number generator, using the random device.113*114* random() returns a number between 0 and randmax() inclusive.115*/116#define RANDOM_MAX (randmax())117uint32_t randmax(void);118uint32_t random(void);119120/*121* Kernel heap memory allocation. Like malloc/free.122* If out of memory, kmalloc returns NULL.123*/124void *kmalloc(size_t size);125void kfree(void *ptr);126void kheap_printstats(void);127128/*129* C string functions.130*131* kstrdup is like strdup, but calls kmalloc instead of malloc.132* If out of memory, it returns NULL.133*/134size_t strlen(const char *str);135int strcmp(const char *str1, const char *str2);136char *strcpy(char *dest, const char *src);137char *strcat(char *dest, const char *src);138char *kstrdup(const char *str);139char *strchr(const char *searched, int searchfor);140char *strrchr(const char *searched, int searchfor);141char *strtok_r(char *buf, const char *seps, char **context);142143void *memcpy(void *dest, const void *src, size_t len);144void *memmove(void *dest, const void *src, size_t len);145void bzero(void *ptr, size_t len);146int atoi(const char *str);147148int snprintf(char *buf, size_t maxlen, const char *fmt, ...) __PF(3,4);149150const char *strerror(int errcode);151152/*153* Low-level console access.154*155* putch_prepare and putch_complete should be called around a series156* of putch() calls, if printing in polling mode is a possibility.157* kprintf does this.158*/159void putch(int ch);160void putch_prepare(void);161void putch_complete(void);162int getch(void);163void beep(void);164165/*166* Higher-level console output.167*168* kprintf is like printf, only in the kernel.169* panic prepends the string "panic: " to the message printed, and then170* resets the system.171* badassert calls panic in a way suitable for an assertion failure.172* kgets is like gets, only with a buffer size argument.173*174* kprintf_bootstrap sets up a lock for kprintf and should be called175* during boot once malloc is available and before any additional176* threads are created.177*/178int kprintf(const char *format, ...) __PF(1,2);179void panic(const char *format, ...) __PF(1,2);180void badassert(const char *expr, const char *file, int line, const char *func);181182void kgets(char *buf, size_t maxbuflen);183184void kprintf_bootstrap(void);185186/*187* Other miscellaneous stuff188*/189190#define DIVROUNDUP(a,b) (((a)+(b)-1)/(b))191#define ROUNDUP(a,b) (DIVROUNDUP(a,b)*b)192193194#endif /* _LIB_H_ */195196197