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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

use bitfield_struct::bitfield;
use chipset_device::interrupt::AcknowledgePicInterrupt;
use chipset_device::interrupt::LineInterruptTarget;
use chipset_device::io::IoError;
use chipset_device::io::IoResult;
use chipset_device::pio::ControlPortIoIntercept;
use chipset_device::pio::PortIoIntercept;
use chipset_device::pio::RegisterPortIoIntercept;
use chipset_device::ChipsetDevice;
use inspect::Inspect;
use inspect::InspectMut;
use inspect_counters::Counter;
use std::num::Wrapping;
use vmcore::device_state::ChangeDeviceState;
use vmcore::line_interrupt::LineInterrupt;

const PRIMARY_PIC_COMMAND_PORT: u16 = 0x20;
const PRIMARY_PIC_DATA_PORT: u16 = 0x21;
const SECONDARY_PIC_COMMAND_PORT: u16 = 0xa0;
const SECONDARY_PIC_DATA_PORT: u16 = 0xa1;
const PRIMARY_PIC_ELCR_PORT: u16 = 0x4d0;
const SECONDARY_PIC_ELCR_PORT: u16 = 0x4d1;

// x86 standard dictates we must use IRQ2 for cross-PIC communication.
const PIC_CHAIN_COMMUNICATION_IRQ: u8 = 2;

/// Mask for the IRQ vector within an individual PIC.
const IRQ_MASK: u8 = 0b111;

/// The spurious IRQ offset within an individual PIC.
const SPURIOUS_IRQ: u8 = 7;

#[derive(InspectMut)]
pub struct DualPic {
    // Runtime glue
    ready: LineInterrupt,
    #[inspect(iter_by_index)]
    port_io_regions: [Box<dyn ControlPortIoIntercept>; 3],

    // Runtime book-keeping
    stats: DualPicStats,

    // Volatile state
    #[inspect(flatten, with = r#"|x| inspect::iter_by_index(x).prefix("pic")"#)]
    pics: [Pic; 2],
}

#[derive(Inspect, Default)]
struct DualPicStats {
    #[inspect(iter_by_index)]
    interrupts_per_irq: [Counter; 16],
    interrupts: Counter,
}

impl DualPic {
    pub fn new(ready: LineInterrupt, port_io: &mut dyn RegisterPortIoIntercept) -> Self {
        let mut primary_region = port_io.new_io_region(
            "primary",
            (PRIMARY_PIC_COMMAND_PORT..=PRIMARY_PIC_DATA_PORT).len() as u16,
        );
        let mut secondary_region = port_io.new_io_region(
            "secondary",
            (SECONDARY_PIC_COMMAND_PORT..=SECONDARY_PIC_DATA_PORT).len() as u16,
        );
        let mut elcr_region = port_io.new_io_region(
            "edge_level_control",
            (PRIMARY_PIC_ELCR_PORT..=SECONDARY_PIC_ELCR_PORT).len() as u16,
        );

        primary_region.map(PRIMARY_PIC_COMMAND_PORT);
        secondary_region.map(SECONDARY_PIC_COMMAND_PORT);
        elcr_region.map(PRIMARY_PIC_ELCR_PORT);

        DualPic {
            pics: [Pic::new(true), Pic::new(false)],
            ready,
            port_io_regions: [primary_region, secondary_region, elcr_region],
            stats: Default::default(),
        }
    }

    /// Sync the interrupt outputs of each PIC with their connection (either the
    /// primary PIC or the CPU).
    fn sync_outputs(&mut self) {
        self.pics[0].set_irq(
            PIC_CHAIN_COMMUNICATION_IRQ,
            self.pics[1].interrupt_pending(),
        );
        self.ready.set_level(self.pics[0].interrupt_pending());
    }

    fn set_irq(&mut self, n: u8, high: bool) {
        if n >= 8 {
            self.pics[1].set_irq(n - 8, high);
        } else {
            self.pics[0].set_irq(n, high);
        }
        self.sync_outputs();
    }
}

impl AcknowledgePicInterrupt for DualPic {
    fn acknowledge_interrupt(&mut self) -> Option<u8> {
        let (requested, n) = self.pics[0].acknowledge_interrupt(&mut self.stats);
        let irq = requested.then(|| {
            if n & IRQ_MASK == PIC_CHAIN_COMMUNICATION_IRQ {
                let (requested, m) = self.pics[1].acknowledge_interrupt(&mut self.stats);
                assert!(requested, "pic1 ready was set");
                m
            } else {
                n
            }
        });
        // Sync to ensure the IRQ2 line gets lowered while the secondary PIC's
        // interrupt is in service.
        self.sync_outputs();
        irq
    }
}

