Pregunta

I remember hearing about an embeddable OS that is essentially just libc (maybe it had support for c++). It didn't have a kernel, pipes or any of the other stuff you expect from an os. I tried looking for it in wikipedia but I didn't see it listed.

Does such an OS exist? Is there an OS that supports either a terminal only or C/C++ + (tcp) sockets to communicate outside of a VM? That would be useful to me as a toy.

¿Fue útil?

Solución

The reason that you're not finding a name for this is that it's not an operating system -- it's the absence of an operating system. Often this is called something like "bare-metal" programming.

The general idea of bare-metal programming is that there is a small bit of general-purpose code -- a "bootloader" -- that sets up the memory controller and other hardware things on the board, and then that transfers control to your program. (Operating systems also have bootloaders, so in that sense your program is replacing the operating system.) Uboot is a fairly common open-source bootloader, so that might be a good place to start looking for information.

One of the tricky bits about bare-metal programming is that, since there isn't an operating system in place to handle any of the hardware communication, you have to think about "what does a printf actually mean as far as what data goes to what peripheral?" and "how do I make it go there?" Again, some bootloaders provide support for this sort of thing, though it's not always trivial to connect it all up. Again, Uboot is a good example.

Meanwhile, the C library itself is actually going to be provided by your compiler, rather than the bootloader.

(I should also add, as a name note: The company I work for makes a series of bare-metal and Linux compilers, known as Sourcery CodeBench. For CodeBench, the bare-metal versions are generally named after the ABI specification they use for linking programs, so the "ELF" or "EABI" versions are all bare-metal compilers, and I think that's a pretty common way of referring to this sort of thing, so you'll see that sort of name around as well.)

Otros consejos

Basically a kernel is not needed, but if you are searching for an minimal os http://wiki.osdev.org/Projects could be a point to start. there are a lot of hobby and semi professional projects out their that support basic things and have a small footprint. Also there are some good tutorials to write it yourself. You also need to consider that drivers etc are need for simple things like network or serial I/O .

Also The linux kernel is always a good start( some time ago there was a linux distro that was just about 20MB)

I think there is a problem with some of your assumptions. You are correct in saying that you don't need a kernel for an OS, but anything that can run applications can statically compile in libc.

See : http://www.superfrink.net/athenaeum/OS-FAQ/os-faq-libc.html

For example, it is possible to use printf as long as you compile that function for your os. So, you can use MenuetOS as long as you build libc for it.

Now there exists a small version of libc at http://pdclib.rootdirectory.de/ which some embedded system can use.

In this way any small OS can be considered an OS for running libc.

There are plenty of those.

Most professional real-time operating systems (RTOS) come with a more or less complete implementation of the C library, and often even for C++ (for example Keil MDK, µItron). Though in practice you often tend to avoid it, because it uses too much of the available resources.

An RTOS typically has a very small kernel, without support for files or pipes. Instead they tend to support tasks, timers, queues and event flags, with very little overhead.

Libcc is not an operating system. Though the definition of OS is somewhat fuzzy, it does include more than an API. It requires memory management, process scheduling, ect.

Newlib minimal runnable example

https://sourceware.org/newlib/

With Newlib, you implement your own system calls for your baremetal platform.

Here I provide a highly automated and documented example that shows Newlib built with crosstool-NG running in QEMU aarch64.

For example, on the above example, we have an example program exit.c:

#include <stdio.h>
#include <stdlib.h>

void main(void) {
    exit(0);
}

and in a separate C file common.c, we implement the exit with ARM semihosting:

void _exit(int status) {
    __asm__ __volatile__ ("mov r0, #0x18; ldr r1, =#0x20026; svc 0x00123456");
}

The other typical syscalls that you will implement are:

  • write to output results to the host. This can be done either with:

    • more semihosting
    • an UART hardware
  • brk for malloc.

    Easy on baremetal, since we don't have to care about paging!

TODO I wonder if it is realistic to reach preemptive scheduling syscalls execution without going into a full blown RTOS like Zephyr or FreeRTOS.

The cool thing about Newlib, is that it implements all the non-OS specific things like string.h for you, and lets you implement just the OS stubs.

Also, you don't have to implement all the stubs, but only the ones you will need. E.g., if your program only need exit, then you don't have to provide a print. Newlib achieves this by giving dummy do-nothing implementations of all syscalls as weak symbols.

The Newlib source tree does already have some implementations, including an ARM semihosting implementation under newlib/libc/sys/arm, but for the most part you have to implement your own. It does however provide a solid base for the task.

The easiest way to setup Newlib is by building your own compiler with crosstool-NG, you just have to tell it that you want to use Newlib as the C library. My setup handles that automatically for you with this script, which uses the newlib configs present at crosstool_ng_config.

C++ should also work in theory, but my initial attempt was unsucessful.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top