3 * Copyright (C) 2000 David Welch <welch@cwcom.net>
5 * Moved to MSVC-compatible inline assembler by Mike Nordell, 2003-12-25
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 * FILE: ntoskrnl/ke/i386/vm86_sup.S
23 * PURPOSE: V86 mode support
24 * PROGRAMMER: David Welch (welch@cwcom.net)
29 /* INCLUDES ******************************************************************/
33 #include <ddk/ntddk.h>
34 #include <ddk/status.h>
35 #include <internal/i386/segment.h>
36 #include <internal/i386/fpu.h>
37 #include <internal/ps.h>
38 #include <ddk/defines.h>
39 #include <internal/v86m.h>
41 #include <internal/trap.h>
42 #include <internal/ps.h>
45 #include <internal/ntoskrnl.h>
46 #include <internal/i386/segment.h>
47 #include <internal/ps.h>
50 // Taken from ntoskrnl/include/internal/v86m.h, since that one must be fixed
51 // a bit before it could be used from here.
52 #define KV86M_REGISTERS_EBP (0x0)
53 #define KV86M_REGISTERS_EDI (0x4)
54 #define KV86M_REGISTERS_ESI (0x8)
55 #define KV86M_REGISTERS_EDX (0xC)
56 #define KV86M_REGISTERS_ECX (0x10)
57 #define KV86M_REGISTERS_EBX (0x14)
58 #define KV86M_REGISTERS_EAX (0x18)
59 #define KV86M_REGISTERS_DS (0x1C)
60 #define KV86M_REGISTERS_ES (0x20)
61 #define KV86M_REGISTERS_FS (0x24)
62 #define KV86M_REGISTERS_GS (0x28)
63 #define KV86M_REGISTERS_EIP (0x2C)
64 #define KV86M_REGISTERS_CS (0x30)
65 #define KV86M_REGISTERS_EFLAGS (0x34)
66 #define KV86M_REGISTERS_ESP (0x38)
67 #define KV86M_REGISTERS_SS (0x3C)
74 * Starts in v86 mode with the registers set to the
79 VOID
Ki386RetToV86Mode(KV86M_REGISTERS
* InRegs
,
80 KV86M_REGISTERS
* OutRegs
)
90 pushad
/* Save registers */
100 * Save a pointer to IN_REGS which the v86m exception handler will
101 * use to handle exceptions
106 * Since we are going to fiddle with the stack pointer this must be
107 * a critical section for this processor
111 * Save the old initial stack
113 mov esi
, fs
:KPCR_CURRENT_THREAD
114 mov edi
, KTHREAD_INITIAL_STACK
[esi
]
118 * We also need to set the stack in the kthread structure
120 mov KTHREAD_INITIAL_STACK
[esi
], esp
123 * The stack used for handling exceptions from v86 mode in this thread
124 * will be the current stack adjusted so we don't overwrite the
125 * existing stack frames
128 mov KTSS_ESP0
[esi
], esp
131 * Create the stack frame for an iret to v86 mode
133 push KV86M_REGISTERS_GS
[ebx
]
134 push KV86M_REGISTERS_FS
[ebx
]
135 push KV86M_REGISTERS_DS
[ebx
]
136 push KV86M_REGISTERS_ES
[ebx
]
137 push KV86M_REGISTERS_SS
[ebx
]
138 push KV86M_REGISTERS_ESP
[ebx
]
139 push KV86M_REGISTERS_EFLAGS
[ebx
]
140 push KV86M_REGISTERS_CS
[ebx
]
141 push KV86M_REGISTERS_EIP
[ebx
]
144 * Setup the CPU registers
146 mov eax
, KV86M_REGISTERS_EAX
[ebx
]
147 mov ecx
, KV86M_REGISTERS_ECX
[ebx
]
148 mov edx
, KV86M_REGISTERS_EDX
[ebx
]
149 mov esi
, KV86M_REGISTERS_ESI
[ebx
]
150 mov edi
, KV86M_REGISTERS_EDI
[ebx
]
151 mov ebp
, KV86M_REGISTERS_EBP
[ebx
]
152 mov ebx
, KV86M_REGISTERS_EBX
[ebx
]
160 * Handle the completion of a vm86 routine. We are called from
161 * an exception handler with the registers at the point of the
162 * exception on the stack.
165 jmp KiV86Complete
// TMN: Function-splitting
174 /* Restore the original ebp */
175 mov ebp
, TF_ORIG_EBP
[esp
]
177 /* Get a pointer to the OUT_REGS structure */
178 mov ebx
, 12[ebp
] // OutRegs
180 /* Skip debug information and unsaved registers */
183 /* Ignore 32-bit segment registers */
186 /* Save the vm86 registers into the OUT_REGS structure */
187 pop dword ptr KV86M_REGISTERS_EDX
[ebx
]
188 pop dword ptr KV86M_REGISTERS_ECX
[ebx
]
189 pop dword ptr KV86M_REGISTERS_EAX
[ebx
]
191 /* Restore the old previous mode */
193 mov ss
:KTHREAD_PREVIOUS_MODE
[esi
], al
195 /* Restore the old exception handler list */
197 mov fs
:KPCR_EXCEPTION_LIST
, eax
199 /* Ignore the 32-bit fs register */
202 pop dword ptr KV86M_REGISTERS_EDI
[ebx
]
203 pop dword ptr KV86M_REGISTERS_ESI
[ebx
]
204 pop dword ptr KV86M_REGISTERS_EBX
[ebx
]
205 pop dword ptr KV86M_REGISTERS_EBP
[ebx
]
207 /* Ignore error code */
210 pop dword ptr KV86M_REGISTERS_EIP
[ebx
]
211 pop dword ptr KV86M_REGISTERS_CS
[ebx
]
212 pop dword ptr KV86M_REGISTERS_EFLAGS
[ebx
]
213 pop dword ptr KV86M_REGISTERS_ESP
[ebx
]
214 pop dword ptr KV86M_REGISTERS_SS
[ebx
]
215 pop dword ptr KV86M_REGISTERS_ES
[ebx
]
216 pop dword ptr KV86M_REGISTERS_DS
[ebx
]
217 pop dword ptr KV86M_REGISTERS_FS
[ebx
]
218 pop dword ptr KV86M_REGISTERS_GS
[ebx
]
221 * We are going to fiddle with the stack so this must be a critical
222 * section for this process
227 * Restore the initial stack
231 mov KTSS_ESP0
[esi
], eax
234 * We also need to set the stack in the kthread structure
236 mov esi
, fs
:KPCR_CURRENT_THREAD
237 mov edi
, KTHREAD_INITIAL_STACK
[esi
]
238 mov KTHREAD_INITIAL_STACK
[esi
], eax
240 /* Exit the critical section */
243 /* Ignore IN_REGS pointer */
246 /* Ignore ebp restored above */
249 /* Return to caller */
255 } // end of __asm block