impl LineInterruptTarget for DualPic {
    fn set_irq(&mut self, n: u32, high: bool) {
        self.set_irq(n as u8, high);
    }

    fn valid_lines(&self) -> &[std::ops::RangeInclusive<u32>] {
        // IRQ2 is used to cascade the secondary PIC to the primary PIC, so it
        // is not available for use.
        &[0..=1, 3..=15]
    }
}

impl ChangeDeviceState for DualPic {
    fn start(&mut self) {}

    async fn stop(&mut self) {}

    async fn reset(&mut self) {
        for pic in &mut self.pics {
            pic.reset(false);
        }
        self.sync_outputs();
    }
}

impl ChipsetDevice for DualPic {
    fn supports_pio(&mut self) -> Option<&mut dyn PortIoIntercept> {
        Some(self)
    }

    fn supports_line_interrupt_target(&mut self) -> Option<&mut dyn LineInterruptTarget> {
        Some(self)
    }

    fn supports_acknowledge_pic_interrupt(&mut self) -> Option<&mut dyn AcknowledgePicInterrupt> {
        Some(self)
    }
}

impl PortIoIntercept for DualPic {
    fn io_read(&mut self, io_port: u16, data: &mut [u8]) -> IoResult {
        if data.len() != 1 {
            return IoResult::Err(IoError::InvalidAccessSize);
        }
        data[0] = match io_port {
            PRIMARY_PIC_COMMAND_PORT => self.pics[0].read_command(&mut self.stats),
            PRIMARY_PIC_DATA_PORT => self.pics[0].read_data(),
            SECONDARY_PIC_COMMAND_PORT => self.pics[1].read_command(&mut self.stats),
            SECONDARY_PIC_DATA_PORT => self.pics[1].read_data(),
            PRIMARY_PIC_ELCR_PORT => self.pics[0].elcr,
            SECONDARY_PIC_ELCR_PORT => self.pics[1].elcr,
            _ => return IoResult::Err(IoError::InvalidRegister),
        };
        IoResult::Ok
    }

    fn io_write(&mut self, io_port: u16, data: &[u8]) -> IoResult {
        // Hyper-V-specific optimization: two-byte write to the command
        // port can be used to write to both command ports in a single
        // instruction.
        if data.len() == 2
            && (io_port == PRIMARY_PIC_COMMAND_PORT || io_port == SECONDARY_PIC_COMMAND_PORT)
        {
            let &[mut prim, mut sec] = data else {
                unreachable!()
            };
            if io_port == SECONDARY_PIC_COMMAND_PORT {
                (prim, sec) = (sec, prim);
            }
            self.pics[0].write_command(prim);
            self.pics[1].write_command(sec);
            self.sync_outputs();
            return IoResult::Ok;
        }

        if data.len() != 1 {
            return IoResult::Err(IoError::InvalidAccessSize);
        }

        match io_port {
            PRIMARY_PIC_COMMAND_PORT => self.pics[0].write_command(data[0]),
            PRIMARY_PIC_DATA_PORT => self.pics[0].write_data(data[0]),
            SECONDARY_PIC_COMMAND_PORT => self.pics[1].write_command(data[0]),
            SECONDARY_PIC_DATA_PORT => self.pics[1].write_data(data[0]),
            PRIMARY_PIC_ELCR_PORT => self.pics[0].elcr = data[0],
            SECONDARY_PIC_ELCR_PORT => self.pics[1].elcr = data[0],
            _ => return IoResult::Err(IoError::InvalidRegister),
        }

        self.sync_outputs();
        IoResult::Ok
    }
}

/// The PIC is controlled through two categories of commands:
/// ICWs: Initialization Control Words
/// OCWs: Operation Command Words
#[derive(Debug, Copy, Clone, Inspect)]
struct Pic {
    /// Our current stage of the initialization process.
    init: InitStage,

    /// Whether or not this PIC is the primary in the dual-pic chain.
    #[inspect(skip)]
    primary: bool,

    /// The Interrupt Vector base address, passed in ICW2.
    /// The least significant 3 bits must be 0.
    #[inspect(hex)]
    icw2: u8,

