applevisor 1.0.0

Rust bindings for the Apple Silicon Hypervisor Framework
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
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
//! Creation and management of virtual machines.

use std::marker::PhantomData;
use std::ptr;
use std::sync::{Arc, Mutex, OnceLock};

use applevisor_sys::*;

use crate::error::*;
#[cfg(feature = "macos-15-0")]
use crate::gic::*;
use crate::hv_unsafe_call;
use crate::memory::*;
use crate::vcpu::*;

// -----------------------------------------------------------------------------------------------
// Virtual Machine Config
// -----------------------------------------------------------------------------------------------

/// Supported intermediate physical address (IPA) granules.
#[cfg(feature = "macos-26-0")]
pub type IpaGranule = hv_ipa_granule_t;

/// Represents a virtual machine configuration object.
#[derive(Debug)]
#[cfg(feature = "macos-13-0")]
pub struct VirtualMachineConfig(hv_vm_config_t);

#[cfg(feature = "macos-13-0")]
impl Default for VirtualMachineConfig {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(feature = "macos-13-0")]
impl Drop for VirtualMachineConfig {
    fn drop(&mut self) {
        unsafe { os_release(self.0) }
    }
}

#[cfg(feature = "macos-13-0")]
impl VirtualMachineConfig {
    /// Creates a virtual machine configuration object.
    pub fn new() -> Self {
        Self(unsafe { hv_vm_config_create() })
    }

    /// Returns the maximum intermediate physical address bit length.
    ///
    /// # Discussion
    ///
    /// The bit length is the number of valid bits from an intermediate physical address (IPA).
    /// For example, max IPA bit length of 36 means only the least significant 36 bits of an IPA
    /// are valid, and covers a 64GB range.
    pub fn get_max_ipa_size() -> Result<u32> {
        let mut ipa_bit_length = 0;
        hv_unsafe_call!(hv_vm_config_get_max_ipa_size(&mut ipa_bit_length))?;
        Ok(ipa_bit_length)
    }

    /// Returns the default intermediate physical address bit length.
    ///
    /// # Discussion
    ///
    /// This default IPA size is used if the IPA size is not set explicitly.
    pub fn get_default_ipa_size() -> Result<u32> {
        let mut ipa_bit_length = 0;
        hv_unsafe_call!(hv_vm_config_get_default_ipa_size(&mut ipa_bit_length))?;
        Ok(ipa_bit_length)
    }

    /// Returns intermediate physical address bit length in configuration.
    pub fn get_ipa_size(&self) -> Result<u32> {
        let mut ipa_bit_length = 0;
        hv_unsafe_call!(hv_vm_config_get_ipa_size(self.0, &mut ipa_bit_length))?;
        Ok(ipa_bit_length)
    }

    /// Sets intermediate physical address bit length in virtual machine configuration.
    ///
    /// # Discussion
    ///
    /// VM IPA size should be no greater than the max IPA size from
    /// [`VirtualMachineConfig::get_max_ipa_size`]
    pub fn set_ipa_size(&mut self, ipa_bit_length: u32) -> Result<()> {
        hv_unsafe_call!(hv_vm_config_set_ipa_size(self.0, ipa_bit_length))
    }

    /// Returns whether or not EL2 is supported on the current platform.
    #[cfg(feature = "macos-15-0")]
    pub fn get_el2_supported() -> Result<bool> {
        let mut el2_supported = false;
        hv_unsafe_call!(hv_vm_config_get_el2_supported(&mut el2_supported))?;
        Ok(el2_supported)
    }

    /// Returns whether or not EL2 is enabled for a VM configuration.
    #[cfg(feature = "macos-15-0")]
    pub fn get_el2_enabled(&self) -> Result<bool> {
        let mut el2_enabled = false;
        hv_unsafe_call!(hv_vm_config_get_el2_enabled(self.0, &mut el2_enabled))?;
        Ok(el2_enabled)
    }

    /// Sets whether or not EL2 is enabled for a VM configuration.
    #[cfg(feature = "macos-15-0")]
    pub fn set_el2_enabled(&mut self, el2_enabled: bool) -> Result<()> {
        hv_unsafe_call!(hv_vm_config_set_el2_enabled(self.0, el2_enabled))?;
        Ok(())
    }

    /// Returns the value of the maximum Streaming Vector Length (SVL) in bytes.
    ///
    /// # Discussion
    ///
    /// This is the maximum SVL that guests may use and separate from the effective SVL that
    /// guests may set using [`SysReg::SMCR_EL1`].
    ///
    /// Returns error [`HypervisorError::Unsupported`] if SME is not supported.
    #[cfg(feature = "macos-15-2")]
    pub fn get_max_svl_bytes() -> Result<usize> {
        let mut value = 0;
        hv_unsafe_call!(hv_sme_config_get_max_svl_bytes(&mut value))?;
        Ok(value)
    }

