• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

google / alioth / 17385983897

01 Sep 2025 07:41PM UTC coverage: 18.009% (-0.1%) from 18.149%
17385983897

Pull #281

github

web-flow
Merge 782c57b11 into 6ec9a6d6b
Pull Request #281: Port to Apple Hypervisor framework

0 of 154 new or added lines in 11 files covered. (0.0%)

1166 existing lines in 29 files now uncovered.

1362 of 7563 relevant lines covered (18.01%)

18.77 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/alioth/src/hv/kvm/bindings.rs
1
// Copyright 2024 Google LLC
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     https://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15
use std::fmt::{Debug, Formatter, Result};
16

17
#[cfg(target_arch = "aarch64")]
18
use bitfield::bitfield;
19
use bitflags::bitflags;
20

21
use crate::c_enum;
22

23
pub const KVMIO: u8 = 0xAE;
24
pub const KVM_API_VERSION: i32 = 12;
25

26
#[cfg(target_arch = "x86_64")]
27
c_enum! {
28
    pub struct KvmVmType(u64);
29
    {
30
        DEFAULT = 0;
31
        SW_PROTECTED = 1;
32
        SEV = 2;
33
        SEV_ES = 3;
34
        SNP = 4;
35
    }
36
}
37

38
#[cfg(target_arch = "aarch64")]
39
pub struct KvmVmType(#[allow(dead_code)] pub u64);
40

41
#[cfg(target_arch = "x86_64")]
42
pub const KVM_MAX_CPUID_ENTRIES: usize = 256;
43

44
bitflags! {
45
    #[derive(Debug, Clone, Copy, Default)]
46
    pub struct KvmCpuid2Flag: u32 {
47
        const SIGNIFCANT_INDEX = 1;
48
    }
49
}
50

51
#[cfg(target_arch = "x86_64")]
52
#[repr(C)]
53
#[derive(Debug, Copy, Clone, Default)]
54
pub struct KvmCpuidEntry2 {
55
    pub function: u32,
56
    pub index: u32,
57
    pub flags: KvmCpuid2Flag,
58
    pub eax: u32,
59
    pub ebx: u32,
60
    pub ecx: u32,
61
    pub edx: u32,
62
    pub padding: [u32; 3],
63
}
64

65
#[cfg(target_arch = "x86_64")]
66
#[repr(C)]
67
#[derive(Debug, Clone)]
68
pub struct KvmCpuid2<const N: usize> {
69
    pub nent: u32,
70
    pub padding: u32,
71
    pub entries: [KvmCpuidEntry2; N],
72
}
73

74
#[cfg(target_arch = "x86_64")]
75
#[repr(C)]
76
#[derive(Debug, Copy, Clone, Default)]
77
pub struct KvmMsrEntry {
78
    pub index: u32,
79
    pub _reserved: u32,
80
    pub data: u64,
81
}
82

83
#[cfg(target_arch = "x86_64")]
84
#[repr(C)]
85
#[derive(Debug, Clone)]
86
pub struct KvmMsrs<const N: usize> {
87
    pub nmsrs: u32,
88
    pub _pad: u32,
89
    pub entries: [KvmMsrEntry; N],
90
}
91

92
#[cfg(target_arch = "x86_64")]
93
pub const MAX_IO_MSRS: usize = 256;
94

95
bitflags! {
96
    #[derive(Debug, Clone, Copy, Default)]
97
    pub struct KvmMemFlag: u32 {
98
        const LOG_DIRTY_PAGES = 1 << 0;
99
        const READONLY = 1 << 1;
100
        const GUEST_MEMFD = 1 << 2;
101
    }
102
}
103

104
#[repr(C)]
105
#[derive(Debug, Copy, Clone)]
106
pub struct KvmUserspaceMemoryRegion {
107
    pub slot: u32,
108
    pub flags: KvmMemFlag,
109
    pub guest_phys_addr: u64,
110
    pub memory_size: u64,
111
    pub userspace_addr: u64,
112
}
113

114
#[repr(C)]
115
#[derive(Debug, Default, Copy, Clone)]
116
pub struct KvmUserspaceMemoryRegion2 {
117
    pub slot: u32,
118
    pub flags: KvmMemFlag,
119
    pub guest_phys_addr: u64,
120
    pub memory_size: u64,
121
    pub userspace_addr: u64,
122
    pub guest_memfd_offset: u64,
123
    pub guest_memfd: u32,
124
    pub _pad1: u32,
125
    pub _pad2: [u64; 14],
126
}
127

128
bitflags! {
129
    #[derive(Debug, Clone, Copy, Default)]
130
    pub struct KvmMemoryAttribute: u64 {
131
        const PRIVATE = 1 << 3;
132
    }
133
}
134

135
#[repr(C)]
136
#[derive(Debug, Default, Copy, Clone)]
137
pub struct KvmMemoryAttributes {
138
    pub address: u64,
139
    pub size: u64,
140
    pub attributes: KvmMemoryAttribute,
141
    pub flags: u64,
142
}
143

144
#[cfg(target_arch = "x86_64")]
145
#[repr(C)]
146
#[derive(Debug, Copy, Clone, Default)]
147
pub struct KvmCreateGuestMemfd {
148
    pub size: u64,
149
    pub flags: u64,
150
    pub reserved: [u64; 6],
151
}
152

153
#[cfg(target_arch = "x86_64")]
154
#[repr(C)]
155
#[derive(Debug, Default, Copy, Clone)]
156
pub struct KvmRegs {
157
    pub rax: u64,
158
    pub rbx: u64,
159
    pub rcx: u64,
160
    pub rdx: u64,
161
    pub rsi: u64,
162
    pub rdi: u64,
163
    pub rsp: u64,
164
    pub rbp: u64,
165
    pub r8: u64,
166
    pub r9: u64,
167
    pub r10: u64,
168
    pub r11: u64,
169
    pub r12: u64,
170
    pub r13: u64,
171
    pub r14: u64,
172
    pub r15: u64,
173
    pub rip: u64,
174
    pub rflags: u64,
175
}
176

177
#[cfg(target_arch = "x86_64")]
178
#[repr(C)]
179
#[derive(Debug, Default, Copy, Clone)]
180
pub struct KvmSegment {
181
    pub base: u64,
182
    pub limit: u32,
183
    pub selector: u16,
184
    pub type_: u8,
185
    pub present: u8,
186
    pub dpl: u8,
187
    pub db: u8,
188
    pub s: u8,
189
    pub l: u8,
190
    pub g: u8,
191
    pub avl: u8,
192
    pub unusable: u8,
193
    pub padding: u8,
194
}
195

196
#[cfg(target_arch = "x86_64")]
197
#[repr(C)]
198
#[derive(Debug, Default, Copy, Clone)]
199
pub struct KvmDtable {
200
    pub base: u64,
201
    pub limit: u16,
202
    pub padding: [u16; 3],
203
}
204

205
#[cfg(target_arch = "x86_64")]
206
#[repr(C)]
207
#[derive(Debug, Default, Copy, Clone)]
208
pub struct KvmSregs {
209
    pub cs: KvmSegment,
210
    pub ds: KvmSegment,
211
    pub es: KvmSegment,
212
    pub fs: KvmSegment,
213
    pub gs: KvmSegment,
214
    pub ss: KvmSegment,
215
    pub tr: KvmSegment,
216
    pub ldt: KvmSegment,
217
    pub gdt: KvmDtable,
218
    pub idt: KvmDtable,
219
    pub cr0: u64,
220
    pub cr2: u64,
221
    pub cr3: u64,
222
    pub cr4: u64,
223
    pub cr8: u64,
224
    pub efer: u64,
225
    pub apic_base: u64,
226
    pub interrupt_bitmap: [u64; 4],
227
}
228

229
#[cfg(target_arch = "x86_64")]
230
#[repr(C)]
231
#[derive(Debug, Default, Copy, Clone)]
232
pub struct KvmSregs2 {
233
    pub cs: KvmSegment,
234
    pub ds: KvmSegment,
235
    pub es: KvmSegment,
236
    pub fs: KvmSegment,
237
    pub gs: KvmSegment,
238
    pub ss: KvmSegment,
239
    pub tr: KvmSegment,
240
    pub ldt: KvmSegment,
241
    pub gdt: KvmDtable,
242
    pub idt: KvmDtable,
243
    pub cr0: u64,
244
    pub cr2: u64,
245
    pub cr3: u64,
246
    pub cr4: u64,
247
    pub cr8: u64,
248
    pub efer: u64,
249
    pub apic_base: u64,
250
    pub flags: u64,
251
    pub pdptrs: [u64; 4],
252
}
253

254
c_enum! {
255
    pub struct KvmExit(u32);
256
    {
257
        IO = 2;
258
        HYPERCALL = 3;
259
        MMIO = 6;
260
        SHUTDOWN = 8;
261
        SYSTEM_EVENT = 24;
262
    }
263
}
264

265
c_enum! {
266
    pub struct KvmSystemEvent(u32);
267
    {
268
        SHUTDOWN = 1;
269
        RESET = 2;
270
    }
271
}
272

273
#[repr(C)]
274
#[derive(Debug, Clone, Copy)]
275
pub struct KvmRunExitSystemEvent {
276
    pub type_: KvmSystemEvent,
277
    pub flags: u64,
278
}
279

280
#[repr(C)]
281
#[derive(Copy, Clone)]
282
pub struct KvmRun {
283
    pub request_interrupt_window: u8,
284
    pub immediate_exit: u8,
285
    pub padding1: [u8; 6],
286
    pub exit_reason: KvmExit,
287
    pub ready_for_interrupt_injection: u8,
288
    pub if_flag: u8,
289
    pub flags: u16,
290
    pub cr8: u64,
291
    pub apic_base: u64,
292
    pub exit: KvmRunExit,
293
    pub kvm_valid_regs: u64,
294
    pub kvm_dirty_regs: u64,
295
    pub s: KvmSyncRegsBlock,
296
}
297
#[repr(C)]
298
#[derive(Copy, Clone)]
299
pub union KvmRunExit {
300
    pub mmio: KvmRunExitMmio,
301
    pub io: KvmRunExitIo,
302
    pub hypercall: KvmRunExitHypercall,
303
    pub system_event: KvmRunExitSystemEvent,
304
    pub padding: [u8; 256],
305
}
306

307
#[repr(C)]
308
#[derive(Debug, Clone, Copy)]
309
pub struct KvmRunExitMmio {
310
    pub phys_addr: u64,
311
    pub data: [u8; 8],
312
    pub len: u32,
313
    pub is_write: u8,
314
}
315

316
c_enum! {
317
    pub struct KvmExitIo(u8);
318
    {
319
        IN = 0;
320
        OUT = 1;
321
    }
322
}
323

324
#[repr(C)]
325
#[derive(Debug, Clone, Copy)]
326
pub struct KvmRunExitIo {
327
    pub direction: KvmExitIo,
328
    pub size: u8,
329
    pub port: u16,
330
    pub count: u32,
331
    pub data_offset: u64,
332
}
333

334
#[repr(C)]
335
#[derive(Debug, Copy, Clone)]
336
pub struct KvmRunExitHypercall {
337
    pub nr: u64,
338
    pub args: [u64; 6],
339
    pub ret: u64,
340
    pub flags: u64,
341
}
342

343
#[repr(C)]
344
#[derive(Copy, Clone)]
345
pub union KvmSyncRegsBlock {
346
    pub padding: [u8; 2048],
347
}
348

349
bitflags! {
350
    #[derive(Default, Debug, Clone, Copy, PartialEq, Eq, Hash)]
351
    pub struct KvmIrqfdFlag: u32 {
352
        const DEASSIGN = 1 << 0;
353
        const RESAMPLE = 1 << 1;
354
    }
355
}
356

357
#[repr(C)]
358
#[derive(Debug, Default, Copy, Clone)]
359
pub struct KvmIrqfd {
360
    pub fd: u32,
361
    pub gsi: u32,
362
    pub flags: KvmIrqfdFlag,
363
    pub resamplefd: u32,
364
    pub pad: [u8; 16usize],
365
}
366

367
pub const KVM_IRQ_ROUTING_IRQCHIP: u32 = 1;
368
pub const KVM_IRQ_ROUTING_MSI: u32 = 2;
369

370
#[cfg(target_arch = "x86_64")]
371
pub const KVM_IRQCHIP_IOAPIC: u32 = 2;
372

373
#[derive(Debug, Clone, Copy, Default)]
374
#[repr(C)]
375
pub struct KvmIrqRoutingIrqchip {
376
    pub irqchip: u32,
377
    pub pin: u32,
378
}
379

380
#[derive(Clone, Copy, Debug, Default)]
381
#[repr(C)]
382
pub struct KvmIrqRoutingMsi {
383
    pub address_lo: u32,
384
    pub address_hi: u32,
385
    pub data: u32,
386
    pub devid: u32,
387
}
388

389
#[derive(Clone, Copy)]
390
#[repr(C)]
391
pub union KvmIrqRoutingType {
392
    pub irqchip: KvmIrqRoutingIrqchip,
393
    pub msi: KvmIrqRoutingMsi,
394
    pub pad: [u32; 8],
395
}
396

397
impl Debug for KvmIrqRoutingType {
UNCOV
398
    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
UNCOV
399
        f.debug_list().entries(unsafe { &self.pad }.iter()).finish()
400
    }
401
}
402