    /// Interrupt Mask Register, also called OCW1
    #[inspect(binary)]
    imr: u8,

    /// The most recently received OCW3
    ocw3: Ocw3,

    /// Interrupt Service Register
    #[inspect(binary)]
    isr: u8,

    /// Edge/Level Control Register
    #[inspect(binary)]
    elcr: u8,

    /// The current status of all the interrupt request lines
    #[inspect(binary)]
    lines: u8,

    /// Whether the line has been low since the last interrupt injection. Used
    /// to track whether an edge-triggered interrupt should be requested when
    /// its line is high.
    #[inspect(binary)]
    line_low_latch: u8,
}

#[derive(Copy, Clone, Debug, Inspect)]
enum InitStage {
    Uninitialized,
    ExpectingIcw2,
    ExpectingIcw3,
    ExpectingIcw4,
    Initialized,
}

impl Pic {
    fn new(primary: bool) -> Self {
        Pic {
            imr: 0,
            init: InitStage::Uninitialized,
            // This is a hack to help set_irq_in_service disambiguate pics before initialization.
            // Set this back to always 0 when that goes away.
            icw2: if primary { 0 } else { 8 },
            ocw3: Ocw3(0),
            isr: 0,
            elcr: 0,
            primary,
            lines: 0,
            line_low_latch: !0,
        }
    }

    /// Resets the PIC state, preserving the line state. If `during_icw1`,
    /// preserve ELCR.
    fn reset(&mut self, during_icw1: bool) {
        *self = Self {
            lines: self.lines,
            elcr: if during_icw1 { self.elcr } else { 0 },
            ..Self::new(self.primary)
        };
    }

    fn irr(&self) -> u8 {
        self.lines & (self.elcr | self.line_low_latch)
    }

    fn set_irq(&mut self, n: u8, high: bool) {
        let bit = 1 << n;
        if high {
            if !matches!(self.init, InitStage::Initialized) {
                tracelimit::warn_ratelimited!(
                    primary = self.primary,
                    ?n,
                    "interrupt request sent to uninitialized PIC"
                );
            }
            self.lines |= bit;
        } else {
            self.lines &= !bit;
            self.line_low_latch |= bit;
        }
    }

    fn ready_vec(&self) -> u8 {
        // Restrict to interrupts with higher priority than the highest priority
        // IRQ already being serviced.
        let isr = Wrapping(self.isr);
        let highest_isr = isr & -isr;
        let higher_not_isr = highest_isr - Wrapping(1);
        // Restrict to requested interrupts that are not masked.
        self.irr() & !self.imr & higher_not_isr.0
    }

    fn interrupt_pending(&self) -> bool {
        self.ready_vec() != 0
    }

    fn pending_line(&self) -> Option<u8> {
        let m = self.ready_vec();
        if m != 0 {
            // Find the highest priority IRQ
            Some(m.trailing_zeros() as u8)
        } else {
            None
        }
    }

    fn acknowledge_interrupt(&mut self, stats: &mut DualPicStats) -> (bool, u8) {
        if !matches!(self.init, InitStage::Initialized) {
            tracelimit::warn_ratelimited!(
                primary = self.primary,
                "interrupt servicing sent to uninitialized PIC"
            );
        }

        let (requested, irq) = if let Some(n) = self.pending_line() {
            let bit = 1 << n;
            // Clear the edge latch so that the line must be low again before
            // another interrupt is injected.
            self.line_low_latch &= !bit;
            // Set in service.
            self.isr |= bit;
            stats.interrupts.increment();
            stats.interrupts_per_irq[if self.primary { 0 } else { 8 } + n as usize].increment();
            (true, n)
        } else {
            // spurious interrupt
            (false, SPURIOUS_IRQ)
        };

        // Combine the IRQ with the base address to construct the full interrupt
        // service routine address.
        assert!(self.icw2 & IRQ_MASK == 0);
        (requested, self.icw2 | irq)
    }

    fn eoi(&mut self, n: Option<u8>) {
        tracing::trace!(primary = self.primary, n, "eoi");
        let bit = match n {
            Some(level) => 1 << level,
            // On non-specific EOIs, find the highest priority interrupt in service
            None => self.isr & self.isr.wrapping_neg(),
        };

        self.isr &= !bit;
    }

