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
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
//! Common types used across the hypervisor crate.

use serde::{Deserialize, Serialize};

/// Returns the effective memory limit for the host process in bytes.
///
/// On macOS this queries `hw.memsize` via `sysctlbyname`.
/// On Linux this returns `min(sysinfo.totalram, cgroup_memory_limit)` so
/// that defaults are sensible inside containers, CI runners, or systemd
/// units with `MemoryMax`.
///
/// Returns 0 if the query fails (should never happen on supported platforms).
#[must_use]
pub fn host_memory_size() -> u64 {
    #[cfg(target_os = "macos")]
    {
        let mut size: u64 = 0;
        let mut len = std::mem::size_of::<u64>();
        // SAFETY: `sysctlbyname` writes at most `len` bytes into `size`,
        // which is a correctly-sized `u64` buffer we own.
        let ret = unsafe {
            libc::sysctlbyname(
                c"hw.memsize".as_ptr(),
                std::ptr::addr_of_mut!(size).cast(),
                &mut len,
                std::ptr::null_mut(),
                0,
            )
        };
        if ret == 0 { size } else { 0 }
    }

    #[cfg(target_os = "linux")]
    {
        // SAFETY: `info` is zero-initialized and we pass a valid pointer.
        // `sysinfo(2)` only writes to the provided struct; fields are
        // plain integers that are valid for any bit pattern.
        let physical = unsafe {
            let mut info: libc::sysinfo = std::mem::zeroed();
            if libc::sysinfo(&mut info) == 0 {
                info.totalram * u64::from(info.mem_unit)
            } else {
                return 0;
            }
        };

        // Respect cgroup memory limits so defaults are sensible inside
        // containers, CI runners, or systemd units with MemoryMax.
        let cgroup = cgroup_memory_limit();
        if cgroup > 0 && cgroup < physical {
            cgroup
        } else {
            physical
        }
    }
}

/// Reads the cgroup memory limit (bytes) for this process.
///
/// Resolves the actual cgroup path via `/proc/self/cgroup`, then reads
/// `memory.max` (v2) or `memory.limit_in_bytes` (v1) from the correct
/// directory. Falls back to root cgroup paths if resolution fails.
/// Returns 0 if no limit is set or the files cannot be read.
#[cfg(target_os = "linux")]
fn cgroup_memory_limit() -> u64 {
    // Try cgroup v2 first, then v1.
    if let Some(v) = cgroup_v2_memory_limit() {
        return v;
    }
    if let Some(v) = cgroup_v1_memory_limit() {
        return v;
    }
    0
}

/// Reads memory.max from the process's cgroup v2 directory.
#[cfg(target_os = "linux")]
fn cgroup_v2_memory_limit() -> Option<u64> {
    // Resolve current cgroup path from /proc/self/cgroup.
    // cgroup v2 has a single "0::" line.
    let cgroup_path = std::fs::read_to_string("/proc/self/cgroup")
        .ok()?
        .lines()
        .find(|l| l.starts_with("0::"))
        .map(|l| l.strip_prefix("0::").unwrap_or("/").to_string())?;

    // Try the process's own cgroup directory first, fall back to root.
    let paths = [
        format!("/sys/fs/cgroup{cgroup_path}/memory.max"),
        "/sys/fs/cgroup/memory.max".to_string(),
    ];
    for path in &paths {
        if let Ok(s) = std::fs::read_to_string(path) {
            let s = s.trim();
            if s != "max" {
                if let Ok(v) = s.parse::<u64>() {
                    return Some(v);
                }
            }
        }
    }
    None
}

