Release 9.12.
[wine.git] / dlls / krnl386.exe16 / interrupts.c
blob5087d1b7a034442d978e63d7ed8bc2db9ea6d8d5
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <stdio.h>
23 #include "wine/winbase16.h"
24 #include "kernel16_private.h"
25 #include "dosexe.h"
26 #include "winternl.h"
27 #include "wine/debug.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(int);
30 WINE_DECLARE_DEBUG_CHANNEL(relay);
32 #define BCD_TO_BIN(x) ((x&15) + (x>>4)*10)
33 #define BIN_TO_BCD(x) ((x%10) + ((x/10)<<4))
35 static void WINAPI DOSVM_Int11Handler(CONTEXT*);
36 static void WINAPI DOSVM_Int12Handler(CONTEXT*);
37 static void WINAPI DOSVM_Int17Handler(CONTEXT*);
38 static void WINAPI DOSVM_Int19Handler(CONTEXT*);
39 static void WINAPI DOSVM_Int1aHandler(CONTEXT*);
40 static void WINAPI DOSVM_Int20Handler(CONTEXT*);
41 static void WINAPI DOSVM_Int2aHandler(CONTEXT*);
42 static void WINAPI DOSVM_Int41Handler(CONTEXT*);
43 static void WINAPI DOSVM_Int4bHandler(CONTEXT*);
44 static void WINAPI DOSVM_Int5cHandler(CONTEXT*);
45 static void WINAPI DOSVM_DefaultHandler(CONTEXT*);
47 static FARPROC16 DOSVM_Vectors16[256];
48 static const INTPROC DOSVM_VectorsBuiltin[] =
50 /* 00 */ 0, 0, 0, 0,
51 /* 04 */ 0, 0, 0, 0,
52 /* 08 */ 0, 0, 0, 0,
53 /* 0C */ 0, 0, 0, 0,
54 /* 10 */ 0, DOSVM_Int11Handler, DOSVM_Int12Handler, 0,
55 /* 14 */ 0, DOSVM_Int15Handler, 0, DOSVM_Int17Handler,
56 /* 18 */ 0, DOSVM_Int19Handler, DOSVM_Int1aHandler, 0,
57 /* 1C */ 0, 0, 0, 0,
58 /* 20 */ DOSVM_Int20Handler, DOSVM_Int21Handler, 0, 0,
59 /* 24 */ 0, DOSVM_Int25Handler, DOSVM_Int26Handler, 0,
60 /* 28 */ 0, 0, DOSVM_Int2aHandler, 0,
61 /* 2C */ 0, 0, 0, DOSVM_Int2fHandler,
62 /* 30 */ 0, DOSVM_Int31Handler, 0, 0,
63 /* 34 */ DOSVM_Int34Handler, DOSVM_Int35Handler, DOSVM_Int36Handler, DOSVM_Int37Handler,
64 /* 38 */ DOSVM_Int38Handler, DOSVM_Int39Handler, DOSVM_Int3aHandler, DOSVM_Int3bHandler,
65 /* 3C */ DOSVM_Int3cHandler, DOSVM_Int3dHandler, DOSVM_Int3eHandler, 0,
66 /* 40 */ 0, DOSVM_Int41Handler, 0, 0,
67 /* 44 */ 0, 0, 0, 0,
68 /* 48 */ 0, 0, 0, DOSVM_Int4bHandler,
69 /* 4C */ 0, 0, 0, 0,
70 /* 50 */ 0, 0, 0, 0,
71 /* 54 */ 0, 0, 0, 0,
72 /* 58 */ 0, 0, 0, 0,
73 /* 5C */ DOSVM_Int5cHandler
78 * Sizes of real mode and protected mode interrupt stubs.
80 #define DOSVM_STUB_PM16 5
83 /**********************************************************************
84 * DOSVM_DefaultHandler
86 * Default interrupt handler. This will be used to emulate all
87 * interrupts that don't have their own interrupt handler.
89 static void WINAPI DOSVM_DefaultHandler( CONTEXT *context )
94 /**********************************************************************
95 * DOSVM_Exit
97 void DOSVM_Exit( WORD retval )
99 ExitThread( retval );
103 /**********************************************************************
104 * DOSVM_GetBuiltinHandler
106 * Return Wine interrupt handler procedure for a given interrupt.
108 static INTPROC DOSVM_GetBuiltinHandler( BYTE intnum )
110 if (intnum < ARRAY_SIZE(DOSVM_VectorsBuiltin)) {
111 INTPROC proc = DOSVM_VectorsBuiltin[intnum];
112 if (proc)
113 return proc;
116 WARN("int%x not implemented, returning dummy handler\n", intnum );
117 return DOSVM_DefaultHandler;
120 /* Set up the context so that we will call __wine_call_int_handler16 upon
121 * resuming execution.
123 * We can't just call the interrupt handler directly, since some code (in
124 * particular, LoadModule16) assumes that it's running on the 32-bit stack and
125 * that CURRENT_STACK16 points to the bottom of the used 16-bit stack. */
126 static void return_to_interrupt_handler( CONTEXT *context, BYTE intnum )
128 FARPROC16 addr = GetProcAddress16( GetModuleHandle16( "KERNEL" ), "__wine_call_int_handler" );
129 WORD *stack = ldt_get_ptr( context->SegSs, context->Esp );
131 *--stack = intnum;
132 *--stack = context->SegCs;
133 *--stack = context->Eip;
134 context->Esp -= 3 * sizeof(WORD);
135 context->SegCs = SELECTOROF(addr);
136 context->Eip = OFFSETOF(addr);
139 /**********************************************************************
140 * DOSVM_PushFlags
142 * This routine is used to make default int25 and int26 handlers leave the
143 * original eflags into stack. In order to do this, stack is manipulated
144 * so that it actually contains two copies of eflags, one of which is
145 * popped during return from interrupt handler.
147 static void DOSVM_PushFlags( CONTEXT *context, BOOL islong, BOOL isstub )
149 if (islong)
151 DWORD *stack = CTX_SEG_OFF_TO_LIN(context,
152 context->SegSs,
153 context->Esp);
154 context->Esp += -4; /* One item will be added to stack. */
156 if (isstub)
158 DWORD ip = stack[0];
159 DWORD cs = stack[1];
160 stack += 2; /* Pop ip and cs. */
161 *(--stack) = context->EFlags;
162 *(--stack) = cs;
163 *(--stack) = ip;
165 else
166 *(--stack) = context->EFlags;
168 else
170 WORD *stack = CTX_SEG_OFF_TO_LIN(context,
171 context->SegSs,
172 context->Esp);
173 ADD_LOWORD( context->Esp, -2 ); /* One item will be added to stack. */
175 if (isstub)
177 WORD ip = stack[0];
178 WORD cs = stack[1];
179 stack += 2; /* Pop ip and cs. */
180 *(--stack) = LOWORD(context->EFlags);
181 *(--stack) = cs;
182 *(--stack) = ip;
184 else
185 *(--stack) = LOWORD(context->EFlags);
190 /**********************************************************************
191 * DOSVM_HardwareInterruptPM
193 * Emulate call to interrupt handler in 16-bit or 32-bit protected mode.
195 * Pushes interrupt frame to stack and changes instruction
196 * pointer to interrupt handler.
198 static void DOSVM_HardwareInterruptPM( CONTEXT *context, BYTE intnum )
200 FARPROC16 addr = DOSVM_GetPMHandler16( intnum );
202 if (SELECTOROF(addr) == int16_sel)
204 TRACE( "builtin interrupt %02x has been invoked "
205 "(through vector %02x)\n",
206 OFFSETOF(addr)/DOSVM_STUB_PM16, intnum );
208 if (intnum == 0x25 || intnum == 0x26)
209 DOSVM_PushFlags( context, FALSE, FALSE );
211 return_to_interrupt_handler( context, OFFSETOF(addr) / DOSVM_STUB_PM16 );
213 else
215 TRACE( "invoking hooked interrupt %02x at %04x:%04x\n",
216 intnum, SELECTOROF(addr), OFFSETOF(addr) );
218 /* Push the flags and return address on the stack */
219 PUSH_WORD16( context, LOWORD(context->EFlags) );
220 PUSH_WORD16( context, context->SegCs );
221 PUSH_WORD16( context, LOWORD(context->Eip) );
223 /* Jump to the interrupt handler */
224 context->SegCs = HIWORD(addr);
225 context->Eip = LOWORD(addr);
230 /**********************************************************************
231 * DOSVM_EmulateInterruptPM
233 * Emulate software interrupt in 16-bit or 32-bit protected mode.
234 * Called from signal handler when intXX opcode is executed.
236 * Pushes interrupt frame to stack and changes instruction
237 * pointer to interrupt handler.
239 BOOL DOSVM_EmulateInterruptPM( CONTEXT *context, BYTE intnum )
241 TRACE_(relay)("\1Call DOS int 0x%02x ret=%04lx:%08lx\n"
242 " eax=%08lx ebx=%08lx ecx=%08lx edx=%08lx\n"
243 " esi=%08lx edi=%08lx ebp=%08lx esp=%08lx\n"
244 " ds=%04lx es=%04lx fs=%04lx gs=%04lx ss=%04lx flags=%08lx\n",
245 intnum, context->SegCs, context->Eip,
246 context->Eax, context->Ebx, context->Ecx, context->Edx,
247 context->Esi, context->Edi, context->Ebp, context->Esp,
248 context->SegDs, context->SegEs, context->SegFs, context->SegGs,
249 context->SegSs, context->EFlags );
251 DOSMEM_InitDosMemory();
253 if (context->SegCs == int16_sel)
255 /* Restore original flags stored into the stack by the caller. */
256 WORD *stack = CTX_SEG_OFF_TO_LIN(context,
257 context->SegSs, context->Esp);
258 context->EFlags = (DWORD)MAKELONG( stack[2], HIWORD(context->EFlags) );
260 if (intnum != context->Eip / DOSVM_STUB_PM16)
261 WARN( "interrupt stub has been modified "
262 "(interrupt is %02x, interrupt stub is %02lx)\n",
263 intnum, context->Eip/DOSVM_STUB_PM16 );
265 TRACE( "builtin interrupt %02x has been branched to\n", intnum );
267 if (intnum == 0x25 || intnum == 0x26)
268 DOSVM_PushFlags( context, FALSE, TRUE );
270 return_to_interrupt_handler( context, intnum );
272 else if (ldt_is_system(context->SegCs))
274 INTPROC proc;
275 if (intnum >= ARRAY_SIZE(DOSVM_VectorsBuiltin)) return FALSE;
276 if (!(proc = DOSVM_VectorsBuiltin[intnum])) return FALSE;
277 proc( context );
279 else
281 DOSVM_HardwareInterruptPM( context, intnum );
283 return TRUE;
287 /**********************************************************************
288 * DOSVM_GetPMHandler16
290 * Return the protected mode interrupt vector for a given interrupt.
292 FARPROC16 DOSVM_GetPMHandler16( BYTE intnum )
294 TDB *pTask;
295 FARPROC16 proc = 0;
297 pTask = GlobalLock16(GetCurrentTask());
298 if (pTask)
300 switch( intnum )
302 case 0x00:
303 proc = pTask->int0;
304 break;
305 case 0x02:
306 proc = pTask->int2;
307 break;
308 case 0x04:
309 proc = pTask->int4;
310 break;
311 case 0x06:
312 proc = pTask->int6;
313 break;
314 case 0x07:
315 proc = pTask->int7;
316 break;
317 case 0x3e:
318 proc = pTask->int3e;
319 break;
320 case 0x75:
321 proc = pTask->int75;
322 break;
324 if( proc )
325 return proc;
327 if (!DOSVM_Vectors16[intnum])
329 proc = (FARPROC16)MAKESEGPTR( int16_sel, DOSVM_STUB_PM16 * intnum );
330 DOSVM_Vectors16[intnum] = proc;
332 return DOSVM_Vectors16[intnum];
336 /**********************************************************************
337 * DOSVM_SetPMHandler16
339 * Set the protected mode interrupt handler for a given interrupt.
341 void DOSVM_SetPMHandler16( BYTE intnum, FARPROC16 handler )
343 TDB *pTask;
345 TRACE("Set protected mode interrupt vector %02x <- %04x:%04x\n",
346 intnum, HIWORD(handler), LOWORD(handler) );
348 pTask = GlobalLock16(GetCurrentTask());
349 if (!pTask)
350 return;
351 switch( intnum )
353 case 0x00:
354 pTask->int0 = handler;
355 break;
356 case 0x02:
357 pTask->int2 = handler;
358 break;
359 case 0x04:
360 pTask->int4 = handler;
361 break;
362 case 0x06:
363 pTask->int6 = handler;
364 break;
365 case 0x07:
366 pTask->int7 = handler;
367 break;
368 case 0x3e:
369 pTask->int3e = handler;
370 break;
371 case 0x75:
372 pTask->int75 = handler;
373 break;
374 default:
375 DOSVM_Vectors16[intnum] = handler;
376 break;
381 /**********************************************************************
382 * DOSVM_CallBuiltinHandler
384 * Execute Wine interrupt handler procedure.
386 static void DOSVM_CallBuiltinHandler( CONTEXT *context, BYTE intnum )
389 * FIXME: Make all builtin interrupt calls go via this routine.
390 * FIXME: Check for PM->RM interrupt reflection.
391 * FIXME: Check for RM->PM interrupt reflection.
394 INTPROC proc = DOSVM_GetBuiltinHandler( intnum );
395 proc( context );
399 /**********************************************************************
400 * __wine_call_int_handler16 (KERNEL.@)
402 void WINAPI __wine_call_int_handler16( BYTE intnum, CONTEXT *context )
404 DOSMEM_InitDosMemory();
405 DOSVM_CallBuiltinHandler( context, intnum );
409 /**********************************************************************
410 * DOSVM_Int11Handler
412 * Handler for int 11h (get equipment list).
415 * Borrowed from Ralph Brown's interrupt lists:
417 * bits 15-14: number of parallel devices
418 * bit 13: [Conv] Internal modem
419 * bit 12: reserved
420 * bits 11- 9: number of serial devices
421 * bit 8: reserved
422 * bits 7- 6: number of diskette drives minus one
423 * bits 5- 4: Initial video mode:
424 * 00b = EGA,VGA,PGA
425 * 01b = 40 x 25 color
426 * 10b = 80 x 25 color
427 * 11b = 80 x 25 mono
428 * bit 3: reserved
429 * bit 2: [PS] =1 if pointing device
430 * [non-PS] reserved
431 * bit 1: =1 if math co-processor
432 * bit 0: =1 if diskette available for boot
435 * Currently the only of these bits correctly set are:
437 * bits 15-14 } Added by William Owen Smith,
438 * bits 11-9 } wos@dcs.warwick.ac.uk
439 * bits 7-6
440 * bit 2 (always set) ( bit 2 = 4 )
441 * bit 1 } Robert 'Admiral' Coeyman
442 * All *nix systems either have a math processor or
443 * emulate one.
445 static void WINAPI DOSVM_Int11Handler( CONTEXT *context )
447 int diskdrives = 0;
448 int parallelports = 0;
449 int serialports = 0;
450 int x;
452 if (GetDriveTypeA("A:\\") == DRIVE_REMOVABLE) diskdrives++;
453 if (GetDriveTypeA("B:\\") == DRIVE_REMOVABLE) diskdrives++;
454 if (diskdrives) diskdrives--;
456 for (x=0; x < 9; x++)
458 HANDLE handle;
459 char file[10];
461 /* serial port name */
462 sprintf( file, "\\\\.\\COM%d", x+1 );
463 handle = CreateFileA( file, 0, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0 );
464 if (handle != INVALID_HANDLE_VALUE)
466 CloseHandle( handle );
467 serialports++;
470 sprintf( file, "\\\\.\\LPT%d", x+1 );
471 handle = CreateFileA( file, 0, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0 );
472 if (handle != INVALID_HANDLE_VALUE)
474 CloseHandle( handle );
475 parallelports++;
479 if (serialports > 7) /* 3 bits -- maximum value = 7 */
480 serialports = 7;
482 if (parallelports > 3) /* 2 bits -- maximum value = 3 */
483 parallelports = 3;
485 SET_AX( context,
486 (diskdrives << 6) | (serialports << 9) | (parallelports << 14) | 0x06 );
490 /**********************************************************************
491 * DOSVM_Int12Handler
493 * Handler for int 12h (get memory size).
495 static void WINAPI DOSVM_Int12Handler( CONTEXT *context )
497 SET_AX( context, 640 );
501 /**********************************************************************
502 * DOSVM_Int17Handler
504 * Handler for int 17h (printer - output character).
506 static void WINAPI DOSVM_Int17Handler( CONTEXT *context )
508 switch( AH_reg(context) )
510 case 0x00:/* Send character*/
511 FIXME("Send character not supported yet\n");
512 SET_AH( context, 0x00 );/*Timeout*/
513 break;
514 case 0x01: /* PRINTER - INITIALIZE */
515 FIXME("Initialize Printer - Not Supported\n");
516 SET_AH( context, 0x30 ); /* selected | out of paper */
517 break;
518 case 0x02: /* PRINTER - GET STATUS */
519 FIXME("Get Printer Status - Not Supported\n");
520 break;
521 default:
522 SET_AH( context, 0 ); /* time out */
523 INT_BARF( context, 0x17 );
528 /**********************************************************************
529 * DOSVM_Int19Handler
531 * Handler for int 19h (Reboot).
533 static void WINAPI DOSVM_Int19Handler( CONTEXT *context )
535 TRACE( "Attempted Reboot\n" );
536 ExitProcess(0);
540 /**********************************************************************
541 * DOSVM_Int1aHandler
543 * Handler for int 1ah.
545 static void WINAPI DOSVM_Int1aHandler( CONTEXT *context )
547 switch(AH_reg(context))
549 case 0x00: /* GET SYSTEM TIME */
551 BIOSDATA *data = DOSVM_BiosData();
553 DOSVM_start_bios_timer();
554 SET_CX( context, HIWORD(data->Ticks) );
555 SET_DX( context, LOWORD(data->Ticks) );
556 SET_AL( context, 0 ); /* FIXME: midnight flag is unsupported */
557 TRACE( "GET SYSTEM TIME - ticks=%ld\n", data->Ticks );
559 break;
561 case 0x01: /* SET SYSTEM TIME */
562 FIXME( "SET SYSTEM TIME - not allowed\n" );
563 break;
565 case 0x02: /* GET REAL-TIME CLOCK TIME */
566 TRACE( "GET REAL-TIME CLOCK TIME\n" );
568 SYSTEMTIME systime;
569 GetLocalTime( &systime );
570 SET_CH( context, BIN_TO_BCD(systime.wHour) );
571 SET_CL( context, BIN_TO_BCD(systime.wMinute) );
572 SET_DH( context, BIN_TO_BCD(systime.wSecond) );
573 SET_DL( context, 0 ); /* FIXME: assume no daylight saving */
574 RESET_CFLAG(context);
576 break;
578 case 0x03: /* SET REAL-TIME CLOCK TIME */
579 FIXME( "SET REAL-TIME CLOCK TIME - not allowed\n" );
580 break;
582 case 0x04: /* GET REAL-TIME CLOCK DATE */
583 TRACE( "GET REAL-TIME CLOCK DATE\n" );
585 SYSTEMTIME systime;
586 GetLocalTime( &systime );
587 SET_CH( context, BIN_TO_BCD(systime.wYear / 100) );
588 SET_CL( context, BIN_TO_BCD(systime.wYear % 100) );
589 SET_DH( context, BIN_TO_BCD(systime.wMonth) );
590 SET_DL( context, BIN_TO_BCD(systime.wDay) );
591 RESET_CFLAG(context);
593 break;
595 case 0x05: /* SET REAL-TIME CLOCK DATE */
596 FIXME( "SET REAL-TIME CLOCK DATE - not allowed\n" );
597 break;
599 case 0x06: /* SET ALARM */
600 FIXME( "SET ALARM - unimplemented\n" );
601 break;
603 case 0x07: /* CANCEL ALARM */
604 FIXME( "CANCEL ALARM - unimplemented\n" );
605 break;
607 case 0x08: /* SET RTC ACTIVATED POWER ON MODE */
608 case 0x09: /* READ RTC ALARM TIME AND STATUS */
609 case 0x0a: /* READ SYSTEM-TIMER DAY COUNTER */
610 case 0x0b: /* SET SYSTEM-TIMER DAY COUNTER */
611 case 0x0c: /* SET RTC DATE/TIME ACTIVATED POWER-ON MODE */
612 case 0x0d: /* RESET RTC DATE/TIME ACTIVATED POWER-ON MODE */
613 case 0x0e: /* GET RTC DATE/TIME ALARM AND STATUS */
614 case 0x0f: /* INITIALIZE REAL-TIME CLOCK */
615 INT_BARF( context, 0x1a );
616 break;
618 case 0xb0:
619 if (CX_reg(context) == 0x4d52 &&
620 DX_reg(context) == 0x4349 &&
621 AL_reg(context) == 0x01)
624 * Microsoft Real-Time Compression Interface (MRCI).
625 * Ignoring this call indicates MRCI is not supported.
627 TRACE( "Microsoft Real-Time Compression Interface - not supported\n" );
629 else
631 INT_BARF(context, 0x1a);
633 break;
635 default:
636 INT_BARF( context, 0x1a );
641 /**********************************************************************
642 * DOSVM_Int20Handler
644 * Handler for int 20h.
646 static void WINAPI DOSVM_Int20Handler( CONTEXT *context )
648 DOSVM_Exit( 0 );
652 /**********************************************************************
653 * DOSVM_Int2aHandler
655 * Handler for int 2ah (network).
657 static void WINAPI DOSVM_Int2aHandler( CONTEXT *context )
659 switch(AH_reg(context))
661 case 0x00: /* NETWORK INSTALLATION CHECK */
662 break;
664 default:
665 INT_BARF( context, 0x2a );
670 /***********************************************************************
671 * DOSVM_Int41Handler
673 static void WINAPI DOSVM_Int41Handler( CONTEXT *context )
675 switch ( AX_reg(context) )
677 case 0x4f:
678 case 0x50:
679 case 0x150:
680 case 0x51:
681 case 0x52:
682 case 0x152:
683 case 0x59:
684 case 0x5a:
685 case 0x5b:
686 case 0x5c:
687 case 0x5d:
688 /* Notifies the debugger of a lot of stuff. We simply ignore it
689 for now, but some of the info might actually be useful ... */
690 break;
692 default:
693 INT_BARF( context, 0x41 );
694 break;
699 /***********************************************************************
700 * DOSVM_Int4bHandler
703 static void WINAPI DOSVM_Int4bHandler( CONTEXT *context )
705 switch(AH_reg(context))
707 case 0x81: /* Virtual DMA Spec (IBM SCSI interface) */
708 if(AL_reg(context) != 0x02) /* if not install check */
710 SET_CFLAG(context);
711 SET_AL( context, 0x0f ); /* function is not implemented */
713 break;
714 default:
715 INT_BARF(context, 0x4b);
720 /***********************************************************************
721 * DOSVM_Int5cHandler
723 * Called from NetBIOSCall16.
725 static void WINAPI DOSVM_Int5cHandler( CONTEXT *context )
727 BYTE* ptr;
728 ptr = MapSL( MAKESEGPTR(context->SegEs,BX_reg(context)) );
729 FIXME("(%p): command code %02x (ignored)\n",context, *ptr);
730 *(ptr+0x01) = 0xFB; /* NetBIOS emulator not found */
731 SET_AL( context, 0xFB );