↑ Back to main site.

EPOS

Experimental Protected-mode Operating System

irq.c
/*
EPOS
http://www.atanaslaskov.com/epos/

FILE:          irq.c
DESCRIPTION:   Interrupts services (for use by device drivers)
 
BSD LICENSE
Copyright (c) 2006, Atanas Laskov
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
   1. Redistributions of source code must retain the above copyright notice,
   this list of conditions and the following disclaimer.
   2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL ATANAS LASKOV BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "..\..\idt\int.h"
#include "..\..\init\init.h"
#include "..\..\memory\memory.h"
#include "..\..\process\process.h"
#include "..\process\process.h"
#include "..\..\paging\paging.h"
#include "..\sync\sync.h"
#include "..\ipc\ipc.h"

#include "irq.h"

// Global for stroing IRQ handlers
//
struct IRQInternal{
    unsigned char bValid    :1;
    unsigned char bExtended :1;
    void (*irq_handler) (unsigned char nIRQ);
    PID dwPID;
};
struct IRQInternal g_arIRQ[16];

// IRQ dispatcher function
//
void irq(unsigned long nint)
{
    struct SYEventHandle ev;
    nint -= KRNL_FIRST_IRQ;

    // Is there a registered valid handler ?
    //
    if(g_arIRQ[nint].bValid)
    {
        pageDirectorySwitch(PAGING_DIR_KERNEL);
       
        if(g_arIRQ[nint].bExtended) {
       
            // Using the new IRQ notification system.
            //         
            // Directly call the handler function located
            // in the user process.
            //
            tmCallLocalFunction(
                g_arIRQ[nint].dwPID,
                (unsigned long)(g_arIRQ[nint].irq_handler)
            );
        }
        else {
            // Using the old IRQ notification system (with IPC).
            //
            // Send message an IPC message tho the handling process
            //
            struct IPCTransfer ipctTransfer;

            ipctTransfer.msg.dwMessage = MSG_KRN_IRQ;
            ipctTransfer.msg.dwSize    = 1;
            ipctTransfer.msg.arData[0] = nint;
            ipctTransfer.hLine         = tmGetProcessLine(g_arIRQ[nint].dwPID);
            k_ipcSend(&ipctTransfer);
        }
       
        pageDirectorySwitch(PAGING_DIR_PROCESS0 + tmGetCrProcessID());
    }

    // reset
    if(nint>=8) access_out(0xA0, 0x20);
    access_out(0x20, 0x20);
}

// This is called on when switching to a different process
//
void k_irqOnSwitch() {
    // Do nothing.
}

// Initialize the IRQ service system
//
void k_irqInit()
{
    // Mark all IRQs as unhandled
    //
    unsigned char i;
    struct SYEvent syeEvent;
    for(i=1; i<16; i++)
    {
        g_arIRQ[i].bValid = 0;
        setvectorex(KRNL_FIRST_IRQ+i, irq);
    }
   
    // Except the timer
    g_arIRQ[0].bValid = 1;

    g_bAutoResetIRQ = 0;
}

// Use this to obtain IRQ. This is the new one
// NOTE: This is the new call
//
void irqLockEx(IRQEX *p) {
    if((p->bIRQ<16) && (!g_arIRQ[p->bIRQ].bValid))
    {
        g_arIRQ[p->bIRQ].bValid      = 1;
        g_arIRQ[p->bIRQ].bExtended   = 1;
        g_arIRQ[p->bIRQ].dwPID       = tmGetCrProcessID();
        g_arIRQ[p->bIRQ].irq_handler = p->irq_handler;
        p->st = I_ST_OK;
        return;
    }
    p->st = I_ST_IRQLOCKED;
}


// User process registers an IRQ handler
// NOTE: This is the old call
//
void irqLock(struct irq *p) {
    if((p->bIRQ<16) && (!g_arIRQ[p->bIRQ].bValid))
    {
        g_arIRQ[p->bIRQ].bValid    = 1;
        g_arIRQ[p->bIRQ].bExtended = 0;
        g_arIRQ[p->bIRQ].dwPID = tmGetCrProcessID();
        p->st = I_ST_OK;
        return;
    }
    p->st = I_ST_IRQLOCKED;
}

// Un-register an IRQ handler
//
void irqFree(struct irq *p) {
    if((p->bIRQ<16) && (g_arIRQ[p->bIRQ].bValid))
    {
        if(g_arIRQ[p->bIRQ].dwPID!=tmGetCrProcessID())
        {
            p->st = I_ST_IRQLOCKED;
            return;
        }

        g_arIRQ[p->bIRQ].bValid = 0;
        p->st = I_ST_OK;
        return;
    }
    p->st = I_ST_IRQFREE;
}