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
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
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
//! Thread-owned state and its synchronization boundaries.

use alloc::{sync::Arc, vec::Vec};
use core::{
    cell::UnsafeCell,
    sync::atomic::{AtomicBool, AtomicI32, AtomicU8, AtomicU32, AtomicUsize, Ordering},
};

use ax_runtime::hal::{cpu::user::UserContext, percpu::CpuPin, time::TimeValue};
use axpoll_set::PollSet;
use scope_local::{ActiveScope, LocalItem, Scope};
use starry_signal::{SignalSet, Signo, api::ThreadSignalManager};

use super::{
    CpuTimeAccounting, Cred, ExitPathLease, PidIdentity, PidNamespaceRef, PidRoleLease,
    ProcessData, ROOT_PID_NS, RttimeWatchdog, SeccompDecision, SeccompState, SeccompStateStore,
    SockFilter, Tid, TidNumber, UserTaskRef,
    bounded_stack::BoundedStack,
    futex::ThreadWaitState,
    future,
    interruption::{InterruptSnapshot, InterruptState},
    ops,
    scheduler_identity::SchedulerIdentity,
    user_memory_access::{UserMemoryAccessDepth, UserMemoryAccessGuard},
    wait_on_pollset,
};
use crate::sync::{IrqMutex, Mutex, NoPreemptIrqSave};

const KRETPROBE_STACK_CAPACITY: usize = 16;
const SYSCALL_WORK_SECCOMP: u32 = 1 << 0;

/// User-visible and scheduler-visible identities retained by one Linux thread.
struct ThreadIdentity {
    scheduler: SchedulerIdentity,
    nice: AtomicI32,
}

impl ThreadIdentity {
    fn new() -> Self {
        Self {
            scheduler: SchedulerIdentity::unbound(),
            nice: AtomicI32::new(0),
        }
    }
}

/// Stable Linux identity ownership retained independently from scheduler IDs.
struct ThreadPidOwnership {
    // Field drop order matters for cancelled, unpublished threads: their
    // reservation may already be gone when scheduler reclamation runs.
    // Release the weak role lease while this final identity pin still lives.
    tid_lease: Option<PidRoleLease<Tid>>,
    identity: Arc<PidIdentity>,
}

/// Scope-local resources and their task-context serialization.
struct ThreadScope {
    scope: UnsafeCell<Scope>,
    access: Mutex<()>,
}

// SAFETY: immutable active-task and remote reads may overlap because every
// scope-local payload is `Sync`. `access` serializes every remote read with the
// only mutable path, while that current-task mutation also disables local IRQs
// and preemption so no active-scope access can overlap it on the owner CPU.
unsafe impl Sync for ThreadScope {}

impl ThreadScope {
    fn new(scope: Scope) -> Self {
        Self {
            scope: UnsafeCell::new(scope),
            access: Mutex::new(()),
        }
    }

    fn with_current_mut<R>(&self, operation: impl FnOnce(&mut Scope) -> R) -> R {
        let _access = self.access.lock();
        let _guard = NoPreemptIrqSave::new();
        // SAFETY: `access` excludes remote scope readers and the IRQ/preempt
        // guard excludes every local active-scope access. The scheduler's
        // current publication proves that this thread owns the selected scope.
        unsafe {
            ax_runtime::hal::percpu::with_cpu_pin(|pin| {
                let scope = &mut *self.scope.get();
                assert!(
                    ActiveScope::is_pinned(scope, pin),
                    "Starry scope mutation does not belong to the current task"
                );
                operation(scope)
            })
            .expect("Starry scope mutation requires an installed CPU area")
        }
    }

    fn clone_item<T>(&self, item: &LocalItem<T>) -> T
    where
        T: Clone + Send + Sync + 'static,
    {
        let _access = self.access.lock();
        // SAFETY: the PI mutex serializes this immutable view with the only
        // mutable path. An on-CPU task may concurrently read the same `Sync`
        // payload through its active scope.
        item.scope(unsafe { &*self.scope.get() }).clone()
    }

    unsafe fn activate_pinned(&self, pin: &CpuPin<'_>) {
        assert!(
            ActiveScope::is_global_pinned(pin),
            "Starry scope activation requires the global scope"
        );
        // SAFETY: Linux-style scheduler placement is the sole on-CPU claim.
        // The switch baton retains this thread and pin until switch-out; the
        // task-local access mutex separately serializes the only mutation.
        unsafe { ActiveScope::set_pinned(&*self.scope.get(), pin) };
    }

    unsafe fn deactivate_pinned(&self, pin: &CpuPin<'_>) {
        assert!(
            ActiveScope::is_pinned(unsafe { &*self.scope.get() }, pin),
            "Starry scope deactivation does not match the current task"
        );
        // SAFETY: the matching scheduler switch-in published this scope under
        // the same CPU's switch baton.
        unsafe { ActiveScope::set_global_pinned(pin) };
    }
}

/// Runtime accounting that follows scheduler switch callbacks.
struct ThreadAccounting {
    cpu_time: CpuTimeAccounting,
    rttime: Mutex<RttimeWatchdog>,
}

impl ThreadAccounting {
    fn new() -> crate::StarryResult<Self> {
        Ok(Self {
            cpu_time: CpuTimeAccounting::new()?,
            rttime: Mutex::new(RttimeWatchdog::new()),
        })
    }
}

struct VforkDone {
    done: bool,
    poll: Arc<PollSet>,
}

