Entering protected mode on x86, code segment
Posted: Sat Oct 23, 2021 5:53 am
SOLVED
When we enter the protected mode, meaning of segment registers changes to index of a GDT entry.
Let's say the GDT has 2 entries: first being a NULL entry and second covering entire address space and starting at 0
Code to enter the protected mode looks like this:
After CPU is done processing mov cr0, eax, it already is in protected mode. It means that current value CS register (assuming CS = 0) means the first GDT entry, which is the NULL descriptor.
Since the code segment descriptor is now invalid, how can the CPU continue executing instructions?
Changing the CS value before entering isn't a good solution either, because in real mode it isn't a valid CS value...
Does the CPU start using absolute addresses when we switch to protected mode? Otherwise I have no idea how it could work.
Thanks in advance
When we enter the protected mode, meaning of segment registers changes to index of a GDT entry.
Let's say the GDT has 2 entries: first being a NULL entry and second covering entire address space and starting at 0
Code to enter the protected mode looks like this:
Code: Select all
lgdt [DescriptorTable]
mov eax, cr0 ;Get current value of control register 0
or eax, 1 ;Enable the PM bit
mov cr0, eax ;Set control register 0 to value with PM bit set, enabling the protected mode
jmp 0x8:ProtectedModeMain ;How TF is this supposed to be executed?
[BITS 32]
ProtectedModeMain:
Since the code segment descriptor is now invalid, how can the CPU continue executing instructions?
Changing the CS value before entering isn't a good solution either, because in real mode it isn't a valid CS value...
Does the CPU start using absolute addresses when we switch to protected mode? Otherwise I have no idea how it could work.
Thanks in advance