hyperlight-host 0.18.0

A lightweight Virtual Machine Manager that can be hosted in an application to safely run untrusted or code within a VM partition with very low latency and overhead.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
// SPDX-License-Identifier: Apache-2.0
// Copyright 2025 The Hyperlight Authors.

//! Windows Hypervisor Platform (WHP) backend for AArch64.
//!
//! This module provides the [`VirtualMachine`] trait implementation using the
//! WHP APIs on Windows ARM64 systems. Because the `windows` crate does not yet
//! expose ARM64 WHP structures, we define our own FFI bindings derived from
//! the Windows SDK header `WinHvPlatformDefs.h` (10.0.26100.0).

use std::os::raw::c_void;
use std::sync::atomic::Ordering;
use std::sync::{Condvar, Mutex};
use std::time::{Duration, Instant};

use hyperlight_common::layout::{
    WHP_GICD_BASE_GPA, WHP_GICR_BASE_GPA, WHP_GITS_TRANSLATOR_BASE_GPA,
};
use hyperlight_common::outb::VmAction;
use windows::Win32::System::Hypervisor::*;
use windows::Win32::System::Threading::GetCurrentProcess;
use windows_result::HRESULT;

use super::release_file_mapping;
use crate::hypervisor::regs::whp_reg::*;
use crate::hypervisor::regs::{
    CommonDebugRegs, CommonFpu, CommonRegisters, CommonSpecialRegisters,
};
use crate::hypervisor::surrogate_process::SurrogateProcess;
use crate::hypervisor::surrogate_process_manager::{
    MAX_WHP_PARTITIONS, get_surrogate_process_manager, surrogates_disabled,
};
use crate::hypervisor::virtual_machine::{
    CreateVmError, HypervisorError, MapMemoryError, RegisterError, ResetVcpuError, RunVcpuError,
    UnmapMemoryError, VirtualMachine, VmExit,
};
use crate::hypervisor::wrappers::HandleWrapper;
use crate::mem::memory_region::{MemoryRegion, MemoryRegionFlags, MemoryRegionType};
#[cfg(feature = "trace_guest")]
use crate::sandbox::trace::TraceContext as SandboxTraceContext;

// ============================================================================
// ARM64 WHP FFI bindings
//
// These are manually translated from WinHvPlatformDefs.h (Windows SDK 10.0.26100.0)
// because the `windows` crate does not expose ARM64 WHP types.
// ============================================================================

const WHV_PARTITION_PROPERTY_CODE_ARM64_IC_PARAMETERS: WHV_PARTITION_PROPERTY_CODE =
    WHV_PARTITION_PROPERTY_CODE(0x00001012);

#[repr(C)]
struct Arm64IcGicV3Parameters {
    gicd_base_address: u64,
    gits_translator_base_address: u64,
    reserved: u32,
    gic_lpi_int_id_bits: u32,
    gic_ppi_overflow_interrupt_from_cntv: u32,
    gic_ppi_performance_monitors_interrupt: u32,
    reserved1: [u32; 6],
}

#[repr(C)]
struct Arm64IcParameters {
    emulation_mode: i32,
    reserved: u32,
    gic_v3_parameters: Arm64IcGicV3Parameters,
}

const _: () = {
    assert!(core::mem::size_of::<Arm64IcGicV3Parameters>() == 56);
    assert!(core::mem::align_of::<Arm64IcGicV3Parameters>() == 8);
    assert!(core::mem::size_of::<Arm64IcParameters>() == 64);
    assert!(core::mem::align_of::<Arm64IcParameters>() == 8);
};

const ARM64_IC_PARAMETERS: Arm64IcParameters = Arm64IcParameters {
    emulation_mode: 1,
    reserved: 0,
    gic_v3_parameters: Arm64IcGicV3Parameters {
        gicd_base_address: WHP_GICD_BASE_GPA,
        gits_translator_base_address: WHP_GITS_TRANSLATOR_BASE_GPA,
        reserved: 0,
        gic_lpi_int_id_bits: 1,
        gic_ppi_overflow_interrupt_from_cntv: 0x1b,
        gic_ppi_performance_monitors_interrupt: 0x17,
        reserved1: [0; 6],
    },
};

const PARTITION_WAIT_TIMEOUT: Duration = Duration::from_secs(10);

type WhvResetPartitionFn = unsafe extern "system" fn(WHV_PARTITION_HANDLE) -> HRESULT;

static WHP_PARTITION_COUNT: Mutex<usize> = Mutex::new(0);
static WHP_PARTITION_AVAILABLE: Condvar = Condvar::new();

#[derive(Debug)]
struct WhpPartitionPermit;

