1 /*
2  * Copyright (c) 2018 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/kernel.h>
8 #include <zephyr/sys/atomic.h>
9 #include <cmsis_os.h>
10 
11 #define TOTAL_CMSIS_THREAD_PRIORITIES (osPriorityRealtime - osPriorityIdle + 1)
12 
_is_thread_cmsis_inactive(struct k_thread * thread)13 static inline int _is_thread_cmsis_inactive(struct k_thread *thread)
14 {
15 	uint8_t state = thread->base.thread_state;
16 
17 	return state & _THREAD_DEAD;
18 }
19 
zephyr_to_cmsis_priority(uint32_t z_prio)20 static inline int32_t zephyr_to_cmsis_priority(uint32_t z_prio)
21 {
22 	return(osPriorityRealtime - z_prio);
23 }
24 
cmsis_to_zephyr_priority(int32_t c_prio)25 static inline uint32_t cmsis_to_zephyr_priority(int32_t c_prio)
26 {
27 	return(osPriorityRealtime - c_prio);
28 }
29 
zephyr_thread_wrapper(void * arg1,void * arg2,void * arg3)30 static void zephyr_thread_wrapper(void *arg1, void *arg2, void *arg3)
31 {
32 	ARG_UNUSED(arg2);
33 
34 	void * (*fun_ptr)(void *) = arg3;
35 
36 	fun_ptr(arg1);
37 }
38 
39 /* clear related bit in cmsis thread status bitarray
40  * when terminating a thread
41  */
thread_abort_hook(struct k_thread * thread)42 void thread_abort_hook(struct k_thread *thread)
43 {
44 	uint32_t offset, instance;
45 
46 	osThreadDef_t *thread_def = (osThreadDef_t *)(thread->custom_data);
47 
48 	if (thread_def != NULL) {
49 		/* get thread instance index according to stack address */
50 		uintptr_t stack_start;
51 
52 #ifdef CONFIG_THREAD_STACK_MEM_MAPPED
53 		/* The offset calculation below requires physical address. */
54 		extern int arch_page_phys_get(void *virt, uintptr_t *phys);
55 		(void)arch_page_phys_get((void *)thread->stack_info.start, &stack_start);
56 #else
57 		stack_start = thread->stack_info.start;
58 #endif /* CONFIG_THREAD_STACK_MEM_MAPPED */
59 
60 		offset = stack_start - (uintptr_t)thread_def->stack_mem;
61 		instance = offset / K_THREAD_STACK_LEN(CONFIG_CMSIS_THREAD_MAX_STACK_SIZE);
62 		sys_bitarray_clear_bit((sys_bitarray_t *)(thread_def->status_mask), instance);
63 	}
64 }
65 
66 /**
67  * @brief Create a new thread.
68  */
osThreadCreate(const osThreadDef_t * thread_def,void * arg)69 osThreadId osThreadCreate(const osThreadDef_t *thread_def, void *arg)
70 {
71 	struct k_thread *cm_thread;
72 	uint32_t prio;
73 	k_tid_t tid;
74 	uint32_t stacksz;
75 	int ret;
76 	size_t instance;
77 
78 	k_thread_stack_t
79 	   (*stk_ptr)[K_THREAD_STACK_LEN(CONFIG_CMSIS_THREAD_MAX_STACK_SIZE)];
80 
81 	if ((thread_def == NULL) || (thread_def->instances == 0)) {
82 		return NULL;
83 	}
84 
85 	BUILD_ASSERT(
86 		CONFIG_NUM_PREEMPT_PRIORITIES >= TOTAL_CMSIS_THREAD_PRIORITIES,
87 		"Configure NUM_PREEMPT_PRIORITIES to at least"
88 		" TOTAL_CMSIS_THREAD_PRIORITIES");
89 
90 	__ASSERT(thread_def->stacksize <= CONFIG_CMSIS_THREAD_MAX_STACK_SIZE,
91 		 "invalid stack size\n");
92 
93 	if (k_is_in_isr()) {
94 		return NULL;
95 	}
96 
97 	__ASSERT((thread_def->tpriority >= osPriorityIdle) &&
98 		 (thread_def->tpriority <= osPriorityRealtime),
99 		 "invalid priority\n");
100 
101 	/* get an available thread instance */
102 	ret = sys_bitarray_alloc((sys_bitarray_t *)(thread_def->status_mask),
103 			1, &instance);
104 	if (ret != 0) {
105 		return NULL;
106 	}
107 
108 	stacksz = thread_def->stacksize;
109 	if (stacksz == 0U) {
110 		stacksz = CONFIG_CMSIS_THREAD_MAX_STACK_SIZE;
111 	}
112 
113 	k_poll_signal_init(thread_def->poll_signal);
114 	k_poll_event_init(thread_def->poll_event, K_POLL_TYPE_SIGNAL,
115 			K_POLL_MODE_NOTIFY_ONLY, thread_def->poll_signal);
116 
117 	cm_thread = thread_def->cm_thread;
118 	stk_ptr = thread_def->stack_mem;
119 	prio = cmsis_to_zephyr_priority(thread_def->tpriority);
120 	k_thread_custom_data_set((void *)thread_def);
121 
122 	tid = k_thread_create(&cm_thread[instance],
123 			stk_ptr[instance], stacksz,
124 			zephyr_thread_wrapper,
125 			(void *)arg, NULL, thread_def->pthread,
126 			prio, 0, K_NO_WAIT);
127 
128 	/* make custom_data pointer of thread point to its source thread_def,
129 	 * then we can use it to release thread instances
130 	 * when terminating threads
131 	 */
132 	tid->custom_data = (void *)thread_def;
133 
134 	return ((osThreadId)tid);
135 }
136 
137 /**
138  * @brief Return the thread ID of the current running thread.
139  */
osThreadGetId(void)140 osThreadId osThreadGetId(void)
141 {
142 	if (k_is_in_isr()) {
143 		return NULL;
144 	}
145 
146 	return (osThreadId)k_current_get();
147 }
148 
149 /**
150  * @brief Get current priority of an active thread.
151  */
osThreadGetPriority(osThreadId thread_id)152 osPriority osThreadGetPriority(osThreadId thread_id)
153 {
154 	k_tid_t thread = (k_tid_t)thread_id;
155 	uint32_t priority;
156 
157 	if ((thread_id == NULL) || (k_is_in_isr())) {
158 		return osPriorityError;
159 	}
160 
161 	priority = k_thread_priority_get(thread);
162 	return zephyr_to_cmsis_priority(priority);
163 }
164 
165 /**
166  * @brief Change priority of an active thread.
167  */
osThreadSetPriority(osThreadId thread_id,osPriority priority)168 osStatus osThreadSetPriority(osThreadId thread_id, osPriority priority)
169 {
170 	if (thread_id == NULL) {
171 		return osErrorParameter;
172 	}
173 
174 	if (k_is_in_isr()) {
175 		return osErrorISR;
176 	}
177 
178 	if (priority < osPriorityIdle || priority > osPriorityRealtime) {
179 		return osErrorValue;
180 	}
181 
182 	if (_is_thread_cmsis_inactive((k_tid_t)thread_id)) {
183 		return osErrorResource;
184 	}
185 
186 	k_thread_priority_set((k_tid_t)thread_id,
187 				cmsis_to_zephyr_priority(priority));
188 
189 	return osOK;
190 }
191 
192 /**
193  * @brief Terminate execution of a thread.
194  */
osThreadTerminate(osThreadId thread_id)195 osStatus osThreadTerminate(osThreadId thread_id)
196 {
197 	if (thread_id == NULL) {
198 		return osErrorParameter;
199 	}
200 
201 	if (k_is_in_isr()) {
202 		return osErrorISR;
203 	}
204 
205 	if (_is_thread_cmsis_inactive((k_tid_t)thread_id)) {
206 		return osErrorResource;
207 	}
208 
209 	k_thread_abort((k_tid_t)thread_id);
210 	return osOK;
211 }
212 
213 /**
214  * @brief Pass control to next thread that is in READY state.
215  */
osThreadYield(void)216 osStatus osThreadYield(void)
217 {
218 	if (k_is_in_isr()) {
219 		return osErrorISR;
220 	}
221 
222 	k_yield();
223 	return osOK;
224 }
225