Skip to content

Instantly share code, notes, and snippets.

@Jeket
Forked from niw/README.en.md
Created April 7, 2021 09:45
Show Gist options
  • Save Jeket/e40073e8e3ad9fee8e28ec5c25c7c0ee to your computer and use it in GitHub Desktop.
Save Jeket/e40073e8e3ad9fee8e28ec5c25c7c0ee to your computer and use it in GitHub Desktop.
How to run Windows 10 on ARM or Ubuntu for ARM64 in QEMU on Apple Silicon Mac

ARM 版 Windows 10 を Hypervisor.framework のパッチをあてた QEMU を使って Apple Silicon な Mac で動かす

ARM 版 Windows 10 を Apple Silicon な Mac で動かす簡単な方法です。

メモ: 2020年12月5日現時点での方法で、今後おおきく改善されていくと思います(だといいな。)

手順

  1. Xcode を App Store で、あるいは Command Line Tools を Apple Silicon な Mac にインストール

    xcode-select --install
    
  2. MacPorts で QEMU の依存ライブラリをインストール

    https://www.macports.org/install.php

    sudo port install ninja pkgconfig texinfo libtool glib2 libpixman
    

    sudo とか使いたくない場合は、/opt/local$USER:staff で作って、MacPorts をソースコードからインストールすれば可能です。

  3. QEMU のソースコードをとってくる

    git clone https://git.qemu.org/git/qemu.git
    
  4. ローカルブランチを作って、@_AlexGraf氏がつくった Hypervisor.framework のパッチをあてる

    cd qemu
    git checkout d73c46e -b wip/hvf # これは現時点での `origin/master` のコミット。
    curl https://patchwork.kernel.org/series/395899/mbox/|git am
    
  5. QEMU をビルドする

    mkdir build
    cd build
    ../configure --target-list=aarch64-softmmu --enable-cocoa
    make -j
    
    • x86_64 な Homebrew などを使っている場合は問題がおこるので、PATH などを書き換えるなどして無効にしておくこと。
    • Intel な Mac と比べて、めっちゃ速い。
  6. Windows 10 on ARM Insider Preview をダウンロードする

    https://www.microsoft.com/en-us/software-download/windowsinsiderpreviewARM64

  7. VHDX から qcow2 に変換する

    VHDX のままだとかなりの頻度でイメージが壊れる印象があるので qcow2 に変換しておく。

    qemu-img convert -p -O qcow2 ~/Downloads/Windows10_InsiderPreview_Client_ARM64_en-us_20231.VHDX ~/Downloads/Windows10_InsiderPreview_Client_ARM64_en-us_20231.qcow2
    
  8. QEMU 用の EDK II OVMF EFI イメージをダウンロードする。

    https://gist.github.com/niw/4f1f9bb572f40d406866f23b3127919b/raw/f4dab9b407a2ea94ceceb7905eb918e1317bc251/QEMU_EFI-a096471-edk2-stable202011.tar.gz

    ソースコードからビルドする場合は以下を参照のこと。

  9. pflash イメージを作って、画面サイズなどを設定する不揮発なストレージを準備する

    QEMU_EFI-....tar.gz をダウンロードしたディレクトリで以下を実行。

    cd ~/Downloads
    tar xzvf QEMU_EFI-*.tar.gz
    dd if=/dev/zero of=pflash0.img bs=1m count=64
    dd if=/dev/zero of=pflash1.img bs=1m count=64
    dd if=QEMU_EFI.fd of=pflash0.img conv=notrunc
    dd if=QEMU_VARS.fd of=pflash1.img conv=notrunc
    
    • このステップは省略可能。その場合は次のステップで -drive ...,if=pflash,... のかわりに -bios ~/Downloads/QEMU_EFI.fd を指定すること。ただし、EFI での変更が保存されなくなります。
  10. Windows 10 を起動する

    ./qemu-system-aarch64 \
      -monitor stdio \
      -M virt,highmem=off \
      -accel hvf \
      -cpu cortex-a72 \
      -smp 4 \
      -m 4096 \
      -drive file=~/Downloads/pflash0.img,format=raw,if=pflash,readonly=on \
      -drive file=~/Downloads/pflash1.img,format=raw,if=pflash \
      -device ramfb \
      -device qemu-xhci \
      -device usb-kbd \
      -device usb-tablet \
      -device intel-hda \
      -device hda-duplex \
      -nic user,model=virtio \
      -drive file=~/Downloads/Windows10_InsiderPreview_Client_ARM64_en-us_20231.qcow2,format=qcow2,if=none,id=boot,cache=writethrough \
      -device nvme,drive=boot,serial=boot
    
    • イメージへのパスなどは適宜修正してください。
    • TianoCore のロゴが出ている時に ESC を押して、Device ManagerOVMF Platform ConfigurationChange Preferred Resolution for Next Boot と進めて解像度を変更できます。
    • -device usb-mouse より -device usb-tablet を使うことでマウスカーソルがロックされなくなり使いやすくなります。
    • -device intel-hda -device hda-duplex でオーディオが使えるようになります。
    • -nic user,model=virtio がネットワークインターフェイスです。有効にするには以下を参照してください。

