1 /*
2  * FreeRTOS Kernel V11.0.1
3  * Copyright (C) 2020 Cambridge Consultants Ltd.
4  *
5  * SPDX-License-Identifier: MIT
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy of
8  * this software and associated documentation files (the "Software"), to deal in
9  * the Software without restriction, including without limitation the rights to
10  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11  * the Software, and to permit persons to whom the Software is furnished to do so,
12  * subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in all
15  * copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  * https://www.FreeRTOS.org
25  * https://github.com/FreeRTOS
26  *
27  */
28 
29 /*-----------------------------------------------------------
30 * Implementation of functions defined in portable.h for the Posix port.
31 *
32 * Each task has a pthread which eases use of standard debuggers
33 * (allowing backtraces of tasks etc). Threads for tasks that are not
34 * running are blocked in sigwait().
35 *
36 * Task switch is done by resuming the thread for the next task by
37 * signaling the condition variable and then waiting on a condition variable
38 * with the current thread.
39 *
40 * The timer interrupt uses SIGALRM and care is taken to ensure that
41 * the signal handler runs only on the thread for the current task.
42 *
43 * Use of part of the standard C library requires care as some
44 * functions can take pthread mutexes internally which can result in
45 * deadlocks as the FreeRTOS kernel can switch tasks while they're
46 * holding a pthread mutex.
47 *
48 * stdio (printf() and friends) should be called from a single task
49 * only or serialized with a FreeRTOS primitive such as a binary
50 * semaphore or mutex.
51 *----------------------------------------------------------*/
52 #include "portmacro.h"
53 
54 #include <errno.h>
55 #include <pthread.h>
56 #include <signal.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include <sys/time.h>
61 #include <sys/times.h>
62 #include <time.h>
63 
64 #ifdef __APPLE__
65     #include <mach/mach_vm.h>
66 #endif
67 
68 /* Scheduler includes. */
69 #include "FreeRTOS.h"
70 #include "task.h"
71 #include "timers.h"
72 #include "utils/wait_for_event.h"
73 /*-----------------------------------------------------------*/
74 
75 #define SIG_RESUME    SIGUSR1
76 
77 typedef struct THREAD
78 {
79     pthread_t pthread;
80     TaskFunction_t pxCode;
81     void * pvParams;
82     BaseType_t xDying;
83     struct event * ev;
84 } Thread_t;
85 
86 /*
87  * The additional per-thread data is stored at the beginning of the
88  * task's stack.
89  */
prvGetThreadFromTask(TaskHandle_t xTask)90 static inline Thread_t * prvGetThreadFromTask( TaskHandle_t xTask )
91 {
92     StackType_t * pxTopOfStack = *( StackType_t ** ) xTask;
93 
94     return ( Thread_t * ) ( pxTopOfStack + 1 );
95 }
96 
97 /*-----------------------------------------------------------*/
98 
99 static pthread_once_t hSigSetupThread = PTHREAD_ONCE_INIT;
100 static sigset_t xAllSignals;
101 static sigset_t xSchedulerOriginalSignalMask;
102 static pthread_t hMainThread = ( pthread_t ) NULL;
103 static volatile BaseType_t uxCriticalNesting;
104 /*-----------------------------------------------------------*/
105 
106 static BaseType_t xSchedulerEnd = pdFALSE;
107 /*-----------------------------------------------------------*/
108 
109 static void prvSetupSignalsAndSchedulerPolicy( void );
110 static void prvSetupTimerInterrupt( void );
111 static void * prvWaitForStart( void * pvParams );
112 static void prvSwitchThread( Thread_t * xThreadToResume,
113                              Thread_t * xThreadToSuspend );
114 static void prvSuspendSelf( Thread_t * thread );
115 static void prvResumeThread( Thread_t * xThreadId );
116 static void vPortSystemTickHandler( int sig );
117 static void vPortStartFirstTask( void );
118 static void prvPortYieldFromISR( void );
119 /*-----------------------------------------------------------*/
120 
121 static void prvFatalError( const char * pcCall,
122                            int iErrno ) __attribute__( ( __noreturn__ ) );
123 
prvFatalError(const char * pcCall,int iErrno)124 void prvFatalError( const char * pcCall,
125                     int iErrno )
126 {
127     fprintf( stderr, "%s: %s\n", pcCall, strerror( iErrno ) );
128     abort();
129 }
130 
131 /*
132  * See header file for description.
133  */
pxPortInitialiseStack(StackType_t * pxTopOfStack,StackType_t * pxEndOfStack,TaskFunction_t pxCode,void * pvParameters)134 StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,
135                                      StackType_t * pxEndOfStack,
136                                      TaskFunction_t pxCode,
137                                      void * pvParameters )
138 {
139     Thread_t * thread;
140     pthread_attr_t xThreadAttributes;
141     size_t ulStackSize;
142     int iRet;
143 
144     ( void ) pthread_once( &hSigSetupThread, prvSetupSignalsAndSchedulerPolicy );
145 
146     /*
147      * Store the additional thread data at the start of the stack.
148      */
149     thread = ( Thread_t * ) ( pxTopOfStack + 1 ) - 1;
150     pxTopOfStack = ( StackType_t * ) thread - 1;
151 
152     #ifdef __APPLE__
153         pxEndOfStack = ( StackType_t * ) mach_vm_round_page( pxEndOfStack );
154     #endif
155 
156     ulStackSize = ( size_t ) ( pxTopOfStack + 1 - pxEndOfStack ) * sizeof( *pxTopOfStack );
157 
158     #ifdef __APPLE__
159         ulStackSize = mach_vm_trunc_page( ulStackSize );
160     #endif
161 
162     thread->pxCode = pxCode;
163     thread->pvParams = pvParameters;
164     thread->xDying = pdFALSE;
165 
166     pthread_attr_init( &xThreadAttributes );
167     iRet = pthread_attr_setstack( &xThreadAttributes, pxEndOfStack, ulStackSize );
168 
169     if( iRet != 0 )
170     {
171         fprintf( stderr, "[WARN] pthread_attr_setstack failed with return value: %d. Default stack will be used.\n", iRet );
172         fprintf( stderr, "[WARN] Increase the stack size to PTHREAD_STACK_MIN.\n" );
173     }
174 
175     thread->ev = event_create();
176 
177     vPortEnterCritical();
178 
179     iRet = pthread_create( &thread->pthread, &xThreadAttributes,
180                            prvWaitForStart, thread );
181 
182     if( iRet != 0 )
183     {
184         prvFatalError( "pthread_create", iRet );
185     }
186 
187     vPortExitCritical();
188 
189     return pxTopOfStack;
190 }
191 /*-----------------------------------------------------------*/
192 
vPortStartFirstTask(void)193 void vPortStartFirstTask( void )
194 {
195     Thread_t * pxFirstThread = prvGetThreadFromTask( xTaskGetCurrentTaskHandle() );
196 
197     /* Start the first task. */
198     prvResumeThread( pxFirstThread );
199 }
200 /*-----------------------------------------------------------*/
201 
202 /*
203  * See header file for description.
204  */
xPortStartScheduler(void)205 BaseType_t xPortStartScheduler( void )
206 {
207     int iSignal;
208     sigset_t xSignals;
209 
210     hMainThread = pthread_self();
211 
212     /* Start the timer that generates the tick ISR(SIGALRM).
213      * Interrupts are disabled here already. */
214     prvSetupTimerInterrupt();
215 
216     /*
217      * Block SIG_RESUME before starting any tasks so the main thread can sigwait on it.
218      * To sigwait on an unblocked signal is undefined.
219      * https://pubs.opengroup.org/onlinepubs/009604499/functions/sigwait.html
220      */
221     sigemptyset( &xSignals );
222     sigaddset( &xSignals, SIG_RESUME );
223     ( void ) pthread_sigmask( SIG_BLOCK, &xSignals, NULL );
224 
225     /* Start the first task. */
226     vPortStartFirstTask();
227 
228     /* Wait until signaled by vPortEndScheduler(). */
229     while( xSchedulerEnd != pdTRUE )
230     {
231         sigwait( &xSignals, &iSignal );
232     }
233 
234     /* Cancel the Idle task and free its resources */
235     #if ( INCLUDE_xTaskGetIdleTaskHandle == 1 )
236         vPortCancelThread( xTaskGetIdleTaskHandle() );
237     #endif
238 
239     #if ( configUSE_TIMERS == 1 )
240         /* Cancel the Timer task and free its resources */
241         vPortCancelThread( xTimerGetTimerDaemonTaskHandle() );
242     #endif /* configUSE_TIMERS */
243 
244     /* Restore original signal mask. */
245     ( void ) pthread_sigmask( SIG_SETMASK, &xSchedulerOriginalSignalMask, NULL );
246 
247     return 0;
248 }
249 /*-----------------------------------------------------------*/
250 
vPortEndScheduler(void)251 void vPortEndScheduler( void )
252 {
253     struct itimerval itimer;
254     struct sigaction sigtick;
255     Thread_t * xCurrentThread;
256 
257     /* Stop the timer and ignore any pending SIGALRMs that would end
258      * up running on the main thread when it is resumed. */
259     itimer.it_value.tv_sec = 0;
260     itimer.it_value.tv_usec = 0;
261 
262     itimer.it_interval.tv_sec = 0;
263     itimer.it_interval.tv_usec = 0;
264     ( void ) setitimer( ITIMER_REAL, &itimer, NULL );
265 
266     sigtick.sa_flags = 0;
267     sigtick.sa_handler = SIG_IGN;
268     sigemptyset( &sigtick.sa_mask );
269     sigaction( SIGALRM, &sigtick, NULL );
270 
271     /* Signal the scheduler to exit its loop. */
272     xSchedulerEnd = pdTRUE;
273     ( void ) pthread_kill( hMainThread, SIG_RESUME );
274 
275     xCurrentThread = prvGetThreadFromTask( xTaskGetCurrentTaskHandle() );
276     prvSuspendSelf( xCurrentThread );
277 }
278 /*-----------------------------------------------------------*/
279 
vPortEnterCritical(void)280 void vPortEnterCritical( void )
281 {
282     if( uxCriticalNesting == 0 )
283     {
284         vPortDisableInterrupts();
285     }
286 
287     uxCriticalNesting++;
288 }
289 /*-----------------------------------------------------------*/
290 
vPortExitCritical(void)291 void vPortExitCritical( void )
292 {
293     uxCriticalNesting--;
294 
295     /* If we have reached 0 then re-enable the interrupts. */
296     if( uxCriticalNesting == 0 )
297     {
298         vPortEnableInterrupts();
299     }
300 }
301 /*-----------------------------------------------------------*/
302 
prvPortYieldFromISR(void)303 static void prvPortYieldFromISR( void )
304 {
305     Thread_t * xThreadToSuspend;
306     Thread_t * xThreadToResume;
307 
308     xThreadToSuspend = prvGetThreadFromTask( xTaskGetCurrentTaskHandle() );
309 
310     vTaskSwitchContext();
311 
312     xThreadToResume = prvGetThreadFromTask( xTaskGetCurrentTaskHandle() );
313 
314     prvSwitchThread( xThreadToResume, xThreadToSuspend );
315 }
316 /*-----------------------------------------------------------*/
317 
vPortYield(void)318 void vPortYield( void )
319 {
320     vPortEnterCritical();
321 
322     prvPortYieldFromISR();
323 
324     vPortExitCritical();
325 }
326 /*-----------------------------------------------------------*/
327 
vPortDisableInterrupts(void)328 void vPortDisableInterrupts( void )
329 {
330     pthread_sigmask( SIG_BLOCK, &xAllSignals, NULL );
331 }
332 /*-----------------------------------------------------------*/
333 
vPortEnableInterrupts(void)334 void vPortEnableInterrupts( void )
335 {
336     pthread_sigmask( SIG_UNBLOCK, &xAllSignals, NULL );
337 }
338 /*-----------------------------------------------------------*/
339 
xPortSetInterruptMask(void)340 UBaseType_t xPortSetInterruptMask( void )
341 {
342     /* Interrupts are always disabled inside ISRs (signals
343      * handlers). */
344     return ( UBaseType_t ) 0;
345 }
346 /*-----------------------------------------------------------*/
347 
vPortClearInterruptMask(UBaseType_t uxMask)348 void vPortClearInterruptMask( UBaseType_t uxMask )
349 {
350     ( void ) uxMask;
351 }
352 /*-----------------------------------------------------------*/
353 
prvGetTimeNs(void)354 static uint64_t prvGetTimeNs( void )
355 {
356     struct timespec t;
357 
358     clock_gettime( CLOCK_MONOTONIC, &t );
359 
360     return ( uint64_t ) t.tv_sec * ( uint64_t ) 1000000000UL + ( uint64_t ) t.tv_nsec;
361 }
362 
363 static uint64_t prvStartTimeNs;
364 
365 /* commented as part of the code below in vPortSystemTickHandler,
366  * to adjust timing according to full demo requirements */
367 /* static uint64_t prvTickCount; */
368 
369 /*
370  * Setup the systick timer to generate the tick interrupts at the required
371  * frequency.
372  */
prvSetupTimerInterrupt(void)373 void prvSetupTimerInterrupt( void )
374 {
375     struct itimerval itimer;
376     int iRet;
377 
378     /* Initialise the structure with the current timer information. */
379     iRet = getitimer( ITIMER_REAL, &itimer );
380 
381     if( iRet == -1 )
382     {
383         prvFatalError( "getitimer", errno );
384     }
385 
386     /* Set the interval between timer events. */
387     itimer.it_interval.tv_sec = 0;
388     itimer.it_interval.tv_usec = portTICK_RATE_MICROSECONDS;
389 
390     /* Set the current count-down. */
391     itimer.it_value.tv_sec = 0;
392     itimer.it_value.tv_usec = portTICK_RATE_MICROSECONDS;
393 
394     /* Set-up the timer interrupt. */
395     iRet = setitimer( ITIMER_REAL, &itimer, NULL );
396 
397     if( iRet == -1 )
398     {
399         prvFatalError( "setitimer", errno );
400     }
401 
402     prvStartTimeNs = prvGetTimeNs();
403 }
404 /*-----------------------------------------------------------*/
405 
vPortSystemTickHandler(int sig)406 static void vPortSystemTickHandler( int sig )
407 {
408     Thread_t * pxThreadToSuspend;
409     Thread_t * pxThreadToResume;
410 
411     ( void ) sig;
412 
413 /* uint64_t xExpectedTicks; */
414 
415     uxCriticalNesting++; /* Signals are blocked in this signal handler. */
416 
417     #if ( configUSE_PREEMPTION == 1 )
418         pxThreadToSuspend = prvGetThreadFromTask( xTaskGetCurrentTaskHandle() );
419     #endif
420 
421     /* Tick Increment, accounting for any lost signals or drift in
422      * the timer. */
423 
424 /*
425  *      Comment code to adjust timing according to full demo requirements
426  *      xExpectedTicks = (prvGetTimeNs() - prvStartTimeNs)
427  *        / (portTICK_RATE_MICROSECONDS * 1000);
428  * do { */
429     xTaskIncrementTick();
430 
431 /*        prvTickCount++;
432  *    } while (prvTickCount < xExpectedTicks);
433  */
434 
435     #if ( configUSE_PREEMPTION == 1 )
436         /* Select Next Task. */
437         vTaskSwitchContext();
438 
439         pxThreadToResume = prvGetThreadFromTask( xTaskGetCurrentTaskHandle() );
440 
441         prvSwitchThread( pxThreadToResume, pxThreadToSuspend );
442     #endif
443 
444     uxCriticalNesting--;
445 }
446 /*-----------------------------------------------------------*/
447 
vPortThreadDying(void * pxTaskToDelete,volatile BaseType_t * pxPendYield)448 void vPortThreadDying( void * pxTaskToDelete,
449                        volatile BaseType_t * pxPendYield )
450 {
451     Thread_t * pxThread = prvGetThreadFromTask( pxTaskToDelete );
452 
453     ( void ) pxPendYield;
454 
455     pxThread->xDying = pdTRUE;
456 }
457 
vPortCancelThread(void * pxTaskToDelete)458 void vPortCancelThread( void * pxTaskToDelete )
459 {
460     Thread_t * pxThreadToCancel = prvGetThreadFromTask( pxTaskToDelete );
461 
462     /*
463      * The thread has already been suspended so it can be safely cancelled.
464      */
465     pthread_cancel( pxThreadToCancel->pthread );
466     pthread_join( pxThreadToCancel->pthread, NULL );
467     event_delete( pxThreadToCancel->ev );
468 }
469 /*-----------------------------------------------------------*/
470 
prvWaitForStart(void * pvParams)471 static void * prvWaitForStart( void * pvParams )
472 {
473     Thread_t * pxThread = pvParams;
474 
475     prvSuspendSelf( pxThread );
476 
477     /* Resumed for the first time, unblocks all signals. */
478     uxCriticalNesting = 0;
479     vPortEnableInterrupts();
480 
481     /* Call the task's entry point. */
482     pxThread->pxCode( pxThread->pvParams );
483 
484     /* A function that implements a task must not exit or attempt to return to
485      * its caller as there is nothing to return to. If a task wants to exit it
486      * should instead call vTaskDelete( NULL ). Artificially force an assert()
487      * to be triggered if configASSERT() is defined, so application writers can
488      * catch the error. */
489     configASSERT( pdFALSE );
490 
491     return NULL;
492 }
493 /*-----------------------------------------------------------*/
494 
prvSwitchThread(Thread_t * pxThreadToResume,Thread_t * pxThreadToSuspend)495 static void prvSwitchThread( Thread_t * pxThreadToResume,
496                              Thread_t * pxThreadToSuspend )
497 {
498     BaseType_t uxSavedCriticalNesting;
499 
500     if( pxThreadToSuspend != pxThreadToResume )
501     {
502         /*
503          * Switch tasks.
504          *
505          * The critical section nesting is per-task, so save it on the
506          * stack of the current (suspending thread), restoring it when
507          * we switch back to this task.
508          */
509         uxSavedCriticalNesting = uxCriticalNesting;
510 
511         prvResumeThread( pxThreadToResume );
512 
513         if( pxThreadToSuspend->xDying == pdTRUE )
514         {
515             pthread_exit( NULL );
516         }
517 
518         prvSuspendSelf( pxThreadToSuspend );
519 
520         uxCriticalNesting = uxSavedCriticalNesting;
521     }
522 }
523 /*-----------------------------------------------------------*/
524 
prvSuspendSelf(Thread_t * thread)525 static void prvSuspendSelf( Thread_t * thread )
526 {
527     /*
528      * Suspend this thread by waiting for a pthread_cond_signal event.
529      *
530      * A suspended thread must not handle signals (interrupts) so
531      * all signals must be blocked by calling this from:
532      *
533      * - Inside a critical section (vPortEnterCritical() /
534      *   vPortExitCritical()).
535      *
536      * - From a signal handler that has all signals masked.
537      *
538      * - A thread with all signals blocked with pthread_sigmask().
539      */
540     event_wait( thread->ev );
541 }
542 
543 /*-----------------------------------------------------------*/
544 
prvResumeThread(Thread_t * xThreadId)545 static void prvResumeThread( Thread_t * xThreadId )
546 {
547     if( pthread_self() != xThreadId->pthread )
548     {
549         event_signal( xThreadId->ev );
550     }
551 }
552 /*-----------------------------------------------------------*/
553 
prvSetupSignalsAndSchedulerPolicy(void)554 static void prvSetupSignalsAndSchedulerPolicy( void )
555 {
556     struct sigaction sigtick;
557     int iRet;
558 
559     hMainThread = pthread_self();
560 
561     /* Initialise common signal masks. */
562     sigfillset( &xAllSignals );
563 
564     /* Don't block SIGINT so this can be used to break into GDB while
565      * in a critical section. */
566     sigdelset( &xAllSignals, SIGINT );
567 
568     /*
569      * Block all signals in this thread so all new threads
570      * inherits this mask.
571      *
572      * When a thread is resumed for the first time, all signals
573      * will be unblocked.
574      */
575     ( void ) pthread_sigmask( SIG_SETMASK,
576                               &xAllSignals,
577                               &xSchedulerOriginalSignalMask );
578 
579     sigtick.sa_flags = 0;
580     sigtick.sa_handler = vPortSystemTickHandler;
581     sigfillset( &sigtick.sa_mask );
582 
583     iRet = sigaction( SIGALRM, &sigtick, NULL );
584 
585     if( iRet == -1 )
586     {
587         prvFatalError( "sigaction", errno );
588     }
589 }
590 /*-----------------------------------------------------------*/
591 
ulPortGetRunTime(void)592 uint32_t ulPortGetRunTime( void )
593 {
594     struct tms xTimes;
595 
596     times( &xTimes );
597 
598     return ( uint32_t ) xTimes.tms_utime;
599 }
600 /*-----------------------------------------------------------*/
601