Building a 'C' routine to Interrupt Handler

Building a 'C' routine to Interrupt Handler

Post by To » Tue, 03 Sep 2002 15:59:50



Can someone there enlighten me out, how to go 'bout
building an Interrupt Handler for a given 'C' routine.

I appreciate your help and experience for directions
concerning this.

Thanks
- Tom

PS: May be a template code of an Interrupt handler would
    be of use, so that i can use a wrapper or something of
    that sort to build my own Interrupt Handler API.

 
 
 

Building a 'C' routine to Interrupt Handler

Post by Robert Schwebe » Tue, 03 Sep 2002 17:50:47



> Can someone there enlighten me out, how to go 'bout
> building an Interrupt Handler for a given 'C' routine.

You should get yourself a copy of Alessandro Rubini's
"Linux Device Drivers, 2nd Edition" aka "The Bible (TM)".

Robert
--
  Dipl.-Ing. Robert Schwebel | http://www.pengutronix.de
  Pengutronix - Linux Solutions for Science and Industry
    Braunschweiger Str. 79,  31134 Hildesheim, Germany
     Phone: +49-5121-28619-0 |  Fax: +49-5121-28619-4

 
 
 

Building a 'C' routine to Interrupt Handler

Post by saki » Tue, 03 Sep 2002 20:17:57


Tom,

If I understand your question correctly u are trying to implement
an Interrupt Handler at Kernel Level ( as this is the only place
u can -Anyone who wants to check up on that statement ?-  ).

The kernel provides u with a set of APIs to register your callback
function, however be very cautious with interrupts, as there is the
possibility to hog down the whole system without a considerable
amount of effort ( from personal experience if your operation within
the ISR takes too long then use Bottom Half - Tasklets ).

The best place to start with the whole interrupt mechanism is to
read  Alessandro Rubini's, Jonathan Corbet's ' Linux Device
Drivers, 2nd Edition ' follow the link below.

http://www.oreilly.com/catalog/linuxdrive2/

Within that link click on the 'Examples' to find some sample code !
But I personally strongly recommend u get a hold of that book to
gain a better understanding of the whole subject and many more !

Hope this helps.

Regards,

Sakis Panou.


> Can someone there enlighten me out, how to go 'bout
> building an Interrupt Handler for a given 'C' routine.

> I appreciate your help and experience for directions
> concerning this.

> Thanks
> - Tom

> PS: May be a template code of an Interrupt handler would
>     be of use, so that i can use a wrapper or something of
>     that sort to build my own Interrupt Handler API.

 
 
 

Building a 'C' routine to Interrupt Handler

Post by To » Thu, 05 Sep 2002 03:32:10


Hi,

  Probably i wld have been more clearer....

  Linux Device Drivers book is heavy for me for the moment.

  Can anyone guide me how to make a 'C' routine to an
  Interrupt handler. Say, I have a vector table and there is
  an API which takes vector number & 'C' routine as arguments.
  Through this API i can easily register an interrupt and it
  works fine.

  I have to implement my own API which does something similar.
  I wld be interested to know what exactly one should add to
  the existing 'C' routine to turn it as an Interrupt handler.

  I just had a look at linux device drivers, cldn't find much
  information in this regard atleast.

Thanks


> Tom,

> If I understand your question correctly u are trying to implement
> an Interrupt Handler at Kernel Level ( as this is the only place
> u can -Anyone who wants to check up on that statement ?-  ).

> The kernel provides u with a set of APIs to register your callback
> function, however be very cautious with interrupts, as there is the
> possibility to hog down the whole system without a considerable
> amount of effort ( from personal experience if your operation within
> the ISR takes too long then use Bottom Half - Tasklets ).

> The best place to start with the whole interrupt mechanism is to
> read  Alessandro Rubini's, Jonathan Corbet's ' Linux Device
> Drivers, 2nd Edition ' follow the link below.

> http://www.oreilly.com/catalog/linuxdrive2/

> Within that link click on the 'Examples' to find some sample code !
> But I personally strongly recommend u get a hold of that book to
> gain a better understanding of the whole subject and many more !

> Hope this helps.

> Regards,

> Sakis Panou.


> > Can someone there enlighten me out, how to go 'bout
> > building an Interrupt Handler for a given 'C' routine.

> > I appreciate your help and experience for directions
> > concerning this.

> > Thanks
> > - Tom

> > PS: May be a template code of an Interrupt handler would
> >     be of use, so that i can use a wrapper or something of
> >     that sort to build my own Interrupt Handler API.

 
 
 

Building a 'C' routine to Interrupt Handler

Post by Michael Schnel » Thu, 05 Sep 2002 15:29:55


Quote:>  I wld be interested to know what exactly one should add to
>  the existing 'C' routine to turn it as an Interrupt handler.


Sounds like a bad idea...

-Michael Schnell, Lumino GmbH,
 Oppumer Stra?e 81-83, D-47799 Krefeld, Germany.

 Tel.: +49-2151-8196-72  Fax: +49-2151-8196-66-72

 
 
 

