Page 1 of 1

Protected Mode Question

Posted: Wed Aug 14, 2002 11:24 am
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

Re:Protected Mode Question

Posted: Wed Aug 14, 2002 1:42 pm
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

Re:Protected Mode Question

Posted: Sun Aug 18, 2002 3:21 am
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.

Re:Protected Mode Question

Posted: Sun Aug 18, 2002 10:28 am
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