OSDev.org

The Place to Start for Operating System Developers
It is currently Sun Nov 28, 2021 4:02 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: What happens with current instruction pointer in PM?
PostPosted: Sat Oct 23, 2021 7:02 am 
Offline
Member
Member

Joined: Sat Oct 23, 2021 5:36 am
Posts: 25
SOLVED

Code:
mov eax, cr0
or eax, 1
mov cr0, eax
jmp 08h:ProtectedMain


Above code is the standard way of enabling PM. After mov cr0, eax, we jump to protected mode main function, where we usually setup the protected mode and continue initializing the OS.
jmp sets CS register to 08h and IP to ProtectedMain, but before jmp is executed CS value is invalid (it comes from real-mode land)... how does the CPU know that jmp is the next instruction to be executed then?

Let's say that cs had value of 0 when we enabled PM. This means it points to first GDT entry, which is usually NULL and covers no bytes.

Are there some special rules regarding the cs (code segment register) or am I missing something?

Thanks in advance :D


Last edited by angods on Mon Oct 25, 2021 1:04 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: What happens with current instruction pointer in PM?
PostPosted: Sat Oct 23, 2021 10:04 pm 
Offline
Member
Member
User avatar

Joined: Wed Sep 28, 2005 11:00 pm
Posts: 82
The CS register has a hidden part that caches the base address. The hidden portion of the CS register is updated whenever you load the register with a new segment selector.


Top
 Profile  
 
 Post subject: Re: What happens with current instruction pointer in PM?
PostPosted: Sun Oct 24, 2021 8:33 am 
Offline
Member
Member

Joined: Wed Aug 30, 2017 8:24 am
Posts: 1132
Also, it is considered bad form to ask the same question twice in such a short amount of time. This is not Twitter. A few hours aren't bad for a response time, and generally you are going to have to allow 24h for everyone to have seen the question during their days, and more on weekends (for example, I was travelling all weekend).

_________________
Thou hast outraged, not insulted me, sir; but for that I ask thee not to beware of Starbuck; thou wouldst but laugh; but let Ahab beware of Ahab; beware of thyself, old man.


Top
 Profile  
 
 Post subject: Re: What happens with current instruction pointer in PM?
PostPosted: Mon Oct 25, 2021 12:05 pm 
Offline
Member
Member

Joined: Sat Oct 23, 2021 5:36 am
Posts: 25
nullplan wrote:
Also, it is considered bad form to ask the same question twice in such a short amount of time. This is not Twitter. A few hours aren't bad for a response time, and generally you are going to have to allow 24h for everyone to have seen the question during their days, and more on weekends (for example, I was travelling all weekend).


Sorry, I'm new to the forum and I thought I did something wrong when my previous post didn't show up after clicking "View your posts".


Top
 Profile  
 
 Post subject: Re: What happens with current instruction pointer in PM?
PostPosted: Mon Oct 25, 2021 12:10 pm 
Offline
Member
Member

Joined: Sat Oct 23, 2021 5:36 am
Posts: 25
deadmutex wrote:
The CS register has a hidden part that caches the base address. The hidden portion of the CS register is updated whenever you load the register with a new segment selector.


So the CS really uses the "hidden pointer" as it's base. And when I jump it replaces the pointer with GDT[SegmentOffset].Base?

i.e.

Code:
//Pseudo-C
void *cs;
void *ip;

void *FetchCode() {
  return cs + ip;
}

void Jump(uint16_t segment, void *new_ip) {
cs = gdt_entries[segment].base;
ip = new_ip;
}


Top
 Profile  
 
 Post subject: Re: What happens with current instruction pointer in PM?
PostPosted: Mon Oct 25, 2021 1:21 pm 
Offline
Member
Member

Joined: Wed Aug 30, 2017 8:24 am
Posts: 1132
angods wrote:
Sorry, I'm new to the forum and I thought I did something wrong when my previous post didn't show up after clicking "View your posts".

New users need Moderator approval before their posts are globally visible. The reason being that we had (have?) a bit of a spam problem, and this is one way around it.

angods wrote:
So the CS really uses the "hidden pointer" as it's base. And when I jump it replaces the pointer with GDT[SegmentOffset].Base?
Yes. This applies more generally to all segments: Their hidden parts are not updated until the descriptors are written, and when they are written, the contents of the segments are updated according to the operating mode in use at the time. This is one reason to overwrite all segments quickly after changing modes: The descriptors still read the same, so if your interrupt code pushes a segment to stack and then pops it back in (and this happens automatically for CS and SS), then the previous value might simply be invalid in the new mode. This is also a very fickle issue, so debugging it can be hard.

Not that that's a concern when switching to protected mode for the first time, since you still need to load a new IDT, and will likely only do that when the segments were reloaded.

_________________
Thou hast outraged, not insulted me, sir; but for that I ask thee not to beware of Starbuck; thou wouldst but laugh; but let Ahab beware of Ahab; beware of thyself, old man.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Google [Bot] and 25 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group