Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
script3r
GitHub Repository: script3r/os161
Path: blob/master/kern/include/lib.h
2093 views
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 0x001
80
#define DB_SYSCALL 0x002
81
#define DB_INTERRUPT 0x004
82
#define DB_DEVICE 0x008
83
#define DB_THREADS 0x010
84
#define DB_VM 0x020
85
#define DB_EXEC 0x040
86
#define DB_VFS 0x080
87
#define DB_SFS 0x100
88
#define DB_NET 0x200
89
#define DB_NETFS 0x400
90
#define DB_KMALLOC 0x800
91
92
extern uint32_t dbflags;
93
94
/*
95
* DEBUG() is for conditionally printing debug messages to the console.
96
*
97
* The idea is that you put lots of lines of the form
98
*
99
* DEBUG(DB_VM, "VM free pages: %u\n", free_pages);
100
*
101
* throughout the kernel; then you can toggle whether these messages
102
* are printed or not at runtime by setting the value of dbflags with
103
* the debugger.
104
*
105
* Unfortunately, as of this writing, there are only a very few such
106
* messages actually present in the system yet. Feel free to add more.
107
*
108
* DEBUG is a varargs macro. These were added to the language in C99.
109
*/
110
#define DEBUG(d, ...) ((dbflags & (d)) ? kprintf(__VA_ARGS__) : 0)
111
112
/*
113
* Random number generator, using the random device.
114
*
115
* random() returns a number between 0 and randmax() inclusive.
116
*/
117
#define RANDOM_MAX (randmax())
118
uint32_t randmax(void);
119
uint32_t random(void);
120
121
/*
122
* Kernel heap memory allocation. Like malloc/free.
123
* If out of memory, kmalloc returns NULL.
124
*/
125
void *kmalloc(size_t size);
126
void kfree(void *ptr);
127
void kheap_printstats(void);
128
129
/*
130
* C string functions.
131
*
132
* kstrdup is like strdup, but calls kmalloc instead of malloc.
133
* If out of memory, it returns NULL.
134
*/
135
size_t strlen(const char *str);
136
int strcmp(const char *str1, const char *str2);
137
char *strcpy(char *dest, const char *src);
138
char *strcat(char *dest, const char *src);
139
char *kstrdup(const char *str);
140
char *strchr(const char *searched, int searchfor);
141
char *strrchr(const char *searched, int searchfor);
142
char *strtok_r(char *buf, const char *seps, char **context);
143
144
void *memcpy(void *dest, const void *src, size_t len);
145
void *memmove(void *dest, const void *src, size_t len);
146
void bzero(void *ptr, size_t len);
147
int atoi(const char *str);
148
149
int snprintf(char *buf, size_t maxlen, const char *fmt, ...) __PF(3,4);
150
151
const char *strerror(int errcode);
152
153
/*
154
* Low-level console access.
155
*
156
* putch_prepare and putch_complete should be called around a series
157
* of putch() calls, if printing in polling mode is a possibility.
158
* kprintf does this.
159
*/
160
void putch(int ch);
161
void putch_prepare(void);
162
void putch_complete(void);
163
int getch(void);
164
void beep(void);
165
166
/*
167
* Higher-level console output.
168
*
169
* kprintf is like printf, only in the kernel.
170
* panic prepends the string "panic: " to the message printed, and then
171
* resets the system.
172
* badassert calls panic in a way suitable for an assertion failure.
173
* kgets is like gets, only with a buffer size argument.
174
*
175
* kprintf_bootstrap sets up a lock for kprintf and should be called
176
* during boot once malloc is available and before any additional
177
* threads are created.
178
*/
179
int kprintf(const char *format, ...) __PF(1,2);
180
void panic(const char *format, ...) __PF(1,2);
181
void badassert(const char *expr, const char *file, int line, const char *func);
182
183
void kgets(char *buf, size_t maxbuflen);
184
185
void kprintf_bootstrap(void);
186
187
/*
188
* Other miscellaneous stuff
189
*/
190
191
#define DIVROUNDUP(a,b) (((a)+(b)-1)/(b))
192
#define ROUNDUP(a,b) (DIVROUNDUP(a,b)*b)
193
194
195
#endif /* _LIB_H_ */
196
197