61. Linux内核启动流程简介

一、vmlinux.lds简介

从arch/arm/kernel/vmlinux.lds分析Linux内核第一行启动代码。找到

ENTRY(stext)
入口函数是stext,image和zImage是经过压缩的,Linux内核会先进行解压缩,解压缩完成以后就要运行Linux内核。要求:
1、MMU关闭
2、D cache关闭
3、I cache无所谓
4、r0 = 0。
5、r1 = machine nr
6、r2=atags 或设备树

二、Linux入口stext

__vet_atags 函数验证atags或dtb是否有效,如果使用设备树的话就是dtb。
__create_page_tables 创建页表。
ldr	r13, =__mmap_switched 也就是r13保存__mmap_switched。
__enable_mmu  使能MMU-> __turn_mmu_on->_mmap_switched->start_kernel  启动内核。
Linux内核第一阶段。

三、__mmap_switched函数

四、start_kernel函数

Linux内核第二阶段

/linux/linux-imx-rel_imx_4.1.15_2.1.0_ga/init/main.c

asmlinkage __visible void __init start_kernel(void)
{char *command_line;char *after_dashes;/** Need to run as early as possible, to initialize the* lockdep hash:*/lockdep_init();set_task_stack_end_magic(&init_task);smp_setup_processor_id();debug_objects_early_init();/** Set up the the initial canary ASAP:*/boot_init_stack_canary();cgroup_init_early();local_irq_disable();early_boot_irqs_disabled = true;/** Interrupts are still disabled. Do necessary setups, then* enable them*/boot_cpu_init();page_address_init();pr_notice("%s", linux_banner);setup_arch(&command_line);mm_init_cpumask(&init_mm);setup_command_line(command_line);setup_nr_cpu_ids();setup_per_cpu_areas();smp_prepare_boot_cpu(); /* arch-specific boot-cpu hooks */build_all_zonelists(NULL, NULL);page_alloc_init();pr_notice("Kernel command line: %s\n", boot_command_line);parse_early_param();after_dashes = parse_args("Booting kernel",static_command_line, __start___param,__stop___param - __start___param,-1, -1, &unknown_bootoption);if (!IS_ERR_OR_NULL(after_dashes))parse_args("Setting init args", after_dashes, NULL, 0, -1, -1,set_init_arg);jump_label_init();/** These use large bootmem allocations and must precede* kmem_cache_init()*/setup_log_buf(0);pidhash_init();vfs_caches_init_early();sort_main_extable();trap_init();mm_init();/** Set up the scheduler prior starting any interrupts (such as the* timer interrupt). Full topology setup happens at smp_init()* time - but meanwhile we still have a functioning scheduler.*/sched_init();/** Disable preemption - early bootup scheduling is extremely* fragile until we cpu_idle() for the first time.*/preempt_disable();if (WARN(!irqs_disabled(),"Interrupts were enabled *very* early, fixing it\n"))local_irq_disable();idr_init_cache();rcu_init();/* trace_printk() and trace points may be used after this */trace_init();context_tracking_init();radix_tree_init();/* init some links before init_ISA_irqs() */early_irq_init();init_IRQ();tick_init();rcu_init_nohz();init_timers();hrtimers_init();softirq_init();timekeeping_init();time_init();sched_clock_postinit();perf_event_init();profile_init();call_function_init();WARN(!irqs_disabled(), "Interrupts were enabled early\n");early_boot_irqs_disabled = false;local_irq_enable();kmem_cache_init_late();/** HACK ALERT! This is early. We're enabling the console before* we've done PCI setups etc, and console_init() must be aware of* this. But we do want output early, in case something goes wrong.*/console_init();if (panic_later)panic("Too many boot %s vars at `%s'", panic_later,panic_param);lockdep_info();/** Need to run this when irqs are enabled, because it wants* to self-test [hard/soft]-irqs on/off lock inversion bugs* too:*/locking_selftest();#ifdef CONFIG_BLK_DEV_INITRDif (initrd_start && !initrd_below_start_ok &&page_to_pfn(virt_to_page((void *)initrd_start)) < min_low_pfn) {pr_crit("initrd overwritten (0x%08lx < 0x%08lx) - disabling it.\n",page_to_pfn(virt_to_page((void *)initrd_start)),min_low_pfn);initrd_start = 0;}
#endifpage_ext_init();debug_objects_mem_init();kmemleak_init();setup_per_cpu_pageset();numa_policy_init();if (late_time_init)late_time_init();sched_clock_init();calibrate_delay();pidmap_init();anon_vma_init();acpi_early_init();
#ifdef CONFIG_X86if (efi_enabled(EFI_RUNTIME_SERVICES))efi_enter_virtual_mode();
#endif
#ifdef CONFIG_X86_ESPFIX64/* Should be run before the first non-init thread is created */init_espfix_bsp();
#endifthread_info_cache_init();cred_init();fork_init();proc_caches_init();buffer_init();key_init();security_init();dbg_late_init();vfs_caches_init(totalram_pages);signals_init();/* rootfs populating might need page-writeback */page_writeback_init();proc_root_init();nsfs_init();cpuset_init();cgroup_init();taskstats_init_early();delayacct_init();check_bugs();acpi_subsystem_init();sfi_init_late();if (efi_enabled(EFI_RUNTIME_SERVICES)) {efi_late_init();efi_free_boot_services();}ftrace_init();/* Do the rest non-__init'ed, we're now alive */rest_init();
}
start_kernel-> rest_init-> kernel_thread(kernel_init, NULL, CLONE_FS);  创建kernel_init进程。也就是
init进程,PID=1-> kernel_thread(kthreadd, NULL, CLONE_FS | CLONE_FILES);  创建kthreadd
进程,进程PID为2。-> cpu_startup_entry 进入空闲进程,也就是主进程退化为空闲进程,idle。
启动开发板,输入:ps -A,列出当前系统所有进程。