    /// Return the default intermediate physical address granule.
    #[cfg(feature = "macos-26-0")]
    pub fn get_default_ipa_granule() -> Result<IpaGranule> {
        let mut value = IpaGranule::HV_IPA_GRANULE_4KB;
        hv_unsafe_call!(hv_vm_config_get_default_ipa_granule(&mut value))?;
        Ok(value)
    }

    /// Return the intermediate physical address granule size in virtual machine configuration.
    #[cfg(feature = "macos-26-0")]
    pub fn get_ipa_granule(&self) -> Result<IpaGranule> {
        let mut value = IpaGranule::HV_IPA_GRANULE_4KB;
        hv_unsafe_call!(hv_vm_config_get_ipa_granule(self.0, &mut value))?;
        Ok(value)
    }

    /// Set the intermediate physical address granule size in virtual machine configuration.
    #[cfg(feature = "macos-26-0")]
    pub fn set_ipa_granule(&mut self, granule: IpaGranule) -> Result<()> {
        hv_unsafe_call!(hv_vm_config_set_ipa_granule(self.0, granule))?;
        Ok(())
    }
}

// -----------------------------------------------------------------------------------------------
// Virtual Machine
// -----------------------------------------------------------------------------------------------

/// Namespace for method instanciating virtual machines.
pub struct VirtualMachine;

impl VirtualMachine {
    /// Creates a new virtual machine instance for the current process.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use applevisor::prelude::*;
    ///
    /// # fn main() -> Result<()> {
    /// // Creates the global instance using the configurations above.
    /// let vm = VirtualMachine::new()?;
    /// # Ok(())
    /// # }
    /// ```
    #[allow(clippy::new_ret_no_self)]
    pub fn new() -> Result<VirtualMachineInstance<GicDisabled>> {
        hv_unsafe_call!(hv_vm_create(ptr::null_mut()))?;
        Ok(VirtualMachineInstance::<GicDisabled> {
            _guard: Some(Arc::new(())),
            _phantom: PhantomData,
        })
    }

    /// Creates a new virtual machine instance for the current process using a user-provided
    /// [`VirtualMachineConfig`].
    ///
    /// # Example
    ///
    /// ```no_run
    /// use applevisor::prelude::*;
    ///
    /// # fn main() -> Result<()> {
    /// // Custom configuration for the virtual machine.
    /// let mut vm_config = VirtualMachineConfig::default();
    /// vm_config.set_el2_enabled(true)?;
    /// // Custom configuration for the GIC.
    /// let mut gic_config = GicConfig::default();
    /// gic_config.set_redistributor_base(0x2000_0000)?;
    ///
    /// // Creates the global instance using the configurations above.
    /// let vm = VirtualMachineInstance::with_config(vm_config)?;
    /// # Ok(())
    /// # }
    /// ```
    #[cfg(feature = "macos-13-0")]
    pub fn with_config(
        config: VirtualMachineConfig,
    ) -> Result<VirtualMachineInstance<GicDisabled>> {
        hv_unsafe_call!(hv_vm_create(config.0))?;
        Ok(VirtualMachineInstance::<GicDisabled> {
            _guard: Some(Arc::new(())),
            _phantom: PhantomData,
        })
    }

    /// Creates a new virtual machine instance for the current process using a user-provided
    /// [`VirtualMachineConfig`], as well as an ARM Generic Interrupt Controller (GIC) v3 device.
    ///
    /// # Discussion
    ///
    /// There must only be a single GIC instance per virtual machine. The device supports a
    /// distributor, redistributors, msi and GIC CPU system registers. When EL2 is enabled, the
    /// device supports GIC hypervisor control registers which are used by the guest hypervisor for
    /// injecting interrupts to its guest.
    ///
    /// GIC v3 uses affinity based interrupt routing. vCPU's must set affinity values in their
    /// [`SysReg::MPIDR_EL1`] register. Once the virtual machine vcpus are running, its topology
    /// is considered final. Destroy vcpus only when you are tearing down the virtual machine.
    ///
    /// GIC MSI support is only provided if both an MSI region base address is configured and an
    /// MSI interrupt range is set.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use applevisor::prelude::*;
    ///
    /// # fn main() -> Result<()> {
    /// // Custom configuration for the virtual machine.
    /// let mut vm_config = VirtualMachineConfig::default();
    /// vm_config.set_el2_enabled(true)?;
    /// // Custom configuration for the GIC.
    /// let mut gic_config = GicConfig::default();
    /// gic_config.set_redistributor_base(0x2000_0000)?;
    ///
    /// // Creates the global instance using the configurations above.
    /// let vm = VirtualMachineInstance::with_gic(vm_config, gic_config)?;
    /// # Ok(())
    /// # }
    /// ```
    #[cfg(feature = "macos-15-0")]
    pub fn with_gic(
        vm_config: VirtualMachineConfig,
        gic_config: GicConfig,
    ) -> Result<VirtualMachineInstance<GicEnabled>> {
        // Creating the VM using `with_config()` is deliberate. This ensures that if
        // `hv_gic_create()` fails, vm will drop and the corresponding instance will be destroyed.
        // This would not be the case with a direct call to `hv_vm_create()`.
        let vm = Self::with_config(vm_config)?;
        hv_unsafe_call!(hv_gic_create(gic_config.0))?;
        Ok(VirtualMachineInstance::<GicEnabled> {
            _guard: vm._guard.clone(),
            _phantom: PhantomData,
        })
    }
}

