↑ Back to main site.

EPOS

Experimental Protected-mode Operating System

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

FILE:        ports.c
DESCRIPTION: Hardware port services (for use by device drivers)

NOTE: Currently this module implements only single byte get and put
methods for port access. This is rather unefficient, having in mind
that each system call involves switching between user mode
and supervisor mode.
 
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 "..\..\access\access.h"
#include "..\..\init\init.h"
#include "..\..\service\service.h"
#include "..\..\gdt\gdt.h"
#include "..\..\memory\memory.h"
#include "..\..\process\process.h"

#include "ports.h"

// Globals
//
struct PAreaInternal{
    struct PArea a;
    unsigned char bValid;

    unsigned long dwMapOfs;
    unsigned long dwMapByteCnt;
    unsigned char dwFirstByteValue;
    unsigned char dwPostByteValue;
};

struct PProcessAreas{
    struct PAreaInternal ar[P_MAX_AREA_CNT_PER_PROCESS];
};

static struct PProcessAreas g_arAreas[MAX_PROCESS_CNT];

// Initialize hardware port services
//
void k_portInit() {
    unsigned long i,j;
   
    for(i=0; i<P_MAX_AREA_CNT_PER_PROCESS; i++)
    {
        for(j=0; j<P_MAX_AREA_CNT_PER_PROCESS; j++)
            g_arAreas[i].ar[j].bValid=0;
    }
}

// Clear security mask
//
void k_portClearMaskOf(PID dwPID)
{
    unsigned long i, j;
    unsigned char* mask = gdt_getp_iomask();
    struct PAreaInternal *p;

    for(i=0; i<P_MAX_AREA_CNT_PER_PROCESS; i++){
        p = g_arAreas[dwPID].ar+i;
        if(p->bValid){
           
            for(j=0; j<=p->dwMapByteCnt; j++) mask[p->dwMapOfs + j] = 0xFF;
        }
    }
}

// Enable port mask for given process ID
//
void k_portSetMaskFor(PID dwPID)
{
    unsigned long i, j;
    unsigned char* mask = gdt_getp_iomask();
    struct PAreaInternal *p;

    for(i=0; i<P_MAX_AREA_CNT_PER_PROCESS; i++){
        p = g_arAreas[dwPID].ar+i;
        if(p->bValid){
            for(j=1; j<p->dwMapByteCnt; j++) mask[p->dwMapOfs + j] = 0x00;
            mask[p->dwMapOfs + j] &= p->dwPostByteValue;
            mask[p->dwMapOfs + 0] &= p->dwFirstByteValue;

            //for(j=0; j<=p->dwMapByteCnt; j++) mask[p->dwMapOfs + j] = 0x00;
        }
    }
}

// Allocate prots to client process
//
void pLockPorts(struct PArea *p)
{
    unsigned long i,b;
    unsigned long dwMapOfs, dwMapByteCnt, setbits,
            mask, dwFirstByteValue, dwPostByteValue;
               
    PID dwPID = tmGetCrProcessID();

    for(i=0; i<P_MAX_AREA_CNT_PER_PROCESS; i++)
    {
        if(!g_arAreas[dwPID].ar[i].bValid)
        {
            // Determine limits
            dwMapOfs      = (p->wStart/8);
            dwMapByteCnt  = (p->wEnd-p->wStart)/8;

            // Locate first byte
            //
            dwFirstByteValue = 0x00;
            setbits = p->wStart%8;
            mask = 1;
           
            for(b=0;b<setbits;b++) {
                dwFirstByteValue = dwFirstByteValue | mask;
                mask*=2;
            }

            // Locate last byte
            //
            dwPostByteValue = 0x00;
            setbits = (8-p->wEnd%8-1);
            mask = 1;
           
            for(b=0;b<setbits;b++) {
                dwPostByteValue = dwPostByteValue | mask;
                mask*=2;
            }

            if(dwMapByteCnt==0) {
                dwPostByteValue = 0xFF;

                // Some extra restrictions
                setbits = (8-p->wEnd%8-1);
                mask = 128;
                for(b=0;b<setbits;b++)
                {
                    dwFirstByteValue = dwFirstByteValue | mask;
                    mask/=2;
                }
            }

            // Setup port area
            //
            g_arAreas[dwPID].ar[i].dwMapOfs         = dwMapOfs;
            g_arAreas[dwPID].ar[i].dwMapByteCnt     = dwMapByteCnt;
            g_arAreas[dwPID].ar[i].dwFirstByteValue = dwFirstByteValue;
            g_arAreas[dwPID].ar[i].dwPostByteValue  = dwPostByteValue;
            g_arAreas[dwPID].ar[i].bValid           = 1;
            g_arAreas[dwPID].ar[i].a.wStart         = p->wStart;
            g_arAreas[dwPID].ar[i].a.wEnd           = p->wEnd;
            p->hAreahandle = i;
           
            // Update security mask
            //
            k_portSetMaskFor(dwPID);
           
            p->st = P_ST_OK;
           
            return;
        }
    }
    p->st = P_ST_OUTOFRES;
}

// Free allocated ports
//
void pFreePorts(struct PHandle *p) {
    // Not implemented.
}

// Put byte to an allocated port
//
void pPutb(struct PTransfer *p) {
    PID dwPID = tmGetCrProcessID();

    if((p->hAreahandle<P_MAX_AREA_CNT_PER_PROCESS) &&
        g_arAreas[dwPID].ar[p->hAreahandle].bValid &&
       ((g_arAreas[dwPID].ar[p->hAreahandle].a.wStart+p->wOfs)
       <= g_arAreas[dwPID].ar[p->hAreahandle].a.wEnd) ) {
     
        access_out(g_arAreas[dwPID].ar[p->hAreahandle].a.wStart + p->wOfs, p->bByte);
        p->st = P_ST_OK;
    }
    else p->st = P_ST_INVPARAM;
}

// Read byte from an allocated port
//
void pGetb(struct PTransfer *p)
{
    PID dwPID = tmGetCrProcessID();

    if((p->hAreahandle<P_MAX_AREA_CNT_PER_PROCESS) &&
        g_arAreas[dwPID].ar[p->hAreahandle].bValid &&
       ((g_arAreas[dwPID].ar[p->hAreahandle].a.wStart+p->wOfs)
          <= g_arAreas[dwPID].ar[p->hAreahandle].a.wEnd) ) {

        p->bByte = access_in(g_arAreas[dwPID].ar[p->hAreahandle].a.wStart + p->wOfs);
        p->st = P_ST_OK;
    }
    else p->st = P_ST_INVPARAM;
}