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

compiling Mbed TLS for cortex-m0 with optimization and ARMC6 compiler results in error #1077

Closed
RonEld opened this issue Sep 4, 2017 · 8 comments · Fixed by #7651
Closed
Labels
bug component-crypto Crypto primitives and low-level interfaces help-wanted This issue is not being actively worked on, but PRs welcome. historical-reviewing Currently reviewing (for legacy PR/issues)

Comments

@RonEld
Copy link
Contributor

RonEld commented Sep 4, 2017

Description

  • Type: Bug
  • Priority: Major
    compilation error with ARMC6 toolchain, on cortex-m0 and optimization flag

Bug

OS
linux

mbed TLS build:
Version: 2.6.0
OS version: Ubuntu 16.04

ARMC6
Additional environment information:

Expected behavior
compilation to succeed
Actual behavior
compilation error:

./library/bignum.c:1130:9: error: inline assembly requires more registers than available
        MULADDC_INIT
        ^
./include/mbedtls/bn_mul.h:581:13: note: expanded from macro 'MULADDC_INIT'
            "ldr    r0, %3                      \n\t"   \
            ^
./library/bignum.c:1145:9: error: inline assembly requires more registers than available
        MULADDC_INIT
        ^

Steps to reproduce
compile using ARMC6 with optimization, such as -Os
Reproduces only for cortex-m0

armclang --target=arm-arm-none-eabi -Os -mthumb -Wno-armcc-pragma-push-pop -Wno-armcc-pragma-anon-unions -mcpu=cortex-m0 -march=armv7-m -I./include -D__ASSERT_MSG -std=gnu99 -o library/bignum.o ./library/bignum.c

Note:
Can be worked around using -DMULADDC_CANNOT_USE_R7 compilation flag, or without optimization flag, but this will compile without the assmebly optimization

@ciarmcom
Copy link

ciarmcom commented Sep 4, 2017

ARM Internal Ref: IOTSSL-1704

@mpg
Copy link
Contributor

mpg commented Sep 5, 2017

By the way, could we test how big a difference this assembly "optimization" makes? I'm not sure it's that significant.

(Of course this issue still needs to be fixed. It's just that we seem to assume this assembly is very important to have, while I'm firmly of the opinion that nothing is faster until you actually measure it.)

@RonEld
Copy link
Contributor Author

RonEld commented Aug 20, 2018

@mpg #1964 shows results of benchmark application with and without the assembly, for a 64 bit limb on bignum. You can see that there is significant improvement in the performance, mostly in RSA

@mpg
Copy link
Contributor

mpg commented Sep 6, 2018

While the measurements in #1964 are a very interesting data point, V8-A and V6-M are pretty different architectures performance-wise, for example in terms the multiplication instructions they have or the number of registers. Also, obviously the performance of handwritten assembly depends quite a lot on how it's written, and how much in improves compare to compiler-generated code depends on how good the compiler backend is for that particular platform and that particular kind of operation (sometimes C cannot express the best way a CPU could perform an operation, sometimes it can). So I'm not sure how much one can extrapolate from one case to the other.

Don't get me wrong, I'm not saying this particular bit of asm on this particular platform is not worth it, I'm just saying I'd like us to make decisions based on actual specific data. I assume the person who contributed #1964 would agree, because they started by showing us some specific data right away. I'm just saying I'd like to have the same kind of data here too.

OTOH, after looking a bit at this bit of asm, I'm thinking fixing could be as easy as using some other register number: it looks like r10 to r12 would be free for use in the thumb case, and even r8-r12 in the thumb2 case. (It seems the author of this asm just used registers in order, perhaps not being aware of r7's special role for some compilers with some options.) If fixing is that easy, I'd be happy with a "fix first, collect data later" strategy, but I'm still interested in some data at some point.

@RonEld RonEld added the component-crypto Crypto primitives and low-level interfaces label Feb 17, 2019
@RonEld RonEld added archived Do not use - historically applied to archived issues help-wanted This issue is not being actively worked on, but PRs welcome. and removed help-wanted This issue is not being actively worked on, but PRs welcome. tracking labels Jun 13, 2019
@RonEld RonEld closed this as completed Jun 13, 2019
@Patater
Copy link
Contributor

Patater commented Jun 18, 2019

Reopening due to interest in fixing this. We shouldn't fail on Cortex-M0 with the default configuration.

@Patater Patater reopened this Jun 18, 2019
@Patater Patater removed archived Do not use - historically applied to archived issues closed_in_jira labels Jun 18, 2019
mame2015 pushed a commit to mame2015/TFM-M that referenced this issue Jul 10, 2019
According to [1], add compiler flag -DMULADDC_CANNOT_USE_R7 in
mbed-crypto/mbedtls building on Armv6-M, to work around builidng
issues.

