Operating system learning series (1. Sail) - basic concepts of operating system

foreword

This series of blog posts is mainly used for the 408 professional content postgraduate entrance examination for in-depth understanding in the second stage. That is to say, before starting this series, it is best to have a basic understanding of 408. The repeated basic concepts will not be repeated. In addition, this series of blog posts will also complete the review and application of C language programming and data structure, although this series is a series of operating system learning.

This series will combine the Wangdao postgraduate entrance examination as the outline navigation, combined with excellent books such as "30 Days Self-made Operating System", consolidate the contents of the book step by step and use assembly and C language to complete the simple operating system design. In this series, the blogger himself is constantly recording the content in the learning process, so there may be many inaccurate parts before the end of the series, please forgive me!

In addition, this article also needs to complete the operation based on Intel CPU, AMD here no.

Basic overview of the operating system

So before we start, let's introduce the concept of the basic operating system. First of all, we need to clarify what the operating system provides us.

First of all, the operating system provides us with several basic management:

  1. memory management
  2. device management
  3. file management
  4. processor management

At the same time, we know that the operating system has several basic characteristics:

  1. concurrency
  2. asynchronous
  3. virtual
  4. shared

If I remember correctly, this should be the content of the first chapter in Wangdao PubMed. I won't say much here, the book is more detailed than me.
So here I want to add more.

to interrupt

The operating system provides me with a program interface, which is composed of a series of system calls. At the same time, for system calls, such as reading and writing files, at this time, an interrupt will be triggered to perform IO operations. This is all the content of the first chapter. We all know why the interrupt is triggered because this IO operation is a system-level, kernel-level call. So in fact, the process is correct, but the realization of this part is actually a very important foundation. We will often refer to real mode and the protected model later when we go into the details of actually implementing an operating system. But here about the interruption, we don't care about it so much, I just want to explain that this interruption is one of the very important mechanisms.

In addition, it is worth mentioning that interrupts are also supported at the hardware level. For Ring0, 1, and 3, this is actually supported in the CPU. Under our protection mode, we will realize the processing of this part through DRT.

Basic interaction with hardware

Still the same sentence, before reading this article, we hope that you have a general understanding of the operating system, and here we really recommend the operating system for the Wangdao postgraduate entrance examination. Although it is biased towards brushing the questions, some concepts are explained very well. What is described in this series of blog posts is here again.
insert image description here

Through the computer composition principle, we know that for the CPU, there are ALU, CU, operation and logic control unit in it. At the same time, there are many registers inside. For example, the common eight registers:

AX: accumulation register
BX: base register
CX: counting register
DX: data register
SP: stack needle register
BP: base stack register
SI: source address register
DI: destination address register
CS: status register

(Note that several registers here are 2 bytes, and this is used under real mode. It is 16-bit under real mode, and 32-bit under protected mode)

Of course, there are many registers in the CPU. At the same time, there is a very important concept here that we need to know roughly. In the principle of computer composition, we know that we have addresses for different devices. Our operations on different devices are actually done by changing the values ​​in these addresses. That is to say, we access the addresses through registers and manipulate the values ​​of the addresses, and we can complete the control of the basic devices.

Of course, we need to be clear here. Most of our programming work here is done by controlling the CPU. When we need to control, such as the keyboard and the screen, it is controlled by modifying the data in the register. Therefore, when we implement it in the early stage, we need to interact with the assembly to a certain extent, and have completed the basic control of the hardware. Of course, the good news here is that the hardware manufacturer provides a good interface, or that Intel and the hardware manufacturer have a good agreement.

base address

As we just said earlier, that is, we complete the basic program control through the CPU. At the same time, we complete the control of the device by finding the device address and then operating the corresponding value in the register (next we will give an output of Hello World. example)

Therefore, here, we clarify a concept, that is, the essence of a computer is actually to perform different operations on data in different locations. It's just that we make the computer present a colorful world through different regulations. For example, an exe file is a string of binary codes, and so is a jpg image, but their effects are completely different.

Therefore, we have clarified the importance of addresses. At the same time, it is similar to devices. Devices also have addresses. As mentioned earlier, we need to operate through registers to control them, and the registers contain the address or data. Therefore, in order to call the device normally, we also need to agree on some addresses.
For example this:
insert image description here

address concept

Then there is the concept of address. In fact, in our operating system, all we use are logical addresses. When a program is executed, we pull data from the physical address and put it in memory, which is very important in the operating system. One point, that is, we need to let the application think that he is monopolizing the CPU, from logical address 0 to logical address 100, assuming that there is a program written like this, then in fact he may be physically from 0x6000 - 0x6545 and so on. Therefore, many exam questions have an address conversion question. At the same time, we also need to know that when the cpu performs calculations, we use binary, but when the operating system expresses, we use hexadecimal. The hexadecimal system has an advantage. First, it can easily represent a large number in the decimal system without wasting too much space. After that, it is very convenient to convert the hexadecimal system to the binary system.

