| 271 | } |
| 272 | |
| 273 | pid_t CreateChildThread(process_t* process, uintptr_t entry, uintptr_t stack, uint64_t cs, uint64_t ss){ |
| 274 | pid_t threadID = process->threadCount++; |
| 275 | process->threads.add_back(new thread_t); |
| 276 | |
| 277 | thread_t& thread = *process->threads[threadID]; |
| 278 | |
| 279 | thread.tid = threadID; |
| 280 | |
| 281 | thread.parent = process; |
| 282 | thread.registers.rip = entry; |
| 283 | thread.registers.rsp = stack; |
| 284 | thread.registers.rbp = stack; |
| 285 | thread.state = ThreadStateRunning; |
| 286 | thread.stack = thread.stackLimit = reinterpret_cast<void*>(stack); |
| 287 | |
| 288 | thread.fxState = Memory::KernelAllocate4KPages(1); // Allocate Memory for the FPU/Extended Register State |
| 289 | Memory::KernelMapVirtualMemory4K(Memory::AllocatePhysicalMemoryBlock(), (uintptr_t)thread.fxState, 1); |
| 290 | memset(thread.fxState, 0, 1024); |
| 291 | |
| 292 | void* kernelStack = Memory::KernelAllocate4KPages(32); // Allocate Memory For Kernel Stack (128KB) |
| 293 | for(int i = 0; i < 32; i++){ |
| 294 | Memory::KernelMapVirtualMemory4K(Memory::AllocatePhysicalMemoryBlock(),reinterpret_cast<uintptr_t>(kernelStack) + PAGE_SIZE_4K * i, 1); |
| 295 | } |
| 296 | memset(kernelStack, 0, PAGE_SIZE_4K * 32); |
| 297 | thread.kernelStack = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(kernelStack) + PAGE_SIZE_4K * 32); |
| 298 | |
| 299 | regs64_t* registers = &thread.registers; |
| 300 | registers->rflags = 0x202; // IF - Interrupt Flag, bit 1 should be 1 |
| 301 | thread.registers.cs = cs; |
| 302 | thread.registers.ss = ss; |
| 303 | thread.timeSliceDefault = THREAD_TIMESLICE_DEFAULT; |
| 304 | thread.timeSlice = thread.timeSliceDefault; |
| 305 | thread.priority = 4; |
| 306 | |
| 307 | ((fx_state_t*)thread.fxState)->mxcsr = 0x1f80; // Default MXCSR (SSE Control Word) State |
| 308 | ((fx_state_t*)thread.fxState)->mxcsrMask = 0xffbf; |
| 309 | ((fx_state_t*)thread.fxState)->fcw = 0x33f; // Default FPU Control Word State |
| 310 | |
| 311 | InsertNewThreadIntoQueue(&thread); |
| 312 | |
| 313 | return threadID; |
| 314 | } |
| 315 | |
| 316 | void EndProcess(process_t* process){ |
| 317 | asm("sti"); |
no test coverage detected