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]
58 mov DWORD [eax], .restore
63 mov ecx, [esp+0x20+12] ; New IP
64 mov eax, [esp+0x20+20] ; New CR3
65 mov esp, [esp+0x20+ 4] ; New SP
81 [global Proc_InitialiseSSE]
84 or eax, (1 << 9)|(1 << 10) ; Set OSFXSR and OSXMMEXCPT
87 and ax, ~(1 << 2) ; Clear EM
88 or eax, (1 << 1) ; Set MP
91 [global Proc_DisableSSE]
94 or ax, 1 << 3 ; Set TS
97 [global Proc_EnableSSE]
100 and ax, ~(1 << 3) ; Clear TS
104 [global Proc_SaveSSE]
109 [global Proc_RestoreSSE]
116 [extern giMP_TimerCount]
117 [extern gpMP_LocalAPIC]
119 [global SetAPICTimerCount]
133 mov eax, [gpMP_LocalAPIC]
137 mov DWORD [eax+0x380], 0xFFFFFFFF ; Set Initial Count
138 mov DWORD [eax+0x320], 0x000000F0 ; Enable the timer on IVT#0xEF (One Shot)
145 mov DWORD [giMP_TimerCount], ecx
147 mov DWORD [eax+0x320], 0x000100EF
148 mov DWORD [eax+0x380], 0
150 ; Update Timer IRQ to the IRQ code
151 mov eax, SchedulerBase
152 sub eax, Isr240.jmp+5
153 mov DWORD [Isr240.jmp+1], eax
155 ;xchg bx, bx ; MAGIC BREAK
166 ; No Error code / int num
172 [extern Proc_Scheduler]
173 [global SchedulerBase]
182 and BYTE [esp+1], 0xFE ; Clear Trap Flag
186 push eax ; Debug Register 0, Current Thread
197 push eax ; Push as argument
203 [global scheduler_return]
204 scheduler_return: ; Used by some hackery in Proc_DumpThreadCPUState
206 add esp, 4 ; Remove CPU Number (thread is poped later)
214 out 0x20, al ; ACK IRQ
219 mov eax, DWORD [gpMP_LocalAPIC]
220 mov DWORD [eax+0x0B0], 0
223 pop eax ; Debug Register 0, Current Thread
232 add esp, 4*2 ; CPU ID + Dummy error code
233 ; No Error code / int num
237 [extern Threads_Exit]
240 ; Call Proc_Clone with Flags=0
245 add esp, 8 ; Remove arguments from stack
250 ; In child, so now set up stack frame
251 mov ebx, [esp+4] ; Child Function
252 mov edx, [esp+8] ; Argument
256 ; Kill thread once done
258 push 0 ; Kill this thread
259 call Threads_Exit ; Kill Thread
264 ; void Proc_ReturnToUser(void *Method, Uint Parameter)
265 ; Calls a user fault handler
267 [global Proc_ReturnToUser]
268 [extern Proc_GetCurThread]
272 ; [EBP+8]: handler to use
273 ; [EBP+12]: parameter
274 ; [EBP+16]: kernel stack top
276 ;call Proc_GetCurThread
278 ; EAX is the current thread
280 ;mov eax, [ebx+12*4] ; Get Kernel Stack
281 mov eax, [ebp+16] ; Get Kernel Stack
282 sub eax, KSTACK_USERSTATE_SIZE
285 ; NOTE: This can cause corruption if the signal happens while the user
286 ; has called a kernel operation.
287 ; Good thing this can only be called on a user fault.
292 mov edx, [eax+KSTACK_USERSTATE_SIZE-12] ; User ESP is at top of kstack - 3*4
295 test BYTE [0xFC3F0000+ecx*4], 1
300 test BYTE [0xFC000000+ecx*4], 1
307 test BYTE [0xFC3F0000+ecx*4], 1
312 test BYTE [0xFC000000+ecx*4], 1
315 ; Get and alter User SP
317 mov edx, [ebp+12] ; Get parameter
318 mov [edi+4], edx ; save to user stack
319 mov [edi], DWORD User_Syscall_RetAndExit ; Return Address
321 ; Restore Segment Registers
330 push 0x202 ; EFLAGS (IP and Rsvd)
332 mov eax, [ebp+8] ; Method to call
337 ; Just kill the bleeding thing
338 ; (I know it calls int 0xAC in kernel mode, but meh)
346 GetCPUNum: ; TODO: Store in debug registers
350 ; shr ax, 3 ; ax /= 8
355 [global GetEIP_Sched]
356 [global GetEIP_Sched_ret]
357 GetEIP_Sched_ret equ GetEIP_Sched.ret
363 ; Usermode code exported by the kernel
365 ; Export a place for the user to jump to to call a syscall
366 ; - Allows the kernel to change the method easily
368 xchg bx, bx ; MAGIC BREAKPOINT
371 ; A place to return to and exit
372 User_Syscall_RetAndExit:
374 call User_Syscall_Exit