impl WhpPartitionPermit {
    fn acquire() -> Result<Self, CreateVmError> {
        let deadline = Instant::now() + PARTITION_WAIT_TIMEOUT;
        let mut count = WHP_PARTITION_COUNT
            .lock()
            .unwrap_or_else(|error| error.into_inner());

        while *count >= MAX_WHP_PARTITIONS {
            let Some(remaining) = deadline.checked_duration_since(Instant::now()) else {
                return Err(CreateVmError::WhpPartitionLimit);
            };
            let (next_count, result) = WHP_PARTITION_AVAILABLE
                .wait_timeout(count, remaining)
                .unwrap_or_else(|error| error.into_inner());
            count = next_count;
            if result.timed_out() && *count >= MAX_WHP_PARTITIONS {
                return Err(CreateVmError::WhpPartitionLimit);
            }
        }

        *count += 1;
        Ok(Self)
    }
}

impl Drop for WhpPartitionPermit {
    fn drop(&mut self) {
        let mut count = WHP_PARTITION_COUNT
            .lock()
            .unwrap_or_else(|error| error.into_inner());
        *count -= 1;
        WHP_PARTITION_AVAILABLE.notify_one();
    }
}

/// ARM64 WHP exit reasons (from the SDK header under `_ARM64_`).
#[allow(dead_code)]
mod arm64_exit_reasons {
    use windows::Win32::System::Hypervisor::WHV_RUN_VP_EXIT_REASON;

    pub const WHV_EXIT_REASON_NONE: WHV_RUN_VP_EXIT_REASON =
        WHV_RUN_VP_EXIT_REASON(0x00000000u32 as i32);
    pub const WHV_EXIT_REASON_UNMAPPED_GPA: WHV_RUN_VP_EXIT_REASON =
        WHV_RUN_VP_EXIT_REASON(0x80000000u32 as i32);
    pub const WHV_EXIT_REASON_GPA_INTERCEPT: WHV_RUN_VP_EXIT_REASON =
        WHV_RUN_VP_EXIT_REASON(0x80000001u32 as i32);
    pub const WHV_EXIT_REASON_UNRECOVERABLE: WHV_RUN_VP_EXIT_REASON =
        WHV_RUN_VP_EXIT_REASON(0x80000021u32 as i32);
    pub const WHV_EXIT_REASON_INVALID_VP_REGISTER: WHV_RUN_VP_EXIT_REASON =
        WHV_RUN_VP_EXIT_REASON(0x80000020u32 as i32);
    pub const WHV_EXIT_REASON_ARM64_RESET: WHV_RUN_VP_EXIT_REASON =
        WHV_RUN_VP_EXIT_REASON(0x8001000cu32 as i32);
    pub const WHV_EXIT_REASON_CANCELLED: WHV_RUN_VP_EXIT_REASON =
        WHV_RUN_VP_EXIT_REASON(0xFFFFFFFFu32 as i32);
}

/// ARM64 WHP exit context layout.
///
/// On ARM64, `WHV_RUN_VP_EXIT_CONTEXT` is:
/// ```c
/// struct { ExitReason: u32, Reserved: u32, Reserved1: u64, union { ... AsUINT64[32] } }
/// ```
/// Total size = 8 (header) + 8 (reserved1) + 256 (union) = 272 bytes.
///
/// The union's `MemoryAccess` variant starts with `WHV_INTERCEPT_MESSAGE_HEADER` (24 bytes):
/// ```c
/// struct { VpIndex: u32, InstructionLength: u8, InterceptAccessType: u8,
///          ExecutionState: u16, Pc: u64, Cpsr: u64 }
/// ```
/// Followed by memory-access-specific fields.
#[repr(C, align(16))]
#[derive(Clone, Copy)]
struct Arm64ExitContext {
    exit_reason: WHV_RUN_VP_EXIT_REASON,
    reserved: u32,
    reserved1: u64,
    /// Raw payload — union of various context types. We interpret based on exit_reason.
    payload: [u64; 32],
}

const _: () = {
    assert!(core::mem::size_of::<Arm64ExitContext>() == 272);
    assert!(core::mem::align_of::<Arm64ExitContext>() == 16);
};

impl Default for Arm64ExitContext {
    fn default() -> Self {
        unsafe { core::mem::zeroed() }
    }
}

/// Parsed fields from `WHV_INTERCEPT_MESSAGE_HEADER` (ARM64 version).
struct InterceptHeader {
    #[allow(dead_code)]
    vp_index: u32,
    instruction_length: u8,
    intercept_access_type: u8,
    #[allow(dead_code)]
    execution_state: u16,
    pc: u64,
    #[allow(dead_code)]
    cpsr: u64,
}

