1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
|
.\" $OpenBSD: kern.9,v 1.21 2014/12/09 14:41:00 jmc Exp $
.\"
.\" Copyright (c) 2002, 2003 CubeSoft Communications, Inc.
.\" <http://www.csoft.org>
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
.\" WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
.\" INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
.\" (INCLUDING BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
.\" SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
.\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
.\" IN ANY WAY OUT OF THE USE OF THIS SOFTWARE EVEN IF ADVISED OF THE
.\" POSSIBILITY OF SUCH DAMAGE.
.\"
.Dd $Mdocdate: December 9 2014 $
.Dt KERN 9
.Os
.Sh NAME
.Nm kern
.Nd kernel library routines
.Sh SYNOPSIS
.In lib/libkern/libkern.h
.Sh DESCRIPTION
The
.Nm
library implements a set of useful functions and macros inside the kernel.
.Sh MATH
.nr nS 1
.Ft int
.Fn imax "int a" "int b"
.Ft int
.Fn imin "int a" "int b"
.Ft long
.Fn lmax "long a" "long b"
.Ft long
.Fn lmin "long a" "long b"
.Ft u_int
.Fn max "u_int a" "u_int b"
.Ft u_int
.Fn min "u_int a" "u_int b"
.Ft u_long
.Fn ulmax "u_long a" "u_long b"
.Ft u_long
.Fn ulmin "u_long a" "u_long b"
.Ft int
.Fn abs "int j"
.nr nS 0
.Pp
The
.Fn min ,
.Fn imin ,
.Fn lmin
and
.Fn ulmin
functions return the smallest integer between
.Fa a
and
.Fa b ,
inclusive.
The
.Fn max ,
.Fn imax ,
.Fn lmax
and
.Fn ulmax
functions return the largest integer between
.Fa a
and
.Fa b ,
inclusive.
.Pp
The
.Fn abs
function computes the absolute value of integer
.Fa j .
.Sh ASSERTIONS
.nr nS 1
.Ft "void"
.Fn assert "CONDITION"
.Ft "void"
.Fn KASSERT "CONDITION"
.Ft "void"
.Fn KDASSERT "CONDITION"
.Ft "void"
.Fn KASSERTMSG "CONDITION" "fmt" "..."
.Ft "void"
.Fn KDASSERTMSG "CONDITION" "fmt" "..."
.nr nS 0
.Pp
These macros cause kernel
.Xr panic 9
if the given condition evaluates to false.
.Fn assert
tests are always compiled in.
.Fn KASSERT
and
.Fn KASSERTMSG
tests are only included if the kernel has
.Dv DIAGNOSTIC
enabled.
.Fn KDASSERT
and
.Fn KDASSERTMSG
tests are only included if the kernel has
.Dv DEBUG
enabled.
The
.Fn KASSERTMSG
and
.Fn KDASSERTMSG
macros append
to the
.Xr panic 9
format string the message specified by
.Fa format
and its subsequent arguments, similar to
.Xr printf 9
functions.
.Pp
.nr nS 1
.Fn CTASSERT "CONDITION"
.nr nS 0
.Pp
This macro causes a compile time error if the given condition evaluates to
false.
Its main purpose is to verify assertions about type and struct sizes that
would otherwise make the code fail at run time.
.Fn CTASSERT
can be used in global scope or at the start of blocks, where variable
declarations are allowed.
.Sh BYTE STRINGS
.nr nS 1
.Ft int
.Fn skpc "int mask" "size_t size" "u_char *cp"
.Ft int
.Fn scanc "u_int size" "const u_char *cp" "const u_char *table" "int mask"
.Ft int
.Fn bcmp "const void *b1" "const void *b2" "size_t len"
.Ft int
.Fn timingsafe_bcmp "const void *b1" "const void *b2" "size_t len"
.Ft void *
.Fn memchr "const void *b" "int c" "size_t len"
.Ft int
.Fn memcmp "const void *b1" "const void *b2" "size_t len"
.Ft int
.Fn ffs "int value"
.Ft int
.Fn fls "int value"
.Ft int
.Fn flsl "long value"
.nr nS 0
.Pp
The
.Fn skpc
function locates the first unsigned character of value different than
.Fa mask
inside the string
.Fa cp .
.Pp
The
.Fn scanc
function expects a string of indexes into the table
.Fa table .
Each table element is bitwise ANDed against
.Fa mask .
.Pp
.Fn skpc
and
.Fn scanc
expect the string to be of size
.Fa size ,
and return the index relative to the end of the string where the match
occurred, or zero if
.Fa mask
is not present in the string.
.Pp
The
.Fn fls
and
.Fn flsl
functions find the last bit set in
.Fa value
and return the index of that bit.
.Pp
The remaining functions have the same semantics as their libc counterparts,
.Xr bcmp 3 ,
.Xr timingsafe_bcmp 3 ,
.Xr memchr 3 ,
.Xr memcmp 3
and
.Xr ffs 3 .
.Sh CHARACTER STRINGS
.nr nS 1
.Ft size_t
.Fn strlen "const char *s"
.Ft char *
.Fn strncpy "char *dst" "const char *src" "size_t len"
.Ft size_t
.Fn strnlen "const char *s" "size_t maxlen"
.Ft size_t
.Fn strlcpy "char *dst" "const char *src" "size_t size"
.Ft size_t
.Fn strlcat "char *dst" "const char *src" "size_t size"
.Ft int
.Fn strcmp "const char *s1" "const char *s2"
.Ft int
.Fn strncmp "const char *s1" "const char *s2" "size_t len"
.Ft int
.Fn strncasecmp "const char *s1" "const char *s2" "size_tlen"
.nr nS 0
.Pp
Those functions have the same semantics as their libc counterparts,
.Xr strlen 3 ,
.Xr strncpy 3 ,
.Xr strnlen 3 ,
.Xr strlcpy 3 ,
.Xr strlcat 3 ,
.Xr strcmp 3 ,
.Xr strncmp 3
and
.Xr strncasecmp 3 .
.Sh MISCELLANEOUS
.nr nS 1
.Ft int
.Fn getsn "char *cp" "int size"
.nr nS 0
.Pp
The
.Fn getsn
function reads user input from the console and returns on newline.
The result is written into
.Fa cp ,
which is assumed to be
.Fa size
bytes long.
.Sh SEE ALSO
.Xr assert 3 ,
.Xr bcmp 3 ,
.Xr ffs 3 ,
.Xr memchr 3 ,
.Xr memcmp 3
.Sh STANDARDS
The
.Fn abs ,
.Fn memchr ,
.Fn memcmp ,
.Fn strlen ,
.Fn strncpy ,
.Fn strcmp ,
.Fn strncmp
and
.Fn strcasecmp
functions conform to
.St -ansiC .
.Sh HISTORY
The
.Fn skpc
and
.Fn scanc
functions are based on vax instructions of the same name.
The
.Fn KASSERTMSG
and
.Fn KDASSERTMSG
macros are taken from
.Nx .
|