1 /*
2  * FreeRTOS Kernel V11.1.0
3  * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
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 Changes from V1.00:
31 
32     + pxPortInitialiseStack() now initialises the stack of new tasks to the
33       same format used by the compiler.  This allows the compiler generated
34       interrupt mechanism to be used for context switches.
35 
36 Changes from V2.4.2:
37 
38     + pvPortMalloc and vPortFree have been removed.  The projects now use
39       the definitions from the source/portable/MemMang directory.
40 
41 Changes from V2.6.1:
42 
43     + usPortCheckFreeStackSpace() has been moved to tasks.c.
44 */
45 
46 
47 
48 #include <stdlib.h>
49 #include "FreeRTOS.h"
50 
51 /*-----------------------------------------------------------*/
52 
53 /* See header file for description. */
pxPortInitialiseStack(StackType_t * pxTopOfStack,TaskFunction_t pxCode,void * pvParameters)54 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
55 {
56 StackType_t DS_Reg = 0;
57 StackType_t * pxOriginalSP;
58 
59     /* Place a few bytes of known values on the bottom of the stack.
60     This is just useful for debugging. */
61 
62     *pxTopOfStack = 0x1111;
63     pxTopOfStack--;
64     *pxTopOfStack = 0x2222;
65     pxTopOfStack--;
66     *pxTopOfStack = 0x3333;
67     pxTopOfStack--;
68     *pxTopOfStack = 0x4444;
69     pxTopOfStack--;
70     *pxTopOfStack = 0x5555;
71     pxTopOfStack--;
72 
73 
74     /*lint -e950 -e611 -e923 Lint doesn't like this much - but nothing I can do about it. */
75 
76     /* We are going to start the scheduler using a return from interrupt
77     instruction to load the program counter, so first there would be the
78     status register and interrupt return address.  We make this the start
79     of the task. */
80     *pxTopOfStack = portINITIAL_SW;
81     pxTopOfStack--;
82     *pxTopOfStack = FP_SEG( pxCode );
83     pxTopOfStack--;
84     *pxTopOfStack = FP_OFF( pxCode );
85     pxTopOfStack--;
86 
87     /* We are going to setup the stack for the new task to look like
88     the stack frame was setup by a compiler generated ISR.  We need to know
89     the address of the existing stack top to place in the SP register within
90     the stack frame.  pxOriginalSP holds SP before (simulated) pusha was
91     called. */
92     pxOriginalSP = pxTopOfStack;
93 
94     /* The remaining registers would be pushed on the stack by our context
95     switch function.  These are loaded with values simply to make debugging
96     easier. */
97     *pxTopOfStack = FP_OFF( pvParameters );     /* AX */
98     pxTopOfStack--;
99     *pxTopOfStack = ( StackType_t ) 0xCCCC; /* CX */
100     pxTopOfStack--;
101     *pxTopOfStack = FP_SEG( pvParameters );     /* DX */
102     pxTopOfStack--;
103     *pxTopOfStack = ( StackType_t ) 0xBBBB; /* BX */
104     pxTopOfStack--;
105     *pxTopOfStack = FP_OFF( pxOriginalSP );     /* SP */
106     pxTopOfStack--;
107     *pxTopOfStack = ( StackType_t ) 0xBBBB; /* BP */
108     pxTopOfStack--;
109     *pxTopOfStack = ( StackType_t ) 0x0123; /* SI */
110     pxTopOfStack--;
111     *pxTopOfStack = ( StackType_t ) 0xDDDD; /* DI */
112 
113     /* We need the true data segment. */
114     __asm{  MOV DS_Reg, DS };
115 
116     pxTopOfStack--;
117     *pxTopOfStack = DS_Reg; /* DS */
118 
119     pxTopOfStack--;
120     *pxTopOfStack = ( StackType_t ) 0xEEEE; /* ES */
121 
122     /* The AX register is pushed again twice - don't know why. */
123     pxTopOfStack--;
124     *pxTopOfStack = FP_OFF( pvParameters );     /* AX */
125     pxTopOfStack--;
126     *pxTopOfStack = FP_OFF( pvParameters );     /* AX */
127 
128 
129     #ifdef DEBUG_BUILD
130         /* The compiler adds space to each ISR stack if building to
131         include debug information.  Presumably this is used by the
132         debugger - we don't need to initialise it to anything just
133         make sure it is there. */
134         pxTopOfStack--;
135     #endif
136 
137     /*lint +e950 +e611 +e923 */
138 
139     return pxTopOfStack;
140 }
141 /*-----------------------------------------------------------*/
142