blob: 76a274f2e22da132bd77fe9e3fbaf6f1a1c1cb4e [file] [log] [blame]
Ken Liu91d44da2018-09-20 22:42:31 +08001/*
Kevin Peng82fbca52021-03-09 13:48:48 +08002 * Copyright (c) 2018-2021, Arm Limited. All rights reserved.
Ken Liu91d44da2018-09-20 22:42:31 +08003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7#ifndef __TFM_THREAD_H__
8#define __TFM_THREAD_H__
9
Ken Liu5a2b9052019-08-15 19:03:29 +080010#include <stdint.h>
11#include <stddef.h>
David Hu50711e32019-06-12 18:32:30 +080012#include "tfm_arch.h"
Ken Liu91d44da2018-09-20 22:42:31 +080013#include "cmsis_compiler.h"
14
Summer Qin66f1e032020-01-06 15:40:03 +080015/* State code */
16#define THRD_STATE_CREATING 0
Kevin Peng82fbca52021-03-09 13:48:48 +080017#define THRD_STATE_RUNNABLE 1
Summer Qin66f1e032020-01-06 15:40:03 +080018#define THRD_STATE_BLOCK 2
19#define THRD_STATE_DETACH 3
20#define THRD_STATE_INVALID 4
Ken Liu91d44da2018-09-20 22:42:31 +080021
22/* Security attribute - default as security */
23#define THRD_ATTR_SECURE_OFFSET 16
24#define THRD_ATTR_SECURE (0)
25#define THRD_ATTR_NON_SECURE (1 << THRD_ATTR_SECURE_OFFSET)
26
27/* Lower value has higher priority */
28#define THRD_PRIOR_MASK 0xFF
29#define THRD_PRIOR_HIGHEST 0x0
Mingyang Sun00df2352021-04-15 15:46:08 +080030#define THRD_PRIOR_HIGH 0xF
31#define THRD_PRIOR_MEDIUM 0x1F
32#define THRD_PRIOR_LOW 0x7F
Ken Liu91d44da2018-09-20 22:42:31 +080033#define THRD_PRIOR_LOWEST 0xFF
34
35/* Error code */
36#define THRD_SUCCESS 0
37#define THRD_ERR_INVALID_PARAM 1
38
39/* Thread entry function type */
Summer Qin66f1e032020-01-06 15:40:03 +080040typedef void *(*tfm_core_thrd_entry_t)(void *);
Ken Liu91d44da2018-09-20 22:42:31 +080041
42/* Thread context */
Summer Qin66f1e032020-01-06 15:40:03 +080043struct tfm_core_thread_t {
44 tfm_core_thrd_entry_t pfn; /* entry function */
Ken Liu5a2b9052019-08-15 19:03:29 +080045 void *param; /* entry parameter */
Summer Qincb190872020-01-06 16:08:26 +080046 uintptr_t stk_btm; /* stack bottom (lower address) */
47 uintptr_t stk_top; /* stack top (higher address)*/
Kevin Pengeca45b92021-02-09 14:46:50 +080048 uintptr_t flih_ctx; /* FLIH context pointer */
Ken Liu5a2b9052019-08-15 19:03:29 +080049 uint32_t prior; /* priority */
Summer Qin66f1e032020-01-06 15:40:03 +080050 uint32_t state; /* state */
Ken Liu91d44da2018-09-20 22:42:31 +080051
Summer Qind2ad7e72020-01-06 18:16:35 +080052 struct tfm_arch_ctx_t arch_ctx; /* State context */
Summer Qin66f1e032020-01-06 15:40:03 +080053 struct tfm_core_thread_t *next; /* next thread in list */
Ken Liu91d44da2018-09-20 22:42:31 +080054};
55
56/*
57 * Initialize a thread context with the necessary info.
58 *
59 * Parameters :
60 * pth - pointer of caller provided thread context
61 * pfn - thread entry function
62 * param - thread entry function parameter
Summer Qincb190872020-01-06 16:08:26 +080063 * stk_top - stack pointer top (higher address)
64 * stk_btm - stack pointer bottom (lower address)
Ken Liu91d44da2018-09-20 22:42:31 +080065 *
66 * Notes :
67 * Thread contex rely on caller allocated memory; initialize members in
68 * context. This function does not insert thread into schedulable list.
69 */
Summer Qin66f1e032020-01-06 15:40:03 +080070void tfm_core_thrd_init(struct tfm_core_thread_t *pth,
71 tfm_core_thrd_entry_t pfn, void *param,
Summer Qincb190872020-01-06 16:08:26 +080072 uintptr_t stk_top, uintptr_t stk_btm);
Ken Liu91d44da2018-09-20 22:42:31 +080073
Summer Qin66f1e032020-01-06 15:40:03 +080074/*
75 * Set thread priority.
Ken Liu91d44da2018-09-20 22:42:31 +080076 *
77 * Parameters :
78 * pth - pointer of thread context
79 * prior - priority value (0~255)
80 *
81 * Notes :
82 * Set thread priority. Priority is set to THRD_PRIOR_MEDIUM in
Summer Qin66f1e032020-01-06 15:40:03 +080083 * tfm_core_thrd_init().
Ken Liu91d44da2018-09-20 22:42:31 +080084 */
Summer Qin66f1e032020-01-06 15:40:03 +080085void __STATIC_INLINE tfm_core_thrd_set_priority(struct tfm_core_thread_t *pth,
86 uint32_t prior)
Ken Liu91d44da2018-09-20 22:42:31 +080087{
88 pth->prior &= ~THRD_PRIOR_MASK;
89 pth->prior |= prior & THRD_PRIOR_MASK;
90}
91
92/*
93 * Set thread security attribute.
94 *
95 * Parameters :
96 * pth - pointer of thread context
97 * attr_secure - THRD_ATTR_SECURE or THRD_ATTR_NON_SECURE
98 *
99 * Notes
100 * Reuse prior of thread context to shift down non-secure thread priority.
101 */
Summer Qin66f1e032020-01-06 15:40:03 +0800102void __STATIC_INLINE tfm_core_thrd_set_secure(struct tfm_core_thread_t *pth,
103 uint32_t attr_secure)
Ken Liu91d44da2018-09-20 22:42:31 +0800104{
105 pth->prior &= ~THRD_ATTR_NON_SECURE;
106 pth->prior |= attr_secure;
107}
108
109/*
Summer Qin66f1e032020-01-06 15:40:03 +0800110 * Set thread state.
Ken Liu91d44da2018-09-20 22:42:31 +0800111 *
112 * Parameters :
113 * pth - pointer of thread context
Summer Qin66f1e032020-01-06 15:40:03 +0800114 * new_state - new state of thread
Ken Liu91d44da2018-09-20 22:42:31 +0800115 *
116 * Return :
117 * None
118 *
119 * Notes :
Summer Qin66f1e032020-01-06 15:40:03 +0800120 * Thread state is not changed if invalid state value inputed.
Ken Liu91d44da2018-09-20 22:42:31 +0800121 */
Summer Qin66f1e032020-01-06 15:40:03 +0800122void tfm_core_thrd_set_state(struct tfm_core_thread_t *pth, uint32_t new_state);
Ken Liu91d44da2018-09-20 22:42:31 +0800123
124/*
Summer Qin66f1e032020-01-06 15:40:03 +0800125 * Get thread state.
Ken Liu91d44da2018-09-20 22:42:31 +0800126 *
127 * Parameters :
128 * pth - pointer of thread context
129 *
130 * Return :
Summer Qin66f1e032020-01-06 15:40:03 +0800131 * State of thread
Ken Liu91d44da2018-09-20 22:42:31 +0800132 */
Summer Qin66f1e032020-01-06 15:40:03 +0800133uint32_t __STATIC_INLINE tfm_core_thrd_get_state(struct tfm_core_thread_t *pth)
Ken Liu91d44da2018-09-20 22:42:31 +0800134{
Summer Qin66f1e032020-01-06 15:40:03 +0800135 return pth->state;
Ken Liu91d44da2018-09-20 22:42:31 +0800136}
137
138/*
139 * Set thread state return value.
140 *
141 * Parameters :
142 * pth - pointer of thread context
143 * retval - return value to be set for thread state
144 *
145 * Notes :
146 * This API is useful for blocked syscall blocking thread. Syscall
147 * could set its return value to the caller before caller goes.
148 */
Summer Qin66f1e032020-01-06 15:40:03 +0800149void __STATIC_INLINE tfm_core_thrd_set_retval(struct tfm_core_thread_t *pth,
150 uint32_t retval)
Ken Liu91d44da2018-09-20 22:42:31 +0800151{
Summer Qind2ad7e72020-01-06 18:16:35 +0800152 TFM_STATE_RET_VAL(&pth->arch_ctx) = retval;
Ken Liu91d44da2018-09-20 22:42:31 +0800153}
154
155/*
156 * Validate thread context and insert it into schedulable list.
157 *
158 * Parameters :
159 * pth - pointer of thread context
160 *
161 * Return :
162 * THRD_SUCCESS for success. Or an error is returned.
163 *
164 * Notes :
165 * This function validates thread info. It returns error if thread info
Xinyu Zhang3a453242021-04-16 17:57:09 +0800166 * is not correct. Thread is available after successful tfm_core_thrd_start().
Ken Liu91d44da2018-09-20 22:42:31 +0800167 */
Summer Qin66f1e032020-01-06 15:40:03 +0800168uint32_t tfm_core_thrd_start(struct tfm_core_thread_t *pth);
Ken Liu91d44da2018-09-20 22:42:31 +0800169
170/*
171 * Get current running thread.
172 *
173 * Return :
174 * Current running thread context pointer.
175 */
Kevin Peng82fbca52021-03-09 13:48:48 +0800176struct tfm_core_thread_t *tfm_core_thrd_get_curr(void);
Ken Liu91d44da2018-09-20 22:42:31 +0800177
178/*
Kevin Pengeca45b92021-02-09 14:46:50 +0800179 * Set the current running thread
180 * Note:
181 * This API is intended to update the current thread in FLIH handling.
182 * So that in nested FLIH interrupts, the handler knows which isolation
183 * boundary was preempted.
184 * Although the CURR_THRD is updated, it does not mean the running Partition
185 * thread is changed. It could also be the FLIH function which runs with the
186 * same isolation boundary of the CURR_THRD.
187 */
188void tfm_core_thrd_set_curr(struct tfm_core_thread_t *pth);
189
190/*
Kevin Peng82fbca52021-03-09 13:48:48 +0800191 * Get next thread to run in list.
Ken Liu91d44da2018-09-20 22:42:31 +0800192 *
193 * Return :
Kevin Peng82fbca52021-03-09 13:48:48 +0800194 * Pointer of next thread to run.
Ken Liu91d44da2018-09-20 22:42:31 +0800195 */
Kevin Peng82fbca52021-03-09 13:48:48 +0800196struct tfm_core_thread_t *tfm_core_thrd_get_next(void);
Ken Liu91d44da2018-09-20 22:42:31 +0800197
198/*
Ken Liu483f5da2019-04-24 10:45:21 +0800199 * Start scheduler for existing threads
200 *
201 * Parameters:
202 * pth - pointer of the caller context collecting thread
203 *
204 * Notes :
205 * This function should be called only ONCE to start the scheduler.
206 * Caller needs to provide a thread object to collect current context.
207 * The usage of the collected context is caller defined.
208 */
Summer Qin66f1e032020-01-06 15:40:03 +0800209void tfm_core_thrd_start_scheduler(struct tfm_core_thread_t *pth);
Ken Liu483f5da2019-04-24 10:45:21 +0800210
211/*
Ken Liu91d44da2018-09-20 22:42:31 +0800212 * Activate a scheduling action after exception.
213 *
214 * Notes :
215 * This function could be called multiple times before scheduling.
216 */
Summer Qin66f1e032020-01-06 15:40:03 +0800217void tfm_core_thrd_activate_schedule(void);
Ken Liu91d44da2018-09-20 22:42:31 +0800218
219/*
Summer Qind2ad7e72020-01-06 18:16:35 +0800220 * Save current architecture context into 'prev' thread and switch to 'next'.
Ken Liu91d44da2018-09-20 22:42:31 +0800221 *
222 * Parameters :
Summer Qind2ad7e72020-01-06 18:16:35 +0800223 * p_actx - latest caller context
Ken Liu91d44da2018-09-20 22:42:31 +0800224 * prev - previous thread to be switched out
225 * next - thread to be run
226 *
227 * Notes :
228 * This function could be called multiple times before scheduling.
229 */
Summer Qind2ad7e72020-01-06 18:16:35 +0800230void tfm_core_thrd_switch_context(struct tfm_arch_ctx_t *p_actx,
Summer Qin66f1e032020-01-06 15:40:03 +0800231 struct tfm_core_thread_t *prev,
232 struct tfm_core_thread_t *next);
Ken Liu91d44da2018-09-20 22:42:31 +0800233
Ken Liu91d44da2018-09-20 22:42:31 +0800234#endif