widl: Generate helper macros for WinRT implementation.
[wine/zf.git] / dlls / krnl386.exe16 / interrupts.c
blobe2a129e4a20d53b419fc3e3bc70c30cfa8732732
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 DWORD count;
101 ReleaseThunkLock( &count );
102 ExitThread( retval );
106 /**********************************************************************
107 * DOSVM_GetBuiltinHandler
109 * Return Wine interrupt handler procedure for a given interrupt.
111 static INTPROC DOSVM_GetBuiltinHandler( BYTE intnum )
113 if (intnum < ARRAY_SIZE(DOSVM_VectorsBuiltin)) {
114 INTPROC proc = DOSVM_VectorsBuiltin[intnum];
115 if (proc)
116 return proc;
119 WARN("int%x not implemented, returning dummy handler\n", intnum );
120 return DOSVM_DefaultHandler;
124 /**********************************************************************
125 * DOSVM_IntProcRelay
127 * Simple DOSRELAY that interprets its argument as INTPROC and calls it.
129 static void DOSVM_IntProcRelay( CONTEXT *context, LPVOID data )
131 INTPROC proc = (INTPROC)data;
132 proc(context);
136 /**********************************************************************
137 * DOSVM_PushFlags
139 * This routine is used to make default int25 and int26 handlers leave the
140 * original eflags into stack. In order to do this, stack is manipulated
141 * so that it actually contains two copies of eflags, one of which is
142 * popped during return from interrupt handler.
144 static void DOSVM_PushFlags( CONTEXT *context, BOOL islong, BOOL isstub )
146 if (islong)
148 DWORD *stack = CTX_SEG_OFF_TO_LIN(context,
149 context->SegSs,
150 context->Esp);
151 context->Esp += -4; /* One item will be added to stack. */
153 if (isstub)
155 DWORD ip = stack[0];
156 DWORD cs = stack[1];
157 stack += 2; /* Pop ip and cs. */
158 *(--stack) = context->EFlags;
159 *(--stack) = cs;
160 *(--stack) = ip;
162 else
163 *(--stack) = context->EFlags;
165 else
167 WORD *stack = CTX_SEG_OFF_TO_LIN(context,
168 context->SegSs,
169 context->Esp);
170 ADD_LOWORD( context->Esp, -2 ); /* One item will be added to stack. */
172 if (isstub)
174 WORD ip = stack[0];
175 WORD cs = stack[1];
176 stack += 2; /* Pop ip and cs. */
177 *(--stack) = LOWORD(context->EFlags);
178 *(--stack) = cs;
179 *(--stack) = ip;
181 else
182 *(--stack) = LOWORD(context->EFlags);
187 /**********************************************************************
188 * DOSVM_HardwareInterruptPM
190 * Emulate call to interrupt handler in 16-bit or 32-bit protected mode.
192 * Pushes interrupt frame to stack and changes instruction
193 * pointer to interrupt handler.
195 static void DOSVM_HardwareInterruptPM( CONTEXT *context, BYTE intnum )
197 FARPROC16 addr = DOSVM_GetPMHandler16( intnum );
199 if (SELECTOROF(addr) == int16_sel)
201 TRACE( "builtin interrupt %02x has been invoked "
202 "(through vector %02x)\n",
203 OFFSETOF(addr)/DOSVM_STUB_PM16, intnum );
205 if (intnum == 0x25 || intnum == 0x26)
206 DOSVM_PushFlags( context, FALSE, FALSE );
208 DOSVM_BuildCallFrame( context,
209 DOSVM_IntProcRelay,
210 DOSVM_GetBuiltinHandler(
211 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=%04x:%08x\n"
242 " eax=%08x ebx=%08x ecx=%08x edx=%08x\n"
243 " esi=%08x edi=%08x ebp=%08x esp=%08x\n"
244 " ds=%04x es=%04x fs=%04x gs=%04x ss=%04x flags=%08x\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 == relay_code_sel)
256 * This must not be called using DOSVM_BuildCallFrame.
258 DOSVM_RelayHandler( context );
260 else if (context->SegCs == int16_sel)
262 /* Restore original flags stored into the stack by the caller. */
263 WORD *stack = CTX_SEG_OFF_TO_LIN(context,
264 context->SegSs, context->Esp);
265 context->EFlags = (DWORD)MAKELONG( stack[2], HIWORD(context->EFlags) );
267 if (intnum != context->Eip / DOSVM_STUB_PM16)
268 WARN( "interrupt stub has been modified "
269 "(interrupt is %02x, interrupt stub is %02x)\n",
270 intnum, context->Eip/DOSVM_STUB_PM16 );
272 TRACE( "builtin interrupt %02x has been branched to\n", intnum );
274 if (intnum == 0x25 || intnum == 0x26)
275 DOSVM_PushFlags( context, FALSE, TRUE );
277 DOSVM_BuildCallFrame( context,
278 DOSVM_IntProcRelay,
279 DOSVM_GetBuiltinHandler(intnum) );
281 else if (ldt_is_system(context->SegCs))
283 INTPROC proc;
284 if (intnum >= ARRAY_SIZE(DOSVM_VectorsBuiltin)) return FALSE;
285 if (!(proc = DOSVM_VectorsBuiltin[intnum])) return FALSE;
286 proc( context );
288 else
290 DOSVM_HardwareInterruptPM( context, intnum );
292 return TRUE;
296 /**********************************************************************
297 * DOSVM_GetPMHandler16
299 * Return the protected mode interrupt vector for a given interrupt.
301 FARPROC16 DOSVM_GetPMHandler16( BYTE intnum )
303 TDB *pTask;
304 FARPROC16 proc = 0;
306 pTask = GlobalLock16(GetCurrentTask());
307 if (pTask)
309 switch( intnum )
311 case 0x00:
312 proc = pTask->int0;
313 break;
314 case 0x02:
315 proc = pTask->int2;
316 break;
317 case 0x04:
318 proc = pTask->int4;
319 break;
320 case 0x06:
321 proc = pTask->int6;
322 break;
323 case 0x07:
324 proc = pTask->int7;
325 break;
326 case 0x3e:
327 proc = pTask->int3e;
328 break;
329 case 0x75:
330 proc = pTask->int75;
331 break;
333 if( proc )
334 return proc;
336 if (!DOSVM_Vectors16[intnum])
338 proc = (FARPROC16)MAKESEGPTR( int16_sel, DOSVM_STUB_PM16 * intnum );
339 DOSVM_Vectors16[intnum] = proc;
341 return DOSVM_Vectors16[intnum];
345 /**********************************************************************
346 * DOSVM_SetPMHandler16
348 * Set the protected mode interrupt handler for a given interrupt.
350 void DOSVM_SetPMHandler16( BYTE intnum, FARPROC16 handler )
352 TDB *pTask;
354 TRACE("Set protected mode interrupt vector %02x <- %04x:%04x\n",
355 intnum, HIWORD(handler), LOWORD(handler) );
357 pTask = GlobalLock16(GetCurrentTask());
358 if (!pTask)
359 return;
360 switch( intnum )
362 case 0x00:
363 pTask->int0 = handler;
364 break;
365 case 0x02:
366 pTask->int2 = handler;
367 break;
368 case 0x04:
369 pTask->int4 = handler;
370 break;
371 case 0x06:
372 pTask->int6 = handler;
373 break;
374 case 0x07:
375 pTask->int7 = handler;
376 break;
377 case 0x3e:
378 pTask->int3e = handler;
379 break;
380 case 0x75:
381 pTask->int75 = handler;
382 break;
383 default:
384 DOSVM_Vectors16[intnum] = handler;
385 break;
390 /**********************************************************************
391 * DOSVM_CallBuiltinHandler
393 * Execute Wine interrupt handler procedure.
395 static void DOSVM_CallBuiltinHandler( CONTEXT *context, BYTE intnum )
398 * FIXME: Make all builtin interrupt calls go via this routine.
399 * FIXME: Check for PM->RM interrupt reflection.
400 * FIXME: Check for RM->PM interrupt reflection.
403 INTPROC proc = DOSVM_GetBuiltinHandler( intnum );
404 proc( context );
408 /**********************************************************************
409 * __wine_call_int_handler (KERNEL.@)
411 void __wine_call_int_handler( CONTEXT *context, BYTE intnum )
413 DOSMEM_InitDosMemory();
414 DOSVM_CallBuiltinHandler( context, intnum );
418 /**********************************************************************
419 * DOSVM_Int11Handler
421 * Handler for int 11h (get equipment list).
424 * Borrowed from Ralph Brown's interrupt lists:
426 * bits 15-14: number of parallel devices
427 * bit 13: [Conv] Internal modem
428 * bit 12: reserved
429 * bits 11- 9: number of serial devices
430 * bit 8: reserved
431 * bits 7- 6: number of diskette drives minus one
432 * bits 5- 4: Initial video mode:
433 * 00b = EGA,VGA,PGA
434 * 01b = 40 x 25 color
435 * 10b = 80 x 25 color
436 * 11b = 80 x 25 mono
437 * bit 3: reserved
438 * bit 2: [PS] =1 if pointing device
439 * [non-PS] reserved
440 * bit 1: =1 if math co-processor
441 * bit 0: =1 if diskette available for boot
444 * Currently the only of these bits correctly set are:
446 * bits 15-14 } Added by William Owen Smith,
447 * bits 11-9 } wos@dcs.warwick.ac.uk
448 * bits 7-6
449 * bit 2 (always set) ( bit 2 = 4 )
450 * bit 1 } Robert 'Admiral' Coeyman
451 * All *nix systems either have a math processor or
452 * emulate one.
454 static void WINAPI DOSVM_Int11Handler( CONTEXT *context )
456 int diskdrives = 0;
457 int parallelports = 0;
458 int serialports = 0;
459 int x;
461 if (GetDriveTypeA("A:\\") == DRIVE_REMOVABLE) diskdrives++;
462 if (GetDriveTypeA("B:\\") == DRIVE_REMOVABLE) diskdrives++;
463 if (diskdrives) diskdrives--;
465 for (x=0; x < 9; x++)
467 HANDLE handle;
468 char file[10];
470 /* serial port name */
471 sprintf( file, "\\\\.\\COM%d", x+1 );
472 handle = CreateFileA( file, 0, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0 );
473 if (handle != INVALID_HANDLE_VALUE)
475 CloseHandle( handle );
476 serialports++;
479 sprintf( file, "\\\\.\\LPT%d", x+1 );
480 handle = CreateFileA( file, 0, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0 );
481 if (handle != INVALID_HANDLE_VALUE)
483 CloseHandle( handle );
484 parallelports++;
488 if (serialports > 7) /* 3 bits -- maximum value = 7 */
489 serialports = 7;
491 if (parallelports > 3) /* 2 bits -- maximum value = 3 */
492 parallelports = 3;
494 SET_AX( context,
495 (diskdrives << 6) | (serialports << 9) | (parallelports << 14) | 0x06 );
499 /**********************************************************************
500 * DOSVM_Int12Handler
502 * Handler for int 12h (get memory size).
504 static void WINAPI DOSVM_Int12Handler( CONTEXT *context )
506 SET_AX( context, 640 );
510 /**********************************************************************
511 * DOSVM_Int17Handler
513 * Handler for int 17h (printer - output character).
515 static void WINAPI DOSVM_Int17Handler( CONTEXT *context )
517 switch( AH_reg(context) )
519 case 0x00:/* Send character*/
520 FIXME("Send character not supported yet\n");
521 SET_AH( context, 0x00 );/*Timeout*/
522 break;
523 case 0x01: /* PRINTER - INITIALIZE */
524 FIXME("Initialize Printer - Not Supported\n");
525 SET_AH( context, 0x30 ); /* selected | out of paper */
526 break;
527 case 0x02: /* PRINTER - GET STATUS */
528 FIXME("Get Printer Status - Not Supported\n");
529 break;
530 default:
531 SET_AH( context, 0 ); /* time out */
532 INT_BARF( context, 0x17 );
537 /**********************************************************************
538 * DOSVM_Int19Handler
540 * Handler for int 19h (Reboot).
542 static void WINAPI DOSVM_Int19Handler( CONTEXT *context )
544 TRACE( "Attempted Reboot\n" );
545 ExitProcess(0);
549 /**********************************************************************
550 * DOSVM_Int1aHandler
552 * Handler for int 1ah.
554 static void WINAPI DOSVM_Int1aHandler( CONTEXT *context )
556 switch(AH_reg(context))
558 case 0x00: /* GET SYSTEM TIME */
560 BIOSDATA *data = DOSVM_BiosData();
561 SET_CX( context, HIWORD(data->Ticks) );
562 SET_DX( context, LOWORD(data->Ticks) );
563 SET_AL( context, 0 ); /* FIXME: midnight flag is unsupported */
564 TRACE( "GET SYSTEM TIME - ticks=%d\n", data->Ticks );
566 break;
568 case 0x01: /* SET SYSTEM TIME */
569 FIXME( "SET SYSTEM TIME - not allowed\n" );
570 break;
572 case 0x02: /* GET REAL-TIME CLOCK TIME */
573 TRACE( "GET REAL-TIME CLOCK TIME\n" );
575 SYSTEMTIME systime;
576 GetLocalTime( &systime );
577 SET_CH( context, BIN_TO_BCD(systime.wHour) );
578 SET_CL( context, BIN_TO_BCD(systime.wMinute) );
579 SET_DH( context, BIN_TO_BCD(systime.wSecond) );
580 SET_DL( context, 0 ); /* FIXME: assume no daylight saving */
581 RESET_CFLAG(context);
583 break;
585 case 0x03: /* SET REAL-TIME CLOCK TIME */
586 FIXME( "SET REAL-TIME CLOCK TIME - not allowed\n" );
587 break;
589 case 0x04: /* GET REAL-TIME CLOCK DATE */
590 TRACE( "GET REAL-TIME CLOCK DATE\n" );
592 SYSTEMTIME systime;
593 GetLocalTime( &systime );
594 SET_CH( context, BIN_TO_BCD(systime.wYear / 100) );
595 SET_CL( context, BIN_TO_BCD(systime.wYear % 100) );
596 SET_DH( context, BIN_TO_BCD(systime.wMonth) );
597 SET_DL( context, BIN_TO_BCD(systime.wDay) );
598 RESET_CFLAG(context);
600 break;
602 case 0x05: /* SET REAL-TIME CLOCK DATE */
603 FIXME( "SET REAL-TIME CLOCK DATE - not allowed\n" );
604 break;
606 case 0x06: /* SET ALARM */
607 FIXME( "SET ALARM - unimplemented\n" );
608 break;
610 case 0x07: /* CANCEL ALARM */
611 FIXME( "CANCEL ALARM - unimplemented\n" );
612 break;
614 case 0x08: /* SET RTC ACTIVATED POWER ON MODE */
615 case 0x09: /* READ RTC ALARM TIME AND STATUS */
616 case 0x0a: /* READ SYSTEM-TIMER DAY COUNTER */
617 case 0x0b: /* SET SYSTEM-TIMER DAY COUNTER */
618 case 0x0c: /* SET RTC DATE/TIME ACTIVATED POWER-ON MODE */
619 case 0x0d: /* RESET RTC DATE/TIME ACTIVATED POWER-ON MODE */
620 case 0x0e: /* GET RTC DATE/TIME ALARM AND STATUS */
621 case 0x0f: /* INITIALIZE REAL-TIME CLOCK */
622 INT_BARF( context, 0x1a );
623 break;
625 case 0xb0:
626 if (CX_reg(context) == 0x4d52 &&
627 DX_reg(context) == 0x4349 &&
628 AL_reg(context) == 0x01)
631 * Microsoft Real-Time Compression Interface (MRCI).
632 * Ignoring this call indicates MRCI is not supported.
634 TRACE( "Microsoft Real-Time Compression Interface - not supported\n" );
636 else
638 INT_BARF(context, 0x1a);
640 break;
642 default:
643 INT_BARF( context, 0x1a );
648 /**********************************************************************
649 * DOSVM_Int20Handler
651 * Handler for int 20h.
653 static void WINAPI DOSVM_Int20Handler( CONTEXT *context )
655 DOSVM_Exit( 0 );
659 /**********************************************************************
660 * DOSVM_Int2aHandler
662 * Handler for int 2ah (network).
664 static void WINAPI DOSVM_Int2aHandler( CONTEXT *context )
666 switch(AH_reg(context))
668 case 0x00: /* NETWORK INSTALLATION CHECK */
669 break;
671 default:
672 INT_BARF( context, 0x2a );
677 /***********************************************************************
678 * DOSVM_Int41Handler
680 static void WINAPI DOSVM_Int41Handler( CONTEXT *context )
682 switch ( AX_reg(context) )
684 case 0x4f:
685 case 0x50:
686 case 0x150:
687 case 0x51:
688 case 0x52:
689 case 0x152:
690 case 0x59:
691 case 0x5a:
692 case 0x5b:
693 case 0x5c:
694 case 0x5d:
695 /* Notifies the debugger of a lot of stuff. We simply ignore it
696 for now, but some of the info might actually be useful ... */
697 break;
699 default:
700 INT_BARF( context, 0x41 );
701 break;
706 /***********************************************************************
707 * DOSVM_Int4bHandler
710 static void WINAPI DOSVM_Int4bHandler( CONTEXT *context )
712 switch(AH_reg(context))
714 case 0x81: /* Virtual DMA Spec (IBM SCSI interface) */
715 if(AL_reg(context) != 0x02) /* if not install check */
717 SET_CFLAG(context);
718 SET_AL( context, 0x0f ); /* function is not implemented */
720 break;
721 default:
722 INT_BARF(context, 0x4b);
727 /***********************************************************************
728 * DOSVM_Int5cHandler
730 * Called from NetBIOSCall16.
732 static void WINAPI DOSVM_Int5cHandler( CONTEXT *context )
734 BYTE* ptr;
735 ptr = MapSL( MAKESEGPTR(context->SegEs,BX_reg(context)) );
736 FIXME("(%p): command code %02x (ignored)\n",context, *ptr);
737 *(ptr+0x01) = 0xFB; /* NetBIOS emulator not found */
738 SET_AL( context, 0xFB );