LittleKernel学习笔记(2)

上一篇我们简单的扯了扯thread_init_early,这一篇我们看看那个啥?忘了,我到前面的笔记看一看。

看了一下lk_primary_cpu_init_level,不知道这个是啥玩意,名字取得直译一下:LK里面primary主要CPU的

static inline void lk_primary_cpu_init_level(uint start_level, uint stop_level) {

lk_init_level(LK_INIT_FLAG_PRIMARY_CPU, start_level, stop_level);
}

转一圈到了lk_init_level,但是注意,3个参数,第一个参数是一个FLAG,第二个参数是起始level,第三个参数是终止level

逮捕令发布了半天终于抓到了,void lk_init_level(enum lk_init_flags flags, uint start_level, uint stop_level);

第一个参数:恭喜enum lk_init_flags

enum lk_init_flags {
LK_INIT_FLAG_PRIMARY_CPU = 0x1,
LK_INIT_FLAG_SECONDARY_CPUS = 0x2,
LK_INIT_FLAG_ALL_CPUS = LK_INIT_FLAG_PRIMARY_CPU | LK_INIT_FLAG_SECONDARY_CPUS,
LK_INIT_FLAG_CPU_SUSPEND = 0x4,
LK_INIT_FLAG_CPU_RESUME = 0x8,
};

接下来该好好审一审

void lk_init_level(enum lk_init_flags required_flag, uint start_level, uint stop_level) {
    LTRACEF("flags %#x, start_level %#x, stop_level %#x\n",
            required_flag, start_level, stop_level);

    ASSERT(start_level > 0);
    uint last_called_level = start_level - 1;//上一次调用等级是当前等级-1
    const struct lk_init_struct *last = NULL;//构造一个const的结构体表示last
    for (;;) {//哇一个死循环
        /* search for the lowest uncalled hook to call *///备注的意思是搜索等级最低的没有呼叫的钩子去呼叫
        LTRACEF("last %p, last_called_level %#x\n", last, last_called_level);

        const struct lk_init_struct *found = NULL;//定义一个查找,感觉像是搜索函数
        bool seen_last = false;//bool变量。啥意思看到了最后?
        for (const struct lk_init_struct *ptr = &__start_lk_init; ptr != &__stop_lk_init; ptr++) {//整这么花哨,不就是从开始到结束for循环搜索吗?太讨厌了
            LTRACEF("looking at %p (%s) level %#x, flags %#x, seen_last %d\n", ptr, ptr->name, ptr->level, ptr->flags, seen_last);

            if (ptr == last)
                seen_last = true;//找到了上一个,懒成这样,多一个{}不香吗?

            /* reject the easy ones *///拒绝那种easy货色,这个注释谁加的,这么霸气侧漏
            if (!(ptr->flags & required_flag))//当前flags和要求的flag进行与运算,如果不一致,则为false,取反一下则true,那么也就是不同则跳过此次循环查找
                continue;
            if (ptr->level > stop_level)//当前level大于结束时候的level,没有查找必要
                continue;
            if (ptr->level < last_called_level)//当前的level小于上一次调用level,上面说了上一次level是当前level减去1哦,过小的level没有查找必要
                continue;
            if (found && found->level <= ptr->level)//如果已经找到了且已经找到的level小于等于当前level,跳出去
                continue;

            /* keep the lowest one we haven't called yet *///保持最低的一个我们还没有调用
            if (ptr->level >= start_level && ptr->level > last_called_level) {//当前level大于等于起始level且当前level大于上一次的level
                found = ptr;//那么符合条件,当前level就是找到的level
                continue;
            }

            /* if we're at the same level as the last one we called and we've
             * already passed over it this time around, we can mark this one
             * and early terminate the loop.
             *///注释说如果我们和上一次调用不处于相同的level,同时我们已经传了,我们可以标记一下这个同时结束这一次的loop。废话啰嗦。
            if (ptr->level == last_called_level && ptr != last && seen_last) {//当前level等于上一次调用的level且已经看到了上一个,且当前指针不是最后一个
                found = ptr;//此处也收入囊中
                break;
            }
        }
//搜索结束,如果找不到,跳出死循环
        if (!found)
            break;

#if TRACE_INIT
        if (found->level >= EARLIEST_TRACE_LEVEL) {
            printf("INIT: cpu %d, calling hook %p (%s) at level %#x, flags %#x\n",
                   arch_curr_cpu_num(), found->hook, found->name, found->level, found->flags);
        }
#endif
        found->hook(found->level);//更新找到的Hook
        last_called_level = found->level;//更新上一次level就是找到的level
        last = found;//last就是找到的。本质就是用过就扔,当前的就是过时的。
    }
}

 

posted @ 2021-08-30 10:10  zhougong12  阅读(539)  评论(0)    收藏  举报