Process* ProcessManager::CreateProcess(char* appname, UINT32 processType) { FILE file; /* open file */ file = volOpenFile(appname); if (file.flags == FS_INVALID) return 0; if ((file.flags & FS_DIRECTORY) == FS_DIRECTORY) return 0; pdirectory* addressSpace = 0; addressSpace = vmmngr_createAddressSpace(); if (!addressSpace) { volCloseFile(&file); return 0; } mapKernelSpace(addressSpace); Process* pProcess = new Process(); pProcess->TaskID = ProcessManager::GetInstance()->GetNextProcessId(); pProcess->pPageDirectory = addressSpace; pProcess->dwPriority = 1; pProcess->dwRunState = PROCESS_STATE_ACTIVE; Thread* pThread = CreateThread(pProcess, &file); List_Add(&pProcess->pThreadQueue, "", pThread); return pProcess; }
//! read command void cmd_read() { //! get pathname char path[32]; DebugPrintf("\n\rex: \"file.txt\", \"a:\\file.txt\", \"a:\\folder\\file.txt\"\n\rFilename> "); console.GetCommand(path, 30); //! open file FILE file = volOpenFile(path); //! test for invalid file if (file.flags == FS_INVALID) { DebugPrintf("\n\rUnable to open file"); return; } //! cant display directories if ((file.flags & FS_DIRECTORY) == FS_DIRECTORY) { DebugPrintf("\n\rUnable to display contents of directory."); return; } //! top line DebugPrintf("\n\n\r-------[%s]-------\n\r", file.name); //! display file contents while (file.eof != 1) { //! read cluster unsigned char buf[512]; volReadFile(&file, buf, 512); //! display file for (int i = 0; i<512; i++) DebugPutc(buf[i]); //! wait for input to continue if not EOF if (file.eof != 1) { DebugPrintf("\n\r------[Press a key to continue]------"); console.GetChar(); DebugPrintf("\r"); //clear last line } } //! done :) DebugPrintf("\n\n\r--------[EOF]--------"); }
/** * Create process * \param appname Application file name * \ret Status code */ int createProcess (char* appname) { IMAGE_DOS_HEADER* dosHeader = 0; IMAGE_NT_HEADERS* ntHeaders = 0; FILE file; pdirectory* addressSpace = 0; process* proc = 0; thread* mainThread = 0; unsigned char* memory = 0; unsigned char buf[512]; uint32_t i = 0; /* open file */ file = volOpenFile (appname); if (file.flags == FS_INVALID) return 0; if (( file.flags & FS_DIRECTORY ) == FS_DIRECTORY) return 0; /* read 512 bytes into buffer */ volReadFile ( &file, buf, 512); if (! validateImage (buf)) { volCloseFile ( &file ); return 0; } dosHeader = (IMAGE_DOS_HEADER*)buf; ntHeaders = (IMAGE_NT_HEADERS*)(dosHeader->e_lfanew + (uint32_t)buf); /* get process virtual address space */ // addressSpace = vmmngr_createAddressSpace (); addressSpace = vmmngr_get_directory (); if (!addressSpace) { volCloseFile (&file); return 0; } /* map kernel space into process address space. Only needed if creating new address space */ //mapKernelSpace (addressSpace); /* create PCB */ proc = getCurrentProcess(); proc->id = 1; proc->pageDirectory = addressSpace; proc->priority = 1; proc->state = PROCESS_STATE_ACTIVE; proc->threadCount = 1; /* create thread descriptor */ mainThread = &proc->threads[0]; mainThread->kernelStack = 0; mainThread->parent = proc; mainThread->priority = 1; mainThread->state = PROCESS_STATE_ACTIVE; mainThread->initialStack = 0; mainThread->stackLimit = (void*) ((uint32_t) mainThread->initialStack + 4096); mainThread->imageBase = ntHeaders->OptionalHeader.ImageBase; mainThread->imageSize = ntHeaders->OptionalHeader.SizeOfImage; memset (&mainThread->frame, 0, sizeof (trapFrame)); mainThread->frame.eip = ntHeaders->OptionalHeader.AddressOfEntryPoint + ntHeaders->OptionalHeader.ImageBase; mainThread->frame.flags = 0x200; /* copy our 512 block read above and rest of 4k block */ memory = (unsigned char*)pmmngr_alloc_block(); memset (memory, 0, 4096); memcpy (memory, buf, 512); /* load image into memory */ for (i=1; i <= mainThread->imageSize/512; i++) { if (file.eof == 1) break; volReadFile ( &file, memory+512*i, 512); } /* map page into address space */ vmmngr_mapPhysicalAddress (proc->pageDirectory, ntHeaders->OptionalHeader.ImageBase, (uint32_t) memory, I86_PTE_PRESENT|I86_PTE_WRITABLE|I86_PTE_USER); /* load and map rest of image */ i = 1; while (file.eof != 1) { /* allocate new frame */ unsigned char* cur = (unsigned char*)pmmngr_alloc_block(); /* read block */ int curBlock = 0; for (curBlock = 0; curBlock < 8; curBlock++) { if (file.eof == 1) break; volReadFile ( &file, cur+512*curBlock, 512); } /* map page into process address space */ vmmngr_mapPhysicalAddress (proc->pageDirectory, ntHeaders->OptionalHeader.ImageBase + i*4096, (uint32_t) cur, I86_PTE_PRESENT|I86_PTE_WRITABLE|I86_PTE_USER); i++; } /* Create userspace stack (process esp=0x100000) */ void* stack = (void*) (ntHeaders->OptionalHeader.ImageBase + ntHeaders->OptionalHeader.SizeOfImage + PAGE_SIZE); void* stackPhys = (void*) pmmngr_alloc_block (); /* map user process stack space */ vmmngr_mapPhysicalAddress (addressSpace, (uint32_t) stack, (uint32_t) stackPhys, I86_PTE_PRESENT|I86_PTE_WRITABLE|I86_PTE_USER); /* final initialization */ mainThread->initialStack = stack; mainThread->frame.esp = (uint32_t)mainThread->initialStack; mainThread->frame.ebp = mainThread->frame.esp; /* close file and return process ID */ volCloseFile(&file); return proc->id; }