1 /*
2 * FreeRTOS Kernel V11.1.0
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 #ifdef __linux__
55 #define __USE_GNU
56 #endif
57
58 #include <errno.h>
59 #include <pthread.h>
60 #include <limits.h>
61 #include <signal.h>
62 #include <stdio.h>
63 #include <stdlib.h>
64 #include <string.h>
65 #include <sys/time.h>
66 #include <sys/times.h>
67 #include <time.h>
68 #include <unistd.h>
69
70 #ifdef __APPLE__
71 #include <mach/mach_vm.h>
72 #endif
73
74 /* Scheduler includes. */
75 #include "FreeRTOS.h"
76 #include "task.h"
77 #include "timers.h"
78 #include "utils/wait_for_event.h"
79 /*-----------------------------------------------------------*/
80
81 #define SIG_RESUME SIGUSR1
82
83 typedef struct THREAD
84 {
85 pthread_t pthread;
86 TaskFunction_t pxCode;
87 void * pvParams;
88 BaseType_t xDying;
89 struct event * ev;
90 } Thread_t;
91
92 /*
93 * The additional per-thread data is stored at the beginning of the
94 * task's stack.
95 */
prvGetThreadFromTask(TaskHandle_t xTask)96 static inline Thread_t * prvGetThreadFromTask( TaskHandle_t xTask )
97 {
98 StackType_t * pxTopOfStack = *( StackType_t ** ) xTask;
99
100 return ( Thread_t * ) ( pxTopOfStack + 1 );
101 }
102
103 /*-----------------------------------------------------------*/
104
105 static pthread_once_t hSigSetupThread = PTHREAD_ONCE_INIT;
106 static sigset_t xAllSignals;
107 static sigset_t xSchedulerOriginalSignalMask;
108 static pthread_t hMainThread = ( pthread_t ) NULL;
109 static volatile BaseType_t uxCriticalNesting;
110 static BaseType_t xSchedulerEnd = pdFALSE;
111 static pthread_t hTimerTickThread;
112 static bool xTimerTickThreadShouldRun;
113 static uint64_t prvStartTimeNs;
114 /*-----------------------------------------------------------*/
115
116 static void prvSetupSignalsAndSchedulerPolicy( void );
117 static void prvSetupTimerInterrupt( void );
118 static void * prvWaitForStart( void * pvParams );
119 static void prvSwitchThread( Thread_t * xThreadToResume,
120 Thread_t * xThreadToSuspend );
121 static void prvSuspendSelf( Thread_t * thread );
122 static void prvResumeThread( Thread_t * xThreadId );
123 static void vPortSystemTickHandler( int sig );
124 static void vPortStartFirstTask( void );
125 static void prvPortYieldFromISR( void );
126 /*-----------------------------------------------------------*/
127
128 static void prvFatalError( const char * pcCall,
129 int iErrno ) __attribute__( ( __noreturn__ ) );
130
prvFatalError(const char * pcCall,int iErrno)131 void prvFatalError( const char * pcCall,
132 int iErrno )
133 {
134 fprintf( stderr, "%s: %s\n", pcCall, strerror( iErrno ) );
135 abort();
136 }
137 /*-----------------------------------------------------------*/
138
prvPortSetCurrentThreadName(char * pxThreadName)139 static void prvPortSetCurrentThreadName(char * pxThreadName)
140 {
141 #ifdef __APPLE__
142 pthread_setname_np(pxThreadName);
143 #else
144 pthread_setname_np(pthread_self(), pxThreadName);
145 #endif
146 }
147 /*-----------------------------------------------------------*/
148
149 /*
150 * See header file for description.
151 */
pxPortInitialiseStack(StackType_t * pxTopOfStack,StackType_t * pxEndOfStack,TaskFunction_t pxCode,void * pvParameters)152 StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,
153 StackType_t * pxEndOfStack,
154 TaskFunction_t pxCode,
155 void * pvParameters )
156 {
157 Thread_t * thread;
158 pthread_attr_t xThreadAttributes;
159 size_t ulStackSize;
160 int iRet;
161
162 ( void ) pthread_once( &hSigSetupThread, prvSetupSignalsAndSchedulerPolicy );
163
164 /*
165 * Store the additional thread data at the start of the stack.
166 */
167 thread = ( Thread_t * ) ( pxTopOfStack + 1 ) - 1;
168 pxTopOfStack = ( StackType_t * ) thread - 1;
169
170 #ifdef __APPLE__
171 pxEndOfStack = ( StackType_t * ) mach_vm_round_page( pxEndOfStack );
172 #endif
173
174 ulStackSize = ( size_t ) ( pxTopOfStack + 1 - pxEndOfStack ) * sizeof( *pxTopOfStack );
175
176 #ifdef __APPLE__
177 ulStackSize = mach_vm_trunc_page( ulStackSize );
178 #endif
179
180 thread->pxCode = pxCode;
181 thread->pvParams = pvParameters;
182 thread->xDying = pdFALSE;
183
184 /* Ensure ulStackSize is at least PTHREAD_STACK_MIN */
185 ulStackSize = (ulStackSize < PTHREAD_STACK_MIN) ? PTHREAD_STACK_MIN : ulStackSize;
186
187 pthread_attr_init( &xThreadAttributes );
188 iRet = pthread_attr_setstacksize( &xThreadAttributes, ulStackSize );
189
190 if( iRet != 0 )
191 {
192 fprintf( stderr, "[WARN] pthread_attr_setstacksize failed with return value: %d. Default stack size will be used.\n", iRet );
193 }
194
195 thread->ev = event_create();
196
197 vPortEnterCritical();
198
199 iRet = pthread_create( &thread->pthread, &xThreadAttributes,
200 prvWaitForStart, thread );
201
202 if( iRet != 0 )
203 {
204 prvFatalError( "pthread_create", iRet );
205 }
206
207 vPortExitCritical();
208
209 return pxTopOfStack;
210 }
211 /*-----------------------------------------------------------*/
212
vPortStartFirstTask(void)213 void vPortStartFirstTask( void )
214 {
215 Thread_t * pxFirstThread = prvGetThreadFromTask( xTaskGetCurrentTaskHandle() );
216
217 /* Start the first task. */
218 prvResumeThread( pxFirstThread );
219 }
220 /*-----------------------------------------------------------*/
221
222 /*
223 * See header file for description.
224 */
xPortStartScheduler(void)225 BaseType_t xPortStartScheduler( void )
226 {
227 int iSignal;
228 sigset_t xSignals;
229
230 hMainThread = pthread_self();
231 prvPortSetCurrentThreadName("Scheduler");
232
233 /* Start the timer that generates the tick ISR(SIGALRM).
234 * Interrupts are disabled here already. */
235 prvSetupTimerInterrupt();
236
237 /*
238 * Block SIG_RESUME before starting any tasks so the main thread can sigwait on it.
239 * To sigwait on an unblocked signal is undefined.
240 * https://pubs.opengroup.org/onlinepubs/009604499/functions/sigwait.html
241 */
242 sigemptyset( &xSignals );
243 sigaddset( &xSignals, SIG_RESUME );
244 ( void ) pthread_sigmask( SIG_BLOCK, &xSignals, NULL );
245
246 /* Start the first task. */
247 vPortStartFirstTask();
248
249 /* Wait until signaled by vPortEndScheduler(). */
250 while( xSchedulerEnd != pdTRUE )
251 {
252 sigwait( &xSignals, &iSignal );
253 }
254
255 /*
256 * clear out the variable that is used to end the scheduler, otherwise
257 * subsequent scheduler restarts will end immediately.
258 */
259 xSchedulerEnd = pdFALSE;
260
261 /* Reset pthread_once_t, needed to restart the scheduler again.
262 * memset the internal struct members for MacOS/Linux Compatability */
263 #if __APPLE__
264 hSigSetupThread.__sig = _PTHREAD_ONCE_SIG_init;
265 memset( ( void * ) &hSigSetupThread.__opaque, 0, sizeof(hSigSetupThread.__opaque));
266 #else /* Linux PTHREAD library*/
267 hSigSetupThread = PTHREAD_ONCE_INIT;
268 #endif /* __APPLE__*/
269
270 /* Restore original signal mask. */
271 ( void ) pthread_sigmask( SIG_SETMASK, &xSchedulerOriginalSignalMask, NULL );
272
273 return 0;
274 }
275 /*-----------------------------------------------------------*/
276
vPortEndScheduler(void)277 void vPortEndScheduler( void )
278 {
279 Thread_t * pxCurrentThread;
280
281 /* Stop the timer tick thread. */
282 xTimerTickThreadShouldRun = false;
283 pthread_join( hTimerTickThread, NULL );
284
285 /* Signal the scheduler to exit its loop. */
286 xSchedulerEnd = pdTRUE;
287 ( void ) pthread_kill( hMainThread, SIG_RESUME );
288
289 /* Waiting to be deleted here. */
290 pxCurrentThread = prvGetThreadFromTask( xTaskGetCurrentTaskHandle() );
291 event_wait( pxCurrentThread->ev );
292 pthread_testcancel();
293 }
294 /*-----------------------------------------------------------*/
295
vPortEnterCritical(void)296 void vPortEnterCritical( void )
297 {
298 if( uxCriticalNesting == 0 )
299 {
300 vPortDisableInterrupts();
301 }
302
303 uxCriticalNesting++;
304 }
305 /*-----------------------------------------------------------*/
306
vPortExitCritical(void)307 void vPortExitCritical( void )
308 {
309 uxCriticalNesting--;
310
311 /* If we have reached 0 then re-enable the interrupts. */
312 if( uxCriticalNesting == 0 )
313 {
314 vPortEnableInterrupts();
315 }
316 }
317 /*-----------------------------------------------------------*/
318
prvPortYieldFromISR(void)319 static void prvPortYieldFromISR( void )
320 {
321 Thread_t * xThreadToSuspend;
322 Thread_t * xThreadToResume;
323
324 xThreadToSuspend = prvGetThreadFromTask( xTaskGetCurrentTaskHandle() );
325
326 vTaskSwitchContext();
327
328 xThreadToResume = prvGetThreadFromTask( xTaskGetCurrentTaskHandle() );
329
330 prvSwitchThread( xThreadToResume, xThreadToSuspend );
331 }
332 /*-----------------------------------------------------------*/
333
vPortYield(void)334 void vPortYield( void )
335 {
336 vPortEnterCritical();
337
338 prvPortYieldFromISR();
339
340 vPortExitCritical();
341 }
342 /*-----------------------------------------------------------*/
343
vPortDisableInterrupts(void)344 void vPortDisableInterrupts( void )
345 {
346 pthread_sigmask( SIG_BLOCK, &xAllSignals, NULL );
347 }
348 /*-----------------------------------------------------------*/
349
vPortEnableInterrupts(void)350 void vPortEnableInterrupts( void )
351 {
352 pthread_sigmask( SIG_UNBLOCK, &xAllSignals, NULL );
353 }
354 /*-----------------------------------------------------------*/
355
xPortSetInterruptMask(void)356 UBaseType_t xPortSetInterruptMask( void )
357 {
358 /* Interrupts are always disabled inside ISRs (signals
359 * handlers). */
360 return ( UBaseType_t ) 0;
361 }
362 /*-----------------------------------------------------------*/
363
vPortClearInterruptMask(UBaseType_t uxMask)364 void vPortClearInterruptMask( UBaseType_t uxMask )
365 {
366 ( void ) uxMask;
367 }
368 /*-----------------------------------------------------------*/
369
prvGetTimeNs(void)370 static uint64_t prvGetTimeNs( void )
371 {
372 struct timespec t;
373
374 clock_gettime( CLOCK_MONOTONIC, &t );
375
376 return ( uint64_t ) t.tv_sec * ( uint64_t ) 1000000000UL + ( uint64_t ) t.tv_nsec;
377 }
378 /*-----------------------------------------------------------*/
379
380 /* commented as part of the code below in vPortSystemTickHandler,
381 * to adjust timing according to full demo requirements */
382 /* static uint64_t prvTickCount; */
383
prvTimerTickHandler(void * arg)384 static void * prvTimerTickHandler( void * arg )
385 {
386 ( void ) arg;
387
388 prvPortSetCurrentThreadName("Scheduler timer");
389
390 while( xTimerTickThreadShouldRun )
391 {
392 /*
393 * signal to the active task to cause tick handling or
394 * preemption (if enabled)
395 */
396 Thread_t * thread = prvGetThreadFromTask( xTaskGetCurrentTaskHandle() );
397 pthread_kill( thread->pthread, SIGALRM );
398 usleep( portTICK_RATE_MICROSECONDS );
399 }
400
401 return NULL;
402 }
403 /*-----------------------------------------------------------*/
404
405 /*
406 * Setup the systick timer to generate the tick interrupts at the required
407 * frequency.
408 */
prvSetupTimerInterrupt(void)409 void prvSetupTimerInterrupt( void )
410 {
411 xTimerTickThreadShouldRun = true;
412 pthread_create( &hTimerTickThread, NULL, prvTimerTickHandler, NULL );
413
414 prvStartTimeNs = prvGetTimeNs();
415 }
416 /*-----------------------------------------------------------*/
417
vPortSystemTickHandler(int sig)418 static void vPortSystemTickHandler( int sig )
419 {
420 Thread_t * pxThreadToSuspend;
421 Thread_t * pxThreadToResume;
422
423 ( void ) sig;
424
425 /* uint64_t xExpectedTicks; */
426
427 uxCriticalNesting++; /* Signals are blocked in this signal handler. */
428
429 #if ( configUSE_PREEMPTION == 1 )
430 pxThreadToSuspend = prvGetThreadFromTask( xTaskGetCurrentTaskHandle() );
431 #endif
432
433 /* Tick Increment, accounting for any lost signals or drift in
434 * the timer. */
435
436 /*
437 * Comment code to adjust timing according to full demo requirements
438 * xExpectedTicks = (prvGetTimeNs() - prvStartTimeNs)
439 * / (portTICK_RATE_MICROSECONDS * 1000);
440 * do { */
441 xTaskIncrementTick();
442
443 /* prvTickCount++;
444 * } while (prvTickCount < xExpectedTicks);
445 */
446
447 #if ( configUSE_PREEMPTION == 1 )
448 /* Select Next Task. */
449 vTaskSwitchContext();
450
451 pxThreadToResume = prvGetThreadFromTask( xTaskGetCurrentTaskHandle() );
452
453 prvSwitchThread( pxThreadToResume, pxThreadToSuspend );
454 #endif
455
456 uxCriticalNesting--;
457 }
458 /*-----------------------------------------------------------*/
459
vPortThreadDying(void * pxTaskToDelete,volatile BaseType_t * pxPendYield)460 void vPortThreadDying( void * pxTaskToDelete,
461 volatile BaseType_t * pxPendYield )
462 {
463 Thread_t * pxThread = prvGetThreadFromTask( pxTaskToDelete );
464
465 ( void ) pxPendYield;
466
467 pxThread->xDying = pdTRUE;
468 }
469 /*-----------------------------------------------------------*/
470
vPortCancelThread(void * pxTaskToDelete)471 void vPortCancelThread( void * pxTaskToDelete )
472 {
473 Thread_t * pxThreadToCancel = prvGetThreadFromTask( pxTaskToDelete );
474
475 /*
476 * The thread has already been suspended so it can be safely cancelled.
477 */
478 pthread_cancel( pxThreadToCancel->pthread );
479 event_signal( pxThreadToCancel->ev );
480 pthread_join( pxThreadToCancel->pthread, NULL );
481 event_delete( pxThreadToCancel->ev );
482 }
483 /*-----------------------------------------------------------*/
484
prvWaitForStart(void * pvParams)485 static void * prvWaitForStart( void * pvParams )
486 {
487 Thread_t * pxThread = pvParams;
488
489 prvSuspendSelf( pxThread );
490
491 /* Resumed for the first time, unblocks all signals. */
492 uxCriticalNesting = 0;
493 vPortEnableInterrupts();
494
495 /* Set thread name */
496 prvPortSetCurrentThreadName(pcTaskGetName(xTaskGetCurrentTaskHandle()));
497
498 /* Call the task's entry point. */
499 pxThread->pxCode( pxThread->pvParams );
500
501 /* A function that implements a task must not exit or attempt to return to
502 * its caller as there is nothing to return to. If a task wants to exit it
503 * should instead call vTaskDelete( NULL ). Artificially force an assert()
504 * to be triggered if configASSERT() is defined, so application writers can
505 * catch the error. */
506 configASSERT( pdFALSE );
507
508 return NULL;
509 }
510 /*-----------------------------------------------------------*/
511
prvSwitchThread(Thread_t * pxThreadToResume,Thread_t * pxThreadToSuspend)512 static void prvSwitchThread( Thread_t * pxThreadToResume,
513 Thread_t * pxThreadToSuspend )
514 {
515 BaseType_t uxSavedCriticalNesting;
516
517 if( pxThreadToSuspend != pxThreadToResume )
518 {
519 /*
520 * Switch tasks.
521 *
522 * The critical section nesting is per-task, so save it on the
523 * stack of the current (suspending thread), restoring it when
524 * we switch back to this task.
525 */
526 uxSavedCriticalNesting = uxCriticalNesting;
527
528 prvResumeThread( pxThreadToResume );
529
530 if( pxThreadToSuspend->xDying == pdTRUE )
531 {
532 pthread_exit( NULL );
533 }
534
535 prvSuspendSelf( pxThreadToSuspend );
536
537 uxCriticalNesting = uxSavedCriticalNesting;
538 }
539 }
540 /*-----------------------------------------------------------*/
541
prvSuspendSelf(Thread_t * thread)542 static void prvSuspendSelf( Thread_t * thread )
543 {
544 /*
545 * Suspend this thread by waiting for a pthread_cond_signal event.
546 *
547 * A suspended thread must not handle signals (interrupts) so
548 * all signals must be blocked by calling this from:
549 *
550 * - Inside a critical section (vPortEnterCritical() /
551 * vPortExitCritical()).
552 *
553 * - From a signal handler that has all signals masked.
554 *
555 * - A thread with all signals blocked with pthread_sigmask().
556 */
557 event_wait( thread->ev );
558 pthread_testcancel();
559 }
560
561 /*-----------------------------------------------------------*/
562
prvResumeThread(Thread_t * xThreadId)563 static void prvResumeThread( Thread_t * xThreadId )
564 {
565 if( pthread_self() != xThreadId->pthread )
566 {
567 event_signal( xThreadId->ev );
568 }
569 }
570 /*-----------------------------------------------------------*/
571
prvSetupSignalsAndSchedulerPolicy(void)572 static void prvSetupSignalsAndSchedulerPolicy( void )
573 {
574 struct sigaction sigtick;
575 int iRet;
576
577 hMainThread = pthread_self();
578
579 /* Initialise common signal masks. */
580 sigfillset( &xAllSignals );
581
582 /* Don't block SIGINT so this can be used to break into GDB while
583 * in a critical section. */
584 sigdelset( &xAllSignals, SIGINT );
585
586 /*
587 * Block all signals in this thread so all new threads
588 * inherits this mask.
589 *
590 * When a thread is resumed for the first time, all signals
591 * will be unblocked.
592 */
593 ( void ) pthread_sigmask( SIG_SETMASK,
594 &xAllSignals,
595 &xSchedulerOriginalSignalMask );
596
597 sigtick.sa_flags = 0;
598 sigtick.sa_handler = vPortSystemTickHandler;
599 sigfillset( &sigtick.sa_mask );
600
601 iRet = sigaction( SIGALRM, &sigtick, NULL );
602
603 if( iRet == -1 )
604 {
605 prvFatalError( "sigaction", errno );
606 }
607 }
608 /*-----------------------------------------------------------*/
609
ulPortGetRunTime(void)610 uint32_t ulPortGetRunTime( void )
611 {
612 struct tms xTimes;
613
614 times( &xTimes );
615
616 return ( uint32_t ) xTimes.tms_utime;
617 }
618 /*-----------------------------------------------------------*/
619