-1

I want to understand operating system development for AMD64 architecture from scratch and came to a point. However, I need to learn how to attach an interrupt handler. I haven't been able to find any good tutorial which describes that process step by step with examples. I read the processor manual. But, I need a C code example.

Can anyone suggest a good source or give a simple example please?

Thank you.

user2972185
  • 231
  • 3
  • 12
  • It should be spelled out in the manual for your processor, but we don't know what processor you are using, so it's hard to point to a tutorial. – Dietrich Epp Aug 10 '14 at 23:47
  • Too broad. The interplay between hardware, interrupt-handler and OS is too complex for one question and would require a book as an answer. Drivers usually have to conform to some pattern, especially if loadable, and the whole lot is architecture, hardware and OS dependent. Just describing driver<->OS buffering and signaling is worthy of a book. – Martin James Aug 11 '14 at 03:19
  • Ok. Do you know any book name. According to a book I have red, interrupt handling is a part of os development. If this is correct, how come interrupt handling is dependent on os. I think I couldn't understand this topic sufficiently. – user2972185 Aug 11 '14 at 05:28

2 Answers2

1

Your question isn't really specific enough to give a good answer to. Are you talking about an interrupt handler in general or one for a specific operating system? Also, it's very architecture dependent. Certain processors generate many levels of interrupt (which have a priority) others only have a single interrupt level. Then there's NMI interrupts, which are usually always generated by hardware.

Also, what level do you need to know how an interrupt happens? I hope this isn't patronising... but I think of an interrupt in terms of a guy writing a letter. The telephone rings and he stops writing the letter, answers the phone and upon completing the call, returns to writing the letter exactly where he left off. (The call being the interrupt and the handling of the interrupt being him answering the call).

Let's take a simple example of an interrupt, say one generated by a timer. For ease, I'm going to refer to an Amiga interrupt because they're very raw in nature whereas in certain architectures, there's some operating system intervention before your application gets to handle the interrupt.

First thing is to understand the hardware. In order for you to get the interrupt from the timer, you'd have to enable it. Usually there's a hardware register for doing this. This register will mask what interrupts are enabled and what interrupts aren't. Now, if you've enabled this interrupt, when it occurs, the processor will stop what it is doing, perform a stack swap (to a supervisor stack) and call a interrupt handling routine or vector at a specific address. For you to handle this interrupt yourself, you'd need to write a handler and write its address into the handler address or vector. Then your routine would be called.

Your routine needs to be aware of the fact that there might be a time constraint on how long you have to process the interrupt, also that you may be interrupted by a higher priority interrupt and that you must service the interrupt or pass the interrupt along to the previous handler who was there before you.

In your interrupt handling routine you should... check for where the interrupt came from because there may be many sources and then you can decide if you want to handle the interrupt. You might have to interrogate hardware devices to identify the source of the interrupt. If it is one you want to handle, then upon performing whatever you want to do, you must acknowledge the interrupt and tell the hardware that you have serviced the interrupt. Usually there will be a specific register for doing this. Then you can exit your interrupt handler, but ONLY if you've handled the interrupt.

When you exit your interrupt handler, with an instruction like rte or rti, the stacks will then again be swapped back from the supervisor stack to the user stack.

You need to be aware that certain architectures build certain stack-frames and they nearly all differ from each other. Usually however, you will always find the Status register and Program Counter on the stack, but you may and probably will find MUCH more on the stack (the supervisor stack)

The program counter will be the return address that will be jumped to upon exit from your handler.

The Welder
  • 916
  • 6
  • 24
  • Thank you for your reply. However, I need a code example to understand this topic. I have updated my question. – user2972185 Aug 11 '14 at 00:18
  • 2
    Then you're going to have to look at the manual for the processor. In there it will explain the hardware registers for enabling/disabling the interrupts, how they can be acknowledged and most importantly the stack frames that are built. Also, most interrupt handlers are written in assembly rather than C, unless you've got a compiler that has intrinsics for stuff like this. This is because in C, you don't really have the semantics for accessing the hardware (as such), although it can be done, but also because of the time constraint of interrupt handling. – The Welder Aug 11 '14 at 00:24
  • Also my reply above actually explains the process step-by-step and that's how it will happen on the AMD64, you just need the detail, but the process will be the same. – The Welder Aug 11 '14 at 00:28
  • I think I understand your explanation. Thank you. But I want to see some codes. I cannot find assambly code either. Would you provide me a source for an assambly code example? – user2972185 Aug 11 '14 at 00:57
  • Err.. Googling 'AMD64 interrupt drivers' gives: 'About 3,330,000 results'. – Martin James Aug 11 '14 at 03:29
0

The general way to "attach" an interrupt handler is through the processor's dispatch table. There are various names for this structure, including Interrupt Dispatch Table.

Your operating system needs to take the address of your interrupt handler routine and load this into the corresponding slot in the the dispatch table. The format of the table entry depends upon the system being used.

Here is an example of someone going through the process:

http://forum.osdev.org/viewtopic.php?f=1&t=17306

user3344003
  • 20,574
  • 3
  • 26
  • 62
  • Thank you. This is what I exactly need. But I cannot combine external asm function with the c code. Linker gives error. Does that code works? – user2972185 Aug 12 '14 at 00:36
  • The code is not mine. However, be sure that your ASM matches you C compiler's name mangling. Some compilers prefix external symbols with _. You need to account for that kind of thing when using ASM. – user3344003 Aug 12 '14 at 03:09