[Solved] AHCI - Enabling port interrupts causes hanging

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
haroldmann
Posts: 6
Joined: Fri Oct 18, 2024 11:47 pm

[Solved] AHCI - Enabling port interrupts causes hanging

Post by haroldmann »

Hello,

I am following the instructions in https://wiki.osdev.org/AHCI. So far I have been able to get basic reading and writing from disk set up.

Now I am following the checklist at the end to set up interrupts. I've found when setting PxIE.DHRE or PxIE.PSE, execution stops completely and I have no idea why. Setting any other interrupt bit does not cause this to happen. Disabling paging before setting the register didn't help either.

PCI Bus mastering and interrupts are enabled.
Interrupts are enabled in the GHC register.
HBA and port memory have been memory mapped with caching disabled.
The HBA and port has been reset before trying to set the register.

Has anyone else encountered this problem? Does anyone know what might be going on? The AHCI code is attached, if more is needed let me know. Any help is appreciated.

As an example:

Code: Select all

kprintf("One\n");
mem->ports[port].ie = PxIE_TFEE | PxIE_HDRE;
kprintf("Two\n");
This will cause "One" to be printed, "Two" will not print.
Attachments
ahci.h
(13.67 KiB) Downloaded 52 times
ahci.c
(13.64 KiB) Downloaded 55 times
Last edited by haroldmann on Tue Jan 07, 2025 7:57 pm, edited 1 time in total.
Octocontrabass
Member
Member
Posts: 5623
Joined: Mon Mar 25, 2013 7:01 pm

Re: AHCI - Enabling port interrupts causes hanging

Post by Octocontrabass »

Have you tried using a debugger to see what the CPU is doing when it hangs?
haroldmann
Posts: 6
Joined: Fri Oct 18, 2024 11:47 pm

Re: AHCI - Enabling port interrupts causes hanging

Post by haroldmann »

I'm using GDB with QEMU i386. I moved the instruction that sets the port interrupts to its own function (ahci_dothing) and set a breakpoint there. It hangs after it returns. Here's what I got from the assembly readout. I attached screenshots, I'm not sure what would be causing problems here.
Attachments
ksnip_20241227-141754.png
ksnip_20241227-141723.png
Octocontrabass
Member
Member
Posts: 5623
Joined: Mon Mar 25, 2013 7:01 pm

Re: AHCI - Enabling port interrupts causes hanging

Post by Octocontrabass »

It sounds like an interrupt is occurring, but as far as I know you should see the debugger step into your interrupt handler when an interrupt occurs.

Unless maybe there's a problem with your interrupt handlers too? Running QEMU with "-d int" should give you an idea of what's going on.

You can also try using QEMU's trace log to see what the emulated AHCI controller thinks is happening.
haroldmann
Posts: 6
Joined: Fri Oct 18, 2024 11:47 pm

Re: AHCI - Enabling port interrupts causes hanging

Post by haroldmann »

Thanks for the reply. I'll look into it and report back.
haroldmann
Posts: 6
Joined: Fri Oct 18, 2024 11:47 pm

Re: AHCI - Enabling port interrupts causes hanging

Post by haroldmann »

I figured it out.

The AHCI interrupt line is connected to the PIC. When the interrupt fires the PIC changes the interrupt number. I forgot about this detail. After registering the AHCI IRQ to number 0x2B, my IRQ got called and it worked.

The hanging was caused by the interrupt not being acknowledged (some bit in HBA memory not being set, I forget which one). After clearing the interrupt, the interrupt would fire again immediately, causing the "hanging".

Thanks for recommending the -d option. I would not have figured this out otherwise.
Post Reply