-
Couldn't load subscription status.
- Fork 8.1k
Add memcpy32 #41273
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 memcpy32 #41273
Conversation
|
I need @alexanderwachter I need some help testing this. |
8c9f69b to
fdf1399
Compare
lib/os/memcpy32.c
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Scary gcc attribute. But I assume all supported toolchains are in CI.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes this is scary, but there is no other way to get around strict aliasing that I know of :(
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we add ZRESTRICT to match memcpy() in lib/libc/minimal/include/string.h?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Scary gcc attribute. But I assume all supported toolchains are in CI.
actually they are not.
Only the Zephyr SDK and clang is in CI, but none of the other commercial toolchains.
Other toolchains are gcc or clang based, so if this works with those two, I expect other toolchain should work.
@pdietl can you ensure that this will be tested by CI for minimum Zephyr SDK and clang.
lib/os/memcpy32.c
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Strange, I thought restrict was only useful if the same memory address was read multiple times.
But we don't read the same address multiple times in this memcpy.
Did you see any changes to the generated machine code with and without the restrict keyword?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Regular memcpy uses restrict in its parameters, so I did the same here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I removed it anyway
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In Zephyr, you can use ZRESTRICT.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Requesting a Kconfig so we don't build this for the vast majority of builds.
|
@henrikbrixandersen huh that's an interesting error. Plz help :) |
|
@pdietl works on the nucleo_g474re START - test_send_and_forget addr2line -a 0x08001cbc -e build/zephyr/zephyr.elf |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does not work on HW
lib/os/memcpy32.c
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| while (len >= 0) { | |
| while (len > 0) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
size_t cannot be negative.
Are you sure ? If not, then we could risk ending in a situation where the when using a As minimum we should ensure Zephyr SDK and |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When building tests/drivers/can/canfd/drivers.canfd for nucleo_g474re I get the following errors (warning turned into errors) with twister:
/projects/github/ncs/zephyr/drivers/can/can_mcan.c: In function 'can_mcan_init':
/projects/github/ncs/zephyr/drivers/can/can_mcan.c:389:2: error: converting a packed 'struct can_mcan_msg_sram' pointer (alignment 1) to a 'uint32_t' {aka 'unsigned int'} pointer (alignment 4) may result in an unaligned pointer value [-Werror=address-of-packed-member]
389 | for (uint32_t *ptr = (uint32_t *)msg_ram;
| ^~~
In file included from /projects/github/ncs/zephyr/drivers/can/can_mcan.c:11:
/projects/github/ncs/zephyr/drivers/can/can_mcan.h:148:8: note: defined here
148 | struct can_mcan_msg_sram {
| ^~~~~~~~~~~~~~~~~
/projects/github/ncs/zephyr/drivers/can/can_mcan.c: In function 'can_mcan_get_message':
/projects/github/ncs/zephyr/drivers/can/can_mcan.c:525:15: error: taking address of packed member of 'struct can_mcan_rx_fifo' may result in an unaligned pointer value [-Werror=address-of-packed-member]
525 | for (src = fifo[get_idx].data_32,
| ^~~~
/projects/github/ncs/zephyr/drivers/can/can_mcan.c: In function 'can_mcan_send':
/projects/github/ncs/zephyr/drivers/can/can_mcan.c:698:11: error: passing argument 1 of 'memcpy32' discards 'volatile' qualifier from pointer target type [-Werror=discarded-qualifiers]
698 | memcpy32(&msg_ram->tx_buffer[put_idx].hdr, &tx_hdr,
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /projects/github/ncs/zephyr/drivers/can/can_mcan.c:7:
/projects/github/ncs/zephyr/include/sys/util.h:385:32: note: expected 'void * restrict' but argument is of type 'volatile struct can_mcan_tx_buffer_hdr *'
385 | void *memcpy32(void *ZRESTRICT dest, const void *ZRESTRICT src, size_t n);
| ~~~~~~~~~~~~~~~~^~~~
/projects/github/ncs/zephyr/drivers/can/can_mcan.c:702:9: error: taking address of packed member of 'struct can_mcan_tx_buffer' may result in an unaligned pointer value [-Werror=address-of-packed-member]
702 | dst = msg_ram->tx_buffer[put_idx].data_32,
| ^~~~~~~
cc1: all warnings being treated as errors
lib/os/memcpy32.c
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Scary gcc attribute. But I assume all supported toolchains are in CI.
actually they are not.
Only the Zephyr SDK and clang is in CI, but none of the other commercial toolchains.
Other toolchains are gcc or clang based, so if this works with those two, I expect other toolchain should work.
@pdietl can you ensure that this will be tested by CI for minimum Zephyr SDK and clang.
|
@tejlmand : If a toolchain does not support this then I assume we will get a reasonable error, so I don't see the need for a depends in the Kconfig. |
|
@SebastianBoe @henrikbrixandersen asked for it to speed up compilation time. Edit: |
except for twister. So just relying on a sane error message build time could impact twister in bad way. But first thing I would like to know is whether or not the |
|
See #40644 for inspiration. |
|
@pdietl Do you plan on picking this up again? |
|
Ok what do I need to do to finish this off @henrikbrixandersen |
|
Since memcpy32_volatile was added already to |
It would be nice to have a generic |
|
Does memcpy32_volatile not use compiler-specific features? If so, re-writing this to use volatile would resolve the main problems with this PR I believe. |
00b3d63 to
9d9f9bd
Compare
Implement memcpy32, a version of memcpy that only performs 32 bit word-wise copying. Signed-off-by: Pete Dietl <petedietl@gmail.com>
Use memcpy32 for word-wise copying Fixes zephyrproject-rtos#41074 Signed-off-by: Pete Dietl <petedietl@gmail.com>
|
@SebastianBoe I don't think so? |
|
@henrikbrixandersen Do you want me to change this version of memcpy32 to be volatile? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Still getting build failures when running twister in tests/drivers/can/canfd/drivers.canfd for nucleo_g474re:
38%] Building C object zephyr/CMakeFiles/zephyr.dir/lib/os/memcpy32.c.obj
/projects/github/ncs/zephyr/lib/os/memcpy32.c: In function 'memcpy32':
/projects/github/ncs/zephyr/lib/os/memcpy32.c:20:14: error: initialization discards 'volatile' qualifier from pointer target type [-Werror=discarded-qualifiers]
20 | char *dst = dst0;
| ^~~~
/projects/github/ncs/zephyr/lib/os/memcpy32.c:21:20: error: initialization discards 'volatile' qualifier from pointer target type [-Werror=discarded-qualifiers]
21 | const char *src = src0;
| ^~~~
cc1: all warnings being treated as errors
make[2]: *** [zephyr/CMakeFiles/zephyr.dir/build.make:398: zephyr/CMakeFiles/zephyr.dir/lib/os/memcpy32.c.obj] Error 1
make[1]: *** [CMakeFiles/Makefile2:2627: zephyr/CMakeFiles/zephyr.dir/all] Error 2
make: *** [Makefile:91: all] Error 2

No description provided.