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

Add support for Zephyr's POSIX architecture #19

Merged
merged 3 commits into from
Oct 24, 2023

Conversation

aescolar
Copy link
Member

@aescolar aescolar commented Oct 11, 2023

Zephyr's "POSIX architecture" is a test architecture which allows building the embedded SW with the Zephyr OS and HW models as a Linux executable.

When building for this target the Zephyr build
system will set the PROJECT_PROCESSOR as "posix".

We need to provide appropriate replacements
for the processor HAL for this "processor".
The "metal_cpu_yield()" call, which in other
architectures is handled as doing nothing, and therefore busy waiting, needs replacing with a call out to
Z_SPIN_DELAY(1)

@arnopo
Copy link
Collaborator

arnopo commented Oct 11, 2023

Hi @aescolar

the libmetal folder is aligned with the code in https://github.com/OpenAMP/libmetal. Therefore, we would like to avoid adding patches on top of itn as it would require rebases.

That said, Any reason to not address this in https://github.com/OpenAMP/libmetal/blob/main/lib/system/zephyr?
and perhaps in https://github.com/OpenAMP/libmetal/edit/main/cmake/options.cmake?

@aescolar
Copy link
Member Author

aescolar commented Oct 11, 2023

Any reason to not address this in https://github.com/OpenAMP/libmetal/blob/main/lib/system/zephyr?

