Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Zephyr kernel objects #5

Merged
merged 26 commits into from
Oct 25, 2024
Merged

Support for Zephyr kernel objects #5

merged 26 commits into from
Oct 25, 2024

Conversation

d3zd3z
Copy link
Collaborator

@d3zd3z d3zd3z commented Sep 17, 2024

This PR adds a Rust abstraction around Zephy's kernel objects. This includes a mechanism to use statically declared kernel objects, as well as the possibility of dynamically allocated objects, managed as a pool for each type.

The initial kernel object supported is the k_mutex, which is managed by the rust type sys::Mutex. This is a thin wrapper around k_mutex, and can be used for simple coordination. It, however, does not manage Send/Sync aspects that are handled by std::sync::Mutex, which functionality will be provided in a later PR.

Closes #16, Closes #17, Closes #18, Closes #19, Closes #20, Closes #21

@d3zd3z d3zd3z force-pushed the kobj branch 2 times, most recently from 1780b80 to 2400c74 Compare September 18, 2024 19:49
@d3zd3z d3zd3z marked this pull request as ready for review October 8, 2024 17:35
@d3zd3z d3zd3z requested review from teburd, cfriedt and ithinuel October 8, 2024 18:21
@d3zd3z d3zd3z self-assigned this Oct 8, 2024
Kconfig Outdated Show resolved Hide resolved
zephyr/src/alloc_impl.rs Outdated Show resolved Hide resolved
@hakehuang
Copy link

hakehuang commented Oct 9, 2024

any hints on build? I get below error

error: RUST (defined at modules/Kconfig.rust:4) is assigned in a configuration file, but is not
directly user-configurable (has no prompt). It gets its value indirectly from other symbols. See
http://docs.zephyrproject.org/latest/kconfig.html#CONFIG_RUST and/or look up RUST in the
menuconfig/guiconfig interface. The Application Development Primer, Setting Configuration Values,
and Kconfig - Tips and Best Practices sections of the manual might be helpful too.

Add a synchronizer for forks based on sending messages over channels to
a worker thread.

Signed-off-by: David Brown <[email protected]>
Rather than just printing a bunch of information out as the various
philosopher threads dine, use some data protected within a Mutex to
collect statistics, and print those out periodically.

Signed-off-by: David Brown <[email protected]>
Although this function has constraints on how it can be used (the thread
that calls unlock must also have called lock).  However, according to
the documentation, it detects this, and returns an error.  As such, the
wrapper in Rust does not need to be `unsafe` but can merely reflect that
error code in the `Result` that it returns.

Signed-off-by: David Brown <[email protected]>
Now that `sys::Mutex::unlock` has lost its `unsafe`, we don't need an
unsafe block for it.

Signed-off-by: David Brown <[email protected]>
This function returns initialized memory, and is therefore inherently
unsafe.  Added some commentary about how it is used safely.

Signed-off-by: David Brown <[email protected]>
Move the Wrapped trait above the StaticKernelObject so that the traits
are immediately declared after the type the apply to.

Signed-off-by: David Brown <[email protected]>
Although these, in their current state, are safe to Clone, having these
semantics will make it difficult for us to later add these types that
are allocated from a pool.

Uses that currently expect to clone can generally wrap these in an Arc,
to allow for the sharing.

Signed-off-by: David Brown <[email protected]>
@d3zd3z
Copy link
Collaborator Author

d3zd3z commented Oct 22, 2024

Nothing new, just a rebase on top of the thread fix.

d3zd3z added 10 commits October 24, 2024 10:41
The `Fixed` type encapsulates something that can either be a statically
allocated object or a dynamically allocated one.  It is conditional on
`CONFIG_RUST_ALLOC`, and if that is not defined, will just end up
represented as the underlying static pointer.

Signed-off-by: David Brown <[email protected]>
Add support for dynamically allocated Semaphores.  These can be freely
allocated, but are not usable from userspace.

Signed-off-by: David Brown <[email protected]>
Add a sample that implements the forks using dynamically allocate
semaphores.

Signed-off-by: David Brown <[email protected]>
Add support for dynamically allocated sys::Mutexes

Signed-off-by: David Brown <[email protected]>
Switch to using dynamic Mutex types for this demo.

Signed-off-by: David Brown <[email protected]>
Add support for dynamically allocate sys::Queues.

Signed-off-by: David Brown <[email protected]>
Implement `unbounded` that is able to dynamically allocate the
underlying Zephyr queue.  This is useful in cases where none of the
queue access happens from userspace.

Signed-off-by: David Brown <[email protected]>
Use the new dynamically allocated Channels to simplify the code.

Signed-off-by: David Brown <[email protected]>
Add dynamic allocation support to the sync Mutex and Condvar.  When
allocation is available, and userspace is not used, these can be used
pretty much the same as those from std.

Signed-off-by: David Brown <[email protected]>
Use the simpler `new` methods instead of static allocaiton.

Signed-off-by: David Brown <[email protected]>
@d3zd3z
Copy link
Collaborator Author

d3zd3z commented Oct 24, 2024

This push adds support for dynamically allocating most of the kernel objects. There are restrictions, mainly that the dynamically allocate objects can't be used from userspace, but this restriction is the same for C code on Zephyr. And, possibly obviously, the dynamically allocate objects require dynamic allocation to be enabled.

@cfriedt
Copy link
Member

cfriedt commented Oct 24, 2024

Still looks reasonable to me. Sorry - I've been reviewing intermittently all morning getting distracted by the odd meeting.

@d3zd3z d3zd3z force-pushed the kobj branch 4 times, most recently from e7d869d to aa1e082 Compare October 25, 2024 16:42
Add -M full to the twister build so that it cleans up space as it goes.
This should allow the build to run to completion.

Add -j4 to the build. Although the CI images currently have 4 cpus, this
will help protect us against future ones that have more.

Give twister an explicit list of platforms.  This fixes two issues.  One
is that the output is getting flooded with messages about skipped tests.
The other is this allows us to add platforms that qemu seems to be
ignoring entirely.

Signed-off-by: David Brown <[email protected]>
@d3zd3z d3zd3z merged commit 37c77e2 into main Oct 25, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Development

Successfully merging this pull request may close these issues.

simple log to printk sync::channel, unbounded sync::Mutex/Condvar sys Queue interface sys Mutex/Condvar
6 participants