1 ; AcessOS Microkernel Version
6 %define SAVEFLAG_FPU 0x1
8 KERNEL_BASE equ 0xC0000000
10 KSTACK_USERSTATE_SIZE equ (4+8+1+5)*4 ; SRegs, GPRegs, CPU, IRET
14 [global NewTaskHeader]
20 add esp, 12 ; Thread, Function, Arg Count
24 push 0 ; 0 = This Thread
28 [global Proc_CloneInt]
49 ; + 8 = Old RSP save loc
51 ; +16 = Old RIP save loc
57 mov eax, [esp+0x20+16]
60 mov DWORD [eax], .restore
66 mov ecx, [esp+0x20+12] ; New IP
67 mov eax, [esp+0x20+20] ; New CR3
68 mov esp, [esp+0x20+ 4] ; New SP
84 [global Proc_InitialiseSSE]
87 or eax, (1 << 9)|(1 << 10) ; Set OSFXSR and OSXMMEXCPT
90 and ax, ~(1 << 2) ; Clear EM
91 or eax, (1 << 1) ; Set MP
94 [global Proc_DisableSSE]
97 or ax, 1 << 3 ; Set TS
100 [global Proc_EnableSSE]
103 and ax, ~(1 << 3) ; Clear TS
107 [global Proc_SaveSSE]
112 [global Proc_RestoreSSE]
119 [extern giMP_TimerCount]
120 [extern gpMP_LocalAPIC]
122 [global SetAPICTimerCount]
136 mov eax, [gpMP_LocalAPIC]
140 mov DWORD [eax+0x380], 0xFFFFFFFF ; Set Initial Count
141 mov DWORD [eax+0x320], 0x000000F0 ; Enable the timer on IVT#0xEF (One Shot)
148 mov DWORD [giMP_TimerCount], ecx
150 mov DWORD [eax+0x320], 0x000100EF
151 mov DWORD [eax+0x380], 0
153 ; Update Timer IRQ to the IRQ code
154 mov eax, SchedulerBase
155 sub eax, Isr240.jmp+5
156 mov DWORD [Isr240.jmp+1], eax
158 ;xchg bx, bx ; MAGIC BREAK
168 add esp, 8 ; CPU ID / Error Code
174 [extern Proc_Scheduler]
175 [global SchedulerBase]
184 and BYTE [esp+1], 0xFE ; Clear Trap Flag
188 push eax ; Debug Register 0, Current Thread
199 push eax ; Push as argument
205 [global scheduler_return]
206 scheduler_return: ; Used by some hackery in Proc_DumpThreadCPUState
208 add esp, 4 ; Remove CPU Number (thread is poped later)
216 out 0x20, al ; ACK IRQ
221 mov eax, DWORD [gpMP_LocalAPIC]
222 mov DWORD [eax+0x0B0], 0
225 pop eax ; Debug Register 0, Current Thread
234 add esp, 4*2 ; CPU ID + Dummy error code
235 ; No Error code / int num
239 [extern Threads_Exit]
242 ; Call Proc_Clone with Flags=0
247 add esp, 8 ; Remove arguments from stack
252 ; In child, so now set up stack frame
253 mov ebx, [esp+4] ; Child Function
254 mov edx, [esp+8] ; Argument
258 ; Kill thread once done
260 push 0 ; Kill this thread
261 call Threads_Exit ; Kill Thread
266 ; void Proc_ReturnToUser(void *Method, Uint Parameter)
267 ; Calls a user fault handler
269 [global Proc_ReturnToUser]
270 [extern Proc_GetCurThread]
274 ; [EBP+8]: handler to use
275 ; [EBP+12]: parameter
276 ; [EBP+16]: kernel stack top
278 ;call Proc_GetCurThread
280 ; EAX is the current thread
282 ;mov eax, [ebx+12*4] ; Get Kernel Stack
283 mov eax, [ebp+16] ; Get Kernel Stack
284 sub eax, KSTACK_USERSTATE_SIZE
287 ; NOTE: This can cause corruption if the signal happens while the user
288 ; has called a kernel operation.
289 ; Good thing this can only be called on a user fault.
294 mov edx, [eax+KSTACK_USERSTATE_SIZE-12] ; User ESP is at top of kstack - 3*4
297 test BYTE [0xFC3F0000+ecx*4], 1
302 test BYTE [0xFC000000+ecx*4], 1
309 test BYTE [0xFC3F0000+ecx*4], 1
314 test BYTE [0xFC000000+ecx*4], 1
317 ; Get and alter User SP
319 mov edx, [ebp+12] ; Get parameter
320 mov [edi+4], edx ; save to user stack
321 mov [edi], DWORD User_Syscall_RetAndExit ; Return Address
323 ; Restore Segment Registers
332 push 0x202 ; EFLAGS (IP and Rsvd)
334 mov eax, [ebp+8] ; Method to call
339 ; Just kill the bleeding thing
340 ; (I know it calls int 0xAC in kernel mode, but meh)
348 GetCPUNum: ; TODO: Store in debug registers
352 ; shr ax, 3 ; ax /= 8
357 [global GetEIP_Sched]
358 [global GetEIP_Sched_ret]
359 GetEIP_Sched_ret equ GetEIP_Sched.ret
365 ; Usermode code exported by the kernel
367 ; Export a place for the user to jump to to call a syscall
368 ; - Allows the kernel to change the method easily
370 xchg bx, bx ; MAGIC BREAKPOINT
373 ; A place to return to and exit
374 User_Syscall_RetAndExit:
376 call User_Syscall_Exit