From a464ffc2156a746d472fc2923d38324bdd06965e Mon Sep 17 00:00:00 2001 From: Mihail Ionescu Date: Wed, 15 Jan 2020 11:45:53 +0000 Subject: [PATCH, GCC/ARM, 9/10] Call nscall function with blxns This change to use BLXNS to call a nonsecure function from secure directly (not using a libcall) is made in 2 steps: - change nonsecure_call patterns to use blxns instead of calling __gnu_cmse_nonsecure_call - loosen requirement for function address to allow any register when doing BLXNS. The former is a straightforward check over whether instructions added in Armv8.1-M Mainline are available while the latter consist in making the nonsecure call pattern accept any register by using match_operand and changing the nonsecure_call_internal expander to no force r4 when targeting Armv8.1-M Mainline. The tricky bit is actually in the test update, specifically how to check that register lists for CLRM have all registers except for the one holding parameters (already done) and the one holding the address used by BLXNS. This is achieved with 3 scan-assembler directives. 1) The first one lists all registers that can appear in CLRM but make each of them optional. Property guaranteed: no wrong register is cleared and none appears twice in the register list. 2) The second directive check that the CLRM is made of a fixed number of the right registers to be cleared. The number used is the number of registers that could contain a secret minus one (used to hold the address of the function to call. Property guaranteed: register list has the right number of registers Cumulated property guaranteed: only registers with a potential secret are cleared and they are all listed but ont 3) The last directive checks that we cannot find a CLRM with a register in it that also appears in BLXNS. This is check via the use of a back-reference on any of the allowed register in CLRM, the back-reference enforcing that whatever register match in CLRM must be the same in the BLXNS. Property guaranteed: register used for BLXNS is different from registers cleared in CLRM. Some more care needs to happen for the gcc.target/arm/cmse/cmse-1.c testcase due to there being two CLRM generated. To ensure the third directive match the right CLRM to the BLXNS, a negative lookahead is used between the CLRM register list and the BLXNS. The way negative lookahead work is by matching the *position* where a given regular expression does not match. In this case, since it comes after the CLRM register list it is requesting that what comes after the register list does not have a CLRM again followed by BLXNS. This guarantees that the .*blxns after only matches a blxns without another CLRM before. *** gcc/ChangeLog *** 2020-01-16 Mihail-Calin Ionescu 2020-01-16 Thomas Preud'homme * config/arm/arm.md (nonsecure_call_internal): Do not force memory address in r4 when targeting Armv8.1-M Mainline. (nonsecure_call_value_internal): Likewise. * config/arm/thumb2.md (nonsecure_call_reg_thumb2): Make memory address a register match_operand again. Emit BLXNS when targeting Armv8.1-M Mainline. (nonsecure_call_value_reg_thumb2): Likewise. *** gcc/testsuite/ChangeLog *** 2020-01-16 Mihail-Calin Ionescu 2020-01-16 Thomas Preud'homme * gcc.target/arm/cmse/cmse-1.c: Add check for BLXNS when instructions introduced in Armv8.1-M Mainline Security Extensions are available and restrict checks for libcall to __gnu_cmse_nonsecure_call to Armv8-M targets only. Adapt CLRM check to verify register used for BLXNS is not in the CLRM register list. * gcc.target/arm/cmse/cmse-14.c: Likewise. * gcc.target/arm/cmse/mainline/8_1m/bitfield-4.c: Likewise and adapt check for LSB clearing bit to be using the same register as BLXNS when targeting Armv8.1-M Mainline. * gcc.target/arm/cmse/mainline/8_1m/bitfield-5.c: Likewise. * gcc.target/arm/cmse/mainline/8_1m/bitfield-6.c: Likewise. * gcc.target/arm/cmse/mainline/8_1m/bitfield-7.c: Likewise. * gcc.target/arm/cmse/mainline/8_1m/bitfield-8.c: Likewise. * gcc.target/arm/cmse/mainline/8_1m/bitfield-9.c: Likewise. * gcc.target/arm/cmse/mainline/8_1m/bitfield-and-union.c: Likewise. * gcc.target/arm/cmse/mainline/8_1m/hard-sp/cmse-13.c: Likewise. * gcc.target/arm/cmse/mainline/8_1m/hard-sp/cmse-7.c: Likewise. * gcc.target/arm/cmse/mainline/8_1m/hard-sp/cmse-8.c: Likewise. * gcc.target/arm/cmse/mainline/8_1m/hard/cmse-13.c: Likewise. * gcc.target/arm/cmse/mainline/8_1m/hard/cmse-7.c: Likewise. * gcc.target/arm/cmse/mainline/8_1m/hard/cmse-8.c: Likewise. * gcc.target/arm/cmse/mainline/8_1m/soft/cmse-13.c: Likewise. * gcc.target/arm/cmse/mainline/8_1m/soft/cmse-7.c: Likewise. * gcc.target/arm/cmse/mainline/8_1m/soft/cmse-8.c: Likewise. * gcc.target/arm/cmse/mainline/8_1m/softfp-sp/cmse-7.c: Likewise. * gcc.target/arm/cmse/mainline/8_1m/softfp-sp/cmse-8.c: Likewise. * gcc.target/arm/cmse/mainline/8_1m/softfp/cmse-13.c: Likewise. * gcc.target/arm/cmse/mainline/8_1m/softfp/cmse-7.c: Likewise. * gcc.target/arm/cmse/mainline/8_1m/softfp/cmse-8.c: Likewise. * gcc.target/arm/cmse/mainline/8_1m/union-1.c: Likewise. * gcc.target/arm/cmse/mainline/8_1m/union-2.c: Likewise. * gcc.target/arm/cmse/cmse-15.c: Count BLXNS when targeting Armv8.1-M Mainline and restrict libcall count to Armv8-M. --- gcc/config/arm/arm.md | 18 ++++++++++++------ gcc/config/arm/thumb2.md | 26 ++++++++++++++++++-------- 2 files changed, 30 insertions(+), 14 deletions(-) (limited to 'gcc/config/arm') diff --git a/gcc/config/arm/arm.md b/gcc/config/arm/arm.md index f89a2d4..6ec6f71 100644 --- a/gcc/config/arm/arm.md +++ b/gcc/config/arm/arm.md @@ -8387,12 +8387,15 @@ "use_cmse" " { - rtx tmp; - tmp = copy_to_suggested_reg (XEXP (operands[0], 0), + if (!TARGET_HAVE_FPCXT_CMSE) + { + rtx tmp = + copy_to_suggested_reg (XEXP (operands[0], 0), gen_rtx_REG (SImode, R4_REGNUM), SImode); - operands[0] = replace_equiv_address (operands[0], tmp); + operands[0] = replace_equiv_address (operands[0], tmp); + } }") (define_insn "*call_reg_armv5" @@ -8495,12 +8498,15 @@ "use_cmse" " { - rtx tmp; - tmp = copy_to_suggested_reg (XEXP (operands[1], 0), + if (!TARGET_HAVE_FPCXT_CMSE) + { + rtx tmp = + copy_to_suggested_reg (XEXP (operands[1], 0), gen_rtx_REG (SImode, R4_REGNUM), SImode); - operands[1] = replace_equiv_address (operands[1], tmp); + operands[1] = replace_equiv_address (operands[1], tmp); + } }") (define_insn "*call_value_reg_armv5" diff --git a/gcc/config/arm/thumb2.md b/gcc/config/arm/thumb2.md index 6866b4e..3ca4911 100644 --- a/gcc/config/arm/thumb2.md +++ b/gcc/config/arm/thumb2.md @@ -537,13 +537,18 @@ ) (define_insn "*nonsecure_call_reg_thumb2" - [(call (unspec:SI [(mem:SI (reg:SI R4_REGNUM))] + [(call (unspec:SI [(mem:SI (match_operand:SI 0 "s_register_operand" "l*r"))] UNSPEC_NONSECURE_MEM) - (match_operand 0 "" "")) - (use (match_operand 1 "" "")) + (match_operand 1 "" "")) + (use (match_operand 2 "" "")) (clobber (reg:SI LR_REGNUM))] "TARGET_THUMB2 && use_cmse" - "bl\\t__gnu_cmse_nonsecure_call" + { + if (TARGET_HAVE_FPCXT_CMSE) + return "blxns\\t%0"; + else + return "bl\\t__gnu_cmse_nonsecure_call"; + } [(set_attr "length" "4") (set_attr "type" "call")] ) @@ -562,13 +567,18 @@ (define_insn "*nonsecure_call_value_reg_thumb2" [(set (match_operand 0 "" "") (call - (unspec:SI [(mem:SI (reg:SI R4_REGNUM))] + (unspec:SI [(mem:SI (match_operand:SI 1 "register_operand" "l*r"))] UNSPEC_NONSECURE_MEM) - (match_operand 1 "" ""))) - (use (match_operand 2 "" "")) + (match_operand 2 "" ""))) + (use (match_operand 3 "" "")) (clobber (reg:SI LR_REGNUM))] "TARGET_THUMB2 && use_cmse" - "bl\t__gnu_cmse_nonsecure_call" + { + if (TARGET_HAVE_FPCXT_CMSE) + return "blxns\\t%1"; + else + return "bl\\t__gnu_cmse_nonsecure_call"; + } [(set_attr "length" "4") (set_attr "type" "call")] ) -- cgit v1.1