News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Stupid question on protected mode

Started by bf2, November 28, 2011, 11:18:46 PM

Previous topic - Next topic

bf2

First off, I really don't know what I am talking about here - just trying to get my head round to the subject, so please be gentle.

Is 'protected mode' a feature of the CPU, or the OS, or both?

I ask because according to the Wikipedia article on protected mode, its features include paging, multitasking privilege levels (rings) - all sound suspiciously like OS features.

On the other hand the article starts with "In computing, protected mode...is an operational mode of x86-compatible central processing units."

And the Intel manuals have tons and tons of information on protected mode.

mineiro

A feature of processor, started with 286 processors. In 386 processors you can exchange betwen these 2 modes.
A program that runs under ms-dos, can exchange (switch) to protected mode, to use some features, and go back again to real mode if they need use some ms-dos feature. In this case, is used a program DPMI (dos protected mode interface) to do this switch betwen modes.
Every time a program need use a ms-dos sub-rotine, it send a command to this switcher and wait. The switcher, switch the processor to real mode, execute that command, switch again to protected and send the result to the program. The program does not know about this, is transparent.
A DPMI example is dos4gb.exe, used by some games.
...
In real mode, the 286 acts like a 8086 but more quickly and maintain compatibility. In protected mode, it show their power, like can access 16mb of ram, multitask, virtual memory in disk and memory protection.

Farabi

Yes it is another name of 32-bit mode which is every memory region is segmented so every data is protected and secured. The rule is FBR: First Booted Ruled.
Those who had universe knowledges can control the world by a micro processor.
http://www.wix.com/farabio/firstpage

"Etos siperi elegi"

hutch--

bf2,

The distinction is between REAL mode and PROTECTED mode memory. The first (REAL mode) is generally only accessed by one program at a time as in the old 16 bit MS_DOS but with the advent of multitasking where you have 2 or more programs running at the same time, controlling access to memory became critical. In 16 bit Windows two different programs could access the same memory at the same time which usually meant that one program overwrote critical parts of the other which caused it to crash and often taking the old 16 bit Windows with it.

With the advent of 32 bit Windows the operating system controls memory access so if you allocate memory and then either read or write past the end of it the operating system halts your program and displays a general protection fault. Protected mode memory works like this. It is protected in that if a running program tries to read or write beyond memory that it has allocated, the operating system "protects" memory space by not allowing it.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

bf2

Quote from: hutch-- on November 29, 2011, 05:27:25 AM
bf2,

The distinction is between REAL mode and PROTECTED mode memory. The first (REAL mode) is generally only accessed by one program at a time as in the old 16 bit MS_DOS but with the advent of multitasking where you have 2 or more programs running at the same time, controlling access to memory became critical. In 16 bit Windows two different programs could access the same memory at the same time which usually meant that one program overwrote critical parts of the other which caused it to crash and often taking the old 16 bit Windows with it.

With the advent of 32 bit Windows the operating system controls memory access so if you allocate memory and then either read or write past the end of it the operating system halts your program and displays a general protection fault. Protected mode memory works like this. It is protected in that if a running program tries to read or write beyond memory that it has allocated, the operating system "protects" memory space by not allowing it.

But Hutch, all this is happening at the OS level. It's the OS that is managing the address spaces of the processes. Where does the CPU come into it?

BogdanOntanu

Quote from: bf2 on November 29, 2011, 06:38:07 AM
Quote from: hutch-- on November 29, 2011, 05:27:25 AM
bf2,

The distinction is between REAL mode and PROTECTED mode memory. The first (REAL mode) is generally only accessed by one program at a time as in the old 16 bit MS_DOS but with the advent of multitasking where you have 2 or more programs running at the same time, controlling access to memory became critical. In 16 bit Windows two different programs could access the same memory at the same time which usually meant that one program overwrote critical parts of the other which caused it to crash and often taking the old 16 bit Windows with it.

With the advent of 32 bit Windows the operating system controls memory access so if you allocate memory and then either read or write past the end of it the operating system halts your program and displays a general protection fault. Protected mode memory works like this. It is protected in that if a running program tries to read or write beyond memory that it has allocated, the operating system "protects" memory space by not allowing it.

But Hutch, all this is happening at the OS level. It's the OS that is managing the address spaces of the processes. Where does the CPU come into it?

Nope, The CPU has this features implemented in hardware (protection, rings, paging, modes) and all this is controlled by some special instructions LIDT, LGDT, special registers (CR0, CR3, etc) special tables like page tables, etc.

The OS is simply keeping track of all this (and it is not so simple or easy to do so).

For example when an application in ring 3 tries to access an invalid memory area an exception occurs in the CPU. The CPU makes an internal switch from ring 3 to ring 0 (in order to  allow special operations by the OS kernel) and invokes an special exception handler code locate din the OS kernel. This OS code will at some moment trigger the display of a window on screen saying something like" ...this program has encountered  a general protection fault at xxxxxx"

Then the OS will kill the application and release the resources in use by it (memory, synchronization objects, open files and handles).

In conclusion the OS is a set of software procedures that runs in ring 0 (kernel mode) and keeps track of special events and hardware resources allocated to applications that are executed in ring 3 (user mode) BUT the protection mechanism and the MMU paging mechanism and the rings are a feature of the Intel CPU hardware by itself.

Ambition is a lame excuse for the ones not brave enough to be lazy.
http://www.oby.ro

bf2


vanjast

In Protected Mode, there are instructions available that are not allowed in real mode.
These instructions are generally for 'management and control'

An OS uses the management instructions to manage the system...hence the RingX security structures of Windoze
I'm trying to remember all this from the dark ages... :bg