db7a885abe7a064f0f10897071e25e1574d14708
[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.15 2004/07/01 01:52:37 royce Exp $
20 *
21 * FILE: ntoskrnl/hal/x86/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 #ifdef DBG
98 /* Trick gdb 6 into backtracing over the system call */
99 movl 4(%ebp), %ebx
100 pushl %ebx /* DebugEIP */
101 movl (%ebp), %ebx
102 pushl %ebx /* DebugEBP */
103 #else
104 movl 0x60(%esp), %ebx
105 pushl %ebx /* DebugEIP */
106 pushl %ebp /* DebugEBP */
107 #endif
108
109 /* Load the segment registers */
110 movl $KERNEL_DS, %ebx
111 movl %ebx, %ds
112 movl %ebx, %es
113 movl %ebx, %gs
114
115 /*
116 * Save the old trap frame pointer over where we would save the EDX
117 * register.
118 */
119 movl KTHREAD_TRAP_FRAME(%esi), %ebx
120 movl %ebx, KTRAP_FRAME_EDX(%esp)
121
122 /* Allocate new Kernel stack frame */
123 movl %esp,%ebp
124
125 /* Save a pointer to the trap frame in the TCB */
126 movl %ebp, KTHREAD_TRAP_FRAME(%esi)
127
128 /* Set ES to kernel segment */
129 movw $KERNEL_DS,%bx
130 movw %bx,%es
131
132 /* Users's current stack frame pointer is source */
133 movl %edx,%esi
134
135 /* Determine system service table to use */
136 cmpl $0x0fff, %eax
137 ja new_useShadowTable
138
139 /* Check to see if EAX is valid/inrange */
140 cmpl %es:_KeServiceDescriptorTable + 8, %eax
141 jbe new_serviceInRange
142 movl $STATUS_INVALID_SYSTEM_SERVICE, %eax
143 movl %eax, KTRAP_FRAME_EAX(%ebp) /* save our return value in PKTRAP_FRAME->Eax */
144 jmp KeReturnFromSystemCall
145
146 new_serviceInRange:
147
148 #ifdef DBG
149 /* GDB thinks the function starts here and
150 wants a standard prolog, so let's give it */
151 pushl %ebp
152 movl %esp,%ebp
153 popl %ebp
154 #endif
155
156 /* Allocate room for argument list from kernel stack */
157 movl %es:_KeServiceDescriptorTable + 12, %ecx
158 movb %es:(%ecx, %eax), %cl
159 movzx %cl, %ecx
160 subl %ecx, %esp
161
162 /* Copy the arguments from the user stack to the kernel stack */
163 movl %esp,%edi
164 cld
165 rep movsb
166
167 /* DS is now also kernel segment */
168 movw %bx, %ds
169
170 /* Call system call hook */
171 pushl %eax
172 call _KiSystemCallHook
173 popl %eax
174
175 /* Make the system service call */
176 movl %es:_KeServiceDescriptorTable, %ecx
177 movl %es:(%ecx, %eax, 4), %eax
178 call *%eax
179 movl %eax, KTRAP_FRAME_EAX(%ebp) /* save our return value in PKTRAP_FRAME->Eax */
180
181 #if CHECKED
182 /* Bump Service Counter */
183 #endif
184
185 jmp KeDeallocateStackAndReturnFromSystemCallWithHook
186
187 new_useShadowTable:
188
189 subl $0x1000, %eax
190
191 /* Check to see if EAX is valid/inrange */
192 cmpl %es:_KeServiceDescriptorTableShadow + 24, %eax
193 jbe new_shadowServiceInRange
194 movl $STATUS_INVALID_SYSTEM_SERVICE, %eax
195 movl %eax, KTRAP_FRAME_EAX(%ebp) /* save our return value in PKTRAP_FRAME->Eax */
196 jmp KeReturnFromSystemCall
197
198 new_shadowServiceInRange:
199
200 #ifdef DBG
201 /* GDB thinks the function starts here and
202 wants a standard prolog, so let's give it */
203 pushl %ebp
204 movl %esp,%ebp
205 popl %ebp
206 #endif
207
208 /* Allocate room for argument list from kernel stack */
209 movl %es:_KeServiceDescriptorTableShadow + 28, %ecx
210 movb %es:(%ecx, %eax), %cl
211 movzx %cl, %ecx
212 subl %ecx, %esp
213
214 /* Copy the arguments from the user stack to the kernel stack */
215 movl %esp,%edi
216 cld
217 rep movsb
218
219 /* DS is now also kernel segment */
220 movw %bx,%ds
221
222 /* Call system call hook */
223 // pushl %eax
224 // call _KiSystemCallHook
225 // popl %eax
226
227 /* Call service check routine */
228 pushl %eax
229 call _KiServiceCheck
230 popl %eax
231
232 /* Make the system service call */
233 movl %es:_KeServiceDescriptorTableShadow + 16, %ecx
234 movl %es:(%ecx, %eax, 4), %eax
235 call *%eax
236 movl %eax, KTRAP_FRAME_EAX(%ebp) /* save our return value in PKTRAP_FRAME->Eax */
237
238 #if CHECKED
239 /* Bump Service Counter */
240 #endif
241
242 KeDeallocateStackAndReturnFromSystemCallWithHook:
243 /* Deallocate the kernel stack frame */
244 movl %ebp,%esp
245
246 KeReturnFromSystemCallWithHook:
247 /* Call the post system call hook and deliver any pending APCs */
248 pushl %esp
249 call _KiAfterSystemCallHook
250 addl $4,%esp
251
252 KeReturnFromSystemCall:
253
254 /* Restore the user context */
255 /* Get a pointer to the current thread */
256 movl %fs:0x124, %esi
257
258 /* Restore the old trap frame pointer */
259 movl KTRAP_FRAME_EDX(%esp), %ebx
260 movl %ebx, KTHREAD_TRAP_FRAME(%esi)
261
262 KiRosTrapReturn:
263 /* Skip debug information and unsaved registers */
264 addl $0x30, %esp
265 popl %gs
266 popl %es
267 popl %ds
268 popl %edx
269 popl %ecx
270 popl %eax
271
272 /* Restore the old previous mode */
273 popl %ebx
274 movb %bl, %ss:KTHREAD_PREVIOUS_MODE(%esi)
275
276 /* Restore the old exception handler list */
277 popl %ebx
278 movl %ebx, %fs:KPCR_EXCEPTION_LIST
279
280 popl %fs
281 popl %edi
282 popl %esi
283 popl %ebx
284 popl %ebp
285 addl $0x4, %esp /* Ignore error code */
286
287 iret
288
289 /* R3: NOTE: This is part of my in-progress attempt at correcting NtContinue
290 * It is not being called, yet...
291 */
292 .globl @KeRosTrapReturn@8
293 @KeRosTrapReturn@8:
294 /* Call the post system call hook and deliver any pending APCs */
295 pushl %esp
296 call _KiAfterSystemCallHook
297 addl $4,%esp
298
299 /* Restore the user context */
300 /* Get a pointer to the current thread */
301 movl %fs:0x124, %esi
302 /* Restore the old trap frame pointer */
303 movl %edx, KTHREAD_TRAP_FRAME(%esi)
304
305 /* point %esp to the trap frame to restore */
306 movl %ecx, %esp
307 jmp KiRosTrapReturn;