starry-kernel 0.10.2

A Linux-compatible OS kernel built on ArceOS unikernel
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
//! Generation-bearing CPU ownership state for task-bound PMU events.

use super::cpu_id::PerfCpuId;

/// Hardware counter selected for one PMU event.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum Counter {
    Cycle,
    Programmable(usize),
}

/// Identity returned by the per-CPU sampling registry.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) struct SampleRegistration {
    owner: PerfCpuId,
    counter: usize,
    generation: u64,
}

impl SampleRegistration {
    /// Creates a registry identity after one slot has been published.
    pub(crate) const fn new(owner: PerfCpuId, counter: usize, generation: u64) -> Self {
        Self {
            owner,
            counter,
            generation,
        }
    }

    /// Returns the CPU whose registry owns the slot.
    pub(crate) const fn owner(self) -> PerfCpuId {
        self.owner
    }

    /// Returns the programmable PMU counter index.
    pub(crate) const fn counter(self) -> usize {
        self.counter
    }

    /// Returns the globally unique slot generation.
    pub(crate) const fn generation(self) -> u64 {
        self.generation
    }
}

/// One schedule-in attempt, before the hardware slot is fully running.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) struct PmuArmTicket {
    owner: PerfCpuId,
    counter: Counter,
    generation: u64,
}

/// One hardware-running schedule generation.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) struct PmuRunLease {
    owner: PerfCpuId,
    counter: Counter,
    generation: u64,
    registration: Option<SampleRegistration>,
}

impl PmuRunLease {
    /// Returns the CPU that owns the programmed counter.
    pub(crate) const fn owner(self) -> PerfCpuId {
        self.owner
    }

    /// Returns the sampling slot identity, when this is a sampling event.
    pub(crate) const fn registration(self) -> Option<SampleRegistration> {
        self.registration
    }

    pub(super) const fn counter(self) -> Counter {
        self.counter
    }

    const fn generation(self) -> u64 {
        self.generation
    }
}

/// Action required after an fd/task teardown or disable request.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum PmuCloseAction {
    /// A previous fd/task teardown already released this event.
    AlreadyClosed,
    /// No hardware generation remains reachable.
    Complete,
    /// The owner CPU must stop this exact generation.
    Stop(PmuRunLease),
}