/// Marks a virtual machine instance configured with a GIC, thus making GIC-related APIs available.
///
/// See [`VirtualMachineInstance<GicEnabled>`].
#[derive(Clone, Debug)]
#[cfg(feature = "macos-15-0")]
pub struct GicEnabled;

/// Marks a virtual machine configured without a GIC instance. Only the base API will be available
/// through these handles.
///
/// See [`VirtualMachineInstance<Gic>`].
#[derive(Clone, Debug)]
pub struct GicDisabled;

/// Represents the unique virtual machine instance of the current process.
///
/// This object can be safely shared among threads and guarantees the VM to exist as long as this
/// handle does.
#[derive(Clone, Debug)]
pub struct VirtualMachineInstance<Gic> {
    pub(crate) _guard: Option<Arc<()>>,
    _phantom: PhantomData<Gic>,
}

/// Destroys the virtual machine context of the current process.
impl<Gic> std::ops::Drop for VirtualMachineInstance<Gic> {
    fn drop(&mut self) {
        // Safe to unwrap here, this is the only place where we modify it directly.
        let guard = self._guard.take().unwrap();
        // If this call succeeds, we know it's the last Arc reference, no other VM instance exists
        // or can be created from this object at this point. We can safely destroy the VM.
        if Arc::into_inner(guard).is_some() {
            // WARN: fails silently if the VM instance could not be cleaned up.
            let _ = hv_unsafe_call!(hv_vm_destroy());
        }
    }
}

impl<Gic> VirtualMachineInstance<Gic> {
    /// Creates a new vCPU.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use applevisor::prelude::*;
    ///
    /// # fn main() -> Result<()> {
    /// # let vm = VirtualMachine::new()?;
    /// let vcpu = vm.vcpu_create()?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn vcpu_create(&self) -> Result<Vcpu> {
        self.vcpu_with_config(VcpuConfig(ptr::null_mut()))
    }

    /// Creates a new vCPU with a user-provided config.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use applevisor::prelude::*;
    ///
    /// # fn main() -> Result<()> {
    /// # let vm = VirtualMachine::new()?;
    /// let vcpu_config = VcpuConfig::default();
    /// let vcpu = vm.vcpu_with_config(vcpu_config)?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn vcpu_with_config(&self, config: VcpuConfig) -> Result<Vcpu> {
        let mut vcpu = 0;
        let mut exit = ptr::null_mut() as *const hv_vcpu_exit_t;
        hv_unsafe_call!(hv_vcpu_create(&mut vcpu, &mut exit, config.0))?;
        Ok(Vcpu {
            vcpu,
            exit,
            _guard_self: Arc::new(()),
            // Safe to unwrap here, it is only empty when the VM object is dropped.
            _guard_vm: Arc::clone(self._guard.as_ref().unwrap()),
            _phantom: PhantomData,
        })
    }

    /// Stops all vCPUs corresponding to the [`VcpuHandle`]s of the `vcpu` input array.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use applevisor::prelude::*;
    /// # use std::sync::mpsc;
    /// # use std::thread;
    ///
    /// # fn main() -> Result<()> {
    /// # let vm = VirtualMachine::new()?;
    /// let thread_count = 3;
    /// let (tx, rx) = mpsc::channel();
    ///
    /// thread::scope(|s| {
    ///     // Each thread will have a Vcpu looping indefinitely.
    ///     for i in 0..thread_count {
    ///         let vm_thread = vm.clone();
    ///         let tx_thread = tx.clone();
    ///         s.spawn(move || {
    ///             // Create the vCPU and memory region that will hold the infinite loop
    ///             // instruction.
    ///             let vcpu = vm_thread.vcpu_create().unwrap();
    ///
    ///             // Write the instruction that while loop indefinitely.
    ///             let mut mem = vm_thread.memory_create(PAGE_SIZE).unwrap();
    ///             let addr = (PAGE_SIZE * i) as u64;
    ///             mem.map(addr, MemPerms::ReadWriteExec).unwrap();
    ///             mem.write_u32(addr, 0x14000000).unwrap();
    ///
    ///             // Set PC to the loop address.
    ///             vcpu.set_reg(Reg::PC, 0x10000).unwrap();
    ///
    ///             // Sending the vCPU handle back to the main thread.
    ///             let handle = vcpu.get_handle();
    ///             tx_thread.send(handle).unwrap();
    ///
    ///             // Starting the VCPU, we should loop indefinitely here.
    ///             vcpu.run().unwrap();
    ///         });
    ///     }
    ///     // Wait for the vCPU handles from each thread.
    ///     let mut handles = vec![];
    ///     for _ in 0..thread_count {
    ///         handles.push(rx.recv().unwrap());
    ///     }
    ///     // Make the vCPU of each thread exit.
    ///     vm.vcpus_exit(&handles).unwrap();
    /// });
    /// # Ok(())
    /// # }
    /// ```
    pub fn vcpus_exit(&self, vcpus: &[VcpuHandle]) -> Result<()> {
        let mut guards = Vec::with_capacity(vcpus.len());
        let mut ids = Vec::with_capacity(vcpus.len());

        for vcpu in vcpus {
            // Hold strong refs to keep vCPUs alive during the call.
            if let Some(strong) = vcpu.take_ref() {
                ids.push(vcpu.id());
                guards.push(strong);
            }
        }

        hv_unsafe_call!(hv_vcpus_exit(ids.as_ptr(), ids.len() as u32))?;

        Ok(())
    }

    /// Creates a memory object.
    ///
    /// # Discussion
    ///
    /// The size provided must be aligned to [`PAGE_SIZE`].
    ///
    /// # Example
    ///
    /// ```no_run
    /// use applevisor::prelude::*;
    ///
    /// # fn main() -> Result<()> {
    /// let vm = VirtualMachine::new()?;
    /// let mem = vm.memory_create(PAGE_SIZE * 10)?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn memory_create(&self, size: usize) -> Result<Memory> {
        let host_alloc = MemAlloc::new(size)?;
        Ok(Memory {
            host_alloc,
            guest_addr: None,
            // Safe to unwrap here, it is only empty when the VM object is dropped.
            _guard_vm: Arc::clone(self._guard.as_ref().unwrap()),
        })
    }
}

