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