/// Thread-exit, userspace restart, and interruptible-wait state.
struct ThreadLifecycle {
    clear_child_tid: AtomicUsize,
    robust_list_head: AtomicUsize,
    exit: Arc<AtomicBool>,
    interrupted: InterruptState,
    user_memory_access: UserMemoryAccessDepth,
    block_next_signal_check: NextSignalCheckBlock,
    exit_event: Arc<PollSet>,
    vfork_done: IrqMutex<Option<VforkDone>>,
    exit_request: OneShotFlag,
    deadline_overrun: OneShotFlag,
    rseq_area: AtomicUsize,
    rseq_signature: AtomicU32,
}

/// Lock-free entry and return work published to one Linux thread.
struct ThreadWork {
    syscall: AtomicU32,
}

impl ThreadWork {
    const fn new() -> Self {
        Self {
            syscall: AtomicU32::new(0),
        }
    }
}

impl ThreadLifecycle {
    fn new() -> crate::StarryResult<Self> {
        Ok(Self {
            clear_child_tid: AtomicUsize::new(0),
            robust_list_head: AtomicUsize::new(0),
            exit: super::allocation::try_arc(AtomicBool::new(false))?,
            interrupted: InterruptState::new(),
            user_memory_access: UserMemoryAccessDepth::new(),
            block_next_signal_check: NextSignalCheckBlock::new(),
            exit_event: super::allocation::try_arc(PollSet::new())?,
            vfork_done: IrqMutex::new(None),
            exit_request: OneShotFlag::new(),
            deadline_overrun: OneShotFlag::new(),
            rseq_area: AtomicUsize::new(0),
            rseq_signature: AtomicU32::new(0),
        })
    }
}

/// Signal queue and signalfd notification state.
struct ThreadSignals {
    manager: Arc<ThreadSignalManager>,
    signalfd_waker: PollSet,
    deferred_mask_restore: IrqMutex<Option<SignalSet>>,
    deferred_mask_restore_pending: AtomicBool,
}

impl ThreadSignals {
    fn new(
        tid: u32,
        process_signal: Arc<starry_signal::api::ProcessSignalManager>,
        signal_mask: SignalSet,
    ) -> crate::StarryResult<Self> {
        Ok(Self {
            manager: ThreadSignalManager::new_with_blocked(tid, process_signal, signal_mask)?,
            signalfd_waker: PollSet::new(),
            deferred_mask_restore: IrqMutex::new(None),
            deferred_mask_restore_pending: AtomicBool::new(false),
        })
    }
}

/// Credentials and one-way security policy owned by a thread.
struct ThreadSecurity {
    oom_score_adj: AtomicI32,
    pdeathsig: AtomicU32,
    no_new_privs: AtomicBool,
    seccomp: SeccompStateStore,
    cred: Mutex<Arc<Cred>>,
    uid_map_written: AtomicBool,
    gid_map_written: AtomicBool,
    setgroups_deny: AtomicBool,
}

impl ThreadSecurity {
    fn new(parent_cred: Option<Arc<Cred>>) -> crate::StarryResult<Self> {
        Ok(Self {
            oom_score_adj: AtomicI32::new(200),
            pdeathsig: AtomicU32::new(0),
            no_new_privs: AtomicBool::new(false),
            seccomp: SeccompStateStore::new()?,
            cred: Mutex::new(match parent_cred {
                Some(cred) => cred,
                None => super::allocation::try_arc(Cred::root())?,
            }),
            uid_map_written: AtomicBool::new(false),
            gid_map_written: AtomicBool::new(false),
            setgroups_deny: AtomicBool::new(false),
        })
    }
}

/// Probe, crash-dump, and PMU state observed from trap or scheduler context.
struct ThreadTrace {
    fault_dump_signo: AtomicU8,
    kretprobe_stack:
        IrqMutex<BoundedStack<kprobe::retprobe::RetprobeInstance, KRETPROBE_STACK_CAPACITY>>,
    #[cfg(target_arch = "aarch64")]
    perf: crate::perf::task_context::ThreadPerfContext,
}

impl ThreadTrace {
    fn new() -> Self {
        Self {
            fault_dump_signo: AtomicU8::new(0),
            kretprobe_stack: IrqMutex::new(BoundedStack::new()),
            #[cfg(target_arch = "aarch64")]
            perf: crate::perf::task_context::ThreadPerfContext::new(),
        }
    }
}

/// A coalescing publication consumed exactly once by its owner thread.
struct OneShotFlag {
    pending: AtomicBool,
    #[cfg(axtest)]
    consume_rmws: AtomicUsize,
}

impl OneShotFlag {
    const fn new() -> Self {
        Self {
            pending: AtomicBool::new(false),
            #[cfg(axtest)]
            consume_rmws: AtomicUsize::new(0),
        }
    }

    fn publish(&self) {
        self.pending.store(true, Ordering::Release);
    }

    fn is_pending(&self) -> bool {
        self.pending.load(Ordering::Acquire)
    }

    fn consume(&self) -> bool {
        if !self.is_pending() {
            return false;
        }
        #[cfg(axtest)]
        self.consume_rmws.fetch_add(1, Ordering::Relaxed);
        self.pending.swap(false, Ordering::AcqRel)
    }

    #[cfg(axtest)]
    fn consume_rmw_count(&self) -> usize {
        self.consume_rmws.load(Ordering::Relaxed)
    }
}

/// A one-shot flag that suppresses exactly one signal check.
struct NextSignalCheckBlock(OneShotFlag);

