linux启动过程浅析(1)

 

没有阅读过源码之前,对操作系统总是抱有神秘感,总是认为操作系统是很奥妙的东西. 其实,对于一个有一定编程基础和系统知识的程序员,操作系统并不是可望而不可及的东西. 操作系统的最终目标只是在应用程序层面与硬件层面之间做的一层协调,可能这个定义有些片面,但本着这样的思想读代码应该会轻松一点.
由于发展到现在,LINUX已经可以支持各种平台,本文讲主要针对一个"古老"的版本--0.11--进行讨论,这也是赵炯博士在"linux内核完全注释"一书中做使用的版本,由于本人也是初学者,不敢妄自对2.6内核作出定论. 我将在以下分析的过程中偶尔加入一些自己对2.6的理解.

linux的启动过程主要涉及到三个源文件: bootsect.s head.s setup.s
在0.11版本中,这三个文件均位于linux/boot/目录下,而在最新版本中,因为支持的平台的多样性,我们可以在linux/arch/i386/下找到它们.
i386体系结构的启动过程大致是这样的:机器首先进入实模式,然后从地址0xFFFF0处开始执行,一般这个地址是在BIOS中,于是BIOS程序会对机器做一些自检工作. 在此之后,一般从磁盘启动的机器会从可启动设备的第一扇区中把启动程序读入内存0x07C00位置,并且跳转到该处继续执行.这里所说到的从磁盘读入的第一扇区内容,其实就是bootsect.s经过汇编以后的二进制码,也就是说,在操作系统意义上系统是从bootsect.s开始的. 在观看bootsect.s源码之前,让我们先看看操作系统得到运行机会以后是如何安排一切的:
Linus对bootsect.s被载入的位置不太满意,事实上这的确不是个良好的位置.0x07C00是一个相当低的位置,Linus期望操作系统的真正内核部分可以从0x00000起始,并且占据整个内存区域的最低位置. boot的程序内容只会在机器启动的时候被运行一次,以后基本就没有用了. 而现在,bootsect.s位于0x07C00的位置上,显然已经"挡住"了操作系统的"路". 要知道,即使是0.11版本的linux,内核的长度(这里应该更准确的称之为system模块,而不是广义上的linux内核. 这个概念在下且跳转到那里执行.文中讲述.)也达到了0x3000, 而在2.6中,更是达到了0x7F00之多. 于是,很自然的,我们首先要做的工作就是把bootsect.s的位置挪开,挪到更高的地址上去,并跳转到那里执行.
做完迁移之后,bootsect.s需要做的第二件事情就是要把后续代码load进来. 这部分代码就是setup.s,它的二进制编码应该存放在启动设备的第二个扇区,长度为4个扇区. load这段程序并没有想象中的那么困难,程序通过int 0x13来完成的. setup.s将被放在紧接在bootsect.s的后面的内存区域中.
现在位置,bootsect.s和setup.s已经被放到了所期望的位置了,然后需要做的就是把操作系统的主要部分,真正的system模块load进来,这部分将被放在内存的最低位上,既0x0000起始的位置上.Linux使用了一种看似比较"绕"的方法:Linux首先将system模块放在0x10000的位置上.而在bootsect.s运行完之后,程序计数器会跳转到setup.s的范围中,此时系统又将system模块往低位处移动,存放到0x00000处.我也是刚接触Linux不久,对此不得其解.因为此时的bootsect已经被移动到了0x90000处了,既是将system模块放在0x00000处应该也不会影响到系统的运行,为什么不直接写到最低位呢?个人猜想时因为系统在BIOS初始话层面中将中断向量表放在了0x00000处,而现阶段此向量表还有可能需要使用.这只是猜测,如果有哪位知晓其中原理,麻烦请将其中真正原因告诉我.我的E-Mail:j.h_zhang@163.com,不甚感激.
到此位置,bootsect.s的任务基本算完成了,0.11版本中,bootsect.s最后还要确定根设备号. 然后就是跳转到SETUPSEG处,既0x9020地址处,运行紧接在bootsect.s二进制码之后的,先前被load进来的setup.s.

