History log of /freebsd-current/sys/sys/libkern.h
Revision Date Author Comments
# 08f6f78f 03-Jun-2024 Doug Moore <dougm@FreeBSD.org>

libkern: don't use MPASS

Using MPASS in libkern breaks buildworld. Replace MPASS with KASSERT
in three places.


# b0056b31 03-Jun-2024 Doug Moore <dougm@FreeBSD.org>

libkern: add ilog2 macro

The kernel source contains several definitions of an ilog2 function;
some are slower than necessary, and one of them is incorrect.
Elimininate them all and define an ilog2 macro in libkern to replace
them, in a way that is fast, correct for all argument types, and, in a
GENERIC kernel, includes a check for an invalid zero parameter.

Folks at Microsoft have verified that having a correct ilog2
definition for their MANA driver doesn't break it.

Reviewed by: alc, markj, mhorne (older version), jhibbits (older version)
Differential Revision: https://reviews.freebsd.org/D45170
Differential Revision: https://reviews.freebsd.org/D45235


# fdafd315 24-Nov-2023 Warner Losh <imp@FreeBSD.org>

sys: Automated cleanup of cdefs and other formatting

Apply the following automated changes to try to eliminate
no-longer-needed sys/cdefs.h includes as well as now-empty
blank lines in a row.

Remove /^#if.*\n#endif.*\n#include\s+<sys/cdefs.h>.*\n/
Remove /\n+#include\s+<sys/cdefs.h>.*\n+#if.*\n#endif.*\n+/
Remove /\n+#if.*\n#endif.*\n+/
Remove /^#if.*\n#endif.*\n/
Remove /\n+#include\s+<sys/cdefs.h>\n#include\s+<sys/types.h>/
Remove /\n+#include\s+<sys/cdefs.h>\n#include\s+<sys/param.h>/
Remove /\n+#include\s+<sys/cdefs.h>\n#include\s+<sys/capsicum.h>/

Sponsored by: Netflix


# 29363fb4 23-Nov-2023 Warner Losh <imp@FreeBSD.org>

sys: Remove ancient SCCS tags.

Remove ancient SCCS tags from the tree, automated scripting, with two
minor fixup to keep things compiling. All the common forms in the tree
were removed with a perl script.

Sponsored by: Netflix


# 229c65a8 07-Sep-2023 Emmanuel Vadot <manu@FreeBSD.org>

kern: Globally define abs64

So we can use it in non-linuxkpi sources.

Reviewed by: emaste, mmel
Differential Revision: https://reviews.freebsd.org/D41767
Sponsored by: Beckhoff Automation GmbH & Co. KG


# 2ff63af9 16-Aug-2023 Warner Losh <imp@FreeBSD.org>

sys: Remove $FreeBSD$: one-line .h pattern

Remove /^\s*\*+\s*\$FreeBSD\$.*$\n/


# a8926207 06-Jul-2023 Mitchell Horne <mhorne@FreeBSD.org>

Consistently provide ffs/fls using builtins

Use of compiler builtin ffs/ctz functions will result in optimized
instruction sequences when possible, and fall back to calling a function
provided by the compiler run-time library. We have slowly shifted our
platforms to take advantage of these builtins in 60645781d613 (arm64),
1c76d3a9fbef (arm), 9e319462a03a (powerpc, partial).

Some platforms still rely on the libkern implementations of these
functions provided by libkern, namely riscv, powerpc (ffs*, flsll), and
i386 (ffsll and flsll). These routines are slow, as they perform a
linear search for the bit in question. Even on platforms lacking
dedicated bit-search instructions, such as riscv, the compiler library
will provide better-optimized routines, e.g. by using binary search.

Consolidate all definitions of these functions (whether currently using
builtins or not) to libkern.h. This should result in equivalent or
better performing routines in all cases.

One wart in all of this is the existing HAVE_INLINE_F*** macros, which
we use in a few places to conditionally avoid the slow libkern routines.
These aren't easily removed in one commit. For now, provide these
defines unconditionally, but marked for removal after subsequent
cleanup.

Removal of the now unused libkern routines will follow in the next
commit.

Reviewed by: dougm, imp (previous version)
Sponsored by: The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D40698


# af3c7888 30-Sep-2022 Ed Schouten <ed@FreeBSD.org>

Alter the prototype of qsort_r(3) to match POSIX, which adopted the
glibc-based interface.

Unfortunately, the glibc maintainers, despite knowing the existence
of the FreeBSD qsort_r(3) interface in 2004 and refused to add the
same interface to glibc based on grounds of the lack of standardization
and portability concerns, has decided it was a good idea to introduce
their own qsort_r(3) interface in 2007 as a GNU extension with a
slightly different and incompatible interface.

With the adoption of their interface as POSIX standard, let's switch
to the same prototype, there is no need to remain incompatible.

C++ and C applications written for the historical FreeBSD interface
get source level compatibility when building in C++ mode, or when
building with a C compiler with C11 generics support, provided that
the caller passes a fifth parameter of qsort_r() that exactly matches
the historical FreeBSD comparator function pointer type and does not
redefine the historical qsort_r(3) prototype in their source code.

Symbol versioning is used to keep old binaries working.

MFC: never
Relnotes: yes
Reviewed by: cem, imp, hps, pauamma
Differential revision: https://reviews.freebsd.org/D17083


# db0ac6de 02-Dec-2021 Cy Schubert <cy@FreeBSD.org>

Revert "wpa: Import wpa_supplicant/hostapd commit 14ab4a816"

This reverts commit 266f97b5e9a7958e365e78288616a459b40d924a, reversing
changes made to a10253cffea84c0c980a36ba6776b00ed96c3e3b.

A mismerge of a merge to catch up to main resulted in files being
committed which should not have been.


# efa5d383 26-Nov-2021 Hubert Mazur <hum@semihalf.com>

sys/libkern.h: Add type conversion helpers

Add helper functions for 32 and 64 bit unsigned to signed integers
conversions.

Reviewed by:
Sponsored by: Alstom
Obtained from: Semihalf
Differential revision: https://reviews.freebsd.org/D33162


# a90d053b 19-Jul-2021 Mark Johnston <markj@FreeBSD.org>

Simplify kernel sanitizer interceptors

