capsules_core/
alarm.rs

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
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
// Licensed under the Apache License, Version 2.0 or the MIT License.
// SPDX-License-Identifier: Apache-2.0 OR MIT
// Copyright Tock Contributors 2022.

//! Tock syscall driver capsule for Alarms, which issue callbacks when
//! a point in time has been reached.

use kernel::grant::{AllowRoCount, AllowRwCount, Grant, UpcallCount};
use kernel::hil::time::{self, Alarm, Ticks};
use kernel::syscall::{CommandReturn, SyscallDriver};
use kernel::{ErrorCode, ProcessId};

/// Syscall driver number.
use crate::driver;
pub const DRIVER_NUM: usize = driver::NUM::Alarm as usize;

#[derive(Copy, Clone, Debug)]
struct Expiration<T: Ticks> {
    reference: T,
    dt: T,
}

#[derive(Copy, Clone)]
pub struct AlarmData<T: Ticks> {
    expiration: Option<Expiration<T>>,
}

const ALARM_CALLBACK_NUM: usize = 0;
const NUM_UPCALLS: u8 = 1;

impl<T: Ticks> Default for AlarmData<T> {
    fn default() -> AlarmData<T> {
        AlarmData { expiration: None }
    }
}

pub struct AlarmDriver<'a, A: Alarm<'a>> {
    alarm: &'a A,
    app_alarms:
        Grant<AlarmData<A::Ticks>, UpcallCount<NUM_UPCALLS>, AllowRoCount<0>, AllowRwCount<0>>,
}

