virt_mshv_vtl/processor/
mod.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4//! This module contains Underhill specific functionality and implementations of require traits
5//! in order to plug into the rest of the common HvLite code.
6
7pub mod mshv;
8mod nice;
9mod vp_state;
10
11cfg_if::cfg_if! {
12    if #[cfg(guest_arch = "x86_64")] {
13        mod hardware_cvm;
14        pub mod snp;
15        pub mod tdx;
16
17        use crate::TlbFlushLockAccess;
18        use crate::VtlCrash;
19        use bitvec::prelude::BitArray;
20        use bitvec::prelude::Lsb0;
21        use hv1_emulator::synic::ProcessorSynic;
22        use hvdef::HvRegisterCrInterceptControl;
23        use hvdef::HvX64RegisterName;
24        use virt::vp::MpState;
25        use virt::x86::MsrError;
26        use virt_support_apic::LocalApic;
27        use virt_support_x86emu::translate::TranslationRegisters;
28        use virt::vp::AccessVpState;
29        use zerocopy::IntoBytes;
30    } else if #[cfg(guest_arch = "aarch64")] {
31        use hv1_hypercall::Arm64RegisterState;
32        use hvdef::HvArm64RegisterName;
33    } else {
34        compile_error!("unsupported guest architecture");
35    }
36}
37
38use super::Error;
39use super::UhPartitionInner;
40use super::UhVpInner;
41use crate::ExitActivity;
42use crate::GuestVtl;
43use crate::WakeReason;
44use cvm_tracing::CVM_ALLOWED;
45use cvm_tracing::CVM_CONFIDENTIAL;
46use hcl::ioctl::Hcl;
47use hcl::ioctl::ProcessorRunner;
48use hv1_emulator::message_queues::MessageQueues;
49use hv1_hypercall::HvRepResult;
50use hv1_structs::ProcessorSet;
51use hv1_structs::VtlArray;
52use hvdef::HvError;
53use hvdef::HvMessage;
54use hvdef::HvSynicSint;
55use hvdef::NUM_SINTS;
56use hvdef::Vtl;
57use inspect::Inspect;
58use inspect::InspectMut;
59use pal::unix::affinity;
60use pal::unix::affinity::CpuSet;
61use pal_async::driver::Driver;
62use pal_async::driver::PollImpl;
63use pal_async::timer::PollTimer;
64use pal_uring::IdleControl;
65use private::BackingPrivate;
66use std::convert::Infallible;
67use std::future::poll_fn;
68use std::marker::PhantomData;
69use std::sync::Arc;
70use std::sync::atomic::Ordering;
71use std::task::Poll;
72use virt::EmulatorMonitorSupport;
73use virt::Processor;
74use virt::StopVp;
75use virt::VpHaltReason;
76use virt::VpIndex;
77use virt::io::CpuIo;
78use vm_topology::processor::TargetVpInfo;
79use vmcore::vmtime::VmTimeAccess;
80
81/// An object to run lower VTLs and to access processor state.
82///
83/// This is not [`Send`] and can only be instantiated from
84/// [`crate::UhProcessorBox::bind_processor`]. This ensures that it can only be used
85/// from a thread that is affinitized to the VP, since it is only possible to
86/// access lower VTL processor state from the same processor.
87#[derive(InspectMut)]
88#[inspect(extra = "UhProcessor::inspect_extra", bound = "T: Backing")]
89pub struct UhProcessor<'a, T: Backing> {
90    _not_send: PhantomData<*mut ()>,
91
92    #[inspect(flatten)]
93    inner: &'a UhVpInner,
94    #[inspect(skip)]
95    partition: &'a UhPartitionInner,
96    #[inspect(skip)]
97    idle_control: Option<&'a mut IdleControl>,
98    #[inspect(skip)]
99    kernel_returns: u64,
100    #[inspect(hex, iter_by_index)]
101    crash_reg: [u64; hvdef::HV_X64_GUEST_CRASH_PARAMETER_MSRS],
102    vmtime: VmTimeAccess,
103    #[inspect(skip)]
104    timer: PollImpl<dyn PollTimer>,
105    #[inspect(mut)]
106    force_exit_sidecar: bool,
107    signaled_sidecar_exit: bool,
108    /// The VTLs on this VP that are currently locked, per requesting VTL.
109    vtls_tlb_locked: VtlsTlbLocked,
110    #[inspect(skip)]
111    shared: &'a T::Shared,
112    #[inspect(hex, with = "|x| inspect::iter_by_index(x.iter()).map_value(|a| a.0)")]
113    exit_activities: VtlArray<ExitActivity, 2>,
114
115    // Put the runner and backing at the end so that monomorphisms of functions
116    // that don't access backing-specific state are more likely to be folded
117    // together by the compiler.
118    #[inspect(skip)]
119    runner: ProcessorRunner<'a, T::HclBacking<'a>>,
120    #[inspect(mut)]
121    backing: T,
122}
123
124#[derive(Inspect)]
125struct VtlsTlbLocked {
126    // vtl0: VtlArray<bool, 0>,
127    vtl1: VtlArray<bool, 1>,
128    vtl2: VtlArray<bool, 2>,
129}
130
131impl VtlsTlbLocked {
132    fn get(&self, requesting_vtl: Vtl, target_vtl: GuestVtl) -> bool {
133        match requesting_vtl {
134            Vtl::Vtl0 => unreachable!(),
135            Vtl::Vtl1 => self.vtl1[target_vtl],
136            Vtl::Vtl2 => self.vtl2[target_vtl],
137        }
138    }
139
140    fn set(&mut self, requesting_vtl: Vtl, target_vtl: GuestVtl, value: bool) {
141        match requesting_vtl {
142            Vtl::Vtl0 => unreachable!(),
143            Vtl::Vtl1 => self.vtl1[target_vtl] = value,
144            Vtl::Vtl2 => self.vtl2[target_vtl] = value,
145        }
146    }
147
148    fn fill(&mut self, requesting_vtl: Vtl, value: bool) {
149        match requesting_vtl {
150            Vtl::Vtl0 => unreachable!(),
151            Vtl::Vtl1 => self.vtl1.fill(value),
152            Vtl::Vtl2 => self.vtl2.fill(value),
153        }
154    }
155}
156
157#[cfg(guest_arch = "x86_64")]
158#[derive(Inspect)]
159pub(crate) struct LapicState {
160    lapic: LocalApic,
161    activity: MpState,
162    nmi_pending: bool,
163}
164
165#[cfg(guest_arch = "x86_64")]
166impl LapicState {
167    pub fn new(lapic: LocalApic, activity: MpState) -> Self {
168        Self {
169            lapic,
170            activity,
171            nmi_pending: false,
172        }
173    }
174}
175
176struct BackingParams<'a, 'b, T: Backing> {
177    partition: &'a UhPartitionInner,
178    vp_info: &'a TargetVpInfo,
179    runner: &'a mut ProcessorRunner<'b, T::HclBacking<'b>>,
180}
181
182mod private {
183    use super::BackingParams;
184    use super::vp_state;
185    use crate::BackingShared;
186    use crate::Error;
187    use crate::GuestVtl;
188    use crate::processor::UhProcessor;
189    use hv1_emulator::hv::ProcessorVtlHv;
190    use hv1_structs::VtlArray;
191    use inspect::InspectMut;
192    use std::future::Future;
193    use virt::StopVp;
194    use virt::VpHaltReason;
195    use virt::io::CpuIo;
196    use virt::vp::AccessVpState;
197
198    #[expect(private_interfaces)]
199    pub trait BackingPrivate: 'static + Sized + InspectMut + Sized {
200        type HclBacking<'b>: hcl::ioctl::Backing<'b>;
201        type EmulationCache;
202        type Shared;
203
204        fn shared(shared: &BackingShared) -> &Self::Shared;
205
206        fn new(params: BackingParams<'_, '_, Self>, shared: &Self::Shared) -> Result<Self, Error>;
207
208        type StateAccess<'p, 'a>: AccessVpState<Error = vp_state::Error>
209        where
210            Self: 'a + 'p,
211            'p: 'a;
212
213        fn init(this: &mut UhProcessor<'_, Self>);
214
215        fn access_vp_state<'a, 'p>(
216            this: &'a mut UhProcessor<'p, Self>,
217            vtl: GuestVtl,
218        ) -> Self::StateAccess<'p, 'a>;
219
220        /// Called before the first `run_vp` call to allow the backing to
221        /// perform any pre-run tasks. Must be idempotent--may be called again
222        /// before some subsequent `run_vp` calls.
223        fn pre_run_vp(_this: &mut UhProcessor<'_, Self>) {}
224
225        fn run_vp(
226            this: &mut UhProcessor<'_, Self>,
227            dev: &impl CpuIo,
228            stop: &mut StopVp<'_>,
229        ) -> impl Future<Output = Result<(), VpHaltReason>>;
230
231        /// Process any pending interrupts. Returns true if reprocessing is required.
232        fn process_interrupts(
233            this: &mut UhProcessor<'_, Self>,
234            scan_irr: VtlArray<bool, 2>,
235            first_scan_irr: &mut bool,
236            dev: &impl CpuIo,
237        ) -> bool;
238
239        /// Process any pending APIC work.
240        fn poll_apic(this: &mut UhProcessor<'_, Self>, vtl: GuestVtl, scan_irr: bool);
241
242        /// Requests the VP to exit when an external interrupt is ready to be
243        /// delivered.
244        ///
245        /// Only used when the hypervisor implements the APIC.
246        fn request_extint_readiness(this: &mut UhProcessor<'_, Self>);
247
248        /// Requests the VP to exit when any of the specified SINTs have a free
249        /// message slot.
250        ///
251        /// This is used for hypervisor-managed and untrusted SINTs.
252        fn request_untrusted_sint_readiness(this: &mut UhProcessor<'_, Self>, sints: u16);
253
254        fn handle_vp_start_enable_vtl_wake(_this: &mut UhProcessor<'_, Self>, _vtl: GuestVtl);
255
256        fn inspect_extra(_this: &mut UhProcessor<'_, Self>, _resp: &mut inspect::Response<'_>) {}
257
258        fn hv(&self, vtl: GuestVtl) -> Option<&ProcessorVtlHv>;
259        fn hv_mut(&mut self, vtl: GuestVtl) -> Option<&mut ProcessorVtlHv>;
260
261        fn vtl1_inspectable(this: &UhProcessor<'_, Self>) -> bool;
262    }
263}
264
265/// Processor backing.
266pub trait Backing: BackingPrivate {}
267
268impl<T: BackingPrivate> Backing for T {}
269
270#[cfg_attr(not(guest_arch = "x86_64"), expect(dead_code))]
271pub(crate) struct BackingSharedParams<'a> {
272    pub cvm_state: Option<crate::UhCvmPartitionState>,
273    #[cfg(guest_arch = "x86_64")]
274    pub cpuid: &'a virt::CpuidLeafSet,
275    pub hcl: &'a Hcl,
276    pub guest_vsm_available: bool,
277}
278
279/// Supported intercept message types.
280#[cfg_attr(guest_arch = "aarch64", expect(dead_code))]
281enum InterceptMessageType {
282    #[cfg(guest_arch = "x86_64")]
283    Register {
284        reg: HvX64RegisterName,
285        value: u64,
286    },
287    Msr {
288        msr: u32,
289    },
290    #[cfg(guest_arch = "x86_64")]
291    IoPort {
292        port_number: u16,
293        access_size: u8,
294        string_access: bool,
295        rep_access: bool,
296    },
297}
298
299/// Per-arch state required to generate an intercept message.
300#[cfg_attr(guest_arch = "aarch64", expect(dead_code))]
301pub(crate) struct InterceptMessageState {
302    instruction_length_and_cr8: u8,
303    cpl: u8,
304    efer_lma: bool,
305    cs: hvdef::HvX64SegmentRegister,
306    rip: u64,
307    rflags: u64,
308    rax: u64,
309    rdx: u64,
310    rcx: u64,
311    rsi: u64,
312    rdi: u64,
313    optional: Option<InterceptMessageOptionalState>,
314}
315
316#[cfg_attr(guest_arch = "aarch64", expect(dead_code))]
317/// Additional per-arch state required to generate an intercept message. Used
318/// for state that is not common across the intercept message types and that
319/// might be slower to retrieve on certain architectures.
320struct InterceptMessageOptionalState {
321    ds: hvdef::HvX64SegmentRegister,
322    es: hvdef::HvX64SegmentRegister,
323}
324
325impl InterceptMessageType {
326    #[cfg(guest_arch = "x86_64")]
327    fn generate_hv_message(
328        &self,
329        vp_index: VpIndex,
330        vtl: GuestVtl,
331        state: InterceptMessageState,
332        is_read: bool,
333    ) -> HvMessage {
334        let header = hvdef::HvX64InterceptMessageHeader {
335            vp_index: vp_index.index(),
336            instruction_length_and_cr8: state.instruction_length_and_cr8,
337            intercept_access_type: if is_read {
338                hvdef::HvInterceptAccessType::READ
339            } else {
340                hvdef::HvInterceptAccessType::WRITE
341            },
342            execution_state: hvdef::HvX64VpExecutionState::new()
343                .with_cpl(state.cpl)
344                .with_vtl(vtl.into())
345                .with_efer_lma(state.efer_lma),
346            cs_segment: state.cs,
347            rip: state.rip,
348            rflags: state.rflags,
349        };
350        match self {
351            InterceptMessageType::Register { reg, value } => {
352                let intercept_message = hvdef::HvX64RegisterInterceptMessage {
353                    header,
354                    flags: hvdef::HvX64RegisterInterceptMessageFlags::new(),
355                    rsvd: 0,
356                    rsvd2: 0,
357                    register_name: *reg,
358                    access_info: hvdef::HvX64RegisterAccessInfo::new_source_value(
359                        hvdef::HvRegisterValue::from(*value),
360                    ),
361                };
362                HvMessage::new(
363                    hvdef::HvMessageType::HvMessageTypeRegisterIntercept,
364                    0,
365                    intercept_message.as_bytes(),
366                )
367            }
368            InterceptMessageType::Msr { msr } => {
369                let intercept_message = hvdef::HvX64MsrInterceptMessage {
370                    header,
371                    msr_number: *msr,
372                    rax: state.rax,
373                    rdx: state.rdx,
374                    reserved: 0,
375                };
376
377                HvMessage::new(
378                    hvdef::HvMessageType::HvMessageTypeMsrIntercept,
379                    0,
380                    intercept_message.as_bytes(),
381                )
382            }
383            InterceptMessageType::IoPort {
384                port_number,
385                access_size,
386                string_access,
387                rep_access,
388            } => {
389                let access_info =
390                    hvdef::HvX64IoPortAccessInfo::new(*access_size, *string_access, *rep_access);
391                let intercept_message = hvdef::HvX64IoPortInterceptMessage {
392                    header,
393                    port_number: *port_number,
394                    access_info,
395                    instruction_byte_count: 0,
396                    reserved: 0,
397                    rax: state.rax,
398                    instruction_bytes: [0u8; 16],
399                    ds_segment: state.optional.as_ref().unwrap().ds,
400                    es_segment: state.optional.as_ref().unwrap().es,
401                    rcx: state.rcx,
402                    rsi: state.rsi,
403                    rdi: state.rdi,
404                };
405
406                HvMessage::new(
407                    hvdef::HvMessageType::HvMessageTypeX64IoPortIntercept,
408                    0,
409                    intercept_message.as_bytes(),
410                )
411            }
412        }
413    }
414}
415
416/// Trait for processor backings that have hardware isolation support.
417#[cfg(guest_arch = "x86_64")]
418pub(crate) trait HardwareIsolatedBacking: Backing {
419    /// Gets CVM specific VP state.
420    fn cvm_state(&self) -> &crate::UhCvmVpState;
421    /// Gets CVM specific VP state.
422    fn cvm_state_mut(&mut self) -> &mut crate::UhCvmVpState;
423    /// Gets CVM specific partition state.
424    fn cvm_partition_state(shared: &Self::Shared) -> &crate::UhCvmPartitionState;
425    /// Gets a struct that can be used to interact with TLB flushing and
426    /// locking.
427    ///
428    /// If a `vp_index` is provided, it will be used to cause the specified VP
429    /// to wait for all TLB locks to be released before returning to a lower VTL.
430    //
431    // FUTURE: This probably shouldn't be optional, but right now MNF doesn't know
432    // what VP it's set from and probably doesn't need it?
433    fn tlb_flush_lock_access<'a>(
434        vp_index: Option<VpIndex>,
435        partition: &'a UhPartitionInner,
436        shared: &'a Self::Shared,
437    ) -> impl TlbFlushLockAccess + 'a;
438    /// Copies shared registers (per VSM TLFS spec) from the source VTL to
439    /// the target VTL that will become active, and set the exit vtl
440    fn switch_vtl(this: &mut UhProcessor<'_, Self>, source_vtl: GuestVtl, target_vtl: GuestVtl);
441    /// Gets registers needed for gva to gpa translation
442    fn translation_registers(
443        &self,
444        this: &UhProcessor<'_, Self>,
445        vtl: GuestVtl,
446    ) -> TranslationRegisters;
447    /// Vector of the event that is pending injection into the guest state, if
448    /// valid.
449    fn pending_event_vector(this: &UhProcessor<'_, Self>, vtl: GuestVtl) -> Option<u8>;
450    /// Check if an interrupt of appropriate priority, or an NMI, is pending for
451    /// the given VTL. `check_rflags` specifies whether RFLAGS.IF should be checked.
452    fn is_interrupt_pending(
453        this: &mut UhProcessor<'_, Self>,
454        vtl: GuestVtl,
455        check_rflags: bool,
456        dev: &impl CpuIo,
457    ) -> bool;
458    /// Sets the pending exception for the guest state.
459    ///
460    /// Note that this will overwrite any existing pending exception. It will
461    /// not handle merging any existing pending exception with the new one.
462    fn set_pending_exception(
463        this: &mut UhProcessor<'_, Self>,
464        vtl: GuestVtl,
465        event: hvdef::HvX64PendingExceptionEvent,
466    );
467
468    fn intercept_message_state(
469        this: &UhProcessor<'_, Self>,
470        vtl: GuestVtl,
471        include_optional_state: bool,
472    ) -> InterceptMessageState;
473
474    /// Individual register for CPUID and crx intercept handling, since
475    /// AccessVpState::registers is relatively slow on TDX.
476    fn cr0(this: &UhProcessor<'_, Self>, vtl: GuestVtl) -> u64;
477    fn cr4(this: &UhProcessor<'_, Self>, vtl: GuestVtl) -> u64;
478
479    fn cr_intercept_registration(
480        this: &mut UhProcessor<'_, Self>,
481        intercept_control: HvRegisterCrInterceptControl,
482    );
483
484    fn untrusted_synic_mut(&mut self) -> Option<&mut ProcessorSynic>;
485}
486
487#[cfg_attr(guest_arch = "aarch64", expect(dead_code))]
488#[derive(Inspect, Debug)]
489#[inspect(tag = "reason")]
490pub(crate) enum SidecarExitReason {
491    #[inspect(transparent)]
492    Exit(SidecarRemoveExit),
493    #[inspect(transparent)]
494    TaskRequest(Arc<str>),
495    ManualRequest,
496}
497
498#[cfg_attr(guest_arch = "aarch64", expect(dead_code))]
499#[derive(Inspect, Debug)]
500#[inspect(tag = "exit")]
501pub(crate) enum SidecarRemoveExit {
502    Msr {
503        #[inspect(hex)]
504        msr: u32,
505        value: Option<u64>,
506    },
507    Io {
508        #[inspect(hex)]
509        port: u16,
510        write: bool,
511    },
512    Mmio {
513        #[inspect(hex)]
514        gpa: u64,
515        write: bool,
516    },
517    Hypercall {
518        #[inspect(debug)]
519        code: hvdef::HypercallCode,
520    },
521    Cpuid {
522        #[inspect(hex)]
523        leaf: u32,
524        #[inspect(hex)]
525        subleaf: u32,
526    },
527    Hypervisor {
528        #[inspect(debug)]
529        message: hvdef::HvMessageType,
530    },
531}
532
533impl UhVpInner {
534    // Create a new vp's state.
535    pub fn new(cpu_index: u32, vp_info: TargetVpInfo) -> Self {
536        Self {
537            wake_reasons: Default::default(),
538            message_queues: VtlArray::from_fn(|_| MessageQueues::new()),
539            waker: Default::default(),
540            cpu_index,
541            vp_info,
542            sidecar_exit_reason: Default::default(),
543        }
544    }
545
546    /// Queues a message for sending, optionally alerting the hypervisor if the queue is empty.
547    pub fn post_message(&self, vtl: GuestVtl, sint: u8, message: &HvMessage) {
548        if self.message_queues[vtl].enqueue_message(sint, message) {
549            self.wake(vtl, WakeReason::MESSAGE_QUEUES);
550        }
551    }
552
553    pub fn wake(&self, vtl: GuestVtl, reason: WakeReason) {
554        let reason = u64::from(reason.0) << (vtl as u8 * 32);
555        if self.wake_reasons.fetch_or(reason, Ordering::Release) & reason == 0 {
556            if let Some(waker) = &*self.waker.read() {
557                waker.wake_by_ref();
558            }
559        }
560    }
561
562    pub fn wake_vtl2(&self) {
563        if let Some(waker) = &*self.waker.read() {
564            waker.wake_by_ref();
565        }
566    }
567
568    pub fn set_sidecar_exit_reason(&self, reason: SidecarExitReason) {
569        self.sidecar_exit_reason.lock().get_or_insert_with(|| {
570            tracing::info!(CVM_ALLOWED, "sidecar exit");
571            tracing::info!(CVM_CONFIDENTIAL, ?reason, "sidecar exit");
572            reason
573        });
574    }
575}
576
577impl<T: Backing> UhProcessor<'_, T> {
578    fn inspect_extra(&mut self, resp: &mut inspect::Response<'_>) {
579        resp.child("stats", |req| {
580            // Get all the VP stats and just grab this VP's.
581            if let Ok(stats) = hcl::stats::vp_stats() {
582                let stats = &stats[self.vp_index().index() as usize];
583                req.respond()
584                    .counter("vtl_transitions", stats.vtl_transitions)
585                    .counter(
586                        "spurious_exits",
587                        stats.vtl_transitions.saturating_sub(self.kernel_returns),
588                    );
589            }
590        })
591        .field(
592            "last_enter_modes",
593            self.runner
594                .enter_mode()
595                .map(|&mut v| inspect::AsHex(u8::from(v))),
596        )
597        .field("sidecar", self.runner.is_sidecar())
598        .field(
599            "sidecar_base_cpu",
600            self.partition.hcl.sidecar_base_cpu(self.vp_index().index()),
601        );
602
603        T::inspect_extra(self, resp);
604    }
605
606    #[cfg(guest_arch = "x86_64")]
607    fn handle_debug_exception(&mut self, vtl: GuestVtl) -> Result<(), VpHaltReason> {
608        // FUTURE: Underhill does not yet support VTL1 so this is only tested with VTL0.
609        if vtl == GuestVtl::Vtl0 {
610            let debug_regs: virt::x86::vp::DebugRegisters = self
611                .access_state(Vtl::Vtl0)
612                .debug_regs()
613                .expect("register query should not fail");
614
615            let dr = [
616                debug_regs.dr0,
617                debug_regs.dr1,
618                debug_regs.dr2,
619                debug_regs.dr3,
620            ];
621
622            if debug_regs.dr6 & x86defs::DR6_SINGLE_STEP != 0 {
623                return Err(VpHaltReason::SingleStep);
624            }
625
626            // Last four bits of DR6 indicate which breakpoint was triggered.
627            const BREAKPOINT_INDEX_OFFSET: usize = 4;
628            let i = debug_regs.dr6.trailing_zeros() as usize;
629            if i >= BREAKPOINT_INDEX_OFFSET {
630                // Received a debug exception not triggered by a breakpoint or single step.
631                return Err(VpHaltReason::InvalidVmState(
632                    UnexpectedDebugException {
633                        dr6: debug_regs.dr6,
634                    }
635                    .into(),
636                ));
637            }
638            let bp = virt::x86::HardwareBreakpoint::from_dr7(debug_regs.dr7, dr[i], i);
639
640            return Err(VpHaltReason::HwBreak(bp));
641        }
642
643        panic!("unexpected debug exception in VTL {:?}", vtl);
644    }
645}
646
647#[cfg(guest_arch = "x86_64")]
648#[derive(Debug, Error)]
649#[error("unexpected debug exception with dr6 value {dr6:#x}")]
650struct UnexpectedDebugException {
651    dr6: u64,
652}
653
654impl<'p, T: Backing> Processor for UhProcessor<'p, T> {
655    type StateAccess<'a>
656        = T::StateAccess<'p, 'a>
657    where
658        Self: 'a;
659
660    #[cfg(guest_arch = "aarch64")]
661    fn set_debug_state(
662        &mut self,
663        _vtl: Vtl,
664        _state: Option<&virt::x86::DebugState>,
665    ) -> Result<(), <T::StateAccess<'p, '_> as virt::vp::AccessVpState>::Error> {
666        unimplemented!()
667    }
668
669    #[cfg(guest_arch = "x86_64")]
670    fn set_debug_state(
671        &mut self,
672        vtl: Vtl,
673        state: Option<&virt::x86::DebugState>,
674    ) -> Result<(), <T::StateAccess<'p, '_> as AccessVpState>::Error> {
675        // FUTURE: Underhill does not yet support VTL1 so this is only tested with VTL0.
676        if vtl == Vtl::Vtl0 {
677            let mut db: [u64; 4] = [0; 4];
678            let mut access_state = self.access_state(vtl);
679            let mut registers = access_state.registers()?;
680            let mut rflags = x86defs::RFlags::from(registers.rflags);
681            let mut dr7: u64 = 0;
682
683            if let Some(state) = state {
684                rflags.set_trap(state.single_step);
685                for (i, bp) in state.breakpoints.iter().enumerate() {
686                    if let Some(bp) = bp {
687                        db[i] = bp.address;
688                        dr7 |= bp.dr7_bits(i);
689                    }
690                }
691            }
692
693            let debug_registers = virt::x86::vp::DebugRegisters {
694                dr0: db[0],
695                dr1: db[1],
696                dr2: db[2],
697                dr3: db[3],
698                dr6: 0,
699                dr7,
700            };
701            access_state.set_debug_regs(&debug_registers)?;
702
703            registers.rflags = rflags.into();
704            access_state.set_registers(&registers)?;
705            return Ok(());
706        }
707
708        panic!("unexpected set debug state in VTL {:?}", vtl);
709    }
710
711    async fn run_vp(
712        &mut self,
713        mut stop: StopVp<'_>,
714        dev: &impl CpuIo,
715    ) -> Result<Infallible, VpHaltReason> {
716        T::pre_run_vp(self);
717
718        if self.runner.is_sidecar() {
719            if self.force_exit_sidecar && !self.signaled_sidecar_exit {
720                self.inner
721                    .set_sidecar_exit_reason(SidecarExitReason::ManualRequest);
722                self.signaled_sidecar_exit = true;
723                return Err(VpHaltReason::Cancel);
724            }
725        } else {
726            let mut current = Default::default();
727            affinity::get_current_thread_affinity(&mut current).unwrap();
728            assert_eq!(&current, CpuSet::new().set(self.inner.cpu_index));
729
730            // Lower the priority of this VP thread so that the VM does not return
731            // to VTL0 while there is still outstanding VTL2 work to do.
732            nice::nice(1);
733        }
734
735        let mut last_waker = None;
736
737        // Force deliverability notifications to be reevaluated.
738        let vtl0_wakes = WakeReason::new()
739            .with_message_queues(true)
740            .with_intcon(true);
741        let vtl1_wakes = WakeReason::new().with_message_queues(true);
742        self.inner.wake_reasons.fetch_or(
743            ((vtl1_wakes.0 as u64) << 32) | (vtl0_wakes.0 as u64),
744            Ordering::Relaxed,
745        );
746
747        let mut first_scan_irr = true;
748
749        loop {
750            // Process VP activity and wait for the VP to be ready.
751            poll_fn(|cx| {
752                loop {
753                    stop.check()?;
754
755                    // Clear the run VP cancel request.
756                    self.runner.clear_cancel();
757
758                    // Cancel any pending timer.
759                    self.vmtime.cancel_timeout();
760
761                    // Ensure the waker is set.
762                    if !last_waker
763                        .as_ref()
764                        .is_some_and(|waker| cx.waker().will_wake(waker))
765                    {
766                        last_waker = Some(cx.waker().clone());
767                        self.inner.waker.write().clone_from(&last_waker);
768                    }
769
770                    // Process wakes.
771                    let scan_irr = if self.inner.wake_reasons.load(Ordering::Relaxed) != 0 {
772                        self.handle_wake()
773                    } else {
774                        [false, false].into()
775                    };
776
777                    if T::process_interrupts(self, scan_irr, &mut first_scan_irr, dev) {
778                        continue;
779                    }
780
781                    // Arm the timer.
782                    if let Some(timeout) = self.vmtime.get_timeout() {
783                        let deadline = self.vmtime.host_time(timeout);
784                        if self.timer.poll_timer(cx, deadline).is_ready() {
785                            continue;
786                        }
787                    }
788
789                    return <Result<_, VpHaltReason>>::Ok(()).into();
790                }
791            })
792            .await?;
793
794            // Yield if the thread pool is not ready to block.
795            if let Some(idle_control) = &mut self.idle_control {
796                if !idle_control.pre_block() {
797                    yield_now().await;
798                    continue;
799                }
800            }
801
802            if let Some(mode) = self.runner.enter_mode() {
803                *mode = self
804                    .partition
805                    .enter_modes_atomic
806                    .load(Ordering::Relaxed)
807                    .into();
808            }
809
810            // Quiesce RCU before running the VP to avoid having to synchronize with
811            // this CPU during memory protection updates.
812            minircu::global().quiesce();
813
814            T::run_vp(self, dev, &mut stop).await?;
815            self.kernel_returns += 1;
816        }
817    }
818
819    fn flush_async_requests(&mut self) {
820        if self.inner.wake_reasons.load(Ordering::Relaxed) != 0 {
821            let scan_irr = self.handle_wake();
822            for vtl in [GuestVtl::Vtl1, GuestVtl::Vtl0] {
823                if scan_irr[vtl] {
824                    T::poll_apic(self, vtl, true);
825                }
826            }
827        }
828        self.runner.flush_deferred_state();
829    }
830
831    fn access_state(&mut self, vtl: Vtl) -> Self::StateAccess<'_> {
832        T::access_vp_state(self, vtl.try_into().unwrap())
833    }
834
835    fn vtl_inspectable(&self, vtl: Vtl) -> bool {
836        match vtl {
837            Vtl::Vtl0 => true,
838            Vtl::Vtl1 => T::vtl1_inspectable(self),
839            Vtl::Vtl2 => false,
840        }
841    }
842}
843
844impl<'a, T: Backing> UhProcessor<'a, T> {
845    pub(super) fn new(
846        driver: &impl Driver,
847        partition: &'a UhPartitionInner,
848        vp_info: TargetVpInfo,
849        idle_control: Option<&'a mut IdleControl>,
850    ) -> Result<Self, Error> {
851        let inner = partition.vp(vp_info.base.vp_index).unwrap();
852        let mut runner = partition
853            .hcl
854            .runner(inner.vp_index().index(), idle_control.is_none())
855            .unwrap();
856
857        let backing_shared = T::shared(&partition.backing_shared);
858
859        let backing = T::new(
860            BackingParams {
861                partition,
862                vp_info: &vp_info,
863                runner: &mut runner,
864            },
865            backing_shared,
866        )?;
867
868        let mut vp = Self {
869            partition,
870            inner,
871            runner,
872            idle_control,
873            kernel_returns: 0,
874            crash_reg: [0; hvdef::HV_X64_GUEST_CRASH_PARAMETER_MSRS],
875            _not_send: PhantomData,
876            backing,
877            shared: backing_shared,
878            vmtime: partition
879                .vmtime
880                .access(format!("vp-{}", vp_info.base.vp_index.index())),
881            timer: driver.new_dyn_timer(),
882            force_exit_sidecar: false,
883            signaled_sidecar_exit: false,
884            vtls_tlb_locked: VtlsTlbLocked {
885                vtl1: VtlArray::new(false),
886                vtl2: VtlArray::new(false),
887            },
888            exit_activities: Default::default(),
889        };
890
891        T::init(&mut vp);
892
893        Ok(vp)
894    }
895
896    /// Returns true if the interrupt controller has work to do.
897    fn handle_wake(&mut self) -> VtlArray<bool, 2> {
898        let wake_reasons_raw = self.inner.wake_reasons.swap(0, Ordering::SeqCst);
899        let wake_reasons_vtl: [WakeReason; 2] = zerocopy::transmute!(wake_reasons_raw);
900        for (vtl, wake_reasons) in [
901            (GuestVtl::Vtl1, wake_reasons_vtl[1]),
902            (GuestVtl::Vtl0, wake_reasons_vtl[0]),
903        ] {
904            if wake_reasons.message_queues() {
905                let pending_sints = self.inner.message_queues[vtl].pending_sints();
906                if pending_sints != 0 {
907                    // Set SINT interest.
908                    let pending_sints = self.inner.message_queues[vtl].pending_sints();
909                    let mut masked_sints = 0;
910
911                    // Determine which of the pending sints are masked.
912                    for sint in 0..NUM_SINTS as u8 {
913                        if pending_sints & (1 << sint) == 0 {
914                            continue;
915                        }
916                        let sint_msr = if let Some(hv) = self.backing.hv(vtl).as_ref() {
917                            hv.synic.sint(sint)
918                        } else {
919                            #[cfg(guest_arch = "x86_64")]
920                            let sint_reg =
921                                HvX64RegisterName(HvX64RegisterName::Sint0.0 + sint as u32);
922                            #[cfg(guest_arch = "aarch64")]
923                            let sint_reg =
924                                HvArm64RegisterName(HvArm64RegisterName::Sint0.0 + sint as u32);
925                            self.runner.get_vp_register(vtl, sint_reg).unwrap().as_u64()
926                        };
927                        masked_sints |= (HvSynicSint::from(sint_msr).masked() as u16) << sint;
928                    }
929
930                    // Drain the queues for all masked SINTs.
931                    self.inner.message_queues[vtl].post_pending_messages(masked_sints, |_, _| {
932                        Err(HvError::InvalidSynicState)
933                    });
934
935                    self.request_sint_notifications(vtl, pending_sints & !masked_sints);
936                }
937            }
938
939            if wake_reasons.extint() {
940                T::request_extint_readiness(self);
941            }
942
943            #[cfg(guest_arch = "x86_64")]
944            if wake_reasons.hv_start_enable_vtl_vp() {
945                T::handle_vp_start_enable_vtl_wake(self, vtl);
946            }
947
948            #[cfg(guest_arch = "x86_64")]
949            if wake_reasons.update_proxy_irr_filter() {
950                // update `proxy_irr_blocked` filter
951                debug_assert!(self.partition.isolation.is_hardware_isolated());
952                self.update_proxy_irr_filter(vtl);
953            }
954        }
955
956        wake_reasons_vtl.map(|w| w.intcon()).into()
957    }
958
959    fn request_sint_notifications(&mut self, vtl: GuestVtl, sints: u16) {
960        if sints == 0 {
961            return;
962        }
963
964        // Send the SINT notifications to the local synic for non-proxied SINTs.
965        let untrusted_sints = if let Some(hv) = self.backing.hv_mut(vtl).as_mut() {
966            let proxied_sints = hv.synic.proxied_sints();
967            hv.synic.request_sint_readiness(sints & !proxied_sints);
968            proxied_sints
969        } else {
970            !0
971        };
972
973        if sints & untrusted_sints != 0 {
974            assert_eq!(vtl, GuestVtl::Vtl0);
975            T::request_untrusted_sint_readiness(self, sints & untrusted_sints);
976        }
977    }
978
979    fn vp_index(&self) -> VpIndex {
980        self.inner.vp_index()
981    }
982
983    #[cfg(guest_arch = "x86_64")]
984    fn write_crash_msr(&mut self, msr: u32, value: u64, vtl: GuestVtl) -> Result<(), MsrError> {
985        match msr {
986            hvdef::HV_X64_MSR_GUEST_CRASH_CTL => {
987                let crash = VtlCrash {
988                    vp_index: self.vp_index(),
989                    last_vtl: vtl,
990                    control: hvdef::GuestCrashCtl::from(value),
991                    parameters: self.crash_reg,
992                };
993                tracelimit::warn_ratelimited!(
994                    CVM_ALLOWED,
995                    ?crash,
996                    "Guest has reported system crash"
997                );
998
999                if crash.control.crash_message() {
1000                    let message_gpa = crash.parameters[3];
1001                    let message_size = std::cmp::min(crash.parameters[4], hvdef::HV_PAGE_SIZE);
1002                    let mut message = vec![0; message_size as usize];
1003                    match self.partition.gm[vtl].read_at(message_gpa, &mut message) {
1004                        Ok(()) => {
1005                            let message = String::from_utf8_lossy(&message).into_owned();
1006                            tracelimit::warn_ratelimited!(
1007                                CVM_CONFIDENTIAL,
1008                                message,
1009                                "Guest has reported a system crash message"
1010                            );
1011                        }
1012                        Err(e) => {
1013                            tracelimit::warn_ratelimited!(
1014                                CVM_ALLOWED,
1015                                ?e,
1016                                "Failed to read crash message"
1017                            );
1018                        }
1019                    }
1020                }
1021
1022                self.partition.crash_notification_send.send(crash);
1023            }
1024            hvdef::HV_X64_MSR_GUEST_CRASH_P0
1025            | hvdef::HV_X64_MSR_GUEST_CRASH_P1
1026            | hvdef::HV_X64_MSR_GUEST_CRASH_P2
1027            | hvdef::HV_X64_MSR_GUEST_CRASH_P3
1028            | hvdef::HV_X64_MSR_GUEST_CRASH_P4 => {
1029                self.crash_reg[(msr - hvdef::HV_X64_MSR_GUEST_CRASH_P0) as usize] = value;
1030            }
1031            _ => return Err(MsrError::Unknown),
1032        }
1033        Ok(())
1034    }
1035
1036    #[cfg(guest_arch = "x86_64")]
1037    fn read_crash_msr(&self, msr: u32, _vtl: GuestVtl) -> Result<u64, MsrError> {
1038        let v = match msr {
1039            // Reads of CRASH_CTL report our supported capabilities, not the
1040            // current value.
1041            hvdef::HV_X64_MSR_GUEST_CRASH_CTL => hvdef::GuestCrashCtl::new()
1042                .with_crash_notify(true)
1043                .with_crash_message(true)
1044                .into(),
1045            hvdef::HV_X64_MSR_GUEST_CRASH_P0 => self.crash_reg[0],
1046            hvdef::HV_X64_MSR_GUEST_CRASH_P1 => self.crash_reg[1],
1047            hvdef::HV_X64_MSR_GUEST_CRASH_P2 => self.crash_reg[2],
1048            hvdef::HV_X64_MSR_GUEST_CRASH_P3 => self.crash_reg[3],
1049            hvdef::HV_X64_MSR_GUEST_CRASH_P4 => self.crash_reg[4],
1050            _ => return Err(MsrError::Unknown),
1051        };
1052        Ok(v)
1053    }
1054
1055    /// Emulates an instruction due to a memory access exit.
1056    #[cfg(guest_arch = "x86_64")]
1057    async fn emulate<D: CpuIo>(
1058        &mut self,
1059        devices: &D,
1060        interruption_pending: bool,
1061        vtl: GuestVtl,
1062        cache: T::EmulationCache,
1063    ) -> Result<(), VpHaltReason>
1064    where
1065        for<'b> UhEmulationState<'b, 'a, D, T>: virt_support_x86emu::emulate::EmulatorSupport,
1066    {
1067        let guest_memory = &self.partition.gm[vtl];
1068        let (kx_guest_memory, ux_guest_memory) = match vtl {
1069            GuestVtl::Vtl0 => (
1070                &self.partition.vtl0_kernel_exec_gm,
1071                &self.partition.vtl0_user_exec_gm,
1072            ),
1073            GuestVtl::Vtl1 => (guest_memory, guest_memory),
1074        };
1075        let emu_mem = virt_support_x86emu::emulate::EmulatorMemoryAccess {
1076            gm: guest_memory,
1077            kx_gm: kx_guest_memory,
1078            ux_gm: ux_guest_memory,
1079        };
1080        let mut emulation_state = UhEmulationState {
1081            vp: &mut *self,
1082            interruption_pending,
1083            devices,
1084            vtl,
1085            cache,
1086        };
1087
1088        virt_support_x86emu::emulate::emulate(&mut emulation_state, &emu_mem, devices).await
1089    }
1090
1091    /// Emulates an instruction due to a memory access exit.
1092    #[cfg(guest_arch = "aarch64")]
1093    async fn emulate<D: CpuIo>(
1094        &mut self,
1095        devices: &D,
1096        intercept_state: &aarch64emu::InterceptState,
1097        vtl: GuestVtl,
1098        cache: T::EmulationCache,
1099    ) -> Result<(), VpHaltReason>
1100    where
1101        for<'b> UhEmulationState<'b, 'a, D, T>: virt_support_aarch64emu::emulate::EmulatorSupport,
1102    {
1103        let guest_memory = &self.partition.gm[vtl];
1104        virt_support_aarch64emu::emulate::emulate(
1105            &mut UhEmulationState {
1106                vp: &mut *self,
1107                interruption_pending: intercept_state.interruption_pending,
1108                devices,
1109                vtl,
1110                cache,
1111            },
1112            intercept_state,
1113            guest_memory,
1114            devices,
1115        )
1116        .await
1117    }
1118
1119    #[cfg(guest_arch = "x86_64")]
1120    fn update_proxy_irr_filter(&mut self, vtl: GuestVtl) {
1121        assert_eq!(vtl, GuestVtl::Vtl0);
1122        let mut irr_bits: BitArray<[u32; 8], Lsb0> = BitArray::new(Default::default());
1123
1124        // Get all not masked && proxy SINT vectors
1125        if let Some(hv) = self.backing.hv(vtl).as_ref() {
1126            for sint in 0..NUM_SINTS as u8 {
1127                let sint_msr = hv.synic.sint(sint);
1128                let hv_sint = HvSynicSint::from(sint_msr);
1129                // When vmbus relay is active, then SINT will not be proxied
1130                // in case of non-relay, guest will setup proxied sint
1131                if (hv_sint.proxy() || self.partition.vmbus_relay) && !hv_sint.masked() {
1132                    irr_bits.set(hv_sint.vector() as usize, true);
1133                }
1134            }
1135        }
1136
1137        // Get all device vectors
1138        self.partition.fill_device_vectors(vtl, &mut irr_bits);
1139
1140        // Update `proxy_irr_blocked` filter in run page
1141        self.runner
1142            .update_proxy_irr_filter_vtl0(&irr_bits.into_inner());
1143    }
1144}
1145
1146fn signal_mnf(dev: &impl CpuIo, connection_id: u32) {
1147    if let Err(err) = dev.signal_synic_event(Vtl::Vtl0, connection_id, 0) {
1148        tracelimit::warn_ratelimited!(
1149            CVM_ALLOWED,
1150            error = &err as &dyn std::error::Error,
1151            connection_id,
1152            "failed to signal mnf"
1153        );
1154    }
1155}
1156
1157/// Yields execution back to the executor.
1158async fn yield_now() {
1159    let mut yielded = false;
1160    poll_fn(|cx| {
1161        if !yielded {
1162            // Wake the waker so that this task gets to run again.
1163            cx.waker().wake_by_ref();
1164            yielded = true;
1165            Poll::Pending
1166        } else {
1167            Poll::Ready(())
1168        }
1169    })
1170    .await;
1171}
1172
1173struct UhEmulationState<'a, 'b, T: CpuIo, U: Backing> {
1174    vp: &'a mut UhProcessor<'b, U>,
1175    interruption_pending: bool,
1176    devices: &'a T,
1177    vtl: GuestVtl,
1178    cache: U::EmulationCache,
1179}
1180
1181impl<T: CpuIo, U: Backing> EmulatorMonitorSupport for UhEmulationState<'_, '_, T, U> {
1182    fn check_write(&self, gpa: u64, bytes: &[u8]) -> bool {
1183        self.vp
1184            .partition
1185            .monitor_page
1186            .check_write(gpa, bytes, |connection_id| {
1187                signal_mnf(self.devices, connection_id);
1188            })
1189    }
1190
1191    fn check_read(&self, gpa: u64, bytes: &mut [u8]) -> bool {
1192        self.vp.partition.monitor_page.check_read(gpa, bytes)
1193    }
1194}
1195
1196struct UhHypercallHandler<'a, 'b, T, B: Backing> {
1197    vp: &'a mut UhProcessor<'b, B>,
1198    bus: &'a T,
1199    /// Indicates if the handler is for trusted hypercalls in case hardware isolation is in use. A
1200    /// hypercall is trusted if it was made by the guest using a regular vmcall instruction, without
1201    /// using any host-visible mechanisms. An untrusted hypercall was intercepted from the
1202    /// hypervisor, such as one made by the guest using an isolated mechanism such as tdcall or
1203    /// GHCB.
1204    ///
1205    /// This should always be false if hardware isolation is not in use, as the distinction does
1206    /// not exist in that case.
1207    trusted: bool,
1208    intercepted_vtl: GuestVtl,
1209}
1210
1211impl<T, B: Backing> UhHypercallHandler<'_, '_, T, B> {
1212    fn target_vtl_no_higher(&self, target_vtl: Vtl) -> Result<GuestVtl, HvError> {
1213        if Vtl::from(self.intercepted_vtl) < target_vtl {
1214            return Err(HvError::AccessDenied);
1215        }
1216        Ok(target_vtl.try_into().unwrap())
1217    }
1218}
1219
1220impl<T, B: Backing> hv1_hypercall::GetVpIndexFromApicId for UhHypercallHandler<'_, '_, T, B> {
1221    fn get_vp_index_from_apic_id(
1222        &mut self,
1223        partition_id: u64,
1224        target_vtl: Vtl,
1225        apic_ids: &[u32],
1226        vp_indices: &mut [u32],
1227    ) -> HvRepResult {
1228        tracing::debug!(partition_id, ?target_vtl, "HvGetVpIndexFromApicId");
1229
1230        if partition_id != hvdef::HV_PARTITION_ID_SELF {
1231            return Err((HvError::InvalidPartitionId, 0));
1232        }
1233
1234        let _target_vtl = self.target_vtl_no_higher(target_vtl).map_err(|e| (e, 0))?;
1235
1236        #[cfg(guest_arch = "aarch64")]
1237        if true {
1238            let _ = apic_ids;
1239            let _ = vp_indices;
1240            todo!("AARCH64_TODO");
1241        }
1242
1243        #[cfg(guest_arch = "x86_64")]
1244        for (i, (&apic_id, vp_index)) in apic_ids.iter().zip(vp_indices).enumerate() {
1245            *vp_index = self
1246                .vp
1247                .partition
1248                .vps
1249                .iter()
1250                .find(|vp| vp.vp_info.apic_id == apic_id)
1251                .ok_or((HvError::InvalidParameter, i))?
1252                .vp_info
1253                .base
1254                .vp_index
1255                .index()
1256        }
1257
1258        Ok(())
1259    }
1260}
1261
1262#[cfg(guest_arch = "aarch64")]
1263impl<T: CpuIo, B: Backing> Arm64RegisterState for UhHypercallHandler<'_, '_, T, B> {
1264    fn pc(&mut self) -> u64 {
1265        self.vp
1266            .runner
1267            .get_vp_register(self.intercepted_vtl, HvArm64RegisterName::XPc)
1268            .expect("get vp register cannot fail")
1269            .as_u64()
1270    }
1271
1272    fn set_pc(&mut self, pc: u64) {
1273        self.vp
1274            .runner
1275            .set_vp_register(self.intercepted_vtl, HvArm64RegisterName::XPc, pc.into())
1276            .expect("set vp register cannot fail");
1277    }
1278
1279    fn x(&mut self, n: u8) -> u64 {
1280        self.vp
1281            .runner
1282            .get_vp_register(
1283                self.intercepted_vtl,
1284                HvArm64RegisterName(HvArm64RegisterName::X0.0 + n as u32),
1285            )
1286            .expect("get vp register cannot fail")
1287            .as_u64()
1288    }
1289
1290    fn set_x(&mut self, n: u8, v: u64) {
1291        self.vp
1292            .runner
1293            .set_vp_register(
1294                self.intercepted_vtl,
1295                HvArm64RegisterName(HvArm64RegisterName::X0.0 + n as u32),
1296                v.into(),
1297            )
1298            .expect("set vp register cannot fail")
1299    }
1300}
1301
1302impl<T: CpuIo, B: Backing> hv1_hypercall::PostMessage for UhHypercallHandler<'_, '_, T, B> {
1303    fn post_message(&mut self, connection_id: u32, message: &[u8]) -> hvdef::HvResult<()> {
1304        tracing::trace!(
1305            connection_id,
1306            self.trusted,
1307            "handling post message intercept"
1308        );
1309
1310        self.bus.post_synic_message(
1311            self.intercepted_vtl.into(),
1312            connection_id,
1313            self.trusted,
1314            message,
1315        )
1316    }
1317}
1318
1319impl<T: CpuIo, B: Backing> hv1_hypercall::SignalEvent for UhHypercallHandler<'_, '_, T, B> {
1320    fn signal_event(&mut self, connection_id: u32, flag: u16) -> hvdef::HvResult<()> {
1321        tracing::trace!(connection_id, "handling signal event intercept");
1322
1323        self.bus
1324            .signal_synic_event(self.intercepted_vtl.into(), connection_id, flag)
1325    }
1326}
1327
1328impl<T: CpuIo, B: Backing> UhHypercallHandler<'_, '_, T, B> {
1329    fn retarget_virtual_interrupt(
1330        &mut self,
1331        device_id: u64,
1332        address: u64,
1333        data: u32,
1334        vector: u32,
1335        multicast: bool,
1336        target_processors: ProcessorSet<'_>,
1337    ) -> hvdef::HvResult<()> {
1338        let target_processors = Vec::from_iter(target_processors);
1339        let vpci_params = vmcore::vpci_msi::VpciInterruptParameters {
1340            vector,
1341            multicast,
1342            target_processors: &target_processors,
1343        };
1344
1345        self.vp
1346            .partition
1347            .software_devices
1348            .as_ref()
1349            .expect("should exist if this intercept is registered or this is a CVM")
1350            .retarget_interrupt(device_id, address, data, &vpci_params)
1351    }
1352}
1353
1354impl<T, B: Backing> hv1_hypercall::ExtendedQueryCapabilities for UhHypercallHandler<'_, '_, T, B> {
1355    fn query_extended_capabilities(&mut self) -> hvdef::HvResult<u64> {
1356        // This capability is not actually supported. However Windows may unconditionally issue this
1357        // hypercall. Return InvalidHypercallCode as the error status. This is the same as not
1358        // implementing this at all, but has the advantage of not causing generating error messages.
1359        Err(HvError::InvalidHypercallCode)
1360    }
1361}