/// Reads memory.limit_in_bytes from the process's cgroup v1 memory controller.
#[cfg(target_os = "linux")]
fn cgroup_v1_memory_limit() -> Option<u64> {
    // Find the memory controller entry in /proc/self/cgroup.
    // v1 lines look like "N:memory:/path".
    let cgroup_path = std::fs::read_to_string("/proc/self/cgroup")
        .ok()?
        .lines()
        .find_map(|l| {
            let parts: Vec<&str> = l.splitn(3, ':').collect();
            if parts.len() == 3 && parts[1].split(',').any(|c| c == "memory") {
                Some(parts[2].to_string())
            } else {
                None
            }
        })?;

    let paths = [
        format!("/sys/fs/cgroup/memory{cgroup_path}/memory.limit_in_bytes"),
        "/sys/fs/cgroup/memory/memory.limit_in_bytes".to_string(),
    ];
    for path in &paths {
        if let Ok(s) = std::fs::read_to_string(path) {
            let s = s.trim();
            if let Ok(v) = s.parse::<u64>() {
                // Kernel sets this to a huge sentinel (PAGE_COUNTER_MAX)
                // when there is no limit; ignore values above 2^62.
                if v < (1 << 62) {
                    return Some(v);
                }
            }
        }
    }
    None
}

/// Returns a sensible default VM memory size based on host physical memory.
///
/// The default is half of host RAM, clamped to `[512 MB, 16 GB]` and
/// rounded down to a 1 MiB boundary (KVM and Virtualization.framework
/// both require page-aligned memory sizes).
/// Falls back to 4 GB if host memory detection fails.
#[must_use]
pub fn default_vm_memory_size() -> u64 {
    const MIN_DEFAULT: u64 = 512 * 1024 * 1024; // 512 MB
    const MAX_DEFAULT: u64 = 16 * 1024 * 1024 * 1024; // 16 GB
    const FALLBACK: u64 = 4 * 1024 * 1024 * 1024; // 4 GB
    const MIB: u64 = 1024 * 1024;

    let host = host_memory_size();
    if host == 0 {
        return FALLBACK;
    }

    let size = (host / 2).clamp(MIN_DEFAULT, MAX_DEFAULT);
    // Round down to 1 MiB boundary.
    size & !(MIB - 1)
}

/// Returns a sensible default VM vCPU count: the host's logical core count.
///
/// Falls back to 4 if host CPU detection fails.
#[must_use]
pub fn default_vm_cpu_count() -> u32 {
    std::thread::available_parallelism().map_or(4, |n| u32::try_from(n.get()).unwrap_or(u32::MAX))
}

/// Emits a warning if `memory_size` exceeds 50% of host RAM.
///
/// Shared by Darwin and KVM `validate_config()` to keep the threshold
/// and message consistent.
pub fn warn_memory_exceeds_host_half(memory_size: u64) {
    let host_mem = host_memory_size();
    if host_mem > 0 && memory_size > host_mem / 2 {
        tracing::warn!(
            "VM memory {}MB exceeds 50% of host RAM ({}MB total) — host may experience memory pressure",
            memory_size / (1024 * 1024),
            host_mem / (1024 * 1024),
        );
    }
}

/// CPU architecture.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum CpuArch {
    /// `x86_64` / AMD64
    X86_64,
    /// ARM64 / `AArch64`
    Aarch64,
}

impl CpuArch {
    /// Returns the native CPU architecture of the current system.
    #[must_use]
    pub const fn native() -> Self {
        #[cfg(target_arch = "x86_64")]
        {
            Self::X86_64
        }
        #[cfg(target_arch = "aarch64")]
        {
            Self::Aarch64
        }
        #[cfg(not(any(target_arch = "x86_64", target_arch = "aarch64")))]
        {
            compile_error!("Unsupported CPU architecture")
        }
    }
}

/// Platform capabilities reported by the hypervisor.
#[derive(Debug, Clone)]
pub struct PlatformCapabilities {
    /// Supported CPU architectures.
    pub supported_archs: Vec<CpuArch>,
    /// Maximum number of vCPUs per VM.
    pub max_vcpus: u32,
    /// Maximum memory size in bytes.
    pub max_memory: u64,
    /// Whether nested virtualization is supported.
    pub nested_virt: bool,
    /// Whether Rosetta 2 translation is available (macOS only).
    pub rosetta: bool,
}

