arcbox-hypervisor 0.4.10

Cross-platform hypervisor abstraction layer for ArcBox
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
//! Integration tests for Darwin vCPU implementation.
//!
//! These tests verify the interaction between DarwinVm and DarwinVcpu,
//! testing the managed execution model of Virtualization.framework.

#![allow(clippy::expect_fun_call)]
#![allow(clippy::field_reassign_with_default)]
#![allow(clippy::unnecessary_cast)]

use std::sync::Arc;
use std::sync::atomic::Ordering;
use std::thread;

#[cfg(target_os = "macos")]
use arcbox_hypervisor::{
    config::VmConfig,
    darwin::{DarwinHypervisor, is_supported},
    traits::{Hypervisor, Vcpu, VirtualMachine},
    types::{CpuArch, VcpuExit},
};

mod vm_vcpu {
    use super::*;

    /// Test that vCPUs can be created from a VM.
    #[cfg(target_os = "macos")]
    #[test]
    fn test_vcpu_creation_from_vm() {
        if !is_supported() {
            println!("Virtualization not supported, skipping");
            return;
        }

        let hypervisor = DarwinHypervisor::new().expect("Failed to create hypervisor");

        let config = VmConfig {
            vcpu_count: 4,
            memory_size: 256 * 1024 * 1024,
            arch: CpuArch::native(),
            ..Default::default()
        };

        let mut vm = hypervisor.create_vm(config).expect("Failed to create VM");

        // Create multiple vCPUs
        for i in 0..4 {
            let vcpu = vm
                .create_vcpu(i)
                .expect(&format!("Failed to create vCPU {}", i));
            assert_eq!(vcpu.id(), i);
        }

        println!("Successfully created 4 vCPUs");
    }

    /// Test that duplicate vCPU creation fails.
    #[cfg(target_os = "macos")]
    #[test]
    fn test_vcpu_duplicate_creation_fails() {
        if !is_supported() {
            println!("Virtualization not supported, skipping");
            return;
        }

        let hypervisor = DarwinHypervisor::new().expect("Failed to create hypervisor");

        let config = VmConfig {
            vcpu_count: 2,
            memory_size: 256 * 1024 * 1024,
            arch: CpuArch::native(),
            ..Default::default()
        };

        let mut vm = hypervisor.create_vm(config).expect("Failed to create VM");

        // First creation should succeed
        let _vcpu0 = vm.create_vcpu(0).expect("Failed to create first vCPU 0");

        // Second creation with same ID should fail
        let result = vm.create_vcpu(0);
        assert!(result.is_err(), "Duplicate vCPU creation should fail");

        println!("Duplicate vCPU creation correctly rejected");
    }

    /// Test that vCPU creation with invalid ID fails.
    #[cfg(target_os = "macos")]
    #[test]
    fn test_vcpu_invalid_id_fails() {
        if !is_supported() {
            println!("Virtualization not supported, skipping");
            return;
        }

        let hypervisor = DarwinHypervisor::new().expect("Failed to create hypervisor");

        let config = VmConfig {
            vcpu_count: 2,
            memory_size: 256 * 1024 * 1024,
            arch: CpuArch::native(),
            ..Default::default()
        };

        let mut vm = hypervisor.create_vm(config).expect("Failed to create VM");

        // ID exceeding vcpu_count should fail
        let result = vm.create_vcpu(2);
        assert!(result.is_err(), "vCPU ID 2 should fail for vcpu_count=2");

        let result = vm.create_vcpu(99);
        assert!(result.is_err(), "vCPU ID 99 should fail for vcpu_count=2");

        println!("Invalid vCPU ID correctly rejected");
    }

