メインコンテンツへスキップ

Hypervisor-101-in-Rustの最初にたどり着くまで

·642 文字·4 分·
技術記事 Rust コードリーディング ハイパーバイザ
著者
on-keyday
隠者

はじまり
#

某氏があるリポジトリにStarを付けていたのを見かけたので見てみたらハイパーバイザを1日で作って学べるぜ的なことが書いてあったのでハイパーバイザを作ってみようとしていた。

※実験的にここだけOGPで拾ってきたカードリンク風

だが以下の最初のところにたどり着くまでにすでに1日(嘘数時間程度)かかって詰まったのでメモを残す。

https://tandasat.github.io/Hypervisor-101-in-Rust/hypervisor-setup-and-operation-cycle/exercise-preparation-building-and-running-the-hypervisor-and-navigating-code.html

なおHypervisor-101-in-Rustは以下のコミット時点のものを参照しており、また筆者の環境はArch Linux(kernel version 6.14.2,x86_64)である。

https://github.com/tandasat/Hypervisor-101-in-Rust/tree/00cdd3dcc6c46a96aff22e732bee5dc15cf681ed

Bochsが起動できない
#

まず最初にBochs を使うことに決めて早速書いてあったとおりに

cargo xtask bochs-intel

と打ってみると

00000000000i[      ] debugger using rc file './bochs/dbg_command.txt'.
00000000000i[      ] BXSHARE not set. using compile time default '/usr/share/bochs'
00000000000i[      ] reading configuration from ./bochs/linux_intel.bxrc
00000000000p[SIM   ] >>PANIC<< Plugin 'busmouse' not found
00000000000e[SIM   ] notify called, but no bxevent_callback function is registered
00000000000e[SIM   ] notify called, but no bxevent_callback function is registered
========================================================================
Bochs is exiting with the following message:
[SIM   ] Plugin 'busmouse' not found
========================================================================
00000000000i[SIM   ] quit_sim called with exit code 1
========================================================================
                        Bochs x86 Emulator 3.0
            Built from GitHub snapshot on February 16, 2025
                Timestamp: Sun Feb 16 10:00:00 CET 2025
========================================================================

といったエラーが出た。まずそもそもこのbochsを呼び出している箇所はxtask/src/bochs.rs内にあり 以下のようになっている。

        let bxrc = format!("./bochs/{}_{cpu_type}.bxrc", env::consts::OS);
            let output = Command::new(bochs)
                .args(["-q", "-unlock", "-rc", DBG_CMD, "-f", &bxrc,"-dbglog","./bochs/debug.log"])
                .current_dir(Path::new("./tests"))
                .stdout(Stdio::piped())
                .spawn()
                .unwrap();

ということでtests/bochs/linux_intel.bxrcを開いてbusmouseという記述を探すと以下のような行が見つかった。

plugin_ctrl: biosdev=true, busmouse=false, e1000=false, es1370=false, extfpuirq=true, parallel=true, sb16=false, serial=true, speaker=false, unmapped=true, usb_ehci=false, usb_ohci=false, usb_uhci=false, usb_xhci=false, voodoo=false

ちゃんとbusmouse=falseと書いてあるくせにnot foundとはこれいかにと思ってウンウン悩んだ末、とりあえずxxx=falseとなっている記述を消して

plugin_ctrl: biosdev=true, extfpuirq=true, parallel=true, serial=true, unmapped=true

にしてみたところ別のエラーになった。とりあえずこのpluginに関する問題は解決したようだ。(根本的にはしてないかもしれないがとりあえず動くからいいのだ…)

noguiが使えない
#

続いて出てきたエラーは以下のようなものであった。

00000000000i[      ] debugger using rc file './bochs/dbg_command.txt'.
00000000000i[      ] BXSHARE not set. using compile time default '/usr/share/bochs'
00000000000i[      ] reading configuration from ./bochs/linux_intel.bxrc
00000000000p[      ] >>PANIC<< ./bochs/linux_intel.bxrc:4: display library 'nogui' not available
00000000000e[SIM   ] notify called, but no bxevent_callback function is registered
00000000000e[SIM   ] notify called, but no bxevent_callback function is registered
========================================================================
Bochs is exiting with the following message:
[      ] ./bochs/linux_intel.bxrc:4: display library 'nogui' not available

あれー?存在しないやつかなぁと思ったがドキュメントを見てもあるっぽい(4.3.3節) https://bochs.sourceforge.io/doc/docbook/user/bochsrc.html

ということでChatGPTに聞いたりしてみると--with-noguiなるビルドオプションをつければいい的なことを言われた。 結局

$ yay bochs --editmenu
5 aur/bochs-gdb-stub 2.8-1 (+1 0.00) 
    A portable x86 PC emulation software package with gdbstub
4 aur/bochs-gdb 2.6.11-3 (+1 0.00) (Out-of-date: 2022-06-17) 
    A portable x86 PC emulation software package with gdbstub
3 aur/bochs-sdl2 2.8-2 (+0 0.00) 
    A portable x86 PC emulation software package, including GUI debugger, with SDL2 support
2 aur/bochs-sdl 2.7-1 (+2 0.00) (Orphaned) 
    A portable x86 PC emulation software package, including GUI debugger, with sdl support
1 aur/bochs 3.0-1 (+4 0.00) (Installed)
    A portable x86 PC emulation software package, including GUI debugger
==> Packages to install (eg: 1 2 3, 1-3 or ^4)
==> 1
AUR Explicit (1): bochs-3.0-1
:: PKGBUILD up to date, skipping download: bochs
  1 bochs                            (Installed) (Build Files Exist)
==> Packages to cleanBuild?
==> [N]one [A]ll [Ab]ort [I]nstalled [No]tInstalled or (1 2 3, 1-3, ^4)
==> 1
:: Deleting (1/1): ~/.cache/yay/bochs
HEAD is now at ecd9a06 updated to 3.0
Removing bochs-3.0-1-x86_64.pkg.tar.zst
Removing bochs-3.0.tar.gz
Removing bochs-debug-3.0-1-x86_64.pkg.tar.zst
  1 bochs                            (Installed) (Build Files Exist)
==> Diffs to show?
==> [N]one [A]ll [Ab]ort [I]nstalled [No]tInstalled or (1 2 3, 1-3, ^4)
==> 
  1 bochs                            (Installed) (Build Files Exist)
==> PKGBUILDs to edit?
==> [N]one [A]ll [Ab]ort [I]nstalled [No]tInstalled or (1 2 3, 1-3, ^4)
==> 1
 -> 
$EDITOR is not set
 -> Add $EDITOR or $VISUAL to your environment variables
==> Edit PKGBUILD with?
==> code

そして以下のように編集

(前略)
    ./configure \
        --prefix=/usr \
        --without-wx \
        --with-x11 \
        --with-x \
        --with-term \
        --disable-docbook \
        --enable-cpu-level=6 \
        --enable-fpu \
        --enable-3dnow \
        --enable-smp \
        --enable-x86-64 \
        --enable-avx \
	--enable-evex \
        --enable-long-phy-address \
        --enable-pcidev \
        --enable-usb \
        --enable-debugger \
+       --with-nogui
        #--with-sdl
(後略)

そして

:: Proceed with install? [Y/n] y

とした これによりbochs自体は起動した。

最後のファイル
#

だが次に

[MEM0  ]p| >>PANIC<< ROM: couldn't open ROM image file '/usr/share/ovmf/OVMF.fd'

というエラーが出た。 再度tests/bochs/linux_intel.bxrcを見に行くと以下のようになっていた。

romimage: file="/usr/share/ovmf/OVMF.fd", address=0xffe00000, options=none
vgaromimage: file="/usr/local/share/bochs/VGABIOS-lgpl-latest"

これはtests/bochs/bios内に同じ名前のファイルがそれぞれ存在していたためそちらのパスに差し替えてみたところ解決した。

そして

   5: #0:ERROR: panicked at 'not yet implemented: E#1-1', hypervisor/src/hardware_vt/vmx.rs:71:9

無事最初のところまでたどり着いたのであった。

おわり
#

あとは指示に従って作っていきましょう…

蛇足
#

ソースコードを探しに行った無駄足メモの記録

エラー箇所 https://github.com/bochs-emu/Bochs/blob/d41c0fb4ce96beb4aab5831911d6921d484505b2/bochs/config.cc#L2535

コメントは追記

    // ここでguiライブラリ名を指定して設定しているようだがおそらく失敗しi=1;となる
    if (SIM->get_param_enum(BXPN_SEL_DISPLAY_LIBRARY)->set_by_name(params[1])) {
      i = 2;
    } else {
      i = 1;
    }
    // 個々の条件でi=1に当てはまるので入る。
    if ((num_params == 3) || (i == 1)) {
      if (!strncmp(params[i], "options=", 8)) { // この条件は今回は当てはまらないのでskip
        SIM->get_param_string(BXPN_DISPLAYLIB_OPTIONS)->set(&params[i][8]);
      } else if (i == 1) { // この条件に当てはまりエラーになる
        PARSE_ERR(("%s: display library '%s' not available", context, params[1]));
      } else {
        PARSE_ERR(("%s: display_library directive malformed", context));
      }
    }