Question

I have a question about how to replace HW interrupt in flat memory mode...

  1. about my application...
    • created by combining Watcom C and DOS32/A.
    • written for running on DOS mode( not on OS mode )
    • with DOS32/A now I can access >1M memory and allocate large memory to use...(running in flat memory mode !!!)
  2. current issue...
    • I want to write an ISR(interrupt service routine) for one PCI card. Thus I need to "replace" the HW interrupt.
    • Ex. the PCI card's interrupt line = 0xE in DOS. That means this device will issue interrupt via 8259's IRQ 14.

But I did not how to achieve my goal to replace this interrupt in flat mode ?

@ resource I found... - in watcom C's library, there is one sample using _dos_getvect, _dos_setvect, and _chain_intr to hook INT 0x1C... I tested this code and found OK. But when I apply it to my case: INT76 ( where IRQ 14 is "INT 0x76" <- (14-8) + 0x70 ) then nothing happened...

  • I checked HW interrupt is generated but my own ISR did not invoked...

Do I lose something ? or are there any functions I can use to achieve my goal ?

===============================================================

[20120809] I tried to use DPMI calls 0x204 and 0x205 and found MyISR() is still not invoked. I described what I did as below and maybe you all can give me some suggestions !

1) Use inline assembly to implement DPMI calls 0x204 and 0x205 and test OK...

Ex. Use DPMI 0x204 to show the interrupt vectors of 16 IRQs and I get(selector:offset) following results: 8:1540(INT8),8:1544(INT9),.....,8:1560(INT70),8:1564(INT71),...,8:157C(INT77)

Ex. Use DPMI 0x205 to set the interrupt vector for IRQ14(INT76) and returned CF=0, indicating successful

2) Create my own ISR MyISR() as follows:

volatile int tick=0;  // global and volatile...
void MyISR(void)
{
  tick = 5;  // simple code to change the value of tick...
}

3) Set new interrupt vector by DPMI call 0x205:

selector = FP_SEG(MyISR);  // selector = 0x838 here
offset = FP_OFF(MyISR);    // offset   = 0x30100963 here
sts = DPMI_SetIntVector(0x76, selector, offset, &out_ax);

Then sts = 0(CF=0) indicating successful !

  • One strange thing here is:my app runs in flat memory model and I think the selector should be 0 for MyISR()... But if selector = 0 for DPMI call 0x205 then I got CF=1 and AX = 0x8022, indicating "invalid selector" !

4) Let HW interrupt be generated and the evidences are:

  • PCI device config register 0x5 bit2(Interrupt Disabled) = 0
  • PCI device config register 0x6 bit3(Interrupt status) = 1
  • PCI device config register 0x3C/0x3D (Interrupt line) = 0xE/0x2
  • In DOS the interrupt mode is PIC mode(8259 mode) and Pin-based(MSIE=0)

5) Display the value of tick and found it is still "0"...

Thus I think MyISR() is not invoked correctly...

Was it helpful?

Solution

Try using DPMI Function 0204h and 0205h instead of '_dos_getvect' and '_dos_setvect', respectively.

The runtime environment of your program is DOS32A or a DPMI Server/host. So use the api they provided instead of using DOS int21h facilities. But DOS32A does intercepts int21h interrupts, so your code should work fine, as far as real mode is concerned.

Actually what you did is you install only real mode interrupt handler for IRQ14 by using '_dos_getvect' and '_dos_setvect' functions.

By using the DPMI functions instead, you install protected mode interrupt handler for IRQ14, and DOS32a will autopassup IRQ14 interrupt to this protected mode handler.

Recall: A dos extender/DPMI server can be in protected mode or real mode while an IRQ is asserted.

This is becoz your application uses some DOS or BIOS API, so extender needs to switch to real mode to execute them and the return back to protected mode to transfer control to you protected mode application.

DOS32a does this by allocating a real-mode callback (at least for hardware interrupts) which calls your protected mode handler if IRQ14 is asserted while the Extender is in real-mode.

If the extender is in protected mode, while IRQ14 is asserted, it will automatically transfer control to your IRQ14 handler.

But if you didn't install protected mode handler for your IRQ, then DOS32a, will not allocate any real-mode callback, and your real-mode irq handler may not get control. But it should recieve control AFAIK.

Anyway give the above two functions a try. And do chain to the previous int76h interrupt handler as Sean said.

In short:

