1 /*
2 * FreeRTOS Kernel V10.6.2
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 portBASE_TYPE uxCriticalNesting;
104 /*-----------------------------------------------------------*/
105
106 static portBASE_TYPE 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 portSTACK_TYPE * 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 = ( portSTACK_TYPE * ) thread - 1;
151 ulStackSize = ( size_t )( pxTopOfStack + 1 - pxEndOfStack ) * sizeof( *pxTopOfStack );
152
153 #ifdef __APPLE__
154 pxEndOfStack = mach_vm_round_page ( pxEndOfStack );
155 ulStackSize = mach_vm_trunc_page ( ulStackSize );
156 #endif
157
158 thread->pxCode = pxCode;
159 thread->pvParams = pvParameters;
160 thread->xDying = pdFALSE;
161
162 pthread_attr_init( &xThreadAttributes );
163 iRet = pthread_attr_setstack( &xThreadAttributes, pxEndOfStack, ulStackSize );
164 if( iRet != 0 )
165 {
166 fprintf( stderr, "[WARN] pthread_attr_setstack failed with return value: %d. Default stack will be used.\n", iRet );
167 fprintf( stderr, "[WARN] Increase the stack size to PTHREAD_STACK_MIN.\n" );
168 }
169
170 thread->ev = event_create();
171
172 vPortEnterCritical();
173
174 iRet = pthread_create( &thread->pthread, &xThreadAttributes,
175 prvWaitForStart, thread );
176
177 if( iRet != 0 )
178 {
179 prvFatalError( "pthread_create", iRet );
180 }
181
182 vPortExitCritical();
183
184 return pxTopOfStack;
185 }
186 /*-----------------------------------------------------------*/
187
vPortStartFirstTask(void)188 void vPortStartFirstTask( void )
189 {
190 Thread_t * pxFirstThread = prvGetThreadFromTask( xTaskGetCurrentTaskHandle() );
191
192 /* Start the first task. */
193 prvResumeThread( pxFirstThread );
194 }
195 /*-----------------------------------------------------------*/
196
197 /*
198 * See header file for description.
199 */
xPortStartScheduler(void)200 portBASE_TYPE xPortStartScheduler( void )
201 {
202 int iSignal;
203 sigset_t xSignals;
204
205 hMainThread = pthread_self();
206
207 /* Start the timer that generates the tick ISR(SIGALRM).
208 * Interrupts are disabled here already. */
209 prvSetupTimerInterrupt();
210
211 /*
212 * Block SIG_RESUME before starting any tasks so the main thread can sigwait on it.
213 * To sigwait on an unblocked signal is undefined.
214 * https://pubs.opengroup.org/onlinepubs/009604499/functions/sigwait.html
215 */
216 sigemptyset( &xSignals );
217 sigaddset( &xSignals, SIG_RESUME );
218 ( void ) pthread_sigmask( SIG_BLOCK, &xSignals, NULL );
219
220 /* Start the first task. */
221 vPortStartFirstTask();
222
223 /* Wait until signaled by vPortEndScheduler(). */
224 while( xSchedulerEnd != pdTRUE )
225 {
226 sigwait( &xSignals, &iSignal );
227 }
228
229 /* Cancel the Idle task and free its resources */
230 #if ( INCLUDE_xTaskGetIdleTaskHandle == 1 )
231 vPortCancelThread( xTaskGetIdleTaskHandle() );
232 #endif
233
234 #if ( configUSE_TIMERS == 1 )
235 /* Cancel the Timer task and free its resources */
236 vPortCancelThread( xTimerGetTimerDaemonTaskHandle() );
237 #endif /* configUSE_TIMERS */
238
239 /* Restore original signal mask. */
240 ( void ) pthread_sigmask( SIG_SETMASK, &xSchedulerOriginalSignalMask, NULL );
241
242 return 0;
243 }
244 /*-----------------------------------------------------------*/
245
vPortEndScheduler(void)246 void vPortEndScheduler( void )
247 {
248 struct itimerval itimer;
249 struct sigaction sigtick;
250 Thread_t * xCurrentThread;
251
252 /* Stop the timer and ignore any pending SIGALRMs that would end
253 * up running on the main thread when it is resumed. */
254 itimer.it_value.tv_sec = 0;
255 itimer.it_value.tv_usec = 0;
256
257 itimer.it_interval.tv_sec = 0;
258 itimer.it_interval.tv_usec = 0;
259 ( void ) setitimer( ITIMER_REAL, &itimer, NULL );
260
261 sigtick.sa_flags = 0;
262 sigtick.sa_handler = SIG_IGN;
263 sigemptyset( &sigtick.sa_mask );
264 sigaction( SIGALRM, &sigtick, NULL );
265
266 /* Signal the scheduler to exit its loop. */
267 xSchedulerEnd = pdTRUE;
268 ( void ) pthread_kill( hMainThread, SIG_RESUME );
269
270 xCurrentThread = prvGetThreadFromTask( xTaskGetCurrentTaskHandle() );
271 prvSuspendSelf( xCurrentThread );
272 }
273 /*-----------------------------------------------------------*/
274
vPortEnterCritical(void)275 void vPortEnterCritical( void )
276 {
277 if( uxCriticalNesting == 0 )
278 {
279 vPortDisableInterrupts();
280 }
281
282 uxCriticalNesting++;
283 }
284 /*-----------------------------------------------------------*/
285
vPortExitCritical(void)286 void vPortExitCritical( void )
287 {
288 uxCriticalNesting--;
289
290 /* If we have reached 0 then re-enable the interrupts. */
291 if( uxCriticalNesting == 0 )
292 {
293 vPortEnableInterrupts();
294 }
295 }
296 /*-----------------------------------------------------------*/
297
prvPortYieldFromISR(void)298 static void prvPortYieldFromISR( void )
299 {
300 Thread_t * xThreadToSuspend;
301 Thread_t * xThreadToResume;
302
303 xThreadToSuspend = prvGetThreadFromTask( xTaskGetCurrentTaskHandle() );
304
305 vTaskSwitchContext();
306
307 xThreadToResume = prvGetThreadFromTask( xTaskGetCurrentTaskHandle() );
308
309 prvSwitchThread( xThreadToResume, xThreadToSuspend );
310 }
311 /*-----------------------------------------------------------*/
312
vPortYield(void)313 void vPortYield( void )
314 {
315 vPortEnterCritical();
316
317 prvPortYieldFromISR();
318
319 vPortExitCritical();
320 }
321 /*-----------------------------------------------------------*/
322
vPortDisableInterrupts(void)323 void vPortDisableInterrupts( void )
324 {
325 pthread_sigmask( SIG_BLOCK, &xAllSignals, NULL );
326 }
327 /*-----------------------------------------------------------*/
328
vPortEnableInterrupts(void)329 void vPortEnableInterrupts( void )
330 {
331 pthread_sigmask( SIG_UNBLOCK, &xAllSignals, NULL );
332 }
333 /*-----------------------------------------------------------*/
334
xPortSetInterruptMask(void)335 UBaseType_t xPortSetInterruptMask( void )
336 {
337 /* Interrupts are always disabled inside ISRs (signals
338 * handlers). */
339 return ( UBaseType_t )0;
340 }
341 /*-----------------------------------------------------------*/
342
vPortClearInterruptMask(UBaseType_t uxMask)343 void vPortClearInterruptMask( UBaseType_t uxMask )
344 {
345 ( void ) uxMask;
346 }
347 /*-----------------------------------------------------------*/
348
prvGetTimeNs(void)349 static uint64_t prvGetTimeNs( void )
350 {
351 struct timespec t;
352
353 clock_gettime( CLOCK_MONOTONIC, &t );
354
355 return ( uint64_t )t.tv_sec * ( uint64_t )1000000000UL + ( uint64_t )t.tv_nsec;
356 }
357
358 static uint64_t prvStartTimeNs;
359
360 /* commented as part of the code below in vPortSystemTickHandler,
361 * to adjust timing according to full demo requirements */
362 /* static uint64_t prvTickCount; */
363
364 /*
365 * Setup the systick timer to generate the tick interrupts at the required
366 * frequency.
367 */
prvSetupTimerInterrupt(void)368 void prvSetupTimerInterrupt( void )
369 {
370 struct itimerval itimer;
371 int iRet;
372
373 /* Initialise the structure with the current timer information. */
374 iRet = getitimer( ITIMER_REAL, &itimer );
375
376 if( iRet == -1 )
377 {
378 prvFatalError( "getitimer", errno );
379 }
380
381 /* Set the interval between timer events. */
382 itimer.it_interval.tv_sec = 0;
383 itimer.it_interval.tv_usec = portTICK_RATE_MICROSECONDS;
384
385 /* Set the current count-down. */
386 itimer.it_value.tv_sec = 0;
387 itimer.it_value.tv_usec = portTICK_RATE_MICROSECONDS;
388
389 /* Set-up the timer interrupt. */
390 iRet = setitimer( ITIMER_REAL, &itimer, NULL );
391
392 if( iRet == -1 )
393 {
394 prvFatalError( "setitimer", errno );
395 }
396
397 prvStartTimeNs = prvGetTimeNs();
398 }
399 /*-----------------------------------------------------------*/
400
vPortSystemTickHandler(int sig)401 static void vPortSystemTickHandler( int sig )
402 {
403 Thread_t * pxThreadToSuspend;
404 Thread_t * pxThreadToResume;
405
406 ( void ) sig;
407
408 /* uint64_t xExpectedTicks; */
409
410 uxCriticalNesting++; /* Signals are blocked in this signal handler. */
411
412 #if ( configUSE_PREEMPTION == 1 )
413 pxThreadToSuspend = prvGetThreadFromTask( xTaskGetCurrentTaskHandle() );
414 #endif
415
416 /* Tick Increment, accounting for any lost signals or drift in
417 * the timer. */
418
419 /*
420 * Comment code to adjust timing according to full demo requirements
421 * xExpectedTicks = (prvGetTimeNs() - prvStartTimeNs)
422 * / (portTICK_RATE_MICROSECONDS * 1000);
423 * do { */
424 xTaskIncrementTick();
425
426 /* prvTickCount++;
427 * } while (prvTickCount < xExpectedTicks);
428 */
429
430 #if ( configUSE_PREEMPTION == 1 )
431 /* Select Next Task. */
432 vTaskSwitchContext();
433
434 pxThreadToResume = prvGetThreadFromTask( xTaskGetCurrentTaskHandle() );
435
436 prvSwitchThread( pxThreadToResume, pxThreadToSuspend );
437 #endif
438
439 uxCriticalNesting--;
440 }
441 /*-----------------------------------------------------------*/
442
vPortThreadDying(void * pxTaskToDelete,volatile BaseType_t * pxPendYield)443 void vPortThreadDying( void * pxTaskToDelete,
444 volatile BaseType_t * pxPendYield )
445 {
446 Thread_t * pxThread = prvGetThreadFromTask( pxTaskToDelete );
447
448 ( void ) pxPendYield;
449
450 pxThread->xDying = pdTRUE;
451 }
452
vPortCancelThread(void * pxTaskToDelete)453 void vPortCancelThread( void * pxTaskToDelete )
454 {
455 Thread_t * pxThreadToCancel = prvGetThreadFromTask( pxTaskToDelete );
456
457 /*
458 * The thread has already been suspended so it can be safely cancelled.
459 */
460 pthread_cancel( pxThreadToCancel->pthread );
461 pthread_join( pxThreadToCancel->pthread, NULL );
462 event_delete( pxThreadToCancel->ev );
463 }
464 /*-----------------------------------------------------------*/
465
prvWaitForStart(void * pvParams)466 static void * prvWaitForStart( void * pvParams )
467 {
468 Thread_t * pxThread = pvParams;
469
470 prvSuspendSelf( pxThread );
471
472 /* Resumed for the first time, unblocks all signals. */
473 uxCriticalNesting = 0;
474 vPortEnableInterrupts();
475
476 /* Call the task's entry point. */
477 pxThread->pxCode( pxThread->pvParams );
478
479 /* A function that implements a task must not exit or attempt to return to
480 * its caller as there is nothing to return to. If a task wants to exit it
481 * should instead call vTaskDelete( NULL ). Artificially force an assert()
482 * to be triggered if configASSERT() is defined, so application writers can
483 * catch the error. */
484 configASSERT( pdFALSE );
485
486 return NULL;
487 }
488 /*-----------------------------------------------------------*/
489
prvSwitchThread(Thread_t * pxThreadToResume,Thread_t * pxThreadToSuspend)490 static void prvSwitchThread( Thread_t * pxThreadToResume,
491 Thread_t * pxThreadToSuspend )
492 {
493 BaseType_t uxSavedCriticalNesting;
494
495 if( pxThreadToSuspend != pxThreadToResume )
496 {
497 /*
498 * Switch tasks.
499 *
500 * The critical section nesting is per-task, so save it on the
501 * stack of the current (suspending thread), restoring it when
502 * we switch back to this task.
503 */
504 uxSavedCriticalNesting = uxCriticalNesting;
505
506 prvResumeThread( pxThreadToResume );
507
508 if( pxThreadToSuspend->xDying == pdTRUE )
509 {
510 pthread_exit( NULL );
511 }
512
513 prvSuspendSelf( pxThreadToSuspend );
514
515 uxCriticalNesting = uxSavedCriticalNesting;
516 }
517 }
518 /*-----------------------------------------------------------*/
519
prvSuspendSelf(Thread_t * thread)520 static void prvSuspendSelf( Thread_t * thread )
521 {
522 /*
523 * Suspend this thread by waiting for a pthread_cond_signal event.
524 *
525 * A suspended thread must not handle signals (interrupts) so
526 * all signals must be blocked by calling this from:
527 *
528 * - Inside a critical section (vPortEnterCritical() /
529 * vPortExitCritical()).
530 *
531 * - From a signal handler that has all signals masked.
532 *
533 * - A thread with all signals blocked with pthread_sigmask().
534 */
535 event_wait( thread->ev );
536 }
537
538 /*-----------------------------------------------------------*/
539
prvResumeThread(Thread_t * xThreadId)540 static void prvResumeThread( Thread_t * xThreadId )
541 {
542 if( pthread_self() != xThreadId->pthread )
543 {
544 event_signal( xThreadId->ev );
545 }
546 }
547 /*-----------------------------------------------------------*/
548
prvSetupSignalsAndSchedulerPolicy(void)549 static void prvSetupSignalsAndSchedulerPolicy( void )
550 {
551 struct sigaction sigtick;
552 int iRet;
553
554 hMainThread = pthread_self();
555
556 /* Initialise common signal masks. */
557 sigfillset( &xAllSignals );
558
559 /* Don't block SIGINT so this can be used to break into GDB while
560 * in a critical section. */
561 sigdelset( &xAllSignals, SIGINT );
562
563 /*
564 * Block all signals in this thread so all new threads
565 * inherits this mask.
566 *
567 * When a thread is resumed for the first time, all signals
568 * will be unblocked.
569 */
570 ( void ) pthread_sigmask( SIG_SETMASK,
571 &xAllSignals,
572 &xSchedulerOriginalSignalMask );
573
574 sigtick.sa_flags = 0;
575 sigtick.sa_handler = vPortSystemTickHandler;
576 sigfillset( &sigtick.sa_mask );
577
578 iRet = sigaction( SIGALRM, &sigtick, NULL );
579
580 if( iRet == -1 )
581 {
582 prvFatalError( "sigaction", errno );
583 }
584 }
585 /*-----------------------------------------------------------*/
586
ulPortGetRunTime(void)587 unsigned long ulPortGetRunTime( void )
588 {
589 struct tms xTimes;
590
591 times( &xTimes );
592
593 return ( unsigned long ) xTimes.tms_utime;
594 }
595 /*-----------------------------------------------------------*/
596