I'm certainly open to that. What would you recommend?
To add a lib/system/zephyr/cpu.h and have it conditionally included if("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "zephyr_native")?
And in either lib/CMakeLists.txt or lib/processor/CMakeLists.txt have the processor directory excluded in that case? (and similarly have lib/cpu.h excluded)

@aescolar
Copy link
Member Author

@arnopo would something like this roughly what you had in mind? (if not please just tell how you'd like it)
If it is, please tell, and I will update the corresponding PR in the openamp/libmetal repo

@aescolar
Copy link
Member Author

@arnopo
Or maybe as a variation: aescolar@d666f16

@arnopo
Copy link
Collaborator

arnopo commented Oct 12, 2023

I took more time to analyze solutions. there are different options...
First I have some questions:

Do you need some other interface than the metal_cpu_yield ?
Is k_busy_wait could be replaced by k_sleep()
Here is one option that seem simple to implement with current design of the libmetal:
->Create libmetal/lib/processor/posix processor
This folder would contains a cpu.h implementing following functionI take more time to analyse solution. there are different options...
First I have some questions:

  • Do you need some other interface than the metal_cpu_yield ?
  • Is k_busy_wait could be replaced by k_sleep()

Here is one option that seem simple to implement with current design of the libmetal:
->Create libmetal/lib/processor/posix processor
This folder would contains a cpu.h implementing following function

static inline void metal_cpu_yield(void)
{
	/*
	 *  Native test environment let 1 microsecond pass
	 *  to allow other threads to run
	 */
	metal_sleep_usec(1); /* I assume you can use the sleep method */
};

With such implementation, you have just to create a machine that will rely on Zephyr metal_sleep_usec() system implementation available here:
https://github.com/OpenAMP/libmetal/blob/main/lib/system/zephyr/sleep.h#L25

@aescolar
Copy link
Member Author

aescolar commented Oct 13, 2023

@arnopo Thank you very much for looking into it, I really appreciate it :)

Do you need some other interface than the metal_cpu_yield ?

It seems the only trouble are the "processor" headers. And for these, only providing metal_cpu_yield() seems necessary.
processor/<..>/atomic.h would not be strictly necessary.

Is k_busy_wait could be replaced by k_sleep()

It would be possible, though not too nice. Zephyr's k_sleep() has a resolution of the system tick period (10ms by default), so calling metal_sleep_usec(1), will sleep between 10ms and 20ms by default (K_USEC() ceils the input value to a multiple of the system tick period).
So if we use this call, we would have a quite different behavior when building for this target.

Due to that, it would be nicer to call either zephyr's Z_SPIN_DELAY(1) or k_busy_wait(1).

If you want, we can have this special processor metal_cpu_yield() call into a __metal_cpu_yield(), and require the system header to define it if they support this target.

Create libmetal/lib/processor/posix processor

If you think that is the best way to go, I can certainly do that. Though if you feel like it would be better to not have a weird name in the processor folder like "posix", but would prefer something a bit more descriptive like "hosted" I can also do that.
Note that setting the processor to "posix" is something the zephyr build system does, and is a quite arbitrary choice. So we can have that be renamed to whatever you'd want before calling into the libmetal cmake.

If you want we can have a chat in discord (aescolar) or Teams.

I updated this PR with something you may like.

Copy link
Collaborator

@arnopo arnopo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made some remarks here but please push on libmetal github for next reviews

libmetal/lib/processor/hosted/CMakeLists.txt Outdated Show resolved Hide resolved
libmetal/lib/processor/hosted/atomic.h Outdated Show resolved Hide resolved
libmetal/lib/processor/hosted/atomic.h Show resolved Hide resolved
libmetal/lib/processor/hosted/cpu.h Outdated Show resolved Hide resolved
libmetal/lib/processor/hosted/cpu.h Outdated Show resolved Hide resolved
libmetal/lib/system/zephyr/sleep.h Outdated Show resolved Hide resolved
@aescolar aescolar force-pushed the posix_arch_support branch 2 times, most recently from 9b5a86e to 96ef92d Compare October 13, 2023 16:39
@aescolar
Copy link
Member Author

Discussion moved to OpenAMP/libmetal#260

Add a new hosted test environment "processor".

This is meant as a build target used when the
code is not built for a real target
but as part of a test in a hosted environment
(for ex. as a test environment like Zephyr's
native_sim target).

When building for this target PROJECT_PROCESSOR
should be set as "hosted".

In this, the "__metal_cpu_yield()" call
is expected to be provided by the system folder
headers, to provide whatever is needed for the
metal_cpu_yield() function to perform its duty.

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
When building for CMAKE_SYSTEM_PROCESSOR will be set to
"posix" which is not usefull. Let's rename it instead
to something else we can use.

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
@aescolar aescolar marked this pull request as ready for review October 19, 2023 13:31
@aescolar
Copy link
Member Author

aescolar commented Oct 19, 2023

As libmetal is now frozen until the end of the month, it would be very nice to make an exception and allow this patch into the zephyr module. Without this fix, it is not possible to build libmetal, and therefore openAMP for the native targets, including the simulated nrf5340.
And without that, we cannot test in CI all configurations which depend on openAMP, which is in turn all configuration which use the split BT and 15.4 stacks.
Once this patch is in, it will be followed right away by the corresponding Zephyr patch to enable this functionality (zephyrproject-rtos/zephyr#63803)
Note that after this we will be running in CI (not just building) quite a lot of configurations using openAMP which should provide nice runtime coverage in Zephyr's CI both for libmetal and OpenAMP (apart from all the code which now we will be able to test, which is this split configuration of the BT and 802.15.4 stacks, and a number of sample and test applications).
This change is quite isolated, so I hope it will be acceptable to take it in ahead of its upstream.

@arnopo
Copy link
Collaborator

arnopo commented Oct 19, 2023

I'm Ok with that, @carlocaione any concerns?
On my side I will push to have more reviews on OpenAMP to be able to merge it ASAP in the lib

Copy link
Collaborator

@carlocaione carlocaione left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No particular concern

@aescolar
Copy link
Member Author

@arnopo @carlocaione should this be merged?

@carlocaione
Copy link
Collaborator

@aescolar with the openamp PR we rushed a bit but this time I'd like to do things properly. Can you open a zephyr PR with a manifest change pointing to this PR? After the Zephyr PR is greed, I will merge this.

@aescolar
Copy link
Member Author

aescolar commented Oct 24, 2023

@carlocaione This is the PR that tests with this module update: zephyrproject-rtos/zephyr#63803
CI is green :)

@carlocaione
Copy link
Collaborator

@carlocaione This is the PR that tests with this module update: zephyrproject-rtos/zephyr#63803 CI is green :)

oooh ok, I missed that.

@carlocaione carlocaione merged commit 03140d7 into zephyrproject-rtos:master Oct 24, 2023
@aescolar
Copy link
Member Author

@carlocaione Thanks! :)

@aescolar aescolar deleted the posix_arch_support branch October 24, 2023 08:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants