↑ Back to main site.

EPOS

Experimental Protected-mode Operating System

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

FILE:        process.c
DESCRIPTION: Process management services
NOTE: This builds on top of kernel\process\process.c
 
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 "..\..\memory\memory.h"
#include "..\..\process\process.h"
#include "process.h"
#include "..\ipc\ipc.h"

// Globals
//
struct PProcessName_internal{
    unsigned char bValid;
    PID           dwPID;
};

static struct PProcessName_internal g_arNames[MAX_PROCESS_CNT];

// Initialize porcess management services
//
void k_pInit() {
    PID i;
   
    for(i=0; i<MAX_PROCESS_CNT; i++)
        g_arNames[i].bValid = 0;
}

PRCSTATE k_pGetPID(struct PAddress* p, PID *pPID)
{
    // PID
    //
    if(p->bType==P_ADDRESS_PID)
    {
        if(p->dwAddress >= MAX_PROCESS_CNT)
            return P_S_INVALID_PROCESS;

        *pPID = p->dwAddress;
    }
    // Process name
    //
    else
    {
        if(p->dwAddress>=MAX_PROCESS_CNT)
            return P_S_INVALID_PROCESS;
   
        if(!g_arNames[p->dwAddress].bValid)
            return P_S_INVALID_PROCESS;

        *pPID = g_arNames[p->dwAddress].dwPID;
    }

    return P_S_OK;
}

// Create a new process
//
void pCreate(struct PCreate *p) {
    p->strModule[40] = 0;
    p->st = tmCreateProcess(&p->dwPID, p->dwSize, p->strModule);
}

// Transfer into the memory of a newly created process
//
void pPut(struct PTransferByte *p)
{
    HMEMORY h;
    struct MemBlock m;
   
    // Validate process state
    //
    if(p->dwPID>=MAX_PROCESS_CNT) {
        p->st = P_S_INVALID_PROCESS;
        return;
    }
   
    if( !(tmGetProcessState(p->dwPID)&T_STATE__SELECTED) ) {
        p->st = P_S_INVALID_PROCESS;
        return;
    }
   
    if( tmGetProcessState(p->dwPID)&T_STATE__ALIVE ) {
        p->st = P_S_INVALID_PROCESS;
        return;
    }

    // Perform transfer
    //
    h = tmGetProcessMemory(p->dwPID);
    memBlockInfo(h, &m);

    if( (m.dwStart+p->dwOfs)>=m.dwEnd ) {
        p->st = P_S_OUTOFPROCESSMEMORY;
        return;
    }

    gdt_linear_setb(m.dwStart+p->dwOfs, p->bByte);
    p->st = P_S_OK;
}

// Add process to the task scheduler
//
void pRun(struct PPID *p) {
    tmLiveProcess(p->dwPID);
}

// Kill a process
//
void pKill(struct PPID *p) {
    p->st = tmKillProcess(p->dwPID);

    //If this is self kill switch to the next process
    if(p->dwPID == tmGetCrProcessID()) tmAddCharge();
}

// Perform suicide :-).
//
void pSelfKill()
{
    // Signal other subsystems of the kernel to
    // free associated resources
    //
    k_ipcProcessDeath(tmGetCrProcessID());

    //k_irqProcessDeath(tmGetCrProcessID());
    //k_memProcessDeath(tmGetCrProcessID());
    //k_portProcessDeath(tmGetCrProcessID());
    //k_smProcessDeath(tmGetCrProcessID());
    //k_syProcessDeath(tmGetCrProcessID());

    // Kill process
    //
    tmKillProcess(tmGetCrProcessID());
    tmAddCharge();

}

// Allocate a name for the client process
//
void pAllocateName(struct PAllocName *p)
{
    // Validate
    //
    if(p->dwName>=MAX_PROCESS_CNT) {
        p->st = P_S_INVALID_NAME;
        return;
    }
   
    if(g_arNames[p->dwName].bValid) {
        p->st = P_S_NAME_OCCUPIED;
        return;
    }
   
    // Allocate name
    //
    g_arNames[p->dwName].bValid = 1;
    g_arNames[p->dwName].dwPID  = tmGetCrProcessID();
   
    p->st = P_S_OK;
}

// Release allocated name
//
void pFreeName(struct PAllocName *p)
{
    if(p->dwName>=MAX_PROCESS_CNT){
        p->st = P_S_INVALID_NAME;
        return;
    }
   
    if(!g_arNames[p->dwName].bValid) {
        p->st = P_S_NAME_FREE;
        return;
    }
   
    g_arNames[p->dwName].bValid = 0;
    p->st = P_S_OK;
}

// Get process ID
//
void pGetProcessID(struct PPID *p)
{
    if(p->dwPID>=MAX_PROCESS_CNT) {
        p->st = P_S_INVALID_PROCESS;
        return;
    }
           
    if(!g_arNames[p->dwPID].bValid)
    {
        p->st = P_S_INVALID_PROCESS;
        return;
    }
   
    p->dwPID = g_arNames[p->dwPID].dwPID;
    p->st = P_S_OK;
}

// Get IPC line to the kernel
//
void pGetKernelLine(struct PHLine *p) {
    p->hLine = tmGetProcessLine(tmGetCrProcessID());
    p->st = P_S_OK;
}

// Get detailed process information
//
void pInfo(struct PInfo *p)
{
    // Validate PID
    //
    if(p->dwPID>=MAX_PROCESS_CNT) {
        p->st = P_S_INVALIDPID;
        return;
    }
    if( !(tmGetProcessState(p->dwPID)&T_STATE__SELECTED) ) {
        p->st = P_S_INVALIDPID;
        return;
    }

    // Extract information
    //
    p->dwMemBase    = tmGetProcessMembase(p->dwPID);
    p->dwMemSize    = tmGetProcessMemSize(p->dwPID);
    p->wState       = tmGetProcessState(p->dwPID);
    p->hKernelLine  = tmGetProcessLine(p->dwPID);
    p->dwParent     = tmGetProcessParent(p->dwPID);
    strcpy(p->strModule, tmGetProcessModule(p->dwPID));
   
    p->st = P_S_OK;
}