《understanding the linux kernel》笔记: 1. linux开机发生了什么?

版权声明:本文为博主原创文章,转载请注明出处,欢迎转载。 https://blog.csdn.net/Dylan_Frank/article/details/82556881

remark:
本笔记来自《understanding the linux kernel》一书,所参考内核是linux 2.6,由于笔者能力有限,对其中内容不负任何责任。

书中这部分附在全书附录中,题目非常的 romantic
分了5个阶段:

  1. A.1. Prehistoric Age: the BIOS
  2. A.2. Ancient Age: the Boot Loader
  3. A.3. Middle Ages: the setup( ) Function
  4. A.4. Renaissance: the startup_32( ) Functions
  5. A.5. Modern Age: the start_kernel( ) Function

the BIOS

开机先执行固化在 ROM中的BIOS 程序(开机按住某个键可进入BIOS),BIOS主要干下面几件事:

  1. 执行硬件检测 POST(开机自检)
  2. 初始化硬件设备
  3. 搜索一个OS 去加载(磁盘的第一个扇区被加载(BootLoader))
  4. jump 0x7c00(BootLoader 启动)

由于历史原因,开机cpu处于实模式(real mode) 下,仅有 1M寻址空间,说以先加载bootloader,在让bootloader去做加载os的操作

bootloader

  1. 加载 kernel的第一个扇区(the first 512 bytes) -> 0x90000,setup()函数的启示地址在kernel 偏移 0x200 的地方,即 0x90200
  2. 加载剩余的内核印象 (IMAGE)到低地址 0x100000或者,0x100000 ,依赖于内核的大小
  3. jump setup

setup

The setup( ) function must initialize the hardware devices in the computer and set up the environment for the execution of the kernel program. Although the BIOS already initialized most hardware devices, Linux does not rely on it, but reinitializes the devices in its own manner to enhance portability and robustness. setup( ) performs essentially the following operations:

具体步骤很多,我简要的列举一下:

  1. 初始化和检查
  2. 开启A20(为切换的32-bit 保护模式做准备)
  3. set GDT/IDT(临时的)
  4. 切换 RM(real mode) -> PM(protected mode)
  5. jump startup_32

the startup_32( )

内核中有两个starup_32程序,第一个,就是setup跳转的那个在 arch/i386/boot/compressed/head.S
它做了这样几件事:

  1. 初始化段寄存器 (segmentation register) & 临时的栈
  2. clear eflags 寄存器
  3. 用0填充内核未初始化的数据,_data和_end 的地方,这个可见第二章 物理内存布局
  4. 调用 decompress_kernel( ) 将内核IMAGE 解压到 0x100000 的地方,(注意,不管之间的内核IMAGE放在,低地址还是高地址,这时都放在,0x100000)
  5. jump 0x100000

0x100000 出的startup_32() 在arch/i386/kernel/head.S 这个函数,主要是为 0号进程(process 0) 建立执行环境,然后跳转到 start_kernel

这里写图片描述

the start_kernel

这个函数主要是完成内核的初始化并执行1号内核线程

这里写图片描述

猜你喜欢

转载自blog.csdn.net/Dylan_Frank/article/details/82556881
今日推荐