sel4_initialize_tls/
set_thread_pointer.rs

1//
2// Copyright 2024, Colias Group, LLC
3//
4// SPDX-License-Identifier: BSD-2-Clause
5//
6
7use core::arch::asm;
8
9use cfg_if::cfg_if;
10
11pub type SetThreadPointerFn = unsafe extern "C" fn(thread_pointer: usize);
12
13pub const DEFAULT_SET_THREAD_POINTER_FN: SetThreadPointerFn = default_set_thread_pointer;
14
15unsafe extern "C" fn default_set_thread_pointer(thread_pointer: usize) {
16    let val = thread_pointer;
17
18    cfg_if! {
19        if #[cfg(target_arch = "aarch64")] {
20            asm!("msr tpidr_el0, {val}", val = in(reg) val);
21        } else if #[cfg(target_arch = "arm")] {
22            asm!("mcr p15, 0, {val}, c13, c0, 2", val = in(reg) val); // tpidrurw
23        } else if #[cfg(any(target_arch = "riscv32", target_arch = "riscv64"))] {
24            asm!("mv tp, {val}", val = in(reg) val);
25        } else if #[cfg(target_arch = "x86_64")] {
26            asm!("wrfsbase {val}", val = in(reg) val);
27        } else {
28            compile_error!("unsupported architecture");
29        }
30    }
31}