1 /**************************************************************************/
2 /*                                                                        */
3 /*       Copyright (c) Microsoft Corporation. All rights reserved.        */
4 /*                                                                        */
5 /*       This software is licensed under the Microsoft Software License   */
6 /*       Terms for Microsoft Azure RTOS. Full text of the license can be  */
7 /*       found in the LICENSE file at https://aka.ms/AzureRTOS_EULA       */
8 /*       and in the root directory of this software.                      */
9 /*                                                                        */
10 /**************************************************************************/
11 
12 
13 /**************************************************************************/
14 /**************************************************************************/
15 /**                                                                       */
16 /** ThreadX Component                                                     */
17 /**                                                                       */
18 /**   Thread                                                              */
19 /**                                                                       */
20 /**************************************************************************/
21 /**************************************************************************/
22 
23 
24 #define TX_SOURCE_CODE
25 #define TX_THREAD_SMP_SOURCE_CODE
26 
27 
28 /* Include necessary system files.  */
29 
30 #include "tx_api.h"
31 #include "tx_thread.h"
32 #include "tx_timer.h"
33 
34 
35 /**************************************************************************/
36 /*                                                                        */
37 /*  FUNCTION                                               RELEASE        */
38 /*                                                                        */
39 /*    _tx_thread_context_save                           SMP/Linux/GCC     */
40 /*                                                           6.1          */
41 /*  AUTHOR                                                                */
42 /*                                                                        */
43 /*    William E. Lamie, Microsoft Corporation                             */
44 /*                                                                        */
45 /*  DESCRIPTION                                                           */
46 /*                                                                        */
47 /*    This function saves the context of an executing thread in the       */
48 /*    beginning of interrupt processing.  The function also ensures that  */
49 /*    the system stack is used upon return to the calling ISR.            */
50 /*                                                                        */
51 /*  INPUT                                                                 */
52 /*                                                                        */
53 /*    None                                                                */
54 /*                                                                        */
55 /*  OUTPUT                                                                */
56 /*                                                                        */
57 /*    None                                                                */
58 /*                                                                        */
59 /*  CALLS                                                                 */
60 /*                                                                        */
61 /*    _tx_linux_debug_entry_insert                                        */
62 /*    _tx_linux_mutex_obtain                                              */
63 /*    _tx_linux_thread_suspend                                            */
64 /*                                                                        */
65 /*  CALLED BY                                                             */
66 /*                                                                        */
67 /*    ISRs                                                                */
68 /*                                                                        */
69 /*  RELEASE HISTORY                                                       */
70 /*                                                                        */
71 /*    DATE              NAME                      DESCRIPTION             */
72 /*                                                                        */
73 /*  09-30-2020     William E. Lamie         Initial Version 6.1           */
74 /*                                                                        */
75 /**************************************************************************/
_tx_thread_context_save(VOID)76 VOID   _tx_thread_context_save(VOID)
77 {
78 
79 TX_THREAD   *thread_ptr;
80 UINT        interrupt_posture;
81 
82     /* Loop to perform retries on thread preemption.  */
83     while(1)
84     {
85 
86         /* Lock mutex to ensure other threads are not playing with
87            the core ThreadX data structures.  */
88         interrupt_posture =  _tx_thread_smp_protect();
89 
90         /* Check for a system error condition.  */
91         if (interrupt_posture != TX_FALSE)
92         {
93 
94             /* This should not happen... increment the system error counter.  */
95             _tx_linux_system_error++;
96         }
97 
98         /* Debug entry.  */
99         _tx_linux_debug_entry_insert("CONTEXT_SAVE", __FILE__, __LINE__);
100 
101         /* All ISRs are assumed to be serviced on core 0.  */
102 
103         /* Pickup the current thread pointer.  */
104         thread_ptr =  _tx_thread_current_ptr[0];
105 
106         /* If an application thread is running, suspend it to simulate preemption. */
107         if ((thread_ptr) && (_tx_thread_system_state[0] == 0))
108         {
109 
110             /* Yes, this is the first interrupt and an application thread is running...
111                suspend it!  */
112             _tx_linux_thread_suspend(thread_ptr -> tx_thread_linux_thread_id);
113 
114             /* Debug entry.  */
115             _tx_linux_debug_entry_insert("CONTEXT_SAVE-suspend_thread", __FILE__, __LINE__);
116         }
117 
118         /* Increment the nested interrupt condition.  */
119         _tx_thread_system_state[0]++;
120 
121         /* Do not release the protection for ISRs, since in SMP mode other threads might be scheduled on
122            interrupted virtual core 0, which will confuse ThreadX.  */
123 
124         /* Get out of the loop.  */
125         break;
126     }
127 }
128 
129