32

我记得听说过一个嵌入式操作系统,它本质上只是 libc(也许它支持 c++)。它没有内核、管道或您期望从操作系统获得的任何其他东西。我尝试在维基百科中查找它,但我没有看到它列出。

这样的操作系统存在吗?是否有支持仅终端或 C/C++ + (tcp) 套接字在 VM 外部通信的操作系统?作为玩具,这对我很有用。

4

6 回答 6

38

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.)

于 2012-10-25T07:44:41.730 回答
6

我认为你的一些假设有问题。您说操作系统不需要内核是正确的,但是任何可以运行应用程序的东西都可以在 libc 中静态编译。

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

例如,只要您为您的操作系统编译该函数,就可以使用 printf。所以,只要你为它构建 libc,你就可以使用 MenuetOS。

现在在http://pdclib.rootdirectory.de/有一个小版本的 libc ,一些嵌入式系统可以使用它。

这样,任何小型操作系统都可以被认为是运行 libc 的操作系统。

于 2012-10-25T06:52:54.860 回答
6

基本上不需要内核,但如果您正在搜索最小的操作系统http://wiki.osdev.org/Projects可能是一个起点。他们有很多爱好和半专业的项目,它们支持基本的东西并且占用空间很小。还有一些很好的教程可以自己编写。您还需要考虑网络或串行 I/O 等简单事物需要驱动程序等。

此外,Linux 内核总是一个好的开始(前段时间有一个 linux 发行版,只有大约 20MB)

于 2012-10-25T06:55:27.263 回答
5

有很多这样的。

大多数专业实时操作系统 (RTOS) 都带有或多或少完整的 C 库实现,甚至通常用于 C++(例如 Keil MDK、µItron)。尽管在实践中您通常倾向于避免使用它,因为它使用了太多可用资源。

RTOS 通常有一个非常小的内核,不支持文件或管道。相反,它们倾向于以很少的开销支持任务、计时器、队列和事件标志。

于 2012-10-25T08:44:37.883 回答
3

Libcc 不是操作系统。尽管 OS 的定义有些模糊,但它确实包含的不仅仅是 API。它需要内存管理、进程调度等。

于 2012-10-25T06:57:39.273 回答
0

Newlib 最小可运行示例

https://sourceware.org/newlib/

使用 Newlib,您可以为裸机平台实现自己的系统调用。

在这里,我提供了一个高度自动化且记录在案的示例,该示例显示了使用在 QEMU aarch64 中运行的 crosstool-NG 构建的 Newlib

例如,在上面的示例中,我们有一个示例程序exit.c

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

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

在一个单独的 C 文件common.c中,我们exit使用ARM 半主机实现:

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

您将实现的其他典型系统调用是:

  • write将结果输出到主机。这可以通过以下方式完成:

    • 更多半主机
    • UART硬件
  • brkmalloc.

    在裸机上很容易,因为我们不必关心分页!

TODO 我想知道在不进入像ZephyrFreeRTOS这样的完整RTOS的情况下实现抢先调度系统调用执行是否现实。

Newlib 很酷的一点是,它为您实现了所有非操作系统特定的东西string.h,并让您只实现操作系统存根。

此外,您不必实现所有存根,只需实现您需要的存根即可。例如,如果您的程序只需要exit,那么您不必提供print. Newlib 通过将所有系统调用的虚拟无操作实现作为弱符号来实现这一点。

Newlib 源代码树已经有一些实现,包括 ARM 半主机实现newlib/libc/sys/arm,但在大多数情况下,您必须自己实现。然而,它确实为这项任务提供了坚实的基础。

设置 Newlib 的最简单方法是使用 crosstool-NG 构建您自己的编译器,您只需告诉它您想使用 Newlib 作为 C 库。我的安装程序使用这个脚本自动为你处理,它使用了存在的 newlib 配置crosstool_ng_config

C++ 理论上也应该可以工作,但我最初的尝试是不成功的

于 2020-01-16T14:13:30.820 回答