Page 1 of 1

Interrupt Issues

Posted: Tue Mar 09, 2021 7:17 pm
by Simponic
Hello everyone, I am trying to write a 32 bit protected mode operating system from scratch to learn about stuff. I took a break after getting my GDT set up, and now I am trying to get interrupts working.

For the life of me, I cannot figure out what I am doing wrong. I am almost certain I set up the descriptor tables right, but if someone could look at the code, it is available at https://github.com/Simponic/SimponicOS.

When I try to run my kernel with the interrupt in the kernel, I get really weird glitches all over the screen, text popping into and out of existence. It is really weird.

Image

Re: Interrupt Issues

Posted: Wed Mar 10, 2021 7:03 pm
by Octocontrabass
What kind of debugging have you done so far?

It looks like you're using QEMU. Try adding "-no-reboot" and "-d int" to your command line. (You may also need to disable hardware acceleration.)

Re: Interrupt Issues

Posted: Wed Mar 10, 2021 8:42 pm
by AndrewAPrice
Nothing super obvious stands out.

I agree with Octocontrabass to add "-no-reboot -d int" and see what QEMU says.

Btw, how did you make the animated gif?

Re: Interrupt Issues

Posted: Thu Mar 11, 2021 12:26 am
by Simponic
Ok I will try that

I made the gif by recording with "peek".

Re: Interrupt Issues

Posted: Thu Mar 11, 2021 12:30 am
by Simponic
Here https://pastebin.com/kfFpjYSZ is the output of

Code: Select all

qemu-system-i386 -kernel os.bin -no-reboot -d int

Re: Interrupt Issues

Posted: Thu Mar 11, 2021 1:20 am
by Octocontrabass

Code: Select all

     0: v=01 e=0000 i=1 cpl=0 IP=0008:00101091
It reached your INT 1 instruction.

Code: Select all

     1: v=0d e=000a i=0 cpl=0 IP=0008:00101091
#GP(0x000A) - there is a problem with your IDT entry for interrupt 1.

Code: Select all

     2: v=08 e=0000 i=0 cpl=0 IP=0008:00101091
#DF - There is also a problem with your IDT entry for #GP.

There is also a problem with your IDT entry for #DF, so the CPU triple faults.

How did you come up with [1*8 - 1]?

Re: Interrupt Issues

Posted: Thu Mar 11, 2021 9:06 pm
by Simponic
I got 1*8 - 1 because each IDT entry is 8 bytes long, and - 1 because array starts at 0.

To me the IDT entry for 1 looks right, what is wrong with it?

Re: Interrupt Issues

Posted: Thu Mar 11, 2021 9:16 pm
by Octocontrabass
If the first entry is at offset 0 and each entry is 8 bytes long, the second entry should be at offset 8.

You're putting it at offset 7.

Re: Interrupt Issues

Posted: Thu Mar 11, 2021 9:50 pm
by Simponic
Still same thing happens when I apply [8*n] instead of [8*n - 1]:
https://pastebin.com/THWdScqJ

Re: Interrupt Issues

Posted: Thu Mar 11, 2021 10:04 pm
by Octocontrabass
Try "info idt" in the QEMU monitor to see if there are any other problems with how you're building your IDT.

Edit: But I've just spotted the one issue it will show you.

Code: Select all

IDT=     00108000 00000000
Your IDT limit is 0.

Re: Interrupt Issues

Posted: Thu Mar 11, 2021 10:43 pm
by Octocontrabass
Your inline assembly is wrong. The input operand is a pointer to the struct instead of the struct itself. Since the struct is not an input operand, the compiler may not initialize its value.

Change your inline assembly so that the struct is the input operand.

Code: Select all

asm("lidt %0" : :"m" (idt_ptr));
If it still doesn't work after fixing this, try "info idt" in the QEMU monitor.

Re: Interrupt Issues

Posted: Thu Mar 11, 2021 10:50 pm
by Simponic
Hmm I can't do that for some reason in the qemu monitor

Code: Select all

qemu-system-i386 -kernel os.bin -monitor stdio
QEMU 5.2.0 monitor - type 'help' for more information
(qemu) info idt
unknown command: 'info idt'

Re: Interrupt Issues

Posted: Thu Mar 11, 2021 10:53 pm
by Simponic
Oh my god that was it. All it took was changing the inline assembly and the index numbers.

Now it is printing to the screen that it got an interrupt a lot of times. Should this be expected of interrupts? I think it should only print once.