Protected Mode Question
- matias_beretta
- Member
- Posts: 101
- Joined: Mon Feb 26, 2007 3:39 pm
Protected Mode Question
Hello, I know there are lots of posts about protected mode, but I can't understand what is the gdt.. and how can i go to protected mode....
thanks
thanks
MatÃas Beretta
- matias_beretta
- Member
- Posts: 101
- Joined: Mon Feb 26, 2007 3:39 pm
Going to PMode is easy and only requires some simple steps, but the thing that most coders find hard is once your in pmode your on your own with no BIOS int's to help you.
Here's the simplest way to go to Pmode:
Here's the simplest way to go to Pmode:
Code: Select all
org 0x7C00
use16
;****************************
; Realmode startup code.
;****************************
start:
xor ax,ax
mov ds,ax
mov es,ax
mov ss,ax
mov sp,0x7C00
;*****************************
; Setting up, to enter pmode.
;*****************************
cli
lgdt [gdtr]
mov eax, cr0
or al,0x1
mov cr0,eax
jmp 0x10: protected
;*****************************
; Pmode. ;-)
;*****************************
use32
protected:
mov ax,0x8
mov ds,ax
mov es,ax
mov ss,ax
mov esp,0x7C00
;*****************************
; Turn floppy off (if space).
;*****************************
mov dx,3F2h
mov al,0
out dx,al
;*****************************
; Print a "P" and loop.
;*****************************
mov edi,0xB809A
mov byte [es:edi],'P'
jmp $
;*************************************
; GDT.
;*************************************
gdt: dw 0x0000, 0x0000, 0x0000, 0x0000
sys_data: dw 0xFFFF, 0x0000, 0x9200, 0x00CF
sys_code: dw 0xFFFF, 0x0000, 0x9800, 0x00CF
gdt_end:
gdtr: dw gdt_end - gdt - 1
dd gdt
;*************************************
; Make program 510 byte's + 0xaa55
;*************************************
times 510- ($-start) db 0
dw 0xaa55
- matias_beretta
- Member
- Posts: 101
- Joined: Mon Feb 26, 2007 3:39 pm
-
- Member
- Posts: 45
- Joined: Fri Jul 20, 2007 1:39 am
No the (only one) GDT contains entries for Segments....But nowadays the people usually don't usw segments. They only have one Segment for Date and one for Code for Ring0 and perhaps Ring3.
Later perhaps one Deskriptor for TSS joins, when you want to usw multitasking with Tasks in more then one Ring.
Later perhaps one Deskriptor for TSS joins, when you want to usw multitasking with Tasks in more then one Ring.
PMode is quite simple, really, i would recommend you do use it
the GDT, as was mentioned, does control segments
think of it as RMode, except the length and starting address of each segment can be controlled by the GDT
most people do only set a single data and a single code segment for each ring they intend to use (usually only 0 and 3) and an entry pointing to the TSS
there are a lot of things that appear complicated about PMode, but its not that hard... and we are all here to help if you need it...
to start, read intel manuals 3A: chapters 2-4, as these chapters do a very good job explaining the various structures you need, and how memory is managed (hint: you will probably want to use a modified form of flat mode (3A:3.2.1), you will need a GDT, IDT, TSS (only a few entries will probably be used), and page tables)
once you have read these 3 chapters, read them again, then post any questions you may have
good luck!
the GDT, as was mentioned, does control segments
think of it as RMode, except the length and starting address of each segment can be controlled by the GDT
most people do only set a single data and a single code segment for each ring they intend to use (usually only 0 and 3) and an entry pointing to the TSS
there are a lot of things that appear complicated about PMode, but its not that hard... and we are all here to help if you need it...
to start, read intel manuals 3A: chapters 2-4, as these chapters do a very good job explaining the various structures you need, and how memory is managed (hint: you will probably want to use a modified form of flat mode (3A:3.2.1), you will need a GDT, IDT, TSS (only a few entries will probably be used), and page tables)
once you have read these 3 chapters, read them again, then post any questions you may have
good luck!
In addition to the above, you might find this helpful:
Clicky
This covers descriptor tables, the gdt, protected mode (pmode) memory addressing (Flat memory model), and entering protected mode.
Clicky
This covers descriptor tables, the gdt, protected mode (pmode) memory addressing (Flat memory model), and entering protected mode.
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
Re: thanks
In there most basic form you can think of them as 4GB segments, 1 for code and the other data (you also need a NULL (all zeroes) as the first descriptors), as shown in my example.matias_beretta wrote:so gdt are the segments for the programs????
Re: thanks
Hi,
In real mode there's segment registers (CS, SS, DS, ES, etc), where the base address of the segment is calculated as "base = segment * 16", and the segments are always 16-bit, and the segment limits are always 64 KB.
Now, what if you used the segment registers as an index into a large array instead? In this case the entry in the array could be used to determine the base address of the segment, if the segment is 16-bit or 32-bit, and the segment limit (and a few other things, like the protection level, segment type, etc).
This large array is the GDT, and the entries in the array are called "descriptors" (as they describe segments).
To be more accurate, there's actually 2 arrays of descriptors - one called the GDT that's meant to be used for all software, and one called the LDT. The LDT is optional, and it was intended that each application/process would have it's own LDT that described "process specific" segments. Also, some descriptors in the GDT are used to describe other things (like call gates and TSSs), but don't worry about them yet.
Like others have pointed out, most OSs don't use many descriptors in the GDT (and don't have any LDT at all) - it's easier to setup descriptors so that there's huge segments (e.g. segments with base = 0 and limit = 4 GB), and so that everything can be accessed without changing segment registers. In this case you can (mostly) forget about segmentation, which can make it easier to use than real mode (e.g. application programmers don't need to know that CS, SS, ES, DS, etc exist).
The tricky part about protected mode isn't the GDT or segmentation - that's fairly easy. The tricky part is all about protecting pieces of software from each other (which isn't even possible in real mode). The other thing that might be annoying is that the BIOS functions are designed for real mode and can't be used in protected mode (but IMHO most of the BIOS functions aren't worth using anyway).
Cheers,
Brendan
I've simplified things, but...matias_beretta wrote:so gdt are the segments for the programs????
In real mode there's segment registers (CS, SS, DS, ES, etc), where the base address of the segment is calculated as "base = segment * 16", and the segments are always 16-bit, and the segment limits are always 64 KB.
Now, what if you used the segment registers as an index into a large array instead? In this case the entry in the array could be used to determine the base address of the segment, if the segment is 16-bit or 32-bit, and the segment limit (and a few other things, like the protection level, segment type, etc).
This large array is the GDT, and the entries in the array are called "descriptors" (as they describe segments).
To be more accurate, there's actually 2 arrays of descriptors - one called the GDT that's meant to be used for all software, and one called the LDT. The LDT is optional, and it was intended that each application/process would have it's own LDT that described "process specific" segments. Also, some descriptors in the GDT are used to describe other things (like call gates and TSSs), but don't worry about them yet.
Like others have pointed out, most OSs don't use many descriptors in the GDT (and don't have any LDT at all) - it's easier to setup descriptors so that there's huge segments (e.g. segments with base = 0 and limit = 4 GB), and so that everything can be accessed without changing segment registers. In this case you can (mostly) forget about segmentation, which can make it easier to use than real mode (e.g. application programmers don't need to know that CS, SS, ES, DS, etc exist).
The tricky part about protected mode isn't the GDT or segmentation - that's fairly easy. The tricky part is all about protecting pieces of software from each other (which isn't even possible in real mode). The other thing that might be annoying is that the BIOS functions are designed for real mode and can't be used in protected mode (but IMHO most of the BIOS functions aren't worth using anyway).
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.