Skip to content

Commit 47205bf

Browse files
authored
Merge pull request #10502 from kfnta/feature_trusted-firmware-m_f2dea5b
PSA: TFM import
2 parents 2cd7aa1 + 14ad60a commit 47205bf

27 files changed

+443
-374
lines changed

components/TARGET_PSA/TARGET_TFM/COMPONENT_NSPE/interface/src/tfm_ns_lock_rtx.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,23 @@ uint32_t tfm_ns_lock_dispatch(veneer_fn fn,
5454
}
5555

5656
/* TFM request protected by NS lock */
57-
osMutexAcquire(ns_lock.id,osWaitForever);
57+
if (osMutexAcquire(ns_lock.id,osWaitForever) != osOK) {
58+
return TFM_ERROR_GENERIC;
59+
}
5860

5961
result = fn(arg0, arg1, arg2, arg3);
6062

61-
osMutexRelease(ns_lock.id);
63+
if (osMutexRelease(ns_lock.id) != osOK) {
64+
return TFM_ERROR_GENERIC;
65+
}
6266

6367
return result;
6468
}
6569

6670
/**
6771
* \brief NS world, Init NS lock
6872
*/
69-
uint32_t tfm_ns_lock_init()
73+
enum tfm_status_e tfm_ns_lock_init()
7074
{
7175
if (ns_lock.init == false) {
7276
ns_lock.id = osMutexNew(&ns_lock_attrib);

components/TARGET_PSA/TARGET_TFM/COMPONENT_SPE/bl2/include/tfm_boot_status.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,16 @@ struct shared_data_tlv_entry {
197197
uint16_t tlv_len; /* size of single TLV entry (including this header). */
198198
};
199199

200+
/**
201+
* \struct tfm_boot_data
202+
*
203+
* \brief Store the data for the runtime SW
204+
*/
205+
struct tfm_boot_data {
206+
struct shared_data_tlv_header header;
207+
uint8_t data[];
208+
};
209+
200210
#define SHARED_DATA_ENTRY_HEADER_SIZE sizeof(struct shared_data_tlv_entry)
201211
#define SHARED_DATA_ENTRY_SIZE(size) (size + SHARED_DATA_ENTRY_HEADER_SIZE)
202212

components/TARGET_PSA/TARGET_TFM/COMPONENT_SPE/secure_fw/core/ipc/include/tfm_spm.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include <stdbool.h>
1111
#include "tfm_list.h"
12+
#include "tfm_secure_api.h"
1213

1314
#ifndef TFM_SPM_MAX_ROT_SERV_NUM
1415
#define TFM_SPM_MAX_ROT_SERV_NUM 28
@@ -283,12 +284,15 @@ int32_t tfm_spm_check_client_version(struct tfm_spm_service_t *service,
283284
* \param[in] buffer Pointer of memory reference
284285
* \param[in] len Length of memory reference in bytes
285286
* \param[in] ns_caller From non-secure caller
287+
* \param[in] access Type of access specified by the
288+
* \ref tfm_memory_access_e
286289
*
287290
* \retval IPC_SUCCESS Success
288291
* \retval IPC_ERROR_BAD_PARAMETERS Bad parameters input
289292
* \retval IPC_ERROR_MEMORY_CHECK Check failed
290293
*/
291-
int32_t tfm_memory_check(void *buffer, size_t len, int32_t ns_caller);
294+
int32_t tfm_memory_check(void *buffer, size_t len, int32_t ns_caller,
295+
enum tfm_memory_access_e access);
292296

293297
/* This function should be called before schedule function */
294298
void tfm_spm_init(void);

components/TARGET_PSA/TARGET_TFM/COMPONENT_SPE/secure_fw/core/ipc/psa_client.c

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,25 @@
1313
__attribute__((naked))
1414
uint32_t psa_framework_version(void)
1515
{
16-
__ASM("SVC %0 \n"
17-
"BX LR \n"
18-
: : "I" (TFM_SVC_PSA_FRAMEWORK_VERSION));
16+
__ASM volatile("SVC %0 \n"
17+
"BX LR \n"
18+
: : "I" (TFM_SVC_PSA_FRAMEWORK_VERSION));
1919
}
2020

2121
__attribute__((naked))
2222
uint32_t psa_version(uint32_t sid)
2323
{
24-
__ASM("SVC %0 \n"
25-
"BX LR \n"
26-
: : "I" (TFM_SVC_PSA_VERSION));
24+
__ASM volatile("SVC %0 \n"
25+
"BX LR \n"
26+
: : "I" (TFM_SVC_PSA_VERSION));
2727
}
2828

2929
__attribute__((naked))
3030
psa_handle_t psa_connect(uint32_t sid, uint32_t minor_version)
3131
{
32-
__ASM("SVC %0 \n"
33-
"BX LR \n"
34-
: : "I" (TFM_SVC_PSA_CONNECT));
32+
__ASM volatile("SVC %0 \n"
33+
"BX LR \n"
34+
: : "I" (TFM_SVC_PSA_CONNECT));
3535
}
3636

3737
__attribute__((naked))
@@ -41,15 +41,15 @@ psa_status_t psa_call(psa_handle_t handle,
4141
psa_outvec *out_vec,
4242
size_t out_len)
4343
{
44-
__ASM("SVC %0 \n"
45-
"BX LR \n"
46-
: : "I" (TFM_SVC_PSA_CALL));
44+
__ASM volatile("SVC %0 \n"
45+
"BX LR \n"
46+
: : "I" (TFM_SVC_PSA_CALL));
4747
}
4848

4949
__attribute__((naked))
5050
void psa_close(psa_handle_t handle)
5151
{
52-
__ASM("SVC %0 \n"
53-
"BX LR \n"
54-
: : "I" (TFM_SVC_PSA_CLOSE));
52+
__ASM volatile("SVC %0 \n"
53+
"BX LR \n"
54+
: : "I" (TFM_SVC_PSA_CLOSE));
5555
}

components/TARGET_PSA/TARGET_TFM/COMPONENT_SPE/secure_fw/core/ipc/psa_service.c

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,82 +15,82 @@ __attribute__((naked))
1515
psa_signal_t psa_wait(psa_signal_t signal_mask, uint32_t timeout)
1616

1717
{
18-
__ASM("SVC %0 \n"
19-
"BX LR \n"
20-
: : "I" (TFM_SVC_PSA_WAIT));
18+
__ASM volatile("SVC %0 \n"
19+
"BX LR \n"
20+
: : "I" (TFM_SVC_PSA_WAIT));
2121
}
2222

2323
__attribute__((naked))
2424
psa_status_t psa_get(psa_signal_t signal, psa_msg_t *msg)
2525
{
26-
__ASM("SVC %0 \n"
27-
"BX LR \n"
28-
: : "I" (TFM_SVC_PSA_GET));
26+
__ASM volatile("SVC %0 \n"
27+
"BX LR \n"
28+
: : "I" (TFM_SVC_PSA_GET));
2929
}
3030

3131
__attribute__((naked))
3232
void psa_set_rhandle(psa_handle_t msg_handle, void *rhandle)
3333
{
34-
__ASM("SVC %0 \n"
35-
"BX LR \n"
36-
: : "I" (TFM_SVC_PSA_SET_RHANDLE));
34+
__ASM volatile("SVC %0 \n"
35+
"BX LR \n"
36+
: : "I" (TFM_SVC_PSA_SET_RHANDLE));
3737
}
3838

3939
__attribute__((naked))
4040
size_t psa_read(psa_handle_t msg_handle, uint32_t invec_idx,
4141
void *buffer, size_t num_bytes)
4242

4343
{
44-
__ASM("SVC %0 \n"
45-
"BX LR \n"
46-
: : "I" (TFM_SVC_PSA_READ));
44+
__ASM volatile("SVC %0 \n"
45+
"BX LR \n"
46+
: : "I" (TFM_SVC_PSA_READ));
4747
}
4848

4949
__attribute__((naked))
5050
size_t psa_skip(psa_handle_t msg_handle, uint32_t invec_idx, size_t num_bytes)
5151
{
52-
__ASM("SVC %0 \n"
53-
"BX LR \n"
54-
: : "I" (TFM_SVC_PSA_SKIP));
52+
__ASM volatile("SVC %0 \n"
53+
"BX LR \n"
54+
: : "I" (TFM_SVC_PSA_SKIP));
5555
}
5656

5757
__attribute__((naked))
5858
void psa_write(psa_handle_t msg_handle, uint32_t outvec_idx,
5959
const void *buffer, size_t num_bytes)
6060
{
61-
__ASM("SVC %0 \n"
62-
"BX LR \n"
63-
: : "I" (TFM_SVC_PSA_WRITE));
61+
__ASM volatile("SVC %0 \n"
62+
"BX LR \n"
63+
: : "I" (TFM_SVC_PSA_WRITE));
6464
}
6565

6666
__attribute__((naked))
6767
void psa_reply(psa_handle_t msg_handle, psa_status_t retval)
6868
{
69-
__ASM("SVC %0 \n"
70-
"BX LR \n"
71-
: : "I" (TFM_SVC_PSA_REPLY));
69+
__ASM volatile("SVC %0 \n"
70+
"BX LR \n"
71+
: : "I" (TFM_SVC_PSA_REPLY));
7272
}
7373

7474
__attribute__((naked))
7575
void psa_notify(int32_t partition_id)
7676
{
77-
__ASM("SVC %0 \n"
78-
"BX LR \n"
79-
: : "I" (TFM_SVC_PSA_NOTIFY));
77+
__ASM volatile("SVC %0 \n"
78+
"BX LR \n"
79+
: : "I" (TFM_SVC_PSA_NOTIFY));
8080
}
8181

8282
__attribute__((naked))
8383
void psa_clear(void)
8484
{
85-
__ASM("SVC %0 \n"
86-
"BX LR \n"
87-
: : "I" (TFM_SVC_PSA_CLEAR));
85+
__ASM volatile("SVC %0 \n"
86+
"BX LR \n"
87+
: : "I" (TFM_SVC_PSA_CLEAR));
8888
}
8989

9090
__attribute__((naked))
9191
void psa_eoi(psa_signal_t irq_signal)
9292
{
93-
__ASM("SVC %0 \n"
94-
"BX LR \n"
95-
: : "I" (TFM_SVC_PSA_EOI));
93+
__ASM volatile("SVC %0 \n"
94+
"BX LR \n"
95+
: : "I" (TFM_SVC_PSA_EOI));
9696
}

components/TARGET_PSA/TARGET_TFM/COMPONENT_SPE/secure_fw/core/ipc/tfm_arch_v8m.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ void tfm_initialize_context(struct tfm_state_context *ctx,
9494
#if defined(__ARM_ARCH_8M_MAIN__)
9595
__attribute__((naked)) void PendSV_Handler(void)
9696
{
97-
__ASM(
97+
__ASM volatile(
9898
"mrs r0, psp \n"
9999
"mrs r1, psplim \n"
100100
"push {r0, r1, r2, lr} \n"
@@ -111,7 +111,7 @@ __attribute__((naked)) void PendSV_Handler(void)
111111
#elif defined(__ARM_ARCH_8M_BASE__)
112112
__attribute__((naked)) void PendSV_Handler(void)
113113
{
114-
__ASM(
114+
__ASM volatile(
115115
"mrs r0, psp \n"
116116
"mrs r1, psplim \n"
117117
"push {r0, r1, r2, lr} \n"
@@ -143,14 +143,14 @@ __attribute__((naked)) void PendSV_Handler(void)
143143
/* Reserved for future usage */
144144
__attribute__((naked)) void MemManage_Handler(void)
145145
{
146-
__ASM("b .");
146+
__ASM volatile("b .");
147147
}
148148

149149
__attribute__((naked)) void BusFault_Handler(void)
150150
{
151-
__ASM("b .");
151+
__ASM volatile("b .");
152152
}
153153
__attribute__((naked)) void UsageFault_Handler(void)
154154
{
155-
__ASM("b .");
155+
__ASM volatile("b .");
156156
}

components/TARGET_PSA/TARGET_TFM/COMPONENT_SPE/secure_fw/core/ipc/tfm_spm.c

Lines changed: 10 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "psa_client.h"
1313
#include "psa_service.h"
1414
#include "tfm_utils.h"
15+
#include "platform/include/tfm_spm_hal.h"
1516
#include "spm_api.h"
1617
#include "spm_db.h"
1718
#include "spm_db_setup.h"
@@ -460,46 +461,11 @@ static uint32_t tfm_spm_partition_get_priority_ext(uint32_t partition_idx)
460461
partition_priority;
461462
}
462463

463-
/* Macros to pick linker symbols and allow references to sections in all level*/
464-
#define REGION_DECLARE_EXT(a, b, c) extern uint32_t REGION_NAME(a, b, c)
465-
466-
REGION_DECLARE_EXT(Image$$, ARM_LIB_HEAP, $$ZI$$Base);
467-
REGION_DECLARE_EXT(Image$$, ARM_LIB_HEAP, $$ZI$$Limit);
468-
REGION_DECLARE_EXT(Image$$, ER_TFM_DATA, $$ZI$$Base);
469-
REGION_DECLARE_EXT(Image$$, ER_TFM_DATA, $$ZI$$Limit);
470-
REGION_DECLARE_EXT(Image$$, ER_TFM_DATA, $$RW$$Base);
471-
REGION_DECLARE_EXT(Image$$, ER_TFM_DATA, $$RW$$Limit);
472-
REGION_DECLARE_EXT(Image$$, TFM_SECURE_STACK, $$ZI$$Base);
473-
REGION_DECLARE_EXT(Image$$, TFM_SECURE_STACK, $$ZI$$Limit);
474-
REGION_DECLARE_EXT(Image$$, TFM_UNPRIV_SCRATCH, $$ZI$$Base);
475-
REGION_DECLARE_EXT(Image$$, TFM_UNPRIV_SCRATCH, $$ZI$$Limit);
476-
477-
/*
478-
* \brief Check the memory whether in the given range.
479-
*
480-
* \param[in] buffer Pointer of memory reference
481-
* \param[in] len Length of memory reference in bytes
482-
* \param[in] base The base address
483-
* \param[in] limit The limit address, the first byte of next
484-
* area memory
485-
*
486-
* \retval IPC_SUCCESS Success
487-
* \retval IPC_ERROR_MEMORY_CHECK Check failed
488-
*/
489-
static int32_t memory_check_range(const void *buffer, size_t len,
490-
uintptr_t base, uintptr_t limit)
491-
{
492-
if (((uintptr_t)buffer >= base) &&
493-
((uintptr_t)((uint8_t *)buffer + len - 1) < limit)) {
494-
return IPC_SUCCESS;
495-
}
496-
return IPC_ERROR_MEMORY_CHECK;
497-
}
498-
499464
/* FixMe: This is only valid for TFM LVL 1 now */
500-
int32_t tfm_memory_check(void *buffer, size_t len, int32_t ns_caller)
465+
int32_t tfm_memory_check(void *buffer, size_t len, int32_t ns_caller,
466+
enum tfm_memory_access_e access)
501467
{
502-
uintptr_t base, limit;
468+
int32_t err;
503469

504470
/* If len is zero, this indicates an empty buffer and base is ignored */
505471
if (len == 0) {
@@ -514,55 +480,13 @@ int32_t tfm_memory_check(void *buffer, size_t len, int32_t ns_caller)
514480
return IPC_ERROR_MEMORY_CHECK;
515481
}
516482

517-
if (ns_caller) {
518-
base = (uintptr_t)NS_DATA_START;
519-
limit = (uintptr_t)(NS_DATA_START + NS_DATA_SIZE);
520-
if (memory_check_range(buffer, len, base, limit) == IPC_SUCCESS) {
521-
return IPC_SUCCESS;
522-
}
523-
524-
base = (uintptr_t)NS_CODE_START;
525-
limit = (uintptr_t)(NS_CODE_START + NS_CODE_SIZE);
526-
if (memory_check_range(buffer, len, base, limit) == IPC_SUCCESS) {
527-
return IPC_SUCCESS;
528-
}
483+
if (access == TFM_MEMORY_ACCESS_RW) {
484+
err = tfm_core_has_write_access_to_region(buffer, len, ns_caller);
529485
} else {
530-
base = (uintptr_t)&REGION_NAME(Image$$, ARM_LIB_HEAP, $$ZI$$Base);
531-
limit = (uintptr_t)&REGION_NAME(Image$$, ARM_LIB_HEAP, $$ZI$$Limit);
532-
if (memory_check_range(buffer, len, base, limit) == IPC_SUCCESS) {
533-
return IPC_SUCCESS;
534-
}
535-
536-
base = (uintptr_t)&REGION_NAME(Image$$, ER_TFM_DATA, $$RW$$Base);
537-
limit = (uintptr_t)&REGION_NAME(Image$$, ER_TFM_DATA, $$RW$$Limit);
538-
if (memory_check_range(buffer, len, base, limit) == IPC_SUCCESS) {
539-
return IPC_SUCCESS;
540-
}
541-
542-
base = (uintptr_t)&REGION_NAME(Image$$, ER_TFM_DATA, $$ZI$$Base);
543-
limit = (uintptr_t)&REGION_NAME(Image$$, ER_TFM_DATA, $$ZI$$Limit);
544-
if (memory_check_range(buffer, len, base, limit) == IPC_SUCCESS) {
545-
return IPC_SUCCESS;
546-
}
547-
548-
base = (uintptr_t)&REGION_NAME(Image$$, TFM_SECURE_STACK, $$ZI$$Base);
549-
limit = (uintptr_t)&REGION_NAME(Image$$, TFM_SECURE_STACK, $$ZI$$Limit);
550-
if (memory_check_range(buffer, len, base, limit) == IPC_SUCCESS) {
551-
return IPC_SUCCESS;
552-
}
553-
554-
base = (uintptr_t)&REGION_NAME(Image$$, TFM_UNPRIV_SCRATCH, $$ZI$$Base);
555-
limit = (uintptr_t)&REGION_NAME(Image$$, TFM_UNPRIV_SCRATCH,
556-
$$ZI$$Limit);
557-
if (memory_check_range(buffer, len, base, limit) == IPC_SUCCESS) {
558-
return IPC_SUCCESS;
559-
}
560-
561-
base = (uintptr_t)S_CODE_START;
562-
limit = (uintptr_t)(S_CODE_START + S_CODE_SIZE);
563-
if (memory_check_range(buffer, len, base, limit) == IPC_SUCCESS) {
564-
return IPC_SUCCESS;
565-
}
486+
err = tfm_core_has_read_access_to_region(buffer, len, ns_caller);
487+
}
488+
if (err == 1) {
489+
return IPC_SUCCESS;
566490
}
567491

568492
return IPC_ERROR_MEMORY_CHECK;

0 commit comments

Comments
 (0)