KASAN and KCSAN implement interceptors for various primitive operations
that are not instrumented by the compiler. KMSAN requires them as well.
Rather than adding new cases for each sanitizer which requires
interceptors, implement the following protocol:
- When interceptor definitions are required, define
SAN_NEEDS_INTERCEPTORS and SANITIZER_INTERCEPTOR_PREFIX.
- In headers that declare functions which need to be intercepted by a
sanitizer runtime, use SANITIZER_INTERCEPTOR_PREFIX to provide
declarations.
- When SAN_RUNTIME is defined, do not redefine the names of intercepted
functions. This is typically the case in files which implement
sanitizer runtimes but is also needed in, for example, files which
define ifunc selectors for intercepted operations.

MFC after: 2 weeks
Sponsored by: The FreeBSD Foundation


# f39d9c91 14-Jul-2021 Greg V <greg@unrelenting.technology>

libkern: import strnstr from libc

Required for drm-kmod 5.6 (amdgpu).

Reviewed by: bz, hselasky, nc
Differential Revision: https://reviews.freebsd.org/D31131


# 56d33e86 14-Jul-2021 Alfonso Gregory <gfunni234@gmail.com>

Remove incorrect __restricted labels from strcspn

strcspn should never have had the __restrict keywords. While both of
these strings are const, it may have unindended side effects. While this
is the kernel, the POSIX definition also omits restrict.

Reviewed by: imp@
Pull Request: https://github.com/freebsd/freebsd-src/pull/497


# ec8f1ea8 24-Mar-2021 Mark Johnston <markj@FreeBSD.org>

Generalize sanitizer interceptors for memory and string routines

