void TerminateMemoryProcess() { Process* pProcess = (Process*)List_GetData(ProcessManager::GetInstance()->pProcessQueue, "", 0); if (pProcess->TaskID == PROC_INVALID_ID) { DebugPrintf("\nsdffdsdsfsd"); return; } /* release threads */ int i = 0; Thread* pThread = (Thread*)List_GetData(pProcess->pThreadQueue, "", 0); /* get physical address of stack */ void* stackFrame = vmmngr_getPhysicalAddress(pProcess->pPageDirectory, (uint32_t)pThread->initialStack); DebugPrintf("\n Plane X : %d, Plane Y : %d", 1, 1); /* unmap and release stack memory */ vmmngr_unmapPhysicalAddress(pProcess->pPageDirectory, (uint32_t)pThread->initialStack); //pmmngr_free_block(stackFrame); /* unmap and release image memory */ for (uint32_t page = 0; page < pThread->imageSize / PAGE_SIZE; page++) { uint32_t phys = 0; uint32_t virt = 0; /* get virtual address of page */ virt = pThread->imageBase + (page * PAGE_SIZE); /* get physical address of page */ phys = (uint32_t)vmmngr_getPhysicalAddress(pProcess->pPageDirectory, virt); /* unmap and release page */ vmmngr_unmapPhysicalAddress(pProcess->pPageDirectory, virt); //pmmngr_free_block((void*)phys); } /* restore kernel selectors */ __asm { cli mov eax, 0x10 mov ds, ax mov es, ax mov fs, ax mov gs, ax sti } pmmngr_load_PDBR((physical_addr)vmmngr_get_directory()); /* return to kernel command shell */ run(); DebugPrintf("\nExit command recieved; demo halted"); for (;;); }
inline int vmmngr_switch_pdirectory(pdirectory * dir) { if (!dir) { return 0; } _cur_directory = dir; pmmngr_load_PDBR((physical_addr) _cur_directory->m_entries); return 1; }
bool ProcessManager::ExecuteProcess(Process* pProcess) { //pTest = ProcessManager::CreateProcess("proc.exe"); if (pProcess == 0) return false; int entryPoint = 0; unsigned int procStack = 0; if (pProcess->TaskID == PROC_INVALID_ID) return false; if (!pProcess->pPageDirectory) return false; Thread* pThread = (Thread*)List_GetData(pProcess->pThreadQueue, "", 0); /* get esp and eip of main thread */ entryPoint = pThread->frame.eip; procStack = pThread->frame.esp; DebugPrintf("\neip : %x", pThread->frame.eip); DebugPrintf("\npage directory : %x", pProcess->pPageDirectory); __asm cli ProcessManager::GetInstance()->g_pCurProcess = pProcess; List_Add(&pProcessQueue, "", pProcess); __asm sti pmmngr_load_PDBR((physical_addr)pProcess->pPageDirectory); __asm { mov ax, 0x23; user mode data selector is 0x20 (GDT entry 3).Also sets RPL to 3 mov ds, ax mov es, ax mov fs, ax mov gs, ax ; ; create stack frame ; push 0x23; SS, notice it uses same selector as above push[procStack]; stack push 0x200; EFLAGS push 0x1b; CS, user mode code selector is 0x18.With RPL 3 this is 0x1b push[entryPoint]; EIP iretd } return true; }
void cmd_memtask() { Process* pProcess = ProcessManager::GetInstance()->CreateMemoryProcess(SampleLoop); if (pProcess) { int entryPoint = 0; unsigned int procStack = 0; Thread* pThread = (Thread*)List_GetData(pProcess->pThreadQueue, "", 0); /* get esp and eip of main thread */ entryPoint = pThread->frame.eip; procStack = pThread->frame.esp; /* switch to process address space */ __asm cli pmmngr_load_PDBR((physical_addr)pProcess->pPageDirectory); /* execute process in user mode */ __asm { mov ax, 0x10; user mode data selector is 0x20 (GDT entry 3).Also sets RPL to 3 mov ds, ax mov es, ax mov fs, ax mov gs, ax ; ; create stack frame ; push 0x10; SS, notice it uses same selector as above push[procStack]; stack push 0x200; EFLAGS push 0x08; CS, user mode code selector is 0x18.With RPL 3 this is 0x1b push[entryPoint]; EIP iretd } } ProcessManager::GetInstance()->CreateMemoryProcess(SampleLoop2); }
error_t vmmngr_switch_directory(pdirectory* dir, physical_addr pdbr) { // if the page directory hasn't change do not flush cr3 as such an action is a performance hit if (pmmngr_get_PDBR() == pdbr) return ERROR_OK; if (!dir) { set_last_error(EINVAL, VMEM_BAD_ARGUMENT, EO_VMMNGR); return ERROR_OCCUR; } current_directory = dir; current_pdbr = pdbr; pmmngr_load_PDBR(current_pdbr); return ERROR_OK; }
/** * Execute process */ void executeProcess () { process* proc = 0; int entryPoint = 0; unsigned int procStack = 0; /* get running process */ proc = getCurrentProcess(); if (proc->id==PROC_INVALID_ID) return; if (!proc->pageDirectory) return; /* get esp and eip of main thread */ entryPoint = proc->threads[0].frame.eip; procStack = proc->threads[0].frame.esp; /* switch to process address space */ __asm cli pmmngr_load_PDBR ((physical_addr)proc->pageDirectory); /* execute process in user mode */ __asm { mov ax, 0x23 ; user mode data selector is 0x20 (GDT entry 3). Also sets RPL to 3 mov ds, ax mov es, ax mov fs, ax mov gs, ax ; ; create stack frame ; push 0x23 ; SS, notice it uses same selector as above push [procStack] ; stack push 0x200 ; EFLAGS push 0x1b ; CS, user mode code selector is 0x18. With RPL 3 this is 0x1b push [entryPoint] ; EIP iretd } }
void TerminateProcess () { Process* cur = ProcessManager::GetInstance()->g_pCurProcess; if (cur->TaskID == PROC_INVALID_ID) { DebugPrintf("\nsdffdsdsfsd"); return; } /* release threads */ int i=0; //Thread* pThread = ProcessManager::GetInstance()->g_pThread; Thread* pThread = (Thread*)List_GetData(cur->pThreadQueue, "", 0); List_Delete(&cur->pThreadQueue, "", 0); u32int heapAddess = pThread->imageBase + pThread->imageSize + PAGE_SIZE + PAGE_SIZE * 2; heapAddess = heapAddess - (heapAddess % PAGE_SIZE); for (int i = 0; i < 300; i++) { uint32_t phys = 0; uint32_t virt = 0; /* get virtual address of page */ virt = heapAddess + (i * PAGE_SIZE); /* get physical address of page */ phys = (uint32_t)vmmngr_getPhysicalAddress(cur->pPageDirectory, virt); /* unmap and release page */ vmmngr_unmapPhysicalAddress(cur->pPageDirectory, virt); } /* unmap and release image memory */ for (uint32_t page = 0; page < cur->dwPageCount; page++) { uint32_t phys = 0; uint32_t virt = 0; /* get virtual address of page */ virt = pThread->imageBase + (page * PAGE_SIZE); /* get physical address of page */ phys = (uint32_t)vmmngr_getPhysicalAddress(cur->pPageDirectory, virt); /* unmap and release page */ vmmngr_unmapPhysicalAddress(cur->pPageDirectory, virt); //pmmngr_free_block ((void*)phys); } /* get physical address of stack */ void* stackFrame = vmmngr_getPhysicalAddress(cur->pPageDirectory, (uint32_t)pThread->initialStack); /* unmap and release stack memory */ vmmngr_unmapPhysicalAddress(cur->pPageDirectory, (uint32_t)pThread->initialStack); //pmmngr_free_block(stackFrame); pmmngr_free_block(cur->pPageDirectory); delete pThread; delete cur; /* restore kernel selectors */ __asm { cli mov eax, 0x10 mov ds, ax mov es, ax mov fs, ax mov gs, ax sti } pmmngr_load_PDBR((physical_addr)vmmngr_get_directory()); /* return to kernel command shell */ run (); DebugPrintf ("\nExit command recieved; demo halted"); for (;;); }
bool Scheduler::DoSchedule(int tick, uint32_t registers) { //DebugPrintf("\nScheduler"); int entryPoint = 0; unsigned int procStack = 0; /* switch to process address space */ if (systemOn == true) { Process* curProcess = ProcessManager::GetInstance()->g_pCurProcess; if (curProcess) { Thread* pCurThread = (Thread*)List_GetData(curProcess->pThreadQueue, "", 0); pCurThread->curESP = (*(uint32_t*)(registers - 8)); pCurThread->curFlags = (*(uint32_t*)(registers - 12)); pCurThread->curCS = (*(uint32_t*)(registers - 16)); pCurThread->curEip = (*(uint32_t*)(registers - 20)); /*pCurThread->curgs = (*(uint16_t*)(registers - 24)); pCurThread->curfs = (*(uint16_t*)(registers - 28)); pCurThread->curEs = (*(uint16_t*)(registers - 32)); pCurThread->curds = (*(uint16_t*)(registers - 36));*/ pCurThread->frame.eax = (*(uint32_t*)(registers - 24)); pCurThread->frame.ecx = (*(uint32_t*)(registers - 28)); pCurThread->frame.edx = (*(uint32_t*)(registers - 32)); pCurThread->frame.ebx = (*(uint32_t*)(registers - 36)); pCurThread->frame.esp = (*(uint32_t*)(registers - 40)); pCurThread->frame.ebp = (*(uint32_t*)(registers - 44)); pCurThread->frame.esi = (*(uint32_t*)(registers - 48)); pCurThread->frame.edi = (*(uint32_t*)(registers - 52)); LISTNODE *pProcessList = ProcessManager::GetInstance()->pProcessQueue; int processCount = List_Count(pProcessList); for (int index = 0; index < processCount; index++) { Process* pProcess = (Process*)List_GetData(pProcessList, "", index); if (curProcess == pProcess) continue; pProcess->dwTickCount++; if (pProcess->dwTickCount > 10) { pProcess->dwTickCount = 0; if (pProcess->dwProcessType == PROCESS_USER) { console.Print("Scheduler1 : %d\n", processCount); ProcessManager::GetInstance()->g_pCurProcess = pProcess; Thread* pThread = (Thread*)List_GetData(pProcess->pThreadQueue, "", 0); entryPoint = pThread->curEip; procStack = pThread->curESP; uint32_t eflags = pThread->curFlags; console.Print("eip : %x\n", pThread->curEip); console.Print("CS : %x\n", pThread->curCS); console.Print("flags : %x\n", pThread->curFlags); console.Print("esp : %x\n", pThread->curESP); //entryPoint = pThread->frame.eip; //procStack = pThread->frame.esp; uint32_t regEax = pThread->frame.eax; uint32_t regEcx = pThread->frame.ecx; uint32_t regEdx = pThread->frame.edx; uint32_t regEbx = pThread->frame.ebx; uint32_t regEsp = pThread->frame.esp; uint32_t regEbp = pThread->frame.ebp; uint32_t regEsi = pThread->frame.esi; uint32_t regEdi = pThread->frame.edi; uint16_t regGs = pThread->curgs; uint16_t regFs = pThread->curfs; uint16_t regEs = pThread->curEs; uint16_t regDs = pThread->curds; pmmngr_load_PDBR((physical_addr)pProcess->pPageDirectory); OutPortByte(0x20, 0x20); __asm { mov ax, 0x23; user mode data selector is 0x20 (GDT entry 3).Also sets RPL to 3 mov ds, ax mov es, ax mov fs, ax mov gs, ax ; create stack frame ; push 0x23; SS, notice it uses same selector as above push[procStack]; stack push[eflags]; EFLAGS push 0x1b; CS, user mode code selector is 0x18.With RPL 3 this is 0x1b push[entryPoint]; EIP mov eax, regEax mov ecx, regEcx mov edx, regEdx mov ebx, regEbx mov esp, regEsp mov ebp, regEbp mov esi, regEsi mov edi, regEdi iretd } } else { /*if (aa == 0) { aa = 1; DebugPrintf("\nasdasas : %x", registers); for (int i = 0; i < 20; i++) DebugPrintf("\nasdasas : %x", (*(uint32_t*)(registers - i * 4))); while (1); }*/ console.Print("Scheduler2\n"); ProcessManager::GetInstance()->g_pCurProcess = pProcess; Thread* pThread = (Thread*)List_GetData(pProcess->pThreadQueue, "", 0); entryPoint = pThread->frame.eip; procStack = pThread->frame.esp; //procStack = 0; pmmngr_load_PDBR((physical_addr)pProcess->pPageDirectory); OutPortByte(0x20, 0x20); __asm { mov ax, 0x10; mov ds, ax mov es, ax mov fs, ax mov gs, ax ; ; create stack frame ; push 0x10; SS, notice it uses same selector as above push[procStack]; stack push 0x200; EFLAGS push 0x08; push[entryPoint]; EIP iretd } } }