fusion: Implement IAssemblyCache::UninstallAssembly.
[wine/multimedia.git] / dlls / krnl386.exe16 / dosvm.c
blob87adf33a1ae8004631cefbde15ee5cddf9f65bb0
1 /*
2 * DOS Virtual Machine
4 * Copyright 1998 Ove Kåven
5 * Copyright 2002 Jukka Heinonen
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 * Note: This code hasn't been completely cleaned up yet.
24 #include "config.h"
25 #include "wine/port.h"
27 #include <stdarg.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <errno.h>
32 #include <fcntl.h>
33 #include <signal.h>
34 #ifdef HAVE_UNISTD_H
35 # include <unistd.h>
36 #endif
37 #ifdef HAVE_SYS_TIME_H
38 # include <sys/time.h>
39 #endif
40 #include <sys/types.h>
42 #include "wine/winbase16.h"
43 #include "wine/exception.h"
44 #include "windef.h"
45 #include "winbase.h"
46 #include "winternl.h"
47 #include "wingdi.h"
48 #include "winuser.h"
49 #include "wownt32.h"
50 #include "winnt.h"
51 #include "wincon.h"
53 #include "dosexe.h"
54 #include "wine/debug.h"
55 #include "excpt.h"
57 WINE_DEFAULT_DEBUG_CHANNEL(int);
58 #ifdef MZ_SUPPORTED
59 WINE_DECLARE_DEBUG_CHANNEL(module);
60 WINE_DECLARE_DEBUG_CHANNEL(relay);
61 #endif
63 WORD DOSVM_psp = 0;
64 WORD DOSVM_retval = 0;
67 * Wine DOS memory layout above 640k:
69 * a0000 - affff : VGA graphics (vga.c)
70 * b0000 - bffff : Monochrome text (unused)
71 * b8000 - bffff : VGA text (vga.c)
72 * c0000 - cffff : EMS frame (int67.c)
73 * d0000 - effff : Free memory for UMBs (himem.c)
74 * f0000 - fffff : BIOS stuff (msdos/dosmem.c)
75 * 100000 -10ffff : High memory area (unused)
79 * Table of real mode segments and protected mode selectors
80 * for code stubs and other miscellaneous storage.
82 struct DPMI_segments *DOSVM_dpmi_segments = NULL;
85 * First and last address available for upper memory blocks.
87 #define DOSVM_UMB_BOTTOM 0xd0000
88 #define DOSVM_UMB_TOP 0xeffff
91 * First free address for upper memory blocks.
93 static DWORD DOSVM_umb_free = DOSVM_UMB_BOTTOM;
96 typedef struct _DOSEVENT {
97 int irq,priority;
98 DOSRELAY relay;
99 void *data;
100 struct _DOSEVENT *next;
101 } DOSEVENT, *LPDOSEVENT;
103 static struct _DOSEVENT *pending_event, *current_event;
104 static HANDLE event_notifier;
106 static CRITICAL_SECTION qcrit;
107 static CRITICAL_SECTION_DEBUG critsect_debug =
109 0, 0, &qcrit,
110 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
111 0, 0, { (DWORD_PTR)(__FILE__ ": qcrit") }
113 static CRITICAL_SECTION qcrit = { &critsect_debug, -1, 0, 0, 0, 0 };
116 /***********************************************************************
117 * DOSVM_HasPendingEvents
119 * Return true if there are pending events that are not
120 * blocked by currently active event.
122 static BOOL DOSVM_HasPendingEvents( void )
124 if (!pending_event)
125 return FALSE;
127 if (!current_event)
128 return TRUE;
130 if (pending_event->priority < current_event->priority)
131 return TRUE;
133 return FALSE;
137 /***********************************************************************
138 * DOSVM_SendOneEvent
140 * Process single pending event.
142 * This function should be called with queue critical section locked.
143 * The function temporarily releases the critical section if it is
144 * possible that internal interrupt handler or user procedure will
145 * be called. This is because we may otherwise get a deadlock if
146 * another thread is waiting for the same critical section.
148 static void DOSVM_SendOneEvent( CONTEXT *context )
150 LPDOSEVENT event = pending_event;
152 /* Remove from pending events list. */
153 pending_event = event->next;
155 /* Process active event. */
156 if (event->irq >= 0)
158 BYTE intnum = (event->irq < 8) ?
159 (event->irq + 8) : (event->irq - 8 + 0x70);
161 /* Event is an IRQ, move it to current events list. */
162 event->next = current_event;
163 current_event = event;
165 TRACE( "Dispatching IRQ %d.\n", event->irq );
167 if (ISV86(context))
170 * Note that if DOSVM_HardwareInterruptRM calls an internal
171 * interrupt directly, current_event might be cleared
172 * (and event freed) in this call.
174 LeaveCriticalSection(&qcrit);
175 DOSVM_HardwareInterruptRM( context, intnum );
176 EnterCriticalSection(&qcrit);
178 else
181 * This routine only modifies current context so it is
182 * not necessary to release critical section.
184 DOSVM_HardwareInterruptPM( context, intnum );
187 else
189 /* Callback event. */
190 TRACE( "Dispatching callback event.\n" );
192 if (ISV86(context))
195 * Call relay immediately in real mode.
197 LeaveCriticalSection(&qcrit);
198 (*event->relay)( context, event->data );
199 EnterCriticalSection(&qcrit);
201 else
204 * Force return to relay code. We do not want to
205 * call relay directly because we may be inside a signal handler.
207 DOSVM_BuildCallFrame( context, event->relay, event->data );
210 HeapFree(GetProcessHeap(), 0, event);
215 /***********************************************************************
216 * DOSVM_SendQueuedEvents
218 * As long as context instruction pointer stays unmodified,
219 * process all pending events that are not blocked by currently
220 * active event.
222 * This routine assumes that caller has already cleared TEB.vm86_pending
223 * and checked that interrupts are enabled.
225 void DOSVM_SendQueuedEvents( CONTEXT *context )
227 DWORD old_cs = context->SegCs;
228 DWORD old_ip = context->Eip;
230 EnterCriticalSection(&qcrit);
232 TRACE( "Called in %s mode %s events pending (time=%d)\n",
233 ISV86(context) ? "real" : "protected",
234 DOSVM_HasPendingEvents() ? "with" : "without",
235 GetTickCount() );
236 TRACE( "cs:ip=%04x:%08x, ss:sp=%04x:%08x\n",
237 context->SegCs, context->Eip, context->SegSs, context->Esp);
239 while (context->SegCs == old_cs &&
240 context->Eip == old_ip &&
241 DOSVM_HasPendingEvents())
243 DOSVM_SendOneEvent(context);
246 * Event handling may have turned pending events flag on.
247 * We disable it here because this prevents some
248 * unnecessary calls to this function.
250 get_vm86_teb_info()->vm86_pending = 0;
253 #ifdef MZ_SUPPORTED
255 if (DOSVM_HasPendingEvents())
258 * Interrupts disabled, but there are still
259 * pending events, make sure that pending flag is turned on.
261 TRACE( "Another event is pending, setting VIP flag.\n" );
262 get_vm86_teb_info()->vm86_pending |= VIP_MASK;
265 #else
267 FIXME("No DOS .exe file support on this platform (yet)\n");
269 #endif /* MZ_SUPPORTED */
271 LeaveCriticalSection(&qcrit);
275 #ifdef MZ_SUPPORTED
276 /***********************************************************************
277 * DOSVM_QueueEvent
279 void DOSVM_QueueEvent( INT irq, INT priority, DOSRELAY relay, LPVOID data)
281 LPDOSEVENT event, cur, prev;
282 BOOL old_pending;
284 if (MZ_Current()) {
285 event = HeapAlloc(GetProcessHeap(), 0, sizeof(DOSEVENT));
286 if (!event) {
287 ERR("out of memory allocating event entry\n");
288 return;
290 event->irq = irq; event->priority = priority;
291 event->relay = relay; event->data = data;
293 EnterCriticalSection(&qcrit);
294 old_pending = DOSVM_HasPendingEvents();
296 /* insert event into linked list, in order *after*
297 * all earlier events of higher or equal priority */
298 cur = pending_event; prev = NULL;
299 while (cur && cur->priority<=priority) {
300 prev = cur;
301 cur = cur->next;
303 event->next = cur;
304 if (prev) prev->next = event;
305 else pending_event = event;
307 if (!old_pending && DOSVM_HasPendingEvents()) {
308 TRACE("new event queued, signalling (time=%d)\n", GetTickCount());
310 /* Alert VM86 thread about the new event. */
311 kill(dosvm_pid,SIGUSR2);
313 /* Wake up DOSVM_Wait so that it can serve pending events. */
314 SetEvent(event_notifier);
315 } else {
316 TRACE("new event queued (time=%d)\n", GetTickCount());
319 LeaveCriticalSection(&qcrit);
320 } else {
321 /* DOS subsystem not running */
322 /* (this probably means that we're running a win16 app
323 * which uses DPMI to thunk down to DOS services) */
324 if (irq<0) {
325 /* callback event, perform it with dummy context */
326 CONTEXT context;
327 memset(&context,0,sizeof(context));
328 (*relay)(&context,data);
329 } else {
330 ERR("IRQ without DOS task: should not happen\n");
335 static void DOSVM_ProcessConsole(void)
337 INPUT_RECORD msg;
338 DWORD res;
339 BYTE scan, ascii;
341 if (ReadConsoleInputA(GetStdHandle(STD_INPUT_HANDLE),&msg,1,&res)) {
342 switch (msg.EventType) {
343 case KEY_EVENT:
344 scan = msg.Event.KeyEvent.wVirtualScanCode;
345 ascii = msg.Event.KeyEvent.uChar.AsciiChar;
346 TRACE("scan %02x, ascii %02x\n", scan, ascii);
348 /* set the "break" (release) flag if key released */
349 if (!msg.Event.KeyEvent.bKeyDown) scan |= 0x80;
351 /* check whether extended bit is set,
352 * and if so, queue the extension prefix */
353 if (msg.Event.KeyEvent.dwControlKeyState & ENHANCED_KEY) {
354 DOSVM_Int09SendScan(0xE0,0);
356 DOSVM_Int09SendScan(scan, ascii);
357 break;
358 case MOUSE_EVENT:
359 DOSVM_Int33Console(&msg.Event.MouseEvent);
360 break;
361 case WINDOW_BUFFER_SIZE_EVENT:
362 FIXME("unhandled WINDOW_BUFFER_SIZE_EVENT.\n");
363 break;
364 case MENU_EVENT:
365 FIXME("unhandled MENU_EVENT.\n");
366 break;
367 case FOCUS_EVENT:
368 FIXME("unhandled FOCUS_EVENT.\n");
369 break;
370 default:
371 FIXME("unknown console event: %d\n", msg.EventType);
376 static void DOSVM_ProcessMessage(MSG *msg)
378 BYTE scan = 0;
380 TRACE("got message %04x, wparam=%08lx, lparam=%08lx\n",msg->message,msg->wParam,msg->lParam);
381 if ((msg->message>=WM_MOUSEFIRST)&&
382 (msg->message<=WM_MOUSELAST)) {
383 DOSVM_Int33Message(msg->message,msg->wParam,msg->lParam);
384 } else {
385 switch (msg->message) {
386 case WM_KEYUP:
387 scan = 0x80;
388 case WM_KEYDOWN:
389 scan |= (msg->lParam >> 16) & 0x7f;
391 /* check whether extended bit is set,
392 * and if so, queue the extension prefix */
393 if (msg->lParam & 0x1000000) {
394 /* FIXME: some keys (function keys) have
395 * extended bit set even when they shouldn't,
396 * should check for them */
397 DOSVM_Int09SendScan(0xE0,0);
399 DOSVM_Int09SendScan(scan,0);
400 break;
406 /***********************************************************************
407 * DOSVM_Wait
409 * Wait for asynchronous events. This routine temporarily enables
410 * interrupts and waits until some asynchronous event has been
411 * processed.
413 void DOSVM_Wait( CONTEXT *waitctx )
415 if (DOSVM_HasPendingEvents())
417 CONTEXT context = *waitctx;
420 * If DOSVM_Wait is called from protected mode we emulate
421 * interrupt reflection and convert context into real mode context.
422 * This is actually the correct thing to do as long as DOSVM_Wait
423 * is only called from those interrupt functions that DPMI reflects
424 * to real mode.
426 * FIXME: Need to think about where to place real mode stack.
427 * FIXME: If DOSVM_Wait calls are nested stack gets corrupted.
428 * Can this really happen?
430 if (!ISV86(&context))
432 context.EFlags |= V86_FLAG;
433 context.SegSs = 0xffff;
434 context.Esp = 0;
437 context.EFlags |= VIF_MASK;
438 context.SegCs = 0;
439 context.Eip = 0;
441 DOSVM_SendQueuedEvents(&context);
443 if(context.SegCs || context.Eip)
444 DPMI_CallRMProc( &context, NULL, 0, TRUE );
446 else
448 HANDLE objs[2];
449 int objc = DOSVM_IsWin16() ? 2 : 1;
450 DWORD waitret;
452 objs[0] = event_notifier;
453 objs[1] = GetStdHandle(STD_INPUT_HANDLE);
455 waitret = MsgWaitForMultipleObjects( objc, objs, FALSE,
456 INFINITE, QS_ALLINPUT );
458 if (waitret == WAIT_OBJECT_0)
461 * New pending event has been queued, we ignore it
462 * here because it will be processed on next call to
463 * DOSVM_Wait.
466 else if (objc == 2 && waitret == WAIT_OBJECT_0 + 1)
468 DOSVM_ProcessConsole();
470 else if (waitret == WAIT_OBJECT_0 + objc)
472 MSG msg;
473 while (PeekMessageA(&msg,0,0,0,PM_REMOVE|PM_NOYIELD))
475 /* got a message */
476 DOSVM_ProcessMessage(&msg);
477 /* we don't need a TranslateMessage here */
478 DispatchMessageA(&msg);
481 else
483 ERR_(module)( "dosvm wait error=%d\n", GetLastError() );
489 DWORD DOSVM_Loop( HANDLE hThread )
491 HANDLE objs[2];
492 int count = 0;
493 MSG msg;
494 DWORD waitret;
496 objs[count++] = hThread;
497 if (GetConsoleMode( GetStdHandle(STD_INPUT_HANDLE), NULL ))
498 objs[count++] = GetStdHandle(STD_INPUT_HANDLE);
500 for(;;) {
501 TRACE_(int)("waiting for action\n");
502 waitret = MsgWaitForMultipleObjects(count, objs, FALSE, INFINITE, QS_ALLINPUT);
503 if (waitret == WAIT_OBJECT_0) {
504 DWORD rv;
505 if(!GetExitCodeThread(hThread, &rv)) {
506 ERR("Failed to get thread exit code!\n");
507 rv = 0;
509 return rv;
511 else if (waitret == WAIT_OBJECT_0 + count) {
512 while (PeekMessageA(&msg,0,0,0,PM_REMOVE)) {
513 if (msg.hwnd) {
514 /* it's a window message */
515 DOSVM_ProcessMessage(&msg);
516 DispatchMessageA(&msg);
517 } else {
518 /* it's a thread message */
519 switch (msg.message) {
520 case WM_QUIT:
521 /* stop this madness!! */
522 return 0;
523 case WM_USER:
524 /* run passed procedure in this thread */
525 /* (sort of like APC, but we signal the completion) */
527 DOS_SPC *spc = (DOS_SPC *)msg.lParam;
528 TRACE_(int)("calling %p with arg %08lx\n", spc->proc, spc->arg);
529 (spc->proc)(spc->arg);
530 TRACE_(int)("done, signalling event %lx\n", msg.wParam);
531 SetEvent( (HANDLE)msg.wParam );
533 break;
534 default:
535 DispatchMessageA(&msg);
540 else if (waitret == WAIT_OBJECT_0 + 1)
542 DOSVM_ProcessConsole();
544 else
546 ERR_(int)("MsgWaitForMultipleObjects returned unexpected value.\n");
547 return 0;
552 static LONG WINAPI exception_handler(EXCEPTION_POINTERS *eptr)
554 EXCEPTION_RECORD *rec = eptr->ExceptionRecord;
555 CONTEXT *context = eptr->ContextRecord;
556 int arg = rec->ExceptionInformation[0];
557 BOOL ret;
559 switch(rec->ExceptionCode) {
560 case EXCEPTION_VM86_INTx:
561 TRACE_(relay)("Call DOS int 0x%02x ret=%04x:%04x\n"
562 " eax=%08x ebx=%08x ecx=%08x edx=%08x esi=%08x edi=%08x\n"
563 " ebp=%08x esp=%08x ds=%04x es=%04x fs=%04x gs=%04x flags=%08x\n",
564 arg, context->SegCs, context->Eip,
565 context->Eax, context->Ebx, context->Ecx, context->Edx, context->Esi, context->Edi,
566 context->Ebp, context->Esp, context->SegDs, context->SegEs, context->SegFs, context->SegGs,
567 context->EFlags );
568 ret = DOSVM_EmulateInterruptRM( context, arg );
569 TRACE_(relay)("Ret DOS int 0x%02x ret=%04x:%04x\n"
570 " eax=%08x ebx=%08x ecx=%08x edx=%08x esi=%08x edi=%08x\n"
571 " ebp=%08x esp=%08x ds=%04x es=%04x fs=%04x gs=%04x flags=%08x\n",
572 arg, context->SegCs, context->Eip,
573 context->Eax, context->Ebx, context->Ecx, context->Edx, context->Esi, context->Edi,
574 context->Ebp, context->Esp, context->SegDs, context->SegEs,
575 context->SegFs, context->SegGs, context->EFlags );
576 return ret ? EXCEPTION_CONTINUE_EXECUTION : EXCEPTION_EXECUTE_HANDLER;
578 case EXCEPTION_VM86_STI:
579 /* case EXCEPTION_VM86_PICRETURN: */
580 if (!ISV86(context))
581 ERR( "Protected mode STI caught by real mode handler!\n" );
582 DOSVM_SendQueuedEvents(context);
583 return EXCEPTION_CONTINUE_EXECUTION;
585 case EXCEPTION_SINGLE_STEP:
586 ret = DOSVM_EmulateInterruptRM( context, 1 );
587 return ret ? EXCEPTION_CONTINUE_EXECUTION : EXCEPTION_EXECUTE_HANDLER;
589 case EXCEPTION_BREAKPOINT:
590 ret = DOSVM_EmulateInterruptRM( context, 3 );
591 return ret ? EXCEPTION_CONTINUE_EXECUTION : EXCEPTION_EXECUTE_HANDLER;
594 return EXCEPTION_CONTINUE_SEARCH;
597 INT DOSVM_Enter( CONTEXT *context )
599 INT ret = 0;
600 if (!ISV86(context))
601 ERR( "Called with protected mode context!\n" );
603 __TRY
605 if (!WOWCallback16Ex( 0, WCB16_REGS, 0, NULL, (DWORD *)context )) ret = -1;
606 TRACE_(module)( "ret %d err %u\n", ret, GetLastError() );
608 __EXCEPT(exception_handler)
610 TRACE_(module)( "leaving vm86 mode\n" );
612 __ENDTRY
614 return ret;
617 /***********************************************************************
618 * DOSVM_PIC_ioport_out
620 void DOSVM_PIC_ioport_out( WORD port, BYTE val)
622 if (port != 0x20)
624 FIXME( "Unsupported PIC port %04x\n", port );
626 else if (val == 0x20 || (val >= 0x60 && val <= 0x67))
628 EnterCriticalSection(&qcrit);
630 if (!current_event)
632 WARN( "%s without active IRQ\n",
633 val == 0x20 ? "EOI" : "Specific EOI" );
635 else if (val != 0x20 && val - 0x60 != current_event->irq)
637 WARN( "Specific EOI but current IRQ %d is not %d\n",
638 current_event->irq, val - 0x60 );
640 else
642 LPDOSEVENT event = current_event;
644 TRACE( "Received %s for current IRQ %d, clearing event\n",
645 val == 0x20 ? "EOI" : "Specific EOI", event->irq );
647 current_event = event->next;
648 if (event->relay)
649 (*event->relay)(NULL,event->data);
650 HeapFree(GetProcessHeap(), 0, event);
652 if (DOSVM_HasPendingEvents())
654 TRACE( "Another event pending, setting pending flag\n" );
655 get_vm86_teb_info()->vm86_pending |= VIP_MASK;
659 LeaveCriticalSection(&qcrit);
661 else
663 FIXME( "Unrecognized PIC command %02x\n", val );
667 #else /* !MZ_SUPPORTED */
669 /***********************************************************************
670 * DOSVM_Enter
672 INT DOSVM_Enter( CONTEXT *context )
674 SetLastError( ERROR_NOT_SUPPORTED );
675 return -1;
678 /***********************************************************************
679 * DOSVM_Wait
681 void DOSVM_Wait( CONTEXT *waitctx ) { }
683 /***********************************************************************
684 * DOSVM_PIC_ioport_out
686 void DOSVM_PIC_ioport_out( WORD port, BYTE val) {}
688 /***********************************************************************
689 * DOSVM_QueueEvent
691 void DOSVM_QueueEvent( INT irq, INT priority, DOSRELAY relay, LPVOID data)
693 if (irq<0) {
694 /* callback event, perform it with dummy context */
695 CONTEXT context;
696 memset(&context,0,sizeof(context));
697 (*relay)(&context,data);
698 } else {
699 ERR("IRQ without DOS task: should not happen\n");
703 #endif /* MZ_SUPPORTED */
706 /**********************************************************************
707 * DOSVM_AcknowledgeIRQ
709 * This routine should be called by all internal IRQ handlers.
711 void WINAPI DOSVM_AcknowledgeIRQ( CONTEXT *context )
714 * Send EOI to PIC.
716 DOSVM_PIC_ioport_out( 0x20, 0x20 );
719 * Protected mode IRQ handlers are supposed
720 * to turn VIF flag on before they return.
722 if (!ISV86(context))
723 get_vm86_teb_info()->dpmi_vif = 1;
727 /***********************************************************************
728 * DOSVM_AllocUMB
730 * Allocate upper memory block (UMB) from upper memory.
731 * Returned pointer is aligned to 16-byte (paragraph) boundary.
733 * This routine is only for allocating static storage for
734 * Wine internal uses. Allocated memory can be accessed from
735 * real mode, memory is taken from area already mapped and reserved
736 * by Wine and the allocation has very little memory and speed
737 * overhead. Use of this routine also preserves precious DOS
738 * conventional memory.
740 static LPVOID DOSVM_AllocUMB( DWORD size )
742 LPVOID ptr = (LPVOID)DOSVM_umb_free;
744 size = ((size + 15) >> 4) << 4;
746 if(DOSVM_umb_free + size - 1 > DOSVM_UMB_TOP) {
747 ERR("Out of upper memory area.\n");
748 return 0;
751 DOSVM_umb_free += size;
752 return ptr;
756 /**********************************************************************
757 * alloc_selector
759 * Allocate a selector corresponding to a real mode address.
760 * size must be < 64k.
762 static WORD alloc_selector( void *base, DWORD size, unsigned char flags )
764 WORD sel = wine_ldt_alloc_entries( 1 );
766 if (sel)
768 LDT_ENTRY entry;
769 wine_ldt_set_base( &entry, base );
770 wine_ldt_set_limit( &entry, size - 1 );
771 wine_ldt_set_flags( &entry, flags );
772 wine_ldt_set_entry( sel, &entry );
774 return sel;
778 /***********************************************************************
779 * DOSVM_AllocCodeUMB
781 * Allocate upper memory block for storing code stubs.
782 * Initializes real mode segment and 16-bit protected mode selector
783 * for the allocated code block.
785 * FIXME: should allocate a single PM selector for the whole UMB range.
787 static LPVOID DOSVM_AllocCodeUMB( DWORD size, WORD *segment, WORD *selector )
789 LPVOID ptr = DOSVM_AllocUMB( size );
791 if (segment)
792 *segment = (DWORD)ptr >> 4;
794 if (selector)
795 *selector = alloc_selector( ptr, size, WINE_LDT_FLAGS_CODE );
797 return ptr;
801 /***********************************************************************
802 * DOSVM_AllocDataUMB
804 * Allocate upper memory block for storing data.
805 * Initializes real mode segment and 16-bit protected mode selector
806 * for the allocated data block.
808 LPVOID DOSVM_AllocDataUMB( DWORD size, WORD *segment, WORD *selector )
810 LPVOID ptr = DOSVM_AllocUMB( size );
812 if (segment)
813 *segment = (DWORD)ptr >> 4;
815 if (selector)
816 *selector = alloc_selector( ptr, size, WINE_LDT_FLAGS_DATA );
818 return ptr;
822 /***********************************************************************
823 * DOSVM_InitSegments
825 * Initializes DOSVM_dpmi_segments. Allocates required memory and
826 * sets up segments and selectors for accessing the memory.
828 void DOSVM_InitSegments(void)
830 LPSTR ptr;
831 int i;
833 static const char wrap_code[]={
834 0xCD,0x31, /* int $0x31 */
835 0xCB /* lret */
838 static const char enter_xms[]=
840 /* XMS hookable entry point */
841 0xEB,0x03, /* jmp entry */
842 0x90,0x90,0x90, /* nop;nop;nop */
843 /* entry: */
844 /* real entry point */
845 /* for simplicity, we'll just use the same hook as DPMI below */
846 0xCD,0x31, /* int $0x31 */
847 0xCB /* lret */
850 static const char enter_pm[]=
852 0x50, /* pushw %ax */
853 0x52, /* pushw %dx */
854 0x55, /* pushw %bp */
855 0x89,0xE5, /* movw %sp,%bp */
856 /* get return CS */
857 0x8B,0x56,0x08, /* movw 8(%bp),%dx */
858 /* just call int 31 here to get into protected mode... */
859 /* it'll check whether it was called from dpmi_seg... */
860 0xCD,0x31, /* int $0x31 */
861 /* we are now in the context of a 16-bit relay call */
862 /* need to fixup our stack;
863 * 16-bit relay return address will be lost,
864 * but we won't worry quite yet
866 0x8E,0xD0, /* movw %ax,%ss */
867 0x66,0x0F,0xB7,0xE5, /* movzwl %bp,%esp */
868 /* set return CS */
869 0x89,0x56,0x08, /* movw %dx,8(%bp) */
870 0x5D, /* popw %bp */
871 0x5A, /* popw %dx */
872 0x58, /* popw %ax */
873 0xfb, /* sti, enable and check virtual interrupts */
874 0xCB /* lret */
877 static const char relay[]=
879 0xca, 0x04, 0x00, /* 16-bit far return and pop 4 bytes (relay void* arg) */
880 0xcd, 0x31, /* int 31 */
881 0xfb, 0x66, 0xcb /* sti and 32-bit far return */
885 * Allocate pointer array.
887 DOSVM_dpmi_segments = DOSVM_AllocUMB( sizeof(struct DPMI_segments) );
890 * RM / offset 0: Exit from real mode.
891 * RM / offset 2: Points to lret opcode.
893 ptr = DOSVM_AllocCodeUMB( sizeof(wrap_code),
894 &DOSVM_dpmi_segments->wrap_seg, 0 );
895 memcpy( ptr, wrap_code, sizeof(wrap_code) );
898 * RM / offset 0: XMS driver entry.
900 ptr = DOSVM_AllocCodeUMB( sizeof(enter_xms),
901 &DOSVM_dpmi_segments->xms_seg, 0 );
902 memcpy( ptr, enter_xms, sizeof(enter_xms) );
905 * RM / offset 0: Switch to DPMI.
906 * PM / offset 8: DPMI raw mode switch.
908 ptr = DOSVM_AllocCodeUMB( sizeof(enter_pm),
909 &DOSVM_dpmi_segments->dpmi_seg,
910 &DOSVM_dpmi_segments->dpmi_sel );
911 memcpy( ptr, enter_pm, sizeof(enter_pm) );
914 * PM / offset N*6: Interrupt N in DPMI32.
916 ptr = DOSVM_AllocCodeUMB( 6 * 256,
917 0, &DOSVM_dpmi_segments->int48_sel );
918 for(i=0; i<256; i++) {
920 * Each 32-bit interrupt handler is 6 bytes:
921 * 0xCD,<i> = int <i> (nested 16-bit interrupt)
922 * 0x66,0xCA,0x04,0x00 = ret 4 (32-bit far return and pop 4 bytes / eflags)
924 ptr[i * 6 + 0] = 0xCD;
925 ptr[i * 6 + 1] = i;
926 ptr[i * 6 + 2] = 0x66;
927 ptr[i * 6 + 3] = 0xCA;
928 ptr[i * 6 + 4] = 0x04;
929 ptr[i * 6 + 5] = 0x00;
933 * PM / offset N*5: Interrupt N in 16-bit protected mode.
935 ptr = DOSVM_AllocCodeUMB( 5 * 256,
936 0, &DOSVM_dpmi_segments->int16_sel );
937 for(i=0; i<256; i++) {
939 * Each 16-bit interrupt handler is 5 bytes:
940 * 0xCD,<i> = int <i> (interrupt)
941 * 0xCA,0x02,0x00 = ret 2 (16-bit far return and pop 2 bytes / eflags)
943 ptr[i * 5 + 0] = 0xCD;
944 ptr[i * 5 + 1] = i;
945 ptr[i * 5 + 2] = 0xCA;
946 ptr[i * 5 + 3] = 0x02;
947 ptr[i * 5 + 4] = 0x00;
951 * PM / offset 0: Stub where __wine_call_from_16_regs returns.
952 * PM / offset 3: Stub which swaps back to 32-bit application code/stack.
953 * PM / offset 5: Stub which enables interrupts
955 ptr = DOSVM_AllocCodeUMB( sizeof(relay),
956 0, &DOSVM_dpmi_segments->relay_code_sel);
957 memcpy( ptr, relay, sizeof(relay) );
960 * Space for 16-bit stack used by relay code.
962 ptr = DOSVM_AllocDataUMB( DOSVM_RELAY_DATA_SIZE,
963 0, &DOSVM_dpmi_segments->relay_data_sel);
964 memset( ptr, 0, DOSVM_RELAY_DATA_SIZE );
967 * As we store code in UMB we should make sure it is executable
969 VirtualProtect((void *)DOSVM_UMB_BOTTOM, DOSVM_UMB_TOP - DOSVM_UMB_BOTTOM, PAGE_EXECUTE_READWRITE, NULL);
971 event_notifier = CreateEventW(NULL, FALSE, FALSE, NULL);