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_hw_pri.h"
33 #include "los_task_pri.h"
34
35 /* support cpu vendors */
36 CpuVendor g_cpuTable[] = {
37 /* armv7-a */
38 { 0xc07, "Cortex-A7" },
39 { 0xc09, "Cortex-A9" },
40 { 0, NULL }
41 };
42
43 /* logical cpu mapping */
44 UINT64 g_cpuMap[LOSCFG_KERNEL_CORE_NUM] = {
45 [0 ... LOSCFG_KERNEL_CORE_NUM - 1] = (UINT64)(-1)
46 };
47
48 /* bit[30] is enable FPU */
49 #define FP_EN (1U << 30)
OsTaskExit(VOID)50 LITE_OS_SEC_TEXT_INIT VOID OsTaskExit(VOID)
51 {
52 __asm__ __volatile__("swi 0");
53 }
54
55 #ifdef LOSCFG_GDB
56 STATIC VOID OsTaskEntrySetupLoopFrame(UINT32) __attribute__((noinline, naked));
OsTaskEntrySetupLoopFrame(UINT32 arg0)57 VOID OsTaskEntrySetupLoopFrame(UINT32 arg0)
58 {
59 asm volatile("\tsub fp, sp, #0x4\n"
60 "\tpush {fp, lr}\n"
61 "\tadd fp, sp, #0x4\n"
62 "\tpush {fp, lr}\n"
63
64 "\tadd fp, sp, #0x4\n"
65 "\tbl OsTaskEntry\n"
66
67 "\tpop {fp, lr}\n"
68 "\tpop {fp, pc}\n");
69 }
70 #endif
71
OsTaskStackInit(UINT32 taskID,UINT32 stackSize,VOID * topStack,BOOL initFlag)72 LITE_OS_SEC_TEXT_INIT VOID *OsTaskStackInit(UINT32 taskID, UINT32 stackSize, VOID *topStack, BOOL initFlag)
73 {
74 if (initFlag == TRUE) {
75 OsStackInit(topStack, stackSize);
76 }
77 TaskContext *taskContext = (TaskContext *)(((UINTPTR)topStack + stackSize) - sizeof(TaskContext));
78
79 /* initialize the task context */
80 #ifdef LOSCFG_GDB
81 taskContext->PC = (UINTPTR)OsTaskEntrySetupLoopFrame;
82 #else
83 taskContext->PC = (UINTPTR)OsTaskEntry;
84 #endif
85 taskContext->LR = (UINTPTR)OsTaskExit; /* LR should be kept, to distinguish it's THUMB or ARM instruction */
86 taskContext->R0 = taskID; /* R0 */
87
88 #ifdef LOSCFG_THUMB
89 taskContext->regCPSR = PSR_MODE_SVC_THUMB; /* CPSR (Enable IRQ and FIQ interrupts, THUMNB-mode) */
90 #else
91 taskContext->regCPSR = PSR_MODE_SVC_ARM; /* CPSR (Enable IRQ and FIQ interrupts, ARM-mode) */
92 #endif
93
94 #if !defined(LOSCFG_ARCH_FPU_DISABLE)
95 /* 0xAAA0000000000000LL : float reg initialed magic word */
96 for (UINT32 index = 0; index < FP_REGS_NUM; index++) {
97 taskContext->D[index] = 0xAAA0000000000000LL + index; /* D0 - D31 */
98 }
99 taskContext->regFPSCR = 0;
100 taskContext->regFPEXC = FP_EN;
101 #endif
102
103 return (VOID *)taskContext;
104 }
105
OsUserCloneParentStack(VOID * childStack,UINTPTR parentTopOfStack,UINT32 parentStackSize)106 LITE_OS_SEC_TEXT VOID OsUserCloneParentStack(VOID *childStack, UINTPTR parentTopOfStack, UINT32 parentStackSize)
107 {
108 LosTaskCB *task = OsCurrTaskGet();
109 sig_cb *sigcb = &task->sig;
110 VOID *cloneStack = NULL;
111
112 if (sigcb->sigContext != NULL) {
113 cloneStack = (VOID *)((UINTPTR)sigcb->sigContext - sizeof(TaskContext));
114 } else {
115 cloneStack = (VOID *)(((UINTPTR)parentTopOfStack + parentStackSize) - sizeof(TaskContext));
116 }
117
118 (VOID)memcpy_s(childStack, sizeof(TaskContext), cloneStack, sizeof(TaskContext));
119 ((TaskContext *)childStack)->R0 = 0;
120 }
121
OsUserTaskStackInit(TaskContext * context,UINTPTR taskEntry,UINTPTR stack)122 LITE_OS_SEC_TEXT_INIT VOID OsUserTaskStackInit(TaskContext *context, UINTPTR taskEntry, UINTPTR stack)
123 {
124 LOS_ASSERT(context != NULL);
125
126 #ifdef LOSCFG_THUMB
127 context->regCPSR = PSR_MODE_USR_THUMB;
128 #else
129 context->regCPSR = PSR_MODE_USR_ARM;
130 #endif
131 context->R0 = stack;
132 context->USP = TRUNCATE(stack, LOSCFG_STACK_POINT_ALIGN_SIZE);
133 context->ULR = 0;
134 context->PC = (UINTPTR)taskEntry;
135 }
136
OsInitSignalContext(const VOID * sp,VOID * signalContext,UINTPTR sigHandler,UINT32 signo,UINT32 param)137 VOID OsInitSignalContext(const VOID *sp, VOID *signalContext, UINTPTR sigHandler, UINT32 signo, UINT32 param)
138 {
139 IrqContext *newSp = (IrqContext *)signalContext;
140 (VOID)memcpy_s(signalContext, sizeof(IrqContext), sp, sizeof(IrqContext));
141 newSp->PC = sigHandler;
142 newSp->R0 = signo;
143 newSp->R1 = param;
144 }
145
Dmb(VOID)146 DEPRECATED VOID Dmb(VOID)
147 {
148 __asm__ __volatile__ ("dmb" : : : "memory");
149 }
150
Dsb(VOID)151 DEPRECATED VOID Dsb(VOID)
152 {
153 __asm__ __volatile__("dsb" : : : "memory");
154 }
155
Isb(VOID)156 DEPRECATED VOID Isb(VOID)
157 {
158 __asm__ __volatile__("isb" : : : "memory");
159 }
160
FlushICache(VOID)161 VOID FlushICache(VOID)
162 {
163 /*
164 * Use ICIALLUIS instead of ICIALLU. ICIALLUIS operates on all processors in the Inner
165 * shareable domain of the processor that performs the operation.
166 */
167 __asm__ __volatile__ ("mcr p15, 0, %0, c7, c1, 0" : : "r" (0) : "memory");
168 }
169
DCacheFlushRange(UINT32 start,UINT32 end)170 VOID DCacheFlushRange(UINT32 start, UINT32 end)
171 {
172 arm_clean_cache_range(start, end);
173 }
174
DCacheInvRange(UINT32 start,UINT32 end)175 VOID DCacheInvRange(UINT32 start, UINT32 end)
176 {
177 arm_inv_cache_range(start, end);
178 }
179
180