L3 OS operating system boot (Netease open class)

 

bootsect.s (boot sector program) did a what?

OS program in the hard disk (setup.S) into memory

Today look after setup.s done something.


 

Why start an operating system program to use assembly to write? Because we want to strictly control program execution where. Compilation can do, but the C language before they can be required to compile, execute and where we can not control.

 

 

 

 

Why do it or not (expansion, acquisition memory size) it?

In order to manage memory later, you must first know the memory size.

But also to obtain other parameters: such as cursor position, the graphics parameters and so on.

Data table will form a structure obtained after. In order to manage the back.

 

After all the code of the operating system will move to the address 0, it is fixed in memory. After application on top of the operating system on it! (This also explains why the pass 4G of memory found enough available memory 4GB, as part of the operating system memory is occupied by friends)

 If this course of all the code (taken from Linux0.11 source code analysis) all understand, you have until the basic look of the operating system. There is spare capacity to go read "Linux0.11 source code analysis."

After this, setup.s to complete the basic work.

The last thing to do is setup.s:

jmpi 0,8 This instruction plays a very vital role in our curriculum. Can understand it can be said that knowledge of the operating system of a certain level.

0,8 jmpi meaning is: 0 to assign IP, 8 assigned to the CS.

This will jump to 00,080 a. But if it jumps at 80 will crash. This instruction actually jumped to 0 address.

Because the memory access capacity of only 20, the largest only 1MB, so now the computer memory access capacity has reached 4GB, then how to change the way it fetch?

Switching from a 16-bit to 32-bit machine like machine. (1M-> 4GB).

32-bit mode, also called protected mode. You must switch from real mode to protected mode of work.

So the last thing setup.s is to switch to protected mode

 What is the essential difference between 16-bit and 32-bit mode is the mode? CPU is not the same interpreter.

Before that CS; after IP, CS left four plus IP. But now this can not explain.

To switch to the other circuits.

If cr0 register is 0, that is, a real mode to protected mode is 1.

After these two instructions is finished, CPU of one circuit go further.

 这条解释执行的电路要怎样解释执行呢?

CS这时变成了选择子(selector)

 

这时查找指令的地址的方式就变成了:CS的作用为选择表中的一个项,在这个项中取出基地址,再和IP偏移地址加在一起产生32位地址。

如果要想32位的寻址方式好使,那么就需要著名的gdt(global discription table)表

setup.s初始化gdt的部分:

 

切换到32位寻址方式后,CS和IP都变成了32位的寄存器。

此时中断函数也要去gdt表项里去查找。

 

其实是跳到了0地址去执行。

 

 

 

编写操作系统,除了编写操作系统的源代码以外,还要编写操作系统的控制代码,比如system是由一大堆子文件集合起来的,那么就需要你控制谁先执行。

这就是著名的makefile

学完了这个课程后,makefile必须会用!

像编辑器,直接用快捷键就能把源代码文件编译好,是因为内部帮你实现了makefile。

system是由许多子文件像bootsect,setup,head,init等组合而成,最后生成一个镜像文件Image。

最后LD,链接,生成了tools/system。

第一个模块是bootsect,接着是setup,接着是head,接着是main

head.s做了些什么事情?

又一次初始化了idt表和gdt表

刚才为啥要初始化gdt表?只是为了跳到jmpi 08这个地方去。所以临时建立gdt表。

这一次建立的表,是使操作系统真正开始工作。

还有一个要注意的是:现在的汇编代码和之前的不一样了。变成了32位的汇编代码。

操作系统特别复杂,使用了3种汇编语言:16位汇编,32位汇编,和内嵌汇编。

 

head.s(汇编)会跳转到main函数部分,main是一段C函数。

可以看到这是压栈操作,下图配和理解,压完main函数后,返回L6,L6将继续执行L6,这时就会死机。所以操作系统是永远不会死机的。

进入了main函数:

可以看出,main函数用于不会退出(因为没有返回L6的语句)。

可以看到,这里的函数大部分作用都是初始化。

这里只说一个,明白了它,其他的作用大概也就明白了

内存的初始化。

每4K进行一次初始化,初始化为0。4K称为一页,我们在内存管理的时候再详细说明。

end_mem就是你的机器的内存大小,这个参数是之前setup.s程序读取内存的时候获取的。

 

到此,系统启动基本完成。其实上就是干了两件事:1.把操作系统读入内存 2.初始化

Guess you like

Origin www.cnblogs.com/JasonPeng1/p/12110141.html