• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2013-2019 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 #include "los_context.h"
33 #include "securec.h"
34 #include "los_arch_regs.h"
35 #include "los_arch_context.h"
36 #include "los_arch_interrupt.h"
37 #include "los_task.h"
38 #include "los_sched.h"
39 #include "los_interrupt.h"
40 #include "los_arch_timer.h"
41 #include "los_timer.h"
42 #include "los_debug.h"
43 
44 STATIC UINT32 g_sysNeedSched = FALSE;
45 
46 UINT32 g_stackDefault[] = {
47     0x00000000,     /* REG_OFF_PC */
48     0x00000000,     /* REG_OFF_PS */
49     0x00000A00,     /* REG_OFF_AR00 */
50     0x00000A01,     /* REG_OFF_AR01 */
51     0x00000A02,     /* REG_OFF_AR02 */
52     0x00000A03,     /* REG_OFF_AR03 */
53     0x00000A04,     /* REG_OFF_AR04 */
54     0x00000A05,     /* REG_OFF_AR05 */
55     0x00000A06,     /* REG_OFF_AR06 */
56     0x00000A07,     /* REG_OFF_AR07 */
57     0x00000A08,     /* REG_OFF_AR08 */
58     0x00000A09,     /* REG_OFF_AR09 */
59     0x00000A10,     /* REG_OFF_AR10 */
60     0x00000A11,     /* REG_OFF_AR11 */
61     0x00000A12,     /* REG_OFF_AR12 */
62     0x00000A13,     /* REG_OFF_AR13 */
63     0x00000A14,     /* REG_OFF_AR14 */
64     0x00000A15,     /* REG_OFF_AR15 */
65     0x00000000,     /* REG_OFF_RESERVED */
66     0x00000000,     /* REG_OFF_EXCCAUSE */
67     0x00000000,     /* REG_OFF_EXCVASSR */
68     0x00000000,     /* REG_OFF_LCOUNT */
69     0x00000000,     /* REG_OFF_LEND */
70     0x00000000,     /* REG_OFF_LBEG */
71 #if (defined (__FPU_PRESENT) && (__FPU_PRESENT == 1U))
72     0x00000000,     /* REG_OFF_TMP0 */
73     0x00000001,     /* REG_OFF_CPENABLE | CONTEXT_OFF_CPSTORED */
74     0x00000000,     /* REG_OFF_FCR */
75     0x00000000,     /* REG_OFF_FSR */
76     0x00000000,     /* REG_OFF_F0 */
77     0x00000000,     /* REG_OFF_F1 */
78     0x00000000,     /* REG_OFF_F2 */
79     0x00000000,     /* REG_OFF_F3 */
80     0x00000000,     /* REG_OFF_F4 */
81     0x00000000,     /* REG_OFF_F5 */
82     0x00000000,     /* REG_OFF_F6 */
83     0x00000000,     /* REG_OFF_F7 */
84     0x00000000,     /* REG_OFF_F8 */
85     0x00000000,     /* REG_OFF_F9 */
86     0x00000000,     /* REG_OFF_F10 */
87     0x00000000,     /* REG_OFF_F11 */
88     0x00000000,     /* REG_OFF_F12 */
89     0x00000000,     /* REG_OFF_F13 */
90     0x00000000,     /* REG_OFF_F14 */
91     0x00000000,     /* REG_OFF_F15 */
92 #endif
93     0x00000000,     /* REG_OFF_SPILL_RESERVED */
94     0x00000000,     /* REG_OFF_SPILL_RESERVED */
95     0x00000000,     /* REG_OFF_SPILL_RESERVED */
96     0x00000000,     /* REG_OFF_SPILL_RESERVED */
97 };
98 
ArchInit(VOID)99 LITE_OS_SEC_TEXT_INIT VOID ArchInit(VOID)
100 {
101     HalHwiInit();
102 }
103 
ArchSysExit(VOID)104 LITE_OS_SEC_TEXT_MINOR VOID ArchSysExit(VOID)
105 {
106     (VOID)LOS_IntLock();
107     while (1) {
108     }
109 }
110 
ArchTskStackInit(UINT32 taskID,UINT32 stackSize,VOID * topStack)111 LITE_OS_SEC_TEXT_INIT VOID *ArchTskStackInit(UINT32 taskID, UINT32 stackSize, VOID *topStack)
112 {
113     errno_t result;
114     TaskContext *context = (TaskContext *)((UINTPTR)topStack + stackSize - sizeof(TaskContext));
115 
116     /* initialize the task context */
117     result = memcpy_s(context, sizeof(TaskContext), g_stackDefault, sizeof(TaskContext));
118     if (result != EOK) {
119         PRINT_ERR("[%s] memcpy_s failed!\n", __func__);
120     }
121 
122     context->pc       = (UINT32)(UINTPTR)OsTaskEntry;
123     context->regA[INDEX_OF_SP]  = (UINTPTR)topStack + stackSize; /* endStack */
124     context->regA[INDEX_OF_ARGS0]  = taskID; /* argument1 */
125     context->ps       = SPREG_PS_STACK_CROSS | WOE_ENABLE | 1 << BIT_CALLINC; /* set to kernel stack */
126 
127     return (VOID *)context;
128 }
129 
HalStartToRun(VOID)130 VOID HalStartToRun(VOID)
131 {
132     __asm__ volatile ("call0 OsStartToRun");
133 }
134 
ArchStartSchedule(VOID)135 LITE_OS_SEC_TEXT_INIT UINT32 ArchStartSchedule(VOID)
136 {
137     (VOID)LOS_IntLock();
138     OsSchedStart();
139     HalStartToRun();
140     return LOS_OK;
141 }
142 
ArchTaskSchedule(VOID)143 VOID ArchTaskSchedule(VOID)
144 {
145     UINT32 intSave;
146 
147     if (OS_INT_ACTIVE) {
148         g_sysNeedSched = TRUE;
149         return;
150     }
151 
152     intSave = LOS_IntLock();
153     g_sysNeedSched = FALSE;
154     BOOL isSwitch = OsSchedTaskSwitch();
155     if (isSwitch) {
156         HalTaskContextSwitch();
157     }
158 
159     LOS_IntRestore(intSave);
160     return;
161 }
162 
HalIrqEndCheckNeedSched(VOID)163 VOID HalIrqEndCheckNeedSched(VOID)
164 {
165     if (g_sysNeedSched && g_taskScheduled && LOS_CHECK_SCHEDULE) {
166         ArchTaskSchedule();
167     }
168 }
169