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.
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 <ddk/defines.h>
36 .globl KeReturnFromSystemCall
37 .globl KeReturnFromSystemCallWithHook
38 .globl _interrupt_handler2e
41 /* Construct a trap frame on the stack */
50 /* Load PCR selector into fs */
51 movl $PCR_SELECTOR, %ebx
54 /* Save the old exception list */
55 movl %fs:KPCR_EXCEPTION_LIST, %ebx
57 /* Set the exception handler chain terminator */
58 movl $0xffffffff, %fs:KPCR_EXCEPTION_LIST
59 /* Get a pointer to the current thread */
60 movl %fs:KPCR_CURRENT_THREAD, %esi
61 /* Save the old previous mode */
63 movb %ss:KTHREAD_PREVIOUS_MODE(%esi), %bl
65 /* Set the new previous mode based on the saved CS selector */
67 andl $0x0000FFFF, %ebx
70 movb $KernelMode, %ss:KTHREAD_PREVIOUS_MODE(%esi)
73 movb $UserMode, %ss:KTHREAD_PREVIOUS_MODE(%esi)
76 /* Save other registers */
89 pushl $0 /* XXX: TempESP */
90 pushl $0 /* XXX: TempCS */
91 pushl $0 /* XXX: DebugPointer */
92 pushl $0 /* XXX: DebugArgMark */
94 pushl %ebx /* DebugEIP */
95 pushl %ebp /* DebugEBP */
97 /* Load the segment registers */
104 * Save the old trap frame pointer over where we would save the EDX
107 movl KTHREAD_TRAP_FRAME(%esi), %ebx
108 movl %ebx, 0x3C(%esp)
110 /* Save a pointer to the trap frame in the TCB */
111 movl %esp, KTHREAD_TRAP_FRAME(%esi)
113 /* Set ES to kernel segment */
117 /* Allocate new Kernel stack frame */
120 /* Users's current stack frame pointer is source */
123 /* Determine system service table to use */
125 ja new_useShadowTable
127 /* Check to see if EAX is valid/inrange */
128 cmpl %es:_KeServiceDescriptorTable + 8, %eax
129 jbe new_serviceInRange
130 movl $STATUS_INVALID_SYSTEM_SERVICE, %eax
131 jmp KeReturnFromSystemCall
135 /* Allocate room for argument list from kernel stack */
136 movl %es:_KeServiceDescriptorTable + 12, %ecx
137 movl %es:(%ecx, %eax, 4), %ecx
140 /* Copy the arguments from the user stack to the kernel stack */
144 /* DS is now also kernel segment */
147 /* Call system call hook */
149 call _KiSystemCallHook
152 /* Make the system service call */
153 movl %es:_KeServiceDescriptorTable, %ecx
154 movl %es:(%ecx, %eax, 4), %eax
158 /* Bump Service Counter */
161 /* Deallocate the kernel stack frame */
164 /* Call the post system call hook and deliver any pending APCs */
167 call _KiAfterSystemCallHook
170 jmp KeReturnFromSystemCall
176 /* Check to see if EAX is valid/inrange */
177 cmpl %es:_KeServiceDescriptorTableShadow + 24, %eax
178 jbe new_shadowServiceInRange
179 movl $STATUS_INVALID_SYSTEM_SERVICE, %eax
180 jmp KeReturnFromSystemCall
182 new_shadowServiceInRange:
184 /* Allocate room for argument list from kernel stack */
185 movl %es:_KeServiceDescriptorTableShadow + 28, %ecx
186 movl %es:(%ecx, %eax, 4), %ecx
189 /* Copy the arguments from the user stack to the kernel stack */
193 /* DS is now also kernel segment */
196 /* Call system call hook */
198 call _KiSystemCallHook
201 /* Make the system service call */
202 movl %es:_KeServiceDescriptorTableShadow + 16, %ecx
203 movl %es:(%ecx, %eax, 4), %eax
207 /* Bump Service Counter */
210 /* Deallocate the kernel stack frame */
213 KeReturnFromSystemCallWithHook:
214 /* Call the post system call hook and deliver any pending APCs */
217 call _KiAfterSystemCallHook
220 KeReturnFromSystemCall:
222 /* Restore the user context */
223 /* Get a pointer to the current thread */
226 /* Restore the old trap frame pointer */
227 movl 0x3c(%esp), %ebx
228 movl %ebx, KTHREAD_TRAP_FRAME(%esi)
230 /* Skip debug information and unsaved registers */
237 addl $0x4, %esp /* Don't restore eax */
239 /* Restore the old previous mode */
241 movb %bl, %ss:KTHREAD_PREVIOUS_MODE(%esi)
243 /* Restore the old exception handler list */
245 movl %ebx, %fs:KPCR_EXCEPTION_LIST
252 addl $0x4, %esp /* Ignore error code */