Example #1
0
Thread* ProcessManager::CreateMemoryThread(Process* pProcess, void(*lpStartAddress)())
{
	Thread* pThread = new Thread();
	pThread->pParent = pProcess;

	pThread->kernelStack = 0;
	pThread->priority = 1;
	pThread->state = PROCESS_STATE_ACTIVE;
	pThread->initialStack = 0;
	pThread->stackLimit = (void*)((uint32_t)pThread->initialStack + 4096);
	pThread->imageBase = 0;
	pThread->imageSize = 0;
	memset(&pThread->frame, 0, sizeof(trapFrame));
	pThread->frame.eip = (uint32_t)lpStartAddress;
	pThread->frame.flags = 0x200;

	/* Create userspace stack (process esp=0x100000) */
	void* stack = (void*)0x200000;
	void* stackPhys = (void*)pmmngr_alloc_block();

	/* map user process stack space */
	vmmngr_mapPhysicalAddress(pProcess->pPageDirectory,
		(uint32_t)stack,
		(uint32_t)stackPhys,
		I86_PTE_PRESENT | I86_PTE_WRITABLE);

	/* final initialization */
	pThread->initialStack = stack;
	pThread->frame.esp = (uint32_t)pThread->initialStack;
	pThread->frame.ebp = pThread->frame.esp;

	return pThread;
	
}
Example #2
0
	void CreateDefaultHeap() {
		
		Process* pProcess = ProcessManager::GetInstance()->g_pCurProcess;
		Thread* pThread = (Thread*)List_GetData(pProcess->pThreadQueue, "", 0);
		//Thread* pThread = ProcessManager::GetInstance()->g_pThread;
		void* pHeapaaPhys = (void*)pmmngr_alloc_blocks(300);
		
		u32int heapAddess = pThread->imageBase + pThread->imageSize + PAGE_SIZE + PAGE_SIZE * 2;
		heapAddess = heapAddess - (heapAddess % PAGE_SIZE);
		//u32int heapAddess = 0xB0000000;
		DebugPrintf("\nheap adress %x", heapAddess);
		
		for (int i = 0; i < 300; i++)
		{
			vmmngr_mapPhysicalAddress(pProcess->pPageDirectory,
				(uint32_t)heapAddess + i * PAGE_SIZE,
				(uint32_t)pHeapaaPhys + i * PAGE_SIZE,
				I86_PTE_PRESENT | I86_PTE_WRITABLE | I86_PTE_USER);
		}
		//pmmngr_load_PDBR((physical_addr)pProcezss->pPageDirectory);
		memset((void*)heapAddess, 0, 300 * PAGE_SIZE);
		DebugPrintf("\nimageSize %x", pThread->imageSize);
		pThread->lpHeap = create_heap((u32int)heapAddess, (uint32_t)heapAddess + 300 * 4096, (uint32_t)heapAddess + 300 * 4096, 0, 0);
		//DebugPrintf("\nThread Creation Success %x", pThread->lpHeap);
		//DebugPrintf("\ndfsdfds %x", pHeapPhys);
		//DebugPrintf("\nkkkkk");
	}
