arm_vgic 0.5.6

ARM Virtual Generic Interrupt Controller (VGIC) implementation.
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
//! Physical GIC and ITS backing lifecycle.

use alloc::vec::Vec;

use axdevice_base::{InterruptTrigger, ItsId};

use super::{ControllerInner, ControllerState, GicV3Controller, MsiBacking, SpiBacking};
use crate::{
    EventId, GicVcpuId, IntId, ItsDeviceId, LpiId, PhysicalInterruptBinding, PhysicalIrqId,
    PhysicalMsiBinding, RedistributorState, SpiId, VgicError, VgicResult, backend_result,
};

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
struct PhysicalInterruptState {
    distributor_enabled: bool,
    interrupt_enabled: bool,
}

impl PhysicalInterruptState {
    const fn delivery_enabled(self) -> bool {
        self.distributor_enabled && self.interrupt_enabled
    }
}

#[derive(Clone, Copy)]
pub(super) struct PhysicalInterruptSnapshot {
    spi: SpiId,
    binding: PhysicalInterruptBinding,
    state: PhysicalInterruptState,
}

#[derive(Clone, Copy)]
pub(super) struct PhysicalInterruptStateChange {
    spi: SpiId,
    binding: PhysicalInterruptBinding,
    previous: PhysicalInterruptState,
    current: PhysicalInterruptState,
}

impl GicV3Controller {
    /// Queues an acknowledged assigned SPI for hardware-backed LR delivery.
    pub fn forward_physical_spi(&self, spi: SpiId) -> VgicResult {
        let wake = {
            let mut state = self.inner.state.lock();
            let binding = match state.spi_backings.get(&spi).copied() {
                Some(SpiBacking::Physical(binding)) => binding,
                _ => {
                    return Err(VgicError::Unsupported {
                        operation: "forward physical SPI",
                        detail: alloc::format!("SPI {} has no physical binding", spi.raw()),
                    });
                }
            };
            state.queue_physical_spi(spi, binding)?
        };
        if let Some(wake) = wake {
            wake.wake()?;
        }
        Ok(())
    }

    pub(super) fn complete_physical_spi(
        &self,
        vcpu: GicVcpuId,
        binding: PhysicalInterruptBinding,
    ) -> VgicResult {
        backend_result(
            self.inner
                .backend
                .complete_physical_interrupt(vcpu, binding),
        )
    }

    /// Binds a guest SPI to an owned physical interrupt and fixed vCPU affinity.
    pub fn bind_physical_spi(
        &self,
        spi: SpiId,
        host: PhysicalIrqId,
        target: GicVcpuId,
    ) -> VgicResult {
        self.bind_physical_spi_with_trigger(spi, host, target, InterruptTrigger::LevelTriggered)
    }

    /// Binds a guest SPI with its immutable host trigger.
    pub fn bind_physical_spi_with_trigger(
        &self,
        spi: SpiId,
        host: PhysicalIrqId,
        target: GicVcpuId,
        trigger: InterruptTrigger,
    ) -> VgicResult {
        let affinity = {
            let state = self.inner.state.lock();
            state
                .redistributors
                .get(&target)
                .map(RedistributorState::affinity)
                .ok_or_else(|| VgicError::ResourceNotFound {
                    resource: alloc::format!("vCPU {}", target.raw()),
                    operation: "bind physical SPI",
                })?
        };
        let binding =
            PhysicalInterruptBinding::new(IntId::Spi(spi), host, target, affinity, trigger);
        {
            let mut state = self.inner.state.lock();
            if state.spi_backings.contains_key(&spi) {
                return Err(VgicError::ResourceConflict {
                    resource: "GICv3 SPI backing",
                    detail: alloc::format!("guest SPI {} already has a backing", spi.raw()),
                });
            }
            if state
                .spi_backings
                .values()
                .any(|existing| matches!(existing, SpiBacking::Physical(binding) if binding.host() == host))
            {
                return Err(VgicError::ResourceConflict {
                    resource: "physical interrupt",
                    detail: alloc::format!("host interrupt {} is already owned", host.raw()),
                });
            }
            state.distributor.claim_physical_spi(spi, affinity)?;
            if let Err(error) = state
                .distributor
                .set_trigger(spi, crate::core::trigger_mode(trigger))
            {
                if let Err(rollback_error) = state.distributor.release_spi_claim(spi) {
                    log::warn!(
                        "failed to roll back SPI ownership after trigger error: {rollback_error}"
                    );
                }
                return Err(error);
            }
            state
                .spi_backings
                .insert(spi, SpiBacking::Physical(binding));
            state.physical_spi_acknowledged.insert(spi, false);
        }
        if let Err(error) = backend_result(self.inner.backend.bind_physical_interrupt(binding)) {
            let mut state = self.inner.state.lock();
            state.spi_backings.remove(&spi);
            state.physical_spi_acknowledged.remove(&spi);
            if let Err(rollback_error) = state.distributor.release_spi_claim(spi) {
                log::warn!(
                    "failed to roll back SPI ownership after backend error: {rollback_error}"
                );
            }
            return Err(error);
        }
        Ok(())
    }

