进入内核源代码目录(一文搞懂内核的启动)
进入内核源代码目录(一文搞懂内核的启动)Linux内核源码/内存调优/文件系统/进程管理/设备驱动/网络协议栈-学习视频教程-腾讯课堂更多linux内核视频教程文档资料免费领取后台私信【内核】自行获取.linux定义的一种初始化过程中才会用到的段,一旦初始化完成,那么这些段所占用的内存会被释放掉,后续会继续说明。Linux启动,会启动内核编译后的文件 vmlinux,vmlinux 是一个 ELF 文件,按照 ./arch/arm64/kernel/vmlinux.lds 设定的规则进行链接,vmlinux.lds 是 vmlinux.lds.S 编译之后生成的。所以为了确定 vmlinux 内核的起始地址, 首先通过 vmlinux.lds.S 链接脚本进行分析。如下所示:$ readelf -h vmlinux ELF Header: Magic: 7f 45 4c 46 02 01 01 00 00 00 00 0
vmLinux 属于 ELF 文件,要想了解如何启动 vmlinux,首先需要知道 ELF 的格式。

- 文本段
 
代码段,通常是指用来存放程序执行代码的一块内存区域。这部分区域的大小在程序运行前就已经确定。
- data段
 
数据段,通常是指用来存放程序中已初始化的全局变量的一块内存区域。数据段属于静态内存分配。
- bss段
 
通常是指用来存放程序中未初始化的全局变量和静态变量的一块内存区域。BSS段属于静态内存分配。
- 初始化段
 
linux定义的一种初始化过程中才会用到的段,一旦初始化完成,那么这些段所占用的内存会被释放掉,后续会继续说明。
vmlinux 入口:第一行运行的代码Linux启动,会启动内核编译后的文件 vmlinux,vmlinux 是一个 ELF 文件,按照 ./arch/arm64/kernel/vmlinux.lds 设定的规则进行链接,vmlinux.lds 是 vmlinux.lds.S 编译之后生成的。所以为了确定 vmlinux 内核的起始地址, 首先通过 vmlinux.lds.S 链接脚本进行分析。如下所示:
$ readelf -h vmlinux
ELF Header:
  Magic:   7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00
  Class:                             ELF64
  Data:                              2's complement  little endian
  Version:                           1 (current)
  OS/ABI:                            UNIX - System V
  ABI Version:                       0
  Type:                              DYN (Shared object File)
  Machine:                           AArch64
  Version:                           0x1
  Entry point address:               0xffff800010000000
  Start of program headers:          64 (bytes into file)
  Start of section headers:          494679672 (bytes into file)
  Flags:                             0x0
  Size of this header:               64 (bytes)
  Size of program headers:           56 (bytes)
  Number of program headers:         5
  Size of section headers:           64 (bytes)
  Number of section headers:         38
  Section header string table index: 37
$ readelf -l vmlinux
Elf file type is DYN (Shared object file)
Entry point 0xffff800010000000
There are 5 program headers  starting at offset 64
Program Headers:
  Type           Offset             VirtAddr           PhysAddr
                 FileSiz            MemSiz              Flags  Align
  LOAD           0x0000000000010000 0xffff800010000000 0xffff800010000000
                 0x0000000001beacdc 0x0000000001beacdc  RWE    10000
  LOAD           0x0000000001c00000 0xffff800011c00000 0xffff800011c00000
                 0x00000000000c899c 0x00000000000c899c  R E    10000
  LOAD           0x0000000001cd0000 0xffff800011cd0000 0xffff800011cd0000
                 0x0000000000876200 0x0000000000905794  RW     10000
  NOTE           0x0000000001bfaca0 0xffff800011beaca0 0xffff800011beaca0
                 0x000000000000003c 0x000000000000003c  R      4
  GNU_STACK      0x0000000000000000 0x0000000000000000 0x0000000000000000
                 0x0000000000000000 0x0000000000000000  RW     10
 Section to Segment mapping:
  Segment Sections...
   00     .head.text .text .got.plt .rodata .pci_fixup __ksymtab __ksymtab_gpl __ksymtab_strings __param __modver __ex_table .notes
   01     .init.text .exit.text .altinstructions
   02     .init.data .data..percpu .hyp.data..percpu .rela.dyn .data __bug_table .mmuoff.data.write .MMUoff.data.read .pecoff_edata_padding .bss
   03     .notes
   04
    
通过上面的查询可知,此 vmlinux 为一个 aarch64 架构平台的 ELF 可执行文件,其程序入口的地址为 0xffff800010000000,此段对应的 section 为.head.text .text .got.plt......,所以 vmlinux 的入口在 .head.text 文本段。
更多linux内核视频教程文档资料免费领取后台私信【内核】自行获取.


Linux内核源码/内存调优/文件系统/进程管理/设备驱动/网络协议栈-学习视频教程-腾讯课堂
.head.text 文本段通过 vmlinux.lds.S 找到 vmlinux 的入口函数。具体分析如下:
/* SPDX-License-Identifier: GPL-2.0 */
/*
 * ld script to make ARM Linux kernel
 * taken from the i386 version by Russell King
 * Written by Martin Mares <mj@atrey.karlin.mff.cuni.cz>
 */