impl<'a, A: Alarm<'a>> AlarmDriver<'a, A> {
    pub const fn new(
        alarm: &'a A,
        grant: Grant<
            AlarmData<A::Ticks>,
            UpcallCount<NUM_UPCALLS>,
            AllowRoCount<0>,
            AllowRwCount<0>,
        >,
    ) -> AlarmDriver<'a, A> {
        AlarmDriver {
            alarm,
            app_alarms: grant,
        }
    }

    /// Find the earliest [`Expiration`] from an iterator of expirations.
    ///
    /// Each [`Expiration`] value is provided as a tuple, with
    /// - `UD`: an additional user-data argument returned together with the
    ///   [`Expiration`], should it be the earliest, and
    /// - `F`: a call-back function invoked when the [`Expiration`] has already
    ///   expired. The callback is porivded the expiration value and a reference
    ///   to its user-data.
    ///
    /// Whether an [`Expiration`] has expired or not is determined with respect
    /// to `now`. If `now` is in `[exp.reference; exp.reference + exp.dt)`, it
    /// the [`Expiration`] has not yet expired.
    ///
    /// An expired [`Expiration`] is not a candidate for "earliest" expiration.
    /// This means that this function will return `Ok(None)` if it receives an
    /// empty iterator, or all [`Expiration`]s have expired.
    ///
    /// To stop iteration on any expired [`Expiration`], its callback can return
    /// `Some(R)`. Then this function will return `Err(Expiration, UD, R)`.
    /// This avoids consuming the entire iterator.
    fn earliest_alarm<UD, R, F: FnOnce(Expiration<A::Ticks>, &UD) -> Option<R>>(
        now: A::Ticks,
        expirations: impl Iterator<Item = (Expiration<A::Ticks>, UD, F)>,
    ) -> Result<Option<(Expiration<A::Ticks>, UD)>, (Expiration<A::Ticks>, UD, R)> {
        let mut earliest: Option<(Expiration<A::Ticks>, UD)> = None;

        for (exp, ud, expired_handler) in expirations {
            let Expiration {
                reference: exp_ref,
                dt: exp_dt,
            } = exp;

            // Pre-compute the absolute "end" time of this expiration (the
            // point at which it should fire):
            let exp_end = exp_ref.wrapping_add(exp_dt);

            // If `now` is not within `[reference, reference + dt)`, this
            // alarm has expired. Call the expired handler. If it returns
            // false, stop here.
            if !now.within_range(exp_ref, exp_end) {
                let expired_handler_res = expired_handler(exp, &ud);
                if let Some(retval) = expired_handler_res {
                    return Err((exp, ud, retval));
                }
            }

            // `exp` has not yet expired. At this point we can assume that
            // `now` is within `[exp_ref, exp_end)`. Check whether it will
            // expire earlier than the current `earliest`:
            match &earliest {
                None => {
                    // Do not have an earliest expiration yet, set this
                    // expriation as earliest:
                    earliest = Some((exp, ud));
                }

                Some((
                    Expiration {
                        reference: earliest_ref,
                        dt: earliest_dt,
                    },
                    _,
                )) => {
                    // As `now` is within `[ref, end)` for both timers, we
                    // can check which end time is closer to `now`. We thus
                    // first compute the end time for the current earliest
                    // alarm as well ...
                    let earliest_end = earliest_ref.wrapping_add(*earliest_dt);

                    // ... and then perform a wrapping_sub against `now` for
                    // both, checking which is smaller:
                    let exp_remain = exp_end.wrapping_sub(now);
                    let earliest_remain = earliest_end.wrapping_sub(now);
                    if exp_remain < earliest_remain {
                        // Our current exp expires earlier than earliest,
                        // replace it:
                        earliest = Some((exp, ud));
                    }
                }
            }
        }

        // We have computed earliest by iterating over all alarms, but have not
        // found one that has already expired. As such return `false`:
        Ok(earliest)
    }

    /// Re-arm the timer. This must be called in response to the underlying
    /// timer firing, or the set of [`Expiration`]s changing. This will iterate
    /// over all [`Expiration`]s and
    ///
    /// - invoke upcalls for all expired app alarms, resetting them afterwards,
    /// - re-arming the alarm for the next earliest [`Expiration`], or
    /// - disarming the alarm if no unexpired [`Expiration`] is found.
    fn process_rearm_or_callback(&self) {
        // Ask the clock about a current reference once. This can incur a
        // volatile read, and this may not be optimized if done in a loop:
        let now = self.alarm.now();

        let expired_handler = |expired: Expiration<A::Ticks>, process_id: &ProcessId| {
            // This closure is run on every expired alarm, _after_ the `enter()`
            // closure on the Grant iterator has returned. We are thus not
            // risking reentrancy here.

            // Enter the app's grant again:
            let _ = self.app_alarms.enter(*process_id, |alarm_state, upcalls| {
                // Reset this app's alarm:
                alarm_state.expiration = None;

                // Deliver the upcall:
                upcalls
                    .schedule_upcall(
                        ALARM_CALLBACK_NUM,
                        (
                            now.into_u32_left_justified() as usize,
                            expired
                                .reference
                                .wrapping_add(expired.dt)
                                .into_u32_left_justified() as usize,
                            0,
                        ),
                    )
                    .ok();
            });

            // Proceed iteration across expirations:
            None::<()>
        };

        // Compute the earliest alarm, and invoke the `expired_handler` for
        // every expired alarm. This will issue a callback and reset the alarms
        // respectively.
        let res = Self::earliest_alarm(
            now,
            // Pass an interator of all non-None expirations:
            self.app_alarms.iter().filter_map(|app| {
                let process_id = app.processid();
                app.enter(|alarm_state, _upcalls| {
                    if let Some(exp) = alarm_state.expiration {
                        Some((exp, process_id, expired_handler))
                    } else {
                        None
                    }
                })
            }),
        );

        // Arm or disarm the alarm accordingly:
        match res {
            // No pending alarm, disarm:
            Ok(None) => {
                let _ = self.alarm.disarm();
            }

            // A future, non-expired alarm should fire:
            Ok(Some((Expiration { reference, dt }, _))) => {
                self.alarm.set_alarm(reference, dt);
            }

            // The expired closure has requested to stop iteration. This should
            // be unreachable, and hence we panic:
            Err((_, _, ())) => {
                unreachable!();
            }
        }
    }

    fn rearm_u32_left_justified_expiration(
        now: A::Ticks,
        reference_u32: Option<u32>,
        dt_u32: u32,
        expiration: &mut Option<Expiration<A::Ticks>>,
    ) -> u32 {
        let reference_unshifted = reference_u32.map(|ref_u32| ref_u32 >> A::Ticks::u32_padding());

        // If the underlying timer is less than 32-bit wide, userspace is able
        // to provide a finer `reference` and `dt` resolution than we can
        // possibly represent in the kernel.
        //
        // We do not want to switch back to userspace *before* the timer
        // fires. As such, when userspace gives us reference and ticks values
        // with a precision unrepresentible using our Ticks object, we round
        // `reference` down, and `dt` up (ensuring that the timer cannot fire
        // earlier than requested).
        let dt_unshifted = if let Some(reference_u32) = reference_u32 {
            // Computing unshifted dt for a userspace alarm can
            // underestimate dt in some cases where both reference and
            // dt had low-order bits that are rounded off by
            // unshifting. To ensure `dt` results in an actual
            // expiration that is at least as long as the expected
            // expiration in user space, compute unshifted dt from an
            // unshifted expiration.
            let expiration_shifted = reference_u32.wrapping_add(dt_u32);
            let expiration_unshifted =
                if expiration_shifted & ((1 << A::Ticks::u32_padding()) - 1) != 0 {
                    // By right-shifting, we would decrease the requested dt value,
                    // firing _before_ the time requested by userspace. Add one to
                    // compensate this:
                    (expiration_shifted >> A::Ticks::u32_padding()) + 1
                } else {
                    expiration_shifted >> A::Ticks::u32_padding()
                };

            expiration_unshifted.wrapping_sub(reference_u32 >> A::Ticks::u32_padding())
        } else if dt_u32 & ((1 << A::Ticks::u32_padding()) - 1) != 0 {
            // By right-shifting, we would decrease the requested dt value,
            // firing _before_ the time requested by userspace. Add one to
            // compensate this:
            (dt_u32 >> A::Ticks::u32_padding()) + 1
        } else {
            // dt does not need to be shifted *or* contains no lower bits
            // unrepresentable in the kernel:
            dt_u32 >> A::Ticks::u32_padding()
        };

        // For timers less than 32-bit wide, we do not have to handle a
        // `reference + dt` overflow specially. This is because those timers are
        // conveyed to us left-justified, and as such userspace would already
        // have to take care of such overflow.
        //
        // However, we *may* need to handle overflow when the timer is *wider*
        // than 32 bit. In this case, if `reference + dt` were to overflow, we
        // need to rebase our reference on the full-width `now` time.
        //
        // If userspace didn't give us a reference, we can skip all of this and
        // simply set the unshifted dt.
        let new_exp = match (reference_unshifted, A::Ticks::width() > 32) {
            (Some(userspace_reference_unshifted), true) => {
                // We have a userspace reference and timer is wider than 32 bit.
                //
                // In this case, we need to check whether the lower 32 bits of the
                // timer `reference` have already wrapped, compared to the reference
                // provided by userspace:
                if now.into_u32() < userspace_reference_unshifted {
                    // The lower 32-bit of reference are smaller than the userspace
                    // reference. This means that the full-width timer has had an
                    // increment in the upper bits. We thus set the full-width
                    // reference to the combination of the current upper timer bits
                    // *minus 1*, concatenated to the user-space provided bits.
                    //
                    // Because we don't know the integer type of the Ticks object
                    // (just that it's larger than a u32), we:
                    //
                    // 1. subtract a full `u32::MAX + 1` to incur a downward wrap,
                    //    effectively subtracting `1` from the upper part,
                    // 2. subtract the lower `u32` bits from this value, setting
                    //    those bits to zero,
                    // 3. adding back the userspace-provided reference.

                    // Build 1 << 32:
                    let bit33 = A::Ticks::from(0xffffffff).wrapping_add(A::Ticks::from(0x1));

                    // Perform step 1, subtracting 1 << 32:
                    let sub_1_upper = now.wrapping_sub(bit33);

                    // Perform step 2, setting first 32 bit to zero:
                    let sub_lower =
                        sub_1_upper.wrapping_sub(A::Ticks::from(sub_1_upper.into_u32()));

                    // Perform step 3, add back the userspace-provided reference:
                    let rebased_reference =
                        sub_lower.wrapping_add(A::Ticks::from(userspace_reference_unshifted));

                    // Finally, return the new expiration. We don't have to do
                    // anything special for `dt`, as it's relative:
                    Expiration {
                        reference: rebased_reference,
                        dt: A::Ticks::from(dt_unshifted),
                    }
                } else {
                    // The lower 32-bit of reference are equal to or larger than the
                    // userspace reference. Thus we can rebase the reference,
                    // touching only the lower 32 bit, by:
                    //
                    // 1. subtract the lower `u32` bits from this value, setting
                    //    those bits to zero,
                    // 2. adding back the userspace-provided reference.

                    // Perform step 1, setting first 32 bit to zero:
                    let sub_lower = now.wrapping_sub(A::Ticks::from(now.into_u32()));

                    // Perform step 2, add back the userspace-provided reference:
                    let rebased_reference =
                        sub_lower.wrapping_add(A::Ticks::from(userspace_reference_unshifted));

                    // Finally, return the new expiration. We don't have to do
                    // anything special for `dt`, as it's relative:
                    Expiration {
                        reference: rebased_reference,
                        dt: A::Ticks::from(dt_unshifted),
                    }
                }
            }

            (Some(userspace_reference_unshifted), false) => {
                // We have a userspace reference and timer is (less than) 32
                // bit. Simply set to unshifted values:

                Expiration {
                    reference: A::Ticks::from(userspace_reference_unshifted),
                    dt: A::Ticks::from(dt_unshifted),
                }
            }

            (None, _) => {
                // We have no userspace reference. Use `now` as a reference:
                Expiration {
                    reference: now,
                    dt: A::Ticks::from(dt_unshifted),
                }
            }
        };

        // Store the new expiration. We already adjusted the armed count above:
        *expiration = Some(new_exp);

        // Return the time left-justified time at which the alarm will fire:
        new_exp
            .reference
            .wrapping_add(new_exp.dt)
            .into_u32_left_justified()
    }
}

