1#-------------------------------------------------------------------------------
2# Copyright (c) 2022-2024, Arm Limited. All rights reserved.
3#
4# SPDX-License-Identifier: BSD-3-Clause
5#
6#-------------------------------------------------------------------------------
7
8cmake_minimum_required(VERSION 3.21)
9
10if(NOT COMMAND tfm_invalid_config)
11    function(tfm_invalid_config)
12        if (${ARGV})
13            string (REPLACE ";" " " ARGV_STRING "${ARGV}")
14            string (REPLACE "STREQUAL"     "=" ARGV_STRING "${ARGV_STRING}")
15            string (REPLACE "GREATER"      ">" ARGV_STRING "${ARGV_STRING}")
16            string (REPLACE "LESS"         "<" ARGV_STRING "${ARGV_STRING}")
17            string (REPLACE "VERSION_LESS" "<" ARGV_STRING "${ARGV_STRING}")
18            string (REPLACE "EQUAL"        "=" ARGV_STRING "${ARGV_STRING}")
19            string (REPLACE "IN_LIST"      "in" ARGV_STRING "${ARGV_STRING}")
20
21            message(FATAL_ERROR "INVALID CONFIG: ${ARGV_STRING}")
22        endif()
23    endfunction()
24endif()
25
26########################## FPU and MVE #########################################
27
28tfm_invalid_config(NOT CMAKE_C_COMPILER_ID STREQUAL "GNU" AND (CONFIG_TFM_ENABLE_MVE OR CONFIG_TFM_ENABLE_MVE_FP))
29tfm_invalid_config((NOT CONFIG_TFM_FP_ARCH) AND (CONFIG_TFM_ENABLE_FP OR CONFIG_TFM_ENABLE_MVE_FP))
30tfm_invalid_config((CMAKE_C_COMPILER_ID STREQUAL "ARMClang") AND (NOT CONFIG_TFM_FP_ARCH_ASM) AND CONFIG_TFM_ENABLE_FP)
31tfm_invalid_config((NOT CONFIG_TFM_ENABLE_FP AND NOT CONFIG_TFM_ENABLE_MVE AND NOT CONFIG_TFM_ENABLE_MVE_FP) AND CONFIG_TFM_LAZY_STACKING)
32tfm_invalid_config((CONFIG_TFM_ENABLE_FP OR CONFIG_TFM_ENABLE_MVE OR CONFIG_TFM_ENABLE_MVE_FP) AND NOT CONFIG_TFM_ENABLE_CP10CP11)
33
34###################### Check compiler for FP vulnerability #####################
35
36# Check compiler with mitigation for the VLLDM instruction security vulnerability or not.
37# For more information, please check https://developer.arm.com/support/arm-security-updates/vlldm-instruction-security-vulnerability.
38if (CONFIG_TFM_FLOAT_ABI STREQUAL "hard")
39    # Create test C file.
40    file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/cvetest.c "int x;")
41    # Compile with mitigation -mfix-cmse-cve-2021-35465.
42    if (CMAKE_C_COMPILER_ID STREQUAL "ARMClang")
43        # This flag is dedicated for this check.
44        # Check command will fail if C flags consist of keyword other than cpu/arch type.
45        # Toolchain file shall define a dedicated CP_CHECK_C_FLAGS to collect cpu/arch type
46        # if CMAKE_C_FLAGS is appended with other flags already before this check.
47        if(NOT DEFINED CP_CHECK_C_FLAGS)
48            set(CP_CHECK_C_FLAGS ${CMAKE_C_FLAGS})
49        endif()
50
51        execute_process (
52            COMMAND ${CMAKE_C_COMPILER} --target=${CROSS_COMPILE} ${CP_CHECK_C_FLAGS} -mcmse -mfix-cmse-cve-2021-35465 -S ${CMAKE_CURRENT_BINARY_DIR}/cvetest.c -o ${CMAKE_CURRENT_BINARY_DIR}/cvetest.s
53            RESULT_VARIABLE ret
54            ERROR_VARIABLE err
55        )
56    elseif (CMAKE_C_COMPILER_ID STREQUAL "GNU")
57        execute_process (
58            COMMAND ${CMAKE_C_COMPILER} -mfix-cmse-cve-2021-35465 -S ${CMAKE_CURRENT_BINARY_DIR}/cvetest.c -o ${CMAKE_CURRENT_BINARY_DIR}/cvetest.s
59            RESULT_VARIABLE ret
60            ERROR_VARIABLE err
61        )
62    elseif (CMAKE_C_COMPILER_ID STREQUAL "IAR")
63        execute_process (
64            COMMAND ${CMAKE_C_COMPILER} --enable_hardware_workaround fix-cmse-cve-2021-35465 ${CMAKE_CURRENT_BINARY_DIR}/cvetest.c -o ${CMAKE_CURRENT_BINARY_DIR}/cvetest.s
65            RESULT_VARIABLE ret
66            ERROR_VARIABLE err
67        )
68    else()
69        message(FATAL_ERROR "Unsupported compiler.")
70    endif()
71    file(REMOVE ${CMAKE_CURRENT_BINARY_DIR}/cvetest.c)
72    # Check result
73    if(NOT ret EQUAL 0)
74        message(FATAL_ERROR "To enable FPU usage in SPE and NSPE both, please use the compiler with '-mfix-cmse-cve-2021-35465' support")
75    else()
76        file(REMOVE ${CMAKE_CURRENT_BINARY_DIR}/cvetest.s)
77    endif()
78endif()
79