五、reset_init函数

/linux/linux-imx-rel_imx_4.1.15_2.1.0_ga/init/main.c

static noinline void __init_refok rest_init(void)
{int pid;rcu_scheduler_starting();smpboot_thread_init();/** We need to spawn init first so that it obtains pid 1, however* the init task will end up wanting to create kthreads, which, if* we schedule it before we create kthreadd, will OOPS.*/kernel_thread(kernel_init, NULL, CLONE_FS);numa_default_policy();pid = kernel_thread(kthreadd, NULL, CLONE_FS | CLONE_FILES);rcu_read_lock();kthreadd_task = find_task_by_pid_ns(pid, &init_pid_ns);rcu_read_unlock();complete(&kthreadd_done);/** The boot idle thread must execute schedule()* at least once to get things moving:*/init_idle_bootup_task(current);schedule_preempt_disabled();/* Call into cpu_idle with preempt disabled */cpu_startup_entry(CPUHP_ONLINE);
}

六、init进程

	kernel_init -> kernel_init_freeable-> 设置标准输入、标准输出、标准错误使用console,比如ttymxc0-> ramdisk_execute_command = "/init";-> 检查/init是否存在,存在的话就运行。-> uboot传递给Linux内核的bootargs可以自定init=xxx,或者叫命令行参数。 
有一些开发板会设置init=linuxrc。-> 试着运行/sbin/init。-> /etc/init-> /bin/init-> /bin/sh
可以看出,最终引出根文件系统。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/894882.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

汽车智能座舱的技术演进与用户体验重构 —— 基于多模态交互与 AI 融合的范式创新

摘要&#xff1a; 汽车智能座舱作为人 - 车 - 环境交互的核心载体&#xff0c;正经历从功能驱动到体验驱动的范式变革。本文通过技术解构与用户行为分析&#xff0c;深入揭示智能座舱在异构计算、多模态感知、服务生态等维度的创新路径。研究表明&#xff0c;智能座舱的竞争焦…