impl<'a, A: Alarm<'a>> SyscallDriver for AlarmDriver<'a, A> {
    /// Setup and read the alarm.
    ///
    /// ### `command_num`
    ///
    /// - `0`: Driver existence check.
    /// - `1`: Return the clock frequency in Hz.
    /// - `2`: Read the current clock value
    /// - `3`: Stop the alarm if it is outstanding
    /// - `4`: Deprecated
    /// - `5`: Set an alarm to fire at a given clock value `time` relative to `now`
    /// - `6`: Set an alarm to fire at a given clock value `time` relative to a provided
    ///        reference point.
    fn command(
        &self,
        cmd_type: usize,
        data: usize,
        data2: usize,
        caller_id: ProcessId,
    ) -> CommandReturn {
        // Returns the error code to return to the user and whether we need to
        // reset which is the next active alarm. We _don't_ reset if
        //   - we're disabling the underlying alarm anyway,
        //   - the underlying alarm is currently disabled and we're enabling the first alarm, or
        //   - on an error (i.e. no change to the alarms).
        self.app_alarms
            .enter(caller_id, |td, _upcalls| {
                let now = self.alarm.now();

                match cmd_type {
                    // Driver check:
                    //
                    // Don't re-arm the timer:
                    0 => (CommandReturn::success(), false),

                    1 => {
                        // Get clock frequency. We return a frequency scaled by
                        // the amount of padding we add to the `ticks` value
                        // returned in command 2 ("capture time"), such that
                        // userspace knows when the timer will wrap and can
                        // accurately determine the duration of a single tick.
                        //
                        // Don't re-arm the timer:
                        let scaled_freq =
                            <A::Ticks>::u32_left_justified_scale_freq::<A::Frequency>();
                        (CommandReturn::success_u32(scaled_freq), false)
                    }
                    2 => {
                        // Capture time. We pad the underlying timer's ticks to
                        // wrap at exactly `(2 ** 32) - 1`. This predictable
                        // wrapping value allows userspace to build long running
                        // timers beyond `2 ** now.width()` ticks.
                        //
                        // Don't re-arm the timer:
                        (
                            CommandReturn::success_u32(now.into_u32_left_justified()),
                            false,
                        )
                    }
                    3 => {
                        // Stop
                        match td.expiration {
                            None => {
                                // Request to stop when already stopped. Don't
                                // re-arm the timer:
                                (CommandReturn::failure(ErrorCode::ALREADY), false)
                            }
                            Some(_old_expiraton) => {
                                // Clear the expiration:
                                td.expiration = None;

                                // Ask for the timer to be re-armed. We can't do
                                // this here, as it would re-enter the grant
                                // region:
                                (CommandReturn::success(), true)
                            }
                        }
                    }
                    4 => {
                        // Deprecated in 2.0, used to be: set absolute expiration
                        //
                        // Don't re-arm the timer:
                        (CommandReturn::failure(ErrorCode::NOSUPPORT), false)
                    }
                    5 => {
                        // Set relative expiration.
                        //
                        // We provided userspace a potentially padded version of
                        // our in-kernel Ticks object, and as such we have to
                        // invert that operation through a right shift.
                        //
                        // Also, we need to keep track of the currently armed
                        // timers.
                        //
                        // All of this is done in the following helper method:
                        let new_exp_left_justified = Self::rearm_u32_left_justified_expiration(
                            // Current time:
                            now,
                            // No userspace-provided reference:
                            None,
                            // Left-justified `dt` value:
                            data as u32,
                            // Reference to the `Option<Expiration>`, also used
                            // to update the counter of armed alarms:
                            &mut td.expiration,
                        );

                        // Report success, with the left-justified time at which
                        // the alarm will fire. Also ask for the timer to be
                        // re-armed. We can't do this here, as it would re-enter
                        // the grant region:
                        (CommandReturn::success_u32(new_exp_left_justified), true)
                    }
                    6 => {
                        // Also, we need to keep track of the currently armed
                        // timers.
                        //
                        // All of this is done in the following helper method:
                        let new_exp_left_justified = Self::rearm_u32_left_justified_expiration(
                            // Current time:
                            now,
                            // Left-justified userspace-provided reference:
                            Some(data as u32),
                            // Left-justified `dt` value:
                            data2 as u32,
                            // Reference to the `Option<Expiration>`, also used
                            // to update the counter of armed alarms:
                            &mut td.expiration,
                        );

                        // Report success, with the left-justified time at which
                        // the alarm will fire. Also ask for the timer to be
                        // re-armed. We can't do this here, as it would re-enter
                        // the grant region:
                        (CommandReturn::success_u32(new_exp_left_justified), true)
                    }

                    // Unknown command:
                    //
                    // Don't re-arm the timer:
                    _ => (CommandReturn::failure(ErrorCode::NOSUPPORT), false),
                }
            })
            .map_or_else(
                |err| CommandReturn::failure(err.into()),
                |(retval, rearm_timer)| {
                    if rearm_timer {
                        self.process_rearm_or_callback();
                    }
                    retval
                },
            )
    }

    fn allocate_grant(&self, processid: ProcessId) -> Result<(), kernel::process::Error> {
        self.app_alarms.enter(processid, |_, _| {})
    }
}

