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;
26;
27;/* Include necessary system files.  */
28;
29;#include "tx_api.h"
30;#include "tx_thread.h"
31;#include "tx_timer.h"
32;
33;
34    .global     __tx_thread_system_state
35    .global     __tx_thread_current_ptr
36    .global     __tx_thread_system_stack_ptr
37    .global     __tx_thread_execute_ptr
38    .global     __tx_timer_time_slice
39    .global     __tx_thread_schedule
40    .global     __tx_thread_preempt_disable
41
42    .text
43
44;/**************************************************************************/
45;/*                                                                        */
46;/*  FUNCTION                                               RELEASE        */
47;/*                                                                        */
48;/*    _tx_thread_context_restore                           RXv1/GNURX     */
49;/*                                                           6.1.11       */
50;/*  AUTHOR                                                                */
51;/*                                                                        */
52;/*    William E. Lamie, Microsoft Corporation                             */
53;/*                                                                        */
54;/*  DESCRIPTION                                                           */
55;/*                                                                        */
56;/*    This function restores the interrupt context if it is processing a  */
57;/*    nested interrupt.  If not, it returns to the interrupt thread if no */
58;/*    preemption is necessary.  Otherwise, if preemption is necessary or  */
59;/*    if no thread was running, the function returns to the scheduler.    */
60;/*                                                                        */
61;/*  INPUT                                                                 */
62;/*                                                                        */
63;/*    None                                                                */
64;/*                                                                        */
65;/*  OUTPUT                                                                */
66;/*                                                                        */
67;/*    None                                                                */
68;/*                                                                        */
69;/*  CALLS                                                                 */
70;/*                                                                        */
71;/*    _tx_thread_schedule                   Thread scheduling routine     */
72;/*                                                                        */
73;/*  CALLED BY                                                             */
74;/*                                                                        */
75;/*    ISRs                                  Interrupt Service Routines    */
76;/*                                                                        */
77;/*  RELEASE HISTORY                                                       */
78;/*                                                                        */
79;/*    DATE              NAME                      DESCRIPTION             */
80;/*                                                                        */
81;/*  08-02-2021     William E. Lamie         Initial Version 6.1.8         */
82;/*  10-15-2021     William E. Lamie         Modified comment(s), and      */
83;/*                                            removed unnecessary stack   */
84;/*                                            type placement,             */
85;/*                                            resulting in version 6.1.9  */
86;/*  01-31-2022     William E. Lamie         Modified comment(s),          */
87;/*                                            resulting in version 6.1.10 */
88;/*  04-25-2022     William E. Lamie         Modified comment(s),          */
89;/*                                            resulting in version 6.1.11 */
90;/*                                                                        */
91;/**************************************************************************/
92;VOID   _tx_thread_context_restore(VOID)
93;{
94    .global __tx_thread_context_restore
95__tx_thread_context_restore:
96;
97;    /* Lockout interrupts.  */
98
99     CLRPSW I                                    ; Disable interrupts
100
101;    /* Determine if interrupts are nested.  */
102;    if (--_tx_thread_system_state)
103;    {
104
105     MOV.L    #__tx_thread_system_state, R1
106     MOV.L    [R1], R2
107     SUB      #1, R2
108     MOV.L    R2,[R1]
109     BEQ      __tx_thread_not_nested_restore
110
111;
112;    /* Interrupts are nested.  */
113;
114;    /* Recover the saved registers from the interrupt stack
115;       and return to the point of interrupt.  */
116;
117__tx_thread_nested_restore:
118     POPM    R14-R15                             ; Restore R14-R15
119     POPM    R3-R5                               ; Restore R3-R5
120     POPM    R1-R2                               ; Restore R1-R2
121     RTE                                         ; Return to point of interrupt, restore PSW including IPL
122;    }
123
124__tx_thread_not_nested_restore:
125;
126;    /* Determine if a thread was interrupted and no preemption is required.  */
127;    else if (((_tx_thread_current_ptr) && (_tx_thread_current_ptr == _tx_thread_execute_ptr)
128;               || (_tx_thread_preempt_disable))
129;    {
130
131     MOV.L    #__tx_thread_current_ptr, R1       ; Pickup current thread ptr address
132     MOV.L    [R1], R2
133     CMP      #0, R2
134     BEQ      __tx_thread_idle_system_restore
135
136     MOV.L    #__tx_thread_preempt_disable, R3   ; Pick up preempt disable flag
137     MOV.L    [R3], R3
138     CMP      #0, R3
139     BNE      __tx_thread_no_preempt_restore     ; If pre-empt disable flag set, we simply return to the original point of interrupt regardless
140
141     MOV.L    #__tx_thread_execute_ptr, R3       ; (_tx_thread_current_ptr != _tx_thread_execute_ptr)
142     CMP      [R3], R2
143     BNE      __tx_thread_preempt_restore        ; Jump to pre-empt restoring
144;
145__tx_thread_no_preempt_restore:
146     SETPSW  U                                   ; User stack
147     POPM    R14-R15                             ; Restore R14-R15
148     POPM    R3-R5                               ; Restore R3-R5
149     POPM    R1-R2                               ; Restore R1-R2
150     RTE                                         ; Return to point of interrupt, restore PSW including IPL
151
152;    }
153;    else
154;    {
155
156__tx_thread_preempt_restore:
157
158;    /* Save the remaining time-slice and disable it.  */
159;    if (_tx_timer_time_slice)
160;    {
161
162     MOV.L    #__tx_timer_time_slice, R3         ; Pickup time-slice address
163     MOV.L    [R3],R4                            ; Pickup actual time-slice
164     CMP      #0, R4
165     BEQ      __tx_thread_dont_save_ts           ; No time slice to save
166;
167;        _tx_thread_current_ptr -> tx_thread_time_slice =  _tx_timer_time_slice;
168;        _tx_timer_time_slice =  0;
169;
170     MOV.L    R4,24[R2]                          ; Save thread's time slice
171     MOV.L    #0,R4                              ; Clear value
172     MOV.L    R4,[R3]                            ; Disable global time slice flag
173;    }
174__tx_thread_dont_save_ts:
175;
176;   /* Now store the remaining registers!   */
177
178     SETPSW   U                                  ; User stack
179     PUSHM    R6-R13
180
181     MVFACHI   R5
182     MVFACMI   R6
183     PUSHM     R5-R6
184
185;
186;    /* Clear the current task pointer.  */
187;    _tx_thread_current_ptr =  TX_NULL;
188;    R1 ->  _tx_thread_current_ptr
189;    R2 -> *_tx_thread_current_ptr
190
191     MOV.L   R0,8[R2]                            ; Save thread's stack pointer in thread control block
192     MOV.L   #0,R2                               ; Build NULL value
193     MOV.L   R2,[R1]                             ; Set current thread to NULL
194
195;    /* Return to the scheduler.  */
196;    _tx_thread_schedule();
197
198__tx_thread_idle_system_restore:
199     MVTC    #0, PSW                             ; Reset interrupt priority level to 0
200     BRA     __tx_thread_schedule                ; Jump to scheduler
201;    }
202;
203;}
204;
205    .end
206