00001 // progtest.cc 00002 // Test routines for demonstrating that Nachos can load 00003 // a user program and execute it. 00004 // 00005 // Also, routines for testing the Console hardware device. 00006 // 00007 // Copyright (c) 1992-1993 The Regents of the University of California. 00008 // All rights reserved. See copyright.h for copyright notice and limitation 00009 // of liability and disclaimer of warranty provisions. 00010 00011 #include "copyright.h" 00012 #include "system.h" 00013 #include "console.h" 00014 #include "addrspace.h" 00015 #include "synch.h" 00016 00017 //---------------------------------------------------------------------- 00018 // StartProcess 00019 // Run a user program. Open the executable, load it into 00020 // memory, and jump to it. 00021 //---------------------------------------------------------------------- 00022 00023 void 00024 StartProcess(char *filename) 00025 { 00026 OpenFile *executable = fileSystem->Open(filename); 00027 AddrSpace *space; 00028 00029 if (executable == NULL) { 00030 printf("Unable to open file %s\n", filename); 00031 return; 00032 } 00033 space = new AddrSpace(executable); 00034 currentThread->space = space; 00035 00036 delete executable; // close file 00037 00038 space->InitRegisters(); // set the initial register values 00039 space->RestoreState(); // load page table register 00040 00041 machine->Run(); // jump to the user progam 00042 ASSERT(FALSE); // machine->Run never returns; 00043 // the address space exits 00044 // by doing the syscall "exit" 00045 } 00046 00047 // Data structures needed for the console test. Threads making 00048 // I/O requests wait on a Semaphore to delay until the I/O completes. 00049 00050 static Console *console; 00051 static Semaphore *readAvail; 00052 static Semaphore *writeDone; 00053 00054 //---------------------------------------------------------------------- 00055 // ConsoleInterruptHandlers 00056 // Wake up the thread that requested the I/O. 00057 //---------------------------------------------------------------------- 00058 00059 static void ReadAvail(int arg) { readAvail->V(); } 00060 static void WriteDone(int arg) { writeDone->V(); } 00061 00062 //---------------------------------------------------------------------- 00063 // ConsoleTest 00064 // Test the console by echoing characters typed at the input onto 00065 // the output. Stop when the user types a 'q'. 00066 //---------------------------------------------------------------------- 00067 00068 void 00069 ConsoleTest (char *in, char *out) 00070 { 00071 char ch; 00072 00073 console = new Console(in, out, ReadAvail, WriteDone, 0); 00074 readAvail = new Semaphore("read avail", 0); 00075 writeDone = new Semaphore("write done", 0); 00076 00077 for (;;) { 00078 readAvail->P(); // wait for character to arrive 00079 ch = console->GetChar(); 00080 console->PutChar(ch); // echo it! 00081 writeDone->P() ; // wait for write to finish 00082 if (ch == 'q') return; // if q, quit 00083 } 00084 }
1.2.14 written by Dimitri van Heesch,
© 1997-2002