    /// Releases one quiescent physical SPI binding.
    ///
    /// A loaded vCPU or an acknowledged/in-flight delivery must be completed
    /// before releasing the host source. Backend failure leaves the complete
    /// canonical binding and distributor claim intact so callers can retry.
    pub fn unbind_physical_spi(&self, spi: SpiId) -> VgicResult {
        let binding = {
            let mut state = self.inner.state.lock();
            let binding = match state.spi_backings.get(&spi).copied() {
                Some(SpiBacking::Physical(binding)) => binding,
                _ => {
                    return Err(VgicError::ResourceNotFound {
                        resource: alloc::format!("physical backing for SPI {}", spi.raw()),
                        operation: "unbind physical SPI",
                    });
                }
            };
            state.ensure_physical_spi_is_quiescent(spi, binding)?;
            if !state.releasing_physical_spis.insert(spi) {
                return Err(VgicError::ResourceConflict {
                    resource: "physical interrupt release",
                    detail: alloc::format!("guest SPI {} is already being released", spi.raw()),
                });
            }
            binding
        };

        if let Err(error) = backend_result(self.inner.backend.unbind_physical_interrupt(binding)) {
            self.inner.state.lock().releasing_physical_spis.remove(&spi);
            return Err(error);
        }

        let mut state = self.inner.state.lock();
        state.spi_backings.remove(&spi);
        state.physical_spi_acknowledged.remove(&spi);
        state.releasing_physical_spis.remove(&spi);
        state.distributor.release_spi_claim(spi)
    }

