"Operating System Restore truth" study notes in Chapter 2

0x1 computer startup

Why the program to be loaded into memory
hardware CPU is designed to run only in program memory, because the memory of memory shipped fast, and large capacity. Second, the operating system can be stored on a floppy disk, can also be stored on the hard disk, U disk and even
what is loaded into memory
called into memory, roughly divided into two parts:
1) the program is loaded loader (software or hardware to a areas).
2) CPU's cs: ip is directed to register the start address of the program.
When the operating system loader, need a loader to store the user program into memory. The loader module is essentially composed of a bunch of functions.
After pressing the mains key, the software is first run BIOS.

0x2 Relay software first leg, BIOS

BIOS full name Base Input & Output System, i.e., basic input output system.

1MB memory segment in real mode 0x2.1

Intel8086 20 address lines, so the 1MB memory space can be accessed, in the range of 0x00000 to 0xFFFFF. Wherein 640K 0x00000 ~ 0x9FFFF DRAM is mapped to physical memory it is what we, and 0xFFFFF which 0xF0000 to 64K of memory is ROM. This is our stored inside the BIOS code. BIOS work is mainly detected, initialize the hardware.
Specific initialization method is to provide their own hardware function call some initialization, BIOS can be called directly. In addition also created a BIOS interrupt vector table so that it can be implemented by related hardware call "int interrupt number." BIOS feature is the establishment of these IO hardware operation, that is, input and output, but because of the size of 64KB of space, can not put all the hardware IO operations to achieve everything, just pick some BIOS important to ensure that the computer hardware can run the basic IO operations, on the line. This is why the BIOS is known as the basic input output system.

0x2.2 CPU's eyes, we are plugged into the motherboard of physical memory is not all in the eyes of its memory

Determines the width of the address bus of memory space can be accessed as 16-bit address bus machine 20, the address space is 1MB. 32-bit address bus width is 32 bits, the address space is 4GB. But above the range of the address bus can reach to the border, the computer means can be achieved on the address boundaries. Although the address bus can access such a large range, but does not mean that the computer must be addressable range of physical memory (memory).
In the computer, not only plugged into the motherboard's memory address bus needs also need access through the address bus access, and some peripherals. If the whole point of physical memory address bus, then the other device can not access. Therefore, computer designers had on the address bus in advance to reserve some address space for peripherals. After leaving enough remaining point to a DRAM address bus, which is inserted in the memory on the motherboard.
How much physical memory are not used, mainly to see the width of the address bus. Depends on the address bus design is not all, for accessing DRAM. So, the address bus is to decide where we access, access, and what access key range. We usually use 32-bit, above all the memory is not used, and arguably more than 4GB of memory size does not make sense, than the portion of the address bus is a waste. This is why 4GB of memory installed, the computer display but only about 3.8GB reasons.
In conclusion, the series of number represents the address input of the address bus, equivalent parameters, and the memory does not matter. CPU can access this address is the address bus to do the mapping, corresponds to a change of address assignment storage unit, the storage unit falls either in a rom, a peripheral memory or falls, or physical fall on the memory. Imagine CPU to the address bus to provide figures, in geological bus opinion, this string of numbers is the address. Address distributing circuit according to this address range, determine the distribution of a memory cell in the storage medium, the last address of the memory corresponding to this storage unit together.

How 0x2.3 BIOS is awake

BIOS is the first software on the computer, he can not own their own load. It is the hardware ROM (Read Only Memory) is loaded.
ROM is non-erasable read-only memory, he was not as dynamic random access memory DRAM, after power, which data will be lost. This storage medium is used to store data in stone, and when data is written into it not be changed after.
BIOS code is also made to work hard and fast, under normal circumstances, it is not necessary to modify itself, and those bad motherboard BIOS in case you want to refresh usually heard exception. Because of the invariance of BIOS, the BIOS is written in the ROM hardware. ROM is memory, also need to address bus access. This ROM is mapped at the top of 1MB of memory locations, i.e., at the address 0xF0000 ~ 0xFFFFF. As long as the address access here is access to the BIOS, this mapping is done the hardware.
BIOS itself is a program, the program needs to perform, you need an address entry, the entry address is 0xFFFF0.
In the electrical connection of instant boot, CPU's cs: ip register will be forced initialization bit 0xF000: 0xFFF0. Since the boot time is in real mode, it is combined into 0xFFFF0 address, i.e. the inlet BIOS. As will be appreciated, CPU points to a power entry point of the BIOS program.
However, we found that only 16 bytes between 0xFFFF0 to 0xFFFFF, simply can not store much data, so here is a jump instruction code can only be interpreted to pass.