以下我们来看一看bootsect.s的源码,看看操作系统最初是怎样的形态.需要注意的是,现在机器还处于实模式之下.其中会有一些我的注释,对于希望观看赵炯博士的详细注释者,请参考"linux内核完全注释".另外,为了便于区分,我加的注释将以两个"!"开头,而程序中原本存在的注释以一个"!"开头:
!
! SYS_SIZE is the number of clicks (16 bytes) to be loaded.
! 0x3000 is 0x30000 bytes = 196kB, more than enough for current
! versions of linux
!
SYSSIZE = 0x3000
!
!bootsect.s(C) 1991 Linus Torvalds
!
! bootsect.s is loaded at 0x7c00 by the bios-startup routines, and moves
! iself out of the way to address 0x90000, and jumps there.
!
! It then loads 'setup' directly after itself (0x90200), and the system
! at 0x10000, using BIOS interrupts.
!
! NOTE! currently system is at most 8*65536 bytes long. This should be no
! problem, even in the future. I want to keep it simple. This 512 kB
! kernel size should be enough, especially as this doesn't contain the
! buffer cache as in minix
!
! The loader has been made as simple as possible, and continuos
! read errors will result in a unbreakable loop. Reboot by hand. It
! loads pretty fast by getting whole sectors at a time whenever possible.

.globl begtext, begdata, begbss, endtext, enddata, endbss
.text
begtext:
.data
begdata:
.bss
begbss:
.text

!!一下定义了一些符号,用来标记系统启动过程中的各种常数
!!比如:
!!BOOTSEG为系统启动时第一条运行的指令地址
!!INITSEG为Linux希望将bootsect.s移动到的地址
!!SETUPSEG为紧接再bootsect.s后存放setup.s的头地址
!!SYSSEG为在bootsect.s运行时,暂时存放system模块的头地址,在setup.s运行时,又会将system移动到0x00000处
!!一下地址是作为基地址存在的,也就是说在实模式下,当用一下地址寻址是,会首先将地址左移4位,然后加上偏移量.
!!如此, "BOOTSEG = 0x07c0"实际上是意味着0x07c00地址.
SETUPLEN = 4! nr of setup-sectors
BOOTSEG = 0x07c0! original address of boot-sector
INITSEG = 0x9000! we move boot here - out of the way
SETUPSEG = 0x9020! setup starts here
SYSSEG = 0x1000! system loaded at 0x10000 (65536).
ENDSEG = SYSSEG + SYSSIZE! where to stop loading

! ROOT_DEV:0x000 - same type of floppy as boot.
!0x301 - first partition on first drive etc
ROOT_DEV = 0x306

entry _start
!!程序的开始,万物的起源. ^_^
_start:
!!一下一段的目的是将bootsect移动到INITSEG(0x90000)处
movax,#BOOTSEG
movds,ax
movax,#INITSEG
moves,ax
movcx,#256
subsi,si
subdi,di
rep
movw
jmpigo,INITSEG!!已经迁移完毕,跳转到高地址处继续运行.
!!INITSEG为基地址.go为偏移量.
!!迁移完以后,相对的更新一些寄存器的值.
go:movax,cs
movds,ax
moves,ax
! put stack at 0x9ff00.
movss,ax
movsp,#0xFF00! arbitrary value >>512

! load the setup-sectors directly after the bootblock.
! Note that 'es' is already set up.

!!从load_setup开始的代码作用是将setup.s读入内存,存在在紧跟bootsect的位置上.
!!使用0x13中断完成此功能.
load_setup:
movdx,#0x0000! drive 0, head 0
movcx,#0x0002! sector 2, track 0
movbx,#0x0200! address = 512, in INITSEG
movax,#0x0200+SETUPLEN! service 2, nr of sectors
int0x13! read it
jncok_load_setup! ok - continue
movdx,#0x0000
movax,#0x0000! reset the diskette
int0x13
jload_setup

!!setup.s已经load完成,读一些关于驱动器的参数.
ok_load_setup:

! Get disk drive parameters, specifically nr of sectors/track

movdl,#0x00
movax,#0x0800! AH=8 is get drive parameters
int0x13
movch,#0x00
seg cs
movsectors,cx
movax,#INITSEG
moves,ax

! Print some inane message

movah,#0x03! read cursor pos
xorbh,bh
int0x10

movcx,#24
movbx,#0x0007! page 0, attribute 7 (normal)
movbp,#msg1
movax,#0x1301! write string, move cursor
int0x10

! ok, we've written the message, now
! we want to load the system (at 0x10000)
!!此处开始是将system模块load到0x10000位置上.

movax,#SYSSEG
moves,ax! segment of 0x010000
callread_it!!就是这个过程调用,read_it具体实现在下面.
callkill_motor

