What' the address space like when switching to protect mode?

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
yuq
Posts: 17
Joined: Tue Jan 11, 2011 2:06 am

What' the address space like when switching to protect mode?

Post by yuq »

I know how to switch to protect mode in x86.

Code: Select all

; set PE bit
mov cr0, eax
or eax, 1
mov eax, cr0

; I need to insert code here

; far jump (cs = selector of code segment)
jmp cs:@pm

@pm:
  ; Now we are in PM.
But now I need to insert some code just before far jump and after "mov eax,cr0". So I'm confused by what's the address space like here, has the GDT take effect already?
User avatar
qw
Member
Member
Posts: 792
Joined: Mon Jan 26, 2009 2:48 am

Re: What' the address space like when switching to protect m

Post by qw »

You are in Protected Mode the moment you change CR0, but the old base and limit of CS are still in use until you reload CS. In real mode, and before reloading CS, the base is 16 times the value of the segment register and the limit is 65535.

The CPU reads the base, limit and attributes from the GDT the moment CS is loaded with the new value, that is, during the processing of the far jump. So you should make sure that the base is the same as it was in real mode otherwise the offset of "@pm" will be incorrect.

BTW you'd better not call the selector "cs" because it will confuse your assembler.

P.S. Watch the order of the operands. Is it Intel or AT&T syntax you are using?
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:

Re: What' the address space like when switching to protect m

Post by Combuster »

Entering protected mode does not change memory mapping of itself - imagine that execution goes off to somewhere else the moment you set PE. Instead it only changes how instructions related to protection and segmentation are interpreted.

Basically, whatever code follows depends on the behaviour of the Descriptor Cache.
"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 ]
yuq
Posts: 17
Joined: Tue Jan 11, 2011 2:06 am

Re: What' the address space like when switching to protect m

Post by yuq »

Thank you. According to your saying, I'm in protect mode because I've set the PE bit. But I have not reload CS, and there's no old global descriptor because I was in real mode before. Further more, my CS may be 0x1234 in real mode which may not be a valid entry in GDT. So what's the address strategy here? And how does the CPU find the next instruction after enable PE (which is far jump here) when CS has no mean before reloading in protect mode?
yuq
Posts: 17
Joined: Tue Jan 11, 2011 2:06 am

Re: What' the address space like when switching to protect m

Post by yuq »

Oh I'm sorry! I miss-understanded what you two guys' saying. Yes, I know the answer just like your saying, thank you. :D
yuq
Posts: 17
Joined: Tue Jan 11, 2011 2:06 am

Re: What' the address space like when switching to protect m

Post by yuq »

I understand the address strategy here: if CS is 0x1234 in real mode, then here I got cached descriptor base 0x12340 with limit 0xffff. But CS has lest bits for memory protection, here the CS is still 0x1234 before reload, will the lest bits take effect here? If it is, may be I should make sure to make these bits of CS cleared for kernel mode in real mode?
landmine
Posts: 1
Joined: Tue Apr 05, 2011 8:44 pm

Re: What' the address space like when switching to protect m

Post by landmine »

Segment privilege levels are only checked when loading the selectors.
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:

Re: What' the address space like when switching to protect m

Post by Combuster »

Before you try to consider any more things about segmentation: when did you modify the descriptor cache? Segment behaviour does not change until you explicitly change it. Between setting PE and changing CS nothing touches the descriptor cache, so nothing changes how addresses are translated.
"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 ]
Post Reply