Hardware interrupt - Apic.. need help..

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
JulienDarc
Member
Member
Posts: 97
Joined: Tue Mar 10, 2015 10:08 am

Hardware interrupt - Apic.. need help..

Post by JulienDarc »

Hello,

First : before asking that question, I read the resources on osdev and other places.

The platform is x86_64.

I've set up my IDT and everything should be fine.
If I fully get how software interrupts are managed, I am not sure to understand how hardware interrupts are.

From my readings, it seems that APIC "tells" the OS an hardware interrupt is occuring.

My concern is that I don't know how to manage that. How to differentiate my 1st HDD from the 2nd from my keyboard ?

Should a hardware interrupt utilize the IDT as well ? No.
Vectors 0 through 8, 10 through 14, and 16 through 19 are the predefined interrupts and exceptions; vectors 32 through 255 are for software-defined interrupts, which are for either software interrupts or maskable hardware interrupts
From intel dev guide vol 1.

I feel like there is a missing piece in my puzzle and I need a little bump :)

Thanks a lot

EDIT :

Section 6.3.1, from intel dev guide vol3 :
The I/O APIC determines the vector number of the interrupt and sends this number to the local APIC
How does it determine the vector number ? Is it at the stage where we parse the DSDT that we assign a number to every peripheral ? (I didn't do that step myself)
User avatar
SpyderTL
Member
Member
Posts: 1074
Joined: Sun Sep 19, 2010 10:05 pm

Re: Hardware interrupt - Apic.. need help..

Post by SpyderTL »

I can't help with the APIC much, but I think I can get you started.

The CPU deals with software interrupts and hardware interrupts the same way. It looks up the interrupt handler in the IDT and jumps to it. For some software interrupts, the cpu also throws some additional information on the stack.

For hardware interrupts, some external component notifies the CPU of the pending interrupt and the interrupt number. In your case, this is the APIC. Your OS must set up the APIC to "route" specific interrupt to specific CPUs (or CPU cores).

The OS is responsible for handling these interrupts, and notifying any drivers currently "listening" for that interrupt number. The driver then double-checks with the device to make sure that it actually requested the interrupt, since multiple devices could be using the same interrupt number. If it did, the driver does whatever it needs to do to handle the interrupt. If not, it simply hands control back to the OS.

You may want to ignore the APIC and just use the old school PIC until you get your hardware interrupts and interrupt handlers under control. It's a little easier to work with, I think.

You only need to activate the APIC if you want to support more than one processor.
Project: OZone
Source: GitHub
Current Task: LIB/OBJ file support
"The more they overthink the plumbing, the easier it is to stop up the drain." - Montgomery Scott
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: Hardware interrupt - Apic.. need help..

Post by Brendan »

Hi,
JulienDarc wrote:Section 6.3.1, from intel dev guide vol3 :
The I/O APIC determines the vector number of the interrupt and sends this number to the local APIC
How does it determine the vector number ? Is it at the stage where we parse the DSDT that we assign a number to every peripheral ? (I didn't do that step myself)
For IO APIC; there's a register for each IO APIC input, and software sets the interrupt vector for an IO APIC input to whatever it likes. The problem is determining which device's IRQs are connected to which IO APIC inputs.

Devices can be split into one of 2 categories: legacy and PCI. For legacy devices (e.g. PIT, CMOS, etc), the IO APIC input they're connected to is fixed and you can use the ACPI MADT/APIC table to determine which IO APIC input. For PCI devices the IO APIC input they're connected to is not fixed (e.g. things like hot-plug PCI mean they can change and therefore can't be described by a table generated by firmware during boot), and you need to interpret the firmware's AML (e.g. with something like ACPICA) to determine which IO APIC input they use.

Alternatively, for old computers (and mostly only if ACPI doesn't exist) you can use the MultiProcessor Specification tables. This will give you the information for both legacy and PCI devices in a fixed table (and can't work for things like hot-plug PCI). Sadly the MultiProcessor Specification tables are deprecated and can't be relied on for newer computers.

There are also 2 other alternatives for determining which IO APIC input is used by which PCI device/s - you can have a native motherboard driver for each different motherboard (e.g. using PCI device and vendor ID for the root bridge and "LPC to PCI bridge" to determine the chipset, and using the chipset and SMBIOS to determine the motherboard and which motherboard driver) where the native motherboard driver knows how PCI devices are mapped to IO APIC inputs; or you can use a clever "auto-detection" scheme.

Alternatively, for newer computers the device/s may provide something called MSI (Message Signalled Interrupt). In this case the device itself sends a message when it wants an IRQ, and you can tell the device what to put in that message (which interrupt vector) without caring about the IO APIC or ACPI or MultiProcessor Specification. MSI was optional for "PCI conventional" but became mandatory for PCI-E.

Also note that for all cases (either IO APIC or MSI) software determines which interrupt vectors to use; and "which interrupt vector" determines the priority of the interrupt. This implies that you'll need something to manage interrupt vectors, so you can allocate interrupt vector/s with a certain priority (and free them).

Finally; none of it depends on the device itself - you can have generic code that figures out which interrupt vector/s a device uses and reserve the interrupt vector/s for the device, and then enable the interrupt/s when a device driver is started. The device driver itself needn't be involved in determining which interrupt the device uses; and the device driver needn't know or care if you're using PIC or IO APIC or MSI. For "very advanced", the OS can abstract interrupts completely (e.g. telling the device driver that its device may have generated an IRQ) where the device driver doesn't know which interrupt vector; which allows the OS to dynamically adjust the interrupt vector/s that devices use whenever it likes without telling the device driver/s that interrupt vectors were changed.


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
JulienDarc
Member
Member
Posts: 97
Joined: Tue Mar 10, 2015 10:08 am

Re: Hardware interrupt - Apic.. need help..

Post by JulienDarc »

Thanks a lot !

I digest all those info and come back very soon.

Both of you really got me started. I am reading the drivers organization, the device trees, the pci(e) way to do things and learn a bit more on apic.

I have a netbsd OS as a reference. I plan to create my OS on top of it to save a lot of time. But I still need to understand the underlyings, otherwise, OS'deving is a nonsense.

Thanks again :)
JulienDarc
Member
Member
Posts: 97
Joined: Tue Mar 10, 2015 10:08 am

Re: Hardware interrupt - Apic.. need help..

Post by JulienDarc »

Hi,

I am progressing. Now I set up the driver to change the irq number so that the os feels at ease and enables me to change some things.

PCI is tough though (as usb). I cannot afford to go the whole spec(s). Like you, I've got a life.

Is it possible to change/overwrite the irq number the device sends ? Not the device driver changing it, but the real deal : the device sending the irq I would I told him to.

It would reduce the pressure on the drivers linked list to "visit" each driver with the same irq number -> O(1) access -> less cache miss -> faster OS (few cpu cycles but that is enough to make me happy).

Thanks
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: Hardware interrupt - Apic.. need help..

Post by Brendan »

Hi,
JulienDarc wrote:Is it possible to change/overwrite the irq number the device sends ? Not the device driver changing it, but the real deal : the device sending the irq I would I told him to.

It would reduce the pressure on the drivers linked list to "visit" each driver with the same irq number -> O(1) access -> less cache miss -> faster OS (few cpu cycles but that is enough to make me happy).
If you're using MSI, then you can change the interrupt for each PCI device individually.

If you're using IO APIC or PIC, it's likely that there's some sort of "PCI IRQ router" that lets you change how a PCI IRQ is mapped to a PIC or IO APIC input; but when multiple devices are using the same PCI IRQ line you have to change all of them and can't change them individually. The PCI IRQ router is described by ACPI's AML (and mostly requires an AML interpreter like ACPICA).

Finally, for some devices built into some chipsets there is a way to change which PCI IRQ line the device uses. This is chipset specific and not functionality that anything (including ACPI) exposes, so the only way to use this is to have a "motherboard driver" that's designed specifically for that motherboard/chipset.


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
JulienDarc
Member
Member
Posts: 97
Joined: Tue Mar 10, 2015 10:08 am

Re: Hardware interrupt - Apic.. need help..

Post by JulienDarc »

mmmmm mmmmm....

That doesn't sound good to me.. It requires too much hardware participation. Definitely not something reliable..

Again, thanks a lot Brendan :)
Post Reply