impl Arm64ExitContext {
    /// Parse the intercept message header from the start of the payload.
    /// This is valid for memory access, unrecoverable, and register intercept exits.
    fn intercept_header(&self) -> InterceptHeader {
        let bytes = unsafe { core::slice::from_raw_parts(self.payload.as_ptr() as *const u8, 24) };
        InterceptHeader {
            vp_index: u32::from_le_bytes(bytes[0..4].try_into().unwrap()),
            instruction_length: bytes[4],
            intercept_access_type: bytes[5],
            execution_state: u16::from_le_bytes(bytes[6..8].try_into().unwrap()),
            pc: u64::from_le_bytes(bytes[8..16].try_into().unwrap()),
            cpsr: u64::from_le_bytes(bytes[16..24].try_into().unwrap()),
        }
    }

    /// For memory access exits: get the GPA from the payload.
    ///
    /// ARM64 `WHV_MEMORY_ACCESS_CONTEXT` layout after the 24-byte header:
    /// ```text
    /// offset 24: Reserved0 (u32)
    /// offset 28: InstructionByteCount (u8)
    /// offset 29: AccessInfo (u8 bitfield: GvaValid, GvaGpaValid, HypercallOutputPending)
    /// offset 30: Reserved1 (u16)
    /// offset 32: InstructionBytes[4] (u32)
    /// offset 36: Reserved2 (u32)
    /// offset 40: Gva (u64)
    /// offset 48: Gpa (u64)
    /// offset 56: Syndrome (u64)
    /// ```
    fn memory_access_gpa(&self) -> u64 {
        let bytes = unsafe { core::slice::from_raw_parts(self.payload.as_ptr() as *const u8, 64) };
        u64::from_le_bytes(bytes[48..56].try_into().unwrap())
    }

    fn memory_access_syndrome(&self) -> u64 {
        let bytes = unsafe { core::slice::from_raw_parts(self.payload.as_ptr() as *const u8, 64) };
        u64::from_le_bytes(bytes[56..64].try_into().unwrap())
    }
}

// ============================================================================
// Surrogate process guard (same as x86_64 version)
// ============================================================================

use std::sync::atomic::AtomicBool as StdAtomicBool;

/// RAII guard: when surrogates are disabled, only one no-surrogate WHP VM
/// can exist at a time. This flag prevents a second one from being created.
static NO_SURROGATE_VM_ACTIVE: StdAtomicBool = StdAtomicBool::new(false);

#[derive(Debug)]
struct NoSurrogateGuard;

impl NoSurrogateGuard {
    fn acquire() -> Result<Self, CreateVmError> {
        if NO_SURROGATE_VM_ACTIVE
            .compare_exchange(false, true, Ordering::SeqCst, Ordering::SeqCst)
            .is_err()
        {
            return Err(CreateVmError::SurrogateProcess(
                "Another no-surrogate WHP VM is already active".to_string(),
            ));
        }
        Ok(NoSurrogateGuard)
    }
}

impl Drop for NoSurrogateGuard {
    fn drop(&mut self) {
        NO_SURROGATE_VM_ACTIVE.store(false, Ordering::SeqCst);
    }
}

// ============================================================================
// WhpVm implementation
// ============================================================================

/// Determine whether the WHP hypervisor API is available.
#[allow(dead_code)]
pub(crate) fn is_hypervisor_present() -> bool {
    const ARM64_SUPPORT_BIT: u64 = 1 << 11;

    let mut capability: WHV_CAPABILITY = Default::default();
    let hypervisor_present = match unsafe {
        WHvGetCapability(
            WHvCapabilityCodeHypervisorPresent,
            &mut capability as *mut _ as *mut c_void,
            std::mem::size_of::<WHV_CAPABILITY>() as u32,
            None,
        )
    } {
        Ok(_) => unsafe { capability.HypervisorPresent.as_bool() },
        Err(_) => {
            tracing::info!("Windows Hypervisor Platform is not available on this system");
            false
        }
    };
    if !hypervisor_present {
        return false;
    }

    match unsafe {
        WHvGetCapability(
            WHvCapabilityCodeFeatures,
            &mut capability as *mut _ as *mut c_void,
            std::mem::size_of::<WHV_CAPABILITY>() as u32,
            None,
        )
    } {
        Ok(_) => unsafe { capability.Features.AsUINT64 & ARM64_SUPPORT_BIT != 0 },
        Err(error) => {
            tracing::info!("Failed to query Windows Hypervisor Platform features: {error}");
            false
        }
    }
}