403
impl Default for KvmIrqRoutingType {
UNCOV
404
    fn default() -> Self {
UNCOV
405
        KvmIrqRoutingType { pad: [0; 8] }
406
    }
407
}
408

409
#[derive(Clone, Copy, Default)]
410
#[repr(C)]
411
pub struct KvmIrqRoutingEntry {
412
    pub gsi: u32,
413
    pub type_: u32,
414
    pub flags: KvmMsiFlag,
415
    pub pad: u32,
416
    pub routing: KvmIrqRoutingType,
417
}
418

419
impl Debug for KvmIrqRoutingEntry {
UNCOV
420
    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
UNCOV
421
        let mut debug_struct = f.debug_struct("KvmIrqRoutingEntry");
UNCOV
422
        debug_struct.field("gsi", &self.gsi);
UNCOV
423
        debug_struct.field("flags", &self.flags);
UNCOV
424
        match self.type_ {
425
            KVM_IRQ_ROUTING_IRQCHIP => {
UNCOV
426
                debug_struct.field("irqchip", unsafe { &self.routing.irqchip })
427
            }
UNCOV
428
            KVM_IRQ_ROUTING_MSI => debug_struct.field("msi", unsafe { &self.routing.msi }),
UNCOV
429
            _ => debug_struct.field("unknown", unsafe { &self.routing.pad }),
430
        };
UNCOV
431
        debug_struct.finish()
432
    }
