1 | ; $Id: PATMA.asm 37955 2011-07-14 12:23:02Z vboxsync $
|
---|
2 | ;; @file
|
---|
3 | ; PATM Assembly Routines.
|
---|
4 | ;
|
---|
5 |
|
---|
6 | ; Copyright (C) 2006-2007 Oracle Corporation
|
---|
7 | ;
|
---|
8 | ; This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
9 | ; available from http://www.virtualbox.org. This file is free software;
|
---|
10 | ; you can redistribute it and/or modify it under the terms of the GNU
|
---|
11 | ; General Public License (GPL) as published by the Free Software
|
---|
12 | ; Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
13 | ; VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
14 | ; hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
15 | ;
|
---|
16 |
|
---|
17 | ;;
|
---|
18 | ; @note This method has problems in theory. If we fault for any reason, then we won't be able to restore
|
---|
19 | ; the guest's context properly!!
|
---|
20 | ; E.g if one of the push instructions causes a fault or SS isn't wide open and our patch GC state accesses aren't valid.
|
---|
21 | ; @assumptions
|
---|
22 | ; - Enough stack for a few pushes
|
---|
23 | ; - The SS selector has base 0 and limit 0xffffffff
|
---|
24 | ;
|
---|
25 | ; @todo stack probing is currently hardcoded and not present everywhere (search for 'probe stack')
|
---|
26 |
|
---|
27 |
|
---|
28 | ;*******************************************************************************
|
---|
29 | ;* Header Files *
|
---|
30 | ;*******************************************************************************
|
---|
31 | %include "VBox/asmdefs.mac"
|
---|
32 | %include "VBox/err.mac"
|
---|
33 | %include "iprt/x86.mac"
|
---|
34 | %include "VBox/vmm/vm.mac"
|
---|
35 | %include "PATMA.mac"
|
---|
36 |
|
---|
37 | %ifdef DEBUG
|
---|
38 | ; Noisy, but useful for debugging certain problems
|
---|
39 | ;;;%define PATM_LOG_PATCHINSTR
|
---|
40 | ;;%define PATM_LOG_PATCHIRET
|
---|
41 | %endif
|
---|
42 |
|
---|
43 | BEGINCONST
|
---|
44 |
|
---|
45 | %ifdef RT_ARCH_AMD64
|
---|
46 | BITS 32 ; switch to 32-bit mode (x86).
|
---|
47 | %endif
|
---|
48 |
|
---|
49 | %ifdef VBOX_WITH_STATISTICS
|
---|
50 | ;
|
---|
51 | ; Patch call statistics
|
---|
52 | ;
|
---|
53 | BEGINPROC PATMStats
|
---|
54 | PATMStats_Start:
|
---|
55 | mov dword [ss:PATM_INTERRUPTFLAG], 0
|
---|
56 | pushf
|
---|
57 | inc dword [ss:PATM_ALLPATCHCALLS]
|
---|
58 | inc dword [ss:PATM_PERPATCHCALLS]
|
---|
59 | popf
|
---|
60 | mov dword [ss:PATM_INTERRUPTFLAG], 1
|
---|
61 | PATMStats_End:
|
---|
62 | ENDPROC PATMStats
|
---|
63 |
|
---|
64 |
|
---|
65 | ; Patch record for statistics
|
---|
66 | GLOBALNAME PATMStatsRecord
|
---|
67 | RTCCPTR_DEF PATMStats_Start
|
---|
68 | DD 0
|
---|
69 | DD 0
|
---|
70 | DD 0
|
---|
71 | DD PATMStats_End - PATMStats_Start
|
---|
72 | DD 4
|
---|
73 | DD PATM_INTERRUPTFLAG
|
---|
74 | DD 0
|
---|
75 | DD PATM_ALLPATCHCALLS
|
---|
76 | DD 0
|
---|
77 | DD PATM_PERPATCHCALLS
|
---|
78 | DD 0
|
---|
79 | DD PATM_INTERRUPTFLAG
|
---|
80 | DD 0
|
---|
81 | DD 0ffffffffh
|
---|
82 | %endif
|
---|
83 |
|
---|
84 | ;
|
---|
85 | ; Set PATM_INTERRUPTFLAG
|
---|
86 | ;
|
---|
87 | BEGINPROC PATMSetPIF
|
---|
88 | PATMSetPIF_Start:
|
---|
89 | mov dword [ss:PATM_INTERRUPTFLAG], 1
|
---|
90 | PATMSetPIF_End:
|
---|
91 | ENDPROC PATMSetPIF
|
---|
92 |
|
---|
93 |
|
---|
94 | ; Patch record for setting PATM_INTERRUPTFLAG
|
---|
95 | GLOBALNAME PATMSetPIFRecord
|
---|
96 | RTCCPTR_DEF PATMSetPIF_Start
|
---|
97 | DD 0
|
---|
98 | DD 0
|
---|
99 | DD 0
|
---|
100 | DD PATMSetPIF_End - PATMSetPIF_Start
|
---|
101 | DD 1
|
---|
102 | DD PATM_INTERRUPTFLAG
|
---|
103 | DD 0
|
---|
104 | DD 0ffffffffh
|
---|
105 |
|
---|
106 | ;
|
---|
107 | ; Clear PATM_INTERRUPTFLAG
|
---|
108 | ;
|
---|
109 | BEGINPROC PATMClearPIF
|
---|
110 | PATMClearPIF_Start:
|
---|
111 | ; probe stack here as we can't recover from page faults later on
|
---|
112 | not dword [esp-64]
|
---|
113 | not dword [esp-64]
|
---|
114 | mov dword [ss:PATM_INTERRUPTFLAG], 0
|
---|
115 | PATMClearPIF_End:
|
---|
116 | ENDPROC PATMClearPIF
|
---|
117 |
|
---|
118 |
|
---|
119 | ; Patch record for clearing PATM_INTERRUPTFLAG
|
---|
120 | GLOBALNAME PATMClearPIFRecord
|
---|
121 | RTCCPTR_DEF PATMClearPIF_Start
|
---|
122 | DD 0
|
---|
123 | DD 0
|
---|
124 | DD 0
|
---|
125 | DD PATMClearPIF_End - PATMClearPIF_Start
|
---|
126 | DD 1
|
---|
127 | DD PATM_INTERRUPTFLAG
|
---|
128 | DD 0
|
---|
129 | DD 0ffffffffh
|
---|
130 |
|
---|
131 | ;
|
---|
132 | ; Clear PATM_INHIBITIRQADDR and fault if IF=0
|
---|
133 | ;
|
---|
134 | BEGINPROC PATMClearInhibitIRQFaultIF0
|
---|
135 | PATMClearInhibitIRQFaultIF0_Start:
|
---|
136 | mov dword [ss:PATM_INTERRUPTFLAG], 0
|
---|
137 | mov dword [ss:PATM_INHIBITIRQADDR], 0
|
---|
138 | pushf
|
---|
139 |
|
---|
140 | test dword [ss:PATM_VMFLAGS], X86_EFL_IF
|
---|
141 | jz PATMClearInhibitIRQFaultIF0_Fault
|
---|
142 |
|
---|
143 | ; if interrupts are pending, then we must go back to the host context to handle them!
|
---|
144 | test dword [ss:PATM_VM_FORCEDACTIONS], VMCPU_FF_INTERRUPT_APIC | VMCPU_FF_INTERRUPT_PIC | VMCPU_FF_TIMER | VMCPU_FF_REQUEST
|
---|
145 | jz PATMClearInhibitIRQFaultIF0_Continue
|
---|
146 |
|
---|
147 | ; Go to our hypervisor trap handler to dispatch the pending irq
|
---|
148 | mov dword [ss:PATM_TEMP_EAX], eax
|
---|
149 | mov dword [ss:PATM_TEMP_ECX], ecx
|
---|
150 | mov dword [ss:PATM_TEMP_EDI], edi
|
---|
151 | mov dword [ss:PATM_TEMP_RESTORE_FLAGS], PATM_RESTORE_EAX | PATM_RESTORE_ECX | PATM_RESTORE_EDI
|
---|
152 | mov eax, PATM_ACTION_DISPATCH_PENDING_IRQ
|
---|
153 | lock or dword [ss:PATM_PENDINGACTION], eax
|
---|
154 | mov ecx, PATM_ACTION_MAGIC
|
---|
155 | mov edi, PATM_NEXTINSTRADDR
|
---|
156 | popfd ; restore flags we pushed above (the or instruction changes the flags as well)
|
---|
157 | db 0fh, 0bh ; illegal instr (hardcoded assumption in PATMHandleIllegalInstrTrap)
|
---|
158 | ; does not return
|
---|
159 |
|
---|
160 | PATMClearInhibitIRQFaultIF0_Fault:
|
---|
161 | popf
|
---|
162 | mov dword [ss:PATM_INTERRUPTFLAG], 1
|
---|
163 | PATM_INT3
|
---|
164 |
|
---|
165 | PATMClearInhibitIRQFaultIF0_Continue:
|
---|
166 | popf
|
---|
167 | mov dword [ss:PATM_INTERRUPTFLAG], 1
|
---|
168 | PATMClearInhibitIRQFaultIF0_End:
|
---|
169 | ENDPROC PATMClearInhibitIRQFaultIF0
|
---|
170 |
|
---|
171 |
|
---|
172 | ; Patch record for clearing PATM_INHIBITIRQADDR
|
---|
173 | GLOBALNAME PATMClearInhibitIRQFaultIF0Record
|
---|
174 | RTCCPTR_DEF PATMClearInhibitIRQFaultIF0_Start
|
---|
175 | DD 0
|
---|
176 | DD 0
|
---|
177 | DD 0
|
---|
178 | DD PATMClearInhibitIRQFaultIF0_End - PATMClearInhibitIRQFaultIF0_Start
|
---|
179 | DD 12
|
---|
180 | DD PATM_INTERRUPTFLAG
|
---|
181 | DD 0
|
---|
182 | DD PATM_INHIBITIRQADDR
|
---|
183 | DD 0
|
---|
184 | DD PATM_VMFLAGS
|
---|
185 | DD 0
|
---|
186 | DD PATM_VM_FORCEDACTIONS
|
---|
187 | DD 0
|
---|
188 | DD PATM_TEMP_EAX
|
---|
189 | DD 0
|
---|
190 | DD PATM_TEMP_ECX
|
---|
191 | DD 0
|
---|
192 | DD PATM_TEMP_EDI
|
---|
193 | DD 0
|
---|
194 | DD PATM_TEMP_RESTORE_FLAGS
|
---|
195 | DD 0
|
---|
196 | DD PATM_PENDINGACTION
|
---|
197 | DD 0
|
---|
198 | DD PATM_NEXTINSTRADDR
|
---|
199 | DD 0
|
---|
200 | DD PATM_INTERRUPTFLAG
|
---|
201 | DD 0
|
---|
202 | DD PATM_INTERRUPTFLAG
|
---|
203 | DD 0
|
---|
204 | DD 0ffffffffh
|
---|
205 |
|
---|
206 | ;
|
---|
207 | ; Clear PATM_INHIBITIRQADDR and continue if IF=0 (duplicated function only; never jump back to guest code afterwards!!)
|
---|
208 | ;
|
---|
209 | BEGINPROC PATMClearInhibitIRQContIF0
|
---|
210 | PATMClearInhibitIRQContIF0_Start:
|
---|
211 | mov dword [ss:PATM_INTERRUPTFLAG], 0
|
---|
212 | mov dword [ss:PATM_INHIBITIRQADDR], 0
|
---|
213 | pushf
|
---|
214 |
|
---|
215 | test dword [ss:PATM_VMFLAGS], X86_EFL_IF
|
---|
216 | jz PATMClearInhibitIRQContIF0_Continue
|
---|
217 |
|
---|
218 | ; if interrupts are pending, then we must go back to the host context to handle them!
|
---|
219 | test dword [ss:PATM_VM_FORCEDACTIONS], VMCPU_FF_INTERRUPT_APIC | VMCPU_FF_INTERRUPT_PIC | VMCPU_FF_TIMER | VMCPU_FF_REQUEST
|
---|
220 | jz PATMClearInhibitIRQContIF0_Continue
|
---|
221 |
|
---|
222 | ; Go to our hypervisor trap handler to dispatch the pending irq
|
---|
223 | mov dword [ss:PATM_TEMP_EAX], eax
|
---|
224 | mov dword [ss:PATM_TEMP_ECX], ecx
|
---|
225 | mov dword [ss:PATM_TEMP_EDI], edi
|
---|
226 | mov dword [ss:PATM_TEMP_RESTORE_FLAGS], PATM_RESTORE_EAX | PATM_RESTORE_ECX | PATM_RESTORE_EDI
|
---|
227 | mov eax, PATM_ACTION_DISPATCH_PENDING_IRQ
|
---|
228 | lock or dword [ss:PATM_PENDINGACTION], eax
|
---|
229 | mov ecx, PATM_ACTION_MAGIC
|
---|
230 | mov edi, PATM_NEXTINSTRADDR
|
---|
231 | popfd ; restore flags we pushed above (the or instruction changes the flags as well)
|
---|
232 | db 0fh, 0bh ; illegal instr (hardcoded assumption in PATMHandleIllegalInstrTrap)
|
---|
233 | ; does not return
|
---|
234 |
|
---|
235 | PATMClearInhibitIRQContIF0_Continue:
|
---|
236 | popf
|
---|
237 | mov dword [ss:PATM_INTERRUPTFLAG], 1
|
---|
238 | PATMClearInhibitIRQContIF0_End:
|
---|
239 | ENDPROC PATMClearInhibitIRQContIF0
|
---|
240 |
|
---|
241 |
|
---|
242 | ; Patch record for clearing PATM_INHIBITIRQADDR
|
---|
243 | GLOBALNAME PATMClearInhibitIRQContIF0Record
|
---|
244 | RTCCPTR_DEF PATMClearInhibitIRQContIF0_Start
|
---|
245 | DD 0
|
---|
246 | DD 0
|
---|
247 | DD 0
|
---|
248 | DD PATMClearInhibitIRQContIF0_End - PATMClearInhibitIRQContIF0_Start
|
---|
249 | DD 11
|
---|
250 | DD PATM_INTERRUPTFLAG
|
---|
251 | DD 0
|
---|
252 | DD PATM_INHIBITIRQADDR
|
---|
253 | DD 0
|
---|
254 | DD PATM_VMFLAGS
|
---|
255 | DD 0
|
---|
256 | DD PATM_VM_FORCEDACTIONS
|
---|
257 | DD 0
|
---|
258 | DD PATM_TEMP_EAX
|
---|
259 | DD 0
|
---|
260 | DD PATM_TEMP_ECX
|
---|
261 | DD 0
|
---|
262 | DD PATM_TEMP_EDI
|
---|
263 | DD 0
|
---|
264 | DD PATM_TEMP_RESTORE_FLAGS
|
---|
265 | DD 0
|
---|
266 | DD PATM_PENDINGACTION
|
---|
267 | DD 0
|
---|
268 | DD PATM_NEXTINSTRADDR
|
---|
269 | DD 0
|
---|
270 | DD PATM_INTERRUPTFLAG
|
---|
271 | DD 0
|
---|
272 | DD 0ffffffffh
|
---|
273 |
|
---|
274 |
|
---|
275 | BEGINPROC PATMCliReplacement
|
---|
276 | PATMCliStart:
|
---|
277 | mov dword [ss:PATM_INTERRUPTFLAG], 0
|
---|
278 | pushf
|
---|
279 | %ifdef PATM_LOG_PATCHINSTR
|
---|
280 | push eax
|
---|
281 | push ecx
|
---|
282 | mov eax, PATM_ACTION_LOG_CLI
|
---|
283 | lock or dword [ss:PATM_PENDINGACTION], eax
|
---|
284 | mov ecx, PATM_ACTION_MAGIC
|
---|
285 | db 0fh, 0bh ; illegal instr (hardcoded assumption in PATMHandleIllegalInstrTrap)
|
---|
286 | pop ecx
|
---|
287 | pop eax
|
---|
288 | %endif
|
---|
289 |
|
---|
290 | and dword [ss:PATM_VMFLAGS], ~X86_EFL_IF
|
---|
291 | popf
|
---|
292 |
|
---|
293 | mov dword [ss:PATM_INTERRUPTFLAG], 1
|
---|
294 | DB 0xE9
|
---|
295 | PATMCliJump:
|
---|
296 | DD PATM_JUMPDELTA
|
---|
297 | PATMCliEnd:
|
---|
298 | ENDPROC PATMCliReplacement
|
---|
299 |
|
---|
300 |
|
---|
301 | ; Patch record for 'cli'
|
---|
302 | GLOBALNAME PATMCliRecord
|
---|
303 | RTCCPTR_DEF PATMCliStart
|
---|
304 | DD PATMCliJump - PATMCliStart
|
---|
305 | DD 0
|
---|
306 | DD 0
|
---|
307 | DD PATMCliEnd - PATMCliStart
|
---|
308 | %ifdef PATM_LOG_PATCHINSTR
|
---|
309 | DD 4
|
---|
310 | %else
|
---|
311 | DD 3
|
---|
312 | %endif
|
---|
313 | DD PATM_INTERRUPTFLAG
|
---|
314 | DD 0
|
---|
315 | %ifdef PATM_LOG_PATCHINSTR
|
---|
316 | DD PATM_PENDINGACTION
|
---|
317 | DD 0
|
---|
318 | %endif
|
---|
319 | DD PATM_VMFLAGS
|
---|
320 | DD 0
|
---|
321 | DD PATM_INTERRUPTFLAG
|
---|
322 | DD 0
|
---|
323 | DD 0ffffffffh
|
---|
324 |
|
---|
325 |
|
---|
326 | BEGINPROC PATMStiReplacement
|
---|
327 | PATMStiStart:
|
---|
328 | mov dword [ss:PATM_INTERRUPTFLAG], 0
|
---|
329 | mov dword [ss:PATM_INHIBITIRQADDR], PATM_NEXTINSTRADDR
|
---|
330 | pushf
|
---|
331 | %ifdef PATM_LOG_PATCHINSTR
|
---|
332 | push eax
|
---|
333 | push ecx
|
---|
334 | mov eax, PATM_ACTION_LOG_STI
|
---|
335 | lock or dword [ss:PATM_PENDINGACTION], eax
|
---|
336 | mov ecx, PATM_ACTION_MAGIC
|
---|
337 | db 0fh, 0bh ; illegal instr (hardcoded assumption in PATMHandleIllegalInstrTrap)
|
---|
338 | pop ecx
|
---|
339 | pop eax
|
---|
340 | %endif
|
---|
341 | or dword [ss:PATM_VMFLAGS], X86_EFL_IF
|
---|
342 | popf
|
---|
343 | mov dword [ss:PATM_INTERRUPTFLAG], 1
|
---|
344 | PATMStiEnd:
|
---|
345 | ENDPROC PATMStiReplacement
|
---|
346 |
|
---|
347 | ; Patch record for 'sti'
|
---|
348 | GLOBALNAME PATMStiRecord
|
---|
349 | RTCCPTR_DEF PATMStiStart
|
---|
350 | DD 0
|
---|
351 | DD 0
|
---|
352 | DD 0
|
---|
353 | DD PATMStiEnd - PATMStiStart
|
---|
354 | %ifdef PATM_LOG_PATCHINSTR
|
---|
355 | DD 6
|
---|
356 | %else
|
---|
357 | DD 5
|
---|
358 | %endif
|
---|
359 | DD PATM_INTERRUPTFLAG
|
---|
360 | DD 0
|
---|
361 | DD PATM_INHIBITIRQADDR
|
---|
362 | DD 0
|
---|
363 | DD PATM_NEXTINSTRADDR
|
---|
364 | DD 0
|
---|
365 | %ifdef PATM_LOG_PATCHINSTR
|
---|
366 | DD PATM_PENDINGACTION
|
---|
367 | DD 0
|
---|
368 | %endif
|
---|
369 | DD PATM_VMFLAGS
|
---|
370 | DD 0
|
---|
371 | DD PATM_INTERRUPTFLAG
|
---|
372 | DD 0
|
---|
373 | DD 0ffffffffh
|
---|
374 |
|
---|
375 | ;
|
---|
376 | ; Trampoline code for trap entry (without error code on the stack)
|
---|
377 | ;
|
---|
378 | ; esp + 32 - GS (V86 only)
|
---|
379 | ; esp + 28 - FS (V86 only)
|
---|
380 | ; esp + 24 - DS (V86 only)
|
---|
381 | ; esp + 20 - ES (V86 only)
|
---|
382 | ; esp + 16 - SS (if transfer to inner ring)
|
---|
383 | ; esp + 12 - ESP (if transfer to inner ring)
|
---|
384 | ; esp + 8 - EFLAGS
|
---|
385 | ; esp + 4 - CS
|
---|
386 | ; esp - EIP
|
---|
387 | ;
|
---|
388 | BEGINPROC PATMTrapEntry
|
---|
389 | PATMTrapEntryStart:
|
---|
390 | mov dword [ss:PATM_INTERRUPTFLAG], 0
|
---|
391 | pushf
|
---|
392 |
|
---|
393 | %ifdef PATM_LOG_PATCHIRET
|
---|
394 | push eax
|
---|
395 | push ecx
|
---|
396 | push edx
|
---|
397 | lea edx, dword [ss:esp+12+4] ;3 dwords + pushed flags -> iret eip
|
---|
398 | mov eax, PATM_ACTION_LOG_GATE_ENTRY
|
---|
399 | lock or dword [ss:PATM_PENDINGACTION], eax
|
---|
400 | mov ecx, PATM_ACTION_MAGIC
|
---|
401 | db 0fh, 0bh ; illegal instr (hardcoded assumption in PATMHandleIllegalInstrTrap)
|
---|
402 | pop edx
|
---|
403 | pop ecx
|
---|
404 | pop eax
|
---|
405 | %endif
|
---|
406 |
|
---|
407 | test dword [esp+12], X86_EFL_VM
|
---|
408 | jnz PATMTrapNoRing1
|
---|
409 |
|
---|
410 | ; make sure the saved CS selector for ring 1 is made 0
|
---|
411 | test dword [esp+8], 2
|
---|
412 | jnz PATMTrapNoRing1
|
---|
413 | test dword [esp+8], 1
|
---|
414 | jz PATMTrapNoRing1
|
---|
415 | and dword [esp+8], dword ~1 ; yasm / nasm dword
|
---|
416 | PATMTrapNoRing1:
|
---|
417 |
|
---|
418 | ; correct EFLAGS on the stack to include the current IOPL
|
---|
419 | push eax
|
---|
420 | mov eax, dword [ss:PATM_VMFLAGS]
|
---|
421 | and eax, X86_EFL_IOPL
|
---|
422 | and dword [esp+16], ~X86_EFL_IOPL ; esp+16 = eflags = esp+8+4(efl)+4(eax)
|
---|
423 | or dword [esp+16], eax
|
---|
424 | pop eax
|
---|
425 |
|
---|
426 | popf
|
---|
427 | mov dword [ss:PATM_INTERRUPTFLAG], 1
|
---|
428 | DB 0xE9
|
---|
429 | PATMTrapEntryJump:
|
---|
430 | DD PATM_JUMPDELTA
|
---|
431 | PATMTrapEntryEnd:
|
---|
432 | ENDPROC PATMTrapEntry
|
---|
433 |
|
---|
434 |
|
---|
435 | ; Patch record for trap gate entrypoint
|
---|
436 | GLOBALNAME PATMTrapEntryRecord
|
---|
437 | RTCCPTR_DEF PATMTrapEntryStart
|
---|
438 | DD PATMTrapEntryJump - PATMTrapEntryStart
|
---|
439 | DD 0
|
---|
440 | DD 0
|
---|
441 | DD PATMTrapEntryEnd - PATMTrapEntryStart
|
---|
442 | %ifdef PATM_LOG_PATCHIRET
|
---|
443 | DD 4
|
---|
444 | %else
|
---|
445 | DD 3
|
---|
446 | %endif
|
---|
447 | DD PATM_INTERRUPTFLAG
|
---|
448 | DD 0
|
---|
449 | %ifdef PATM_LOG_PATCHIRET
|
---|
450 | DD PATM_PENDINGACTION
|
---|
451 | DD 0
|
---|
452 | %endif
|
---|
453 | DD PATM_VMFLAGS
|
---|
454 | DD 0
|
---|
455 | DD PATM_INTERRUPTFLAG
|
---|
456 | DD 0
|
---|
457 | DD 0ffffffffh
|
---|
458 |
|
---|
459 | ;
|
---|
460 | ; Trampoline code for trap entry (with error code on the stack)
|
---|
461 | ;
|
---|
462 | ; esp + 36 - GS (V86 only)
|
---|
463 | ; esp + 32 - FS (V86 only)
|
---|
464 | ; esp + 28 - DS (V86 only)
|
---|
465 | ; esp + 24 - ES (V86 only)
|
---|
466 | ; esp + 20 - SS (if transfer to inner ring)
|
---|
467 | ; esp + 16 - ESP (if transfer to inner ring)
|
---|
468 | ; esp + 12 - EFLAGS
|
---|
469 | ; esp + 8 - CS
|
---|
470 | ; esp + 4 - EIP
|
---|
471 | ; esp - error code
|
---|
472 | ;
|
---|
473 | BEGINPROC PATMTrapEntryErrorCode
|
---|
474 | PATMTrapErrorCodeEntryStart:
|
---|
475 | mov dword [ss:PATM_INTERRUPTFLAG], 0
|
---|
476 | pushf
|
---|
477 |
|
---|
478 | %ifdef PATM_LOG_PATCHIRET
|
---|
479 | push eax
|
---|
480 | push ecx
|
---|
481 | push edx
|
---|
482 | lea edx, dword [ss:esp+12+4+4] ;3 dwords + pushed flags + error code -> iret eip
|
---|
483 | mov eax, PATM_ACTION_LOG_GATE_ENTRY
|
---|
484 | lock or dword [ss:PATM_PENDINGACTION], eax
|
---|
485 | mov ecx, PATM_ACTION_MAGIC
|
---|
486 | db 0fh, 0bh ; illegal instr (hardcoded assumption in PATMHandleIllegalInstrTrap)
|
---|
487 | pop edx
|
---|
488 | pop ecx
|
---|
489 | pop eax
|
---|
490 | %endif
|
---|
491 |
|
---|
492 | test dword [esp+16], X86_EFL_VM
|
---|
493 | jnz PATMTrapErrorCodeNoRing1
|
---|
494 |
|
---|
495 | ; make sure the saved CS selector for ring 1 is made 0
|
---|
496 | test dword [esp+12], 2
|
---|
497 | jnz PATMTrapErrorCodeNoRing1
|
---|
498 | test dword [esp+12], 1
|
---|
499 | jz PATMTrapErrorCodeNoRing1
|
---|
500 | and dword [esp+12], dword ~1 ; yasm / nasm dword
|
---|
501 | PATMTrapErrorCodeNoRing1:
|
---|
502 |
|
---|
503 | ; correct EFLAGS on the stack to include the current IOPL
|
---|
504 | push eax
|
---|
505 | mov eax, dword [ss:PATM_VMFLAGS]
|
---|
506 | and eax, X86_EFL_IOPL
|
---|
507 | and dword [esp+20], ~X86_EFL_IOPL ; esp+20 = eflags = esp+8+4(efl)+4(error code)+4(eax)
|
---|
508 | or dword [esp+20], eax
|
---|
509 | pop eax
|
---|
510 |
|
---|
511 | popf
|
---|
512 | mov dword [ss:PATM_INTERRUPTFLAG], 1
|
---|
513 | DB 0xE9
|
---|
514 | PATMTrapErrorCodeEntryJump:
|
---|
515 | DD PATM_JUMPDELTA
|
---|
516 | PATMTrapErrorCodeEntryEnd:
|
---|
517 | ENDPROC PATMTrapEntryErrorCode
|
---|
518 |
|
---|
519 |
|
---|
520 | ; Patch record for trap gate entrypoint
|
---|
521 | GLOBALNAME PATMTrapEntryRecordErrorCode
|
---|
522 | RTCCPTR_DEF PATMTrapErrorCodeEntryStart
|
---|
523 | DD PATMTrapErrorCodeEntryJump - PATMTrapErrorCodeEntryStart
|
---|
524 | DD 0
|
---|
525 | DD 0
|
---|
526 | DD PATMTrapErrorCodeEntryEnd - PATMTrapErrorCodeEntryStart
|
---|
527 | %ifdef PATM_LOG_PATCHIRET
|
---|
528 | DD 4
|
---|
529 | %else
|
---|
530 | DD 3
|
---|
531 | %endif
|
---|
532 | DD PATM_INTERRUPTFLAG
|
---|
533 | DD 0
|
---|
534 | %ifdef PATM_LOG_PATCHIRET
|
---|
535 | DD PATM_PENDINGACTION
|
---|
536 | DD 0
|
---|
537 | %endif
|
---|
538 | DD PATM_VMFLAGS
|
---|
539 | DD 0
|
---|
540 | DD PATM_INTERRUPTFLAG
|
---|
541 | DD 0
|
---|
542 | DD 0ffffffffh
|
---|
543 |
|
---|
544 |
|
---|
545 | ;
|
---|
546 | ; Trampoline code for interrupt gate entry (without error code on the stack)
|
---|
547 | ;
|
---|
548 | ; esp + 32 - GS (V86 only)
|
---|
549 | ; esp + 28 - FS (V86 only)
|
---|
550 | ; esp + 24 - DS (V86 only)
|
---|
551 | ; esp + 20 - ES (V86 only)
|
---|
552 | ; esp + 16 - SS (if transfer to inner ring)
|
---|
553 | ; esp + 12 - ESP (if transfer to inner ring)
|
---|
554 | ; esp + 8 - EFLAGS
|
---|
555 | ; esp + 4 - CS
|
---|
556 | ; esp - EIP
|
---|
557 | ;
|
---|
558 | BEGINPROC PATMIntEntry
|
---|
559 | PATMIntEntryStart:
|
---|
560 | mov dword [ss:PATM_INTERRUPTFLAG], 0
|
---|
561 | pushf
|
---|
562 |
|
---|
563 | %ifdef PATM_LOG_PATCHIRET
|
---|
564 | push eax
|
---|
565 | push ecx
|
---|
566 | push edx
|
---|
567 | lea edx, dword [ss:esp+12+4] ;3 dwords + pushed flags -> iret eip
|
---|
568 | mov eax, PATM_ACTION_LOG_GATE_ENTRY
|
---|
569 | lock or dword [ss:PATM_PENDINGACTION], eax
|
---|
570 | mov ecx, PATM_ACTION_MAGIC
|
---|
571 | db 0fh, 0bh ; illegal instr (hardcoded assumption in PATMHandleIllegalInstrTrap)
|
---|
572 | pop edx
|
---|
573 | pop ecx
|
---|
574 | pop eax
|
---|
575 | %endif
|
---|
576 |
|
---|
577 | test dword [esp+12], X86_EFL_VM
|
---|
578 | jnz PATMIntNoRing1
|
---|
579 |
|
---|
580 | ; make sure the saved CS selector for ring 1 is made 0
|
---|
581 | test dword [esp+8], 2
|
---|
582 | jnz PATMIntNoRing1
|
---|
583 | test dword [esp+8], 1
|
---|
584 | jz PATMIntNoRing1
|
---|
585 | and dword [esp+8], dword ~1 ; yasm / nasm dword
|
---|
586 | PATMIntNoRing1:
|
---|
587 |
|
---|
588 | ; correct EFLAGS on the stack to include the current IOPL
|
---|
589 | push eax
|
---|
590 | mov eax, dword [ss:PATM_VMFLAGS]
|
---|
591 | and eax, X86_EFL_IOPL
|
---|
592 | and dword [esp+16], ~X86_EFL_IOPL ; esp+16 = eflags = esp+8+4(efl)+4(eax)
|
---|
593 | or dword [esp+16], eax
|
---|
594 | pop eax
|
---|
595 |
|
---|
596 | popf
|
---|
597 | mov dword [ss:PATM_INTERRUPTFLAG], 1
|
---|
598 | PATMIntEntryEnd:
|
---|
599 | ENDPROC PATMIntEntry
|
---|
600 |
|
---|
601 |
|
---|
602 | ; Patch record for interrupt gate entrypoint
|
---|
603 | GLOBALNAME PATMIntEntryRecord
|
---|
604 | RTCCPTR_DEF PATMIntEntryStart
|
---|
605 | DD 0
|
---|
606 | DD 0
|
---|
607 | DD 0
|
---|
608 | DD PATMIntEntryEnd - PATMIntEntryStart
|
---|
609 | %ifdef PATM_LOG_PATCHIRET
|
---|
610 | DD 4
|
---|
611 | %else
|
---|
612 | DD 3
|
---|
613 | %endif
|
---|
614 | DD PATM_INTERRUPTFLAG
|
---|
615 | DD 0
|
---|
616 | %ifdef PATM_LOG_PATCHIRET
|
---|
617 | DD PATM_PENDINGACTION
|
---|
618 | DD 0
|
---|
619 | %endif
|
---|
620 | DD PATM_VMFLAGS
|
---|
621 | DD 0
|
---|
622 | DD PATM_INTERRUPTFLAG
|
---|
623 | DD 0
|
---|
624 | DD 0ffffffffh
|
---|
625 |
|
---|
626 | ;
|
---|
627 | ; Trampoline code for interrupt gate entry (*with* error code on the stack)
|
---|
628 | ;
|
---|
629 | ; esp + 36 - GS (V86 only)
|
---|
630 | ; esp + 32 - FS (V86 only)
|
---|
631 | ; esp + 28 - DS (V86 only)
|
---|
632 | ; esp + 24 - ES (V86 only)
|
---|
633 | ; esp + 20 - SS (if transfer to inner ring)
|
---|
634 | ; esp + 16 - ESP (if transfer to inner ring)
|
---|
635 | ; esp + 12 - EFLAGS
|
---|
636 | ; esp + 8 - CS
|
---|
637 | ; esp + 4 - EIP
|
---|
638 | ; esp - error code
|
---|
639 | ;
|
---|
640 | BEGINPROC PATMIntEntryErrorCode
|
---|
641 | PATMIntEntryErrorCodeStart:
|
---|
642 | mov dword [ss:PATM_INTERRUPTFLAG], 0
|
---|
643 | pushf
|
---|
644 |
|
---|
645 | %ifdef PATM_LOG_PATCHIRET
|
---|
646 | push eax
|
---|
647 | push ecx
|
---|
648 | push edx
|
---|
649 | lea edx, dword [ss:esp+12+4+4] ;3 dwords + pushed flags + error code -> iret eip
|
---|
650 | mov eax, PATM_ACTION_LOG_GATE_ENTRY
|
---|
651 | lock or dword [ss:PATM_PENDINGACTION], eax
|
---|
652 | mov ecx, PATM_ACTION_MAGIC
|
---|
653 | db 0fh, 0bh ; illegal instr (hardcoded assumption in PATMHandleIllegalInstrTrap)
|
---|
654 | pop edx
|
---|
655 | pop ecx
|
---|
656 | pop eax
|
---|
657 | %endif
|
---|
658 |
|
---|
659 | test dword [esp+16], X86_EFL_VM
|
---|
660 | jnz PATMIntNoRing1_ErrorCode
|
---|
661 |
|
---|
662 | ; make sure the saved CS selector for ring 1 is made 0
|
---|
663 | test dword [esp+12], 2
|
---|
664 | jnz PATMIntNoRing1_ErrorCode
|
---|
665 | test dword [esp+12], 1
|
---|
666 | jz PATMIntNoRing1_ErrorCode
|
---|
667 | and dword [esp+12], dword ~1 ; yasm / nasm dword
|
---|
668 | PATMIntNoRing1_ErrorCode:
|
---|
669 |
|
---|
670 | ; correct EFLAGS on the stack to include the current IOPL
|
---|
671 | push eax
|
---|
672 | mov eax, dword [ss:PATM_VMFLAGS]
|
---|
673 | and eax, X86_EFL_IOPL
|
---|
674 | and dword [esp+20], ~X86_EFL_IOPL ; esp+20 = eflags = esp+8+4(efl)+4(eax)+4(error code)
|
---|
675 | or dword [esp+20], eax
|
---|
676 | pop eax
|
---|
677 |
|
---|
678 | popf
|
---|
679 | mov dword [ss:PATM_INTERRUPTFLAG], 1
|
---|
680 | PATMIntEntryErrorCodeEnd:
|
---|
681 | ENDPROC PATMIntEntryErrorCode
|
---|
682 |
|
---|
683 |
|
---|
684 | ; Patch record for interrupt gate entrypoint
|
---|
685 | GLOBALNAME PATMIntEntryRecordErrorCode
|
---|
686 | RTCCPTR_DEF PATMIntEntryErrorCodeStart
|
---|
687 | DD 0
|
---|
688 | DD 0
|
---|
689 | DD 0
|
---|
690 | DD PATMIntEntryErrorCodeEnd - PATMIntEntryErrorCodeStart
|
---|
691 | %ifdef PATM_LOG_PATCHIRET
|
---|
692 | DD 4
|
---|
693 | %else
|
---|
694 | DD 3
|
---|
695 | %endif
|
---|
696 | DD PATM_INTERRUPTFLAG
|
---|
697 | DD 0
|
---|
698 | %ifdef PATM_LOG_PATCHIRET
|
---|
699 | DD PATM_PENDINGACTION
|
---|
700 | DD 0
|
---|
701 | %endif
|
---|
702 | DD PATM_VMFLAGS
|
---|
703 | DD 0
|
---|
704 | DD PATM_INTERRUPTFLAG
|
---|
705 | DD 0
|
---|
706 | DD 0ffffffffh
|
---|
707 |
|
---|
708 | ;
|
---|
709 | ; 32 bits Popf replacement that faults when IF remains 0
|
---|
710 | ;
|
---|
711 | BEGINPROC PATMPopf32Replacement
|
---|
712 | PATMPopf32Start:
|
---|
713 | mov dword [ss:PATM_INTERRUPTFLAG], 0
|
---|
714 | %ifdef PATM_LOG_PATCHINSTR
|
---|
715 | push eax
|
---|
716 | push ecx
|
---|
717 | mov eax, PATM_ACTION_LOG_POPF_IF1
|
---|
718 | test dword [esp+8], X86_EFL_IF
|
---|
719 | jnz PATMPopf32_Log
|
---|
720 | mov eax, PATM_ACTION_LOG_POPF_IF0
|
---|
721 |
|
---|
722 | PATMPopf32_Log:
|
---|
723 | lock or dword [ss:PATM_PENDINGACTION], eax
|
---|
724 | mov ecx, PATM_ACTION_MAGIC
|
---|
725 | db 0fh, 0bh ; illegal instr (hardcoded assumption in PATMHandleIllegalInstrTrap)
|
---|
726 | pop ecx
|
---|
727 | pop eax
|
---|
728 | %endif
|
---|
729 |
|
---|
730 | test dword [esp], X86_EFL_IF
|
---|
731 | jnz PATMPopf32_Ok
|
---|
732 | mov dword [ss:PATM_INTERRUPTFLAG], 1
|
---|
733 | PATM_INT3
|
---|
734 |
|
---|
735 | PATMPopf32_Ok:
|
---|
736 | ; Note: we don't allow popf instructions to change the current IOPL; we simply ignore such changes (!!!)
|
---|
737 | ; In this particular patch it's rather unlikely the pushf was included, so we have no way to check if the flags on the stack were correctly synced
|
---|
738 | ; PATMPopf32Replacement_NoExit is different, because it's only used in IDT and function patches
|
---|
739 | or dword [ss:PATM_VMFLAGS], X86_EFL_IF
|
---|
740 |
|
---|
741 | ; if interrupts are pending, then we must go back to the host context to handle them!
|
---|
742 | test dword [ss:PATM_VM_FORCEDACTIONS], VMCPU_FF_INTERRUPT_APIC | VMCPU_FF_INTERRUPT_PIC | VMCPU_FF_TIMER | VMCPU_FF_REQUEST
|
---|
743 | jz PATMPopf32_Continue
|
---|
744 |
|
---|
745 | ; Go to our hypervisor trap handler to dispatch the pending irq
|
---|
746 | mov dword [ss:PATM_TEMP_EAX], eax
|
---|
747 | mov dword [ss:PATM_TEMP_ECX], ecx
|
---|
748 | mov dword [ss:PATM_TEMP_EDI], edi
|
---|
749 | mov dword [ss:PATM_TEMP_RESTORE_FLAGS], PATM_RESTORE_EAX | PATM_RESTORE_ECX | PATM_RESTORE_EDI
|
---|
750 | mov eax, PATM_ACTION_DISPATCH_PENDING_IRQ
|
---|
751 | lock or dword [ss:PATM_PENDINGACTION], eax
|
---|
752 | mov ecx, PATM_ACTION_MAGIC
|
---|
753 | mov edi, PATM_NEXTINSTRADDR
|
---|
754 |
|
---|
755 | popfd ; restore flags we pushed above (the or instruction changes the flags as well)
|
---|
756 | db 0fh, 0bh ; illegal instr (hardcoded assumption in PATMHandleIllegalInstrTrap)
|
---|
757 | ; does not return
|
---|
758 |
|
---|
759 | PATMPopf32_Continue:
|
---|
760 | popfd ; restore flags we pushed above
|
---|
761 | mov dword [ss:PATM_INTERRUPTFLAG], 1
|
---|
762 | DB 0xE9
|
---|
763 | PATMPopf32Jump:
|
---|
764 | DD PATM_JUMPDELTA
|
---|
765 | PATMPopf32End:
|
---|
766 | ENDPROC PATMPopf32Replacement
|
---|
767 |
|
---|
768 |
|
---|
769 | ; Patch record for 'popfd'
|
---|
770 | GLOBALNAME PATMPopf32Record
|
---|
771 | RTCCPTR_DEF PATMPopf32Start
|
---|
772 | DD PATMPopf32Jump - PATMPopf32Start
|
---|
773 | DD 0
|
---|
774 | DD 0
|
---|
775 | DD PATMPopf32End - PATMPopf32Start
|
---|
776 | %ifdef PATM_LOG_PATCHINSTR
|
---|
777 | DD 12
|
---|
778 | %else
|
---|
779 | DD 11
|
---|
780 | %endif
|
---|
781 | DD PATM_INTERRUPTFLAG
|
---|
782 | DD 0
|
---|
783 | %ifdef PATM_LOG_PATCHINSTR
|
---|
784 | DD PATM_PENDINGACTION
|
---|
785 | DD 0
|
---|
786 | %endif
|
---|
787 | DD PATM_INTERRUPTFLAG
|
---|
788 | DD 0
|
---|
789 | DD PATM_VMFLAGS
|
---|
790 | DD 0
|
---|
791 | DD PATM_VM_FORCEDACTIONS
|
---|
792 | DD 0
|
---|
793 | DD PATM_TEMP_EAX
|
---|
794 | DD 0
|
---|
795 | DD PATM_TEMP_ECX
|
---|
796 | DD 0
|
---|
797 | DD PATM_TEMP_EDI
|
---|
798 | DD 0
|
---|
799 | DD PATM_TEMP_RESTORE_FLAGS
|
---|
800 | DD 0
|
---|
801 | DD PATM_PENDINGACTION
|
---|
802 | DD 0
|
---|
803 | DD PATM_NEXTINSTRADDR
|
---|
804 | DD 0
|
---|
805 | DD PATM_INTERRUPTFLAG
|
---|
806 | DD 0
|
---|
807 | DD 0ffffffffh
|
---|
808 |
|
---|
809 | ; no need to check the IF flag when popf isn't an exit point of a patch (e.g. function duplication)
|
---|
810 | BEGINPROC PATMPopf32Replacement_NoExit
|
---|
811 | PATMPopf32_NoExitStart:
|
---|
812 | mov dword [ss:PATM_INTERRUPTFLAG], 0
|
---|
813 | %ifdef PATM_LOG_PATCHINSTR
|
---|
814 | push eax
|
---|
815 | push ecx
|
---|
816 | mov eax, PATM_ACTION_LOG_POPF_IF1
|
---|
817 | test dword [esp+8], X86_EFL_IF
|
---|
818 | jnz PATMPopf32_NoExitLog
|
---|
819 | mov eax, PATM_ACTION_LOG_POPF_IF0
|
---|
820 |
|
---|
821 | PATMPopf32_NoExitLog:
|
---|
822 | lock or dword [ss:PATM_PENDINGACTION], eax
|
---|
823 | mov ecx, PATM_ACTION_MAGIC
|
---|
824 | db 0fh, 0bh ; illegal instr (hardcoded assumption in PATMHandleIllegalInstrTrap)
|
---|
825 | pop ecx
|
---|
826 | pop eax
|
---|
827 | %endif
|
---|
828 | test dword [esp], X86_EFL_IF
|
---|
829 | jz PATMPopf32_NoExit_Continue
|
---|
830 |
|
---|
831 | ; if interrupts are pending, then we must go back to the host context to handle them!
|
---|
832 | test dword [ss:PATM_VM_FORCEDACTIONS], VMCPU_FF_INTERRUPT_APIC | VMCPU_FF_INTERRUPT_PIC | VMCPU_FF_TIMER | VMCPU_FF_REQUEST
|
---|
833 | jz PATMPopf32_NoExit_Continue
|
---|
834 |
|
---|
835 | ; Go to our hypervisor trap handler to dispatch the pending irq
|
---|
836 | mov dword [ss:PATM_TEMP_EAX], eax
|
---|
837 | mov dword [ss:PATM_TEMP_ECX], ecx
|
---|
838 | mov dword [ss:PATM_TEMP_EDI], edi
|
---|
839 | mov dword [ss:PATM_TEMP_RESTORE_FLAGS], PATM_RESTORE_EAX | PATM_RESTORE_ECX | PATM_RESTORE_EDI
|
---|
840 | mov eax, PATM_ACTION_DISPATCH_PENDING_IRQ
|
---|
841 | lock or dword [ss:PATM_PENDINGACTION], eax
|
---|
842 | mov ecx, PATM_ACTION_MAGIC
|
---|
843 | mov edi, PATM_NEXTINSTRADDR
|
---|
844 |
|
---|
845 | pop dword [ss:PATM_VMFLAGS] ; restore flags now (the or instruction changes the flags as well)
|
---|
846 | push dword [ss:PATM_VMFLAGS]
|
---|
847 | popfd
|
---|
848 |
|
---|
849 | db 0fh, 0bh ; illegal instr (hardcoded assumption in PATMHandleIllegalInstrTrap)
|
---|
850 | ; does not return
|
---|
851 |
|
---|
852 | PATMPopf32_NoExit_Continue:
|
---|
853 | pop dword [ss:PATM_VMFLAGS]
|
---|
854 | push dword [ss:PATM_VMFLAGS]
|
---|
855 | popfd
|
---|
856 | mov dword [ss:PATM_INTERRUPTFLAG], 1
|
---|
857 | PATMPopf32_NoExitEnd:
|
---|
858 | ENDPROC PATMPopf32Replacement_NoExit
|
---|
859 |
|
---|
860 |
|
---|
861 | ; Patch record for 'popfd'
|
---|
862 | GLOBALNAME PATMPopf32Record_NoExit
|
---|
863 | RTCCPTR_DEF PATMPopf32_NoExitStart
|
---|
864 | DD 0
|
---|
865 | DD 0
|
---|
866 | DD 0
|
---|
867 | DD PATMPopf32_NoExitEnd - PATMPopf32_NoExitStart
|
---|
868 | %ifdef PATM_LOG_PATCHINSTR
|
---|
869 | DD 14
|
---|
870 | %else
|
---|
871 | DD 13
|
---|
872 | %endif
|
---|
873 | DD PATM_INTERRUPTFLAG
|
---|
874 | DD 0
|
---|
875 | %ifdef PATM_LOG_PATCHINSTR
|
---|
876 | DD PATM_PENDINGACTION
|
---|
877 | DD 0
|
---|
878 | %endif
|
---|
879 | DD PATM_VM_FORCEDACTIONS
|
---|
880 | DD 0
|
---|
881 | DD PATM_TEMP_EAX
|
---|
882 | DD 0
|
---|
883 | DD PATM_TEMP_ECX
|
---|
884 | DD 0
|
---|
885 | DD PATM_TEMP_EDI
|
---|
886 | DD 0
|
---|
887 | DD PATM_TEMP_RESTORE_FLAGS
|
---|
888 | DD 0
|
---|
889 | DD PATM_PENDINGACTION
|
---|
890 | DD 0
|
---|
891 | DD PATM_NEXTINSTRADDR
|
---|
892 | DD 0
|
---|
893 | DD PATM_VMFLAGS
|
---|
894 | DD 0
|
---|
895 | DD PATM_VMFLAGS
|
---|
896 | DD 0
|
---|
897 | DD PATM_VMFLAGS
|
---|
898 | DD 0
|
---|
899 | DD PATM_VMFLAGS
|
---|
900 | DD 0
|
---|
901 | DD PATM_INTERRUPTFLAG
|
---|
902 | DD 0
|
---|
903 | DD 0ffffffffh
|
---|
904 |
|
---|
905 |
|
---|
906 | ;
|
---|
907 | ; 16 bits Popf replacement that faults when IF remains 0
|
---|
908 | ;
|
---|
909 | BEGINPROC PATMPopf16Replacement
|
---|
910 | PATMPopf16Start:
|
---|
911 | mov dword [ss:PATM_INTERRUPTFLAG], 0
|
---|
912 | test word [esp], X86_EFL_IF
|
---|
913 | jnz PATMPopf16_Ok
|
---|
914 | mov dword [ss:PATM_INTERRUPTFLAG], 1
|
---|
915 | PATM_INT3
|
---|
916 |
|
---|
917 | PATMPopf16_Ok:
|
---|
918 | ; if interrupts are pending, then we must go back to the host context to handle them!
|
---|
919 | ; @note we destroy the flags here, but that should really not matter (PATM_INT3 case)
|
---|
920 | test dword [ss:PATM_VM_FORCEDACTIONS], VMCPU_FF_INTERRUPT_APIC | VMCPU_FF_INTERRUPT_PIC | VMCPU_FF_TIMER | VMCPU_FF_REQUEST
|
---|
921 | jz PATMPopf16_Continue
|
---|
922 | mov dword [ss:PATM_INTERRUPTFLAG], 1
|
---|
923 | PATM_INT3
|
---|
924 |
|
---|
925 | PATMPopf16_Continue:
|
---|
926 |
|
---|
927 | pop word [ss:PATM_VMFLAGS]
|
---|
928 | push word [ss:PATM_VMFLAGS]
|
---|
929 | and dword [ss:PATM_VMFLAGS], PATM_VIRTUAL_FLAGS_MASK
|
---|
930 | or dword [ss:PATM_VMFLAGS], PATM_VIRTUAL_FLAGS_MASK
|
---|
931 |
|
---|
932 | DB 0x66 ; size override
|
---|
933 | popf ;after the and and or operations!! (flags must be preserved)
|
---|
934 | mov dword [ss:PATM_INTERRUPTFLAG], 1
|
---|
935 |
|
---|
936 | DB 0xE9
|
---|
937 | PATMPopf16Jump:
|
---|
938 | DD PATM_JUMPDELTA
|
---|
939 | PATMPopf16End:
|
---|
940 | ENDPROC PATMPopf16Replacement
|
---|
941 |
|
---|
942 |
|
---|
943 | ; Patch record for 'popf'
|
---|
944 | GLOBALNAME PATMPopf16Record
|
---|
945 | RTCCPTR_DEF PATMPopf16Start
|
---|
946 | DD PATMPopf16Jump - PATMPopf16Start
|
---|
947 | DD 0
|
---|
948 | DD 0
|
---|
949 | DD PATMPopf16End - PATMPopf16Start
|
---|
950 | DD 9
|
---|
951 | DD PATM_INTERRUPTFLAG
|
---|
952 | DD 0
|
---|
953 | DD PATM_INTERRUPTFLAG
|
---|
954 | DD 0
|
---|
955 | DD PATM_VM_FORCEDACTIONS
|
---|
956 | DD 0
|
---|
957 | DD PATM_INTERRUPTFLAG
|
---|
958 | DD 0
|
---|
959 | DD PATM_VMFLAGS
|
---|
960 | DD 0
|
---|
961 | DD PATM_VMFLAGS
|
---|
962 | DD 0
|
---|
963 | DD PATM_VMFLAGS
|
---|
964 | DD 0
|
---|
965 | DD PATM_VMFLAGS
|
---|
966 | DD 0
|
---|
967 | DD PATM_INTERRUPTFLAG
|
---|
968 | DD 0
|
---|
969 | DD 0ffffffffh
|
---|
970 |
|
---|
971 | ;
|
---|
972 | ; 16 bits Popf replacement that faults when IF remains 0
|
---|
973 | ; @todo not necessary to fault in that case (see 32 bits version)
|
---|
974 | BEGINPROC PATMPopf16Replacement_NoExit
|
---|
975 | PATMPopf16Start_NoExit:
|
---|
976 | mov dword [ss:PATM_INTERRUPTFLAG], 0
|
---|
977 | test word [esp], X86_EFL_IF
|
---|
978 | jnz PATMPopf16_Ok_NoExit
|
---|
979 | mov dword [ss:PATM_INTERRUPTFLAG], 1
|
---|
980 | PATM_INT3
|
---|
981 |
|
---|
982 | PATMPopf16_Ok_NoExit:
|
---|
983 | ; if interrupts are pending, then we must go back to the host context to handle them!
|
---|
984 | ; @note we destroy the flags here, but that should really not matter (PATM_INT3 case)
|
---|
985 | test dword [ss:PATM_VM_FORCEDACTIONS], VMCPU_FF_INTERRUPT_APIC | VMCPU_FF_INTERRUPT_PIC | VMCPU_FF_TIMER | VMCPU_FF_REQUEST
|
---|
986 | jz PATMPopf16_Continue_NoExit
|
---|
987 | mov dword [ss:PATM_INTERRUPTFLAG], 1
|
---|
988 | PATM_INT3
|
---|
989 |
|
---|
990 | PATMPopf16_Continue_NoExit:
|
---|
991 |
|
---|
992 | pop word [ss:PATM_VMFLAGS]
|
---|
993 | push word [ss:PATM_VMFLAGS]
|
---|
994 | and dword [ss:PATM_VMFLAGS], PATM_VIRTUAL_FLAGS_MASK
|
---|
995 | or dword [ss:PATM_VMFLAGS], PATM_VIRTUAL_FLAGS_MASK
|
---|
996 |
|
---|
997 | DB 0x66 ; size override
|
---|
998 | popf ;after the and and or operations!! (flags must be preserved)
|
---|
999 | mov dword [ss:PATM_INTERRUPTFLAG], 1
|
---|
1000 | PATMPopf16End_NoExit:
|
---|
1001 | ENDPROC PATMPopf16Replacement_NoExit
|
---|
1002 |
|
---|
1003 |
|
---|
1004 | ; Patch record for 'popf'
|
---|
1005 | GLOBALNAME PATMPopf16Record_NoExit
|
---|
1006 | RTCCPTR_DEF PATMPopf16Start_NoExit
|
---|
1007 | DD 0
|
---|
1008 | DD 0
|
---|
1009 | DD 0
|
---|
1010 | DD PATMPopf16End_NoExit - PATMPopf16Start_NoExit
|
---|
1011 | DD 9
|
---|
1012 | DD PATM_INTERRUPTFLAG
|
---|
1013 | DD 0
|
---|
1014 | DD PATM_INTERRUPTFLAG
|
---|
1015 | DD 0
|
---|
1016 | DD PATM_VM_FORCEDACTIONS
|
---|
1017 | DD 0
|
---|
1018 | DD PATM_INTERRUPTFLAG
|
---|
1019 | DD 0
|
---|
1020 | DD PATM_VMFLAGS
|
---|
1021 | DD 0
|
---|
1022 | DD PATM_VMFLAGS
|
---|
1023 | DD 0
|
---|
1024 | DD PATM_VMFLAGS
|
---|
1025 | DD 0
|
---|
1026 | DD PATM_VMFLAGS
|
---|
1027 | DD 0
|
---|
1028 | DD PATM_INTERRUPTFLAG
|
---|
1029 | DD 0
|
---|
1030 | DD 0ffffffffh
|
---|
1031 |
|
---|
1032 |
|
---|
1033 | BEGINPROC PATMPushf32Replacement
|
---|
1034 | PATMPushf32Start:
|
---|
1035 | mov dword [ss:PATM_INTERRUPTFLAG], 0
|
---|
1036 | pushfd
|
---|
1037 | %ifdef PATM_LOG_PATCHINSTR
|
---|
1038 | push eax
|
---|
1039 | push ecx
|
---|
1040 | mov eax, PATM_ACTION_LOG_PUSHF
|
---|
1041 | lock or dword [ss:PATM_PENDINGACTION], eax
|
---|
1042 | mov ecx, PATM_ACTION_MAGIC
|
---|
1043 | db 0fh, 0bh ; illegal instr (hardcoded assumption in PATMHandleIllegalInstrTrap)
|
---|
1044 | pop ecx
|
---|
1045 | pop eax
|
---|
1046 | %endif
|
---|
1047 |
|
---|
1048 | pushfd
|
---|
1049 | push eax
|
---|
1050 | mov eax, dword [esp+8]
|
---|
1051 | and eax, PATM_FLAGS_MASK
|
---|
1052 | or eax, dword [ss:PATM_VMFLAGS]
|
---|
1053 | mov dword [esp+8], eax
|
---|
1054 | pop eax
|
---|
1055 | popfd
|
---|
1056 | mov dword [ss:PATM_INTERRUPTFLAG], 1
|
---|
1057 | PATMPushf32End:
|
---|
1058 | ENDPROC PATMPushf32Replacement
|
---|
1059 |
|
---|
1060 |
|
---|
1061 | ; Patch record for 'pushfd'
|
---|
1062 | GLOBALNAME PATMPushf32Record
|
---|
1063 | RTCCPTR_DEF PATMPushf32Start
|
---|
1064 | DD 0
|
---|
1065 | DD 0
|
---|
1066 | DD 0
|
---|
1067 | DD PATMPushf32End - PATMPushf32Start
|
---|
1068 | %ifdef PATM_LOG_PATCHINSTR
|
---|
1069 | DD 4
|
---|
1070 | %else
|
---|
1071 | DD 3
|
---|
1072 | %endif
|
---|
1073 | DD PATM_INTERRUPTFLAG
|
---|
1074 | DD 0
|
---|
1075 | %ifdef PATM_LOG_PATCHINSTR
|
---|
1076 | DD PATM_PENDINGACTION
|
---|
1077 | DD 0
|
---|
1078 | %endif
|
---|
1079 | DD PATM_VMFLAGS
|
---|
1080 | DD 0
|
---|
1081 | DD PATM_INTERRUPTFLAG
|
---|
1082 | DD 0
|
---|
1083 | DD 0ffffffffh
|
---|
1084 |
|
---|
1085 |
|
---|
1086 | BEGINPROC PATMPushf16Replacement
|
---|
1087 | PATMPushf16Start:
|
---|
1088 | mov dword [ss:PATM_INTERRUPTFLAG], 0
|
---|
1089 | DB 0x66 ; size override
|
---|
1090 | pushf
|
---|
1091 | DB 0x66 ; size override
|
---|
1092 | pushf
|
---|
1093 | push eax
|
---|
1094 | xor eax, eax
|
---|
1095 | mov ax, word [esp+6]
|
---|
1096 | and eax, PATM_FLAGS_MASK
|
---|
1097 | or eax, dword [ss:PATM_VMFLAGS]
|
---|
1098 | mov word [esp+6], ax
|
---|
1099 | pop eax
|
---|
1100 |
|
---|
1101 | DB 0x66 ; size override
|
---|
1102 | popf
|
---|
1103 | mov dword [ss:PATM_INTERRUPTFLAG], 1
|
---|
1104 | PATMPushf16End:
|
---|
1105 | ENDPROC PATMPushf16Replacement
|
---|
1106 |
|
---|
1107 |
|
---|
1108 | ; Patch record for 'pushf'
|
---|
1109 | GLOBALNAME PATMPushf16Record
|
---|
1110 | RTCCPTR_DEF PATMPushf16Start
|
---|
1111 | DD 0
|
---|
1112 | DD 0
|
---|
1113 | DD 0
|
---|
1114 | DD PATMPushf16End - PATMPushf16Start
|
---|
1115 | DD 3
|
---|
1116 | DD PATM_INTERRUPTFLAG
|
---|
1117 | DD 0
|
---|
1118 | DD PATM_VMFLAGS
|
---|
1119 | DD 0
|
---|
1120 | DD PATM_INTERRUPTFLAG
|
---|
1121 | DD 0
|
---|
1122 | DD 0ffffffffh
|
---|
1123 |
|
---|
1124 |
|
---|
1125 | BEGINPROC PATMPushCSReplacement
|
---|
1126 | PATMPushCSStart:
|
---|
1127 | mov dword [ss:PATM_INTERRUPTFLAG], 0
|
---|
1128 | push cs
|
---|
1129 | pushfd
|
---|
1130 |
|
---|
1131 | test dword [esp+4], 2
|
---|
1132 | jnz pushcs_notring1
|
---|
1133 |
|
---|
1134 | ; change dpl from 1 to 0
|
---|
1135 | and dword [esp+4], dword ~1 ; yasm / nasm dword
|
---|
1136 |
|
---|
1137 | pushcs_notring1:
|
---|
1138 | popfd
|
---|
1139 |
|
---|
1140 | mov dword [ss:PATM_INTERRUPTFLAG], 1
|
---|
1141 | DB 0xE9
|
---|
1142 | PATMPushCSJump:
|
---|
1143 | DD PATM_JUMPDELTA
|
---|
1144 | PATMPushCSEnd:
|
---|
1145 | ENDPROC PATMPushCSReplacement
|
---|
1146 |
|
---|
1147 |
|
---|
1148 | ; Patch record for 'push cs'
|
---|
1149 | GLOBALNAME PATMPushCSRecord
|
---|
1150 | RTCCPTR_DEF PATMPushCSStart
|
---|
1151 | DD PATMPushCSJump - PATMPushCSStart
|
---|
1152 | DD 0
|
---|
1153 | DD 0
|
---|
1154 | DD PATMPushCSEnd - PATMPushCSStart
|
---|
1155 | DD 2
|
---|
1156 | DD PATM_INTERRUPTFLAG
|
---|
1157 | DD 0
|
---|
1158 | DD PATM_INTERRUPTFLAG
|
---|
1159 | DD 0
|
---|
1160 | DD 0ffffffffh
|
---|
1161 |
|
---|
1162 | ;;****************************************************
|
---|
1163 | ;; Abstract:
|
---|
1164 | ;;
|
---|
1165 | ;; if eflags.NT==0 && iretstack.eflags.VM==0 && iretstack.eflags.IOPL==0
|
---|
1166 | ;; then
|
---|
1167 | ;; if return to ring 0 (iretstack.new_cs & 3 == 0)
|
---|
1168 | ;; then
|
---|
1169 | ;; if iretstack.new_eflags.IF == 1 && iretstack.new_eflags.IOPL == 0
|
---|
1170 | ;; then
|
---|
1171 | ;; iretstack.new_cs |= 1
|
---|
1172 | ;; else
|
---|
1173 | ;; int 3
|
---|
1174 | ;; endif
|
---|
1175 | ;; uVMFlags &= ~X86_EFL_IF
|
---|
1176 | ;; iret
|
---|
1177 | ;; else
|
---|
1178 | ;; int 3
|
---|
1179 | ;;****************************************************
|
---|
1180 | ;;
|
---|
1181 | ; Stack:
|
---|
1182 | ;
|
---|
1183 | ; esp + 32 - GS (V86 only)
|
---|
1184 | ; esp + 28 - FS (V86 only)
|
---|
1185 | ; esp + 24 - DS (V86 only)
|
---|
1186 | ; esp + 20 - ES (V86 only)
|
---|
1187 | ; esp + 16 - SS (if transfer to outer ring)
|
---|
1188 | ; esp + 12 - ESP (if transfer to outer ring)
|
---|
1189 | ; esp + 8 - EFLAGS
|
---|
1190 | ; esp + 4 - CS
|
---|
1191 | ; esp - EIP
|
---|
1192 | ;;
|
---|
1193 | BEGINPROC PATMIretReplacement
|
---|
1194 | PATMIretStart:
|
---|
1195 | mov dword [ss:PATM_INTERRUPTFLAG], 0
|
---|
1196 | pushfd
|
---|
1197 |
|
---|
1198 | %ifdef PATM_LOG_PATCHIRET
|
---|
1199 | push eax
|
---|
1200 | push ecx
|
---|
1201 | push edx
|
---|
1202 | lea edx, dword [ss:esp+12+4] ;3 dwords + pushed flags -> iret eip
|
---|
1203 | mov eax, PATM_ACTION_LOG_IRET
|
---|
1204 | lock or dword [ss:PATM_PENDINGACTION], eax
|
---|
1205 | mov ecx, PATM_ACTION_MAGIC
|
---|
1206 | db 0fh, 0bh ; illegal instr (hardcoded assumption in PATMHandleIllegalInstrTrap)
|
---|
1207 | pop edx
|
---|
1208 | pop ecx
|
---|
1209 | pop eax
|
---|
1210 | %endif
|
---|
1211 |
|
---|
1212 | test dword [esp], X86_EFL_NT
|
---|
1213 | jnz near iret_fault1
|
---|
1214 |
|
---|
1215 | ; we can't do an iret to v86 code, as we run with CPL=1. The iret would attempt a protected mode iret and (most likely) fault.
|
---|
1216 | test dword [esp+12], X86_EFL_VM
|
---|
1217 | jnz near iret_return_to_v86
|
---|
1218 |
|
---|
1219 | ;;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
---|
1220 | ;;@todo: not correct for iret back to ring 2!!!!!
|
---|
1221 | ;;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
---|
1222 |
|
---|
1223 | test dword [esp+8], 2
|
---|
1224 | jnz iret_notring0
|
---|
1225 |
|
---|
1226 | test dword [esp+12], X86_EFL_IF
|
---|
1227 | jz near iret_clearIF
|
---|
1228 |
|
---|
1229 | ; force ring 1 CS RPL
|
---|
1230 | or dword [esp+8], 1
|
---|
1231 | iret_notring0:
|
---|
1232 |
|
---|
1233 | ; if interrupts are pending, then we must go back to the host context to handle them!
|
---|
1234 | ; Note: This is very important as pending pic interrupts can be overridden by apic interrupts if we don't check early enough (Fedora 5 boot)
|
---|
1235 | ; @@todo fix this properly, so we can dispatch pending interrupts in GC
|
---|
1236 | test dword [ss:PATM_VM_FORCEDACTIONS], VMCPU_FF_INTERRUPT_APIC | VMCPU_FF_INTERRUPT_PIC
|
---|
1237 | jz iret_continue
|
---|
1238 |
|
---|
1239 | ; Go to our hypervisor trap handler to dispatch the pending irq
|
---|
1240 | mov dword [ss:PATM_TEMP_EAX], eax
|
---|
1241 | mov dword [ss:PATM_TEMP_ECX], ecx
|
---|
1242 | mov dword [ss:PATM_TEMP_EDI], edi
|
---|
1243 | mov dword [ss:PATM_TEMP_RESTORE_FLAGS], PATM_RESTORE_EAX | PATM_RESTORE_ECX | PATM_RESTORE_EDI
|
---|
1244 | mov eax, PATM_ACTION_PENDING_IRQ_AFTER_IRET
|
---|
1245 | lock or dword [ss:PATM_PENDINGACTION], eax
|
---|
1246 | mov ecx, PATM_ACTION_MAGIC
|
---|
1247 | mov edi, PATM_CURINSTRADDR
|
---|
1248 |
|
---|
1249 | popfd
|
---|
1250 | db 0fh, 0bh ; illegal instr (hardcoded assumption in PATMHandleIllegalInstrTrap)
|
---|
1251 | ; does not return
|
---|
1252 |
|
---|
1253 | iret_continue :
|
---|
1254 | ; This section must *always* be executed (!!)
|
---|
1255 | ; Extract the IOPL from the return flags, save them to our virtual flags and
|
---|
1256 | ; put them back to zero
|
---|
1257 | ; @note we assume iretd doesn't fault!!!
|
---|
1258 | push eax
|
---|
1259 | mov eax, dword [esp+16]
|
---|
1260 | and eax, X86_EFL_IOPL
|
---|
1261 | and dword [ss:PATM_VMFLAGS], ~X86_EFL_IOPL
|
---|
1262 | or dword [ss:PATM_VMFLAGS], eax
|
---|
1263 | pop eax
|
---|
1264 | and dword [esp+12], ~X86_EFL_IOPL
|
---|
1265 |
|
---|
1266 | ; Set IF again; below we make sure this won't cause problems.
|
---|
1267 | or dword [ss:PATM_VMFLAGS], X86_EFL_IF
|
---|
1268 |
|
---|
1269 | ; make sure iret is executed fully (including the iret below; cli ... iret can otherwise be interrupted)
|
---|
1270 | mov dword [ss:PATM_INHIBITIRQADDR], PATM_CURINSTRADDR
|
---|
1271 |
|
---|
1272 | popfd
|
---|
1273 | mov dword [ss:PATM_INTERRUPTFLAG], 1
|
---|
1274 | iretd
|
---|
1275 | PATM_INT3
|
---|
1276 |
|
---|
1277 | iret_fault:
|
---|
1278 | popfd
|
---|
1279 | mov dword [ss:PATM_INTERRUPTFLAG], 1
|
---|
1280 | PATM_INT3
|
---|
1281 |
|
---|
1282 | iret_fault1:
|
---|
1283 | nop
|
---|
1284 | popfd
|
---|
1285 | mov dword [ss:PATM_INTERRUPTFLAG], 1
|
---|
1286 | PATM_INT3
|
---|
1287 |
|
---|
1288 | iret_clearIF:
|
---|
1289 | push dword [esp+4] ; eip to return to
|
---|
1290 | pushfd
|
---|
1291 | push eax
|
---|
1292 | push PATM_FIXUP
|
---|
1293 | DB 0E8h ; call
|
---|
1294 | DD PATM_IRET_FUNCTION
|
---|
1295 | add esp, 4 ; pushed address of jump table
|
---|
1296 |
|
---|
1297 | cmp eax, 0
|
---|
1298 | je near iret_fault3
|
---|
1299 |
|
---|
1300 | mov dword [esp+12+4], eax ; stored eip in iret frame
|
---|
1301 | pop eax
|
---|
1302 | popfd
|
---|
1303 | add esp, 4 ; pushed eip
|
---|
1304 |
|
---|
1305 | ; always ring 0 return -> change to ring 1 (CS in iret frame)
|
---|
1306 | or dword [esp+8], 1
|
---|
1307 |
|
---|
1308 | ; This section must *always* be executed (!!)
|
---|
1309 | ; Extract the IOPL from the return flags, save them to our virtual flags and
|
---|
1310 | ; put them back to zero
|
---|
1311 | push eax
|
---|
1312 | mov eax, dword [esp+16]
|
---|
1313 | and eax, X86_EFL_IOPL
|
---|
1314 | and dword [ss:PATM_VMFLAGS], ~X86_EFL_IOPL
|
---|
1315 | or dword [ss:PATM_VMFLAGS], eax
|
---|
1316 | pop eax
|
---|
1317 | and dword [esp+12], ~X86_EFL_IOPL
|
---|
1318 |
|
---|
1319 | ; Clear IF
|
---|
1320 | and dword [ss:PATM_VMFLAGS], ~X86_EFL_IF
|
---|
1321 | popfd
|
---|
1322 |
|
---|
1323 | ; the patched destination code will set PATM_INTERRUPTFLAG after the return!
|
---|
1324 | iretd
|
---|
1325 |
|
---|
1326 | iret_return_to_v86:
|
---|
1327 | test dword [esp+12], X86_EFL_IF
|
---|
1328 | jz iret_fault
|
---|
1329 |
|
---|
1330 | ; Go to our hypervisor trap handler to perform the iret to v86 code
|
---|
1331 | mov dword [ss:PATM_TEMP_EAX], eax
|
---|
1332 | mov dword [ss:PATM_TEMP_ECX], ecx
|
---|
1333 | mov dword [ss:PATM_TEMP_RESTORE_FLAGS], PATM_RESTORE_EAX | PATM_RESTORE_ECX
|
---|
1334 | mov eax, PATM_ACTION_DO_V86_IRET
|
---|
1335 | lock or dword [ss:PATM_PENDINGACTION], eax
|
---|
1336 | mov ecx, PATM_ACTION_MAGIC
|
---|
1337 |
|
---|
1338 | popfd
|
---|
1339 |
|
---|
1340 | db 0fh, 0bh ; illegal instr (hardcoded assumption in PATMHandleIllegalInstrTrap)
|
---|
1341 | ; does not return
|
---|
1342 |
|
---|
1343 |
|
---|
1344 | iret_fault3:
|
---|
1345 | pop eax
|
---|
1346 | popfd
|
---|
1347 | add esp, 4 ; pushed eip
|
---|
1348 | jmp iret_fault
|
---|
1349 |
|
---|
1350 | align 4
|
---|
1351 | PATMIretTable:
|
---|
1352 | DW PATM_MAX_JUMPTABLE_ENTRIES ; nrSlots
|
---|
1353 | DW 0 ; ulInsertPos
|
---|
1354 | DD 0 ; cAddresses
|
---|
1355 | TIMES PATCHJUMPTABLE_SIZE DB 0 ; lookup slots
|
---|
1356 |
|
---|
1357 | PATMIretEnd:
|
---|
1358 | ENDPROC PATMIretReplacement
|
---|
1359 |
|
---|
1360 | ; Patch record for 'iretd'
|
---|
1361 | GLOBALNAME PATMIretRecord
|
---|
1362 | RTCCPTR_DEF PATMIretStart
|
---|
1363 | DD 0
|
---|
1364 | DD 0
|
---|
1365 | DD 0
|
---|
1366 | DD PATMIretEnd- PATMIretStart
|
---|
1367 | %ifdef PATM_LOG_PATCHIRET
|
---|
1368 | DD 26
|
---|
1369 | %else
|
---|
1370 | DD 25
|
---|
1371 | %endif
|
---|
1372 | DD PATM_INTERRUPTFLAG
|
---|
1373 | DD 0
|
---|
1374 | %ifdef PATM_LOG_PATCHIRET
|
---|
1375 | DD PATM_PENDINGACTION
|
---|
1376 | DD 0
|
---|
1377 | %endif
|
---|
1378 | DD PATM_VM_FORCEDACTIONS
|
---|
1379 | DD 0
|
---|
1380 | DD PATM_TEMP_EAX
|
---|
1381 | DD 0
|
---|
1382 | DD PATM_TEMP_ECX
|
---|
1383 | DD 0
|
---|
1384 | DD PATM_TEMP_EDI
|
---|
1385 | DD 0
|
---|
1386 | DD PATM_TEMP_RESTORE_FLAGS
|
---|
1387 | DD 0
|
---|
1388 | DD PATM_PENDINGACTION
|
---|
1389 | DD 0
|
---|
1390 | DD PATM_CURINSTRADDR
|
---|
1391 | DD 0
|
---|
1392 | DD PATM_VMFLAGS
|
---|
1393 | DD 0
|
---|
1394 | DD PATM_VMFLAGS
|
---|
1395 | DD 0
|
---|
1396 | DD PATM_VMFLAGS
|
---|
1397 | DD 0
|
---|
1398 | DD PATM_INHIBITIRQADDR
|
---|
1399 | DD 0
|
---|
1400 | DD PATM_CURINSTRADDR
|
---|
1401 | DD 0
|
---|
1402 | DD PATM_INTERRUPTFLAG
|
---|
1403 | DD 0
|
---|
1404 | DD PATM_INTERRUPTFLAG
|
---|
1405 | DD 0
|
---|
1406 | DD PATM_INTERRUPTFLAG
|
---|
1407 | DD 0
|
---|
1408 | DD PATM_FIXUP
|
---|
1409 | DD PATMIretTable - PATMIretStart
|
---|
1410 | DD PATM_IRET_FUNCTION
|
---|
1411 | DD 0
|
---|
1412 | DD PATM_VMFLAGS
|
---|
1413 | DD 0
|
---|
1414 | DD PATM_VMFLAGS
|
---|
1415 | DD 0
|
---|
1416 | DD PATM_VMFLAGS
|
---|
1417 | DD 0
|
---|
1418 | DD PATM_TEMP_EAX
|
---|
1419 | DD 0
|
---|
1420 | DD PATM_TEMP_ECX
|
---|
1421 | DD 0
|
---|
1422 | DD PATM_TEMP_RESTORE_FLAGS
|
---|
1423 | DD 0
|
---|
1424 | DD PATM_PENDINGACTION
|
---|
1425 | DD 0
|
---|
1426 | DD 0ffffffffh
|
---|
1427 |
|
---|
1428 |
|
---|
1429 | ;
|
---|
1430 | ; global function for implementing 'iret' to code with IF cleared
|
---|
1431 | ;
|
---|
1432 | ; Caller is responsible for right stack layout
|
---|
1433 | ; + 16 original return address
|
---|
1434 | ; + 12 eflags
|
---|
1435 | ; + 8 eax
|
---|
1436 | ; + 4 Jump table address
|
---|
1437 | ;( + 0 return address )
|
---|
1438 | ;
|
---|
1439 | ; @note assumes PATM_INTERRUPTFLAG is zero
|
---|
1440 | ; @note assumes it can trash eax and eflags
|
---|
1441 | ;
|
---|
1442 | ; @returns eax=0 on failure
|
---|
1443 | ; otherwise return address in eax
|
---|
1444 | ;
|
---|
1445 | ; @note NEVER change this without bumping the SSM version
|
---|
1446 | align 32
|
---|
1447 | BEGINPROC PATMIretFunction
|
---|
1448 | PATMIretFunction_Start:
|
---|
1449 | push ecx
|
---|
1450 | push edx
|
---|
1451 | push edi
|
---|
1452 |
|
---|
1453 | ; Event order:
|
---|
1454 | ; 1) Check if the return patch address can be found in the lookup table
|
---|
1455 | ; 2) Query return patch address from the hypervisor
|
---|
1456 |
|
---|
1457 | ; 1) Check if the return patch address can be found in the lookup table
|
---|
1458 | mov edx, dword [esp+12+16] ; pushed target address
|
---|
1459 |
|
---|
1460 | xor eax, eax ; default result -> nothing found
|
---|
1461 | mov edi, dword [esp+12+4] ; jump table
|
---|
1462 | mov ecx, [ss:edi + PATCHJUMPTABLE.cAddresses]
|
---|
1463 | cmp ecx, 0
|
---|
1464 | je near PATMIretFunction_AskHypervisor
|
---|
1465 |
|
---|
1466 | PATMIretFunction_SearchStart:
|
---|
1467 | cmp [ss:edi + PATCHJUMPTABLE.Slot_pInstrGC + eax*8], edx ; edx = GC address to search for
|
---|
1468 | je near PATMIretFunction_SearchHit
|
---|
1469 | inc eax
|
---|
1470 | cmp eax, ecx
|
---|
1471 | jl near PATMIretFunction_SearchStart
|
---|
1472 |
|
---|
1473 | PATMIretFunction_AskHypervisor:
|
---|
1474 | ; 2) Query return patch address from the hypervisor
|
---|
1475 | ; @todo private ugly interface, since we have nothing generic at the moment
|
---|
1476 | lock or dword [ss:PATM_PENDINGACTION], PATM_ACTION_LOOKUP_ADDRESS
|
---|
1477 | mov eax, PATM_ACTION_LOOKUP_ADDRESS
|
---|
1478 | mov ecx, PATM_ACTION_MAGIC
|
---|
1479 | mov edi, dword [esp+12+4] ; jump table address
|
---|
1480 | mov edx, dword [esp+12+16] ; original return address
|
---|
1481 | db 0fh, 0bh ; illegal instr (hardcoded assumption in PATMHandleIllegalInstrTrap)
|
---|
1482 | jmp near PATMIretFunction_SearchEnd
|
---|
1483 |
|
---|
1484 | PATMIretFunction_SearchHit:
|
---|
1485 | mov eax, [ss:edi + PATCHJUMPTABLE.Slot_pRelPatchGC + eax*8] ; found a match!
|
---|
1486 | ;@note can be zero, so the next check is required!!
|
---|
1487 |
|
---|
1488 | PATMIretFunction_SearchEnd:
|
---|
1489 | cmp eax, 0
|
---|
1490 | jz PATMIretFunction_Failure
|
---|
1491 |
|
---|
1492 | add eax, PATM_PATCHBASE
|
---|
1493 |
|
---|
1494 | pop edi
|
---|
1495 | pop edx
|
---|
1496 | pop ecx
|
---|
1497 | ret
|
---|
1498 |
|
---|
1499 | PATMIretFunction_Failure:
|
---|
1500 | ;signal error
|
---|
1501 | xor eax, eax
|
---|
1502 | pop edi
|
---|
1503 | pop edx
|
---|
1504 | pop ecx
|
---|
1505 | ret
|
---|
1506 |
|
---|
1507 | PATMIretFunction_End:
|
---|
1508 | ENDPROC PATMIretFunction
|
---|
1509 |
|
---|
1510 | GLOBALNAME PATMIretFunctionRecord
|
---|
1511 | RTCCPTR_DEF PATMIretFunction_Start
|
---|
1512 | DD 0
|
---|
1513 | DD 0
|
---|
1514 | DD 0
|
---|
1515 | DD PATMIretFunction_End - PATMIretFunction_Start
|
---|
1516 | DD 2
|
---|
1517 | DD PATM_PENDINGACTION
|
---|
1518 | DD 0
|
---|
1519 | DD PATM_PATCHBASE
|
---|
1520 | DD 0
|
---|
1521 | DD 0ffffffffh
|
---|
1522 |
|
---|
1523 |
|
---|
1524 | align 32 ; yasm / nasm diff - remove me!
|
---|
1525 | BEGINPROC PATMCpuidReplacement
|
---|
1526 | PATMCpuidStart:
|
---|
1527 | mov dword [ss:PATM_INTERRUPTFLAG], 0
|
---|
1528 | pushf
|
---|
1529 |
|
---|
1530 | cmp eax, PATM_CPUID_STD_MAX
|
---|
1531 | jb cpuid_std
|
---|
1532 | cmp eax, 0x80000000
|
---|
1533 | jb cpuid_def
|
---|
1534 | cmp eax, PATM_CPUID_EXT_MAX
|
---|
1535 | jb cpuid_ext
|
---|
1536 | cmp eax, 0xc0000000
|
---|
1537 | jb cpuid_def
|
---|
1538 | cmp eax, PATM_CPUID_CENTAUR_MAX
|
---|
1539 | jb cpuid_centaur
|
---|
1540 |
|
---|
1541 | ; Dirty assumptions in patmCorrectFixup about the pointer fixup order!!!!
|
---|
1542 | cpuid_def:
|
---|
1543 | mov eax, PATM_CPUID_DEF_PTR
|
---|
1544 | jmp cpuid_fetch
|
---|
1545 |
|
---|
1546 | cpuid_std:
|
---|
1547 | mov edx, PATM_CPUID_STD_PTR
|
---|
1548 | jmp cpuid_calc
|
---|
1549 |
|
---|
1550 | cpuid_ext:
|
---|
1551 | and eax, 0ffh ; strictly speaking not necessary.
|
---|
1552 | mov edx, PATM_CPUID_EXT_PTR
|
---|
1553 | jmp cpuid_calc
|
---|
1554 |
|
---|
1555 | cpuid_centaur:
|
---|
1556 | and eax, 0ffh ; strictly speaking not necessary.
|
---|
1557 | mov edx, PATM_CPUID_CENTAUR_PTR
|
---|
1558 |
|
---|
1559 | cpuid_calc:
|
---|
1560 | lea eax, [ss:eax * 4] ; 4 entries...
|
---|
1561 | lea eax, [ss:eax * 4] ; 4 bytes each
|
---|
1562 | add eax, edx
|
---|
1563 |
|
---|
1564 | cpuid_fetch:
|
---|
1565 | mov edx, [ss:eax + 12] ; CPUMCPUID layout assumptions!
|
---|
1566 | mov ecx, [ss:eax + 8]
|
---|
1567 | mov ebx, [ss:eax + 4]
|
---|
1568 | mov eax, [ss:eax]
|
---|
1569 |
|
---|
1570 | popf
|
---|
1571 | mov dword [ss:PATM_INTERRUPTFLAG], 1
|
---|
1572 |
|
---|
1573 | PATMCpuidEnd:
|
---|
1574 | ENDPROC PATMCpuidReplacement
|
---|
1575 |
|
---|
1576 | ; Patch record for 'cpuid'
|
---|
1577 | GLOBALNAME PATMCpuidRecord
|
---|
1578 | RTCCPTR_DEF PATMCpuidStart
|
---|
1579 | DD 0
|
---|
1580 | DD 0
|
---|
1581 | DD 0
|
---|
1582 | DD PATMCpuidEnd- PATMCpuidStart
|
---|
1583 | DD 9
|
---|
1584 | DD PATM_INTERRUPTFLAG
|
---|
1585 | DD 0
|
---|
1586 | DD PATM_CPUID_STD_MAX
|
---|
1587 | DD 0
|
---|
1588 | DD PATM_CPUID_EXT_MAX
|
---|
1589 | DD 0
|
---|
1590 | DD PATM_CPUID_CENTAUR_MAX
|
---|
1591 | DD 0
|
---|
1592 | DD PATM_CPUID_DEF_PTR
|
---|
1593 | DD 0
|
---|
1594 | DD PATM_CPUID_STD_PTR
|
---|
1595 | DD 0
|
---|
1596 | DD PATM_CPUID_EXT_PTR
|
---|
1597 | DD 0
|
---|
1598 | DD PATM_CPUID_CENTAUR_PTR
|
---|
1599 | DD 0
|
---|
1600 | DD PATM_INTERRUPTFLAG
|
---|
1601 | DD 0
|
---|
1602 | DD 0ffffffffh
|
---|
1603 |
|
---|
1604 |
|
---|
1605 | BEGINPROC PATMJEcxReplacement
|
---|
1606 | PATMJEcxStart:
|
---|
1607 | mov dword [ss:PATM_INTERRUPTFLAG], 0
|
---|
1608 | pushfd
|
---|
1609 | PATMJEcxSizeOverride:
|
---|
1610 | DB 0x90 ; nop
|
---|
1611 | cmp ecx, dword 0 ; yasm / nasm dword
|
---|
1612 | jnz PATMJEcxContinue
|
---|
1613 |
|
---|
1614 | popfd
|
---|
1615 | mov dword [ss:PATM_INTERRUPTFLAG], 1
|
---|
1616 | DB 0xE9
|
---|
1617 | PATMJEcxJump:
|
---|
1618 | DD PATM_JUMPDELTA
|
---|
1619 |
|
---|
1620 | PATMJEcxContinue:
|
---|
1621 | popfd
|
---|
1622 | mov dword [ss:PATM_INTERRUPTFLAG], 1
|
---|
1623 | PATMJEcxEnd:
|
---|
1624 | ENDPROC PATMJEcxReplacement
|
---|
1625 |
|
---|
1626 | ; Patch record for 'JEcx'
|
---|
1627 | GLOBALNAME PATMJEcxRecord
|
---|
1628 | RTCCPTR_DEF PATMJEcxStart
|
---|
1629 | DD 0
|
---|
1630 | DD PATMJEcxJump - PATMJEcxStart
|
---|
1631 | DD PATMJEcxSizeOverride - PATMJEcxStart
|
---|
1632 | DD PATMJEcxEnd- PATMJEcxStart
|
---|
1633 | DD 3
|
---|
1634 | DD PATM_INTERRUPTFLAG
|
---|
1635 | DD 0
|
---|
1636 | DD PATM_INTERRUPTFLAG
|
---|
1637 | DD 0
|
---|
1638 | DD PATM_INTERRUPTFLAG
|
---|
1639 | DD 0
|
---|
1640 | DD 0ffffffffh
|
---|
1641 |
|
---|
1642 | align 32; yasm / nasm diffing. remove me!
|
---|
1643 | BEGINPROC PATMLoopReplacement
|
---|
1644 | PATMLoopStart:
|
---|
1645 | mov dword [ss:PATM_INTERRUPTFLAG], 0
|
---|
1646 | pushfd
|
---|
1647 | PATMLoopSizeOverride:
|
---|
1648 | DB 0x90 ; nop
|
---|
1649 | dec ecx
|
---|
1650 | jz PATMLoopContinue
|
---|
1651 |
|
---|
1652 | popfd
|
---|
1653 | mov dword [ss:PATM_INTERRUPTFLAG], 1
|
---|
1654 | DB 0xE9
|
---|
1655 | PATMLoopJump:
|
---|
1656 | DD PATM_JUMPDELTA
|
---|
1657 |
|
---|
1658 | PATMLoopContinue:
|
---|
1659 | popfd
|
---|
1660 | mov dword [ss:PATM_INTERRUPTFLAG], 1
|
---|
1661 | PATMLoopEnd:
|
---|
1662 | ENDPROC PATMLoopReplacement
|
---|
1663 |
|
---|
1664 | ; Patch record for 'Loop'
|
---|
1665 | GLOBALNAME PATMLoopRecord
|
---|
1666 | RTCCPTR_DEF PATMLoopStart
|
---|
1667 | DD 0
|
---|
1668 | DD PATMLoopJump - PATMLoopStart
|
---|
1669 | DD PATMLoopSizeOverride - PATMLoopStart
|
---|
1670 | DD PATMLoopEnd- PATMLoopStart
|
---|
1671 | DD 3
|
---|
1672 | DD PATM_INTERRUPTFLAG
|
---|
1673 | DD 0
|
---|
1674 | DD PATM_INTERRUPTFLAG
|
---|
1675 | DD 0
|
---|
1676 | DD PATM_INTERRUPTFLAG
|
---|
1677 | DD 0
|
---|
1678 | DD 0ffffffffh
|
---|
1679 |
|
---|
1680 | BEGINPROC PATMLoopZReplacement
|
---|
1681 | PATMLoopZStart:
|
---|
1682 | ; jump if ZF=1 AND (E)CX != 0
|
---|
1683 |
|
---|
1684 | mov dword [ss:PATM_INTERRUPTFLAG], 0
|
---|
1685 | jnz PATMLoopZEnd
|
---|
1686 | pushfd
|
---|
1687 | PATMLoopZSizeOverride:
|
---|
1688 | DB 0x90 ; nop
|
---|
1689 | dec ecx
|
---|
1690 | jz PATMLoopZContinue
|
---|
1691 |
|
---|
1692 | popfd
|
---|
1693 | mov dword [ss:PATM_INTERRUPTFLAG], 1
|
---|
1694 | DB 0xE9
|
---|
1695 | PATMLoopZJump:
|
---|
1696 | DD PATM_JUMPDELTA
|
---|
1697 |
|
---|
1698 | PATMLoopZContinue:
|
---|
1699 | popfd
|
---|
1700 | mov dword [ss:PATM_INTERRUPTFLAG], 1
|
---|
1701 | PATMLoopZEnd:
|
---|
1702 | ENDPROC PATMLoopZReplacement
|
---|
1703 |
|
---|
1704 | ; Patch record for 'Loopz'
|
---|
1705 | GLOBALNAME PATMLoopZRecord
|
---|
1706 | RTCCPTR_DEF PATMLoopZStart
|
---|
1707 | DD 0
|
---|
1708 | DD PATMLoopZJump - PATMLoopZStart
|
---|
1709 | DD PATMLoopZSizeOverride - PATMLoopZStart
|
---|
1710 | DD PATMLoopZEnd- PATMLoopZStart
|
---|
1711 | DD 3
|
---|
1712 | DD PATM_INTERRUPTFLAG
|
---|
1713 | DD 0
|
---|
1714 | DD PATM_INTERRUPTFLAG
|
---|
1715 | DD 0
|
---|
1716 | DD PATM_INTERRUPTFLAG
|
---|
1717 | DD 0
|
---|
1718 | DD 0ffffffffh
|
---|
1719 |
|
---|
1720 |
|
---|
1721 | BEGINPROC PATMLoopNZReplacement
|
---|
1722 | PATMLoopNZStart:
|
---|
1723 | ; jump if ZF=0 AND (E)CX != 0
|
---|
1724 |
|
---|
1725 | mov dword [ss:PATM_INTERRUPTFLAG], 0
|
---|
1726 | jz PATMLoopNZEnd
|
---|
1727 | pushfd
|
---|
1728 | PATMLoopNZSizeOverride:
|
---|
1729 | DB 0x90 ; nop
|
---|
1730 | dec ecx
|
---|
1731 | jz PATMLoopNZContinue
|
---|
1732 |
|
---|
1733 | popfd
|
---|
1734 | mov dword [ss:PATM_INTERRUPTFLAG], 1
|
---|
1735 | DB 0xE9
|
---|
1736 | PATMLoopNZJump:
|
---|
1737 | DD PATM_JUMPDELTA
|
---|
1738 |
|
---|
1739 | PATMLoopNZContinue:
|
---|
1740 | popfd
|
---|
1741 | mov dword [ss:PATM_INTERRUPTFLAG], 1
|
---|
1742 | PATMLoopNZEnd:
|
---|
1743 | ENDPROC PATMLoopNZReplacement
|
---|
1744 |
|
---|
1745 | ; Patch record for 'LoopNZ'
|
---|
1746 | GLOBALNAME PATMLoopNZRecord
|
---|
1747 | RTCCPTR_DEF PATMLoopNZStart
|
---|
1748 | DD 0
|
---|
1749 | DD PATMLoopNZJump - PATMLoopNZStart
|
---|
1750 | DD PATMLoopNZSizeOverride - PATMLoopNZStart
|
---|
1751 | DD PATMLoopNZEnd- PATMLoopNZStart
|
---|
1752 | DD 3
|
---|
1753 | DD PATM_INTERRUPTFLAG
|
---|
1754 | DD 0
|
---|
1755 | DD PATM_INTERRUPTFLAG
|
---|
1756 | DD 0
|
---|
1757 | DD PATM_INTERRUPTFLAG
|
---|
1758 | DD 0
|
---|
1759 | DD 0ffffffffh
|
---|
1760 |
|
---|
1761 | align 32
|
---|
1762 | ; Global patch function for indirect calls
|
---|
1763 | ; Caller is responsible for clearing PATM_INTERRUPTFLAG and doing:
|
---|
1764 | ; + 20 push [pTargetGC]
|
---|
1765 | ; + 16 pushfd
|
---|
1766 | ; + 12 push [JumpTableAddress]
|
---|
1767 | ; + 8 push [PATMRelReturnAddress]
|
---|
1768 | ; + 4 push [GuestReturnAddress]
|
---|
1769 | ;( + 0 return address )
|
---|
1770 | ;
|
---|
1771 | ; @note NEVER change this without bumping the SSM version
|
---|
1772 | BEGINPROC PATMLookupAndCall
|
---|
1773 | PATMLookupAndCallStart:
|
---|
1774 | push eax
|
---|
1775 | push edx
|
---|
1776 | push edi
|
---|
1777 | push ecx
|
---|
1778 |
|
---|
1779 | mov eax, dword [esp+16+4] ; guest return address
|
---|
1780 | mov dword [ss:PATM_CALL_RETURN_ADDR], eax ; temporary storage
|
---|
1781 |
|
---|
1782 | mov edx, dword [esp+16+20] ; pushed target address
|
---|
1783 |
|
---|
1784 | xor eax, eax ; default result -> nothing found
|
---|
1785 | mov edi, dword [esp+16+12] ; jump table
|
---|
1786 | mov ecx, [ss:edi + PATCHJUMPTABLE.cAddresses]
|
---|
1787 | cmp ecx, 0
|
---|
1788 | je near PATMLookupAndCall_QueryPATM
|
---|
1789 |
|
---|
1790 | PATMLookupAndCall_SearchStart:
|
---|
1791 | cmp [ss:edi + PATCHJUMPTABLE.Slot_pInstrGC + eax*8], edx ; edx = GC address to search for
|
---|
1792 | je near PATMLookupAndCall_SearchHit
|
---|
1793 | inc eax
|
---|
1794 | cmp eax, ecx
|
---|
1795 | jl near PATMLookupAndCall_SearchStart
|
---|
1796 |
|
---|
1797 | PATMLookupAndCall_QueryPATM:
|
---|
1798 | ; nothing found -> let our trap handler try to find it
|
---|
1799 | ; @todo private ugly interface, since we have nothing generic at the moment
|
---|
1800 | lock or dword [ss:PATM_PENDINGACTION], PATM_ACTION_LOOKUP_ADDRESS
|
---|
1801 | mov eax, PATM_ACTION_LOOKUP_ADDRESS
|
---|
1802 | mov ecx, PATM_ACTION_MAGIC
|
---|
1803 | ; edx = GC address to find
|
---|
1804 | ; edi = jump table address
|
---|
1805 | db 0fh, 0bh ; illegal instr (hardcoded assumption in PATMHandleIllegalInstrTrap)
|
---|
1806 |
|
---|
1807 | jmp near PATMLookupAndCall_SearchEnd
|
---|
1808 |
|
---|
1809 | PATMLookupAndCall_Failure:
|
---|
1810 | ; return to caller; it must raise an error, due to patch to guest address translation (remember that there's only one copy of this code block).
|
---|
1811 | pop ecx
|
---|
1812 | pop edi
|
---|
1813 | pop edx
|
---|
1814 | pop eax
|
---|
1815 | ret
|
---|
1816 |
|
---|
1817 | PATMLookupAndCall_SearchHit:
|
---|
1818 | mov eax, [ss:edi + PATCHJUMPTABLE.Slot_pRelPatchGC + eax*8] ; found a match!
|
---|
1819 |
|
---|
1820 | ;@note can be zero, so the next check is required!!
|
---|
1821 |
|
---|
1822 | PATMLookupAndCall_SearchEnd:
|
---|
1823 | cmp eax, 0
|
---|
1824 | je near PATMLookupAndCall_Failure
|
---|
1825 |
|
---|
1826 | mov ecx, eax ; ECX = target address (relative!)
|
---|
1827 | add ecx, PATM_PATCHBASE ; Make it absolute
|
---|
1828 |
|
---|
1829 | mov edx, dword PATM_STACKPTR
|
---|
1830 | cmp dword [ss:edx], PATM_STACK_SIZE
|
---|
1831 | ja near PATMLookupAndCall_Failure ; should never happen actually!!!
|
---|
1832 | cmp dword [ss:edx], 0
|
---|
1833 | je near PATMLookupAndCall_Failure ; no more room
|
---|
1834 |
|
---|
1835 | ; save the patch return address on our private stack
|
---|
1836 | sub dword [ss:edx], 4 ; sizeof(RTGCPTR)
|
---|
1837 | mov eax, dword PATM_STACKBASE
|
---|
1838 | add eax, dword [ss:edx] ; stack base + stack position
|
---|
1839 | mov edi, dword [esp+16+8] ; PATM return address
|
---|
1840 | mov dword [ss:eax], edi ; relative address of patch return (instruction following this block)
|
---|
1841 |
|
---|
1842 | ; save the original return address as well (checked by ret to make sure the guest hasn't messed around with the stack)
|
---|
1843 | mov edi, dword PATM_STACKBASE_GUEST
|
---|
1844 | add edi, dword [ss:edx] ; stack base (guest) + stack position
|
---|
1845 | mov eax, dword [esp+16+4] ; guest return address
|
---|
1846 | mov dword [ss:edi], eax
|
---|
1847 |
|
---|
1848 | mov dword [ss:PATM_CALL_PATCH_TARGET_ADDR], ecx ; temporarily store the target address
|
---|
1849 | pop ecx
|
---|
1850 | pop edi
|
---|
1851 | pop edx
|
---|
1852 | pop eax
|
---|
1853 | add esp, 24 ; parameters + return address pushed by caller (changes the flags, but that shouldn't matter)
|
---|
1854 |
|
---|
1855 | %ifdef PATM_LOG_PATCHINSTR
|
---|
1856 | push eax
|
---|
1857 | push ecx
|
---|
1858 | push edx
|
---|
1859 | lea edx, [esp + 12 - 4] ; stack address to store return address
|
---|
1860 | lock or dword [ss:PATM_PENDINGACTION], PATM_ACTION_LOG_CALL
|
---|
1861 | mov eax, PATM_ACTION_LOG_CALL
|
---|
1862 | mov ecx, PATM_ACTION_MAGIC
|
---|
1863 | db 0fh, 0bh ; illegal instr (hardcoded assumption in PATMHandleIllegalInstrTrap)
|
---|
1864 | pop edx
|
---|
1865 | pop ecx
|
---|
1866 | pop eax
|
---|
1867 | %endif
|
---|
1868 |
|
---|
1869 | push dword [ss:PATM_CALL_RETURN_ADDR] ; push original guest return address
|
---|
1870 |
|
---|
1871 | ; the called function will set PATM_INTERRUPTFLAG (!!)
|
---|
1872 | jmp dword [ss:PATM_CALL_PATCH_TARGET_ADDR]
|
---|
1873 |
|
---|
1874 | PATMLookupAndCallEnd:
|
---|
1875 | ; returning here -> do not add code here or after the jmp!!!!!
|
---|
1876 | ENDPROC PATMLookupAndCall
|
---|
1877 |
|
---|
1878 | ; Patch record for indirect calls and jumps
|
---|
1879 | GLOBALNAME PATMLookupAndCallRecord
|
---|
1880 | RTCCPTR_DEF PATMLookupAndCallStart
|
---|
1881 | DD 0
|
---|
1882 | DD 0
|
---|
1883 | DD 0
|
---|
1884 | DD PATMLookupAndCallEnd - PATMLookupAndCallStart
|
---|
1885 | %ifdef PATM_LOG_PATCHINSTR
|
---|
1886 | DD 10
|
---|
1887 | %else
|
---|
1888 | DD 9
|
---|
1889 | %endif
|
---|
1890 | DD PATM_CALL_RETURN_ADDR
|
---|
1891 | DD 0
|
---|
1892 | DD PATM_PENDINGACTION
|
---|
1893 | DD 0
|
---|
1894 | DD PATM_PATCHBASE
|
---|
1895 | DD 0
|
---|
1896 | DD PATM_STACKPTR
|
---|
1897 | DD 0
|
---|
1898 | DD PATM_STACKBASE
|
---|
1899 | DD 0
|
---|
1900 | DD PATM_STACKBASE_GUEST
|
---|
1901 | DD 0
|
---|
1902 | DD PATM_CALL_PATCH_TARGET_ADDR
|
---|
1903 | DD 0
|
---|
1904 | %ifdef PATM_LOG_PATCHINSTR
|
---|
1905 | DD PATM_PENDINGACTION
|
---|
1906 | DD 0
|
---|
1907 | %endif
|
---|
1908 | DD PATM_CALL_RETURN_ADDR
|
---|
1909 | DD 0
|
---|
1910 | DD PATM_CALL_PATCH_TARGET_ADDR
|
---|
1911 | DD 0
|
---|
1912 | DD 0ffffffffh
|
---|
1913 |
|
---|
1914 |
|
---|
1915 | align 32
|
---|
1916 | ; Global patch function for indirect jumps
|
---|
1917 | ; Caller is responsible for clearing PATM_INTERRUPTFLAG and doing:
|
---|
1918 | ; + 8 push [pTargetGC]
|
---|
1919 | ; + 4 push [JumpTableAddress]
|
---|
1920 | ;( + 0 return address )
|
---|
1921 | ; And saving eflags in PATM_TEMP_EFLAGS
|
---|
1922 | ;
|
---|
1923 | ; @note NEVER change this without bumping the SSM version
|
---|
1924 | BEGINPROC PATMLookupAndJump
|
---|
1925 | PATMLookupAndJumpStart:
|
---|
1926 | push eax
|
---|
1927 | push edx
|
---|
1928 | push edi
|
---|
1929 | push ecx
|
---|
1930 |
|
---|
1931 | mov edx, dword [esp+16+8] ; pushed target address
|
---|
1932 |
|
---|
1933 | xor eax, eax ; default result -> nothing found
|
---|
1934 | mov edi, dword [esp+16+4] ; jump table
|
---|
1935 | mov ecx, [ss:edi + PATCHJUMPTABLE.cAddresses]
|
---|
1936 | cmp ecx, 0
|
---|
1937 | je near PATMLookupAndJump_QueryPATM
|
---|
1938 |
|
---|
1939 | PATMLookupAndJump_SearchStart:
|
---|
1940 | cmp [ss:edi + PATCHJUMPTABLE.Slot_pInstrGC + eax*8], edx ; edx = GC address to search for
|
---|
1941 | je near PATMLookupAndJump_SearchHit
|
---|
1942 | inc eax
|
---|
1943 | cmp eax, ecx
|
---|
1944 | jl near PATMLookupAndJump_SearchStart
|
---|
1945 |
|
---|
1946 | PATMLookupAndJump_QueryPATM:
|
---|
1947 | ; nothing found -> let our trap handler try to find it
|
---|
1948 | ; @todo private ugly interface, since we have nothing generic at the moment
|
---|
1949 | lock or dword [ss:PATM_PENDINGACTION], PATM_ACTION_LOOKUP_ADDRESS
|
---|
1950 | mov eax, PATM_ACTION_LOOKUP_ADDRESS
|
---|
1951 | mov ecx, PATM_ACTION_MAGIC
|
---|
1952 | ; edx = GC address to find
|
---|
1953 | ; edi = jump table address
|
---|
1954 | db 0fh, 0bh ; illegal instr (hardcoded assumption in PATMHandleIllegalInstrTrap)
|
---|
1955 |
|
---|
1956 | jmp near PATMLookupAndJump_SearchEnd
|
---|
1957 |
|
---|
1958 | PATMLookupAndJump_Failure:
|
---|
1959 | ; return to caller; it must raise an error, due to patch to guest address translation (remember that there's only one copy of this code block).
|
---|
1960 | pop ecx
|
---|
1961 | pop edi
|
---|
1962 | pop edx
|
---|
1963 | pop eax
|
---|
1964 | ret
|
---|
1965 |
|
---|
1966 | PATMLookupAndJump_SearchHit:
|
---|
1967 | mov eax, [ss:edi + PATCHJUMPTABLE.Slot_pRelPatchGC + eax*8] ; found a match!
|
---|
1968 |
|
---|
1969 | ;@note can be zero, so the next check is required!!
|
---|
1970 |
|
---|
1971 | PATMLookupAndJump_SearchEnd:
|
---|
1972 | cmp eax, 0
|
---|
1973 | je near PATMLookupAndJump_Failure
|
---|
1974 |
|
---|
1975 | mov ecx, eax ; ECX = target address (relative!)
|
---|
1976 | add ecx, PATM_PATCHBASE ; Make it absolute
|
---|
1977 |
|
---|
1978 | ; save jump patch target
|
---|
1979 | mov dword [ss:PATM_TEMP_EAX], ecx
|
---|
1980 | pop ecx
|
---|
1981 | pop edi
|
---|
1982 | pop edx
|
---|
1983 | pop eax
|
---|
1984 | add esp, 12 ; parameters + return address pushed by caller
|
---|
1985 | ; restore flags (just to be sure)
|
---|
1986 | push dword [ss:PATM_TEMP_EFLAGS]
|
---|
1987 | popfd
|
---|
1988 |
|
---|
1989 | ; the jump destination will set PATM_INTERRUPTFLAG (!!)
|
---|
1990 | jmp dword [ss:PATM_TEMP_EAX] ; call duplicated patch destination address
|
---|
1991 |
|
---|
1992 | PATMLookupAndJumpEnd:
|
---|
1993 | ENDPROC PATMLookupAndJump
|
---|
1994 |
|
---|
1995 | ; Patch record for indirect calls and jumps
|
---|
1996 | GLOBALNAME PATMLookupAndJumpRecord
|
---|
1997 | RTCCPTR_DEF PATMLookupAndJumpStart
|
---|
1998 | DD 0
|
---|
1999 | DD 0
|
---|
2000 | DD 0
|
---|
2001 | DD PATMLookupAndJumpEnd - PATMLookupAndJumpStart
|
---|
2002 | DD 5
|
---|
2003 | DD PATM_PENDINGACTION
|
---|
2004 | DD 0
|
---|
2005 | DD PATM_PATCHBASE
|
---|
2006 | DD 0
|
---|
2007 | DD PATM_TEMP_EAX
|
---|
2008 | DD 0
|
---|
2009 | DD PATM_TEMP_EFLAGS
|
---|
2010 | DD 0
|
---|
2011 | DD PATM_TEMP_EAX
|
---|
2012 | DD 0
|
---|
2013 | DD 0ffffffffh
|
---|
2014 |
|
---|
2015 |
|
---|
2016 |
|
---|
2017 |
|
---|
2018 | align 32
|
---|
2019 | ; Patch function for static calls
|
---|
2020 | ; @note static calls have only one lookup slot!
|
---|
2021 | ; Caller is responsible for clearing PATM_INTERRUPTFLAG and adding:
|
---|
2022 | ; push [pTargetGC]
|
---|
2023 | ;
|
---|
2024 | BEGINPROC PATMCall
|
---|
2025 | PATMCallStart:
|
---|
2026 | pushfd
|
---|
2027 | push PATM_FIXUP ; fixup for jump table below
|
---|
2028 | push PATM_PATCHNEXTBLOCK
|
---|
2029 | push PATM_RETURNADDR
|
---|
2030 | DB 0E8h ; call
|
---|
2031 | DD PATM_LOOKUP_AND_CALL_FUNCTION
|
---|
2032 | ; we only return in case of a failure
|
---|
2033 | add esp, 12 ; pushed address of jump table
|
---|
2034 | popfd
|
---|
2035 | add esp, 4 ; pushed by caller (changes the flags, but that shouldn't matter (@todo))
|
---|
2036 | mov dword [ss:PATM_INTERRUPTFLAG], 1
|
---|
2037 | PATM_INT3
|
---|
2038 | %ifdef DEBUG
|
---|
2039 | ; for disassembly
|
---|
2040 | jmp PATMCallEnd
|
---|
2041 | %endif
|
---|
2042 |
|
---|
2043 | align 4
|
---|
2044 | PATMCallTable:
|
---|
2045 | DW 1 ; nrSlots
|
---|
2046 | DW 0 ; ulInsertPos
|
---|
2047 | DD 0 ; cAddresses
|
---|
2048 | TIMES PATCHDIRECTJUMPTABLE_SIZE DB 0 ; only one lookup slot
|
---|
2049 |
|
---|
2050 | PATMCallEnd:
|
---|
2051 | ; returning here -> do not add code here or after the jmp!!!!!
|
---|
2052 | ENDPROC PATMCall
|
---|
2053 |
|
---|
2054 | ; Patch record for direct calls
|
---|
2055 | GLOBALNAME PATMCallRecord
|
---|
2056 | RTCCPTR_DEF PATMCallStart
|
---|
2057 | DD 0
|
---|
2058 | DD 0
|
---|
2059 | DD 0
|
---|
2060 | DD PATMCallEnd - PATMCallStart
|
---|
2061 | DD 5
|
---|
2062 | DD PATM_FIXUP
|
---|
2063 | DD PATMCallTable - PATMCallStart
|
---|
2064 | DD PATM_PATCHNEXTBLOCK
|
---|
2065 | DD 0
|
---|
2066 | DD PATM_RETURNADDR
|
---|
2067 | DD 0
|
---|
2068 | DD PATM_LOOKUP_AND_CALL_FUNCTION
|
---|
2069 | DD 0
|
---|
2070 | DD PATM_INTERRUPTFLAG
|
---|
2071 | DD 0
|
---|
2072 | DD 0ffffffffh
|
---|
2073 |
|
---|
2074 |
|
---|
2075 | align 32
|
---|
2076 | ; Patch function for indirect calls
|
---|
2077 | ; Caller is responsible for clearing PATM_INTERRUPTFLAG and adding:
|
---|
2078 | ; push [pTargetGC]
|
---|
2079 | ;
|
---|
2080 | BEGINPROC PATMCallIndirect
|
---|
2081 | PATMCallIndirectStart:
|
---|
2082 | pushfd
|
---|
2083 | push PATM_FIXUP ; fixup for jump table below
|
---|
2084 | push PATM_PATCHNEXTBLOCK
|
---|
2085 | push PATM_RETURNADDR
|
---|
2086 | DB 0E8h ; call
|
---|
2087 | DD PATM_LOOKUP_AND_CALL_FUNCTION
|
---|
2088 | ; we only return in case of a failure
|
---|
2089 | add esp, 12 ; pushed address of jump table
|
---|
2090 | popfd
|
---|
2091 | add esp, 4 ; pushed by caller (changes the flags, but that shouldn't matter (@todo))
|
---|
2092 | mov dword [ss:PATM_INTERRUPTFLAG], 1
|
---|
2093 | PATM_INT3
|
---|
2094 | %ifdef DEBUG
|
---|
2095 | ; for disassembly
|
---|
2096 | jmp PATMCallIndirectEnd
|
---|
2097 | %endif
|
---|
2098 |
|
---|
2099 | align 4
|
---|
2100 | PATMCallIndirectTable:
|
---|
2101 | DW PATM_MAX_JUMPTABLE_ENTRIES ; nrSlots
|
---|
2102 | DW 0 ; ulInsertPos
|
---|
2103 | DD 0 ; cAddresses
|
---|
2104 | TIMES PATCHJUMPTABLE_SIZE DB 0 ; lookup slots
|
---|
2105 |
|
---|
2106 | PATMCallIndirectEnd:
|
---|
2107 | ; returning here -> do not add code here or after the jmp!!!!!
|
---|
2108 | ENDPROC PATMCallIndirect
|
---|
2109 |
|
---|
2110 | ; Patch record for indirect calls
|
---|
2111 | GLOBALNAME PATMCallIndirectRecord
|
---|
2112 | RTCCPTR_DEF PATMCallIndirectStart
|
---|
2113 | DD 0
|
---|
2114 | DD 0
|
---|
2115 | DD 0
|
---|
2116 | DD PATMCallIndirectEnd - PATMCallIndirectStart
|
---|
2117 | DD 5
|
---|
2118 | DD PATM_FIXUP
|
---|
2119 | DD PATMCallIndirectTable - PATMCallIndirectStart
|
---|
2120 | DD PATM_PATCHNEXTBLOCK
|
---|
2121 | DD 0
|
---|
2122 | DD PATM_RETURNADDR
|
---|
2123 | DD 0
|
---|
2124 | DD PATM_LOOKUP_AND_CALL_FUNCTION
|
---|
2125 | DD 0
|
---|
2126 | DD PATM_INTERRUPTFLAG
|
---|
2127 | DD 0
|
---|
2128 | DD 0ffffffffh
|
---|
2129 |
|
---|
2130 |
|
---|
2131 | align 32
|
---|
2132 | ; Patch function for indirect jumps
|
---|
2133 | ; Caller is responsible for clearing PATM_INTERRUPTFLAG and adding:
|
---|
2134 | ; push [pTargetGC]
|
---|
2135 | ;
|
---|
2136 | BEGINPROC PATMJumpIndirect
|
---|
2137 | PATMJumpIndirectStart:
|
---|
2138 | ; save flags (just to be sure)
|
---|
2139 | pushfd
|
---|
2140 | pop dword [ss:PATM_TEMP_EFLAGS]
|
---|
2141 |
|
---|
2142 | push PATM_FIXUP ; fixup for jump table below
|
---|
2143 | DB 0E8h ; call
|
---|
2144 | DD PATM_LOOKUP_AND_JUMP_FUNCTION
|
---|
2145 | ; we only return in case of a failure
|
---|
2146 | add esp, 8 ; pushed address of jump table + pushed target address
|
---|
2147 |
|
---|
2148 | ; restore flags (just to be sure)
|
---|
2149 | push dword [ss:PATM_TEMP_EFLAGS]
|
---|
2150 | popfd
|
---|
2151 |
|
---|
2152 | mov dword [ss:PATM_INTERRUPTFLAG], 1
|
---|
2153 | PATM_INT3
|
---|
2154 |
|
---|
2155 | %ifdef DEBUG
|
---|
2156 | ; for disassembly
|
---|
2157 | jmp PATMJumpIndirectEnd
|
---|
2158 | %endif
|
---|
2159 |
|
---|
2160 | align 4
|
---|
2161 | PATMJumpIndirectTable:
|
---|
2162 | DW PATM_MAX_JUMPTABLE_ENTRIES ; nrSlots
|
---|
2163 | DW 0 ; ulInsertPos
|
---|
2164 | DD 0 ; cAddresses
|
---|
2165 | TIMES PATCHJUMPTABLE_SIZE DB 0 ; lookup slots
|
---|
2166 |
|
---|
2167 | PATMJumpIndirectEnd:
|
---|
2168 | ; returning here -> do not add code here or after the jmp!!!!!
|
---|
2169 | ENDPROC PATMJumpIndirect
|
---|
2170 |
|
---|
2171 | ; Patch record for indirect jumps
|
---|
2172 | GLOBALNAME PATMJumpIndirectRecord
|
---|
2173 | RTCCPTR_DEF PATMJumpIndirectStart
|
---|
2174 | DD 0
|
---|
2175 | DD 0
|
---|
2176 | DD 0
|
---|
2177 | DD PATMJumpIndirectEnd - PATMJumpIndirectStart
|
---|
2178 | DD 5
|
---|
2179 | DD PATM_TEMP_EFLAGS
|
---|
2180 | DD 0
|
---|
2181 | DD PATM_FIXUP
|
---|
2182 | DD PATMJumpIndirectTable - PATMJumpIndirectStart
|
---|
2183 | DD PATM_LOOKUP_AND_JUMP_FUNCTION
|
---|
2184 | DD 0
|
---|
2185 | DD PATM_TEMP_EFLAGS
|
---|
2186 | DD 0
|
---|
2187 | DD PATM_INTERRUPTFLAG
|
---|
2188 | DD 0
|
---|
2189 | DD 0ffffffffh
|
---|
2190 |
|
---|
2191 | ;
|
---|
2192 | ; return from duplicated function
|
---|
2193 | ;
|
---|
2194 | align 32
|
---|
2195 | BEGINPROC PATMRet
|
---|
2196 | PATMRet_Start:
|
---|
2197 | ; probe stack here as we can't recover from page faults later on
|
---|
2198 | not dword [esp-32]
|
---|
2199 | not dword [esp-32]
|
---|
2200 | mov dword [ss:PATM_INTERRUPTFLAG], 0
|
---|
2201 | pushfd
|
---|
2202 | push eax
|
---|
2203 | push PATM_FIXUP
|
---|
2204 | DB 0E8h ; call
|
---|
2205 | DD PATM_RETURN_FUNCTION
|
---|
2206 | add esp, 4 ; pushed address of jump table
|
---|
2207 |
|
---|
2208 | cmp eax, 0
|
---|
2209 | jne near PATMRet_Success
|
---|
2210 |
|
---|
2211 | pop eax
|
---|
2212 | popfd
|
---|
2213 | mov dword [ss:PATM_INTERRUPTFLAG], 1
|
---|
2214 | PATM_INT3
|
---|
2215 |
|
---|
2216 | %ifdef DEBUG
|
---|
2217 | ; for disassembly
|
---|
2218 | jmp PATMRet_Success
|
---|
2219 | %endif
|
---|
2220 | align 4
|
---|
2221 | PATMRetTable:
|
---|
2222 | DW PATM_MAX_JUMPTABLE_ENTRIES ; nrSlots
|
---|
2223 | DW 0 ; ulInsertPos
|
---|
2224 | DD 0 ; cAddresses
|
---|
2225 | TIMES PATCHJUMPTABLE_SIZE DB 0 ; lookup slots
|
---|
2226 |
|
---|
2227 | PATMRet_Success:
|
---|
2228 | mov dword [esp+8], eax ; overwrite the saved return address
|
---|
2229 | pop eax
|
---|
2230 | popf
|
---|
2231 | ; caller will duplicate the ret or ret n instruction
|
---|
2232 | ; the patched call will set PATM_INTERRUPTFLAG after the return!
|
---|
2233 | PATMRet_End:
|
---|
2234 | ENDPROC PATMRet
|
---|
2235 |
|
---|
2236 | GLOBALNAME PATMRetRecord
|
---|
2237 | RTCCPTR_DEF PATMRet_Start
|
---|
2238 | DD 0
|
---|
2239 | DD 0
|
---|
2240 | DD 0
|
---|
2241 | DD PATMRet_End - PATMRet_Start
|
---|
2242 | DD 4
|
---|
2243 | DD PATM_INTERRUPTFLAG
|
---|
2244 | DD 0
|
---|
2245 | DD PATM_FIXUP
|
---|
2246 | DD PATMRetTable - PATMRet_Start
|
---|
2247 | DD PATM_RETURN_FUNCTION
|
---|
2248 | DD 0
|
---|
2249 | DD PATM_INTERRUPTFLAG
|
---|
2250 | DD 0
|
---|
2251 | DD 0ffffffffh
|
---|
2252 |
|
---|
2253 | ;
|
---|
2254 | ; global function for implementing 'retn'
|
---|
2255 | ;
|
---|
2256 | ; Caller is responsible for right stack layout
|
---|
2257 | ; + 16 original return address
|
---|
2258 | ; + 12 eflags
|
---|
2259 | ; + 8 eax
|
---|
2260 | ; + 4 Jump table address
|
---|
2261 | ;( + 0 return address )
|
---|
2262 | ;
|
---|
2263 | ; @note assumes PATM_INTERRUPTFLAG is zero
|
---|
2264 | ; @note assumes it can trash eax and eflags
|
---|
2265 | ;
|
---|
2266 | ; @returns eax=0 on failure
|
---|
2267 | ; otherwise return address in eax
|
---|
2268 | ;
|
---|
2269 | ; @note NEVER change this without bumping the SSM version
|
---|
2270 | align 32
|
---|
2271 | BEGINPROC PATMRetFunction
|
---|
2272 | PATMRetFunction_Start:
|
---|
2273 | push ecx
|
---|
2274 | push edx
|
---|
2275 | push edi
|
---|
2276 |
|
---|
2277 | ; Event order:
|
---|
2278 | ; (@todo figure out which path is taken most often (1 or 2))
|
---|
2279 | ; 1) Check if the return patch address was pushed onto the PATM stack
|
---|
2280 | ; 2) Check if the return patch address can be found in the lookup table
|
---|
2281 | ; 3) Query return patch address from the hypervisor
|
---|
2282 |
|
---|
2283 |
|
---|
2284 | ; 1) Check if the return patch address was pushed on the PATM stack
|
---|
2285 | cmp dword [ss:PATM_STACKPTR], PATM_STACK_SIZE
|
---|
2286 | jae near PATMRetFunction_FindReturnAddress
|
---|
2287 |
|
---|
2288 | mov edx, dword PATM_STACKPTR
|
---|
2289 |
|
---|
2290 | ; check if the return address is what we expect it to be
|
---|
2291 | mov eax, dword PATM_STACKBASE_GUEST
|
---|
2292 | add eax, dword [ss:edx] ; stack base + stack position
|
---|
2293 | mov eax, dword [ss:eax] ; original return address
|
---|
2294 | cmp eax, dword [esp+12+16] ; pushed return address
|
---|
2295 |
|
---|
2296 | ; the return address was changed -> let our trap handler try to find it
|
---|
2297 | ; (can happen when the guest messes with the stack (seen it) or when we didn't call this function ourselves)
|
---|
2298 | jne near PATMRetFunction_FindReturnAddress
|
---|
2299 |
|
---|
2300 | ; found it, convert relative to absolute patch address and return the result to the caller
|
---|
2301 | mov eax, dword PATM_STACKBASE
|
---|
2302 | add eax, dword [ss:edx] ; stack base + stack position
|
---|
2303 | mov eax, dword [ss:eax] ; relative patm return address
|
---|
2304 | add eax, PATM_PATCHBASE
|
---|
2305 |
|
---|
2306 | %ifdef PATM_LOG_PATCHINSTR
|
---|
2307 | push eax
|
---|
2308 | push ebx
|
---|
2309 | push ecx
|
---|
2310 | push edx
|
---|
2311 | mov edx, eax ; return address
|
---|
2312 | lea ebx, [esp+16+12+16] ; stack address containing the return address
|
---|
2313 | lock or dword [ss:PATM_PENDINGACTION], PATM_ACTION_LOG_RET
|
---|
2314 | mov eax, PATM_ACTION_LOG_RET
|
---|
2315 | mov ecx, PATM_ACTION_MAGIC
|
---|
2316 | db 0fh, 0bh ; illegal instr (hardcoded assumption in PATMHandleIllegalInstrTrap)
|
---|
2317 | pop edx
|
---|
2318 | pop ecx
|
---|
2319 | pop ebx
|
---|
2320 | pop eax
|
---|
2321 | %endif
|
---|
2322 |
|
---|
2323 | add dword [ss:edx], 4 ; pop return address from the PATM stack (sizeof(RTGCPTR); @note hardcoded assumption!)
|
---|
2324 |
|
---|
2325 | pop edi
|
---|
2326 | pop edx
|
---|
2327 | pop ecx
|
---|
2328 | ret
|
---|
2329 |
|
---|
2330 | PATMRetFunction_FindReturnAddress:
|
---|
2331 | ; 2) Check if the return patch address can be found in the lookup table
|
---|
2332 | mov edx, dword [esp+12+16] ; pushed target address
|
---|
2333 |
|
---|
2334 | xor eax, eax ; default result -> nothing found
|
---|
2335 | mov edi, dword [esp+12+4] ; jump table
|
---|
2336 | mov ecx, [ss:edi + PATCHJUMPTABLE.cAddresses]
|
---|
2337 | cmp ecx, 0
|
---|
2338 | je near PATMRetFunction_AskHypervisor
|
---|
2339 |
|
---|
2340 | PATMRetFunction_SearchStart:
|
---|
2341 | cmp [ss:edi + PATCHJUMPTABLE.Slot_pInstrGC + eax*8], edx ; edx = GC address to search for
|
---|
2342 | je near PATMRetFunction_SearchHit
|
---|
2343 | inc eax
|
---|
2344 | cmp eax, ecx
|
---|
2345 | jl near PATMRetFunction_SearchStart
|
---|
2346 |
|
---|
2347 | PATMRetFunction_AskHypervisor:
|
---|
2348 | ; 3) Query return patch address from the hypervisor
|
---|
2349 | ; @todo private ugly interface, since we have nothing generic at the moment
|
---|
2350 | lock or dword [ss:PATM_PENDINGACTION], PATM_ACTION_LOOKUP_ADDRESS
|
---|
2351 | mov eax, PATM_ACTION_LOOKUP_ADDRESS
|
---|
2352 | mov ecx, PATM_ACTION_MAGIC
|
---|
2353 | mov edi, dword [esp+12+4] ; jump table address
|
---|
2354 | mov edx, dword [esp+12+16] ; original return address
|
---|
2355 | db 0fh, 0bh ; illegal instr (hardcoded assumption in PATMHandleIllegalInstrTrap)
|
---|
2356 | jmp near PATMRetFunction_SearchEnd
|
---|
2357 |
|
---|
2358 | PATMRetFunction_SearchHit:
|
---|
2359 | mov eax, [ss:edi + PATCHJUMPTABLE.Slot_pRelPatchGC + eax*8] ; found a match!
|
---|
2360 | ;@note can be zero, so the next check is required!!
|
---|
2361 |
|
---|
2362 | PATMRetFunction_SearchEnd:
|
---|
2363 | cmp eax, 0
|
---|
2364 | jz PATMRetFunction_Failure
|
---|
2365 |
|
---|
2366 | add eax, PATM_PATCHBASE
|
---|
2367 |
|
---|
2368 | %ifdef PATM_LOG_PATCHINSTR
|
---|
2369 | push eax
|
---|
2370 | push ebx
|
---|
2371 | push ecx
|
---|
2372 | push edx
|
---|
2373 | mov edx, eax ; return address
|
---|
2374 | lea ebx, [esp+16+12+16] ; stack address containing the return address
|
---|
2375 | lock or dword [ss:PATM_PENDINGACTION], PATM_ACTION_LOG_RET
|
---|
2376 | mov eax, PATM_ACTION_LOG_RET
|
---|
2377 | mov ecx, PATM_ACTION_MAGIC
|
---|
2378 | db 0fh, 0bh ; illegal instr (hardcoded assumption in PATMHandleIllegalInstrTrap)
|
---|
2379 | pop edx
|
---|
2380 | pop ecx
|
---|
2381 | pop ebx
|
---|
2382 | pop eax
|
---|
2383 | %endif
|
---|
2384 |
|
---|
2385 | pop edi
|
---|
2386 | pop edx
|
---|
2387 | pop ecx
|
---|
2388 | ret
|
---|
2389 |
|
---|
2390 | PATMRetFunction_Failure:
|
---|
2391 | ;signal error
|
---|
2392 | xor eax, eax
|
---|
2393 | pop edi
|
---|
2394 | pop edx
|
---|
2395 | pop ecx
|
---|
2396 | ret
|
---|
2397 |
|
---|
2398 | PATMRetFunction_End:
|
---|
2399 | ENDPROC PATMRetFunction
|
---|
2400 |
|
---|
2401 | GLOBALNAME PATMRetFunctionRecord
|
---|
2402 | RTCCPTR_DEF PATMRetFunction_Start
|
---|
2403 | DD 0
|
---|
2404 | DD 0
|
---|
2405 | DD 0
|
---|
2406 | DD PATMRetFunction_End - PATMRetFunction_Start
|
---|
2407 | %ifdef PATM_LOG_PATCHINSTR
|
---|
2408 | DD 9
|
---|
2409 | %else
|
---|
2410 | DD 7
|
---|
2411 | %endif
|
---|
2412 | DD PATM_STACKPTR
|
---|
2413 | DD 0
|
---|
2414 | DD PATM_STACKPTR
|
---|
2415 | DD 0
|
---|
2416 | DD PATM_STACKBASE_GUEST
|
---|
2417 | DD 0
|
---|
2418 | DD PATM_STACKBASE
|
---|
2419 | DD 0
|
---|
2420 | DD PATM_PATCHBASE
|
---|
2421 | DD 0
|
---|
2422 | %ifdef PATM_LOG_PATCHINSTR
|
---|
2423 | DD PATM_PENDINGACTION
|
---|
2424 | DD 0
|
---|
2425 | %endif
|
---|
2426 | DD PATM_PENDINGACTION
|
---|
2427 | DD 0
|
---|
2428 | DD PATM_PATCHBASE
|
---|
2429 | DD 0
|
---|
2430 | %ifdef PATM_LOG_PATCHINSTR
|
---|
2431 | DD PATM_PENDINGACTION
|
---|
2432 | DD 0
|
---|
2433 | %endif
|
---|
2434 | DD 0ffffffffh
|
---|
2435 |
|
---|
2436 |
|
---|
2437 | ;
|
---|
2438 | ; Jump to original instruction if IF=1
|
---|
2439 | ;
|
---|
2440 | BEGINPROC PATMCheckIF
|
---|
2441 | PATMCheckIF_Start:
|
---|
2442 | mov dword [ss:PATM_INTERRUPTFLAG], 0
|
---|
2443 | pushf
|
---|
2444 | test dword [ss:PATM_VMFLAGS], X86_EFL_IF
|
---|
2445 | jnz PATMCheckIF_Safe
|
---|
2446 | nop
|
---|
2447 |
|
---|
2448 | ; IF=0 -> unsafe, so we must call the duplicated function (which we don't do here)
|
---|
2449 | popf
|
---|
2450 | mov dword [ss:PATM_INTERRUPTFLAG], 1
|
---|
2451 | jmp PATMCheckIF_End
|
---|
2452 |
|
---|
2453 | PATMCheckIF_Safe:
|
---|
2454 | ; invalidate the PATM stack as we'll jump back to guest code
|
---|
2455 | mov dword [ss:PATM_STACKPTR], PATM_STACK_SIZE
|
---|
2456 |
|
---|
2457 | %ifdef PATM_LOG_PATCHINSTR
|
---|
2458 | push eax
|
---|
2459 | push ecx
|
---|
2460 | lock or dword [ss:PATM_PENDINGACTION], PATM_ACTION_LOG_IF1
|
---|
2461 | mov eax, PATM_ACTION_LOG_IF1
|
---|
2462 | mov ecx, PATM_ACTION_MAGIC
|
---|
2463 | db 0fh, 0bh ; illegal instr (hardcoded assumption in PATMHandleIllegalInstrTrap)
|
---|
2464 | pop ecx
|
---|
2465 | pop eax
|
---|
2466 | %endif
|
---|
2467 | popf
|
---|
2468 | mov dword [ss:PATM_INTERRUPTFLAG], 1
|
---|
2469 | ; IF=1 -> we can safely jump back to the original instruction
|
---|
2470 | DB 0xE9
|
---|
2471 | PATMCheckIF_Jump:
|
---|
2472 | DD PATM_JUMPDELTA
|
---|
2473 | PATMCheckIF_End:
|
---|
2474 | ENDPROC PATMCheckIF
|
---|
2475 |
|
---|
2476 | ; Patch record for call instructions
|
---|
2477 | GLOBALNAME PATMCheckIFRecord
|
---|
2478 | RTCCPTR_DEF PATMCheckIF_Start
|
---|
2479 | DD PATMCheckIF_Jump - PATMCheckIF_Start
|
---|
2480 | DD 0
|
---|
2481 | DD 0
|
---|
2482 | DD PATMCheckIF_End - PATMCheckIF_Start
|
---|
2483 | %ifdef PATM_LOG_PATCHINSTR
|
---|
2484 | DD 6
|
---|
2485 | %else
|
---|
2486 | DD 5
|
---|
2487 | %endif
|
---|
2488 | DD PATM_INTERRUPTFLAG
|
---|
2489 | DD 0
|
---|
2490 | DD PATM_VMFLAGS
|
---|
2491 | DD 0
|
---|
2492 | DD PATM_INTERRUPTFLAG
|
---|
2493 | DD 0
|
---|
2494 | DD PATM_STACKPTR
|
---|
2495 | DD 0
|
---|
2496 | %ifdef PATM_LOG_PATCHINSTR
|
---|
2497 | DD PATM_PENDINGACTION
|
---|
2498 | DD 0
|
---|
2499 | %endif
|
---|
2500 | DD PATM_INTERRUPTFLAG
|
---|
2501 | DD 0
|
---|
2502 | DD 0ffffffffh
|
---|
2503 |
|
---|
2504 | ;
|
---|
2505 | ; Jump back to guest if IF=1, else fault
|
---|
2506 | ;
|
---|
2507 | BEGINPROC PATMJumpToGuest_IF1
|
---|
2508 | PATMJumpToGuest_IF1_Start:
|
---|
2509 | mov dword [ss:PATM_INTERRUPTFLAG], 0
|
---|
2510 | pushf
|
---|
2511 | test dword [ss:PATM_VMFLAGS], X86_EFL_IF
|
---|
2512 | jnz PATMJumpToGuest_IF1_Safe
|
---|
2513 | nop
|
---|
2514 |
|
---|
2515 | ; IF=0 -> unsafe, so fault
|
---|
2516 | popf
|
---|
2517 | mov dword [ss:PATM_INTERRUPTFLAG], 1
|
---|
2518 | PATM_INT3
|
---|
2519 |
|
---|
2520 | PATMJumpToGuest_IF1_Safe:
|
---|
2521 | ; IF=1 -> we can safely jump back to the original instruction
|
---|
2522 | popf
|
---|
2523 | mov dword [ss:PATM_INTERRUPTFLAG], 1
|
---|
2524 | DB 0xE9
|
---|
2525 | PATMJumpToGuest_IF1_Jump:
|
---|
2526 | DD PATM_JUMPDELTA
|
---|
2527 | PATMJumpToGuest_IF1_End:
|
---|
2528 | ENDPROC PATMJumpToGuest_IF1
|
---|
2529 |
|
---|
2530 | ; Patch record for call instructions
|
---|
2531 | GLOBALNAME PATMJumpToGuest_IF1Record
|
---|
2532 | RTCCPTR_DEF PATMJumpToGuest_IF1_Start
|
---|
2533 | DD PATMJumpToGuest_IF1_Jump - PATMJumpToGuest_IF1_Start
|
---|
2534 | DD 0
|
---|
2535 | DD 0
|
---|
2536 | DD PATMJumpToGuest_IF1_End - PATMJumpToGuest_IF1_Start
|
---|
2537 | DD 4
|
---|
2538 | DD PATM_INTERRUPTFLAG
|
---|
2539 | DD 0
|
---|
2540 | DD PATM_VMFLAGS
|
---|
2541 | DD 0
|
---|
2542 | DD PATM_INTERRUPTFLAG
|
---|
2543 | DD 0
|
---|
2544 | DD PATM_INTERRUPTFLAG
|
---|
2545 | DD 0
|
---|
2546 | DD 0ffffffffh
|
---|
2547 |
|
---|
2548 |
|
---|
2549 | ; check and correct RPL of pushed ss
|
---|
2550 | BEGINPROC PATMMovFromSS
|
---|
2551 | PATMMovFromSS_Start:
|
---|
2552 | push eax
|
---|
2553 | pushfd
|
---|
2554 | mov ax, ss
|
---|
2555 | and ax, 3
|
---|
2556 | cmp ax, 1
|
---|
2557 | jne near PATMMovFromSS_Continue
|
---|
2558 |
|
---|
2559 | and dword [esp+8], ~3 ; clear RPL 1
|
---|
2560 | PATMMovFromSS_Continue:
|
---|
2561 | popfd
|
---|
2562 | pop eax
|
---|
2563 | PATMMovFromSS_Start_End:
|
---|
2564 | ENDPROC PATMMovFromSS
|
---|
2565 |
|
---|
2566 | GLOBALNAME PATMMovFromSSRecord
|
---|
2567 | RTCCPTR_DEF PATMMovFromSS_Start
|
---|
2568 | DD 0
|
---|
2569 | DD 0
|
---|
2570 | DD 0
|
---|
2571 | DD PATMMovFromSS_Start_End - PATMMovFromSS_Start
|
---|
2572 | DD 0
|
---|
2573 | DD 0ffffffffh
|
---|
2574 |
|
---|
2575 |
|
---|
2576 |
|
---|
2577 |
|
---|
2578 | ; For assertion during init (to make absolutely sure the flags are in sync in vm.mac & vm.h)
|
---|
2579 | GLOBALNAME PATMInterruptFlag
|
---|
2580 | DD VMCPU_FF_INTERRUPT_APIC | VMCPU_FF_INTERRUPT_PIC | VMCPU_FF_TIMER | VMCPU_FF_REQUEST
|
---|
2581 |
|
---|