microsoft/openvmm
Publicmirrored fromhttps://github.com/microsoft/openvmmAvailable
Guide/src/dev_guide/getting_started/cross_compile.md
303lines · modecode
| 1 | # \[WSL2] Cross Compiling from WSL2 to Windows |
| 2 | |
| 3 | Setting up cross compilation is very useful, as it allows using the same repo |
| 4 | cloned in WSL2 to both develop OpenHCL, as well as launch it via OpenVMM via the |
| 5 | WHP backend. |
| 6 | |
| 7 | ## Required Dependencies |
| 8 | |
| 9 | Note that this requires some additional dependencies, described below. |
| 10 | |
| 11 | ### Windows deps |
| 12 | |
| 13 | Visual Studio build tools must be installed, along with the Windows SDK. |
| 14 | [This is the same as what's required to build OpenVMM on windows.](./windows.md#installing-rust) |
| 15 | |
| 16 | ### WSL deps |
| 17 | |
| 18 | The msvc target `x86_64-pc-windows-msvc` must be installed for the toolchain |
| 19 | being used in WSL. This can be added by doing the following: |
| 20 | |
| 21 | ```bash |
| 22 | rustup target add x86_64-pc-windows-msvc |
| 23 | ``` |
| 24 | |
| 25 | Additional build tools must be installed as well. If your distro has LLVM 14 |
| 26 | available (Ubuntu 22.04 or newer): |
| 27 | ```bash |
| 28 | sudo apt install clang-tools-14 lld-14 llvm-dev |
| 29 | ``` |
| 30 | |
| 31 | Otherwise, follow the steps at https://apt.llvm.org/ to install a specific |
| 32 | version, by adding the correct apt repos. Note that you must install |
| 33 | `clang-tools-14` as default `clang-14` uses gcc style arguments, where |
| 34 | `clang-cl-14` uses msvc style arguments. You can use their helper script as |
| 35 | well: |
| 36 | ```bash |
| 37 | wget https://apt.llvm.org/llvm.sh |
| 38 | chmod +x llvm.sh |
| 39 | sudo ./llvm.sh 14 |
| 40 | sudo apt install clang-tools-14 |
| 41 | ``` |
| 42 | |
| 43 | ## Setting up the terminal environment |
| 44 | |
| 45 | Source the `build_support/setup_windows_cross.sh` script from your terminal |
| 46 | instance. For example, the following script will do this along with setting a |
| 47 | default cargo build target: |
| 48 | |
| 49 | ```bash |
| 50 | #!/bin/bash |
| 51 | |
| 52 | # Setup environment and windows cross tooling. |
| 53 | |
| 54 | export CARGO_BUILD_TARGET=x86_64-unknown-linux-gnu |
| 55 | cd path/to/openvmm || exit |
| 56 | . build_support/setup_windows_cross.sh |
| 57 | exec "$SHELL" |
| 58 | ``` |
| 59 | |
| 60 | For developers using shells other than bash, you may need to run the |
| 61 | `setup_windows_cross.sh` script in bash then launch your shell in order to get |
| 62 | the correct environment variables. |
| 63 | |
| 64 | ## Editing with vscode |
| 65 | |
| 66 | You can have rust-analyzer target Windows, which will allow you to use the same |
| 67 | repo for OpenHCL, Linux, and Windows changes, but the vscode remote server |
| 68 | must be launched from the terminal window that sourced the setup script. You can |
| 69 | do this by closing all vscode windows then opening your workspace with |
| 70 | `code <path to workspace>` in your terminal. |
| 71 | |
| 72 | Add the following to your workspace settings for a vscode workspace |
| 73 | dedicated to Windows: |
| 74 | |
| 75 | ```json |
| 76 | "settings": { |
| 77 | "rust-analyzer.cargo.target": "x86_64-pc-windows-msvc" |
| 78 | } |
| 79 | ``` |
| 80 | |
| 81 | ## Running Windows OpenVMM from within WSL |
| 82 | |
| 83 | You can build and run the windows version of OpenVMM by overriding the target |
| 84 | field of cargo commands, via `--target x86_64-pc-windows-msvc`. For example, the |
| 85 | following command will run OpenVMM with WHP: |
| 86 | |
| 87 | ```bash |
| 88 | cargo run --target x86_64-pc-windows-msvc |
| 89 | ``` |
| 90 | |
| 91 | You can optionally set cargo aliases for this so that you don't have to type out |
| 92 | the full target every time. Add the following to your `~/.cargo/config.toml`: |
| 93 | |
| 94 | ```toml |
| 95 | [alias] |
| 96 | winbuild = "build --target x86_64-pc-windows-msvc" |
| 97 | wincheck = "check --target x86_64-pc-windows-msvc" |
| 98 | winclippy = "clippy --target x86_64-pc-windows-msvc" |
| 99 | windoc = "doc --target x86_64-pc-windows-msvc" |
| 100 | winrun = "run --target x86_64-pc-windows-msvc" |
| 101 | wintest = "test --target x86_64-pc-windows-msvc" |
| 102 | ``` |
| 103 | |
| 104 | You can then run the windows version of OpenVMM by running: |
| 105 | |
| 106 | ```bash |
| 107 | cargo winrun |
| 108 | ``` |
| 109 | |
| 110 | OpenVMM configures some environment variables that specify the default Linux kernel, |
| 111 | initrd, and UEFI firmware. To make those variables available in Windows, run the following: |
| 112 | |
| 113 | ```bash |
| 114 | export WSLENV=$WSLENV:X86_64_OPENVMM_LINUX_DIRECT_KERNEL:X86_64_OPENVMM_LINUX_DIRECT_INITRD:AARCH64_OPENVMM_LINUX_DIRECT_KERNEL:AARCH64_OPENVMM_LINUX_DIRECT_INITRD:X86_64_OPENVMM_UEFI_FIRMWARE:AARCH64_OPENVMM_UEFI_FIRMWARE:RUST_BACKTRACE |
| 115 | ``` |
| 116 | |
| 117 | ### Speeding up Windows OpenVMM launch |
| 118 | |
| 119 | Due to filesystem limitations on WSL, launching OpenVMM directly will be somewhat |
| 120 | slow. Instead, you can copy the built binaries to a location on in the Windows |
| 121 | filesystem and then launch them via WSL. |
| 122 | |
| 123 | Quite a few folks working on the OpenVMM project have hacked together personal |
| 124 | helper scripts to automate this process. Here is one example: (update the |
| 125 | variables at the top of the file as necessary.) |
| 126 | |
| 127 | ```bash |
| 128 | #!/bin/bash |
| 129 | |
| 130 | # build & run script for openhcl testing with openvmm |
| 131 | |
| 132 | set -e |
| 133 | |
| 134 | args="-m 4GB -p 4" |
| 135 | copy_symbols=true |
| 136 | copy_remote=false |
| 137 | windows_temp="/mnt/e/cross" |
| 138 | windows_temp_win="E:\\cross" |
| 139 | remote_temp="\\\\<remote_computer>\\cross" |
| 140 | windows_enlistment="/mnt/e/openvmm" |
| 141 | |
| 142 | disk_path="$windows_temp_win\\disk.vhdx" |
| 143 | uefi_firmware="$windows_temp_win\\MSVM.fd" |
| 144 | |
| 145 | if [[ $# -lt 2 ]]; then |
| 146 | echo "Usage: $0 <build|run|ohcldiag-dev> <x64|aarch64>..." |
| 147 | exit 1 |
| 148 | fi |
| 149 | |
| 150 | if [[ $2 == "aarch64" ]]; then |
| 151 | arch="aarch64" |
| 152 | short_arch="aarch64" |
| 153 | elif [[ $2 == "x64" ]]; then |
| 154 | arch="x86_64" |
| 155 | short_arch="x64" |
| 156 | else |
| 157 | echo "Unknown arch: $2" |
| 158 | echo "Usage: $0 $1 <x64|aarch64>" |
| 159 | exit 2 |
| 160 | fi |
| 161 | |
| 162 | uhdiag_path="$windows_temp_win\\uhdiag" |
| 163 | openvmm_path="$windows_temp/openvmm.exe" |
| 164 | windows_openvmm="$windows_temp/openvmm" |
| 165 | base_igvm="flowey-out/artifacts/build-igvm" |
| 166 | win_target="$arch-pc-windows-msvc" |
| 167 | base_win="target/$win_target/debug" |
| 168 | |
| 169 | if [[ $1 == "build" || $1 == "run" ]]; then |
| 170 | |
| 171 | build_args="--target $arch-pc-windows-msvc" |
| 172 | |
| 173 | if [[ $3 == "vmm" ]]; then |
| 174 | |
| 175 | |
| 176 | if [[ $4 == "uefi" ]]; then |
| 177 | args+=" --uefi --uefi-firmware $uefi_firmware --disk memdiff:$disk_path" |
| 178 | elif [[ $4 == "linux" ]]; then |
| 179 | args+="" |
| 180 | else |
| 181 | echo "Unknown load mode: $4" |
| 182 | echo "Usage: $0 $1 $2 $3 <uefi|linux>" |
| 183 | exit 2 |
| 184 | fi |
| 185 | |
| 186 | elif [[ $3 == "hcl" ]]; then |
| 187 | |
| 188 | if [[ $4 == "uefi" ]]; then |
| 189 | recipe="$short_arch" |
| 190 | args+=" --disk memdiff:$disk_path --gfx --vmbus-com1-serial term --uefi-console-mode com1 --uefi" |
| 191 | elif [[ $4 == "linux" ]]; then |
| 192 | recipe="$short_arch-test-linux-direct" |
| 193 | args+=" --vmbus-com1-serial term --vmbus-com2-serial term" |
| 194 | else |
| 195 | echo "Unknown load mode: $4" |
| 196 | echo "Usage: $0 $1 $2 $3 <uefi|linux>" |
| 197 | exit 2 |
| 198 | fi |
| 199 | |
| 200 | ohcl_name="openhcl-$recipe.bin" |
| 201 | ohcl_path="$base_igvm/debug/$recipe" |
| 202 | ohcl_symbols="openvmm_hcl" |
| 203 | |
| 204 | args+=" --hv --vtl2 --igvm $windows_temp_win\\$ohcl_name --vtl2-vsock-path $uhdiag_path --com3 term" |
| 205 | |
| 206 | echo "Building OpenHCL..." |
| 207 | ( |
| 208 | set -x |
| 209 | cargo xflowey build-igvm $recipe |
| 210 | ) |
| 211 | |
| 212 | else |
| 213 | echo "Unknown package: $2" |
| 214 | echo "Usage: $0 $1 <vmm|hcl>" |
| 215 | exit 2 |
| 216 | fi |
| 217 | |
| 218 | echo |
| 219 | |
| 220 | if [[ $5 == "unstable" ]]; then |
| 221 | build_args+=" --features unstable_whp" |
| 222 | fi |
| 223 | |
| 224 | echo "Building openvmm..." |
| 225 | ( |
| 226 | set -x |
| 227 | cargo build $build_args |
| 228 | ) |
| 229 | echo |
| 230 | |
| 231 | # Copy to Windows |
| 232 | echo "Copying to windows" |
| 233 | |
| 234 | if [[ $3 == "hcl" ]]; then |
| 235 | ( |
| 236 | set -x |
| 237 | cp -u "$ohcl_path/$ohcl_name" "$windows_temp/$ohcl_name" -f |
| 238 | mkdir -p "$windows_enlistment/$ohcl_path" |
| 239 | cp -u "$ohcl_path/$ohcl_name" "$windows_enlistment/$ohcl_path/$ohcl_name" -f |
| 240 | ) |
| 241 | if $copy_remote; then |
| 242 | ( |
| 243 | set -x |
| 244 | powershell.exe Copy-Item "$windows_temp_win\\$ohcl_name" "$remote_temp\\$ohcl_name" -Force |
| 245 | ) |
| 246 | fi |
| 247 | if $copy_symbols; then |
| 248 | ( |
| 249 | set -x |
| 250 | cp -u "$ohcl_path/$ohcl_symbols" "$windows_temp/openvmm_hcl" -f |
| 251 | cp -u "$ohcl_path/$ohcl_symbols.dbg" "$windows_temp/openvmm_hcl.dbg" -f |
| 252 | ) |
| 253 | fi |
| 254 | fi |
| 255 | |
| 256 | ( |
| 257 | set -x |
| 258 | cp -u "$base_win/openvmm.exe" $openvmm_path -f |
| 259 | mkdir -p "$windows_enlistment/$base_win" |
| 260 | cp -u "$base_win/openvmm.exe" "$windows_enlistment/$base_win/openvmm.exe" -f |
| 261 | ) |
| 262 | if $copy_remote; then |
| 263 | ( |
| 264 | set -x |
| 265 | powershell.exe Copy-Item "$windows_temp_win\\openvmm.exe" "$remote_temp\\openvmm.exe" -Force |
| 266 | ) |
| 267 | fi |
| 268 | if $copy_symbols; then |
| 269 | ( |
| 270 | set -x |
| 271 | cp -u "$base_win/openvmm.pdb" "$windows_temp/openvmm.pdb" -f |
| 272 | ) |
| 273 | fi |
| 274 | |
| 275 | echo |
| 276 | |
| 277 | if [[ $1 == "run" ]]; then |
| 278 | ( |
| 279 | set -x |
| 280 | $openvmm_path $args |
| 281 | ) |
| 282 | else |
| 283 | echo $openvmm_path $args |
| 284 | fi |
| 285 | |
| 286 | elif [[ $1 == "ohcldiag-dev" ]]; then |
| 287 | |
| 288 | shift 2 |
| 289 | ( |
| 290 | set -x |
| 291 | cargo build --target $arch-pc-windows-msvc -p ohcldiag-dev |
| 292 | cp -u $base_win/ohcldiag-dev.exe "$windows_temp/ohcldiag-dev.exe" -f |
| 293 | "$windows_temp/ohcldiag-dev.exe" "$uhdiag_path" "$@" |
| 294 | ) |
| 295 | |
| 296 | else |
| 297 | |
| 298 | echo "Unknown command: $1" |
| 299 | echo "Usage: $0 <build|run|ohcldiag-dev> <x64|aarch64>..." |
| 300 | exit 1 |
| 301 | |
| 302 | fi |
| 303 | ``` |