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.6.1
37 
38     + Move usPortCheckFreeStackSpace() to tasks.c.
39 */
40 
41 
42 #include <dos.h>
43 #include <stdlib.h>
44 #include "FreeRTOS.h"
45 
46 /*-----------------------------------------------------------*/
47 
48 /* See header file for description. */
pxPortInitialiseStack(StackType_t * pxTopOfStack,TaskFunction_t pxCode,void * pvParameters)49 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
50 {
51 StackType_t DS_Reg = 0;
52 
53     /* Place a few bytes of known values on the bottom of the stack.
54     This is just useful for debugging. */
55 
56     *pxTopOfStack = 0x1111;
57     pxTopOfStack--;
58     *pxTopOfStack = 0x2222;
59     pxTopOfStack--;
60     *pxTopOfStack = 0x3333;
61     pxTopOfStack--;
62     *pxTopOfStack = 0x4444;
63     pxTopOfStack--;
64     *pxTopOfStack = 0x5555;
65     pxTopOfStack--;
66 
67 
68     /*lint -e950 -e611 -e923 Lint doesn't like this much - but nothing I can do about it. */
69 
70     /* We are going to start the scheduler using a return from interrupt
71     instruction to load the program counter, so first there would be the
72     function call with parameters preamble. */
73 
74     *pxTopOfStack = FP_SEG( pvParameters );
75     pxTopOfStack--;
76     *pxTopOfStack = FP_OFF( pvParameters );
77     pxTopOfStack--;
78     *pxTopOfStack = FP_SEG( pxCode );
79     pxTopOfStack--;
80     *pxTopOfStack = FP_OFF( pxCode );
81     pxTopOfStack--;
82 
83     /* Next the status register and interrupt return address. */
84     *pxTopOfStack = portINITIAL_SW;
85     pxTopOfStack--;
86     *pxTopOfStack = FP_SEG( pxCode );
87     pxTopOfStack--;
88     *pxTopOfStack = FP_OFF( pxCode );
89     pxTopOfStack--;
90 
91     /* The remaining registers would be pushed on the stack by our context
92     switch function.  These are loaded with values simply to make debugging
93     easier. */
94     *pxTopOfStack = ( StackType_t ) 0xAAAA; /* AX */
95     pxTopOfStack--;
96     *pxTopOfStack = ( StackType_t ) 0xBBBB; /* BX */
97     pxTopOfStack--;
98     *pxTopOfStack = ( StackType_t ) 0xCCCC; /* CX */
99     pxTopOfStack--;
100     *pxTopOfStack = ( StackType_t ) 0xDDDD; /* DX */
101     pxTopOfStack--;
102     *pxTopOfStack = ( StackType_t ) 0xEEEE; /* ES */
103     pxTopOfStack--;
104 
105     /* We need the true data segment. */
106     __asm{  MOV DS_Reg, DS };
107 
108     *pxTopOfStack = DS_Reg;                     /* DS */
109     pxTopOfStack--;
110     *pxTopOfStack = ( StackType_t ) 0x0123; /* SI */
111     pxTopOfStack--;
112     *pxTopOfStack = ( StackType_t ) 0xDDDD; /* DI */
113     pxTopOfStack--;
114     *pxTopOfStack = ( StackType_t ) 0xBBBB; /* BP */
115 
116     /*lint +e950 +e611 +e923 */
117 
118     return pxTopOfStack;
119 }
120 /*-----------------------------------------------------------*/
121