impl NextSignalCheckBlock {
    const fn new() -> Self {
        Self(OneShotFlag::new())
    }

    fn block(&self) {
        self.0.publish();
    }

    fn unblock(&self) -> bool {
        self.0.consume()
    }
}

/// The Starry state attached to one generation-bearing scheduler thread.
pub struct Thread {
    identity: ThreadIdentity,
    pid: IrqMutex<ThreadPidOwnership>,

    /// The process data shared by all threads in the process.
    pub proc_data: Arc<ProcessData>,

    scope: ThreadScope,
    accounting: ThreadAccounting,
    lifecycle: ThreadLifecycle,
    work: ThreadWork,
    wait: ThreadWaitState,
    signals: ThreadSignals,
    security: ThreadSecurity,
    trace: ThreadTrace,
    /// Per-task software perf bindings. Inherited tasks own slice-local state.
    pub(crate) perf_sw_counters: Arc<IrqMutex<crate::perf::sw::SwTaskContext>>,
    /// Last CPU observed by the software perf scheduler hook.
    pub(crate) perf_sw_last_cpu: AtomicU32,
}

impl Thread {
    /// Prepares this unpublished child's MM-release completion.
    pub(crate) fn prepare_vfork_done(&self) -> crate::StarryResult<()> {
        let poll = super::allocation::try_arc(PollSet::new())?;
        let mut completion = self.lifecycle.vfork_done.lock();
        assert!(completion.is_none(), "vfork completion installed twice");
        *completion = Some(VforkDone { done: false, poll });
        Ok(())
    }

    /// Waits for MM release, or detaches a killed parent as TASK_KILLABLE does.
    /// Returns whether the child completed the wait (and permits VFORK_DONE).
    pub(crate) fn wait_vfork_done(&self, parent: &UserTaskRef) -> bool {
        let poll = {
            let guard = self.lifecycle.vfork_done.lock();
            match guard.as_ref() {
                Some(vfork) => vfork.poll.clone(),
                None => return true,
            }
        };
        let curr_thr = parent.as_thread();
        loop {
            let result = future::block_on_user(
                parent,
                wait_on_pollset(&poll, || {
                    self.lifecycle
                        .vfork_done
                        .lock()
                        .as_ref()
                        .map(|vfork| vfork.done)
                        .unwrap_or(true)
                        .then_some(())
                }),
            );
            match result {
                future::UserWaitOutcome::Ready(()) => return true,
                future::UserWaitOutcome::Interrupted
                    if curr_thr.has_exit_request()
                        || curr_thr.signal().pending().has(Signo::SIGKILL) =>
                {
                    // Linux clears child->vfork_done under task_lock before
                    // letting a killed parent leave its completion wait. Drop
                    // the detached poll owner after releasing our IRQ lock.
                    let detached = self.lifecycle.vfork_done.lock().take();
                    drop(detached);
                    return false;
                }
                future::UserWaitOutcome::Interrupted => continue,
                future::UserWaitOutcome::TimedOut => {
                    unreachable!("vfork completion wait has no deadline")
                }
            }
        }
    }

    /// Publishes vfork completion before waking the parent.
    pub(crate) fn notify_vfork_done(&self) {
        let poll = {
            let mut guard = self.lifecycle.vfork_done.lock();
            match guard.as_mut() {
                Some(vfork) => {
                    vfork.done = true;
                    vfork.poll.clone()
                }
                None => return,
            }
        };
        unsafe { poll.wake(axpoll::IoEvents::IN) };
    }
    /// Creates a new thread state object before the scheduler identity is bound.
    pub fn new(
        identity: Arc<PidIdentity>,
        tid_lease: PidRoleLease<Tid>,
        proc_data: Arc<ProcessData>,
        parent_cred: Option<Arc<Cred>>,
        signal_mask: SignalSet,
        scope: Scope,
    ) -> crate::StarryResult<Self> {
        let tid = identity
            .visible_number(&ROOT_PID_NS)
            .expect("new thread identity has no root PID binding")
            .get();
        let process_signal = proc_data.signal.clone();
        let process_identity = proc_data.identity();
        let thread = Self {
            identity: ThreadIdentity::new(),
            pid: IrqMutex::new(ThreadPidOwnership {
                identity: identity.clone(),
                tid_lease: Some(tid_lease),
            }),
            proc_data,
            scope: ThreadScope::new(scope),
            accounting: ThreadAccounting::new()?,
            lifecycle: ThreadLifecycle::new()?,
            work: ThreadWork::new(),
            wait: ThreadWaitState::new(),
            security: ThreadSecurity::new(parent_cred)?,
            trace: ThreadTrace::new(),
            perf_sw_counters: super::allocation::try_arc(IrqMutex::new(Default::default()))?,
            perf_sw_last_cpu: AtomicU32::new(crate::perf::sw::CPU_UNSET),
            // Register with the process only after every private allocation succeeds.
            signals: ThreadSignals::new(tid, process_signal, signal_mask)?,
        };
        identity.bind_thread_pidfd(&process_identity, thread.exit_flag());
        Ok(thread)
    }

    pub(super) const fn wait_state(&self) -> &ThreadWaitState {
        &self.wait
    }

    /// Mutates the current thread's resource scope.
    ///
    /// Contending task-context readers and writers sleep on the outer PI mutex.
    /// The closure itself runs with preemption and local IRQs disabled and must
    /// only install already-prepared scope entries.
    pub(crate) fn with_current_scope_mut<R>(&self, f: impl FnOnce(&mut Scope) -> R) -> R {
        self.scope.with_current_mut(f)
    }

