3 * Copyright (C) 2000 David Welch <welch@cwcom.net>
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.
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.
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.
19 /* $Id: syscall.S,v 1.15 2004/07/01 01:52:37 royce Exp $
21 * FILE: ntoskrnl/hal/x86/syscall.s
22 * PURPOSE: 2E trap handler
23 * PROGRAMMER: David Welch (david.welch@seh.ox.ac.uk)
28 #include <ddk/status.h>
29 #include <internal/i386/segment.h>
30 #include <internal/ps.h>
31 #include <internal/i386/ke.h>
34 #define KernelMode (0)
40 .globl KeReturnFromSystemCall
41 .globl KeReturnFromSystemCallWithHook
42 .globl _interrupt_handler2e
45 /* Construct a trap frame on the stack */
54 /* Load PCR selector into fs */
55 movl $PCR_SELECTOR, %ebx
58 /* Save the old exception list */
59 movl %fs:KPCR_EXCEPTION_LIST, %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 */
67 movb %ss:KTHREAD_PREVIOUS_MODE(%esi), %bl
69 /* Set the new previous mode based on the saved CS selector */
71 andl $0x0000FFFF, %ebx
74 movb $KernelMode, %ss:KTHREAD_PREVIOUS_MODE(%esi)
77 movb $UserMode, %ss:KTHREAD_PREVIOUS_MODE(%esi)
80 /* Save other registers */
93 pushl $0 /* XXX: TempESP */
94 pushl $0 /* XXX: TempCS */
95 pushl $0 /* XXX: DebugPointer */
96 pushl $0 /* XXX: DebugArgMark */
98 /* Trick gdb 6 into backtracing over the system call */
100 pushl %ebx /* DebugEIP */
102 pushl %ebx /* DebugEBP */
104 movl 0x60(%esp), %ebx
105 pushl %ebx /* DebugEIP */
106 pushl %ebp /* DebugEBP */
109 /* Load the segment registers */
110 movl $KERNEL_DS, %ebx
116 * Save the old trap frame pointer over where we would save the EDX
119 movl KTHREAD_TRAP_FRAME(%esi), %ebx
120 movl %ebx, KTRAP_FRAME_EDX(%esp)
122 /* Allocate new Kernel stack frame */
125 /* Save a pointer to the trap frame in the TCB */
126 movl %ebp, KTHREAD_TRAP_FRAME(%esi)
128 /* Set ES to kernel segment */
132 /* Users's current stack frame pointer is source */
135 /* Determine system service table to use */
137 ja new_useShadowTable
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
149 /* GDB thinks the function starts here and
150 wants a standard prolog, so let's give it */
156 /* Allocate room for argument list from kernel stack */
157 movl %es:_KeServiceDescriptorTable + 12, %ecx
158 movb %es:(%ecx, %eax), %cl
162 /* Copy the arguments from the user stack to the kernel stack */
167 /* DS is now also kernel segment */
170 /* Call system call hook */
172 call _KiSystemCallHook
175 /* Make the system service call */
176 movl %es:_KeServiceDescriptorTable, %ecx
177 movl %es:(%ecx, %eax, 4), %eax
179 movl %eax, KTRAP_FRAME_EAX(%ebp) /* save our return value in PKTRAP_FRAME->Eax */
182 /* Bump Service Counter */
185 jmp KeDeallocateStackAndReturnFromSystemCallWithHook
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
198 new_shadowServiceInRange:
201 /* GDB thinks the function starts here and
202 wants a standard prolog, so let's give it */
208 /* Allocate room for argument list from kernel stack */
209 movl %es:_KeServiceDescriptorTableShadow + 28, %ecx
210 movb %es:(%ecx, %eax), %cl
214 /* Copy the arguments from the user stack to the kernel stack */
219 /* DS is now also kernel segment */
222 /* Call system call hook */
224 // call _KiSystemCallHook
227 /* Call service check routine */
232 /* Make the system service call */
233 movl %es:_KeServiceDescriptorTableShadow + 16, %ecx
234 movl %es:(%ecx, %eax, 4), %eax
236 movl %eax, KTRAP_FRAME_EAX(%ebp) /* save our return value in PKTRAP_FRAME->Eax */
239 /* Bump Service Counter */
242 KeDeallocateStackAndReturnFromSystemCallWithHook:
243 /* Deallocate the kernel stack frame */
246 KeReturnFromSystemCallWithHook:
247 /* Call the post system call hook and deliver any pending APCs */
249 call _KiAfterSystemCallHook
252 KeReturnFromSystemCall:
254 /* Restore the user context */
255 /* Get a pointer to the current thread */
258 /* Restore the old trap frame pointer */
259 movl KTRAP_FRAME_EDX(%esp), %ebx
260 movl %ebx, KTHREAD_TRAP_FRAME(%esi)
263 /* Skip debug information and unsaved registers */
272 /* Restore the old previous mode */
274 movb %bl, %ss:KTHREAD_PREVIOUS_MODE(%esi)
276 /* Restore the old exception handler list */
278 movl %ebx, %fs:KPCR_EXCEPTION_LIST
285 addl $0x4, %esp /* Ignore error code */
289 /* R3: NOTE: This is part of my in-progress attempt at correcting NtContinue
290 * It is not being called, yet...
292 .globl @KeRosTrapReturn@8
294 /* Call the post system call hook and deliver any pending APCs */
296 call _KiAfterSystemCallHook
299 /* Restore the user context */
300 /* Get a pointer to the current thread */
302 /* Restore the old trap frame pointer */
303 movl %edx, KTHREAD_TRAP_FRAME(%esi)
305 /* point %esp to the trap frame to restore */