From d48c961c880628518481a85971f941e78a66f48c Mon Sep 17 00:00:00 2001 From: Tyler Hall Date: Sun, 30 Jun 2024 21:17:29 -0400 Subject: [PATCH] zephyr-core: fix k_current_get for Zephyr 3.5 --- rust/zephyr-core/build.rs | 10 ++++++---- rust/zephyr-core/src/thread.rs | 15 +++++++++++++-- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/rust/zephyr-core/build.rs b/rust/zephyr-core/build.rs index 750c4ac..fbf2bb1 100644 --- a/rust/zephyr-core/build.rs +++ b/rust/zephyr-core/build.rs @@ -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"); diff --git a/rust/zephyr-core/src/thread.rs b/rust/zephyr-core/src/thread.rs index 80ee46a..e7a99f9 100644 --- a/rust/zephyr-core/src/thread.rs +++ b/rust/zephyr-core/src/thread.rs @@ -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" { @@ -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(kobj: &K, thread: ThreadId) { if !zephyr_sys::raw::RUST_CONFIG_USERSPACE { // Avoid unnecessary call to stub function