1 
2 /**************************************************************************/
3 /*   Copyright (c) Cadence Design Systems, Inc.                           */
4 /*                                                                        */
5 /* Permission is hereby granted, free of charge, to any person obtaining  */
6 /* a copy of this software and associated documentation files (the        */
7 /* "Software"), to deal in the Software without restriction, including    */
8 /* without limitation the rights to use, copy, modify, merge, publish,    */
9 /* distribute, sublicense, and/or sell copies of the Software, and to     */
10 /* permit persons to whom the Software is furnished to do so, subject to  */
11 /* the following conditions:                                              */
12 /*                                                                        */
13 /* The above copyright notice and this permission notice shall be         */
14 /* included in all copies or substantial portions of the Software.        */
15 /*                                                                        */
16 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,        */
17 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF     */
18 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
19 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY   */
20 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,   */
21 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE      */
22 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                 */
23 /**************************************************************************/
24 
25 /**************************************************************************/
26 /*                                                                        */
27 /*  DESCRIPTION                                                           */
28 /*                                                                        */
29 /*    This file contains the interface functions to provide thread-safe   */
30 /*    operation of the C library. Both newlib and the Xtensa C Library    */
31 /*    are supported.                                                      */
32 /*                                                                        */
33 /*  RELEASE HISTORY                                                       */
34 /*                                                                        */
35 /*    DATE              NAME                      DESCRIPTION             */
36 /*                                                                        */
37 /*  12-31-2020     Cadence Design Systems   Initial Version 6.1.3         */
38 /*  12-31-2023     Xiuwen Cai               Modified comment(s), and      */
39 /*                                            added error handling in     */
40 /*                                            lock initialization,        */
41 /*                                            resulting in version 6.4.0  */
42 /*                                                                        */
43 /**************************************************************************/
44 
45 #include "tx_api.h"     /* TX_THREAD_SAFE_CLIB may be defined by tx_port.h */
46 #include "tx_thread.h"
47 #include "tx_initialize.h"
48 
49 /* Xtensa specific */
50 #include <xtensa/config/system.h>
51 
52 #ifdef TX_THREAD_SAFE_CLIB    /* this file is only needed if using C lib */
53 
54 #include <errno.h>
55 #include <stdlib.h>
56 #include <stdio.h>
57 #include <stdint.h>
58 
59 
60 #if XSHAL_CLIB == XTHAL_CLIB_NEWLIB
61 
62 #include <malloc.h>
63 
64 /* NOTE: should have been declared in reent.h... */
65 extern void _wrapup_reent(struct _reent * ptr);
66 
67 /* Mutex used for all C library protection */
68 TX_MUTEX  clib_lock_mutex;
69 
70 
71 /**************************************************************************/
72 /*    __malloc_lock - called by the malloc() family of routines when they */
73 /*    need to lock the memory pool. A call to malloc() may call this      */
74 /*    function recursively.                                               */
75 /**************************************************************************/
76 void
__malloc_lock(struct _reent * ptr)77 __malloc_lock (struct _reent * ptr)
78 {
79     if (_tx_thread_system_state != TX_INITIALIZE_IS_FINISHED) {
80         return;
81     }
82 
83     tx_mutex_get (&clib_lock_mutex, TX_WAIT_FOREVER);
84 }
85 
86 /**************************************************************************/
87 /*    __malloc_unlock - called by the malloc() family of routines when    */
88 /*    need to unlock the memory pool.                                     */
89 /**************************************************************************/
90 void
__malloc_unlock(struct _reent * ptr)91 __malloc_unlock (struct _reent * ptr)
92 {
93     if (_tx_thread_system_state != TX_INITIALIZE_IS_FINISHED) {
94         return;
95     }
96 
97 #ifndef THREADX_TESTSUITE   /* see THREADX_TESTSUITE comments below */
98     tx_mutex_prioritize (&clib_lock_mutex);    /* is this at all necessary? */
99 #endif
100     tx_mutex_put (&clib_lock_mutex);
101 }
102 
103 
104 /**************************************************************************/
105 /*    __env_lock - called by the setenv() family of routines when they    */
106 /*    need to modify the environment. A call to setenv() may call this    */
107 /*    function recursively.                                               */
108 /**************************************************************************/
109 void
__env_lock(struct _reent * ptr)110 __env_lock (struct _reent * ptr)
111 {
112     if (_tx_thread_system_state != TX_INITIALIZE_IS_FINISHED) {
113         return;
114     }
115 
116     tx_mutex_get (&clib_lock_mutex, TX_WAIT_FOREVER);
117 }
118 
119 /**************************************************************************/
120 /*    __env_unlock - called by the setenv() family of routines when they  */
121 /*    need to unlock the environment.                                     */
122 /**************************************************************************/
123 void
__env_unlock(struct _reent * ptr)124 __env_unlock (struct _reent * ptr)
125 {
126     if (_tx_thread_system_state != TX_INITIALIZE_IS_FINISHED) {
127         return;
128     }
129 
130     tx_mutex_prioritize (&clib_lock_mutex);
131     tx_mutex_put (&clib_lock_mutex);
132 }
133 
134 #endif /* XSHAL_CLIB == XTHAL_CLIB_NEWLIB */
135 
136 #if XSHAL_CLIB == XTHAL_CLIB_XCLIB
137 
138 #include <errno.h>
139 #include <sys/reent.h>
140 
141 #define XT_NUM_CLIB_LOCKS      (_MAX_LOCK + FOPEN_MAX)
142 
143 typedef TX_MUTEX * _Rmtx;
144 
145 static TX_MUTEX xclib_locks[XT_NUM_CLIB_LOCKS];
146 static uint32_t lcnt;
147 
148 /* Override this and set to nonzero to enable locking. */
149 int32_t _xclib_use_mt = 1;
150 
151 
152 /**************************************************************************/
153 /*    _Mtxinit - initialize a lock. Called once for each lock.            */
154 /**************************************************************************/
155 void
_Mtxinit(_Rmtx * mtx)156 _Mtxinit (_Rmtx * mtx)
157 {
158     TX_MUTEX * lock;
159 
160     if (lcnt >= XT_NUM_CLIB_LOCKS) {
161         /* Fatal error */
162         *mtx = NULL;
163         return;
164     }
165 
166     lock = &(xclib_locks[lcnt]);
167     lcnt++;
168 
169     /* See notes for newlib case below. */
170 #ifdef THREADX_TESTSUITE
171     tx_mutex_create (lock, "Clib lock", 0);
172 #else
173     tx_mutex_create (lock, "Clib lock", TX_INHERIT);
174 #endif
175 
176     *mtx = lock;
177 }
178 
179 /**************************************************************************/
180 /*    _Mtxdst - destroy a lock. Called once for each lock.                */
181 /**************************************************************************/
182 void
_Mtxdst(_Rmtx * mtx)183 _Mtxdst (_Rmtx * mtx)
184 {
185     if ((mtx) && (*mtx)) {
186         tx_mutex_delete (*mtx);
187     }
188 }
189 
190 /**************************************************************************/
191 /*    _Mtxlock - acquire lock.                                            */
192 /**************************************************************************/
193 void
_Mtxlock(_Rmtx * mtx)194 _Mtxlock (_Rmtx * mtx)
195 {
196     if ((mtx) && (*mtx)) {
197         tx_mutex_get (*mtx, TX_WAIT_FOREVER);
198     }
199 }
200 
201 /**************************************************************************/
202 /*    _Mtxunlock - release a lock.                                        */
203 /**************************************************************************/
204 void
_Mtxunlock(_Rmtx * mtx)205 _Mtxunlock (_Rmtx * mtx)
206 {
207     if ((mtx) && (*mtx)) {
208         tx_mutex_put (*mtx);
209     }
210 }
211 
212 #endif /* XSHAL_CLIB == XTHAL_CLIB_XCLIB */
213 
214 
215 /**************************************************************************/
216 /*    _sbrk_r - heap allocator. This function is called when the memory   */
217 /*    allocator needs a new chunk of memory.                              */
218 /*    The bounds of the heap area are global variables so that customer   */
219 /*    startup code can easily override them if needed.                    */
220 /*                                                                        */
221 /*    _tx_clib_heap_start    is the start of memory assigned to the heap  */
222 /*                           or 0 (NULL) if no memory is assigned (in     */
223 /*                           that case all calls to malloc will fail).    */
224 /*                                                                        */
225 /*   _tx_clib_heap_end       is the end of memory assigned to the heap    */
226 /*                           or 0 (NULL) if no memory is assigned. If a   */
227 /*                           nonzero start value is set then a nonzero    */
228 /*                           end value must be set.                       */
229 /**************************************************************************/
230 
231 char * _tx_clib_heap_start = NULL;
232 char * _tx_clib_heap_end   = NULL;
233 
234 void *
_sbrk_r(struct _reent * reent,int32_t incr)235 _sbrk_r (struct _reent * reent, int32_t incr)
236 {
237     static char * heap_ptr;
238     char * new_heap_ptr;
239     char * alloc_ptr;
240 
241     /* The heap is bound by _tx_clib_heap_{start,end}. */
242     if (heap_ptr == NULL) {
243         heap_ptr = _tx_clib_heap_start;
244     }
245 
246     new_heap_ptr = heap_ptr + incr;
247     if ((heap_ptr == NULL) ||                   /* no heap        */
248         (new_heap_ptr >= _tx_clib_heap_end) ||  /* heap exhausted */
249         (new_heap_ptr < heap_ptr)) {            /* wraparound     */
250         reent->_errno = ENOMEM;
251         return (void *) -1;
252     }
253 
254     alloc_ptr = heap_ptr;
255     heap_ptr = new_heap_ptr;
256     return (void *) alloc_ptr;
257 }
258 
259 
260 /**************************************************************************/
261 /*    _tx_clib_init - initialize C library thread safety support.         */
262 /*    Called by _tx_initialize_low_level().                               */
263 /**************************************************************************/
264 void
_tx_clib_init(void)265 _tx_clib_init (void)
266 {
267 #if XSHAL_CLIB == XTHAL_CLIB_NEWLIB
268 #ifdef THREADX_TESTSUITE
269   /*  Priority inheritance causes printf() (which calls malloc()
270       which calls __malloc_unlock() which calls tx_mutex_put()
271       which calls _tx_mutex_priority_change() if TX_INHERIT is set)
272       which causes the task to suspend and resume which sometimes
273       changes execution order in the very sensitive testsuite
274       and makes it fail.  So, for the testsuite, don't request
275       priority inheritance (it doesn't need it in any case).  */
276     tx_mutex_create (&clib_lock_mutex, "Clib lock", 0);
277 #else
278     tx_mutex_create (&clib_lock_mutex, "Clib lock", TX_INHERIT);
279 #endif
280 #endif /* NEWLIB */
281 
282 #if XSHAL_CLIB == XTHAL_CLIB_XCLIB
283     /* Nothing. */
284 #endif /* XCLIB */
285 }
286 
287 
288 /**************************************************************************/
289 /*    _tx_clib_reent_init - initialize C library thread reent structure.  */
290 /*    Called by tx_thread_create() to init per-thread C library state.    */
291 /**************************************************************************/
292 void
_tx_clib_reent_init(TX_THREAD * thread_ptr)293 _tx_clib_reent_init (TX_THREAD * thread_ptr)
294 {
295     if (thread_ptr == NULL) {
296         /* Should never happen */
297         return;
298     }
299 
300 #if XSHAL_CLIB == XTHAL_CLIB_NEWLIB
301     struct _reent * reent = &(thread_ptr->tx_thread_clib_reent);
302 
303     memset (reent, 0, sizeof(struct _reent));
304     _REENT_INIT_PTR (reent);
305     thread_ptr->tx_thread_clib_ptr = reent;
306 #endif
307 
308 #if XSHAL_CLIB == XTHAL_CLIB_XCLIB
309     thread_ptr->tx_thread_clib_ptr = &(thread_ptr->tx_thread_clib_reent);
310     _init_reent (thread_ptr->tx_thread_clib_ptr);
311 #endif
312 }
313 
314 
315 /**************************************************************************/
316 /*    _tx_clib_reent_cleanup - clean up C library thread reent structure. */
317 /*    Called by tx_thread_delete() to clean up per-thread C library state */
318 /*    and free any allocated memory (partial = 0).                        */
319 /*    Called by tx_thread_shell_entry and tx_thread_terminate to perform  */
320 /*    "atexit" processing and clean up stdio, but leaving the rest of the */
321 /*    structure intact so the thread can be restarted (partial = 1).      */
322 /**************************************************************************/
323 void
_tx_clib_reent_cleanup(TX_THREAD * thread_ptr,int32_t partial)324 _tx_clib_reent_cleanup (TX_THREAD * thread_ptr, int32_t partial)
325 {
326 #if XSHAL_CLIB == XTHAL_CLIB_NEWLIB
327     struct _reent * reent = &(thread_ptr->tx_thread_clib_reent);
328     FILE * fp = &(reent->__sf[0]);
329     int32_t i;
330 
331   /* Avoid closing stdin,stdout,stderr so other threads can still use them. */
332     for (i = 0; i < 3; i++) {
333         fp->_close = NULL;
334         fp++;
335     }
336 
337     if (partial != 0) {
338         /* Perform "atexit" processing and clean up stdio. */
339         _wrapup_reent (reent);
340     }
341     else {
342         /* Free all the heap memory allocated in the reent structure.
343            ThreadX requires that the thread has either exited or been
344            terminated before it can be deleted so we can assume that
345            _wrapup_reent has already been called for this thread. */
346         _reclaim_reent (reent);
347     }
348 #endif
349 
350 #if XSHAL_CLIB == XTHAL_CLIB_XCLIB
351   /* Unused, keep compiler happy */
352   (void) partial;
353 
354   /* File handle table is global; no allocated memory in struct. */
355   thread_ptr->tx_thread_clib_ptr = 0;
356 #endif
357 }
358 
359 
360 /**************************************************************************/
361 /*    _xt_wrapper - thread wrapper to handle C library init/cleanup.      */
362 /*    If C library thread safety is enabled, every thread is invoked      */
363 /*    via this wrapper in order to handle thread context setup/cleanup.   */
364 /**************************************************************************/
365 void
_xt_thread_wrapper(ULONG arg)366 _xt_thread_wrapper (ULONG arg)
367 {
368     TX_INTERRUPT_SAVE_AREA
369     TX_THREAD * thread_ptr = _tx_thread_current_ptr;
370 
371     /* No use for this parameter */
372     (void) arg;
373 
374     /* Init the C library thread context */
375     _tx_clib_reent_init (thread_ptr);
376 
377     /* Disable interrupts around the global context ptr update */
378     TX_DISABLE
379 
380 #if XSHAL_CLIB == XTHAL_CLIB_NEWLIB
381     _impure_ptr = thread_ptr->tx_thread_clib_ptr;
382 #endif
383 #if XSHAL_CLIB == XTHAL_CLIB_XCLIB
384     _reent_ptr = thread_ptr->tx_thread_clib_ptr;
385 #endif
386 
387     TX_RESTORE
388 
389     /* Call actual thread entry point */
390     (thread_ptr->tx_real_thread_entry)(thread_ptr->tx_thread_entry_parameter);
391 
392     /* Clean up C library thread context */
393     _tx_clib_reent_cleanup(thread_ptr, 1);
394     _tx_clib_reent_cleanup(thread_ptr, 0);
395 }
396 
397 
398 /**************************************************************************/
399 /*    _tx_clib_thread_setup - Xtensa-specific thread setup actions.       */
400 /*    This function will be called only if thread safe C library usage    */
401 /*    is enabled. It inserts the wrapper as the thread entry point and    */
402 /*    saves the actual entry point for later use.                         */
403 /**************************************************************************/
404 void
_tx_clib_thread_setup(TX_THREAD * thread_ptr)405 _tx_clib_thread_setup (TX_THREAD * thread_ptr)
406 {
407     thread_ptr->tx_real_thread_entry = thread_ptr->tx_thread_entry;
408     thread_ptr->tx_thread_entry      = &(_xt_thread_wrapper);
409 }
410 
411 #endif /* TX_THREAD_SAFE_CLIB */
412 
413