/// A WHP-backed single-vCPU VM on ARM64.
#[derive(Debug)]
pub(crate) struct WhpVm {
    partition: WHV_PARTITION_HANDLE,
    reset_partition: WhvResetPartitionFn,
    map_gpa_range2: WhvMapGpaRange2Fn,
    _partition_permit: WhpPartitionPermit,
    surrogate_process: Option<SurrogateProcess>,
    /// Tracks host-side file mappings for cleanup.
    file_mappings: Vec<(HandleWrapper, *mut c_void)>,
    _no_surrogate_guard: Option<NoSurrogateGuard>,
}

// Safety: same reasoning as x86_64 WhpVm — raw pointers are kernel resource handles,
// not dereferenced, safe to transfer between threads.
unsafe impl Send for WhpVm {}

impl WhpVm {
    pub(crate) fn new() -> Result<Self, CreateVmError> {
        const NUM_CPU: u32 = 1;

        let reset_partition = unsafe { try_load_whv_reset_partition() }
            .map_err(|error| CreateVmError::InitializeVm(error.into()))?;
        let map_gpa_range2 = unsafe { try_load_whv_map_gpa_range2() }
            .map_err(|error| CreateVmError::InitializeVm(error.into()))?;
        let partition_permit = WhpPartitionPermit::acquire()?;
        let no_surrogate = surrogates_disabled();
        let no_surrogate_guard = if no_surrogate {
            Some(NoSurrogateGuard::acquire()?)
        } else {
            None
        };

        let partition = unsafe {
            let p = WHvCreatePartition().map_err(|e| CreateVmError::CreateVmFd(e.into()))?;
            let mut vcpu_created = false;
            let setup_result = (|| {
                WHvSetPartitionProperty(
                    p,
                    WHvPartitionPropertyCodeProcessorCount,
                    &NUM_CPU as *const _ as *const _,
                    std::mem::size_of_val(&NUM_CPU) as _,
                )
                .map_err(|e| CreateVmError::SetPartitionProperty(e.into()))?;

                WHvSetPartitionProperty(
                    p,
                    WHV_PARTITION_PROPERTY_CODE_ARM64_IC_PARAMETERS,
                    &ARM64_IC_PARAMETERS as *const _ as *const _,
                    std::mem::size_of_val(&ARM64_IC_PARAMETERS) as _,
                )
                .map_err(|e| CreateVmError::SetPartitionProperty(e.into()))?;

                WHvSetupPartition(p).map_err(|e| CreateVmError::InitializeVm(e.into()))?;
                WHvCreateVirtualProcessor(p, 0, 0)
                    .map_err(|e| CreateVmError::CreateVcpuFd(e.into()))?;
                vcpu_created = true;

                let names = [WHV_ARM64_REGISTER_GICR_BASE_GPA];
                let values = [Align16(WHV_REGISTER_VALUE {
                    Reg64: WHP_GICR_BASE_GPA,
                })];
                WHvSetVirtualProcessorRegisters(p, 0, names.as_ptr(), 1, values.as_ptr().cast())
                    .map_err(|e| CreateVmError::InitializeVm(e.into()))
            })();

            if let Err(error) = setup_result {
                if vcpu_created && let Err(cleanup_error) = WHvDeleteVirtualProcessor(p, 0) {
                    tracing::error!("Failed to delete virtual processor: {cleanup_error:?}");
                }
                if let Err(cleanup_error) = WHvDeletePartition(p) {
                    tracing::error!("Failed to delete partition: {cleanup_error:?}");
                }
                return Err(error);
            }

            p
        };

        let mut vm = WhpVm {
            partition,
            reset_partition,
            map_gpa_range2,
            _partition_permit: partition_permit,
            surrogate_process: None,
            file_mappings: Vec::new(),
            _no_surrogate_guard: no_surrogate_guard,
        };

        if !no_surrogate {
            let mgr = get_surrogate_process_manager()
                .map_err(|e| CreateVmError::SurrogateProcess(e.to_string()))?;
            vm.surrogate_process = Some(
                mgr.get_surrogate_process()
                    .map_err(|e| CreateVmError::SurrogateProcess(e.to_string()))?,
            );
        }

        Ok(vm)
    }

    fn get_registers<const N: usize>(
        &self,
        names: &[WHV_REGISTER_NAME; N],
    ) -> Result<[Align16<WHV_REGISTER_VALUE>; N], HypervisorError> {
        let mut values: [Align16<WHV_REGISTER_VALUE>; N] = unsafe { core::mem::zeroed() };
        unsafe {
            WHvGetVirtualProcessorRegisters(
                self.partition,
                0,
                names.as_ptr(),
                N as u32,
                values.as_mut_ptr().cast(),
            )
            .map_err(HypervisorError::from)?;
        }
        Ok(values)
    }

