Skip to content

Commit

Permalink
Merge pull request #14396 from LDong-Arm/tfm_os_wrapper
Browse files Browse the repository at this point in the history
TF-M: Switch to vanilla TF-M's OS wrapper
  • Loading branch information
0xc0170 committed Mar 22, 2021
2 parents 7ef27d9 + a6081e5 commit 96e19af
Show file tree
Hide file tree
Showing 13 changed files with 518 additions and 27 deletions.
4 changes: 2 additions & 2 deletions drivers/usb/include/usb/USBCDC_ECM.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
#include "USBDescriptor.h"
#include "USBDevice.h"
#include "ByteBuffer.h"
#include "Mutex.h"
#include "rtos/Mutex.h"
#include "EventFlags.h"
#include "events/EventQueue.h"
#include "Thread.h"
#include "rtos/Thread.h"
#include "Callback.h"

#define MAX_PACKET_SIZE_INT (64)
Expand Down
2 changes: 1 addition & 1 deletion drivers/usb/include/usb/USBMIDI.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#include "USBDevice.h"
#include "MIDIMessage.h"
#include "EventFlags.h"
#include "Mutex.h"
#include "rtos/Mutex.h"
#include "Callback.h"

#define DEFAULT_CONFIGURATION (1)
Expand Down
2 changes: 1 addition & 1 deletion drivers/usb/include/usb/USBMSD.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#include "usb/internal/PolledQueue.h"
#include "usb/internal/Task.h"
#include "BlockDevice.h"
#include "Mutex.h"
#include "rtos/Mutex.h"

#include "USBDevice.h"

Expand Down
4 changes: 2 additions & 2 deletions drivers/usb/include/usb/internal/AsyncOp.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
#ifndef MBED_ASYNC_OP_H
#define MBED_ASYNC_OP_H

#include "Mutex.h"
#include "Semaphore.h"
#include "rtos/Mutex.h"
#include "rtos/Semaphore.h"
#include "Callback.h"

#include "LinkEntry.h"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ target_include_directories(mbed-psa