    /// Test that vCPU run() returns Halt when VM is not started.
    #[cfg(target_os = "macos")]
    #[test]
    fn test_vcpu_run_before_vm_start() {
        if !is_supported() {
            println!("Virtualization not supported, skipping");
            return;
        }

        let hypervisor = DarwinHypervisor::new().expect("Failed to create hypervisor");

        let config = VmConfig {
            vcpu_count: 1,
            memory_size: 256 * 1024 * 1024,
            arch: CpuArch::native(),
            ..Default::default()
        };

        let mut vm = hypervisor.create_vm(config).expect("Failed to create VM");

        // Create vCPU before VM is started (no VZ VM created yet)
        let mut vcpu = vm.create_vcpu(0).expect("Failed to create vCPU");

        // run() should return Halt immediately since VM is not started
        // (vz_vm pointer is null before finalize_configuration)
        let exit = vcpu.run().expect("run() should succeed");
        assert!(
            matches!(exit, VcpuExit::Halt),
            "Expected Halt exit before VM start, got {:?}",
            exit
        );

        println!("vCPU run() correctly returns Halt before VM start");
    }

    /// Test that is_managed_execution returns true for Darwin VM.
    #[cfg(target_os = "macos")]
    #[test]
    fn test_vm_is_managed_execution() {
        if !is_supported() {
            println!("Virtualization not supported, skipping");
            return;
        }

        let hypervisor = DarwinHypervisor::new().expect("Failed to create hypervisor");

        let config = VmConfig {
            vcpu_count: 1,
            memory_size: 256 * 1024 * 1024,
            arch: CpuArch::native(),
            ..Default::default()
        };

        let vm = hypervisor.create_vm(config).expect("Failed to create VM");

        // Darwin VM should use managed execution
        assert!(
            vm.is_managed_execution(),
            "Darwin VM should report managed execution"
        );

        println!("Darwin VM correctly reports managed execution");
    }

    /// Test vCPU register operations through the VM interface.
    #[cfg(target_os = "macos")]
    #[test]
    fn test_vcpu_registers_through_vm() {
        if !is_supported() {
            println!("Virtualization not supported, skipping");
            return;
        }

        let hypervisor = DarwinHypervisor::new().expect("Failed to create hypervisor");

        let config = VmConfig {
            vcpu_count: 2,
            memory_size: 256 * 1024 * 1024,
            arch: CpuArch::native(),
            ..Default::default()
        };

        let mut vm = hypervisor.create_vm(config).expect("Failed to create VM");

        // Create two vCPUs and test independent register state
        let mut vcpu0 = vm.create_vcpu(0).expect("Failed to create vCPU 0");
        let mut vcpu1 = vm.create_vcpu(1).expect("Failed to create vCPU 1");

        // Set different registers for each vCPU
        let mut regs0 = arcbox_hypervisor::types::Registers::default();
        regs0.rax = 0x1111;
        regs0.rip = 0x2222;

        let mut regs1 = arcbox_hypervisor::types::Registers::default();
        regs1.rax = 0x3333;
        regs1.rip = 0x4444;

        vcpu0.set_regs(&regs0).expect("Failed to set vCPU 0 regs");
        vcpu1.set_regs(&regs1).expect("Failed to set vCPU 1 regs");

        // Verify registers are independent
        let read0 = vcpu0.get_regs().expect("Failed to get vCPU 0 regs");
        let read1 = vcpu1.get_regs().expect("Failed to get vCPU 1 regs");

        assert_eq!(read0.rax, 0x1111);
        assert_eq!(read0.rip, 0x2222);
        assert_eq!(read1.rax, 0x3333);
        assert_eq!(read1.rip, 0x4444);

        println!("vCPU registers are correctly independent");
    }

