memcpy crashes when writing to non-cached memory on ARM M7

Bug #1857469 reported by David Crocker on 2019-12-24
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
GNU Arm Embedded Toolchain
Undecided
Unassigned

Bug Description

The version of memcpy in standard newlib (not the one in newlib-nano) crashes when it is used to write to memory that has been tagged as non-cacheable in the MPU. This is because non-cacheable memory only supports aligned accesses. In embedded systems using the ARM Cortex M7 processor, sections of non-cacheable memory are often required, because the write-back cache of the ARM M7 plays havoc with DMA. Although memcpy attempts to do aligned accesses, if the number of bytes to be copied is one less than a multiple of 4 then memcpy uses a 16-bit transfer to copy the last 2 bytes.

Workaround 1: use newlib-nano instead. Unfortunately, newlib-nano as provided is not compatible with programs that use exception handling.

Workaround 2: copy the source code for memcpy from newlib source into your project, and compile it with option -fno-unaligned-access (like the rest of the project that uses non-cached memory). As a precaution I did the same for memset, memmove and memcmp too.

Suggested fix: as it is likely that programs targeting the ARM Cortex M7 will need to use areas of non-cached memory, either all of newlib or just these memory access functions should be compiled with option -fno-unaligned-access when targeting the ARM Cortex M7.

Download full text (3.9 KiB)

Ich bin bis 08.01. nicht im Haus und kann Ihre Nachricht daher leider nicht bearbeiten. In dringenden Fällen wenden Sie sich bitte an <email address hidden> bzw. für technische Fragen an <email address hidden>.

I am out of office until January 8th and won't be able to read your message. In urgent cases, please refer to <email address hidden> or for technical questions to <email address hidden>.

Mit freundlichen Grüßen / Best regards

Steffen Wolfer

--
Dipl.-Inform. Steffen Wolfer
Software Engineer Embedded Systems

WEISS ROBOTICS GmbH & Co. KG
Karl-Heinrich-Käferle-Str. 8
D-71640 Ludwigsburg, Germany

Phone: +49 7141 94702-22
Fax: +49 7141 94702-99
http://www.weiss-robotics.com

Sitz der Gesellschaft: Ludwigsburg
Registergericht Stuttgart, HRA725006

Pers. haftende Gesellschafterin:
Weiss Robotics Verwaltungs-GmbH, Sitz Ludwigsburg
Registergericht Stuttgart, HRB73310
Geschäftsführer: Dr. Karsten Weiß

Public bug reported:

The version of memcpy in standard newlib (not the one in newlib-nano)
crashes when it is used to write to memory that has been tagged as non-
cacheable in the MPU. This is because non-cacheable memory only supports
aligned accesses. In embedded systems using the ARM Cortex M7 processor,
sections of non-cacheable memory are often required, because the write-
back cache of the ARM M7 plays havoc with DMA. Although memcpy attempts
to do aligned accesses, if the number of bytes to be copied is one less
than a multiple of 4 then memcpy uses a 16-bit transfer to copy the last
2 bytes.

Workaround 1: use newlib-nano instead. Unfortunately, newlib-nano as
provided is not compatible with programs that use exception handling.

Workaround 2: copy the source code for memcpy from newlib source into
your project, and compile it with option -fno-unaligned-access (like the
rest of the project that uses non-cached memory). As a precaution I did
the same for memset, memmove and memcmp too.

Suggested fix: as it is likely that programs targeting the ARM Cortex M7
will need to use areas of non-cached memory, either all of newlib or
just these memory access functions should be compiled with option -fno-
unaligned-access when targeting the ARM Cortex M7.

** Affects: gcc-arm-embedded
     Importance: Undecided
         Status: New

** Tags: memcpy

--
You received this bug notification because you are subscribed to GNU Arm
Embedded Toolchain.
Matching subscriptions: Älles
https://bugs.launchpad.net/bugs/1857469

Title:
  memcpy crashes when writing to non-cached memory on ARM M7

Status in GNU Arm Embedded Toolchain:
  New

Bug description:
  The version of memcpy in standard newlib (not the one in newlib-nano)
  crashes when it is used to write to memory that has been tagged as
  non-cacheable in the MPU. This is because non-cacheable memory only
  supports aligned accesses. In embedded systems using the ARM Cortex M7
  processor, sections of non-cacheable memory are often required,
  because the write-back cache of the ARM M7 plays havoc with DMA.
  Although memcpy attempts to do aligned accesses, if the number of
  bytes to be copied is one less than a multiple of 4 then memcpy uses a
  16-bit transfer to copy the la...

Read more...

Darius Babrauskas (darius1) wrote :

Hi,
I am working with ARM Cortex M7 too.
My notes:
>>Workaround 1: use newlib-nano instead. Unfortunately, newlib-nano as provided is not compatible with programs that use exception handling.

if use newlib-nano and non-cacheable memory and optimization -Og, then your project need compile with option -fno-unaligned-access, because memcpy(ps,pd,4) after optimization became *(int32_t*)ps = *(int32_t*)pd; So if address not aligned we got alignment exception. Same with strcpy ...

>> Suggested fix: as it is likely that programs targeting the ARM Cortex M7
will need to use areas of non-cached memory, either all of newlib or
just these memory access functions should be compiled with option -fno-
unaligned-access when targeting the ARM Cortex M7.

Yes I agree. My opinion, need version of newlib-nano compiled with -fno-
unaligned-access.

To post a comment you must log in.
This report contains Public information  Edit
Everyone can see this information.

Other bug subscribers