X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=Kernel%2Farch%2Fx86%2Fproc.asm;h=4e7d1604ac218217df778d2213a080c5bf7685e9;hb=c5de7b08d4a82908a5acf6454fd7836bdab68715;hp=732d35fce2b3234a757f67287a27b5f336982d15;hpb=2a05bcd81312a2885f824dac79e82c01a6e60c6c;p=tpg%2Facess2.git diff --git a/Kernel/arch/x86/proc.asm b/Kernel/arch/x86/proc.asm index 732d35fc..4e7d1604 100644 --- a/Kernel/arch/x86/proc.asm +++ b/Kernel/arch/x86/proc.asm @@ -3,11 +3,115 @@ [bits 32] +%define SAVEFLAG_FPU 0x1 + KERNEL_BASE equ 0xC0000000 KSTACK_USERSTATE_SIZE equ (4+8+1+5)*4 ; SRegs, GPRegs, CPU, IRET [section .text] + +[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] @@ -74,6 +178,10 @@ SchedulerBase: push fs push gs + pushf + and BYTE [esp+1], 0xFE ; Clear Trap Flag + popf + mov eax, dr0 push eax ; Debug Register 0, Current Thread @@ -92,8 +200,10 @@ SchedulerBase: %endif call Proc_Scheduler +[global scheduler_return] +scheduler_return: ; Used by some hackery in Proc_DumpThreadCPUState - add esp, 4 ; Remove Argument + add esp, 4 ; Remove CPU Number (thread is poped later) %if USE_MP test ebx, ebx @@ -119,7 +229,7 @@ SchedulerBase: pop ds popa - add esp, 4 ; CPU ID + add esp, 4*2 ; CPU ID + Dummy error code ; No Error code / int num iret @@ -129,7 +239,7 @@ SchedulerBase: SpawnTask: ; Call Proc_Clone with Flags=0 xor eax, eax - push eax +; push eax push eax call Proc_Clone add esp, 8 ; Remove arguments from stack @@ -140,9 +250,10 @@ SpawnTask: ; In child, so now set up stack frame mov ebx, [esp+4] ; Child Function mov edx, [esp+8] ; Argument - ; Child + ; Child Function push edx ; Argument call ebx ; Function + ; Kill thread once done push eax ; Exit Code push 0 ; Kill this thread call Threads_Exit ; Kill Thread @@ -158,14 +269,16 @@ SpawnTask: Proc_ReturnToUser: push ebp mov ebp, esp - ; [EBP+4]: handler to use - ; [EBP+8]: parameter + ; [EBP+8]: handler to use + ; [EBP+12]: parameter + ; [EBP+16]: kernel stack top - call Proc_GetCurThread + ;call Proc_GetCurThread ; EAX is the current thread - mov ebx, eax - mov eax, [ebx+12*4] ; Get Kernel Stack + ;mov ebx, eax + ;mov eax, [ebx+12*4] ; Get Kernel Stack + mov eax, [ebp+16] ; Get Kernel Stack sub eax, KSTACK_USERSTATE_SIZE ; @@ -177,14 +290,10 @@ Proc_ReturnToUser: ; Validate user ESP ; - Page Table mov edx, [eax+KSTACK_USERSTATE_SIZE-12] ; User ESP is at top of kstack - 3*4 - %if USE_PAE - %error PAE Support - %else mov ecx, edx shr ecx, 22 test BYTE [0xFC3F0000+ecx*4], 1 jnz .justKillIt - %endif ; - Page mov ecx, edx shr ecx, 12 @@ -193,13 +302,10 @@ Proc_ReturnToUser: ; Adjust sub edx, 8 ; - Page Table - %if USE_PAE - %else mov ecx, edx shr ecx, 22 test BYTE [0xFC3F0000+ecx*4], 1 jnz .justKillIt - %endif ; - Page mov ecx, edx shr ecx, 12 @@ -208,7 +314,7 @@ Proc_ReturnToUser: ; Get and alter User SP mov edi, edx - mov edx, [ebp+8] ; Get parameter + mov edx, [ebp+12] ; Get parameter mov [edi+4], edx ; save to user stack mov [edi], DWORD User_Syscall_RetAndExit ; Return Address @@ -223,7 +329,7 @@ Proc_ReturnToUser: push edi ; ESP push 0x202 ; EFLAGS (IP and Rsvd) push 0x1B ; CS - mov eax, [ebp+4] ; Method to call + mov eax, [ebp+8] ; Method to call push eax ; EIP iret @@ -233,15 +339,25 @@ Proc_ReturnToUser: .justKillIt: xor eax, eax xor ebx, ebx - dec ebx + dec ebx ; EBX = -1 int 0xAC [global GetCPUNum] GetCPUNum: ; TODO: Store in debug registers - xor eax, eax - str ax - sub ax, 0x30 - shr ax, 3 ; ax /= 8 +; xor eax, eax +; str ax +; sub ax, 0x30 +; shr ax, 3 ; ax /= 8 + mov eax, dr1 + ret + +[extern GetEIP] +[global GetEIP_Sched] +[global GetEIP_Sched_ret] +GetEIP_Sched_ret equ GetEIP_Sched.ret +GetEIP_Sched: + call GetEIP +GetEIP_Sched.ret: ret ; Usermode code exported by the kernel