Skip to main content

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 std::sync::atomic::{AtomicPtr, Ordering};
7
8/// The type of function that must be provided by the kernel binary to enter the syscall loop.
9pub type SyscallLoopEntry = fn(&mut CurrentTask) -> ExitStatus;
10
11// Need to make sure the function pointer is actually just a pointer to store it safely in atomic.
12static_assertions::assert_eq_size!(SyscallLoopEntry, *const ());
13
14static SYSCALL_LOOP_ENTRY: AtomicPtr<()> = AtomicPtr::new(std::ptr::null_mut());
15
16/// Initialize the syscall loop entry function.
17pub fn initialize_syscall_loop(enter_loop: SyscallLoopEntry) {
18    SYSCALL_LOOP_ENTRY.store(enter_loop as *mut (), Ordering::Relaxed);
19}
20
21/// Enter the syscall loop on the calling thread.
22///
23/// Returns the final exit status of the task.
24pub(crate) fn enter_syscall_loop(current_task: &mut CurrentTask) -> ExitStatus {
25    let raw_entry: *mut () = SYSCALL_LOOP_ENTRY.load(Ordering::Relaxed);
26    assert!(!raw_entry.is_null(), "must call initialize_syscall_loop() before executing tasks");
27    // SAFETY: the static variable only has SyscallLoopEntry values stored into it.
28    let entry: SyscallLoopEntry = unsafe {
29        let raw_entry_ptr = &raw_entry as *const *mut ();
30        let entry_ptr = raw_entry_ptr as *const SyscallLoopEntry;
31        *entry_ptr
32    };
33    entry(current_task)
34}