In addition, it is emphasized here again that we need to read the operating system book of Wangdao's postgraduate entrance examination first, and have a basic understanding of it, because our side is actually different from the content in Wangdao's postgraduate entrance examination. In fact, we need to start from the disk first, pull our program from the disk, and then load it, and so on, until we need to process the program in our operating system, we need to consider the design of multi-programming, concurrency, shared. Frequently tested pv operations, replacement algorithms, rotation algorithms, etc.

start process

This should be the content of the first chapter in the Wangdao postgraduate entrance examination. Next, let's briefly introduce it.

First of all, when starting, we will start the Bios. In the bios, we will find the mbr under the operating system Boot, and then load it into our loader program through the mbr, and then load it into the system kernel through the loader. Then mbr is one of our guides. Pass, guide, and we will complete the follow-up operations. Then the question comes, why do we need mbr->loader->keneral first? The reason is very simple. In the bios, the space given to us is only 512 bytes, which is obviously not enough to start a system and the kernel.

So we need to complete the startup operation through this process.

Their general process is actually like this:
insert image description here

In this process, we transfer the execution of the cpu to the operating system

Relationship between application and operating system

Great, finally arrived at this one concept that I need to add.

We until, a program, such as this program.

#include<stdio.h>
int main(){
    
    
	printf("Hello world");
	return 0;
}

We can translate this code into a bin binary file for execution when the executable file .exe is programmed at the bottom layer. The .exe file will be executable in the windows system.

Well, in fact, we know that the operating system is actually a special software in essence, so if the software we write can also be placed in the BIOS when it starts up, and the execution of the CPU is transferred to our program, then does it also mean that , our software can also be the same as the operating system. In fact, this is called a special "operating system" is also possible. However, the actual situation is that we start the software through the operating system, not just mentioned, if it is the way just said, then the operating system seems to have no effect.

So in fact, a complete program (win) of a program is like this:
insert image description here
So, if we want to implement our operating system, we must provide the corresponding program-dependent interface, yes, we also need to define our operation System executables. This is actually very similar to the browser. Our programs are actually equivalent to each web page. The programs in the web page need to rely on the browser to display the effect and complete the analysis through the v8 engine. The same is true for our operating system.

So back to our program just now.
It is so simple for us to output "Hello Word" through C language.
In fact, we called stdio.h, which is equivalent to a python package. It provides output functions, and input and output, we know that this requires system calls, so we need to implement it at the operating system level. Because the application program does not have this permission, only our operating system has it.

Then if we want to output a sentence from the screen, it is very troublesome for us to implement it at the bottom layer. At the same time, when implementing it, we cannot call any system implementation, that is, functions such as printf(), but need to implement similar functions through assembly. The code for this one:

(Yes, this is the output content we mentioned earlier, we found xb800, this is the address of the flat-panel display)

mov ax,0xb800               ;在bios当中将指针指向文本显示地方
mov es,ax

mov byte [es:0x00],'L'      ;找到的文本显示的地址,然后写入内容
mov byte [es:0x01], 0x07    ;设置颜色
mov byte [es:0x02],'Y'
mov byte [es:0x03], 0x06

time 510 - ($-$$) db 0      ;填充剩下的空间,引导区是512
dw 0xaa55                   ;以这个结尾才会别biso识别

Hello World launch

Very good, we have added the content that is not the same as Wang Dao. Of course, it is a pity that the space is limited here, and the code corresponding to the assembly part will not be explained here. But I think the explanation of the corresponding assembly in the book "30 Days of Self-made Operating System" is very good. Although the book uses ask assembly, it is actually a custom version of nasm, which is generally consistent with assembly.

So now, we start the first hello word. Here, we will not follow the book, but use a similar scheme.

First of all, we start writing the first startup program, this is our mbr.

org 07c00h
mov ax,cs
mov ds,ax
mov es,ax
call DD
jmp $
DD:
	mov ax,BOOTMSG
	mov bp,ax
	mov cx,16
	mov ax,01301h
	mov bx,000ch
	mov dl,0
	int 10h
	ret
BOOTMSG:	db "hello, OS World!"
times 510-($-$$)	db 0
dw 0xaa55

Then, at this point we need to compile this code.

Before again you need to prepare nasm by yourself
insert image description here
At this point we are done compiling. After that we also need to create a virtual hard disk, next we will boot on the virtual machine.
insert image description here

After that, we write the "first operating system" to disk.
insert image description here

Then we can create a virtual machine, I am using virtual Box here

Then click Start to complete the first Hello OS World

Summarize

This article is just a blogger's essay, and will be fully supplemented later. Of course, in fact, for the postgraduate entrance examination party, it is more important to write questions, and this is a bonus item for practical operation. In addition, bloggers default that readers and bloggers are of a similar level.

Guess you like

Origin blog.csdn.net/FUTEROX/article/details/130926862