更新日期: 2022/06/01 来源: https://gitee.com/weharmony/kernel_liteos_a_note
los_hw.c
浏览该文件的文档.
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
36/* support cpu vendors | 支持的CPU供应商*/
38 /* armv7-a */
39 { 0xc07, "Cortex-A7" },
40 { 0xc09, "Cortex-A9" },
41 { 0, NULL }
42};
43
44/* logical cpu mapping | cpu 逻辑层映射*/
45UINT64 g_cpuMap[LOSCFG_KERNEL_CORE_NUM] = {
46 [0 ... LOSCFG_KERNEL_CORE_NUM - 1] = (UINT64)(-1) //统一赋值,这个赋值方式还挺别致的.
47};//此处可查看鸿蒙内核源码分析(编码方式篇) https://my.oschina.net/weharmony/blog/5516143
48
49/* bit[30] is enable FPU */
50#define FP_EN (1U << 30)
51LITE_OS_SEC_TEXT_INIT VOID OsTaskExit(VOID)
52{// swi {cond} <immed_24>
53 __asm__ __volatile__("swi 0");//处理器产生软中断异常,swi指令的低24位存放的是0
54}//该指令可以产生SWI异常,ARM通过该指令从用户模式切到SVC模式.
55
56#ifdef LOSCFG_GDB
57STATIC VOID OsTaskEntrySetupLoopFrame(UINT32) __attribute__((noinline, naked));
59{
60 asm volatile("\tsub fp, sp, #0x4\n"
61 "\tpush {fp, lr}\n"
62 "\tadd fp, sp, #0x4\n"
63 "\tpush {fp, lr}\n"
64
65 "\tadd fp, sp, #0x4\n"
66 "\tbl OsTaskEntry\n"
67
68 "\tpop {fp, lr}\n"
69 "\tpop {fp, pc}\n");
70}
71#endif
72/// 内核态任务运行栈初始化
73LITE_OS_SEC_TEXT_INIT VOID *OsTaskStackInit(UINT32 taskID, UINT32 stackSize, VOID *topStack, BOOL initFlag)
74{
75 if (initFlag == TRUE) {
76 OsStackInit(topStack, stackSize);
77 }
78 TaskContext *taskContext = (TaskContext *)(((UINTPTR)topStack + stackSize) - sizeof(TaskContext));//上下文存放在栈的底部
79
80 /* initialize the task context */ //初始化任务上下文
81#ifdef LOSCFG_GDB
82 taskContext->PC = (UINTPTR)OsTaskEntrySetupLoopFrame;
83#else
84 taskContext->PC = (UINTPTR)OsTaskEntry;//内核态任务有统一的入口地址.
85#endif
86 taskContext->LR = (UINTPTR)OsTaskExit; /* LR should be kept, to distinguish it's THUMB or ARM instruction */
87 taskContext->R0 = taskID; /* R0 */
88
89#ifdef LOSCFG_THUMB
90 taskContext->regCPSR = PSR_MODE_SVC_THUMB; /* CPSR (Enable IRQ and FIQ interrupts, THUMNB-mode) */
91#else //用于设置CPSR寄存器
92 taskContext->regCPSR = PSR_MODE_SVC_ARM; /* CPSR (Enable IRQ and FIQ interrupts, ARM-mode) */
93#endif
94
95#if !defined(LOSCFG_ARCH_FPU_DISABLE)
96 /* 0xAAA0000000000000LL : float reg initialed magic word */
97 for (UINT32 index = 0; index < FP_REGS_NUM; index++) {
98 taskContext->D[index] = 0xAAA0000000000000LL + index; /* D0 - D31 */
99 }
100 taskContext->regFPSCR = 0;
101 taskContext->regFPEXC = FP_EN;
102#endif
103
104 return (VOID *)taskContext;
105}
106///把父任务上下文克隆给子任务
107LITE_OS_SEC_TEXT VOID OsUserCloneParentStack(VOID *childStack, UINTPTR parentTopOfStack, UINT32 parentStackSize)
108{
109 LosTaskCB *task = OsCurrTaskGet();
110 sig_cb *sigcb = &task->sig;
111 VOID *cloneStack = NULL;
112
113 if (sigcb->sigContext != NULL) {
114 cloneStack = (VOID *)((UINTPTR)sigcb->sigContext - sizeof(TaskContext));
115 } else {//cloneStack指向 TaskContext
116 cloneStack = (VOID *)(((UINTPTR)parentTopOfStack + parentStackSize) - sizeof(TaskContext));
117 }
118 (VOID)memcpy_s(childStack, sizeof(TaskContext), cloneStack, sizeof(TaskContext));//直接把任务上下文拷贝了一份
119 ((TaskContext *)childStack)->R0 = 0;//R0寄存器为0,这个很重要!!! pid = fork() pid == 0 是子进程返回.
120}
121
122/// 用户态运行栈初始化,此时上下文还在内核区
123LITE_OS_SEC_TEXT_INIT VOID OsUserTaskStackInit(TaskContext *context, UINTPTR taskEntry, UINTPTR stack)
124{
125 LOS_ASSERT(context != NULL);
126
127#ifdef LOSCFG_THUMB
128 context->regCPSR = PSR_MODE_USR_THUMB;
129#else
130 context->regCPSR = PSR_MODE_USR_ARM;
131#endif
132 context->R0 = stack;//将用户态栈指针保存到上下文R0处
133 context->USP = TRUNCATE(stack, LOSCFG_STACK_POINT_ALIGN_SIZE);//改变 上下文的SP值,指向用户栈空间
134 context->ULR = 0;//保存子程序返回地址 例如 a call b ,在b中保存 a地址
135 context->PC = (UINTPTR)taskEntry;//入口函数,由外部传入,由上层应用指定,固然每个都不一样.
136}
137///初始化信号上下文
138VOID OsInitSignalContext(const VOID *sp, VOID *signalContext, UINTPTR sigHandler, UINT32 signo, UINT32 param)
139{
140 IrqContext *newSp = (IrqContext *)signalContext;
141 (VOID)memcpy_s(signalContext, sizeof(IrqContext), sp, sizeof(IrqContext));
142 newSp->PC = sigHandler; //信号处理函数
143 newSp->R0 = signo; //sigHandler 参数一
144 newSp->R1 = param; //sigHandler 参数二
145}
146DEPRECATED VOID Dmb(VOID)
147{
148 __asm__ __volatile__ ("dmb" : : : "memory");
149}
150
151DEPRECATED VOID Dsb(VOID)
152{
153 __asm__ __volatile__("dsb" : : : "memory");
154}
155
156DEPRECATED VOID Isb(VOID)
157{
158 __asm__ __volatile__("isb" : : : "memory");
159}
160
161VOID 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
171{
172 arm_clean_cache_range(start, end);
173}
174
176{
177 arm_inv_cache_range(start, end);
178}
179
macro EXC_SP_SET stackSize
Definition: asm.h:50
macro EXC_SP_SET reg1 mrc 获取CPU信息 and mov mul reg0 计算当前CPU栈的偏移位置 ldr reg1 相减得到栈顶 mov sp
Definition: asm.h:57
VOID FlushICache(VOID)
Invalidate instruction cache.
Definition: los_hw.c:161
__attribute__((aligned(MMU_DESCRIPTOR_L1_SMALL_ENTRY_NUMBERS)))
Definition: los_arch_mmu.c:98
LITE_OS_SEC_TEXT_INIT VOID * OsTaskStackInit(UINT32 taskID, UINT32 stackSize, VOID *topStack, BOOL initFlag)
内核态任务运行栈初始化
Definition: los_hw.c:73
VOID OsInitSignalContext(const VOID *sp, VOID *signalContext, UINTPTR sigHandler, UINT32 signo, UINT32 param)
初始化信号上下文
Definition: los_hw.c:138
VOID DCacheInvRange(UINT32 start, UINT32 end)
Definition: los_hw.c:175
LITE_OS_SEC_TEXT_INIT VOID OsTaskExit(VOID)
Definition: los_hw.c:51
CpuVendor g_cpuTable[]
Definition: los_hw.c:37
STATIC VOID OsTaskEntrySetupLoopFrame(UINT32)
Definition: los_hw.c:57
LITE_OS_SEC_TEXT_INIT VOID OsUserTaskStackInit(TaskContext *context, UINTPTR taskEntry, UINTPTR stack)
用户态运行栈初始化,此时上下文还在内核区
Definition: los_hw.c:123
DEPRECATED VOID Dmb(VOID)
Definition: los_hw.c:146
DEPRECATED VOID Dsb(VOID)
Definition: los_hw.c:151
DEPRECATED VOID Isb(VOID)
Definition: los_hw.c:156
LITE_OS_SEC_TEXT VOID OsUserCloneParentStack(VOID *childStack, UINTPTR parentTopOfStack, UINT32 parentStackSize)
把父任务上下文克隆给子任务
Definition: los_hw.c:107
VOID DCacheFlushRange(UINT32 start, UINT32 end)
Definition: los_hw.c:170
UINT64 g_cpuMap[LOSCFG_KERNEL_CORE_NUM]
Definition: los_hw.c:45
void arm_inv_cache_range(UINTPTR start, UINTPTR end)
void arm_clean_cache_range(UINTPTR start, UINTPTR end)
STATIC INLINE LosTaskCB * OsCurrTaskGet(VOID)
VOID OsStackInit(VOID *stacktop, UINT32 stacksize)
task栈的初始化,设置固定的值. 0xcccccccc 和 0xcacacaca
LITE_OS_SEC_TEXT_INIT VOID OsTaskEntry(UINT32 taskID)
Definition: los_task.c:493
long unsigned int UINT64
Definition: los_typedef.h:66
unsigned long UINTPTR
Definition: los_typedef.h:68
unsigned int UINT32
Definition: los_typedef.h:57
size_t BOOL
Definition: los_typedef.h:88
UINT32 R0
Definition: los_hw_pri.h:87
UINT32 PC
Definition: los_hw_pri.h:93
UINT32 R1
Definition: los_hw_pri.h:88
sig_cb sig
信号控制块,用于异步通信,类似于 linux singal模块
UINT32 regCPSR
Definition: los_hw_pri.h:79
UINT32 LR
Definition: los_hw_pri.h:77
UINT64 D[FP_REGS_NUM]
Definition: los_hw_pri.h:54
UINT32 ULR
Definition: los_hw_pri.h:71
UINT32 USP
Definition: los_hw_pri.h:70
UINT32 R0
Definition: los_hw_pri.h:72
UINT32 PC
Definition: los_hw_pri.h:78
UINT32 regFPSCR
Definition: los_hw_pri.h:55
UINT32 regFPEXC
Definition: los_hw_pri.h:56
信号控制块(描述符)
Definition: los_signal.h:220
void * sigContext
信号上下文
Definition: los_signal.h:230