    /// Clones one owned scope-local value under task-context serialization.
    ///
    /// The scope lease and writer gate are released before the returned owner
    /// can acquire any lock it contains.
    pub(crate) fn clone_scope_item<T>(&self, item: &LocalItem<T>) -> T
    where
        T: Clone + Send + Sync + 'static,
    {
        self.scope.clone_item(item)
    }

    /// Returns the root-namespace TID, independent from the scheduler ID.
    pub fn tid(&self) -> TidNumber {
        self.tid_number()
    }

    pub(crate) fn tid_number(&self) -> TidNumber {
        TidNumber::from(
            self.pid
                .lock()
                .identity
                .visible_number(&ROOT_PID_NS)
                .expect("live thread lost its root PID binding"),
        )
    }

    /// Returns this thread's stable PID generation.
    pub(crate) fn pid_identity(&self) -> Arc<PidIdentity> {
        self.pid.lock().identity.clone()
    }

    /// Returns the active PID namespace derived from the identity itself.
    pub(crate) fn active_pid_namespace(&self) -> PidNamespaceRef {
        self.pid.lock().identity.active_namespace()
    }

    /// Returns the TID as observed from this thread's active namespace.
    pub(crate) fn user_tid(&self) -> TidNumber {
        let pid = self.pid.lock();
        let active = pid.identity.active_namespace();
        TidNumber::from(
            pid.identity
                .visible_number(&active)
                .expect("thread identity is not visible from its active namespace"),
        )
    }

    /// Publishes the runtime link immediately before scheduler activation.
    pub(crate) fn attach_pid_task(&self, task: &UserTaskRef) {
        self.pid.lock().identity.attach_task(task);
    }

    /// Releases the runtime link while transferring the TID role and the
    /// remaining exit-path responsibility to the caller.
    ///
    /// The returned [`ExitPathLease`] owns everything the retired task still
    /// owes its PID namespaces; the caller completes it once zombie
    /// publication, parent notification, and relation close finished.
    pub(crate) fn retire_pid_retaining_tid(&self) -> (PidRoleLease<Tid>, ExitPathLease) {
        let (identity, lease) = {
            let mut pid = self.pid.lock();
            (pid.identity.clone(), pid.tid_lease.take())
        };
        let exit_path = identity.mark_task_exited();
        (
            lease.expect("thread TID lease transferred twice"),
            exit_path,
        )
    }

    /// Releases the runtime link and TID role after scheduler-visible exit.
    ///
    /// The returned [`ExitPathLease`] keeps the identity's exit path pending
    /// until the caller completes it at the end of `do_exit`.
    pub(crate) fn retire_pid(&self) -> ExitPathLease {
        let (tid_lease, exit_path) = self.retire_pid_retaining_tid();
        exit_path.retain_tid(tid_lease)
    }

    /// Atomically transfers a fully retired leader identity to this runtime
    /// task at exec.
    ///
    /// The caller's previous identity is fully retired here, not left to a
    /// later `do_exit`: after the swap that exit runs under the leader
    /// identity and will never complete the previous one. The caller waits for
    /// the retired leader's exit-path lease before this transfer, matching
    /// Linux `de_thread`, which observes the old leader's exit state and runs
    /// its `release_task` path before adopting the leader PID.
    pub(crate) fn transfer_pid_identity(
        &self,
        task: &UserTaskRef,
        identity: Arc<PidIdentity>,
        tid_lease: PidRoleLease<Tid>,
    ) {
        let previous = {
            let mut pid = self.pid.lock();
            let _irq_guard = NoPreemptIrqSave::new();
            task.transfer_irq_pid_identity(&identity)
                .expect("exec leader identity differs from the cached process identity");
            core::mem::replace(
                &mut *pid,
                ThreadPidOwnership {
                    identity: identity.clone(),
                    tid_lease: Some(tid_lease),
                },
            )
        };
        // Dropping the TID role lease first releases the previous role before
        // the exit path completes, so a roleless identity can also detach its
        // namespace number in the same step.
        let previous_identity = {
            drop(previous.tid_lease);
            previous.identity
        };
        previous_identity.mark_task_exited().complete();
        identity.transfer_task(task, &self.proc_data.identity(), self.exit_flag());
    }

    /// Returns this Linux task's retained nice value.
    pub fn nice(&self) -> i32 {
        self.identity.nice.load(Ordering::Acquire)
    }

    /// Updates this Linux task's retained nice value.
    pub fn set_nice(&self, nice: i32) {
        self.identity.nice.store(nice, Ordering::Release);
    }

    /// Returns the generation-bearing scheduler identity, if bound.
    pub fn scheduler_id(&self) -> Option<ax_std::os::arceos::task::thread::ThreadId> {
        self.identity.scheduler.get()
    }

    pub(super) fn scheduler_runtime_ns(&self) -> u64 {
        self.identity
            .scheduler
            .get()
            .and_then(|id| {
                ax_runtime::task::thread::ThreadHandle::lookup(id)
                    .and_then(|thread| thread.runtime())
                    .ok()
            })
            .map(|snapshot| snapshot.charged_runtime_ns())
            .unwrap_or_else(|| self.accounting.cpu_time.published_runtime_ns())
    }

    /// Binds the scheduler identity exactly once.
    pub(crate) fn bind_scheduler_id(
        &self,
        id: ax_std::os::arceos::task::thread::ThreadId,
    ) -> crate::StarryResult<()> {
        self.identity.scheduler.bind(id)
    }

