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(
608        &mut self,
609        dev: &impl CpuIo,
610        vtl: GuestVtl,
611    ) -> Result<(), VpHaltReason> {
612        // FUTURE: Underhill does not yet support VTL1 so this is only tested with VTL0.
613        if vtl == GuestVtl::Vtl0 {
614            let debug_regs: virt::x86::vp::DebugRegisters = self
615                .access_state(Vtl::Vtl0)
616                .debug_regs()
617                .expect("register query should not fail");
618
619            let dr = [
620                debug_regs.dr0,
621                debug_regs.dr1,
622                debug_regs.dr2,
623                debug_regs.dr3,
624            ];
625
626            if debug_regs.dr6 & x86defs::DR6_SINGLE_STEP != 0 {
627                return Err(VpHaltReason::SingleStep);
628            }
629
630            // Last four bits of DR6 indicate which breakpoint was triggered.
631            const BREAKPOINT_INDEX_OFFSET: usize = 4;
632            let i = debug_regs.dr6.trailing_zeros() as usize;
633            if i >= BREAKPOINT_INDEX_OFFSET {
634                // Received a debug exception not triggered by a breakpoint or single step.
635                return Err(dev.fatal_error(
636                    UnexpectedDebugException {
637                        dr6: debug_regs.dr6,
638                    }
639                    .into(),
640                ));
641            }
642            let bp = virt::x86::HardwareBreakpoint::from_dr7(debug_regs.dr7, dr[i], i);
643
644            return Err(VpHaltReason::HwBreak(bp));
645        }
646
647        panic!("unexpected debug exception in VTL {:?}", vtl);
648    }
649}
650
651#[cfg(guest_arch = "x86_64")]
652#[derive(Debug, Error)]
653#[error("unexpected debug exception with dr6 value {dr6:#x}")]
654struct UnexpectedDebugException {
655    dr6: u64,
656}
657
658impl<'p, T: Backing> Processor for UhProcessor<'p, T> {
659    type StateAccess<'a>
660        = T::StateAccess<'p, 'a>
661    where
662        Self: 'a;
663
664    #[cfg(guest_arch = "aarch64")]
665    fn set_debug_state(
666        &mut self,
667        _vtl: Vtl,
668        _state: Option<&virt::x86::DebugState>,
669    ) -> Result<(), <T::StateAccess<'p, '_> as virt::vp::AccessVpState>::Error> {
670        unimplemented!()
671    }
672
673    #[cfg(guest_arch = "x86_64")]
674    fn set_debug_state(
675        &mut self,
676        vtl: Vtl,
677        state: Option<&virt::x86::DebugState>,
678    ) -> Result<(), <T::StateAccess<'p, '_> as AccessVpState>::Error> {
679        // FUTURE: Underhill does not yet support VTL1 so this is only tested with VTL0.
680        if vtl == Vtl::Vtl0 {
681            let mut db: [u64; 4] = [0; 4];
682            let mut access_state = self.access_state(vtl);
683            let mut registers = access_state.registers()?;
684            let mut rflags = x86defs::RFlags::from(registers.rflags);
685            let mut dr7: u64 = 0;
686
687            if let Some(state) = state {
688                rflags.set_trap(state.single_step);
689                for (i, bp) in state.breakpoints.iter().enumerate() {
690                    if let Some(bp) = bp {
691                        db[i] = bp.address;
692                        dr7 |= bp.dr7_bits(i);
693                    }
694                }
695            }
696
697            let debug_registers = virt::x86::vp::DebugRegisters {
698                dr0: db[0],
699                dr1: db[1],
700                dr2: db[2],
701                dr3: db[3],
702                dr6: 0,
703                dr7,
704            };
705            access_state.set_debug_regs(&debug_registers)?;
706
707            registers.rflags = rflags.into();
708            access_state.set_registers(&registers)?;
709            return Ok(());
710        }
711
712        panic!("unexpected set debug state in VTL {:?}", vtl);
713    }
714
715    async fn run_vp(
716        &mut self,
717        mut stop: StopVp<'_>,
718        dev: &impl CpuIo,
719    ) -> Result<Infallible, VpHaltReason> {
720        T::pre_run_vp(self);
721
722        if self.runner.is_sidecar() {
723            if self.force_exit_sidecar && !self.signaled_sidecar_exit {
724                self.inner
725                    .set_sidecar_exit_reason(SidecarExitReason::ManualRequest);
726                self.signaled_sidecar_exit = true;
727                return Err(VpHaltReason::Cancel);
728            }
729        } else {
730            let mut current = Default::default();
731            affinity::get_current_thread_affinity(&mut current).unwrap();
732            assert_eq!(&current, CpuSet::new().set(self.inner.cpu_index));
733
734            // Lower the priority of this VP thread so that the VM does not return
735            // to VTL0 while there is still outstanding VTL2 work to do.
736            nice::nice(1);
737        }
738
739        let mut last_waker = None;
740
741        // Force deliverability notifications to be reevaluated.
742        let vtl0_wakes = WakeReason::new()
743            .with_message_queues(true)
744            .with_intcon(true);
745        let vtl1_wakes = WakeReason::new().with_message_queues(true);
746        self.inner.wake_reasons.fetch_or(
747            ((vtl1_wakes.0 as u64) << 32) | (vtl0_wakes.0 as u64),
748            Ordering::Relaxed,
749        );
750
751        let mut first_scan_irr = true;
752
753        loop {
754            // Process VP activity and wait for the VP to be ready.
755            poll_fn(|cx| {
756                loop {
757                    stop.check()?;
758
759                    // Clear the run VP cancel request.
760                    self.runner.clear_cancel();
761
762                    // Cancel any pending timer.
763                    self.vmtime.cancel_timeout();
764
765                    // Ensure the waker is set.
766                    if !last_waker
767                        .as_ref()
768                        .is_some_and(|waker| cx.waker().will_wake(waker))
769                    {
770                        last_waker = Some(cx.waker().clone());
771                        self.inner.waker.write().clone_from(&last_waker);
772                    }
773
774                    // Process wakes.
775                    let scan_irr = if self.inner.wake_reasons.load(Ordering::Relaxed) != 0 {
776                        self.handle_wake()
777                    } else {
778                        [false, false].into()
779                    };
780
781                    if T::process_interrupts(self, scan_irr, &mut first_scan_irr, dev) {
782                        continue;
783                    }
784
785                    // Arm the timer.
786                    if let Some(timeout) = self.vmtime.get_timeout() {
787                        let deadline = self.vmtime.host_time(timeout);
788                        if self.timer.poll_timer(cx, deadline).is_ready() {
789                            continue;
790                        }
791                    }
792
793                    return <Result<_, VpHaltReason>>::Ok(()).into();
794                }
795            })
796            .await?;
797
798            // Yield if the thread pool is not ready to block.
799            if let Some(idle_control) = &mut self.idle_control {
800                if !idle_control.pre_block() {
801                    yield_now().await;
802                    continue;
803                }
804            }
805
806            if let Some(mode) = self.runner.enter_mode() {
807                *mode = self
808                    .partition
809                    .enter_modes_atomic
810                    .load(Ordering::Relaxed)
811                    .into();
812            }
813
814            // Quiesce RCU before running the VP to avoid having to synchronize with
815            // this CPU during memory protection updates.
816            minircu::global().quiesce();
817
818            T::run_vp(self, dev, &mut stop).await?;
819            self.kernel_returns += 1;
820        }
821    }
822
823    fn flush_async_requests(&mut self) {
824        if self.inner.wake_reasons.load(Ordering::Relaxed) != 0 {
825            let scan_irr = self.handle_wake();
826            for vtl in [GuestVtl::Vtl1, GuestVtl::Vtl0] {
827                if scan_irr[vtl] {
828                    T::poll_apic(self, vtl, true);
829                }
830            }
831        }
832        self.runner.flush_deferred_state();
833    }
834
835    fn access_state(&mut self, vtl: Vtl) -> Self::StateAccess<'_> {
836        T::access_vp_state(self, vtl.try_into().unwrap())
837    }
838
839    fn vtl_inspectable(&self, vtl: Vtl) -> bool {
840        match vtl {
841            Vtl::Vtl0 => true,
842            Vtl::Vtl1 => T::vtl1_inspectable(self),
843            Vtl::Vtl2 => false,
844        }
845    }
846}
847
848impl<'a, T: Backing> UhProcessor<'a, T> {
849    pub(super) fn new(
850        driver: &impl Driver,
851        partition: &'a UhPartitionInner,
852        vp_info: TargetVpInfo,
853        idle_control: Option<&'a mut IdleControl>,
854    ) -> Result<Self, Error> {
855        let inner = partition.vp(vp_info.base.vp_index).unwrap();
856        let mut runner = partition
857            .hcl
858            .runner(inner.vp_index().index(), idle_control.is_none())
859            .unwrap();
860
861        let backing_shared = T::shared(&partition.backing_shared);
862
863        let backing = T::new(
864            BackingParams {
865                partition,
866                vp_info: &vp_info,
867                runner: &mut runner,
868            },
869            backing_shared,
870        )?;
871
872        let mut vp = Self {
873            partition,
874            inner,
875            runner,
876            idle_control,
877            kernel_returns: 0,
878            crash_reg: [0; hvdef::HV_X64_GUEST_CRASH_PARAMETER_MSRS],
879            _not_send: PhantomData,
880            backing,
881            shared: backing_shared,
882            vmtime: partition
883                .vmtime
884                .access(format!("vp-{}", vp_info.base.vp_index.index())),
885            timer: driver.new_dyn_timer(),
886            force_exit_sidecar: false,
887            signaled_sidecar_exit: false,
888            vtls_tlb_locked: VtlsTlbLocked {
889                vtl1: VtlArray::new(false),
890                vtl2: VtlArray::new(false),
891            },
892            exit_activities: Default::default(),
893        };
894
895        T::init(&mut vp);
896
897        Ok(vp)
898    }
899
900    /// Returns true if the interrupt controller has work to do.
901    fn handle_wake(&mut self) -> VtlArray<bool, 2> {
902        let wake_reasons_raw = self.inner.wake_reasons.swap(0, Ordering::SeqCst);
903        let wake_reasons_vtl: [WakeReason; 2] = zerocopy::transmute!(wake_reasons_raw);
904        for (vtl, wake_reasons) in [
905            (GuestVtl::Vtl1, wake_reasons_vtl[1]),
906            (GuestVtl::Vtl0, wake_reasons_vtl[0]),
907        ] {
908            if wake_reasons.message_queues() {
909                let pending_sints = self.inner.message_queues[vtl].pending_sints();
910                if pending_sints != 0 {
911                    // Set SINT interest.
912                    let pending_sints = self.inner.message_queues[vtl].pending_sints();
913                    let mut masked_sints = 0;
914
915                    // Determine which of the pending sints are masked.
916                    for sint in 0..NUM_SINTS as u8 {
917                        if pending_sints & (1 << sint) == 0 {
918                            continue;
919                        }
920                        let sint_msr = if let Some(hv) = self.backing.hv(vtl).as_ref() {
921                            hv.synic.sint(sint)
922                        } else {
923                            #[cfg(guest_arch = "x86_64")]
924                            let sint_reg =
925                                HvX64RegisterName(HvX64RegisterName::Sint0.0 + sint as u32);
926                            #[cfg(guest_arch = "aarch64")]
927                            let sint_reg =
928                                HvArm64RegisterName(HvArm64RegisterName::Sint0.0 + sint as u32);
929                            self.runner.get_vp_register(vtl, sint_reg).unwrap().as_u64()
930                        };
931                        masked_sints |= (HvSynicSint::from(sint_msr).masked() as u16) << sint;
932                    }
933
934                    // Drain the queues for all masked SINTs.
935                    self.inner.message_queues[vtl].post_pending_messages(masked_sints, |_, _| {
936                        Err(HvError::InvalidSynicState)
937                    });
938
939                    self.request_sint_notifications(vtl, pending_sints & !masked_sints);
940                }
941            }
942
943            if wake_reasons.extint() {
944                T::request_extint_readiness(self);
945            }
946
947            #[cfg(guest_arch = "x86_64")]
948            if wake_reasons.hv_start_enable_vtl_vp() {
949                T::handle_vp_start_enable_vtl_wake(self, vtl);
950            }
951
952            #[cfg(guest_arch = "x86_64")]
953            if wake_reasons.update_proxy_irr_filter() {
954                // update `proxy_irr_blocked` filter
955                debug_assert!(self.partition.isolation.is_hardware_isolated());
956                self.update_proxy_irr_filter(vtl);
957            }
958        }
959
960        wake_reasons_vtl.map(|w| w.intcon()).into()
961    }
962
963    fn request_sint_notifications(&mut self, vtl: GuestVtl, sints: u16) {
964        if sints == 0 {
965            return;
966        }
967
968        // Send the SINT notifications to the local synic for non-proxied SINTs.
969        let untrusted_sints = if let Some(hv) = self.backing.hv_mut(vtl).as_mut() {
970            let proxied_sints = hv.synic.proxied_sints();
971            hv.synic.request_sint_readiness(sints & !proxied_sints);
972            proxied_sints
973        } else {
974            !0
975        };
976
977        if sints & untrusted_sints != 0 {
978            assert_eq!(vtl, GuestVtl::Vtl0);
979            T::request_untrusted_sint_readiness(self, sints & untrusted_sints);
980        }
981    }
982
983    fn vp_index(&self) -> VpIndex {
984        self.inner.vp_index()
985    }
986
987    #[cfg(guest_arch = "x86_64")]
988    fn write_crash_msr(&mut self, msr: u32, value: u64, vtl: GuestVtl) -> Result<(), MsrError> {
989        match msr {
990            hvdef::HV_X64_MSR_GUEST_CRASH_CTL => {
991                let crash = VtlCrash {
992                    vp_index: self.vp_index(),
993                    last_vtl: vtl,
994                    control: hvdef::GuestCrashCtl::from(value),
995                    parameters: self.crash_reg,
996                };
997                tracelimit::warn_ratelimited!(
998                    CVM_ALLOWED,
999                    ?crash,
1000                    "Guest has reported system crash"
1001                );
1002
1003                if crash.control.crash_message() {
1004                    let message_gpa = crash.parameters[3];
1005                    let message_size = std::cmp::min(crash.parameters[4], hvdef::HV_PAGE_SIZE);
1006                    let mut message = vec![0; message_size as usize];
1007                    match self.partition.gm[vtl].read_at(message_gpa, &mut message) {
1008                        Ok(()) => {
1009                            let message = String::from_utf8_lossy(&message).into_owned();
1010                            tracelimit::warn_ratelimited!(
1011                                CVM_CONFIDENTIAL,
1012                                message,
1013                                "Guest has reported a system crash message"
1014                            );
1015                        }
1016                        Err(e) => {
1017                            tracelimit::warn_ratelimited!(
1018                                CVM_ALLOWED,
1019                                ?e,
1020                                "Failed to read crash message"
1021                            );
1022                        }
1023                    }
1024                }
1025
1026                self.partition.crash_notification_send.send(crash);
1027            }
1028            hvdef::HV_X64_MSR_GUEST_CRASH_P0
1029            | hvdef::HV_X64_MSR_GUEST_CRASH_P1
1030            | hvdef::HV_X64_MSR_GUEST_CRASH_P2
1031            | hvdef::HV_X64_MSR_GUEST_CRASH_P3
1032            | hvdef::HV_X64_MSR_GUEST_CRASH_P4 => {
1033                self.crash_reg[(msr - hvdef::HV_X64_MSR_GUEST_CRASH_P0) as usize] = value;
1034            }
1035            _ => return Err(MsrError::Unknown),
1036        }
1037        Ok(())
1038    }
1039
1040    #[cfg(guest_arch = "x86_64")]
1041    fn read_crash_msr(&self, msr: u32, _vtl: GuestVtl) -> Result<u64, MsrError> {
1042        let v = match msr {
1043            // Reads of CRASH_CTL report our supported capabilities, not the
1044            // current value.
1045            hvdef::HV_X64_MSR_GUEST_CRASH_CTL => hvdef::GuestCrashCtl::new()
1046                .with_crash_notify(true)
1047                .with_crash_message(true)
1048                .into(),
1049            hvdef::HV_X64_MSR_GUEST_CRASH_P0 => self.crash_reg[0],
1050            hvdef::HV_X64_MSR_GUEST_CRASH_P1 => self.crash_reg[1],
1051            hvdef::HV_X64_MSR_GUEST_CRASH_P2 => self.crash_reg[2],
1052            hvdef::HV_X64_MSR_GUEST_CRASH_P3 => self.crash_reg[3],
1053            hvdef::HV_X64_MSR_GUEST_CRASH_P4 => self.crash_reg[4],
1054            _ => return Err(MsrError::Unknown),
1055        };
1056        Ok(v)
1057    }
1058
1059    /// Emulates an instruction due to a memory access exit.
1060    #[cfg(guest_arch = "x86_64")]
1061    async fn emulate<D: CpuIo>(
1062        &mut self,
1063        devices: &D,
1064        interruption_pending: bool,
1065        vtl: GuestVtl,
1066        cache: T::EmulationCache,
1067    ) -> Result<(), VpHaltReason>
1068    where
1069        for<'b> UhEmulationState<'b, 'a, D, T>: virt_support_x86emu::emulate::EmulatorSupport,
1070    {
1071        let guest_memory = &self.partition.gm[vtl];
1072        let (kx_guest_memory, ux_guest_memory) = match vtl {
1073            GuestVtl::Vtl0 => (
1074                &self.partition.vtl0_kernel_exec_gm,
1075                &self.partition.vtl0_user_exec_gm,
1076            ),
1077            GuestVtl::Vtl1 => (guest_memory, guest_memory),
1078        };
1079        let emu_mem = virt_support_x86emu::emulate::EmulatorMemoryAccess {
1080            gm: guest_memory,
1081            kx_gm: kx_guest_memory,
1082            ux_gm: ux_guest_memory,
1083        };
1084        let mut emulation_state = UhEmulationState {
1085            vp: &mut *self,
1086            interruption_pending,
1087            devices,
1088            vtl,
1089            cache,
1090        };
1091
1092        virt_support_x86emu::emulate::emulate(&mut emulation_state, &emu_mem, devices).await
1093    }
1094
1095    /// Emulates an instruction due to a memory access exit.
1096    #[cfg(guest_arch = "aarch64")]
1097    async fn emulate<D: CpuIo>(
1098        &mut self,
1099        devices: &D,
1100        intercept_state: &aarch64emu::InterceptState,
1101        vtl: GuestVtl,
1102        cache: T::EmulationCache,
1103    ) -> Result<(), VpHaltReason>
1104    where
1105        for<'b> UhEmulationState<'b, 'a, D, T>: virt_support_aarch64emu::emulate::EmulatorSupport,
1106    {
1107        let guest_memory = &self.partition.gm[vtl];
1108        virt_support_aarch64emu::emulate::emulate(
1109            &mut UhEmulationState {
1110                vp: &mut *self,
1111                interruption_pending: intercept_state.interruption_pending,
1112                devices,
1113                vtl,
1114                cache,
1115            },
1116            intercept_state,
1117            guest_memory,
1118            devices,
1119        )
1120        .await
1121    }
1122
1123    #[cfg(guest_arch = "x86_64")]
1124    fn update_proxy_irr_filter(&mut self, vtl: GuestVtl) {
1125        assert_eq!(vtl, GuestVtl::Vtl0);
1126        let mut irr_bits: BitArray<[u32; 8], Lsb0> = BitArray::new(Default::default());
1127
1128        // Get all not masked && proxy SINT vectors
1129        if let Some(hv) = self.backing.hv(vtl).as_ref() {
1130            for sint in 0..NUM_SINTS as u8 {
1131                let sint_msr = hv.synic.sint(sint);
1132                let hv_sint = HvSynicSint::from(sint_msr);
1133                // When vmbus relay is active, then SINT will not be proxied
1134                // in case of non-relay, guest will setup proxied sint
1135                if (hv_sint.proxy() || self.partition.vmbus_relay) && !hv_sint.masked() {
1136                    irr_bits.set(hv_sint.vector() as usize, true);
1137                }
1138            }
1139        }
1140
1141        // Get all device vectors
1142        self.partition.fill_device_vectors(vtl, &mut irr_bits);
1143
1144        // Update `proxy_irr_blocked` filter in run page
1145        self.runner
1146            .update_proxy_irr_filter_vtl0(&irr_bits.into_inner());
1147    }
1148}
1149
1150fn signal_mnf(dev: &impl CpuIo, connection_id: u32) {
1151    if let Err(err) = dev.signal_synic_event(Vtl::Vtl0, connection_id, 0) {
1152        tracelimit::warn_ratelimited!(
1153            CVM_ALLOWED,
1154            error = &err as &dyn std::error::Error,
1155            connection_id,
1156            "failed to signal mnf"
1157        );
1158    }
1159}
1160
1161/// Yields execution back to the executor.
1162async fn yield_now() {
1163    let mut yielded = false;
1164    poll_fn(|cx| {
1165        if !yielded {
1166            // Wake the waker so that this task gets to run again.
1167            cx.waker().wake_by_ref();
1168            yielded = true;
1169            Poll::Pending
1170        } else {
1171            Poll::Ready(())
1172        }
1173    })
1174    .await;
1175}
1176
1177struct UhEmulationState<'a, 'b, T: CpuIo, U: Backing> {
1178    vp: &'a mut UhProcessor<'b, U>,
1179    interruption_pending: bool,
1180    devices: &'a T,
1181    vtl: GuestVtl,
1182    cache: U::EmulationCache,
1183}
1184
1185impl<T: CpuIo, U: Backing> EmulatorMonitorSupport for UhEmulationState<'_, '_, T, U> {
1186    fn check_write(&self, gpa: u64, bytes: &[u8]) -> bool {
1187        self.vp
1188            .partition
1189            .monitor_page
1190            .check_write(gpa, bytes, |connection_id| {
1191                signal_mnf(self.devices, connection_id);
1192            })
1193    }
1194
1195    fn check_read(&self, gpa: u64, bytes: &mut [u8]) -> bool {
1196        self.vp.partition.monitor_page.check_read(gpa, bytes)
1197    }
1198}
1199
1200struct UhHypercallHandler<'a, 'b, T, B: Backing> {
1201    vp: &'a mut UhProcessor<'b, B>,
1202    bus: &'a T,
1203    /// Indicates if the handler is for trusted hypercalls in case hardware isolation is in use. A
1204    /// hypercall is trusted if it was made by the guest using a regular vmcall instruction, without
1205    /// using any host-visible mechanisms. An untrusted hypercall was intercepted from the
1206    /// hypervisor, such as one made by the guest using an isolated mechanism such as tdcall or
1207    /// GHCB.
1208    ///
1209    /// This should always be false if hardware isolation is not in use, as the distinction does
1210    /// not exist in that case.
1211    trusted: bool,
1212    intercepted_vtl: GuestVtl,
1213}
1214
1215impl<T, B: Backing> UhHypercallHandler<'_, '_, T, B> {
1216    fn target_vtl_no_higher(&self, target_vtl: Vtl) -> Result<GuestVtl, HvError> {
1217        if Vtl::from(self.intercepted_vtl) < target_vtl {
1218            return Err(HvError::AccessDenied);
1219        }
1220        Ok(target_vtl.try_into().unwrap())
1221    }
1222}
1223
1224impl<T, B: Backing> hv1_hypercall::GetVpIndexFromApicId for UhHypercallHandler<'_, '_, T, B> {
1225    fn get_vp_index_from_apic_id(
1226        &mut self,
1227        partition_id: u64,
1228        target_vtl: Vtl,
1229        apic_ids: &[u32],
1230        vp_indices: &mut [u32],
1231    ) -> HvRepResult {
1232        tracing::debug!(partition_id, ?target_vtl, "HvGetVpIndexFromApicId");
1233
1234        if partition_id != hvdef::HV_PARTITION_ID_SELF {
1235            return Err((HvError::InvalidPartitionId, 0));
1236        }
1237
1238        let _target_vtl = self.target_vtl_no_higher(target_vtl).map_err(|e| (e, 0))?;
1239
1240        #[cfg(guest_arch = "aarch64")]
1241        if true {
1242            let _ = apic_ids;
1243            let _ = vp_indices;
1244            todo!("AARCH64_TODO");
1245        }
1246
1247        #[cfg(guest_arch = "x86_64")]
1248        for (i, (&apic_id, vp_index)) in apic_ids.iter().zip(vp_indices).enumerate() {
1249            *vp_index = self
1250                .vp
1251                .partition
1252                .vps
1253                .iter()
1254                .find(|vp| vp.vp_info.apic_id == apic_id)
1255                .ok_or((HvError::InvalidParameter, i))?
1256                .vp_info
1257                .base
1258                .vp_index
1259                .index()
1260        }
1261
1262        Ok(())
1263    }
1264}
1265
1266#[cfg(guest_arch = "aarch64")]
1267impl<T: CpuIo, B: Backing> Arm64RegisterState for UhHypercallHandler<'_, '_, T, B> {
1268    fn pc(&mut self) -> u64 {
1269        self.vp
1270            .runner
1271            .get_vp_register(self.intercepted_vtl, HvArm64RegisterName::XPc)
1272            .expect("get vp register cannot fail")
1273            .as_u64()
1274    }
1275
1276    fn set_pc(&mut self, pc: u64) {
1277        self.vp
1278            .runner
1279            .set_vp_register(self.intercepted_vtl, HvArm64RegisterName::XPc, pc.into())
1280            .expect("set vp register cannot fail");
1281    }
1282
1283    fn x(&mut self, n: u8) -> u64 {
1284        self.vp
1285            .runner
1286            .get_vp_register(
1287                self.intercepted_vtl,
1288                HvArm64RegisterName(HvArm64RegisterName::X0.0 + n as u32),
1289            )
1290            .expect("get vp register cannot fail")
1291            .as_u64()
1292    }
1293
1294    fn set_x(&mut self, n: u8, v: u64) {
1295        self.vp
1296            .runner
1297            .set_vp_register(
1298                self.intercepted_vtl,
1299                HvArm64RegisterName(HvArm64RegisterName::X0.0 + n as u32),
1300                v.into(),
1301            )
1302            .expect("set vp register cannot fail")
1303    }
1304}
1305
1306impl<T: CpuIo, B: Backing> hv1_hypercall::PostMessage for UhHypercallHandler<'_, '_, T, B> {
1307    fn post_message(&mut self, connection_id: u32, message: &[u8]) -> hvdef::HvResult<()> {
1308        tracing::trace!(
1309            connection_id,
1310            self.trusted,
1311            "handling post message intercept"
1312        );
1313
1314        self.bus.post_synic_message(
1315            self.intercepted_vtl.into(),
1316            connection_id,
1317            self.trusted,
1318            message,
1319        )
1320    }
1321}
1322
1323impl<T: CpuIo, B: Backing> hv1_hypercall::SignalEvent for UhHypercallHandler<'_, '_, T, B> {
1324    fn signal_event(&mut self, connection_id: u32, flag: u16) -> hvdef::HvResult<()> {
1325        tracing::trace!(connection_id, "handling signal event intercept");
1326
1327        self.bus
1328            .signal_synic_event(self.intercepted_vtl.into(), connection_id, flag)
1329    }
1330}
1331
1332impl<T: CpuIo, B: Backing> UhHypercallHandler<'_, '_, T, B> {
1333    fn retarget_virtual_interrupt(
1334        &mut self,
1335        device_id: u64,
1336        address: u64,
1337        data: u32,
1338        vector: u32,
1339        multicast: bool,
1340        target_processors: ProcessorSet<'_>,
1341    ) -> hvdef::HvResult<()> {
1342        let target_processors = Vec::from_iter(target_processors);
1343        let vpci_params = vmcore::vpci_msi::VpciInterruptParameters {
1344            vector,
1345            multicast,
1346            target_processors: &target_processors,
1347        };
1348
1349        self.vp
1350            .partition
1351            .software_devices
1352            .as_ref()
1353            .expect("should exist if this intercept is registered or this is a CVM")
1354            .retarget_interrupt(device_id, address, data, &vpci_params)
1355    }
1356}
1357
1358impl<T, B: Backing> hv1_hypercall::ExtendedQueryCapabilities for UhHypercallHandler<'_, '_, T, B> {
1359    fn query_extended_capabilities(&mut self) -> hvdef::HvResult<u64> {
1360        // This capability is not actually supported. However Windows may unconditionally issue this
1361        // hypercall. Return InvalidHypercallCode as the error status. This is the same as not
1362        // implementing this at all, but has the advantage of not causing generating error messages.
1363        Err(HvError::InvalidHypercallCode)
1364    }
1365}