[笔记]U-boot下从从核启动流程学习(linux内核)
1:前言
最近对u-boot下的代码以及linux下的dts代码进行了查看与学习,在项目中使用了8核arm处理器,为了实现如何从从核启动linux而非主核启动,进行了部分总结如下:现在拟将8核分为0-3与4-7核分别交给不同的操作系统,例如我用0-3核进行国产操作系统的启动,而同时4-7核进行linux的启动,为了实现这个操作我会现在linux的dts文件中关闭一些不需要的外设用来节省内存,并且将核0-3设置为 “status = disabled”进行失能操作,以确保linux使用的是4-7核,具体实现通过下文展示。
本篇笔记是通过学习
1:《ARMv8 Cortex-a 编程向导手册学习_7.AArch64 异常处理_aarch64 arm文档-CSDN博客》
所做的笔记
2:Boot启动linux内核流程
1:说在前面:
在某些特定场景中,常需将多核处理器划分为不同集群以运行异构操作系统。例如,在安全敏感领域,国产实时操作系统负责关键任务,而Linux处理通用计算。本文基于8核ARMv8处理器,探讨如何将核0-3分配给国产OS,核4-7运行Linux,并实现从从核(如核4)启动Linux的完整流程。
技术挑战
-
核隔离:需通过DTS配置禁用核0-3,避免Linux调度器误触。
-
从核启动:传统U-Boot默认从主核(核0)启动,需修改启动流程实现从核跳转。
-
资源预留:关闭Linux侧非必要外设(如GPU/PCIe),防止内存冲突。
修改后的DTS关键片段

