Page 1 of 1

How to set selector in IDT descriptor?

Posted: Thu Jun 11, 2020 10:50 am
by antoni
I wrote a lot of 32-bit operating systems using different guides so far. Now I'm writing my own 64-bit OS basing on my own experience and osdev wiki. I cut myself on setting up IDT. In IDT entry, there is such value, "selector". It has to point to an entry in GDT and has only 16 bits... Does this mean that my IDT must be in the first 65536 bytes? I had to move something more than once, so it seems quite likely to me, but still I ask...

Re: How to set selector in IDT descriptor?

Posted: Thu Jun 11, 2020 11:32 am
by iansjack
It's a code segment selector - i.e. the value to be loaded in CS. So it's an index into the GDT.

This doesn't affect where you place the IDT.

Have you read the Intel manuals?

Re: How to set selector in IDT descriptor?

Posted: Thu Jun 11, 2020 12:47 pm
by PeterX
antoni wrote:Does this mean that my IDT must be in the first 65536 bytes?
No.
Grab a protected mode tutorial and learn descriptor tables (GDT, LDT, IDT) and such. Then you know what a selector is and how to setup GDT and IDT. It's worth the effort, really!

It's at a first sight complicated. But actually it's not that difficult.

Happy hacking
Peter

Re: How to set selector in IDT descriptor?

Posted: Fri Jun 12, 2020 10:33 am
by antoni
I already know what a selector is, but I still have a problem with the IDT setup. I tried to run "GNU Assembler" example from this page - https://wiki.osdev.org/IDT_problems#Assembly_Examples from "Assembly Examples" section. It looks like this:

Code: Select all

.text
int_handler:
    movq $0x123abc, 0x0 // this places magic value "0x123abc" at the beginning of memory
    hlt
 
.p2align 4
idt:
    .skip 50*16
 
  idtr:
    .short (50*16)-1
    .quad idt
 
.globl do_test
do_test:
    lidt idtr
    movq $int_handler, %rax
    mov %ax, idt+49*16
    movw $0x20, idt+49*16+2 // replace 0x20 with your code section selector
    movw $0x8e00, idt+49*16+4
    shr $16, %rax
    mov %ax, idt+49*16+6
    shr $16, %rax
    mov %rax, idt+49*16+8
    int $49
Of course, I previously changed 0x20 to 8. In my GDT I only have 3 entries: null, code and data, so the code selector should be 8. I don't know why it doesn't work. Could it be because I copied the hardware IRQ's rerouting procedure from my 32-bit OS? It looks like this:

Code: Select all

BITS 32

section .text

global reroute_irqs
reroute_irqs:

        cli
        in      al,0x21
        mov     ah,al
        in      al,0xA1
        mov     cx,ax

        mov     al,0x11
        out     0x20,al
        out     0xEB,al
        out     0xA0,al
        out     0xEB,al

        mov     al,0x20
        out     0x21,al
        out     0xEB,al

        add     al,0x8
        out     0xA1,al
        out     0xEB,al

        mov     al,0x04
        out     0x21,al
        out     0xEB,al
        shr     al,1
        out     0xA1,al
        out     0xEB,al
        shr     al,1
        out     0x21,al
        out     0xEB,al
        out     0xA1,al
        out     0xEB,al

        mov     ax,cx
        out     0xA1,al
        mov     al,ah
        out     0x21,al
        mov     ecx,0x1000
        cld

picl1:

        out     0xEB,al
        loop    picl1

        cli
        mov     al,255
        out     0xa1,al
        out     0x21,al

        ret

I have no idea where I got it from, but it worked in 32-bit system ...

Re: How to set selector in IDT descriptor?

Posted: Fri Jun 12, 2020 10:42 am
by PeterX
Forget what I wrote. My apologize.

Greetings
Peter