    /// Test multiple vCPU run() calls don't interfere with each other.
    #[cfg(target_os = "macos")]
    #[test]
    fn test_multiple_vcpu_run_independent() {
        if !is_supported() {
            println!("Virtualization not supported, skipping");
            return;
        }

        let hypervisor = DarwinHypervisor::new().expect("Failed to create hypervisor");

        let config = VmConfig {
            vcpu_count: 4,
            memory_size: 256 * 1024 * 1024,
            arch: CpuArch::native(),
            ..Default::default()
        };

        let mut vm = hypervisor.create_vm(config).expect("Failed to create VM");

        // Create all vCPUs
        let mut vcpus: Vec<_> = (0..4)
            .map(|i| {
                vm.create_vcpu(i)
                    .expect(&format!("Failed to create vCPU {}", i))
            })
            .collect();

        // Call run() on each vCPU - should all return Halt immediately
        for (i, vcpu) in vcpus.iter_mut().enumerate() {
            let exit = vcpu.run().expect(&format!("vCPU {} run() failed", i));
            assert!(
                matches!(exit, VcpuExit::Halt),
                "vCPU {} expected Halt, got {:?}",
                i,
                exit
            );
        }

        println!("Multiple vCPU run() calls are independent");
    }
}

mod threading {
    use super::*;

    /// Test that vCPU can be moved to another thread.
    #[cfg(target_os = "macos")]
    #[test]
    fn test_vcpu_send_to_thread() {
        if !is_supported() {
            println!("Virtualization not supported, skipping");
            return;
        }

        let hypervisor = DarwinHypervisor::new().expect("Failed to create hypervisor");

        let config = VmConfig {
            vcpu_count: 1,
            memory_size: 256 * 1024 * 1024,
            arch: CpuArch::native(),
            ..Default::default()
        };

        let mut vm = hypervisor.create_vm(config).expect("Failed to create VM");
        let mut vcpu = vm.create_vcpu(0).expect("Failed to create vCPU");

        // Move vCPU to another thread and run it
        let handle = thread::spawn(move || {
            let exit = vcpu.run().expect("run() failed in thread");
            assert!(matches!(exit, VcpuExit::Halt));
            vcpu.id()
        });

        let id = handle.join().expect("Thread panicked");
        assert_eq!(id, 0);

        println!("vCPU successfully moved to and executed in another thread");
    }

    /// Test running multiple vCPUs in parallel threads.
    #[cfg(target_os = "macos")]
    #[test]
    fn test_multiple_vcpus_parallel_threads() {
        if !is_supported() {
            println!("Virtualization not supported, skipping");
            return;
        }

        let hypervisor = DarwinHypervisor::new().expect("Failed to create hypervisor");

        let config = VmConfig {
            vcpu_count: 4,
            memory_size: 256 * 1024 * 1024,
            arch: CpuArch::native(),
            ..Default::default()
        };

        let mut vm = hypervisor.create_vm(config).expect("Failed to create VM");

        // Create vCPUs
        let vcpus: Vec<_> = (0..4)
            .map(|i| {
                vm.create_vcpu(i)
                    .expect(&format!("Failed to create vCPU {}", i))
            })
            .collect();

        // Run each vCPU in its own thread
        let handles: Vec<_> = vcpus
            .into_iter()
            .map(|mut vcpu| {
                thread::spawn(move || {
                    let id = vcpu.id();
                    let exit = vcpu.run().expect(&format!("vCPU {} run() failed", id));
                    (id, exit)
                })
            })
            .collect();

        // Collect results
        let mut results: Vec<_> = handles
            .into_iter()
            .map(|h| h.join().expect("Thread panicked"))
            .collect();

        results.sort_by_key(|(id, _)| *id);

        for (id, exit) in &results {
            assert!(
                matches!(exit, VcpuExit::Halt),
                "vCPU {} expected Halt, got {:?}",
                id,
                exit
            );
        }

        println!("4 vCPUs successfully ran in parallel threads");
    }

