Bare metal library

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
OSwhatever
Member
Member
Posts: 595
Joined: Mon Jul 05, 2010 4:15 pm

Bare metal library

Post 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?
User avatar
bzt
Member
Member
Posts: 1584
Joined: Thu Oct 13, 2016 4:55 pm
Contact:

Re: Bare metal library

Post 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
kzinti
Member
Member
Posts: 898
Joined: Mon Feb 02, 2015 7:11 pm

Re: Bare metal library

Post by kzinti »

OSwhatever wrote:memsearchword - search for a given word
And that's strstr(): http://www.cplusplus.com/reference/cstring/strstr/
User avatar
bzt
Member
Member
Posts: 1584
Joined: Thu Oct 13, 2016 4:55 pm
Contact:

Re: Bare metal library

Post 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
Octocontrabass
Member
Member
Posts: 5575
Joined: Mon Mar 25, 2013 7:01 pm

Re: Bare metal library

Post by Octocontrabass »

bzt wrote:c) use a compiler (like gcc) which has builtin versions of the memory functions, like __builtin_memcmp, __builtin_memcpy etc.
Post Reply