I always think that the operating system is the ultimate goal of every computer person in the field of professional technology. Although this view is biased, it is at least one of the basic skills for a computer specialist to master the operating system.
Yu Yuan's implementation of an orange's operating system is a very good operating system book. In the undergraduate course class, he is talking about the theoretical things and can still do the exercises, however, too abstract things are always bad, especially for engineering, engineering is to practice. Yu Yuan's book teaches you how to write an operating system from scratch. From compilation to C, you can go deep into the basic principles of the operating system step by step. Although you read the previous chapters, but after reading it over and over again, the previous doubts gradually dispersed, and the basic concepts gradually took root. This is a wonderful feeling that the curtain of a huge software is gradually opened.
Let's talk about the protection model in the third chapter of the book today.
The protection mode is a product left behind by the previous dynasty. In Yuanyuan's words, from the real mode to the protection mode is a process of changing the status of the system, which is very impressive, because most of the time it means progress. Real-time mode is a product of the DOS era, including 16-bit registers, 16-bit data lines, and 32-bit address lines. However, as the times call, the CPU will eventually evolve and the program will eventually become larger, therefore, it puts forward higher requirements for this operating system.
The first thing I need to understand is whether a 32-bit CPU is used, and the original 16-bit CPU is required to be completely negated, just as the Soviet Union cannot contradict Stalin's pattern. So here we introduce the real and protection modes. That is to say, the emergence of both the real and protection modes is a process in which culture is well-rounded and inclusive.
The Startup Process of the operating system first enters the real mode and then the protection mode. At the beginning of the protection mode section, Yu Yuan gave a jump code. This code is dizzy at first glance. It is a good thing to be dizzy. It means that you do not understand it. If you can overcome the fear of the unknown, we will be able to grow.
In a 16-bit code segment, the author initializes the Global Descriptor, which is a concept mentioned in many system-level books, we only need to remember that this is used to provide a segmented storage mechanism. Return to the 16-bit code segment mentioned above. In this segment, all commands are of the old system, that is, 16-bit, in this 16-bit system, what should we prepare for the 32-bit System in the future protection mode? Prepare the Global Descriptor (gdt ). This is well understood, because in 32-bit systems, in the segment-based storage mechanism, we must use gdt, that is to say, gdt must be formed when you enter the protection mode. It is good and can be used directly, however, the 16-bit system we have to go through after starting the system is only a legacy problem. What should we do in this 16-bit system? The answer is to prepare some things required by the 32-bit system. So with this Code:
movax, csshleax, 4addeax, LABEL_SEG_CODE32movword [LABEL_DESC_CODE32 + 2], axshreax, 16movbyte [LABEL_DESC_CODE32 + 4], almovbyte [LABEL_DESC_CODE32 + 7], ah
The main function of these statements is to initialize gdt, that is, to declare the address of the code segment in the memory of the program. We already know that gdt is a data structure set up for the block storage mechanism, so an important part of the gdt table item is the address of the segment in the whole program space. Now, I finally introduced the address and finally arrived at the things in those textbooks. It was finally related to my usual questions, here, the segment address obtained from gdt plus the offset address provided by the program is the linear address we have learned before. The linear address is not the beginning of the address chain in our mind, but should start with the logical address, what is the logical address? The logical address is the input in the gdt table. Now we have a clue. To give you an intuitive explanation, we will draw a picture to express it.
Well, we already know the relationship between logical addresses and linear addresses, but what is the value of logical addresses and what is the value of linear addresses? First, give the answer. The logical address is "Select Sub: Offset address", and the linear address is "segment address: Offset address ]. What is sub-selection? We seem to have entered a DFS, but it doesn't matter. We have to break through all the test points. Don't be afraid of unfamiliar concepts. The sub-database is a 16-bit structure, with the 13-bit height representing the offset of the current segment (remember, we have upgraded and started to manage programs in segments) in gdt, the lower Three Represent equal priority settings. Do not worry about them for the moment. Now we understand that the logical address is the offset of this segment in the Global Descriptor Table (gdt) and the offset in this segment. Now, I will give you one minute to learn how to convert logical addresses to linear addresses based on the knowledge provided above.
Sleep (1000 );
Are you clear? The logical address provides the selector and offset addresses. Then, based on the offset, we can find the table items in this segment in gdt. What is stored in gdt? It is the starting address of the current segment in the whole program space, that is, the segment address, with the segment address and the offset on the logical address, the sum of these two quantities is called a linear address --- [segment address: Offset address ]. Contact the process shown in to see if it is clear.
Well, we omitted a lot of details in this process, such as storing the gdt base address in the Cr0 register. Next let's take a look at how the authors program in the protection mode section, so that we can better understand the conversion.
The following code is consistent with the previously posted code. It is pasted again for convenience.
movax, csshleax, 4addeax, LABEL_SEG_CODE32movword [LABEL_DESC_CODE32 + 2], axshreax, 16movbyte [LABEL_DESC_CODE32 + 4], almovbyte [LABEL_DESC_CODE32 + 7], ah
The above Code comes from the initialization process of the gdt table in real mode. First, the last three of the four mov commands in the Code fill the gdt table items. Other details can be ignored first. Okay, let's take a look at the specific filling content: CS register content, shifts four places left, and then adds the starting address of the 32-bit code segment, then, store the obtained address to the corresponding table item. With just a few simple words, the filling of a table item is completed. Suddenly, the addressing space of the address line is 1 MB in real mode, in order to make up the 20-bit address, the CPU processing method is exactly as follows: CS shifts four places left to get the so-called "segment", and then adds the offset, the obtained address is called a linear address with 16 bits. Enter this address. The gdt address of the code segment serves as the entry address of the 32-bit code segment, that is, the segment address.
Now, the initialization description of our segments ends. Then let's see how to get a linear address through the logical address.
movax, SelectorVideomovgs, axmovedi, (80 * 11 + 79) * 2;moval, 'P'mov[gs:edi], ax
The above program function writes a character P to the video memory. Ignore the details. For the first line, we already know that the logical address provides [select Sub: Offset address ], therefore, the first and second lines are used to load the Selection Sub of the video memory into the GS register, and then determine the EDI in the third line, that is, at the offset address, the fourth line is to write the string P into the register Al, and the last line is the goods line. It is to write the content in the Al Register into the memory pointed to by [GS: EDI. We can assume that in the protection mode, [GS; EDI] completes the transformation of the Logical Address to the linear address during compilation. That is to say, in protection mode, the CPU finds the table items in gdt through the content in Gs, obtains the base address of the segment, and adds the offset address to obtain a linear address.
Now, the logic address and linear address are explained. The relationship between physical address and linear address and the content of more protection modes will be described in the following logs.