impl Default for PlatformCapabilities {
    fn default() -> Self {
        Self {
            supported_archs: vec![CpuArch::native()],
            max_vcpus: 1,
            max_memory: 1024 * 1024 * 1024, // 1GB default
            nested_virt: false,
            rosetta: false,
        }
    }
}

/// CPU register state.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct Registers {
    // General purpose registers (x86_64)
    pub rax: u64,
    pub rbx: u64,
    pub rcx: u64,
    pub rdx: u64,
    pub rsi: u64,
    pub rdi: u64,
    pub rsp: u64,
    pub rbp: u64,
    pub r8: u64,
    pub r9: u64,
    pub r10: u64,
    pub r11: u64,
    pub r12: u64,
    pub r13: u64,
    pub r14: u64,
    pub r15: u64,

    // Instruction pointer and flags
    pub rip: u64,
    pub rflags: u64,
}

/// Reason for vCPU exit.
#[derive(Debug, Clone)]
pub enum VcpuExit {
    /// VM halted.
    Halt,
    /// I/O port access.
    IoOut {
        port: u16,
        size: u8,
        data: u64,
    },
    IoIn {
        port: u16,
        size: u8,
    },
    /// Memory-mapped I/O.
    MmioRead {
        addr: u64,
        size: u8,
    },
    MmioWrite {
        addr: u64,
        size: u8,
        data: u64,
    },
    /// Hypercall.
    Hypercall {
        nr: u64,
        args: [u64; 6],
    },
    /// System reset requested.
    SystemReset,
    /// Shutdown requested.
    Shutdown,
    /// Debug exception.
    Debug,
    /// Unknown exit reason.
    Unknown(i32),
}

/// `VirtIO` device configuration for attaching to a VM.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VirtioDeviceConfig {
    /// Device type.
    pub device_type: VirtioDeviceType,
    /// Device-specific configuration.
    pub config: Vec<u8>,
    /// Path to device (for block/fs devices).
    pub path: Option<String>,
    /// Whether the device is read-only.
    pub read_only: bool,
    /// Tag for filesystem devices.
    pub tag: Option<String>,
    /// File descriptor for file-handle-based network attachment.
    #[serde(skip)]
    pub net_fd: Option<i32>,
    /// Optional MAC address for network devices.
    pub mac_address: Option<String>,
}

impl VirtioDeviceConfig {
    /// Creates a new block device configuration.
    pub fn block(path: impl Into<String>, read_only: bool) -> Self {
        Self {
            device_type: VirtioDeviceType::Block,
            config: Vec::new(),
            path: Some(path.into()),
            read_only,
            tag: None,
            net_fd: None,
            mac_address: None,
        }
    }

    /// Creates a new network device configuration with NAT attachment.
    #[must_use]
    pub const fn network() -> Self {
        Self {
            device_type: VirtioDeviceType::Net,
            config: Vec::new(),
            path: None,
            read_only: false,
            tag: None,
            net_fd: None,
            mac_address: None,
        }
    }

    /// Creates a new network device configuration with NAT attachment and an explicit MAC address.
    pub fn network_with_mac(mac_address: impl Into<String>) -> Self {
        Self {
            device_type: VirtioDeviceType::Net,
            config: Vec::new(),
            path: None,
            read_only: false,
            tag: None,
            net_fd: None,
            mac_address: Some(mac_address.into()),
        }
    }

    /// Creates a network device configuration with file-handle attachment.
    ///
    /// The VZ framework side uses one connected datagram socket file descriptor
    /// for bidirectional frame I/O.
    #[must_use]
    pub const fn network_file_handle(fd: i32) -> Self {
        Self {
            device_type: VirtioDeviceType::Net,
            config: Vec::new(),
            path: None,
            read_only: false,
            tag: None,
            net_fd: Some(fd),
            mac_address: None,
        }
    }

