+
+[global NewTaskHeader]
+NewTaskHeader:
+ mov eax, [esp]
+ mov dr0, eax
+
+ mov eax, [esp+4]
+ add esp, 12 ; Thread, Function, Arg Count
+ call eax
+
+ push eax ; Ret val
+ push 0 ; 0 = This Thread
+ call Threads_Exit
+
+[extern MM_Clone]
+[global Proc_CloneInt]
+Proc_CloneInt:
+ pusha
+ ; Save RSP
+ mov eax, [esp+0x20+4]
+ mov [eax], esp
+ call MM_Clone
+ ; Save CR3
+ mov esi, [esp+0x20+8]
+ mov [esi], eax
+ ; Undo the pusha
+ add esp, 0x20
+ mov eax, .newTask
+ ret
+.newTask:
+ popa
+ xor eax, eax
+ ret
+
+[global SwitchTasks]
+; + 4 = New RSP
+; + 8 = Old RSP save loc
+; +12 = New RIP
+; +16 = Old RIP save loc
+; +20 = CR3
+SwitchTasks:
+ pusha
+
+ ; Old IP
+ mov eax, [esp+0x20+16]
+ mov DWORD [eax], .restore
+ ; Old SP
+ mov eax, [esp+0x20+8]
+ mov [eax], esp
+
+ mov ecx, [esp+0x20+12] ; New IP
+ mov eax, [esp+0x20+20] ; New CR3
+ mov esp, [esp+0x20+ 4] ; New SP
+
+ test eax, eax
+ jz .setState
+ mov cr3, eax
+ invlpg [esp]
+ invlpg [esp+0x1000]
+.setState:
+ jmp ecx
+
+.restore:
+
+ popa
+ xor eax, eax
+ ret
+
+[global Proc_InitialiseSSE]
+Proc_InitialiseSSE:
+ mov eax, cr4
+ or eax, (1 << 9)|(1 << 10) ; Set OSFXSR and OSXMMEXCPT
+ mov cr4, eax
+ mov eax, cr0
+ and ax, ~(1 << 2) ; Clear EM
+ or eax, (1 << 1) ; Set MP
+ mov eax, cr0
+ ret
+[global Proc_DisableSSE]
+Proc_DisableSSE:
+ mov eax, cr0
+ or ax, 1 << 3 ; Set TS
+ mov cr0, eax
+ ret
+[global Proc_EnableSSE]
+Proc_EnableSSE:
+ mov eax, cr0
+ and ax, ~(1 << 3) ; Clear TS
+ mov cr0, eax
+ ret
+
+[global Proc_SaveSSE]
+Proc_SaveSSE:
+ mov eax, [esp+4]
+ fxsave [eax]
+ ret
+[global Proc_RestoreSSE]
+Proc_RestoreSSE:
+ mov eax, [esp+4]
+ fxrstor [eax]
+ ret
+
+%if USE_MP
+[extern giMP_TimerCount]
+[extern gpMP_LocalAPIC]
+[extern Isr240.jmp]
+[global SetAPICTimerCount]
+SetAPICTimerCount:
+ pusha
+ push ds
+ push es
+ push fs
+ push gs
+
+ mov ax, 0x10
+ mov ds, ax
+ mov es, ax
+ mov fs, ax
+ mov gs, ax
+
+ mov eax, [gpMP_LocalAPIC]
+ mov ecx, [eax+0x320]
+ test ecx, 0x00010000
+ jz .setTime
+ mov DWORD [eax+0x380], 0xFFFFFFFF ; Set Initial Count
+ mov DWORD [eax+0x320], 0x000000F0 ; Enable the timer on IVT#0xEF (One Shot)
+ jmp .ret
+
+.setTime:
+ ; Get Timer Count
+ mov ecx, 0xFFFFFFFF
+ sub ecx, [eax+0x390]
+ mov DWORD [giMP_TimerCount], ecx
+ ; Disable APIC Timer
+ mov DWORD [eax+0x320], 0x000100EF
+ mov DWORD [eax+0x380], 0
+
+ ; Update Timer IRQ to the IRQ code
+ mov eax, SchedulerBase
+ sub eax, Isr240.jmp+5
+ mov DWORD [Isr240.jmp+1], eax
+
+ ;xchg bx, bx ; MAGIC BREAK
+.ret:
+ mov dx, 0x20
+ mov al, 0x20
+ out dx, al ; ACK IRQ
+ pop gs
+ pop fs
+ pop es
+ pop ds
+ popa
+ add esp, 4 ; CPU ID
+ ; No Error code / int num
+ iret
+%endif