Similar to commit 3ead60236f ("Generalize bus_space(9) and atomic(9)
sanitizer interceptors"), use a more generic scheme for interposing
sanitizer implementations of routines like memcpy().

No functional change intended.

MFC after: 1 month
Sponsored by: The FreeBSD Foundation


# 81e074d5 07-Feb-2021 Mateusz Guzik <mjg@FreeBSD.org>

libkern: use compiler builtins for strcpy, strcmp and strlen


# bc861033 06-Oct-2020 Vladimir Kondratyev <wulf@FreeBSD.org>

libkern/strcasestr.c: Drop xlocale support and connect to build.

Reviewed by: markj, hselasky
Differential revision: https://reviews.freebsd.org/D27866


# 10b1a175 10-Oct-2020 Conrad Meyer <cem@FreeBSD.org>

arc4random(9): Integrate with RANDOM_FENESTRASX push-reseed

There is no functional change for the existing Fortuna random(4)
implementation, which remains the default in GENERIC.

In the FenestrasX model, when the root CSPRNG is reseeded from pools due to
an (infrequent) timer, child CSPRNGs can cheaply detect this condition and
reseed. To do so, they just need to track an additional 64-bit value in the
associated state, and compare it against the root seed version (generation)
on random reads.

This revision integrates arc4random(9) into that model without substantially
changing the design or implementation of arc4random(9). The motivation is
that arc4random(9) is immediately reseeded when the backing random(4)
implementation has additional entropy. This is arguably most important
during boot, when fenestrasX is reseeding at 1, 3, 9, 27, etc., second
intervals. Today, arc4random(9) has a hardcoded 300 second reseed window.
Without this mechanism, if arc4random(9) gets weak entropy during initial
seed (and arc4random(9) is used early in boot, so this is quite possible),
it may continue to emit poorly seeded output for 5 minutes. The FenestrasX
push-reseed scheme corrects consumers, like arc4random(9), as soon as
possible.

Reviewed by: markm
Approved by: csprng (markm)
Differential Revision: https://reviews.freebsd.org/D22838


# f6e54eb3 01-Sep-2020 Mateusz Guzik <mjg@FreeBSD.org>

sys: clean up empty lines in .c and .h files


# 353d02e9 23-May-2020 Emmanuel Vadot <manu@FreeBSD.org>

libkern: Add arc4random_uniform

This variant get a random number up to the limit passed as the argument.
This is simply a copy of the libc version.

Sponsored-by: The FreeBSD Foundation
Reviewed by: cem, hselasky (previous version)
Differential Revision: https://reviews.freebsd.org/D24962


# f3bae413 26-Dec-2019 Conrad Meyer <cem@FreeBSD.org>

random(9): Deprecate random(9), remove meaningless srandom(9)

srandom(9) is meaningless on SMP systems or any system with, say,
interrupts. One could never rely on random(9) to produce a reproducible
sequence of outputs on the basis of a specific srandom() seed because the
global state was shared by all kernel contexts. As such, removing it is
literally indistinguishable to random(9) consumers (as compared with
retaining it).

Mark random(9) as deprecated and slated for quick removal. This is not to
say we intend to remove all fast, non-cryptographic PRNG(s) in the kernel.
It/they just won't be random(9), as it exists today, in either name or
implementation.

Before random(9) is removed, a replacement will be provided and in-tree
consumers will be converted.

Note that despite the name, the random(9) interface does not bear any
resemblance to random(3). Instead, it is the same crummy 1988 Park-Miller
LCG used in libc rand(3).


# 849aef49 21-Nov-2019 Andrew Turner <andrew@FreeBSD.org>

Port the NetBSD KCSAN runtime to FreeBSD.

Update the NetBSD Kernel Concurrency Sanitizer (KCSAN) runtime to work in
the FreeBSD kernel. It is a useful tool for finding data races between
threads executing on different CPUs.

This can be enabled by enabling KCSAN in the kernel config, or by using the
GENERIC-KCSAN amd64 kernel. It works on amd64 and arm64, however the later
needs a compiler change to allow -fsanitize=thread that KCSAN uses.

Sponsored by: DARPA, AFRL
Differential Revision: https://reviews.freebsd.org/D22315


# 2b0ebb77 19-Aug-2019 Conrad Meyer <cem@FreeBSD.org>

libkern: Implement strchrnul(3)


# f89d2072 17-Jun-2019 Xin LI <delphij@FreeBSD.org>

Separate kernel crc32() implementation to its own header (gsb_crc32.h) and
rename the source to gsb_crc32.c.

This is a prerequisite of unifying kernel zlib instances.

PR: 229763
Submitted by: Yoshihiro Ota <ota at j.email.ne.jp>
Differential Revision: https://reviews.freebsd.org/D20193


# 6b97c2e3 19-Apr-2019 Conrad Meyer <cem@FreeBSD.org>

Revert r346410 and r346411

libkern in .PATH has too many filename conflicts with libc and my -DNO_CLEAN
tinderbox didn't catch that ahead of time. Mea culpa.


# 7deb4b19 19-Apr-2019 Conrad Meyer <cem@FreeBSD.org>

libkern: Bring in arc4random_uniform(9) from libc

It is a useful arc4random wrapper in the kernel for much the same reasons as
in userspace. Move the source to libkern (because kernel build is
restricted to sys/, but userspace can include any file it likes) and build
kernel and libc versions from the same source file.

Copy the documentation from arc4random_uniform(3) to the section 9 page.

While here, add missing arc4random_buf(9) symlink.

Sponsored by: Dell EMC Isilon


# 744799ea 20-Feb-2019 Matt Macy <mmacy@FreeBSD.org>

Add non-sleepable strdup variant strdup_flags

debugfs expects to do non-sleepable allocations

Reviewed by: hps@
MFC after: 1 week
Sponsored by: iX Systems
Differential Revision: https://reviews.freebsd.org/D19259


# ba96f377 02-Jun-2018 Mateusz Guzik <mjg@FreeBSD.org>

Use __builtin for various mem* and b* (e.g. bzero) routines.

Some of the routines were using artificially limited builtin already,
drop the explicit limit.

The use of builtins allows quite often allows the compiler to elide the call
or most zeroing to begin with. For instance, if the target object is 32 bytes
in size and gets zeroed + has 16 bytes initialized, the compiler can just
add code to zero out the rest.

Note not all the primites have asm variants and some of the existing ones
are not optimized. Maintaines are strongly encourage to take a look
(regardless of this change).


# 97e89848 02-Jun-2018 Mateusz Guzik <mjg@FreeBSD.org>

libkern: tidy up memset

1. Remove special-casing of 0 as it just results in an extra function call.
This is clearly pessimal.
2. Drop the inline stuff. For the most part it is much better served with
__builtin_memset (coming later).
3. Move the declaration to systm.h to match other funcs.

Archs are encouraged to implement the variant for their own platform so that
this implementation can be dropped.


# b6f4732c 31-Dec-2017 Ian Lepore <ian@FreeBSD.org>

Add a validbcd() routine that uses the bcd2bin_data[] array and returns a
bool indicating whether the input value represents a valid BCD byte.

The existing bcd2bin() routine will KASSERT if asked to convert a bad value,
but sometimes the kernel has to handle BCD data from untrusted sources, so
this will provide a mechanism to validate data before attempting conversion.

This would be have easier/cleaner if the bcd2bin_data[] array contained an
out-of-range value (such as 0xff) in the infill locations that aren't valid,
but it's a global symbol that might be referenced by out-of-tree code
relying on the current scheme, so I'm leaving that alone.


# 51369649 20-Nov-2017 Pedro F. Giffuni <pfg@FreeBSD.org>

sys: further adoption of SPDX licensing ID tags.

Mainly focus on files that use BSD 3-Clause license.

The Software Package Data Exchange (SPDX) group provides a specification
to make it easier for automated tools to detect and summarize well known
opensource licenses. We are gradually adopting the specification, noting
that the tags are considered only advisory and do not, in any way,
superceed or replace the license texts.

Special thanks to Wind River for providing access to "The Duke of
Highlander" tool: an older (2014) run over FreeBSD tree was useful as a
starting point.


# d7f27102 27-Apr-2017 Michael Tuexen <tuexen@FreeBSD.org>

armv8 has support for optional CRC32C instructions. This patch checks if they are
available and if that is true make use of them.
Thank you very much to Andrew Turner for providing help and review the patch!
Reviewed by: andrew
MFC after: 1 week
Differential Revision: https://reviews.freebsd.org/D10499


# 150890b0 16-Apr-2017 Mark Murray <markm@FreeBSD.org>

Replace the RC4 algorithm for generating in-kernel secure random
numbers with Chacha20. Keep the API, though, as that is what the
other *BSD's have done.

Use the boot-time entropy stash (if present) to bootstrap the
in-kernel entropy source.

Reviewed by: delphij,rwatson
Approved by: so(delphij)
MFC after: 2 months
Relnotes: yes
Differential Revision: https://reviews.freebsd.org/D10048


# fbbd9655 28-Feb-2017 Warner Losh <imp@FreeBSD.org>

Renumber copyright clause 4

Renumber cluase 4 to 3, per what everybody else did when BSD granted
them permission to remove clause 3. My insistance on keeping the same
numbering for legal reasons is too pedantic, so give up on that point.

Submitted by: Jan Schaumann <jschauma@stevens.edu>
Pull Request: https://github.com/freebsd/freebsd/pull/96


# 6be2ff7d 30-Jan-2017 Conrad Meyer <cem@FreeBSD.org>

calculate_crc32c: Add SSE4.2 implementation on x86

Derived from an implementation by Mark Adler.

The fast loop performs three simultaneous CRCs over subsets of the data
before composing them. This takes advantage of certain properties of
the CRC32 implementation in Intel hardware. (The CRC instruction takes 1
cycle but has 2-3 cycles of latency.)

The CRC32 instruction does not manipulate FPU state.

i386 does not have the crc32q instruction, so avoid it there. Otherwise
the implementation is identical to amd64.

Add basic userland tests to verify correctness on a variety of inputs.

PR: 216467
Reported by: Ben RUBSON <ben.rubson at gmail.com>
Reviewed by: kib@, markj@ (earlier version)
Sponsored by: Dell EMC Isilon
Differential Revision: https://reviews.freebsd.org/D9342


# 90a79ac5 24-Jan-2017 Conrad Meyer <cem@FreeBSD.org>

Use time_t for intermediate values to avoid overflow in clock_ts_to_ct

Add additionally safety and overflow checks to clock_ts_to_ct and the
BCD routines while we're here.

Perform a safety check in sys_clock_settime() first to avoid easy local
root panic, without having to propagate an error value back through
dozens of APIs currently lacking error returns.

PR: 211960, 214300
Submitted by: Justin McOmie <justin.mcomie at gmail.com>, kib@
Reported by: Tim Newsham <tim.newsham at nccgroup.trust>
Reviewed by: kib@
Sponsored by: Dell EMC Isilon, FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D9279


# 7b54043f 17-Mar-2016 Justin Hibbits <jhibbits@FreeBSD.org>

Add ummax()/ummin() to libkern.

This is committed in isolation from a larger patch so that it can be MFC'd
separately if needed.


# b3936ebe 23-Dec-2015 Justin Hibbits <jhibbits@FreeBSD.org>

Extend Book-E to support >4GB RAM

Summary:
With some additional changes for AIM, that could also support much
larger physmem sizes. Given that 32-bit AIM is more or less obsolete, though,
it's not worth it at this time.

Differential Revision: https://reviews.freebsd.org/D4345


# 3d3e385e 22-Oct-2015 Conrad Meyer <cem@FreeBSD.org>

Add libkern ffsll() for parity with flsll()

Sponsored by: EMC / Isilon Storage Division
Differential Revision: https://reviews.freebsd.org/D3962


# 589b2c1c 20-Mar-2015 John Baldwin <jhb@FreeBSD.org>

Expand the bitcount* API to support 64-bit integers, plain ints and longs
and create a "hidden" API that can be used in other system headers without
adding namespace pollution.
- If the POPCNT instruction is enabled at compile time, use
__builtin_popcount*() to implement __bitcount*(), otherwise fall back
to software implementations.
- Use the existing bitcount16() and bitcount32() from <sys/systm.h> to
implement the non-POPCNT __bitcount16() and __bitcount32() in
<sys/types.h>.
- For the non-POPCNT __bitcount64(), use a similar SWAR method on 64-bit
systems. For 32-bit systems, use two __bitcount32() operations on the
two halves.
- Use __bitcount32() to provide a __bitcount() that operates on plain ints.
- Use either __bitcount32() or __bitcount64() to provide a
__bitcountl() that operates on longs.
- Add public bitcount*() wrappers for __bitcount*() for use in the kernel
in <sys/libkern.h>.
- Use __builtinl() instead of __builtin_popcountl() in BIT_COUNT().

Discussed with: bde


# 08fca7a5 12-Dec-2014 John-Mark Gurney <jmg@FreeBSD.org>

Add some new modes to OpenCrypto. These modes are AES-ICM (can be used
for counter mode), and AES-GCM. Both of these modes have been added to
the aesni module.

Included is a set of tests to validate that the software and aesni
module calculate the correct values. These use the NIST KAT test
vectors. To run the test, you will need to install a soon to be
committed port, nist-kat that will install the vectors. Using a port
is necessary as the test vectors are around 25MB.

All the man pages were updated. I have added a new man page, crypto.7,
which includes a description of how to use each mode. All the new modes
and some other AES modes are present. It would be good for someone
else to go through and document the other modes.

A new ioctl was added to support AEAD modes which AES-GCM is one of them.
Without this ioctl, it is not possible to test AEAD modes from userland.

Add a timing safe bcmp for use to compare MACs. Previously we were using
bcmp which could leak timing info and result in the ability to forge
messages.

Add a minor optimization to the aesni module so that single segment
mbufs don't get copied and instead are updated in place. The aesni
module needs to be updated to support blocked IO so segmented mbufs
don't have to be copied.

We require that the IV be specified for all calls for both GCM and ICM.
This is to ensure proper use of these functions.

Obtained from: p4: //depot/projects/opencrypto
Relnotes: yes
Sponsored by: FreeBSD Foundation
Sponsored by: NetGate


# 27ecc2ad 05-Sep-2014 Benno Rice <benno@FreeBSD.org>

Add support for gdb's memory searching capabilities to our in-kernel gdb
server.

Submitted by: Daniel O'Connor <daniel.oconnor@isilon.com>
Reviewed by: jhb
Sponsored by: EMC Isilon Storage Division


# 3914ddf8 17-Aug-2014 Edward Tomasz Napierala <trasz@FreeBSD.org>

Bring in the new automounter, similar to what's provided in most other
UNIX systems, eg. MacOS X and Solaris. It uses Sun-compatible map format,
has proper kernel support, and LDAP integration.

There are still a few outstanding problems; they will be fixed shortly.

Reviewed by: allanjude@, emaste@, kib@, wblock@ (earlier versions)
Phabric: D523
MFC after: 2 weeks
Relnotes: yes
Sponsored by: The FreeBSD Foundation


# 3d30404f 27-Jul-2013 Alfred Perlstein <alfred@FreeBSD.org>

Fix watchdog pretimeout.

The original API calls for pow2ns, however the new APIs from
Linux call for seconds.

We need to be able to convert to/from 2^Nns to seconds in both
userland and kernel to fix this and properly compare units.


# 2b50ce65 18-Apr-2013 Andrey A. Chernov <ache@FreeBSD.org>

Attempt to mitigate poor initialization of arc4 by one-shot
reinitialization from yarrow right after good entropy is harvested.

Approved by: secteam (delphij)
MFC after: 1 week


# f7b9ae08 26-Mar-2012 Marius Strobl <marius@FreeBSD.org>

Remove second consts in r233288 in order to appease C++ compilers.
While at it, remove some style(9) bugs in libkern.h.

Submitted by: kan


# cc0c154f 21-Mar-2012 Marius Strobl <marius@FreeBSD.org>

Declare the CRC lookup-tables const as they hardly should change at
run-time.


# 22fec34a 03-Jan-2012 Ed Schouten <ed@FreeBSD.org>

Implement extensions on top of standards instead of the other way around.

Now that index() and rindex() have become unused, simply turn them into
wrappers around strchr() and strrchr(), respectively.


# 41adcfa1 01-Jan-2012 Ed Schouten <ed@FreeBSD.org>

Remove the now unused skpc() function.

It was only used by ufs and ext2 and I have really strong doubts that
there are other pieces of code that also use this function. If it turns
out that external drivers use this code as well, I'd be happy to migrate
or revert.

Bump __FreeBSD_version while there.


# de85bfc5 01-Jan-2012 Ed Schouten <ed@FreeBSD.org>

Introducing memcchr(3).

It seems two of the file system drivers we have in the tree, namely ufs
and ext3, use a function called `skpc()'. The meaning of this function
does not seem to be documented in FreeBSD, but it turns out one needs to
be a VAX programmer to understand what it does.

SPKC is an instruction on the VAX that does the opposite of memchr(). It
searches for the non-equal character. Add a new function called
memcchr() to the tree that has the following advantages over skpc():

- It has a name that makes more sense than skpc(). Just like strcspn()
matches the complement of strspn(), memcchr() is the complement of
memchr().

- It is faster than skpc(). Similar to our strlen() in libc, it compares
entire words, instead of single bytes. It seems that for this routine
this yields a sixfold performance increase on amd64.

- It has a man page.


# 6aa40c01 17-Dec-2011 Andriy Gapon <avg@FreeBSD.org>

retire libkern gets

Inspired by: bde
MFC after: 2 months
X-MFC-Note: if deemed a part of KPI, just call cngets internally


# ac8dee69 17-Dec-2011 Andriy Gapon <avg@FreeBSD.org>

move GETS_*ECHO* defintions from libkern.h to cons.h

MFC after: 2 months


# f359758c 04-Oct-2011 Jung-uk Kim <jkim@FreeBSD.org>

Add strnlen() to libkern.


# eb4c31fd 14-Nov-2010 Ed Schouten <ed@FreeBSD.org>

Add support for asterisk characters when filling in the GELI password
during boot.

Change the last argument of gets() to indicate a visibility flag and add
definitions for the numerical constants. Except for the value 2, gets()
will behave exactly the same, so existing consumers shouldn't break. We
only use it in two places, though.

Submitted by: lme (older version)


# a7d5f7eb 19-Oct-2010 Jamie Gritton <jamie@FreeBSD.org>

A new jail(8) with a configuration file, to replace the work currently done
by /etc/rc.d/jail.


# 1b127017 02-Jun-2010 Rafal Jaworowski <raj@FreeBSD.org>

Provide memchr() in the libkern.

This is required by libfdt and will be compiled in conditionally only for
FDT-enabled platforms.

Reviewed by: imp
Sponsored by: The FreeBSD Foundation


# eef9e53e 14-Feb-2009 Randall Stewart <rrs@FreeBSD.org>

This commit fixes the issue with alias_sctp.c. No
longer do we require SCTP to be in the kernel for the
lib to be able to handle SCTP. We do this by moving
the CRC32c checksum into libkern/crc32.c and then adjusting
all routines to use the common methods. Note that this
will improve the performance of iSCSI since they were
using the old single 256 bit table lookup versus the
slicing 8 algorithm (which gives a 4x speed up in
CRC32c calculation :-D)

Reviewed by:rwatson, gnn, scottl, paolo
MFC after: 4 week? (assuming we MFC the alias_sctp changes)


# d7f03759 19-Oct-2008 Ulf Lilleengen <lulf@FreeBSD.org>

- Import the HEAD csup code which is the basis for the cvsmode work.


# ae72afe0 23-Sep-2008 David E. O'Brien <obrien@FreeBSD.org>

The kernel implemented 'memcmp' is an alias for 'bcmp'. However, memcmp
and bcmp are not the same thing. 'man bcmp' states that the return is
"non-zero" if the two byte strings are not identical. Where as,
'man memcmp' states that the return is the "difference between the
first two differing bytes (treated as unsigned char values" if the
two byte strings are not identical.

So provide a proper memcmp(9), but it is a C implementation not a tuned
assembly implementation. Therefore bcmp(9) should be preferred over memcmp(9).


# bbbc5bc1 14-Aug-2008 Kip Macy <kmacy@FreeBSD.org>

Add strcspn to libkern for use by xenbus routines. Will add to build
in separate commit.


# 19891848 14-Jul-2008 David E. O'Brien <obrien@FreeBSD.org>

Match the implementation of the inline function from libkern.h.


# f7caeade 10-Apr-2007 Wojciech A. Koszek <wkoszek@FreeBSD.org>

strchr() and strrchr() are already present in the kernel, but with less
popular names. Hence:

- comment current index() and rindex() functions, as these serve the same
functionality as, respectively, strchr() and strrchr() from userland;
- add inlined version of strchr() and strrchr(), as we tend to use them more
often;
- remove str[r]chr() definitions from ZFS code;

Reviewed by: pjd
Approved by: cognet (mentor)


# d99b0dd2 02-Nov-2006 Andre Oppermann <andre@FreeBSD.org>

Rewrite kern_sendfile() to work in two loops, the inner which turns as many
VM pages into mbufs as it can -- up to the free send socket buffer space.
The outer loop then drops the whole mbuf chain into the send socket buffer,
calls tcp_output() on it and then waits until 50% of the socket buffer are
free again to repeat the cycle. This way tcp_output() gets the full amount
of data to work with and can issue up to 64K sends for TSO to chop up in
the network adapter without using any CPU cycles. Thus it gets very efficient
especially with the readahead the VM and I/O system do.

The previous sendfile(2) code simply looped over the file, turned each 4K
page into an mbuf and sent it off. This had the effect that TSO could only
generate 2 packets per send instead of up to 44 at its maximum of 64K.

Add experimental SF_MNOWAIT flag to sendfile(2) to return ENOMEM instead of
sleeping on mbuf allocation failures.

Benchmarking shows significant improvements (95% confidence):
45% less cpu (or 1.81 times better) with new sendfile vs. old sendfile (non-TSO)
83% less cpu (or 5.7 times better) with new sendfile vs. old sendfile (TSO)

(Sender AMD Opteron 852 (2.6GHz) with em(4) PCI-X-133 interface and receiver
DELL Poweredge SC1425 P-IV Xeon 3.2GHz with em(4) LOM connected back to back
at 1000Base-TX full duplex.)

Sponsored by: TCP/IP Optimization Fundraise 2005
MFC after: 3 month


# 73c0c411 12-Aug-2006 Pawel Jakub Dawidek <pjd@FreeBSD.org>

Add strstr() function to the libkern.


# eb184de3 17-Jul-2006 Poul-Henning Kamp <phk@FreeBSD.org>

Add some casts to make these files more C++ compatible.

Submitted by: Kristen Nielsen <krn@krn.dk>


# 24f8c87b 06-Oct-2005 Pawel Jakub Dawidek <pjd@FreeBSD.org>

Backout strtok() addition to libkern, strsep() is enough and strtok()
is not safe.

Discussed with: stefanf, njl


# 5e66cbae 06-Oct-2005 Pawel Jakub Dawidek <pjd@FreeBSD.org>

Add strtok() and strtok_r() function to libkern.

MFC after: 2 weeks


# c812ca43 08-Aug-2005 Pawel Jakub Dawidek <pjd@FreeBSD.org>

Add strcasecmp() and strncasecmp() to libkern and connect to the build.


# db302d16 27-Apr-2005 Marcel Moolenaar <marcel@FreeBSD.org>

Inline functions belong in <sys/libkern.h>, not in <sys/systm.h>.
Move crc32() and crc32_raw() from the latter to the former. Move
the declaration of crc32_tab[] to <sys/libkern.h> as well.

Pointed out by: bde@
Tested on: ia64, sparc64


# 75737f34 10-Feb-2005 Gleb Smirnoff <glebius@FreeBSD.org>

Add strspn() to libkern.

Ok'ed by: rwatson


# f627315f 03-Feb-2005 Pawel Jakub Dawidek <pjd@FreeBSD.org>

- Move gets() function to libkern (I want to use it outside vfs_mount.c).
- Add buffer size limitations (overflow will not be possible anymore).
- Add 'visible' option, which will allow for passphrase reading in the
future.
- Remove special treatment of '@' and '#', those two are only confusing.

Discussed with: rwatson
MFC after: 2 weeks


# b1e34c44 15-Jul-2004 Gleb Smirnoff <glebius@FreeBSD.org>

Copy qsort_r(3) from libc to libkern.

Reviewed by: phk
Approved by: julian (mentor)


# 82c6e879 06-Apr-2004 Warner Losh <imp@FreeBSD.org>

Remove advertising clause from University of California Regent's license,
per letter dated July 22, 1999.

Approved by: core


# 3e704d0b 16-Feb-2004 Dag-Erling Smørgrav <des@FreeBSD.org>

Back out previous commit; it doesn't seem to do what I thought it does.


# b14160b8 16-Feb-2004 Dag-Erling Smørgrav <des@FreeBSD.org>

Avoid code duplication on platforms where int and long are the same size.


# 8892f143 14-Jan-2004 Dag-Erling Smørgrav <des@FreeBSD.org>

Back out previous commit, which as bde@ pointed out is a no-op.


# 13ffdb96 14-Jan-2004 Dag-Erling Smørgrav <des@FreeBSD.org>

#include <machine/cpufunc.h>, which may define inline versions of some
of the functions in libkern. Without this, parts of the kernel would
reference a non-existent (undeclared and undefined) ffs() function; the
only reason this didn't break the kernel build is that gcc happens to
have a built-in ffs() and incorrectly fails to warn about the lack of
prototypes for built-in functions.


# f04ecda5 13-Jan-2004 Dag-Erling Smørgrav <des@FreeBSD.org>

Add C implementations of ffsl(), fls() and flsl().


# 354d43ab 25-Feb-2003 Robert Watson <rwatson@FreeBSD.org>

Pass a malloc type into the libkern strdup() implementation explicitly,
so that callers can specify what malloc pool the resulting memory
should come from.

Requested by: phk


# 96c4266c 23-Feb-2003 Robert Watson <rwatson@FreeBSD.org>

Add an implementation of strdup() to libkern. Allocated memory is of
type M_STRING, now defined in malloc.h. Useful when string parsing
must occur using the kernel strsep() and we want to avoid toasting
the source string.

Obtained from: TrustedBSD Project
Sponsored by: DARPA, Network Associates Laboratories


# 93eb73aa 09-Feb-2003 Jeffrey Hsu <hsu@FreeBSD.org>

Add restrict keyword to string functions.

Reviewed by: bde


# 146cc872 17-Jan-2003 Matthew N. Dodd <mdodd@FreeBSD.org>

- Style fixes.
- llabs() -> qabs(); long long isn't valid for the kernel.

Submitted by: bde


# e2fdcaf2 14-Jan-2003 Matthew N. Dodd <mdodd@FreeBSD.org>

- Add inline functions for {ll,l,}abs() to libkern.
- Remove hand rolled abs() functions.


# 2c38619b 11-Oct-2002 Poul-Henning Kamp <phk@FreeBSD.org>

Slight overhaul of arc4random() and friends.

One bug fixed: Use getmicrouptime() to trigger reseeds so that we
cannot be tricked by a clock being stepped backwards.

Express parameters in natural units and with natural names.

Don't use struct timeval more than we need to.

Various stylistic and readability polishing.

Introduce arc4rand(void *ptr, u_int len, int reseed) function which
returns a stream of pseudo-random bytes, observing the automatic
reseed criteria as well as allowing forced reseeds.

Rewrite arc4random() in terms of arc4rand().

Sponsored by: DARPA & NAI Labs.


# 469fda7e 10-Oct-2002 Robert Watson <rwatson@FreeBSD.org>

Hook up strsep(3) to libkern following a repo-copy by Peter. This will
allow us to avoid nasty by-hand string parsing stuff in a number of
places in the kernel, reducing the risk of unexpected consequences
for kernel correctness.


# 20a6cb8c 07-Oct-2002 Dima Dorfman <dd@FreeBSD.org>

Import the libc fnmatch() into the kernel. This will be used by,
among other things, the DEVFS rule subsystem to match nodes against a
path pattern supplied by the user.

fnmatch.c was repo-copied from src/lib/libc/gen/fnmatch.c, and the
only changes to it are those necessary to make it compile in the
kernel. The relevant parts of fnmatch.h were imported into libkern.h.

Approved by: -arch


# 9c2060cd 02-Sep-2002 Brooks Davis <brooks@FreeBSD.org>

Hook up libkern/strlcpy.c and libkern/strlcat.c after repocopy.

Obtained from: OpenBSD
Discussed on: -arch


# c58eb46e 23-Mar-2002 Bruce Evans <bde@FreeBSD.org>

Fixed some style bugs in the removal of __P(()). The main ones were
not removing tabs before "__P((", and not outdenting continuation lines
to preserve non-KNF lining up of code with parentheses. Switch to KNF
formatting and/or rewrap the whole prototype in some cases.


# 789f12fe 19-Mar-2002 Alfred Perlstein <alfred@FreeBSD.org>

Remove __P


# 5578933d 17-Feb-2002 Robert Watson <rwatson@FreeBSD.org>

Add a 'strvalid()' call to libkern. Given a character pointer, and
buffer length, determine if the pointer is to a valid string. Currently,
the only check is whether a '\0' appears in the buffer. This is useful
when pulling in a structure from userland that may contain one or more
strings, and validity testing must be performed on elements of the
structure. When copying normal string arguments, copyinstr() is
expected to be used.


# be8e1fea 27-Sep-2001 Luigi Rizzo <luigi@FreeBSD.org>

namei.h: move "struct componentname" definition outside "struct nameidata",
and provide a valid STDC/C++ definition for function NDINIT

queue.h libkern.h: put explicit casts from void * in insque, remque and memset

(for the records, these changes are necessary to let the files
compile with g++, which is used to build a FreeBSD module
for "Click" -- see www.pdos.lcs.mit.edu/click/ .
Given that they have zero impact on our code, it is worthwhile
to have them in.

MFC after: 3 days


# 3393f8da 26-Mar-2001 Kenneth D. Merry <ken@FreeBSD.org>

Rewrite of the CAM error recovery code.

Some of the major changes include:

- The SCSI error handling portion of cam_periph_error() has
been broken out into a number of subfunctions to better
modularize the code that handles the hierarchy of SCSI errors.
As a result, the code is now much easier to read.

- String handling and error printing has been significantly
revamped. We now use sbufs to do string formatting instead
of using printfs (for the kernel) and snprintf/strncat (for
userland) as before.

There is a new catchall error printing routine,
cam_error_print() and its string-based counterpart,
cam_error_string() that allow the kernel and userland
applications to pass in a CCB and have errors printed out
properly, whether or not they're SCSI errors. Among other
things, this helped eliminate a fair amount of duplicate code
in camcontrol.

We now print out more information than before, including
the CAM status and SCSI status and the error recovery action
taken to remedy the problem.

- sbufs are now available in userland, via libsbuf. This
change was necessary since most of the error printing code
is shared between libcam and the kernel.

- A new transfer settings interface is included in this checkin.
This code is #ifdef'ed out, and is primarily intended to aid
discussion with HBA driver authors on the final form the
interface should take. There is example code in the ahc(4)
driver that implements the HBA driver side of the new
interface. The new transfer settings code won't be enabled
until we're ready to switch all HBA drivers over to the new
interface.

src/Makefile.inc1,
lib/Makefile: Add libsbuf. It must be built before libcam,
since libcam uses sbuf routines.

libcam/Makefile: libcam now depends on libsbuf.

libsbuf/Makefile: Add a makefile for libsbuf. This pulls in the
sbuf sources from sys/kern.

bsd.libnames.mk: Add LIBSBUF.

camcontrol/Makefile: Add -lsbuf. Since camcontrol is statically
linked, we can't depend on the dynamic linker
to pull in libsbuf.

camcontrol.c: Use cam_error_print() instead of checking for
CAM_SCSI_STATUS_ERROR on every failed CCB.

sbuf.9: Change the prototypes for sbuf_cat() and
sbuf_cpy() so that the source string is now a
const char *. This is more in line wth the
standard system string functions, and helps
eliminate warnings when dealing with a const
source buffer.

Fix a typo.

cam.c: Add description strings for the various CAM
error status values, as well as routines to
look up those strings.

Add new cam_error_string() and
cam_error_print() routines for userland and
the kernel.

cam.h: Add a new CAM flag, CAM_RETRY_SELTO.

Add enumerated types for the various options
available with cam_error_print() and
cam_error_string().

cam_ccb.h: Add new transfer negotiation structures/types.

Change inq_len in the ccb_getdev structure to
be "reserved". This field has never been
filled in, and will be removed when we next
bump the CAM version.

cam_debug.h: Fix typo.

cam_periph.c: Modularize cam_periph_error(). The SCSI error
handling part of cam_periph_error() is now
in camperiphscsistatuserror() and
camperiphscsisenseerror().

In cam_periph_lock(), increase the reference
count on the periph while we wait for our lock
attempt to succeed so that the periph won't go
away while we're sleeping.

cam_xpt.c: Add new transfer negotiation code. (ifdefed
out)

Add a new function, xpt_path_string(). This
is a string/sbuf analog to xpt_print_path().

scsi_all.c: Revamp string handing and error printing code.
We now use sbufs for much of the string
formatting code. More of that code is shared
between userland the kernel.

scsi_all.h: Get rid of SS_TURSTART, it wasn't terribly
useful in the first place.

Add a new error action, SS_REQSENSE. (Send a
request sense and then retry the command.)
This is useful when the controller hasn't
performed autosense for some reason.

Change the default actions around a bit.

scsi_cd.c,
scsi_da.c,
scsi_pt.c,
scsi_ses.c: SF_RETRY_SELTO -> CAM_RETRY_SELTO. Selection
timeouts shouldn't be covered by a sense flag.

scsi_pass.[ch]: SF_RETRY_SELTO -> CAM_RETRY_SELTO.

Get rid of the last vestiges of a read/write
interface.

libkern/bsearch.c,
sys/libkern.h,
conf/files: Add bsearch.c, which is needed for some of the
new table lookup routines.

aic7xxx_freebsd.c: Define AHC_NEW_TRAN_SETTINGS if
CAM_NEW_TRAN_CODE is defined.

sbuf.h,
subr_sbuf.c: Add the appropriate #ifdefs so sbufs can
compile and run in userland.

Change sbuf_printf() to use vsnprintf()
instead of kvprintf(), which is only available
in the kernel.

Change the source string for sbuf_cpy() and
sbuf_cat() to be a const char *.

Add __BEGIN_DECLS and __END_DECLS around
function prototypes since they're now exported
to userland.

kdump/mkioctls: Include stdio.h before cam.h since cam.h now
includes a function with a FILE * argument.

Submitted by: gibbs (mostly)
Reviewed by: jdp, marcel (libsbuf makefile changes)
Reviewed by: des (sbuf changes)
Reviewed by: ken


# a67036f7 18-Dec-2000 Assar Westerlund <assar@FreeBSD.org>

revert addition of strlcpy/strlcat


# 41155d57 17-Dec-2000 Assar Westerlund <assar@FreeBSD.org>

add strlcpy and strlcat to kernel


# a3197dee 03-Sep-2000 Peter Wemm <peter@FreeBSD.org>

sys/systm.h is incompatable with sys/types.h in userland.
(#define major() in sys/types.h for example.)

Reported by: Nickolay Dudorov <nnd@mail.nsk.ru>


# 96b2a9d4 03-Sep-2000 Peter Wemm <peter@FreeBSD.org>

libkern.h now internally uses the bzero() definition from sys/systm.h.
This is kinda important since the bzero symbol on i386 is not a function
but a function pointer.. If memset() tried to call it as though it were
a function, things would be less than satisfactory. In reality though
this was not an actual problem and just caused compile warnings.


# 0b32abca 02-Sep-2000 Poul-Henning Kamp <phk@FreeBSD.org>

Bite the bullet and provde memcmp() and memset(), this will be cheaper
than all the copy&paste versions we already have :-(


# 45371389 10-Dec-1999 Peter Wemm <peter@FreeBSD.org>

Zap c_index() and c_rindex(). Bruce prefers these to implicitly convert
a const into a non-const as they do in libc. I feel that defeating the
type checking like that quite evil, but that's the way it is.


# ee3fd601 28-Nov-1999 Dan Moschuk <dan@FreeBSD.org>

Introduce OpenBSD-like Random PIDs. Controlled by a sysctl knob
(kern.randompid), which is currently defaulted off. Use ARC4 (RC4) for our
random number generation, which will not get me executed for violating
crypto laws; a Good Thing(tm).

Reviewed and Approved by: bde, imp


# 95dc37f6 20-Nov-1999 Peter Wemm <peter@FreeBSD.org>

Tempt fate and stop index from converting a const char * into a char *.
I've made a seperate version (c_index() etc) that use const/const, but
I'm not sure it's worth it considering there is one file in the tree
that uses index on const strings (kern_linker.c) and it's easily adjusted
to scan the strings directly (and is perhaps more efficient that way).


# c3aac50f 27-Aug-1999 Peter Wemm <peter@FreeBSD.org>

$Id$ -> $FreeBSD$


# 6875d254 22-Feb-1997 Peter Wemm <peter@FreeBSD.org>

Back out part 1 of the MCFH that changed $Id$ to $FreeBSD$. We are not
ready for it yet.


# 1130b656 14-Jan-1997 Jordan K. Hubbard <jkh@FreeBSD.org>

Make the long-awaited change from $Id$ to $FreeBSD$

This will make a number of things easier in the future, as well as (finally!)
avoiding the Id-smashing problem which has plagued developers for so long.

Boy, I'm glad we're not using sup anymore. This update would have been
insane otherwise.


# 840a0352 22-Sep-1996 Peter Wemm <peter@FreeBSD.org>

add simple srandom() and prototype


# e0c95ed9 31-Aug-1996 Bruce Evans <bde@FreeBSD.org>

Fixed the easy cases of const poisoning in the kernel. Cosmetic.


# a2970ccb 01-Aug-1996 Garrett Wollman <wollman@FreeBSD.org>

Declare fls() for the non-inline case. Defer writing the generic version
until it actually matters.


# 477af064 08-Jun-1996 Nate Williams <nate@FreeBSD.org>

Bring in the prototype for index() now that's used by default.


# 87b620ba 19-Apr-1996 Bruce Evans <bde@FreeBSD.org>

Don't depend on <sys/types.h> including <sys/cdefs.h>.


# 6b3f8066 16-Jan-1996 Bruce Evans <bde@FreeBSD.org>

Moved BCD declarations to the correct header (libkern.h).

Fixed BCD declarations. They didn't match their definitions...

libkern.h, bcd.c:
KNFised. `indent' worked 99% perfectly on bcd.c. It worked 99%
_imperfectly_ on subr_prf.c.


# a4cc2728 09-Nov-1995 Poul-Henning Kamp <phk@FreeBSD.org>

Add qsort prototype.
Add userland_sysctl prototype.


# 7666fb47 23-Apr-1995 Bruce Evans <bde@FreeBSD.org>

inline -> __inline.

Headers should always use `__inline' for inline functions to avoid
syntax errors when modules that don't even use the offending functions
are compiled with `gcc -ansi'.


# b5e8ce9f 16-Mar-1995 Bruce Evans <bde@FreeBSD.org>

Add and move declarations to fix all of the warnings from `gcc -Wimplicit'
(except in netccitt, netiso and netns) and most of the warnings from
`gcc -Wnested-externs'. Fix all the bugs found. There were no serious
ones.


# 1b099e1e 14-Nov-1994 Bruce Evans <bde@FreeBSD.org>

Hide the declaration of ffs() if an inline ffs() is implemented.

I couldn't find a better way to avoid compiler warnings about
redundant and/or inconsistent declaration of ffs(). I'd like to
be able to declare prototypes in general headers without committing
to implementing them as `static inline' or `extern', but there
seems to be no way to do this with gcc-2.6.1. E.g.,

int foo(void);
static __inline int foo(void) { return 1; }

causes a warning about the linkage mismatch, while the opposite
order causes a warning about the redundant declaration.


# 6485b5ff 18-Sep-1994 Bruce Evans <bde@FreeBSD.org>

Use new-style declarations for min/max functions so that
"gcc -Wstrict-prototypes" doesn't emit warnings about them.

Write each min/max functions on a single line so that the similarity and
triviality of the functions is more obvious.

Put the quad min/max functions in the correct place (aphabetical order).

The u_quad min/max functions are missing. Only 3 or 4 of the min/max
functions are actually used. sys/socketvar.h ``should use "lmin" but
it doesn't exist now''. lmin does exist now, but isn't used. Since we
depend on gcc for `inline', perhaps we should depend on it for __typeof
and function-expressions and use only macros min() and max() that work
for any types (I'm not sure how to handle mixed types).


# 147db1cb 30-Aug-1994 David Greenman <dg@FreeBSD.org>

Added qmin and qmax functions to deal with quad_t's.


# 3c4dd356 02-Aug-1994 David Greenman <dg@FreeBSD.org>

Added $Id$


# df8bae1d 24-May-1994 Rodney W. Grimes <rgrimes@FreeBSD.org>

BSD 4.4 Lite Kernel Sources