--->Diese Seite gibts auch in Deutsch.

General description of Real Mode, Protected and V86 Mode

Content:

Real Mode

Real Mode is the addressing mode in which the CPU behaves like being a 8086. For compatibility reasons, all x86 CPUs start in this mode.

The 8086 has 20 address lines, which means that it can access a maximum of 2^20 = 1048576 = 1024 K = 1MB bytes of RAM. For addressing it, the 8086 has two kinds of registers, both 16 bit wide: The one are called segment registers. They point to an address somewhere in the memory which is a multiple of 16. The other registers are called index or offset registers. They point to an address between 0 and 2^16=65536 RELATIVE to an address residing in a segment register.


The picture shows this graphically:
Graphic showing how to access RAM in 8086 Real Mode
The physical address (the one which appears on the signal lines of your mainboard) is the same as the logical address (the address you can see in your program). The physical address can be calculated as
Value_in_segment_register * 16 + Value_in_offset_register. You may realize that a program can access the whole address space of the CPU by simply loading other segment register values.
There is no protection against programs which can overwrite the operating system and causing the system to crash.

286 Protected Mode

The 286 contains several major improvements:
These improvements are only available in protected mode.

Privilege levels

Privilege levels ( PL )(they are often called rings, but I think this terminology is terribly stupid) have been implemented for ensuring that user programs cannot destroy the operating system or crash the computer. Privilege Level 0 means that the program can execute all CPU instructions. The operating system has this PL. Other programs have higher PLs, normal user programs are typically assigned PL3. The lower the PL is, the more the program is allowed to do. Programs running in low PLs are considered as being secure and safe while programs with high PLs may be potentially buggy.

Programs are only allowed to call functions which have the same or lower PL, this prevents system programs from using potentially unsafe code. It is also possible to prohibit port accesses (printer, keyboard, harddisk, soundcard,...) for several privilege levels.

If a program does something it is not allowed to do, the operating system will be called by the CPU for solving the problem (it has to carry out or deny the port access, kill the program or present an error message etc.).

Memory management in protected mode

In Real Mode, the memory was addressed by a segment register containing a start address and an index register which points to the address relatively to the address contained in the segment register.
In Protected Mode index registers are used in the same way, but the role of the segment register has completely changed: It does no longer point to a physical address. Instead it is used as an index, too - an index into a table which has been created somewhere in the RAM by the OS. This table contains the descriptions of the memory areas being accessed by the programs running. One of these descriptions - they are called descriptors and the table is therefore called descriptor table- contains of the following elements:

At all, memory is accessed by the following way:
description of 286 pmode memory access

One can realize that the logical address consisting of the segment register (now called selector coz it selects a descriptor within the table) and the index has nothing to do with the physical address. Thus it is also called a virtual address. The independence of virtual and physical addresses allows the OS to move the memory block to another physical position without causing trouble in the program using this block (this method is called paging).

386 Protected Mode extensions

The 386 introduced the following improvements:

The main difference between 286 and 386 in Protected Mode is that memory blocks can be bigger than on the 286 and that the starting address can be bigger than 16MB. Doing so, using a 386 is quite the same as using a 286.

Even today, more than a decade since the introduction of the 386, computers use only a small percentage of the 4 GB of RAM which can be used together with a 386. Paging on the 286 was easy, every computer had enough RAM to load and store a complete logical memory block into physical RAM. But the 386 allows these blocks to be up to 4GB large - more than most systems have. The problem was solved by including a separate paging unit into the 386.

386 paging unit

This new processor extension operates completely separated from the normal Protected Mode memory addressing unit. The 4GB address space is divided into small chunks which can be moved around inside the RAM or they can be removed from RAM and stored onto the harddisk freeing RAM for other chunks.

Because it has nothing to do with the selector-offset addressing even programs creating or changing their own descriptors will not be able to recognize if paging is active or not. Memory access is now a three-layer thing:

Picture showing how paging extends Protected Mode on 386

Note: Of course, the memory described in the descriptor itself may be paged out there (see 286 part).

Virtual V86 extension

Although Protected Mode is a powerful thing, Real Mode applications were still famous. Because running Real Mode applications in Protected Mode was very problematic to realize, the V86 Mode was included into the 386.
This mode allows trapping specific I/O ports for emulating them in software and code running in V86 uses the same addressing method like in Real Mode. This allows a Real Mode emulation within protected mode for one or more programs running at the same time.

The only difference to pure Real Mode is that these programs are only allowed to use the instructions available in Privilege Level 3. The simulated Real Mode memory can be mapped everywhere inside the Protected Mode address space, including memory swapping. All accesses to the hardware interfering with other applications have to be handled by the OS.

Multitasking

Multitasking is executing several pieces of code simultaneously. As x86 CPU cores can only execute one program thread at a certain time they have to switch between all programs running so fast that it looks like they are running parallel.

A lot of help by the OS is needed for multitasking, also depending on the kind and complexity of the task switching implemented. There are two different types of multitasking: Cooperative multitasking is realized in the way that a program is called, it executes its part of code and then it gives the control back to the OS for the next program. The problem is that this realization can lock up easily even if only one program is buggy.
The other implementation uses frequently occurring events like the system timer ticks for switching between the programs. This is much safer than the other version. However, the programs are interrupted asynchronously. So every switch needs a lot of organization overhead to backup and restore the complete state of the processor.

Many people think multitasking needs a 286 or higher. In fact, it may also be possible to do multitasking on an 8086. This is more theoretical, not only due to CPU speed and memory limits, but mainly because you need the Protected Mode for preventing programs from disturbing other programs or the OS. And the 286 and especially the 386 have a lot of goodies making the switches faster and easier.

Further reading

Michael Chourdakis has written an article called The Real,Protected,Long mode assembly tutorial for PCs with source code.
Also check out the pmode sources in the coding links.

Mail the Author: webmeister@deinmeister.de

Homepage Programming Win32Asm Downloads Software Hardware Cartoons Sitemap