#define RO_EXCEPTION_TABLE_ALIGN        8
#define runtime_DISCARD_EXIT
#include <asm-generic/vmlinux.lds.h>
#include <asm/cache.h>
#include <asm/hyp_image.h>
#include <asm/kernel-pgtable.h>
#include <asm/memory.h>
#include <asm/page.h>
#include "image.h"
OUTPUT_ARCH(aarch64)
ENTRY(_text)
    
根据链接脚本语法,可以知道 OUTPUT_ARCH 关键字指定了链接之后的输出文件的体系结构是 aarch64。ENTRY 关键字指定了输出文件 vmlinux 的入口 地址是 _text, 因此只需找到 _text 的定义就可以知道 vmlinux 的入口函数。接下来的代码是:

- 上图中的宏 HEAD_TEXT 定义在文件 include/asm-generic/vmlinux.lds.S 中,其定义为 .head.text 文本段。
 - 上图中的 idmap_pg_dir,init_pg_dir 是页表映射,idmap_pg_dir 是 identity mapping 用到的页表,init_pg_dir 是 kernel_image_mapping 用到的页表。
 
/* include/asm-generic/vmlinux.lds.h文件 */
#define HEAD_TEXT KEEP(*(.head.text))
/* include/linux/init.h 文件*/
#define __HEAD .section ".head.text" "ax"
    
故转向 arch/arm64/kernel/head.S 中继续执行。
        __HEAD
_head:
        /*
         * DO NOT MODIFY. Image header expected by Linux boot-loaders.
         */
#ifdef CONFIG_EFI
        /*
         * This add instruction has no meaningful effect except that
         * its opcode forms the magic "MZ" signature required by UEFI.
         */
        add     x13  x18  #0x16
        b       primary_entry
#else
        b       primary_entry                   // branch to kernel start  magic
        .long   0                               // reserved
#endif
primary_entry
    
进入正式的初始化流程。
SYM_CODE_START(primary_entry)
        bl      preserve_boot_args
        bl      el2_setup                       // Drop to EL1  w0=cpu_boot_mode
        adrp    x23  __PHYS_OFFSET
        and     x23  x23  MIN_KIMG_ALIGN - 1    // KASLR offset  defaults to 0
        bl      set_cpu_boot_mode_flag
        bl      __create_page_tables
        /*
         * The following calls CPU setup code  see arch/arm64/mm/proc.S for
         * details.
         * On return  the CPU will be ready for the MMU to be turned on and
         * the TCR will have been set.
         */
        bl      __cpu_setup                     // initialise processor
        b       __primary_switch
SYM_CODE_END(primary_entry)
    
preserve_boot_args 是用来保存从 bootloader 传递的参数,使 dcache 失效。
el2_setup 设定 core 启动状态。
set_cpu_boot_mode_flag 设置 core 启动的 EL。
__create_page_tables 创建页表我们知道 idmap_pg_dir 是 identity mapping 用到的页表,init_pg_dir 是 kernel_image_mapping 用到的页表。这里通过 __create_page_tables 来填充这两个页表。
SYM_FUNC_START_LOCAL(__create_page_tables)
 mov x28  lr
  ......
 /*
  * Create the identity mapping.
  */
 adrp x0  idmap_pg_dir
 adrp x3  __idmap_text_start  // __pa(__idmap_text_start)
  ......
  adrp x5  __idmap_text_end
  ......
 /*
  * Map the kernel image (starting with PHYS_OFFSET).
  */
 adrp x0  init_pg_dir
 mov_q x5  KIMAGE_VADDR  // compile time __va(_text)
 add x5  x5  x23   // add KASLR displacement
 mov x4  PTRS_PER_PGD
 adrp x6  _end   // runtime __pa(_end)
 adrp x3  _text   // runtime __pa(_text)
 sub x6  x6  x3   // _end - _text
 add x6  x6  x5   // runtime __va(_end)
  ......
SYM_FUNC_END(__create_page_tables)
    

__cpu_setup 初始话 CPU为开启 MMU 做一些 CPU 的初始化工作。
SYM_FUNC_START(__cpu_setup)
 tlbi vmalle1    // Invalidate local TLB
 dsb nsh
 mov x1  #3 << 20
 msr cpacr_el1  x1   // Enable FP/ASIMD
 mov x1  #1 << 12   // Reset mdscr_el1 and disable
 msr mdscr_el1  x1   // access to the DCC from EL0
 isb     // Unmask debug exceptions now 
 enable_dbg    // since this is per-cpu
 reset_pmuserenr_el0 x1   // Disable PMU access from EL0
 reset_amuserenr_el0 x1   // Disable AMU access from EL0
  
 /*
  * Memory region attributes
  */
 mov_q x5  MAIR_EL1_SET
    
前面做 TLB/FP/ASIMD/DCC/PMU/AMU 的初始化,后面做 Memory region attributes。
__primary_switch 开启切换到虚拟地址,并调用 __primary_switched。
__primary_switched__primary_switched主要完成了如下的工作:
- 为init进程设置好堆栈地址和大小,保存当前进程描述符地址到sp_el0;
 - 设置异常向量表基址寄存器;
 - 保存FDT地址到__fdt_pointer变量;
 - 将kimage的虚拟地址和物理地址的偏移保存到kimage_voffset
 - 清除 bss
 - 跳转到start_kernel
 
用一张图概括:






