blob: 6b177d442298447666b516be3a4d0910ed21eb51 [file] [log] [blame]
Miklos Balint386b8b52017-11-29 13:12:32 +00001/*
Jamie Foxd4c3c742020-02-17 16:08:40 +00002 * Copyright (c) 2017-2020, Arm Limited. All rights reserved.
Miklos Balint386b8b52017-11-29 13:12:32 +00003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
8#ifndef __TFM_SECURE_API_H__
9#define __TFM_SECURE_API_H__
10
David Hud0d21952019-09-23 20:35:43 +080011#ifndef TFM_MULTI_CORE_TOPOLOGY
Hugues de Valon4bf875b2019-02-19 14:53:49 +000012#include <arm_cmse.h>
David Hud0d21952019-09-23 20:35:43 +080013#endif
David Hu50711e32019-06-12 18:32:30 +080014#include "tfm_arch.h"
Ken Liu1f345b02020-05-30 21:11:05 +080015#include "tfm/tfm_core_svc.h"
Miklos Balintace4c3f2018-07-30 12:31:15 +020016#include "tfm_api.h"
Mate Toth-Pal070279f2019-09-10 16:36:27 +020017#include "tfm_utils.h"
Mingyang Sun67a1c0e2020-06-04 17:18:16 +080018#include "tfm_boot_status.h"
Jamie Foxcc31d402019-01-28 17:13:52 +000019#include "psa/service.h"
Miklos Balint386b8b52017-11-29 13:12:32 +000020
David Hud0d21952019-09-23 20:35:43 +080021#ifndef TFM_MULTI_CORE_TOPOLOGY
Antonio de Angeliseba14e12018-03-27 11:03:20 +010022/*!
23 * \def __tfm_secure_gateway_attributes__
24 *
25 * \brief Attributes for secure gateway functions
26 */
David Hu94d424a2020-04-29 15:49:24 +080027#if defined(__GNUC__) && !defined(__ARMCC_VERSION)
28/*
29 * GNUARM requires noclone attribute to protect gateway function symbol from
30 * being renamed and cloned
31 */
Antonio de Angeliseba14e12018-03-27 11:03:20 +010032#define __tfm_secure_gateway_attributes__ \
David Hu94d424a2020-04-29 15:49:24 +080033 __attribute__((cmse_nonsecure_entry, noclone, section("SFN")))
34#else
35#define __tfm_secure_gateway_attributes__ \
36 __attribute__((cmse_nonsecure_entry, section("SFN")))
37#endif /* __GNUC__ && !__ARMCC_VERSION */
Summer Qin483da642019-09-25 17:57:41 +080038
39/*!
40 * \def __tfm_psa_secure_gateway_attributes__
41 *
42 * \brief Attributes for psa api secure gateway functions
43 */
David Hu94d424a2020-04-29 15:49:24 +080044#if defined(__GNUC__) && !defined(__ARMCC_VERSION)
45/*
46 * GNUARM requires noclone attribute to protect gateway function symbol from
47 * being renamed and cloned
48 */
Summer Qin483da642019-09-25 17:57:41 +080049#define __tfm_psa_secure_gateway_attributes__ \
David Hu94d424a2020-04-29 15:49:24 +080050 __attribute__((cmse_nonsecure_entry, noclone, naked, section("SFN")))
51#else
52#define __tfm_psa_secure_gateway_attributes__ \
53 __attribute__((cmse_nonsecure_entry, naked, section("SFN")))
54#endif /* __GNUC__ && !__ARMCC_VERSION */
55#endif /* TFM_MULTI_CORE_TOPOLOGY */
Antonio de Angeliseba14e12018-03-27 11:03:20 +010056
Miklos Balint386b8b52017-11-29 13:12:32 +000057/* Hide specific errors if not debugging */
58#ifdef TFM_CORE_DEBUG
59#define TFM_ERROR_STATUS(status) (status)
60#else
Mate Toth-Pal7de74b52018-02-23 15:46:47 +010061#define TFM_ERROR_STATUS(status) (TFM_PARTITION_BUSY)
Miklos Balint386b8b52017-11-29 13:12:32 +000062#endif
63
64#ifndef TFM_LVL
Miklos Balint386b8b52017-11-29 13:12:32 +000065#error TFM_LVL is not defined!
66#endif
Miklos Balint386b8b52017-11-29 13:12:32 +000067
68extern void tfm_secure_api_error_handler(void);
69
Miklos Balint6a139ae2018-04-04 19:44:37 +020070typedef int32_t(*sfn_t)(int32_t, int32_t, int32_t, int32_t);
71
Miklos Balint386b8b52017-11-29 13:12:32 +000072struct tfm_sfn_req_s {
Miklos Balint6a139ae2018-04-04 19:44:37 +020073 uint32_t sp_id;
74 sfn_t sfn;
75 int32_t *args;
Miklos Balint1fdb12f2018-09-11 11:30:30 +020076 uint32_t caller_part_idx;
Summer Qin43c185d2019-10-10 15:44:42 +080077 bool ns_caller;
Miklos Balint386b8b52017-11-29 13:12:32 +000078};
79
Hugues de Valon1c640762019-04-10 16:36:17 +010080enum tfm_memory_access_e {
81 TFM_MEMORY_ACCESS_RO = 1,
82 TFM_MEMORY_ACCESS_RW = 2,
83};
84
Mate Toth-Pal21a74c92018-04-13 14:05:41 +020085extern int32_t tfm_core_get_caller_client_id(int32_t *caller_client_id);
86
Tamas Bana24ce042019-02-20 11:50:22 +000087extern int32_t tfm_core_get_boot_data(uint8_t major_type,
88 struct tfm_boot_data *boot_data,
Tamas Ban9ff535b2018-09-18 08:15:18 +010089 uint32_t len);
90
Hugues de Valon4bf875b2019-02-19 14:53:49 +000091int32_t tfm_core_sfn_request(const struct tfm_sfn_req_s *desc_ptr);
Miklos Balintace4c3f2018-07-30 12:31:15 +020092
Mingyang Sunabb1aab2020-02-18 13:49:08 +080093int32_t tfm_spm_sfn_request_thread_mode(struct tfm_sfn_req_s *desc_ptr);
Miklos Balintace4c3f2018-07-30 12:31:15 +020094
Summer Qin424d4db2019-03-25 14:09:51 +080095/**
David Hu326f3972019-08-06 14:16:51 +080096 * \brief Check whether a memory range is inside a memory region.
Summer Qin424d4db2019-03-25 14:09:51 +080097 *
David Hu326f3972019-08-06 14:16:51 +080098 * \param[in] p The start address of the range to check
99 * \param[in] s The size of the range to check
100 * \param[in] region_start The start address of the region, which should
101 * contain the range
102 * \param[in] region_limit The end address of the region, which should contain
103 * the range
Summer Qin424d4db2019-03-25 14:09:51 +0800104 *
David Hu326f3972019-08-06 14:16:51 +0800105 * \return TFM_SUCCESS if the region contains the range,
Summer Qin0fc3f592019-04-11 16:00:10 +0800106 * TFM_ERROR_GENERIC otherwise.
Summer Qin424d4db2019-03-25 14:09:51 +0800107 */
David Hu326f3972019-08-06 14:16:51 +0800108enum tfm_status_e check_address_range(const void *p, size_t s,
109 uintptr_t region_start,
110 uintptr_t region_limit);
Summer Qin424d4db2019-03-25 14:09:51 +0800111
Mate Toth-Pal4341de02018-10-02 12:55:47 +0200112void tfm_enable_irq(psa_signal_t irq_signal);
113void tfm_disable_irq(psa_signal_t irq_signal);
114
Miklos Balintdd02bb32019-05-26 21:13:12 +0200115#ifdef TFM_PSA_API
116/* The following macros are only valid if secure services can be called
117 * using veneer functions. This is not the case if IPC messaging is enabled
118 */
119#define TFM_CORE_IOVEC_SFN_REQUEST(id, fn, a, b, c, d) \
120 do { \
121 ERROR_MSG("Invalid TF-M configuration detected"); \
122 tfm_secure_api_error_handler(); \
123 /* This point never reached */ \
124 return (int32_t)TFM_ERROR_GENERIC; \
125 } while (0)
Miklos Balintdd02bb32019-05-26 21:13:12 +0200126#else
David Hu8d1f1072020-04-30 12:52:14 +0800127#define TFM_CORE_IOVEC_SFN_REQUEST(id, is_ns, fn, a, b, c, d) \
128 return tfm_core_partition_request(id, is_ns, fn, \
Mate Toth-Palb8ce0dd2018-07-25 10:18:34 +0200129 (int32_t)a, (int32_t)b, (int32_t)c, (int32_t)d)
Miklos Balint386b8b52017-11-29 13:12:32 +0000130
David Hu8d1f1072020-04-30 12:52:14 +0800131int32_t tfm_core_partition_request(uint32_t id, bool is_ns, void *fn,
132 int32_t arg1, int32_t arg2, int32_t arg3, int32_t arg4);
Miklos Balint386b8b52017-11-29 13:12:32 +0000133
David Hu8d1f1072020-04-30 12:52:14 +0800134__attribute__ ((always_inline)) __STATIC_INLINE
135bool tfm_core_is_ns_client(void)
136{
Hugues de Valon4bf875b2019-02-19 14:53:49 +0000137 /*
138 * This preprocessor condition checks if a version of GCC smaller than
139 * 7.3.1 is being used to compile the code.
140 * These versions are affected by a bug on the cmse_nonsecure_caller
141 * intrinsic which returns incorrect results.
142 * Please check Bug 85203 on GCC Bugzilla for more information.
143 */
144#if defined(__GNUC__) && !defined(__ARMCC_VERSION) && \
145 (__GNUC__ < 7 || \
146 (__GNUC__ == 7 && (__GNUC_MINOR__ < 3 || \
147 (__GNUC_MINOR__ == 3 && __GNUC_PATCHLEVEL__ < 1))))
148 /*
149 * Use the fact that, if called from Non-Secure, the LSB of the return
150 * address is set to 0.
151 */
David Hu8d1f1072020-04-30 12:52:14 +0800152 return !(
153 (uintptr_t)__builtin_extract_return_addr(__builtin_return_address(0U))
154 & 0x1);
Hugues de Valon4bf875b2019-02-19 14:53:49 +0000155#else
156 /*
Summer Qin43c185d2019-10-10 15:44:42 +0800157 * Convert the result of cmse_nonsecure_caller from an int to a bool
Hugues de Valon4bf875b2019-02-19 14:53:49 +0000158 * to prevent using an int in the tfm_sfn_req_s structure.
159 */
David Hu8d1f1072020-04-30 12:52:14 +0800160 return (cmse_nonsecure_caller() != 0) ? true : false;
Hugues de Valon4bf875b2019-02-19 14:53:49 +0000161#endif /* Check for GCC compiler version smaller than 7.3.1 */
Miklos Balint386b8b52017-11-29 13:12:32 +0000162}
Miklos Balintdd02bb32019-05-26 21:13:12 +0200163#endif
Miklos Balint386b8b52017-11-29 13:12:32 +0000164
165#endif /* __TFM_SECURE_API_H__ */