-
Notifications
You must be signed in to change notification settings - Fork 156
/
Copy pathint31.c
1521 lines (1340 loc) · 48.6 KB
/
int31.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* DPMI 0.9 emulation
*
* Copyright 1995 Alexandre Julliard
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include "config.h"
#include "wine/port.h"
#include <stdarg.h>
#include "windef.h"
#include "winbase.h"
#include "winternl.h"
#include "wine/winbase16.h"
#include "windows/wownt32.h"
#include "kernel16_private.h"
#include "dosexe.h"
#include "excpt.h"
#include "wine/debug.h"
#include "wine/exception.h"
WINE_DEFAULT_DEBUG_CHANNEL(int31);
/* Structure for real-mode callbacks */
typedef struct
{
DWORD edi;
DWORD esi;
DWORD ebp;
DWORD reserved;
DWORD ebx;
DWORD edx;
DWORD ecx;
DWORD eax;
WORD fl;
WORD es;
WORD ds;
WORD fs;
WORD gs;
WORD ip;
WORD cs;
WORD sp;
WORD ss;
} REALMODECALL;
typedef struct tagRMCB {
DWORD address;
DWORD proc_ofs,proc_sel;
DWORD regs_ofs,regs_sel;
struct tagRMCB *next;
} RMCB;
static RMCB *FirstRMCB = NULL;
static WORD dpmi_flag;
static void* lastvalloced = NULL;
static BYTE DPMI_retval;
#include "pshpack1.h"
typedef struct {
WORD Handle;
DWORD Offset;
} MOVEOFS;
typedef struct {
DWORD Length;
MOVEOFS Source;
MOVEOFS Dest;
} MOVESTRUCT;
#include "poppack.h"
/**********************************************************************
* DOSVM_IsDos32
*
* Return TRUE if we are in 32-bit protected mode DOS process.
*/
BOOL DOSVM_IsDos32(void)
{
return (dpmi_flag & 1) != 0;
}
/**********************************************************************
* alloc_pm_selector
*
* Allocate a 64k sized selector corresponding to a real mode segment.
*/
static WORD alloc_pm_selector( WORD seg, unsigned char flags )
{
WORD sel = wine_ldt_alloc_entries( 1 );
if (sel)
{
LDT_ENTRY entry;
wine_ldt_set_base( &entry, PTR_REAL_TO_LIN(seg, 0) );
wine_ldt_set_limit( &entry, 0xffff );
wine_ldt_set_flags( &entry, flags );
wine_ldt_set_entry( sel, &entry );
}
return sel;
}
/**********************************************************************
* dpmi_exception_handler
*
* Handle EXCEPTION_VM86_STI exceptions generated
* when there are pending asynchronous events.
*/
static LONG WINAPI dpmi_exception_handler(EXCEPTION_POINTERS *eptr)
{
EXCEPTION_RECORD *rec = eptr->ExceptionRecord;
CONTEXT *context = eptr->ContextRecord;
if (rec->ExceptionCode == EXCEPTION_VM86_STI)
{
if (ISV86(context))
ERR( "Real mode STI caught by protected mode handler!\n" );
DOSVM_SendQueuedEvents(context);
return EXCEPTION_CONTINUE_EXECUTION;
}
else if (rec->ExceptionCode == EXCEPTION_VM86_INTx)
{
if (ISV86(context))
ERR( "Real mode INTx caught by protected mode handler!\n" );
DPMI_retval = (BYTE)rec->ExceptionInformation[0];
return EXCEPTION_EXECUTE_HANDLER;
}
return EXCEPTION_CONTINUE_SEARCH;
}
/**********************************************************************
* INT_GetRealModeContext
*/
static void INT_GetRealModeContext( REALMODECALL *call, CONTEXT *context )
{
context->Eax = call->eax;
context->Ebx = call->ebx;
context->Ecx = call->ecx;
context->Edx = call->edx;
context->Esi = call->esi;
context->Edi = call->edi;
context->Ebp = call->ebp;
context->EFlags = call->fl | V86_FLAG;
context->Eip = call->ip;
context->Esp = call->sp;
context->SegCs = call->cs;
context->SegDs = call->ds;
context->SegEs = call->es;
context->SegFs = call->fs;
context->SegGs = call->gs;
context->SegSs = call->ss;
}
/**********************************************************************
* INT_SetRealModeContext
*/
static void INT_SetRealModeContext( REALMODECALL *call, CONTEXT *context, BOOL mod_csip_ssssp )
{
call->eax = context->Eax;
call->ebx = context->Ebx;
call->ecx = context->Ecx;
call->edx = context->Edx;
call->esi = context->Esi;
call->edi = context->Edi;
call->ebp = context->Ebp;
call->fl = LOWORD(context->EFlags);
if (mod_csip_ssssp)
{
call->ip = LOWORD(context->Eip);
call->sp = LOWORD(context->Esp);
call->cs = context->SegCs;
call->ss = context->SegSs;
}
call->ds = context->SegDs;
call->es = context->SegEs;
call->fs = context->SegFs;
call->gs = context->SegGs;
}
/**********************************************************************
* DPMI_xalloc
* special virtualalloc, allocates linearly monoton growing memory.
* (the usual VirtualAlloc does not satisfy that restriction)
*/
static LPVOID DPMI_xalloc( DWORD len )
{
LPVOID ret;
LPVOID oldlastv = lastvalloced;
if (lastvalloced)
{
int xflag = 0;
ret = NULL;
while (!ret)
{
ret = VirtualAlloc( lastvalloced, len,
MEM_COMMIT|MEM_RESERVE, PAGE_EXECUTE_READWRITE );
if (!ret)
lastvalloced = (char *) lastvalloced + 0x10000;
/* we failed to allocate one in the first round.
* try non-linear
*/
if (!xflag && (lastvalloced<oldlastv))
{
/* wrapped */
FIXME( "failed to allocate linearly growing memory (%u bytes), "
"using non-linear growing...\n", len );
xflag++;
}
/* if we even fail to allocate something in the next
* round, return NULL
*/
if ((xflag==1) && (lastvalloced >= oldlastv))
xflag++;
if ((xflag==2) && (lastvalloced < oldlastv)) {
FIXME( "failed to allocate any memory of %u bytes!\n", len );
return NULL;
}
}
}
else
{
ret = VirtualAlloc( NULL, len,
MEM_COMMIT|MEM_RESERVE, PAGE_EXECUTE_READWRITE );
}
lastvalloced = (LPVOID)(((DWORD)ret+len+0xffff)&~0xffff);
return ret;
}
/**********************************************************************
* DPMI_xfree
*/
static void DPMI_xfree( LPVOID ptr )
{
VirtualFree( ptr, 0, MEM_RELEASE );
}
/**********************************************************************
* DPMI_xrealloc
*
* FIXME: perhaps we could grow this mapped area...
*/
static LPVOID DPMI_xrealloc( LPVOID ptr, DWORD newsize )
{
MEMORY_BASIC_INFORMATION mbi;
if (ptr)
{
LPVOID newptr;
if (!VirtualQuery(ptr,&mbi,sizeof(mbi)))
{
FIXME( "realloc of DPMI_xallocd region %p?\n", ptr );
return NULL;
}
if (mbi.State == MEM_FREE)
{
FIXME( "realloc of DPMI_xallocd region %p?\n", ptr );
return NULL;
}
/* We do not shrink allocated memory. most reallocs
* only do grows anyway
*/
if (newsize <= mbi.RegionSize)
return ptr;
newptr = DPMI_xalloc( newsize );
if (!newptr)
return NULL;
memcpy( newptr, ptr, mbi.RegionSize );
DPMI_xfree( ptr );
return newptr;
}
return DPMI_xalloc( newsize );
}
extern void DPMI_CallRMCB32(RMCB *rmcb, UINT16 ss, DWORD esp, UINT16*es, DWORD*edi);
#if 0 /* original code, which early gccs puke on */
{
int _clobber;
__asm__ __volatile__(
"pushl %%ebp\n"
"pushl %%ebx\n"
"pushl %%es\n"
"pushl %%ds\n"
"pushfl\n"
"mov %7,%%es\n"
"mov %5,%%ds\n"
".byte 0x36, 0xff, 0x18\n" /* lcall *%ss:(%eax) */
"popl %%ds\n"
"mov %%es,%0\n"
"popl %%es\n"
"popl %%ebx\n"
"popl %%ebp\n"
: "=d" (*es), "=D" (*edi), "=S" (_clobber), "=a" (_clobber), "=c" (_clobber)
: "0" (ss), "2" (esp),
"4" (rmcb->regs_sel), "1" (rmcb->regs_ofs),
"3" (&rmcb->proc_ofs) );
}
#else /* code generated by a gcc new enough */
;
__ASM_GLOBAL_FUNC(_DPMI_CallRMCB32,
"pushl %ebp\n\t"
__ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
__ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
"movl %esp,%ebp\n\t"
__ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
"pushl %edi\n\t"
__ASM_CFI(".cfi_rel_offset %edi,-4\n\t")
"pushl %esi\n\t"
__ASM_CFI(".cfi_rel_offset %esi,-8\n\t")
"movl 0x8(%ebp),%eax\n\t"
"movl 0x10(%ebp),%esi\n\t"
"movl 0xc(%ebp),%edx\n\t"
"movl 0x10(%eax),%ecx\n\t"
"movl 0xc(%eax),%edi\n\t"
"addl $0x4,%eax\n\t"
"pushl %ebp\n\t"
"pushl %ebx\n\t"
"pushl %es\n\t"
"pushl %ds\n\t"
"pushfl\n\t"
"mov %cx,%es\n\t"
"mov %dx,%ds\n\t"
".byte 0x36, 0xff, 0x18\n\t" /* lcall *%ss:(%eax) */
"popl %ds\n\t"
"mov %es,%dx\n\t"
"popl %es\n\t"
"popl %ebx\n\t"
"popl %ebp\n\t"
"movl 0x14(%ebp),%eax\n\t"
"movw %dx,(%eax)\n\t"
"movl 0x18(%ebp),%edx\n\t"
"movl %edi,(%edx)\n\t"
"popl %esi\n\t"
__ASM_CFI(".cfi_same_value %esi\n\t")
"popl %edi\n\t"
__ASM_CFI(".cfi_same_value %edi\n\t")
"leave\n\t"
__ASM_CFI(".cfi_def_cfa %esp,4\n\t")
__ASM_CFI(".cfi_same_value %ebp\n\t")
"ret")
#endif
/**********************************************************************
* DPMI_CallRMCBProc
*
* This routine does the hard work of calling a callback procedure.
*/
static void DPMI_CallRMCBProc( CONTEXT *context, RMCB *rmcb, WORD flag )
{
DWORD old_vif = get_vm86_teb_info()->dpmi_vif;
/* Disable virtual interrupts. */
get_vm86_teb_info()->dpmi_vif = 0;
if (wine_ldt_is_system( rmcb->proc_sel )) {
/* Wine-internal RMCB, call directly */
((RMCBPROC)rmcb->proc_ofs)(context);
} else __TRY {
UINT16 ss,es;
DWORD esp,edi;
INT_SetRealModeContext(MapSL(MAKESEGPTR( rmcb->regs_sel, rmcb->regs_ofs )), context, TRUE);
ss = alloc_pm_selector( context->SegSs, WINE_LDT_FLAGS_DATA );
esp = context->Esp;
FIXME("untested!\n");
/* The called proc ends with an IRET, and takes these parameters:
* DS:ESI = pointer to real-mode SS:SP
* ES:EDI = pointer to real-mode call structure
* It returns:
* ES:EDI = pointer to real-mode call structure (may be a copy)
* It is the proc's responsibility to change the return CS:IP in the
* real-mode call structure. */
if (flag & 1) {
/* 32-bit DPMI client */
DPMI_CallRMCB32(rmcb, ss, esp, &es, &edi);
} else {
/* 16-bit DPMI client */
CONTEXT ctx = *context;
ctx.SegCs = rmcb->proc_sel;
ctx.Eip = rmcb->proc_ofs;
ctx.SegDs = ss;
ctx.Esi = esp;
ctx.SegEs = rmcb->regs_sel;
ctx.Edi = rmcb->regs_ofs;
/* FIXME: I'm pretty sure this isn't right - should push flags first */
WOWCallback16Ex( 0, WCB16_REGS, 0, NULL, (DWORD *)&ctx );
es = ctx.SegEs;
edi = ctx.Edi;
}
wine_ldt_free_entries( ss, 1 );
INT_GetRealModeContext( MapSL( MAKESEGPTR( es, edi )), context);
} __EXCEPT(dpmi_exception_handler) { } __ENDTRY
/* Restore virtual interrupt flag. */
get_vm86_teb_info()->dpmi_vif = old_vif;
}
/**********************************************************************
* DPMI_CallRMProc
*
* This routine does the hard work of calling a real mode procedure.
*/
int DPMI_CallRMProc( CONTEXT *context, LPWORD stack, int args, int iret )
{
LPWORD stack16;
LPVOID addr = NULL; /* avoid gcc warning */
RMCB *CurrRMCB;
BOOL alloc = FALSE, already = FALSE;
BYTE *code;
TRACE("EAX=%08x EBX=%08x ECX=%08x EDX=%08x\n",
context->Eax, context->Ebx, context->Ecx, context->Edx );
TRACE("ESI=%08x EDI=%08x ES=%04x DS=%04x CS:IP=%04x:%04x, %d WORD arguments, %s\n",
context->Esi, context->Edi, context->SegEs, context->SegDs,
context->SegCs, LOWORD(context->Eip), args, iret?"IRET":"FAR" );
callrmproc_again:
/* there might be some code that just jumps to RMCBs or the like,
in which case following the jumps here might get us to a shortcut */
code = CTX_SEG_OFF_TO_LIN(context, context->SegCs, context->Eip);
switch (*code) {
case 0xe9: /* JMP NEAR */
context->Eip += 3 + *(WORD *)(code+1);
/* yeah, I know these gotos don't look good... */
goto callrmproc_again;
case 0xea: /* JMP FAR */
context->Eip = *(WORD *)(code+1);
context->SegCs = *(WORD *)(code+3);
/* ...but since the label is there anyway... */
goto callrmproc_again;
case 0xeb: /* JMP SHORT */
context->Eip += 2 + *(signed char *)(code+1);
/* ...because of other gotos below, so... */
goto callrmproc_again;
}
/* shortcut for chaining to internal interrupt handlers */
if ((context->SegCs == 0xF000) && iret)
{
DOSVM_CallBuiltinHandler( context, LOWORD(context->Eip)/4 );
return 0;
}
/* shortcut for RMCBs */
CurrRMCB = FirstRMCB;
while (CurrRMCB && (HIWORD(CurrRMCB->address) != context->SegCs))
CurrRMCB = CurrRMCB->next;
if (!CurrRMCB && !MZ_Current())
{
FIXME("DPMI real-mode call using DOS VM task system, not fully tested!\n");
TRACE("creating VM86 task\n");
MZ_AllocDPMITask();
}
if (!already) {
if (!context->SegSs) {
alloc = TRUE; /* allocate default stack */
stack16 = addr = DOSMEM_AllocBlock( 64, (UINT16 *)&(context->SegSs) );
context->Esp = 64-2;
stack16 += 32-1;
if (!addr) {
ERR("could not allocate default stack\n");
return 1;
}
} else {
stack16 = CTX_SEG_OFF_TO_LIN(context, context->SegSs, context->Esp);
}
context->Esp -= (args + (iret?1:0)) * sizeof(WORD);
stack16 -= args;
if (args) memcpy(stack16, stack, args*sizeof(WORD) );
/* push flags if iret */
if (iret) {
stack16--; args++;
*stack16 = LOWORD(context->EFlags);
}
/* push return address (return to interrupt wrapper) */
*(--stack16) = DOSVM_dpmi_segments->wrap_seg;
*(--stack16) = 0;
/* adjust stack */
context->Esp -= 2*sizeof(WORD);
already = TRUE;
}
if (CurrRMCB) {
/* RMCB call, invoke protected-mode handler directly */
DPMI_CallRMCBProc(context, CurrRMCB, dpmi_flag);
/* check if we returned to where we thought we would */
if ((context->SegCs != DOSVM_dpmi_segments->wrap_seg) ||
(LOWORD(context->Eip) != 0)) {
/* we need to continue at different address in real-mode space,
so we need to set it all up for real mode again */
goto callrmproc_again;
}
} else {
TRACE("entering real mode...\n");
DOSVM_Enter( context );
TRACE("returned from real-mode call\n");
}
if (alloc) DOSMEM_FreeBlock( addr );
return 0;
}
/**********************************************************************
* CallRMInt
*/
static void DOSVM_CallRMInt( CONTEXT *context )
{
CONTEXT realmode_ctx;
FARPROC16 rm_int = DOSVM_GetRMHandler( BL_reg(context) );
REALMODECALL *call = CTX_SEG_OFF_TO_LIN( context,
context->SegEs,
context->Edi );
INT_GetRealModeContext( call, &realmode_ctx );
/* we need to check if a real-mode program has hooked the interrupt */
if (HIWORD(rm_int)!=0xF000) {
/* yup, which means we need to switch to real mode... */
realmode_ctx.SegCs = HIWORD(rm_int);
realmode_ctx.Eip = LOWORD(rm_int);
if (DPMI_CallRMProc( &realmode_ctx, NULL, 0, TRUE))
SET_CFLAG(context);
} else {
RESET_CFLAG(context);
/* use the IP we have instead of BL_reg, in case some apps
decide to move interrupts around for whatever reason... */
DOSVM_CallBuiltinHandler( &realmode_ctx, LOWORD(rm_int)/4 );
}
INT_SetRealModeContext( call, &realmode_ctx, FALSE );
}
/**********************************************************************
* CallRMProc
*/
static void DOSVM_CallRMProc( CONTEXT *context, int iret )
{
REALMODECALL *p = CTX_SEG_OFF_TO_LIN( context,
context->SegEs,
context->Edi );
CONTEXT context16;
TRACE("RealModeCall: EAX=%08x EBX=%08x ECX=%08x EDX=%08x\n",
p->eax, p->ebx, p->ecx, p->edx);
TRACE(" ESI=%08x EDI=%08x ES=%04x DS=%04x CS:IP=%04x:%04x, %d WORD arguments, %s\n",
p->esi, p->edi, p->es, p->ds, p->cs, p->ip, CX_reg(context), iret?"IRET":"FAR" );
if (!(p->cs) && !(p->ip)) { /* remove this check
if Int21/6501 case map function
has been implemented */
SET_CFLAG(context);
return;
}
INT_GetRealModeContext(p, &context16);
DPMI_CallRMProc( &context16, ((LPWORD)MapSL(MAKESEGPTR(context->SegSs, LOWORD(context->Esp))))+3,
CX_reg(context), iret );
INT_SetRealModeContext(p, &context16, FALSE);
}
/* (see dosmem.c, function DOSMEM_InitDPMI) */
static void StartPM( CONTEXT *context )
{
UINT16 cs, ss, ds, es;
CONTEXT pm_ctx;
PDB16 *psp = (PDB16 *)PTR_REAL_TO_LIN(DOSVM_psp, 0);
HANDLE16 env_seg = psp->environment;
unsigned char selflags = WINE_LDT_FLAGS_DATA;
RESET_CFLAG(context);
dpmi_flag = AX_reg(context);
/* our mode switch wrapper have placed the desired CS into DX */
cs = alloc_pm_selector( context->Edx, WINE_LDT_FLAGS_CODE );
/* due to a flaw in some CPUs (at least mine), it is best to mark stack segments as 32-bit if they
can be used in 32-bit code. Otherwise, these CPUs may not set the high word of esp during a
ring transition (from kernel code) to the 16-bit stack, and this causes trouble if executing
32-bit code using this stack. */
if (dpmi_flag & 1) selflags |= WINE_LDT_FLAGS_32BIT;
ss = alloc_pm_selector( context->SegSs, selflags );
/* do the same for the data segments, just in case */
if (context->SegDs == context->SegSs) ds = ss;
else ds = alloc_pm_selector( context->SegDs, selflags );
es = alloc_pm_selector( DOSVM_psp, selflags );
/* convert environment pointer, as the spec says, but we're a bit lazy about the size here... */
psp->environment = alloc_pm_selector( env_seg, WINE_LDT_FLAGS_DATA );
pm_ctx = *context;
pm_ctx.SegCs = DOSVM_dpmi_segments->dpmi_sel;
/* our mode switch wrapper expects the new CS in DX, and the new SS in AX */
pm_ctx.Eax = ss;
pm_ctx.Edx = cs;
pm_ctx.SegDs = ds;
pm_ctx.SegEs = es;
pm_ctx.SegFs = wine_get_fs();
pm_ctx.SegGs = wine_get_gs();
pm_ctx.EFlags &= ~V86_FLAG;
TRACE("DOS program is now entering %d-bit protected mode\n",
DOSVM_IsDos32() ? 32 : 16);
__TRY
{
WOWCallback16Ex( 0, WCB16_REGS, 0, NULL, (DWORD *)&pm_ctx );
}
__EXCEPT(dpmi_exception_handler)
{
}
__ENDTRY
TRACE( "Protected mode DOS program is terminating\n" );
/*
* FIXME: Instead of calling DOSVM_Exit, we should release all
* allocated protected mode resources and call MZ_Exit
* using real mode context. See DPMI specification.
*/
DOSVM_Exit( DPMI_retval );
#if 0
wine_ldt_free_entries( psp->environment, 1 );
psp->environment = env_seg;
wine_ldt_free_entries(es,1);
if (ds != ss) wine_ldt_free_entries(ds,1);
wine_ldt_free_entries(ss,1);
wine_ldt_free_entries(cs,1);
#endif
}
static RMCB *DPMI_AllocRMCB( void )
{
RMCB *NewRMCB = HeapAlloc(GetProcessHeap(), 0, sizeof(RMCB));
UINT16 uParagraph;
if (NewRMCB)
{
LPVOID RMCBmem = DOSMEM_AllocBlock(4, &uParagraph);
LPBYTE p = RMCBmem;
*p++ = 0xcd; /* RMCB: */
*p++ = 0x31; /* int $0x31 */
/* it is the called procedure's task to change the return CS:EIP
the DPMI 0.9 spec states that if it doesn't, it will be called again */
*p++ = 0xeb;
*p++ = 0xfc; /* jmp RMCB */
NewRMCB->address = MAKELONG(0, uParagraph);
NewRMCB->next = FirstRMCB;
FirstRMCB = NewRMCB;
}
return NewRMCB;
}
FARPROC16 DPMI_AllocInternalRMCB( RMCBPROC proc )
{
RMCB *NewRMCB = DPMI_AllocRMCB();
if (NewRMCB) {
NewRMCB->proc_ofs = (DWORD)proc;
NewRMCB->proc_sel = 0;
NewRMCB->regs_ofs = 0;
NewRMCB->regs_sel = 0;
return (FARPROC16)(NewRMCB->address);
}
return NULL;
}
static BOOL DPMI_FreeRMCB( DWORD address )
{
RMCB *CurrRMCB = FirstRMCB;
RMCB *PrevRMCB = NULL;
while (CurrRMCB && (CurrRMCB->address != address))
{
PrevRMCB = CurrRMCB;
CurrRMCB = CurrRMCB->next;
}
if (CurrRMCB)
{
if (PrevRMCB)
PrevRMCB->next = CurrRMCB->next;
else
FirstRMCB = CurrRMCB->next;
DOSMEM_FreeBlock(PTR_REAL_TO_LIN(SELECTOROF(CurrRMCB->address),OFFSETOF(CurrRMCB->address)));
HeapFree(GetProcessHeap(), 0, CurrRMCB);
return TRUE;
}
return FALSE;
}
/**********************************************************************
* DOSVM_RawModeSwitchHandler
*
* DPMI Raw Mode Switch handler
*/
void WINAPI DOSVM_RawModeSwitchHandler( CONTEXT *context )
{
CONTEXT rm_ctx;
int ret;
/* initialize real-mode context as per spec */
memset(&rm_ctx, 0, sizeof(rm_ctx));
rm_ctx.SegDs = AX_reg(context);
rm_ctx.SegEs = CX_reg(context);
rm_ctx.SegSs = DX_reg(context);
rm_ctx.Esp = context->Ebx;
rm_ctx.SegCs = SI_reg(context);
rm_ctx.Eip = context->Edi;
rm_ctx.Ebp = context->Ebp;
rm_ctx.SegFs = 0;
rm_ctx.SegGs = 0;
/* Copy interrupt state. */
if (get_vm86_teb_info()->dpmi_vif)
rm_ctx.EFlags = V86_FLAG | VIF_MASK;
else
rm_ctx.EFlags = V86_FLAG;
/* enter real mode again */
TRACE("re-entering real mode at %04x:%04x\n",rm_ctx.SegCs,rm_ctx.Eip);
ret = DOSVM_Enter( &rm_ctx );
/* when the real-mode stuff call its mode switch address,
DOSVM_Enter will return and we will continue here */
if (ret<0) {
ERR("Sync lost!\n");
/* if the sync was lost, there's no way to recover */
ExitProcess(1);
}
/* alter protected-mode context as per spec */
context->SegDs = LOWORD(rm_ctx.Eax);
context->SegEs = LOWORD(rm_ctx.Ecx);
context->SegSs = LOWORD(rm_ctx.Edx);
context->Esp = rm_ctx.Ebx;
context->SegCs = LOWORD(rm_ctx.Esi);
context->Eip = rm_ctx.Edi;
context->Ebp = rm_ctx.Ebp;
context->SegFs = 0;
context->SegGs = 0;
/* Copy interrupt state. */
if (rm_ctx.EFlags & VIF_MASK)
get_vm86_teb_info()->dpmi_vif = 1;
else
get_vm86_teb_info()->dpmi_vif = 0;
/* Return to new address and hope that we didn't mess up */
TRACE("re-entering protected mode at %04x:%08x\n",
context->SegCs, context->Eip);
}
/**********************************************************************
* AllocRMCB
*/
static void DOSVM_AllocRMCB( CONTEXT *context )
{
RMCB *NewRMCB = DPMI_AllocRMCB();
TRACE("Function to call: %04x:%04x\n", (WORD)context->SegDs, SI_reg(context) );
if (NewRMCB)
{
NewRMCB->proc_ofs = DOSVM_IsDos32() ? context->Esi : LOWORD(context->Esi);
NewRMCB->proc_sel = context->SegDs;
NewRMCB->regs_ofs = DOSVM_IsDos32() ? context->Edi : LOWORD(context->Edi);
NewRMCB->regs_sel = context->SegEs;
SET_CX( context, HIWORD(NewRMCB->address) );
SET_DX( context, LOWORD(NewRMCB->address) );
}
else
{
SET_AX( context, 0x8015 ); /* callback unavailable */
SET_CFLAG(context);
}
}
/**********************************************************************
* FreeRMCB
*/
static void DOSVM_FreeRMCB( CONTEXT *context )
{
FIXME("callback address: %04x:%04x\n",
CX_reg(context), DX_reg(context));
if (!DPMI_FreeRMCB(MAKELONG(DX_reg(context), CX_reg(context)))) {
SET_AX( context, 0x8024 ); /* invalid callback address */
SET_CFLAG(context);
}
}
static BYTE * XMS_Offset( MOVEOFS *ofs )
{
if (ofs->Handle) return (BYTE*)GlobalLock16(ofs->Handle)+ofs->Offset;
else return PTR_REAL_TO_LIN(SELECTOROF(ofs->Offset),OFFSETOF(ofs->Offset));
}
/**********************************************************************
* XMS_Handler
*/
static void XMS_Handler( CONTEXT *context )
{
switch(AH_reg(context))
{
case 0x00: /* Get XMS version number */
TRACE("get XMS version number\n");
SET_AX( context, 0x0200 ); /* 2.0 */
SET_BX( context, 0x0000 ); /* internal revision */
SET_DX( context, 0x0001 ); /* HMA exists */
break;
case 0x08: /* Query Free Extended Memory */
{
MEMORYSTATUS status;
TRACE("query free extended memory\n");
GlobalMemoryStatus( &status );
SET_DX( context, status.dwAvailVirtual >> 10 );
SET_AX( context, status.dwAvailVirtual >> 10 );
TRACE("returning largest %dK, total %dK\n", AX_reg(context), DX_reg(context));
}
break;
case 0x09: /* Allocate Extended Memory Block */
TRACE("allocate extended memory block (%dK)\n",
DX_reg(context));
SET_DX( context, GlobalAlloc16(GMEM_MOVEABLE, (DWORD)DX_reg(context)<<10) );
SET_AX( context, DX_reg(context) ? 1 : 0 );
if (!DX_reg(context)) SET_BL( context, 0xA0 ); /* out of memory */
break;
case 0x0a: /* Free Extended Memory Block */
TRACE("free extended memory block %04x\n",DX_reg(context));
if(!DX_reg(context) || GlobalFree16(DX_reg(context))) {
SET_AX( context, 0 ); /* failure */
SET_BL( context, 0xa2 ); /* invalid handle */
} else
SET_AX( context, 1 ); /* success */
break;
case 0x0b: /* Move Extended Memory Block */
{
MOVESTRUCT*move=CTX_SEG_OFF_TO_LIN(context,
context->SegDs,context->Esi);
BYTE*src,*dst;
TRACE("move extended memory block\n");
src=XMS_Offset(&move->Source);
dst=XMS_Offset(&move->Dest);
memcpy(dst,src,move->Length);
if (move->Source.Handle) GlobalUnlock16(move->Source.Handle);
if (move->Dest.Handle) GlobalUnlock16(move->Dest.Handle);
break;
}
case 0x88: /* Query Any Free Extended Memory */
{
MEMORYSTATUS status;
SYSTEM_INFO info;
TRACE("query any free extended memory\n");
GlobalMemoryStatus( &status );
GetSystemInfo( &info );
context->Eax = status.dwAvailVirtual >> 10;
context->Edx = status.dwAvailVirtual >> 10;
context->Ecx = (DWORD)info.lpMaximumApplicationAddress;
SET_BL( context, 0 ); /* No errors. */
TRACE("returning largest %dK, total %dK, highest 0x%x\n",
context->Eax, context->Edx, context->Ecx);
}
break;
default:
INT_BARF( context, 0x31 );
SET_AX( context, 0x0000 ); /* failure */
SET_BL( context, 0x80 ); /* function not implemented */
break;
}
}
/**********************************************************************
* DOSVM_CheckWrappers
*
* Check if this was really a wrapper call instead of an interrupt.
*/
BOOL DOSVM_CheckWrappers( CONTEXT *context )
{
if (context->SegCs==DOSVM_dpmi_segments->dpmi_seg) {
/* This is the protected mode switch */
StartPM(context);
return TRUE;
}
else if (context->SegCs==DOSVM_dpmi_segments->xms_seg)
{
/* This is the XMS driver entry point */
XMS_Handler(context);
return TRUE;
}
else
{
/* Check for RMCB */
RMCB *CurrRMCB = FirstRMCB;
while (CurrRMCB && (HIWORD(CurrRMCB->address) != context->SegCs))
CurrRMCB = CurrRMCB->next;
if (CurrRMCB) {
/* RMCB call, propagate to protected-mode handler */
DPMI_CallRMCBProc(context, CurrRMCB, dpmi_flag);
return TRUE;
}
}
return FALSE;
}
/**********************************************************************
* DOSVM_Int31Handler
*
* Handler for int 31h (DPMI).
*/
void WINAPI DOSVM_Int31Handler( CONTEXT *context )
{
RESET_CFLAG(context);
switch(AX_reg(context))
{
case 0x0000: /* Allocate LDT descriptors */
TRACE( "allocate LDT descriptors (%d)\n", CX_reg(context) );
{
WORD sel = AllocSelectorArray16( CX_reg(context) );
if(!sel)
{
TRACE( "failed\n" );
SET_AX( context, 0x8011 ); /* descriptor unavailable */
SET_CFLAG( context );
}
else
{
TRACE( "success, array starts at 0x%04x\n", sel );
SET_AX( context, sel );
}
}
break;
case 0x0001: /* Free LDT descriptor */
TRACE( "free LDT descriptor (0x%04x)\n", BX_reg(context) );
if (FreeSelector16( BX_reg(context) ))
{
SET_AX( context, 0x8022 ); /* invalid selector */
SET_CFLAG( context );
}
else
{
/* If a segment register contains the selector being freed, */
/* set it to zero. */
if (!((context->SegDs^BX_reg(context)) & ~3)) context->SegDs = 0;
if (!((context->SegEs^BX_reg(context)) & ~3)) context->SegEs = 0;
if (!((context->SegFs^BX_reg(context)) & ~3)) context->SegFs = 0;
if (!((context->SegGs^BX_reg(context)) & ~3)) context->SegGs = 0;
}
break;