target_sources(mbed-psa
INTERFACE
src/os_wrapper_cmsis_rtos_v2.c
src/tfm_crypto_ipc_api.c
src/tfm_initial_attestation_ipc_api.c
src/tfm_its_ipc_api.c
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,32 @@
#include <stdint.h>
#include <stdbool.h>

#include "os_wrapper/mutex.h"

#include "tfm_api.h"
#include "tfm_ns_interface.h"
#include "cmsis_os2.h"

/**
* \brief the ns_lock ID
*/
static osMutexId_t ns_lock_handle = NULL;
static void *ns_lock_handle = NULL;

__attribute__((weak))
int32_t tfm_ns_interface_dispatch(veneer_fn fn,
uint32_t arg0, uint32_t arg1,
uint32_t arg2, uint32_t arg3)
{
int32_t result;
osStatus_t status;

/* TFM request protected by NS lock */
status = osMutexAcquire(ns_lock_handle, osWaitForever);
if (status != osOK) {
if (os_wrapper_mutex_acquire(ns_lock_handle, OS_WRAPPER_WAIT_FOREVER)
!= OS_WRAPPER_SUCCESS) {
return (int32_t)TFM_ERROR_GENERIC;
}

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

status = osMutexRelease(ns_lock_handle);
if (status != osOK) {
if (os_wrapper_mutex_release(ns_lock_handle) != OS_WRAPPER_SUCCESS) {
return (int32_t)TFM_ERROR_GENERIC;
}

Expand All @@ -43,22 +42,13 @@ int32_t tfm_ns_interface_dispatch(veneer_fn fn,
__attribute__((weak))
enum tfm_status_e tfm_ns_interface_init(void)
{
const osMutexAttr_t attr = {
.name = NULL,
.attr_bits = osMutexPrioInherit, /* Priority inheritance is recommended
* to enable if it is supported.
* For recursive mutex and the ability
* of auto release when owner being
* terminated is not required.
*/
.cb_mem = NULL,
.cb_size = 0U
};
void *handle;

ns_lock_handle = osMutexNew(&attr);
if (!ns_lock_handle) {
handle = os_wrapper_mutex_create();
if (!handle) {
return TFM_ERROR_GENERIC;
}

ns_lock_handle = handle;
return TFM_SUCCESS;
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
706a1e6db255
8635d8a23341
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (c) 2017-2019, Arm Limited. All rights reserved.
*
* SPDX-License-Identifier: BSD-3-Clause
*
*/

#ifndef __OS_WRAPPER_COMMON_H__
#define __OS_WRAPPER_COMMON_H__

#ifdef __cplusplus
extern "C" {
#endif

#include <stdint.h>

#define OS_WRAPPER_SUCCESS (0x0)
#define OS_WRAPPER_ERROR (0xFFFFFFFFU)
#define OS_WRAPPER_WAIT_FOREVER (0xFFFFFFFFU)
#define OS_WRAPPER_DEFAULT_STACK_SIZE (-1)

#ifdef __cplusplus
}
#endif

#endif /* __OS_WRAPPER_COMMON_H__ */
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright (c) 2017-2019, Arm Limited. All rights reserved.
*
* SPDX-License-Identifier: BSD-3-Clause
*
*/

#ifndef __OS_WRAPPER_MUTEX_H__
#define __OS_WRAPPER_MUTEX_H__

#ifdef __cplusplus
extern "C" {
#endif

#include "common.h"

/**
* \brief Creates a mutex for mutual exclusion of resources
*
* \return The handle of the created mutex on success or NULL on error
*/
void *os_wrapper_mutex_create(void);

/**
* \brief Acquires a mutex that is created by \ref os_wrapper_mutex_create()
*
* \param[in] handle The handle of the mutex to acquire. Should be one of the
* handles returned by \ref os_wrapper_mutex_create()
* \param[in] timeout The maximum amount of time(in tick periods) for the
* thread to wait for the mutex to be available.
* If timeout is zero, the function will return immediately.
* Setting timeout to \ref OS_WRAPPER_WAIT_FOREVER will
* cause the thread to wait indefinitely
*
* \return \ref OS_WRAPPER_SUCCESS on success or \ref OS_WRAPPER_ERROR on error
*/
uint32_t os_wrapper_mutex_acquire(void *handle, uint32_t timeout);

/**
* \brief Releases the mutex acquired previously
*
* \param[in] handle The handle of the mutex that has been acquired
*
* \return \ref OS_WRAPPER_SUCCESS on success or \ref OS_WRAPPER_ERROR on error
*/
uint32_t os_wrapper_mutex_release(void *handle);

/**
* \brief Deletes a mutex that is created by \ref os_wrapper_mutex_create()
*
* \param[in] handle The handle of the mutex to be deleted
*
* \return \ref OS_WRAPPER_SUCCESS on success or \ref OS_WRAPPER_ERROR on error
*/
uint32_t os_wrapper_mutex_delete(void *handle);

#ifdef __cplusplus
}
#endif

#endif /* __OS_WRAPPER_MUTEX_H__ */
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright (c) 2017-2020, Arm Limited. All rights reserved.
*
* SPDX-License-Identifier: BSD-3-Clause
*
*/

#ifndef __OS_WRAPPER_SEMAPHORE_H__
#define __OS_WRAPPER_SEMAPHORE_H__

#ifdef __cplusplus
extern "C" {
#endif

#include "common.h"

/**
* \brief Creates a new semaphore
*
* \param[in] max_count Highest count of the semaphore
* \param[in] initial_count Starting count of the available semaphore
* \param[in] name Name of the semaphore
*
* \return Returns handle of the semaphore created, or NULL in case of error
*/
void *os_wrapper_semaphore_create(uint32_t max_count, uint32_t initial_count,
const char *name);

/**
* \brief Acquires the semaphore
*
* \param[in] hanlde Semaphore handle
* \param[in] timeout Timeout value
*
* \return \ref OS_WRAPPER_SUCCESS in case of successful acquision, or
* \ref OS_WRAPPER_ERROR in case of error
*/
uint32_t os_wrapper_semaphore_acquire(void *handle, uint32_t timeout);

/**
* \brief Releases the semaphore
*
* \param[in] hanlde Semaphore handle
*
* \return \ref OS_WRAPPER_SUCCESS in case of successful release, or
* \ref OS_WRAPPER_ERROR in case of error
*/
uint32_t os_wrapper_semaphore_release(void *handle);

/**
* \brief Deletes the semaphore
*
* \param[in] handle Semaphore handle
*
* \return \ref OS_WRAPPER_SUCCESS in case of successful release, or
* \ref OS_WRAPPER_ERROR in case of error
*/
uint32_t os_wrapper_semaphore_delete(void *handle);

#ifdef __cplusplus
}
#endif

#endif /* __OS_WRAPPER_SEMAPHORE_H__ */
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* Copyright (c) 2017-2020, Arm Limited. All rights reserved.
*
* SPDX-License-Identifier: BSD-3-Clause
*
*/

#ifndef __OS_WRAPPER_THREAD_H__
#define __OS_WRAPPER_THREAD_H__

#ifdef __cplusplus
extern "C" {
#endif

#include "common.h"

/* prototype for the thread entry function */
typedef void (*os_wrapper_thread_func) (void *argument);

/**
* \brief Creates a new thread
*
* \param[in] name Name of the thread
* \param[in] stack_size Size of stack to be allocated for this thread. It can
* be \ref OS_WRAPPER_DEFAULT_STACK_SIZE to use the
* default value provided by the underlying RTOS
* \param[in] func Pointer to the function invoked by thread
* \param[in] arg Argument to pass to the function invoked by thread
* \param[in] priority Initial thread priority
*
* \return Returns the thread handle created, or NULL in case of error
*/
void *os_wrapper_thread_new(const char *name, int32_t stack_size,
os_wrapper_thread_func func, void *arg,
uint32_t priority);
/**
* \brief Gets current thread handle
*
* \return Returns the thread handle, or NULL in case of error
*/
void *os_wrapper_thread_get_handle(void);

/**
* \brief Gets thread priority
*
* \param[in] handle Thread handle
* \param[out] priority The priority of the thread
*
* \return Returns \ref OS_WRAPPER_SUCCESS on success, or \ref OS_WRAPPER_ERROR
* in case of error
*/
uint32_t os_wrapper_thread_get_priority(void *handle, uint32_t *priority);

/**
* \brief Exits the calling thread
*/
void os_wrapper_thread_exit(void);

/**
* \brief Set the event flags for synchronizing a thread specified by handle.
*
* \note This function may not be allowed to be called from Interrupt Service
* Routines.
*
* \param[in] handle Thread handle to be notified
* \param[in] flags Event flags value
*
* \return Returns \ref OS_WRAPPER_SUCCESS on success, or \ref OS_WRAPPER_ERROR
* in case of error
*/
uint32_t os_wrapper_thread_set_flag(void *handle, uint32_t flags);

/**
* \brief Set the event flags in an interrupt handler for synchronizing a thread
* specified by handle.
*
* \param[in] handle Thread handle to be notified
* \param[in] flags Event flags value
*
* \return Returns \ref OS_WRAPPER_SUCCESS on success, or \ref OS_WRAPPER_ERROR
* in case of error
*/
uint32_t os_wrapper_thread_set_flag_isr(void *handle, uint32_t flags);

/**
* \brief Wait for the event flags for synchronizing threads.
*
* \note This function may not be allowed to be called from Interrupt Service
* Routines.
*
* \param[in] flags Specify the flags to wait for
* \param[in] timeout Timeout value
*
* \return Returns \ref OS_WRAPPER_SUCCESS on success, or \ref OS_WRAPPER_ERROR
* in case of error
*/
uint32_t os_wrapper_thread_wait_flag(uint32_t flags, uint32_t timeout);

#ifdef __cplusplus
}
#endif

#endif /* __OS_WRAPPER_THREAD_H__ */

0 comments on commit 96e19af

Please sign in to comment.