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.
Intel System Programming guide states that segment selector consists of:
Index (Bits 3 through 15) — Selects one of 8192 descriptors in the GDT or LDT. The processor multiplies
the index value by 8 (the number of bytes in a segment descriptor) and adds the result to the base
address of the GDT or LDT (from the GDTR or LDTR register, respectively).
However the GDT Tutorial and lots of example and OS's source code suggests that segment selector is a byte offset from the descriptor table's beginning. That is first valid selector is 0x08, next is 0x10 and so forth.
The second approach actually works but what about Intel Manual? Which is it really: index or byte offset?
The manuals are technically correct since the low 3 bits of segment registers store the RPL or CPL and TI bit. However, it can also be used as an offset when you ignore the low 3 bits.
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
Selector is 0x8
This is binary 0000000000001000.
The index is bits 3-15 - 0000000000001, i.e decimal 1, so the index is 1.
Multiply this by 8 and you get the offset into the GDT - exactly what the Intel manual says.
Thank you for the reply, that is actually a very confusing accident (or not?). However this means that it's actually an index (in 3-15 bits) otherwise it would be wrong when lower bits are used.
Someone (maybe I will) should probably describe the selector in more detail in the wiki, because currently it just contains hardcoded values without much explanation about their origin.
It should probably be "promoted" to a real Wiki page, though.
Project: OZone
Source: GitHub
Current Task: LIB/OBJ file support
"The more they overthink the plumbing, the easier it is to stop up the drain." - Montgomery Scott