Why is 0x2.4 0x7c00

BIOS boot disk check last job content located 0 0 1 sector. The contents of the sector is the MBR. Magic number 0x55 this sector and 0xaa, BIOS executable program will be considered in this sector does exist, then loaded into a physical address 0x7c00, then jumps to perform here. Jump to the BIOS 0x7c00 is 0 jmp: 0x7c00 achieved, which is far jmp instruction absolute jump transfer usage, will be replaced cs segment register, where 0 is the base address of the segment, i.e. cs becomes 0 from the previous 0xf000.
If this is not the last two sectors and 0x55 0xaa, even if there is executable code which does not help.
Why is content 0 0 1 sector
of the MBR presumably to quickly find content quickly and easily loader to the specified 0x7c00.
Why is the physical address or other address 0x7c00 not nice address
0x7c00 role is the magic number, first appeared in IBM PC ROM BIOS 5150 the INT 19 interrupt handler. After power on, call BIOS interrupt 0x19, that is, call int 19h. The interrupt handler, BIOS to detect this computer how much hard disk or floppy disk, if it detects any available disk, BIOS loads the first sector to put it 0x7c00.
The reason why x86 can not find the manual, because it belongs to the BIOS specification. So it must be defined by the number of IBM PC 5150 BIOS development team.
MBR is not a place where casual will do, first of all can not overwrite the existing data, and secondly, can not be covered by other data prematurely. Usually, MBR task is to load a program (this is generally the kernel loader program, there is little direct load the kernel) to the specified location, and passes control to him. The so-called transfer of control is jmp past it. After the MBR is no use, it does not matter covered. Premature covered here, refers not let mbr destruction of their own, such as the loaded program, such as the kernel loader, positioned as if MBR where their range, which destroy themselves.
8086CPU required physical address 0x0 ~ 0x3FF stored interrupt vector table, where it can not move.
32KB DOS 1.0 by the minimum memory requirements for, MBR want to reserve as much space, so is the preservation of their own practices, to avoid it being prematurely overwritten. So MBR can only be placed at the end of 32KB.
MBR itself is a program, the program is necessary to use the stack, the stack is in memory, although the MBR itself is only 512 bytes, but the point stack allocation but also for its use of space, so the actual effect of the memory space is greater than 512 byte estimated 1KB of memory is sufficient.
Combining these three points, the last 32KB 1KB select the most appropriate. 32KB converted to hexadecimal 0x8000, minus 1KB (0x400), equal to 0x7c00. This is the origin of 0x7c00.

0x2.5 MBR began to realize

First we have to clear the MBR size is 512 bytes, the last two bytes of disk magic number is 0x55 and 0xaa, namely 510 and 511 bytes. According to the initial offset zero calculation. bochs simulates the x86 platform, so is the little endian, so the last two bytes of content 0xaa55

0x2.5.1 magical and easy to use $ $ $, confusing section

$ $ $ Is the key and the sub-compiler NASM reserved, used to represent the current row address and the present section, acts as a label, which is provided NASM not native CPU supported, corresponds to the same directive, the CPU would be false.
This directive is relative to the CPU instruction is recognized, and it (directive) compiler defines knowledge, the CPU does not exist in this directive, hard to let the CPU executes directive, CPU throws "UD (undefined opcode) "exception. Some symbols are directive to the compiler developers to write code that provided convenient, these symbols at compile time, the compiler will be converted by the CPU into something recognizable, such as an instruction address.
$ Are "implicitly" hidden in the front line of code label, which is currently scheduled compiler to address, but you do not see everywhere in $ per line we have. Or only "explicitly" with the $ place, nasm compiler will publish the address it.