    pub(crate) fn validate_scheduler_id(
        &self,
        id: ax_std::os::arceos::task::thread::ThreadId,
    ) -> crate::StarryResult<()> {
        self.identity.scheduler.validate_bound(id)
    }

    pub(super) fn scheduler_switch_in(
        &self,
        id: ax_std::os::arceos::task::thread::ThreadId,
        realtime_policy: bool,
        charged_runtime_ns: u64,
        cpu_pin: &CpuPin<'_>,
    ) {
        debug_assert!(self.validate_scheduler_id(id).is_ok());
        self.accounting
            .cpu_time
            .scheduler_switch_in(realtime_policy, || charged_runtime_ns);
        // SAFETY: the scheduler switch baton pins this CPU and retains the
        // thread-owned ProcessData until the matching switch-out callback.
        unsafe { self.scope.activate_pinned(cpu_pin) };
        #[cfg(target_arch = "aarch64")]
        crate::perf::task::perf_sched_in(self);
        crate::perf::sw::sched_in(self);
    }

    pub(super) fn scheduler_switch_out(
        &self,
        reason: ax_std::os::arceos::task::thread::SwitchReason,
        cpu_pin: &CpuPin<'_>,
    ) {
        #[cfg(target_arch = "aarch64")]
        crate::perf::task::perf_sched_out(self);
        crate::perf::sw::sched_out(self);
        // SAFETY: switch-in established exactly one activation for this task,
        // and the scheduler baton still pins the same CPU during switch-out.
        unsafe { self.scope.deactivate_pinned(cpu_pin) };
        self.accounting.cpu_time.scheduler_switch_out(reason);
    }

    pub(crate) fn apply_cpu_time_policy(&self, realtime_policy: bool, _observed_ns: u64) {
        self.accounting
            .cpu_time
            .apply_realtime_policy(realtime_policy);
    }

    pub(crate) fn cpu_time_output(&self) -> (TimeValue, TimeValue) {
        self.accounting.cpu_time.output(self.scheduler_runtime_ns())
    }

    pub(crate) fn commit_cpu_time_now(&self) {
        let runtime_ns = self.scheduler_runtime_ns();
        self.proc_data.record_cpu_time_transition(|| {
            self.accounting.cpu_time.publish_committed_delta(runtime_ns)
        });
    }

    pub(super) fn sample_scheduler_tick_cpu_time(&self, _observed_ns: u64) {
        let runtime_ns = self.scheduler_runtime_ns();
        self.proc_data.record_cpu_time_transition(|| {
            self.accounting.cpu_time.sample_scheduler_tick(runtime_ns)
        });
    }

    pub(crate) fn cpu_time(&self) -> &CpuTimeAccounting {
        &self.accounting.cpu_time
    }

    pub(crate) fn rttime(&self) -> &Mutex<RttimeWatchdog> {
        &self.accounting.rttime
    }

    /// Returns the clear-child-TID address.
    pub fn clear_child_tid(&self) -> usize {
        self.lifecycle.clear_child_tid.load(Ordering::Relaxed)
    }

    /// Updates the clear-child-TID address.
    pub fn set_clear_child_tid(&self, clear_child_tid: usize) {
        self.lifecycle
            .clear_child_tid
            .store(clear_child_tid, Ordering::Relaxed);
    }

    /// Returns the robust-list head address.
    pub fn robust_list_head(&self) -> usize {
        self.lifecycle.robust_list_head.load(Ordering::SeqCst)
    }

    /// Updates the robust-list head address.
    pub fn set_robust_list_head(&self, robust_list_head: usize) {
        self.lifecycle
            .robust_list_head
            .store(robust_list_head, Ordering::SeqCst);
    }

    /// Returns whether the thread exit transaction has completed.
    pub fn pending_exit(&self) -> bool {
        self.lifecycle.exit.load(Ordering::Acquire)
    }

    /// Claims this thread's exit transaction exactly once.
    pub fn begin_exit(&self) -> bool {
        self.signal().begin_exit()
    }

    /// Publishes completion of the thread exit transaction.
    pub fn set_exit(&self) {
        self.lifecycle.exit.store(true, Ordering::Release);
    }

    pub(crate) fn exit_flag(&self) -> Arc<AtomicBool> {
        self.lifecycle.exit.clone()
    }

    pub(crate) fn exit_event(&self) -> Arc<PollSet> {
        self.lifecycle.exit_event.clone()
    }

    /// Consumes one pending thread-only exit request.
    pub fn take_exit_request(&self) -> bool {
        self.lifecycle.exit_request.consume()
    }

    /// Probes a pending thread-only exit request without consuming it.
    pub fn has_exit_request(&self) -> bool {
        self.lifecycle.exit_request.is_pending()
    }

    /// Requests a thread-only exit at the next signal safe point.
    pub fn set_exit_request(&self) {
        self.lifecycle.exit_request.publish();
    }

    pub(super) fn publish_deadline_overrun(&self) {
        self.lifecycle.deadline_overrun.publish();
    }

    pub(super) fn take_deadline_overrun(&self) -> bool {
        self.lifecycle.deadline_overrun.consume()
    }