/// Transformes a `GicEnabled` instance into a `GicDisabled` one.
/// The underlying object still has a GIC instance, but related APIs can't be called.
#[cfg(feature = "macos-15-0")]
impl From<VirtualMachineInstance<GicEnabled>> for VirtualMachineInstance<GicDisabled> {
    fn from(value: VirtualMachineInstance<GicEnabled>) -> Self {
        VirtualMachineInstance::<GicDisabled> {
            _guard: value._guard.clone(),
            _phantom: PhantomData,
        }
    }
}

/// Static global VM instance.
#[cfg(not(test))]
static _VM_INSTANCE: OnceLock<VirtualMachineStaticInstance> = OnceLock::new();

/// Static global VM instance, but mutable, for testing purposes only.
#[cfg(test)]
static mut _VM_INSTANCE: OnceLock<VirtualMachineStaticInstance> = OnceLock::new();

/// Global lock making sure only one thread performs [`_VM_INSTANCE`] initialization.
static _VM_INIT_LOCK: Mutex<()> = Mutex::new(());

/// Returns the pointer to the global VM instance.
macro_rules! vm_static_instance {
    () => {{
        #[cfg(test)]
        unsafe {
            // Get the raw pointer.
            let ptr: *const OnceLock<VirtualMachineStaticInstance> = &raw const _VM_INSTANCE;
            // Convert the raw pointer to a reference.
            &*ptr
        }
        #[cfg(not(test))]
        &_VM_INSTANCE
    }};
}

/// Resets the mutable global VM instance used for testing.
#[cfg(test)]
pub(crate) fn vm_static_instance_reset() {
    // Making sure there's no static instance when this test starts running.
    // SAFETY: as long as the test it's used in is marked as serial, no other threads has a
    // reference to the vm instance, we can therefore change it however we want.
    unsafe {
        let ptr = &mut *&raw mut _VM_INSTANCE;
        std::ptr::drop_in_place(ptr);
        std::ptr::write(
            ptr as *mut OnceLock<VirtualMachineStaticInstance>,
            OnceLock::new(),
        );
    }
}

/// Wrapper for the static global virtual machine instance.
///
/// Once this instance is set, it cannot be changed.
#[derive(Debug)]
pub enum VirtualMachineStaticInstance {
    /// Container for a virtual machine instance configured without a GIC.
    NoGic(VirtualMachineInstance<GicDisabled>),
    /// Container for a virtual machine instance configured with a GIC.
    #[cfg(feature = "macos-15-0")]
    Gic(VirtualMachineInstance<GicEnabled>),
}

