1 /*
2 * Copyright (c) 2009-2022 Huawei Technologies Co., Ltd. All rights reserved.
3 *
4 * UniProton is licensed under Mulan PSL v2.
5 * You can use this software according to the terms and conditions of the Mulan PSL v2.
6 * You may obtain a copy of Mulan PSL v2 at:
7 * http://license.coscl.org.cn/MulanPSL2
8 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
9 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
10 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
11 * See the Mulan PSL v2 for more details.
12 * Create: 2009-07-24
13 * Description: Hardware Initialization
14 */
15 #include "prt_cpu_external.h"
16
17 #define OS_CPSR_REG_DEFAULT_VAL 0x01000000U
18 #define OS_EXCRETURN_DEFAULT_VAL 0xFFFFFFFDU
19
OsTskContextInit(U32 taskId,U32 stackSize,uintptr_t * topStack,uintptr_t funcTskEntry)20 OS_SEC_L4_TEXT void *OsTskContextInit(U32 taskId, U32 stackSize, uintptr_t *topStack, uintptr_t funcTskEntry)
21 {
22 uintptr_t endStack;
23 struct TagHwContext *context = NULL;
24
25 /*
26 * O0优化情况下,子函数会自动将父函数的栈指针,压入到父函数栈顶+4
27 * 处,根函数没有父函数,需要构造虚拟父函数,预留16字节空间,
28 * 未使用的空间作为维测使用
29 * O2优化情况下,编译器生成指令不会进行上述操作,每个任务的栈底预留16
30 * 个字节均初始化魔术字,作为维测使用,以判断是否有任务从栈底溢出
31 */
32 endStack = TRUNCATE((uintptr_t)topStack + stackSize, OS_TSK_STACK_ADDR_ALIGN);
33 context = (struct TagHwContext *)(endStack - sizeof(struct TagHwContext));
34 /* 初始化任务上下文 */
35 *context = (struct TagHwContext){0};
36 // 配置寄存器默认初值
37 context->basePri = 0;
38 context->excReturn = OS_EXCRETURN_DEFAULT_VAL;
39 /* 任务的第一个参数 */
40 context->r0 = taskId;
41 context->lr = 0;
42 context->pc = (uintptr_t)funcTskEntry;
43 context->psr = OS_CPSR_REG_DEFAULT_VAL; /* bit20表示thumb指令 */
44
45 return (void *)context;
46 }
47
OsTskContextGet(uintptr_t saveAddr,struct TskContext * context)48 OS_SEC_L4_TEXT void OsTskContextGet(uintptr_t saveAddr, struct TskContext *context)
49 {
50 *context = *((struct TskContext *)saveAddr);
51 }
52