Protected Mode Question

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
beyondsociety

Protected Mode Question

Post by beyondsociety »

Does the CS and other segment registers have to be set up before you enter pmode or can it be after entering pmode?

example:

cli
lgdt [gdt_ptr]

mov ax,LINEAR_DATA_SEL ; I'm talking about this
mov ds,ax
mov es,ax
mov ss,ax
mov fs,ax
mov gs,ax ; here!

mov eax,cr0
or eax,0x1
mov cro,eax

jmp LINEAR_CODE_SEL: gdt_done

[bits 32]
[gdt_done:

; now in pmode

gdt: ; our descriptors

; NULL descriptor
dw 0 ; limit 15:0
dw 0 ; base 15:0
db 0 ; base 23:16
db 0 ; type
db 0 ; limit 19:16, flags
db 0 ; base 31:24

; unused descriptor
dw 0
dw 0
db 0
db 0
db 0
db 0

; data segment descriptor

LINEAR_DATA_SEL equ $-gdt
dw 0FFFFh ; limit 0xFFFF (1 meg, 4 gig)
dw 0 ; base for this ine is always 0
db 0
db 92h ; present, ring 0, data, expand-up, writable
db 0CFh ; page-granular (4 gig limit), 32-bit
db 0

; code segment descriptor

LINEAR_CODE_SEL equ $-gdt
dw 0FFFFh
dw 0 ; base gets set above
db 0
db 9Ah ; present,ring 0,code,non-conforming,readable
db 0CFh ; page-granular (4 gig limit), 32-bit
db 0

gdt_end:

gdt_ptr:
dw gdt_end - gdt - 1 ; GDT limit
dd gdt ; linear, physical address of GDT
roswell

Re:Protected Mode Question

Post by roswell »

Hi,

The first thing you MUST do after entering pmode is a long jump in order to initialize CS.

The data segment registers can be set only on use.

Roswell
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Protected Mode Question

Post by Pype.Clicker »

and, imho, this is plain silly to do some DS, ES ... initialisation before entering pmode, simply because this will surely *not* fill in the 'ghost' part of the registers (i.e. base, limit, etc.) but only the selector, so it won't have the expected result.
Tim

Re:Protected Mode Question

Post by Tim »

Exactly... unless CR0.PE is enabled, segment register reloads will only change the base portion to their real-mode values.

The sequence of events is:
  • LGDT
  • enable CR0.PE bit
  • far JMP to the 32-bit entry point
  • reload DS, ES, etc.
  • do whatever you want
Post Reply