In case of DOS32a, you need not use '_dos_getvect' and '_dos_setvect' functions. Instead use the DPMI functions 0204h and 0205h for installing your protected mode IRQ handler.

An advise : In your interrupt handler the first step should be to check whether your device actually generated interrupt or it is some other device sharing this irq(IRQ14 in your case). You can do this by checking a 'interrupt pending bit' in your device, if it is set, service your device and chain to next handler. If it is not set to 1, simply chain to next handler.

EDITED: Use the latest version of DOS32a, instead of one that comes with OW.

Update on 2012-08-14:

Yes, you can use FP_SEG and FP_OFF macros for obtaining selector and offset respectively, just like you would use these macros in real modes to get segment and offset.

You can also use MK_FP macro to create far pointers from selector and offset. eg. MK_FP(selector, offset).

You should declare your interrupt handler with ' __interrupt ', keyword when writing handlers in C.

Here is a snippet:

             #include <i86.h>  /* for FP_OFF, FP_SEG, and MK_FP in OW */

             /* C  Prototype for your IRQ handler */
             void   __interrupt    __far irqHandler(void);
                        .
                        . 
                        .
          irq_selector = (unsigned short)FP_SEG( &irqHandler );
          irq_offset = (unsigned long)FP_OFF( &irqHandler );

          __dpmi_SetVect( intNum, irq_selector, irq_offset );
                        .
                        . 
                        .

or, try this:

          extern void sendEOItoMaster(void);  
          # pragma aux sendEOItoMaster = \                         
                "mov  al,    0x20"  \       
                "out  0x20,  al"    \       
                modify [eax] ;



          extern void sendEOItoSlave(void);  
          # pragma aux sendEOItoSlave = \                           
              "mov  al,    0x20"  \       
              "out  0xA0,  al"    \       
              modify [eax] ;


      unsigned int   old76_selector, new76_selector;
      unsigned long  old76_offset, new76_offset;


      volatile int chain = 1; /* Chain to the old handler */
      volatile int tick=0;  // global and volatile...


     void (__interrupt __far *old76Handler)(void) = NULL;      // function pointer declaration

     void __interrupt __far new76Handler(void) {


            tick = 5;  // simple code to change the value of tick...

               .
               .
               .

           if( chain ){

                 // disable irqs if enabled above.

                 _chain_intr( old76Handler );  // 'jumping' to the old handler

                //  ( *old76Handler )();          // 'calling' the old handler 

           }else{

               sendEOItoMaster();
               sendEOItoSlave();
           }

      }


      __dpmi_GetVect( 0x76, &old76_selector, &old76_offset );

      old76Handler = ( void (__interrupt __far *)(void) ) MK_FP (old76_selector, old76_offset)

      new76_selector = (unsigned int)FP_SEG( &new76Handler );
      new76_offset = (unsigned long)FP_OFF( &new76Handler );

      __dpmi_SetVect( 0x76, new76_selector, new76_offset );

                    .
                    .

NOTE:

You should first double check that the IRQ# you are hooking is really assigned/mapped to the interrupt pin of your concerned PCI device. IOWs, first read 'Interrupt Line register' (NOT Interrupt Pin register) from PCI configuration space, and hook only that irq#. The valid values for this register, in your case are: 0x00 through 0x0F inclusive, with 0x00 means IRQ0 and 0x01 means IRQ1 and so on.

POST/BIOS code writes a value in 'Interrupt Line register', while booting, and you MUST NOT modify this register at any cost.(of course, unless you are dealing with interrupt routing issues which an OS writer will deal with)

You should also get and save the selector and offset of the old handler by using DPMI call 0204h, in case you are chaining to old handler. If not, don't forget to send EOI(End-of-interrupt) to BOTH master and slave PICs in case you hooked an IRQ belonging to slave PIC(ie INT 70h through 77h, including INT 0Ah), and ONLY to master PIC in case you hooked an IRQ belonging to master PIC.

In flat model, the BASE address is 0 and Limit is 0xFFFFF, with G bit(ie Granularity bit) = 1.

The base and limit(along with attribute bits(e.g G bit) of a segment) reside in the descriptor corresponding to a particular segment. The descriptor itself, sits in the descriptor table.

Descriptor tables are an array with each entry being 8bytes.

The selector is merely a pointer(or an index) to the 8-byte descriptor entry, in the Descriptor table(either GDT or LDT). So a selector CAN'T be 0.

