Make all newly created surfces dirty, so that they are loaded properly
[wine/multimedia.git] / dlls / winedos / interrupts.c
blob49c2dfc9112a363ab714ef7fd3e02e12458f40c1
1 /*
2 * Interrupt emulation
4 * Copyright 2002 Jukka Heinonen
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "config.h"
23 #include "dosexe.h"
24 #include "wine/debug.h"
25 #include "wine/winbase16.h"
27 #include "thread.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(int);
30 WINE_DECLARE_DEBUG_CHANNEL(relay);
33 static FARPROC16 DOSVM_Vectors16[256];
34 static FARPROC48 DOSVM_Vectors48[256];
35 static const INTPROC DOSVM_VectorsBuiltin[] =
37 /* 00 */ 0, 0, 0, 0,
38 /* 04 */ 0, 0, 0, 0,
39 /* 08 */ DOSVM_Int08Handler, DOSVM_Int09Handler, 0, 0,
40 /* 0C */ 0, 0, 0, 0,
41 /* 10 */ DOSVM_Int10Handler, DOSVM_Int11Handler, DOSVM_Int12Handler, DOSVM_Int13Handler,
42 /* 14 */ 0, DOSVM_Int15Handler, DOSVM_Int16Handler, DOSVM_Int17Handler,
43 /* 18 */ 0, DOSVM_Int19Handler, DOSVM_Int1aHandler, 0,
44 /* 1C */ 0, 0, 0, 0,
45 /* 20 */ DOSVM_Int20Handler, DOSVM_Int21Handler, 0, 0,
46 /* 24 */ 0, DOSVM_Int25Handler, DOSVM_Int26Handler, 0,
47 /* 28 */ 0, DOSVM_Int29Handler, DOSVM_Int2aHandler, 0,
48 /* 2C */ 0, 0, 0, DOSVM_Int2fHandler,
49 /* 30 */ 0, DOSVM_Int31Handler, 0, DOSVM_Int33Handler,
50 /* 34 */ DOSVM_Int34Handler, DOSVM_Int35Handler, DOSVM_Int36Handler, DOSVM_Int37Handler,
51 /* 38 */ DOSVM_Int38Handler, DOSVM_Int39Handler, DOSVM_Int3aHandler, DOSVM_Int3bHandler,
52 /* 3C */ DOSVM_Int3cHandler, DOSVM_Int3dHandler, DOSVM_Int3eHandler, 0,
53 /* 40 */ 0, DOSVM_Int41Handler, 0, 0,
54 /* 44 */ 0, 0, 0, 0,
55 /* 48 */ 0, 0, 0, DOSVM_Int4bHandler,
56 /* 4C */ 0, 0, 0, 0,
57 /* 50 */ 0, 0, 0, 0,
58 /* 54 */ 0, 0, 0, 0,
59 /* 58 */ 0, 0, 0, 0,
60 /* 5C */ DOSVM_Int5cHandler, 0, 0, 0,
61 /* 60 */ 0, 0, 0, 0,
62 /* 64 */ 0, 0, 0, DOSVM_Int67Handler
67 * Sizes of real mode and protected mode interrupt stubs.
69 #define DOSVM_STUB_RM 4
70 #define DOSVM_STUB_PM16 5
71 #define DOSVM_STUB_PM48 6
74 /**********************************************************************
75 * DOSVM_GetRMVector
77 * Return pointer to real mode interrupt vector. These are not at fixed
78 * location because those Win16 programs that do not use any real mode
79 * code have protected NULL pointer catching block at low linear memory
80 * and interrupt vectors have been moved to another location.
82 static FARPROC16* DOSVM_GetRMVector( BYTE intnum )
84 LDT_ENTRY entry;
85 FARPROC16 proc;
87 proc = GetProcAddress16( GetModuleHandle16( "KERNEL" ),
88 (LPCSTR)(ULONG_PTR)183 );
89 wine_ldt_get_entry( LOWORD(proc), &entry );
91 return (FARPROC16*)wine_ldt_get_base( &entry ) + intnum;
95 /**********************************************************************
96 * DOSVM_IsIRQ
98 * Return TRUE if interrupt is an IRQ.
100 static BOOL DOSVM_IsIRQ( BYTE intnum )
102 if (intnum >= 0x08 && intnum <= 0x0f)
103 return TRUE;
105 if (intnum >= 0x70 && intnum <= 0x77)
106 return TRUE;
108 return FALSE;
112 /**********************************************************************
113 * DOSVM_DefaultHandler
115 * Default interrupt handler. This will be used to emulate all
116 * interrupts that don't have their own interrupt handler.
118 void WINAPI DOSVM_DefaultHandler( CONTEXT86 *context )
123 /**********************************************************************
124 * DOSVM_GetBuiltinHandler
126 * Return Wine interrupt handler procedure for a given interrupt.
128 static INTPROC DOSVM_GetBuiltinHandler( BYTE intnum )
130 if (intnum < sizeof(DOSVM_VectorsBuiltin)/sizeof(INTPROC)) {
131 INTPROC proc = DOSVM_VectorsBuiltin[intnum];
132 if (proc)
133 return proc;
136 WARN("int%x not implemented, returning dummy handler\n", intnum );
138 if (DOSVM_IsIRQ(intnum))
139 return DOSVM_AcknowledgeIRQ;
141 return DOSVM_DefaultHandler;
145 /**********************************************************************
146 * DOSVM_IntProcRelay
148 * Simple DOSRELAY that interprets its argument as INTPROC and calls it.
150 static void DOSVM_IntProcRelay( CONTEXT86 *context, LPVOID data )
152 INTPROC proc = (INTPROC)data;
153 proc(context);
157 /**********************************************************************
158 * DOSVM_PrepareIRQ
161 static void DOSVM_PrepareIRQ( CONTEXT86 *context, BOOL isbuiltin )
163 /* Disable virtual interrupts. */
164 NtCurrentTeb()->dpmi_vif = 0;
166 if (!isbuiltin)
168 DWORD *stack = CTX_SEG_OFF_TO_LIN(context,
169 context->SegSs,
170 context->Esp);
172 /* Push return address to stack. */
173 *(--stack) = context->SegCs;
174 *(--stack) = context->Eip;
175 context->Esp += -8;
177 /* Jump to enable interrupts stub. */
178 context->SegCs = DOSVM_dpmi_segments->relay_code_sel;
179 context->Eip = 5;
184 /**********************************************************************
185 * DOSVM_PushFlags
187 * This routine is used to make default int25 and int26 handlers leave the
188 * original eflags into stack. In order to do this, stack is manipulated
189 * so that it actually contains two copies of eflags, one of which is
190 * popped during return from interrupt handler.
192 static void DOSVM_PushFlags( CONTEXT86 *context, BOOL islong, BOOL isstub )
194 if (islong)
196 DWORD *stack = CTX_SEG_OFF_TO_LIN(context,
197 context->SegSs,
198 context->Esp);
199 context->Esp += -4; /* One item will be added to stack. */
201 if (isstub)
203 DWORD ip = stack[0];
204 DWORD cs = stack[1];
205 stack += 2; /* Pop ip and cs. */
206 *(--stack) = context->EFlags;
207 *(--stack) = cs;
208 *(--stack) = ip;
210 else
211 *(--stack) = context->EFlags;
213 else
215 WORD *stack = CTX_SEG_OFF_TO_LIN(context,
216 context->SegSs,
217 context->Esp);
218 ADD_LOWORD( context->Esp, -2 ); /* One item will be added to stack. */
220 if (isstub)
222 WORD ip = stack[0];
223 WORD cs = stack[1];
224 stack += 2; /* Pop ip and cs. */
225 *(--stack) = LOWORD(context->EFlags);
226 *(--stack) = cs;
227 *(--stack) = ip;
229 else
230 *(--stack) = LOWORD(context->EFlags);
235 /**********************************************************************
236 * DOSVM_EmulateInterruptPM
238 * Emulate software interrupt in 16-bit or 32-bit protected mode.
239 * Called from signal handler when intXX opcode is executed.
241 * Pushes interrupt frame to stack and changes instruction
242 * pointer to interrupt handler.
244 void WINAPI DOSVM_EmulateInterruptPM( CONTEXT86 *context, BYTE intnum )
246 TRACE_(relay)("Call DOS int 0x%02x ret=%04lx:%08lx\n"
247 " eax=%08lx ebx=%08lx ecx=%08lx edx=%08lx\n"
248 " esi=%08lx edi=%08lx ebp=%08lx esp=%08lx \n"
249 " ds=%04lx es=%04lx fs=%04lx gs=%04lx ss=%04lx flags=%08lx\n",
250 intnum, context->SegCs, context->Eip,
251 context->Eax, context->Ebx, context->Ecx, context->Edx,
252 context->Esi, context->Edi, context->Ebp, context->Esp,
253 context->SegDs, context->SegEs, context->SegFs, context->SegGs,
254 context->SegSs, context->EFlags );
256 if (context->SegCs == DOSVM_dpmi_segments->dpmi_sel)
258 DOSVM_BuildCallFrame( context,
259 DOSVM_IntProcRelay,
260 DOSVM_RawModeSwitchHandler );
262 else if (context->SegCs == DOSVM_dpmi_segments->relay_code_sel)
265 * This must not be called using DOSVM_BuildCallFrame.
267 DOSVM_RelayHandler( context );
269 else if (context->SegCs == DOSVM_dpmi_segments->int48_sel)
271 /* Restore original flags stored into the stack by the caller. */
272 DWORD *stack = CTX_SEG_OFF_TO_LIN(context,
273 context->SegSs, context->Esp);
274 context->EFlags = stack[2];
276 if (intnum != context->Eip / DOSVM_STUB_PM48)
277 WARN( "interrupt stub has been modified "
278 "(interrupt is %02x, interrupt stub is %02lx)\n",
279 intnum, context->Eip/DOSVM_STUB_PM48 );
281 TRACE( "builtin interrupt %02x has been branched to\n", intnum );
283 if (intnum == 0x25 || intnum == 0x26)
284 DOSVM_PushFlags( context, TRUE, TRUE );
286 DOSVM_BuildCallFrame( context,
287 DOSVM_IntProcRelay,
288 DOSVM_GetBuiltinHandler(intnum) );
290 else if (context->SegCs == DOSVM_dpmi_segments->int16_sel)
292 /* Restore original flags stored into the stack by the caller. */
293 WORD *stack = CTX_SEG_OFF_TO_LIN(context,
294 context->SegSs, context->Esp);
295 context->EFlags = (DWORD)MAKELONG( stack[2], HIWORD(context->EFlags) );
297 if (intnum != context->Eip / DOSVM_STUB_PM16)
298 WARN( "interrupt stub has been modified "
299 "(interrupt is %02x, interrupt stub is %02lx)\n",
300 intnum, context->Eip/DOSVM_STUB_PM16 );
302 TRACE( "builtin interrupt %02x has been branched to\n", intnum );
304 if (intnum == 0x25 || intnum == 0x26)
305 DOSVM_PushFlags( context, FALSE, TRUE );
307 DOSVM_BuildCallFrame( context,
308 DOSVM_IntProcRelay,
309 DOSVM_GetBuiltinHandler(intnum) );
311 else
313 DOSVM_HardwareInterruptPM( context, intnum );
318 /**********************************************************************
319 * DOSVM_HardwareInterruptPM
321 * Emulate call to interrupt handler in 16-bit or 32-bit protected mode.
323 * Pushes interrupt frame to stack and changes instruction
324 * pointer to interrupt handler.
326 void DOSVM_HardwareInterruptPM( CONTEXT86 *context, BYTE intnum )
328 if(DOSVM_IsDos32())
330 FARPROC48 addr = DOSVM_GetPMHandler48( intnum );
332 if (addr.selector == DOSVM_dpmi_segments->int48_sel)
334 TRACE( "builtin interrupt %02lx has been invoked "
335 "(through vector %02x)\n",
336 addr.offset / DOSVM_STUB_PM48, intnum );
338 if (intnum == 0x25 || intnum == 0x26)
339 DOSVM_PushFlags( context, TRUE, FALSE );
340 else if (DOSVM_IsIRQ(intnum))
341 DOSVM_PrepareIRQ( context, TRUE );
343 DOSVM_BuildCallFrame( context,
344 DOSVM_IntProcRelay,
345 DOSVM_GetBuiltinHandler(
346 addr.offset/DOSVM_STUB_PM48 ) );
348 else
350 DWORD *stack;
352 TRACE( "invoking hooked interrupt %02x at %04x:%08lx\n",
353 intnum, addr.selector, addr.offset );
355 if (DOSVM_IsIRQ(intnum))
356 DOSVM_PrepareIRQ( context, FALSE );
358 /* Push the flags and return address on the stack */
359 stack = CTX_SEG_OFF_TO_LIN(context, context->SegSs, context->Esp);
360 *(--stack) = context->EFlags;
361 *(--stack) = context->SegCs;
362 *(--stack) = context->Eip;
363 context->Esp += -12;
365 /* Jump to the interrupt handler */
366 context->SegCs = addr.selector;
367 context->Eip = addr.offset;
370 else
372 FARPROC16 addr = DOSVM_GetPMHandler16( intnum );
374 if (SELECTOROF(addr) == DOSVM_dpmi_segments->int16_sel)
376 TRACE( "builtin interrupt %02x has been invoked "
377 "(through vector %02x)\n",
378 OFFSETOF(addr)/DOSVM_STUB_PM16, intnum );
380 if (intnum == 0x25 || intnum == 0x26)
381 DOSVM_PushFlags( context, FALSE, FALSE );
382 else if (DOSVM_IsIRQ(intnum))
383 DOSVM_PrepareIRQ( context, TRUE );
385 DOSVM_BuildCallFrame( context,
386 DOSVM_IntProcRelay,
387 DOSVM_GetBuiltinHandler(
388 OFFSETOF(addr)/DOSVM_STUB_PM16 ) );
390 else
392 TRACE( "invoking hooked interrupt %02x at %04x:%04x\n",
393 intnum, SELECTOROF(addr), OFFSETOF(addr) );
395 if (DOSVM_IsIRQ(intnum))
396 DOSVM_PrepareIRQ( context, FALSE );
398 /* Push the flags and return address on the stack */
399 PUSH_WORD16( context, LOWORD(context->EFlags) );
400 PUSH_WORD16( context, context->SegCs );
401 PUSH_WORD16( context, LOWORD(context->Eip) );
403 /* Jump to the interrupt handler */
404 context->SegCs = HIWORD(addr);
405 context->Eip = LOWORD(addr);
411 /**********************************************************************
412 * DOSVM_EmulateInterruptRM
414 * Emulate software interrupt in real mode.
415 * Called from VM86 emulation when intXX opcode is executed.
417 * Either calls directly builtin handler or pushes interrupt frame to
418 * stack and changes instruction pointer to interrupt handler.
420 * Returns FALSE if this interrupt was caused by return
421 * from real mode wrapper.
423 BOOL WINAPI DOSVM_EmulateInterruptRM( CONTEXT86 *context, BYTE intnum )
425 TRACE_(relay)("Call DOS int 0x%02x ret=%04lx:%08lx\n"
426 " eax=%08lx ebx=%08lx ecx=%08lx edx=%08lx\n"
427 " esi=%08lx edi=%08lx ebp=%08lx esp=%08lx \n"
428 " ds=%04lx es=%04lx fs=%04lx gs=%04lx ss=%04lx flags=%08lx\n",
429 intnum, context->SegCs, context->Eip,
430 context->Eax, context->Ebx, context->Ecx, context->Edx,
431 context->Esi, context->Edi, context->Ebp, context->Esp,
432 context->SegDs, context->SegEs, context->SegFs, context->SegGs,
433 context->SegSs, context->EFlags );
435 /* check for our real-mode hooks */
436 if (intnum == 0x31)
438 /* is this exit from real-mode wrapper */
439 if (context->SegCs == DOSVM_dpmi_segments->wrap_seg)
440 return FALSE;
442 if (DOSVM_CheckWrappers( context ))
443 return TRUE;
446 /* check if the call is from our fake BIOS interrupt stubs */
447 if (context->SegCs==0xf000)
449 /* Restore original flags stored into the stack by the caller. */
450 WORD *stack = CTX_SEG_OFF_TO_LIN(context,
451 context->SegSs, context->Esp);
452 context->EFlags = (DWORD)MAKELONG( stack[2], HIWORD(context->EFlags) );
454 if (intnum != context->Eip / DOSVM_STUB_RM)
455 WARN( "interrupt stub has been modified "
456 "(interrupt is %02x, interrupt stub is %02lx)\n",
457 intnum, context->Eip/DOSVM_STUB_RM );
459 TRACE( "builtin interrupt %02x has been branched to\n", intnum );
461 DOSVM_CallBuiltinHandler( context, intnum );
463 /* Real mode stubs use IRET so we must put flags back into stack. */
464 stack[2] = LOWORD(context->EFlags);
466 else
468 DOSVM_HardwareInterruptRM( context, intnum );
471 return TRUE;
475 /**********************************************************************
476 * DOSVM_HardwareInterruptRM
478 * Emulate call to interrupt handler in real mode.
480 * Either calls directly builtin handler or pushes interrupt frame to
481 * stack and changes instruction pointer to interrupt handler.
483 void DOSVM_HardwareInterruptRM( CONTEXT86 *context, BYTE intnum )
485 FARPROC16 handler = DOSVM_GetRMHandler( intnum );
487 /* check if the call goes to an unhooked interrupt */
488 if (SELECTOROF(handler) == 0xf000)
490 /* if so, call it directly */
491 TRACE( "builtin interrupt %02x has been invoked "
492 "(through vector %02x)\n",
493 OFFSETOF(handler)/DOSVM_STUB_RM, intnum );
494 DOSVM_CallBuiltinHandler( context, OFFSETOF(handler)/DOSVM_STUB_RM );
496 else
498 /* the interrupt is hooked, simulate interrupt in DOS space */
499 WORD flag = LOWORD( context->EFlags );
501 TRACE( "invoking hooked interrupt %02x at %04x:%04x\n",
502 intnum, SELECTOROF(handler), OFFSETOF(handler) );
504 /* Copy virtual interrupt flag to pushed interrupt flag. */
505 if (context->EFlags & VIF_MASK)
506 flag |= IF_MASK;
507 else
508 flag &= ~IF_MASK;
510 PUSH_WORD16( context, flag );
511 PUSH_WORD16( context, context->SegCs );
512 PUSH_WORD16( context, LOWORD( context->Eip ));
514 context->SegCs = SELECTOROF( handler );
515 context->Eip = OFFSETOF( handler );
517 /* Clear virtual interrupt flag. */
518 context->EFlags &= ~VIF_MASK;
523 /**********************************************************************
524 * DOSVM_GetRMHandler
526 * Return the real mode interrupt vector for a given interrupt.
528 FARPROC16 DOSVM_GetRMHandler( BYTE intnum )
530 return *DOSVM_GetRMVector( intnum );
534 /**********************************************************************
535 * DOSVM_SetRMHandler
537 * Set the real mode interrupt handler for a given interrupt.
539 void DOSVM_SetRMHandler( BYTE intnum, FARPROC16 handler )
541 TRACE("Set real mode interrupt vector %02x <- %04x:%04x\n",
542 intnum, HIWORD(handler), LOWORD(handler) );
543 *DOSVM_GetRMVector( intnum ) = handler;
547 /**********************************************************************
548 * DOSVM_GetPMHandler16
550 * Return the protected mode interrupt vector for a given interrupt.
552 FARPROC16 DOSVM_GetPMHandler16( BYTE intnum )
554 TDB *pTask;
555 FARPROC16 proc = 0;
557 pTask = GlobalLock16(GetCurrentTask());
558 if (pTask)
560 switch( intnum )
562 case 0x00:
563 proc = pTask->int0;
564 break;
565 case 0x02:
566 proc = pTask->int2;
567 break;
568 case 0x04:
569 proc = pTask->int4;
570 break;
571 case 0x06:
572 proc = pTask->int6;
573 break;
574 case 0x07:
575 proc = pTask->int7;
576 break;
577 case 0x3e:
578 proc = pTask->int3e;
579 break;
580 case 0x75:
581 proc = pTask->int75;
582 break;
584 if( proc )
585 return proc;
587 if (!DOSVM_Vectors16[intnum])
589 proc = (FARPROC16)MAKESEGPTR( DOSVM_dpmi_segments->int16_sel,
590 DOSVM_STUB_PM16 * intnum );
591 DOSVM_Vectors16[intnum] = proc;
593 return DOSVM_Vectors16[intnum];
597 /**********************************************************************
598 * DOSVM_SetPMHandler16
600 * Set the protected mode interrupt handler for a given interrupt.
602 void DOSVM_SetPMHandler16( BYTE intnum, FARPROC16 handler )
604 TDB *pTask;
606 TRACE("Set protected mode interrupt vector %02x <- %04x:%04x\n",
607 intnum, HIWORD(handler), LOWORD(handler) );
609 pTask = GlobalLock16(GetCurrentTask());
610 if (!pTask)
611 return;
612 switch( intnum )
614 case 0x00:
615 pTask->int0 = handler;
616 break;
617 case 0x02:
618 pTask->int2 = handler;
619 break;
620 case 0x04:
621 pTask->int4 = handler;
622 break;
623 case 0x06:
624 pTask->int6 = handler;
625 break;
626 case 0x07:
627 pTask->int7 = handler;
628 break;
629 case 0x3e:
630 pTask->int3e = handler;
631 break;
632 case 0x75:
633 pTask->int75 = handler;
634 break;
635 default:
636 DOSVM_Vectors16[intnum] = handler;
637 break;
642 /**********************************************************************
643 * DOSVM_GetPMHandler48
645 * Return the protected mode interrupt vector for a given interrupt.
646 * Used to get 48-bit pointer for 32-bit interrupt handlers in DPMI32.
648 FARPROC48 DOSVM_GetPMHandler48( BYTE intnum )
650 if (!DOSVM_Vectors48[intnum].selector)
652 DOSVM_Vectors48[intnum].selector = DOSVM_dpmi_segments->int48_sel;
653 DOSVM_Vectors48[intnum].offset = DOSVM_STUB_PM48 * intnum;
655 return DOSVM_Vectors48[intnum];
659 /**********************************************************************
660 * DOSVM_SetPMHandler48
662 * Set the protected mode interrupt handler for a given interrupt.
663 * Used to set 48-bit pointer for 32-bit interrupt handlers in DPMI32.
665 void DOSVM_SetPMHandler48( BYTE intnum, FARPROC48 handler )
667 TRACE("Set 32-bit protected mode interrupt vector %02x <- %04x:%08lx\n",
668 intnum, handler.selector, handler.offset );
669 DOSVM_Vectors48[intnum] = handler;
673 /**********************************************************************
674 * DOSVM_CallBuiltinHandler
676 * Execute Wine interrupt handler procedure.
678 void WINAPI DOSVM_CallBuiltinHandler( CONTEXT86 *context, BYTE intnum )
681 * FIXME: Make all builtin interrupt calls go via this routine.
682 * FIXME: Check for PM->RM interrupt reflection.
683 * FIXME: Check for RM->PM interrupt reflection.
686 INTPROC proc = DOSVM_GetBuiltinHandler( intnum );
687 proc( context );