Skip to content

Commit

Permalink
zephyr-core: fix k_current_get for Zephyr 3.5
Browse files Browse the repository at this point in the history
  • Loading branch information
tylerwhall committed Sep 20, 2024
1 parent bf9f7e3 commit d48c961
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
10 changes: 6 additions & 4 deletions rust/zephyr-core/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@ fn main() {
let kernel_version = u32::from_str_radix(&kernel_version_str_trimmed, 16)
.expect("ZEPHYR_KERNEL_VERSION_NUM must be an integer");

if kernel_version >= 0x3_00_00 {
println!("cargo:rustc-cfg=zephyr300");
}
if kernel_version >= 0x2_05_00 {
println!("cargo:rustc-cfg=zephyr250");
}

if kernel_version >= 0x2_07_00 {
println!("cargo:rustc-cfg=zephyr270");
}
if kernel_version >= 0x3_00_00 {
println!("cargo:rustc-cfg=zephyr300");
}
if kernel_version >= 0x3_05_00 {
println!("cargo:rustc-cfg=zephyr350");
}

if std::env::var("CONFIG_USERSPACE").expect("CONFIG_USERSPACE must be set") == "y" {
println!("cargo:rustc-cfg=usermode");
Expand Down
15 changes: 13 additions & 2 deletions rust/zephyr-core/src/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@ macro_rules! trait_impl {
unsafe { zephyr_sys::syscalls::$context::k_wakeup(thread.tid()) }
}

#[cfg(not(any(zephyr270, zephyr300)))]
/* If less than 2.7, always use the k_current_get syscall */
#[cfg(not(zephyr270))]
fn k_current_get() -> crate::thread::ThreadId {
ThreadId(unsafe {
NonNull::new_unchecked(zephyr_sys::syscalls::$context::k_current_get())
})
}

/* 2.7 and above with TLS allows pulling from TLS */
#[cfg(all(zephyr270, tls))]
fn k_current_get() -> crate::thread::ThreadId {
extern "C" {
Expand All @@ -50,13 +52,22 @@ macro_rules! trait_impl {
})
}

#[cfg(any(zephyr300, all(zephyr270, not(tls))))]
/* 2.7 without TLS renames the syscall to z_current_get */
#[cfg(all(zephyr270, not(zephyr350), not(tls)))]
fn k_current_get() -> crate::thread::ThreadId {
ThreadId(unsafe {
NonNull::new_unchecked(zephyr_sys::syscalls::$context::z_current_get())
})
}

/* 3.5 renames the syscall again to k_sched_current_thread_query */
#[cfg(all(zephyr350, not(tls)))]
fn k_current_get() -> crate::thread::ThreadId {
ThreadId(unsafe {
NonNull::new_unchecked(zephyr_sys::syscalls::$context::k_sched_current_thread_query())
})
}

fn k_object_access_grant<K: KObj>(kobj: &K, thread: ThreadId) {
if !zephyr_sys::raw::RUST_CONFIG_USERSPACE {
// Avoid unnecessary call to stub function
Expand Down

0 comments on commit d48c961

Please sign in to comment.