2:Boot启动Linux内核流程
与传统启动流程的差异
| 步骤 | 传统流程 | 多核隔离启动 |
|---|---|---|
| 核初始化 | 所有核由主核唤醒 | 仅核4-7由U-Boot激活 |
| DTB传递 | 主核直接加载DTB | 需动态修改DTB的cpu-map节点 |
| 异常级别切换 | 主核从EL3跳转EL2 | 从核需绕过主核锁机制 |
3:关键代码分析
1):do_boota函数
int do_boota(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
{
struct bootm_info bmi;
int states;
int ret;
/* Consume 'booti' */
argc--; argv++;
bootm_init(&bmi);
if (argc)
bmi.addr_img = argv[0];
if (argc > 1)
bmi.conf_ramdisk = argv[1];
if (argc > 2)
bmi.conf_fdt = argv[2];
bmi.boot_progress = true;
bmi.cmd_name = "booti";
/* do not set up argc and argv[] since nothing uses them */
if (booti_start(&bmi))
return 1;
/*
* We are doing the BOOTM_STATE_LOADOS state ourselves, so must
* disable interrupts ourselves
*/
bootm_disable_interrupts();
images.os.os = IH_OS_LINUX;
if (IS_ENABLED(CONFIG_RISCV_SMODE))
images.os.arch = IH_ARCH_RISCV;
else if (IS_ENABLED(CONFIG_ARM64))
images.os.arch = IH_ARCH_ARM64;
states = BOOTM_STATE_MEASURE | BOOTM_STATE_OS_PREP |
BOOTM_STATE_OS_FAKE_GO | BOOTM_STATE_OS_GO;
if (IS_ENABLED(CONFIG_SYS_BOOT_RAMDISK_HIGH))
states |= BOOTM_STATE_RAMDISK;
ret = bootm_run_states(&bmi, states);
return ret;
}
第一步调用do_boota函数其中关键点有如下几点:
1:images.os.os = IH_OS_LINUX;(告知目前要启动的系统镜像image的是linux)
2:ret = bootm_run_states(&bmi, states); 主要关注status中的BOOTM_STATE_OS_PREP |
BOOTM_STATE_OS_FAKE_GO | BOOTM_STATE_OS_GO;这三个参数
2):booti_start函数
static int booti_start(struct bootm_info *bmi)
{
struct bootm_headers *images = bmi->images;
int ret;
ulong ld;
ulong relocated_addr;
ulong image_size;
uint8_t *temp;
ulong dest;
ulong dest_end;
unsigned long comp_len;
unsigned long decomp_len;
int ctype;
ret = bootm_run_states(bmi, BOOTM_STATE_START);
/* Setup Linux kernel Image entry point */
if (!bmi->addr_img) {
ld = image_load_addr;
debug("* kernel: default image load address = 0x%08lx\n",
image_load_addr);
} else {
ld = hextoul(bmi->addr_img, NULL);
debug("* kernel: cmdline image address = 0x%08lx\n", ld);
}
temp = map_sysmem(ld, 0);
ctype = image_decomp_type(temp, 2);
if (ctype > 0) {
dest = env_get_ulong("kernel_comp_addr_r", 16, 0);
comp_len = env_get_ulong("kernel_comp_size", 16, 0);
if (!dest || !comp_len) {
puts("kernel_comp_addr_r or kernel_comp_size is not provided!\n");
return -EINVAL;
}
if (dest < gd->ram_base || dest > gd->ram_top) {
puts("kernel_comp_addr_r is outside of DRAM range!\n");
return -EINVAL;
}
debug("kernel image compression type %d size = 0x%08lx address = 0x%08lx\n",
ctype, comp_len, (ulong)dest);
decomp_len = comp_len * 10;
ret = image_decomp(ctype, 0, ld, IH_TYPE_KERNEL,
(void *)dest, (void *)ld, comp_len,
decomp_len, &dest_end);
if (ret)
return ret;
/* dest_end contains the uncompressed Image size */
memmove((void *) ld, (void *)dest, dest_end);
}
unmap_sysmem((void *)ld);
ret = booti_setup(ld, &relocated_addr, &image_size, false);
if (ret)
return 1;
/* Handle BOOTM_STATE_LOADOS */
if (relocated_addr != ld) {
printf("Moving Image from 0x%lx to 0x%lx, end=0x%lx\n", ld,
relocated_addr, relocated_addr + image_size);
memmove((void *)relocated_addr, (void *)ld, image_size);
}
images->ep = relocated_addr;
images->os.start = relocated_addr;
images->os.end = relocated_addr + image_size;
lmb_reserve(images->ep, le32_to_cpu(image_size), LMB_NONE);
/*
* Handle the BOOTM_STATE_FINDOTHER state ourselves as we do not
* have a header that provide this informaiton.
*/
if (bootm_find_images(image_load_addr, bmi->conf_ramdisk, bmi->conf_fdt,
relocated_addr, image_size))
return 1;
return 0;
}
该函数作用如下:
1:通过bootm_run_states(bmi, BOOTM_STATE_START)函数执行BOOTM_STATE_START阶段
2:设置ep的image_load_addr;也就是系统的镜像入口
3:booti_setup该函数作用为判断系统镜像文件是否为linux镜像
4:bootm_find_images该函数是去查找对应dtb文件
5:该函数的主要作用是去用于初始化image的相关参数
3):bootm_run_states(该函数非常重要)
int bootm_run_states(struct bootm_info *bmi, int states)
{
struct bootm_headers *images = bmi->images;
boot_os_fn *boot_fn;
ulong iflag = 0;
int ret = 0, need_boot_fn;
images->state |= states;
/*
* Work through the states and see how far we get. We stop on
* any error.
*/
if (states & BOOTM_STATE_START)
ret = bootm_start();
if (!ret && (states & BOOTM_STATE_PRE_LOAD))
ret = bootm_pre_load(bmi->addr_img);
if (!ret && (states & BOOTM_STATE_FINDOS))
ret = bootm_find_os(bmi->cmd_name, bmi->addr_img);
if (!ret && (states & BOOTM_STATE_FINDOTHER)) {
ulong img_addr;
img_addr = bmi->addr_img ? hextoul(bmi->addr_img, NULL)
: image_load_addr;
ret = bootm_find_other(img_addr, bmi->conf_ramdisk,
bmi->conf_fdt);
}
if (IS_ENABLED(CONFIG_MEASURED_BOOT) && !ret &&
(states & BOOTM_STATE_MEASURE))
bootm_measure(images);
/* Load the OS */
if (!ret && (states & BOOTM_STATE_LOADOS)) {
iflag = bootm_disable_interrupts();
ret = bootm_load_os(images, 0);
if (ret && ret != BOOTM_ERR_OVERLAP)
goto err;
else if (ret == BOOTM_ERR_OVERLAP)
ret = 0;
}
/* Relocate the ramdisk */
#ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
if (!ret && (states & BOOTM_STATE_RAMDISK)) {
ulong rd_len = images->rd_end - images->rd_start;
ret = boot_ramdisk_high(images->rd_start, rd_len,
&images->initrd_start,
&images->initrd_end);
if (!ret) {
env_set_hex("initrd_start", images->initrd_start);
env_set_hex("initrd_end", images->initrd_end);
}
}
#endif
#if CONFIG_IS_ENABLED(OF_LIBFDT) && CONFIG_IS_ENABLED(LMB)
if (!ret && (states & BOOTM_STATE_FDT)) {
boot_fdt_add_mem_rsv_regions(images->ft_addr);
ret = boot_relocate_fdt(&images->ft_addr, &images->ft_len);
}
#endif
/* From now on, we need the OS boot function */
if (ret)
return ret;
boot_fn = bootm_os_get_boot_func(images->os.os);
need_boot_fn = states & (BOOTM_STATE_OS_CMDLINE |
BOOTM_STATE_OS_BD_T | BOOTM_STATE_OS_PREP |
BOOTM_STATE_OS_FAKE_GO | BOOTM_STATE_OS_GO);
if (boot_fn == NULL && need_boot_fn) {
if (iflag)
enable_interrupts();
printf("ERROR: booting os '%s' (%d) is not supported\n",
genimg_get_os_name(images->os.os), images->os.os);
bootstage_error(BOOTSTAGE_ID_CHECK_BOOT_OS);
return 1;
}
/* Call various other states that are not generally used */
if (!ret && (states & BOOTM_STATE_OS_CMDLINE))
ret = boot_fn(BOOTM_STATE_OS_CMDLINE, bmi);
if (!ret && (states & BOOTM_STATE_OS_BD_T))
ret = boot_fn(BOOTM_STATE_OS_BD_T, bmi);
if (!ret && (states & BOOTM_STATE_OS_PREP)) {
int flags = 0;
/* For Linux OS do all substitutions at console processing */
if (images->os.os == IH_OS_LINUX)
flags = BOOTM_CL_ALL;
ret = bootm_process_cmdline_env(flags);
if (ret) {
printf("Cmdline setup failed (err=%d)\n", ret);
ret = CMD_RET_FAILURE;
goto err;
}
ret = boot_fn(BOOTM_STATE_OS_PREP, bmi);
}
#ifdef CONFIG_TRACE
/* Pretend to run the OS, then run a user command */
if (!ret && (states & BOOTM_STATE_OS_FAKE_GO)) {
char *cmd_list = env_get("fakegocmd");
ret = boot_selected_os(BOOTM_STATE_OS_FAKE_GO, bmi, boot_fn);
if (!ret && cmd_list)
ret = run_command_list(cmd_list, -1, 0);
}
#endif
/* Check for unsupported subcommand. */
if (ret) {
printf("subcommand failed (err=%d)\n", ret);
return ret;
}
/* Now run the OS! We hope this doesn't return */
if (!ret && (states & BOOTM_STATE_OS_GO))
ret = boot_selected_os(BOOTM_STATE_OS_GO, bmi, boot_fn);
/* Deal with any fallout */
err:
if (iflag)
enable_interrupts();
if (ret == BOOTM_ERR_UNIMPLEMENTED) {
bootstage_error(BOOTSTAGE_ID_DECOMP_UNIMPL);
} else if (ret == BOOTM_ERR_RESET) {
printf("Resetting the board...\n");
reset_cpu();
}
return ret;
}
该函数的关键点如下所示:
1:代码会通过states & BOOTM_STATE_START 根据states的属性进行对应代码段函数
2:

该代码之前的函数并不是很重要,boot_fn的主要功能就是去启动linux的镜像文件,在bootm_os_get_boot_func函数中会去直接调用boot.os功能,该函数中去调用do_bootm_linux函数

并且通过该函数中的boot_jump_linux进行linux的启动,具体修改对不同的处理器处理不同,具体根据芯片手册进行判断,具体修改在函数armv8_switch_to_el2中进行汇编代码修改。
4):从核启动的修改点(提供思路,感兴趣的可以进行验证)
在armv8_switch_to_el2中强制指定从核ID(如核4)作为启动核:
armv8_switch_to_el2:
mrs x0, mpidr_el1
and x0, x0, #0xFF // 提取CPU ID
cmp x0, #4 // 仅允许核4启动
b.eq 1f
wfi // 其他核休眠
1: bl boot_jump_linux // 核4继续执行
3:总结与展望
关键成果
1:成功通过DTS隔离核0-3,Linux仅调度核4-7。
2:修改U-Boot的启动流程,实现从核4启动Linux。
更多推荐



所有评论(0)