Protected mode - when to use it?

Programming, for all ages and all languages.
Post Reply
User avatar
nitinjavakid
Member
Member
Posts: 65
Joined: Sat Oct 21, 2006 11:28 am
Location: Exams over!
Contact:

Protected mode - when to use it?

Post by nitinjavakid »

When do we use protected mode?

MS-DOS I guess ran in real mode and real mode provides access to 1 mb of memory. How was MS-DOS able to run programs(games and all) within 1 mb of memory?

Also, any good link or book that explains Protected mode in details?
Regards


Nitin
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Post by Combuster »

Protected Mode: Have you visited the wiki, yet?

As for DOS: Real mode applications are limited to the 640k area DOS provides. Programs back then werent too big. They are just loaded somewhere in memory and be executed. Long live the good old days where efficient programming and hand optimizing were standard.
The more recent applications use a dos extender, which basically loads an protected mode environment over dos so to give the application access to all memory available.
How DOS works exactly, you should ask an expert in this area.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Tyler
Member
Member
Posts: 514
Joined: Tue Nov 07, 2006 7:37 am
Location: York, England

Post by Tyler »

Unless you are experimenting with Real mode or want to learn exactly how the bios or video ram works in a basic operating enviroment... use protected mode. It is the mode of all processors since 386 (286 had slightly different form - 16bit) and Real Mode is only a compatability mode which has no use outside of writing for the 186 or compatability.

Check the wiki as said above... it told me all i needed to know and i implememnted it after one tutorial from the OS Resource Centre. Also check the intel manuals at intel under the latest processors technical documents. It tells you everythin about setting up protected mode in the way you are expected to do it by them.
User avatar
nitinjavakid
Member
Member
Posts: 65
Joined: Sat Oct 21, 2006 11:28 am
Location: Exams over!
Contact:

Post by nitinjavakid »

Thanks! Will go through those.

For programs using more than 1 mb of memory is protected mode necessary or can they be written to run in real mode?
Regards


Nitin
Midas
Member
Member
Posts: 140
Joined: Sat Jun 24, 2006 4:40 pm
Location: Falkirk, Scotland
Contact:

Post by Midas »

nitinjavakid wrote:Thanks! Will go through those.

For programs using more than 1 mb of memory is protected mode necessary or can they be written to run in real mode?
IIRC, they can still run in real mode. You can just fiddle in a similar way as those DOS extenders work (I think). You switch into protected mode, load a pmode GDT (with support for up to 4GB memory) and switch back again, without changing the descriptor table.

No?
Regards,
Angus [Óengus] 'Midas' Lepper
User avatar
nitinjavakid
Member
Member
Posts: 65
Joined: Sat Oct 21, 2006 11:28 am
Location: Exams over!
Contact:

Post by nitinjavakid »

IIRC, they can still run in real mode. You can just fiddle in a similar way as those DOS extenders work (I think). You switch into protected mode, load a pmode GDT (with support for up to 4GB memory) and switch back again, without changing the descriptor table.
Are you talking about something related to this link - http://www.osdev.org/osfaq2/index.php/BabyStep7 ?
Regards


Nitin
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Post by Combuster »

"Unreal mode" is one option, and possibly the most used one to get more out of real mode.

Also, i heard rumours one could use the DMA controller to access higher memory for you, which would give you 16 MB to work with.

You could count Virtual 8086 mode, but technically thats protected mode. Still, you do get paging for free that way.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
nitinjavakid
Member
Member
Posts: 65
Joined: Sat Oct 21, 2006 11:28 am
Location: Exams over!
Contact:

Post by nitinjavakid »

In the 'unreal mode' example,
mov bx, 0x08 ; select descriptor 1
mov ds, bx ; 8h = 1000b
Why is ds initialized to 1000b even though the selector is not being used with protected mode :?:

Also, if there is a gdt like this one

Code: Select all

gdt   dd 0,0  ; entry 0 is always unused
flatdesc    db 0xff, 0xff, 0, 0, 0, 10011010b, 11001111b, 0
flatdesc1  db 0xff, 0xff, 0, 0, 0, 10010010b, 11001111b, 0
gdt_end:
cs should be 08h and ds should be 10h because difference between flatdesc and flatdesc1 is that of 8 bytes right? or is there some other reason for that?[/code]
Regards


Nitin
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Post by Combuster »

Its a decent approximation. (and it will always work for ring 0 segments)
However, an selector is divided in three parts:

Code: Select all

0000 0000 0000 1 0 00b 
|--------------| | \/
  offset into    | RPL
  GDT            |
            Table select
the higher 13 bits define what to add to the GDT offset (or LDT) to get the requred descriptor. To form a 16 bit offset, the processor fills the 3 missing bits in as being 0s. Consequently it only picks an 8-byte aligned offset as well as preventing to be able to pick parts of two descriptors.
The RPL is a mechanism to reduce the privileges of the segment. In normal use this equals the Privilege Level (Ring): 00b for ring 0, 11b for ring 3.
Table select just says where to get the information from: the GDT (0) or the LDT (1), but LDT usage is rare.

Read intel's Big Book of Patterns for a more elaborate description.

As for the other question: the processor only loads the hidden parts of the segment registers when the processor is in protected mode. So DS is loaded to force the hidden limit register to be set to 4GB. After that, protected mode is left and DS is loaded again, but since we are back in real mode the limit is not loaded, so it stays 4GB. The result: Unreal mode
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
nitinjavakid
Member
Member
Posts: 65
Joined: Sat Oct 21, 2006 11:28 am
Location: Exams over!
Contact:

Post by nitinjavakid »

Great! Now some of my concepts are getting cleared. Guess whats my next question is?

How to create a new gdt without lossing the existing ones. For example,
gdt1:
gdt1 ...............
gdt2 ...............
gdtend1:
this one is from x to y address of memory and has been loaded by using lgdt.

Now suppose I am loading a program.How do I assign a seperate memory space cs,ds to the new program? The result should be like this
Program 1(Already loaded one)
cs: 00001000
ds: 00010000

program 2(new one)
cs: 00011000
ds: 00100000

Is it possible to create something like this or do I have to use lgdt again and again to keep on shifting continuously?
Regards


Nitin
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Post by Brendan »

Hi,
nitinjavakid wrote:Great! Now some of my concepts are getting cleared. Guess whats my next question is?
There's an old saying: "Give a man a fish; you have fed him for today. Teach a man to fish; and you have fed him for a lifetime". Would you like a fish, or do you want to learn how to fish? In other words, do you want us to answer this question, or teach you how to find the answer yourself?

Have you read and understood the manuals for the CPU you're trying to write software for?

Have you considered using paging and read about all it's details, and pondered the possible uses of the LDT, and then formed an educated descision to use the GDT for application's segments (instead of having an address space with the same segments re-used by all applications, and/or LDT for each application)?


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
User avatar
nitinjavakid
Member
Member
Posts: 65
Joined: Sat Oct 21, 2006 11:28 am
Location: Exams over!
Contact:

Post by nitinjavakid »

Unfortunately because of my semester exams(starting from 21st Nov) I was unable to read the manuals thoroughly :( . You are right; I must start reading the manuals(thoroughly). So, I guess I will have to wait till 19th Dec (when the exams end). Thanks for the advice. I am switching myself to sleep mode till 19th Dec.
Regards


Nitin
Post Reply