    fn read_command(&mut self, stats: &mut DualPicStats) -> u8 {
        if self.ocw3.p() {
            self.ocw3.set_p(false);
            let (int, irq) = self.acknowledge_interrupt(stats);
            ((int as u8) << 7) | (irq & IRQ_MASK)
        } else if self.ocw3.rr() {
            if self.ocw3.ris() {
                self.isr
            } else {
                self.irr()
            }
        } else {
            0
        }
    }

    fn read_data(&self) -> u8 {
        self.imr
    }

    fn write_command(&mut self, data: u8) {
        const INIT_BIT: u8 = 0b10000;
        const COMMAND_BIT: u8 = 0b1000;

        if data & INIT_BIT != 0 {
            // ICW1
            // We do not support Single PIC mode or Level Triggered mode.
            // We require IC4 to be set so we can be put in x86 mode in ICW4.
            if data != 0b00010001 {
                tracelimit::error_ratelimited!(primary = self.primary, ?data, "unsupported ICW1");
            }

            self.reset(true);
            self.init = InitStage::ExpectingIcw2;
        } else {
            if !matches!(self.init, InitStage::Initialized) {
                tracelimit::warn_ratelimited!(
                    primary = self.primary,
                    ?data,
                    "OCW sent to uninitialized PIC"
                );
            }
            if data & COMMAND_BIT == 0 {
                let ocw2 = Ocw2(data);

                match (ocw2.r(), ocw2.sl(), ocw2.eoi()) {
                    (true, _, _) | (false, false, false) => {
                        tracelimit::error_ratelimited!(
                            primary = self.primary,
                            ?data,
                            "unsupported OCW2"
                        )
                    }
                    (false, true, true) => self.eoi(Some(ocw2.level())),
                    (false, false, true) => self.eoi(None),
                    (false, true, false) => {} // No-op
                }
            } else {
                self.ocw3 = Ocw3(data);
                // We do not support Special Mask Mode.
                if self.ocw3.esmm() || self.ocw3.smm() {
                    tracelimit::error_ratelimited!(
                        primary = self.primary,
                        ?data,
                        "unsupported OCW3"
                    );
                }
            }
        }
    }

    fn write_data(&mut self, data: u8) {
        match self.init {
            InitStage::Uninitialized | InitStage::Initialized => {
                self.imr = data; // OCW1
            }
            InitStage::ExpectingIcw2 => {
                if data & IRQ_MASK != 0 {
                    tracelimit::error_ratelimited!(primary = self.primary, ?data, "invalid ICW2");
                }
                self.icw2 = data & !IRQ_MASK;
                self.init = InitStage::ExpectingIcw3;
            }
            InitStage::ExpectingIcw3 => {
                // x86 standard dictates we must use IRQ2 for cross-PIC communication.
                if self.primary {
                    if data != (1 << PIC_CHAIN_COMMUNICATION_IRQ) {
                        tracelimit::error_ratelimited!(
                            primary = self.primary,
                            ?data,
                            "invalid primary ICW3"
                        );
                    }
                } else {
                    if data != PIC_CHAIN_COMMUNICATION_IRQ {
                        tracelimit::error_ratelimited!(
                            primary = self.primary,
                            ?data,
                            "invalid secondary ICW3"
                        );
                    }
                }

                self.init = InitStage::ExpectingIcw4;
            }
            InitStage::ExpectingIcw4 => {
                // We do not support any advanced operating modes controlled by ICW4.
                // We require being put into x86 mode.
                if data != 1 {
                    // Linux sends an ICW4 of 3 during boot, then very quickly overwrites it
                    if data == 3 {
                        tracing::debug!(
                            primary = self.primary,
                            "got ICW4 of 3, this is expected for Linux boot but not any other time"
                        );
                    } else {
                        tracelimit::error_ratelimited!(
                            primary = self.primary,
                            ?data,
                            "unsupported ICW4"
                        );
                    }
                };
                self.init = InitStage::Initialized;
            }
        }
    }
}

#[bitfield(u8)]
/// Operation Command Word 2
struct Ocw2 {
    /// Interrupt level
    #[bits(3)]
    level: u8,

    #[bits(2)]
    _command: u8,

    /// End of Interrupt
    eoi: bool,

    /// Selection
    sl: bool,

    /// Rotation
    r: bool,
}

#[derive(Inspect)]
#[bitfield(u8)]
/// Operation Command Word 3
struct Ocw3 {
    /// Read Selector
    ris: bool,

    /// Read Register
    rr: bool,

    /// Polling
    p: bool,

    #[bits(2)]
    _command: u8,