433
}
434

435
#[repr(C)]
436
#[derive(Clone, Copy)]
437
pub struct KvmIrqRouting<const N: usize> {
438
    pub nr: u32,
439
    pub _flags: u32,
440
    pub entries: [KvmIrqRoutingEntry; N],
441
}
442

443
impl<const N: usize> Debug for KvmIrqRouting<N> {
444
    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
×
445
        f.debug_list()
×
446
            .entries(self.entries.iter().take(self.nr as usize))
×
447
            .finish()
448
    }
449
}
450

451
bitflags! {
452
    #[derive(Default, Debug, Clone, Copy, PartialEq, Eq)]
453
    #[repr(transparent)]
454
    pub struct KvmMsiFlag: u32 {
455
        #[cfg(target_arch = "aarch64")]
456
        const VALID_DEVID = 1 << 0;
457
    }
458
}
459

460
#[repr(C)]
461
#[derive(Debug, Copy, Clone, Default)]
462
pub struct KvmMsi {
463
    pub address_lo: u32,
464
    pub address_hi: u32,
465
    pub data: u32,
466
    pub flags: KvmMsiFlag,
467
    pub devid: u32,
468
    pub pad: [u8; 12usize],
469
}
470

471
c_enum! {
472
    pub struct KvmCap(u32);
473
    {
474
        IRQFD = 32;
475
        SIGNAL_MSI = 77;
476
        ARM_PSCI_0_2 = 102;
477
        EXIT_HYPERCALL = 201;
478
        // GUEST_MEMFD = 234;
479
        // VM_TYPES = 235;
480
    }
481
}
482