    /// Retires and releases one physical SPI while tearing down a stopped VM.
    ///
    /// Unlike [`Self::unbind_physical_spi`], this operation may discard an
    /// acknowledged or in-flight guest delivery. No vCPU interface may be
    /// loaded. The physical source is masked before canonical state is
    /// retired, and the backend restores host ownership only after any
    /// outstanding activation has been explicitly deactivated.
    ///
    /// If backend unbind fails after deactivation, the binding and claim stay
    /// owned but their delivery state remains quiescent, so retrying this
    /// method never deactivates the same activation twice.
    pub fn teardown_physical_spi(&self, spi: SpiId) -> VgicResult {
        let (binding, needs_deactivation) = {
            let mut state = self.inner.state.lock();
            let binding = match state.spi_backings.get(&spi).copied() {
                Some(SpiBacking::Physical(binding)) => binding,
                _ => {
                    return Err(VgicError::ResourceNotFound {
                        resource: alloc::format!("physical backing for SPI {}", spi.raw()),
                        operation: "tear down physical SPI",
                    });
                }
            };
            if !state.active_vcpus.is_empty() {
                return Err(VgicError::InvalidStateTransition {
                    intid: IntId::Spi(spi),
                    operation: "tear down physical SPI",
                    detail: "one or more virtual CPU interfaces are still loaded".into(),
                });
            }
            if !state.releasing_physical_spis.insert(spi) {
                return Err(VgicError::ResourceConflict {
                    resource: "physical interrupt release",
                    detail: alloc::format!("guest SPI {} is already being released", spi.raw()),
                });
            }
            (binding, state.physical_spi_has_delivery(spi, binding))
        };

        if let Err(error) = backend_result(
            self.inner
                .backend
                .set_physical_interrupt_enabled(binding, false),
        ) {
            self.inner.state.lock().releasing_physical_spis.remove(&spi);
            return Err(error);
        }
        if needs_deactivation
            && let Err(error) = backend_result(
                self.inner
                    .backend
                    .deactivate_physical_interrupt(binding.target(), binding),
            )
        {
            self.inner.state.lock().releasing_physical_spis.remove(&spi);
            return Err(error);
        }

        // There are no loaded vCPUs and `releasing_physical_spis` rejects new
        // forwards, so the canonical delivery cannot change while it is
        // retired. Commit this before unbind: if unbind fails, a retry sees a
        // quiescent binding and must not issue DIR for the same activation.
        self.inner
            .state
            .lock()
            .clear_physical_spi_delivery(spi, binding);

        if let Err(error) = backend_result(self.inner.backend.unbind_physical_interrupt(binding)) {
            self.inner.state.lock().releasing_physical_spis.remove(&spi);
            return Err(error);
        }

        let mut state = self.inner.state.lock();
        state.spi_backings.remove(&spi);
        state.physical_spi_acknowledged.remove(&spi);
        state.releasing_physical_spis.remove(&spi);
        state.distributor.release_spi_claim(spi)
    }

    pub(super) fn apply_physical_interrupt_state_changes(
        &self,
        changes: Vec<PhysicalInterruptStateChange>,
    ) -> VgicResult {
        for (applied, change) in changes.iter().enumerate() {
            if let Err(error) = self.transition_physical_interrupt(change) {
                for completed in changes[..applied].iter().rev() {
                    if let Err(rollback_error) =
                        self.transition_physical_interrupt(&PhysicalInterruptStateChange {
                            spi: completed.spi,
                            binding: completed.binding,
                            previous: completed.current,
                            current: completed.previous,
                        })
                    {
                        log::warn!(
                            "failed to roll back physical interrupt state for {:?}: \
                             {rollback_error}",
                            completed.binding.host()
                        );
                    }
                }
                if let Err(rollback_error) = self
                    .inner
                    .state
                    .lock()
                    .restore_physical_interrupt_state_changes(&changes)
                {
                    log::warn!(
                        "failed to restore GICv3 physical SPI state after backend error: \
                         {rollback_error}"
                    );
                }
                return backend_result(Err(error));
            }
        }
        Ok(())
    }

    fn transition_physical_interrupt(
        &self,
        change: &PhysicalInterruptStateChange,
    ) -> Result<(), crate::GicV3BackendError> {
        let previous = change.previous.delivery_enabled();
        let current = change.current.delivery_enabled();
        if previous != current
            && let Err(error) = self
                .inner
                .backend
                .set_physical_interrupt_enabled(change.binding, current)
        {
            let _ = self
                .inner
                .backend
                .set_physical_interrupt_enabled(change.binding, previous);
            return Err(error);
        }
        Ok(())
    }

    /// Binds one guest MSI translation to VM-owned physical ITS resources.
    pub fn bind_physical_msi(
        &self,
        device: ItsDeviceId,
        event: EventId,
        lpi: LpiId,
        target: GicVcpuId,
    ) -> VgicResult {
        self.bind_physical_msi_for(ItsId::new(0), device, event, lpi, target)
    }