Note that lowest 3 bits of 16-bit selector have special meaning, and only the upper 13-bits are used to index a descriptor entry from a descriptor table.

GDT = Global Descriptor Table

LDT = Local Descriptor Table

A system can have only one GDT, but many LDTs.

As entry number 0 in GDT, is reserved and can't be used. AFAIK, DOS32A, does not create any LDT for its applications, instead it simply allocate and initalize descriptor entries corresponding to the application, in GDT itself.

Selector MUST not be 0, as x86 architecture regards 0 selector as invalid, when you try to access memory with that selector; though you can successfully place 0 in any segment register, it is only when you try to access(read/write/execute) that segment, the cpu generates an exception.

In case of interrupt handlers, the base address need not be 0, even in case of flat mode. The DPMI environment must have valid reasons for doing this so. After all, you still need to tackle segmentation at some level in x86 architecture.

                  PCI device config register 0x5 bit2(Interrupt Disabled) = 0

                  PCI device config register 0x6 bit3(Interrupt status) = 1  

I think, you mean Bus master command and status registers respectively. They actually reside in either I/O space or memory space, but NOT in PCI configuration space. So you can read/write them directly via IN/OUT or MOV, instructions.

For reading/writing, PCI configuration registers you must use configuration red/write methods or PCI BIOS routines.

NOTE:

Many PCI disk controllers, have a bit called 'Interrupt enable/disable' bit. The register that contains this bit is usually in the PCI configuration space, and can be found from the datasheet.

Actually, this setting is for "forwarding" the interrupt generated by the device attached to the PCI controller, to the PCI bus.

If, interrupts are disabled via this bit, then even if your device(attached to PCI controller) is generating the interrupt, the interrupt will NOT be forwarded to the PCI bus(and hence cpu will never know if interrupt occurred), but the interrupt bit(This bit is different from 'Interrupt enable/disable' bit) in PCI controller is still set to notify that the device(attached to PCI controller, eg a hard disk) generated an interrupt, so that the program can read this bit and take appropriate actions. It is similar to polling, from programming perspective.

This usually apply only for non-bus master transfers.

But, it seems that you are using bus master transfers(ie DMA), so it should not apply in your case.

But anyway, I would suggest you do read the datasheet of the PCI controller carefully, especially looking for bits/registers related to interrupt handling

EDITED:

Well, as far as application level programming is concerned, you need not encounter/use _far pointers, as your program will not access anything outside to your code.

But this is not completely true, when you go to system-level programming, you need to access memory mapped device registers, external ROM, or implementing interrupt handlers, etc.

The story changes here. The creation of a segment ie allocating descriptor and getting its associated selector, ensures that even if there is a bug in code, it will not annoyingly change anything external to that particular segment from which current code is executing. If it tries to do so, cpu will generate a fault. So when accessing external devices(especially memory mapped device's registers), or accessing some rom data, eg BIOS etc., it is a good idea to have allocate a descriptor and set the base and segment limits according to the area you need to execute/read/write and proceed. But you are not bound to do so.

Some external code residing for eg in rom, assume that they will be invoked with a far call.

As I said earlier, in x86 architecture, at some level(the farther below you go) you need to deal with segmentation as there is no way to disable it completely. But in flat model, segmentation is present as an aid to programmer, as I said above, when accessing external(wrt to your program) things. But you need not use if you don't desire to do so.

When an interrupt handler is invoked, it doesn't know the base and limits of program that was interrupted. It doesn't know the segment attributes, limits etc. of the interrupted program, we say except CS and EIP all registers are in undefined state wrt interrupt handler. So it is needed to be declared as far function to indicate that it resides somewhere external to currently executing program.

OTHER TIPS

it's been a while since I fiddled with interrupts, but the table is a pointer to set where the processor should go to to process an interrupt. I can give you the process, but not code, as I only ever used 8086 code.

Pseudo code:

Initialize:
    Get current vector - store value
    Set vector to point to the entry point of your routine

next:

Process Interrupt:
    Your code decides what to do with data
    If it's your data:
        process it, and return
    If not:
        jump to the stored vector that we got during initialize, 
        and let the chain of interrupts continue as they normally would

finally:

Program End:
    check to see if interrupt still points to your code
        if yes, set vector back to the saved value
        if no, set beginning of your code to long jump to vector address you saved, 
            or set a flag that lets your program not process anything
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top