    fn set_registers<const N: usize>(
        &self,
        names: &[WHV_REGISTER_NAME; N],
        values: &[Align16<WHV_REGISTER_VALUE>; N],
    ) -> Result<(), HypervisorError> {
        unsafe {
            WHvSetVirtualProcessorRegisters(
                self.partition,
                0,
                names.as_ptr(),
                N as u32,
                values.as_ptr().cast(),
            )
            .map_err(HypervisorError::from)?;
        }
        Ok(())
    }
}

impl VirtualMachine for WhpVm {
    unsafe fn map_memory(
        &mut self,
        (_slot, region): (u32, &MemoryRegion),
    ) -> Result<(), MapMemoryError> {
        let flags = region
            .flags
            .iter()
            .map(|flag| match flag {
                MemoryRegionFlags::NONE => Ok(WHvMapGpaRangeFlagNone),
                MemoryRegionFlags::READ => Ok(WHvMapGpaRangeFlagRead),
                MemoryRegionFlags::WRITE => Ok(WHvMapGpaRangeFlagWrite),
                MemoryRegionFlags::EXECUTE => Ok(WHvMapGpaRangeFlagExecute),
                _ => Err(MapMemoryError::InvalidFlags(format!(
                    "Invalid memory region flag: {:?}",
                    flag
                ))),
            })
            .collect::<Result<Vec<WHV_MAP_GPA_RANGE_FLAGS>, MapMemoryError>>()?
            .iter()
            .fold(WHvMapGpaRangeFlagNone, |acc, flag| acc | *flag);

        let (process_handle, host_addr) = match &mut self.surrogate_process {
            None => (
                unsafe { GetCurrentProcess() },
                (region.host_region.start.handle_base + region.host_region.start.offset)
                    as *const c_void,
            ),
            Some(surrogate) => {
                let surrogate_base = surrogate
                    .map(
                        region.host_region.start.from_handle,
                        region.host_region.start.handle_base,
                        region.host_region.start.handle_size,
                        &region.region_type.surrogate_mapping(),
                    )
                    .map_err(|e| MapMemoryError::SurrogateProcess(e.to_string()))?;
                let surrogate_addr = surrogate_base.wrapping_add(region.host_region.start.offset);
                (
                    surrogate.process_handle.into(),
                    surrogate_addr as *const c_void,
                )
            }
        };

        let result = unsafe {
            (self.map_gpa_range2)(
                self.partition,
                process_handle,
                host_addr,
                region.guest_region.start as u64,
                region.guest_region.len() as u64,
                flags,
            )
        };
        if result.is_err() {
            return Err(MapMemoryError::Hypervisor(
                super::super::HypervisorError::WindowsError(windows_result::Error::from_hresult(
                    result,
                )),
            ));
        }

        if region.region_type == MemoryRegionType::MappedFile {
            self.file_mappings.push((
                region.host_region.start.from_handle,
                region.host_region.start.handle_base as *mut c_void,
            ));
        }

        Ok(())
    }

    fn unmap_memory(
        &mut self,
        (_slot, region): (u32, &MemoryRegion),
    ) -> Result<(), UnmapMemoryError> {
        unsafe {
            WHvUnmapGpaRange(
                self.partition,
                region.guest_region.start as u64,
                region.guest_region.len() as u64,
            )
            .map_err(|e| {
                UnmapMemoryError::Hypervisor(super::super::HypervisorError::WindowsError(e))
            })?;
        }
        if let Some(surrogate) = &mut self.surrogate_process {
            surrogate.unmap(region.host_region.start.handle_base);
        }

        if region.region_type == MemoryRegionType::MappedFile {
            let handle_base = region.host_region.start.handle_base as *mut c_void;
            if let Some(pos) = self
                .file_mappings
                .iter()
                .position(|(_, vb)| *vb == handle_base)
            {
                let (handle, view) = self.file_mappings.swap_remove(pos);
                release_file_mapping(view, handle);
            }
        }

        Ok(())
    }

