Page 1 of 1
What happens with current instruction pointer in PM?
Posted: Sat Oct 23, 2021 7:02 am
by angods
SOLVED
Code: Select all
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
Re: What happens with current instruction pointer in PM?
Posted: Sat Oct 23, 2021 10:04 pm
by deadmutex
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.
Re: What happens with current instruction pointer in PM?
Posted: Sun Oct 24, 2021 8:33 am
by nullplan
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).
Re: What happens with current instruction pointer in PM?
Posted: Mon Oct 25, 2021 12:05 pm
by angods
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".
Re: What happens with current instruction pointer in PM?
Posted: Mon Oct 25, 2021 12:10 pm
by angods
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: Select all
//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;
}
Re: What happens with current instruction pointer in PM?
Posted: Mon Oct 25, 2021 1:21 pm
by nullplan
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.