使用 Let‘s Encrypt 和 OpenResty 实现域名转发与 SSL 配置

在搭建网站或服务时&#xff0c;确保域名的安全性和正确的流量转发是非常重要的。本文将介绍如何使用 Let’s Encrypt 获取免费的 SSL 证书&#xff0c;并将其配置到 OpenResty 中&#xff0c;同时实现特定的域名转发规则。这不仅可以提升网站的安全性&#xff0c;还能优化流量…

SpringBoot3整合Swagger3时出现Type javax.servlet.http.HttpServletRequest not present错误

目录 错误详情 错误原因 解决方法 引入依赖 修改配置信息 创建文件 访问 错误详情 错误原因 SpringBoot3和Swagger3版本不匹配 解决方法 使用springdoc替代springfox&#xff0c;具体步骤如下&#xff1a; 引入依赖 在pom.xml文件中添加如下依赖&#xff1a; <…

ChatGPT提问技巧:行业热门应用提示词案例-文案写作

ChatGPT 作为强大的 AI 语言模型&#xff0c;已经成为文案写作的得力助手。但要让它写出真正符合你需求的文案&#xff0c;关键在于如何与它“沟通”&#xff0c;也就是如何设计提示词&#xff08;Prompt&#xff09;。以下是一些实用的提示词案例&#xff0c;帮助你解锁 ChatG…

供排水水工公司开展企业获得用水营商环境满意度调查

为了持续提升企业的供水服务品质&#xff0c;进一步优化当地的营商环境&#xff0c;深圳市供排水公司水工公司紧密结合其实际工作情况&#xff0c;特别委托民安智库开展了2023年度优化营商环境调查专项工作。该项目的核心目的是深入了解并评估市各类获得用水企业的用水环境满意…

【Elasticsearch】分桶聚合功能概述

这些聚合功能可以根据它们的作用和应用场景分为几大类&#xff0c;以下是分类后的结果&#xff1a; 1.基础聚合&#xff08;Basic Aggregations&#xff09; • Terms&#xff08;字段聚合&#xff09; 根据字段值对数据进行分组并统计。 例子&#xff1a;按产品类别统计销…

mysql的cpu使用率100%问题排查

背景 线上mysql服务器经常性出现cpu使用率100%的告警&#xff0c; 因此整理一下排查该问题的常规流程。 1. 确认CPU占用来源 检查系统进程 使用 top 或 htop 命令&#xff0c;确认是否是 mysqld 进程导致CPU满载&#xff1a;top -c -p $(pgrep mysqld)2. 实时分析MySQL活动 …

人工智能赋能企业系统架构设计:以ERP与CRM系统为例

一、引言 1.1 研究背景与意义 在数字化时代&#xff0c;信息技术飞速发展&#xff0c;人工智能&#xff08;Artificial Intelligence, AI&#xff09;作为一项具有变革性的技术&#xff0c;正深刻地影响着各个领域。近年来&#xff0c;AI 在技术上取得了显著突破&#xff0c;…

使用jmeter进行压力测试

使用jmeter进行压力测试 jmeter安装 官网安装包下载&#xff0c;选择二进制文件&#xff0c;解压。 tar -xzvf apache-jmeter-x.tgz依赖jdk安装。 yum install java-1.8.0-openjdk环境变量配置&#xff0c;修改/etc/profile文件&#xff0c;添加以下内容。 export JMETER/…

深入理解流(Streams)—— 声明式数据处理的艺术

1. 引言 大家好&#xff01;欢迎来到本系列博客的第三篇。在前两篇文章中&#xff0c;我们已经领略了 Java 8 中 行为参数化 和 Lambda 表达式 的魅力。 在第 1 章 Java行为参数化&#xff1a;从啰嗦到简洁的代码进化中&#xff0c;我们了解到如何通过将行为&#xff08;代码…