/**
* Map kernel space into address space
* \param addressSpace Page directory
*/
void mapKernelSpace (pdirectory* addressSpace) {
	uint32_t virtualAddr;
	uint32_t physAddr;
	/*
		default flags. Note USER bit not set to prevent user mode access
	*/
	int flags = I86_PTE_PRESENT|I86_PTE_WRITABLE;
	/*
		map kernel stack space (at idenitity mapped 0x8000-0x9fff)
	*/
	vmmngr_mapPhysicalAddress (addressSpace, 0x8000, 0x8000, flags);
	vmmngr_mapPhysicalAddress (addressSpace, 0x9000, 0x9000, flags);
	/*
		map kernel image (7 pages at physical 1MB, virtual 3GB)
	*/
	virtualAddr = 0xc0000000;
	physAddr    = 0x100000;
	for (uint32_t i=0; i<10; i++) {
		vmmngr_mapPhysicalAddress (addressSpace,
			virtualAddr+(i*PAGE_SIZE),
			physAddr+(i*PAGE_SIZE),
			flags);
	}
	/*
		map display memory for debug minidriver
		idenitity mapped 0xa0000-0xBF000.
		Note:
			A better alternative is to have a driver associated
			with the physical memory range map it. This should be automatic;
			through an IO manager or driver manager.
	*/
	virtualAddr = 0xa0000;
	physAddr = 0xa0000;
	for (uint32_t i=0; i<31; i++) {
		vmmngr_mapPhysicalAddress (addressSpace,
			virtualAddr+(i*PAGE_SIZE),
			physAddr+(i*PAGE_SIZE),
			flags);
	}

	/* map page directory itself into its address space */
	vmmngr_mapPhysicalAddress (addressSpace, (uint32_t) addressSpace,
			(uint32_t) addressSpace, I86_PTE_PRESENT|I86_PTE_WRITABLE);
}
Example #4
0
Thread* ProcessManager::CreateThread(Process* pProcess, FILE* file)
{
	unsigned char buf[512];
	IMAGE_DOS_HEADER* dosHeader = 0;
	IMAGE_NT_HEADERS* ntHeaders = 0;
	unsigned char* memory = 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);

	Thread* pThread = new Thread();
	pThread->pParent = pProcess;

	pThread->kernelStack = 0;
	pThread->priority = 1;
	pThread->state = PROCESS_STATE_ACTIVE;
	pThread->initialStack = 0;
	pThread->stackLimit = (void*)((uint32_t)pThread->initialStack + 4096);
	pThread->imageBase = ntHeaders->OptionalHeader.ImageBase;
	pThread->imageSize = ntHeaders->OptionalHeader.SizeOfImage;
	memset(&pThread->frame, 0, sizeof(trapFrame));
	pThread->frame.eip = (uint32_t)ntHeaders->OptionalHeader.AddressOfEntryPoint + ntHeaders->OptionalHeader.ImageBase;
	pThread->frame.flags = 0x200;
	

	int pageRest = 0;

	if ((pThread->imageSize % 4096) > 0)
		pageRest = 1;

	pProcess->dwPageCount = (pThread->imageSize / 4096) + pageRest;

	memory = (unsigned char*)pmmngr_alloc_blocks(pProcess->dwPageCount);
	
	/* map page into address space */
	for (int i = 0; i <pProcess->dwPageCount; i++)
	{
		//DebugPrintf("\ndsfdsd");
		vmmngr_mapPhysicalAddress(pProcess->pPageDirectory,
			ntHeaders->OptionalHeader.ImageBase + i * 4096,
			(uint32_t)memory + i * 4096,
			I86_PTE_PRESENT | I86_PTE_WRITABLE | I86_PTE_USER);
	}
	memset(memory, 0, pThread->imageSize);
	memcpy(memory, buf, 512);

	/* load image into memory */

	int fileRest = 0;
	if ((pThread->imageSize % 512) != 0)
		fileRest = 1;

	int readCount = (pThread->imageSize / 512) + fileRest;
	for (int i = 1; i < readCount; i++) {
		if (file->eof == 1)
			break;
		volReadFile(file, memory + 512 * i, 512);
	}
		
	/* close file and return process ID */
	volCloseFile(file);

	/* 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(pProcess->pPageDirectory,
		(uint32_t)stack,
		(uint32_t)stackPhys,
		I86_PTE_PRESENT | I86_PTE_WRITABLE | I86_PTE_USER);

	/* Create Heap*/
	
	/* final initialization */
	pThread->initialStack = stack;
	pThread->frame.esp = (uint32_t)pThread->initialStack;
	pThread->frame.ebp = pThread->frame.esp;

	//DebugPrintf("\nThread Creation Success");
	
	return pThread;

}
/**
* 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;
}