    /// Test vCPU running flag is visible from other threads.
    #[cfg(target_os = "macos")]
    #[test]
    fn test_vcpu_running_flag_cross_thread() {
        if !is_supported() {
            println!("Virtualization not supported, skipping");
            return;
        }

        let hypervisor = DarwinHypervisor::new().expect("Failed to create hypervisor");

        let config = VmConfig {
            vcpu_count: 1,
            memory_size: 256 * 1024 * 1024,
            arch: CpuArch::native(),
            ..Default::default()
        };

        let mut vm = hypervisor.create_vm(config).expect("Failed to create VM");
        let vcpu = vm.create_vcpu(0).expect("Failed to create vCPU");

        // Get the running flag before moving vCPU
        let running_flag = vcpu.running_flag();

        // Initially not running
        assert!(!running_flag.load(Ordering::SeqCst));

        // Note: With null VZ VM pointer, run() returns immediately so we can't
        // test the running=true state from another thread. This test verifies
        // that the flag is accessible across threads.

        let flag_clone = Arc::clone(&running_flag);
        let handle = thread::spawn(move || {
            // Check flag from another thread
            assert!(!flag_clone.load(Ordering::SeqCst));
        });

        handle.join().expect("Thread panicked");

        println!("vCPU running flag is correctly shared across threads");
    }
}

mod io_mmio {
    use super::*;

    /// Test that I/O result setting is a no-op but doesn't error.
    #[cfg(target_os = "macos")]
    #[test]
    fn test_vcpu_io_result_noop_integration() {
        if !is_supported() {
            println!("Virtualization not supported, skipping");
            return;
        }

        let hypervisor = DarwinHypervisor::new().expect("Failed to create hypervisor");

        let config = VmConfig {
            vcpu_count: 1,
            memory_size: 256 * 1024 * 1024,
            arch: CpuArch::native(),
            ..Default::default()
        };

        let mut vm = hypervisor.create_vm(config).expect("Failed to create VM");
        let mut vcpu = vm.create_vcpu(0).expect("Failed to create vCPU");

        // These should all succeed (no-op on Darwin)
        vcpu.set_io_result(0x00).expect("set_io_result failed");
        vcpu.set_io_result(0xFF).expect("set_io_result failed");
        vcpu.set_io_result(0xFFFF_FFFF_FFFF_FFFF)
            .expect("set_io_result failed");

        vcpu.set_mmio_result(0x00).expect("set_mmio_result failed");
        vcpu.set_mmio_result(0xFF).expect("set_mmio_result failed");
        vcpu.set_mmio_result(0xFFFF_FFFF_FFFF_FFFF)
            .expect("set_mmio_result failed");

        println!("I/O and MMIO result setting correctly succeeds (no-op on Darwin)");
    }
}

mod memory_access {
    use super::*;

    /// Test that vCPU can access VM memory through the memory() method.
    #[cfg(target_os = "macos")]
    #[test]
    fn test_vcpu_memory_access() {
        use arcbox_hypervisor::memory::GuestAddress;
        use arcbox_hypervisor::traits::GuestMemory;

        if !is_supported() {
            println!("Virtualization not supported, skipping");
            return;
        }

        let hypervisor = DarwinHypervisor::new().expect("Failed to create hypervisor");

        let config = VmConfig {
            vcpu_count: 1,
            memory_size: 256 * 1024 * 1024,
            arch: CpuArch::native(),
            ..Default::default()
        };

        let mut vm = hypervisor.create_vm(config).expect("Failed to create VM");

        // Access memory through VM
        let memory = vm.memory();

        // Write some data
        let test_data = [0xDE, 0xAD, 0xBE, 0xEF];
        memory
            .write(GuestAddress(0x1000), &test_data)
            .expect("Memory write failed");

        // Read it back
        let mut read_buf = [0u8; 4];
        memory
            .read(GuestAddress(0x1000), &mut read_buf)
            .expect("Memory read failed");

        assert_eq!(read_buf, test_data);

        // Create vCPU after memory setup
        let _vcpu = vm.create_vcpu(0).expect("Failed to create vCPU");

        // Memory should still be accessible
        let mut verify_buf = [0u8; 4];
        vm.memory()
            .read(GuestAddress(0x1000), &mut verify_buf)
            .expect("Memory read after vCPU creation failed");

        assert_eq!(verify_buf, test_data);

        println!("VM memory access works correctly with vCPU");
    }
}