    pub(crate) fn enter_user_memory_access(&self) -> UserMemoryAccessGuard<'_> {
        self.lifecycle.user_memory_access.enter()
    }

    pub(crate) fn has_active_user_memory_access(&self) -> bool {
        self.lifecycle.user_memory_access.is_active()
    }

    pub(super) fn interrupt(&self) {
        self.lifecycle.interrupted.publish();
    }

    pub(super) fn take_interrupt(&self) -> bool {
        self.lifecycle.interrupted.consume()
    }

    pub(super) fn interrupted(&self) -> bool {
        self.lifecycle.interrupted.is_pending()
    }

    pub(super) fn interrupt_snapshot(&self) -> InterruptSnapshot {
        self.lifecycle.interrupted.snapshot()
    }

    pub(super) fn acknowledge_interrupt(&self, snapshot: InterruptSnapshot) {
        let _advanced = self.lifecycle.interrupted.acknowledge(snapshot);
    }

    /// Returns the registered rseq area pointer.
    pub fn rseq_area(&self) -> usize {
        self.lifecycle.rseq_area.load(Ordering::SeqCst)
    }

    /// Returns the registered rseq signature.
    pub fn rseq_signature(&self) -> u32 {
        self.lifecycle.rseq_signature.load(Ordering::SeqCst)
    }

    /// Updates the registered rseq area and signature.
    pub fn set_rseq_state(&self, addr: usize, sig: u32) {
        self.lifecycle.rseq_area.store(addr, Ordering::SeqCst);
        self.lifecycle.rseq_signature.store(sig, Ordering::SeqCst);
    }

    /// Clears the registered rseq state.
    pub fn clear_rseq_state(&self) {
        self.lifecycle.rseq_area.store(0, Ordering::SeqCst);
        self.lifecycle.rseq_signature.store(0, Ordering::SeqCst);
    }

    /// Blocks the next signal check for this thread.
    pub fn block_next_signal_check(&self) {
        self.lifecycle.block_next_signal_check.block();
    }

    /// Consumes the one-shot signal-check block.
    pub fn unblock_next_signal_check(&self) -> bool {
        self.lifecycle.block_next_signal_check.unblock()
    }

    /// Returns this thread's signal manager.
    pub fn signal(&self) -> &Arc<ThreadSignalManager> {
        &self.signals.manager
    }

    /// Defers restoration of a temporary syscall signal mask until delivery.
    pub(crate) fn defer_signal_mask_restore(&self, mask: SignalSet) {
        let previous = self.signals.deferred_mask_restore.lock().replace(mask);
        assert!(
            previous.is_none(),
            "one thread cannot own nested deferred signal-mask restores"
        );
        self.signals
            .deferred_mask_restore_pending
            .store(true, Ordering::Release);
    }

    /// Takes the mask that the next delivered signal frame must restore.
    pub(crate) fn take_deferred_signal_mask_restore(&self) -> Option<SignalSet> {
        if !self
            .signals
            .deferred_mask_restore_pending
            .load(Ordering::Acquire)
        {
            return None;
        }
        let restore = self.signals.deferred_mask_restore.lock().take();
        self.signals
            .deferred_mask_restore_pending
            .store(false, Ordering::Release);
        restore
    }

    /// Tests the Linux-style return-to-user work flags without entering any
    /// signal, exit, or realtime-limit state machine.
    pub(super) fn has_user_return_work(&self) -> bool {
        // A publication can outlive its signal (consumed, ignored or masked).
        // The return path must still reconcile and acknowledge that epoch.
        self.interrupted()
            || self.signal().has_pending_signal_work()
            || self.has_exit_request()
            || self.lifecycle.deadline_overrun.is_pending()
            || self
                .signals
                .deferred_mask_restore_pending
                .load(Ordering::Acquire)
    }

    pub(crate) fn wake_signalfd(&self) {
        // Pending signal state is published before pollers are woken.
        unsafe { self.signals.signalfd_waker.wake(axpoll::IoEvents::IN) };
    }

    pub(crate) fn signalfd_poll_source(&self) -> &PollSet {
        &self.signals.signalfd_waker
    }

    /// Returns the OOM score adjustment value.
    pub fn oom_score_adj(&self) -> i32 {
        self.security.oom_score_adj.load(Ordering::SeqCst)
    }

    /// Updates the OOM score adjustment value.
    pub fn set_oom_score_adj(&self, value: i32) {
        self.security.oom_score_adj.store(value, Ordering::SeqCst);
    }

    /// Returns the parent-death signal.
    pub fn pdeathsig(&self) -> u32 {
        self.security.pdeathsig.load(Ordering::Relaxed)
    }

    /// Updates the parent-death signal.
    pub fn set_pdeathsig(&self, sig: u32) {
        self.security.pdeathsig.store(sig, Ordering::Relaxed);
    }

    /// Returns whether no-new-privileges is active.
    pub fn no_new_privs(&self) -> bool {
        self.security.no_new_privs.load(Ordering::Relaxed)
    }

    /// Permanently enables no-new-privileges.
    pub fn set_no_new_privs(&self) {
        self.security.no_new_privs.store(true, Ordering::Relaxed);
    }

    /// Returns a snapshot of the seccomp state.
    pub fn seccomp_state(&self) -> Arc<SeccompState> {
        self.security.seccomp.snapshot()
    }

    /// Evaluates the immutable seccomp snapshot published for this thread.
    pub(crate) fn evaluate_seccomp(&self, uctx: &UserContext) -> SeccompDecision {
        self.security.seccomp.evaluate(uctx)
    }

    /// Tests Linux-style syscall entry work without touching its cold state.
    pub(crate) fn has_seccomp_syscall_work(&self) -> bool {
        self.work.syscall.load(Ordering::Acquire) & SYSCALL_WORK_SECCOMP != 0
    }

    fn publish_seccomp_syscall_work(&self) {
        self.work
            .syscall
            .fetch_or(SYSCALL_WORK_SECCOMP, Ordering::Release);
    }

    /// Copies the parent's final security state while its publication gate is held.
    pub(crate) fn inherit_security(&self, parent: &Thread) -> crate::StarryResult<()> {
        let state = parent.seccomp_state();
        let active = state.is_active();
        self.security.seccomp.inherit(state)?;
        if parent.no_new_privs() {
            self.set_no_new_privs();
        }
        if active {
            self.publish_seccomp_syscall_work();
        }
        Ok(())
    }

    /// Replaces inherited seccomp state.
    pub fn set_seccomp_state(&self, state: Arc<SeccompState>) {
        let active = state.is_active();
        self.security.seccomp.replace(state);
        if active {
            self.publish_seccomp_syscall_work();
        }
    }

    /// Enables strict seccomp mode.
    pub fn install_seccomp_strict(&self) -> crate::StarryResult<()> {
        self.security.seccomp.update(SeccompState::install_strict)?;
        self.publish_seccomp_syscall_work();
        Ok(())
    }

    /// Appends one seccomp filter program.
    pub fn append_seccomp_filter(&self, insns: Vec<SockFilter>) -> crate::StarryResult<()> {
        self.security
            .seccomp
            .update(move |state| state.append_filter(insns))?;
        self.publish_seccomp_syscall_work();
        Ok(())
    }

    /// Returns a credential snapshot.
    pub fn cred(&self) -> Arc<Cred> {
        self.security.cred.lock().clone()
    }

    fn set_cred_single(&self, new_cred: Arc<Cred>) {
        let previous = {
            let mut current = self.security.cred.lock();
            core::mem::replace(&mut *current, new_cred)
        };
        drop(previous);
    }

    /// Replaces credentials for this thread only.
    pub(crate) fn set_thread_cred(&self, new_cred: Cred) {
        self.set_cred_single(Arc::new(new_cred));
    }

    /// Replaces credentials for every thread in this process.
    pub fn set_cred(&self, new_cred: Cred) {
        let new_arc = Arc::new(new_cred);
        self.set_cred_single(new_arc.clone());

        let mut tids = self.proc_data.proc.threads();
        tids.sort_unstable();
        for tid in &tids {
            if let Ok(task) = ops::get_task_by_number(*tid) {
                task.as_thread().set_cred_single(new_arc.clone());
            }
        }
    }

    /// Updates every thread from its own credential snapshot.
    ///
    /// Process-wide set-ID transitions must preserve thread-local state such
    /// as `PR_SET_KEEPCAPS` while publishing the shared ID change.
    pub(crate) fn update_process_creds(&self, update: impl Fn(&Cred) -> Cred) {
        let old_cred = self.cred();
        self.set_cred_single(Arc::new(update(&old_cred)));

        let mut tids = self.proc_data.proc.threads();
        tids.sort_unstable();
        for tid in &tids {
            if let Ok(task) = ops::get_task_by_number(*tid) {
                let thread = task.as_thread();
                if core::ptr::eq(thread, self) {
                    continue;
                }
                let old_cred = thread.cred();
                thread.set_cred_single(Arc::new(update(&old_cred)));
            }
        }
    }

    /// Returns whether `uid_map` has been written.
    pub fn uid_map_written(&self) -> bool {
        self.security.uid_map_written.load(Ordering::Relaxed)
    }

    /// Updates the `uid_map` publication state.
    pub fn set_uid_map_written(&self, val: bool) {
        self.security.uid_map_written.store(val, Ordering::Relaxed);
    }

    /// Returns whether `gid_map` has been written.
    pub fn gid_map_written(&self) -> bool {
        self.security.gid_map_written.load(Ordering::Relaxed)
    }

    /// Updates the `gid_map` publication state.
    pub fn set_gid_map_written(&self, val: bool) {
        self.security.gid_map_written.store(val, Ordering::Relaxed);
    }

    /// Returns whether `setgroups` is denied.
    pub fn setgroups_deny(&self) -> bool {
        self.security.setgroups_deny.load(Ordering::Relaxed)
    }

    /// Updates the `setgroups` deny state.
    pub fn set_setgroups_deny(&self, val: bool) {
        self.security.setgroups_deny.store(val, Ordering::Relaxed);
    }

    pub(crate) fn claim_fault_dump(&self, signo: u8) -> bool {
        self.trace
            .fault_dump_signo
            .compare_exchange(signo, 0, Ordering::AcqRel, Ordering::Relaxed)
            .is_ok()
    }

    pub(crate) fn set_fault_dump(&self, signo: u8) {
        self.trace.fault_dump_signo.store(signo, Ordering::Release);
    }

    pub(crate) fn clear_fault_dump(&self) {
        self.trace.fault_dump_signo.store(0, Ordering::Release);
    }

    pub(super) fn push_kretprobe(&self, instance: kprobe::retprobe::RetprobeInstance) {
        let Some(mut stack) = self.trace.kretprobe_stack.try_lock() else {
            panic!("nested kretprobe tried to re-enter the current task stack");
        };
        if let Err(instance) = stack.try_push(instance) {
            core::mem::forget(instance);
            panic!("current task exceeded its fixed kretprobe nesting capacity");
        }
    }

    pub(super) fn pop_kretprobe(&self) -> kprobe::retprobe::RetprobeInstance {
        let Some(mut stack) = self.trace.kretprobe_stack.try_lock() else {
            panic!("nested kretprobe tried to re-enter the current task stack");
        };
        stack.pop().expect("kretprobe instance stack underflow")
    }

    #[cfg(target_arch = "aarch64")]
    pub(crate) fn perf_context(&self) -> &crate::perf::task_context::ThreadPerfContext {
        &self.trace.perf
    }
}

