Protected Mode: Difference between revisions

From OSDev.wiki
Jump to navigation Jump to search
[unchecked revision][unchecked revision]
Content added Content deleted
m (Added real mode link)
m (Some more explanation and cleanup.)
Line 1: Line 1:
'''Protected mode''' is the 'native' operating mode of Intel processors (and clones) since the 80286. On 80386s and later, it allows the developer to work with several virtual address spaces, each of which has a maximum of 4GB of addressable memory and allows the system to enforce strict memory protection as well as restricting the available instruction set (so that your application cannot control the hard disk directly while the kernel can)
'''Protected mode''' is the main operating mode of modern Intel processors (and clones) since the 80286 (there in 16 bit). On 80386s and later, the 32 bit Protected Mode (henceforth just called ''Protected Mode'') allows to work with several virtual address spaces, each of which has a maximum of 4GB of addressable memory; and and enables the system to enforce strict memory and hardware I/O protection as well as restricting the available instruction set (so that your application cannot access the hardware directly; while the kernel can).


Protected mode unleashes the real power of your CPU, so you better get informed about it if you are considering writing an OS. However, it will prevent you from using virtually any of the BIOS interrupts (unless you have a V86 monitor).
A CPU that is initialized by the [[BIOS]] starts in [[Real Mode]]. Enabling Protected Mode unleashes the real power of your CPU. However, it will prevent you from using most of the BIOS interrupts, since these work in Real Mode (unless you have also written a [[Virtual 8086 Mode|V86]] monitor).


Before switch to Protected Mode, you have to disable interrupts and [[Non Maskable Interrupt|NMI]] (as suggested by Intel Developers Manual), optionally enable [[A20 Line]], and load the [[Global Descriptor Table]]
Before switching to Protected Mode, you have to disable interrupts, including [[Non Maskable Interrupt|NMI]] (as suggested by Intel Developers Manual), possibly enable the [[A20 Line]], and load the [[Global Descriptor Table]] with segment descriptors suitable for code, data, and stack.


Whether the CPU is in [[Real Mode]] or in protected mode is defined by the lowest bit of the CR0 or MSW register. Example:
Whether the CPU is in [[Real Mode]] or in Protected Mode is defined by the lowest bit of the CR0 or MSW register.


An example (TODO: explain):
<source lang="asm">
<source lang="asm">
cli ; disable interrupts
lgdt [gdtr] ; load GDT register with start address of Global Descriptor Table
mov eax, cr0
or al, 1 ; set PE (Protection Enable) bit in CR0 (Control Register 0)
mov cr0, eax


; Perform far jump to selector 08h (offset into GDT, pointing at a 32bit PM code segment descriptor)
CLI
; to load CS with proper PM32 descriptor)
LGDT [GDTR]
MOV EAX, CR0
OR AL, 1
MOV CR0, EAX

; Immediately after that you have to jump to the code segment in the GDT:


JMP 08h:PModeMain
JMP 08h:PModeMain


; [...]

PModeMain:
; load DS, ES, FS, GS, SS, ESP.
</source>
</source>


This takes you to protected mode...
This takes you to Protected Mode...


Good Luck
Good Luck

Revision as of 20:23, 6 May 2013

Protected mode is the main operating mode of modern Intel processors (and clones) since the 80286 (there in 16 bit). On 80386s and later, the 32 bit Protected Mode (henceforth just called Protected Mode) allows to work with several virtual address spaces, each of which has a maximum of 4GB of addressable memory; and and enables the system to enforce strict memory and hardware I/O protection as well as restricting the available instruction set (so that your application cannot access the hardware directly; while the kernel can).

A CPU that is initialized by the BIOS starts in Real Mode. Enabling Protected Mode unleashes the real power of your CPU. However, it will prevent you from using most of the BIOS interrupts, since these work in Real Mode (unless you have also written a V86 monitor).

Before switching to Protected Mode, you have to disable interrupts, including NMI (as suggested by Intel Developers Manual), possibly enable the A20 Line, and load the Global Descriptor Table with segment descriptors suitable for code, data, and stack.

Whether the CPU is in Real Mode or in Protected Mode is defined by the lowest bit of the CR0 or MSW register.

An example (TODO: explain):

cli          ; disable interrupts
lgdt [gdtr]  ; load GDT register with start address of Global Descriptor Table
mov eax, cr0 
or al, 1     ; set PE (Protection Enable) bit in CR0 (Control Register 0)
mov cr0, eax

; Perform far jump to selector 08h (offset into GDT, pointing at a 32bit PM code segment descriptor) 
; to load CS with proper PM32 descriptor)

JMP 08h:PModeMain

; [...]

PModeMain:
; load DS, ES, FS, GS, SS, ESP.

This takes you to Protected Mode...

Good Luck

See Also

Articles

External Links