    /// Special Mask Mode
    smm: bool,

    /// Enable Special Mask Mode
    esmm: bool,

    _reserved: bool,
}

mod save_restore {
    use super::DualPic;
    use super::InitStage;
    use super::Ocw3;
    use super::Pic;
    use super::IRQ_MASK;
    use thiserror::Error;
    use vmcore::save_restore::RestoreError;
    use vmcore::save_restore::SaveError;
    use vmcore::save_restore::SaveRestore;

    mod state {
        use mesh::payload::Protobuf;
        use vmcore::save_restore::SavedStateRoot;

        #[derive(Protobuf, SavedStateRoot)]
        #[mesh(package = "chipset.pic")]
        pub struct SavedState {
            #[mesh(1)]
            pub(super) primary: SavedPic,
            #[mesh(2)]
            pub(super) secondary: SavedPic,
        }

        #[derive(Protobuf)]
        #[mesh(package = "chipset.pic")]
        pub struct SavedPic {
            #[mesh(1)]
            pub init: SavedInitStage,
            #[mesh(2)]
            pub icw2: u8,
            #[mesh(3)]
            pub imr: u8,
            #[mesh(4)]
            pub ocw3: u8,
            #[mesh(5)]
            pub isr: u8,
            #[mesh(6)]
            pub elcr: u8,
            #[mesh(7)]
            pub line_low_latch: u8,
        }

        #[derive(Protobuf)]
        #[mesh(package = "chipset.pic")]
        pub enum SavedInitStage {
            #[mesh(1)]
            Uninitialized,
            #[mesh(2)]
            ExpectingIcw2,
            #[mesh(3)]
            ExpectingIcw3,
            #[mesh(4)]
            ExpectingIcw4,
            #[mesh(5)]
            Initialized,
        }
    }

    #[derive(Debug, Error)]
    enum Error {
        #[error("invalid icw2 value {0:#x}")]
        InvalidIcw2(u8),
    }

    impl state::SavedPic {
        fn restore(self, pic: &mut Pic) -> Result<(), Error> {
            let Self {
                init,
                icw2,
                imr,
                ocw3,
                isr,
                elcr,
                line_low_latch,
            } = self;
            pic.init = match init {
                state::SavedInitStage::Uninitialized => InitStage::Uninitialized,
                state::SavedInitStage::ExpectingIcw2 => InitStage::ExpectingIcw2,
                state::SavedInitStage::ExpectingIcw3 => InitStage::ExpectingIcw3,
                state::SavedInitStage::ExpectingIcw4 => InitStage::ExpectingIcw4,
                state::SavedInitStage::Initialized => InitStage::Initialized,
            };
            if icw2 & IRQ_MASK != 0 {
                return Err(Error::InvalidIcw2(icw2));
            }
            pic.icw2 = icw2;
            pic.imr = imr;
            pic.ocw3 = Ocw3(ocw3);
            pic.isr = isr;
            pic.elcr = elcr;
            pic.line_low_latch = line_low_latch;
            Ok(())
        }

        fn save(pic: &Pic) -> Self {
            let &Pic {
                init,
                primary: _,
                icw2,
                imr,
                ocw3,
                isr,
                elcr,
                lines: _,
                line_low_latch,
            } = pic;
            Self {
                init: match init {
                    InitStage::Uninitialized => state::SavedInitStage::Uninitialized,
                    InitStage::ExpectingIcw2 => state::SavedInitStage::ExpectingIcw2,
                    InitStage::ExpectingIcw3 => state::SavedInitStage::ExpectingIcw3,
                    InitStage::ExpectingIcw4 => state::SavedInitStage::ExpectingIcw4,
                    InitStage::Initialized => state::SavedInitStage::Initialized,
                },
                icw2,
                imr,
                ocw3: ocw3.0,
                isr,
                elcr,
                line_low_latch,
            }
        }
    }

    impl SaveRestore for DualPic {
        type SavedState = state::SavedState;

        fn save(&mut self) -> Result<Self::SavedState, SaveError> {
            let Self {
                ready: _,
                stats: _,
                port_io_regions: _,
                pics: [primary, secondary],
            } = &self;

            Ok(state::SavedState {
                primary: state::SavedPic::save(primary),
                secondary: state::SavedPic::save(secondary),
            })
        }

