更新日期: 2022/06/01 来源: https://gitee.com/weharmony/kernel_liteos_a_note
los_config.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_config.h"
33#include "stdio.h"
34#include "string.h"
35#include "gic_common.h"
36#include "los_atomic.h"
37#include "los_exc_pri.h"
38#include "los_hwi_pri.h"
39#include "los_hw_tick_pri.h"
40#include "los_init_pri.h"
41#include "los_memory_pri.h"
42#include "los_mp.h"
43#include "los_mux_pri.h"
44#include "los_printf.h"
45#include "los_process_pri.h"
46#include "los_queue_pri.h"
47#include "los_sem_pri.h"
48#include "los_spinlock.h"
49#include "los_swtmr_pri.h"
50#include "los_task_pri.h"
51#include "los_sched_pri.h"
52#include "los_tick.h"
53#include "los_vm_boot.h"
54#include "los_smp.h"
55
56STATIC SystemRebootFunc g_rebootHook = NULL;///< 系统重启钩子函数
57/// 设置系统重启钩子函数
59{
60 g_rebootHook = func;
61}
62///获取系统重启钩子函数
64{
65 return g_rebootHook;
66}
67
68LITE_OS_SEC_TEXT_INIT STATIC UINT32 EarliestInit(VOID)
69{
70 /* Must be placed at the beginning of the boot process *///必须放在启动过程的开头
71 OsSetMainTask();//为每个CPU核设置临时主任务
72 OsCurrTaskSet(OsGetMainTask());//设置当前任务
73 OsSchedRunqueueInit();//初始化调度队列
74
75 g_sysClock = OS_SYS_CLOCK; //设置系统时钟
76 g_tickPerSecond = LOSCFG_BASE_CORE_TICK_PER_SECOND; // 设置TICK间隔
77
78 return LOS_OK;
79}
80//硬件早期初始化
81LITE_OS_SEC_TEXT_INIT STATIC UINT32 ArchEarlyInit(VOID)
82{
83 UINT32 ret;
84
85 /* set system counter freq | 设置系统计数器频率*/
86#ifndef LOSCFG_TEE_ENABLE
87 HalClockFreqWrite(OS_SYS_CLOCK); //写寄存器 MCR p15, 0, <Rt>, c14, c0, 0 ; Write Rt to CNTFRQ
88#endif
89
90#ifdef LOSCFG_PLATFORM_HWI
91 OsHwiInit(); // 硬中断初始化
92#endif
93
94 OsExcInit(); // 异常初始化
95
96 ret = OsTickInit(g_sysClock, LOSCFG_BASE_CORE_TICK_PER_SECOND);
97 if (ret != LOS_OK) {
98 PRINT_ERR("OsTickInit error!\n");
99 return ret;
100 }
101
102 return LOS_OK;
103}
104//平台早期初始化
105LITE_OS_SEC_TEXT_INIT STATIC UINT32 PlatformEarlyInit(VOID)
106{
107#if defined(LOSCFG_PLATFORM_UART_WITHOUT_VFS) && defined(LOSCFG_DRIVERS)
108 uart_init(); //初始化串口
109#endif /* LOSCFG_PLATFORM_UART_WITHOUT_VFS */
110
111 return LOS_OK;
112}
113
114LITE_OS_SEC_TEXT_INIT STATIC UINT32 OsIpcInit(VOID)
115{
116 UINT32 ret;
117
118#ifdef LOSCFG_BASE_IPC_SEM
119 ret = OsSemInit();
120 if (ret != LOS_OK) {
121 PRINT_ERR("OsSemInit error\n");
122 return ret;
123 }
124#endif
125
126#ifdef LOSCFG_BASE_IPC_QUEUE
127 ret = OsQueueInit();
128 if (ret != LOS_OK) {
129 PRINT_ERR("OsQueueInit error\n");
130 return ret;
131 }
132#endif
133 return LOS_OK;
134}
135
136LITE_OS_SEC_TEXT_INIT STATIC UINT32 ArchInit(VOID)
137{
138#ifdef LOSCFG_KERNEL_MMU
140#endif
141 return LOS_OK;
142}
143
144LITE_OS_SEC_TEXT_INIT STATIC UINT32 PlatformInit(VOID)
145{
146 return LOS_OK;
147}
148//内核关键模块初始化
149LITE_OS_SEC_TEXT_INIT STATIC UINT32 KModInit(VOID)
150{
151#ifdef LOSCFG_BASE_CORE_SWTMR_ENABLE
152 OsSwtmrInit(); //软件定时器模块 , 软定由专门的 0 级任务实现
153#endif
154 return LOS_OK;
155}
156
157LITE_OS_SEC_TEXT_INIT VOID OsSystemInfo(VOID)
158{
159#ifdef LOSCFG_DEBUG_VERSION
160 const CHAR *buildType = "debug";
161#else
162 const CHAR *buildType = "release";
163#endif /* LOSCFG_DEBUG_VERSION */
164
165 PRINT_RELEASE("\n******************Welcome******************\n\n"
166 "Processor : %s"
167#ifdef LOSCFG_KERNEL_SMP
168 " * %d\n"
169 "Run Mode : SMP\n"
170#else
171 "\n"
172 "Run Mode : UP\n"
173#endif
174 "GIC Rev : %s\n"
175 "build time : %s %s\n"
176 "Kernel : %s %d.%d.%d.%d/%s\n"
177 "\n*******************************************\n",
178 LOS_CpuInfo(),
179#ifdef LOSCFG_KERNEL_SMP
180 LOSCFG_KERNEL_SMP_CORE_NUM,
181#endif
182 HalIrqVersion(), __DATE__, __TIME__,\
183 KERNEL_NAME, KERNEL_MAJOR, KERNEL_MINOR, KERNEL_PATCH, KERNEL_ITRE, buildType);
184}
185///由汇编调用,鸿蒙C语言层级的入口点
186LITE_OS_SEC_TEXT_INIT UINT32 OsMain(VOID)
187{
188 UINT32 ret;
189#ifdef LOS_INIT_STATISTICS
190 UINT64 startNsec, endNsec, durationUsec;
191#endif
192
193 ret = EarliestInit();//鸿蒙初开,天地混沌
194 if (ret != LOS_OK) {
195 return ret;
196 }
197 OsInitCall(LOS_INIT_LEVEL_EARLIEST);
198
199 ret = ArchEarlyInit(); //架构级初始化,包括硬中断
200 if (ret != LOS_OK) {
201 return ret;
202 }
203 OsInitCall(LOS_INIT_LEVEL_ARCH_EARLY);
204
205 ret = PlatformEarlyInit();//平台级初始化
206 if (ret != LOS_OK) {
207 return ret;
208 }
209 OsInitCall(LOS_INIT_LEVEL_PLATFORM_EARLY);
210
211 /* system and chip info */
212 OsSystemInfo();
213
214 PRINT_RELEASE("\nmain core booting up...\n");
215
216#ifdef LOS_INIT_STATISTICS
217 startNsec = LOS_CurrNanosec();
218#endif
219
220 ret = OsTaskInit();//任务池初始化
221 if (ret != LOS_OK) {
222 return ret;
223 }
224
225 OsInitCall(LOS_INIT_LEVEL_KMOD_PREVM);
226
227 ret = OsSysMemInit();//系统内存初始化
228 if (ret != LOS_OK) {
229 return ret;
230 }
231
232 OsInitCall(LOS_INIT_LEVEL_VM_COMPLETE);
233
234 ret = OsIpcInit();//进程间通讯模块初始化
235 if (ret != LOS_OK) {
236 return ret;
237 }
238
239 ret = OsSystemProcessCreate();//创建系统进程
240 if (ret != LOS_OK) {
241 return ret;
242 }
243
244 ret = ArchInit(); //MMU架构初始化
245 if (ret != LOS_OK) {
246 return ret;
247 }
248 OsInitCall(LOS_INIT_LEVEL_ARCH);
249
250 ret = PlatformInit();
251 if (ret != LOS_OK) {
252 return ret;
253 }
254 OsInitCall(LOS_INIT_LEVEL_PLATFORM);
255
256 ret = KModInit();
257 if (ret != LOS_OK) {
258 return ret;
259 }
260
261 OsInitCall(LOS_INIT_LEVEL_KMOD_BASIC);
262
263 OsInitCall(LOS_INIT_LEVEL_KMOD_EXTENDED);
264
265#ifdef LOSCFG_KERNEL_SMP
266 OsSmpInit();
267#endif
268
269 OsInitCall(LOS_INIT_LEVEL_KMOD_TASK);
270
271#ifdef LOS_INIT_STATISTICS
272 endNsec = LOS_CurrNanosec();
273 durationUsec = (endNsec - startNsec) / OS_SYS_NS_PER_US;
274 PRINTK("The main core takes %lluus to start.\n", durationUsec);
275#endif
276
277 return LOS_OK;
278}
279
280#ifndef LOSCFG_PLATFORM_ADAPT
281STATIC VOID SystemInit(VOID)
282{
283 PRINTK("dummy: *** %s ***\n", __FUNCTION__);
284}
285#else
286extern VOID SystemInit(VOID);
287#endif
288#ifndef LOSCFG_ENABLE_KERNEL_TEST
289///创建系统初始任务并申请调度
291{
292 UINT32 taskID;
293 TSK_INIT_PARAM_S sysTask;
294
295 (VOID)memset_s(&sysTask, sizeof(TSK_INIT_PARAM_S), 0, sizeof(TSK_INIT_PARAM_S));
297 sysTask.uwStackSize = LOSCFG_BASE_CORE_TSK_DEFAULT_STACK_SIZE;
298 sysTask.pcName = "SystemInit";
299 sysTask.usTaskPrio = LOSCFG_BASE_CORE_TSK_DEFAULT_PRIO;
300 sysTask.uwResved = LOS_TASK_STATUS_DETACHED;
301#ifdef LOSCFG_KERNEL_SMP
302 sysTask.usCpuAffiMask = CPUID_TO_AFFI_MASK(ArchCurrCpuid());
303#endif
304 return LOS_TaskCreate(&taskID, &sysTask);
305}
306
307//系统任务初始化
309{
310 UINT32 ret;
311
313 if (ret != LOS_OK) {
314 return ret;
315 }
316
317 return 0;
318}
319
320LOS_MODULE_INIT(OsSystemInit, LOS_INIT_LEVEL_KMOD_TASK);//模块初始化
321#endif
GIC(Generic Interrupt Controller)是ARM公司提供的一个通用的中断控制器 http://weharmonyos.com/blog/44....
CHAR * HalIrqVersion(VOID)
Definition: gic_v2.c:179
LITE_OS_SEC_DATA_INIT UINT32 g_tickPerSecond
每秒Tick数,鸿蒙默认是每秒100次,即:10ms
Definition: los_tick.c:41
LITE_OS_SEC_DATA_INIT UINT32 g_sysClock
系统时钟,是绝大部分部件工作的时钟源,也是其他所有外设的时钟的来源
Definition: los_tick.c:40
STATIC INLINE const CHAR * LOS_CpuInfo(VOID)
Get cpu core name.
Definition: los_hw.h:168
struct tagTskInitParam TSK_INIT_PARAM_S
LITE_OS_SEC_TEXT_INIT UINT32 LOS_TaskCreate(UINT32 *taskID, TSK_INIT_PARAM_S *initParam)
创建任务,并使该任务进入ready状态,如果就绪队列中没有更高优先级的任务,则运行该任务
Definition: los_task.c:718
VOID *(* TSK_ENTRY_FUNC)(UINTPTR param1, UINTPTR param2, UINTPTR param3, UINTPTR param4)
Define the type of a task entrance function.
Definition: los_task.h:480
LITE_OS_SEC_TEXT_MINOR UINT64 LOS_CurrNanosec(VOID)
获取自系统启动以来的纳秒数
Definition: los_hw_tick.c:62
VOID HalClockFreqWrite(UINT32 freq)
VOID OsArchMmuInitPerCPU(VOID)
原子操作 http://weharmonyos.com/openharmony/zh-cn/device-dev/kernel/kernel-small-basic-atomic....
LITE_OS_SEC_TEXT_INIT UINT32 OsMain(VOID)
由汇编调用,鸿蒙C语言层级的入口点
Definition: los_config.c:186
SystemRebootFunc OsGetRebootHook(VOID)
获取系统重启钩子函数
Definition: los_config.c:63
LITE_OS_SEC_TEXT_INIT VOID OsSystemInfo(VOID)
Definition: los_config.c:157
STATIC VOID SystemInit(VOID)
Definition: los_config.c:281
STATIC UINT32 OsSystemInitTaskCreate(VOID)
创建系统初始任务并申请调度
Definition: los_config.c:290
STATIC UINT32 OsSystemInit(VOID)
Definition: los_config.c:308
VOID OsSetRebootHook(SystemRebootFunc func)
设置系统重启钩子函数
Definition: los_config.c:58
LITE_OS_SEC_TEXT_INIT STATIC UINT32 ArchInit(VOID)
Definition: los_config.c:136
LITE_OS_SEC_TEXT_INIT STATIC UINT32 PlatformEarlyInit(VOID)
Definition: los_config.c:105
LITE_OS_SEC_TEXT_INIT STATIC UINT32 OsIpcInit(VOID)
Definition: los_config.c:114
LITE_OS_SEC_TEXT_INIT STATIC UINT32 KModInit(VOID)
Definition: los_config.c:149
LITE_OS_SEC_TEXT_INIT STATIC UINT32 PlatformInit(VOID)
Definition: los_config.c:144
LITE_OS_SEC_TEXT_INIT STATIC UINT32 ArchEarlyInit(VOID)
Definition: los_config.c:81
LOS_MODULE_INIT(OsSystemInit, LOS_INIT_LEVEL_KMOD_TASK)
LITE_OS_SEC_TEXT_INIT STATIC UINT32 EarliestInit(VOID)
Definition: los_config.c:68
STATIC SystemRebootFunc g_rebootHook
Definition: los_config.c:56
VOID(* SystemRebootFunc)(VOID)
Definition: los_config.h:475
VOID OsExcInit(VOID)
异常接管模块的初始化
Definition: los_exc.c:937
STATIC INLINE UINT32 ArchCurrCpuid(VOID)
Definition: los_hw_cpu.h:168
LITE_OS_SEC_TEXT_INIT UINT32 OsTickInit(UINT32 systemClock, UINT32 tickPerSecond)
节拍初始化
Definition: los_hw_tick.c:37
VOID OsHwiInit(VOID)
Definition: los_hwi.c:401
VOID OsInitCall(const UINT32 level)
初始化调用日志打印,这个函数的功能主要是为了记录某个步骤的耗时.在一个函数前后各调用一次就知道函数的执行情况. 这为开机调试提供了很重要的日志依据.
Definition: los_init.c:127
LITE_OS_SEC_TEXT_INIT UINT32 OsSystemProcessCreate(VOID)
Definition: los_process.c:896
UINT32 OsQueueInit(VOID)
Definition: los_queue.c:105
VOID OsSchedRunqueueInit(VOID)
Definition: los_sched.c:194
STATIC INLINE VOID OsCurrTaskSet(LosTaskCB *task)
注意任务地址由硬件保存,见于 CP15 | TPIDRPRW
UINT32 OsSemInit(VOID)
Definition: los_sem.c:103
VOID OsSmpInit(VOID)
Definition: los_smp.c:63
LITE_OS_SEC_TEXT_INIT UINT32 OsSwtmrInit(VOID)
Definition: los_swtmr.c:441
LosTaskCB * OsGetMainTask()
Definition: los_task.c:162
LITE_OS_SEC_TEXT_INIT UINT32 OsTaskInit(VOID)
Definition: los_task.c:259
VOID OsSetMainTask()
Definition: los_task.c:167
long unsigned int UINT64
Definition: los_typedef.h:66
unsigned int UINT32
Definition: los_typedef.h:57
char CHAR
Definition: los_typedef.h:63
UINT32 OsSysMemInit(VOID)
整个系统内存初始化
Definition: los_vm_boot.c:65
UINT16 usTaskPrio
Definition: los_task.h:505
UINT16 usCpuAffiMask
Definition: los_task.h:511
UINT32 uwStackSize
Definition: los_task.h:508
CHAR * pcName
Definition: los_task.h:509
TSK_ENTRY_FUNC pfnTaskEntry
Definition: los_task.h:504
UINT32 uwResved
Definition: los_task.h:513