Custom-OS-Kernel / phase2 / exception_handler.c
exception_handler.c
Raw
// Shahir Ahmed and Boosung Kim

#include "../h/exception_handler.h"
#include "../h/pandos_const.h"

int exceptionCode;
extern pcb_t* currentProcess; // pointer to the current executing process


void exceptionHandler()
{
    state_t* exceptionState = (state_t*) BIOSDATAPAGE; // processor 0 exception state (BIOS data page)

    // 0 0 00 000000000000 00000000 0 11111 00
    unsigned int cause = (exceptionState->cause & GETEXECCODE) >> 2; // cause of the exception
    exceptionCode = cause;
    
    if(cause == 0){
        // Device interrupt
        InterruptHandler(exceptionState->cause); // Invoke Nucleus device interrupt handler
        return;
    }

    if(cause == 1 || cause == 2 || cause == 3){
        // TLB exceptions
        PassUpOrDie(0, exceptionState); // TLB exception handler
        return;
    }

    if(cause == 8){
        if(((exceptionState->status) & 0b1000) == 0){
            // Kernel mode
            SyscallExceptionHandler(exceptionState);
        } else {
            // User mode
            cause = 10;
            exceptionHandler();
        }
        return;
    }

    PassUpOrDie(1, exceptionState);

}

void PassUpOrDie(int exceptionType, state_t* exceptionState)
{
    if (currentProcess->p_supportStruct == NULL) {
        // Die portion - the current process and all its progeny are terminated
        Terminate_Process_SYS2();
    } else {
        // Passed up portion - pass up the handling of an exception to the support level (phase 3)
        (currentProcess->p_supportStruct)->sup_exceptState[exceptionType] = *exceptionState;
        context_t info = (currentProcess->p_supportStruct)->sup_exceptContext[exceptionType];
        LDCXT(info.stackPtr, info.status, info.pc );
    }
}