        fn restore(&mut self, state: Self::SavedState) -> Result<(), RestoreError> {
            let state::SavedState { primary, secondary } = state;
            primary
                .restore(&mut self.pics[0])
                .map_err(|err| RestoreError::Other(err.into()))?;
            secondary
                .restore(&mut self.pics[1])
                .map_err(|err| RestoreError::Other(err.into()))?;
            self.sync_outputs();
            Ok(())
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use chipset_device::pio::ExternallyManagedPortIoIntercepts;
    use chipset_device::pio::PortIoIntercept;
    use vmcore::line_interrupt::test_helpers::TestLineInterruptTarget;

    const IV_BASE: u8 = 0x30;

    fn create_pic() -> (impl Fn() -> bool, DualPic) {
        let ready = TestLineInterruptTarget::new_arc();
        let mut pic = DualPic::new(
            LineInterrupt::new_with_target("ready", ready.clone(), 0),
            &mut ExternallyManagedPortIoIntercepts,
        );

        // Initialization sequence copied by logging what Linux Direct does:
        pic.io_write(0x20, &[0x11]).unwrap();
        pic.io_write(0x21, &[IV_BASE]).unwrap(); // Primary ICW2
        pic.io_write(0x21, &[0x04]).unwrap();
        pic.io_write(0x21, &[0x01]).unwrap();
        pic.io_write(0xa0, &[0x11]).unwrap();
        pic.io_write(0xa1, &[IV_BASE + 8]).unwrap(); // Secondary ICW2
        pic.io_write(0xa1, &[0x02]).unwrap();
        pic.io_write(0xa1, &[0x01]).unwrap();

        (move || ready.is_high(0), pic)
    }

    fn send_eoi(pic: &mut DualPic, consts: PicTestConstants) {
        let (v, _) = consts;
        match v {
            0..=7 => pic
                .io_write(
                    PRIMARY_PIC_COMMAND_PORT,
                    &[Ocw2::new().with_eoi(true).with_sl(true).with_level(v).0],
                )
                .unwrap(),
            8..=15 => {
                pic.io_write(
                    PRIMARY_PIC_COMMAND_PORT,
                    &[Ocw2::new()
                        .with_eoi(true)
                        .with_sl(true)
                        .with_level(PIC_CHAIN_COMMUNICATION_IRQ)
                        .0],
                )
                .unwrap();
                pic.io_write(
                    SECONDARY_PIC_COMMAND_PORT,
                    &[Ocw2::new().with_eoi(true).with_sl(true).with_level(v - 8).0],
                )
                .unwrap();
            }
            _ => unreachable!(),
        }
    }

    #[test]
    fn test_create_pic() {
        let (ready, pic) = create_pic();
        assert!(matches!(pic.pics[0].init, InitStage::Initialized));
        assert!(matches!(pic.pics[1].init, InitStage::Initialized));
        assert!(!ready());
    }

    // (Vector, pics index)
    type PicTestConstants = (u8, usize);
    const PRIMARY: PicTestConstants = (0, 0);
    const SECONDARY: PicTestConstants = (8, 1);

    #[test]
    fn test_edge_interrupt_primary() {
        test_edge_interrupt(PRIMARY);
    }

    #[test]
    fn test_edge_interrupt_secondary() {
        test_edge_interrupt(SECONDARY);
    }

    fn test_edge_interrupt(consts: PicTestConstants) {
        let (ready, mut pic) = create_pic();
        let (v, i) = consts;

        pic.set_irq(v, true);
        assert!(ready());
        assert!(pic.pics[i].irr() == 0b1);
        pic.set_irq(v, false);
        assert!(pic.pics[i].irr() == 0b0);
        let pending = pic.acknowledge_interrupt();
        assert!(pending.is_none());

        assert!(!ready());

        pic.set_irq(v, true);
        assert!(ready());
        assert!(pic.pics[i].irr() == 0b1);

        let pending = pic.acknowledge_interrupt();
        assert_eq!(pending, Some(IV_BASE + v));
        assert!(pic.pics[i].irr() == 0);
        assert!(pic.pics[i].isr == 0b1);

        send_eoi(&mut pic, consts);

        assert!(pic.pics[i].irr() == 0);
        assert!(pic.pics[i].isr == 0);
        let pending = pic.acknowledge_interrupt();
        assert!(pending.is_none());

        assert!(!ready());

        pic.set_irq(v, true);
        assert!(pic.pics[i].irr() == 0b0);
        pic.set_irq(v, false);
        pic.set_irq(v, true);
        assert!(pic.pics[i].irr() == 0b1);
        assert!(ready());

        pic.set_irq(v, false);
        assert!(pic.pics[i].irr() == 0b0);
    }

    #[test]
    fn test_level_interrupts_primary() {
        test_level_interrupts(PRIMARY);
    }

    #[test]
    fn test_level_interrupts_secondary() {
        test_level_interrupts(SECONDARY);
    }

    fn test_level_interrupts(consts: PicTestConstants) {
        let (ready, mut pic) = create_pic();
        let (v, i) = consts;

        pic.io_write(
            match i {
                0 => PRIMARY_PIC_ELCR_PORT,
                1 => SECONDARY_PIC_ELCR_PORT,
                _ => unreachable!(),
            },
            &[0b1],
        )
        .unwrap();

        pic.set_irq(v, true);
        assert!(ready());
        assert!(pic.pics[i].irr() == 0b1);
        pic.set_irq(v, false);
        assert!(pic.pics[i].irr() == 0b0);
        let pending = pic.acknowledge_interrupt();

        // No spurious interrupt is possible with the current interface.
        assert!(pending.is_none());

        assert!(!ready());

        pic.set_irq(v, true);
        assert!(ready());
        assert!(pic.pics[i].irr() == 0b1);

        let pending = pic.acknowledge_interrupt();
        assert_eq!(pending, Some(IV_BASE + v));
        assert!(pic.pics[i].irr() == 0b1);
        assert!(pic.pics[i].isr == 0b1);

        send_eoi(&mut pic, consts);
        assert!(pic.pics[i].irr() == 0b1);
        assert!(pic.pics[i].isr == 0);

        let pending = pic.acknowledge_interrupt();
        assert_eq!(pending, Some(IV_BASE + v));
        pic.set_irq(v, false);
        send_eoi(&mut pic, consts);
        assert!(pic.pics[i].irr() == 0);
        let pending = pic.acknowledge_interrupt();
        assert!(pending.is_none());
    }

    #[test]
    fn test_multiple_edge_interrupt_primary() {
        test_multiple_edge_interrupt(PRIMARY);
    }

    #[test]
    fn test_multiple_edge_interrupt_secondary() {
        test_multiple_edge_interrupt(SECONDARY);
    }

    fn test_multiple_edge_interrupt(consts: PicTestConstants) {
        let (ready, mut pic) = create_pic();
        let (v, i) = consts;

        pic.set_irq(v, true);
        assert!(ready());
        assert!(pic.pics[i].irr() == 0b1);
        pic.set_irq(v, false);
        assert!(pic.pics[i].irr() == 0b0);

        pic.set_irq(v + 1, true);
        assert!(pic.pics[i].irr() == 0b10);
        pic.set_irq(v + 1, false);
        assert!(pic.pics[i].irr() == 0b00);

        assert!(!ready());

        pic.set_irq(v, true);
        assert!(ready());
        assert!(pic.pics[i].irr() == 0b1);
        pic.set_irq(v + 1, true);
        assert!(pic.pics[i].irr() == 0b11);

        let pending = pic.acknowledge_interrupt();
        assert_eq!(pending, Some(IV_BASE + v));
        assert!(pic.pics[i].irr() == 0b10);
        assert!(pic.pics[i].isr == 0b01);

        send_eoi(&mut pic, consts);
        assert!(pic.pics[i].irr() == 0b10);
        assert!(pic.pics[i].isr == 0b00);
        assert!(ready());
        let pending = pic.acknowledge_interrupt();
        assert_eq!(pending, Some(IV_BASE + 1 + v));
        assert!(!ready());
    }

    #[test]
    fn test_non_specific_eois() {
        let (_, mut pic) = create_pic();

        pic.set_irq(5, true);
        assert_eq!(pic.acknowledge_interrupt(), Some(IV_BASE + 5));

        pic.set_irq(3, true);
        assert_eq!(pic.acknowledge_interrupt(), Some(IV_BASE + 3));

        pic.set_irq(1, true);
        assert_eq!(pic.acknowledge_interrupt(), Some(IV_BASE + 1));

        assert_eq!(pic.pics[0].isr, 0b101010);

        pic.io_write(
            PRIMARY_PIC_COMMAND_PORT,
            &[Ocw2::new().with_eoi(true).with_sl(false).0],
        )
        .unwrap();

        assert_eq!(pic.pics[0].isr, 0b101000);
    }
}