QEMU如何根据我执行的ELF文件动态地从aarch64切换到x86?

huangapple go评论65阅读模式
英文:

How can QEMU dynamically switch from aarch64 to x86 based on the ELF I'm executing?

问题

I have an aarch64 image that I've extracted to a folder on my x86 Linux host. I perform the following procedure using QEMU version 6.2:

image_root@user$ qemu-aarch64 -L $PWD ./bin/bash.bash
I have no name!@user:/path/to/image_root$ sudo chroot . ./bin/bash.bash
[sudo] password for user:
bash.bash-5.0#

The shell I end up in is the aarch64 version of bash from the image, which is what I wanted (Yey!).

My question is how could I run both the x86_64 chroot and the aarch64 bash.bash from inside the same QEMU emulation?

My guess is QEMU is dynamically switching from X86_64 and aarch64 depending on the ELF, but It's just a guess and I couldn't find anything about that from a quick browse of the docs.

I know that procedure doesn't work on QEMU version 5.2 and lower, and I'm trying to integrate it into an automation I'm doing, so I'd really like to know what's going on.

英文:

I have an aarch64 image that I've extracted to a folder on my x86 Linux host. I perform the following procedure using QEMU version 6.2:

image_root@user$ qemu-aarch64 -L $PWD ./bin/bash.bash
I have no name!@user:/path/to/image_root$ sudo chroot . ./bin/bash.bash
[sudo] password for user:
bash.bash-5.0#

The shell I end up in is the aarch64 version of bash from the image, which is what I wanted (Yey!).

My question is how could I run both the x86_64 chroot and the aarch64 bash.bash from inside the same QEMU emulation?

My guess is QEMU is dynamically switching from X86_64 and aarch64 depending on the ELF, but It's just a guess and I couldn't find anything about that from a quick browse of the docs.

I know that procedure doesn't work on QEMU version 5.2 and lower, and I'm trying to integrate it into an automation I'm doing, so I'd really like to know what's going on.

答案1

得分: 3

这是因为主机内核的binfmt-misc支持。这是一种灵活的方式,您可以配置内核以表示"如果有人尝试执行与此模式匹配的文件,执行该操作的方式是运行另一个程序并传递该文件"。

当安装了发行版的QEMU包版本时,通常也会负责在binfmt-misc处理中注册QEMU,因此如果您尝试执行AArch64 ELF文件,内核知道必须通过运行qemu-aarch64并传递ELF文件来运行它。

在QEMU内部,exec()系统调用不会特殊处理--我们只是将它传递给主机内核。

综合起来,这意味着如果您尝试执行x86二进制文件,内核会直接运行它,如果您尝试执行aarch64二进制文件,内核会将其传递给QEMU。而且,无论"尝试执行"是从x86程序还是从在QEMU下运行的aarch64程序开始的,都会发生这种情况。

英文:

This works because of the host kernel's <a href="https://docs.kernel.org/admin-guide/binfmt-misc.html">binfmt-misc support</a>. This is a flexible way that you can configure the kernel to say "if somebody tries to execute a file which matches this pattern, the way to do that is to run some other program and pass it the file".

When a distro package version of QEMU is installed, the packaging typically also takes care of registering QEMU with the binfmt-misc handling, so that if you try to execute an AArch64 ELF file then the kernel knows that it must run it by running qemu-aarch64 and passing the ELF file.

Within QEMU itself the exec() system call is not handled specially -- we just pass it to the host kernel.

Put together, this means that if you try to execute an x86 binary, the kernel runs it directly, and if you try to execute an aarch64 binary, the kernel passes it to QEMU. And this happens whether the "try to execute" started from an x86 program or from an aarch64 program run under QEMU.

huangapple
  • 本文由 发表于 2023年4月17日 17:32:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76033661.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定