course_os icon indicating copy to clipboard operation
course_os copied to clipboard

Kernel "libc"

Open rellermeyer opened this issue 10 years ago • 53 comments

We will need a restricted set of libc-like functions in the kernel. E.g., some way of printing output for debugging, some initial way of dynamically allocating memory a la kmalloc, etc.

rellermeyer avatar Mar 08 '14 04:03 rellermeyer

From some cursory research it seems like this will take a bit of mucking around in assembly to get the basic library routines up. Is that correct?

SensitiveDongle avatar Mar 08 '14 19:03 SensitiveDongle

It might possibly involve some assembly but I would think that the majority is in C.

rellermeyer avatar Mar 08 '14 21:03 rellermeyer

We're not reinventing the wheel here, so we'll be able to use things like stdlib.h to construct this libc? I think I would be interested in working on this.

ascensionra avatar Mar 11 '14 18:03 ascensionra

Well, we cannot use code from glibc or alike that because of license issues but we can take code from musl-libc (http://www.musl-libc.org) which is under MIT license.

However, for the kernel we should focus on only the library functions that we really need since the library is only needed inside the kernel and nowhere else. For some that we will need, e.g., a way for the kernel to print debug output, we can discuss how we want them because it does not necessarily have to be printf. Just to give an example, some people (most prominently the designers of Solaris) believe that kernel code should not support variable length strings for reliability and security reasons, etc.

rellermeyer avatar Mar 12 '14 02:03 rellermeyer

I would like to request a string compare function like memcmp. All it does is compare two strings of bytes.

SheldonSandbekkhaug avatar Mar 19 '14 15:03 SheldonSandbekkhaug

So we are allowed to use anything from the musl library? Once it is added to the repo that is.

stv8 avatar Mar 19 '14 19:03 stv8

Yes, if we want certain functionality from it we can pull it in. Just make sure that all copyright headers are left in place to correctly attribute the authorship of the code.

rellermeyer avatar Mar 20 '14 01:03 rellermeyer

Do you guys need any help with this? Jeremy and I are trying to write the ELF parser, but we can't do anything until we have libc finished.

null-sysadmin avatar Mar 28 '14 00:03 null-sysadmin

Hi guys... First off, my apologies. I've been dealing with a lot of family stuff recently (brother in hospital, thousands of miles in road trips to deal with these issues), and I've fallen behind. Any help would be welcome, primarily if you could identify the key functionality you need from the library to get the parser to work. I will focus on getting those items completed. Again, my apologies for dropping the ball on this one...

ascensionra avatar Mar 28 '14 16:03 ascensionra

For the argument parser, all we need at the moment is a memory comparing function like memcmp so that we can compare two strings. I'll let you know if the parser needs anything else.

SheldonSandbekkhaug avatar Mar 28 '14 20:03 SheldonSandbekkhaug

Would memcmp and strcmp be helpful? I'll get those added in ASAP.

ascensionra avatar Mar 28 '14 20:03 ascensionra

memcmp and strmp added. Config.mk and makefile updated, Make run works. Added COPYRIGHT, since I used and made minor changes to the musl-libc.

ascensionra avatar Mar 28 '14 23:03 ascensionra

Hey, no worries. For the ELF parser, we mainly need read and print functionality.

null-sysadmin avatar Mar 29 '14 20:03 null-sysadmin

Read as the IO call? I don't think we will have this any time soon, it requires an IO subsystem. I would think that in the first phase you can consider the program binary to be mapped into memory and you will get a pointer to the start.

rellermeyer avatar Mar 30 '14 05:03 rellermeyer

Thanks for adding those two functions. Do you think you could push your changes so I can use them?

SheldonSandbekkhaug avatar Mar 31 '14 03:03 SheldonSandbekkhaug

Ok, I merged the header and source for libc to master. Please make sure to read the comments in the files. To avoid conflict with existing functions, I prepended the functions with os_, so to call strcmp, you'd use os_strcmp

ascensionra avatar Mar 31 '14 03:03 ascensionra

I've been thinking that a function to split a string (like strtok) may be useful. Is that something that should be in our libc?

SheldonSandbekkhaug avatar Apr 01 '14 15:04 SheldonSandbekkhaug

If you have a use for it, you should add it or ask the klibc maintainers to add it.

rellermeyer avatar Apr 01 '14 16:04 rellermeyer

Would it be alright with you guys (the people working on libc) if I went ahead and added strtok or something like it from MUSL?

SheldonSandbekkhaug avatar Apr 01 '14 23:04 SheldonSandbekkhaug

I'll add strtok. Anything else you would like to request while I'm at it?

ascensionra avatar Apr 02 '14 05:04 ascensionra

I think memcpy (or a function that copies a region of memory from one location to another) could be useful.

SheldonSandbekkhaug avatar Apr 07 '14 19:04 SheldonSandbekkhaug

^That was an accident, sorry.

SheldonSandbekkhaug avatar Apr 07 '14 19:04 SheldonSandbekkhaug

I'll try to get this done today (along with strtok if it's not there already).

ascensionra avatar Apr 07 '14 19:04 ascensionra

Are you guys still working on klibc? I wouldn't mind adding strtok from MUSL myself if that's alright with you guys.

SheldonSandbekkhaug avatar Apr 17 '14 04:04 SheldonSandbekkhaug

I've added a couple of functions from MUSL to klibc. However, I think strtok is not working properly, so I'm going try to figure out what the problem is.

SheldonSandbekkhaug avatar Apr 20 '14 22:04 SheldonSandbekkhaug

Hey Sheldon, thanks for picking up the slack on this. I'm still dealing with a lot of family stuff, so it is much appreciated.

Is there anything that is critical right now that I can resume working on?

ascensionra avatar Apr 21 '14 02:04 ascensionra

@SheldonSandbekkhaug I wrote a strtok in our test_framework branch. It doesn't work yet, but I could send it to you if you want.

SUMDude21 avatar Apr 21 '14 02:04 SUMDude21

ascensionra, no problem! I just want to get things done, you know? :)

SUMDude21, I had copied the strtok from MUSL. I put it in klibc.c under os_strtok(). It turns out the problem with strtok was actually in strspn and strcspn (for some reason, those work in desktop Linux but not in CourseOS on the virtual machine). I just replaced the MUSL versions of strspn and strcspn with functions I wrote myself, so there may be hidden bugs that I haven't found yet. See commit d3615e93fd74f7aa51a1ed5bf09eadf3ade7d771

SheldonSandbekkhaug avatar Apr 21 '14 04:04 SheldonSandbekkhaug

@SheldonSandbekkhaug All right I'll write some tests for them, anything else I can help with?

SUMDude21 avatar Apr 22 '14 19:04 SUMDude21

I can't think of anything at the moment, though things will come up, except if you ever need to use a library function in the kernel, just add it from MUSL if you can!

SheldonSandbekkhaug avatar Apr 22 '14 20:04 SheldonSandbekkhaug