! After that we check which root-device to use. If the device is
! defined (!= 0), nothing is done and the given device is used.
! Otherwise, either /dev/PS0 (2,28) or /dev/at0 (2,8), depending
! on the number of sectors that the BIOS reports currently.
!!此后是检查根设备号.
seg cs
movax,root_dev
cmpax,#0
jneroot_defined
seg cs
movbx,sectors
movax,#0x0208! /dev/ps0 - 1.2Mb
cmpbx,#15
jeroot_defined
movax,#0x021c! /dev/PS0 - 1.44Mb
cmpbx,#18
jeroot_defined
undef_root:
jmp undef_root
root_defined:
seg cs
movroot_dev,ax

! after that (everyting loaded), we jump to
! the setup-routine loaded directly after
! the bootblock:

jmpi0,SETUPSEG!!跳入setup.s,到此为止,bootsect的任务就算完成了.

! This routine loads the system at address 0x10000, making sure
! no 64kB boundaries are crossed. We try to load it as fast as
! possible, loading whole tracks whenever we can.
!
! in:es - starting address segment (normally 0x1000)
!
sread:.word 1+SETUPLEN! sectors read of current track
head:.word 0! current head
track:.word 0! current track

read_it:
mov ax,es
test ax,#0x0fff
die:jne die! es must be at 64kB boundary
xor bx,bx! bx is starting address within segment
rp_read:
mov ax,es
cmp ax,#ENDSEG! have we loaded all yet?
jb ok1_read
ret
ok1_read:
seg cs
mov ax,sectors
sub ax,sread
mov cx,ax
shl cx,#9
add cx,bx
jnc ok2_read
je ok2_read
xor ax,ax
sub ax,bx
shr ax,#9
ok2_read:
call read_track
mov cx,ax
add ax,sread
seg cs
cmp ax,sectors
jne ok3_read
mov ax,#1
sub ax,head
jne ok4_read
inc track
ok4_read:
mov head,ax
xor ax,ax
ok3_read:
mov sread,ax
shl cx,#9
add bx,cx
jnc rp_read
mov ax,es
add ax,#0x1000
mov es,ax
xor bx,bx
jmp rp_read

read_track:
push ax
push bx
push cx
push dx
mov dx,track
mov cx,sread
inc cx
mov ch,dl
mov dx,head
mov dh,dl
mov dl,#0
and dx,#0x0100
mov ah,#2
int 0x13
jc bad_rt
pop dx
pop cx
pop bx
pop ax
ret
bad_rt:mov ax,#0
mov dx,#0
int 0x13
pop dx
pop cx
pop bx
pop ax
jmp read_track

!/*
! * This procedure turns off the floppy drive motor, so
! * that we enter the kernel in a known state, and
! * don't have to worry about it later.
! */
kill_motor:
push dx
mov dx,#0x3f2
mov al,#0
outb
pop dx
ret

sectors:
.word 0

msg1:
.byte 13,10
.ascii "Loading system ..."
.byte 13,10,13,10

.org 508
root_dev:
.word ROOT_DEV
boot_flag:
.word 0xAA55

.text
endtext:
.data
enddata:
.bss
endbss:

关于2.6的说明: 以上我们看到的源码是属于0.11版本的Linux的,在2.6版本中,bootsect.s发生了很大的变化.首先,以上代码是使用intel 80x86汇编代码写的,但是在2.6中使用的GNU ASM(gas)写的. 更大的区别在于,2.6以后,Linux已经不再支持直接从磁盘引导了,而是必须使用GRUB或者LILO进行引导,因此我们会发现2.6代码中的bootsect.s基本没有什么作用.

以上是本人对Linux启动时的最初一段的理解,仅仅到bootsect.s完成之后,进入setup.s之后,我将在第二篇讲述.之后我也希望又足够的时间和动力把整个Linux启动过程讲述清楚,甚至在此之后能够多涉及一些其他的方面.到此位置,Linux仅仅完成了很小一部分的工作,甚至连system模块在内存中的位置还不正确,系统的初始化也没有完成. 我对bootsect.s作用的理解是它作为操作系统于硬件机制的接口而存在,它把原来原来显得混乱的内存位置稍微整理了一番,而且得到了驱动器的参数,也是把磁盘中的内容读入内存的起始. bootsect.s一定会存在于磁盘的第一扇区中,这样BIOS就能够找到它了.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值