    fn run_vcpu(
        &mut self,
        #[cfg(feature = "trace_guest")] _tc: &mut SandboxTraceContext,
    ) -> Result<VmExit, RunVcpuError> {
        use arm64_exit_reasons::*;
        let mut exit_context = Arm64ExitContext::default();

        unsafe {
            WHvRunVirtualProcessor(
                self.partition,
                0,
                &mut exit_context as *mut _ as *mut c_void,
                std::mem::size_of::<Arm64ExitContext>() as u32,
            )
            .map_err(|e| RunVcpuError::Unknown(e.into()))?;
        }

        match exit_context.exit_reason {
            WHV_EXIT_REASON_UNMAPPED_GPA | WHV_EXIT_REASON_GPA_INTERCEPT => {
                let header = exit_context.intercept_header();
                let gpa = exit_context.memory_access_gpa();

                // On ARM64, I/O is performed via MMIO writes to the I/O page.
                let io_page_gpa = const { hyperlight_common::layout::io_page().unwrap().0 };
                let is_write = header.intercept_access_type == 1;

                if is_write
                    && gpa >= io_page_gpa
                    && (gpa - io_page_gpa) < hyperlight_common::vmem::PAGE_SIZE as u64
                {
                    let off = (gpa - io_page_gpa) as usize;
                    let port = off / core::mem::size_of::<u64>();

                    // Advance PC past the faulting instruction.
                    // WHP ARM64 does not auto-advance PC on intercepts.
                    let next_pc = header.pc + header.instruction_length as u64;
                    self.set_registers(
                        &[WHV_ARM64_REGISTER_PC],
                        &[Align16(WHV_REGISTER_VALUE { Reg64: next_pc })],
                    )
                    .map_err(RunVcpuError::IncrementRip)?;

                    if port == VmAction::Halt as usize {
                        Ok(VmExit::Halt())
                    } else {
                        let syndrome = exit_context.memory_access_syndrome();
                        const ISV: u64 = 1 << 24;
                        const WNR: u64 = 1 << 6;
                        if syndrome & (ISV | WNR) != ISV | WNR {
                            return Ok(VmExit::MmioWrite(gpa));
                        }
                        let source_register = ((syndrome >> 16) & 0x1f) as u32;
                        let access_size = 1usize << ((syndrome >> 22) & 0x3);
                        let value = if source_register == 31 {
                            0
                        } else {
                            let values = self
                                .get_registers(&[xreg(source_register)])
                                .map_err(RunVcpuError::Unknown)?;
                            unsafe { values[0].0.Reg64 }
                        };
                        Ok(VmExit::IoOut(
                            port as u16,
                            value.to_le_bytes()[..access_size].to_vec(),
                        ))
                    }
                } else {
                    // Non-I/O page memory access
                    if is_write {
                        Ok(VmExit::MmioWrite(gpa))
                    } else {
                        Ok(VmExit::MmioRead(gpa))
                    }
                }
            }
            WHV_EXIT_REASON_CANCELLED => Ok(VmExit::Cancelled()),
            WHV_EXIT_REASON_ARM64_RESET => Ok(VmExit::Halt()),
            WHV_EXIT_REASON_UNRECOVERABLE => {
                let header = exit_context.intercept_header();
                Ok(VmExit::Unknown(format!(
                    "Unrecoverable exception at PC={:#x}",
                    header.pc
                )))
            }
            WHV_EXIT_REASON_INVALID_VP_REGISTER => {
                Ok(VmExit::Unknown("Invalid VP register value".to_string()))
            }
            other => Ok(VmExit::Unknown(format!(
                "Unknown WHP ARM64 exit reason: {:#x}",
                other.0 as u32
            ))),
        }
    }

    fn regs(&self) -> Result<CommonRegisters, RegisterError> {
        // Get all 31 GP regs + PC + SP + PSTATE in one batch
        const COUNT: usize = 31 + 3; // X0..X30, PC, SP, PSTATE
        let mut names = [WHV_REGISTER_NAME(0); COUNT];
        for i in 0..31u32 {
            names[i as usize] = xreg(i);
        }
        names[31] = WHV_ARM64_REGISTER_PC;
        names[32] = WHV_ARM64_REGISTER_SP_EL0;
        names[33] = WHV_ARM64_REGISTER_PSTATE;

        let values = self.get_registers(&names).map_err(RegisterError::GetRegs)?;

        let mut x = [0u64; 31];
        for i in 0..31 {
            x[i] = unsafe { values[i].0.Reg64 };
        }

        Ok(CommonRegisters {
            x,
            pc: unsafe { values[31].0.Reg64 },
            sp: unsafe { values[32].0.Reg64 },
            pstate: unsafe { values[33].0.Reg64 },
        })
    }

