开机代码注释完善

    百图画鸿蒙 + 百文说内核 + 百万注源码  => 挖透鸿蒙内核源码
    鸿蒙研究站 | http://weharmonyos.com (国内)
              | https://weharmony.github.io (国外)
    oschina | https://my.oschina.net/weharmony
    博客园 | https://www.cnblogs.com/weharmony/
    知乎 | https://www.zhihu.com/people/weharmonyos
    csdn | https://blog.csdn.net/kuangyufei
    51cto | https://harmonyos.51cto.com/column/34
    掘金 | https://juejin.cn/user/756888642000808
    公众号 | 鸿蒙研究站 (weharmonyos)
上级 30e9cd60
......@@ -146,12 +146,12 @@ extern "C" {
#define TPIDRPRW CP15_REG(c13, 0, c0, 4) /*! PL1 only Thread ID Register | 仅PL1线程ID寄存器*/
#define MPIDR_CPUID_MASK (0xffU)
/// 获取当前task
/// 获取当前task的地址
STATIC INLINE VOID *ArchCurrTaskGet(VOID)
{
return (VOID *)(UINTPTR)ARM_SYSREG_READ(TPIDRPRW);//读c13寄存器
}
/// 设置当前task ID
/// 向CP15 - > C13 保存当前任务的地址
STATIC INLINE VOID ArchCurrTaskSet(VOID *val)
{
ARM_SYSREG_WRITE(TPIDRPRW, (UINT32)(UINTPTR)val);
......
......@@ -283,7 +283,7 @@ FUNCTION(ArchSpinLock) //非要拿到锁
strexeq r2, r1, [r0]//此时CPU被重新唤醒,尝试令lock->rawLock=1,成功写入则r2=0
cmpeq r2, #0 //再来比较r2是否等于0,如果相等则获取到了锁
bne 1b //如果不相等,继续进入循环
dmb //DMB指令来隔离,以保证缓冲中的数据已经落RAM
dmb //DMB指令来隔离,以保证缓冲中的数据已经落RAM
bx lr //此时是一定拿到锁了,跳回调用ArchSpinLock函数
......@@ -294,7 +294,7 @@ FUNCTION(ArchSpinTrylock) //尝试拿锁
ldrex r0, [r2] //r2 = &lock->rawLock, r0 = lock->rawLock
cmp r0, #0 //r00比较
strexeq r0, r1, [r2] //尝试令lock->rawLock=1,成功写入则r0=0,否则 r0 =1
dmb //数据存储隔离,以保证缓冲中的数据已经落RAM
dmb //数据存储隔离,以保证缓冲中的数据已经落RAM
bx lr //跳回调用ArchSpinLock函数
......@@ -304,7 +304,7 @@ FUNCTION(ArchSpinUnlock) //释放锁
dmb //数据存储隔离,以保证缓冲中的数据已经落实到RAM
str r1, [r0] //lock->rawLock = 0
dsb //数据同步隔离
sev //给各CPU广播事件,唤醒沉睡的CPU
sev //sev为发送事件指令,这种事件指的是CPU核与核之间的事件,广播事件给各个CPU
bx lr //跳回调用ArchSpinLock函数
......@@ -44,7 +44,7 @@ CpuVendor g_cpuTable[] = {
/* logical cpu mapping | cpu 逻辑层映射*/
UINT64 g_cpuMap[LOSCFG_KERNEL_CORE_NUM] = {
[0 ... LOSCFG_KERNEL_CORE_NUM - 1] = (UINT64)(-1) //统一赋值,这个赋值方式还挺别致的.
};
};//此处可查看鸿蒙内核源码分析(编码方式篇) https://my.oschina.net/weharmony/blog/5516143
/* bit[30] is enable FPU */
#define FP_EN (1U << 30)
......
......@@ -117,18 +117,18 @@ OsGetArgsAddr:
reset_vector: //鸿蒙开机代码
/* clear register TPIDRPRW */
mov r0, #0 //r0 = 0
mcr p15, 0, r0, c13, c0, 4 //清除线程标识符寄存器TPIDRPRW0
mcr p15, 0, r0, c13, c0, 4 //复位线程标识符寄存器TPIDRPRW , 不复位将导致系统不能启动
/* do some early cpu setup: i/d cache disable, mmu disabled */
mrc p15, 0, r0, c1, c0, 0 //System Control Register-SCTLR
bic r0, #(1<<12) //不使能指令缓存功能
bic r0, #(1<<2 | 1<<0) //不使能数据和TLB的缓存功能(bit2) | 不使能mmu功能(bit0)
mcr p15, 0, r0, c1, c0, 0
mrc p15, 0, r0, c1, c0, 0 //System Control Register-SCTLR | 读取系统控制寄存器内容
bic r0, #(1<<12) //禁用指令缓存功能
bic r0, #(1<<2 | 1<<0) //禁用数据和TLB的缓存功能(bit2) | mmu功能(bit0)
mcr p15, 0, r0, c1, c0, 0 //写系统控制寄存器
/* enable fpu+neon 一些系统寄存器的操作
| 使能浮点运算(floating point unit) NEON就是一种基于SIMD思想的ARM技术,相比于ARMv6或之前的架构,
NEON结合了64-bit和128-bitSIMD指令集,提供128-bit宽的向量运算(vector operations)*/
#ifndef LOSCFG_TEE_ENABLE //Trusted Execution Environment 可信执行环境
MRC p15, 0, r0, c1, c1, 2 //Non-Secure Access Control Register - NSACR
MRC p15, 0, r0, c1, c1, 2 //非安全模式访问寄存器 (Non-Secure Access Control Register - NSACR)
ORR r0, r0, #0xC00 //使能安全和非安全访问协处理器1011(Coprocessor 1011)
BIC r0, r0, #0xC000 //设置bit150,不会影响修改CPACR.ASEDIS寄存器位(控制Advanced SIMD功能)| bit14 reserved
MCR p15, 0, r0, c1, c1, 2
......@@ -137,22 +137,22 @@ reset_vector: //鸿蒙开机代码
MCR p15, 0, r0, c1, c0, 2
ISB
#endif
MOV r3, #0x40000000 //设置FPEXCEN位来使能FPU
VMSR FPEXC, r3
MOV r3, #0x40000000 //EN, bit[30] 设置FPEXCEN位来使能FPU
VMSR FPEXC, r3 //浮点异常控制寄存器 (Floating-Point Exception Control register | B4.1.57)
/* r11: delta of physical address and virtual address | 内核运行虚拟地址和物理地址之间的差值 */
adr r11, pa_va_offset //获取pa_va_offset变量的当前物理地址
/* adr是伪指令被汇编为sub机器指令;程序链接过程中pa_va_offset标识虚拟地址和adr伪指令的虚拟
* 地址是相对固定的,所以二者偏移量就是确定的offset值;adr伪指令在Rn不为pc时结果就如sub Rn pc #offset,即
* sub R11 pc #offset //pc是当前指令的物理地址
adr r11, pa_va_offset //获取pa_va_offset变量物理地址,由于这时候mmu已经被关闭,所以这个值就表示pa_va_offset变量的物理地址。
/*adr 是一条小范围的地址读取伪指令,它将基于PC的相对偏移的地址值读到目标寄存器中。
*编译源程序时,汇编器首先计算当前PC值(当前指令位置)到exper的距离,然后用一条ADD或者SUB指令替换这条伪指令,
*例如:ADD register,PC,#offset_to_exper 注意,标号exper与指令必须在同一代码段
*/
ldr r0, [r11] //r0 = *r11 获取pa_va_offset变量虚拟地址
sub r11, r11, r0
ldr r0, [r11] //r0 = *r11 获取pa_va_offset变量虚拟地址
sub r11, r11, r0 //物理地址-虚拟地址 = 偏移量 放入r11
mrc p15, 0, r12, c0, c0, 5 /* Multiprocessor Affinity Register-MPIDR */
and r12, r12, #MPIDR_CPUID_MASK
and r12, r12, #MPIDR_CPUID_MASK //掩码过滤
cmp r12, #0 //主控核0判断
bne secondary_cpu_init
bne secondary_cpu_init //初始化CPU次核
/*
* adr是小范围的地址读取伪指令,它将基于PC寄存器相对偏移的地址值读取到寄存器中,
* 例如: 0x00000004 : adr r4, __exception_handlers
......@@ -164,9 +164,9 @@ reset_vector: //鸿蒙开机代码
* 一般也就是一一映射,也就是物理地址和虚拟地址值是一样的,所以可以认为内核加载地址值是物理地址
*/
/* if we need to relocate to proper location or not | 如果需要重新安装到合适的位置*/
adr r4, __exception_handlers /* r4: base of load address */
ldr r5, =SYS_MEM_BASE /* r5: base of physical address */
subs r12, r4, r5 /* r12: delta of load address and physical address */
adr r4, __exception_handlers /* r4: base of load address | 加载基址*/
ldr r5, =SYS_MEM_BASE /* r5: base of physical address | 物理基址*/
subs r12, r4, r5 /* r12: delta of load address and physical address | 二者偏移量*/
beq reloc_img_to_bottom_done /* if we load image at the bottom of physical address | 不相等就需要重定位 */
/* we need to relocate image at the bottom of physical address */
......@@ -223,7 +223,7 @@ init_mmu_done:
rsb r7, r11, r6, lsl #20 /* r7: va */
str r12, [r4, r7, lsr #(20 - 2)] /* jumpTable[vaIndex] = pt entry */
bl mmu_setup /* set up the mmu */
bl mmu_setup /* set up the mmu | 设置MMU*/
#endif
/* clear out the interrupt and exception stack and set magic num to check the overflow */
ldr r0, =__svc_stack //中断栈的底部,从这里可以看出中断栈处于最高位
......
......@@ -40,7 +40,7 @@
#ifdef LOSCFG_KERNEL_SMP
STATIC struct SmpOps *g_smpOps = NULL;
/// 多核中次级CPU核初始化,每个核都会调用一次
STATIC VOID OsSmpSecondaryInit(VOID *arg)
{
UNUSED(arg);
......@@ -49,12 +49,12 @@ STATIC VOID OsSmpSecondaryInit(VOID *arg)
OsSwtmrInit();
#endif
OsIdleTaskCreate();
OsIdleTaskCreate();//每个核都有一个空闲任务
OsInitCall(LOS_INIT_LEVEL_KMOD_TASK);
OsSchedStart();
}
/// 设置多核操作接口, 通过外部注册
VOID LOS_SmpOpsSet(struct SmpOps *ops)
{
g_smpOps = ops;
......
......@@ -461,7 +461,7 @@ STATIC INLINE LosTaskCB *OsCurrTaskGet(VOID)
{
return (LosTaskCB *)ArchCurrTaskGet();
}
/// 注意任务地址由硬件保存,见于 CP15 | TPIDRPRW
STATIC INLINE VOID OsCurrTaskSet(LosTaskCB *task)
{
ArchCurrTaskSet(task);
......
......@@ -307,14 +307,14 @@ VOID OsSchedStart(VOID)
newTask->taskStatus |= OS_TASK_STATUS_RUNNING;
#ifdef LOSCFG_KERNEL_SMP
/*
/* 注意:需要设置当前cpu,以防第一个任务删除失败,因为这个标志与真实的当前cpu不匹配
* attention: current cpu needs to be set, in case first task deletion
* may fail because this flag mismatch with the real current cpu.
*/
newTask->currCpu = cpuid;
newTask->currCpu = cpuid;//记录本次运行交给哪个CPU核
#endif
OsCurrTaskSet((VOID *)newTask);
OsCurrTaskSet((VOID *)newTask);//设置新任务,背后的逻辑是将新任务的地址交给硬件保存
newTask->startTime = OsGetCurrSchedTimeCycle();
......
......@@ -70,10 +70,10 @@ LITE_OS_SEC_TEXT_INIT STATIC UINT32 EarliestInit(VOID)
/* Must be placed at the beginning of the boot process *///必须放在启动过程的开头
OsSetMainTask();//为每个CPU核设置临时主任务
OsCurrTaskSet(OsGetMainTask());//设置当前任务
OsSchedRunqueueInit();
OsSchedRunqueueInit();//初始化调度队列
g_sysClock = OS_SYS_CLOCK;
g_tickPerSecond = LOSCFG_BASE_CORE_TICK_PER_SECOND;
g_sysClock = OS_SYS_CLOCK; //设置系统时钟
g_tickPerSecond = LOSCFG_BASE_CORE_TICK_PER_SECOND; // 设置TICK间隔
return LOS_OK;
}
......@@ -88,10 +88,10 @@ LITE_OS_SEC_TEXT_INIT STATIC UINT32 ArchEarlyInit(VOID)
#endif
#ifdef LOSCFG_PLATFORM_HWI
OsHwiInit();
OsHwiInit(); // 硬中断初始化
#endif
OsExcInit();
OsExcInit(); // 异常初始化
ret = OsTickInit(g_sysClock, LOSCFG_BASE_CORE_TICK_PER_SECOND);
if (ret != LOS_OK) {
......
/*
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _LOS_SMP_H
#define _LOS_SMP_H
#include "smp.h"
#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif /* __cplusplus */
#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif /* __cplusplus */
#endif /* __cplusplus */
VOID LOS_SmpOpsSet(struct SmpOps *ops);
VOID OsSmpInit(VOID);
#ifdef __cplusplus
#if __cplusplus
}
#endif /* __cplusplus */
#endif /* __cplusplus */
#ifdef __cplusplus
#if __cplusplus
}
#endif /* __cplusplus */
#endif /* __cplusplus */
#endif /* _LOS_SMP_H */
......@@ -28,13 +28,6 @@
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "los_vm_zone.h"
#define TEXT_BASE KERNEL_VADDR_BASE //代码区为内核起始地址
OUTPUT_FORMAT("elf32-littlearm", "elf32-littlearm", "elf32-littlearm")
OUTPUT_ARCH(arm)
/***********************************************
本文件经过
python build.py ipcamera_hi3516dv300
......@@ -81,6 +74,14 @@ ATTR :定义该存储区域的属性。ATTR属性内可以出现以下7 个字
ORIGIN :关键字,区域的开始地址,可简写成org o
LENGTH :关键字,区域的大小,可简写成len l
*/
#include "los_vm_zone.h"
#define TEXT_BASE KERNEL_VADDR_BASE //代码区为内核起始地址
OUTPUT_FORMAT("elf32-littlearm", "elf32-littlearm", "elf32-littlearm")
OUTPUT_ARCH(arm)
MEMORY
{ //ram,sram为存储区域的名字,可以随意取
ram : ORIGIN = KERNEL_VADDR_BASE, LENGTH = KERNEL_VADDR_SIZE
......@@ -89,7 +90,7 @@ MEMORY
}
SECTIONS
{
/DISCARD/ : { *(.comment .note) }
/DISCARD/ : { *(.comment .note) } //过滤掉 .comment .note
.ram_vectors TEXT_BASE : {
__ram_vectors_vma = .;
......
git add -A
git commit -m ' 同步官方源码 标准库(musl , newlib) 目录调整
git commit -m ' 开机代码注释完善
百图画鸿蒙 + 百文说内核 + 百万注源码 => 挖透鸿蒙内核源码
鸿蒙研究站 | http://weharmonyos.com (国内)
| https://weharmony.github.io (国外)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册