前言
对于操作系统的分析,是一个复杂且枯燥的过程,其中包括中断、调用等一系列的问题,需要从原理和代码两层的角度进行分析,包含汇编、C语言等较为难理解的知识,以及算法的思维。
【学习资料】
启动流程
setup模块扇区长度为4个扇区,各个模块存放在内存的地址。
这里的ROOT_DEV=0x306表示第二个硬盘的第一个扇区。
基础
设备号 = 主设备号 * 256 + 次设备号
主设备号是定义好的:
1-内存
2-磁盘
3-硬盘
PC机的BIOS把bootsect的一个固定地址拿到了内存中某个固定地址(0x90000),
并且进行硬件初始化和参数设置。
bootsect中的代码首先移动到0X7C00,之后又转移到0X90000。
代码分析
bootsect模块移入内存
start:
mov ax,#BOOTSEG
mov ds,ax
mov ax,#INITSEG
mov es,ax
mov cx,#256
sub si,si
sub di,di
rep
movw
jmpi go,INITSEG
把bootsect中代码移动到内存中0X90000中,
可以看到此时的ds 存储0x07c0 地址:es 0x90000。
利用寄存器从ds:si到es:di
jmpi go,INITSEG
INITSEG是段地址,go是偏移地址,跳转到程序执行的地方(0x90000)。
bootsect.s作用
1.首先加载bootsect的代码(磁盘引导块程序,在磁盘中第一个扇区的程序)
2.将setup.s中代码加载到bootsect.s中代码之后
3.将system模块加载到0x10000地方,最后跳转到setup.s中运行
栈的位置
go: mov ax,cs
mov ds,ax
mov es,ax
! put stack at 0x9ff00.
mov ss,ax
mov sp,#0xFF00 ! arbitrary value >>512
设置ds、es、ss、sp
对栈的设置 es:sp =0x90000:0xff00
setup模块移入内存
load_setup:
mov dx,#0x0000 ! drive 0, head 0
//驱动器号0;磁头号0
mov cx,#0x0002 ! sector 2, track 0
//起始扇区2;磁道0
mov bx,#0x0200 ! address = 512, in INITSEG
mov ax,#0x0200+SETUPLEN ! service 2, nr of sectors
int 0x13 ! read it
jnc ok_load_setup ! ok - continue
mov dx,#0x0000
mov ax,#0x0000 ! reset the diskette
int 0x13
j load_setup
setup模块加载到0x90200中。
setup.s作用
- 解析BIOS传递过来的参数;
- 设置系统内核运行的局部描述符,中断描述寄存器,全局描述符;
- 设置中断控制芯片,进入保护模式;
- 跳转到system模块中head.s中代码执行。
system模块移入内存
! we want to load the system (at 0x10000)
mov ax,#SYSSEG
mov es,ax ! segment of 0x010000
call read_it
call kill_motor
把system模块加载0x10000处,
关闭驱动器。
【学习资料】
head.s作用
- 加载内核运行时的各种数据段寄存器,重新设置中断描述表;
- 开启内核正常运行时的协处理器;
- 设置内存管理的分页机制;
- 跳转到main.c开始运行。
中断调用:
有出错号、无出错号
中断范围:
int0 ~ int255。
int0 ~ int31 : 软件中断,由Intel固定设置的。
int32 ~ int255: 可由用户自己设置。其中int32 ~ int47 对应8259A的IRQ0 ~ IRQ15中断。
特殊的一个:int128 为系统调用中断(system_call)。
#int7 – 设备不存在。
#int14 – 页错误。
#int16 – 协处理器错误。
#int 0x20 – 时钟中断。
#int 0x80 – 系统调用。
asm.s分析
寄存器入栈:
no_error_code:xchgl %eax,(%esp)pushl %ebxpushl %ecxpushl %edxpushl %edipushl %esipushl %ebppush %dspush %espush %fs
异常码入栈:
pushl $0 # “error code”
函数返回值入栈:
lea 44(%esp),%edx //把中断的地方压栈pushl %edxmovl $0x10,%edxmov %dx,%dsmov %dx,%esmov %dx,%fs
调用中断服务函数:
call *%eax //调用中断打印函数
出栈函数返回值:
addl $8,%esp //函数参数出栈pop %fspop %espop %dspopl %ebppopl %esipopl %edipopl %edxpopl %ecxpopl %ebxpopl %eaxiret
error_code:
xchgl %eax,4(%esp) # error code <-> %eax //中断错误码xchgl %ebx,(%esp) # &function <-> %ebx //中断函数pushl %ecxpushl %edxpushl %edipushl %esipushl %ebppush %dspush %espush %fspushl %eax # error code //出错号入栈lea 44(%esp),%eax # offsetpushl %eaxm