    fn set_regs(&mut self, regs: &CommonRegisters) -> Result<(), RegisterError> {
        const COUNT: usize = 31 + 3;
        let mut names = [WHV_REGISTER_NAME(0); COUNT];
        let mut values: [Align16<WHV_REGISTER_VALUE>; COUNT] = unsafe { core::mem::zeroed() };

        for i in 0..31u32 {
            names[i as usize] = xreg(i);
            values[i as usize] = Align16(WHV_REGISTER_VALUE {
                Reg64: regs.x[i as usize],
            });
        }
        names[31] = WHV_ARM64_REGISTER_PC;
        values[31] = Align16(WHV_REGISTER_VALUE { Reg64: regs.pc });
        names[32] = WHV_ARM64_REGISTER_SP_EL0;
        values[32] = Align16(WHV_REGISTER_VALUE { Reg64: regs.sp });
        names[33] = WHV_ARM64_REGISTER_PSTATE;
        values[33] = Align16(WHV_REGISTER_VALUE { Reg64: regs.pstate });

        self.set_registers(&names, &values)
            .map_err(RegisterError::SetRegs)
    }

    fn fpu(&self) -> Result<CommonFpu, RegisterError> {
        const COUNT: usize = 34;
        let mut names = [WHV_REGISTER_NAME(0); COUNT];
        for i in 0..32u32 {
            names[i as usize] = qreg(i);
        }
        names[32] = WHV_ARM64_REGISTER_FPSR;
        names[33] = WHV_ARM64_REGISTER_FPCR;

        let values = self.get_registers(&names).map_err(RegisterError::GetFpu)?;
        let mut v = [0u128; 32];
        for i in 0..32 {
            let value = unsafe { values[i].0.Reg128 };
            v[i] = (unsafe { value.Anonymous.High64 } as u128) << 64
                | unsafe { value.Anonymous.Low64 } as u128;
        }
        let fpsr = unsafe { values[32].0.Reg64 } as u32;
        let fpcr = unsafe { values[33].0.Reg64 } as u32;

        Ok(CommonFpu { v, fpsr, fpcr })
    }

    fn set_fpu(&mut self, fpu: &CommonFpu) -> Result<(), RegisterError> {
        const COUNT: usize = 34;
        let mut names = [WHV_REGISTER_NAME(0); COUNT];
        let mut values: [Align16<WHV_REGISTER_VALUE>; COUNT] = unsafe { core::mem::zeroed() };
        for i in 0..32u32 {
            let value = fpu.v[i as usize];
            names[i as usize] = qreg(i);
            values[i as usize] = Align16(WHV_REGISTER_VALUE {
                Reg128: WHV_UINT128 {
                    Anonymous: WHV_UINT128_0 {
                        Low64: value as u64,
                        High64: (value >> 64) as u64,
                    },
                },
            });
        }
        names[32] = WHV_ARM64_REGISTER_FPSR;
        values[32] = Align16(WHV_REGISTER_VALUE {
            Reg64: fpu.fpsr as u64,
        });
        names[33] = WHV_ARM64_REGISTER_FPCR;
        values[33] = Align16(WHV_REGISTER_VALUE {
            Reg64: fpu.fpcr as u64,
        });

        self.set_registers(&names, &values)
            .map_err(RegisterError::SetFpu)
    }

    fn sregs(&self) -> Result<CommonSpecialRegisters, RegisterError> {
        let names = [
            WHV_ARM64_REGISTER_TTBR0_EL1,
            WHV_ARM64_REGISTER_TCR_EL1,
            WHV_ARM64_REGISTER_MAIR_EL1,
            WHV_ARM64_REGISTER_SCTLR_EL1,
            WHV_ARM64_REGISTER_CPACR_EL1,
            WHV_ARM64_REGISTER_VBAR_EL1,
            WHV_ARM64_REGISTER_SP_EL1,
        ];
        let values = self.get_registers(&names).map_err(RegisterError::GetRegs)?;
        Ok(CommonSpecialRegisters {
            ttbr0_el1: unsafe { values[0].0.Reg64 },
            tcr_el1: unsafe { values[1].0.Reg64 },
            mair_el1: unsafe { values[2].0.Reg64 },
            sctlr_el1: unsafe { values[3].0.Reg64 },
            cpacr_el1: unsafe { values[4].0.Reg64 },
            vbar_el1: unsafe { values[5].0.Reg64 },
            sp_el1: unsafe { values[6].0.Reg64 },
        })
    }

