/* * Disables the interrupts. */ static void disableInterrupts() { /* turn the interrupts OFF iff we are in kernel mode */ if( (USLOSS_PSR_CURRENT_MODE & USLOSS_PsrGet()) == 0 ) { //not in kernel mode USLOSS_Console("Kernel Error: Not in kernel mode, may not "); USLOSS_Console("disable interrupts\n"); USLOSS_Halt(1); } else /* We ARE in kernel mode */ USLOSS_PsrSet( USLOSS_PsrGet() & ~USLOSS_PSR_CURRENT_INT ); } /* disableInterrupts */
/* * Enables the interrupts */ static void enableInterrupts(int dev, void *args) { /* turn the interrupts ON iff we are in kernel mode */ if ( (USLOSS_PSR_CURRENT_MODE & USLOSS_PsrGet()) == 0 ) { //not in kernel mode USLOSS_Console("Kernel Error: Not in kernel mode, may not "); USLOSS_Console("disable interrupts\n"); USLOSS_Halt(1); } else { /* We ARE in kernel mode */ USLOSS_PsrSet( USLOSS_PsrGet() | USLOSS_PSR_CURRENT_INT); } }
/* 0 == we are in kernel mode. continue. 1 == we are not in kernel mode. error message. */ static int permissionCheck(void) { if ((USLOSS_PsrGet() & 0x1) < 1) { USLOSS_Console("Must be in Kernel mode to perform this request. Stopping requested operation\n"); return 1; } return 0; }
/* ------------------------------------------------------------------------ Name - launch Purpose - Dummy function to enable interrupts and launch a given process upon startup. Parameters - none Returns - nothing Side Effects - enable interrupts ------------------------------------------------------------------------ */ void launch(void){ Check_Your_Privilege(); int rc; USLOSS_PsrSet(USLOSS_PsrGet() | USLOSS_PSR_CURRENT_INT); rc = procTable[currPid].startFunc(procTable[currPid].startArg); // USLOSS_Console("Laung Ending for %s\n",procTable[currPid].name); /* quit if we ever come back */ P1_Quit(rc); } /* End of launch */
void startup() { USLOSS_Console("startup\n"); USLOSS_IntVec[USLOSS_CLOCK_INT] = dummy; USLOSS_Console("1 psr = %d\n", USLOSS_PsrGet()); USLOSS_PsrSet(USLOSS_PsrGet() | USLOSS_PSR_CURRENT_INT); USLOSS_Console("2 psr = %d\n", USLOSS_PsrGet()); USLOSS_PsrSet(USLOSS_PsrGet() & ~USLOSS_PSR_CURRENT_INT); USLOSS_Console("3 psr = %d\n", USLOSS_PsrGet()); USLOSS_PsrSet((USLOSS_PsrGet() & ~USLOSS_PSR_CURRENT_MODE) | USLOSS_PSR_CURRENT_INT); USLOSS_Console("4 psr = %d\n", USLOSS_PsrGet()); USLOSS_Console("This will cause a trap.\n"); USLOSS_PsrSet(3); }
void startup() { int status; int i, j, k; FILE *f; char name[50]; int n; // Compute the inputs and write them to the term*.in file. Each terminal reads unique content. k = 0; for(i = 0; i < USLOSS_TERM_UNITS; i++) { memset(buffers[i], '\0', NUMCHARS); counts[i] = 0; snprintf(name, sizeof(name), "term%d.in", i); f = fopen(name, "w"); assert(f != NULL); for (j = 0; j < NUMCHARS; j++) { inputs[i][j] = 'a' + k++; } n = fwrite(inputs[i], 1, NUMCHARS, f); assert(n == NUMCHARS); fclose(f); } for (i = 0; i < USLOSS_NUM_INTS; i++) { USLOSS_IntVec[i] = dummy_handler; } // Turn on receive interrupts. for(i = 0; i < USLOSS_TERM_UNITS; i++) { status = USLOSS_DeviceOutput(USLOSS_TERM_DEV, i, (void *) USLOSS_TERM_CTRL_RECV_INT(0)); } USLOSS_IntVec[USLOSS_TERM_INT] = term_handler; // Turn on interrupts. USLOSS_PsrSet(USLOSS_PsrGet() | USLOSS_PSR_CURRENT_INT); // Read from the terminals. while(done < USLOSS_TERM_UNITS ) { USLOSS_WaitInt(); } USLOSS_Halt(0); }
static void syscallHandler(int dev, void *args) { // get args systemArgs *sysPtr = (systemArgs *) args; // check if valid dev if (dev != USLOSS_SYSCALL_INT) { USLOSS_Console("syscallHandler(): Bad call\n"); USLOSS_Halt(1); } // check if valid range of args if (sysPtr->number < 0 || sysPtr->number >= MAXSYSCALLS) { USLOSS_Console("syscallHandler(): sys number %d is wrong. Halting...\n", sysPtr->number); USLOSS_Halt(1); } USLOSS_PsrSet( USLOSS_PsrGet() | USLOSS_PSR_CURRENT_INT); sys_vec[sysPtr->number](sysPtr); }
void startup() { int status; int i; for (i = 0; i < USLOSS_NUM_INTS; i++) { USLOSS_IntVec[i] = dummy_handler; } // Turn on receive interrupts for terminal 0. status = USLOSS_DeviceOutput(USLOSS_TERM_DEV, 0, (void *) USLOSS_TERM_CTRL_RECV_INT(0)); USLOSS_IntVec[USLOSS_TERM_INT] = term_handler; // Turn on interrupts. USLOSS_PsrSet(USLOSS_PsrGet() | USLOSS_PSR_CURRENT_INT); // Wait in an infinite loops for interrupts. while(1) { USLOSS_WaitInt(); } }
static void userMode(void) { USLOSS_PsrSet(USLOSS_PsrGet() & ~(USLOSS_PSR_CURRENT_MODE)); }
static void interruptsOn(void) { USLOSS_PsrSet(USLOSS_PsrGet() | USLOSS_PSR_CURRENT_INT); }
/* * Checks if we are in Kernel mode */ void check_kernel_mode(char *name) { if ((USLOSS_PSR_CURRENT_MODE & USLOSS_PsrGet()) == 0) { USLOSS_Console("%s(): Called while in user mode by process %d. Halting...\n", name, getpid()); USLOSS_Halt(1); } }
/* ------------------------------------------------------------------------ Name - P1_Fork Purpose - Gets a new process from the process table and initializes information of the process. Updates information in the parent process to reflect this child process creation. Parameters - the process procedure address, the size of the stack and the priority to be assigned to the child process. Returns - the process id of the created child or an error code. Side Effects - ReadyList is changed, procTable is changed, Current process information changed ------------------------------------------------------------------------ */ int P1_Fork(char *name, int (*f)(void *), void *arg, int stacksize, int priority) { /*Check current Mode. If not Kernel Mode return error*/ Check_Your_Privilege(); // USLOSS_Console("Proc %s passed Privilege Check\n",name); //free the available spots free_Procs(); /*Check Priority and Stack Size*/ if(priority<1||priority>6){//is priority # valid return -3; } if(stacksize<USLOSS_MIN_STACK){//is stacksize valid return -2; } // P1_Semaphore sema=&semTable[0]; //find PID int newPid = 0; while(procTable[newPid].priority!=-1){ newPid++; if(newPid>=P1_MAXPROC){ return -1; } } int_disable(); /* stack = allocated stack here */ // void* newStack=malloc(stacksize*sizeof(char)); procTable[newPid].stack=malloc(stacksize*sizeof(char)); procTable[newPid].notFreed=1; /*set PCB fields*/ procTable[newPid].PID=newPid; procTable[newPid].cpuTime=0; procTable[newPid].lastStartedTime=FIRST_RUN; procTable[newPid].state=1;//0=running 1=ready,2=killed,3=quit,4=waiting procTable[newPid].status=DEFAULT; procTable[newPid].isOrphan= 0; if(currPid==-1){ // USLOSS_Console("Start up %s Orphan\n",name); procTable[newPid].isOrphan=1; } procTable[newPid].parent=currPid; procTable[currPid].numChildren++;//increment parents numChildren procTable[newPid].numChildren=0; procTable[newPid].priority=priority; procTable[newPid].waitingOnDevice=0; procTable[newPid].name=strdup(name); procTable[newPid].startFunc = f; procTable[newPid].startArg = arg; /*PCB Fields are set*/ // USLOSS_Console("proc %s forked to PID %d\n",name,newPid); /*add to ready list*/ addToReadyList(newPid); /*increment numProcs*/ numProcs++; /*initialize context*/ USLOSS_ContextInit(&(procTable[newPid].context), USLOSS_PsrGet(), procTable[newPid].stack, stacksize, launch); int_enable(); /*Run dispatcher if forking higher priority process*/ if(currPid != -1&&priority<procTable[currPid].priority){ dispatcher(); } //USLOSS_Console("In Fork PID -- after: %d\n", currPid); return newPid; } /* End of fork */
void int_enable(){ USLOSS_PsrSet(USLOSS_PsrGet() | USLOSS_PSR_CURRENT_INT); }
void int_disable(){ USLOSS_PsrSet(USLOSS_PsrGet() & ~(USLOSS_PSR_CURRENT_INT)); }
/*Checks Whether or not thecurrent process is in Kernel Mode*/ void Check_Your_Privilege(){ if((USLOSS_PsrGet() & USLOSS_PSR_CURRENT_MODE)==0){ USLOSS_Console("Error: Access Denied to User Mode\n"); exit(1); } }