b0a1ef05b205065e02b693f4d1ea7c290708f2b6
[tpg/acess2.git] / Kernel / arch / x86 / proc.asm
1 ; AcessOS Microkernel Version
2 ; Start.asm
3
4 [bits 32]
5
6 KERNEL_BASE     equ 0xC0000000
7
8 KSTACK_USERSTATE_SIZE   equ     (4+8+1+5)*4     ; SRegs, GPRegs, CPU, IRET
9
10 [section .text]
11 ; --------------
12 ; Task Scheduler
13 ; --------------
14 [extern Proc_Scheduler]
15 [global SchedulerBase]
16 SchedulerBase:
17         pusha
18         push ds
19         push es
20         push fs
21         push gs
22         
23         mov ax, 0x10
24         mov ds, ax
25         mov es, ax
26         mov fs, ax
27         mov gs, ax
28         
29         call GetCPUNum
30         push eax        ; Push as argument
31         
32         call Proc_Scheduler
33         
34         add esp, 4      ; Remove Argument
35         
36         pop gs
37         pop fs
38         pop es
39         pop ds
40
41         mov dx, 0x20
42         mov al, 0x20
43         out dx, al              ; ACK IRQ
44         popa
45         add esp, 4      ; CPU ID
46         ; No Error code / int num
47         iret
48
49 [extern Proc_Clone]
50 [extern Threads_Exit]
51 [global SpawnTask]
52 SpawnTask:
53         ; Call Proc_Clone with Flags=0
54         xor eax, eax
55         push eax
56         push eax
57         call Proc_Clone
58         add esp, 8      ; Remove arguments from stack
59         
60         test eax, eax
61         jnz .parent
62         
63         ; In child, so now set up stack frame
64         mov ebx, [esp+4]        ; Child Function
65         mov edx, [esp+8]        ; Argument
66         ; Child
67         push edx        ; Argument
68         call ebx        ; Function
69         call Threads_Exit       ; Kill Thread
70         
71 .parent:
72         ret
73
74 ;
75 ; Calls a user fault handler
76 ;
77 [global Proc_ReturnToUser]
78 [extern Proc_GetCurThread]
79 Proc_ReturnToUser:
80         ; EBP is the handler to use
81         
82         call Proc_GetCurThread
83         
84         ; EAX is the current thread
85         mov ebx, eax
86         mov eax, [ebx+40]       ; Get Kernel Stack
87         sub eax, KSTACK_USERSTATE_SIZE
88         
89         ;
90         ; NOTE: This can cause corruption if the signal happens while the user
91         ;       has called a kernel operation.
92         ; Good thing this can only be called on a user fault.
93         ;
94         
95         ; Validate user ESP
96         ; - Page Table
97         mov edx, [eax+KSTACK_USERSTATE_SIZE-12] ; User ESP is at top of kstack - 3*4
98         %if USE_PAE
99         %error PAE Support
100         %else
101         mov ecx, edx
102         shr ecx, 22
103         test BYTE [0xFC3F0000+ecx*4], 1
104         jnz .justKillIt
105         %endif
106         ; - Page
107         mov ecx, edx
108         shr ecx, 12
109         test BYTE [0xFC000000+ecx*4], 1
110         jnz .justKillIt
111         ; Adjust
112         sub edx, 8
113         ; - Page Table
114         %if USE_PAE
115         %else
116         mov ecx, edx
117         shr ecx, 22
118         test BYTE [0xFC3F0000+ecx*4], 1
119         jnz .justKillIt
120         %endif
121         ; - Page
122         mov ecx, edx
123         shr ecx, 12
124         test BYTE [0xFC000000+ecx*4], 1
125         jnz .justKillIt
126         
127         ; Get and alter User SP
128         mov ecx, edx
129         mov edx, [ebx+60]       ; Get Signal Number from TCB
130         mov [ecx+4], edx        ; Parameter (Signal/Error Number)
131         mov [ecx], DWORD User_Syscall_RetAndExit        ; Return Address
132         
133         ; Restore Segment Registers
134         mov ax, 0x23
135         mov ds, ax
136         mov es, ax
137         mov fs, ax
138         mov gs, ax
139         
140         push 0x23       ; SS
141         push ecx        ; ESP
142         push 0x202      ; EFLAGS (IP and Rsvd)
143         push 0x1B       ; CS
144         push ebp        ; EIP
145         
146         iret
147         
148         ; Just kill the bleeding thing
149         ; (I know it calls int 0xAC in kernel mode, but meh)
150 .justKillIt:
151         xor eax, eax
152         xor ebx, ebx
153         dec ebx
154         int 0xAC
155
156 [global GetCPUNum]
157 GetCPUNum:
158         xor eax, eax
159         str ax
160         sub ax, 0x30
161         shr ax, 3       ; ax /= 8
162         ret
163
164 ; Usermode code exported by the kernel
165 [section .usertext]
166 User_Syscall_RetAndExit:
167         push eax
168         call User_Syscall_Exit
169 User_Syscall_Exit:
170         xor eax, eax
171         mov ebx, [esp+4]
172         int 0xAC

UCC git Repository :: git.ucc.asn.au