    /// Creates a network device configuration with file-handle attachment and
    /// an explicit MAC address.
    ///
    /// Use this when the MAC must match an external interface (e.g. vmnet) so
    /// that bridge FDB lookups resolve correctly.
    pub fn network_file_handle_with_mac(fd: i32, mac_address: impl Into<String>) -> Self {
        Self {
            device_type: VirtioDeviceType::Net,
            config: Vec::new(),
            path: None,
            read_only: false,
            tag: None,
            net_fd: Some(fd),
            mac_address: Some(mac_address.into()),
        }
    }

    /// Creates a new console device configuration.
    #[must_use]
    pub const fn console() -> Self {
        Self {
            device_type: VirtioDeviceType::Console,
            config: Vec::new(),
            path: None,
            read_only: false,
            tag: None,
            net_fd: None,
            mac_address: None,
        }
    }

    /// Creates a new filesystem device configuration.
    pub fn filesystem(path: impl Into<String>, tag: impl Into<String>, read_only: bool) -> Self {
        Self {
            device_type: VirtioDeviceType::Fs,
            config: Vec::new(),
            path: Some(path.into()),
            read_only,
            tag: Some(tag.into()),
            net_fd: None,
            mac_address: None,
        }
    }

    /// Creates a new vsock device configuration.
    #[must_use]
    pub const fn vsock() -> Self {
        Self {
            device_type: VirtioDeviceType::Vsock,
            config: Vec::new(),
            path: None,
            read_only: false,
            tag: None,
            net_fd: None,
            mac_address: None,
        }
    }

    /// Creates a new entropy device configuration.
    #[must_use]
    pub const fn entropy() -> Self {
        Self {
            device_type: VirtioDeviceType::Rng,
            config: Vec::new(),
            path: None,
            read_only: false,
            tag: None,
            net_fd: None,
            mac_address: None,
        }
    }
}

/// `VirtIO` device types.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum VirtioDeviceType {
    /// Block device.
    Block,
    /// Network device.
    Net,
    /// Console device.
    Console,
    /// Filesystem (9p/virtiofs).
    Fs,
    /// Socket device.
    Vsock,
    /// Entropy source.
    Rng,
    /// Balloon device.
    Balloon,
    /// GPU device.
    Gpu,
}

/// Memory balloon statistics.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct BalloonStats {
    /// Target memory size in bytes.
    ///
    /// This is the memory size the balloon is trying to achieve.
    pub target_bytes: u64,

    /// Current balloon size in bytes.
    ///
    /// This is how much memory the balloon has currently claimed.
    /// `actual_guest_memory = configured_memory - current_balloon_size`
    pub current_bytes: u64,

    /// Configured VM memory size in bytes.
    ///
    /// This is the maximum memory available to the guest when
    /// the balloon is fully deflated.
    pub configured_bytes: u64,
}

impl BalloonStats {
    /// Returns the effective memory available to the guest in bytes.
    ///
    /// This is `configured_bytes - current_bytes`.
    #[must_use]
    pub const fn effective_memory(&self) -> u64 {
        self.configured_bytes.saturating_sub(self.current_bytes)
    }

    /// Returns the target memory as a percentage of configured memory.
    #[must_use]
    pub fn target_percent(&self) -> f64 {
        if self.configured_bytes == 0 {
            return 100.0;
        }
        (self.target_bytes as f64 / self.configured_bytes as f64) * 100.0
    }
}

impl VirtioDeviceConfig {
    /// Creates a new balloon device configuration.
    ///
    /// The balloon device allows dynamic memory management by inflating
    /// (reclaiming memory from guest) or deflating (returning memory to guest).
    #[must_use]
    pub const fn balloon() -> Self {
        Self {
            device_type: VirtioDeviceType::Balloon,
            config: Vec::new(),
            path: None,
            read_only: false,
            tag: None,
            net_fd: None,
            mac_address: None,
        }
    }
}