#[cfg(axtest)]
fn inactive_one_shot_flag_consumption_is_read_only_for_test() -> bool {
    let flag = OneShotFlag::new();
    if flag.consume() {
        return false;
    }
    flag.publish();
    flag.consume() && !flag.consume() && flag.consume_rmw_count() == 1
}

#[cfg(all(test, axtest))]
mod axtests {
    #[axtest::axtest]
    fn cancelled_thread_releases_tid_before_last_identity() {
        use alloc::sync::Arc;

        use crate::task::{PidReservation, PidReservationKind, Tid};

        let namespace = crate::task::new_test_pid_namespace();
        let reservation = PidReservation::reserve(&namespace, PidReservationKind::Thread).unwrap();
        let identity = reservation.identity();
        let number = identity.root_number();
        let retired = Arc::downgrade(&identity);
        let ownership = super::ThreadPidOwnership {
            tid_lease: Some(identity.acquire_role::<Tid>().unwrap()),
            identity,
        };

        // Cancellation can retire the reservation before deferred scheduler
        // reclamation destroys the prepared thread and its final identity pin.
        drop(reservation);
        assert!(namespace.lookup(number).is_none());
        drop(ownership);
        assert!(retired.upgrade().is_none());
    }

    #[axtest::axtest]
    fn inactive_one_shot_flag_consumption_is_read_only() {
        assert!(super::inactive_one_shot_flag_consumption_is_read_only_for_test());
    }
}

