Skip to content

Commit 38b0d9a

Browse files
committed
zephyr: memdomain api
1 parent 9b8a432 commit 38b0d9a

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

rust-app/zephyr/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ extern crate derive_more;
66

77
pub mod device;
88
pub mod kobj;
9+
pub mod memdomain;
910
pub mod mempool;
1011
pub mod mutex;
1112
pub mod semaphore;

rust-app/zephyr/src/memdomain.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#[cfg(not(usermode))]
2+
use core::marker::PhantomData;
3+
4+
pub use zephyr_sys::raw::k_mem_domain;
5+
6+
use crate::thread::ThreadId;
7+
8+
#[cfg(usermode)]
9+
pub struct MemDomain<'a>(&'a k_mem_domain);
10+
#[cfg(not(usermode))]
11+
pub struct MemDomain<'a>(PhantomData<&'a ()>);
12+
13+
impl<'a> MemDomain<'a> {
14+
#[cfg(usermode)]
15+
pub unsafe fn new(domain: &'a k_mem_domain) -> Self {
16+
MemDomain(domain)
17+
}
18+
#[cfg(not(usermode))]
19+
pub fn new() -> Self {
20+
MemDomain(PhantomData)
21+
}
22+
23+
pub fn add_thread<C: MemDomainAPI>(&self, _thread: ThreadId) {
24+
#[cfg(usermode)]
25+
C::k_mem_domain_add_thread(self.0, _thread)
26+
}
27+
}
28+
29+
pub trait MemDomainAPI {
30+
fn k_mem_domain_add_thread(domain: &k_mem_domain, thread: ThreadId);
31+
}
32+
33+
impl MemDomainAPI for crate::context::Kernel {
34+
fn k_mem_domain_add_thread(domain: &k_mem_domain, thread: ThreadId) {
35+
unsafe {
36+
zephyr_sys::raw::k_mem_domain_add_thread(domain as *const _ as *mut _, thread.tid())
37+
}
38+
}
39+
}
40+
41+
/// Get a static reference to an external mem domain
42+
#[cfg(usermode)]
43+
#[macro_export]
44+
macro_rules! static_mem_domain {
45+
($domain:ident) => {{
46+
extern "C" {
47+
#[no_mangle]
48+
static $domain: $crate::memdomain::k_mem_domain;
49+
}
50+
51+
unsafe { $crate::memdomain::MemDomain::new(&$domain) }
52+
}};
53+
}
54+
/// Get a static reference to an external mem domain
55+
#[cfg(not(usermode))]
56+
#[macro_export]
57+
macro_rules! static_mem_domain {
58+
($domain:ident) => {{
59+
$crate::memdomain::MemDomain::new()
60+
}};
61+
}

rust-app/zephyr/src/thread.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ use crate::kobj::KObj;
44
pub struct ThreadId(zephyr_sys::raw::k_tid_t);
55

66
impl ThreadId {
7+
pub fn tid(&self) -> zephyr_sys::raw::k_tid_t {
8+
self.0
9+
}
10+
711
pub fn k_object_access_grant<C: ThreadSyscalls, K: KObj>(&self, kobj: &K) {
812
C::k_object_access_grant(kobj, *self)
913
}

0 commit comments

Comments
 (0)