インターネットに接続する

VirtIO NIC は標準では有効にならないので、デバイスドライバを入れる必要がある。

  1. VirtIO デバイスドライバの ISO を Fedora Project からダウンロード。

    https://fedorapeople.org/groups/virt/virtio-win/direct-downloads/archive-virtio/virtio-win-0.1.190-1/virtio-win-0.1.190.iso

  2. デバイスドライバの ISO をマウント

    qemu-system-aarch64 のオプションに以下を追加する。

    -drive file=~/Downloads/virtio-win-0.1.190.iso,media=cdrom,if=none,id=drivers \
    -device usb-storage,drive=drivers
    

    イメージへのパスなどは適宜修正してください。

  3. デバイスドライバの署名検証を無効にする

    Windows を起動して Windows スタートボタン を右クリック、Command Prompt (Admin) を選択。

    bcdedit を使ってテスト署名されたデバイスドライバを有効にする。

    bcdedit.exe -set TESTSIGNING ON
    

    このあと Windows を再起動すること。

    詳細はこちらを参照のこと。

  4. デバイスドライバのインストール

    Windows が再度起動したら Windows スタートボタン を右クリック、Device Manager を選択。

    Device Manager のウィンドウで、View メニューの Devices by Connection を選択。

    デバイスのツリーから、ACPU ARM64-based PCMicrosoft ACPI-Compliant SystemPCI Express Root Complex と選択してその中にある Unknown device を探す。 (Unknown device はデバイスのツリーに沢山あるけれど、PCI Express Root Complexの下にあるのが、VirtIO NIC。)

    該当の Unknown device を右クリックして、Update Drivers を選択、Browse my computer for drivers を選んで、D:\NetKVM\w10\ARM64 を指定。

    Next をクリックして Red Hat VertIO Ethernet Adapter をインストール。

EDK II の OVMF EFI イメージをビルドする

OVMF EFI イメージをソースコードから作る場合、とりあえず簡単にビルドするには、まず VMWare Fusion とか、VirtualBox とかを Intel Mac (そう、Intel Mac が必要)にインストールして、Ubuntu などの Linux が必要になります。

  1. EDK II のソースコードを取ってくる。

    git clone https://github.com/tianocore/edk2.git
    cd edk2
    git checkout edk2-stable202011 # stable ってタグされてるやつがいい
    git submodule update --init --recursive
    

    Hypervisor.framework パッチの制限で xHCI を無効にする必要があれば /ArmVirtPkg/ArmVirtQemu.dsc/ArmVirtPkg/ArmVirtQemuFvMain.fdf.inc にある MdeModulePkg/Bus/Pci/XhciDxe/XhciDxe.inf の行をコメントアウト。たぶん現状は問題ない。

  2. ビルドする

    apt install install iasl python3 python3-distutils uuid-dev gcc-aarch64-linux-gnu
    source edksetup.sh
    make -C BaseTools
    env GCC5_AARCH64_PREFIX=aarch64-linux-gnu- build -a AARCH64 -t GCC5 -p ArmVirtPkg/ArmVirtQemu.dsc
    

    Build/ArmVirtQemu-AARCH64/DEBUG_GCC5/FVQEMU_EFI.fd とかができるはず。

How to run Windows 10 on ARM in QEMU with Hypervisor.framework patches on Apple Silicon Mac

Here is easy steps to try Windows 10 on ARM on your Apple Silicon Mac. Enjoy!

NOTE: that this is current, 12/5/2020 state. It will be improved a lot in upcoming a few months... (I hope.)

