Page 1 of 1

[Solved] The value of selector in the IDT table

Posted: Wed Jan 06, 2021 7:09 pm
by growlnx
Why the value of "selector" is always 0x08 in the IDT table?

Code: Select all

struct IDT_entry{
	unsigned short int offset_lowerbits;
	unsigned short int selector;
	unsigned char zero;
	unsigned char type_attr;
	unsigned short int offset_higherbits;
};
Can the "gdt_code" label in the GDT table be used instead?

Code: Select all

	
	irq0_address = (unsigned long)irq0; 
	IDT[32].offset_lowerbits = irq0_address & 0xffff;
	IDT[32].selector = 0x08; /* KERNEL_CODE_SEGMENT_OFFSET */
	IDT[32].zero = 0;
	IDT[32].type_attr = 0x8e; /* INTERRUPT_GATE */
	IDT[32].offset_higherbits = (irq0_address & 0xffff0000) >> 16;

Re: The value of selector in the IDT table

Posted: Wed Jan 06, 2021 7:20 pm
by Octocontrabass
growlnx wrote:Why the value of "selector" is always 0x08 in the IDT table?
Because 0x08 is the kernel code segment selector in the example code.
growlnx wrote:Can the "gdt_code" label in the GDT table be used instead?
No. That label will translate into the address of the code segment descriptor in memory, but you need the code segment selector.

Re: The value of selector in the IDT table

Posted: Wed Jan 06, 2021 7:55 pm
by growlnx
Because 0x08 is the kernel code segment selector in the example code.
This offset is in relation to the beginning of the GDT table, right?

Code: Select all

gdt_start:
    dq 0x00 ; 8 null bytes here        

gdt_code:    ; so here's the "kernel code segment selector" ?
    dw 0xFFFF      
    dw 0x0000       
    db 0x00         
    db 10011010b   
    db 11001111b    
    db 0x00        

.gdt_data:    ; and the "kernel data segment selector" ?
    dw 0xFFFF       
    dw 0x0000       
    db 0x00         
    db 10010010b    
    db 11001111b   
    db 0x00         
gdt_end:

; GDT descriptor
gdt_descriptor:
    dw gdt_end - gdt_start - 1 
    dd gdt_start 

Re: The value of selector in the IDT table

Posted: Wed Jan 06, 2021 8:25 pm
by Octocontrabass
growlnx wrote:This offset is in relation to the beginning of the GDT table, right?
That's a selector. The offset is encoded using the upper 13 bits of the selector, and some other information goes in the lower 3 bits. The lower 3 bits are all zero for ring 0 GDT segments, so your selectors are equal to the offset, but that's not true for all selectors.

For example, if you add a ring 3 descriptor at offset 0x18 in your GDT, you'll probably use the selector 0x1B to access it.

Re: The value of selector in the IDT table

Posted: Thu Jan 07, 2021 7:52 am
by growlnx
Octocontrabass wrote:
growlnx wrote:This offset is in relation to the beginning of the GDT table, right?
That's a selector. The offset is encoded using the upper 13 bits of the selector, and some other information goes in the lower 3 bits. The lower 3 bits are all zero for ring 0 GDT segments, so your selectors are equal to the offset, but that's not true for all selectors.

For example, if you add a ring 3 descriptor at offset 0x18 in your GDT, you'll probably use the selector 0x1B to access it.
thanks, i believe i understand now.

Some References i found about segment selector: