关于FreeNOS使用grub引导启动遵守Multiboot规范的内核过程分析(三)

FreeNOS使用grub引导启动内核镜像,并且遵守Grub Multiboot规范,此文分析grub启动过程以及简要介绍Grub Multiboot规范。

从iso.py文件中可以看到,制作的boot/boot.iso是与boot/boot.isodesc文件相关的,首先看一下boot/boot.isodesc文件的内容:在这里插入图片描述

可以看到boot.iso最开始包含stage1、stage2_eltorito,这两个文件在grub目录下已经存在,这些文件是与grub的设计有关系,去查阅grub相关资料:
https://www.gnu.org/software/grub/manual/legacy/Making-a-GRUB-bootable-CD_002dROM.html#Making-a-GRUB-bootable-CD_002dROM
找到如下描述:在这里插入图片描述
按照目前使用cd-rom启动方式其实只需要stage2_eltorito以及menu.lst文件就够了,那么我们重点放在stage2_eltorito文件上,暂时先不用去管stage1,我自己试了一下,删除boot.isodesc中的./boot/grub/stage1,重新生成boot.iso,使用qemu启动成功,而且重新生成的boot.iso文件中也确实没有stage1文件:
在这里插入图片描述
关于stage2_eltorito,该网站上也有描述:
在这里插入图片描述
与EI Torito有关:
https://en.wikipedia.org/wiki/El_Torito_(CD-ROM_standard)
有以下关于grub的描述:
在这里插入图片描述
stage2_eltorito能引导启动遵守Multiboot规范的grub,接下来就该关注grub中的Multiboot规范。
在这里插入图片描述
那么在grub将控制权交到32bit系统内核kernel时机器状态是如何呢,在Multiboot规范中找到如下描述:
来自《The Multiboot Specification version 0.6.96》

3.2 Machine state
When the boot loader invokes the 32-bit operating system, the machine must have the
following state:
‘EAX’ Must contain the magic value ‘0x2BADB002’; the presence of this value indicates
to the operating system that it was loaded by a Multiboot-compliant boot loader
(e.g. as opposed to another type of boot loader that the operating system can
also be loaded from).
‘EBX’ Must contain the 32-bit physical address of the Multiboot information structure
provided by the boot loader (see Section 3.3 [Boot information format], page 8).
8 Multiboot Specification version 0.6.96
‘CS’ Must be a 32-bit read/execute code segment with an offset of ‘0’ and a limit of
‘0xFFFFFFFF’. The exact value is undefined.
‘DS’
‘ES’
‘FS’
‘GS’
‘SS’ Must be a 32-bit read/write data segment with an offset of ‘0’ and a limit of
‘0xFFFFFFFF’. The exact values are all undefined.
‘A20 gate’ Must be enabled.
‘CR0’ Bit 31 (PG) must be cleared. Bit 0 (PE) must be set. Other bits are all
undefined.
‘EFLAGS’ Bit 17 (VM) must be cleared. Bit 9 (IF) must be cleared. Other bits are all
undefined.
All other processor registers and flag bits are undefined. This includes, in particular:
‘ESP’ The OS image must create its own stack as soon as it needs one.
‘GDTR’ Even though the segment registers are set up as described above, the ‘GDTR’
may be invalid, so the OS image must not load any segment registers (even just
reloading the same values!) until it sets up its own ‘GDT’.
‘IDTR’ The OS image must leave interrupts disabled until it sets up its own IDT.
However, other machine state should be left by the boot loader in normal working
order, i.e. as initialized by the bios (or DOS, if that’s what the boot loader runs from). In
other words, the operating system should be able to make bios calls and such after being
loaded, as long as it does not overwrite the bios data structures before doing so. Also, the
boot loader must leave the pic programmed with the normal bios/DOS values, even if it
changed them during the switch to 32-bit mode.

按照上述描述也就是说
GRUB向内核传递信息
启动后,在32位内核进入点,机器状态如下:
1.EAX = 0x2BADB002 (魔数)
2. 保存Multiboot信息结构体的32bit物理地址保存在 EBX中(相当于一个指针)。
3. CS指向一个32bit具有可读可执行权限,限长为4G – 1的代码段描述符
4.DS,SS,ES,FS和GS指向具有同可读可写权限,限长为4G – 1的数据段描述符。
5.A20地址线已经打开。
6.CR0 第31位(PG)必须为0。第0位(PE)必须为1。其他位未定义。
7.EFLAGS 第17位(VM)必须为0。第9位(IF)必须为1 。其他位未定义。
8.所有其他的处理器寄存器和标志位未定义。

这包括: ESP 当需要使用堆栈时,OS映象必须自己创建一个。 GDTR 尽管段寄存器像上面那样定义了,GDTR也可能是无效的,所以OS映象决不能载入任何段寄存器(即使是载入相同的值也不行!)直到它设定了自己的GDT。 IDTR OS映象必须在设置完它的IDT之后才能开中断。

在明白转入内核状态之前的以上机器状态有助于分析程序跳转到系统内核,即kerenel目录下的kernel程序,程序执行入口是_start,相关的寄存器状态被设定为给定值。
从程序跳转到_start入口之后,自此开始,就进入了系统内核,也相当于控制权有内核所控制,内核会对指定物理地址Multiboot信息结构体进行相关信息分析,建立内核堆栈以及设置GDT以及IDT,开启页面保护机制等工作。
下节开始正式进入系统内核的分析,也就是FreeNOS-0.0.04/kernel目录下的源码分析。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值