Steps

  1. Install Xcode from App Store or install Command Line Tools on your Mac running on Apple Silicon.

    xcode-select --install
    
  2. Install MacPorts and QEMU dependencies.

    https://www.macports.org/install.php

    sudo port install ninja pkgconfig texinfo libtool glib2 libpixman
    

    Don’t want to use sudo with MacPorts? Create /opt/local as $USER:staff and install MacPorts from the source code.

  3. Clone QEMU source code.

    git clone https://git.qemu.org/git/qemu.git
    
  4. Create a local branch then apply Hypervisor.framework patches that @_AlexGraf made.

    cd qemu
    git checkout d73c46e -b wip/hvf # This is current `origin/master` when this document is written.
    curl https://patchwork.kernel.org/series/395899/mbox/|git am
    
  5. Build QEMU

    mkdir build
    cd build
    ../configure --target-list=aarch64-softmmu --enable-cocoa
    make -j
    
    • Please disable any x86_64 tools such as the one installed by Homebrew, please rewrite PATH etc to disable them.
    • This is surprisingly fast on Apple Silicon Mac, compare to Intel Mac...
  6. Download Windows 10 on ARM Insider Preview

    https://www.microsoft.com/en-us/software-download/windowsinsiderpreviewARM64

  7. Convert VHDX downloaded Windows image to qcow2 format

    It seems that VHDX often gets broken while it is being used, so probably it is better to convert it to qcow2 format before using it.

    qemu-img convert -p -O qcow2 ~/Downloads/Windows10_InsiderPreview_Client_ARM64_en-us_20231.VHDX ~/Downloads/Windows10_InsiderPreview_Client_ARM64_en-us_20231.qcow2
    
  8. Download pre-build EDK II OVMF EFI image for QEMU.

    https://gist.github.com/niw/4f1f9bb572f40d406866f23b3127919b/raw/f4dab9b407a2ea94ceceb7905eb918e1317bc251/QEMU_EFI-a096471-edk2-stable202011.tar.gz

    To build it from the source code, see the following section.

  9. Prepare pflash for non-volatile variable store, such as screen resolution.

    In the directory where you downloaded QEMU_EFI-....tar.gz, run following commands.

    cd ~/Downloads
    tar xzvf QEMU_EFI-*.tar.gz
    dd if=/dev/zero of=pflash0.img bs=1m count=64
    dd if=/dev/zero of=pflash1.img bs=1m count=64
    dd if=QEMU_EFI.fd of=pflash0.img conv=notrunc
    dd if=QEMU_VARS.fd of=pflash1.img conv=notrunc
    
    • This step is optional, you can use -bios ~/Downloads/QEMU_EFI.fd instead of -drive ...if=pflash lines in the next step, but in that case, any changes in EFI will not be persistent.
  10. Run Windows 10

    ./qemu-system-aarch64 \
      -monitor stdio \
      -M virt,highmem=off \
      -accel hvf \
      -cpu cortex-a72 \
      -smp 4 \
      -m 4096 \
      -drive file=~/Downloads/pflash0.img,format=raw,if=pflash,readonly=on \
      -drive file=~/Downloads/pflash1.img,format=raw,if=pflash \
      -device ramfb \
      -device qemu-xhci \
      -device usb-kbd \
      -device usb-tablet \
      -device intel-hda \
      -device hda-duplex \
      -drive file=~/Downloads/Windows10_InsiderPreview_Client_ARM64_en-us_20231.qcow2,format=qcow2,if=none,id=boot,cache=writethrough \
      -device nvme,drive=boot,serial=boot
    
    • Please modify each path to the image depends on your environment.
    • Hit ESC while you see TianoCore, then Device Manager, OVMF Platform Configuration, Change Preferred Resolution for Next Boot to change screen resolution.
    • Use -device usb-tablet instead of -device usb-mouse allows transparently moving mouse cursor.
    • -device intel-hda -device hda-duplex to enable audio.
    • -nic user,model=virtio is the network interface. To enable it, see the following section.

Enable the internet

VirtIO NIC is not enabled by default. To make it works, you need to install device driver.

  1. Download VirtIO device drivers ISO from Fedora Project.

    https://fedorapeople.org/groups/virt/virtio-win/direct-downloads/archive-virtio/virtio-win-0.1.190-1/virtio-win-0.1.190.iso

  2. Mount device drivers ISO

    Add next options to qemu-system-aarch64.

    -drive file=~/Downloads/virtio-win-0.1.190.iso,media=cdrom,if=none,id=drivers \
    -device usb-storage,drive=drivers
    

    Please modify each path to the image depends on your environment.

  3. Disable device driver signature enforcement

    Boot Windows, then right click Windows Start button, then select Command Prompt (Admin).

    Use bcdedit to enable test-signed device drivers.

    bcdedit.exe -set TESTSIGNING ON
    

    Then reboot Windows.

    See here for the details.

  4. Install driver

    Once Windows booted again, then right click Windows Start button, then select Device Manager.

    In Device Manager, select View menu then Devices by Connection.

    Navigate in the device tree, select ACPU ARM64-based PC, Microsoft ACPI-Compliant System, PCI Express Root Complex, then you will see one Unknown device there. (There are many Unknown device in tree but the one under PCI Express Root Complex is the VirtIO NIC.)

    Right click Unknown device then select Update Drivers, then Browse my computer for drivers, then select D:\NetKVM\w10\ARM64.

    Click Next to install Red Hat VertIO Ethernet Adapter.

Build EDK II OVMF EFI image from the source code

To build this from the source code, you may need to setup Linux environment. Install VMWare Fusion or VirtualBox on your Intel Mac (Yes, Intel Mac.) and install Ubuntu.

  1. Checkout EDK II source code.

    git clone https://github.com/tianocore/edk2.git
    cd edk2
    git checkout edk2-stable202011 # or any stable tagged commit
    git submodule update --init --recursive
    

    You may want to disable xHCI due to current Hypervisor.framework patch limitation. Comment out MdeModulePkg/Bus/Pci/XhciDxe/XhciDxe.inf in /ArmVirtPkg/ArmVirtQemu.dsc and /ArmVirtPkg/ArmVirtQemuFvMain.fdf.inc. Probably this is no longer needed.

  2. Build it.

    apt install install iasl python3 python3-distutils uuid-dev gcc-aarch64-linux-gnu
    source edksetup.sh
    make -C BaseTools
    env GCC5_AARCH64_PREFIX=aarch64-linux-gnu- build -a AARCH64 -t GCC5 -p ArmVirtPkg/ArmVirtQemu.dsc
    

    Then, you will get QEMU_EFI.fd in Build/ArmVirtQemu-AARCH64/DEBUG_GCC5/FV.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment