Linux QEMU 加载内核时崩溃

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4631345/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 02:24:41  来源:igfitidea点击:

QEMU Crashes When Loading Kernel

clinuxemulationkernelqemu

提问by Daniel Lopez

I've created a C kernel, and I'm loading the kernel in the QEMU emulator. But when I load the kernel, it seems to crash QEMU and it complains that it can't access the kvm folder. Does it mean that kvm is missing, or that I'm not as an administrator; because I logged in as a root administrator. Here is the error information, that originated from the Terminal:

我已经创建了一个 C 内核,我正在将内核加载到 QEMU 模拟器中。但是当我加载内核时,它似乎崩溃了 QEMU 并且它抱怨它无法访问 kvm 文件夹。是不是说kvm不见了,或者我不是管理员;因为我以 root 管理员身份登录。这是源自终端的错误信息:

danny@ubuntu:~/Desktop$ sudo qemu -kernel os.bin
open /dev/kvm: No such file or directory
Could not initialize KVM, will disable KVM support
pci_add_option_rom: failed to find romfile "pxe-rtl8139.bin"
qemu: fatal: Trying to execute code outside RAM or ROM at 0x000a0000

EAX=00004500 EBX=00000000 ECX=00000000 EDX=00000000
ESI=00000000 EDI=00000000 EBP=00000000 ESP=00009fe0
EIP=0000fdfb EFL=00000002 [-------] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =9000 00090000 ffffffff 00cf9300
CS =9020 00090200 0000ffff 00009b0f
SS =9000 00090000 0000ffff 00009300
DS =9000 00090000 0000ffff 00009300
FS =9000 00090000 0000ffff 00009300
GS =9000 00090000 0000ffff 00009300
LDT=0000 00000000 0000ffff 00008200
TR =0000 00000000 0000ffff 00008b00
GDT=     000cba40 00000017
IDT=     00000000 000003ff
CR0=00000010 CR2=00000000 CR3=00000000 CR4=00000000
DR0=00000000 DR1=00000000 DR2=00000000 DR3=00000000 
DR6=ffff0ff0 DR7=00000400
CCS=00004500 CCD=00004546 CCO=ADDB    
FCW=037f FSW=0000 [ST=0] FTW=00 MXCSR=00001f80
FPR0=0000000000000000 0000 FPR1=0000000000000000 0000
FPR2=0000000000000000 0000 FPR3=0000000000000000 0000
FPR4=0000000000000000 0000 FPR5=0000000000000000 0000
FPR6=0000000000000000 0000 FPR7=0000000000000000 0000
XMM00=00000000000000000000000000000000 XMM01=00000000000000000000000000000000
XMM02=00000000000000000000000000000000 XMM03=00000000000000000000000000000000
XMM04=00000000000000000000000000000000 XMM05=00000000000000000000000000000000
XMM06=00000000000000000000000000000000 XMM07=00000000000000000000000000000000
Aborted

The error also seems to show information, that seems to be NASM registers, and it complains that it couldn't find a ROM file. So could anyone please tell me what I'm doing wrong, I'd appreciate your time, and effort.

该错误似乎也显示了信息,这似乎是 NASM 寄存器,并且它抱怨找不到 ROM 文件。所以任何人都可以告诉我我做错了什么,我很感激你的时间和努力。

采纳答案by Job

The suggestion made by Ben Voigt is not your problem. I have the exact same output when I run my kernel and it doesn't cause any problems.

Ben Voigt 提出的建议不是您的问题。当我运行内核时,我有完全相同的输出,它不会导致任何问题。

The reason QEMU aborts is the following:

QEMU 中止的原因如下:

qemu: fatal: Trying to execute code outside RAM or ROM at 0x000a0000

qemu:致命:试图在 0x000a0000 处的 RAM 或 ROM 之外执行代码

This means your kernel tries to execute code from an invalid memory location. Thus, it's a bug in your kernel and has nothing to do with QEMU.

这意味着您的内核会尝试从无效的内存位置执行代码。因此,这是您内核中的一个错误,与 QEMU 无关。

Edit:Just a hint on where your bug may be. Looking at your register dump, it is clear that the last executed instruction is just below 640K (at 0x9fffb). On my machine, QEMU reports all memory between 637K and 1M as unavailable. You always have to be careful not to use unavailable memory. A safe bet is to just stay below 637K until you are able to get a memory map and knowwhat memory you can use.

编辑:只是提示您的错误可能在哪里。查看您的寄存器转储,很明显最后执行的指令刚好低于 640K(在 0x9fffb)。在我的机器上,QEMU 将 637K 和 1M 之间的所有内存报告为不可用。您必须始终小心不要使用不可用的内存。一个安全的选择是保持在 637K 以下,直到您能够获得内存映射并知道可以使用的内存。

