Page 1 of 1

Bare metal library

Posted: Sun Apr 19, 2020 4:42 pm
by OSwhatever
Currently a lot of us bring in some form of c library in order obtain simple functions like memset or memcpy. Bringing in the entire C library is totally unnecessary for these kind of functions which in turn might use some form of compiler hook or library in order to use optimized functions. With the compiler there are specialized functions but these often have funky names which makes it non portable.

The functions I really need for the bare metal stuff are:
memcpy
memset
memcmp
memsearchbyte - search for a given byte, strlen could be implemented easily using this
memsearchword - search for a given word
memsearcharray- search memory for a given byte array, can implemented using memcmp

With these types of functions you can come pretty far. You are welcome to mention any other function you think should be added.

LLVM already have some of this as intrinsics but it would be nice to have a "standard bare metal library" that gives us this basic functionality. Most of the C library operates on its stupid null terminated strings, something that kernels should avoid anyway. Rust which is more modern has done this more or less with its core library. Of course you could write all these things yourself but you don't really want to write an optimized function for every CPU architecture each with its own special tricks to make it optimized.

Do we have any such library?

Re: Bare metal library

Posted: Sun Apr 19, 2020 6:47 pm
by bzt
OSwhatever wrote:Do we have any such library?
Yes, it is called libc.
OSwhatever wrote:Rust which is more modern has done this more or less with its core library.
I honestly don't understand what's the fuzz. Just like Rust has a runtime library, the same way C has a runtime library too, and it is (surprisingly) called libc. That is the core library for C. Linking your kernel with it statically will only include the functions that your kernel actually uses, no need for dynamic linking nor for shared objects.

So you have three options:
a) statically link with a libc (pick any, I would recommend musl)
b) because most libc are Open Source, simply copy'n'paste the functions you need into your source
c) use a compiler (like gcc) which has builtin versions of the memory functions, like __builtin_memcmp, __builtin_memcpy etc.
OSwhatever wrote:these often have funky names which makes it non portable.
Btw, "memsearchbyte" is called "memchr", and "memsearcharray"'s name is "memmem" in C standard. The "__builtin_" prefix is handled by the compiler 100% transparently, but you could also use some defines to support non-ansi C compilers. So what do you mean by non portable?

Just a sidenote: to clearify, all libc functions of memcpy, memchr, memcmp, memem, memset etc. operate on raw memory buffers, and not on null terminated strings.

Cheers,
bzt

Re: Bare metal library

Posted: Sun Apr 19, 2020 9:35 pm
by kzinti
OSwhatever wrote:memsearchword - search for a given word
And that's strstr(): http://www.cplusplus.com/reference/cstring/strstr/

Re: Bare metal library

Posted: Mon Apr 20, 2020 3:19 am
by bzt
kzinti wrote:And that's strstr(): http://www.cplusplus.com/reference/cstring/strstr/
True, but the OP wants to avoid null terminated C strings. That's why they need memmem instead, which works with Pascal strings too. Originally it was a glibc extension, but musl and many other libc implementations support it too.

Cheers,
bzt

Re: Bare metal library

Posted: Mon Apr 20, 2020 4:13 am
by Octocontrabass
bzt wrote:c) use a compiler (like gcc) which has builtin versions of the memory functions, like __builtin_memcmp, __builtin_memcpy etc.