Building a 'C' routine to Interrupt Handler

Post by Geir Frode Raane » Thu, 05 Sep 2002 17:08:39


: Hi,

:  Probably i wld have been more clearer....

Probably.
You could start by clarify if you intend to use Linux at all.

:  Linux Device Drivers book is heavy for me for the moment.

If you do intend to use Linux, then you are into deep here...

:  Can anyone guide me how to make a 'C' routine to an
:  Interrupt handler. Say, I have a vector table and there is
:  an API which takes vector number & 'C' routine as arguments.
:  Through this API i can easily register an interrupt and it
:  works fine.

What you are asking is how to do this down on the iron itself.
In Linux, all interrupts are fetched by a 'frontend,' which
dispatches/wakes up one of its correspondingly registered
(top halves) of Interrupt Service Routines. There is a
correspondence between the vector table and the ISR list,
but it is far from 1:1. The ISRs are again integrated pieces
of device drivers. The Linux ISRs have 'C' calling convention
and is a such indistinguishable from 'C' functions.

The Linux IRQ 'frontend' OTOH... This is how most OSes does
it nowadays, and it is also the reason the fully vectorised
approch of e.g. M68K is a waste. Hence, nowadays the PowerPC
only have one external interrupt vector at 0x500. The x86 have
two - maskeable and non-maskeable interrupts. But x86 would not
be the PITA it is if there was not a catch, and that is the
integrated 8259 handshake mechanism. Not sure about ARM.

:  I have to implement my own API which does something similar.
:  I wld be interested to know what exactly one should add to
:  the existing 'C' routine to turn it as an Interrupt handler.

Turning a 'C' function into a bare ISR does require help from
the C compiler. In the case of GCC, this support is sparse and
limited to microcontrollers like AVR and H8. ISRs are extensions
to the 'C' language and hence, requires special keywords to invoke.
For GCC, this is usually done by the __attribute__ directive:

    #define SIG_INTERRUPT0       _vector_1

    void signame (void) __attribute__ ((signal));
    void signame (void) __attribute__ ((interrupt));

The examples are fetched from AVR-GCC. The difference between
'signal' and 'interrupt' refer to whether the ISR should run
with globar interrupts masked or not. The trouble does not end
there though. All __attribute__s are passed on untouched by the
GCC compiler frontend down to the AVR RTL backend. Hence, the
GCC optimizer does not have the faintest idea about the
asynchronous nature of ISRs. Thus, any symbol/variable shared
between the main program and a ISR must be declared 'volatile.'

There is however always the possibility to write your own
ISR frontend in assembly. What need to be done in this is
to save the current context - all of it, since you do not
have any prior knowledge what the ISR might touch - and then
construct a valid stack frame on top of the stack. You then
call your 'C' function/ISR to do the actual job, and upon
return your assembly dismantle the stack frame, replace the
prior context (from the stack frame copy) and return from IRQ.
The placement of this/these assembly routine(s) in the memory
layout and referencing them in the vector table is a exercise
in linker scripting...

--
  ******************************************************
  Never ever underestimate the power of human stupidity.
  -Robert Anson Heinlein


  ******************************************************

 
 
 

Building a 'C' routine to Interrupt Handler

Post by Tauno Voipi » Thu, 05 Sep 2002 17:25:27



Quote:> Hi,

>   Probably i wld have been more clearer....

>   Linux Device Drivers book is heavy for me for the moment.

>   Can anyone guide me how to make a 'C' routine to an
>   Interrupt handler. Say, I have a vector table and there is
>   an API which takes vector number & 'C' routine as arguments.
>   Through this API i can easily register an interrupt and it
>   works fine.

>   I have to implement my own API which does something similar.
>   I wld be interested to know what exactly one should add to
>   the existing 'C' routine to turn it as an Interrupt handler.

>   I just had a look at linux device drivers, cldn't find much
>   information in this regard atleast.

Once again - you cannot do that if the Linux kernel is in. Please note that
Linux is *not* DOS, where an user is let to mess with interrupts directly.
Of necessity (hardware protection mechanism), the interrupt handler has to
be a part of the kernel code with kernel rights - it is not safe to let
whatever user code to hook in.

Get another book to understand:

Daniel P. Bovet & Marco Cesati, Understanding the Linux Kernel, O'Reilly,
ISBN 0-596-00002-2.

If there is no kernel running, you could maybe do it. To see, how the things
are done on Intel 386+ hardware, get the book:

386DX Programmer's Reference Manual, Intel doc. 230985-002, or a newer
printing.

The 386+ version of GCC does not support interrupt routines, as the
interrupt handler prolog and epilog are not unique - the handling differs
depending on the interrupt handling mode (see hardware reference, interrupt
gates).

There is a way to embed the interrupt handling specialities using embedded
assembler - I'm using it with Atmel AT91 ARM's.

HTH

Tauno Voipio