/// Result of attempting to claim an exact owner-CPU stop.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum PmuStopClaim {
    /// This caller exclusively owns the hardware stop transaction.
    Claimed(PmuRunLease),
    /// The same generation was already stopped by switch-out.
    AlreadyComplete,
    /// Another owner-CPU path is currently stopping this generation.
    InProgress,
    /// The requested generation is not the active or last-completed one.
    Stale,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum PmuStopGoal {
    Detach,
    Close,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum PmuRunPhase {
    Detached,
    Arming(PmuArmTicket),
    Registered(PmuArmTicket, SampleRegistration),
    Running(PmuRunLease),
    StopRequested(PmuRunLease, PmuStopGoal),
    Stopping(PmuRunLease, PmuStopGoal),
    Closed,
}

/// Serialized lifecycle for scheduler hooks and task-context control.
#[derive(Debug)]
pub(crate) struct PmuRunState {
    phase: PmuRunPhase,
    next_generation: u64,
    last_stopped_generation: u64,
}

impl PmuRunState {
    /// Creates a detached event.
    pub(crate) const fn new() -> Self {
        Self {
            phase: PmuRunPhase::Detached,
            next_generation: 0,
            last_stopped_generation: 0,
        }
    }

    /// Starts one schedule-in generation.
    pub(crate) fn begin_arm(&mut self, owner: PerfCpuId, counter: Counter) -> Option<PmuArmTicket> {
        if self.phase != PmuRunPhase::Detached {
            return None;
        }
        self.next_generation = self
            .next_generation
            .checked_add(1)
            .expect("PMU run generation exhausted");
        let ticket = PmuArmTicket {
            owner,
            counter,
            generation: self.next_generation,
        };
        self.phase = PmuRunPhase::Arming(ticket);
        Some(ticket)
    }

    /// Publishes the exact per-CPU registry identity for this arm attempt.
    pub(crate) fn publish_registration(
        &mut self,
        ticket: PmuArmTicket,
        registration: SampleRegistration,
    ) {
        assert_eq!(self.phase, PmuRunPhase::Arming(ticket));
        self.phase = PmuRunPhase::Registered(ticket, registration);
    }

    /// Marks the hardware counter running after the registry is reachable.
    pub(crate) fn finish_arm(&mut self, ticket: PmuArmTicket) {
        let (observed, registration) = match self.phase {
            PmuRunPhase::Arming(observed) => (observed, None),
            PmuRunPhase::Registered(observed, registration) => (observed, Some(registration)),
            _ => panic!("PMU arm completed from an invalid lifecycle phase"),
        };
        assert_eq!(observed, ticket);
        self.phase = PmuRunPhase::Running(PmuRunLease {
            owner: ticket.owner,
            counter: ticket.counter,
            generation: ticket.generation,
            registration,
        });
    }

    /// Aborts an arm attempt before any registry entry is published.
    pub(crate) fn cancel_arm(&mut self, ticket: PmuArmTicket) {
        assert_eq!(self.phase, PmuRunPhase::Arming(ticket));
        self.phase = PmuRunPhase::Detached;
    }

    /// Returns the hardware-live generation, including a requested/in-flight stop.
    ///
    /// A close request must remain visible here so the task's switch-out path
    /// can quiesce PMU hardware before an affine worker is scheduled.
    pub(crate) const fn running(&self) -> Option<PmuRunLease> {
        match self.phase {
            PmuRunPhase::Running(lease)
            | PmuRunPhase::StopRequested(lease, _)
            | PmuRunPhase::Stopping(lease, _) => Some(lease),
            _ => None,
        }
    }

    /// Claims the hardware generation for the scheduler switch-out path.
    pub(crate) fn claim_schedule_out(&mut self) -> Option<PmuRunLease> {
        let (lease, goal) = match self.phase {
            PmuRunPhase::Running(lease) => (lease, PmuStopGoal::Detach),
            PmuRunPhase::StopRequested(lease, goal) => (lease, goal),
            _ => return None,
        };
        self.phase = PmuRunPhase::Stopping(lease, goal);
        Some(lease)
    }

    /// Claims a stop previously requested by disable or close.
    pub(crate) fn claim_requested_stop(&mut self, lease: PmuRunLease) -> PmuStopClaim {
        match self.phase {
            PmuRunPhase::StopRequested(observed, goal) if observed == lease => {
                self.phase = PmuRunPhase::Stopping(observed, goal);
                PmuStopClaim::Claimed(observed)
            }
            PmuRunPhase::Stopping(observed, _) if observed == lease => PmuStopClaim::InProgress,
            PmuRunPhase::Detached | PmuRunPhase::Closed
                if self.last_stopped_generation == lease.generation() =>
            {
                PmuStopClaim::AlreadyComplete
            }
            _ => PmuStopClaim::Stale,
        }
    }

    /// Publishes the result of one exact owner-CPU stop.
    pub(crate) fn finish_owner_stop(&mut self, lease: PmuRunLease) {
        let goal = match self.phase {
            PmuRunPhase::Stopping(observed, goal) if observed == lease => goal,
            _ => panic!("PMU stop completed from an invalid lifecycle phase"),
        };
        self.last_stopped_generation = lease.generation();
        self.phase = match goal {
            PmuStopGoal::Detach => PmuRunPhase::Detached,
            PmuStopGoal::Close => PmuRunPhase::Closed,
        };
    }

    /// Returns a failed owner-CPU stop to the requested state for retry.
    ///
    /// The exact generation and the strongest requested goal are retained. In
    /// particular, a concurrent close that upgraded a disable transaction must
    /// remain a permanent-close request after the architecture operation fails.
    pub(crate) fn abort_owner_stop(&mut self, lease: PmuRunLease) {
        let goal = match self.phase {
            PmuRunPhase::Stopping(observed, goal) if observed == lease => goal,
            _ => panic!("PMU stop aborted from an invalid lifecycle phase"),
        };
        self.phase = PmuRunPhase::StopRequested(lease, goal);
    }

    /// Reports whether permanent teardown was requested or completed.
    pub(crate) const fn is_stopping(&self) -> bool {
        matches!(
            self.phase,
            PmuRunPhase::StopRequested(_, PmuStopGoal::Close)
                | PmuRunPhase::Stopping(_, PmuStopGoal::Close)
                | PmuRunPhase::Closed
        )
    }

    /// Requests an owner-CPU stop without permanently closing the event.
    pub(crate) fn begin_disable(&mut self) -> PmuCloseAction {
        self.request_stop(PmuStopGoal::Detach)
    }

    /// Starts idempotent permanent teardown.
    pub(crate) fn begin_close(&mut self) -> PmuCloseAction {
        self.request_stop(PmuStopGoal::Close)
    }

    fn request_stop(&mut self, requested_goal: PmuStopGoal) -> PmuCloseAction {
        let (lease, phase_is_stopping, current_goal) = match self.phase {
            PmuRunPhase::Registered(ticket, registration) => (
                PmuRunLease {
                    owner: ticket.owner,
                    counter: ticket.counter,
                    generation: ticket.generation,
                    registration: Some(registration),
                },
                false,
                requested_goal,
            ),
            PmuRunPhase::Running(lease) => (lease, false, requested_goal),
            PmuRunPhase::StopRequested(lease, goal) => (lease, false, goal),
            PmuRunPhase::Stopping(lease, goal) => (lease, true, goal),
            PmuRunPhase::Detached => {
                if requested_goal == PmuStopGoal::Close {
                    self.phase = PmuRunPhase::Closed;
                }
                return PmuCloseAction::Complete;
            }
            PmuRunPhase::Closed => return PmuCloseAction::AlreadyClosed,
            PmuRunPhase::Arming(_) => {
                panic!("PMU stop observed an arm before registry publication")
            }
        };
        let goal = if requested_goal == PmuStopGoal::Close {
            PmuStopGoal::Close
        } else {
            current_goal
        };
        self.phase = if phase_is_stopping {
            PmuRunPhase::Stopping(lease, goal)
        } else {
            PmuRunPhase::StopRequested(lease, goal)
        };
        PmuCloseAction::Stop(lease)
    }
}

#[cfg(all(test, not(axtest)))]
mod tests {
    use super::*;

    const TEST_COUNTER: Counter = Counter::Programmable(2);

    #[test]
    fn cancelled_arm_returns_to_the_detached_state() {
        let cpu = PerfCpuId::new(0);
        let mut state = PmuRunState::new();
        let arm = state.begin_arm(cpu, Counter::Cycle).unwrap();

        state.cancel_arm(arm);

        assert!(state.begin_arm(cpu, Counter::Cycle).is_some());
    }

    #[test]
    fn close_after_registry_publish_must_disarm_before_reclaim() {
        let cpu = PerfCpuId::new(1);
        let mut state = PmuRunState::new();
        let arm = state.begin_arm(cpu, TEST_COUNTER).unwrap();
        let registration = SampleRegistration::new(cpu, 3, 17);
        state.publish_registration(arm, registration);

        let PmuCloseAction::Stop(lease) = state.begin_close() else {
            panic!("a slot is IRQ-reachable before the legacy running flag is published");
        };
        assert_eq!(lease.owner(), cpu);
        assert_eq!(lease.counter(), TEST_COUNTER);
        assert_eq!(lease.registration(), Some(registration));
    }

    #[test]
    fn fully_running_generation_is_disarmed_on_its_owner_cpu() {
        let cpu = PerfCpuId::new(2);
        let mut state = PmuRunState::new();
        let arm = state.begin_arm(cpu, TEST_COUNTER).unwrap();
        let registration = SampleRegistration::new(cpu, 4, 23);
        state.publish_registration(arm, registration);
        state.finish_arm(arm);

        let PmuCloseAction::Stop(lease) = state.begin_close() else {
            panic!("running registration was not disarmed");
        };
        let observed = lease.registration().unwrap();
        assert_eq!(observed.owner(), cpu);
        assert_eq!(observed.counter(), 4);
        assert_eq!(observed.generation(), 23);
    }

    #[test]
    fn close_request_remains_visible_to_the_switch_out_owner() {
        let cpu = PerfCpuId::new(3);
        let mut state = PmuRunState::new();
        let arm = state.begin_arm(cpu, TEST_COUNTER).unwrap();
        state.finish_arm(arm);

        let PmuCloseAction::Stop(lease) = state.begin_close() else {
            panic!("running event must request an owner-CPU stop");
        };
        assert_eq!(
            state.running(),
            Some(lease),
            "switch-out must still claim a close-requested hardware generation"
        );
        assert_eq!(state.claim_schedule_out(), Some(lease));
        state.finish_owner_stop(lease);
        assert_eq!(
            state.claim_requested_stop(lease),
            PmuStopClaim::AlreadyComplete,
            "the affine worker must treat a switch-out winner as a completed fence"
        );
        assert!(state.is_stopping());
    }

    #[test]
    fn disable_stops_one_generation_without_closing_the_event() {
        let cpu = PerfCpuId::new(1);
        let mut state = PmuRunState::new();
        let arm = state.begin_arm(cpu, TEST_COUNTER).unwrap();
        state.finish_arm(arm);

        let PmuCloseAction::Stop(lease) = state.begin_disable() else {
            panic!("disable must fence the active generation");
        };
        assert_eq!(
            state.claim_requested_stop(lease),
            PmuStopClaim::Claimed(lease)
        );
        state.finish_owner_stop(lease);
        assert!(!state.is_stopping());
        assert!(
            state.begin_arm(cpu, TEST_COUNTER).is_some(),
            "disable must permit re-enable"
        );
    }

    #[test]
    fn failed_owner_stop_can_be_claimed_again() {
        let cpu = PerfCpuId::new(2);
        let mut state = PmuRunState::new();
        let arm = state.begin_arm(cpu, TEST_COUNTER).unwrap();
        state.finish_arm(arm);

        let PmuCloseAction::Stop(lease) = state.begin_close() else {
            panic!("close must fence the active generation");
        };
        assert_eq!(
            state.claim_requested_stop(lease),
            PmuStopClaim::Claimed(lease)
        );

        // Model a fixed-CPU worker that claimed the stop but could not complete the
        // architecture operation. Teardown must retain the exact generation and
        // permit a later fd/task release to retry it.
        state.abort_owner_stop(lease);
        assert_eq!(
            state.claim_requested_stop(lease),
            PmuStopClaim::Claimed(lease)
        );
    }

    #[test]
    fn close_upgrades_an_in_flight_disable_to_permanent_teardown() {
        let cpu = PerfCpuId::new(4);
        let mut state = PmuRunState::new();
        let arm = state.begin_arm(cpu, TEST_COUNTER).unwrap();
        state.finish_arm(arm);

        let PmuCloseAction::Stop(lease) = state.begin_disable() else {
            panic!("disable must fence the active generation");
        };
        assert_eq!(
            state.claim_requested_stop(lease),
            PmuStopClaim::Claimed(lease)
        );
        assert_eq!(state.begin_close(), PmuCloseAction::Stop(lease));
        state.finish_owner_stop(lease);

        assert!(state.is_stopping());
        assert_eq!(state.begin_close(), PmuCloseAction::AlreadyClosed);
    }

    #[test]
    fn a_stale_lease_cannot_stop_the_next_arm_generation() {
        let cpu = PerfCpuId::new(5);
        let mut state = PmuRunState::new();
        let first_arm = state.begin_arm(cpu, TEST_COUNTER).unwrap();
        state.finish_arm(first_arm);
        let PmuCloseAction::Stop(first) = state.begin_disable() else {
            panic!("first disable must return its lease");
        };
        assert_eq!(
            state.claim_requested_stop(first),
            PmuStopClaim::Claimed(first)
        );
        state.finish_owner_stop(first);

        let second_arm = state.begin_arm(cpu, TEST_COUNTER).unwrap();
        state.finish_arm(second_arm);
        assert_eq!(state.claim_requested_stop(first), PmuStopClaim::Stale);
    }
}