Skip to content

Thread class tz #6486

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions rtos/Thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ extern "C" void thread_terminate_hook(osThreadId_t id)

namespace rtos {

void Thread::constructor(osPriority priority,
#ifndef MBED_TZ_DEFAULT_ACCESS
#define MBED_TZ_DEFAULT_ACCESS 0
#endif

void Thread::constructor(uint32_t tz_module, osPriority priority,
uint32_t stack_size, unsigned char *stack_mem, const char *name) {

const uintptr_t unaligned_mem = reinterpret_cast<uintptr_t>(stack_mem);
Expand All @@ -60,11 +64,17 @@ void Thread::constructor(osPriority priority,
_attr.stack_size = aligned_size;
_attr.name = name ? name : "application_unnamed_thread";
_attr.stack_mem = reinterpret_cast<uint32_t*>(aligned_mem);
_attr.tz_module = tz_module;
}

void Thread::constructor(osPriority priority,
uint32_t stack_size, unsigned char *stack_mem, const char *name) {
constructor(MBED_TZ_DEFAULT_ACCESS, priority, stack_size, stack_mem, name);
}

void Thread::constructor(Callback<void()> task,
osPriority priority, uint32_t stack_size, unsigned char *stack_mem, const char *name) {
constructor(priority, stack_size, stack_mem, name);
constructor(MBED_TZ_DEFAULT_ACCESS, priority, stack_size, stack_mem, name);

switch (start(task)) {
case osErrorResource:
Expand Down
36 changes: 36 additions & 0 deletions rtos/Thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,16 @@ namespace rtos {
* Memory considerations: The thread control structures will be created on current thread's stack, both for the mbed OS
* and underlying RTOS objects (static or dynamic RTOS memory pools are not being used).
* Additionally the stack memory for this thread will be allocated on the heap, if it wasn't supplied to the constructor.
*
* @note
* MBED_TZ_DEFAULT_ACCESS (default:0) flag can be used to change the default access of all user threads in non-secure mode.
* MBED_TZ_DEFAULT_ACCESS set to 1, means all non-secure user threads have access to call secure functions.
* MBED_TZ_DEFAULT_ACCESS set to 0, means none of the non-secure user thread have access to call secure functions,
* to give access to particular thread used overloaded constructor with `tz_module` as argument during thread creation.
*
* MBED_TZ_DEFAULT_ACCESS is target specific define, should be set in targets.json file for Cortex-M23/M33 devices.
*/

class Thread : private mbed::NonCopyable<Thread> {
public:
/** Allocate a new thread without starting execution
Expand All @@ -81,14 +90,36 @@ class Thread : private mbed::NonCopyable<Thread> {
@param stack_mem pointer to the stack area to be used by this thread (default: NULL).
@param name name to be used for this thread. It has to stay allocated for the lifetime of the thread (default: NULL)

@note Default value of tz_module will be MBED_TZ_DEFAULT_ACCESS
@note You cannot call this function from ISR context.
*/

Thread(osPriority priority=osPriorityNormal,
uint32_t stack_size=OS_STACK_SIZE,
unsigned char *stack_mem=NULL, const char *name=NULL) {
constructor(priority, stack_size, stack_mem, name);
}

/** Allocate a new thread without starting execution
@param tz_module trustzone thread identifier (osThreadAttr_t::tz_module)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's a trustzone thread identifier ? How a user gets one and how does it alter a thread behavior ?

It would be interesting to have pointers to those answers in the function documentation.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@0xc0170 @pan- I have added description after this comment. Description is below this line, hence the comment is not outdated.
Does below description explain enough?

Context of RTOS threads in non-secure state must be saved when calling secure functions. tz_module ID is used to allocate context memory for threads, and it can be safely set to zero for threads not using secure calls at all. See "TrustZone RTOS Context Management" for more details.

Context of RTOS threads in non-secure state must be saved when calling secure functions.
tz_module ID is used to allocate context memory for threads, and it can be safely set to zero for
threads not using secure calls at all. See "TrustZone RTOS Context Management" for more details.
@param priority initial priority of the thread function. (default: osPriorityNormal).
@param stack_size stack size (in bytes) requirements for the thread function. (default: OS_STACK_SIZE).
@param stack_mem pointer to the stack area to be used by this thread (default: NULL).
@param name name to be used for this thread. It has to stay allocated for the lifetime of the thread (default: NULL)

@note You cannot call this function from ISR context.
*/

Thread(uint32_t tz_module, osPriority priority=osPriorityNormal,
uint32_t stack_size=OS_STACK_SIZE,
unsigned char *stack_mem=NULL, const char *name=NULL) {
constructor(tz_module, priority, stack_size, stack_mem, name);
}


/** Create a new thread, and start it executing the specified function.
@param task function to be executed by this thread.
@param priority initial priority of the thread function. (default: osPriorityNormal).
Expand Down Expand Up @@ -433,6 +464,11 @@ class Thread : private mbed::NonCopyable<Thread> {
uint32_t stack_size=OS_STACK_SIZE,
unsigned char *stack_mem=NULL,
const char *name=NULL);
void constructor(uint32_t tz_module,
osPriority priority=osPriorityNormal,
uint32_t stack_size=OS_STACK_SIZE,
unsigned char *stack_mem=NULL,
const char *name=NULL);
static void _thunk(void * thread_ptr);

mbed::Callback<void()> _task;
Expand Down