483
pub const KVM_HC_MAP_GPA_RANGE: u64 = 12;
484

485
bitflags! {
486
    #[derive(Default, Debug, Clone, Copy, PartialEq, Eq)]
487
    pub struct KvmMapGpaRangeFlag: u64 {
488
        const PAGE_2M = 1 << 0;
489
        const PAGE_1G = 1 << 1;
490
        const ENCRYPTED = 1 << 4;
491
    }
492
}
493

494
bitflags! {
495
    #[derive(Default, Debug, Clone, Copy, PartialEq, Eq, Hash)]
496
    pub struct KvmIoEventFdFlag: u32 {
497
        const DATA_MATCH = 1 << 0;
498
        const PIO = 1 << 1;
499
        const DEASSIGN = 1 << 2;
500
    }
501
}
502

503
#[repr(C)]
504
#[derive(Debug, Copy, Clone, Default)]
505
pub struct KvmIoEventFd {
506
    pub datamatch: u64,
507
    pub addr: u64,
508
    pub len: u32,
509
    pub fd: i32,
510
    pub flags: KvmIoEventFdFlag,
511
    pub pad: [u32; 9],
512
}
513

514
#[repr(C)]
515
#[derive(Debug, Copy, Clone)]
516
pub struct KvmEncRegion {
517
    pub addr: u64,
518
    pub size: u64,
519
}
520

