In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/03 Report--
Operating system-from real mode to protected mode
one。 From Real Mode to protected Mode (part I)
a. We need to start with the history of computers here.
1. Program development in ancient times: direct manipulation of physical memory
The operands of the 2.CPU instruction directly use the real address (the actual memory address)
3. Programmers have absolute power (using cpu to tell where to play)
The power of the real mode at that time brought a lot of problems-difficult to locate, mainly because programs needed the same address of memory to execute every time, and also brought obstacles to multi-programming, mainly because no matter how large the memory was, as long as a byte was occupied by other programs, it could not be executed.
b. In order to solve the above problems, there is a milestone in the history of this CPU-8086
1. The address width is 20 bits and can access 1m memory space.
two。 Introduce the memory access method of [segment address: offset address]-8086 segment register and general register bit 16 bits. A single register addresses up to 64K memory space, which requires the cooperation of two registers to complete the access of all memory space.
Segment address: offset address-- the use and definition of these two have two meanings
1. The work done by the hardware-the segment address is moved 4 bits to the left to form a 20-bit base address (starting address), while the real address = base address + offset address
two。 Meaning for developers-functions that can divide memory more effectively (data segments, code segments, etc.), and resolve conflicts by modifying segment addresses when program addresses conflict
Detailed introduction of 8086 https://baike.baidu.com/item/8086/7716347?fr=aladdin
Q: 8086 will lead to a question-segment address: offset address can access the maximum address bit 0xFFFF:0xFFFF, that is, 10FFEF; beyond the space of 1MB, what should CPU do?
We know that 8086 of the high-end address area
So 8086 processing-since 8086 has only 20-bit address lines, the highest bit is discarded
So the problems in the application program during the period of 8086
1.1MB memory is not enough at all-memory is not enough at any time
two。 Developers use a lot of memory rollback technology in their programs-HMA addresses are used
3. There are no boundaries between applications and interfere with each other at will-A program can access data in B program at will, C program can modify instructions of system scheduler
So 80286 appears-8086 no longer has so many applications, so it must be compatible and re-compatible, increase the memory capacity, increase the number of address lines (24 bits), [segment address: offset address] can be strengthened, can provide more attributes for each segment (such as scope, privilege level, etc.), can provide a fixed way for the definition of each segment 80286 is fully compatible with 8086 by default (real mode). By default, it can directly access the memory space of 1MB, but access 1MB + space in a special way.
c. Protection mode
1. Each piece of memory has an attribute definition (descriptor)
two。 The attribute definitions of all segments form a table (descriptor table)
3. The segment register holds the index of the attribute defined in the table (selector)
Memory structure of descriptor
Descriptor table
The structure of the selector
The way to enter the protected mode-1. Define descriptor table 2. Open A20 address line 3. Load description Table 4. Notify CPU to enter protected mode
Summary
1. The addressing method of [segment address: offset address] solves the rare problem of early program relocation.
2.8086 programs in real mode cannot guarantee security
The protection mode is put forward in 3.80286, which strengthens the security of memory segment.
4. For compatibility reasons, processors after 80286 have two operating modes
5. The processor needs specific setup steps to enter the protected mode. The default is real mode.
two。 Real mode to protected mode (middle)
The emergence of 80286 introduces the protection mode, which lays the foundation for modern operating systems and applications, but there are still some defects in the design-the segment register is 24 bits and the general register is 16. Theoretically, the value in the segment register can be directly used as the segment base address, and the 16-bit general register can access 64K memory at most. In order to access 16m memory, the segment base address must be constantly switched.
A.80386 (improved version 80386 due to the deficiency of 80286)
1.32-bit address bus, which supports 4G memory space
two。 Segment register and general register bit 32 bit
3. Any register can access any corner of memory-- ushering in a new era of flat memory mode, with a segment base address of 0, and using general registers to access 4G memory space
There are three ways to use memory in the new era.
1. Real mode-compatible with 8086 memory usage
two。 Segmentation mode-segmenting memory functionally (data segment, code segment) by [segment address: offset address]
3. Flat mode-all memory is a segment [0:32 bit offset address]
Segment attribute definition
Select a child attribute definition
Segment definition in protected mode
Compile tips
The section keyword is used to define a collection of code "logical".
The code snippet defined by section is different from that of [segment address: offset address].
The code snippets defined by section are limited to those in the source code
The code segment of [segment address: offset address] refers to the code segment in memory.
Bits16- is used to instruct the compiler to compile code in a 16-bit manner
Bits32- is used to instruct the compiler to compile the code in 32-bit mode
What we need to pay attention to here is
1. The 0th descriptor in the segment description table does not use the
two。 The 16-bit and 32-bit code snippets that must be displayed in the code
3. You must use the jmp instruction to jump from a 16-bit snippet to a 32-bit snippet
Protected mode programming experiment-the raw material of the experiment requires inc.asm and the loader.asm needs to be modified.
The loader.asm is modified as follows
% include "inc.asm" org 0x9000jmp CODE16_ message [section .gdt]; GDT definitionGDT_ENTRY: Descriptor 0,0, 0CODE32_DESC: Descriptor 0, Code32SegLen-1, DA_C + DA_32; GDT endGdtLen equ $- GDT_ENTRYGdtPtr: dw GdtLen-1 dd 0; GDT SelectorCode32Selector equ (0x0001 > 16) & 0xFF Segment base address 2 dw ((% 2 > 8) & 0xF00) | (% 3 & 0xF0FF); attribute 1 + segment limit 2 + attribute 2 db (% 1 > > 24) & 0xFF; segment base address 3%endmacro; total 8 bytes
The dependency of make needs to be modified
After the preparation work, make,bochs will see the results.
It is found that the result is not printed under bochs, so it is necessary to set a breakpoint to continue to verify the experiment. First, decompile loader.asm to get the result on the left of the figure, and find that the arrow corresponds to loader.asm, that is, the arrow on the right.
You can set a breakpoint at the address of the corresponding point on the left to analyze the results as follows
As can be seen from the result on the right, we can do the assignment after this jump, and for the experiment, we do a single step many times, and we find that the result is consistent, which means an endless loop, so we go from real mode to protected mode. enter from 16-bit code segment to 32-bit code segment for execution.
Why don't we just use the tag to define the segment base address in the descriptor in the above code? Why do 16-bit code snippets to 32-bit code segments have to jump unconditionally? Then in assembly, NASM compiles the assembly file as a separate code segment, and the Label in the assembly code represents the offset address within the segment. In real mode, the physical address of the tag needs to be calculated with the value in the segment register, which is why we do not directly use the tag to define the segment base address in the descriptor. The code jump is due to the concept of pipelining technology in assembly. What is pipelining technology? The processor prefetches current and subsequent instructions to the pipeline in order to improve efficiency, so there may be both 16-bit and 32-bit codes in the expected instructions at the same time. In order to avoid running 32-bit code as 16-bit code, you need to refresh the pipeline, so you need to use unconditional jump jmp technology to force the refresh of the pipeline.
Summary
1.80386 processor is a milestone in the history of computer development.
2.32-bit register and address bus can directly access any corner of 4G memory.
3. The data in GDT needs to be initialized in 16-bit real mode
4. The code requires the bit GDT to define an identity data structure
5. You need to jump from a 16-bit code to a 32-bit code using the jmp instruction
three。 Real mode to protected mode (part two)
In the above experiment, we noticed the use of jmp dword Code32Selector: 0, why do we need dword, and know the role of jmp here (s16-s32)-in 16-bit code, all immediate numbers default to 16 bits, and when you jump from a 16-bit code segment to a 32-bit code, you must do a forced conversion, otherwise, the intra-segment offset address may be truncated.
In this section, you need in-depth protection mode: define the display segment, in order to display data, there must be two major hardware: video card + monitor. The video card provides the data to be displayed for the display and controls the mode and state of the display. The display displays the target data on the screen in a visible way. The concept and meaning of video memory is that the video card has its own internal data memory, which is essentially no different from ordinary memory, which is used to store target data, and the operation of the data in video memory will lead to changes in the content of the display.
There are two working modes of the graphics card-text mode and graphics mode. In different modes, the video card interprets the contents of the video memory differently. The working mode of the video card can be changed by using exclusive instructions or int 0x10 interrupts. The address range mapping bit of the video memory in the text mode is [0xB80000.xBFFFF]. A screen can display 25 lines of 80 characters each.
Text display principle of graphics card and display characters in text mode
After setting the segment base address and segment properties and printing the result, it is found that the result p will be printed on the bochs.
After printing a single character, you can further realize string printing in specified memory. The first preparation is to define a global stack segment (.gs) to protect function calls in mode, then to define a global data segment (.dat) to define read-only data, and finally to define a string printing function using the operation on the display segment.
The design of the print function can be shown in the following figure
It should be noted here that the multiplication operation in 32-bit protected mode is placed in the AX register, the multiplier is placed in the general register or memory unit (16 bits), and the result of the multiplication is placed in the EAX register; at the same time, $represents the offset of the current line from the starting position of the code, and $represents the starting position of the current section of code.
During the implementation process and the implementation result, you can see that the implementation result prints out the set string
Summary
1. 32-bit registers and 32-bit addresses can be used in real mode
two。 Video memory is the internal storage unit of the video card, which is essentially no different from ordinary memory.
3. There are two working modes for graphics cards-text mode and graphics mode
4. The data in the operating display memory unit in text mode can be immediately reflected to the display.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.