General description of Real Mode, Protected and V86 Mode

Content:

Real Mode

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

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


The picture shows this graphically:
Graphic showing how to access RAM in 8086 Real Mode
The physical adress (the one which appears on the signal lines of your mainboard) is the same as the logical adress (the adress you can see in your program). The physical adress can be calculated as
Value_in_segment_register * 16 + Value_in_offset_register. You may realize that a programme can access the whole adress 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 programmes cannot destroy the operating system or crash the computer. Privilege Level 0 means that the programme can execute all CPU instructions. The operating system has this PL. Other programmes have higher PLs, normal user programmes are typically assigned PL3. The lower the PL is, the more the programme is allowed to do. Programmes running in low PLs are considered as being secure and safe while programmes with high PLs may be potentially buggy.

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

If a programme 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 programme or present an error message etc.).

Memory management in protected mode

In Real Mode, the memory was adressed by a segment register containing a start adress and an index register which points to the adress relatively to the adress 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 adress. 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 programmes 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 programme 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 adress 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 programmes 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 programmes running at the same time.

The only difference to pure Real Mode is that these programmes 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 CPUs can only execute one programme at a certain time they have to switch between all programmes 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 programm is called, it executes its part of code and then it gives the control back to the OS for the next programme. The problem is that this realization can lock up easily even if only one programme is buggy.
The other implementation uses frequently occuring events like the system timer ticks for switching between the programmes. This is much safer than the other version. However, the programmes 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 programmes from disturbing other programmes or the OS. And the 286 and especially the 386 have a lot of goodies making the switches faster and easier.


Back to the coding index    Mail the author: mailto:webmaster@deinmeister.de?subject=Homepage