#[cfg(all(test, not(axtest)))]
mod tests {
    use core::sync::atomic::{AtomicBool, Ordering};

    use super::{NextSignalCheckBlock, ThreadSecurity};
    use crate::{sync::Mutex, task::SeccompStateStore};

    #[test]
    fn seccomp_reads_use_an_immutable_snapshot_store() {
        fn assert_pi_mutex<T>(_: &Mutex<T>) {}
        fn assert_seccomp_store(_: &SeccompStateStore) {}
        fn assert_security_lock_types(security: &ThreadSecurity) {
            assert_seccomp_store(&security.seccomp);
            assert_pi_mutex(&security.cred);
        }

        let _ = assert_security_lock_types as fn(&ThreadSecurity);
    }

    #[test]
    fn old_global_signal_check_block_leaks_between_threads() {
        static OLD_BLOCK_NEXT_SIGNAL_CHECK: AtomicBool = AtomicBool::new(false);

        fn block_next_signal() {
            OLD_BLOCK_NEXT_SIGNAL_CHECK.store(true, Ordering::SeqCst);
        }

        fn unblock_next_signal() -> bool {
            OLD_BLOCK_NEXT_SIGNAL_CHECK.swap(false, Ordering::SeqCst)
        }

        block_next_signal();
        assert!(unblock_next_signal());
        assert!(!unblock_next_signal());
    }

    #[test]
    fn per_thread_signal_check_block_is_isolated() {
        let thread_a = NextSignalCheckBlock::new();
        let thread_b = NextSignalCheckBlock::new();

        thread_a.block();

        assert!(!thread_b.unblock());
        assert!(thread_a.unblock());
        assert!(!thread_a.unblock());
    }
}

#[cfg(axtest)]
#[axtest::axtest]
fn thread_state_creation_returns_allocation_failure() {
    use ax_std::os::arceos::task::thread::ThreadAllocationProbe;

    use crate::task::{PidReservation, PidReservationKind, Tgid};

    let attempt = |failure| {
        let reservation =
            PidReservation::reserve(&ROOT_PID_NS, PidReservationKind::ProcessLeader).unwrap();
        let identity = reservation.identity();
        let tid = identity.acquire_role::<Tid>().unwrap();
        let tgid = identity.acquire_role::<Tgid>().unwrap();
        let process = crate::task::new_test_process_data(identity.clone(), tgid);
        let retired = Arc::downgrade(&process);
        let probe = ThreadAllocationProbe::fail_at(failure).unwrap();
        let result = Thread::new(
            identity,
            tid,
            process,
            None,
            Default::default(),
            Scope::new(),
        );
        let attempts = probe.attempts();
        drop(probe);
        if failure == usize::MAX {
            let thread = result.expect("private thread construction must succeed");
            let probe = ThreadAllocationProbe::fail_at(0).unwrap();
            let error = thread
                .prepare_vfork_done()
                .expect_err("vfork completion allocation failure must return ENOMEM");
            assert_eq!(error.linux_errno(), syscalls::Errno::ENOMEM);
            assert_eq!(probe.attempts(), 1);
            drop(probe);
            assert!(thread.lifecycle.vfork_done.lock().is_none());
            thread.prepare_vfork_done().unwrap();
            drop(thread);
        } else {
            let error = result
                .err()
                .expect("thread state allocation must return ENOMEM");
            assert_eq!(error.linux_errno(), syscalls::Errno::ENOMEM);
            assert_eq!(attempts, failure + 1);
        }
        assert!(
            retired.upgrade().is_none(),
            "failed thread state retained process ownership"
        );
        drop(reservation);
        attempts
    };
    let attempts = attempt(usize::MAX);
    assert!(attempts > 0);
    for failure in 0..attempts {
        attempt(failure);
    }
}