简体   繁体   English

为什么自定义 init 在自定义 linux 系统上失败并显示错误代码 -8?

[英]Why does a custom init fail with error code -8 on a custom linux system?

I'm building a custom init for a linux system (for a thin client) (currently emulated over qemu).我正在为 linux 系统(用于瘦客户端)(目前通过 qemu 模拟)构建自定义 init。 I'm compiling my executable with:我正在编译我的可执行文件:

$ gcc -fpic -static -nostdlib init.c -o init

For some reason when this executable is put over /bin/, system refuses to boot and spits out a error code that is extremely unhelpful (-8).出于某种原因,当这个可执行文件被放在 /bin/ 上时,系统拒绝启动并吐出一个非常无用的错误代码 (-8)。 Based on some googling this seems to be corresponding to a signal 8 which is SIGFPE, but oddly enough nothing related to impossible mathematics like dividing by zero happens at any place in the code or in the complicated assembly examined.根据一些谷歌搜索,这似乎对应于信号 8,即 SIGFPE,但奇怪的是,在代码或检查的复杂程序集中的任何位置都没有发生与不可能的数学相关的事情,例如除以零。 The source code doesn't matter at all, any source fails to build but here is a example that fails.源代码根本不重要,任何来源都无法构建,但这里有一个失败的例子。

size_t strlen(const char* s) {
    for(unsigned i = 0; ; ++i) if(s[i] == 0) return i;
}
void puts(char* s) {
    asm("mov %0, %%r11" :: "r"(s));
    asm("mov %0, %%r14" :: "r"(strlen(s)));
    asm("mov $1, %rax\n"
        "mov  %r11, %rsi\n"
        "mov %r14, %rdx\n"
        "mov $1, %rdi\n"
        "syscall");
}
int _start() {
    puts("test");






    asm volatile(
        "mov $60, %rax\n"
        "push %r11\n"
        "mov %r11, %rdi\n"
        "pop %r11\n"
        "syscall"
    );
    return 0;
}

bin/init 失败,退出状态代码为 -8

Run /bin/init as init process
Kernel panic - not syncing: Requested init /bin/init failed (error -8).

Kernel Offset: disabled

This issue happens if you forgot to toggle 64 bit kernel in configuration before compiling the kernel it may also caused by using a wrong compiler such as a compiler that isn't for Linux but rather was made to produce elf executable for other unixes.如果您在编译 kernel 之前忘记在配置中切换 64 位 kernel,则会发生此问题,这也可能是由于使用了错误的编译器,例如不是用于 Linux 而是为其他 unix 生成 elf 可执行文件的编译器。 Code uses 64 bit registers while kernel is only 32 bit.代码使用 64 位寄存器,而 kernel 只有 32 位。

Another cause could be due a badly made initramfs.另一个原因可能是制作不当的 initramfs。 I had trouble with cpio files until I read the manual which is extremely helpful:在阅读非常有帮助的手册之前,我遇到了 cpio 文件的问题:

Using the initial RAM disk (initrd)使用初始 RAM 磁盘 (initrd)

While not related also do not use exit syscall on init process well because Init is supposed to be run until system shuts down and exiting init will result in a kernel panic.虽然不相关,但也不要在 init 进程上很好地使用 exit 系统调用,因为 Init 应该运行到系统关闭,退出 init 将导致 kernel 恐慌。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM