Commit graph

3451 commits

Author SHA1 Message Date
Chris Morgan
ddc89fa985 POSIX - Switch from posix timers to a timer thread to fix signal handling with non-FreeRTOS pthreads
Improve upon the elegant approach of using signals to cause task/pthreads
suspension and scheduler execution by using directed signals.

This fixes:
- Deadlocks in non-FreeRTOS pthreads
- Multiple FreeRTOS tasks(pthreads) incorrectly running at the same time

By directing the signals using pthread_kill() the signal handler in the presently running
FreeRTOS task/pthread will be called, ensuring that the scheduler runs both in the context
of a FreeRTOS task/pthread and from the presently executing FreeRTOS task/pthread.

Details
==============

The POSIX port uses signals to preempt FreeRTOS tasks (implemented as pthreads), a very neat and elegant
approach to forcing tasks/pthreads to suspend and run the scheduler.

Signal handlers are process global.

Posix timers generate signals when the timer expires, and the signal is sent to the currently
running pthread.

In systems where there are pthreads that are NOT a result of creating FreeRTOS tasks, such as the
entry point thread that calls main(), or user created pthreads, this poses a serious issue.

While the POSIX port only allows a single FreeRTOS pthread to run at once, by causing all suspended
threads to not be scheduled due to their waiting on a pthread condition variable,
this isn't the case with non-FreeRTOS pthreads.

Thus it is possible that a non-FreeRTOS pthread is running when the timer expires and the signal
is generated. This results in the signal handler running in the non-FreeRTOS thread.

The sequence of events results in these events from signal handler context:
- vPortSystemTickHandler() being called
- The scheduler running
- Selecting another FreeRTOS task to run and switching the active task
- The newly selected task released from suspension by pthread_cond_signal()
- The presently active thread calling event_wait()
- The pthread calling pthread_cond_wait(), suspending the thread and allowing the host OS scheduler
  to schedule another thread to run.

If this occurs from a non-FreeRTOS thread this results in:
- The active FreeRTOS pthread (Task A/Thread A) continuing to run (as the signal handler that calls
  event_wait() ran instead in a non-FreeRTOS pthread.
- The pthread where the signal handler did run (Thread B) will call event_wait() and pthread_cond_wait(),
  but on the condition variable of the previously active FreeRTOS task, oops. This causes the
  non-FreeRTOS pthread to block unexpectedly relative to what the developer might have expected.
- The newly selected FreeRTOS Task (Task C/Thread C) will resume and start running.

At this point Task A/Thread A is running concurrently with Task C/Thread C. While this may not
necessarily be an issue, it does not replicate the expected behavior of a single Task running at
once.

Note that Thread B will resume if/when Task A/ThreadA is switched to. However, this could be delayed
by an arbitrary amount of time, or could never occur.

Also note that if there are multiple non-FreeRTOS pthreads that Thread D, E, F...etc could suffer the
same fate as Thread B, if the scheduler were to suspend Task C/Thread C and resume Task E/Thread E.

Implementation
==============

Timer details
-------------
A standalone pthread for the signal generation thread was chosen, rather than using
a posix timer_settime() handler function because the latter creates a temporary
pthread for each handler callback. This makes debugging much more difficult due to
gdb detecting the creation and destruction of these temporary threads.

Signal delivery
--------------
While signal handlers are per-thread, it is possible for pthreads to selectively block
signals, rather than using thread directed signals. However, the approach of blocking
signals in non-FreeRTOS pthreads adds complexity to each of these non-FreeRTOS pthreads
including ensuring that these signals are blocked at thread creation, prior to the thread
starting up. Directed signals removes the requirement for non-FreeRTOS pthreads to be aware
of and take action to protect against these signals, reducing complexity.
2024-01-11 10:53:54 -08:00
Soren Ptak
fdd8565f35 Add words back to the lexicon that were accidentally removed 2024-01-11 13:35:11 -05:00
Soren Ptak
bdeb9eb91d
Merge branch 'main' into ARM_CRx_MPU 2024-01-10 15:32:41 -05:00
Soren Ptak
529de5606e
Revert #768 on the XCC/Xtensa portable files (#948) 2024-01-10 20:28:45 +05:30
Soren Ptak
53c84a1f6f Update the port to use FreeRTOS_IRQ_Handler instead of the VIM functions 2024-01-10 09:19:56 -05:00
Soren Ptak
745384b04d Make vPortYieldWithinAPI and FreeRTOS_Tick_Handler into more configurable functions 2024-01-09 15:45:45 -05:00
Soren Ptak
c1124d5707 Merge branch 'ARM_CRx_MPU' of github.com:Skptak/FreeRTOS-Kernel into ARM_CRx_MPU 2024-01-09 11:48:26 -05:00
Soren Ptak
501daf43bc Update spelling wordlist 2024-01-09 11:48:20 -05:00
Soren Ptak
8f9deb4514 Add the system call table as an actual extern declaration to the ASM file 2024-01-09 11:34:48 -05:00
chinglee-iot
94cb87ad30
Assign idle task to each core before SMP scheduler start (#945)
Co-authored-by: Rahul Kar <118818625+kar-rahul-aws@users.noreply.github.com>
2024-01-09 14:03:47 +08:00
Soren Ptak
460e953076
Add new common words to the cSpellWordList.txt (#946) 2024-01-08 11:58:18 +05:30
Soren Ptak
8de6be2b56
Merge branch 'main' into ARM_CRx_MPU 2024-01-04 15:46:34 -05:00
Gabriele Monaco
1947dd2f94
Added ability to change task notification index for streambuffers (#939)
* Added possibility to change notification index for streambuffers

* Uncrustify: triggered by comment.

* Minor code review suggestions.

Signed-off-by: Gaurav Aggarwal <aggarg@amazon.com>

---------

Signed-off-by: Gaurav Aggarwal <aggarg@amazon.com>
Co-authored-by: GitHub Action <action@github.com>
Co-authored-by: Aniruddha Kanhere <60444055+AniruddhaKanhere@users.noreply.github.com>
Co-authored-by: Gaurav Aggarwal <aggarg@amazon.com>
2024-01-04 11:43:34 -08:00
Soren Ptak
aacae9f21c Fix formatting in portmacro_asm.h 2024-01-04 12:16:55 -05:00
Soren Ptak
63dfe612be Fix assert check in svc enter, remove un-used defines 2024-01-04 08:59:47 -05:00
Soren Ptak
0fc0a5fbba Formatting fix 2024-01-03 17:14:36 -05:00
Soren Ptak
8202d2ea82
Merge branch 'main' into ARM_CRx_MPU 2024-01-03 17:14:18 -05:00
Soren Ptak
22ff9003bb Merge branch 'ARM_CRx_MPU' of github.com:Skptak/FreeRTOS-Kernel into ARM_CRx_MPU 2024-01-03 17:05:06 -05:00
Soren Ptak
d23078088c Remove the use of ulContext and a controlled SVC stack pointer for the port 2024-01-03 17:04:10 -05:00
Eric Jackson
4568507507
Fix documentation for xQueueTakeMutexRecursive (#943) 2024-01-03 11:49:02 -08:00
Soren Ptak
96ab824bb9
Merge branch 'main' into ARM_CRx_MPU 2024-01-03 11:07:48 -05:00
Soren Ptak
ae127206fa Formatting changes for the portASM.S file 2024-01-03 11:06:40 -05:00
Soren Ptak
56b400f1a2 Change the parameters for prvMpuSetRegion() to follow the naming convention a bit more 2024-01-03 11:05:50 -05:00
Soren Ptak
a60fe75267 Fix cSpellWords issue 2024-01-03 10:16:51 -05:00
chinglee-iot
be880a1fc8
Fix portSET_INTERRUPT_MASK_FROM_ISR definition for atomic operation (#940)
* Introduce portHAS_NESTED_INTERRUPTS to identify if port has nested interrupt or not.
* Update atomic.h to use portHAS_NESTED_INTERRUPTS instead of portSET_INTERRUPT_MASK_FROM_ISR definition.

---------

Co-authored-by: Gaurav-Aggarwal-AWS <33462878+aggarg@users.noreply.github.com>
Co-authored-by: ActoryOu <jay2002824@gmail.com>
2024-01-03 15:47:05 +08:00
dps.lwk
75c4044b7e
RP2040: Fix removal of idle_task_static_memory.c (#935)
Co-authored-by: Rahul Kar <118818625+kar-rahul-aws@users.noreply.github.com>
2024-01-03 11:06:04 +05:30
Soren Ptak
fd19c0ee52 Throw an error if configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS is set to 1 2024-01-02 15:15:34 -05:00
Soren Ptak
b0ec6d0cc6 Ignore the clang format file for the header checks 2024-01-02 13:38:39 -05:00
Soren Ptak
e25b6e145d Formatting fix 2024-01-02 13:26:24 -05:00
Soren Ptak
d965464f4e Use my fork for the Demo builds 2024-01-02 13:09:29 -05:00
Soren Ptak
be4dc05b78 Slight format change 2024-01-02 13:06:59 -05:00
Soren Ptak
b2c406c3a3 Change to make the .clang-format file work with V14 of clang-format. Moving the comment for PRIVILEGED_FUNCTION to the front to help with formatting, also makes more sense when looking at the func imo 2024-01-02 12:58:46 -05:00
Soren Ptak
b701a46312
Merge branch 'main' into ARM_CRx_MPU 2024-01-02 12:52:35 -05:00
Soren Ptak
dc0e508008 Add the ARM_CRx_MPU Demos to the kernel build checks 2024-01-02 12:51:08 -05:00
Soren Ptak
bdd9f7129c Add the CI-CD check for clang formatting the ARM_CRx_MPU port 2024-01-02 11:49:19 -05:00
Soren Ptak
4c53a433df Add the .clang-format file that is being used to the port 2024-01-02 11:48:48 -05:00
Soren Ptak
7650690bef Clang format portmacro.h with slight tweaks to the format options 2024-01-02 11:48:30 -05:00
Soren Ptak
4f65931294 Clang format port.c with slight tweaks to the format options 2024-01-02 11:48:21 -05:00
Soren Ptak
89227494cf Clang format the mpu_wrappers_v2_asm.h file with slight tweaks to the format options 2024-01-02 11:48:07 -05:00
Jeff Tenney
5544c78299
Fix build error for MSP430 and Cortex A with IAR (#937)
* fix whitespace in asm macros

* Revert formatting ARM_CA5_No_GIC and ARM_CA9
2023-12-29 11:48:56 -08:00
Soren Ptak
43946db07b Refactoring xPortIsAuthorizedToAccessBuffer 2023-12-29 11:18:27 -05:00
Soren Ptak
a1191243f9 Merge branch 'ARM_CRx_MPU' of github.com:Skptak/FreeRTOS-Kernel into ARM_CRx_MPU 2023-12-28 17:51:47 -05:00
Soren Ptak
80d09879d1 Add in Access Control List support to the port 2023-12-28 17:51:42 -05:00
Soren Ptak
26019cd2d7 Fix copyright headers in the new port files 2023-12-27 21:44:04 -05:00
Soren Ptak
a33a6ddc76 Add new spell check words 2023-12-27 20:31:08 -05:00
Soren Ptak
d8b08270d0 Add support for the port in the CMakeLists.txt files 2023-12-27 19:39:38 -05:00
Soren Ptak
4a74745eb2 First draft of a CRx MPU Port for FreeRTOS 2023-12-27 18:50:31 -05:00
Soren Ptak
58f0d36e76
Export MPU Section Attributes (#931)
Export the PRIVILEGED_FUNCTION, PRIVILEGED_DATA, and FREERTOS_SYSTEM_CALL
attributes to make it easier for end users to add their own privileged functions and
system calls.
2023-12-26 16:06:50 +05:30
dps.lwk
93380c02a1
RP2040: FreeRTOS-Kernel-Static use configKERNEL_PROVIDED_STATIC_MEMORY (#934)
Remove the idle_task_static_memory.c and use the new default implementations
to allows for FreeRTOS-Kernel-Static to be used with configNUMBER_OF_CORES > 1
2023-12-26 17:02:47 +08:00
Forty-Bot
ec93432a59
Fix build with modern GCC (#933)
* GCC: MSP430F449: Add missing attributes

Apparently at some point in the past, GCC (or TI's GCC) used to define
these attributes. Define them ourselves so that we can compile the demo
application.

* GCC: MSP430F449: Make interrupts return void

If a return type of a function is not specified, it defaults to int. Set
the return type of interrupts to void to avoid warnings.

* GCC: MSP430F449: Define portPOINTER_SIZE_TYPE

portPOINTER_SIZE_TYPE defaults to uint32_t if undefined. Define it to
uint16_t, which is correct for this port.
2023-12-22 14:09:55 -07:00