-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkthread.rs
More file actions
417 lines (365 loc) · 14.4 KB
/
Copy pathkthread.rs
File metadata and controls
417 lines (365 loc) · 14.4 KB
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
//! linux-parity: complete
//! linux-source: vendor/linux/kernel/kthread.c
//! test-origin: linux:vendor/linux/kernel/kthread.c
//! Kernel thread lifecycle — Milestone 22.
//!
//! Provides the public kthread API on top of the low-level pool allocator in
//! `sched.rs`. Mirrors Linux `kernel/kthread.c` and `include/linux/kthread.h`.
//!
//! # Architecture
//!
//! - `KThread` is a heap-allocated struct that tracks per-thread lifecycle
//! state (stop flag, exit result, function pointer, back-link to task).
//! - A global `KTHREAD_TABLE` maps `*mut TaskStruct` → `*mut KThread` for the
//! static pool. This avoids adding fields to `TaskStruct` (which has
//! carefully validated Linux-ABI offsets).
//! - `kthread_create_on_node` allocates a pool slot via `sched_alloc_kthread_raw`,
//! assigns a real PID, creates the `KThread`, and leaves the task in the
//! stopped state (not yet enqueued).
//! - `kthread_run` = create + enqueue.
//! - `kthread_stop` sets `KTHREAD_SHOULD_STOP`, then spin-yields until the
//! thread marks itself `TASK_DEAD` and calls `dequeue_task`.
//! - The kthread function itself calls `kthread_should_stop()` in its main
//! loop and returns an `i32` result. The `kthread_start` trampoline stores
//! the result and handles TASK_DEAD + dequeue.
//!
//! # Deferred to M29
//! - `kthreadd` daemon for deferred thread creation
//! - `kthread_park` / `kthread_unpark`
//! - Per-CPU kthreads (`kthread_create_on_cpu`)
//!
//! References:
//! Linux `kernel/kthread.c`
//! Linux `include/linux/kthread.h`
extern crate alloc;
use alloc::boxed::Box;
use core::ffi::c_void;
use core::sync::atomic::{AtomicI32, AtomicUsize, Ordering};
use spin::Mutex;
use crate::kernel::pid::{INIT_PID_NS, alloc_pid};
use crate::kernel::sched::{
MAX_KTHREADS, dequeue_task, enqueue_task, get_current, sched_alloc_kthread_raw,
schedule_with_irqs_enabled,
};
use crate::kernel::task::TaskStruct;
// ── Constants ────────────────────────────────────────────────────────────────
/// Bit index of the "should stop" flag in `KThread::flags`.
/// Matches Linux `KTHREAD_SHOULD_STOP` in `enum KTHREAD_BITS`.
pub const KTHREAD_SHOULD_STOP: usize = 1;
/// Bit index of the "should park" flag.
/// Matches Linux `KTHREAD_SHOULD_PARK`. Parking itself is deferred to M29.
pub const KTHREAD_SHOULD_PARK: usize = 2;
/// `task.__state` value for a dead / exited task. Matches Linux `TASK_DEAD`
/// (= 0x80) — see `crate::kernel::task::task_state`.
use crate::kernel::task::task_state::TASK_DEAD;
// ── KThread ──────────────────────────────────────────────────────────────────
/// Per-kthread lifecycle state.
///
/// Mirrors Linux `struct kthread` from `kernel/kthread.c`.
/// Heap-allocated and registered in `KTHREAD_TABLE`.
pub struct KThread {
/// Lifecycle flags: `KTHREAD_SHOULD_STOP` at bit 1.
pub flags: AtomicUsize,
/// Exit result set by the thread before it returns.
pub result: AtomicI32,
/// The user-provided thread function (returns `i32`, unlike `KthreadFn`).
pub threadfn: unsafe extern "C" fn(*mut c_void) -> i32,
/// Argument passed to `threadfn`.
pub data: *mut c_void,
/// Back-pointer to the owning `TaskStruct`.
pub task: *mut TaskStruct,
}
// SAFETY: KThread is only accessed from its owning thread (read) or from
// kthread_stop (write to flags), under cooperative scheduling in M22.
unsafe impl Send for KThread {}
unsafe impl Sync for KThread {}
// ── KTHREAD_TABLE — side-table: task pointer → KThread pointer ────────────
struct KThreadTable {
entries: [(*mut TaskStruct, *mut KThread); MAX_KTHREADS],
len: usize,
}
// SAFETY: All access is serialised through the Mutex below.
unsafe impl Send for KThreadTable {}
static KTHREAD_TABLE: Mutex<KThreadTable> = Mutex::new(KThreadTable {
entries: [(core::ptr::null_mut(), core::ptr::null_mut()); MAX_KTHREADS],
len: 0,
});
fn register_kthread(task: *mut TaskStruct, kt: *mut KThread) {
let mut tbl = KTHREAD_TABLE.lock();
if tbl.len < MAX_KTHREADS {
let idx = tbl.len;
tbl.entries[idx] = (task, kt);
tbl.len += 1;
}
}
fn lookup_kthread(task: *mut TaskStruct) -> *mut KThread {
let tbl = KTHREAD_TABLE.lock();
for &(t, kt) in &tbl.entries[..tbl.len] {
if t == task {
return kt;
}
}
core::ptr::null_mut()
}
/// Return the opaque argument originally passed to a managed kthread.
pub fn kthread_data(task: *mut TaskStruct) -> *mut c_void {
let kt = lookup_kthread(task);
if kt.is_null() {
core::ptr::null_mut()
} else {
unsafe { (*kt).data }
}
}
// ── kthread_start trampoline ─────────────────────────────────────────────────
/// Heap-allocated closure passed as `arg` to `kthread_entry_stub`.
///
/// Stored in RBX by `sched_alloc_kthread_raw` and loaded into RDI by
/// `kthread_entry_stub` before calling `kthread_start`.
struct KthreadStart {
kt: *mut KThread,
}
unsafe impl Send for KthreadStart {}
unsafe impl Sync for KthreadStart {}
/// Trampoline that adapts `fn -> i32` kthreads to the `fn -> !` contract
/// required by `kthread_entry_stub`.
///
/// Called with `arg = *mut KthreadStart` (moved out of the heap Box here).
/// Runs the real `threadfn`, stores the result, dequeues the task, and halts.
///
/// SAFETY: must only be called from `kthread_entry_stub` on first scheduling.
unsafe extern "C" fn kthread_start(arg: *mut c_void) -> ! {
// Reclaim the KthreadStart Box.
let start = unsafe { Box::from_raw(arg as *mut KthreadStart) };
let kt = unsafe { &*start.kt };
let task = kt.task;
unsafe {
crate::arch::x86::kernel::cpu::common::set_linux_current_task(task);
crate::log_info!(
"kthread",
"kthread_start: task={:p} linux_current={:p} threadfn={:#x} pid={}",
task,
crate::arch::x86::kernel::cpu::common::linux_current_task(),
kt.threadfn as usize,
(*task).pid
);
}
// Run the actual kthread function.
let result = unsafe { (kt.threadfn)(kt.data) };
// Store the exit result for kthread_stop to read.
kt.result.store(result, Ordering::Release);
// Mark the task as dead and remove it from the run queue.
unsafe {
(*task).__state.store(TASK_DEAD, Ordering::Release);
dequeue_task(task);
}
// Halt indefinitely. kthread_stop's spin loop exits when it sees TASK_DEAD.
loop {
core::hint::spin_loop();
}
}
// ── Public API ───────────────────────────────────────────────────────────────
/// Kernel thread function signature: takes one opaque argument, returns `i32`.
///
/// The return value is reported to `kthread_stop()`.
/// The function must regularly call `kthread_should_stop()` and return when
/// it is true, so that `kthread_stop()` can terminate the thread cleanly.
///
/// Mirrors Linux `int (*threadfn)(void *data)`.
pub type KthreadFnI32 = unsafe extern "C" fn(*mut c_void) -> i32;
/// Create a kernel thread but do not start it (leave it in the stopped state).
///
/// Returns a `*mut TaskStruct` with a real PID, NOT yet added to the run queue.
/// The caller must call `enqueue_task` (or use `kthread_run`) to start it.
///
/// Mirrors Linux `kthread_create_on_node()`.
///
/// # Safety
/// - `threadfn` must be a valid kernel thread function.
/// - `name` must be a 16-byte NUL-terminated array (TASK_COMM_LEN).
/// - Returns NULL on pool/heap/PID exhaustion.
pub unsafe fn kthread_create_on_node(
threadfn: KthreadFnI32,
data: *mut c_void,
name: &[u8; 16],
) -> *mut TaskStruct {
// Allocate the KThread struct on the heap first, so we can pass its
// pointer as the `arg` to kthread_entry_stub via KthreadStart.
let kt_box = Box::new(KThread {
flags: AtomicUsize::new(0),
result: AtomicI32::new(0),
threadfn,
data,
task: core::ptr::null_mut(), // filled in below
});
let kt: *mut KThread = Box::into_raw(kt_box);
// Allocate the KthreadStart closure on the heap.
let start_box = Box::new(KthreadStart { kt });
let start_ptr: *mut KthreadStart = Box::into_raw(start_box);
// `kthread_start` is `unsafe extern "C" fn(*mut c_void) -> !` — the same
// type as `KthreadFn` defined in sched.rs, so no cast is needed.
//
// Allocate a pool slot (no PID assigned yet).
let task = unsafe {
sched_alloc_kthread_raw(
kthread_start,
start_ptr as *mut c_void,
name,
crate::kernel::sched::kthread_entry_stub_addr(),
)
};
if task.is_null() {
// Pool exhausted — free our heap allocations.
unsafe {
drop(Box::from_raw(start_ptr));
drop(Box::from_raw(kt));
}
return core::ptr::null_mut();
}
// Assign a real PID.
let nr = match alloc_pid(&INIT_PID_NS, None) {
Ok(kpid) => {
let nr = kpid.numbers[0].nr;
// Leak the KPid Box — cleanup (put_pid) deferred to M26 (do_exit).
let _ = Box::into_raw(kpid);
nr
}
Err(_) => {
// PID namespace exhausted.
unsafe {
drop(Box::from_raw(start_ptr));
drop(Box::from_raw(kt));
}
return core::ptr::null_mut();
}
};
unsafe {
(*task).pid = nr;
(*task).tgid = nr;
// Fill the back-pointer now that we have the task address.
(*kt).task = task;
}
register_kthread(task, kt);
task
}
/// Create and immediately enqueue (start) a kernel thread.
///
/// Mirrors Linux `kthread_run()`.
///
/// # Safety
/// Same as `kthread_create_on_node`. Returns NULL on failure.
pub unsafe fn kthread_run(
threadfn: KthreadFnI32,
data: *mut c_void,
name: &[u8; 16],
) -> *mut TaskStruct {
let task = unsafe { kthread_create_on_node(threadfn, data, name) };
if !task.is_null() {
unsafe { enqueue_task(task) };
}
task
}
/// Signal a kthread to stop and wait for it to exit.
///
/// Sets `KTHREAD_SHOULD_STOP` in the kthread's flags, then spin-yields
/// (calling `schedule()`) until the kthread marks itself `TASK_DEAD`.
/// Returns the kthread's exit value (set by the thread before it returns).
///
/// The kthread function must call `kthread_should_stop()` and return when
/// it is true; otherwise `kthread_stop` will spin forever.
///
/// Mirrors Linux `kthread_stop()`.
///
/// # Safety
/// `task` must point to a live kthread previously created by
/// `kthread_create_on_node` or `kthread_run`.
pub unsafe fn kthread_stop(task: *mut TaskStruct) -> i32 {
let kt = lookup_kthread(task);
if kt.is_null() {
return -1; // not a managed kthread
}
// Request the thread to stop.
unsafe { &*kt }
.flags
.fetch_or(1 << KTHREAD_SHOULD_STOP, Ordering::Release);
// Spin-yield until the kthread marks itself TASK_DEAD.
loop {
let state = unsafe { (*task).__state.load(Ordering::Acquire) };
if state == TASK_DEAD {
break;
}
// Yield to let the kthread run.
unsafe { schedule_with_irqs_enabled() };
}
unsafe { &*kt }.result.load(Ordering::Acquire)
}
/// Return `true` if the current kthread has been asked to stop.
///
/// Must be called from within a kthread function (i.e. after the thread
/// has been scheduled for the first time). Returns `false` if the current
/// task is not a managed kthread.
///
/// Mirrors Linux `kthread_should_stop()`.
///
/// # Safety
/// Must be called after `sched_init()` (so `get_current()` is valid).
pub unsafe fn kthread_should_stop() -> bool {
let task = unsafe { get_current() };
if task.is_null() {
return false;
}
let kt = lookup_kthread(task);
if kt.is_null() {
return false;
}
let flags = unsafe { &*kt }.flags.load(Ordering::Acquire);
flags & (1 << KTHREAD_SHOULD_STOP) != 0
}
// ── Unit tests ───────────────────────────────────────────────────────────────
#[cfg(test)]
mod tests {
use super::*;
// ── Constants match Linux ────────────────────────────────────────────────
#[test]
fn kthread_should_stop_bit_is_1() {
assert_eq!(KTHREAD_SHOULD_STOP, 1);
}
#[test]
fn kthread_should_park_bit_is_2() {
assert_eq!(KTHREAD_SHOULD_PARK, 2);
}
// ── KThread flag semantics ───────────────────────────────────────────────
#[test]
fn kthread_should_stop_false_by_default() {
let flags = AtomicUsize::new(0);
let is_stop = flags.load(Ordering::Relaxed) & (1 << KTHREAD_SHOULD_STOP) != 0;
assert!(
!is_stop,
"SHOULD_STOP bit must not be set on a fresh KThread"
);
}
#[test]
fn kthread_should_stop_true_after_setting_bit() {
let flags = AtomicUsize::new(0);
flags.fetch_or(1 << KTHREAD_SHOULD_STOP, Ordering::Relaxed);
let is_stop = flags.load(Ordering::Relaxed) & (1 << KTHREAD_SHOULD_STOP) != 0;
assert!(is_stop, "SHOULD_STOP bit must be set after fetch_or");
}
#[test]
fn kthread_stop_bit_does_not_affect_park_bit() {
let flags = AtomicUsize::new(0);
flags.fetch_or(1 << KTHREAD_SHOULD_STOP, Ordering::Relaxed);
let is_park = flags.load(Ordering::Relaxed) & (1 << KTHREAD_SHOULD_PARK) != 0;
assert!(!is_park, "Setting SHOULD_STOP must not affect SHOULD_PARK");
}
#[test]
fn kthread_result_starts_at_zero() {
let result = AtomicI32::new(0);
assert_eq!(result.load(Ordering::Relaxed), 0);
}
#[test]
fn kthread_result_is_readable_after_store() {
let result = AtomicI32::new(0);
result.store(42, Ordering::Release);
assert_eq!(result.load(Ordering::Acquire), 42);
}
}