    /// Binds one guest MSI translation in a specific ITS namespace.
    pub fn bind_physical_msi_for(
        &self,
        its: ItsId,
        device: ItsDeviceId,
        event: EventId,
        lpi: LpiId,
        target: GicVcpuId,
    ) -> VgicResult {
        if !self
            .inner
            .config
            .its_instances()
            .iter()
            .any(|(configured, _)| *configured == its)
        {
            return Err(VgicError::Unsupported {
                operation: "bind physical MSI",
                detail: alloc::format!("this controller has no assigned ITS {its:?} resources"),
            });
        }
        if lpi.raw() > self.inner.config.lpi_limit() {
            return Err(VgicError::InvalidIntId { raw: lpi.raw() });
        }
        let affinity = {
            let state = self.inner.state.lock();
            state
                .redistributors
                .get(&target)
                .map(RedistributorState::affinity)
                .ok_or_else(|| VgicError::ResourceNotFound {
                    resource: alloc::format!("vCPU {}", target.raw()),
                    operation: "bind physical MSI",
                })?
        };
        let binding = PhysicalMsiBinding::new(its, device, event, lpi, target, affinity);
        {
            let mut state = self.inner.state.lock();
            if state.msi_backings.contains_key(&(its, device, event)) {
                return Err(VgicError::ResourceConflict {
                    resource: "GICv3 MSI backing",
                    detail: alloc::format!(
                        "MSI event ({}, {}) already has a backing",
                        device.raw(),
                        event.raw()
                    ),
                });
            }
            if state
                .msi_backings
                .values()
                .any(|existing| matches!(existing, MsiBacking::Physical(binding) if binding.lpi() == lpi))
            {
                return Err(VgicError::ResourceConflict {
                    resource: "physical LPI",
                    detail: alloc::format!("LPI {} is already owned", lpi.raw()),
                });
            }
            state
                .msi_backings
                .insert((its, device, event), MsiBacking::Physical(binding));
        }
        if let Err(error) = backend_result(self.inner.backend.bind_physical_msi(binding)) {
            self.inner
                .state
                .lock()
                .msi_backings
                .remove(&(its, device, event));
            return Err(error);
        }
        Ok(())
    }
}

impl ControllerState {
    fn physical_spi_has_delivery(&self, spi: SpiId, binding: PhysicalInterruptBinding) -> bool {
        self.physical_spi_acknowledged
            .get(&spi)
            .copied()
            .unwrap_or(false)
            || self.redistributors.values().any(|redistributor| {
                redistributor.has_physical_delivery(IntId::Spi(spi), binding.host())
            })
            || self
                .distributor
                .interrupt(spi)
                .is_ok_and(crate::InterruptRecord::has_delivery_state)
    }

    fn clear_physical_spi_delivery(&mut self, spi: SpiId, binding: PhysicalInterruptBinding) {
        *self
            .physical_spi_acknowledged
            .get_mut(&spi)
            .expect("an owned physical SPI must have acknowledgement state") = false;
        for redistributor in self.redistributors.values_mut() {
            redistributor.remove_physical_delivery(IntId::Spi(spi), binding.host());
        }
        self.distributor
            .interrupt_mut(spi)
            .expect("an owned physical SPI must have a Distributor record")
            .clear_delivery_state();
    }

    fn ensure_physical_spi_is_quiescent(
        &self,
        spi: SpiId,
        binding: PhysicalInterruptBinding,
    ) -> VgicResult {
        if !self.active_vcpus.is_empty() {
            return Err(VgicError::InvalidStateTransition {
                intid: IntId::Spi(spi),
                operation: "unbind physical SPI",
                detail: "one or more virtual CPU interfaces are still loaded".into(),
            });
        }
        if self.physical_spi_has_delivery(spi, binding) {
            return Err(VgicError::InvalidStateTransition {
                intid: IntId::Spi(spi),
                operation: "unbind physical SPI",
                detail: "an acknowledged or in-flight physical delivery is still pending".into(),
            });
        }
        Ok(())
    }