impl VirtualMachineStaticInstance {
    /// Initializes the global vm instance with a [`VirtualMachine`] created with a default config.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use applevisor::prelude::*;
    ///
    /// # fn main() -> Result<()> {
    /// VirtualMachineStaticInstance::init()?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn init() -> Result<()> {
        // If the instance has already been created, we can simply return.
        if vm_static_instance!().get().is_some() {
            return Ok(());
        }
        // Otherwise, we take the global lock to perform the initialization.
        let _guard = _VM_INIT_LOCK.lock().unwrap();
        // We make sure that another thread didn't perform the initialization while we waited for
        // the lock.
        if vm_static_instance!().get().is_some() {
            return Ok(());
        }
        let vm = VirtualMachine::new()?;
        // We can safely unwrap here, we have exclusive control of `_VM_INSTANCE` and we made sure
        // that it was uninitialized.
        vm_static_instance!()
            .set(VirtualMachineStaticInstance::NoGic(vm))
            .unwrap();
        Ok(())
    }

    /// Initializes the global vm instance with a [`VirtualMachine`] created with a custom config.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use applevisor::prelude::*;
    ///
    /// # fn main() -> Result<()> {
    /// // Custom configuration for the virtual machine.
    /// let mut config = VirtualMachineConfig::default();
    /// config.set_el2_enabled(true)?;
    ///
    /// // Creates the global instance using the configuration above.
    /// let _ = VirtualMachineStaticInstance::init_with_config(config)?;
    /// # Ok(())
    /// # }
    /// ```
    #[cfg(feature = "macos-13-0")]
    pub fn init_with_config(vm_config: VirtualMachineConfig) -> Result<()> {
        // If the instance has already been created, we can simply return.
        if vm_static_instance!().get().is_some() {
            return Ok(());
        }
        // Otherwise, we take the global lock to perform the initialization.
        let _guard = _VM_INIT_LOCK.lock().unwrap();
        // We make sure that another thread didn't perform the initialization while we waited for
        // the lock.
        if vm_static_instance!().get().is_some() {
            return Ok(());
        }
        let vm = VirtualMachine::with_config(vm_config)?;
        // We can safely unwrap here, we have exclusive control of `_VM_INSTANCE` and we made sure
        // that it was uninitialized.
        vm_static_instance!()
            .set(VirtualMachineStaticInstance::NoGic(vm))
            .unwrap();
        Ok(())
    }

    /// Initializes the global vm instance with a [`VirtualMachine`] created with a custom config
    /// and a GIC.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use applevisor::prelude::*;
    ///
    /// # #[cfg(feature="macos-15-0")]
    /// # fn main() -> Result<()> {
    /// // Custom configuration for the virtual machine.
    /// let mut vm_config = VirtualMachineConfig::default();
    /// vm_config.set_el2_enabled(true)?;
    /// // Custom configuration for the GIC.
    /// let mut gic_config = GicConfig::default();
    /// gic_config.set_redistributor_base(0x2000_0000)?;
    ///
    /// // Creates the global instance using the configurations above.
    /// let _ = VirtualMachineStaticInstance::init_with_gic(vm_config, gic_config)?;
    /// # Ok(())
    /// # }
    /// ```
    #[cfg(feature = "macos-15-0")]
    pub fn init_with_gic(vm_config: VirtualMachineConfig, gic_config: GicConfig) -> Result<()> {
        // If the instance has already been created, we can simply return.
        if vm_static_instance!().get().is_some() {
            return Ok(());
        }
        // Otherwise, we take the global lock to perform the initialization.
        let _guard = _VM_INIT_LOCK.lock().unwrap();
        // We make sure that another thread didn't perform the initialization while we waited for
        // the lock.
        if vm_static_instance!().get().is_some() {
            return Ok(());
        }
        let vm = VirtualMachine::with_gic(vm_config, gic_config)?;
        // We can safely unwrap here, we have exclusive control of `_VM_INSTANCE` and we made sure
        // that it was uninitialized.
        vm_static_instance!()
            .set(VirtualMachineStaticInstance::Gic(vm))
            .unwrap();
        Ok(())
    }

    /// Retrieves a [`GicDisabled`] handle to the current VM global instance, even if the current
    /// instance has a GIC configured.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use applevisor::prelude::*;
    ///
    /// # fn main() -> Result<()> {
    /// assert!(VirtualMachineStaticInstance::get().is_none());
    ///
    /// let _ = VirtualMachineStaticInstance::init()?;
    /// // The VM instance can be retrieved once it has been initialized.
    /// assert!(VirtualMachineStaticInstance::get().is_some());
    /// # Ok(())
    /// # }
    /// ```
    pub fn get() -> Option<VirtualMachineInstance<GicDisabled>> {
        match vm_static_instance!().get() {
            Some(VirtualMachineStaticInstance::NoGic(vm)) => Some(vm.clone()),
            #[cfg(feature = "macos-15-0")]
            Some(VirtualMachineStaticInstance::Gic(vm)) => Some(Into::<
                VirtualMachineInstance<GicDisabled>,
            >::into(vm.clone())),
            _ => None,
        }
    }

    /// Retrieves a [`GicEnabled`] handle to the current VM global instance, only if the current
    /// instance has a GIC configured.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use applevisor::prelude::*;
    ///
    /// # #[cfg(feature = "macos-15-0")]
    /// # fn main() -> Result<()> {
    /// # let mut vm_config = VirtualMachineConfig::default();
    /// # vm_config.set_el2_enabled(true)?;
    /// # let mut gic_config = GicConfig::default();
    /// # gic_config.set_redistributor_base(0x2000_0000)?;
    /// // No instance has been created yet.
    /// assert!(VirtualMachineStaticInstance::get_gic().is_none());
    ///
    /// let _ = VirtualMachineStaticInstance::init_with_gic(vm_config, gic_config)?;
    /// // The VM instance can be retrieved once it has been initialized.
    /// assert!(VirtualMachineStaticInstance::get_gic().is_some());
    /// # Ok(())
    /// # }
    /// ```
    #[cfg(feature = "macos-15-0")]
    pub fn get_gic() -> Option<VirtualMachineInstance<GicEnabled>> {
        match vm_static_instance!().get() {
            Some(VirtualMachineStaticInstance::Gic(vm)) => Some(vm.clone()),
            _ => None,
        }
    }
}

