MIT 6.828 Jos Study Note 6. Appendix 1: Real Mode with protection (Protected mode)

Source: Internet
Author: User

When we read the boot loader code, we encountered two very important concepts, real mode and protected mode (protected modes).

First of all we need to know that both modes are CPU operating mode, real mode is the operating mode of early CPU operation, while the protection mode is the mode of modern CPU operation.

But why do modern CPUs still have to go into real mode when they run boot loader? This is the only way to achieve backward compatibility with the software.

Let's look at the basic principles of these two modes of work separately.

Real Mode

Real mode appears in the early 8088CPU period. Due to the limited performance of the CPU, there were only 20-bit address lines (so the address space was only 1MB), and 8 16-bit universal registers, and 4 16-bit segment registers. Therefore, in order to be able to use these 16-bit registers to form the 20-bit main memory address, must take a special way. When a directive wants to access a memory address, it usually needs to be represented in the following format:

(segment Base: Segment offset)

Where the first field is the segment base, and its value is provided by the segment register. There are 4 types of segment registers,%cs,%ds,%ss,%es. The specific segment of this instruction is determined by the type of the instruction. For example, to take instructions is to use the%CS register, to read or write data is the%DS register, if the stack operation is%SS register. In short, no matter what the instructions, there will be a segment register to provide a 16-bit segment base.

The second field is the offset within the segment, which represents the offset of the memory address you want to access from this segment base. Its value is provided by the Universal register, so it is also 16 bits. So the question is, how do the two 16-bit values combine into a 20-bit address? The method used here is to move the segment base provided by the segment register first to the left by 4 bits. This becomes a 20-bit value, which is then added to the segment offset. So the algorithm is as follows:

Physical Address = segment Base <<4 + interval offset

So suppose the value in%cs is 0xff00,%ax = 0x0110. The true physical address corresponding to this address is 0xff00<<4 + 0x0110 = 0xff110 (%cs:%ax).

Above is the principle of real mode access memory address.

Protection modes (Protected mode)

But with the development of the CPU, the number of address lines of the CPU has changed from 20 to 32, so the memory space can be accessed from 1MB to now 4GB, and the number of registers becomes 32 bits. So the memory address calculation method in real mode is no longer suitable. Therefore, the present protection mode is introduced to achieve greater space and more flexible memory access.

Before we introduce how protection mode works, we must first understand the following confusing concepts. Logical address, virtual address, linear address (linear addresses), physical addresses (physical address).

As we all know, the program is now running under the virtual address space when the program is written, that is, the address that appears in the instruction when the programmer writes the program is not necessarily the memory address that the program actually accesses when it is running in the middle of the store. The purpose of this is to allow programmers to program without having to manipulate the real address directly, because the distribution of the individual programs in memory is impossible to know when you write the program when it is actually running. So this instruction of this program actually accesses which memory unit is determined by the operating system. So this is a conversion from the virtual address to the physical address (physical addresses) in the real main memory.

So what is the logical address (logical)? According to the paragraph above, we know that the programmer is writing a virtual address, but it is not that the programmer directly writes the virtual address to the instruction. It is derived from a logical address. So the logical address that is actually present in the instruction. A logical address is made up of two parts, a segment selector (segment selector), and an offset within a segment, usually written segment:offset. and which segment selector is usually implied in the instruction, the programmer usually only needs to indicate the offset within the segment. The Segmented Management Agency (segmentation hardware) will then convert this logical address to a linear address (linear addresses). If the machine does not have a paging mechanism (paging hardware), this time linear address is the last main memory physical address. But if there is a paging device in the machine, such as the memory size is actually only 1 g, but according to the previous we know the accessible space is 4G. At this point, the paging mechanism (paging hardware) is required to translate this linear address into the final real physical address. So the meaning of the visible virtual address and the linear address is similar. We can see the address conversion process described above. In boot loader, no paging mechanism was opened. So the calculated linear address is the real memory address to be accessed.

  

So in protected mode, how do we finally get the physical address through segment:offset?

First, there are two tables in a computer, Gdt,ldt. These two are actually the same type of table, the former is called the Global Segment Descriptor table, the latter is called the local segment descriptor list. They are all used to store segmented information about a program that runs in memory. For example, where a program's code snippet starts, how big, and where the data segment starts and how big it is. The GDT table is globally visible, which means that every program running in memory can see the table. So the section information of the operating system kernel program exists inside this. There is also a LDT table, which is contained in every program in memory, which indicates the segment information for each program. We can look at the structure of the two tables, as shown in:

  

We can see that either the GDT or the LDT. Each table entry consists of three fields:

The BASE:32 bit, which represents the base address of this segment of the program.

The LIMIT:20 bit, which represents the size of this segment of the program.

The Flags:12 bit, which represents the access rights for this segment of the program.

When the logic address Segment:offset is given in the program, he is not using the segment value as the segment base as in real mode. Instead, the value of this segment is used as a selector, which represents the index of the Segment table entry in the Gdt/ldt table. For example, the address you are currently visiting is Segment:offset = 0x01:0x0000ffff, at which point the Segment table entry at address 8 should be removed because the length of each segment table entry is 8. It then first determines whether the content of this segment can be accessed based on the Flags field, so that it is possible to protect the inter-process address. If access is available, the contents of the base field are taken out and added directly to offset, and the linear address (linear addresses) is obtained. After that, the address translation is based on whether there is a paging mechanism.

For example, the value of the current base field is 0x00f0000, then the value of the last linear address is 0x00f0ffff.

As described above is the method of calculating the memory address in protected mode.

  

In summary, the above narrative shows that the protection mode is much more flexible than real mode, which can be seen in the following aspects:

1. Real mode the base address must be an integer multiple of 16, protected mode the base address can be any one address in 4GB space.

2. The length of the lower segment in the real mode is 65536B, but the length of the lower section in the protected mode is also up to 4GB.

3. Protected mode can add a layer of protection to memory access, but real mode does not.

  

What's the problem, you can send me e-mail ~

[Email protected]

MIT 6.828 Jos Study Note 6. Appendix 1: Real Mode with protection (Protected mode)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.