code_start:
	jmp $
//等同于jmp code_start

$ $ This section refer to the starting address, this address is the address of the same compiler arrangement, by default, the value thereof is offset relative to the beginning of the text file. As for the actual arrangement it is how much, depends on the programmer whether to add a section in the vstart. This keyword can affect the behavior of the compiler arrangements address, if the section with the vstart = xxxx modified value of $ $ is the starting virtual address this section of xxxx. The value of $ xxxx is the starting address postponed. If you use the keyword vstart, want to get the true offset (real address) of this section is contained in this document how to do? nasm provides this method.

section.节名.start

If not defined section, nasm same as a default all the code section, starting at address 0.
Next, copy the following code author

;主引导程序
;-----------------------------------------------------
SECTION MBR vstart=0x7c00
	mov ax,cs				;因为BIOS执行完毕后cs:ip为0x0:0x7c00,所以用cs初始化各寄存器
	mov ds,ax				;ds、es、ss、fs不能给立即数初始化,需要用ax寄存器初始化
	mov es,ax
	mov ss,ax
	mov fs,ax
	mov sp,0x7c00			;初始化堆栈指针,因为目前0x7c00以下的内存暂时可用
;清屏利用0x06号功能,上卷全部行,则可清屏
;-----------------------------------------------------
;INT 0x10	功能号:0x06		功能描述:上卷窗口
;-----------------------------------------------------
;输入:
;AH	功能号= 0x06
;AL = 上卷行数(如果为0,表示全部)
;BH = 上卷行属性
;(CL,CH) = 窗口左上角的(X,Y)位置
;(DL,DH) = 窗口右下角的(X,Y)位置
;无返回值:
	mov	ax,0x600			;上卷行数:全部	功能号:06
	mov bx,0x700			;上卷属性
	mov cx,0				;左上角:(00)
	mov dx,0x184f			;右下角:(80,25)
							;VGA文本模式中,一行只能容纳80字节,共25;下标从0开始,所以0x18=240x4f=79
	int 0x10				;int 0x10

;;;;;;;;	下面这三行代码获取光标位置		;;;;;;;;
;.get_cursor获取当前光标位置,在光标位置处打印字符。
	mov ah,3				;输入:3号子功能是获取光标位置,需要存入ah寄存器
	mov bh,0				;bh寄存器存储的是待获取光标的页号
	
	int 0x10				;输出:ch=光标开始行,cl=光标结束行
							;dh=光标所在行号,dl=光标所在列号
;;;;;;;;	获取光标位置结束		;;;;;;;;

;;;;;;;;	打印字符串		;;;;;;;;
	;还是用10h中断,不过这次调用13号子功能打印字符串
	mov	ax,message
	mov bp,ax				;es:bp为串首地址,es此时同cs一致,
							;开头时已经为sreg初始化
	
	;光标位置要用到dx寄存器中内容,cx中光标位置可忽略
	mov cx,5				;cx为串长度,不包括结束符0的字符个数
	mov ax,0x1301			;子功能号13时显示字符及属性,要存入ah寄存器,
							;al设置写字符串方式al=01:显示字符串,光标跟随移动
	mov bx,0x2				;bh存储要显示的页号,此处时第0页,
							;bl中式字符属性,属性黑底绿字(bl = 02h)
	int 0x10				;执行BIOS 0x10 号中断
;;;;;;;;	打字字符串结束		;;;;;;;;

	jmp $					;使用程序悬停在此
	
	message db "1 MBR"
	times 510-($-$$) db 0
	db 0x55,0xaa

The above code is compiled nasm -o mbr.bin mbr.S, the compiled file size is 512 bytes of
Here Insert Picture Description
the next instruction in accordance with the Linux dd, we will write the compiled binaries empty boot disk file created in the previous chapter dd if=/root/mbr.bin of=/root/Downloads/bochs/hd60M.img bs=512 count=1 conv=notrunc
Here Insert Picture Description
to start the next program is able to verify the output bochs we want results
Here Insert Picture Description

Published 30 original articles · won praise 5 · Views 1934

Guess you like

Origin blog.csdn.net/AlexSmoker/article/details/104080821