回答by Ben Voigt

You need to fix this:

你需要解决这个问题:

pci_add_option_rom: failed to find romfile "pxe-rtl8139.bin"

pci_add_option_rom: 找不到 romfile "pxe-rtl8139.bin"

Either provide the necessary file (re-install qemu?) or change the VM configuration and remove the network card. The virtual network can't operate without this file.

要么提供必要的文件(重新安装 qemu?),要么更改 VM 配置并移除网卡。没有这个文件,虚拟网络就无法运行。

回答by Radek

I think, You should point to valid bzImage instead of os.bin. Personally I use kvm -kernel arch/x86/boot/bzImage.

我认为,您应该指向有效的 bzImage 而不是 os.bin。我个人使用 kvm -kernel arch/x86/boot/bzImage。

There are two nice tutorials

有两个不错的教程

http://softperience.eu/wiki/Wiki.jsp?page=Developing%20Linux%20Kernel%20with%20Netbeans

http://softperience.eu/wiki/Wiki.jsp?page=Developing%20Linux%20Kernel%20with%20Netbeans

http://softperience.eu/wiki/Wiki.jsp?page=Advanced%20Linux%20Kernel%20Developing%20with%20Netbeansw

http://softperience.eu/wiki/Wiki.jsp?page=Advanced%20Linux%20Kernel%20Developing%20with%20Netbeansw

with few nice tricks

有一些不错的技巧

回答by Peter Teoh

Firstly if there is no kvm, ie, u must "modprobe kvm" and "modprobe kvm_intel" (or modprobe kvm_amd" if you are on AMD-based processor), to load the kvm kernel module before using qemu. But when qemu detected there is no kvm loaded, meaning /dev/kvm is not present, then it will still go ahead with the execution, except there is no hardware virtualization (see http://en.wikipedia.org/wiki/X86_virtualization).

首先,如果没有 kvm,即你必须“modprobe kvm”和“modprobe kvm_intel”(如果你使用的是基于 AMD 的处理器,或者 modprobe kvm_amd),在使用 qemu 之前加载 kvm 内核模块。但是当 qemu 在那里检测到时没有加载 kvm,意味着 /dev/kvm 不存在,那么它仍然会继续执行,除了没有硬件虚拟化(参见http://en.wikipedia.org/wiki/X86_virtualization)。

Neither is the option rom ("pxe-rtl8139.bin") the showstopper, I think, which is why it still continue execution (see Qemu source code):

我认为,选项 rom ("pxe-rtl8139.bin") 也不是显示停止器,这就是它仍然继续执行的原因(请参阅 Qemu 源代码):

./hw/pci.c:
        error_report("%s: failed to find romfile \"%s\"",

But the main error in your case is the address 0xa000:

但是您的情况的主要错误是地址 0xa000:

"Trying to execute code outside RAM or ROM at 0x000a0000"

“试图在 0x000a0000 处执行 RAM 或 ROM 之外的代码”

And that is illegal - as the address higher than 0xa0000 is called the memory hole. Refer to the diagram in:

这是非法的——因为高于 0xa0000 的地址被称为内存空洞。参考图:

http://www.cs.cmu.edu/~410-s07/p4/p4-boot.pdf

http://www.cs.cmu.edu/~410-s07/p4/p4-boot.pdf

which describe the task needed in writing a bootloader (see page 15 for the description on memory hole).

其中描述了编写引导加载程序所需的任务(有关内存空洞的描述,请参见第 15 页)。

    static inline tb_page_addr_t get_page_addr_code(CPUState *env1, target_ulong addr)
{
    int mmu_idx, page_index, pd;
    void *p;

    page_index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
    mmu_idx = cpu_mmu_index(env1);
    if (unlikely(env1->tlb_table[mmu_idx][page_index].addr_code !=
                 (addr & TARGET_PAGE_MASK))) {
        ldub_code(addr);
    }
    pd = env1->tlb_table[mmu_idx][page_index].addr_code & ~TARGET_PAGE_MASK;
    if (pd > IO_MEM_ROM && !(pd & IO_MEM_ROMD)) {
        cpu_abort(env1, "Trying to execute code outside RAM or ROM at 0x" TARGET_FMT_lx "\n", addr);
    }
    p = (void *)((uintptr_t)addr + env1->tlb_table[mmu_idx][page_index].addend);
    return qemu_ram_addr_from_host_nofail(p);
}

And as u can see, the error is a serious "cpu_abort()" serious error. Essentially, in page 5 of the CMU's bootloader article above0xa0000 is the highest address u can access during realmode bootup.

正如您所看到的,该错误是一个严重的“cpu_abort()”严重错误。本质上,在 CMU引导加载程序文章的第 5 页中0xa0000以上是您在实模式引导期间可以访问的最高地址。