// -----------------------------------------------------------------------------------------------
// Tests
// -----------------------------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use serial_test::*;
    use std::sync::{mpsc, Barrier};
    use std::thread;

    use crate::memory::PAGE_SIZE;
    use crate::next_mem_addr;

    use super::*;

    #[cfg(feature = "macos-13-0")]
    #[test]
    #[parallel]
    fn config_ipa_size() {
        let mut config = VirtualMachineConfig::default();

        let max_ipa_size = VirtualMachineConfig::get_max_ipa_size();
        assert!(max_ipa_size.is_ok());
        let max_ipa_size = max_ipa_size.unwrap();

        let default_ipa_size = VirtualMachineConfig::get_default_ipa_size();
        assert!(default_ipa_size.is_ok());
        let default_ipa_size = default_ipa_size.unwrap();

        assert_eq!(config.set_ipa_size(0), Ok(()));
        assert_eq!(config.get_ipa_size(), Ok(0));
        assert_eq!(config.set_ipa_size(default_ipa_size - 1), Ok(()));
        assert_eq!(config.get_ipa_size(), Ok(default_ipa_size - 1));
        assert_eq!(config.set_ipa_size(default_ipa_size), Ok(()));
        assert_eq!(config.get_ipa_size(), Ok(default_ipa_size));
        assert_eq!(config.set_ipa_size(max_ipa_size - 1), Ok(()));
        assert_eq!(config.get_ipa_size(), Ok(max_ipa_size - 1));
        assert_eq!(config.set_ipa_size(max_ipa_size), Ok(()));
        assert_eq!(config.get_ipa_size(), Ok(max_ipa_size));
        assert_eq!(
            config.set_ipa_size(max_ipa_size + 1),
            Err(HypervisorError::Unsupported)
        );
        assert_eq!(
            config.set_ipa_size(u32::MAX),
            Err(HypervisorError::Unsupported)
        );
    }

    #[cfg(feature = "macos-15-0")]
    #[test]
    #[parallel]
    fn config_el2() {
        let mut config = VirtualMachineConfig::default();

        let el2_supported = VirtualMachineConfig::get_el2_supported();
        assert!(el2_supported.is_ok());

        assert_eq!(config.set_el2_enabled(false), Ok(()));
        assert_eq!(config.get_el2_enabled(), Ok(false));
        assert_eq!(config.set_el2_enabled(true), Ok(()));
        assert_eq!(config.get_el2_enabled(), Ok(true));
    }

    #[cfg(feature = "macos-15-2")]
    #[test]
    #[parallel]
    fn config_max_svl_bytes() {
        let max_svl_bytes_status = match VirtualMachineConfig::get_max_svl_bytes() {
            Ok(_) | Err(HypervisorError::Unsupported) => Ok(()),
            Err(x) => Err(x),
        };
        assert!(max_svl_bytes_status.is_ok())
    }

    #[cfg(feature = "macos-26-0")]
    #[test]
    #[parallel]
    fn config_ipa_granule() {
        let mut config = VirtualMachineConfig::default();

        let granule_size = VirtualMachineConfig::get_default_ipa_granule();
        assert!(granule_size.is_ok());

        assert_eq!(
            config.set_ipa_granule(IpaGranule::HV_IPA_GRANULE_4KB),
            Ok(())
        );
        assert_eq!(config.get_ipa_granule(), Ok(IpaGranule::HV_IPA_GRANULE_4KB));
        assert_eq!(
            config.set_ipa_granule(IpaGranule::HV_IPA_GRANULE_16KB),
            Ok(())
        );
        assert_eq!(
            config.get_ipa_granule(),
            Ok(IpaGranule::HV_IPA_GRANULE_16KB)
        );
    }

    #[test]
    #[serial]
    fn create_a_default_vm() {
        // Making sure there's no static instance when this test starts running.
        vm_static_instance_reset();

        let vm = VirtualMachine::new();
        assert!(vm.is_ok());
    }

    #[test]
    #[serial]
    fn create_a_default_static_vm_instance() {
        // Making sure there's no static instance when this test starts running.
        vm_static_instance_reset();

        let ret = VirtualMachineStaticInstance::init();
        assert!(ret.is_ok());
        let vm = VirtualMachineStaticInstance::get();
        assert!(vm.is_some());
    }

    #[cfg(feature = "macos-13-0")]
    #[test]
    #[serial]
    fn create_a_vm_with_a_custom_config() {
        // Making sure there's no static instance when this test starts running.
        vm_static_instance_reset();

        let mut config = VirtualMachineConfig::default();

        #[cfg(feature = "macos-15-0")]
        {
            if VirtualMachineConfig::get_el2_supported().unwrap() {
                config.set_el2_enabled(true).unwrap();
            }
        }

        let max_ipa_size = VirtualMachineConfig::get_max_ipa_size().unwrap();
        config.set_ipa_size(max_ipa_size).unwrap();

        let vm = VirtualMachine::with_config(config);
        assert!(vm.is_ok());
    }

    #[cfg(feature = "macos-13-0")]
    #[test]
    #[serial]
    fn create_a_static_vm_instance_with_a_custom_config() {
        // Making sure there's no static instance when this test starts running.
        vm_static_instance_reset();

        let mut config = VirtualMachineConfig::default();

        #[cfg(feature = "macos-15-0")]
        {
            if VirtualMachineConfig::get_el2_supported().unwrap() {
                config.set_el2_enabled(true).unwrap();
            }
        }

        let max_ipa_size = VirtualMachineConfig::get_max_ipa_size().unwrap();
        config.set_ipa_size(max_ipa_size).unwrap();

        let ret = VirtualMachineStaticInstance::init_with_config(config);
        assert!(ret.is_ok());
        let vm = VirtualMachineStaticInstance::get();
        assert!(vm.is_some());
    }

    #[cfg(feature = "macos-15-0")]
    #[test]
    #[serial]
    fn create_a_vm_with_a_gic() {
        // Making sure there's no static instance when this test starts running.
        vm_static_instance_reset();

        // A default GIC config will fail.
        {
            let vm_config = VirtualMachineConfig::default();
            let gic_config = GicConfig::new();
            let vm = VirtualMachine::with_gic(vm_config, gic_config);
            assert!(matches!(vm, Err(HypervisorError::BadArgument)));
        }

        // We need to at least provide the redistributor for the GIC to work.
        {
            let vm_config = VirtualMachineConfig::default();
            let mut gic_config = GicConfig::new();
            assert!(gic_config.set_redistributor_base(0x20000).is_ok());
            let vm = VirtualMachine::with_gic(vm_config, gic_config);
            assert!(vm.is_ok());
        }
    }

    #[cfg(feature = "macos-15-0")]
    #[test]
    #[serial]
    fn create_a_static_vm_instance_with_a_gic() {
        // Making sure there's no static instance when this test starts running.
        vm_static_instance_reset();

        let vm_config = VirtualMachineConfig::default();
        let mut gic_config = GicConfig::new();
        assert!(gic_config.set_redistributor_base(0x20000).is_ok());

        let ret = VirtualMachineStaticInstance::init_with_gic(vm_config, gic_config);
        assert!(ret.is_ok());
        let vm = VirtualMachineStaticInstance::get();
        assert!(vm.is_some());
    }

    #[test]
    #[serial]
    fn create_multiple_vm_instances_from_a_single_thread() {
        // Making sure there's no static instance when this test starts running.
        vm_static_instance_reset();

        {
            // Creating a first VM instance should work!
            let vm1 = VirtualMachine::new();
            assert!(vm1.is_ok());
            // Creating a second instance should fail.
            let vm2 = VirtualMachine::new();
            assert!(matches!(vm2, Err(HypervisorError::Busy)));
            // But cloning the first instance should work.
            let vm3 = vm1.clone();
            drop(vm1);
            assert!(vm3.is_ok());
            // And creating a new instance should still fail.
            let vm4 = VirtualMachine::new();
            assert!(matches!(vm4, Err(HypervisorError::Busy)));
            // Then, if we drop all VM instances created until now...
        }
        // ... creating a brand new instance should work.
        let vm5 = VirtualMachine::new();
        assert!(vm5.is_ok());
    }

    #[test]
    #[serial]
    fn use_one_vm_instance_from_multiple_threads() {
        // Making sure there's no static instance when this test starts running.
        vm_static_instance_reset();

        let barrier = Barrier::new(2);
        let vm = VirtualMachine::new().unwrap();

        thread::scope(|s| {
            // We create one thread holding a reference to the VM instance.
            let vm_thread = vm.clone();
            s.spawn(|| {
                let _ = vm_thread;
                barrier.wait();
            });

            // At this stage, we have two references, we drop one of them.
            drop(vm);

            // Our thread is still waiting on the barrier, so it should be holding the last
            // reference for now. Trying to create a new VM instance will thus fail.
            let vm2 = VirtualMachine::new();
            assert!(matches!(vm2, Err(HypervisorError::Busy)));

            // Our thread will now return, the last reference will drop, and the VM will be
            // destroyed.
            barrier.wait();
        });

        // Now we should be able to create a new VM instance.
        let vm3 = VirtualMachine::new();
        assert!(vm3.is_ok());
    }

    #[test]
    #[parallel]
    fn create_a_vcpu_from_a_vm_instance() {
        let _ = VirtualMachineStaticInstance::init();
        let vm = VirtualMachineStaticInstance::get().unwrap();

        let vcpu = vm.vcpu_create();
        assert!(vcpu.is_ok());
    }

    #[test]
    #[parallel]
    fn create_a_vcpu_with_a_custom_config_from_a_vm_instance() {
        let _ = VirtualMachineStaticInstance::init();
        let vm = VirtualMachineStaticInstance::get().unwrap();

        let vcpu_config = VcpuConfig::default();
        let vcpu = vm.vcpu_with_config(vcpu_config);
        assert!(vcpu.is_ok());
    }

    #[cfg(feature = "macos-15-0")]
    #[test]
    #[serial]
    fn downgrade_a_gicenabled_vm_instance_to_a_gicdisabled_vm_instance() {
        // Making sure there's no static instance when this test starts running.
        vm_static_instance_reset();

        let vm_config = VirtualMachineConfig::default();
        let mut gic_config = GicConfig::new();
        assert!(gic_config.set_redistributor_base(0x20000).is_ok());

        let vm = VirtualMachine::with_gic(vm_config, gic_config);
        assert!(vm.is_ok());
        let vm = vm.unwrap();
        let vm_no_gic = Into::<VirtualMachineInstance<GicDisabled>>::into(vm);
        let vcpu = vm_no_gic.vcpu_create();
        assert!(vcpu.is_ok());
    }

    #[test]
    #[parallel]
    fn exit_running_vcpus() {
        let _ = VirtualMachineStaticInstance::init();
        let vm = VirtualMachineStaticInstance::get().unwrap();

        let thread_count = 4;
        let (tx, rx) = mpsc::channel();

        thread::scope(|s| {
            // Each thread will have a Vcpu looping indefinitely.
            for _ in 0..thread_count {
                let vm_thread = vm.clone();
                let tx_thread = tx.clone();
                s.spawn(move || {
                    // Create the vCPU and memory region that will hold the infinite loop
                    // instruction.
                    let vcpu = vm_thread.vcpu_create().unwrap();
                    let addr = next_mem_addr();
                    let mut mem = vm_thread.memory_create(PAGE_SIZE).unwrap();
                    mem.map(addr, MemPerms::ReadWriteExec).unwrap();
                    // \x00\x00\x00\x14 corresponds to `b .+0`.
                    mem.write_u32(addr, 0x14000000).unwrap();
                    vcpu.set_reg(Reg::PC, addr).unwrap();
                    // Sending the vCPU handle back to the main thread.
                    let handle = vcpu.get_handle();
                    tx_thread.send(handle).unwrap();
                    // Starting the VCPU, we should loop indefinitely here.
                    vcpu.run().unwrap();
                });
            }
            // Wait for the vCPU handles from each thread.
            let mut handles = vec![];
            for _ in 0..thread_count {
                handles.push(rx.recv().unwrap());
            }
            // Make the vCPU of each thread exit.
            vm.vcpus_exit(&handles).unwrap();
        });
    }

    #[test]
    #[serial]
    fn making_sure_vm_static_instances_behave_correctly() {
        // Making sure there's no static instance when this test starts running.
        vm_static_instance_reset();

        // Trying to get the instance when none exists returns None.
        assert!(VirtualMachineStaticInstance::get().is_none());
        #[cfg(feature = "macos-15-0")]
        assert!(VirtualMachineStaticInstance::get_gic().is_none());

        // After creating a new standard instance ...
        assert_eq!(VirtualMachineStaticInstance::init(), Ok(()));
        // ... we can get the standard one ...
        assert!(VirtualMachineStaticInstance::get().is_some());
        // ... but not a GIC-configured one.
        #[cfg(feature = "macos-15-0")]
        assert!(VirtualMachineStaticInstance::get_gic().is_none());

        #[cfg(feature = "macos-15-0")]
        {
            // Removing the static instance.
            vm_static_instance_reset();

            // After creating a new GIC instance ...
            let vm_config = VirtualMachineConfig::default();
            let mut gic_config = GicConfig::new();
            gic_config.set_redistributor_base(0x20000).unwrap();
            assert_eq!(
                VirtualMachineStaticInstance::init_with_gic(vm_config, gic_config),
                Ok(())
            );
            // ... we can get a standard one ...
            assert!(VirtualMachineStaticInstance::get().is_some());
            // ... as well as a GIC one.
            assert!(VirtualMachineStaticInstance::get_gic().is_some());
        }

        // Removing the static instance.
        vm_static_instance_reset();

        // It's ok if we try to recreate the instance when one already exists.
        assert_eq!(VirtualMachineStaticInstance::init(), Ok(()));
        assert_eq!(VirtualMachineStaticInstance::init(), Ok(()));

        #[cfg(feature = "macos-13-0")]
        {
            let vm_config = VirtualMachineConfig::default();
            assert_eq!(
                VirtualMachineStaticInstance::init_with_config(vm_config),
                Ok(())
            );
        }

        #[cfg(feature = "macos-15-0")]
        {
            let vm_config = VirtualMachineConfig::default();
            let mut gic_config = GicConfig::new();
            gic_config.set_redistributor_base(0x20000).unwrap();
            assert_eq!(
                VirtualMachineStaticInstance::init_with_gic(vm_config, gic_config),
                Ok(())
            );
        }
    }
}