1 /*
2 * Copyright (c) 2013-2020, Huawei Technologies Co., Ltd. All rights reserved.
3 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this list of
9 * conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 * of conditions and the following disclaimer in the documentation and/or other materials
13 * provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16 * to endorse or promote products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #ifndef _LOS_ARCH_CONTEXT_H
33 #define _LOS_ARCH_CONTEXT_H
34
35 #include "los_compiler.h"
36 #include "los_context.h"
37
38 #ifdef __cplusplus
39 #if __cplusplus
40 extern "C" {
41 #endif /* __cplusplus */
42 #endif /* __cplusplus */
43
44 /**
45 * @ingroup los_arch_context
46 * The initialization value of stack space.
47 */
48 #define EMPTY_STACK 0xCACA
49
50 #define TP_INIT_VALUE 0x02020202L
51 #define SP_INIT_VALUE 0x03030303L
52 #define S11_INIT_VALUE 0x04040404L
53 #define S10_INIT_VALUE 0x05050505L
54 #define S9_INIT_VALUE 0x06060606L
55 #define S8_INIT_VALUE 0x07070707L
56 #define S7_INIT_VALUE 0x08080808L
57 #define S6_INIT_VALUE 0x09090909L
58 #define S5_INIT_VALUE 0x10101010L
59 #define S4_INIT_VALUE 0x11111111L
60 #define S3_INIT_VALUE 0x12121212L
61 #define S2_INIT_VALUE 0x13131313L
62 #define S1_INIT_VALUE 0x14141414L
63 #define FP_INIT_VALUE 0x15151515L
64 #define T6_INIT_VALUE 0x16161616L
65 #define T5_INIT_VALUE 0x17171717L
66 #define T4_INIT_VALUE 0x18181818L
67 #define T3_INIT_VALUE 0x19191919L
68 #define A7_INIT_VALUE 0x20202020L
69 #define A6_INIT_VALUE 0x21212121L
70 #define A5_INIT_VALUE 0x22222222L
71 #define A4_INIT_VALUE 0x23232323L
72 #define A3_INIT_VALUE 0x24242424L
73 #define A2_INIT_VALUE 0x25252525L
74 #define A1_INIT_VALUE 0x26262626L
75 #define T2_INIT_VALUE 0x28282828L
76 #define T1_INIT_VALUE 0x29292929L
77 #define T0_INIT_VALUE 0x30303030L
78
79 /**
80 * @ingroup los_arch_context
81 * Define the type of a task context control block.
82 */
83 typedef struct {
84 UINT32 sp;
85 UINT32 tp;
86 UINT32 t6;
87 UINT32 t5;
88 UINT32 t4;
89 UINT32 t3;
90 UINT32 t2;
91 UINT32 t1;
92 UINT32 t0;
93 UINT32 s11;
94 UINT32 s10;
95 UINT32 s9;
96 UINT32 s8;
97 UINT32 s7;
98 UINT32 s6;
99 UINT32 s5;
100 UINT32 mstatus;
101 UINT32 mepc;
102 UINT32 a7;
103 UINT32 a6;
104 UINT32 a5;
105 UINT32 a4;
106 UINT32 a3;
107 UINT32 a2;
108 UINT32 a1;
109 UINT32 a0;
110 UINT32 s4;
111 UINT32 s3;
112 UINT32 s2;
113 UINT32 s1;
114 UINT32 s0;
115 UINT32 ra;
116 } TaskContext;
117
GetSP(VOID)118 STATIC INLINE UINTPTR GetSP(VOID)
119 {
120 UINTPTR spSave;
121 __asm__ __volatile__("mv %0, sp" : "=r"(spSave));
122 return spSave;
123 }
124
GetFp(VOID)125 STATIC INLINE UINTPTR GetFp(VOID)
126 {
127 UINTPTR fpSave = 0;
128 __asm__ __volatile__("mv %0, s0" : "=r"(fpSave));
129 return fpSave;
130 }
131
132 extern VOID HalStartToRun(VOID);
133
134 extern VOID HalTaskContextSwitch(UINT32 intSave);
135
136 /**
137 * @ingroup los_arch_context
138 * @brief Wait for interrupt.
139 *
140 * @par Description:
141 * <ul>
142 * <li>This API is used to suspend execution until interrupt or a debug request occurs.</li>
143 * </ul>
144 * @attention None.
145 *
146 * @param None.
147 *
148 * @retval: None.
149 *
150 * @par Dependency:
151 * los_arch_context.h: the header file that contains the API declaration.
152 * @see None.
153 */
154 extern VOID wfi(VOID);
155
156 /**
157 * @ingroup los_arch_context
158 * @brief: mem fence function.
159 *
160 * @par Description:
161 * This API is used to fence for memory.
162 *
163 * @attention:
164 * <ul><li>None.</li></ul>
165 *
166 * @param: None.
167 *
168 * @retval:None.
169 * @par Dependency:
170 * <ul><li>los_arch_context.h: the header file that contains the API declaration.</li></ul>
171 * @see None.
172 */
173 extern VOID mb(VOID);
174
175 /**
176 * @ingroup los_arch_context
177 * @brief: mem fence function.
178 *
179 * @par Description:
180 * This API is same as mb, it just for adaptation.
181 *
182 * @attention:
183 * <ul><li>None.</li></ul>
184 *
185 * @param: None.
186 *
187 * @retval:None.
188 * @par Dependency:
189 * <ul><li>los_arch_context.h: the header file that contains the API declaration.</li></ul>
190 * @see None.
191 */
192 extern VOID dsb(VOID);
193
194 #ifdef __cplusplus
195 #if __cplusplus
196 }
197 #endif /* __cplusplus */
198 #endif /* __cplusplus */
199
200 #endif /* _LOS_ARCH_CONTEXT_H */
201