    fn set_sregs(&mut self, sregs: &CommonSpecialRegisters) -> Result<(), RegisterError> {
        let names = [
            WHV_ARM64_REGISTER_TTBR0_EL1,
            WHV_ARM64_REGISTER_TCR_EL1,
            WHV_ARM64_REGISTER_MAIR_EL1,
            WHV_ARM64_REGISTER_SCTLR_EL1,
            WHV_ARM64_REGISTER_CPACR_EL1,
            WHV_ARM64_REGISTER_VBAR_EL1,
            WHV_ARM64_REGISTER_SP_EL1,
        ];
        let values = [
            Align16(WHV_REGISTER_VALUE {
                Reg64: sregs.ttbr0_el1,
            }),
            Align16(WHV_REGISTER_VALUE {
                Reg64: sregs.tcr_el1,
            }),
            Align16(WHV_REGISTER_VALUE {
                Reg64: sregs.mair_el1,
            }),
            Align16(WHV_REGISTER_VALUE {
                Reg64: sregs.sctlr_el1,
            }),
            Align16(WHV_REGISTER_VALUE {
                Reg64: sregs.cpacr_el1,
            }),
            Align16(WHV_REGISTER_VALUE {
                Reg64: sregs.vbar_el1,
            }),
            Align16(WHV_REGISTER_VALUE {
                Reg64: sregs.sp_el1,
            }),
        ];
        self.set_registers(&names, &values)
            .map_err(RegisterError::SetRegs)
    }

    fn debug_regs(&self) -> Result<CommonDebugRegs, RegisterError> {
        // Debug register support on ARM64 WHP not yet implemented
        Ok(CommonDebugRegs::default())
    }

    fn set_debug_regs(&self, _drs: &CommonDebugRegs) -> Result<(), RegisterError> {
        // Debug register support on ARM64 WHP not yet implemented
        Ok(())
    }

    fn can_reset_vcpu(&self) -> bool {
        true
    }

    fn reset_vcpu(&mut self) -> Result<(), ResetVcpuError> {
        unsafe {
            let result = (self.reset_partition)(self.partition);
            if result.is_err() {
                return Err(ResetVcpuError::Hypervisor(
                    windows_result::Error::from_hresult(result).into(),
                ));
            }

            let names = [WHV_ARM64_REGISTER_GICR_BASE_GPA];
            let values = [Align16(WHV_REGISTER_VALUE {
                Reg64: WHP_GICR_BASE_GPA,
            })];
            WHvSetVirtualProcessorRegisters(
                self.partition,
                0,
                names.as_ptr(),
                1,
                values.as_ptr().cast(),
            )
            .map_err(|e| ResetVcpuError::Hypervisor(e.into()))?;
        }
        Ok(())
    }

    fn partition_handle(&self) -> WHV_PARTITION_HANDLE {
        self.partition
    }
}

impl Drop for WhpVm {
    fn drop(&mut self) {
        // Clean up file mappings
        for (handle, view) in self.file_mappings.drain(..) {
            release_file_mapping(view, handle);
        }

        unsafe {
            if let Err(e) = WHvDeleteVirtualProcessor(self.partition, 0) {
                tracing::error!("Failed to delete virtual processor: {e:?}");
            }
            if let Err(e) = WHvDeletePartition(self.partition) {
                tracing::error!("Failed to delete partition: {e:?}");
            }
        }
    }
}

// ============================================================================
// Helpers: dynamically load optional WHP APIs
// ============================================================================

unsafe fn try_load_whv_reset_partition() -> Result<WhvResetPartitionFn, windows_result::Error> {
    use windows::Win32::System::LibraryLoader::{GetModuleHandleA, GetProcAddress};
    use windows::core::s;

    let module = unsafe { GetModuleHandleA(s!("winhvplatform.dll")) }?;
    let proc = unsafe { GetProcAddress(module, s!("WHvResetPartition")) };
    proc.map(|function| unsafe {
        std::mem::transmute::<unsafe extern "system" fn() -> isize, WhvResetPartitionFn>(function)
    })
    .ok_or_else(|| {
        windows_result::Error::new(
            HRESULT::from_win32(127),
            "Failed to find WHvResetPartition in winhvplatform.dll",
        )
    })
}

type WhvMapGpaRange2Fn = unsafe extern "system" fn(
    WHV_PARTITION_HANDLE,
    windows::Win32::Foundation::HANDLE,
    *const c_void,
    u64,
    u64,
    WHV_MAP_GPA_RANGE_FLAGS,
) -> HRESULT;

unsafe fn try_load_whv_map_gpa_range2() -> Result<WhvMapGpaRange2Fn, windows_result::Error> {
    use windows::Win32::System::LibraryLoader::{GetModuleHandleA, GetProcAddress};
    use windows::core::s;

    let module = unsafe { GetModuleHandleA(s!("winhvplatform.dll")) }?;
    let proc = unsafe { GetProcAddress(module, s!("WHvMapGpaRange2")) };
    proc.map(|function| unsafe {
        std::mem::transmute::<unsafe extern "system" fn() -> isize, WhvMapGpaRange2Fn>(function)
    })
    .ok_or_else(|| {
        windows_result::Error::new(
            HRESULT::from_win32(127),
            "Failed to find WHvMapGpaRange2 in winhvplatform.dll",
        )
    })
}