microsoft/openvmm

Public

mirrored fromhttps://github.com/microsoft/openvmmAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
e8acaefba35969bbca0ffae96452cd05b47aa621

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

Guide/src/dev_guide/getting_started/cross_compile.md

303lines · modecode

1# \[WSL2] Cross Compiling from WSL2 to Windows
2
3Setting up cross compilation is very useful, as it allows using the same repo
4cloned in WSL2 to both develop OpenHCL, as well as launch it via OpenVMM via the
5WHP backend.
6
7## Required Dependencies
8
9Note that this requires some additional dependencies, described below.
10
11### Windows deps
12
13Visual 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
18The msvc target `x86_64-pc-windows-msvc` must be installed for the toolchain
19being used in WSL. This can be added by doing the following:
20
21```bash
22rustup target add x86_64-pc-windows-msvc
23```
24
25Additional build tools must be installed as well. If your distro has LLVM 14
26available (Ubuntu 22.04 or newer):
27```bash
28sudo apt install clang-tools-14 lld-14 llvm-dev
29```
30
31Otherwise, follow the steps at https://apt.llvm.org/ to install a specific
32version, 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
35well:
36```bash
37wget https://apt.llvm.org/llvm.sh
38chmod +x llvm.sh
39sudo ./llvm.sh 14
40sudo apt install clang-tools-14
41```
42
43## Setting up the terminal environment
44
45Source the `build_support/setup_windows_cross.sh` script from your terminal
46instance. For example, the following script will do this along with setting a
47default cargo build target:
48
49```bash
50#!/bin/bash
51
52# Setup environment and windows cross tooling.
53
54export CARGO_BUILD_TARGET=x86_64-unknown-linux-gnu
55cd path/to/openvmm || exit
56. build_support/setup_windows_cross.sh
57exec "$SHELL"
58```
59
60For 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
62the correct environment variables.
63
64## Editing with vscode
65
66You can have rust-analyzer target Windows, which will allow you to use the same
67repo for OpenHCL, Linux, and Windows changes, but the vscode remote server
68must be launched from the terminal window that sourced the setup script. You can
69do this by closing all vscode windows then opening your workspace with
70`code <path to workspace>` in your terminal.
71
72Add the following to your workspace settings for a vscode workspace
73dedicated 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
83You can build and run the windows version of OpenVMM by overriding the target
84field of cargo commands, via `--target x86_64-pc-windows-msvc`. For example, the
85following command will run OpenVMM with WHP:
86
87```bash
88cargo run --target x86_64-pc-windows-msvc
89```
90
91You can optionally set cargo aliases for this so that you don't have to type out
92the full target every time. Add the following to your `~/.cargo/config.toml`:
93
94```toml
95[alias]
96winbuild = "build --target x86_64-pc-windows-msvc"
97wincheck = "check --target x86_64-pc-windows-msvc"
98winclippy = "clippy --target x86_64-pc-windows-msvc"
99windoc = "doc --target x86_64-pc-windows-msvc"
100winrun = "run --target x86_64-pc-windows-msvc"
101wintest = "test --target x86_64-pc-windows-msvc"
102```
103
104You can then run the windows version of OpenVMM by running:
105
106```bash
107cargo winrun
108```
109
110OpenVMM configures some environment variables that specify the default Linux kernel,
111initrd, and UEFI firmware. To make those variables available in Windows, run the following:
112
113```bash
114export 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
119Due to filesystem limitations on WSL, launching OpenVMM directly will be somewhat
120slow. Instead, you can copy the built binaries to a location on in the Windows
121filesystem and then launch them via WSL.
122
123Quite a few folks working on the OpenVMM project have hacked together personal
124helper scripts to automate this process. Here is one example: (update the
125variables 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
132set -e
133
134args="-m 4GB -p 4"
135copy_symbols=true
136copy_remote=false
137windows_temp="/mnt/e/cross"
138windows_temp_win="E:\\cross"
139remote_temp="\\\\<remote_computer>\\cross"
140windows_enlistment="/mnt/e/openvmm"
141
142disk_path="$windows_temp_win\\disk.vhdx"
143uefi_firmware="$windows_temp_win\\MSVM.fd"
144
145if [[ $# -lt 2 ]]; then
146 echo "Usage: $0 <build|run|ohcldiag-dev> <x64|aarch64>..."
147 exit 1
148fi
149
150if [[ $2 == "aarch64" ]]; then
151 arch="aarch64"
152 short_arch="aarch64"
153elif [[ $2 == "x64" ]]; then
154 arch="x86_64"
155 short_arch="x64"
156else
157 echo "Unknown arch: $2"
158 echo "Usage: $0 $1 <x64|aarch64>"
159 exit 2
160fi
161
162uhdiag_path="$windows_temp_win\\uhdiag"
163openvmm_path="$windows_temp/openvmm.exe"
164windows_openvmm="$windows_temp/openvmm"
165base_igvm="flowey-out/artifacts/build-igvm"
166win_target="$arch-pc-windows-msvc"
167base_win="target/$win_target/debug"
168
169if [[ $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
286elif [[ $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
296else
297
298 echo "Unknown command: $1"
299 echo "Usage: $0 <build|run|ohcldiag-dev> <x64|aarch64>..."
300 exit 1
301
302fi
303```