impl<'a, A: Alarm<'a>> time::AlarmClient for AlarmDriver<'a, A> {
    fn alarm(&self) {
        self.process_rearm_or_callback();
    }
}

#[cfg(test)]
mod test {
    use core::cell::Cell;
    use core::marker::PhantomData;

    use kernel::hil::time::{
        Alarm, AlarmClient, Freq10MHz, Frequency, Ticks, Ticks24, Ticks32, Ticks64, Time,
    };
    use kernel::utilities::cells::OptionalCell;
    use kernel::ErrorCode;

    use super::{AlarmDriver, Expiration};

    struct MockAlarm<'a, T: Ticks, F: Frequency> {
        current_ticks: Cell<T>,
        client: OptionalCell<&'a dyn AlarmClient>,
        _frequency: PhantomData<F>,
    }

    impl<T: Ticks, F: Frequency> Time for MockAlarm<'_, T, F> {
        type Frequency = F;
        type Ticks = T;

        fn now(&self) -> Self::Ticks {
            self.current_ticks.get()
        }
    }

    impl<'a, T: Ticks, F: Frequency> Alarm<'a> for MockAlarm<'a, T, F> {
        fn set_alarm_client(&self, client: &'a dyn AlarmClient) {
            self.client.set(client);
        }

        fn set_alarm(&self, _reference: Self::Ticks, _dt: Self::Ticks) {
            unimplemented!()
        }

        fn get_alarm(&self) -> Self::Ticks {
            unimplemented!()
        }

        fn disarm(&self) -> Result<(), ErrorCode> {
            unimplemented!()
        }

        fn is_armed(&self) -> bool {
            unimplemented!()
        }

        fn minimum_dt(&self) -> Self::Ticks {
            unimplemented!()
        }
    }

    #[test]
    fn test_earliest_alarm_no_alarms() {
        assert!(
            AlarmDriver::<MockAlarm<Ticks32, Freq10MHz>>::earliest_alarm(
                // Now:
                Ticks32::from(42_u32),
                // Expirations:
                <[(
                    Expiration<kernel::hil::time::Ticks32>,
                    (),
                    fn(_, &()) -> Option<()>
                ); 0] as IntoIterator>::into_iter([])
            )
            .unwrap()
            .is_none()
        )
    }

    #[test]
    fn test_earliest_alarm_multiple_unexpired() {
        // Should never be called:
        let exp_handler = |exp, id: &usize| -> Option<()> {
            panic!("Alarm should not be expired: {:?}, id: {}", exp, id)
        };

        let (earliest, id) = AlarmDriver::<MockAlarm<Ticks32, Freq10MHz>>::earliest_alarm(
            // Now:
            42_u32.into(),
            // Expirations:
            [
                (
                    // Will expire at 52:
                    Expiration {
                        reference: 42_u32.into(),
                        dt: 10_u32.into(),
                    },
                    0,
                    exp_handler,
                ),
                (
                    // Will expire at exactly 43:
                    Expiration {
                        reference: u32::MAX.into(),
                        dt: 44_u32.into(),
                    },
                    1,
                    exp_handler,
                ),
                (
                    // Will expire at 44:
                    Expiration {
                        reference: 10_u32.into(),
                        dt: 34_u32.into(),
                    },
                    2,
                    exp_handler,
                ),
            ]
            .into_iter(),
        )
        .unwrap()
        .unwrap();

        assert!(earliest.reference.into_u32() == u32::MAX);
        assert!(earliest.dt.into_u32() == 44);
        assert!(id == 1);
    }

    #[test]
    fn test_earliest_alarm_multiple_expired() {
        let exp_list: [Cell<bool>; 7] = Default::default();

        let exp_handler = |_exp, id: &usize| -> Option<()> {
            exp_list[*id].set(true);

            // Don't stop iterating on the first expired alarm:
            None
        };

        let (earliest, id) = AlarmDriver::<MockAlarm<Ticks32, Freq10MHz>>::earliest_alarm(
            // Now:
            42_u32.into(),
            // Expirations:
            [
                (
                    // Has expired at 42 (current cycle), should fire!
                    Expiration {
                        reference: 41_u32.into(),
                        dt: 1_u32.into(),
                    },
                    0,
                    &exp_handler,
                ),
                (
                    // Will expire at 52, should not fire.
                    Expiration {
                        reference: 42_u32.into(),
                        dt: 10_u32.into(),
                    },
                    1,
                    &exp_handler,
                ),
                (
                    // Will expire at exactly 43, should not fire.
                    Expiration {
                        reference: u32::MAX.into(),
                        dt: 44_u32.into(),
                    },
                    2,
                    &exp_handler,
                ),
                (
                    // Reference is current time, expiration in the future,
                    // should not fire:
                    Expiration {
                        reference: 42_u32.into(),
                        dt: 1_u32.into(),
                    },
                    3,
                    &exp_handler,
                ),
                (
                    // Reference is 43 (current time + 1), interpreted as "in
                    // the past", should fire:
                    Expiration {
                        reference: 43_u32.into(),
                        dt: 1_u32.into(),
                    },
                    4,
                    &exp_handler,
                ),
                (
                    // Reference is 0, end is at 1, in the past, should fire:
                    Expiration {
                        reference: 0_u32.into(),
                        dt: 1_u32.into(),
                    },
                    5,
                    &exp_handler,
                ),
                (
                    // Reference is u32::MAX, end is at 0, in the past, should fire:
                    Expiration {
                        reference: u32::MAX.into(),
                        dt: 1_u32.into(),
                    },
                    6,
                    &exp_handler,
                ),
            ]
            .into_iter(),
        )
        .unwrap()
        .unwrap();

        assert!(earliest.reference.into_u32() == 41);
        assert!(earliest.dt.into_u32() == 1);
        assert!(id == 0);

        let mut bool_exp_list: [bool; 7] = [false; 7];
        exp_list
            .into_iter()
            .zip(bool_exp_list.iter_mut())
            .for_each(|(src, dst)| *dst = src.get());

        assert!(bool_exp_list == [true, false, false, false, true, true, true]);
    }

    #[test]
    fn test_earliest_alarm_expired_stop() {
        let exp_list: [Cell<bool>; 4] = Default::default();

        let exp_handler = |_exp, id: &usize| -> Option<&'static str> {
            exp_list[*id].set(true);

            // Stop iterating on id == 3
            if *id == 3 {
                Some("stopped")
            } else {
                None
            }
        };

        let (expired, id, expired_ret) =
            AlarmDriver::<MockAlarm<Ticks32, Freq10MHz>>::earliest_alarm(
                // Now:
                42_u32.into(),
                // Expirations:
                [
                    (
                        // Will expire at 52, should not fire.
                        Expiration {
                            reference: 42_u32.into(),
                            dt: 10_u32.into(),
                        },
                        0,
                        &exp_handler,
                    ),
                    (
                        // Has expired at 42 (current cycle), should fire!
                        Expiration {
                            reference: 41_u32.into(),
                            dt: 1_u32.into(),
                        },
                        1,
                        &exp_handler,
                    ),
                    (
                        // Will expire at exactly 43, should not fire.
                        Expiration {
                            reference: u32::MAX.into(),
                            dt: 44_u32.into(),
                        },
                        2,
                        &exp_handler,
                    ),
                    (
                        // Reference is 0, end is at 1, in the past, should fire:
                        Expiration {
                            reference: 0_u32.into(),
                            dt: 1_u32.into(),
                        },
                        3,
                        &exp_handler,
                    ),
                ]
                .into_iter(),
            )
            .err()
            .unwrap();

        assert!(expired.reference.into_u32() == 0);
        assert!(expired.dt.into_u32() == 1);
        assert!(id == 3);
        assert!(expired_ret == "stopped");

        let mut bool_exp_list: [bool; 4] = [false; 4];
        exp_list
            .into_iter()
            .zip(bool_exp_list.iter_mut())
            .for_each(|(src, dst)| *dst = src.get());

        assert!(bool_exp_list == [false, true, false, true,]);
    }

    #[test]
    fn test_rearm_24bit_left_justified_noref_basic() {
        let mut expiration = None;

        assert!(Ticks24::u32_padding() == 8);

        let armed_time =
            AlarmDriver::<MockAlarm<Ticks24, Freq10MHz>>::rearm_u32_left_justified_expiration(
                // Current time:
                Ticks24::from(1337_u32),
                // No userspace-provided reference:
                None,
                // Left-justified `dt` value:
                1234_u32 << Ticks24::u32_padding(),
                // Reference to the `Option<Expiration>`, also used
                // to update the counter of armed alarms:
                &mut expiration,
            );

        let expiration = expiration.unwrap();

        assert_eq!(armed_time, (1337 + 1234) << Ticks24::u32_padding());
        assert_eq!(expiration.reference.into_u32(), 1337);
        assert_eq!(expiration.dt.into_u32(), 1234);
    }

    #[test]
    fn test_rearm_24bit_left_justified_noref_wrapping() {
        let mut expiration = None;

        let armed_time =
            AlarmDriver::<MockAlarm<Ticks24, Freq10MHz>>::rearm_u32_left_justified_expiration(
                // Current time:
                Ticks24::from(1337_u32),
                // No userspace-provided reference:
                None,
                // Left-justified `dt` value (in this case, with some
                // irrepresentable precision)
                u32::MAX - (42 << Ticks24::u32_padding()),
                // Reference to the `Option<Expiration>`, also used
                // to update the counter of armed alarms:
                &mut expiration,
            );

        let expiration = expiration.unwrap();

        // (1337 + ((0xffffffff - (42 << 8)) >> 8) + 1) % 0x01000000 = 1295
        assert_eq!(armed_time, 1295 << Ticks24::u32_padding());
        assert_eq!(expiration.reference.into_u32(), 1337);
        assert_eq!(
            expiration.dt.into_u32(),
            // dt is rounded up to the next representable tick:
            ((u32::MAX - (42 << Ticks24::u32_padding())) >> Ticks24::u32_padding()) + 1
        );
    }

    #[test]
    fn test_rearm_24bit_left_justified_ref_low_bits_basic() {
        let mut expiration = None;

        assert!(Ticks24::u32_padding() == 8);

        let armed_time =
            AlarmDriver::<MockAlarm<Ticks24, Freq10MHz>>::rearm_u32_left_justified_expiration(
                // Current time:
                Ticks24::from(0_u32),
                // Userspace-provided reference, below minimum precision of Ticks24, will be rounded down:
                Some(1_u32),
                // Left-justified `dt` value, below minimum precision of Ticks24, will be rounded up:
                3_u32,
                // Reference to the `Option<Expiration>`, also used
                // to update the counter of armed alarms:
                &mut expiration,
            );

        let expiration = expiration.unwrap();

        // ((1 >> 8) + ((3 >> 8) + 1)  << 8) = 1
        assert_eq!(armed_time, 1 << 8);
        assert_eq!(expiration.reference.into_u32(), 0);
        assert_eq!(expiration.dt.into_u32(), 1);
    }

    #[test]
    fn test_rearm_24bit_left_justified_ref_low_bits_max_int() {
        let mut expiration = None;

        assert!(Ticks24::u32_padding() == 8);

        let armed_time =
            AlarmDriver::<MockAlarm<Ticks24, Freq10MHz>>::rearm_u32_left_justified_expiration(
                // Current time:
                Ticks24::from(6_u32),
                // Userspace-provided reference, including bits not representable in Ticks24:
                // (5 << 8) - 43 = 1237
                Some(Ticks24::from(5_u32).into_u32_left_justified() - 43),
                // Left-justified `dt` value, including bits not representable in Ticks24:
                // (2 << 8) - 43 = 469
                Ticks24::from(2_u32).into_u32_left_justified() + 43,
                // Reference to the `Option<Expiration>`, also used
                // to update the counter of armed alarms:
                &mut expiration,
            );

        let expiration = expiration.unwrap();

        // When we naively round down reference `(1237 / 256 ~= 4.83 -> 4)` and
        // round up dt `(469 / 256 ~= 1.83 -> 2)` we'd arm the alarm to
        // `2 + 4 = 6`. However, when considering the full resolution
        // `reference + dt` `(1237 + 256) / 256 ~= 6.67` we can see that arming
        // to `6` will have the alarm fire too early. The alarm rearm code needs
        // to compensate for the case that (reference + dt) overflow and generate
        // a dt from that rounded value, in this case `7`.
        assert_eq!(armed_time, 7 << 8);
        assert_eq!(expiration.reference.into_u32(), 4);
        assert_eq!(expiration.dt, Ticks24::from(3));
    }

    #[test]
    fn test_rearm_32bit_left_justified_noref_basic() {
        let mut expiration = Some(Expiration {
            reference: 0_u32.into(),
            dt: 1_u32.into(),
        });

        assert!(Ticks32::u32_padding() == 0);

        let armed_time =
            AlarmDriver::<MockAlarm<Ticks32, Freq10MHz>>::rearm_u32_left_justified_expiration(
                // Current time:
                Ticks32::from(1337_u32),
                // No userspace-provided reference:
                None,
                // Left-justified `dt` value, unshifted for 32 bit:
                1234_u32,
                // Reference to the `Option<Expiration>`, also used
                // to update the counter of armed alarms:
                &mut expiration,
            );

        let expiration = expiration.unwrap();

        assert_eq!(armed_time, 1337 + 1234);
        assert_eq!(expiration.reference.into_u32(), 1337);
        assert_eq!(expiration.dt.into_u32(), 1234);
    }

    #[test]
    fn test_rearm_32bit_left_justified_noref_wrapping() {
        let mut expiration = None;

        let armed_time =
            AlarmDriver::<MockAlarm<Ticks32, Freq10MHz>>::rearm_u32_left_justified_expiration(
                // Current time:
                Ticks32::from(1337_u32),
                // No userspace-provided reference:
                None,
                // Left-justified `dt` value (in this case, with some
                // irrepresentable precision)
                u32::MAX - 42,
                // Reference to the `Option<Expiration>`, also used
                // to update the counter of armed alarms:
                &mut expiration,
            );

        let expiration = expiration.unwrap();

        // (1337 + (0xffffffff - 42)) % 0x100000000 = 1294
        assert_eq!(armed_time, 1294);
        assert_eq!(expiration.reference.into_u32(), 1337);
        assert_eq!(expiration.dt.into_u32(), u32::MAX - 42);
    }

    #[test]
    fn test_rearm_64bit_left_justified_noref_wrapping() {
        let mut expiration = Some(Expiration {
            reference: 0_u32.into(),
            dt: 1_u32.into(),
        });

        assert!(Ticks64::u32_padding() == 0);

        let armed_time =
            AlarmDriver::<MockAlarm<Ticks64, Freq10MHz>>::rearm_u32_left_justified_expiration(
                // Current time:
                Ticks64::from(0xDEADBEEFCAFE_u64),
                // No userspace-provided reference:
                None,
                // Left-justified `dt` value, unshifted for 32 bit:
                0xDEADC0DE_u32,
                // Reference to the `Option<Expiration>`, also used
                // to update the counter of armed alarms:
                &mut expiration,
            );

        let expiration = expiration.unwrap();

        assert_eq!(armed_time, 0x9D9D8BDC_u32);
        assert_eq!(expiration.reference.into_u64(), 0xDEADBEEFCAFE_u64);
        assert_eq!(expiration.dt.into_u64(), 0xDEADC0DE_u64);
    }

    #[test]
    fn test_rearm_64bit_left_justified_refnowrap_dtnorwap() {
        let mut expiration = None;

        // reference smaller than now & 0xffffffff, reference + dt don't wrap:
        let armed_time =
            AlarmDriver::<MockAlarm<Ticks64, Freq10MHz>>::rearm_u32_left_justified_expiration(
                // Current time:
                Ticks64::from(0xDEADBEEFCAFE_u64),
                // Userspace-provided reference, smaller than now and dt
                Some(0xBEEFC0DE_u32),
                // Left-justified `dt` value, unshifted for 32 bit:
                0x1BADB002_u32,
                // Reference to the `Option<Expiration>`, also used
                // to update the counter of armed alarms:
                &mut expiration,
            );

        let expiration = expiration.unwrap();

        assert_eq!(armed_time, 0xDA9D70E0_u32); // remains at 0xDEAD
        assert_eq!(expiration.reference.into_u64(), 0xDEADBEEFC0DE_u64);
        assert_eq!(expiration.dt.into_u64(), 0x1BADB002_u64);
    }

    #[test]
    fn test_rearm_64bit_left_justified_refnowrwap_dtwrap() {
        let mut expiration = Some(Expiration {
            reference: 0_u32.into(),
            dt: 1_u32.into(),
        });

        // reference smaller than now & 0xffffffff, reference + dt wrap:
        let armed_time =
            AlarmDriver::<MockAlarm<Ticks64, Freq10MHz>>::rearm_u32_left_justified_expiration(
                // Current time:
                Ticks64::from(0xDEADBEEFCAFE_u64),
                // Userspace-provided reference, smaller than lower 32-bit of now
                Some(0x8BADF00D_u32),
                // Left-justified `dt` value, unshifted for 32 bit:
                0xFEEDC0DE_u32,
                // Reference to the `Option<Expiration>`, also used
                // to update the counter of armed alarms:
                &mut expiration,
            );

        let expiration = expiration.unwrap();

        assert_eq!(armed_time, 0x8A9BB0EB_u32); // wraps t0 0x0xDEAE
        assert_eq!(expiration.reference.into_u64(), 0xDEAD8BADF00D_u64);
        assert_eq!(expiration.dt.into_u64(), 0xFEEDC0DE_u64);
    }

    #[test]
    fn test_rearm_64bit_left_justified_refwrap_dtwrap() {
        let mut expiration = None;

        // reference larger than now & 0xffffffff, reference + dt wrap:
        let armed_time =
            AlarmDriver::<MockAlarm<Ticks64, Freq10MHz>>::rearm_u32_left_justified_expiration(
                // Current time:
                Ticks64::from(0xDEADBEEFCAFE_u64),
                // Userspace-provided reference, larger than lower 32-bit of
                // now, meaning that it's already past:
                Some(0xCAFEB0BA_u32),
                // Left-justified `dt` value, unshifted for 32 bit:
                0xFEEDC0DE_u32,
                // Reference to the `Option<Expiration>`, also used
                // to update the counter of armed alarms:
                &mut expiration,
            );

        let expiration = expiration.unwrap();

        assert_eq!(armed_time, 0xC9EC7198_u32); // wraps to 0xDEAE
        assert_eq!(expiration.reference.into_u64(), 0xDEACCAFEB0BA_u64);
        assert_eq!(expiration.dt.into_u64(), 0xFEEDC0DE_u64);
    }

    #[test]
    fn test_rearm_64bit_left_justified_refwrap_dtnowrap() {
        let mut expiration = Some(Expiration {
            reference: 0_u32.into(),
            dt: 1_u32.into(),
        });

        // reference larger than now & 0xffffffff, reference + dt don't wrap
        let armed_time =
            AlarmDriver::<MockAlarm<Ticks64, Freq10MHz>>::rearm_u32_left_justified_expiration(
                // Current time:
                Ticks64::from(0xDEADBEEFCAFE_u64),
                // Userspace-provided reference, larger than lower 32-bit of now
                Some(0xCAFEB0BA_u32),
                // Left-justified `dt` value, unshifted for 32 bit:
                0x1BADB002_u32,
                // Reference to the `Option<Expiration>`, also used
                // to update the counter of armed alarms:
                &mut expiration,
            );

        let expiration = expiration.unwrap();

        assert_eq!(armed_time, 0xE6AC60BC_u32); // remains at 0xDEAD
        assert_eq!(expiration.reference.into_u64(), 0xDEACCAFEB0BA_u64);
        assert_eq!(expiration.dt.into_u64(), 0x1BADB002_u64);
    }
}