Ken Liu | 91d44da | 2018-09-20 22:42:31 +0800 | [diff] [blame] | 1 | /* |
Summer Qin | 66f1e03 | 2020-01-06 15:40:03 +0800 | [diff] [blame] | 2 | * Copyright (c) 2018-2020, Arm Limited. All rights reserved. |
Ken Liu | 91d44da | 2018-09-20 22:42:31 +0800 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | * |
| 6 | */ |
| 7 | #include <inttypes.h> |
David Hu | 50711e3 | 2019-06-12 18:32:30 +0800 | [diff] [blame] | 8 | #include "tfm_arch.h" |
Ken Liu | 91d44da | 2018-09-20 22:42:31 +0800 | [diff] [blame] | 9 | #include "tfm_thread.h" |
| 10 | #include "tfm_utils.h" |
Edison Ai | 807fedb | 2019-03-07 11:22:03 +0800 | [diff] [blame] | 11 | #include "tfm_memory_utils.h" |
Ken Liu | 1f345b0 | 2020-05-30 21:11:05 +0800 | [diff] [blame^] | 12 | #include "tfm/tfm_core_svc.h" |
| 13 | #include "tfm/spm_api.h" |
Mingyang Sun | 94b1b41 | 2019-09-20 15:11:14 +0800 | [diff] [blame] | 14 | #include "tfm_core_utils.h" |
Ken Liu | 91d44da | 2018-09-20 22:42:31 +0800 | [diff] [blame] | 15 | |
| 16 | /* Force ZERO in case ZI(bss) clear is missing */ |
Summer Qin | 66f1e03 | 2020-01-06 15:40:03 +0800 | [diff] [blame] | 17 | static struct tfm_core_thread_t *p_thrd_head = NULL; |
| 18 | static struct tfm_core_thread_t *p_runn_head = NULL; |
| 19 | static struct tfm_core_thread_t *p_curr_thrd = NULL; |
Ken Liu | 91d44da | 2018-09-20 22:42:31 +0800 | [diff] [blame] | 20 | |
| 21 | /* Define Macro to fetch global to support future expansion (PERCPU e.g.) */ |
| 22 | #define LIST_HEAD p_thrd_head |
| 23 | #define RUNN_HEAD p_runn_head |
| 24 | #define CURR_THRD p_curr_thrd |
| 25 | |
Summer Qin | 66f1e03 | 2020-01-06 15:40:03 +0800 | [diff] [blame] | 26 | static struct tfm_core_thread_t *find_next_running_thread( |
| 27 | struct tfm_core_thread_t *pth) |
Ken Liu | 91d44da | 2018-09-20 22:42:31 +0800 | [diff] [blame] | 28 | { |
Summer Qin | 66f1e03 | 2020-01-06 15:40:03 +0800 | [diff] [blame] | 29 | while (pth && pth->state != THRD_STATE_RUNNING) { |
Ken Liu | 91d44da | 2018-09-20 22:42:31 +0800 | [diff] [blame] | 30 | pth = pth->next; |
| 31 | } |
| 32 | |
| 33 | return pth; |
| 34 | } |
| 35 | |
Mate Toth-Pal | 82cc07c | 2019-01-30 14:04:13 +0100 | [diff] [blame] | 36 | /* To get next running thread for scheduler */ |
Summer Qin | 66f1e03 | 2020-01-06 15:40:03 +0800 | [diff] [blame] | 37 | struct tfm_core_thread_t *tfm_core_thrd_get_next_thread(void) |
Mate Toth-Pal | 82cc07c | 2019-01-30 14:04:13 +0100 | [diff] [blame] | 38 | { |
| 39 | /* |
| 40 | * First RUNNING thread has highest priority since threads are sorted with |
| 41 | * priority. |
| 42 | */ |
| 43 | return find_next_running_thread(RUNN_HEAD); |
| 44 | } |
| 45 | |
Ken Liu | 91d44da | 2018-09-20 22:42:31 +0800 | [diff] [blame] | 46 | /* To get current thread for caller */ |
Summer Qin | 66f1e03 | 2020-01-06 15:40:03 +0800 | [diff] [blame] | 47 | struct tfm_core_thread_t *tfm_core_thrd_get_curr_thread(void) |
Ken Liu | 91d44da | 2018-09-20 22:42:31 +0800 | [diff] [blame] | 48 | { |
| 49 | return CURR_THRD; |
| 50 | } |
| 51 | |
| 52 | /* Insert a new thread into list by descending priority (Highest at head) */ |
Summer Qin | 66f1e03 | 2020-01-06 15:40:03 +0800 | [diff] [blame] | 53 | static void insert_by_prior(struct tfm_core_thread_t **head, |
| 54 | struct tfm_core_thread_t *node) |
Ken Liu | 91d44da | 2018-09-20 22:42:31 +0800 | [diff] [blame] | 55 | { |
| 56 | if (*head == NULL || (node->prior <= (*head)->prior)) { |
| 57 | node->next = *head; |
| 58 | *head = node; |
| 59 | } else { |
Summer Qin | 66f1e03 | 2020-01-06 15:40:03 +0800 | [diff] [blame] | 60 | struct tfm_core_thread_t *iter = *head; |
Ken Liu | 91d44da | 2018-09-20 22:42:31 +0800 | [diff] [blame] | 61 | |
| 62 | while (iter->next && (node->prior > iter->next->prior)) { |
| 63 | iter = iter->next; |
| 64 | } |
| 65 | node->next = iter->next; |
| 66 | iter->next = node; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | /* |
| 71 | * Set first running thread as head to reduce enumerate |
| 72 | * depth while searching for a first running thread. |
| 73 | */ |
Summer Qin | 66f1e03 | 2020-01-06 15:40:03 +0800 | [diff] [blame] | 74 | static void update_running_head(struct tfm_core_thread_t **runn, |
| 75 | struct tfm_core_thread_t *node) |
Ken Liu | 91d44da | 2018-09-20 22:42:31 +0800 | [diff] [blame] | 76 | { |
Summer Qin | 66f1e03 | 2020-01-06 15:40:03 +0800 | [diff] [blame] | 77 | if ((node->state == THRD_STATE_RUNNING) && |
Mate Toth-Pal | 82cc07c | 2019-01-30 14:04:13 +0100 | [diff] [blame] | 78 | (*runn == NULL || (node->prior < (*runn)->prior))) { |
Ken Liu | 91d44da | 2018-09-20 22:42:31 +0800 | [diff] [blame] | 79 | *runn = node; |
| 80 | } else { |
Summer Qin | ff8db15 | 2019-07-22 14:47:26 +0800 | [diff] [blame] | 81 | *runn = LIST_HEAD; |
Ken Liu | 91d44da | 2018-09-20 22:42:31 +0800 | [diff] [blame] | 82 | } |
| 83 | } |
| 84 | |
| 85 | /* Set context members only. No validation here */ |
Summer Qin | 66f1e03 | 2020-01-06 15:40:03 +0800 | [diff] [blame] | 86 | void tfm_core_thrd_init(struct tfm_core_thread_t *pth, |
| 87 | tfm_core_thrd_entry_t pfn, void *param, |
Summer Qin | cb19087 | 2020-01-06 16:08:26 +0800 | [diff] [blame] | 88 | uintptr_t stk_top, uintptr_t stk_btm) |
Ken Liu | 91d44da | 2018-09-20 22:42:31 +0800 | [diff] [blame] | 89 | { |
| 90 | pth->prior = THRD_PRIOR_MEDIUM; |
Summer Qin | 66f1e03 | 2020-01-06 15:40:03 +0800 | [diff] [blame] | 91 | pth->state = THRD_STATE_CREATING; |
Ken Liu | 91d44da | 2018-09-20 22:42:31 +0800 | [diff] [blame] | 92 | pth->pfn = pfn; |
| 93 | pth->param = param; |
Summer Qin | cb19087 | 2020-01-06 16:08:26 +0800 | [diff] [blame] | 94 | pth->stk_btm = stk_btm; |
| 95 | pth->stk_top = stk_top; |
Ken Liu | 91d44da | 2018-09-20 22:42:31 +0800 | [diff] [blame] | 96 | } |
| 97 | |
Summer Qin | 66f1e03 | 2020-01-06 15:40:03 +0800 | [diff] [blame] | 98 | uint32_t tfm_core_thrd_start(struct tfm_core_thread_t *pth) |
Ken Liu | 91d44da | 2018-09-20 22:42:31 +0800 | [diff] [blame] | 99 | { |
| 100 | /* Validate parameters before really start */ |
Summer Qin | 66f1e03 | 2020-01-06 15:40:03 +0800 | [diff] [blame] | 101 | if ((pth->state != THRD_STATE_CREATING) || |
Ken Liu | 91d44da | 2018-09-20 22:42:31 +0800 | [diff] [blame] | 102 | (pth->pfn == NULL) || |
Summer Qin | cb19087 | 2020-01-06 16:08:26 +0800 | [diff] [blame] | 103 | (pth->stk_btm == 0) || |
| 104 | (pth->stk_top == 0)) { |
Ken Liu | 91d44da | 2018-09-20 22:42:31 +0800 | [diff] [blame] | 105 | return THRD_ERR_INVALID_PARAM; |
| 106 | } |
| 107 | |
| 108 | /* Thread management runs in handler mode; set context for thread mode. */ |
Summer Qin | af3b9e1 | 2020-01-13 15:56:36 +0800 | [diff] [blame] | 109 | tfm_arch_init_context(&pth->arch_ctx, pth->param, (uintptr_t)pth->pfn, |
| 110 | pth->stk_btm, pth->stk_top); |
Ken Liu | 91d44da | 2018-09-20 22:42:31 +0800 | [diff] [blame] | 111 | |
| 112 | /* Insert a new thread with priority */ |
| 113 | insert_by_prior(&LIST_HEAD, pth); |
| 114 | |
| 115 | /* Mark it as RUNNING after insertion */ |
Summer Qin | 66f1e03 | 2020-01-06 15:40:03 +0800 | [diff] [blame] | 116 | tfm_core_thrd_set_state(pth, THRD_STATE_RUNNING); |
Ken Liu | 91d44da | 2018-09-20 22:42:31 +0800 | [diff] [blame] | 117 | |
| 118 | return THRD_SUCCESS; |
| 119 | } |
| 120 | |
Summer Qin | 66f1e03 | 2020-01-06 15:40:03 +0800 | [diff] [blame] | 121 | void tfm_core_thrd_set_state(struct tfm_core_thread_t *pth, uint32_t new_state) |
Ken Liu | 91d44da | 2018-09-20 22:42:31 +0800 | [diff] [blame] | 122 | { |
Summer Qin | 66f1e03 | 2020-01-06 15:40:03 +0800 | [diff] [blame] | 123 | TFM_CORE_ASSERT(pth != NULL && new_state < THRD_STATE_INVALID); |
Ken Liu | 91d44da | 2018-09-20 22:42:31 +0800 | [diff] [blame] | 124 | |
Summer Qin | 66f1e03 | 2020-01-06 15:40:03 +0800 | [diff] [blame] | 125 | pth->state = new_state; |
Ken Liu | 91d44da | 2018-09-20 22:42:31 +0800 | [diff] [blame] | 126 | update_running_head(&RUNN_HEAD, pth); |
| 127 | } |
| 128 | |
Ken Liu | 91d44da | 2018-09-20 22:42:31 +0800 | [diff] [blame] | 129 | /* Scheduling won't happen immediately but after the exception returns */ |
Summer Qin | 66f1e03 | 2020-01-06 15:40:03 +0800 | [diff] [blame] | 130 | void tfm_core_thrd_activate_schedule(void) |
Ken Liu | 91d44da | 2018-09-20 22:42:31 +0800 | [diff] [blame] | 131 | { |
David Hu | 50711e3 | 2019-06-12 18:32:30 +0800 | [diff] [blame] | 132 | tfm_arch_trigger_pendsv(); |
Ken Liu | 91d44da | 2018-09-20 22:42:31 +0800 | [diff] [blame] | 133 | } |
| 134 | |
Summer Qin | 66f1e03 | 2020-01-06 15:40:03 +0800 | [diff] [blame] | 135 | void tfm_core_thrd_start_scheduler(struct tfm_core_thread_t *pth) |
Ken Liu | 483f5da | 2019-04-24 10:45:21 +0800 | [diff] [blame] | 136 | { |
| 137 | /* |
Ken Liu | 490281d | 2019-12-30 15:55:26 +0800 | [diff] [blame] | 138 | * There is no selected thread before scheduler start, assign the caller |
| 139 | * provided thread as the current thread. Update the hardware PSP/PSPLIM |
| 140 | * with the value in thread context to ensure they are identical. |
| 141 | * This function can be called only ONCE; further calling triggers assert. |
Ken Liu | 483f5da | 2019-04-24 10:45:21 +0800 | [diff] [blame] | 142 | */ |
Ken Liu | f250b8b | 2019-12-27 16:31:24 +0800 | [diff] [blame] | 143 | TFM_CORE_ASSERT(CURR_THRD == NULL); |
| 144 | TFM_CORE_ASSERT(pth != NULL); |
Summer Qin | d2ad7e7 | 2020-01-06 18:16:35 +0800 | [diff] [blame] | 145 | TFM_CORE_ASSERT(pth->arch_ctx.sp != 0); |
Ken Liu | 490281d | 2019-12-30 15:55:26 +0800 | [diff] [blame] | 146 | |
Summer Qin | d2ad7e7 | 2020-01-06 18:16:35 +0800 | [diff] [blame] | 147 | tfm_arch_update_ctx(&pth->arch_ctx); |
Ken Liu | 483f5da | 2019-04-24 10:45:21 +0800 | [diff] [blame] | 148 | |
| 149 | CURR_THRD = pth; |
Ken Liu | 490281d | 2019-12-30 15:55:26 +0800 | [diff] [blame] | 150 | |
Summer Qin | 66f1e03 | 2020-01-06 15:40:03 +0800 | [diff] [blame] | 151 | tfm_core_thrd_activate_schedule(); |
Ken Liu | 483f5da | 2019-04-24 10:45:21 +0800 | [diff] [blame] | 152 | } |
| 153 | |
Summer Qin | d2ad7e7 | 2020-01-06 18:16:35 +0800 | [diff] [blame] | 154 | void tfm_core_thrd_switch_context(struct tfm_arch_ctx_t *p_actx, |
Summer Qin | 66f1e03 | 2020-01-06 15:40:03 +0800 | [diff] [blame] | 155 | struct tfm_core_thread_t *prev, |
| 156 | struct tfm_core_thread_t *next) |
Ken Liu | 91d44da | 2018-09-20 22:42:31 +0800 | [diff] [blame] | 157 | { |
Ken Liu | f250b8b | 2019-12-27 16:31:24 +0800 | [diff] [blame] | 158 | TFM_CORE_ASSERT(prev != NULL); |
| 159 | TFM_CORE_ASSERT(next != NULL); |
Ken Liu | 483f5da | 2019-04-24 10:45:21 +0800 | [diff] [blame] | 160 | |
| 161 | /* |
| 162 | * First, update latest context into the current thread context. |
| 163 | * Then, update background context with next thread's context. |
| 164 | */ |
Summer Qin | d2ad7e7 | 2020-01-06 18:16:35 +0800 | [diff] [blame] | 165 | tfm_core_util_memcpy(&prev->arch_ctx, p_actx, sizeof(*p_actx)); |
| 166 | tfm_core_util_memcpy(p_actx, &next->arch_ctx, sizeof(next->arch_ctx)); |
Ken Liu | 483f5da | 2019-04-24 10:45:21 +0800 | [diff] [blame] | 167 | |
| 168 | /* Update current thread indicator */ |
Ken Liu | 91d44da | 2018-09-20 22:42:31 +0800 | [diff] [blame] | 169 | CURR_THRD = next; |
| 170 | } |