blob: 818f55c0d6cc2c1c14233eacf9c19a4f8bec36b2 [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 Balint386b8b52017-11-29 13:12:32 +000016#include "tfm_core.h"
Miklos Balintace4c3f2018-07-30 12:31:15 +020017#include "tfm_api.h"
Mate Toth-Pal070279f2019-09-10 16:36:27 +020018#include "tfm_utils.h"
Mingyang Sun67a1c0e2020-06-04 17:18:16 +080019#include "tfm_boot_status.h"
Jamie Foxcc31d402019-01-28 17:13:52 +000020#include "psa/service.h"
Miklos Balint386b8b52017-11-29 13:12:32 +000021
David Hud0d21952019-09-23 20:35:43 +080022#ifndef TFM_MULTI_CORE_TOPOLOGY
Antonio de Angeliseba14e12018-03-27 11:03:20 +010023/*!
24 * \def __tfm_secure_gateway_attributes__
25 *
26 * \brief Attributes for secure gateway functions
27 */
David Hu94d424a2020-04-29 15:49:24 +080028#if defined(__GNUC__) && !defined(__ARMCC_VERSION)
29/*
30 * GNUARM requires noclone attribute to protect gateway function symbol from
31 * being renamed and cloned
32 */
Antonio de Angeliseba14e12018-03-27 11:03:20 +010033#define __tfm_secure_gateway_attributes__ \
David Hu94d424a2020-04-29 15:49:24 +080034 __attribute__((cmse_nonsecure_entry, noclone, section("SFN")))
35#else
36#define __tfm_secure_gateway_attributes__ \
37 __attribute__((cmse_nonsecure_entry, section("SFN")))
38#endif /* __GNUC__ && !__ARMCC_VERSION */
Summer Qin483da642019-09-25 17:57:41 +080039
40/*!
41 * \def __tfm_psa_secure_gateway_attributes__
42 *
43 * \brief Attributes for psa api secure gateway functions
44 */
David Hu94d424a2020-04-29 15:49:24 +080045#if defined(__GNUC__) && !defined(__ARMCC_VERSION)
46/*
47 * GNUARM requires noclone attribute to protect gateway function symbol from
48 * being renamed and cloned
49 */
Summer Qin483da642019-09-25 17:57:41 +080050#define __tfm_psa_secure_gateway_attributes__ \
David Hu94d424a2020-04-29 15:49:24 +080051 __attribute__((cmse_nonsecure_entry, noclone, naked, section("SFN")))
52#else
53#define __tfm_psa_secure_gateway_attributes__ \
54 __attribute__((cmse_nonsecure_entry, naked, section("SFN")))
55#endif /* __GNUC__ && !__ARMCC_VERSION */
56#endif /* TFM_MULTI_CORE_TOPOLOGY */
Antonio de Angeliseba14e12018-03-27 11:03:20 +010057
Miklos Balint386b8b52017-11-29 13:12:32 +000058/* Hide specific errors if not debugging */
59#ifdef TFM_CORE_DEBUG
60#define TFM_ERROR_STATUS(status) (status)
61#else
Mate Toth-Pal7de74b52018-02-23 15:46:47 +010062#define TFM_ERROR_STATUS(status) (TFM_PARTITION_BUSY)
Miklos Balint386b8b52017-11-29 13:12:32 +000063#endif
64
65#ifndef TFM_LVL
Miklos Balint386b8b52017-11-29 13:12:32 +000066#error TFM_LVL is not defined!
67#endif
Miklos Balint386b8b52017-11-29 13:12:32 +000068
69extern void tfm_secure_api_error_handler(void);
70
Miklos Balint6a139ae2018-04-04 19:44:37 +020071typedef int32_t(*sfn_t)(int32_t, int32_t, int32_t, int32_t);
72
Miklos Balint386b8b52017-11-29 13:12:32 +000073struct tfm_sfn_req_s {
Miklos Balint6a139ae2018-04-04 19:44:37 +020074 uint32_t sp_id;
75 sfn_t sfn;
76 int32_t *args;
Miklos Balint1fdb12f2018-09-11 11:30:30 +020077 uint32_t caller_part_idx;
Summer Qin43c185d2019-10-10 15:44:42 +080078 bool ns_caller;
Miklos Balint386b8b52017-11-29 13:12:32 +000079};
80
Hugues de Valon1c640762019-04-10 16:36:17 +010081enum tfm_memory_access_e {
82 TFM_MEMORY_ACCESS_RO = 1,
83 TFM_MEMORY_ACCESS_RW = 2,
84};
85
Miklos Balint386b8b52017-11-29 13:12:32 +000086extern int32_t tfm_core_validate_secure_caller(void);
87
Mate Toth-Pal21a74c92018-04-13 14:05:41 +020088extern int32_t tfm_core_get_caller_client_id(int32_t *caller_client_id);
89
Tamas Bana24ce042019-02-20 11:50:22 +000090extern int32_t tfm_core_get_boot_data(uint8_t major_type,
91 struct tfm_boot_data *boot_data,
Tamas Ban9ff535b2018-09-18 08:15:18 +010092 uint32_t len);
93
Hugues de Valon4bf875b2019-02-19 14:53:49 +000094int32_t tfm_core_sfn_request(const struct tfm_sfn_req_s *desc_ptr);
Miklos Balintace4c3f2018-07-30 12:31:15 +020095
Mingyang Sunabb1aab2020-02-18 13:49:08 +080096int32_t tfm_spm_sfn_request_thread_mode(struct tfm_sfn_req_s *desc_ptr);
Miklos Balintace4c3f2018-07-30 12:31:15 +020097
Summer Qin424d4db2019-03-25 14:09:51 +080098/**
David Hu326f3972019-08-06 14:16:51 +080099 * \brief Check whether a memory range is inside a memory region.
Summer Qin424d4db2019-03-25 14:09:51 +0800100 *
David Hu326f3972019-08-06 14:16:51 +0800101 * \param[in] p The start address of the range to check
102 * \param[in] s The size of the range to check
103 * \param[in] region_start The start address of the region, which should
104 * contain the range
105 * \param[in] region_limit The end address of the region, which should contain
106 * the range
Summer Qin424d4db2019-03-25 14:09:51 +0800107 *
David Hu326f3972019-08-06 14:16:51 +0800108 * \return TFM_SUCCESS if the region contains the range,
Summer Qin0fc3f592019-04-11 16:00:10 +0800109 * TFM_ERROR_GENERIC otherwise.
Summer Qin424d4db2019-03-25 14:09:51 +0800110 */
David Hu326f3972019-08-06 14:16:51 +0800111enum tfm_status_e check_address_range(const void *p, size_t s,
112 uintptr_t region_start,
113 uintptr_t region_limit);
Summer Qin424d4db2019-03-25 14:09:51 +0800114
Mate Toth-Pal4341de02018-10-02 12:55:47 +0200115void tfm_enable_irq(psa_signal_t irq_signal);
116void tfm_disable_irq(psa_signal_t irq_signal);
117
Miklos Balintdd02bb32019-05-26 21:13:12 +0200118#ifdef TFM_PSA_API
119/* The following macros are only valid if secure services can be called
120 * using veneer functions. This is not the case if IPC messaging is enabled
121 */
122#define TFM_CORE_IOVEC_SFN_REQUEST(id, fn, a, b, c, d) \
123 do { \
124 ERROR_MSG("Invalid TF-M configuration detected"); \
125 tfm_secure_api_error_handler(); \
126 /* This point never reached */ \
127 return (int32_t)TFM_ERROR_GENERIC; \
128 } while (0)
Miklos Balintdd02bb32019-05-26 21:13:12 +0200129#else
David Hu8d1f1072020-04-30 12:52:14 +0800130#define TFM_CORE_IOVEC_SFN_REQUEST(id, is_ns, fn, a, b, c, d) \
131 return tfm_core_partition_request(id, is_ns, fn, \
Mate Toth-Palb8ce0dd2018-07-25 10:18:34 +0200132 (int32_t)a, (int32_t)b, (int32_t)c, (int32_t)d)
Miklos Balint386b8b52017-11-29 13:12:32 +0000133
David Hu8d1f1072020-04-30 12:52:14 +0800134int32_t tfm_core_partition_request(uint32_t id, bool is_ns, void *fn,
135 int32_t arg1, int32_t arg2, int32_t arg3, int32_t arg4);
Miklos Balint386b8b52017-11-29 13:12:32 +0000136
David Hu8d1f1072020-04-30 12:52:14 +0800137__attribute__ ((always_inline)) __STATIC_INLINE
138bool tfm_core_is_ns_client(void)
139{
Hugues de Valon4bf875b2019-02-19 14:53:49 +0000140 /*
141 * This preprocessor condition checks if a version of GCC smaller than
142 * 7.3.1 is being used to compile the code.
143 * These versions are affected by a bug on the cmse_nonsecure_caller
144 * intrinsic which returns incorrect results.
145 * Please check Bug 85203 on GCC Bugzilla for more information.
146 */
147#if defined(__GNUC__) && !defined(__ARMCC_VERSION) && \
148 (__GNUC__ < 7 || \
149 (__GNUC__ == 7 && (__GNUC_MINOR__ < 3 || \
150 (__GNUC_MINOR__ == 3 && __GNUC_PATCHLEVEL__ < 1))))
151 /*
152 * Use the fact that, if called from Non-Secure, the LSB of the return
153 * address is set to 0.
154 */
David Hu8d1f1072020-04-30 12:52:14 +0800155 return !(
156 (uintptr_t)__builtin_extract_return_addr(__builtin_return_address(0U))
157 & 0x1);
Hugues de Valon4bf875b2019-02-19 14:53:49 +0000158#else
159 /*
Summer Qin43c185d2019-10-10 15:44:42 +0800160 * Convert the result of cmse_nonsecure_caller from an int to a bool
Hugues de Valon4bf875b2019-02-19 14:53:49 +0000161 * to prevent using an int in the tfm_sfn_req_s structure.
162 */
David Hu8d1f1072020-04-30 12:52:14 +0800163 return (cmse_nonsecure_caller() != 0) ? true : false;
Hugues de Valon4bf875b2019-02-19 14:53:49 +0000164#endif /* Check for GCC compiler version smaller than 7.3.1 */
Miklos Balint386b8b52017-11-29 13:12:32 +0000165}
Miklos Balintdd02bb32019-05-26 21:13:12 +0200166#endif
Miklos Balint386b8b52017-11-29 13:12:32 +0000167
168#endif /* __TFM_SECURE_API_H__ */