【Linux】之【Get√】nmcli device wifi list 与 wpa_cli scan 和 wpa_cli scan_result 区别

nmcli device wifi list 是 NetworkManager 的命令行工具 nmcli 的一部分&#xff0c;它用于列出当前可用的无线网络。它的作用和 wpa_cli 的扫描功能类似&#xff0c;但有一些不同点。 1. nmcli device wifi list 功能&#xff1a; nmcli device wifi list 命令用于显示当前…

【蓝桥杯嵌入式】6_定时器输入捕获

全部代码网盘自取 链接&#xff1a;https://pan.baidu.com/s/1PX2NCQxnADxYBQx5CsOgPA?pwd3ii2 提取码&#xff1a;3ii2 这是两个信号发生器&#xff0c;可以通过调节板上的两个电位器R39和R40调节输出频率。 将PB4、PA15选择ch1&#xff0c;两个信号发生器只能选择TIM3和TIM…

详解SQLAlchemy的函数relationship

在 SQLAlchemy 中&#xff0c;relationship 是一个非常重要的函数&#xff0c;用于定义模型之间的关系。它用于在 ORM 层面上表示数据库表之间的关联关系&#xff08;如 1 对 1、1 对多和多对多&#xff09;。relationship 的主要作用是提供一个高级接口&#xff0c;用于在模型…

分桶函数的使用

除了 NTILE 函数&#xff0c;SQL 中还有其他一些与 分桶&#xff08;bucketization&#xff09;相关的函数&#xff0c;虽然它们的实现方式不同&#xff0c;但都涉及将数据分成多个区间或组。以下是一些常用的分桶函数&#xff1a; 1. CASE 语句 虽然 CASE 不是开窗函数&…

iOS 音频录制、播放与格式转换

iOS 音频录制、播放与格式转换:基于 AVFoundation 和 FFmpegKit 的实现 在 iOS 开发中,音频处理是一个非常常见的需求,比如录音、播放音频、音频格式转换等。本文将详细解读一段基于 AVFoundation 和 FFmpegKit 的代码,展示如何实现音频录制、播放以及 PCM 和 AAC 格式之间…

数据结构与算法(test1)

一、树和二叉树 1. 看图&#xff0c;完成以下填空 (1).树的度为________。 (2).树中结点的最大层次&#xff0c;称为树的_____或树的______&#xff0c;值是______。 (3).结点A和B的度分别为________ 和 ________。 (4).结点A是结点B的________。 (5).结点B是结点A的________…

新版AndroidStudio 修改 jdk版本

一、问题 之前&#xff0c;在安卓项目中配置JDK和Gradle的过程非常直观&#xff0c;只需要进入Android Studio的File菜单中的Project Structure即可进行设置&#xff0c;十分方便。 如下图可以在这修改JDK: 但是升级AndroidStudio之后&#xff0c;比如我升级到了Android Stu…

litemall,又一个小商场系统

litemall Spring Boot后端 Vue管理员前端 微信小程序用户前端 Vue用户移动端 代码地址&#xff1a;litemall: 又一个小商城。 litemall Spring Boot后端 Vue管理员前端 微信小程序用户前端 Vue用户移动端

cursor 开发java项目教程简单上手

1.官网下载 Cursor - The AI Code Editor 下载完后注册账号&#xff0c;可以使用无限邮的方式 注册完之后 设置中文 可以选择设置为中文 Ctrl Shift X 进入设置页面输入chinese 然后重启 更改jdk跟maven仓库设置 ctrlshiftp 打开输入框后输入json&#xff0c;把下面代码…

安装和使用 Ollama(实验环境windows)

下载安装 下载 https://ollama.com/download/windows 安装 Windows 安装 如果直接双击 OllamaSetup.exe 安装&#xff0c;默认会安装到 C 盘&#xff0c;如果需要指定安装目录&#xff0c;需要通过命令行指定安装地址&#xff0c;如下&#xff1a; # 切换到安装目录 C:\Use…