lib.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. #ifndef _LIB_H_
  30. #define _LIB_H_
  31. /*
  32. * Miscellaneous standard C functions for the kernel, and other widely used
  33. * kernel functions.
  34. *
  35. * Note: setjmp and longjmp are in <setjmp.h>.
  36. */
  37. #include <cdefs.h>
  38. /*
  39. * Assert macros.
  40. *
  41. * KASSERT and DEBUGASSERT are the same, except that they can be
  42. * toggled independently. DEBUGASSERT is used in places where making
  43. * checks is likely to be expensive and relatively unlikely to be
  44. * helpful.
  45. *
  46. * Note that there's also a COMPILE_ASSERT for compile-time checks;
  47. * it's in <cdefs.h>.
  48. *
  49. * Regular assertions (KASSERT) are disabled by the kernel config
  50. * option "noasserts". DEBUGASSERT could be controlled by kernel
  51. * config also, but since it's likely to be wanted only rarely during
  52. * testing there doesn't seem much point; one can just edit this file
  53. * temporarily instead.
  54. */
  55. #include "opt-noasserts.h"
  56. #if OPT_NOASSERTS
  57. #define KASSERT(expr) ((void)(expr))
  58. #else
  59. #define KASSERT(expr) \
  60. ((expr) ? (void)0 : badassert(#expr, __FILE__, __LINE__, __func__))
  61. #endif
  62. #if 1 /* no debug asserts */
  63. #define DEBUGASSERT(expr) ((void)(expr))
  64. #else
  65. #define DEBUGASSERT(expr) \
  66. ((expr) ? (void)0 : badassert(#expr, __FILE__, __LINE__, __func__))
  67. #endif
  68. /*
  69. * Bit flags for DEBUG()
  70. */
  71. #define DB_LOCORE 0x0001
  72. #define DB_SYSCALL 0x0002
  73. #define DB_INTERRUPT 0x0004
  74. #define DB_DEVICE 0x0008
  75. #define DB_THREADS 0x0010
  76. #define DB_VM 0x0020
  77. #define DB_EXEC 0x0040
  78. #define DB_VFS 0x0080
  79. #define DB_SFS 0x0100
  80. #define DB_NET 0x0200
  81. #define DB_NETFS 0x0400
  82. #define DB_KMALLOC 0x0800
  83. #define DB_SYNCPROB 0x1000
  84. extern uint32_t dbflags;
  85. /*
  86. * DEBUG() is for conditionally printing debug messages to the console.
  87. *
  88. * The idea is that you put lots of lines of the form
  89. *
  90. * DEBUG(DB_VM, "VM free pages: %u\n", free_pages);
  91. *
  92. * throughout the kernel; then you can toggle whether these messages
  93. * are printed or not at runtime by setting the value of dbflags with
  94. * the debugger.
  95. *
  96. * Unfortunately, as of this writing, there are only a very few such
  97. * messages actually present in the system yet. Feel free to add more.
  98. *
  99. * DEBUG is a varargs macro. These were added to the language in C99.
  100. */
  101. #define DEBUG(d, ...) ((dbflags & (d)) ? kprintf(__VA_ARGS__) : 0)
  102. /*
  103. * Random number generator, using the random device.
  104. *
  105. * random() returns a number between 0 and randmax() inclusive.
  106. */
  107. #define RANDOM_MAX (randmax())
  108. uint32_t randmax(void);
  109. uint32_t random(void);
  110. /*
  111. * Kernel heap memory allocation. Like malloc/free.
  112. * If out of memory, kmalloc returns NULL.
  113. */
  114. void *kmalloc(size_t size);
  115. void kfree(void *ptr);
  116. void kheap_printstats(void);
  117. /*
  118. * C string functions.
  119. *
  120. * kstrdup is like strdup, but calls kmalloc instead of malloc.
  121. * If out of memory, it returns NULL.
  122. */
  123. size_t strlen(const char *str);
  124. int strcmp(const char *str1, const char *str2);
  125. char *strcpy(char *dest, const char *src);
  126. char *strcat(char *dest, const char *src);
  127. char *kstrdup(const char *str);
  128. char *strchr(const char *searched, int searchfor);
  129. char *strrchr(const char *searched, int searchfor);
  130. char *strtok_r(char *buf, const char *seps, char **context);
  131. void *memcpy(void *dest, const void *src, size_t len);
  132. void *memmove(void *dest, const void *src, size_t len);
  133. void bzero(void *ptr, size_t len);
  134. int atoi(const char *str);
  135. int snprintf(char *buf, size_t maxlen, const char *fmt, ...) __PF(3,4);
  136. const char *strerror(int errcode);
  137. /*
  138. * Low-level console access.
  139. *
  140. * putch_prepare and putch_complete should be called around a series
  141. * of putch() calls, if printing in polling mode is a possibility.
  142. * kprintf does this.
  143. */
  144. void putch(int ch);
  145. void putch_prepare(void);
  146. void putch_complete(void);
  147. int getch(void);
  148. void beep(void);
  149. /*
  150. * Higher-level console output.
  151. *
  152. * kprintf is like printf, only in the kernel.
  153. * panic prepends the string "panic: " to the message printed, and then
  154. * resets the system.
  155. * badassert calls panic in a way suitable for an assertion failure.
  156. * kgets is like gets, only with a buffer size argument.
  157. *
  158. * kprintf_bootstrap sets up a lock for kprintf and should be called
  159. * during boot once malloc is available and before any additional
  160. * threads are created.
  161. */
  162. int kprintf(const char *format, ...) __PF(1,2);
  163. void panic(const char *format, ...) __PF(1,2);
  164. void badassert(const char *expr, const char *file, int line, const char *func);
  165. void kgets(char *buf, size_t maxbuflen);
  166. void kprintf_bootstrap(void);
  167. /*
  168. * Other miscellaneous stuff
  169. */
  170. #define DIVROUNDUP(a,b) (((a)+(b)-1)/(b))
  171. #define ROUNDUP(a,b) (DIVROUNDUP(a,b)*b)
  172. #endif /* _LIB_H_ */