starnix_core/execution/
loop_entry.rs

1// Copyright 2025 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use crate::task::{CurrentTask, ExitStatus};
6use starnix_sync::{Locked, Unlocked};
7use std::sync::atomic::{AtomicPtr, Ordering};
8
9/// The type of function that must be provided by the kernel binary to enter the syscall loop.
10pub type SyscallLoopEntry = fn(&mut Locked<Unlocked>, &mut CurrentTask) -> ExitStatus;
11
12// Need to make sure the function pointer is actually just a pointer to store it safely in atomic.
13static_assertions::assert_eq_size!(SyscallLoopEntry, *const ());
14
15static SYSCALL_LOOP_ENTRY: AtomicPtr<()> = AtomicPtr::new(std::ptr::null_mut());
16
17/// Initialize the syscall loop entry function.
18pub fn initialize_syscall_loop(enter_loop: SyscallLoopEntry) {
19    SYSCALL_LOOP_ENTRY.store(enter_loop as *mut (), Ordering::Relaxed);
20}
21
22/// Enter the syscall loop on the calling thread.
23///
24/// Returns the final exit status of the task.
25pub(crate) fn enter_syscall_loop(
26    locked: &mut Locked<Unlocked>,
27    current_task: &mut CurrentTask,
28) -> ExitStatus {
29    let raw_entry: *mut () = SYSCALL_LOOP_ENTRY.load(Ordering::Relaxed);
30    assert!(!raw_entry.is_null(), "must call initialize_syscall_loop() before executing tasks");
31    // SAFETY: the static variable only has SyscallLoopEntry values stored into it.
32    let entry: SyscallLoopEntry = unsafe {
33        let raw_entry_ptr = &raw_entry as *const *mut ();
34        let entry_ptr = raw_entry_ptr as *const SyscallLoopEntry;
35        *entry_ptr
36    };
37    entry(locked, current_task)
38}