/// ARM64 register state for snapshots.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct Arm64Registers {
    /// General purpose registers X0-X30.
    pub x: [u64; 31],
    /// Stack pointer (SP).
    pub sp: u64,
    /// Program counter (PC).
    pub pc: u64,
    /// Processor state (PSTATE/CPSR).
    pub pstate: u64,
    /// Floating point control register.
    pub fpcr: u64,
    /// Floating point status register.
    pub fpsr: u64,
    /// Vector registers Q0-Q31 (128-bit each, stored as [u64; 2]).
    pub v: [[u64; 2]; 32],
}

/// vCPU snapshot state.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VcpuSnapshot {
    /// vCPU ID.
    pub id: u32,
    /// CPU architecture.
    pub arch: CpuArch,
    /// `x86_64` registers (if applicable).
    pub x86_regs: Option<Registers>,
    /// ARM64 registers (if applicable).
    pub arm64_regs: Option<Arm64Registers>,
    /// Additional architecture-specific state (opaque bytes).
    pub extra_state: Vec<u8>,
}

impl VcpuSnapshot {
    /// Creates a new `x86_64` vCPU snapshot.
    #[must_use]
    pub const fn new_x86(id: u32, regs: Registers) -> Self {
        Self {
            id,
            arch: CpuArch::X86_64,
            x86_regs: Some(regs),
            arm64_regs: None,
            extra_state: Vec::new(),
        }
    }

    /// Creates a new ARM64 vCPU snapshot.
    #[must_use]
    pub const fn new_arm64(id: u32, regs: Arm64Registers) -> Self {
        Self {
            id,
            arch: CpuArch::Aarch64,
            x86_regs: None,
            arm64_regs: Some(regs),
            extra_state: Vec::new(),
        }
    }

    /// Returns `true` if this snapshot contains only default (zeroed) register
    /// state, i.e. it was created as a placeholder and does not represent real
    /// captured vCPU registers.
    #[must_use]
    pub fn is_placeholder(&self) -> bool {
        match (&self.x86_regs, &self.arm64_regs) {
            (Some(regs), _) => regs.rip == 0 && regs.rsp == 0 && regs.rflags == 0,
            (_, Some(regs)) => regs.pc == 0 && regs.sp == 0 && regs.pstate == 0,
            (None, None) => true,
        }
    }
}

/// Device snapshot state.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DeviceSnapshot {
    /// Device type.
    pub device_type: VirtioDeviceType,
    /// Device name/identifier.
    pub name: String,
    /// Device-specific state (serialized).
    pub state: Vec<u8>,
}

/// Memory region info for snapshots.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MemoryRegionSnapshot {
    /// Guest physical address start.
    pub guest_addr: u64,
    /// Region size in bytes.
    pub size: u64,
    /// Whether this region is read-only.
    pub read_only: bool,
    /// Offset in the memory dump file.
    pub file_offset: u64,
}

/// Dirty page tracking info.
#[derive(Debug, Clone)]
pub struct DirtyPageInfo {
    /// Guest physical address of the page.
    pub guest_addr: u64,
    /// Page size (usually 4KB).
    pub size: u64,
}

/// Full VM snapshot metadata.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VmSnapshot {
    /// Snapshot format version.
    pub version: u32,
    /// CPU architecture.
    pub arch: CpuArch,
    /// vCPU states.
    pub vcpus: Vec<VcpuSnapshot>,
    /// Device states.
    pub devices: Vec<DeviceSnapshot>,
    /// Memory region info.
    pub memory_regions: Vec<MemoryRegionSnapshot>,
    /// Total memory size.
    pub total_memory: u64,
    /// Whether memory is compressed.
    pub compressed: bool,
    /// Compression algorithm (if compressed).
    pub compression: Option<String>,
    /// Parent snapshot ID (for incremental).
    pub parent_id: Option<String>,
}