521
#[cfg(target_arch = "x86_64")]
522
#[repr(C)]
523
#[derive(Debug, Clone)]
524
pub struct KvmEnableCap {
525
    pub cap: KvmCap,
526
    pub flags: u32,
527
    pub args: [u64; 4],
528
    pub pad: [u8; 64],
529
}
530

531
#[cfg(not(target_arch = "x86_64"))]
532
#[repr(C)]
533
#[derive(Debug, Copy, Clone, Default)]
534
pub struct KvmOneReg {
535
    pub id: u64,
536
    pub addr: u64,
537
}
538

539
#[cfg(target_arch = "aarch64")]
540
#[derive(Debug, Clone, Copy)]
541
#[repr(C)]
542
pub struct KvmCreateDevice {
543
    pub type_: KvmDevType,
544
    pub fd: i32,
545
    pub flags: u32,
546
}
547

548
#[cfg(target_arch = "aarch64")]
549
#[derive(Debug, Clone, Copy, Default)]
550
#[repr(C)]
551
pub struct KvmDeviceAttr {
552
    pub _flags: u32,
553
    pub group: u32,
554
    pub attr: u64,
555
    pub addr: u64,
556
}
557

558
#[cfg(target_arch = "aarch64")]
559
c_enum! {
560
    pub struct KvmDevType(u32);
561
    {
562
        ARM_VGIC_V2 = 5;
563
        ARM_VGIC_V3 = 7;
564
        ARM_ITS = 8;
565
    }
566
}
567

568
#[cfg(target_arch = "aarch64")]
569
c_enum! {
570
    pub struct KvmDevArmVgicGrp(u32);
571
    {
572
        ADDR = 0;
573
        DIST_REGS = 1;
574
        CPU_REGS = 2;
575
        NR_IRQS = 3;
576
        CTL = 4;
577
        REDIS_REG = 5;
578
        CPU_SYSREGS = 6;
579
    }
580
}
581

582
#[cfg(target_arch = "aarch64")]
583
c_enum! {
584
    pub struct KvmVgicAddrType(u64);
585
    {
586
        DIST_V2 = 0;
587
        CPU_V2 = 1;
588
        DIST_V3 = 2;
589
        REDIST_V3 = 3;
590
        ITS = 4;
591
        REDIST_REGION_V3 = 5;
592
    }
593
}
594

595
#[cfg(target_arch = "aarch64")]
596
c_enum! {
597
    pub struct KvmDevArmVgicCtrl(u64);
598
    {
599
        INIT = 0;
600
        ITS_SAVE_TABLES = 1;
601
        ITS_RESTORE_TABLES = 2;
602
        VGIC_SAVE_PENDING_TABLES = 3;
603
        ITS_RESET = 4;
604
    }
605
}
606

607
#[cfg(target_arch = "aarch64")]
608
bitfield! {
609
    #[derive(Copy, Clone, Default, PartialEq, Eq, Hash)]
610
    #[repr(transparent)]
611
    pub struct KvmVgicV3RedistRegion(u64);
612
    impl Debug;
613
    pub count, set_count: 63, 52;
614
    pub base, set_base: 51, 16;
615
    pub index, set_index: 11, 0;
616
}
617

618
#[cfg(target_arch = "aarch64")]
619
#[repr(C)]
620
#[derive(Debug, Copy, Clone, Default)]
621
pub struct KvmVcpuInit {
622
    pub target: u32,
623
    pub features: [u32; 7],
624
}
625

626
bitflags! {
627
    #[derive(Debug, Clone, Copy, Default)]
628
    pub struct KvmArmVcpuFeature: u32 {
629
        const POWER_OFF = 1 << 0;
630
        const EL1_32BIT = 1 << 1;
631
        const PSCI_0_2 = 1 << 2;
632
        const PMU_V3 = 1 << 3;
633
    }
634
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc