hv1_emulator/
hypercall.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4//! Methods to support emulating hypercalls.
5
6/// Sets a VP context, as in `HvEnableVpVtl` and `HvStartVirtualProcessor`.
7///
8/// Sets the register state and clears halt/wait-for-sipi states.
9pub fn set_x86_vp_context<T: virt::x86::vp::AccessVpState>(
10    access: &mut T,
11    context: &hvdef::hypercall::InitialVpContextX64,
12) -> Result<(), T::Error> {
13    let &hvdef::hypercall::InitialVpContextX64 {
14        rip,
15        rsp,
16        rflags,
17        cs,
18        ds,
19        es,
20        fs,
21        gs,
22        ss,
23        tr,
24        ldtr,
25        idtr,
26        gdtr,
27        efer,
28        cr0,
29        cr3,
30        cr4,
31        msr_cr_pat,
32    } = context;
33
34    let registers = access.registers()?;
35    let registers = virt::x86::vp::Registers {
36        rsp,
37        rip,
38        rflags,
39        cr0,
40        cr3,
41        cr4,
42        efer,
43        cs: cs.into(),
44        ds: ds.into(),
45        es: es.into(),
46        fs: fs.into(),
47        gs: gs.into(),
48        ss: ss.into(),
49        tr: tr.into(),
50        ldtr: ldtr.into(),
51        idtr: idtr.into(),
52        gdtr: gdtr.into(),
53        ..registers
54    };
55
56    access.set_registers(&registers)?;
57    access.set_pat(&virt::x86::vp::Pat { value: msr_cr_pat })?;
58
59    let mut activity = access.activity()?;
60    activity.mp_state = virt::x86::vp::MpState::Running;
61    access.set_activity(&activity)?;
62
63    Ok(())
64}