microsoft/openvmm

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
7d61702bf0cba28ca985f1a5ef27c4e1caec009c

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

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