[Solved] The value of selector in the IDT table

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.
Post Reply
User avatar
growlnx
Posts: 15
Joined: Sun Jan 05, 2020 5:51 pm
Libera.chat IRC: growlnx
Location: 0.0.0.0
Contact:

[Solved] The value of selector in the IDT table

Post 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;
Last edited by growlnx on Fri Jan 08, 2021 12:56 am, edited 1 time in total.
Regards,
Growlnx.
Octocontrabass
Member
Member
Posts: 5568
Joined: Mon Mar 25, 2013 7:01 pm

Re: The value of selector in the IDT table

Post 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.
User avatar
growlnx
Posts: 15
Joined: Sun Jan 05, 2020 5:51 pm
Libera.chat IRC: growlnx
Location: 0.0.0.0
Contact:

Re: The value of selector in the IDT table

Post 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 
Regards,
Growlnx.
Octocontrabass
Member
Member
Posts: 5568
Joined: Mon Mar 25, 2013 7:01 pm

Re: The value of selector in the IDT table

Post 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.
User avatar
growlnx
Posts: 15
Joined: Sun Jan 05, 2020 5:51 pm
Libera.chat IRC: growlnx
Location: 0.0.0.0
Contact:

Re: The value of selector in the IDT table

Post 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:
Regards,
Growlnx.
Post Reply