9d634cf7b605f78e207199d8a079c384814f84e2
[reactos.git] / reactos / ntoskrnl / ke / i386 / syscall.S
1 /*
2 * ReactOS kernel
3 * Copyright (C) 2000 David Welch <welch@cwcom.net>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19 /* $Id: syscall.S,v 1.17 2004/08/21 21:13:45 tamlin Exp $
20 *
21 * FILE: ntoskrnl/ke/i386/syscall.S
22 * PURPOSE: 2E trap handler
23 * PROGRAMMER: David Welch (david.welch@seh.ox.ac.uk)
24 * UPDATE HISTORY:
25 * ???
26 */
27
28 #include <ddk/status.h>
29 #include <internal/i386/segment.h>
30 #include <internal/ps.h>
31 #include <internal/i386/ke.h>
32 #include <roscfg.h>
33
34 #define KernelMode (0)
35 #define UserMode (1)
36
37 /*
38 *
39 */
40 .globl KeReturnFromSystemCall
41 .globl KeReturnFromSystemCallWithHook
42 .globl _interrupt_handler2e
43 _interrupt_handler2e:
44
45 /* Construct a trap frame on the stack */
46
47 /* Error code */
48 pushl $0
49 pushl %ebp
50 pushl %ebx
51 pushl %esi
52 pushl %edi
53 pushl %fs
54 /* Load PCR selector into fs */
55 movl $PCR_SELECTOR, %ebx
56 movl %ebx, %fs
57
58 /* Save the old exception list */
59 movl %fs:KPCR_EXCEPTION_LIST, %ebx
60 pushl %ebx
61 /* Set the exception handler chain terminator */
62 movl $0xffffffff, %fs:KPCR_EXCEPTION_LIST
63 /* Get a pointer to the current thread */
64 movl %fs:KPCR_CURRENT_THREAD, %esi
65 /* Save the old previous mode */
66 movl $0, %ebx
67 movb %ss:KTHREAD_PREVIOUS_MODE(%esi), %bl
68 pushl %ebx
69 /* Set the new previous mode based on the saved CS selector */
70 movl 0x24(%esp), %ebx
71 andl $0x0000FFFF, %ebx
72 cmpl $KERNEL_CS, %ebx
73 jne L1
74 movb $KernelMode, %ss:KTHREAD_PREVIOUS_MODE(%esi)
75 jmp L3
76 L1:
77 movb $UserMode, %ss:KTHREAD_PREVIOUS_MODE(%esi)
78 L3:
79
80 /* Save other registers */
81 pushl %eax
82 pushl %ecx
83 pushl %edx
84 pushl %ds
85 pushl %es
86 pushl %gs
87 pushl $0 /* DR7 */
88 pushl $0 /* DR6 */
89 pushl $0 /* DR3 */
90 pushl $0 /* DR2 */
91 pushl $0 /* DR1 */
92 pushl $0 /* DR0 */
93 pushl $0 /* XXX: TempESP */
94 pushl $0 /* XXX: TempCS */
95 pushl $0 /* XXX: DebugPointer */
96 pushl $0 /* XXX: DebugArgMark */
97
98 #ifdef DBG
99 /* Trick gdb 6 into backtracing over the system call */
100 movl 4(%ebp), %ebx
101 pushl %ebx /* DebugEIP */
102 movl (%ebp), %ebx
103 pushl %ebx /* DebugEBP */
104 #else
105 movl 0x60(%esp), %ebx
106 pushl %ebx /* DebugEIP */
107 pushl %ebp /* DebugEBP */
108 #endif
109
110 /* Load the segment registers */
111 movl $KERNEL_DS, %ebx
112 movl %ebx, %ds
113 movl %ebx, %es
114 movl %ebx, %gs
115
116 /*
117 * Save the old trap frame pointer over where we would save the EDX
118 * register.
119 */
120 movl KTHREAD_TRAP_FRAME(%esi), %ebx
121 movl %ebx, KTRAP_FRAME_EDX(%esp)
122
123 /* Allocate new Kernel stack frame */
124 movl %esp,%ebp
125
126 /* Save a pointer to the trap frame in the TCB */
127 movl %ebp, KTHREAD_TRAP_FRAME(%esi)
128
129 /* Set ES to kernel segment */
130 movw $KERNEL_DS,%bx
131 movw %bx,%es
132
133 /* Users's current stack frame pointer is source */
134 movl %edx,%esi
135
136 /* Determine system service table to use */
137 cmpl $0x0fff, %eax
138 ja new_useShadowTable
139
140 /* Check to see if EAX is valid/inrange */
141 cmpl %es:_KeServiceDescriptorTable + 8, %eax
142 jbe new_serviceInRange
143 movl $STATUS_INVALID_SYSTEM_SERVICE, %eax
144 movl %eax, KTRAP_FRAME_EAX(%ebp) /* save our return value in PKTRAP_FRAME->Eax */
145 jmp KeReturnFromSystemCall
146
147 new_serviceInRange:
148
149 #ifdef DBG
150 /* GDB thinks the function starts here and
151 wants a standard prolog, so let's give it */
152 pushl %ebp
153 movl %esp,%ebp
154 popl %ebp
155 #endif
156
157 /* Allocate room for argument list from kernel stack */
158 movl %es:_KeServiceDescriptorTable + 12, %ecx
159 movb %es:(%ecx, %eax), %cl
160 movzx %cl, %ecx
161 subl %ecx, %esp
162
163 /* Copy the arguments from the user stack to the kernel stack */
164 movl %esp,%edi
165 cld
166 rep movsb
167
168 /* DS is now also kernel segment */
169 movw %bx, %ds
170
171 /* Call system call hook */
172 pushl %eax
173 call _KiSystemCallHook
174 popl %eax
175
176 /* Make the system service call */
177 movl %es:_KeServiceDescriptorTable, %ecx
178 movl %es:(%ecx, %eax, 4), %eax
179 call *%eax
180 movl %eax, KTRAP_FRAME_EAX(%ebp) /* save our return value in PKTRAP_FRAME->Eax */
181
182 #if CHECKED
183 /* Bump Service Counter */
184 #endif
185
186 jmp KeDeallocateStackAndReturnFromSystemCallWithHook
187
188 new_useShadowTable:
189
190 subl $0x1000, %eax
191
192 /* Check to see if EAX is valid/inrange */
193 cmpl %es:_KeServiceDescriptorTableShadow + 24, %eax
194 jbe new_shadowServiceInRange
195 movl $STATUS_INVALID_SYSTEM_SERVICE, %eax
196 movl %eax, KTRAP_FRAME_EAX(%ebp) /* save our return value in PKTRAP_FRAME->Eax */
197 jmp KeReturnFromSystemCall
198
199 new_shadowServiceInRange:
200
201 #ifdef DBG
202 /* GDB thinks the function starts here and
203 wants a standard prolog, so let's give it */
204 pushl %ebp
205 movl %esp,%ebp
206 popl %ebp
207 #endif
208
209 /* Allocate room for argument list from kernel stack */
210 movl %es:_KeServiceDescriptorTableShadow + 28, %ecx
211 movb %es:(%ecx, %eax), %cl
212 movzx %cl, %ecx
213 subl %ecx, %esp
214
215 /* Copy the arguments from the user stack to the kernel stack */
216 movl %esp,%edi
217 cld
218 rep movsb
219
220 /* DS is now also kernel segment */
221 movw %bx,%ds
222
223 /* Call system call hook */
224 // pushl %eax
225 // call _KiSystemCallHook
226 // popl %eax
227
228 /* Call service check routine */
229 pushl %eax
230 call _KiServiceCheck
231 popl %eax
232
233 /* Make the system service call */
234 movl %es:_KeServiceDescriptorTableShadow + 16, %ecx
235 movl %es:(%ecx, %eax, 4), %eax
236 call *%eax
237 movl %eax, KTRAP_FRAME_EAX(%ebp) /* save our return value in PKTRAP_FRAME->Eax */
238
239 #if CHECKED
240 /* Bump Service Counter */
241 #endif
242
243 KeDeallocateStackAndReturnFromSystemCallWithHook:
244 /* Deallocate the kernel stack frame */
245 movl %ebp,%esp
246
247 KeReturnFromSystemCallWithHook:
248 /* Call the post system call hook and deliver any pending APCs */
249 pushl %esp
250 call _KiAfterSystemCallHook
251 addl $4,%esp
252
253 KeReturnFromSystemCall:
254
255 /* Restore the user context */
256 /* Get a pointer to the current thread */
257 movl %fs:0x124, %esi
258
259 /* Restore the old trap frame pointer */
260 movl KTRAP_FRAME_EDX(%esp), %ebx
261 movl %ebx, KTHREAD_TRAP_FRAME(%esi)
262
263 KiRosTrapReturn:
264
265 #if 0
266 mov KTRAP_FRAME_RESERVED1(%ebp), %ax
267 cmp %ax, SSIDX_NTCONTINUE
268 jnz KeNoEpilogPrint
269 movl KTRAP_FRAME_ESP(%ebp), %ecx
270 movl KTRAP_FRAME_EBP(%ebp), %edx
271 call @KeRosPrintEspEbp@8
272 KeNoEpilogPrint:
273 #endif
274
275 /* Skip debug information and unsaved registers */
276 addl $0x30, %esp
277 popl %gs
278 popl %es
279 popl %ds
280 popl %edx
281 popl %ecx
282 popl %eax
283
284 /* Restore the old previous mode */
285 popl %ebx
286 movb %bl, %ss:KTHREAD_PREVIOUS_MODE(%esi)
287
288 /* Restore the old exception handler list */
289 popl %ebx
290 movl %ebx, %fs:KPCR_EXCEPTION_LIST
291
292 popl %fs
293 popl %edi
294 popl %esi
295 popl %ebx
296 popl %ebp
297 addl $0x4, %esp /* Ignore error code */
298
299 iret
300
301 /* R3: NOTE: This is part of my in-progress attempt at correcting NtContinue
302 * It is not being called, yet...
303 */
304 .globl @KeRosTrapReturn@8
305 @KeRosTrapReturn@8:
306
307 /* point %esp to the trap frame to restore */
308 movl %ecx, %esp
309 movl %esp, %ebp
310
311 /* Call the post system call hook and deliver any pending APCs */
312 pushl %esp
313 call _KiAfterSystemCallHook
314 addl $4,%esp
315
316 /* Restore the user context */
317 /* Get a pointer to the current thread */
318 movl %fs:0x124, %esi
319 /* Restore the old trap frame pointer */
320 movl %edx, KTHREAD_TRAP_FRAME(%esi)
321
322 jmp KiRosTrapReturn;