    pub(super) fn physical_interrupt_snapshot(&self) -> VgicResult<Vec<PhysicalInterruptSnapshot>> {
        self.spi_backings
            .iter()
            .filter_map(|(spi, backing)| match backing {
                SpiBacking::Software => None,
                SpiBacking::Physical(binding) => Some((*spi, *binding)),
            })
            .map(|(spi, binding)| {
                Ok(PhysicalInterruptSnapshot {
                    spi,
                    binding,
                    state: self.physical_interrupt_state(spi)?,
                })
            })
            .collect()
    }

    pub(super) fn physical_interrupt_state_changes(
        &self,
        snapshots: &[PhysicalInterruptSnapshot],
    ) -> VgicResult<Vec<PhysicalInterruptStateChange>> {
        let mut changes = Vec::new();
        for snapshot in snapshots {
            let current = self.physical_interrupt_state(snapshot.spi)?;
            if current.delivery_enabled() != snapshot.state.delivery_enabled() {
                changes.push(PhysicalInterruptStateChange {
                    spi: snapshot.spi,
                    binding: snapshot.binding,
                    previous: snapshot.state,
                    current,
                });
            }
        }
        Ok(changes)
    }

    fn physical_interrupt_state(&self, spi: SpiId) -> VgicResult<PhysicalInterruptState> {
        let interrupt = self.distributor.interrupt(spi)?;
        Ok(PhysicalInterruptState {
            // The host source must stay masked unless both architectural
            // gates exposed to the guest are open. Otherwise a level SPI can
            // enter the host while GICD_CTLR disables guest delivery, fail to
            // acquire a hardware-backed LR, and immediately retrigger.
            distributor_enabled: self.distributor.enabled(),
            interrupt_enabled: interrupt.enabled(),
        })
    }

    fn restore_physical_interrupt_state_changes(
        &mut self,
        changes: &[PhysicalInterruptStateChange],
    ) -> VgicResult {
        for change in changes {
            self.distributor
                .set_enabled_for_rollback(change.previous.distributor_enabled);
            let interrupt = self.distributor.interrupt_mut(change.spi)?;
            interrupt.set_enabled(change.previous.interrupt_enabled);
        }
        Ok(())
    }
}

impl Drop for ControllerInner {
    fn drop(&mut self) {
        let (interrupts, retained_interrupts, msi) = {
            let state = self.state.lock();
            (
                state
                    .spi_backings
                    .iter()
                    .filter_map(|(spi, backing)| match backing {
                        SpiBacking::Physical(binding)
                            if state
                                .ensure_physical_spi_is_quiescent(*spi, *binding)
                                .is_ok() =>
                        {
                            Some(*binding)
                        }
                        SpiBacking::Software | SpiBacking::Physical(_) => None,
                    })
                    .collect::<Vec<_>>(),
                state
                    .spi_backings
                    .iter()
                    .filter_map(|(spi, backing)| match backing {
                        SpiBacking::Physical(binding)
                            if state
                                .ensure_physical_spi_is_quiescent(*spi, *binding)
                                .is_err() =>
                        {
                            Some(*binding)
                        }
                        SpiBacking::Software | SpiBacking::Physical(_) => None,
                    })
                    .collect::<Vec<_>>(),
                state
                    .msi_backings
                    .values()
                    .filter_map(|backing| match backing {
                        MsiBacking::Software { .. } => None,
                        MsiBacking::Physical(binding) => Some(*binding),
                    })
                    .collect::<Vec<_>>(),
            )
        };
        for binding in retained_interrupts {
            log::warn!(
                "leaving active physical interrupt {} bound because VGIC teardown was not \
                 completed explicitly",
                binding.host().raw()
            );
        }
        for binding in interrupts {
            if let Err(error) = self.backend.unbind_physical_interrupt(binding) {
                log::warn!("failed to release physical interrupt binding: {error}");
            }
        }
        for binding in msi {
            if let Err(error) = self.backend.unbind_physical_msi(binding) {
                log::warn!("failed to release physical MSI binding: {error}");
            }
        }
    }
}