[1]: Mbed-TLS/mbedtls#1077

Change-Id: I091f7c93a7d275045a7ec17d39e692b27e0544e3
Signed-off-by: David Hu <[email protected]>
wschang0 pushed a commit to wschang0/trusted-firmware-m-m2351 that referenced this issue Nov 20, 2019
According to [1], add compiler flag -DMULADDC_CANNOT_USE_R7 in
mbed-crypto/mbedtls building on Armv6-M, to work around builidng
issues.

[1]: Mbed-TLS/mbedtls#1077

Change-Id: I091f7c93a7d275045a7ec17d39e692b27e0544e3
Signed-off-by: David Hu <[email protected]>
LDong-Arm added a commit to LDong-Arm/mbed-os that referenced this issue Apr 9, 2021
Due to a known issue in Mbed TLS's architecture determination
(Mbed-TLS/mbedtls#1077), we get the error

    error: inline assembly requires more registers than available

when compiling `bignum.c` for Cortex-M0/0+/1,

The workaround is to define the macro `MULADDC_CANNOT_USE_R7` which
is already defined by Mbed CLI 1 but missing in our CMake support.

Fixes ARMmbed/mbed-os-example-lorawan#220
LDong-Arm added a commit to LDong-Arm/mbed-os that referenced this issue Apr 9, 2021
Due to a known issue in Mbed TLS's architecture determination
(Mbed-TLS/mbedtls#1077), we get the error

    error: inline assembly requires more registers than available

when compiling `bignum.c` for Cortex-M0/0+/1,

The workaround is to define the macro `MULADDC_CANNOT_USE_R7` which
is already defined by Mbed CLI 1 but missing in our CMake support.

Fixes ARMmbed/mbed-os-example-lorawan#220
LDong-Arm added a commit to LDong-Arm/mbed-os that referenced this issue Apr 15, 2021
Due to a known issue in Mbed TLS's architecture determination
(Mbed-TLS/mbedtls#1077), we get the error

    error: inline assembly requires more registers than available

when compiling `bignum.c` for Cortex-M0/0+/1/M23 which do not have
the macro `__thumb2__` set by the compiler.

The workaround is to define the macro `MULADDC_CANNOT_USE_R7` which
is already defined by Mbed CLI 1 but missing in our CMake support.

Fixes ARMmbed/mbed-os-example-lorawan#220
pennam pushed a commit to pennam/mbed-os that referenced this issue Apr 23, 2021
Due to a known issue in Mbed TLS's architecture determination
(Mbed-TLS/mbedtls#1077), we get the error

    error: inline assembly requires more registers than available

when compiling `bignum.c` for Cortex-M0/0+/1/M23 which do not have
the macro `__thumb2__` set by the compiler.

The workaround is to define the macro `MULADDC_CANNOT_USE_R7` which
is already defined by Mbed CLI 1 but missing in our CMake support.

Fixes ARMmbed/mbed-os-example-lorawan#220
@tom-cosgrove-arm tom-cosgrove-arm added the historical-reviewing Currently reviewing (for legacy PR/issues) label Nov 11, 2022
@daverodgman
Copy link
Contributor

make lib CFLAGS="-Os --target=arm-arm-none-eabi -mcpu=cortex-m0" CC=armclang still fails on latest development

@mpg
Copy link
Contributor

mpg commented May 24, 2023

OTOH, after looking a bit at this bit of asm, I'm thinking fixing could be as easy as using some other register number: it looks like r10 to r12 would be free for use in the thumb case, and even r8-r12 in the thumb2 case.

I don't have time to try it atm, but if someone wants to try s/r7/r10/g in the offending bit of assembly (lines 680-731 of bn_mul.h in current development if I'm not mistaken) and see if that fixes it, that's probably worth a try.

@daverodgman
Copy link
Contributor

We still get a bunch of errors with Manuel's suggestion, I think because the likes of M0 don't support all of Thumb2. So I think the changes would have to be a lot more extensive than simply using a different register.

minosgalanakis pushed a commit that referenced this issue Oct 5, 2023
2.28 backport - Use CT module more consistently
openci-bot pushed a commit to TrustedFirmware-M/trusted-firmware-m that referenced this issue May 13, 2024
The workaround for Mbed-TLS/mbedtls#1077
is not required anymore in BL2/Crypto service Mbed TLS builds
as the issue has been fixed since 2023

Signed-off-by: Antonio de Angelis <[email protected]>
Change-Id: I1a9cb0f83d6b1876d00b98e87700dea9c2097616
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug component-crypto Crypto primitives and low-level interfaces help-wanted This issue is not being actively worked on, but PRs welcome. historical-reviewing Currently reviewing (for legacy PR/issues)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants