Doubts on the first instruction after enabling ProtectedMode
Posted: Sat May 16, 2020 10:15 am
I am new to kernel and I'm currently re-reading xv6's source code.
I find that sth I used to assume right seems very confusing now...
Here is the code snippet:
---------------------------------------------------------------
lgdt gdtdesc
movl %cr0, %eax
orl $CR0_PE, %eax
movl %eax, %cr0
# Complete the transition to 32-bit protected mode by using a long jmp
# to reload %cs and %eip. The segment descriptors are set up with no
# translation, so that the mapping is still the identity mapping.
ljmp $8, $start32
---------------------------------------------------------------
Before enabling protected mode, cs=0, we work in real mode, cs*16+ip works just fine.
What makes me confused is that, how to make sure the 1st instruction getting executed after enabling protected mode is the ljmp above? AFAIK now, when protected mode is on, processor will use hidden/invisible content of cs register to perform the logical address -> linear address translation -- but the hidden content seems never set before...
Currently I can only assume that the hidden content is 'SOMEHOW' set by 'SOMEONE' so that when protected mode is on, hidden content for cs gives us BASE=0 and LIMIT=0xFFFFF.
Can someone please explain me the exact detail make the 'ljmp $8, $start32' the 1st instruction after enabling protected mode?
I find that sth I used to assume right seems very confusing now...
Here is the code snippet:
---------------------------------------------------------------
lgdt gdtdesc
movl %cr0, %eax
orl $CR0_PE, %eax
movl %eax, %cr0
# Complete the transition to 32-bit protected mode by using a long jmp
# to reload %cs and %eip. The segment descriptors are set up with no
# translation, so that the mapping is still the identity mapping.
ljmp $8, $start32
---------------------------------------------------------------
Before enabling protected mode, cs=0, we work in real mode, cs*16+ip works just fine.
What makes me confused is that, how to make sure the 1st instruction getting executed after enabling protected mode is the ljmp above? AFAIK now, when protected mode is on, processor will use hidden/invisible content of cs register to perform the logical address -> linear address translation -- but the hidden content seems never set before...
Currently I can only assume that the hidden content is 'SOMEHOW' set by 'SOMEONE' so that when protected mode is on, hidden content for cs gives us BASE=0 and LIMIT=0xFFFFF.
Can someone please explain me the exact detail make the 'ljmp $8, $start32' the 1st instruction after enabling protected mode?